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 "OIC_RI_PAYLOADCONVERT"
35 // Arbitrarily chosen size that seems to contain the majority of packages
36 #define INIT_SIZE (255)
38 // Discovery Links Map Length.
39 #define LINKS_MAP_LEN 4
41 // Functions all return either a CborError, or a negative version of the OC_STACK return values
42 static CborError OCConvertPayloadHelper(OCPayload *payload, uint8_t *outPayload, size_t *size);
43 static CborError OCConvertDiscoveryPayload(OCDiscoveryPayload *payload, uint8_t *outPayload,
45 static CborError OCConvertDevicePayload(OCDevicePayload *payload, uint8_t *outPayload,
47 static CborError OCConvertPlatformPayload(OCPlatformPayload *payload, uint8_t *outPayload,
49 static CborError OCConvertRepPayload(OCRepPayload *payload, uint8_t *outPayload, size_t *size);
50 static CborError OCConvertRepMap(CborEncoder *map, const OCRepPayload *payload);
51 static CborError OCConvertPresencePayload(OCPresencePayload *payload, uint8_t *outPayload,
53 static CborError OCConvertSecurityPayload(OCSecurityPayload *payload, uint8_t *outPayload,
55 static CborError OCConvertSingleRepPayload(CborEncoder *parent, const OCRepPayload *payload);
56 static CborError OCConvertArray(CborEncoder *parent, const OCRepPayloadValueArray *valArray);
58 static CborError AddTextStringToMap(CborEncoder *map, const char *key, size_t keylen,
60 static CborError ConditionalAddTextStringToMap(CborEncoder *map, const char *key, size_t keylen,
63 OCStackResult OCConvertPayload(OCPayload* payload, uint8_t** outPayload, size_t* size)
65 // TinyCbor Version 47a78569c0 or better on master is required for the re-allocation
66 // strategy to work. If you receive the following assertion error, please do a git-pull
67 // from the extlibs/tinycbor/tinycbor directory
68 #define CborNeedsUpdating (CborErrorOutOfMemory < CborErrorDataTooLarge)
69 OC_STATIC_ASSERT(!CborNeedsUpdating, "tinycbor needs to be updated to at least 47a78569c0");
70 #undef CborNeedsUpdating
72 OCStackResult ret = OC_STACK_INVALID_PARAM;
75 size_t curSize = INIT_SIZE;
77 VERIFY_PARAM_NON_NULL(TAG, payload, "Input param, payload is NULL");
78 VERIFY_PARAM_NON_NULL(TAG, outPayload, "OutPayload parameter is NULL");
79 VERIFY_PARAM_NON_NULL(TAG, size, "size parameter is NULL");
81 OIC_LOG_V(INFO, TAG, "Converting payload of type %d", payload->type);
83 out = (uint8_t *)OICCalloc(1, curSize);
84 VERIFY_PARAM_NON_NULL(TAG, out, "Failed to allocate payload");
85 err = OCConvertPayloadHelper(payload, out, &curSize);
86 ret = OC_STACK_NO_MEMORY;
88 if (err == CborErrorOutOfMemory)
90 // reallocate "out" and try again!
91 uint8_t* out2 = (uint8_t *)OICRealloc(out, curSize);
92 VERIFY_PARAM_NON_NULL(TAG, out2, "Failed to increase payload size");
94 err = OCConvertPayloadHelper(payload, out, &curSize);
97 if (err == CborNoError)
99 if (curSize < INIT_SIZE)
101 uint8_t *out2 = (uint8_t *)OICRealloc(out, curSize);
102 VERIFY_PARAM_NON_NULL(TAG, out2, "Failed to increase payload size");
111 //TODO: Proper conversion from CborError to OCStackResult.
112 ret = (OCStackResult)-err;
119 static CborError OCConvertPayloadHelper(OCPayload* payload, uint8_t* outPayload, size_t* size)
121 switch(payload->type)
123 case PAYLOAD_TYPE_DISCOVERY:
124 return OCConvertDiscoveryPayload((OCDiscoveryPayload*)payload, outPayload, size);
125 case PAYLOAD_TYPE_DEVICE:
126 return OCConvertDevicePayload((OCDevicePayload*)payload, outPayload, size);
127 case PAYLOAD_TYPE_PLATFORM:
128 return OCConvertPlatformPayload((OCPlatformPayload*)payload, outPayload, size);
129 case PAYLOAD_TYPE_REPRESENTATION:
130 return OCConvertRepPayload((OCRepPayload*)payload, outPayload, size);
131 case PAYLOAD_TYPE_PRESENCE:
132 return OCConvertPresencePayload((OCPresencePayload*)payload, outPayload, size);
133 case PAYLOAD_TYPE_SECURITY:
134 return OCConvertSecurityPayload((OCSecurityPayload*)payload, outPayload, size);
135 case PAYLOAD_TYPE_RD:
136 return OCRDPayloadToCbor((OCRDPayload*)payload, outPayload, size);
138 OIC_LOG_V(INFO,TAG, "ConvertPayload default %d", payload->type);
139 return CborErrorUnknownType;
143 static CborError checkError(CborError err, CborEncoder* encoder, uint8_t* outPayload, size_t* size)
145 if (err == CborErrorOutOfMemory)
147 *size += encoder->ptr - encoder->end;
150 else if (err != CborNoError)
152 OIC_LOG_V(ERROR, TAG, "Convert Payload failed : %s", cbor_error_string(err));
157 *size = encoder->ptr - outPayload;
162 static CborError OCConvertSecurityPayload(OCSecurityPayload* payload, uint8_t* outPayload,
166 cbor_encoder_init(&encoder, outPayload, *size, 0);
169 CborError err = cbor_encoder_create_map(&encoder, &map, CborIndefiniteLength);
170 VERIFY_CBOR_SUCCESS(TAG, err, "Creating security map");
172 if (payload->securityData)
174 err = cbor_encode_text_string(&map, payload->securityData,
175 strlen(payload->securityData));
176 VERIFY_CBOR_SUCCESS(TAG, err, "Retrieving security data");
179 err = cbor_encoder_close_container(&encoder, &map);
180 VERIFY_CBOR_SUCCESS(TAG, err, "closing security map");
182 return checkError(err, &encoder, outPayload, size);
185 static char* OCStringLLJoin(OCStringLL* val)
187 OCStringLL* temp = val;
188 size_t size = strlen(temp->value);
194 size += strlen(temp->value);
197 char* joinedStr = (char*)OICCalloc(sizeof(char), size + 1);
204 OICStrcat(joinedStr, size + 1, val->value);
208 OICStrcat(joinedStr, size + 1, " ");
209 OICStrcat(joinedStr, size + 1, val->value);
214 static CborError OCConvertDiscoveryPayload(OCDiscoveryPayload *payload, uint8_t *outPayload,
218 CborError err = CborNoError;
220 cbor_encoder_init(&encoder, outPayload, *size, 0);
222 if (payload->resources)
225 The format for the payload is "modelled" as JSON.
229 "di" : UUID, // device ID
230 links :[ // linksArray contains maps of resources
232 href, rt, if, policy // Resource 1
235 href, rt, if, policy // Resource 2
244 // Open the main root array
245 CborEncoder rootArray;
246 size_t resourceCount = OCDiscoveryPayloadGetResourceCount(payload);
247 err = cbor_encoder_create_array(&encoder, &rootArray, 1);
248 VERIFY_CBOR_SUCCESS(TAG, err, "Failed creating discovery root array");
250 // Open the root map in the root array
252 err = cbor_encoder_create_map(&rootArray, &rootMap, CborIndefiniteLength);
253 VERIFY_CBOR_SUCCESS(TAG, err, "Failed creating discovery map");
255 // Insert Device ID into the root map
256 err = cbor_encode_text_string(&rootMap, OC_RSRVD_DEVICE_ID,
257 sizeof(OC_RSRVD_DEVICE_ID) - 1);
258 VERIFY_CBOR_SUCCESS(TAG, err, "Failed setting tag device id");
259 err = cbor_encode_byte_string(&rootMap, payload->sid, UUID_SIZE);
260 VERIFY_CBOR_SUCCESS(TAG, err, "Failed setting value of device id");
262 // Insert baseURI if present
263 err = ConditionalAddTextStringToMap(&rootMap, OC_RSRVD_BASE_URI,
264 sizeof(OC_RSRVD_BASE_URI) - 1,
266 VERIFY_CBOR_SUCCESS(TAG, err, "Failed setting baseURI");
268 // Insert Links into the root map.
269 CborEncoder linkArray;
270 err = cbor_encode_text_string(&rootMap, OC_RSRVD_LINKS, sizeof(OC_RSRVD_LINKS) - 1);
271 VERIFY_CBOR_SUCCESS(TAG, err, "Failed setting links array tag");
272 err = cbor_encoder_create_array(&rootMap, &linkArray, resourceCount);
273 VERIFY_CBOR_SUCCESS(TAG, err, "Failed setting links array");
275 for(size_t i = 0; i < resourceCount; ++i)
278 OCResourcePayload *resource = OCDiscoveryPayloadGetResource(payload, i);
279 VERIFY_PARAM_NON_NULL(TAG, resource, "Failed retrieving resource");
281 // resource map inside the links array.
282 err = cbor_encoder_create_map(&linkArray, &linkMap, LINKS_MAP_LEN);
283 VERIFY_CBOR_SUCCESS(TAG, err, "Failed creating links map");
285 // Below are insertions of the resource properties into the map.
287 err = AddTextStringToMap(&linkMap, OC_RSRVD_HREF, sizeof(OC_RSRVD_HREF) - 1,
289 VERIFY_CBOR_SUCCESS(TAG, err, "Failed adding uri to links map");
294 err = cbor_encode_text_string(&linkMap, OC_RSRVD_RESOURCE_TYPE,
295 sizeof(OC_RSRVD_RESOURCE_TYPE) - 1);
296 VERIFY_CBOR_SUCCESS(TAG, err, "Failed adding resource types tag to links map");
297 char *joinedTypes = OCStringLLJoin(resource->types);
298 VERIFY_PARAM_NON_NULL(TAG, joinedTypes, "Failed creating joined string");
299 err = cbor_encode_text_string(&linkMap, joinedTypes, strlen(joinedTypes));
300 OICFree(joinedTypes);
301 VERIFY_CBOR_SUCCESS(TAG, err, "Failed adding resource types value to links map");
304 if (resource->interfaces)
306 err = cbor_encode_text_string(&linkMap, OC_RSRVD_INTERFACE,
307 sizeof(OC_RSRVD_INTERFACE) - 1);
308 VERIFY_CBOR_SUCCESS(TAG, err, "Failed adding interfaces tag to links map");
309 char* joinedInterfaces = OCStringLLJoin(resource->interfaces);
310 VERIFY_PARAM_NON_NULL(TAG, joinedInterfaces, "Failed creating joined string");
311 err = cbor_encode_text_string(&linkMap, joinedInterfaces, strlen(joinedInterfaces));
312 OICFree(joinedInterfaces);
313 VERIFY_CBOR_SUCCESS(TAG, err, "Failed adding interfaces value to links map");
317 CborEncoder policyMap;
318 err = cbor_encode_text_string(&linkMap, OC_RSRVD_POLICY, sizeof(OC_RSRVD_POLICY) - 1);
319 VERIFY_CBOR_SUCCESS(TAG, err, "Failed adding policy tag to links map");
320 err = cbor_encoder_create_map(&linkMap, &policyMap, CborIndefiniteLength);
321 VERIFY_CBOR_SUCCESS(TAG, err, "Failed adding policy map to links map");
324 err = cbor_encode_text_string(&policyMap, OC_RSRVD_BITMAP, sizeof(OC_RSRVD_BITMAP) - 1);
325 VERIFY_CBOR_SUCCESS(TAG, err, "Failed adding bitmap tag to policy map");
326 err = cbor_encode_uint(&policyMap, resource->bitmap);
327 VERIFY_CBOR_SUCCESS(TAG, err, "Failed adding bitmap value to policy map");
329 if (resource->secure)
331 err = cbor_encode_text_string(&policyMap, OC_RSRVD_SECURE,
332 sizeof(OC_RSRVD_SECURE) - 1);
333 VERIFY_CBOR_SUCCESS(TAG, err, "Failed adding secure tag to policy map");
334 err = cbor_encode_boolean(&policyMap, OC_RESOURCE_SECURE);
335 VERIFY_CBOR_SUCCESS(TAG, err, "Failed adding secure value to policy map");
337 if ((resource->secure && resource->port != 0) || payload->baseURI)
339 err = cbor_encode_text_string(&policyMap, OC_RSRVD_HOSTING_PORT,
340 sizeof(OC_RSRVD_HOSTING_PORT) - 1);
341 VERIFY_CBOR_SUCCESS(TAG, err, "Failed adding secure port tag");
342 err = cbor_encode_uint(&policyMap, resource->port);
343 VERIFY_CBOR_SUCCESS(TAG, err, "Failed adding secure port value");
346 err = cbor_encoder_close_container(&linkMap, &policyMap);
347 VERIFY_CBOR_SUCCESS(TAG, err, "Failed closing policy map");
349 // Finsihed encoding a resource, close the map.
350 err = cbor_encoder_close_container(&linkArray, &linkMap);
351 VERIFY_CBOR_SUCCESS(TAG, err, "Failed closing link map");
353 // Close links array inside the root map.
354 err = cbor_encoder_close_container(&rootMap, &linkArray);
355 VERIFY_CBOR_SUCCESS(TAG, err, "Failed closing link array");
356 // close root map inside the root array.
357 err = cbor_encoder_close_container(&rootArray, &rootMap);
358 VERIFY_CBOR_SUCCESS(TAG, err, "Failed closing root map");
359 // Close the final root array.
360 err = cbor_encoder_close_container(&encoder, &rootArray);
361 VERIFY_CBOR_SUCCESS(TAG, err, "Failed closing root array");
365 return checkError(err, &encoder, outPayload, size);
368 static CborError OCConvertDevicePayload(OCDevicePayload *payload, uint8_t *outPayload,
371 CborError err = CborNoError;
374 cbor_encoder_init(&encoder, outPayload, *size, 0);
376 err = cbor_encoder_create_map(&encoder, &repMap, CborIndefiniteLength);
377 VERIFY_CBOR_SUCCESS(TAG, err, "Failed creating device map");
380 err = cbor_encode_text_string(&repMap, OC_RSRVD_DEVICE_ID, sizeof(OC_RSRVD_DEVICE_ID) - 1);
381 VERIFY_CBOR_SUCCESS(TAG, err, "Failed adding device id tag");
382 err = cbor_encode_byte_string(&repMap, payload->sid, UUID_SIZE);
383 VERIFY_CBOR_SUCCESS(TAG, err, "Failed adding data device id");
386 err = ConditionalAddTextStringToMap(&repMap, OC_RSRVD_DEVICE_NAME,
387 sizeof(OC_RSRVD_DEVICE_NAME) - 1, payload->deviceName);
388 VERIFY_CBOR_SUCCESS(TAG, err, "Failed adding device name");
390 // Device Spec Version
391 err = ConditionalAddTextStringToMap(&repMap, OC_RSRVD_SPEC_VERSION,
392 sizeof(OC_RSRVD_SPEC_VERSION) - 1, payload->specVersion);
393 VERIFY_CBOR_SUCCESS(TAG, err, "Failed adding data spec version");
395 // Device data Model Version
396 err = ConditionalAddTextStringToMap(&repMap, OC_RSRVD_DATA_MODEL_VERSION,
397 sizeof(OC_RSRVD_DATA_MODEL_VERSION) - 1, payload->dataModelVersion);
398 VERIFY_CBOR_SUCCESS(TAG, err, "Failed adding data model version");
400 err = cbor_encoder_close_container(&encoder, &repMap);
401 VERIFY_CBOR_SUCCESS(TAG, err, "Failed closing device map");
404 return checkError(err, &encoder, outPayload, size);
407 static CborError OCConvertPlatformPayload(OCPlatformPayload *payload, uint8_t *outPayload,
410 CborError err = CborNoError;
413 cbor_encoder_init(&encoder, outPayload, *size, 0);
416 err = cbor_encoder_create_map(&encoder, &repMap, CborIndefiniteLength);
417 VERIFY_CBOR_SUCCESS(TAG, err, "Failed creating platform map");
420 err = ConditionalAddTextStringToMap(&repMap, OC_RSRVD_PLATFORM_ID,
421 sizeof(OC_RSRVD_PLATFORM_ID) - 1, payload->info.platformID);
422 VERIFY_CBOR_SUCCESS(TAG, err, "Failed adding platform id");
425 err = ConditionalAddTextStringToMap(&repMap, OC_RSRVD_MFG_NAME, sizeof(OC_RSRVD_MFG_NAME) - 1,
426 payload->info.manufacturerName);
427 VERIFY_CBOR_SUCCESS(TAG, err, "Failed adding mfg name");
430 err = ConditionalAddTextStringToMap(&repMap, OC_RSRVD_MFG_URL, sizeof(OC_RSRVD_MFG_URL) - 1,
431 payload->info.manufacturerUrl);
432 VERIFY_CBOR_SUCCESS(TAG, err, "Failed adding mfg url");
435 err = ConditionalAddTextStringToMap(&repMap, OC_RSRVD_MODEL_NUM, sizeof(OC_RSRVD_MODEL_NUM) -1,
436 payload->info.modelNumber);
437 VERIFY_CBOR_SUCCESS(TAG, err, "Failed adding model num");
440 err = ConditionalAddTextStringToMap(&repMap, OC_RSRVD_MFG_DATE, sizeof(OC_RSRVD_MFG_DATE) - 1,
441 payload->info.dateOfManufacture);
442 VERIFY_CBOR_SUCCESS(TAG, err, "Failed adding mfg date");
445 err = ConditionalAddTextStringToMap(&repMap, OC_RSRVD_PLATFORM_VERSION,
446 sizeof(OC_RSRVD_PLATFORM_VERSION) - 1, payload->info.platformVersion);
447 VERIFY_CBOR_SUCCESS(TAG, err, "Failed adding platform version");
450 err = ConditionalAddTextStringToMap(&repMap, OC_RSRVD_OS_VERSION,
451 sizeof(OC_RSRVD_OS_VERSION) - 1, payload->info.operatingSystemVersion);
452 VERIFY_CBOR_SUCCESS(TAG, err, "Failed adding OS version");
455 err = ConditionalAddTextStringToMap(&repMap, OC_RSRVD_HARDWARE_VERSION,
456 sizeof(OC_RSRVD_HARDWARE_VERSION) - 1, payload->info.hardwareVersion);
457 VERIFY_CBOR_SUCCESS(TAG, err, "Failed adding HW version");
460 err = ConditionalAddTextStringToMap(&repMap, OC_RSRVD_FIRMWARE_VERSION,
461 sizeof(OC_RSRVD_FIRMWARE_VERSION) - 1, payload->info.firmwareVersion);
462 VERIFY_CBOR_SUCCESS(TAG, err, "Failed adding firmware version");
465 err = ConditionalAddTextStringToMap(&repMap, OC_RSRVD_SUPPORT_URL,
466 sizeof(OC_RSRVD_SUPPORT_URL) - 1, payload->info.supportUrl);
467 VERIFY_CBOR_SUCCESS(TAG, err, "Failed adding support url");
470 err = ConditionalAddTextStringToMap(&repMap, OC_RSRVD_SYSTEM_TIME,
471 sizeof(OC_RSRVD_SYSTEM_TIME) - 1, payload->info.systemTime);
472 VERIFY_CBOR_SUCCESS(TAG, err, "Failed adding system time");
475 err = cbor_encoder_close_container(&encoder, &repMap);
476 VERIFY_CBOR_SUCCESS(TAG, err, "Failed closing rep map");
479 return checkError(err, &encoder, outPayload, size);
482 static CborError OCConvertArrayItem(CborEncoder *array, const OCRepPayloadValueArray *valArray,
485 CborError err = CborNoError;
486 switch (valArray->type)
488 case OCREP_PROP_NULL:
489 OIC_LOG(ERROR, TAG, "ConvertArray Invalid NULL");
490 err = CborUnknownError;
493 err = cbor_encode_int(array, valArray->iArray[index]);
495 case OCREP_PROP_DOUBLE:
496 err = cbor_encode_double(array, valArray->dArray[index]);
498 case OCREP_PROP_BOOL:
499 err = cbor_encode_boolean(array, valArray->bArray[index]);
501 case OCREP_PROP_STRING:
502 if (!valArray->strArray[index])
504 err = cbor_encode_null(array);
508 err = cbor_encode_text_string(array, valArray->strArray[index],
509 strlen(valArray->strArray[index]));
512 case OCREP_PROP_BYTE_STRING:
513 if (!valArray->strArray[index])
515 err = cbor_encode_null(array);
519 err = cbor_encode_byte_string(array, valArray->ocByteStrArray[index].bytes,
520 valArray->ocByteStrArray[index].len);
523 case OCREP_PROP_OBJECT:
524 if (!valArray->objArray[index])
526 err = cbor_encode_null(array);
530 err = OCConvertRepMap(array, valArray->objArray[index]);
533 case OCREP_PROP_ARRAY:
534 OIC_LOG(ERROR, TAG, "ConvertArray Invalid child array");
535 err = CborUnknownError;
542 static CborError OCConvertArray(CborEncoder *parent, const OCRepPayloadValueArray *valArray)
544 CborError err = CborNoError;
546 err = cbor_encoder_create_array(parent, &array, valArray->dimensions[0]);
547 VERIFY_CBOR_SUCCESS(TAG, err, "Failed creating rep array");
549 for (size_t i = 0; i < valArray->dimensions[0]; ++i)
551 if (0 != valArray->dimensions[1])
554 err = cbor_encoder_create_array(&array, &array2, valArray->dimensions[1]);
555 VERIFY_CBOR_SUCCESS(TAG, err, "Failed creating rep array2");
557 for (size_t j = 0; j < valArray->dimensions[1]; ++j)
559 if (0 != valArray->dimensions[2])
562 err = cbor_encoder_create_array(&array2, &array3, valArray->dimensions[2]);
563 VERIFY_CBOR_SUCCESS(TAG, err, "Failed creating rep array3");
565 for(size_t k = 0; k < valArray->dimensions[2]; ++k)
567 err = OCConvertArrayItem(&array3, valArray,
568 j * valArray->dimensions[2] +
569 i * valArray->dimensions[2] * valArray->dimensions[1] +
571 VERIFY_CBOR_SUCCESS(TAG, err, "Failed adding rep array3 value");
573 err = cbor_encoder_close_container(&array2, &array3);
574 VERIFY_CBOR_SUCCESS(TAG, err, "Failed closing rep array3");
578 err = OCConvertArrayItem(&array2, valArray, i * valArray->dimensions[1] + j);
579 VERIFY_CBOR_SUCCESS(TAG, err, "Failed adding rep array2 value");
582 err = cbor_encoder_close_container(&array, &array2);
583 VERIFY_CBOR_SUCCESS(TAG, err, "Failed closing rep array2");
587 err = OCConvertArrayItem(&array, valArray, i);
588 VERIFY_CBOR_SUCCESS(TAG, err, "Failed adding rep array value");
591 err = cbor_encoder_close_container(parent, &array);
592 VERIFY_CBOR_SUCCESS(TAG, err, "Failed closing rep array");
598 static CborError OCConvertRepMap(CborEncoder *map, const OCRepPayload *payload)
600 CborError err = CborNoError;
602 err = cbor_encoder_create_map(map, &repMap, CborIndefiniteLength);
603 VERIFY_CBOR_SUCCESS(TAG, err, "Failed creating rep map");
604 err = OCConvertSingleRepPayload(&repMap, payload);
605 VERIFY_CBOR_SUCCESS(TAG, err, "Failed converting single rep payload");
606 err = cbor_encoder_close_container(map, &repMap);
607 VERIFY_CBOR_SUCCESS(TAG, err, "Failed closing rep map");
612 static CborError OCConvertSingleRepPayload(CborEncoder *repMap, const OCRepPayload *payload)
614 CborError err = CborNoError;
615 OCRepPayloadValue *value = payload->values;
618 err = cbor_encode_text_string(repMap, value->name, strlen(value->name));
619 VERIFY_CBOR_SUCCESS(TAG, err, "Failed adding tag name");
623 case OCREP_PROP_NULL:
624 err = cbor_encode_null(repMap);
627 err = cbor_encode_int(repMap, value->i);
629 case OCREP_PROP_DOUBLE:
630 err = cbor_encode_double(repMap, value->d);
632 case OCREP_PROP_BOOL:
633 err = cbor_encode_boolean(repMap, value->b);
635 case OCREP_PROP_STRING:
636 err = cbor_encode_text_string(repMap, value->str, strlen(value->str));
638 case OCREP_PROP_BYTE_STRING:
639 err = cbor_encode_byte_string(repMap, value->ocByteStr.bytes, value->ocByteStr.len);
641 case OCREP_PROP_OBJECT:
642 err = OCConvertRepMap(repMap, value->obj);
644 case OCREP_PROP_ARRAY:
645 err = OCConvertArray(repMap, &value->arr);
648 OIC_LOG_V(ERROR, TAG, "Invalid Prop type: %d", value->type);
651 VERIFY_CBOR_SUCCESS(TAG, err, "Failed adding single rep value");
659 static CborError OCConvertRepPayload(OCRepPayload *payload, uint8_t *outPayload, size_t *size)
662 CborError err = CborNoError;
664 cbor_encoder_init(&encoder, outPayload, *size, 0);
666 size_t arrayCount = 0;
667 for (OCRepPayload *temp = payload; temp; temp = temp->next)
671 CborEncoder rootArray;
674 err = cbor_encoder_create_array(&encoder, &rootArray, arrayCount);
675 VERIFY_CBOR_SUCCESS(TAG, err, "Failed adding rep root map");
678 while (payload != NULL && (err == CborNoError))
681 err = cbor_encoder_create_map(((arrayCount == 1)? &encoder: &rootArray),
682 &rootMap, CborIndefiniteLength);
683 VERIFY_CBOR_SUCCESS(TAG, err, "Failed creating root map");
685 // Only in case of collection href is included.
686 if (arrayCount > 1 && payload->uri && strlen(payload->uri) > 0)
688 OIC_LOG(INFO, TAG, "Payload has uri");
689 err = cbor_encode_text_string(&rootMap, OC_RSRVD_HREF, strlen(OC_RSRVD_HREF));
690 VERIFY_CBOR_SUCCESS(TAG, err, "Failed adding rep href tag");
691 err = cbor_encode_text_string(&rootMap, payload->uri, strlen(payload->uri));
692 VERIFY_CBOR_SUCCESS(TAG, err, "Failed adding rep href value");
696 OIC_LOG(INFO, TAG, "Payload has types");
697 err = cbor_encode_text_string(&rootMap, OC_RSRVD_RESOURCE_TYPE,
698 sizeof(OC_RSRVD_RESOURCE_TYPE) - 1);
699 VERIFY_CBOR_SUCCESS(TAG, err, "Failed adding rep resource type tag");
700 char* joinedTypes = OCStringLLJoin(payload->types);
701 VERIFY_PARAM_NON_NULL(TAG, joinedTypes, "Failed creating joined string");
702 err = cbor_encode_text_string(&rootMap, joinedTypes, strlen(joinedTypes));
703 OICFree(joinedTypes);
704 VERIFY_CBOR_SUCCESS(TAG, err, "Failed adding rep resource type value");
706 if (payload->interfaces)
708 OIC_LOG(INFO, TAG, "Payload has interfaces");
709 err = cbor_encode_text_string(&rootMap, OC_RSRVD_INTERFACE,
710 sizeof(OC_RSRVD_INTERFACE) - 1);
711 VERIFY_CBOR_SUCCESS(TAG, err, "Failed adding rep interface tag");
712 char* joinedInterfaces = OCStringLLJoin(payload->interfaces);
713 VERIFY_PARAM_NON_NULL(TAG, joinedInterfaces, "Failed creating joined string");
714 err = cbor_encode_text_string(&rootMap, joinedInterfaces, strlen(joinedInterfaces));
715 OICFree(joinedInterfaces);
716 VERIFY_CBOR_SUCCESS(TAG, err, "Failed adding rep interface value");
719 err = OCConvertSingleRepPayload(&rootMap, payload);
720 VERIFY_CBOR_SUCCESS(TAG, err, "Failed setting rep payload");
723 err = cbor_encoder_close_container(((arrayCount == 1) ? &encoder: &rootArray), &rootMap);
724 VERIFY_CBOR_SUCCESS(TAG, err, "Failed closing root map");
725 payload = payload->next;
729 err = cbor_encoder_close_container(&encoder, &rootArray);
730 VERIFY_CBOR_SUCCESS(TAG, err, "Failed closing root array");
734 return checkError(err, &encoder, outPayload, size);
737 static CborError OCConvertPresencePayload(OCPresencePayload *payload, uint8_t *outPayload,
740 CborError err = CborNoError;
743 cbor_encoder_init(&encoder, outPayload, *size, 0);
745 err = cbor_encoder_create_map(&encoder, &map, CborIndefiniteLength);
746 VERIFY_CBOR_SUCCESS(TAG, err, "Failed creating presence map");
749 err = cbor_encode_text_string(&map, OC_RSRVD_NONCE, sizeof(OC_RSRVD_NONCE) - 1);
750 VERIFY_CBOR_SUCCESS(TAG, err, "Failed adding nonce tag to presence map");
751 err = cbor_encode_uint(&map, payload->sequenceNumber);
752 VERIFY_CBOR_SUCCESS(TAG, err, "Failed adding nonce value to presence map");
755 err = cbor_encode_text_string(&map, OC_RSRVD_TTL, sizeof(OC_RSRVD_TTL) - 1);
756 VERIFY_CBOR_SUCCESS(TAG, err, "Failed adding ttl tag to presence map");
757 err = cbor_encode_uint(&map, payload->maxAge);
758 VERIFY_CBOR_SUCCESS(TAG, err, "Failed adding ttl value to presence map");
761 err = cbor_encode_text_string(&map, OC_RSRVD_TRIGGER, sizeof(OC_RSRVD_TRIGGER) - 1);
762 VERIFY_CBOR_SUCCESS(TAG, err, "Failed adding trigger tag to presence map");
763 err = cbor_encode_simple_value(&map, payload->trigger);
764 VERIFY_CBOR_SUCCESS(TAG, err, "Failed adding trigger value to presence map");
766 // Resource type name
767 if (payload->trigger != OC_PRESENCE_TRIGGER_DELETE)
769 err = ConditionalAddTextStringToMap(&map, OC_RSRVD_RESOURCE_TYPE,
770 sizeof(OC_RSRVD_RESOURCE_TYPE) - 1, payload->resourceType);
771 VERIFY_CBOR_SUCCESS(TAG, err, "Failed adding resource type to presence map");
775 err = cbor_encoder_close_container(&encoder, &map);
776 VERIFY_CBOR_SUCCESS(TAG, err, "Failed closing presence map");
779 return checkError(err, &encoder, outPayload, size);
782 static CborError AddTextStringToMap(CborEncoder* map, const char* key, size_t keylen,
785 CborError err = cbor_encode_text_string(map, key, keylen);
786 if (CborNoError != err)
790 return cbor_encode_text_string(map, value, strlen(value));
793 static CborError ConditionalAddTextStringToMap(CborEncoder* map, const char* key, size_t keylen,
796 return value ? AddTextStringToMap(map, key, keylen, value) : CborNoError;