WarpTwin
Documentation for WarpTwin models and classes.
Loading...
Searching...
No Matches
JsonCdl.hpp
Go to the documentation of this file.
1/******************************************************************************
2* Copyright (c) ATTX INC 2026. All Rights Reserved.
3*
4* This software and associated documentation (the "Software") are the
5* proprietary and confidential information of ATTX INC. The Software is
6* furnished under a license agreement between ATTX and the user organization
7* and may be used or copied only in accordance with the terms of the agreement.
8* Refer to 'license/attx_license.adoc' for standard license terms.
9*
10* EXPORT CONTROL NOTICE: THIS SOFTWARE MAY INCLUDE CONTENT CONTROLLED UNDER THE
11* INTERNATIONAL TRAFFIC IN ARMS REGULATIONS (ITAR) OR THE EXPORT ADMINISTRATION
12* REGULATIONS (EAR99). No part of the Software may be used, reproduced, or
13* transmitted in any form or by any means, for any purpose, without the express
14* written permission of ATTX INC.
15******************************************************************************/
16#ifndef LOGGING_JSON_CDL_H
17#define LOGGING_JSON_CDL_H
18
19#include "types.h"
21#include "utils/json.h"
22#include "utils/stringutils.hpp"
24#include "dynamics/Quaternion.h"
25
26namespace clockwerk {
27
35
51template <uint32 NUM_CDLS, uint32 MAX_JSON_KEY_LEN, uint32 MAX_JSON_FIELD_LEN>
52class JsonCdl {
53public:
56
63 int16 registerCdl(const char* name, cdl_types_e cdl_type, uint8 cdl_size, void* data_ptr);
64
69 int16 parseCdlFile(const char* file_data, uint32 file_len);
70
71 // Counting statistics for CDLs
73 uint32 num_missing = 0;
74 uint32 num_errors = 0;
76
77protected:
79 struct _cdl_info_t {
80 char key[MAX_JSON_KEY_LEN];
82 uint8 size;
83 void* ptr;
84 };
85
92 int16 _loadSingleCdl(const _cdl_info_t* cdl_info_ptr, const char* file_data, jsmntok_t* json_ptr, uint8 num_tok);
93
94 // Internal tracking variables and arrays
95 uint32 _storage_index = 0;
96 _cdl_info_t _cdls[NUM_CDLS];
97};
98
99template <uint32 NUM_CDLS, uint32 MAX_JSON_KEY_LEN, uint32 MAX_JSON_FIELD_LEN>
100int16 JsonCdl<NUM_CDLS, MAX_JSON_KEY_LEN, MAX_JSON_FIELD_LEN>::registerCdl(const char* name, cdl_types_e cdl_type, uint8 cdl_size, void* data_ptr) {
101 // If we're full, return error
102 if(_storage_index == NUM_CDLS) {
104 return ERROR_QUEUE_FULL;
105 }
106 if(name == nullptr || data_ptr == nullptr) {
108 return ERROR_NULLPTR;
109 }
110 if(cdl_size <= 0) {
112 return ERROR_DIMENSIONS;
113 }
114
115 // Otherwise add CDL and return
116 strcpy(_cdls[_storage_index].key, name);
117 _cdls[_storage_index].type = cdl_type;
118 _cdls[_storage_index].size = cdl_size;
119 _cdls[_storage_index].ptr = data_ptr;
121
122 return NO_ERROR;
123}
124
125template <uint32 NUM_CDLS, uint32 MAX_JSON_KEY_LEN, uint32 MAX_JSON_FIELD_LEN>
127 // Verify we have valid file data
128 if(file_data == nullptr) {
129 return ERROR_NULLPTR;
130 }
131
132 // Init our json object and parse into tokens
133 jsmn_parser parser;
134 jsmntok_t tokens[2*NUM_CDLS + 1];
135 jsmn_init(&parser);
136 int32 num_tokens = jsmn_parse(&parser, file_data, file_len, tokens, 2*NUM_CDLS + 1);
137 if (num_tokens < 1) {
138 return ERROR_INVALID_RANGE;
139 }
140
141 // Now loop through CDL parameters and load
142 for(uint32 i = 0; i < _storage_index; i++) {
143 _loadSingleCdl(&_cdls[i], file_data, tokens, num_tokens);
144 }
145
146 return NO_ERROR;
147}
148
149template <uint32 NUM_CDLS, uint32 MAX_JSON_KEY_LEN, uint32 MAX_JSON_FIELD_LEN>
150int16 JsonCdl<NUM_CDLS, MAX_JSON_KEY_LEN, MAX_JSON_FIELD_LEN>::_loadSingleCdl(const _cdl_info_t* cdl_info_ptr, const char* file_data, jsmntok_t* json_ptr, uint8 num_tok) {
151 if(cdl_info_ptr == nullptr || file_data == nullptr || json_ptr == nullptr) {
152 return ERROR_NULLPTR;
153 }
154
155 // Loop through tokens starting from i=1 (index 0 is all json info) and stepping
156 // in increments of 2 because tokens are key/value/key/value/key/value/...
157 uint32 val_len = 0;
158 uint32 key_len;
159 jsmntok_t val;
160 char value[MAX_JSON_FIELD_LEN];
161 bool found = false;
162 for(int32 i = 1; i < num_tok; i+=2) {
163 if(json_ptr[i].type == JSMN_STRING) {
164 key_len = json_ptr[i].end - json_ptr[i].start;
165 const char* key = file_data + json_ptr[i].start;
166 val = json_ptr[i + 1];
167 val_len = val.end - val.start;
168 if(val_len > MAX_JSON_FIELD_LEN) {
169 return ERROR_DIMENSIONS;
170 }
171 snprintf(value, sizeof(value), "%.*s", val_len, file_data + val.start); // Note null terminator automatic
172
173 if(key_len == strlen(cdl_info_ptr->key) && strncmp(cdl_info_ptr->key, key, key_len) == 0) {
174 found = true;
175 if(cdl_info_ptr->type == cdl_types_e::INT32_TP) {
176 // Write as if we have an array no matter what -- doesn't matter
177 int32* write_ptr = static_cast<int32*>(cdl_info_ptr->ptr);
178 int16 err = clockwerk::setArrayFromString(value, write_ptr, cdl_info_ptr->size);
179 if(err > 0) {
180 num_errors++;
181 } else {
183 }
184 break;
185
186 } else if(cdl_info_ptr->type == cdl_types_e::FPOINT_TP) {
187 // Write as if we have an array no matter what -- doesn't matter
188 floating_point* write_ptr = static_cast<floating_point*>(cdl_info_ptr->ptr);
189 int16 err = clockwerk::setArrayFromString(value, write_ptr, cdl_info_ptr->size);
190 if(err > 0) {
191 num_errors++;
192 } else {
194 }
195 break;
196
197 } else if(cdl_info_ptr->type == cdl_types_e::CARTESIANVECTOR3_TP) {
198 clockwerk::CartesianVector<3>* converted_ptr = static_cast<clockwerk::CartesianVector<3>*>(cdl_info_ptr->ptr);
199
200 int16 err = converted_ptr->fromStr(value);
201 if(err) {
202 num_errors++;
203 } else {
205 }
206 break;
207
208 } else { // Must be quaternion
209 clockwerk::Quaternion* converted_ptr = static_cast<clockwerk::Quaternion*>(cdl_info_ptr->ptr);
210
211 int16 err = converted_ptr->fromStr(value);
212 if(err) {
213 num_errors++;
214 } else {
216 }
217 break;
218 }
219 }
220 }
221 }
222
223 if(!found) {
224 num_missing++;
225 return WARNING_NO_MATCH;
226 }
227
228 return NO_ERROR;
229}
230
231}
232
233#endif
Standard vector class derived from Matrix.
Definition CartesianVector.hpp:39
uint32 num_missing
Definition JsonCdl.hpp:73
uint32 num_errors
Definition JsonCdl.hpp:74
int16 parseCdlFile(const char *file_data, uint32 file_len)
Parse a CDL file given at file_data.
Definition JsonCdl.hpp:126
uint32 _storage_index
Definition JsonCdl.hpp:95
JsonCdl()
Definition JsonCdl.hpp:54
~JsonCdl()
Definition JsonCdl.hpp:55
int16 registerCdl(const char *name, cdl_types_e cdl_type, uint8 cdl_size, void *data_ptr)
Register a CDL to be loaded.
Definition JsonCdl.hpp:100
uint32 num_loaded_correct
Definition JsonCdl.hpp:72
_cdl_info_t _cdls[NUM_CDLS]
Storage array for CDLs.
Definition JsonCdl.hpp:96
int16 _loadSingleCdl(const _cdl_info_t *cdl_info_ptr, const char *file_data, jsmntok_t *json_ptr, uint8 num_tok)
Load a single CDL from the json file.
Definition JsonCdl.hpp:150
uint32 num_failed_registries
Definition JsonCdl.hpp:75
int16 fromStr(const char *val)
Set value of matrix from string in same format as str().
Definition Matrix.hpp:341
Quaternion class for attitude representation.
Definition Quaternion.h:68
#define NO_ERROR
Error code in the case where matrix math executed successfully.
Definition clockwerkerrors.h:34
#define ERROR_NULLPTR
Error code in case of a null pointer.
Definition clockwerkerrors.h:60
#define ERROR_DIMENSIONS
Definition clockwerkerrors.h:41
#define ERROR_QUEUE_FULL
Error in case the queue is full.
Definition clockwerkerrors.h:195
#define ERROR_INVALID_RANGE
Definition clockwerkerrors.h:53
#define WARNING_NO_MATCH
Error in the case where dynamics model overruns steps or gets out of whack.
Definition clockwerkerrors.h:185
void jsmn_init(jsmn_parser *parser)
Definition json.cpp:393
int jsmn_parse(jsmn_parser *parser, const char *js, const size_t len, jsmntok_t *tokens, const unsigned int num_tokens)
Definition json.cpp:202
@ JSMN_STRING
Definition json.h:63
struct jsmntok jsmntok_t
Definition CircularBuffer.hpp:28
cdl_types_e
Type definition for CDL types which may be loaded.
Definition JsonCdl.hpp:29
@ INT32_TP
Integer type – can be single value or array.
Definition JsonCdl.hpp:30
@ CARTESIANVECTOR3_TP
Cartesian vector type – is floating_point based.
Definition JsonCdl.hpp:32
@ QUATERNION_TP
Quaternion type – is floating_point based.
Definition JsonCdl.hpp:33
@ FPOINT_TP
floating_point type – can be single value or array
Definition JsonCdl.hpp:31
int16 setArrayFromString(const char *s, T *array, size_t array_size)
Converts a bracket-enclosed comma-separated string to a float array.
Definition stringutils.hpp:230
Hold all info associated with a single CDL.
Definition JsonCdl.hpp:79
void * ptr
Pointer to where to write CDL value.
Definition JsonCdl.hpp:83
char key[MAX_JSON_KEY_LEN]
Key for the CDL.
Definition JsonCdl.hpp:80
cdl_types_e type
Type of the CDL.
Definition JsonCdl.hpp:81
uint8 size
Size of the CDL.
Definition JsonCdl.hpp:82
Definition json.h:96
int end
Definition json.h:85
int start
Definition json.h:84