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 "OIC_RI_RDPAYLOAD"
31 #define CBOR_ROOT_ARRAY_LENGTH 1
33 static int64_t OCTagsPayloadToCbor(OCTagsPayload *tags, CborEncoder *setMap);
34 static int64_t OCLinksPayloadToCbor(OCLinksPayload *rtPtr, CborEncoder *setMap);
35 static int64_t ConditionalAddTextStringToMap(CborEncoder* map, const char* key, const char *value);
36 static int64_t ConditionalAddIntToMap(CborEncoder *map, const char *tags, const uint64_t *value);
37 static int64_t AddStringLLToMap(CborEncoder *map, const char *tag, const OCStringLL *value);
38 static CborError OCTagsCborToPayload(CborValue *tagsMap, OCTagsPayload **tagsPayload);
39 static CborError OCLinksCborToPayload(CborValue *linksArray, OCLinksPayload **linksPayload);
40 static CborError FindStringInMap(const CborValue *map, const char *tags, char **value);
41 static CborError FindIntInMap(const CborValue *map, const char *tags, uint64_t *value);
42 static CborError FindStringLLInMap(const CborValue *linksMap, const char *tag, OCStringLL **links);
44 int64_t OCRDPayloadToCbor(const OCRDPayload *rdPayload, uint8_t *outPayload, size_t *size)
46 int64_t cborEncoderResult = CborErrorIO;
49 VERIFY_PARAM_NON_NULL(TAG, rdPayload, "Invalid input parameter rdPayload");
50 VERIFY_PARAM_NON_NULL(TAG, outPayload, "Invalid input parameter outPayload");
51 VERIFY_PARAM_NON_NULL(TAG, size, "Invalid input parameter size");
53 cbor_encoder_init(&encoder, outPayload, *size, flags);
55 if (rdPayload->rdDiscovery)
58 cborEncoderResult |= cbor_encoder_create_map(&encoder, &map, CborIndefiniteLength);
59 VERIFY_CBOR_SUCCESS(TAG, cborEncoderResult, "Failed to create discovery map");
61 cborEncoderResult |= ConditionalAddTextStringToMap(&map, OC_RSRVD_DEVICE_NAME,
62 rdPayload->rdDiscovery->n.deviceName);
63 VERIFY_CBOR_SUCCESS(TAG, cborEncoderResult, "Failed to add OC_RSRVD_DEVICE_NAME in map");
65 cborEncoderResult |= ConditionalAddTextStringToMap(&map, OC_RSRVD_DEVICE_ID,
66 (char *)rdPayload->rdDiscovery->di.id);
67 VERIFY_CBOR_SUCCESS(TAG, cborEncoderResult, "Failed to add OC_RSRVD_DEVICE_ID in map");
70 uint64_t value = rdPayload->rdDiscovery->sel;
71 cborEncoderResult |= ConditionalAddIntToMap(&map, OC_RSRVD_RD_DISCOVERY_SEL, &value);
72 VERIFY_CBOR_SUCCESS(TAG, cborEncoderResult, "Failed to add RD_DISCOVERY_SEL in map");
74 cborEncoderResult |= cbor_encoder_close_container(&encoder, &map);
75 VERIFY_CBOR_SUCCESS(TAG, cborEncoderResult, "Failed closing discovery map");
77 else if (rdPayload->rdPublish)
80 cborEncoderResult |= cbor_encoder_create_array(&encoder, &colArray, CborIndefiniteLength);
81 VERIFY_CBOR_SUCCESS(TAG, cborEncoderResult, "Failed to create collection array");
83 OCResourceCollectionPayload *rdPublish = rdPayload->rdPublish;
86 cborEncoderResult |= OCTagsPayloadToCbor(rdPublish->tags, &colArray);
87 VERIFY_CBOR_SUCCESS(TAG, cborEncoderResult, "Failed adding tags payload");
88 cborEncoderResult |= OCLinksPayloadToCbor(rdPublish->setLinks, &colArray);
89 VERIFY_CBOR_SUCCESS(TAG, cborEncoderResult, "Failed adding setLinks payload");
90 rdPublish = rdPublish->next;
92 cborEncoderResult |= cbor_encoder_close_container(&encoder, &colArray);
93 VERIFY_CBOR_SUCCESS(TAG, cborEncoderResult, "Failed closing collection array");
98 cborEncoderResult |= cbor_encoder_create_map(&encoder, &map, CborIndefiniteLength);
99 VERIFY_CBOR_SUCCESS(TAG, cborEncoderResult, "Failed entering discovery map");
100 cborEncoderResult |= cbor_encoder_close_container(&encoder, &map);
101 VERIFY_CBOR_SUCCESS(TAG, cborEncoderResult, "Failed closing discovery map");
104 if (cborEncoderResult == CborErrorOutOfMemory)
106 *size += encoder.ptr - encoder.end;
110 *size = encoder.ptr - outPayload;
113 return cborEncoderResult;
117 return cborEncoderResult;
120 static int64_t OCTagsPayloadToCbor(OCTagsPayload *tags, CborEncoder *setMap)
123 int64_t cborEncoderResult = CborNoError;
124 cborEncoderResult |= cbor_encoder_create_map(setMap, &tagsMap, CborIndefiniteLength);
125 VERIFY_CBOR_SUCCESS(TAG, cborEncoderResult, "Failed to create tags map");
127 cborEncoderResult |= ConditionalAddTextStringToMap(&tagsMap, OC_RSRVD_DEVICE_NAME, tags->n.deviceName);
128 VERIFY_CBOR_SUCCESS(TAG, cborEncoderResult, "Failed to add OC_RSRVD_DEVICE_NAME in tags map");
130 cborEncoderResult |= ConditionalAddTextStringToMap(&tagsMap, OC_RSRVD_DEVICE_ID,
131 (char *)tags->di.id);
132 VERIFY_CBOR_SUCCESS(TAG, cborEncoderResult, "Failed to add OC_RSRVD_DEVICE_ID in tags map");
134 cborEncoderResult |= ConditionalAddTextStringToMap(&tagsMap, OC_RSRVD_RTS, tags->rts);
135 VERIFY_CBOR_SUCCESS(TAG, cborEncoderResult, "Failed to add OC_RSRVD_RTS in tags map");
137 cborEncoderResult |= ConditionalAddTextStringToMap(&tagsMap, OC_RSRVD_DREL, tags->drel);
138 VERIFY_CBOR_SUCCESS(TAG, cborEncoderResult, "Failed to add OC_RSRVD_DREL in tags map");
140 cborEncoderResult |= ConditionalAddTextStringToMap(&tagsMap, OC_RSRVD_BASE_URI, tags->baseURI);
141 VERIFY_CBOR_SUCCESS(TAG, cborEncoderResult, "Failed to add OC_RSRVD_BASE_URI in tags map");
144 uint64_t value = tags->bitmap;
145 cborEncoderResult |= ConditionalAddIntToMap(&tagsMap, OC_RSRVD_BITMAP, &value);
146 VERIFY_CBOR_SUCCESS(TAG, cborEncoderResult, "Failed to add OC_RSRVD_BITMAP in tags map");
149 cborEncoderResult |= ConditionalAddIntToMap(&tagsMap, OC_RSRVD_HOSTING_PORT, &value);
150 VERIFY_CBOR_SUCCESS(TAG, cborEncoderResult, "Failed to add OC_RSRVD_HOSTING_PORT in tags map");
153 cborEncoderResult |= ConditionalAddIntToMap(&tagsMap, OC_RSRVD_INS, &value);
154 VERIFY_CBOR_SUCCESS(TAG, cborEncoderResult, "Failed to add OC_RSRVD_INS in tags map");
157 cborEncoderResult |= ConditionalAddIntToMap(&tagsMap, OC_RSRVD_TTL, &value);
158 VERIFY_CBOR_SUCCESS(TAG, cborEncoderResult, "Failed to add OC_RSRVD_TTL in tags map");
161 cborEncoderResult |= cbor_encoder_close_container(setMap, &tagsMap);
162 VERIFY_CBOR_SUCCESS(TAG, cborEncoderResult, "Failed to close container");
165 return cborEncoderResult;
168 static int64_t OCLinksPayloadToCbor(OCLinksPayload *rtPtr, CborEncoder *setMap)
170 CborEncoder linksArray;
171 int64_t cborEncoderResult = CborNoError;
173 cborEncoderResult |= cbor_encoder_create_array(setMap, &linksArray, CborIndefiniteLength);
174 VERIFY_CBOR_SUCCESS(TAG, cborEncoderResult, "Failed to create Links array");
178 CborEncoder linksMap;
179 cborEncoderResult |= cbor_encoder_create_map(&linksArray, &linksMap, CborIndefiniteLength);
180 VERIFY_CBOR_SUCCESS(TAG, cborEncoderResult, "Failed to create Links map");
182 cborEncoderResult |= ConditionalAddTextStringToMap(&linksMap, OC_RSRVD_HREF, rtPtr->href);
183 VERIFY_CBOR_SUCCESS(TAG, cborEncoderResult, "Failed to add OC_RSRVD_HREF in Links map");
185 cborEncoderResult|= ConditionalAddTextStringToMap(&linksMap, OC_RSRVD_REL, rtPtr->rel);
186 VERIFY_CBOR_SUCCESS(TAG, cborEncoderResult, "Failed to add OC_RSRVD_REL in Links map");
188 cborEncoderResult |= ConditionalAddTextStringToMap(&linksMap, OC_RSRVD_TITLE, rtPtr->title);
189 VERIFY_CBOR_SUCCESS(TAG, cborEncoderResult, "Failed to add OC_RSRVD_TITLE in Links map");
191 cborEncoderResult |= ConditionalAddTextStringToMap(&linksMap, OC_RSRVD_URI, rtPtr->uri);
192 VERIFY_CBOR_SUCCESS(TAG, cborEncoderResult, "Failed to add OC_RSRVD_URI in Links map");
194 cborEncoderResult |= AddStringLLToMap(&linksMap, OC_RSRVD_RESOURCE_TYPE, rtPtr->rt);
195 VERIFY_CBOR_SUCCESS(TAG, cborEncoderResult, "Failed to add OC_RSRVD_RT in Links map");
197 cborEncoderResult |= AddStringLLToMap(&linksMap, OC_RSRVD_INTERFACE, rtPtr->itf);
198 VERIFY_CBOR_SUCCESS(TAG, cborEncoderResult, "Failed to add OC_RSRVD_ITF in Links map");
200 cborEncoderResult |= AddStringLLToMap(&linksMap, OC_RSRVD_MEDIA_TYPE, rtPtr->mt);
201 VERIFY_CBOR_SUCCESS(TAG, cborEncoderResult, "Failed to add OC_RSRVD_MT in Links map");
204 uint64_t value = rtPtr->ins;
205 cborEncoderResult |= ConditionalAddIntToMap(&linksMap, OC_RSRVD_INS, &value);
206 VERIFY_CBOR_SUCCESS(TAG, cborEncoderResult, "Failed to add OC_RSRVD_INS in Links map");
209 cborEncoderResult |= cbor_encoder_close_container(&linksArray, &linksMap);
210 VERIFY_CBOR_SUCCESS(TAG, cborEncoderResult, "Failed closing Links map");
214 cborEncoderResult |= cbor_encoder_close_container(setMap, &linksArray);
215 VERIFY_CBOR_SUCCESS(TAG, cborEncoderResult, "Failed closing links array");
218 return cborEncoderResult;
221 OCStackResult OCRDCborToPayload(const CborValue *cborPayload, OCPayload **outPayload)
223 CborValue *rdCBORPayload = (CborValue *)cborPayload;
224 OCStackResult ret = OC_STACK_NO_MEMORY;
225 CborError cborFindResult;
227 OCRDPayload *rdPayload = OCRDPayloadCreate();
228 VERIFY_PARAM_NON_NULL(TAG, rdPayload, "Failed allocating rdPayload");
230 ret = OC_STACK_MALFORMED_RESPONSE;
232 if (cbor_value_is_array(rdCBORPayload))
234 OCLinksPayload *linksPayload = NULL;
235 OCTagsPayload *tagsPayload = NULL;
237 while (cbor_value_is_container(rdCBORPayload))
241 cborFindResult = cbor_value_enter_container(rdCBORPayload, &tags);
242 VERIFY_CBOR_SUCCESS(TAG, cborFindResult, "Failed entering tags container.");
244 cborFindResult= OCTagsCborToPayload(&tags, &tagsPayload);
245 VERIFY_CBOR_SUCCESS(TAG, cborFindResult, "Failed parsing tags payload.");
246 OCTagsLog(DEBUG, tagsPayload);
248 cborFindResult = OCLinksCborToPayload(&tags, &linksPayload);
249 VERIFY_CBOR_SUCCESS(TAG, cborFindResult, "Failed parsing links payload.");
250 OCLinksLog(DEBUG, linksPayload);
252 // Move from tags payload to links array.
253 cborFindResult = cbor_value_advance(rdCBORPayload);
254 VERIFY_CBOR_SUCCESS(TAG, cborFindResult, "Failed advancing rdCborPayload.");
256 rdPayload->rdPublish = OCCopyCollectionResource(tagsPayload, linksPayload);
257 VERIFY_PARAM_NON_NULL(TAG, rdPayload->rdPublish, "Failed allocating rdPayload->rdPublish");
259 else if (cbor_value_is_map(rdCBORPayload))
261 rdPayload->rdDiscovery = (OCRDDiscoveryPayload *)OICCalloc(1, sizeof(OCRDDiscoveryPayload));
262 VERIFY_PARAM_NON_NULL(TAG, rdPayload->rdDiscovery, "Failed allocating discoveryPayload");
264 cborFindResult = FindStringInMap(rdCBORPayload, OC_RSRVD_DEVICE_NAME,
265 &rdPayload->rdDiscovery->n.deviceName);
266 VERIFY_CBOR_SUCCESS(TAG, cborFindResult, "Failed finding OC_RSRVD_DEVICE_NAME.");
267 char *deviceId = NULL;
268 cborFindResult = FindStringInMap(rdCBORPayload, OC_RSRVD_DEVICE_ID, &deviceId);
271 memcpy(rdPayload->rdDiscovery->di.id, deviceId, strlen(deviceId));
274 VERIFY_CBOR_SUCCESS(TAG, cborFindResult, "Failed finding OC_RSRVD_DEVICE_ID.");
278 cborFindResult = FindIntInMap(rdCBORPayload, OC_RSRVD_RD_DISCOVERY_SEL, &value);
279 VERIFY_CBOR_SUCCESS(TAG, cborFindResult, "Failed finding OC_RSRVD_RD_DISCOVERY_SEL.");
280 rdPayload->rdDiscovery->sel = value;
283 cborFindResult = cbor_value_advance(rdCBORPayload);
284 VERIFY_CBOR_SUCCESS(TAG, cborFindResult, "Failed advancing rdCBORPayload.");
286 OIC_LOG_PAYLOAD(DEBUG, (OCPayload *) rdPayload);
287 *outPayload = (OCPayload *)rdPayload;
291 OCRDPayloadDestroy(rdPayload);
295 static CborError FindStringInMap(const CborValue *map, const char *tags, char **value)
298 CborError cborFindResult = cbor_value_map_find_value(map, tags, &curVal);
299 if (CborNoError == cborFindResult && cbor_value_is_text_string(&curVal))
302 cborFindResult = cbor_value_dup_text_string(&curVal, value, &len, NULL);
304 return cborFindResult;
307 static CborError FindIntInMap(const CborValue *map, const char *tags, uint64_t *value)
310 CborError cborFindResult = cbor_value_map_find_value(map, tags, &curVal);
311 if (CborNoError == cborFindResult && cbor_value_is_unsigned_integer(&curVal))
313 cborFindResult = cbor_value_get_uint64(&curVal, value);
315 return cborFindResult;
318 static CborError FindStringLLInMap(const CborValue *linksMap, const char *tag, OCStringLL **links)
322 OCStringLL* llPtr = *links;
323 CborError cborFindResult = cbor_value_map_find_value(linksMap, tag, &rtArray);
324 VERIFY_CBOR_SUCCESS(TAG, cborFindResult, "Failed finding tag");
327 cborFindResult = cbor_value_enter_container(&rtArray, &rtVal);
328 VERIFY_CBOR_SUCCESS(TAG, cborFindResult, "Failed entering container");
330 while (cbor_value_is_text_string(&rtVal))
334 llPtr = (OCStringLL *)OICCalloc(1, sizeof(OCStringLL));
335 VERIFY_PARAM_NON_NULL(TAG, llPtr, "Failed allocating OCStringLL");
344 llPtr->next = (OCStringLL *)OICCalloc(1, sizeof(OCStringLL));
345 VERIFY_PARAM_NON_NULL(TAG, llPtr->next, "Failed allocating OCStringLL->next");
347 cborFindResult = cbor_value_dup_text_string(&rtVal, &(llPtr->value), &len, NULL);
348 VERIFY_CBOR_SUCCESS(TAG, cborFindResult, "Failed duplicating value");
349 cborFindResult = cbor_value_advance(&rtVal);
350 VERIFY_CBOR_SUCCESS(TAG, cborFindResult, "Failed advancing OCStringLL");
353 cborFindResult = cbor_value_leave_container(&rtArray, &rtVal);
354 VERIFY_CBOR_SUCCESS(TAG, cborFindResult, "Failed leaving container");
357 return cborFindResult;
360 static CborError OCTagsCborToPayload(CborValue *tagsMap, OCTagsPayload **tagsPayload)
362 CborError cborFindResult = CborErrorOutOfMemory;
363 OCTagsPayload *tags = (OCTagsPayload *)OICCalloc(1, sizeof(OCTagsPayload));
364 VERIFY_PARAM_NON_NULL(TAG, tags, "Failed allocating tags");
366 if (cbor_value_is_map(tagsMap))
368 cborFindResult = FindStringInMap(tagsMap, OC_RSRVD_DEVICE_NAME, &tags->n.deviceName);
369 VERIFY_CBOR_SUCCESS(TAG, cborFindResult, "Failed finding deviceName");
370 cborFindResult = FindStringInMap(tagsMap, OC_RSRVD_DREL, &tags->drel);
371 VERIFY_CBOR_SUCCESS(TAG, cborFindResult, "Failed finding drel");
372 cborFindResult = FindStringInMap(tagsMap, OC_RSRVD_RTS, &tags->rts);
373 VERIFY_CBOR_SUCCESS(TAG, cborFindResult, "Failed finding rts");
374 cborFindResult = FindStringInMap(tagsMap, OC_RSRVD_BASE_URI, &tags->baseURI);
375 VERIFY_CBOR_SUCCESS(TAG, cborFindResult, "Failed finding baseURI");
376 char *deviceId = NULL;
377 cborFindResult = FindStringInMap(tagsMap, OC_RSRVD_DEVICE_ID, &deviceId);
380 memcpy(tags->di.id, deviceId, strlen(deviceId));
383 VERIFY_CBOR_SUCCESS(TAG, cborFindResult, "Failed finding deviceId");
386 cborFindResult = FindIntInMap(tagsMap, OC_RSRVD_HOSTING_PORT, &value);
387 VERIFY_CBOR_SUCCESS(TAG, cborFindResult, "Failed finding port");
390 cborFindResult = FindIntInMap(tagsMap, OC_RSRVD_BITMAP, &value);
391 VERIFY_CBOR_SUCCESS(TAG, cborFindResult, "Failed finding bitmap");
392 tags->bitmap = value;
394 cborFindResult = FindIntInMap(tagsMap, OC_RSRVD_INS, &value);
395 VERIFY_CBOR_SUCCESS(TAG, cborFindResult, "Failed finding ins");
398 cborFindResult = FindIntInMap(tagsMap, OC_RSRVD_TTL, &value);
401 VERIFY_CBOR_SUCCESS(TAG, cborFindResult, "Failed finding ttl");
402 cborFindResult = cbor_value_advance(tagsMap);
403 VERIFY_CBOR_SUCCESS(TAG, cborFindResult, "Failed advancing bitmap");
406 return cborFindResult;
409 OCFreeTagsResource(tags);
410 return cborFindResult;
413 static CborError OCLinksCborToPayload(CborValue *linksArray, OCLinksPayload **linksPayload)
416 OCLinksPayload *setLinks = NULL;
417 CborError cborFindResult = cbor_value_enter_container(linksArray, &linksMap);
418 VERIFY_CBOR_SUCCESS(TAG, cborFindResult, "Failed entering links map container");
420 while (cbor_value_is_map(&linksMap))
422 setLinks = (OCLinksPayload *)OICCalloc(1, sizeof(OCLinksPayload));
423 VERIFY_PARAM_NON_NULL(TAG, setLinks, "Failed allocating setLinks");
425 cborFindResult = FindStringInMap(&linksMap, OC_RSRVD_HREF, &setLinks->href);
426 VERIFY_CBOR_SUCCESS(TAG, cborFindResult, "Failed finding href value");
428 cborFindResult = FindStringInMap(&linksMap, OC_RSRVD_REL, &setLinks->rel);
429 VERIFY_CBOR_SUCCESS(TAG, cborFindResult, "Failed finding rel value");
431 cborFindResult = FindStringInMap(&linksMap, OC_RSRVD_TITLE, &setLinks->title);
432 VERIFY_CBOR_SUCCESS(TAG, cborFindResult, "Failed finding title value");
434 cborFindResult = FindStringInMap(&linksMap, OC_RSRVD_URI, &setLinks->uri);
435 VERIFY_CBOR_SUCCESS(TAG, cborFindResult, "Failed finding uri value");
437 cborFindResult = FindStringLLInMap(&linksMap, OC_RSRVD_RESOURCE_TYPE, &setLinks->rt);
438 VERIFY_CBOR_SUCCESS(TAG, cborFindResult, "Failed finding rt value");
440 cborFindResult = FindStringLLInMap(&linksMap, OC_RSRVD_INTERFACE, &setLinks->itf);
441 VERIFY_CBOR_SUCCESS(TAG, cborFindResult, "Failed finding itf value");
443 cborFindResult = FindStringLLInMap(&linksMap, OC_RSRVD_MEDIA_TYPE, &setLinks->mt);
444 VERIFY_CBOR_SUCCESS(TAG, cborFindResult, "Failed finding mt value");
448 cborFindResult = FindIntInMap(&linksMap, OC_RSRVD_INS, &value);
449 VERIFY_CBOR_SUCCESS(TAG, cborFindResult, "Failed finding ins value");
450 setLinks->ins = value;
455 *linksPayload = setLinks;
459 OCLinksPayload *temp = *linksPayload;
464 temp->next = setLinks;
466 cborFindResult = cbor_value_advance(&linksMap);
467 VERIFY_CBOR_SUCCESS(TAG, cborFindResult, "Failed advancing links map");
470 return cborFindResult;
473 OCFreeLinksResource(*linksPayload);
474 OCFreeLinksResource(setLinks);
475 return cborFindResult;
478 static int64_t AddTextStringToMap(CborEncoder* map, const char* key, const char* value)
480 int64_t err = cbor_encode_text_string(map, key, strlen(key));
481 VERIFY_CBOR_SUCCESS(TAG, err, "Failed setting key value");
482 err |= cbor_encode_text_string(map, value, strlen(value));
487 static int64_t ConditionalAddTextStringToMap(CborEncoder* map, const char* key, const char* value)
489 return value ? AddTextStringToMap(map, key, value) : CborNoError;
492 static int64_t ConditionalAddIntToMap(CborEncoder *map, const char *tags, const uint64_t *value)
494 int64_t err = CborNoError;
497 err |= cbor_encode_text_string(map, tags, strlen(tags));
498 VERIFY_CBOR_SUCCESS(TAG, err, "failed setting value");
499 err |= cbor_encode_uint(map, *value);
505 static int64_t AddStringLLToMap(CborEncoder *map, const char *tag, const OCStringLL *strType)
508 int64_t cborEncoderResult = CborNoError;
509 cborEncoderResult |= cbor_encode_text_string(map, tag, strlen(tag));
510 VERIFY_CBOR_SUCCESS(TAG, cborEncoderResult, "Failed encoding string tag name");
511 cborEncoderResult |= cbor_encoder_create_array(map, &array, CborIndefiniteLength);
512 VERIFY_CBOR_SUCCESS(TAG, cborEncoderResult, "Failed creating stringLL array");
515 cborEncoderResult |= cbor_encode_text_string(&array, strType->value, strlen(strType->value));
516 VERIFY_CBOR_SUCCESS(TAG, cborEncoderResult, "Failed encoding string value");
517 strType = strType->next;
519 cborEncoderResult |= cbor_encoder_close_container(map, &array);
520 VERIFY_CBOR_SUCCESS(TAG, cborEncoderResult, "Failed closing string array");
522 return cborEncoderResult;
525 OCRDPayload *OCRDPayloadCreate()
527 OCRDPayload *rdPayload = (OCRDPayload *)OICCalloc(1, sizeof(OCRDPayload));
528 VERIFY_PARAM_NON_NULL(TAG, rdPayload, "Failed allocating rdPayload");
529 rdPayload->base.type = PAYLOAD_TYPE_RD;
535 OCRDDiscoveryPayload *OCRDDiscoveryPayloadCreate(const char *deviceName, const char *id, int biasFactor)
537 OCRDDiscoveryPayload *discoveryPayload = (OCRDDiscoveryPayload *)OICCalloc(1, sizeof(OCRDDiscoveryPayload));
538 VERIFY_PARAM_NON_NULL(TAG, discoveryPayload, "Failed allocating memory for discovery payload");
542 discoveryPayload->n.deviceName = OICStrdup(deviceName);
543 VERIFY_PARAM_NON_NULL(TAG, discoveryPayload->n.deviceName,
544 "Failed allocating memory for discovery device name");
548 OICStrcpy((char*)discoveryPayload->di.id, MAX_IDENTITY_SIZE, id);
551 discoveryPayload->sel = biasFactor;
552 return discoveryPayload;
554 OICFree(discoveryPayload);
555 discoveryPayload = NULL;
556 return discoveryPayload;
559 void OCRDPayloadDestroy(OCRDPayload *payload)
566 if (payload->rdDiscovery)
568 if (payload->rdDiscovery->n.deviceName)
570 OICFree(payload->rdDiscovery->n.deviceName);
572 OICFree(payload->rdDiscovery);
575 if (payload->rdPublish)
577 for (OCResourceCollectionPayload *col = payload->rdPublish; col; )
581 OCFreeLinksResource(col->setLinks);
586 OCFreeTagsResource(col->tags);
588 OCResourceCollectionPayload *temp = col->next;
597 OCTagsPayload* OCCopyTagsResources(const char *deviceName, const unsigned char *id, const char *baseURI,
598 uint8_t bitmap, uint16_t port, uint8_t ins, const char *rts,const char *drel, uint32_t ttl)
600 OCTagsPayload *tags = (OCTagsPayload *)OICCalloc(1, sizeof(OCTagsPayload));
607 tags->n.deviceName = OICStrdup(deviceName);
608 if (!tags->n.deviceName)
610 goto memory_allocation_failed;
615 OICStrcpy((char*)tags->di.id, MAX_IDENTITY_SIZE, (char *)id);
618 goto memory_allocation_failed;
623 tags->baseURI = OICStrdup(baseURI);
626 goto memory_allocation_failed;
629 tags->bitmap = bitmap;
634 tags->rts = OICStrdup(rts);
637 goto memory_allocation_failed;
642 tags->drel = OICStrdup(drel);
645 goto memory_allocation_failed;
651 memory_allocation_failed:
652 OIC_LOG(ERROR, TAG, "Memory allocation failed.");
653 OCFreeTagsResource(tags);
657 OCLinksPayload* OCCopyLinksResources(const char *href, OCStringLL *rt, OCStringLL *itf,
658 const char *rel, bool obs, const char *title, const char *uri, uint8_t ins, OCStringLL *mt)
660 OCLinksPayload *links = (OCLinksPayload *)OICCalloc(1, sizeof(OCLinksPayload));
663 OIC_LOG(ERROR, TAG, "Failed allocating memory.");
668 links->href = OICStrdup(href);
671 goto memory_allocation_failed;
676 links->rt = CloneOCStringLL(rt);
679 goto memory_allocation_failed;
684 links->itf = CloneOCStringLL(itf);
687 goto memory_allocation_failed;
692 links->rel = OICStrdup(rel);
695 goto memory_allocation_failed;
701 links->title = OICStrdup(title);
704 goto memory_allocation_failed;
709 links->uri = OICStrdup(uri);
712 goto memory_allocation_failed;
718 links->mt = CloneOCStringLL(mt);
721 goto memory_allocation_failed;
727 memory_allocation_failed:
728 OIC_LOG(ERROR, TAG, "Memory allocation failed.");
729 OCFreeLinksResource(links);
733 OCResourceCollectionPayload* OCCopyCollectionResource(OCTagsPayload *tags, OCLinksPayload *links)
735 OCResourceCollectionPayload *pl = NULL;
736 VERIFY_PARAM_NON_NULL(TAG, tags, "Invalid param tags");
737 VERIFY_PARAM_NON_NULL(TAG, links, "Invalid param links");
739 pl = (OCResourceCollectionPayload *)OICCalloc(1, sizeof(OCResourceCollectionPayload));
740 VERIFY_PARAM_NON_NULL(TAG, pl, "Failed allocating memory for the OCResourceCollectionPayload");
743 pl->setLinks = links;
749 void OCFreeLinksResource(OCLinksPayload *payload)
755 OICFree(payload->href);
756 OCFreeOCStringLL(payload->rt);
757 OCFreeOCStringLL(payload->itf);
758 OICFree(payload->rel);
759 OICFree(payload->title);
760 OICFree(payload->uri);
761 OCFreeOCStringLL(payload->mt);
762 OCFreeLinksResource(payload->next);
766 void OCFreeTagsResource(OCTagsPayload *payload)
772 OICFree(payload->n.deviceName);
773 OICFree(payload->baseURI);
774 OICFree(payload->rts);
775 OICFree(payload->drel);
779 void OCFreeCollectionResource(OCResourceCollectionPayload *payload)
787 OCFreeTagsResource(payload->tags);
789 if (payload->setLinks)
791 OCFreeLinksResource(payload->setLinks);
793 OCFreeCollectionResource(payload->next);
797 void OCTagsLog(const LogLevel level, const OCTagsPayload *tags)
801 if (tags->n.deviceName)
803 OIC_LOG_V(level, TAG, " Device Name : %s ",tags->n.deviceName);
807 OIC_LOG_V(level, TAG, " Base URI : %s ",tags->baseURI);
809 OIC_LOG_V(level, TAG, " Device ID : %s ",tags->di.id);
810 OIC_LOG_V(level, TAG, " Bitmap : %d ",tags->bitmap);
811 OIC_LOG_V(level, TAG, " Port : %d ",tags->port);
812 OIC_LOG_V(level, TAG, " Ins : %d ",tags->ins);
813 OIC_LOG_V(level, TAG, " Ttl : %d ",tags->ttl);
817 OIC_LOG_V(level, TAG, " RTS : %s ",tags->rts);
821 OIC_LOG_V(level, TAG, " DREL : %s ",tags->drel);
830 void OCLinksLog(const LogLevel level, const OCLinksPayload *links)
836 OIC_LOG_V(level, TAG, " href: %s ",links->href);
838 OIC_LOG(level, TAG, " RT: ");
839 OCStringLL *rt = links->rt;
844 OIC_LOG_V(level, TAG, " %s", rt->value);
848 OIC_LOG(level, TAG, " IF: ");
849 OCStringLL *itf = links->itf;
854 OIC_LOG_V(level, TAG, " %s", itf->value);
858 OIC_LOG(level, TAG, " MT: ");
859 OCStringLL *mt = links->mt;
864 OIC_LOG_V(level, TAG, " %s", mt->value);
868 OIC_LOG_V(level, TAG, " INS: %d", links->ins);
869 OIC_LOG_V(level, TAG, " OBS: %d", links->obs);
872 OIC_LOG_V(level, TAG, " REL: %s", links->rel);
876 OIC_LOG_V(level, TAG, " TITLE: %s", links->title);
880 OIC_LOG_V(level, TAG, " URI: %s", links->uri);