-
Notifications
You must be signed in to change notification settings - Fork 19
Expand file tree
/
Copy pathedslib_displaydb_stringconv.c
More file actions
568 lines (525 loc) · 20.2 KB
/
Copy pathedslib_displaydb_stringconv.c
File metadata and controls
568 lines (525 loc) · 20.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
/*
* LEW-19710-1, CCSDS SOIS Electronic Data Sheet Implementation
*
* Copyright (c) 2020 United States Government as represented by
* the Administrator of the National Aeronautics and Space Administration.
* All Rights Reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
/**
* \file edslib_displaydb_stringconv.c
* \ingroup fsw
* \author joseph.p.hickey@nasa.gov
*
* Utility functions to convert EDS data structure members to/from strings
* according to the display information in the EDS.
*
* Linked as part of the "full" EDS runtime library
*/
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <ctype.h>
#include <limits.h>
#include <stdint.h>
#include "edslib_internal.h"
/*----------------------------------------------------------------
*
* EdsLib local helper function
*
*----------------------------------------------------------------*/
static int32_t EdsLib_Internal_StringCompare_IgnoreCase(const char *String1,
const char *String2)
{
uint32_t Index = 0U;
int32_t Result = 0;
int32_t Char1 = 0;
int32_t Char2 = 0;
while (Result == 0)
{
if (String1[Index] == '\0' || String2[Index] == '\0')
{
Result = String1[Index] - String2[Index];
break;
}
Char1 = (int32_t)String1[Index];
Char2 = (int32_t)String2[Index];
/* Convert characters to lower case if they're alphabetical */
if (isalpha(Char1))
{
Char1 = tolower(Char1);
}
if (isalpha(Char2))
{
Char2 = tolower(Char2);
}
Result = Char1 - Char2;
++Index;
}
return Result;
}
/*----------------------------------------------------------------
*
* EdsLib internal function
* See description in header file for argument/return detail
*
*-----------------------------------------------------------------*/
const EdsLib_SymbolTableEntry_t *EdsLib_DisplaySymbolLookup_GetByName(const EdsLib_SymbolTableEntry_t *SymbolDict,
uint16_t TableSize,
const char *String,
uint32_t StringLen)
{
const EdsLib_SymbolTableEntry_t *Sym;
int Compare;
uint32_t LowIndex;
uint32_t HighIndex;
uint32_t SearchIndex;
Sym = NULL;
LowIndex = 0;
HighIndex = TableSize;
while (true)
{
if (LowIndex >= HighIndex)
{
Sym = NULL;
break;
}
SearchIndex = (LowIndex + HighIndex) / 2;
Sym = &SymbolDict[SearchIndex];
Compare = strncmp(Sym->SymName, String, StringLen);
if (Compare == 0 && Sym->SymName[StringLen] == 0)
{
break;
}
if (Compare < 0)
{
LowIndex = SearchIndex + 1;
}
else
{
HighIndex = SearchIndex;
}
}
return Sym;
}
/*----------------------------------------------------------------
*
* EdsLib internal function
* See description in header file for argument/return detail
*
*-----------------------------------------------------------------*/
const EdsLib_SymbolTableEntry_t *
EdsLib_DisplaySymbolLookup_GetByValue(const EdsLib_SymbolTableEntry_t *SymbolDict, uint16_t TableSize, intmax_t Value)
{
const EdsLib_SymbolTableEntry_t *Result;
Result = SymbolDict;
/*
* The list is ordered by name, not by value, so we need to
* do a less efficient sequential search to find the value.
*/
while (1)
{
if (TableSize == 0)
{
Result = NULL;
break;
}
if (Result->SymValue == Value)
{
break;
}
--TableSize;
++Result;
}
return Result;
}
/*----------------------------------------------------------------
*
* EdsLib internal function
* See description in header file for argument/return detail
*
*-----------------------------------------------------------------*/
int32_t EdsLib_DisplayScalarConv_ToString_Impl(const EdsLib_DataTypeDB_Entry_t *DictEntryPtr,
const EdsLib_DisplayDB_Entry_t *DisplayInfoPtr,
char *OutputBuffer,
uint32_t BufferSize,
const void *SourcePtr)
{
EdsLib_GenericValueBuffer_t NumberBuffer;
EdsLib_ConstPtr_t Src;
int32_t Status;
Src.Ptr = SourcePtr;
NumberBuffer.ValueType = EDSLIB_BASICTYPE_NONE;
/*
* Do not waste effort trying to convert something that is
* not directly representable by a string
*/
if (DictEntryPtr == NULL)
{
Status = EDSLIB_INVALID_SIZE_OR_TYPE;
}
else if (DictEntryPtr->BasicType == EDSLIB_BASICTYPE_CONTAINER)
{
snprintf(OutputBuffer, BufferSize, "<ContainerDataType>");
Status = EDSLIB_SUCCESS;
}
else if (DictEntryPtr->BasicType == EDSLIB_BASICTYPE_COMPONENT)
{
snprintf(OutputBuffer, BufferSize, "<Component>");
Status = EDSLIB_SUCCESS;
}
else if (DictEntryPtr->BasicType == EDSLIB_BASICTYPE_ARRAY)
{
snprintf(OutputBuffer, BufferSize, "<ArrayDataType>");
Status = EDSLIB_SUCCESS;
}
else if (DictEntryPtr->BasicType == EDSLIB_BASICTYPE_GENERIC)
{
snprintf(OutputBuffer, BufferSize, "<GenericType>");
Status = EDSLIB_SUCCESS;
}
else
{
Status = EDSLIB_NOT_IMPLEMENTED;
EdsLib_DataTypeLoad_Impl(&NumberBuffer, Src, DictEntryPtr);
/*
* First preference:
* Get the display hint from the DB, and use it if available
*/
if (DisplayInfoPtr != NULL)
{
switch (DisplayInfoPtr->DisplayHint)
{
case EDSLIB_DISPLAYHINT_STRING:
{
uint16_t SrcSize = DictEntryPtr->SizeInfo.Bytes;
/*
* binary blob contains character string data.
* copy only PRINTABLE chars to output buffer,
* as this is intended for UI display
*/
while (SrcSize > 0 && BufferSize > 1 && Src.u->u8 != 0)
{
if (isprint((int)Src.u->u8))
{
*OutputBuffer = Src.u->u8;
}
else
{
*OutputBuffer = '.';
}
++OutputBuffer;
--BufferSize;
++Src.Addr;
--SrcSize;
}
if (BufferSize > 0)
{
*OutputBuffer = 0;
}
Status = EDSLIB_SUCCESS;
break;
}
case EDSLIB_DISPLAYHINT_BASE64:
{
/*
* binary blob contains binary data, which cannot be
* put into a normal string. Use Base64 encoding.
*/
EdsLib_DisplayDB_Base64Encode(OutputBuffer, BufferSize, Src.Addr, DictEntryPtr->SizeInfo.Bits);
Status = EDSLIB_SUCCESS;
break;
}
case EDSLIB_DISPLAYHINT_ENUM_SYMTABLE:
{
const EdsLib_SymbolTableEntry_t *Symbol;
if (NumberBuffer.ValueType == EDSLIB_BASICTYPE_SIGNED_INT)
{
Symbol = EdsLib_DisplaySymbolLookup_GetByValue(DisplayInfoPtr->DisplayArg.SymTable,
DisplayInfoPtr->DisplayArgTableSize,
NumberBuffer.Value.SignedInteger);
}
else if (NumberBuffer.ValueType == EDSLIB_BASICTYPE_UNSIGNED_INT)
{
Symbol = EdsLib_DisplaySymbolLookup_GetByValue(DisplayInfoPtr->DisplayArg.SymTable,
DisplayInfoPtr->DisplayArgTableSize,
NumberBuffer.Value.UnsignedInteger);
}
else
{
Symbol = NULL;
}
if (Symbol != NULL)
{
snprintf(OutputBuffer, BufferSize, "%s", Symbol->SymName);
Status = EDSLIB_SUCCESS;
}
break;
}
case EDSLIB_DISPLAYHINT_ADDRESS:
{
/* memory addresses are always hex values */
if (NumberBuffer.ValueType == EDSLIB_BASICTYPE_SIGNED_INT
|| NumberBuffer.ValueType == EDSLIB_BASICTYPE_UNSIGNED_INT)
{
snprintf(OutputBuffer,
BufferSize,
"0x%0*llx",
(int)(DictEntryPtr->SizeInfo.Bytes * 2),
NumberBuffer.Value.UnsignedInteger);
Status = EDSLIB_SUCCESS;
}
break;
}
case EDSLIB_DISPLAYHINT_BOOLEAN:
{
/* memory addresses are always hex values */
if ((NumberBuffer.ValueType == EDSLIB_BASICTYPE_UNSIGNED_INT
&& NumberBuffer.Value.UnsignedInteger != 0)
|| (NumberBuffer.ValueType == EDSLIB_BASICTYPE_SIGNED_INT
&& NumberBuffer.Value.SignedInteger != 0))
{
snprintf(OutputBuffer, BufferSize, "true");
}
else
{
snprintf(OutputBuffer, BufferSize, "false");
}
Status = EDSLIB_SUCCESS;
break;
}
default:
break;
}
}
}
/*
* If not converted, then use a fallback/default conversion
*/
if (Status != EDSLIB_SUCCESS)
{
switch (NumberBuffer.ValueType)
{
case EDSLIB_BASICTYPE_UNSIGNED_INT:
snprintf(OutputBuffer, BufferSize, "%llu", NumberBuffer.Value.UnsignedInteger);
Status = EDSLIB_SUCCESS;
break;
case EDSLIB_BASICTYPE_SIGNED_INT:
snprintf(OutputBuffer, BufferSize, "%lld", NumberBuffer.Value.SignedInteger);
Status = EDSLIB_SUCCESS;
break;
case EDSLIB_BASICTYPE_FLOAT:
snprintf(OutputBuffer, BufferSize, "%.4Lg", NumberBuffer.Value.FloatingPoint);
Status = EDSLIB_SUCCESS;
break;
default:
/*
* catch-all case, to ensure something is written to the buffer.
* If this is seen in the output it likely means a DB is missing.
*/
snprintf(OutputBuffer, BufferSize, "<\?\?\?>");
break;
}
}
return Status;
}
/*----------------------------------------------------------------
*
* EdsLib internal function
* See description in header file for argument/return detail
*
*-----------------------------------------------------------------*/
int32_t EdsLib_DisplayScalarConv_FromString_Impl(const EdsLib_DataTypeDB_Entry_t *DictEntryPtr,
const EdsLib_DisplayDB_Entry_t *DisplayInfoPtr,
void *DestPtr,
const char *SrcString)
{
EdsLib_GenericValueBuffer_t NumberBuffer;
int32_t Status;
EdsLib_Ptr_t Dst;
Dst.Ptr = DestPtr;
NumberBuffer.ValueType = EDSLIB_BASICTYPE_NONE;
if (DictEntryPtr == NULL)
{
Status = EDSLIB_INVALID_SIZE_OR_TYPE;
}
else if (DictEntryPtr->NumSubElements > 0)
{
/* Cannot convert compound values using this routine */
Status = EDSLIB_NOT_IMPLEMENTED;
}
else
{
Status = EDSLIB_FAILURE;
/*
* Get the display hint from the DB, and use it if available
*/
if (DisplayInfoPtr != NULL)
{
switch (DisplayInfoPtr->DisplayHint)
{
case EDSLIB_DISPLAYHINT_STRING:
{
uint16_t DstSize = DictEntryPtr->SizeInfo.Bytes;
while (DstSize > 0)
{
Dst.u->u8 = *SrcString;
if (*SrcString != 0)
{
++SrcString;
}
++Dst.Addr;
--DstSize;
}
Status = EDSLIB_SUCCESS;
break;
}
case EDSLIB_DISPLAYHINT_BASE64:
{
/*
* binary blob contains binary data, which cannot be
* put into a normal string. Use Base64 encoding.
*/
EdsLib_DisplayDB_Base64Decode(Dst.Addr, DictEntryPtr->SizeInfo.Bits, SrcString);
Status = EDSLIB_SUCCESS;
break;
}
case EDSLIB_DISPLAYHINT_ENUM_SYMTABLE:
{
const EdsLib_SymbolTableEntry_t *Symbol;
Symbol = EdsLib_DisplaySymbolLookup_GetByName(DisplayInfoPtr->DisplayArg.SymTable,
DisplayInfoPtr->DisplayArgTableSize,
SrcString,
strlen(SrcString));
if (Symbol != NULL)
{
NumberBuffer.Value.SignedInteger = Symbol->SymValue;
NumberBuffer.ValueType = EDSLIB_BASICTYPE_SIGNED_INT;
}
break;
}
case EDSLIB_DISPLAYHINT_ADDRESS:
{
/* memory addresses are always hex values */
const char *EndPtr;
NumberBuffer.Value.UnsignedInteger = strtoull(SrcString, (char **)&EndPtr, 16);
if (EndPtr != SrcString && *EndPtr == 0)
{
NumberBuffer.ValueType = EDSLIB_BASICTYPE_UNSIGNED_INT;
}
break;
}
case EDSLIB_DISPLAYHINT_BOOLEAN:
{
/* typically TRUE/FALSE, also accept YES/NO or 0/1 */
const char *EndPtr = SrcString;
if (EdsLib_Internal_StringCompare_IgnoreCase(SrcString, "true") == 0)
{
EndPtr += 4;
NumberBuffer.Value.UnsignedInteger = 1;
}
else if (EdsLib_Internal_StringCompare_IgnoreCase(SrcString, "false") == 0)
{
EndPtr += 4;
NumberBuffer.Value.UnsignedInteger = 0;
}
else if (EdsLib_Internal_StringCompare_IgnoreCase(SrcString, "yes") == 0)
{
EndPtr += 3;
NumberBuffer.Value.UnsignedInteger = 1;
}
else if (EdsLib_Internal_StringCompare_IgnoreCase(SrcString, "no") == 0)
{
EndPtr += 2;
NumberBuffer.Value.UnsignedInteger = 0;
}
else
{
NumberBuffer.Value.UnsignedInteger = strtol(SrcString, (char **)&EndPtr, 0);
}
if (EndPtr != SrcString && *EndPtr == 0)
{
NumberBuffer.ValueType = EDSLIB_BASICTYPE_UNSIGNED_INT;
}
break;
}
default:
break;
}
}
if (Status != EDSLIB_SUCCESS && NumberBuffer.ValueType == EDSLIB_BASICTYPE_NONE)
{
const char *EndPtr;
switch (DictEntryPtr->BasicType)
{
case EDSLIB_BASICTYPE_UNSIGNED_INT:
case EDSLIB_BASICTYPE_SIGNED_INT:
case EDSLIB_BASICTYPE_FLOAT:
{
/*
* NOTE: conversion is done into the system long double type --
* This is because we do not have control over how the input data is
* formatted. For instance, if the target type is a signed integer and
* supplied string is "-5.0" or "1e3", these should still work.
* Using a long double here should provide for a 64-bit mantissa, so
* even 64 bit integer values can be stored without precision loss
*/
NumberBuffer.Value.FloatingPoint = strtold(SrcString, (char **)&EndPtr);
if (EndPtr != SrcString && *EndPtr == 0)
{
NumberBuffer.ValueType = EDSLIB_BASICTYPE_FLOAT;
}
break;
}
case EDSLIB_BASICTYPE_BINARY:
{
uint16_t DstSize = DictEntryPtr->SizeInfo.Bytes;
size_t SrcSize = strlen(SrcString);
Status = EDSLIB_SUCCESS;
while (DstSize > 0 && SrcSize >= 2)
{
NumberBuffer.Value.StringData[0] = *SrcString;
++SrcString;
NumberBuffer.Value.StringData[1] = *SrcString;
++SrcString;
NumberBuffer.Value.StringData[2] = 0;
SrcSize -= 2;
Dst.u->u8 = strtoul(NumberBuffer.Value.StringData, (char **)&EndPtr, 16);
if (*EndPtr != 0)
{
Status = EDSLIB_FAILURE;
break;
}
++Dst.Addr;
--DstSize;
}
break;
}
default:
{
break;
}
}
}
if (Status != EDSLIB_SUCCESS)
{
EdsLib_DataTypeStore_Impl(Dst, &NumberBuffer, DictEntryPtr);
if (NumberBuffer.ValueType == DictEntryPtr->BasicType)
{
Status = EDSLIB_SUCCESS;
}
}
}
return Status;
}