1 //******************************************************************
3 // Copyright 2015 Samsung Electronics 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 //-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
20 #include "rdpayload.h"
22 #include "oic_malloc.h"
23 #include "oic_string.h"
26 #include "ocpayload.h"
27 #include "payload_logging.h"
29 #define TAG "OCRDPayload"
31 #define CBOR_ROOT_ARRAY_LENGTH 1
33 static CborError FindStringInMap(CborValue *map, char *tags, char **value);
34 static CborError FindIntInMap(CborValue *map, char *tags, uint64_t *value);
35 static CborError FindStringLLInMap(const CborValue *linksMap, char *tag, OCStringLL **links);
36 static int64_t ConditionalAddTextStringToMap(CborEncoder* map, const char* key, size_t keylen, const char *value);
37 static int64_t ConditionalAddIntToMap(CborEncoder *map, const char *tags, const size_t size, const uint64_t *value);
38 static int64_t AddStringLLToMap(CborEncoder *map, char *tag, const size_t size, OCStringLL *value);
40 int64_t OCRDPayloadToCbor(const OCRDPayload *rdPayload, uint8_t *outPayload, size_t *size)
42 if (!outPayload || !size)
44 OC_LOG(ERROR, TAG, "Invalid parameters.");
45 return OC_STACK_INVALID_PARAM;
50 cbor_encoder_init(&encoder, outPayload, *size, flags);
52 CborError cborEncoderResult;
53 if (rdPayload->rdDiscovery)
56 cborEncoderResult = cbor_encoder_create_map(&encoder, &map, CborIndefiniteLength);
57 if (CborNoError != cborEncoderResult)
59 OC_LOG(ERROR, TAG, "Failed creating discovery map.");
62 if (CborNoError != ConditionalAddTextStringToMap(&map, OC_RSRVD_DEVICE_NAME,
63 sizeof(OC_RSRVD_DEVICE_NAME) - 1, (char *)rdPayload->rdDiscovery->n.deviceName))
65 OC_LOG(ERROR, TAG, "Failed setting OC_RSRVD_DEVICE_NAME.");
68 if (CborNoError != ConditionalAddTextStringToMap(&map, OC_RSRVD_DEVICE_ID,
69 sizeof(OC_RSRVD_DEVICE_ID) - 1, (char *)rdPayload->rdDiscovery->di.id))
71 OC_LOG(ERROR, TAG, "Failed setting OC_RSRVD_DEVICE_ID.");
74 uint64_t sel = (uint8_t) rdPayload->rdDiscovery->sel;
75 if (CborNoError != ConditionalAddIntToMap(&map, OC_RSRVD_RD_DISCOVERY_SEL,
76 sizeof(OC_RSRVD_RD_DISCOVERY_SEL) - 1, &sel))
78 OC_LOG(ERROR, TAG, "Failed setting OC_RSRVD_RD_DISCOVERY_SEL.");
81 cborEncoderResult = cbor_encoder_close_container(&encoder, &map);
82 if (CborNoError != cborEncoderResult)
84 OC_LOG(ERROR, TAG, "Failed closing discovery map.");
88 else if (rdPayload->rdPublish)
91 cborEncoderResult = cbor_encoder_create_array(&encoder, &colArray, CborIndefiniteLength);
92 if (CborNoError != cborEncoderResult)
94 OC_LOG(ERROR, TAG, "Failed creating collection array.");
98 OCResourceCollectionPayload *rdPublish = rdPayload->rdPublish;
101 if (OC_STACK_OK != OCTagsPayloadToCbor(rdPublish->tags, &colArray))
103 OC_LOG(ERROR, TAG, "Failed creating tags payload.");
106 if (OC_STACK_OK != OCLinksPayloadToCbor(rdPublish->setLinks, &colArray))
108 OC_LOG(ERROR, TAG, "Failed creating links payload.");
111 rdPublish = rdPublish->next;
113 cborEncoderResult = cbor_encoder_close_container(&encoder, &colArray);
114 if (CborNoError != cborEncoderResult)
116 OC_LOG(ERROR, TAG, "Failed closing collection array.");
123 cborEncoderResult = cbor_encoder_create_map(&encoder, &map, CborIndefiniteLength);
124 if (CborNoError != cborEncoderResult)
126 OC_LOG(ERROR, TAG, "Failed creating discovery map.");
129 cborEncoderResult = cbor_encoder_close_container(&encoder, &map);
130 if (CborNoError != cborEncoderResult)
132 OC_LOG(ERROR, TAG, "Failed creating discovery map.");
136 *size = encoder.ptr - outPayload;
141 return OC_STACK_ERROR;
144 OCStackResult OCTagsPayloadToCbor(OCTagsPayload *tags, CborEncoder *setMap)
147 CborError cborEncoderResult = cbor_encoder_create_map(setMap, &tagsMap, CborIndefiniteLength);
148 if (CborNoError != cborEncoderResult)
150 OC_LOG(ERROR, TAG, "Failed creating TAGS map.");
151 return OC_STACK_ERROR;
154 if (CborNoError != ConditionalAddTextStringToMap(&tagsMap, OC_RSRVD_DEVICE_NAME,
155 sizeof(OC_RSRVD_DEVICE_NAME) - 1, (char *)tags->n.deviceName))
157 OC_LOG(ERROR, TAG, "Failed adding OC_RSRVD_DEVICE_NAME in TAGS map.");
158 return OC_STACK_ERROR;
160 if (CborNoError != ConditionalAddTextStringToMap(&tagsMap, OC_RSRVD_DEVICE_ID,
161 sizeof(OC_RSRVD_DEVICE_ID) - 1, (char *)tags->di.id))
163 OC_LOG(ERROR, TAG, "Failed adding OC_RSRVD_DEVICE_ID in TAGS map.");
164 return OC_STACK_ERROR;
166 if (CborNoError != ConditionalAddTextStringToMap(&tagsMap, OC_RSRVD_RTS,
167 sizeof(OC_RSRVD_RTS) - 1, (char *)tags->rts))
169 OC_LOG(ERROR, TAG, "Failed adding OC_RSRVD_RTS in TAGS map.");
170 return OC_STACK_ERROR;
172 if (CborNoError != ConditionalAddTextStringToMap(&tagsMap, OC_RSRVD_DREL,
173 sizeof(OC_RSRVD_DREL) - 1, (char *)tags->drel))
175 OC_LOG(ERROR, TAG, "Failed adding OC_RSRVD_DREL in TAGS map.");
176 return OC_STACK_ERROR;
178 if (CborNoError != ConditionalAddTextStringToMap(&tagsMap, OC_RSRVD_BASE_URI,
179 sizeof(OC_RSRVD_BASE_URI) - 1, (char *)tags->baseURI))
181 OC_LOG(ERROR, TAG, "Failed adding OC_RSRVD_BASE_URI in TAGS map.");
182 return OC_STACK_ERROR;
184 uint64_t temp = (uint64_t)tags->bitmap;
185 if (CborNoError != ConditionalAddIntToMap(&tagsMap, OC_RSRVD_BITMAP,
186 sizeof(OC_RSRVD_BITMAP) - 1, &temp))
188 OC_LOG(ERROR, TAG, "Failed adding OC_RSRVD_BITMAP in TAGS map.");
189 return OC_STACK_ERROR;
191 temp = (uint64_t)tags->port;
192 if (CborNoError != ConditionalAddIntToMap(&tagsMap, OC_RSRVD_HOSTING_PORT,
193 sizeof(OC_RSRVD_HOSTING_PORT) - 1, &temp))
195 OC_LOG(ERROR, TAG, "Failed adding OC_RSRVD_HOSTING_PORT in TAGS map.");
196 return OC_STACK_ERROR;
198 temp = (uint64_t)tags->ins;
199 if (CborNoError != ConditionalAddIntToMap(&tagsMap, OC_RSRVD_INS,
200 sizeof(OC_RSRVD_INS) - 1, &temp))
202 OC_LOG(ERROR, TAG, "Failed adding OC_RSRVD_INS in TAGS map.");
203 return OC_STACK_ERROR;
205 temp = (uint64_t)tags->ttl;
206 if (CborNoError != ConditionalAddIntToMap(&tagsMap, OC_RSRVD_TTL,
207 sizeof(OC_RSRVD_TTL) - 1, &temp))
209 OC_LOG(ERROR, TAG, "Failed adding OC_RSRVD_TTL in TAGS map.");
210 return OC_STACK_ERROR;
212 cborEncoderResult = cbor_encoder_close_container(setMap, &tagsMap);
213 if (CborNoError != cborEncoderResult)
215 OC_LOG(ERROR, TAG, "Failed closing TAGS map.");
216 return OC_STACK_ERROR;
221 OCStackResult OCLinksPayloadToCbor(OCLinksPayload *rtPtr, CborEncoder *setMap)
223 CborEncoder linksArray;
224 CborError cborEncoderResult;
226 cborEncoderResult = cbor_encoder_create_array(setMap, &linksArray, CborIndefiniteLength);
227 if (CborNoError != cborEncoderResult)
229 OC_LOG(ERROR, TAG, "Failed creating LINKS array.");
230 return OC_STACK_ERROR;
234 CborEncoder linksMap;
235 cborEncoderResult = cbor_encoder_create_map(&linksArray, &linksMap,
236 CborIndefiniteLength);
237 if (CborNoError != cborEncoderResult)
239 OC_LOG(ERROR, TAG, "Failed creating LINKS map.");
240 return OC_STACK_ERROR;
242 if (CborNoError != ConditionalAddTextStringToMap(&linksMap, OC_RSRVD_HREF,
243 sizeof(OC_RSRVD_HREF) - 1, rtPtr->href))
245 OC_LOG(ERROR, TAG, "Failed adding OC_RSRVD_HREF in LINKS map.");
246 return OC_STACK_ERROR;
248 if (CborNoError != ConditionalAddTextStringToMap(&linksMap, OC_RSRVD_REL,
249 sizeof(OC_RSRVD_REL) - 1, rtPtr->rel))
251 OC_LOG(ERROR, TAG, "Failed adding OC_RSRVD_REL in LINKS map.");
252 return OC_STACK_ERROR;
254 if (CborNoError != ConditionalAddTextStringToMap(&linksMap, OC_RSRVD_TITLE,
255 sizeof(OC_RSRVD_TITLE) - 1, rtPtr->title))
257 OC_LOG(ERROR, TAG, "Failed adding OC_RSRVD_TITLE in LINKS map.");
258 return OC_STACK_ERROR;
260 if (CborNoError != ConditionalAddTextStringToMap(&linksMap, OC_RSRVD_URI,
261 sizeof(OC_RSRVD_URI) - 1, rtPtr->uri))
263 OC_LOG(ERROR, TAG, "Failed adding OC_RSRVD_URI in LINKS map.");
264 return OC_STACK_ERROR;
266 if (CborNoError != AddStringLLToMap(&linksMap, OC_RSRVD_RESOURCE_TYPE,
267 sizeof(OC_RSRVD_RESOURCE_TYPE) - 1, rtPtr->rt))
269 OC_LOG(ERROR, TAG, "Failed adding OC_RSRVD_RESOURCE_TYPE in LINKS map.");
270 return OC_STACK_ERROR;
272 if (CborNoError != AddStringLLToMap(&linksMap, OC_RSRVD_INTERFACE,
273 sizeof(OC_RSRVD_INTERFACE) - 1, rtPtr->itf))
275 OC_LOG(ERROR, TAG, "Failed adding OC_RSRVD_INTERFACE in LINKS map.");
276 return OC_STACK_ERROR;
278 if (CborNoError != AddStringLLToMap(&linksMap, OC_RSRVD_MEDIA_TYPE,
279 sizeof(OC_RSRVD_MEDIA_TYPE) - 1, rtPtr->mt))
281 OC_LOG(ERROR, TAG, "Failed adding OC_RSRVD_MEDIA_TYPE in LINKS map.");
282 return OC_STACK_ERROR;
284 uint64_t temp = (uint64_t)rtPtr->ins;
285 if (CborNoError != ConditionalAddIntToMap(&linksMap, OC_RSRVD_INS,
286 sizeof(OC_RSRVD_INS) - 1, &temp))
288 OC_LOG(ERROR, TAG, "Failed adding OC_RSRVD_INS in LINKS map.");
289 return OC_STACK_ERROR;
291 cborEncoderResult = cbor_encoder_close_container(&linksArray, &linksMap);
292 if (CborNoError != cborEncoderResult)
294 OC_LOG(ERROR, TAG, "Failed closing LINKS map.");
295 return OC_STACK_ERROR;
299 cborEncoderResult = cbor_encoder_close_container(setMap, &linksArray);
300 if (CborNoError != cborEncoderResult)
302 OC_LOG(ERROR, TAG, "Failed closing LINKS array.");
303 return OC_STACK_ERROR;;
308 OCStackResult OCRDCborToPayload(const CborValue *cborPayload, OCPayload **outPayload)
310 CborValue *rdCBORPayload = (CborValue *)cborPayload;
311 CborError cborFindResult;
313 OCRDPayload *rdPayload = OCRDPayloadCreate();
319 if (cbor_value_is_array(rdCBORPayload))
321 OCLinksPayload *linksPayload = NULL;
322 OCTagsPayload *tagsPayload = NULL;
324 while (cbor_value_is_container(rdCBORPayload))
328 cborFindResult = cbor_value_enter_container(rdCBORPayload, &tags);
329 if (cborFindResult != CborNoError)
333 if (OC_STACK_OK != OCTagsCborToPayload(&tags, &tagsPayload))
335 OCFreeTagsResource(tagsPayload);
338 OCTagsLog(DEBUG, tagsPayload);
339 if (OC_STACK_OK != OCLinksCborToPayload(&tags, &linksPayload))
341 OCFreeLinksResource(linksPayload);
342 OCFreeTagsResource(tagsPayload);
345 OCLinksLog(DEBUG, linksPayload);
346 // Move from tags payload to links array.
347 if (CborNoError != cbor_value_advance(rdCBORPayload))
349 OC_LOG(DEBUG, TAG, "Failed advancing from tags payload to links.");
350 OCFreeLinksResource(linksPayload);
351 OCFreeTagsResource(tagsPayload);
355 rdPayload->rdPublish = OCCopyCollectionResource(tagsPayload, linksPayload);
356 if (!rdPayload->rdPublish)
361 else if (cbor_value_is_map(rdCBORPayload))
364 if (CborNoError != FindStringInMap(rdCBORPayload, OC_RSRVD_DEVICE_NAME, &name))
369 if (CborNoError != FindStringInMap(rdCBORPayload, OC_RSRVD_DEVICE_ID, &id))
373 uint64_t biasFactor = 0;
374 if (CborNoError != FindIntInMap(rdCBORPayload, OC_RSRVD_RD_DISCOVERY_SEL, &biasFactor))
378 rdPayload->rdDiscovery = OCRDDiscoveryPayloadCreate(name, id, (uint8_t)biasFactor);
379 if (!rdPayload->rdDiscovery)
385 cborFindResult = cbor_value_advance(rdCBORPayload);
386 if (CborNoError != cborFindResult)
391 OC_LOG_PAYLOAD(DEBUG, (OCPayload *) rdPayload);
392 *outPayload = (OCPayload *)rdPayload;
395 OC_LOG(ERROR, TAG, "Failed allocating memory.");
396 OCRDPayloadDestroy(rdPayload);
397 return OC_STACK_NO_MEMORY;
400 OCRDPayloadDestroy(rdPayload);
401 return OC_STACK_ERROR;
404 static CborError FindStringInMap(CborValue *map, char *tags, char **value)
408 CborError cborFindResult = cbor_value_map_find_value(map, tags, &curVal);
409 if (CborNoError == cborFindResult && cbor_value_is_text_string(&curVal))
411 cborFindResult = cbor_value_dup_text_string(&curVal, value, &len, NULL);
412 if (CborNoError != cborFindResult)
414 OC_LOG_V(ERROR, TAG, "Failed finding value for tag %s .", tags);
415 return cborFindResult;
421 static CborError FindIntInMap(CborValue *map, char *tags, uint64_t *value)
424 CborError cborFindResult = cbor_value_map_find_value(map, tags, &curVal);
425 if (CborNoError == cborFindResult && cbor_value_is_unsigned_integer(&curVal))
427 cborFindResult = cbor_value_get_uint64(&curVal, value);
428 if (CborNoError != cborFindResult)
430 OC_LOG_V(ERROR, TAG, "Failed finding value for tag %s .", tags);
431 return cborFindResult;
437 static CborError FindStringLLInMap(const CborValue *linksMap, char *tag, OCStringLL **links)
440 CborError cborFindResult;
442 cborFindResult = cbor_value_map_find_value(linksMap, tag, &rtArray);
443 if (CborNoError != cborFindResult)
445 return CborUnknownError;
448 cborFindResult = cbor_value_enter_container(&rtArray, &rtVal);
449 if (CborNoError != cborFindResult)
451 return CborUnknownError;
453 OCStringLL* llPtr = *links;
454 while (cbor_value_is_text_string(&rtVal))
458 llPtr = (OCStringLL *)OICCalloc(1, sizeof(OCStringLL));
461 return CborUnknownError;
471 llPtr->next = (OCStringLL *)OICCalloc(1, sizeof(OCStringLL));
474 return CborUnknownError;
477 cborFindResult = cbor_value_dup_text_string(&rtVal, &(llPtr->value), &len, NULL);
478 if (CborNoError != cborFindResult)
480 return CborUnknownError;
482 cborFindResult = cbor_value_advance(&rtVal);
483 if (CborNoError != cborFindResult)
485 return CborUnknownError;
489 cborFindResult = cbor_value_leave_container(&rtArray, &rtVal);
490 return cborFindResult;
493 OCStackResult OCTagsCborToPayload(CborValue *tagsMap, OCTagsPayload **tagsPayload)
495 OCTagsPayload *tags = (OCTagsPayload *)OICCalloc(1, sizeof(OCTagsPayload));
498 return OC_STACK_NO_MEMORY;
500 if (cbor_value_is_map(tagsMap))
502 if (CborNoError != FindStringInMap(tagsMap, OC_RSRVD_DEVICE_NAME, &tags->n.deviceName))
504 OCFreeTagsResource(tags);
505 return OC_STACK_ERROR;
507 if (CborNoError != FindStringInMap(tagsMap, OC_RSRVD_DREL, &tags->drel))
509 OCFreeTagsResource(tags);
510 return OC_STACK_ERROR;
512 if (CborNoError != FindStringInMap(tagsMap, OC_RSRVD_RTS, &tags->rts))
514 OCFreeTagsResource(tags);
515 return OC_STACK_ERROR;
517 if (CborNoError != FindStringInMap(tagsMap, OC_RSRVD_BASE_URI, &tags->baseURI))
519 OCFreeTagsResource(tags);
520 return OC_STACK_ERROR;
523 if (CborNoError != FindStringInMap(tagsMap, OC_RSRVD_DEVICE_ID, &id))
525 OCFreeTagsResource(tags);
526 return OC_STACK_ERROR;
530 OICStrcpy((char*)tags->di.id, MAX_IDENTITY_SIZE, id);
531 tags->di.id_length = MAX_IDENTITY_SIZE;
535 if (CborNoError != FindIntInMap(tagsMap, OC_RSRVD_HOSTING_PORT, &temp))
537 OCFreeTagsResource(tags);
538 return OC_STACK_ERROR;
540 tags->port = (uint16_t) temp;
541 if (CborNoError != FindIntInMap(tagsMap, OC_RSRVD_BITMAP, &temp))
543 OCFreeTagsResource(tags);
544 return OC_STACK_ERROR;
546 tags->bitmap = (uint8_t) temp;
547 if (CborNoError != FindIntInMap(tagsMap, OC_RSRVD_INS, &temp))
549 OCFreeTagsResource(tags);
550 return OC_STACK_ERROR;
552 tags->ins = (uint8_t) temp;
553 if (CborNoError != FindIntInMap(tagsMap, OC_RSRVD_TTL, &temp))
555 OCFreeTagsResource(tags);
556 return OC_STACK_ERROR;
558 tags->ttl = (uint32_t) temp;
560 if (CborNoError != cbor_value_advance(tagsMap))
562 OCFreeTagsResource(tags);
563 return OC_STACK_ERROR;
570 OCStackResult OCLinksCborToPayload(CborValue *linksArray, OCLinksPayload **linksPayload)
573 CborError cborFindResult = cbor_value_enter_container(linksArray, &linksMap);
574 if (CborNoError != cborFindResult)
576 OC_LOG(ERROR, TAG, "Failed enter links map");
577 return OC_STACK_ERROR;
580 while (cbor_value_is_map(&linksMap))
582 OCLinksPayload *setLinks = (OCLinksPayload *)OICCalloc(1, sizeof(OCLinksPayload));
585 OC_LOG(ERROR, TAG, "Failed allocating memory.");
586 OCFreeLinksResource(*linksPayload);
587 return OC_STACK_NO_MEMORY;
589 cborFindResult = FindStringInMap(&linksMap, OC_RSRVD_HREF, &setLinks->href);
590 if (CborNoError != cborFindResult)
592 OCFreeLinksResource(*linksPayload);
593 OCFreeLinksResource(setLinks);
594 return OC_STACK_ERROR;
596 cborFindResult = FindStringInMap(&linksMap, OC_RSRVD_REL, &setLinks->rel);
597 if (CborNoError != cborFindResult)
599 OCFreeLinksResource(*linksPayload);
600 OCFreeLinksResource(setLinks);
601 return OC_STACK_ERROR;
604 cborFindResult = FindStringInMap(&linksMap, OC_RSRVD_TITLE, &setLinks->title);
605 if (CborNoError != cborFindResult)
607 OCFreeLinksResource(*linksPayload);
608 OCFreeLinksResource(setLinks);
609 return OC_STACK_ERROR;
611 cborFindResult = FindStringInMap(&linksMap, OC_RSRVD_URI, &setLinks->uri);
612 if (CborNoError != cborFindResult)
614 OCFreeLinksResource(*linksPayload);
615 OCFreeLinksResource(setLinks);
616 return OC_STACK_ERROR;
618 cborFindResult = FindStringLLInMap(&linksMap, OC_RSRVD_RESOURCE_TYPE, &setLinks->rt);
619 if (CborNoError != cborFindResult)
621 OCFreeLinksResource(*linksPayload);
622 OCFreeLinksResource(setLinks);
623 return OC_STACK_ERROR;
625 cborFindResult = FindStringLLInMap(&linksMap, OC_RSRVD_INTERFACE, &setLinks->itf);
626 if (CborNoError != cborFindResult)
628 OCFreeLinksResource(*linksPayload);
629 OCFreeLinksResource(setLinks);
630 return OC_STACK_ERROR;
632 cborFindResult = FindStringLLInMap(&linksMap, OC_RSRVD_MEDIA_TYPE, &setLinks->mt);
633 if (CborNoError != cborFindResult)
635 OCFreeLinksResource(*linksPayload);
636 OCFreeLinksResource(setLinks);
637 return OC_STACK_ERROR;
640 cborFindResult = FindIntInMap(&linksMap, OC_RSRVD_INS, &temp);
641 if (CborNoError != cborFindResult)
643 OCFreeLinksResource(*linksPayload);
644 OCFreeLinksResource(setLinks);
645 return OC_STACK_ERROR;
647 setLinks->ins = (uint8_t) temp;
651 *linksPayload = setLinks;
655 OCLinksPayload *temp = *linksPayload;
660 temp->next = setLinks;
662 cborFindResult = cbor_value_advance(&linksMap);
663 if (CborNoError != cborFindResult)
665 OC_LOG(ERROR, TAG, "Failed advancing links map");
666 OCFreeLinksResource(*linksPayload);
667 OCFreeLinksResource(setLinks);
668 return OC_STACK_ERROR;
674 static int64_t AddTextStringToMap(CborEncoder* map, const char* key, size_t keylen,
677 return cbor_encode_text_string(map, key, keylen) |
678 cbor_encode_text_string(map, value, strlen(value));
681 static int64_t ConditionalAddTextStringToMap(CborEncoder* map, const char* key, size_t keylen,
684 return value ? AddTextStringToMap(map, key, keylen, value) : 0;
687 static int64_t ConditionalAddIntToMap(CborEncoder *map, const char *tags, const size_t size,
688 const uint64_t *value)
690 return (*value) ? (cbor_encode_text_string(map, tags, size) |
691 cbor_encode_uint(map, *value)): 0;
694 static int64_t AddStringLLToMap(CborEncoder *map, char *tag, const size_t size, OCStringLL *value)
697 CborError cborEncoderResult;
698 cborEncoderResult = cbor_encode_text_string(map, tag, size);
699 if (CborNoError != cborEncoderResult)
701 return cborEncoderResult;
703 cborEncoderResult = cbor_encoder_create_array(map, &array, CborIndefiniteLength);
704 if (CborNoError != cborEncoderResult)
706 return cborEncoderResult;
708 OCStringLL *strType = value;
711 cborEncoderResult = cbor_encode_text_string(&array, strType->value, strlen(strType->value));
712 if (CborNoError != cborEncoderResult)
714 return cborEncoderResult;
716 strType = strType->next;
718 cborEncoderResult = cbor_encoder_close_container(map, &array);
719 if (CborNoError != cborEncoderResult)
721 return cborEncoderResult;
723 return cborEncoderResult;
726 OCRDPayload *OCRDPayloadCreate()
728 OCRDPayload *rdPayload = (OCRDPayload *)OICCalloc(1, sizeof(OCRDPayload));
735 rdPayload->base.type = PAYLOAD_TYPE_RD;
740 OCRDDiscoveryPayload *OCRDDiscoveryPayloadCreate(const char *deviceName, const char *id, int biasFactor)
742 OCRDDiscoveryPayload *discoveryPayload = (OCRDDiscoveryPayload *)OICCalloc(1, sizeof(OCRDDiscoveryPayload));
744 if (!discoveryPayload)
751 discoveryPayload->n.deviceName = OICStrdup(deviceName);
752 if (!discoveryPayload->n.deviceName)
754 OICFree(discoveryPayload);
760 OICStrcpy((char*)discoveryPayload->di.id, MAX_IDENTITY_SIZE, id);
763 discoveryPayload->sel = biasFactor;
765 return discoveryPayload;
768 void OCRDPayloadDestroy(OCRDPayload *payload)
775 if (payload->rdDiscovery)
777 if (payload->rdDiscovery->n.deviceName)
779 OICFree(payload->rdDiscovery->n.deviceName);
781 OICFree(payload->rdDiscovery);
784 if (payload->rdPublish)
786 for (OCResourceCollectionPayload *col = payload->rdPublish; col; )
790 OCFreeLinksResource(col->setLinks);
795 OCFreeTagsResource(col->tags);
797 OCResourceCollectionPayload *temp = col->next;
806 OCTagsPayload* OCCopyTagsResources(const char *deviceName, const unsigned char *id, const char *baseURI,
807 uint8_t bitmap, uint16_t port, uint8_t ins, const char *rts,const char *drel, uint32_t ttl)
809 OCTagsPayload *tags = (OCTagsPayload *)OICCalloc(1, sizeof(OCTagsPayload));
816 tags->n.deviceName = OICStrdup(deviceName);
817 if (!tags->n.deviceName)
819 goto memory_allocation_failed;
824 OICStrcpy((char*)tags->di.id, MAX_IDENTITY_SIZE, (char *)id);
827 goto memory_allocation_failed;
832 tags->baseURI = OICStrdup(baseURI);
835 goto memory_allocation_failed;
838 tags->bitmap = bitmap;
843 tags->rts = OICStrdup(rts);
846 goto memory_allocation_failed;
851 tags->drel = OICStrdup(drel);
854 goto memory_allocation_failed;
860 memory_allocation_failed:
861 OC_LOG(ERROR, TAG, "Memory allocation failed.");
862 OCFreeTagsResource(tags);
866 OCLinksPayload* OCCopyLinksResources(const char *href, OCStringLL *rt, OCStringLL *itf,
867 const char *rel, bool obs, const char *title, const char *uri, uint8_t ins, OCStringLL *mt)
869 OCLinksPayload *links = (OCLinksPayload *)OICCalloc(1, sizeof(OCLinksPayload));
872 OC_LOG(ERROR, TAG, "Failed allocating memory.");
877 links->href = OICStrdup(href);
880 goto memory_allocation_failed;
885 links->rt = CloneOCStringLL(rt);
888 goto memory_allocation_failed;
893 links->itf = CloneOCStringLL(itf);
896 goto memory_allocation_failed;
901 links->rel = OICStrdup(rel);
904 goto memory_allocation_failed;
910 links->title = OICStrdup(title);
913 goto memory_allocation_failed;
918 links->uri = OICStrdup(uri);
921 goto memory_allocation_failed;
927 links->mt = CloneOCStringLL(mt);
930 goto memory_allocation_failed;
936 memory_allocation_failed:
937 OC_LOG(ERROR, TAG, "Memory allocation failed.");
938 OCFreeLinksResource(links);
942 void OCLinksAddResource(OCDiscoveryPayload *payload, const char *href, OCStringLL *rt,
943 OCStringLL *itf, const char *rel, bool obs, const char *title, const char *uri,
944 uint8_t ins, OCStringLL *mt)
946 if(!payload->collectionResources->setLinks)
948 payload->collectionResources->setLinks =
949 OCCopyLinksResources(href, rt, itf, rel, obs, title, uri, ins, mt);
953 OCLinksPayload *p = payload->collectionResources->setLinks;
958 p->next = OCCopyLinksResources(href, rt, itf, rel, obs, title, uri, ins, mt);
962 OCResourceCollectionPayload* OCCopyCollectionResource(OCTagsPayload *tags, OCLinksPayload *links)
968 OCResourceCollectionPayload *pl = (OCResourceCollectionPayload *)OICCalloc(1, sizeof(OCResourceCollectionPayload));
971 OC_LOG(ERROR, TAG, "Failed allocating memory for the OCResourceCollectionPayload.");
975 pl->setLinks = links;
980 OCStackResult OCDiscoveryCollectionPayloadAddResource(OCDiscoveryPayload *payload, OCTagsPayload *tags, OCLinksPayload *links)
982 OCResourceCollectionPayload* res = OCCopyCollectionResource(tags, links);
985 return OC_STACK_NO_MEMORY;
987 if(!payload->collectionResources)
989 payload->collectionResources = res;
993 OCResourceCollectionPayload *p = payload->collectionResources;
1003 void OCFreeLinksResource(OCLinksPayload *payload)
1009 OICFree(payload->href);
1010 OCFreeOCStringLL(payload->rt);
1011 OCFreeOCStringLL(payload->itf);
1012 OICFree(payload->rel);
1013 OICFree(payload->title);
1014 OICFree(payload->uri);
1015 OCFreeOCStringLL(payload->mt);
1016 OCFreeLinksResource(payload->next);
1020 void OCFreeTagsResource(OCTagsPayload *payload)
1026 OICFree(payload->n.deviceName);
1027 OICFree(payload->baseURI);
1028 OICFree(payload->rts);
1029 OICFree(payload->drel);
1033 void OCFreeCollectionResource(OCResourceCollectionPayload *payload)
1041 OCFreeTagsResource(payload->tags);
1043 if (payload->setLinks)
1045 OCFreeLinksResource(payload->setLinks);
1047 OCFreeCollectionResource(payload->next);
1051 void OCDiscoveryCollectionPayloadDestroy(OCDiscoveryPayload* payload)
1058 OCFreeCollectionResource(payload->collectionResources);
1063 void OCTagsLog(const LogLevel level, const OCTagsPayload *tags)
1067 if (tags->n.deviceName)
1069 OC_LOG_V(level, TAG, " Device Name : %s ",tags->n.deviceName);
1073 OC_LOG_V(level, TAG, " Base URI : %s ",tags->baseURI);
1075 OC_LOG_V(level, TAG, " Device ID : %s ",tags->di.id);
1076 OC_LOG_V(level, TAG, " Bitmap : %d ",tags->bitmap);
1077 OC_LOG_V(level, TAG, " Port : %d ",tags->port);
1078 OC_LOG_V(level, TAG, " Ins : %d ",tags->ins);
1079 OC_LOG_V(level, TAG, " Ttl : %d ",tags->ttl);
1083 OC_LOG_V(level, TAG, " RTS : %s ",tags->rts);
1087 OC_LOG_V(level, TAG, " DREL : %s ",tags->drel);
1092 void OCLinksLog(const LogLevel level, const OCLinksPayload *links)
1098 OC_LOG_V(level, TAG, " href: %s ",links->href);
1100 OC_LOG(level, TAG, " RT: ");
1101 OCStringLL *rt = links->rt;
1106 OC_LOG_V(level, TAG, " %s", rt->value);
1110 OC_LOG(level, TAG, " IF: ");
1111 OCStringLL *itf = links->itf;
1116 OC_LOG_V(level, TAG, " %s", itf->value);
1120 OC_LOG(level, TAG, " MT: ");
1121 OCStringLL *mt = links->mt;
1126 OC_LOG_V(level, TAG, " %s", mt->value);
1130 OC_LOG_V(level, TAG, " INS: %d", links->ins);
1131 OC_LOG_V(level, TAG, " OBS: %d", links->obs);
1134 OC_LOG_V(level, TAG, " REL: %s", links->rel);
1138 OC_LOG_V(level, TAG, " TITLE: %s", links->title);
1142 OC_LOG_V(level, TAG, " URI: %s", links->uri);
1144 links = links->next;