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"
25 #include "oic_string.h"
27 #include "ocpayload.h"
29 #include "ocresourcehandler.h"
31 #include "rdpayload.h"
33 #define TAG "OCPayloadConvert"
34 // Arbitrarily chosen size that seems to contain the majority of packages
35 #define INIT_SIZE (255)
38 #define DISCOVERY_CBOR_ARRAY_LEN 1
39 // CBOR Res Map Length
40 #define DISCOVERY_CBOR_RES_MAP_LEN 2
41 // CBOR Links Map Length
42 #define DISCOVERY_CBOR_LINKS_MAP_LEN 4
44 // Functions all return either a CborError, or a negative version of the OC_STACK return values
45 static int64_t OCConvertPayloadHelper(OCPayload* payload, uint8_t* outPayload, size_t* size);
46 static int64_t OCConvertDiscoveryPayload(OCDiscoveryPayload* payload, uint8_t* outPayload,
48 static int64_t OCConvertDevicePayload(OCDevicePayload* payload, uint8_t* outPayload,
50 static int64_t OCConvertPlatformPayload(OCPlatformPayload* payload, uint8_t* outPayload,
52 static int64_t OCConvertRepPayload(OCRepPayload* payload, uint8_t* outPayload, size_t* size);
53 static int64_t OCConvertPresencePayload(OCPresencePayload* payload, uint8_t* outPayload,
55 static int64_t OCConvertSecurityPayload(OCSecurityPayload* payload, uint8_t* outPayload,
57 static int64_t OCConvertSingleRepPayload(CborEncoder* parent, const OCRepPayload* payload);
58 static int64_t OCConvertArray(CborEncoder* parent, const OCRepPayloadValueArray* valArray);
60 static int64_t AddTextStringToMap(CborEncoder* map, const char* key, size_t keylen,
63 static int64_t ConditionalAddTextStringToMap(CborEncoder* map, const char* key, size_t keylen,
66 OCStackResult OCConvertPayload(OCPayload* payload, uint8_t** outPayload, size_t* size)
68 // TinyCbor Version 47a78569c0 or better on master is required for the re-allocation
69 // strategy to work. If you receive the following assertion error, please do a git-pull
70 // from the extlibs/tinycbor/tinycbor directory
71 #define CborNeedsUpdating (CborErrorOutOfMemory < CborErrorDataTooLarge)
72 OC_STATIC_ASSERT(!CborNeedsUpdating, "tinycbor needs to be updated to at least 47a78569c0");
73 #undef CborNeedsUpdating
76 OC_LOG(ERROR, TAG, "Payload parameter NULL");
77 return OC_STACK_INVALID_PARAM;
80 if (!outPayload || !size)
82 OC_LOG(ERROR, TAG, "Out parameter/s parameter NULL");
83 return OC_STACK_INVALID_PARAM;
86 OC_LOG_V(INFO, TAG, "Converting payload of type %d", payload->type);
88 size_t curSize = INIT_SIZE;
89 uint8_t* out = (uint8_t*)OICCalloc(1, curSize);
90 int64_t err = OCConvertPayloadHelper(payload, out, &curSize);
92 if (err == CborErrorOutOfMemory)
94 // reallocate "out" and try again!
95 uint8_t* out2 = (uint8_t*)OICRealloc(out, curSize);
100 return OC_STACK_NO_MEMORY;
104 err = OCConvertPayloadHelper(payload, out, &curSize);
109 if (curSize < INIT_SIZE)
111 uint8_t* out2 = (uint8_t*)OICRealloc(out, curSize);
116 return OC_STACK_NO_MEMORY;
128 return (OCStackResult)-err;
132 return OC_STACK_ERROR;
136 static int64_t OCConvertPayloadHelper(OCPayload* payload, uint8_t* outPayload, size_t* size)
138 switch(payload->type)
140 case PAYLOAD_TYPE_DISCOVERY:
141 return OCConvertDiscoveryPayload((OCDiscoveryPayload*)payload, outPayload, size);
142 case PAYLOAD_TYPE_DEVICE:
143 return OCConvertDevicePayload((OCDevicePayload*)payload, outPayload, size);
144 case PAYLOAD_TYPE_PLATFORM:
145 return OCConvertPlatformPayload((OCPlatformPayload*)payload, outPayload, size);
146 case PAYLOAD_TYPE_REPRESENTATION:
147 return OCConvertRepPayload((OCRepPayload*)payload, outPayload, size);
148 case PAYLOAD_TYPE_PRESENCE:
149 return OCConvertPresencePayload((OCPresencePayload*)payload, outPayload, size);
150 case PAYLOAD_TYPE_SECURITY:
151 return OCConvertSecurityPayload((OCSecurityPayload*)payload, outPayload, size);
152 case PAYLOAD_TYPE_RD:
153 return OCRDPayloadToCbor((OCRDPayload*)payload, outPayload, size);
155 OC_LOG_V(INFO,TAG, "ConvertPayload default %d", payload->type);
156 return OC_STACK_NOTIMPL;
160 static int64_t checkError(int64_t err, CborEncoder* encoder, uint8_t* outPayload, size_t* size)
162 if (err == CborErrorOutOfMemory)
164 *size += encoder->ptr - encoder->end;
169 OC_LOG_V(ERROR, TAG, "Convert Payload failed : %zd", err);
174 *size = encoder->ptr - outPayload;
179 static int64_t OCConvertSecurityPayload(OCSecurityPayload* payload, uint8_t* outPayload,
185 cbor_encoder_init(&encoder, outPayload, *size, 0);
187 CborEncoder rootArray;
188 err = err | cbor_encoder_create_array(&encoder, &rootArray, 1);
191 err = err | cbor_encoder_create_map(&rootArray, &map, CborIndefiniteLength);
193 if(payload->securityData)
195 err = err | AddTextStringToMap(&map, OC_RSRVD_REPRESENTATION,
196 sizeof(OC_RSRVD_REPRESENTATION) - 1,
197 payload->securityData);
200 err = err | cbor_encoder_close_container(&rootArray, &map);
202 err = err | cbor_encoder_close_container(&encoder, &rootArray);
203 return checkError(err, &encoder, outPayload, size);
206 static char* OCStringLLJoin(OCStringLL* val)
208 OCStringLL* temp = val;
209 size_t size = strlen(temp->value);
215 size += strlen(temp->value);
218 char* joinedStr = (char*)OICCalloc(sizeof(char), size + 1);
225 OICStrcat(joinedStr, size + 1, val->value);
229 OICStrcat(joinedStr, size + 1, " ");
230 OICStrcat(joinedStr, size + 1, val->value);
235 static int64_t OCConvertDiscoveryPayload(OCDiscoveryPayload* payload, uint8_t* outPayload,
238 CborEncoder encoder = {0};
239 CborEncoder rootArray = {0};
242 cbor_encoder_init(&encoder, outPayload, *size, 0);
244 if (payload->collectionResources)
246 CborError cborEncoderResult;
247 cborEncoderResult = cbor_encoder_create_array(&encoder, &rootArray, DISCOVERY_CBOR_ARRAY_LEN);
248 if (CborNoError != cborEncoderResult)
250 OC_LOG(ERROR, TAG, "Failed creating root array.");
254 CborEncoder colArray;
255 cborEncoderResult = cbor_encoder_create_array(&rootArray, &colArray, CborIndefiniteLength);
256 if (CborNoError != cborEncoderResult)
258 OC_LOG(ERROR, TAG, "Failed creating collection array.");
261 OCResourceCollectionPayload *colResources = payload->collectionResources;
264 if (OC_STACK_OK != OCTagsPayloadToCbor(colResources->tags, &colArray))
268 if (OC_STACK_OK != OCLinksPayloadToCbor(colResources->setLinks, &colArray))
272 colResources = colResources->next;
274 cborEncoderResult = cbor_encoder_close_container(&rootArray, &colArray);
275 if (CborNoError != cborEncoderResult)
277 OC_LOG(ERROR, TAG, "Failed closing collection array.");
280 cborEncoderResult = cbor_encoder_close_container(&encoder, &rootArray);
281 if (CborNoError != cborEncoderResult)
283 OC_LOG(ERROR, TAG, "Failed closing root array.");
287 else if (payload->resources)
289 size_t resourceCount = OCDiscoveryPayloadGetResourceCount(payload);
290 err = err | cbor_encoder_create_array(&encoder, &rootArray, resourceCount);
292 for(size_t i = 0; i < resourceCount; ++i)
295 OCResourcePayload* resource = OCDiscoveryPayloadGetResource(payload, i);
299 return OC_STACK_INVALID_PARAM;
302 err = err | cbor_encoder_create_map(&rootArray, &map, DISCOVERY_CBOR_RES_MAP_LEN);
305 err = err | cbor_encode_text_string(&map, OC_RSRVD_DEVICE_ID,
306 sizeof(OC_RSRVD_DEVICE_ID) - 1);
307 err = err | cbor_encode_byte_string(&map, resource->sid, UUID_SIZE);
310 CborEncoder linkArray;
311 err = err | cbor_encode_text_string(&map, OC_RSRVD_LINKS, sizeof(OC_RSRVD_LINKS) -1);
312 err = err | cbor_encoder_create_array(&map, &linkArray, CborIndefiniteLength);
317 err = err | cbor_encoder_create_map(&linkArray, &linkMap, DISCOVERY_CBOR_LINKS_MAP_LEN);
320 err = err | AddTextStringToMap(&linkMap, OC_RSRVD_HREF,
321 sizeof(OC_RSRVD_HREF) - 1,
326 char* joinedTypes = OCStringLLJoin(resource->types);
329 err = err | cbor_encode_text_string(&linkMap, OC_RSRVD_RESOURCE_TYPE,
330 sizeof(OC_RSRVD_RESOURCE_TYPE) - 1);
331 err = err | cbor_encode_text_string(&linkMap, joinedTypes,
332 strlen(joinedTypes));
333 OICFree(joinedTypes);
337 return OC_STACK_NO_MEMORY;
341 if (resource->interfaces)
343 char* joinedInterfaces = OCStringLLJoin(resource->interfaces);
344 if (joinedInterfaces)
346 err = err | cbor_encode_text_string(&linkMap, OC_RSRVD_INTERFACE,
347 sizeof(OC_RSRVD_INTERFACE) - 1);
348 err = err | cbor_encode_text_string(&linkMap, joinedInterfaces,
349 strlen(joinedInterfaces));
350 OICFree(joinedInterfaces);
354 return OC_STACK_NO_MEMORY;
359 CborEncoder policyMap;
360 err = err | cbor_encode_text_string(&linkMap, OC_RSRVD_POLICY,
361 sizeof(OC_RSRVD_POLICY) - 1);
362 err = err | cbor_encoder_create_map(&linkMap, &policyMap, CborIndefiniteLength);
365 err = err | cbor_encode_text_string(&policyMap, OC_RSRVD_BITMAP,
366 sizeof(OC_RSRVD_BITMAP) - 1);
367 err = err | cbor_encode_uint(&policyMap, resource->bitmap);
371 err = err | cbor_encode_text_string(&policyMap, OC_RSRVD_SECURE,
372 sizeof(OC_RSRVD_SECURE) - 1);
373 err = err | cbor_encode_boolean(&policyMap, OC_RESOURCE_SECURE);
375 if(resource->port != 0)
377 err = err | cbor_encode_text_string(&policyMap, OC_RSRVD_HOSTING_PORT,
378 sizeof(OC_RSRVD_HOSTING_PORT) - 1);
379 err = err | cbor_encode_uint(&policyMap, resource->port);
383 err = err | cbor_encoder_close_container(&linkMap, &policyMap);
386 err = err | cbor_encoder_close_container(&linkArray, &linkMap);
388 err = err | cbor_encoder_close_container(&map, &linkArray);
390 err = err | cbor_encoder_close_container(&rootArray, &map);
393 err = err | cbor_encoder_close_container(&encoder, &rootArray);
396 return checkError(err, &encoder, outPayload, size);
399 return OC_STACK_ERROR;
402 static int64_t OCConvertDevicePayload(OCDevicePayload* payload, uint8_t* outPayload,
405 CborEncoder encoder = {0};
408 cbor_encoder_init(&encoder, outPayload, *size, 0);
409 CborEncoder rootArray;
410 err = err | cbor_encoder_create_array(&encoder, &rootArray, 1);
414 err = err | cbor_encoder_create_map(&rootArray, &map, CborIndefiniteLength);
417 err = err | ConditionalAddTextStringToMap(&map, OC_RSRVD_HREF, sizeof(OC_RSRVD_HREF) - 1,
423 err = err | cbor_encode_text_string(&map, OC_RSRVD_REPRESENTATION,
424 sizeof(OC_RSRVD_REPRESENTATION) - 1);
425 err = err | cbor_encoder_create_map(&map, &repMap, CborIndefiniteLength);
428 err = err | cbor_encode_text_string(&repMap, OC_RSRVD_DEVICE_ID,
429 sizeof(OC_RSRVD_DEVICE_ID) - 1);
430 err = err | cbor_encode_byte_string(&repMap, payload->sid, UUID_SIZE);
433 err = err | ConditionalAddTextStringToMap(&repMap, OC_RSRVD_DEVICE_NAME,
434 sizeof(OC_RSRVD_DEVICE_NAME) - 1,
435 payload->deviceName);
437 // Device Spec Version
438 err = err | ConditionalAddTextStringToMap(&repMap, OC_RSRVD_SPEC_VERSION,
439 sizeof(OC_RSRVD_SPEC_VERSION) - 1,
440 payload->specVersion);
442 // Device data Model Version
443 err = err | ConditionalAddTextStringToMap(&repMap, OC_RSRVD_DATA_MODEL_VERSION,
444 sizeof(OC_RSRVD_DATA_MODEL_VERSION) - 1,
445 payload->dataModelVersion);
447 err = err | cbor_encoder_close_container(&map, &repMap);
451 err = err | cbor_encoder_close_container(&rootArray, &map);
455 err = err | cbor_encoder_close_container(&encoder, &rootArray);
457 return checkError(err, &encoder, outPayload, size);
460 static int64_t OCConvertPlatformPayload(OCPlatformPayload* payload, uint8_t* outPayload,
463 CborEncoder encoder = {0};
466 cbor_encoder_init(&encoder, outPayload, *size, 0);
467 CborEncoder rootArray;
468 err = err | cbor_encoder_create_array(&encoder, &rootArray, 1);
471 err = err | cbor_encoder_create_map(&rootArray, &map, CborIndefiniteLength);
474 err = err | ConditionalAddTextStringToMap(&map, OC_RSRVD_HREF, sizeof(OC_RSRVD_HREF) - 1,
480 err = err | cbor_encode_text_string(&map, OC_RSRVD_REPRESENTATION,
481 sizeof(OC_RSRVD_REPRESENTATION) - 1);
482 err = err | cbor_encoder_create_map(&map, &repMap, CborIndefiniteLength);
485 err = err | ConditionalAddTextStringToMap(&repMap, OC_RSRVD_PLATFORM_ID,
486 sizeof(OC_RSRVD_PLATFORM_ID) - 1,
487 payload->info.platformID);
490 err = err | ConditionalAddTextStringToMap(&repMap, OC_RSRVD_MFG_NAME,
491 sizeof(OC_RSRVD_MFG_NAME) - 1,
492 payload->info.manufacturerName);
495 err = err | ConditionalAddTextStringToMap(&repMap, OC_RSRVD_MFG_URL,
496 sizeof(OC_RSRVD_MFG_URL) - 1,
497 payload->info.manufacturerUrl);
500 err = err | ConditionalAddTextStringToMap(&repMap, OC_RSRVD_MODEL_NUM,
501 sizeof(OC_RSRVD_MODEL_NUM) - 1,
502 payload->info.modelNumber);
505 err = err | ConditionalAddTextStringToMap(&repMap, OC_RSRVD_MFG_DATE,
506 sizeof(OC_RSRVD_MFG_DATE) - 1,
507 payload->info.dateOfManufacture);
510 err = err | ConditionalAddTextStringToMap(&repMap, OC_RSRVD_PLATFORM_VERSION,
511 sizeof(OC_RSRVD_PLATFORM_VERSION) - 1,
512 payload->info.platformVersion);
515 err = err | ConditionalAddTextStringToMap(&repMap, OC_RSRVD_OS_VERSION,
516 sizeof(OC_RSRVD_OS_VERSION) - 1,
517 payload->info.operatingSystemVersion);
520 err = err | ConditionalAddTextStringToMap(&repMap, OC_RSRVD_HARDWARE_VERSION,
521 sizeof(OC_RSRVD_HARDWARE_VERSION) - 1,
522 payload->info.hardwareVersion);
525 err = err | ConditionalAddTextStringToMap(&repMap, OC_RSRVD_FIRMWARE_VERSION,
526 sizeof(OC_RSRVD_FIRMWARE_VERSION) - 1,
527 payload->info.firmwareVersion);
530 err = err | ConditionalAddTextStringToMap(&repMap, OC_RSRVD_SUPPORT_URL,
531 sizeof(OC_RSRVD_SUPPORT_URL) - 1,
532 payload->info.supportUrl);
535 err = err | ConditionalAddTextStringToMap(&repMap, OC_RSRVD_SYSTEM_TIME,
536 sizeof(OC_RSRVD_SYSTEM_TIME) - 1,
537 payload->info.systemTime);
538 err = err | cbor_encoder_close_container(&map, &repMap);
542 err = err | cbor_encoder_close_container(&rootArray, &map);
546 err = err | cbor_encoder_close_container(&encoder, &rootArray);
548 return checkError(err, &encoder, outPayload, size);
551 static int64_t OCConvertArrayItem(CborEncoder* array, const OCRepPayloadValueArray* valArray,
555 switch (valArray->type)
557 case OCREP_PROP_NULL:
558 OC_LOG(ERROR, TAG, "ConvertArray Invalid NULL");
559 err = CborUnknownError;
562 err = err | cbor_encode_int(array, valArray->iArray[index]);
564 case OCREP_PROP_DOUBLE:
565 err = err | cbor_encode_double(array, valArray->dArray[index]);
567 case OCREP_PROP_BOOL:
568 err = err | cbor_encode_boolean(array, valArray->bArray[index]);
570 case OCREP_PROP_STRING:
571 if (!valArray->strArray[index])
573 err = err | cbor_encode_null(array);
577 err = err | cbor_encode_text_string(array, valArray->strArray[index],
578 strlen(valArray->strArray[index]));
581 case OCREP_PROP_BYTE_STRING:
582 if (!valArray->strArray[index])
584 err = err | cbor_encode_null(array);
588 err = err | cbor_encode_byte_string(array, valArray->ocByteStrArray[index].bytes,
589 valArray->ocByteStrArray[index].len);
592 case OCREP_PROP_OBJECT:
593 if (!valArray->objArray[index])
595 err = err | cbor_encode_null(array);
599 err = OCConvertSingleRepPayload(array, valArray->objArray[index]);
602 case OCREP_PROP_ARRAY:
603 OC_LOG(ERROR, TAG, "ConvertArray Invalid child array");
604 err = CborUnknownError;
610 static int64_t OCConvertArray(CborEncoder* parent, const OCRepPayloadValueArray* valArray)
615 err = err | cbor_encoder_create_array(parent, &array, valArray->dimensions[0]);
617 for (size_t i = 0; i < valArray->dimensions[0];++i)
619 if (valArray->dimensions[1] != 0)
622 err = err | cbor_encoder_create_array(&array, &array2, valArray->dimensions[1]);
624 for (size_t j = 0; j < valArray->dimensions[1]; ++j)
626 if (valArray->dimensions[2] != 0)
629 err = err | cbor_encoder_create_array(&array2, &array3,
630 valArray->dimensions[2]);
632 for(size_t k = 0; k < valArray->dimensions[2]; ++k)
634 OCConvertArrayItem(&array3, valArray,
635 j * valArray->dimensions[2] +
636 i * valArray->dimensions[2] * valArray->dimensions[1] +
639 err = err | cbor_encoder_close_container(&array2, &array3);
643 OCConvertArrayItem(&array2, valArray,
644 i * valArray->dimensions[1] + j);
647 err = err | cbor_encoder_close_container(&array, &array2);
651 OCConvertArrayItem(&array, valArray, i);
654 err = err | cbor_encoder_close_container(parent, &array);
658 static int64_t OCConvertSingleRepPayload(CborEncoder* parent, const OCRepPayload* payload)
662 err = err | cbor_encoder_create_map(parent, &map, CborIndefiniteLength);
665 err = err | ConditionalAddTextStringToMap(&map, OC_RSRVD_HREF,
666 sizeof(OC_RSRVD_HREF) - 1,
670 // resource types, interfaces
671 if(payload->types || payload->interfaces)
673 OC_LOG(INFO, TAG, "Payload has types or interfaces");
674 err = err | cbor_encode_text_string(&map,
676 sizeof(OC_RSRVD_PROPERTY) - 1);
678 err = err | cbor_encoder_create_map(&map, &propMap, CborIndefiniteLength);
682 char* joinedTypes = OCStringLLJoin(payload->types);
685 err = err | cbor_encode_text_string(&propMap, OC_RSRVD_RESOURCE_TYPE,
686 sizeof(OC_RSRVD_RESOURCE_TYPE) - 1);
687 err = err | cbor_encode_text_string(&propMap, joinedTypes,
688 strlen(joinedTypes));
689 OICFree(joinedTypes);
693 return OC_STACK_NO_MEMORY;
696 if (payload->interfaces)
698 char* joinedInterfaces = OCStringLLJoin(payload->interfaces);
699 if (joinedInterfaces)
701 err = err | cbor_encode_text_string(&propMap, OC_RSRVD_INTERFACE,
702 sizeof(OC_RSRVD_INTERFACE) - 1);
703 err = err | cbor_encode_text_string(&propMap, joinedInterfaces,
704 strlen(joinedInterfaces));
705 OICFree(joinedInterfaces);
709 return OC_STACK_NO_MEMORY;
712 err = err | cbor_encoder_close_container(&map, &propMap);
718 err = err | cbor_encode_text_string(&map,
719 OC_RSRVD_REPRESENTATION,
720 sizeof(OC_RSRVD_REPRESENTATION) - 1);
721 err = err | cbor_encoder_create_map(&map, &repMap, CborIndefiniteLength);
722 OCRepPayloadValue* value = payload->values;
725 err = err | cbor_encode_text_string(&repMap,
727 strlen(value->name));
730 case OCREP_PROP_NULL:
731 err = err | cbor_encode_null(&repMap);
734 err = err | cbor_encode_int(&repMap,
737 case OCREP_PROP_DOUBLE:
738 err = err | cbor_encode_double(&repMap,
741 case OCREP_PROP_BOOL:
742 err = err | cbor_encode_boolean(&repMap,
745 case OCREP_PROP_STRING:
746 err = err | cbor_encode_text_string(&repMap,
747 value->str, strlen(value->str));
749 case OCREP_PROP_BYTE_STRING:
750 err = err | cbor_encode_byte_string(&repMap,
751 value->ocByteStr.bytes, value->ocByteStr.len);
753 case OCREP_PROP_OBJECT:
754 err = err | OCConvertSingleRepPayload(&repMap, value->obj);
756 case OCREP_PROP_ARRAY:
757 err = err | OCConvertArray(&repMap, &value->arr);
760 OC_LOG_V(ERROR, TAG, "Invalid Prop type: %d",
767 err = err | cbor_encoder_close_container(&map, &repMap);
771 err = err | cbor_encoder_close_container(parent, &map);
776 static int64_t OCConvertRepPayload(OCRepPayload* payload, uint8_t* outPayload, size_t* size)
778 CborEncoder encoder = {0};
781 cbor_encoder_init(&encoder, outPayload, *size, 0);
782 CborEncoder rootArray;
783 err = err | cbor_encoder_create_array(&encoder, &rootArray, CborIndefiniteLength);
785 while(payload != NULL && (err == 0 || err == CborErrorOutOfMemory))
787 err = err | OCConvertSingleRepPayload(&rootArray, payload);
788 payload = payload->next;
792 err = err | cbor_encoder_close_container(&encoder, &rootArray);
794 return checkError(err, &encoder, outPayload, size);
797 static int64_t OCConvertPresencePayload(OCPresencePayload* payload,
798 uint8_t* outPayload, size_t* size)
800 CborEncoder encoder = {0};
803 cbor_encoder_init(&encoder, outPayload, *size, 0);
804 CborEncoder rootArray;
806 err = err | cbor_encoder_create_array(&encoder, &rootArray, 1);
809 err = err | cbor_encoder_create_map(&rootArray, &map, CborIndefiniteLength);
812 err = err | cbor_encode_text_string(&map,
814 sizeof(OC_RSRVD_NONCE) - 1);
815 err = err | cbor_encode_uint(&map, payload->sequenceNumber);
818 err = err | cbor_encode_text_string(&map,
820 sizeof(OC_RSRVD_TTL) - 1);
821 err = err | cbor_encode_uint(&map, payload->maxAge);
824 const char* triggerStr = convertTriggerEnumToString(payload->trigger);
825 err = err | AddTextStringToMap(&map, OC_RSRVD_TRIGGER, sizeof(OC_RSRVD_TRIGGER) - 1,
828 // Resource type name
829 if(payload->trigger != OC_PRESENCE_TRIGGER_DELETE)
831 err = err | ConditionalAddTextStringToMap(&map, OC_RSRVD_RESOURCE_TYPE,
832 sizeof(OC_RSRVD_RESOURCE_TYPE) - 1, payload->resourceType);
836 err = err | cbor_encoder_close_container(&rootArray, &map);
837 err = err | cbor_encoder_close_container(&encoder, &rootArray);
839 return checkError(err, &encoder, outPayload, size);
842 static int64_t AddTextStringToMap(CborEncoder* map, const char* key, size_t keylen,
845 return cbor_encode_text_string(map, key, keylen) |
846 cbor_encode_text_string(map, value, strlen(value));
849 static int64_t ConditionalAddTextStringToMap(CborEncoder* map, const char* key, size_t keylen,
852 return value ? AddTextStringToMap(map, key, keylen, value) : 0;