Laurence Lundblade | d4cd723 | 2020-07-03 19:30:48 -0700 | [diff] [blame] | 1 | /*============================================================================== |
| 2 | example.c -- Example code for QCBOR |
| 3 | |
| 4 | Copyright (c) 2020, Laurence Lundblade. All rights reserved. |
| 5 | |
| 6 | SPDX-License-Identifier: BSD-3-Clause |
| 7 | |
| 8 | See BSD-3-Clause license in README.md |
| 9 | |
| 10 | Created on 6/30/2020 |
| 11 | =============================================================================*/ |
| 12 | |
| 13 | |
| 14 | #include <stdio.h> |
| 15 | #include "example.h" |
| 16 | #include "qcbor/qcbor_encode.h" |
| 17 | #include "qcbor/qcbor_decode.h" |
| 18 | |
| 19 | #define MAX_CYLINDERS 16 |
| 20 | |
| 21 | typedef struct |
| 22 | { |
| 23 | UsefulBufC Manufacturer; |
Laurence Lundblade | 06c8304 | 2020-07-03 23:04:53 -0700 | [diff] [blame] | 24 | int64_t uNumCylinders; |
| 25 | int64_t uDisplacement; |
| 26 | int64_t uHorsePower; |
Laurence Lundblade | e6bbf55 | 2020-07-05 22:57:57 -0700 | [diff] [blame^] | 27 | double dDesignedCompresion; |
Laurence Lundblade | d4cd723 | 2020-07-03 19:30:48 -0700 | [diff] [blame] | 28 | struct { |
| 29 | double uMeasuredCompression; |
| 30 | } cylinders[MAX_CYLINDERS]; |
| 31 | bool bTurboCharged; |
| 32 | } Engine; |
| 33 | |
| 34 | |
| 35 | void EngineInit(Engine *pE) |
| 36 | { |
| 37 | pE->uNumCylinders = 6; |
| 38 | pE->bTurboCharged = false; |
| 39 | pE->Manufacturer = UsefulBuf_FROM_SZ_LITERAL("Porsche"); |
| 40 | pE->uDisplacement = 3296; |
| 41 | pE->uHorsePower = 210; |
Laurence Lundblade | e6bbf55 | 2020-07-05 22:57:57 -0700 | [diff] [blame^] | 42 | pE->dDesignedCompresion = 9.1; |
Laurence Lundblade | d4cd723 | 2020-07-03 19:30:48 -0700 | [diff] [blame] | 43 | pE->cylinders[0].uMeasuredCompression = 9.0; |
| 44 | pE->cylinders[1].uMeasuredCompression = 9.2; |
| 45 | pE->cylinders[2].uMeasuredCompression = 8.9; |
| 46 | pE->cylinders[3].uMeasuredCompression = 8.9; |
| 47 | pE->cylinders[4].uMeasuredCompression = 9.1; |
| 48 | pE->cylinders[5].uMeasuredCompression = 9.0; |
| 49 | } |
| 50 | |
| 51 | |
Laurence Lundblade | e6bbf55 | 2020-07-05 22:57:57 -0700 | [diff] [blame^] | 52 | bool EngineCompare(Engine *pE1, Engine *pE2) |
| 53 | { |
| 54 | if(pE1->uNumCylinders != pE2->uNumCylinders) { |
| 55 | return false; |
| 56 | } |
| 57 | if(pE1->bTurboCharged != pE2->bTurboCharged) { |
| 58 | return false; |
| 59 | } |
| 60 | if(pE1->uDisplacement != pE2->uDisplacement) { |
| 61 | return false; |
| 62 | } |
| 63 | if(pE1->uHorsePower != pE2->uHorsePower) { |
| 64 | return false; |
| 65 | } |
| 66 | if(pE1->dDesignedCompresion != pE2->dDesignedCompresion) { |
| 67 | return false; |
| 68 | } |
| 69 | for(int64_t i = 0; i < pE2->uNumCylinders; i++) { |
| 70 | if(pE1->cylinders[i].uMeasuredCompression != |
| 71 | pE2->cylinders[i].uMeasuredCompression) { |
| 72 | return false; |
| 73 | } |
| 74 | } |
| 75 | |
| 76 | if(UsefulBuf_Compare(pE1->Manufacturer, pE2->Manufacturer)) { |
| 77 | return false; |
| 78 | } |
| 79 | |
| 80 | return true; |
| 81 | } |
| 82 | |
| 83 | |
Laurence Lundblade | d4cd723 | 2020-07-03 19:30:48 -0700 | [diff] [blame] | 84 | UsefulBufC EncodeEngine(const Engine *pEngine, UsefulBuf Buffer) |
| 85 | { |
Laurence Lundblade | 06c8304 | 2020-07-03 23:04:53 -0700 | [diff] [blame] | 86 | /* Initialize th encoder with the buffer big enough to hold the expected output. |
| 87 | If it is too small, QCBOREncode_Finish() will return an error. */ |
Laurence Lundblade | d4cd723 | 2020-07-03 19:30:48 -0700 | [diff] [blame] | 88 | QCBOREncodeContext EncodeCtx; |
Laurence Lundblade | d4cd723 | 2020-07-03 19:30:48 -0700 | [diff] [blame] | 89 | QCBOREncode_Init(&EncodeCtx, Buffer); |
Laurence Lundblade | 06c8304 | 2020-07-03 23:04:53 -0700 | [diff] [blame] | 90 | |
| 91 | /* Proceed output all the items, letting the internal error |
| 92 | tracking do its work. */ |
Laurence Lundblade | d4cd723 | 2020-07-03 19:30:48 -0700 | [diff] [blame] | 93 | QCBOREncode_OpenMap(&EncodeCtx); |
| 94 | QCBOREncode_AddTextToMap(&EncodeCtx, "Manufacturer", pEngine->Manufacturer); |
Laurence Lundblade | 06c8304 | 2020-07-03 23:04:53 -0700 | [diff] [blame] | 95 | QCBOREncode_AddInt64ToMap(&EncodeCtx, "NumCylinders", pEngine->uNumCylinders); |
| 96 | QCBOREncode_AddInt64ToMap(&EncodeCtx, "Displacement", pEngine->uDisplacement); |
| 97 | QCBOREncode_AddInt64ToMap(&EncodeCtx, "Horsepower", pEngine->uHorsePower); |
Laurence Lundblade | e6bbf55 | 2020-07-05 22:57:57 -0700 | [diff] [blame^] | 98 | QCBOREncode_AddDoubleToMap(&EncodeCtx, "DesignedCompression", pEngine->dDesignedCompresion); |
Laurence Lundblade | d4cd723 | 2020-07-03 19:30:48 -0700 | [diff] [blame] | 99 | QCBOREncode_OpenArrayInMap(&EncodeCtx, "Cylinders"); |
Laurence Lundblade | 06c8304 | 2020-07-03 23:04:53 -0700 | [diff] [blame] | 100 | for(int64_t i = 0 ; i < pEngine->uNumCylinders; i++) { |
Laurence Lundblade | d4cd723 | 2020-07-03 19:30:48 -0700 | [diff] [blame] | 101 | QCBOREncode_AddDouble(&EncodeCtx, pEngine->cylinders[i].uMeasuredCompression); |
| 102 | } |
| 103 | QCBOREncode_CloseArray(&EncodeCtx); |
| 104 | QCBOREncode_AddBoolToMap(&EncodeCtx, "turbo", pEngine->bTurboCharged); |
| 105 | QCBOREncode_CloseMap(&EncodeCtx); |
| 106 | |
Laurence Lundblade | 06c8304 | 2020-07-03 23:04:53 -0700 | [diff] [blame] | 107 | /* Get the pointer and length of the encoded output. If there was |
| 108 | anny error it will be returned here. */ |
Laurence Lundblade | d4cd723 | 2020-07-03 19:30:48 -0700 | [diff] [blame] | 109 | UsefulBufC EncodedCBOR; |
| 110 | QCBORError uErr; |
| 111 | uErr = QCBOREncode_Finish(&EncodeCtx, &EncodedCBOR); |
| 112 | if(uErr != QCBOR_SUCCESS) { |
| 113 | return NULLUsefulBufC; |
| 114 | } else { |
| 115 | return EncodedCBOR; |
| 116 | } |
| 117 | } |
| 118 | |
| 119 | |
| 120 | UsefulBufC EncodeEngineIndefinteLen(const Engine *pEngine, UsefulBuf Buffer) |
| 121 | { |
| 122 | QCBOREncodeContext EncodeCtx; |
| 123 | |
| 124 | QCBOREncode_Init(&EncodeCtx, Buffer); |
| 125 | QCBOREncode_OpenMapIndefiniteLength(&EncodeCtx); |
| 126 | QCBOREncode_AddTextToMap(&EncodeCtx, "Manufacturer", pEngine->Manufacturer); |
Laurence Lundblade | 06c8304 | 2020-07-03 23:04:53 -0700 | [diff] [blame] | 127 | QCBOREncode_AddInt64ToMap(&EncodeCtx, "Displacement", pEngine->uDisplacement); |
| 128 | QCBOREncode_AddInt64ToMap(&EncodeCtx, "Horsepower", pEngine->uHorsePower); |
Laurence Lundblade | e6bbf55 | 2020-07-05 22:57:57 -0700 | [diff] [blame^] | 129 | QCBOREncode_AddDoubleToMap(&EncodeCtx, "DesignedCompression", pEngine->dDesignedCompresion); |
Laurence Lundblade | 06c8304 | 2020-07-03 23:04:53 -0700 | [diff] [blame] | 130 | QCBOREncode_AddInt64ToMap(&EncodeCtx, "NumCylinders", pEngine->uNumCylinders); |
Laurence Lundblade | d4cd723 | 2020-07-03 19:30:48 -0700 | [diff] [blame] | 131 | QCBOREncode_OpenArrayIndefiniteLengthInMap(&EncodeCtx, "Cylinders"); |
Laurence Lundblade | 06c8304 | 2020-07-03 23:04:53 -0700 | [diff] [blame] | 132 | for(int64_t i = 0 ; i < pEngine->uNumCylinders; i++) { |
Laurence Lundblade | d4cd723 | 2020-07-03 19:30:48 -0700 | [diff] [blame] | 133 | QCBOREncode_AddDouble(&EncodeCtx, pEngine->cylinders[i].uMeasuredCompression); |
| 134 | } |
| 135 | QCBOREncode_CloseArrayIndefiniteLength(&EncodeCtx); |
| 136 | QCBOREncode_AddBoolToMap(&EncodeCtx, "turbo", pEngine->bTurboCharged); |
| 137 | QCBOREncode_CloseMapIndefiniteLength(&EncodeCtx); |
| 138 | |
| 139 | UsefulBufC EncodedCBOR; |
| 140 | QCBORError uErr; |
| 141 | uErr = QCBOREncode_Finish(&EncodeCtx, &EncodedCBOR); |
| 142 | if(uErr != QCBOR_SUCCESS) { |
| 143 | return NULLUsefulBufC; |
| 144 | } else { |
| 145 | return EncodedCBOR; |
| 146 | } |
| 147 | } |
| 148 | |
| 149 | |
Laurence Lundblade | d4cd723 | 2020-07-03 19:30:48 -0700 | [diff] [blame] | 150 | |
Laurence Lundblade | 06c8304 | 2020-07-03 23:04:53 -0700 | [diff] [blame] | 151 | typedef enum { |
| 152 | EngineSuccess, |
| 153 | CBORNotWellFormed, |
| 154 | TooManyCylinders, |
| 155 | EngineProtocolerror, |
| 156 | WrongNumberOfCylinders |
| 157 | } EngineDecodeErrors; |
Laurence Lundblade | d4cd723 | 2020-07-03 19:30:48 -0700 | [diff] [blame] | 158 | |
Laurence Lundblade | d4cd723 | 2020-07-03 19:30:48 -0700 | [diff] [blame] | 159 | |
Laurence Lundblade | 06c8304 | 2020-07-03 23:04:53 -0700 | [diff] [blame] | 160 | EngineDecodeErrors ConvertError(QCBORError uErr) |
Laurence Lundblade | d4cd723 | 2020-07-03 19:30:48 -0700 | [diff] [blame] | 161 | { |
Laurence Lundblade | 06c8304 | 2020-07-03 23:04:53 -0700 | [diff] [blame] | 162 | EngineDecodeErrors uReturn; |
| 163 | |
| 164 | switch(uErr) |
| 165 | { |
| 166 | case QCBOR_SUCCESS: |
| 167 | uReturn = EngineSuccess; |
| 168 | break; |
| 169 | |
| 170 | case QCBOR_ERR_HIT_END: |
| 171 | uReturn = CBORNotWellFormed; |
| 172 | break; |
| 173 | |
| 174 | default: |
| 175 | uReturn = EngineProtocolerror; |
| 176 | break; |
| 177 | } |
| 178 | |
| 179 | return uReturn; |
| 180 | } |
| 181 | |
| 182 | |
| 183 | /* |
| 184 | Decode using the advanced decode features. This pulls in more |
| 185 | code from the QCBOR library, but is much simpler and |
| 186 | roughly mirrors the encoding implementation. |
| 187 | */ |
| 188 | EngineDecodeErrors DecodeEngine(UsefulBufC EncodedEngine, Engine *pE) |
| 189 | { |
| 190 | QCBORError uErr; |
Laurence Lundblade | d4cd723 | 2020-07-03 19:30:48 -0700 | [diff] [blame] | 191 | QCBORDecodeContext DecodeCtx; |
| 192 | |
| 193 | QCBORDecode_Init(&DecodeCtx, EncodedEngine, QCBOR_DECODE_MODE_NORMAL); |
| 194 | QCBORDecode_EnterMap(&DecodeCtx); |
| 195 | QCBORDecode_GetTextInMapSZ(&DecodeCtx, "Manufacturer", &(pE->Manufacturer)); |
Laurence Lundblade | 06c8304 | 2020-07-03 23:04:53 -0700 | [diff] [blame] | 196 | QCBORDecode_GetInt64InMapSZ(&DecodeCtx, "Displacement", &(pE->uDisplacement)); |
| 197 | QCBORDecode_GetInt64InMapSZ(&DecodeCtx, "Horsepower", &(pE->uHorsePower)); |
Laurence Lundblade | e6bbf55 | 2020-07-05 22:57:57 -0700 | [diff] [blame^] | 198 | QCBORDecode_GetDoubleInMapSZ(&DecodeCtx, "DesignedCompression", &(pE->dDesignedCompresion)); |
Laurence Lundblade | d4cd723 | 2020-07-03 19:30:48 -0700 | [diff] [blame] | 199 | QCBORDecode_GetBoolInMapSZ(&DecodeCtx, "turbo", &(pE->bTurboCharged)); |
| 200 | |
Laurence Lundblade | 06c8304 | 2020-07-03 23:04:53 -0700 | [diff] [blame] | 201 | QCBORDecode_GetInt64InMapSZ(&DecodeCtx, "NumCylinders", &(pE->uNumCylinders)); |
Laurence Lundblade | d4cd723 | 2020-07-03 19:30:48 -0700 | [diff] [blame] | 202 | |
| 203 | /* Must check error before referencing pE->uNumCylinders to be sure it |
| 204 | is valid. If any of the above errored, it won't be valid. */ |
Laurence Lundblade | 06c8304 | 2020-07-03 23:04:53 -0700 | [diff] [blame] | 205 | uErr = QCBORDecode_GetError(&DecodeCtx); |
| 206 | if(uErr != QCBOR_SUCCESS) { |
| 207 | goto Done; |
Laurence Lundblade | d4cd723 | 2020-07-03 19:30:48 -0700 | [diff] [blame] | 208 | } |
| 209 | |
| 210 | if(pE->uNumCylinders > MAX_CYLINDERS) { |
Laurence Lundblade | 06c8304 | 2020-07-03 23:04:53 -0700 | [diff] [blame] | 211 | return TooManyCylinders; |
Laurence Lundblade | d4cd723 | 2020-07-03 19:30:48 -0700 | [diff] [blame] | 212 | } |
| 213 | |
| 214 | QCBORDecode_EnterArrayFromMapSZ(&DecodeCtx, "Cylinders"); |
Laurence Lundblade | e6bbf55 | 2020-07-05 22:57:57 -0700 | [diff] [blame^] | 215 | for(int64_t i = 0; i < pE->uNumCylinders; i++) { |
Laurence Lundblade | d4cd723 | 2020-07-03 19:30:48 -0700 | [diff] [blame] | 216 | QCBORDecode_GetDouble(&DecodeCtx, &(pE->cylinders[i].uMeasuredCompression)); |
Laurence Lundblade | d4cd723 | 2020-07-03 19:30:48 -0700 | [diff] [blame] | 217 | } |
| 218 | QCBORDecode_ExitArray(&DecodeCtx); |
| 219 | QCBORDecode_ExitMap(&DecodeCtx); |
| 220 | |
Laurence Lundblade | 06c8304 | 2020-07-03 23:04:53 -0700 | [diff] [blame] | 221 | /* Catch the remainder of errors here */ |
| 222 | uErr = QCBORDecode_Finish(&DecodeCtx); |
Laurence Lundblade | d4cd723 | 2020-07-03 19:30:48 -0700 | [diff] [blame] | 223 | |
Laurence Lundblade | 06c8304 | 2020-07-03 23:04:53 -0700 | [diff] [blame] | 224 | Done: |
| 225 | return ConvertError(uErr); |
Laurence Lundblade | d4cd723 | 2020-07-03 19:30:48 -0700 | [diff] [blame] | 226 | } |
| 227 | |
Laurence Lundblade | 06c8304 | 2020-07-03 23:04:53 -0700 | [diff] [blame] | 228 | |
| 229 | |
Laurence Lundblade | e6bbf55 | 2020-07-05 22:57:57 -0700 | [diff] [blame^] | 230 | EngineDecodeErrors DecodeEngineXX(UsefulBufC EncodedEngine, Engine *pE) |
| 231 | { |
| 232 | QCBORError uErr; |
| 233 | QCBORDecodeContext DecodeCtx; |
| 234 | |
| 235 | QCBORDecode_Init(&DecodeCtx, EncodedEngine, QCBOR_DECODE_MODE_NORMAL); |
| 236 | QCBORDecode_EnterMap(&DecodeCtx); |
| 237 | |
| 238 | QCBORItem XX[7]; |
| 239 | XX[0].uLabelType = QCBOR_TYPE_TEXT_STRING; |
| 240 | XX[0].label.string = UsefulBuf_FROM_SZ_LITERAL("Manufacturer"); |
| 241 | XX[0].uDataType = QCBOR_TYPE_TEXT_STRING; |
| 242 | |
| 243 | XX[1].uLabelType = QCBOR_TYPE_TEXT_STRING; |
| 244 | XX[1].label.string = UsefulBuf_FROM_SZ_LITERAL("Displacement"); |
| 245 | XX[1].uDataType = QCBOR_TYPE_INT64; |
| 246 | |
| 247 | XX[2].uLabelType = QCBOR_TYPE_TEXT_STRING; |
| 248 | XX[2].label.string = UsefulBuf_FROM_SZ_LITERAL("Horsepower"); |
| 249 | XX[2].uDataType = QCBOR_TYPE_INT64; |
| 250 | |
| 251 | XX[3].uLabelType = QCBOR_TYPE_TEXT_STRING; |
| 252 | XX[3].label.string = UsefulBuf_FROM_SZ_LITERAL("DesignedCompression"); |
| 253 | XX[3].uDataType = QCBOR_TYPE_DOUBLE; |
| 254 | |
| 255 | XX[4].uLabelType = QCBOR_TYPE_TEXT_STRING; |
| 256 | XX[4].label.string = UsefulBuf_FROM_SZ_LITERAL("turbo"); |
| 257 | XX[4].uDataType = QCBOR_TYPE_ANY; |
| 258 | |
| 259 | XX[5].uLabelType = QCBOR_TYPE_TEXT_STRING; |
| 260 | XX[5].label.string = UsefulBuf_FROM_SZ_LITERAL("NumCylinders"); |
| 261 | XX[5].uDataType = QCBOR_TYPE_INT64; |
| 262 | |
| 263 | XX[6].uLabelType = QCBOR_TYPE_NONE; |
| 264 | |
| 265 | uErr = QCBORDecode_GetItemsInMap(&DecodeCtx, XX); |
| 266 | if(uErr != QCBOR_SUCCESS) { |
| 267 | goto Done; |
| 268 | } |
| 269 | |
| 270 | pE->Manufacturer = XX[0].val.string; |
| 271 | pE->uDisplacement = XX[1].val.int64; |
| 272 | pE->uHorsePower = XX[2].val.int64; |
| 273 | pE->dDesignedCompresion = XX[3].val.dfnum; |
| 274 | pE->uNumCylinders = XX[5].val.int64; |
| 275 | |
| 276 | if(XX[4].uDataType == QCBOR_TYPE_TRUE) { |
| 277 | pE->bTurboCharged = true; |
| 278 | } else if(XX[4].uDataType == QCBOR_TYPE_FALSE) { |
| 279 | pE->bTurboCharged = false; |
| 280 | } else { |
| 281 | return EngineProtocolerror; |
| 282 | } |
| 283 | |
| 284 | |
| 285 | /* Must check error before referencing pE->uNumCylinders to be sure it |
| 286 | is valid. If any of the above errored, it won't be valid. */ |
| 287 | uErr = QCBORDecode_GetError(&DecodeCtx); |
| 288 | if(uErr != QCBOR_SUCCESS) { |
| 289 | goto Done; |
| 290 | } |
| 291 | |
| 292 | if(pE->uNumCylinders > MAX_CYLINDERS) { |
| 293 | return TooManyCylinders; |
| 294 | } |
| 295 | |
| 296 | QCBORDecode_EnterArrayFromMapSZ(&DecodeCtx, "Cylinders"); |
| 297 | for(int64_t i = 0; i < pE->uNumCylinders; i++) { |
| 298 | QCBORDecode_GetDouble(&DecodeCtx, &(pE->cylinders[i].uMeasuredCompression)); |
| 299 | } |
| 300 | QCBORDecode_ExitArray(&DecodeCtx); |
| 301 | QCBORDecode_ExitMap(&DecodeCtx); |
| 302 | |
| 303 | /* Catch the remainder of errors here */ |
| 304 | uErr = QCBORDecode_Finish(&DecodeCtx); |
| 305 | |
| 306 | Done: |
| 307 | return ConvertError(uErr); |
| 308 | } |
| 309 | |
| 310 | |
| 311 | |
| 312 | |
Laurence Lundblade | 06c8304 | 2020-07-03 23:04:53 -0700 | [diff] [blame] | 313 | |
| 314 | /* |
| 315 | |
| 316 | - Match |
| 317 | - Error |
| 318 | - No match |
| 319 | |
| 320 | */ |
| 321 | |
Laurence Lundblade | d4cd723 | 2020-07-03 19:30:48 -0700 | [diff] [blame] | 322 | QCBORError CheckLabelAndType(const char *szLabel, uint8_t uQCBORType, QCBORItem *pItem) |
| 323 | { |
| 324 | if(pItem->uLabelType != QCBOR_TYPE_TEXT_STRING) { |
| 325 | return QCBOR_ERR_NOT_FOUND; |
| 326 | } |
| 327 | |
| 328 | UsefulBufC Label = UsefulBuf_FromSZ(szLabel); |
| 329 | |
Laurence Lundblade | 06c8304 | 2020-07-03 23:04:53 -0700 | [diff] [blame] | 330 | if(UsefulBuf_Compare(Label, pItem->label.string)) { |
Laurence Lundblade | d4cd723 | 2020-07-03 19:30:48 -0700 | [diff] [blame] | 331 | return QCBOR_ERR_NOT_FOUND; |
| 332 | } |
| 333 | |
Laurence Lundblade | 06c8304 | 2020-07-03 23:04:53 -0700 | [diff] [blame] | 334 | if(pItem->uDataType != uQCBORType && uQCBORType != QCBOR_TYPE_ANY) { |
Laurence Lundblade | d4cd723 | 2020-07-03 19:30:48 -0700 | [diff] [blame] | 335 | return QCBOR_ERR_UNEXPECTED_TYPE; |
| 336 | } |
| 337 | |
| 338 | return QCBOR_SUCCESS; |
| 339 | } |
| 340 | |
Laurence Lundblade | d4cd723 | 2020-07-03 19:30:48 -0700 | [diff] [blame] | 341 | |
Laurence Lundblade | 06c8304 | 2020-07-03 23:04:53 -0700 | [diff] [blame] | 342 | EngineDecodeErrors DecodeCylinders(QCBORDecodeContext *pDecodeCtx, |
| 343 | Engine *pE, |
| 344 | const QCBORItem *pItem) |
| 345 | { |
| 346 | int i = 0; |
| 347 | QCBORItem Item; |
| 348 | |
| 349 | /* Loop getting all the items in the array */ |
| 350 | do { |
| 351 | QCBORError uErr; |
| 352 | |
| 353 | uErr = QCBORDecode_GetNext(pDecodeCtx, &Item); |
| 354 | if(uErr != QCBOR_SUCCESS) { |
| 355 | return CBORNotWellFormed; |
| 356 | } |
| 357 | if(Item.uDataType != QCBOR_TYPE_DOUBLE) { |
| 358 | return CBORNotWellFormed; |
| 359 | } |
| 360 | |
| 361 | if(i < MAX_CYLINDERS) { |
| 362 | pE->cylinders[i].uMeasuredCompression = Item.val.dfnum; |
| 363 | i++; |
| 364 | } |
| 365 | |
| 366 | } while (Item.uNextNestLevel == pItem->uNextNestLevel); |
| 367 | |
| 368 | if(i != pE->uNumCylinders) { |
| 369 | return WrongNumberOfCylinders; |
| 370 | } else { |
| 371 | return EngineSuccess; |
| 372 | } |
Laurence Lundblade | d4cd723 | 2020-07-03 19:30:48 -0700 | [diff] [blame] | 373 | } |
| 374 | |
Laurence Lundblade | 06c8304 | 2020-07-03 23:04:53 -0700 | [diff] [blame] | 375 | |
| 376 | |
| 377 | EngineDecodeErrors DecodeEngineBasic(UsefulBufC EncodedEngine, Engine *pE) |
Laurence Lundblade | d4cd723 | 2020-07-03 19:30:48 -0700 | [diff] [blame] | 378 | { |
| 379 | QCBORDecodeContext DecodeCtx; |
| 380 | |
| 381 | QCBORDecode_Init(&DecodeCtx, EncodedEngine, 0);// TODO: fill in mode; |
| 382 | |
| 383 | QCBORItem Item; |
| 384 | QCBORError uErr; |
Laurence Lundblade | 06c8304 | 2020-07-03 23:04:53 -0700 | [diff] [blame] | 385 | EngineDecodeErrors uReturn; |
| 386 | |
Laurence Lundblade | d4cd723 | 2020-07-03 19:30:48 -0700 | [diff] [blame] | 387 | |
| 388 | uErr = QCBORDecode_GetNext(&DecodeCtx, &Item); |
| 389 | if(uErr != QCBOR_SUCCESS) { |
Laurence Lundblade | 06c8304 | 2020-07-03 23:04:53 -0700 | [diff] [blame] | 390 | uReturn = CBORNotWellFormed; |
Laurence Lundblade | d4cd723 | 2020-07-03 19:30:48 -0700 | [diff] [blame] | 391 | goto Done; |
| 392 | } |
| 393 | if(Item.uDataType != QCBOR_TYPE_MAP) { |
Laurence Lundblade | 06c8304 | 2020-07-03 23:04:53 -0700 | [diff] [blame] | 394 | uReturn = CBORNotWellFormed; |
Laurence Lundblade | d4cd723 | 2020-07-03 19:30:48 -0700 | [diff] [blame] | 395 | goto Done; |
| 396 | } |
| 397 | |
| 398 | while(1) { |
| 399 | uErr = QCBORDecode_GetNext(&DecodeCtx, &Item); |
| 400 | if(uErr != QCBOR_SUCCESS) { |
Laurence Lundblade | 06c8304 | 2020-07-03 23:04:53 -0700 | [diff] [blame] | 401 | if(uErr == QCBOR_ERR_NO_MORE_ITEMS) { |
| 402 | break; /* Non-error exit from the loop */ |
Laurence Lundblade | d4cd723 | 2020-07-03 19:30:48 -0700 | [diff] [blame] | 403 | } else { |
Laurence Lundblade | 06c8304 | 2020-07-03 23:04:53 -0700 | [diff] [blame] | 404 | uReturn = CBORNotWellFormed; |
| 405 | goto Done; |
Laurence Lundblade | d4cd723 | 2020-07-03 19:30:48 -0700 | [diff] [blame] | 406 | } |
Laurence Lundblade | 06c8304 | 2020-07-03 23:04:53 -0700 | [diff] [blame] | 407 | } |
Laurence Lundblade | d4cd723 | 2020-07-03 19:30:48 -0700 | [diff] [blame] | 408 | |
Laurence Lundblade | 06c8304 | 2020-07-03 23:04:53 -0700 | [diff] [blame] | 409 | uErr = CheckLabelAndType("Manufacturer", QCBOR_TYPE_TEXT_STRING, &Item); |
| 410 | if(uErr == QCBOR_SUCCESS) { |
| 411 | pE->Manufacturer = Item.val.string; |
| 412 | continue; |
| 413 | } else if(uErr != QCBOR_ERR_NOT_FOUND){ |
| 414 | /* Maunfacturer field missing or badly formed */ |
| 415 | return EngineProtocolerror; |
| 416 | } /* continue on and try for another match */ |
| 417 | |
| 418 | |
| 419 | |
| 420 | uErr = CheckLabelAndType("NumCylinders", QCBOR_TYPE_INT64, &Item); |
| 421 | if(uErr == QCBOR_SUCCESS) { |
| 422 | if(Item.val.int64 > MAX_CYLINDERS) { |
| 423 | return TooManyCylinders; |
| 424 | } else { |
| 425 | pE->uNumCylinders = (uint8_t)Item.val.int64; |
| 426 | continue; |
| 427 | } |
| 428 | } else if(uErr != QCBOR_ERR_NOT_FOUND){ |
| 429 | /* Maunfacturer field missing or badly formed */ |
| 430 | return EngineProtocolerror; |
| 431 | } |
| 432 | |
| 433 | uErr = CheckLabelAndType("Cylinders", QCBOR_TYPE_ARRAY, &Item); |
| 434 | if(uErr == QCBOR_SUCCESS) { |
Laurence Lundblade | d4cd723 | 2020-07-03 19:30:48 -0700 | [diff] [blame] | 435 | DecodeCylinders(&DecodeCtx, pE, &Item); |
Laurence Lundblade | 06c8304 | 2020-07-03 23:04:53 -0700 | [diff] [blame] | 436 | continue; |
| 437 | } else if(uErr != QCBOR_ERR_NOT_FOUND){ |
| 438 | return EngineProtocolerror; |
| 439 | } |
Laurence Lundblade | d4cd723 | 2020-07-03 19:30:48 -0700 | [diff] [blame] | 440 | |
Laurence Lundblade | 06c8304 | 2020-07-03 23:04:53 -0700 | [diff] [blame] | 441 | uErr = CheckLabelAndType("Displacement", QCBOR_TYPE_INT64, &Item); |
| 442 | if(uErr == QCBOR_SUCCESS) { |
| 443 | pE->uDisplacement = Item.val.int64; |
| 444 | continue; |
| 445 | } else if(uErr != QCBOR_ERR_NOT_FOUND){ |
| 446 | return EngineProtocolerror; |
| 447 | } |
| 448 | |
| 449 | uErr = CheckLabelAndType("Horsepower", QCBOR_TYPE_INT64, &Item); |
| 450 | if(uErr == QCBOR_SUCCESS) { |
| 451 | pE->uHorsePower = Item.val.int64; |
| 452 | continue; |
| 453 | } else if(uErr != QCBOR_ERR_NOT_FOUND){ |
| 454 | return EngineProtocolerror; |
| 455 | } |
| 456 | |
| 457 | uErr = CheckLabelAndType("DesignedCompression", QCBOR_TYPE_DOUBLE, &Item); |
| 458 | if(uErr == QCBOR_SUCCESS) { |
Laurence Lundblade | e6bbf55 | 2020-07-05 22:57:57 -0700 | [diff] [blame^] | 459 | pE->dDesignedCompresion = Item.val.dfnum; |
Laurence Lundblade | 06c8304 | 2020-07-03 23:04:53 -0700 | [diff] [blame] | 460 | continue; |
| 461 | } else if(uErr != QCBOR_ERR_NOT_FOUND){ |
| 462 | return EngineProtocolerror; |
| 463 | } |
| 464 | |
| 465 | uErr = CheckLabelAndType("turbo", QCBOR_TYPE_ANY, &Item); |
| 466 | if(uErr == QCBOR_SUCCESS) { |
| 467 | if(Item.uDataType == QCBOR_TYPE_TRUE) { |
| 468 | pE->bTurboCharged = true; |
| 469 | } else if(Item.uDataType == QCBOR_TYPE_FALSE) { |
| 470 | pE->bTurboCharged = false; |
| 471 | } else { |
| 472 | return EngineProtocolerror; |
| 473 | } |
| 474 | continue; |
| 475 | } else if(uErr != QCBOR_ERR_NOT_FOUND){ |
| 476 | return EngineProtocolerror; |
| 477 | } |
| 478 | |
| 479 | |
| 480 | /* Some label data item that is not known |
| 481 | (could just ignore extras data items) */ |
| 482 | return EngineProtocolerror; |
Laurence Lundblade | d4cd723 | 2020-07-03 19:30:48 -0700 | [diff] [blame] | 483 | } |
Laurence Lundblade | 06c8304 | 2020-07-03 23:04:53 -0700 | [diff] [blame] | 484 | uReturn = EngineSuccess; |
Laurence Lundblade | d4cd723 | 2020-07-03 19:30:48 -0700 | [diff] [blame] | 485 | |
| 486 | |
| 487 | Done: |
Laurence Lundblade | 06c8304 | 2020-07-03 23:04:53 -0700 | [diff] [blame] | 488 | return uReturn; |
Laurence Lundblade | d4cd723 | 2020-07-03 19:30:48 -0700 | [diff] [blame] | 489 | } |
| 490 | |
Laurence Lundblade | d4cd723 | 2020-07-03 19:30:48 -0700 | [diff] [blame] | 491 | |
| 492 | |
| 493 | |
| 494 | |
| 495 | void RunQCborExample() |
| 496 | { |
| 497 | Engine E, DecodedEngine; |
| 498 | MakeUsefulBufOnStack( EngineBuffer, 300); |
| 499 | UsefulBufC EncodedEngine; |
| 500 | |
| 501 | MakeUsefulBufOnStack( InDefEngineBuffer, 300); |
| 502 | UsefulBufC InDefEncodedEngine; |
| 503 | |
| 504 | EngineInit(&E); |
| 505 | |
| 506 | EncodedEngine = EncodeEngine(&E, EngineBuffer); |
| 507 | |
| 508 | printf("Engine Encoded in %zu bytes\n", EncodedEngine.len); |
| 509 | |
Laurence Lundblade | 06c8304 | 2020-07-03 23:04:53 -0700 | [diff] [blame] | 510 | int x = (int)DecodeEngine(EncodedEngine, &DecodedEngine); |
| 511 | printf("Engine Decode Result: %d\n", x); |
Laurence Lundblade | d4cd723 | 2020-07-03 19:30:48 -0700 | [diff] [blame] | 512 | |
| 513 | |
| 514 | InDefEncodedEngine = EncodeEngineIndefinteLen(&E, InDefEngineBuffer); |
| 515 | |
| 516 | printf("Indef Engine Encoded in %zu bytes\n", InDefEncodedEngine.len); |
| 517 | |
Laurence Lundblade | 06c8304 | 2020-07-03 23:04:53 -0700 | [diff] [blame] | 518 | x = (int)DecodeEngine(InDefEncodedEngine, &DecodedEngine); |
| 519 | printf("Indef Engine Decode Result: %d\n", x); |
| 520 | |
Laurence Lundblade | e6bbf55 | 2020-07-05 22:57:57 -0700 | [diff] [blame^] | 521 | if(!EngineCompare(&E, &DecodedEngine)) { |
| 522 | printf("decode comparison fail\n"); |
| 523 | } |
| 524 | |
Laurence Lundblade | 06c8304 | 2020-07-03 23:04:53 -0700 | [diff] [blame] | 525 | |
| 526 | x = (int)DecodeEngineBasic(EncodedEngine, &DecodedEngine); |
| 527 | printf("Engine Basic Decode Result: %d\n", x); |
Laurence Lundblade | e6bbf55 | 2020-07-05 22:57:57 -0700 | [diff] [blame^] | 528 | |
| 529 | if(!EngineCompare(&E, &DecodedEngine)) { |
| 530 | printf("decode comparison fail\n"); |
| 531 | } |
| 532 | |
| 533 | |
| 534 | x = (int)DecodeEngineBasic(InDefEncodedEngine, &DecodedEngine); |
| 535 | printf("Indef Engine Basic Decode Result: %d\n", x); |
| 536 | |
| 537 | if(!EngineCompare(&E, &DecodedEngine)) { |
| 538 | printf("indef decode comparison fail\n"); |
| 539 | } |
| 540 | |
| 541 | x = (int)DecodeEngineXX(EncodedEngine, &DecodedEngine); |
| 542 | printf("Efficient Engine Basic Decode Result: %d\n", x); |
| 543 | |
| 544 | if(!EngineCompare(&E, &DecodedEngine)) { |
| 545 | printf("effcieit decode comparison fail\n"); |
| 546 | } |
Laurence Lundblade | d4cd723 | 2020-07-03 19:30:48 -0700 | [diff] [blame] | 547 | } |