1 //******************************************************************
3 // Copyright 2015 Intel Mobile Communications GmbH All Rights Reserved.
5 //-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
7 // Licensed under the Apache License, Version 2.0 (the "License");
8 // you may not use this file except in compliance with the License.
9 // You may obtain a copy of the License at
11 // http://www.apache.org/licenses/LICENSE-2.0
13 // Unless required by applicable law or agreed to in writing, software
14 // distributed under the License is distributed on an "AS IS" BASIS,
15 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 // See the License for the specific language governing permissions and
17 // limitations under the License.
19 //-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
21 #include "ocpayloadcbor.h"
22 #include "platform_features.h"
24 #include "oic_malloc.h"
26 #include "ocpayload.h"
28 #include "ocresourcehandler.h"
31 #define TAG "OCPayloadConvert"
32 // Arbitrarily chosen size that seems to contain the majority of packages
33 #define INIT_SIZE (255)
35 // Functions all return either a CborError, or a negative version of the OC_STACK return values
36 static int64_t OCConvertPayloadHelper(OCPayload* payload, uint8_t* outPayload, size_t* size);
37 static int64_t OCConvertDiscoveryPayload(OCDiscoveryPayload* payload, uint8_t* outPayload,
39 static int64_t OCConvertDevicePayload(OCDevicePayload* payload, uint8_t* outPayload,
41 static int64_t OCConvertPlatformPayload(OCPlatformPayload* payload, uint8_t* outPayload,
43 static int64_t OCConvertRepPayload(OCRepPayload* payload, uint8_t* outPayload, size_t* size);
44 static int64_t OCConvertPresencePayload(OCPresencePayload* payload, uint8_t* outPayload,
46 static int64_t OCConvertSecurityPayload(OCSecurityPayload* payload, uint8_t* outPayload,
48 static int64_t OCConvertSingleRepPayload(CborEncoder* parent, const OCRepPayload* payload);
49 static int64_t OCConvertArray(CborEncoder* parent, const OCRepPayloadValueArray* valArray);
51 static int64_t AddTextStringToMap(CborEncoder* map, const char* key, size_t keylen,
54 static int64_t ConditionalAddTextStringToMap(CborEncoder* map, const char* key, size_t keylen,
57 #define STRINGIFY(s) XSTRINGIFY(s)
58 #define XSTRINGIFY(s) #s
60 OCStackResult OCConvertPayload(OCPayload* payload, uint8_t** outPayload, size_t* size)
62 // TinyCbor Version 47a78569c0 or better on master is required for the re-allocation
63 // strategy to work. If you receive the following assertion error, please do a git-pull
64 // from the extlibs/tinycbor/tinycbor directory
65 #define CborNeedsUpdating (CborErrorOutOfMemory < CborErrorDataTooLarge)
66 OC_STATIC_ASSERT(!CborNeedsUpdating, "tinycbor needs to be updated to at least 47a78569c0");
67 #undef CborNeedsUpdating
70 OC_LOG(ERROR, TAG, "Payload parameter NULL");
71 return OC_STACK_INVALID_PARAM;
74 if (!outPayload || !size)
76 OC_LOG(ERROR, TAG, "Out parameter/s parameter NULL");
77 return OC_STACK_INVALID_PARAM;
80 OC_LOG_V(INFO, TAG, "Converting payload of type %d", payload->type);
82 size_t curSize = INIT_SIZE;
83 uint8_t* out = (uint8_t*)OICCalloc(1, curSize);
84 int64_t err = OCConvertPayloadHelper(payload, out, &curSize);
86 if (err == CborErrorOutOfMemory)
88 // reallocate "out" and try again!
89 uint8_t* out2 = (uint8_t*)OICRealloc(out, curSize);
94 return OC_STACK_NO_MEMORY;
98 err = OCConvertPayloadHelper(payload, out, &curSize);
103 if (curSize < INIT_SIZE)
105 uint8_t* out2 = (uint8_t*)OICRealloc(out, curSize);
110 return OC_STACK_NO_MEMORY;
122 return (OCStackResult)-err;
126 return OC_STACK_ERROR;
130 static int64_t OCConvertPayloadHelper(OCPayload* payload, uint8_t* outPayload, size_t* size)
132 switch(payload->type)
134 case PAYLOAD_TYPE_DISCOVERY:
135 return OCConvertDiscoveryPayload((OCDiscoveryPayload*)payload, outPayload, size);
136 case PAYLOAD_TYPE_DEVICE:
137 return OCConvertDevicePayload((OCDevicePayload*)payload, outPayload, size);
138 case PAYLOAD_TYPE_PLATFORM:
139 return OCConvertPlatformPayload((OCPlatformPayload*)payload, outPayload, size);
140 case PAYLOAD_TYPE_REPRESENTATION:
141 return OCConvertRepPayload((OCRepPayload*)payload, outPayload, size);
142 case PAYLOAD_TYPE_PRESENCE:
143 return OCConvertPresencePayload((OCPresencePayload*)payload, outPayload, size);
144 case PAYLOAD_TYPE_SECURITY:
145 return OCConvertSecurityPayload((OCSecurityPayload*)payload, outPayload, size);
147 OC_LOG_V(INFO,TAG, "ConvertPayload default %d", payload->type);
148 return OC_STACK_NOTIMPL;
152 static int64_t checkError(int64_t err, CborEncoder* encoder, uint8_t* outPayload, size_t* size)
154 if (err == CborErrorOutOfMemory)
156 *size += encoder->ptr - encoder->end;
161 OC_LOG_V(ERROR, TAG, "Convert Payload failed", err);
166 *size = encoder->ptr - outPayload;
170 static int64_t OCConvertSecurityPayload(OCSecurityPayload* payload, uint8_t* outPayload,
176 cbor_encoder_init(&encoder, outPayload, *size, 0);
178 CborEncoder rootArray;
179 err = err | cbor_encoder_create_array(&encoder, &rootArray, 1);
182 err = err | cbor_encoder_create_map(&rootArray, &map, CborIndefiniteLength);
184 if(payload->securityData)
186 err = err | AddTextStringToMap(&map, OC_RSRVD_REPRESENTATION,
187 sizeof(OC_RSRVD_REPRESENTATION) - 1,
188 payload->securityData);
191 err = err | cbor_encoder_close_container(&rootArray, &map);
193 err = err | cbor_encoder_close_container(&encoder, &rootArray);
194 return checkError(err, &encoder, outPayload, size);
198 static int64_t OCConvertDiscoveryPayload(OCDiscoveryPayload* payload, uint8_t* outPayload,
201 CborEncoder encoder = {0};
203 size_t resourceCount = OCDiscoveryPayloadGetResourceCount(payload);
205 cbor_encoder_init(&encoder, outPayload, *size, 0);
207 CborEncoder rootArray;
208 err = err | cbor_encoder_create_array(&encoder, &rootArray, resourceCount);
210 for(size_t i = 0; i < resourceCount; ++i)
213 OCResourcePayload* resource = OCDiscoveryPayloadGetResource(payload, i);
217 return OC_STACK_INVALID_PARAM;
220 err = err | cbor_encoder_create_map(&rootArray, &map, 3);
222 err = err | AddTextStringToMap(&map, OC_RSRVD_HREF,
223 sizeof(OC_RSRVD_HREF) - 1,
227 err = err | cbor_encode_text_string(&map, OC_RSRVD_SERVER_INSTANCE_ID,
228 sizeof(OC_RSRVD_SERVER_INSTANCE_ID) - 1);
229 err = err | cbor_encode_byte_string(&map, resource->sid, UUID_SIZE);
233 err = err | cbor_encode_text_string(&map, OC_RSRVD_PROPERTY,
234 sizeof(OC_RSRVD_PROPERTY) -1 );
235 err = err | cbor_encoder_create_map(&map, &propMap, 3);
240 err = err | cbor_encode_text_string(&propMap, OC_RSRVD_RESOURCE_TYPE,
241 sizeof(OC_RSRVD_RESOURCE_TYPE) - 1);
242 err = err | cbor_encoder_create_array(&propMap, &rtArray, CborIndefiniteLength);
244 OCStringLL* rtPtr = resource->types;
247 err = err | cbor_encode_text_string(&rtArray, rtPtr->value,
248 strlen(rtPtr->value));
252 err = err | cbor_encoder_close_container(&propMap, &rtArray);
258 err = err | cbor_encode_text_string(&propMap, OC_RSRVD_INTERFACE,
259 sizeof(OC_RSRVD_INTERFACE) - 1);
260 err = err | cbor_encoder_create_array(&propMap, &ifArray, CborIndefiniteLength);
261 OCStringLL* ifPtr = resource->interfaces;
265 err = err | cbor_encode_text_string(&ifArray, ifPtr->value,
266 strlen(ifPtr->value));
270 err = err | cbor_encoder_close_container(&propMap, &ifArray);
274 CborEncoder policyMap;
275 err = err | cbor_encode_text_string(&propMap, OC_RSRVD_POLICY,
276 sizeof(OC_RSRVD_POLICY) - 1);
277 err = err | cbor_encoder_create_map(&propMap, &policyMap, CborIndefiniteLength);
280 err = err | cbor_encode_text_string(&policyMap, OC_RSRVD_BITMAP,
281 sizeof(OC_RSRVD_BITMAP) - 1);
282 err = err | cbor_encode_uint(&policyMap, resource->bitmap);
286 err = err | cbor_encode_text_string(&policyMap, OC_RSRVD_SECURE,
287 sizeof(OC_RSRVD_SECURE) - 1);
288 err = err | cbor_encode_boolean(&policyMap, OC_RESOURCE_SECURE);
290 if(resource->port != 0)
292 err = err | cbor_encode_text_string(&policyMap, OC_RSRVD_HOSTING_PORT,
293 sizeof(OC_RSRVD_HOSTING_PORT) - 1);
294 err = err | cbor_encode_uint(&policyMap, resource->port);
298 err = err | cbor_encoder_close_container(&propMap, &policyMap);
301 err = err | cbor_encoder_close_container(&map, &propMap);
304 err = err | cbor_encoder_close_container(&rootArray, &map);
307 err = err | cbor_encoder_close_container(&encoder, &rootArray);
309 return checkError(err, &encoder, outPayload, size);
312 static int64_t OCConvertDevicePayload(OCDevicePayload* payload, uint8_t* outPayload,
315 CborEncoder encoder = {0};
318 cbor_encoder_init(&encoder, outPayload, *size, 0);
319 CborEncoder rootArray;
320 err = err | cbor_encoder_create_array(&encoder, &rootArray, 1);
324 err = err | cbor_encoder_create_map(&rootArray, &map, 2);
327 err = err | AddTextStringToMap(&map, OC_RSRVD_HREF, sizeof(OC_RSRVD_HREF) - 1,
333 err = err | cbor_encode_text_string(&map, OC_RSRVD_REPRESENTATION,
334 sizeof(OC_RSRVD_REPRESENTATION) - 1);
335 err = err | cbor_encoder_create_map(&map, &repMap, 4);
338 err = err | cbor_encode_text_string(&repMap, OC_RSRVD_DEVICE_ID,
339 sizeof(OC_RSRVD_DEVICE_ID) - 1);
340 err = err | cbor_encode_byte_string(&repMap, payload->sid, UUID_SIZE);
343 err = err | AddTextStringToMap(&repMap, OC_RSRVD_DEVICE_NAME,
344 sizeof(OC_RSRVD_DEVICE_NAME) - 1,
345 payload->deviceName);
347 // Device Spec Version
348 err = err | AddTextStringToMap(&repMap, OC_RSRVD_SPEC_VERSION,
349 sizeof(OC_RSRVD_SPEC_VERSION) - 1,
350 payload->specVersion);
352 // Device data Model Version
353 err = err | AddTextStringToMap(&repMap, OC_RSRVD_DATA_MODEL_VERSION,
354 sizeof(OC_RSRVD_DATA_MODEL_VERSION) - 1,
355 payload->dataModelVersion);
357 err = err | cbor_encoder_close_container(&map, &repMap);
361 err = err | cbor_encoder_close_container(&rootArray, &map);
365 err = err | cbor_encoder_close_container(&encoder, &rootArray);
367 return checkError(err, &encoder, outPayload, size);
370 static int64_t OCConvertPlatformPayload(OCPlatformPayload* payload, uint8_t* outPayload,
373 CborEncoder encoder = {0};
376 cbor_encoder_init(&encoder, outPayload, *size, 0);
377 CborEncoder rootArray;
378 err = err | cbor_encoder_create_array(&encoder, &rootArray, 1);
381 err = err | cbor_encoder_create_map(&rootArray, &map, CborIndefiniteLength);
384 err = err | AddTextStringToMap(&map, OC_RSRVD_HREF, sizeof(OC_RSRVD_HREF) - 1,
390 err = err | cbor_encode_text_string(&map, OC_RSRVD_REPRESENTATION,
391 sizeof(OC_RSRVD_REPRESENTATION) - 1);
392 err = err | cbor_encoder_create_map(&map, &repMap, CborIndefiniteLength);
395 err = err | AddTextStringToMap(&repMap, OC_RSRVD_PLATFORM_ID,
396 sizeof(OC_RSRVD_PLATFORM_ID) - 1,
397 payload->info.platformID);
400 err = err | AddTextStringToMap(&repMap, OC_RSRVD_MFG_NAME,
401 sizeof(OC_RSRVD_MFG_NAME) - 1,
402 payload->info.manufacturerName);
405 err = err | ConditionalAddTextStringToMap(&repMap, OC_RSRVD_MFG_URL,
406 sizeof(OC_RSRVD_MFG_URL) - 1,
407 payload->info.manufacturerUrl);
410 err = err | ConditionalAddTextStringToMap(&repMap, OC_RSRVD_MODEL_NUM,
411 sizeof(OC_RSRVD_MODEL_NUM) - 1,
412 payload->info.modelNumber);
415 err = err | ConditionalAddTextStringToMap(&repMap, OC_RSRVD_MFG_DATE,
416 sizeof(OC_RSRVD_MFG_DATE) - 1,
417 payload->info.dateOfManufacture);
420 err = err | ConditionalAddTextStringToMap(&repMap, OC_RSRVD_PLATFORM_VERSION,
421 sizeof(OC_RSRVD_PLATFORM_VERSION) - 1,
422 payload->info.platformVersion);
425 err = err | ConditionalAddTextStringToMap(&repMap, OC_RSRVD_OS_VERSION,
426 sizeof(OC_RSRVD_OS_VERSION) - 1,
427 payload->info.operatingSystemVersion);
430 err = err | ConditionalAddTextStringToMap(&repMap, OC_RSRVD_HARDWARE_VERSION,
431 sizeof(OC_RSRVD_HARDWARE_VERSION) - 1,
432 payload->info.hardwareVersion);
435 err = err | ConditionalAddTextStringToMap(&repMap, OC_RSRVD_FIRMWARE_VERSION,
436 sizeof(OC_RSRVD_FIRMWARE_VERSION) - 1,
437 payload->info.firmwareVersion);
440 err = err | ConditionalAddTextStringToMap(&repMap, OC_RSRVD_SUPPORT_URL,
441 sizeof(OC_RSRVD_SUPPORT_URL) - 1,
442 payload->info.supportUrl);
445 err = err | ConditionalAddTextStringToMap(&repMap, OC_RSRVD_SYSTEM_TIME,
446 sizeof(OC_RSRVD_SYSTEM_TIME) - 1,
447 payload->info.systemTime);
448 err = err | cbor_encoder_close_container(&map, &repMap);
452 err = err | cbor_encoder_close_container(&rootArray, &map);
456 err = err | cbor_encoder_close_container(&encoder, &rootArray);
458 return checkError(err, &encoder, outPayload, size);
461 static int64_t OCConvertArrayItem(CborEncoder* array, const OCRepPayloadValueArray* valArray,
465 switch (valArray->type)
467 case OCREP_PROP_NULL:
468 OC_LOG(ERROR, TAG, "ConvertArray Invalid NULL");
469 err = CborUnknownError;
472 err = err | cbor_encode_int(array, valArray->iArray[index]);
474 case OCREP_PROP_DOUBLE:
475 err = err | cbor_encode_double(array, valArray->dArray[index]);
477 case OCREP_PROP_BOOL:
478 err = err | cbor_encode_boolean(array, valArray->bArray[index]);
480 case OCREP_PROP_STRING:
481 if (!valArray->strArray[index])
483 err = err | cbor_encode_null(array);
487 err = err | cbor_encode_text_string(array, valArray->strArray[index],
488 strlen(valArray->strArray[index]));
491 case OCREP_PROP_OBJECT:
492 if (!valArray->objArray[index])
494 err = err | cbor_encode_null(array);
498 err = OCConvertSingleRepPayload(array, valArray->objArray[index]);
501 case OCREP_PROP_ARRAY:
502 OC_LOG(ERROR, TAG, "ConvertArray Invalid child array");
503 err = CborUnknownError;
509 static int64_t OCConvertArray(CborEncoder* parent, const OCRepPayloadValueArray* valArray)
514 err = err | cbor_encoder_create_array(parent, &array, valArray->dimensions[0]);
516 for (size_t i = 0; i < valArray->dimensions[0];++i)
518 if (valArray->dimensions[1] != 0)
521 err = err | cbor_encoder_create_array(&array, &array2, valArray->dimensions[1]);
523 for (size_t j = 0; j < valArray->dimensions[1]; ++j)
525 if (valArray->dimensions[2] != 0)
528 err = err | cbor_encoder_create_array(&array2, &array3,
529 valArray->dimensions[2]);
531 for(size_t k = 0; k < valArray->dimensions[2]; ++k)
533 OCConvertArrayItem(&array3, valArray,
534 j * valArray->dimensions[2] +
535 i * valArray->dimensions[2] * valArray->dimensions[1] +
538 err = err | cbor_encoder_close_container(&array2, &array3);
542 OCConvertArrayItem(&array2, valArray,
543 i * valArray->dimensions[1] + j);
546 err = err | cbor_encoder_close_container(&array, &array2);
550 OCConvertArrayItem(&array, valArray, i);
553 err = err | cbor_encoder_close_container(parent, &array);
557 static int64_t OCConvertSingleRepPayload(CborEncoder* parent, const OCRepPayload* payload)
561 err = err | cbor_encoder_create_map(parent, &map, CborIndefiniteLength);
564 err = err | ConditionalAddTextStringToMap(&map, OC_RSRVD_HREF,
565 sizeof(OC_RSRVD_HREF) - 1,
569 // resource types, interfaces
570 if(payload->types || payload->interfaces)
572 OC_LOG(INFO, TAG, "Payload has types or interfaces");
573 err = err | cbor_encode_text_string(&map,
575 sizeof(OC_RSRVD_PROPERTY) - 1);
577 err = err | cbor_encoder_create_map(&map, &propMap, 2);
579 CborEncoder curArray;
582 err = err | cbor_encode_text_string(&propMap,
583 OC_RSRVD_RESOURCE_TYPE,
584 sizeof(OC_RSRVD_RESOURCE_TYPE) - 1);
585 err = err | cbor_encoder_create_array(&propMap, &curArray, CborIndefiniteLength);
586 OCStringLL* val = payload->types;
589 err = err | cbor_encode_text_string(&curArray, val->value, strlen(val->value));
592 err = err | cbor_encoder_close_container(&propMap, &curArray);
594 if(payload->interfaces)
596 err = err | cbor_encode_text_string(&propMap,
598 sizeof(OC_RSRVD_INTERFACE) - 1);
599 err = err | cbor_encoder_create_array(&propMap, &curArray, CborIndefiniteLength);
600 OCStringLL* val = payload->interfaces;
603 err = err | cbor_encode_text_string(&curArray, val->value, strlen(val->value));
606 err = err | cbor_encoder_close_container(&propMap, &curArray);
608 err = err | cbor_encoder_close_container(&map, &propMap);
614 err = err | cbor_encode_text_string(&map,
615 OC_RSRVD_REPRESENTATION,
616 sizeof(OC_RSRVD_REPRESENTATION) - 1);
617 err = err | cbor_encoder_create_map(&map, &repMap, CborIndefiniteLength);
618 OCRepPayloadValue* value = payload->values;
621 err = err | cbor_encode_text_string(&repMap,
623 strlen(value->name));
626 case OCREP_PROP_NULL:
627 err = err | cbor_encode_null(&repMap);
630 err = err | cbor_encode_int(&repMap,
633 case OCREP_PROP_DOUBLE:
634 err = err | cbor_encode_double(&repMap,
637 case OCREP_PROP_BOOL:
638 err = err | cbor_encode_boolean(&repMap,
641 case OCREP_PROP_STRING:
642 err = err | cbor_encode_text_string(&repMap,
643 value->str, strlen(value->str));
645 case OCREP_PROP_OBJECT:
646 err = err | OCConvertSingleRepPayload(&repMap, value->obj);
648 case OCREP_PROP_ARRAY:
649 err = err | OCConvertArray(&repMap, &value->arr);
652 OC_LOG_V(ERROR, TAG, "Invalid Prop type: %d",
659 err = err | cbor_encoder_close_container(&map, &repMap);
663 err = err | cbor_encoder_close_container(parent, &map);
668 static int64_t OCConvertRepPayload(OCRepPayload* payload, uint8_t* outPayload, size_t* size)
670 CborEncoder encoder = {0};
673 cbor_encoder_init(&encoder, outPayload, *size, 0);
674 CborEncoder rootArray;
675 err = err | cbor_encoder_create_array(&encoder, &rootArray, CborIndefiniteLength);
677 while(payload != NULL && (err == 0 || err == CborErrorOutOfMemory))
679 err = err | OCConvertSingleRepPayload(&rootArray, payload);
680 payload = payload->next;
684 err = err | cbor_encoder_close_container(&encoder, &rootArray);
686 return checkError(err, &encoder, outPayload, size);
689 static int64_t OCConvertPresencePayload(OCPresencePayload* payload,
690 uint8_t* outPayload, size_t* size)
692 CborEncoder encoder = {0};
695 cbor_encoder_init(&encoder, outPayload, *size, 0);
696 CborEncoder rootArray;
698 err = err | cbor_encoder_create_array(&encoder, &rootArray, 1);
701 err = err | cbor_encoder_create_map(&rootArray, &map, CborIndefiniteLength);
704 err = err | cbor_encode_text_string(&map,
706 sizeof(OC_RSRVD_NONCE) - 1);
707 err = err | cbor_encode_uint(&map, payload->sequenceNumber);
710 err = err | cbor_encode_text_string(&map,
712 sizeof(OC_RSRVD_TTL) - 1);
713 err = err | cbor_encode_uint(&map, payload->maxAge);
716 const char* triggerStr = convertTriggerEnumToString(payload->trigger);
717 err = err | AddTextStringToMap(&map, OC_RSRVD_TRIGGER, sizeof(OC_RSRVD_TRIGGER) - 1,
720 // Resource type name
721 if(payload->trigger != OC_PRESENCE_TRIGGER_DELETE)
723 err = err | ConditionalAddTextStringToMap(&map, OC_RSRVD_RESOURCE_TYPE,
724 sizeof(OC_RSRVD_RESOURCE_TYPE) - 1, payload->resourceType);
728 err = err | cbor_encoder_close_container(&rootArray, &map);
729 err = err | cbor_encoder_close_container(&encoder, &rootArray);
731 return checkError(err, &encoder, outPayload, size);
734 static int64_t AddTextStringToMap(CborEncoder* map, const char* key, size_t keylen,
737 return cbor_encode_text_string(map, key, keylen) |
738 cbor_encode_text_string(map, value, strlen(value));
741 static int64_t ConditionalAddTextStringToMap(CborEncoder* map, const char* key, size_t keylen,
744 return value ? AddTextStringToMap(map, key, keylen, value) : 0;