Add secure and port in policy
[platform/upstream/iotivity.git] / resource / csdk / stack / src / ocresource.c
1 //******************************************************************
2 //
3 // Copyright 2014 Intel Mobile Communications GmbH All Rights Reserved.
4 //
5 //-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
6 //
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
10 //
11 //      http://www.apache.org/licenses/LICENSE-2.0
12 //
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.
18 //
19 //-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
20
21 // Defining _POSIX_C_SOURCE macro with 200112L (or greater) as value
22 // causes header files to expose definitions
23 // corresponding to the POSIX.1-2001 base
24 // specification (excluding the XSI extension).
25 // For POSIX.1-2001 base specification,
26 // Refer http://pubs.opengroup.org/onlinepubs/009695399/
27 #define _POSIX_C_SOURCE 200112L
28 #ifndef _GNU_SOURCE
29 #define _GNU_SOURCE
30 #endif
31
32 #include "iotivity_config.h"
33 #ifdef HAVE_STRING_H
34 #include <string.h>
35 #endif
36 #ifdef HAVE_STRINGS_H
37 #include <strings.h>
38 #endif
39
40 #include "ocresource.h"
41 #include "ocresourcehandler.h"
42 #include "ocobserve.h"
43 #include "occollection.h"
44 #include "oic_malloc.h"
45 #include "oic_string.h"
46 #include "logger.h"
47 #include "cJSON.h"
48 #include "ocpayload.h"
49 #include "secureresourcemanager.h"
50 #include "cacommon.h"
51 #include "cainterface.h"
52 #include "ocpayload.h"
53 #include "platform_features.h"
54 #include "payload_logging.h"
55 #ifdef ROUTING_GATEWAY
56 #include "routingmanager.h"
57 #endif
58
59 #ifdef RD_SERVER
60 #include "rd_database.h"
61 #endif
62
63 /// Module Name
64 #define TAG "OIC_RI_RESOURCE"
65
66 #define VERIFY_SUCCESS(op) { if (op != (OC_STACK_OK)) \
67             {OIC_LOG_V(FATAL, TAG, "%s failed!!", #op); goto exit;} }
68
69 extern OCResource *headResource;
70
71 /**
72  * Prepares a Payload for response.
73  */
74 static OCStackResult BuildVirtualResourceResponse(const OCResource *resourcePtr,
75                                                   OCDiscoveryPayload* payload,
76                                                   OCDevAddr *endpoint);
77
78 //-----------------------------------------------------------------------------
79 // Default resource entity handler function
80 //-----------------------------------------------------------------------------
81 OCEntityHandlerResult defaultResourceEHandler(OCEntityHandlerFlag flag,
82         OCEntityHandlerRequest * request, void* callbackParam)
83 {
84     //TODO ("Implement me!!!!");
85     // TODO:  remove silence unused param warnings
86     (void) flag;
87     (void) request;
88     (void) callbackParam;
89     return  OC_EH_OK; // Making sure that the Default EH and the Vendor EH have matching signatures
90 }
91
92 /* This method will retrieve the port at which the secure resource is hosted */
93 static OCStackResult GetSecurePortInfo(OCDevAddr *endpoint, uint16_t *port)
94 {
95     uint16_t p = 0;
96
97     if (endpoint->adapter == OC_ADAPTER_IP)
98     {
99         if (endpoint->flags & OC_IP_USE_V6)
100         {
101             p = caglobals.ip.u6s.port;
102         }
103         else if (endpoint->flags & OC_IP_USE_V4)
104         {
105             p = caglobals.ip.u4s.port;
106         }
107     }
108
109     *port = p;
110     return OC_STACK_OK;
111 }
112
113 #ifdef TCP_ADAPTER
114 /* This method will retrieve the tcp port */
115 static OCStackResult GetTCPPortInfo(OCDevAddr *endpoint, uint16_t *port)
116 {
117     uint16_t p = 0;
118
119     if (endpoint->adapter == OC_ADAPTER_IP)
120     {
121         if (endpoint->flags & OC_IP_USE_V4)
122         {
123             p = caglobals.tcp.ipv4.port;
124         }
125         else if (endpoint->flags & OC_IP_USE_V6)
126         {
127             p = caglobals.tcp.ipv6.port;
128         }
129     }
130
131     *port = p;
132     return OC_STACK_OK;
133 }
134 #endif
135
136 /*
137  * Function will extract 0, 1 or 2 filters from query.
138  * More than 2 filters or unsupported filters will result in error.
139  * If both filters are of the same supported type, the 2nd one will be picked.
140  * Resource and device filters in the SAME query are NOT validated
141  * and resources will likely not clear filters.
142  */
143 static OCStackResult ExtractFiltersFromQuery(char *query, char **filterOne, char **filterTwo)
144 {
145     char *key = NULL;
146     char *value = NULL;
147     char *queryDup = NULL;
148     char *restOfQuery = NULL;
149     char *keyValuePair = NULL;
150     int numKeyValuePairsParsed = 0;
151
152     *filterOne = NULL;
153     *filterTwo = NULL;
154
155     queryDup = OICStrdup(query);
156     if (NULL == queryDup)
157     {
158         OIC_LOG_V(ERROR, TAG, "Creating duplicate string failed!");
159         return OC_STACK_NO_MEMORY;
160     }
161
162     OIC_LOG_V(INFO, TAG, "Extracting params from %s", queryDup);
163
164     OCStackResult eCode = OC_STACK_INVALID_QUERY;
165     if (strnlen(queryDup, MAX_QUERY_LENGTH) >= MAX_QUERY_LENGTH)
166     {
167         OIC_LOG(ERROR, TAG, "Query exceeds maximum length.");
168         goto exit;
169     }
170
171     keyValuePair = strtok_r (queryDup, OC_QUERY_SEPARATOR, &restOfQuery);
172
173     while(keyValuePair)
174     {
175         if (numKeyValuePairsParsed >= 2)
176         {
177             OIC_LOG(ERROR, TAG, "More than 2 queries params in URI.");
178             goto exit;
179         }
180
181         key = strtok_r(keyValuePair, OC_KEY_VALUE_DELIMITER, &value);
182
183         if (!key || !value)
184         {
185             goto exit;
186         }
187         else if (strncasecmp(key, OC_RSRVD_INTERFACE, sizeof(OC_RSRVD_INTERFACE) - 1) == 0)
188         {
189             *filterOne = value;     // if
190         }
191         else if (strncasecmp(key, OC_RSRVD_RESOURCE_TYPE, sizeof(OC_RSRVD_INTERFACE) - 1) == 0)
192         {
193             *filterTwo = value;     // rt
194         }
195         else
196         {
197             OIC_LOG_V(ERROR, TAG, "Unsupported query key: %s", key);
198             goto exit;
199         }
200         ++numKeyValuePairsParsed;
201
202         keyValuePair = strtok_r(NULL, OC_QUERY_SEPARATOR, &restOfQuery);
203     }
204
205     if (*filterOne)
206     {
207         *filterOne = OICStrdup(*filterOne);
208         if (NULL == *filterOne)
209         {
210             OIC_LOG_V(ERROR, TAG, "Creating duplicate string failed!");
211             eCode = OC_STACK_NO_MEMORY;
212             goto exit;
213         }
214     }
215
216     if (*filterTwo)
217     {
218         *filterTwo = OICStrdup(*filterTwo);
219         if (NULL == *filterTwo)
220         {
221             OIC_LOG_V(ERROR, TAG, "Creating duplicate string failed!");
222             OICFree(*filterOne);
223             eCode = OC_STACK_NO_MEMORY;
224             goto exit;
225         }
226     }
227
228     OICFree(queryDup);
229     OIC_LOG_V(INFO, TAG, "Extracted params if: %s and rt: %s.", *filterOne, *filterTwo);
230     return OC_STACK_OK;
231
232 exit:
233     *filterOne = NULL;
234     *filterTwo = NULL;
235     OICFree(queryDup);
236     return eCode;
237 }
238
239 static OCVirtualResources GetTypeOfVirtualURI(const char *uriInRequest)
240 {
241     if (strcmp(uriInRequest, OC_RSRVD_WELL_KNOWN_URI) == 0)
242     {
243         return OC_WELL_KNOWN_URI;
244     }
245     else if (strcmp(uriInRequest, OC_RSRVD_DEVICE_URI) == 0)
246     {
247         return OC_DEVICE_URI;
248     }
249     else if (strcmp(uriInRequest, OC_RSRVD_PLATFORM_URI) == 0)
250     {
251         return OC_PLATFORM_URI;
252     }
253     else if (strcmp(uriInRequest, OC_RSRVD_RESOURCE_TYPES_URI) == 0)
254     {
255         return OC_RESOURCE_TYPES_URI;
256     }
257 #ifdef ROUTING_GATEWAY
258     else if (0 == strcmp(uriInRequest, OC_RSRVD_GATEWAY_URI))
259     {
260         return OC_GATEWAY_URI;
261     }
262 #endif
263 #ifdef WITH_PRESENCE
264     else if (strcmp(uriInRequest, OC_RSRVD_PRESENCE_URI) == 0)
265     {
266         return OC_PRESENCE;
267     }
268 #endif //WITH_PRESENCE
269
270 #ifdef MQ_BROKER
271     else if (0 == strcmp(uriInRequest, OC_RSRVD_WELL_KNOWN_MQ_URI))
272     {
273         return OC_MQ_BROKER_URI;
274     }
275 #endif //MQ_BROKER
276
277 #ifdef TCP_ADAPTER
278     else if (strcmp(uriInRequest, OC_RSRVD_KEEPALIVE_URI) == 0)
279     {
280         return OC_KEEPALIVE_RESOURCE_URI;
281     }
282 #endif
283
284     return OC_UNKNOWN_URI;
285 }
286
287 static OCStackResult getQueryParamsForFiltering (OCVirtualResources uri, char *query,
288                                             char **filterOne, char **filterTwo)
289 {
290     if(!filterOne || !filterTwo)
291     {
292         return OC_STACK_INVALID_PARAM;
293     }
294
295     *filterOne = NULL;
296     *filterTwo = NULL;
297
298     #ifdef WITH_PRESENCE
299     if (uri == OC_PRESENCE)
300     {
301         //Nothing needs to be done, except for pass a OC_PRESENCE query through as OC_STACK_OK.
302         OIC_LOG(INFO, TAG, "OC_PRESENCE Request for virtual resource.");
303         return OC_STACK_OK;
304     }
305     #endif
306
307     OCStackResult result = OC_STACK_OK;
308
309     if (query && *query)
310     {
311         result = ExtractFiltersFromQuery(query, filterOne, filterTwo);
312     }
313
314     return result;
315 }
316
317 bool appendOCStringLL(OCRepPayload *device, OCStringLL *dmv)
318 {
319     int size = 0;
320     for (OCStringLL *ll = dmv; ll; ll = ll->next, size++);
321     size_t dim[MAX_REP_ARRAY_DEPTH] = {size, 0, 0};
322     char **dt = (char **)OICMalloc(sizeof(char *) * size);
323     int i = 0;
324     VERIFY_PARAM_NON_NULL(TAG, dt, "Data Model Version allocation failed.");
325     for (OCStringLL *ll = dmv; ll; ll = ll->next, i++)
326     {
327         dt[i] = OICStrdup(ll->value);
328         VERIFY_PARAM_NON_NULL(TAG, dt[i], "Data Model Version adding failed.");
329     }
330     if (!OCRepPayloadSetStringArrayAsOwner(device, OC_RSRVD_DATA_MODEL_VERSION, dt, dim))
331     {
332         goto exit;
333     }
334     return true;
335
336 exit:
337     for (int i = 0; i < size; i++)
338     {
339         OICFree(dt[i]);
340     }
341     OICFree(dt);
342     return false;
343 }
344
345 OCStackResult BuildResponseRepresentation(const OCResource *resourcePtr,
346                     OCRepPayload** payload, OCDevAddr *devAddr)
347 {
348     OCRepPayload *tempPayload = OCRepPayloadCreate();
349
350     if (!resourcePtr)
351     {
352         OCRepPayloadDestroy(tempPayload);
353         return OC_STACK_INVALID_PARAM;
354     }
355
356     if(!tempPayload)
357     {
358         return OC_STACK_NO_MEMORY;
359     }
360
361     OCRepPayloadSetUri(tempPayload, resourcePtr->uri);
362
363     OCResourceType *resType = resourcePtr->rsrcType;
364     while(resType)
365     {
366         OCRepPayloadAddResourceType(tempPayload, resType->resourcetypename);
367         resType = resType->next;
368     }
369
370     OCResourceInterface *resInterface = resourcePtr->rsrcInterface;
371     while(resInterface)
372     {
373         OCRepPayloadAddInterface(tempPayload, resInterface->name);
374         resInterface = resInterface->next;
375     }
376
377     OCAttribute *resAttrib = resourcePtr->rsrcAttributes;
378     while(resAttrib)
379     {
380         if (resAttrib->attrName && resAttrib->attrValue)
381         {
382             if (0 == strcmp(OC_RSRVD_DATA_MODEL_VERSION, resAttrib->attrName))
383             {
384                 appendOCStringLL(tempPayload, (OCStringLL *)resAttrib->attrValue);
385             }
386             else
387             {
388                 OCRepPayloadSetPropString(tempPayload, resAttrib->attrName, (char *)resAttrib->attrValue);
389             }
390         }
391         resAttrib = resAttrib->next;
392     }
393
394     OCResourceProperty p = OCGetResourceProperties((OCResourceHandle *)resourcePtr);
395     OCRepPayload *policy = OCRepPayloadCreate();
396     if (!policy)
397     {
398         OCPayloadDestroy((OCPayload *)tempPayload);
399         return OC_STACK_NO_MEMORY;
400     }
401     OCRepPayloadSetPropInt(policy, OC_RSRVD_BITMAP, ((p & OC_DISCOVERABLE) | (p & OC_OBSERVABLE)));
402     if (p & OC_SECURE)
403     {
404         OCRepPayloadSetPropBool(policy, OC_RSRVD_SECURE, p & OC_SECURE);
405         uint16_t securePort = 0;
406         if (GetSecurePortInfo(devAddr, &securePort) != OC_STACK_OK)
407         {
408             securePort = 0;
409         }
410         OCRepPayloadSetPropInt(policy, OC_RSRVD_HOSTING_PORT, securePort);
411     }
412     OCRepPayloadSetPropObjectAsOwner(tempPayload, OC_RSRVD_POLICY, policy);
413
414     if(!*payload)
415     {
416         *payload = tempPayload;
417     }
418     else
419     {
420         OCRepPayloadAppend(*payload, tempPayload);
421     }
422
423     return OC_STACK_OK;
424 }
425
426 OCStackResult BuildVirtualResourceResponse(const OCResource *resourcePtr,
427                         OCDiscoveryPayload *payload, OCDevAddr *devAddr)
428 {
429     if (!resourcePtr || !payload)
430     {
431         return OC_STACK_INVALID_PARAM;
432     }
433     uint16_t securePort = 0;
434     if (resourcePtr->resourceProperties & OC_SECURE)
435     {
436        if (GetSecurePortInfo(devAddr, &securePort) != OC_STACK_OK)
437        {
438            securePort = 0;
439        }
440     }
441
442 #ifdef TCP_ADAPTER
443     uint16_t tcpPort = 0;
444     if (GetTCPPortInfo(devAddr, &tcpPort) != OC_STACK_OK)
445     {
446         tcpPort = 0;
447     }
448     OCDiscoveryPayloadAddResource(payload, resourcePtr, securePort, tcpPort);
449 #else
450     OCDiscoveryPayloadAddResource(payload, resourcePtr, securePort);
451 #endif
452
453     return OC_STACK_OK;
454 }
455
456 uint8_t IsCollectionResource (OCResource *resource)
457 {
458     if(!resource)
459     {
460         return 0;
461     }
462
463     if(resource->rsrcChildResourcesHead != NULL)
464     {
465         return 1;
466     }
467
468     return 0;
469 }
470
471 OCResource *FindResourceByUri(const char* resourceUri)
472 {
473     if(!resourceUri)
474     {
475         return NULL;
476     }
477
478     OCResource * pointer = headResource;
479     while (pointer)
480     {
481         if (strcmp(resourceUri, pointer->uri) == 0)
482         {
483             return pointer;
484         }
485         pointer = pointer->next;
486     }
487     OIC_LOG_V(INFO, TAG, "Resource %s not found", resourceUri);
488     return NULL;
489 }
490
491
492 OCStackResult DetermineResourceHandling (const OCServerRequest *request,
493                                          ResourceHandling *handling,
494                                          OCResource **resource)
495 {
496     if(!request || !handling || !resource)
497     {
498         return OC_STACK_INVALID_PARAM;
499     }
500
501     OIC_LOG_V(INFO, TAG, "DetermineResourceHandling for %s", request->resourceUrl);
502
503     // Check if virtual resource
504     if (GetTypeOfVirtualURI(request->resourceUrl) != OC_UNKNOWN_URI)
505     {
506         OIC_LOG_V (INFO, TAG, "%s is virtual", request->resourceUrl);
507         *handling = OC_RESOURCE_VIRTUAL;
508         *resource = headResource;
509         return OC_STACK_OK;
510     }
511     if (strlen((const char*)(request->resourceUrl)) == 0)
512     {
513         // Resource URL not specified
514         *handling = OC_RESOURCE_NOT_SPECIFIED;
515         return OC_STACK_NO_RESOURCE;
516     }
517     else
518     {
519         OCResource *resourcePtr = FindResourceByUri((const char*)request->resourceUrl);
520         *resource = resourcePtr;
521         if (!resourcePtr)
522         {
523             if(defaultDeviceHandler)
524             {
525                 *handling = OC_RESOURCE_DEFAULT_DEVICE_ENTITYHANDLER;
526                 return OC_STACK_OK;
527             }
528
529             // Resource does not exist
530             // and default device handler does not exist
531             *handling = OC_RESOURCE_NOT_SPECIFIED;
532             return OC_STACK_NO_RESOURCE;
533         }
534
535         if (IsCollectionResource (resourcePtr))
536         {
537             // Collection resource
538             if (resourcePtr->entityHandler != defaultResourceEHandler)
539             {
540                 *handling = OC_RESOURCE_COLLECTION_WITH_ENTITYHANDLER;
541                 return OC_STACK_OK;
542             }
543             else
544             {
545                 *handling = OC_RESOURCE_COLLECTION_DEFAULT_ENTITYHANDLER;
546                 return OC_STACK_OK;
547             }
548         }
549         else
550         {
551             // Resource not a collection
552             if (resourcePtr->entityHandler != defaultResourceEHandler)
553             {
554                 *handling = OC_RESOURCE_NOT_COLLECTION_WITH_ENTITYHANDLER;
555                 return OC_STACK_OK;
556             }
557             else
558             {
559                 *handling = OC_RESOURCE_NOT_COLLECTION_DEFAULT_ENTITYHANDLER;
560                 return OC_STACK_OK;
561             }
562         }
563     }
564 }
565
566 OCStackResult EntityHandlerCodeToOCStackCode(OCEntityHandlerResult ehResult)
567 {
568     OCStackResult result;
569
570     switch (ehResult)
571     {
572         case OC_EH_OK:
573         case OC_EH_CONTENT:
574         case OC_EH_VALID:
575             result = OC_STACK_OK;
576             break;
577         case OC_EH_SLOW:
578             result = OC_STACK_SLOW_RESOURCE;
579             break;
580         case OC_EH_ERROR:
581             result = OC_STACK_ERROR;
582             break;
583         case OC_EH_FORBIDDEN:
584             result = OC_STACK_FORBIDDEN_REQ;
585             break;
586         case OC_EH_INTERNAL_SERVER_ERROR:
587             result = OC_STACK_INTERNAL_SERVER_ERROR;
588             break;
589         case OC_EH_RESOURCE_CREATED:
590             result = OC_STACK_RESOURCE_CREATED;
591             break;
592         case OC_EH_RESOURCE_DELETED:
593             result = OC_STACK_RESOURCE_DELETED;
594             break;
595         case OC_EH_CHANGED:
596             result = OC_STACK_RESOURCE_CHANGED;
597             break;
598         case OC_EH_RESOURCE_NOT_FOUND:
599             result = OC_STACK_NO_RESOURCE;
600             break;
601         default:
602             result = OC_STACK_ERROR;
603     }
604
605     return result;
606 }
607
608 static bool resourceMatchesRTFilter(OCResource *resource, char *resourceTypeFilter)
609 {
610     if (!resource)
611     {
612         return false;
613     }
614
615     // Null or empty is analogous to no filter.
616     if (resourceTypeFilter == NULL || *resourceTypeFilter == 0)
617     {
618         return true;
619     }
620
621     OCResourceType *resourceTypePtr = resource->rsrcType;
622
623     while (resourceTypePtr)
624     {
625         if (strcmp (resourceTypePtr->resourcetypename, resourceTypeFilter) == 0)
626         {
627             return true;
628         }
629         resourceTypePtr = resourceTypePtr->next;
630     }
631
632     OIC_LOG_V(INFO, TAG, "%s does not contain rt=%s.", resource->uri, resourceTypeFilter);
633     return false;
634 }
635
636 static bool resourceMatchesIFFilter(OCResource *resource, char *interfaceFilter)
637 {
638     if (!resource)
639     {
640         return false;
641     }
642
643     // Null or empty is analogous to no filter.
644     if (interfaceFilter == NULL || *interfaceFilter == 0)
645     {
646         return true;
647     }
648
649     OCResourceInterface *interfacePtr = resource->rsrcInterface;
650
651     while (interfacePtr)
652     {
653         if (strcmp (interfacePtr->name, interfaceFilter) == 0 ||
654             strcmp (OC_RSRVD_INTERFACE_LL, interfaceFilter) == 0 ||
655             strcmp (OC_RSRVD_INTERFACE_DEFAULT, interfaceFilter) == 0)
656         {
657             return true;
658         }
659         interfacePtr = interfacePtr->next;
660     }
661
662     OIC_LOG_V(INFO, TAG, "%s does not contain if=%s.", resource->uri, interfaceFilter);
663     return false;
664 }
665
666 /*
667  * If the filters are null, they will be assumed to NOT be present
668  * and the resource will not be matched against them.
669  * Function will return true if all non null AND non empty filters passed in find a match.
670  */
671 static bool includeThisResourceInResponse(OCResource *resource,
672                                           char *interfaceFilter,
673                                           char *resourceTypeFilter)
674 {
675     if (!resource)
676     {
677         OIC_LOG(ERROR, TAG, "Invalid resource");
678         return false;
679     }
680
681     if (resource->resourceProperties & OC_EXPLICIT_DISCOVERABLE)
682     {
683         /*
684          * At least one valid filter should be available to
685          * include the resource in discovery response
686          */
687         if (!(resourceTypeFilter && *resourceTypeFilter))
688         {
689             OIC_LOG_V(INFO, TAG, "%s no query string for EXPLICIT_DISCOVERABLE \
690                 resource", resource->uri);
691             return false;
692         }
693     }
694     else if ( !(resource->resourceProperties & OC_ACTIVE) ||
695          !(resource->resourceProperties & OC_DISCOVERABLE))
696     {
697         OIC_LOG_V(INFO, TAG, "%s not ACTIVE or DISCOVERABLE", resource->uri);
698         return false;
699     }
700
701     return resourceMatchesIFFilter(resource, interfaceFilter) &&
702            resourceMatchesRTFilter(resource, resourceTypeFilter);
703
704 }
705
706 OCStackResult SendNonPersistantDiscoveryResponse(OCServerRequest *request, OCResource *resource,
707                                 OCPayload *discoveryPayload, OCEntityHandlerResult ehResult)
708 {
709     OCEntityHandlerResponse response = {0};
710
711     response.ehResult = ehResult;
712     response.payload = discoveryPayload;
713     response.persistentBufferFlag = 0;
714     response.requestHandle = (OCRequestHandle) request;
715     response.resourceHandle = (OCResourceHandle) resource;
716
717     return OCDoResponse(&response);
718 }
719 #ifdef RD_SERVER
720 /**
721  * Find resource at the resource directory server. This resource is not local resource but a
722  * remote resource.
723  *
724  * @param resource The resource to check the matching resource URI.
725  * @param interfaceQuery The interface query parameter.
726  * @param resourceTypeQuery The resourceType query parameter.
727  * @param discPayload The payload that will be added with the resource information if found at RD.
728  *
729  * @return ::OC_STACK_OK if the resource is found else ::OC_STACK_NO_RESOURCE.
730  * In case if build is not with flag RD_SERVER, it returns ::OC_STACK_NO_RESOURCE.
731  */
732 static OCStackResult findResourceAtRD(const OCResource* resource, const char *interfaceQuery,
733     const char *resourceTypeQuery, OCDiscoveryPayload *discPayload)
734 {
735     if (strcmp(resource->uri, OC_RSRVD_RD_URI) == 0)
736     {
737         if (OC_STACK_OK == OCRDDatabaseCheckResources(interfaceQuery, resourceTypeQuery, discPayload))
738         {
739             return OC_STACK_OK;
740         }
741     }
742
743     return OC_STACK_NO_RESOURCE;
744 }
745 #endif
746
747 /**
748  * Creates a discovery payload and add device id information. This information is included in all
749  * /oic/res response.
750  *
751  * @param payload  payload that will have memory alllocated and device id information added.
752  *
753  * @return ::OC_STACK_OK if successful in allocating memory and adding ID information.
754  * ::OC_STACK_NO_MEMORY if failed allocating the memory.
755  */
756 static OCStackResult discoveryPayloadCreateAndAddDeviceId(OCPayload **payload)
757 {
758     if (*payload)
759     {
760         OIC_LOG_V(DEBUG, TAG, "Payload is already allocated");
761         return OC_STACK_OK;
762     }
763
764     *payload = (OCPayload *) OCDiscoveryPayloadCreate();
765     VERIFY_PARAM_NON_NULL(TAG, *payload, "Failed adding device id to discovery payload.");
766
767     {
768         OCDiscoveryPayload *discPayload = (OCDiscoveryPayload *)*payload;
769         discPayload->sid = (char *)OICCalloc(1, UUID_STRING_SIZE);
770         VERIFY_PARAM_NON_NULL(TAG, discPayload->sid, "Failed adding device id to discovery payload.");
771
772         const char* uid = OCGetServerInstanceIDString();
773         if (uid)
774         {
775             memcpy(discPayload->sid, uid, UUID_STRING_SIZE);
776         }
777
778     }
779     return OC_STACK_OK;
780 exit:
781     OCPayloadDestroy(*payload);
782     return OC_STACK_NO_MEMORY;
783 }
784
785 /**
786  * Add the common properties to the payload, they are only included in case of oic.if.baseline response.
787  *
788  * @param discPayload payload that will have the baseline information included.
789  *
790  * @return ::OC_STACK_OK if successful in adding all the information. ::OC_STACK_NO_MEMORY if failed
791  * allocating the memory for the baseline information.
792  */
793 static OCStackResult addDiscoveryBaselineCommonProperties(OCDiscoveryPayload *discPayload)
794 {
795     discPayload->uri = OICStrdup(OC_RSRVD_WELL_KNOWN_URI);
796     VERIFY_PARAM_NON_NULL(TAG, discPayload->uri, "Failed adding href to discovery payload.");
797
798     OCGetPropertyValue(PAYLOAD_TYPE_DEVICE, "deviceName", (void **)&discPayload->name);
799     VERIFY_PARAM_NON_NULL(TAG, discPayload->name, "Failed adding name to discovery payload.");
800
801     discPayload->type = (OCStringLL*)OICCalloc(1, sizeof(OCStringLL));
802     VERIFY_PARAM_NON_NULL(TAG, discPayload->type, "Failed adding rt to discovery payload.");
803     discPayload->type->value = OICStrdup(OC_RSRVD_RESOURCE_TYPE_RES);
804     VERIFY_PARAM_NON_NULL(TAG, discPayload->type, "Failed adding rt value to discovery payload.");
805
806     OCResourcePayloadAddStringLL(&discPayload->iface, OC_RSRVD_INTERFACE_LL);
807     OCResourcePayloadAddStringLL(&discPayload->iface, OC_RSRVD_INTERFACE_DEFAULT);
808     VERIFY_PARAM_NON_NULL(TAG, discPayload->iface, "Failed adding if to discovery payload.");
809
810     return OC_STACK_OK;
811
812 exit:
813     return OC_STACK_NO_MEMORY;
814 }
815
816 static OCStackResult HandleVirtualResource (OCServerRequest *request, OCResource* resource)
817 {
818     if (!request || !resource)
819     {
820         return OC_STACK_INVALID_PARAM;
821     }
822
823     OCStackResult discoveryResult = OC_STACK_ERROR;
824     if (request->method == OC_REST_PUT || request->method == OC_REST_POST ||
825         request->method == OC_REST_DELETE)
826     {
827         OIC_LOG_V(ERROR, TAG, "Resource : %s not permitted for method: %d",
828             request->resourceUrl, request->method);
829         return OC_STACK_UNAUTHORIZED_REQ;
830     }
831
832     OCPayload* payload = NULL;
833     char *interfaceQuery = NULL;
834     char *resourceTypeQuery = NULL;
835     char *dataModelVersions = NULL;
836
837     OIC_LOG(INFO, TAG, "Entering HandleVirtualResource");
838
839     OCVirtualResources virtualUriInRequest = GetTypeOfVirtualURI (request->resourceUrl);
840
841     // Step 1: Generate the response to discovery request
842     if (virtualUriInRequest == OC_WELL_KNOWN_URI
843 #ifdef MQ_BROKER
844             || virtualUriInRequest == OC_MQ_BROKER_URI
845 #endif
846             )
847     {
848         discoveryResult = getQueryParamsForFiltering (virtualUriInRequest, request->query,
849                 &interfaceQuery, &resourceTypeQuery);
850         VERIFY_SUCCESS(discoveryResult);
851         if (!interfaceQuery && !resourceTypeQuery)
852         {
853             // If no query is sent, default interface is used i.e. oic.if.ll.
854             interfaceQuery = OICStrdup(OC_RSRVD_INTERFACE_LL);
855         }
856
857         bool baselineQuery = false;
858         if (interfaceQuery && 0 != strcmp(interfaceQuery, OC_RSRVD_INTERFACE_LL))
859         {
860             baselineQuery = true;
861         }
862
863         discoveryResult = discoveryPayloadCreateAndAddDeviceId(&payload);
864         VERIFY_PARAM_NON_NULL(TAG, payload, "Failed creating Discovery Payload.");
865         VERIFY_SUCCESS(discoveryResult);
866
867         OCDiscoveryPayload *discPayload = (OCDiscoveryPayload *)payload;
868         if (baselineQuery)
869         {
870             discoveryResult = addDiscoveryBaselineCommonProperties(discPayload);
871             VERIFY_SUCCESS(discoveryResult);
872         }
873         OCResourceProperty prop = OC_DISCOVERABLE;
874 #ifdef MQ_BROKER
875         prop = (OC_MQ_BROKER_URI == virtualUriInRequest) ? OC_MQ_BROKER : prop;
876 #endif
877         for (; resource && discoveryResult == OC_STACK_OK; resource = resource->next)
878         {
879             discoveryResult = OC_STACK_NO_RESOURCE;
880 #ifdef RD_SERVER
881             discoveryResult = findResourceAtRD(resource, interfaceQuery, resourceTypeQuery,
882                 discPayload);
883 #endif
884             if (OC_STACK_NO_RESOURCE == discoveryResult)
885             {
886                 if ((!baselineQuery && (resource->resourceProperties & prop)) ||
887                     (baselineQuery && (includeThisResourceInResponse(resource, interfaceQuery,
888                                                                      resourceTypeQuery))))
889                 {
890                     discoveryResult = BuildVirtualResourceResponse(resource, discPayload,
891                         &request->devAddr);
892                 }
893                 else
894                 {
895                     discoveryResult = OC_STACK_OK;
896                 }
897             }
898         }
899         if (discPayload->resources == NULL)
900         {
901             discoveryResult = OC_STACK_NO_RESOURCE;
902         }
903     }
904     else if (virtualUriInRequest == OC_DEVICE_URI)
905     {
906         OCResource *resourcePtr = FindResourceByUri(OC_RSRVD_DEVICE_URI);
907         VERIFY_PARAM_NON_NULL(TAG, resourcePtr, "Device URI not found.");
908         discoveryResult = BuildResponseRepresentation(resourcePtr, (OCRepPayload **)&payload, &request->devAddr);
909     }
910     else if (virtualUriInRequest == OC_PLATFORM_URI)
911     {
912         OCResource *resourcePtr = FindResourceByUri(OC_RSRVD_PLATFORM_URI);
913         VERIFY_PARAM_NON_NULL(TAG, resourcePtr, "Platform URI not found.");
914         discoveryResult = BuildResponseRepresentation(resourcePtr, (OCRepPayload **)&payload, &request->devAddr);
915     }
916 #ifdef ROUTING_GATEWAY
917     else if (OC_GATEWAY_URI == virtualUriInRequest)
918     {
919         // Received request for a gateway
920         OIC_LOG(INFO, TAG, "Request is for Gateway Virtual Request");
921         discoveryResult = RMHandleGatewayRequest(request, resource);
922     }
923 #endif
924 #ifdef TCP_ADAPTER
925     else if (OC_KEEPALIVE_RESOURCE_URI == virtualUriInRequest)
926     {
927         // Received request for a keepalive
928         OIC_LOG(INFO, TAG, "Request is for KeepAlive Request");
929         discoveryResult = HandleKeepAliveRequest(request, resource);
930     }
931 #endif
932     /**
933      * Step 2: Send the discovery response
934      *
935      * Iotivity should respond to discovery requests in below manner:
936      * 1)If query filter matching fails and discovery request is multicast,
937      *   it should NOT send any response.
938      * 2)If query filter matching fails and discovery request is unicast,
939      *   it should send an error(RESOURCE_NOT_FOUND - 404) response.
940      * 3)If Server does not have any 'DISCOVERABLE' resources and discovery
941      *   request is multicast, it should NOT send any response.
942      * 4)If Server does not have any 'DISCOVERABLE' resources and discovery
943      *   request is unicast, it should send an error(RESOURCE_NOT_FOUND - 404) response.
944      */
945
946 #ifdef WITH_PRESENCE
947     if ((virtualUriInRequest == OC_PRESENCE) &&
948         (resource->resourceProperties & OC_ACTIVE))
949     {
950         // Need to send ACK when the request is CON.
951         if (request->qos == OC_HIGH_QOS)
952         {
953             CAEndpoint_t endpoint = { .adapter = CA_DEFAULT_ADAPTER };
954             CopyDevAddrToEndpoint(&request->devAddr, &endpoint);
955             SendDirectStackResponse(&endpoint, request->coapID, CA_EMPTY, CA_MSG_ACKNOWLEDGE,
956                                     0, NULL, NULL, 0, NULL, CA_RESPONSE_FOR_RES);
957         }
958         FindAndDeleteServerRequest(request);
959
960         // Presence uses observer notification api to respond via SendPresenceNotification.
961         SendPresenceNotification(resource->rsrcType, OC_PRESENCE_TRIGGER_CHANGE);
962     }
963     else
964 #endif
965 #if ROUTING_GATEWAY
966     // Gateway uses the RMHandleGatewayRequest to respond to the request.
967     if (OC_GATEWAY_URI != virtualUriInRequest)
968 #endif
969     {
970 #if TCP_ADAPTER
971         // KeepAlive uses the HandleKeepAliveRequest to respond to the request.
972         if (OC_KEEPALIVE_RESOURCE_URI != virtualUriInRequest)
973 #endif
974         {
975             OIC_LOG_PAYLOAD(DEBUG, payload);
976             if(discoveryResult == OC_STACK_OK)
977             {
978                 SendNonPersistantDiscoveryResponse(request, resource, payload, OC_EH_OK);
979             }
980             else if(((request->devAddr.flags &  OC_MULTICAST) == false) &&
981                 (request->devAddr.adapter != OC_ADAPTER_RFCOMM_BTEDR) &&
982                 (request->devAddr.adapter != OC_ADAPTER_GATT_BTLE))
983             {
984                 OIC_LOG_V(ERROR, TAG, "Sending a (%d) error to (%d) discovery request",
985                     discoveryResult, virtualUriInRequest);
986                 SendNonPersistantDiscoveryResponse(request, resource, NULL,
987                     (discoveryResult == OC_STACK_NO_RESOURCE) ?
988                             OC_EH_RESOURCE_NOT_FOUND : OC_EH_ERROR);
989             }
990             else
991             {
992                 // Ignoring the discovery request as per RFC 7252, Section #8.2
993                 OIC_LOG(INFO, TAG, "Silently ignoring the request since no useful data to send.");
994                 // the request should be removed.
995                 // since it never remove and causes a big memory waste.
996                 FindAndDeleteServerRequest(request);
997             }
998         }
999     }
1000
1001 exit:
1002     if (interfaceQuery)
1003     {
1004         OICFree(interfaceQuery);
1005     }
1006
1007     if (resourceTypeQuery)
1008     {
1009         OICFree(resourceTypeQuery);
1010     }
1011     OCPayloadDestroy(payload);
1012     if (dataModelVersions)
1013     {
1014         OICFree(dataModelVersions);
1015     }
1016     return discoveryResult;
1017 }
1018
1019 static OCStackResult
1020 HandleDefaultDeviceEntityHandler (OCServerRequest *request)
1021 {
1022     if(!request)
1023     {
1024         return OC_STACK_INVALID_PARAM;
1025     }
1026
1027     OCStackResult result = OC_STACK_OK;
1028     OCEntityHandlerResult ehResult = OC_EH_ERROR;
1029     OCEntityHandlerRequest ehRequest = {0};
1030
1031     OIC_LOG(INFO, TAG, "Entering HandleResourceWithDefaultDeviceEntityHandler");
1032     result = FormOCEntityHandlerRequest(&ehRequest,
1033                                         (OCRequestHandle) request,
1034                                         request->method,
1035                                         &request->devAddr,
1036                                         (OCResourceHandle) NULL, request->query,
1037                                         PAYLOAD_TYPE_REPRESENTATION,
1038                                         request->payload,
1039                                         request->payloadSize,
1040                                         request->numRcvdVendorSpecificHeaderOptions,
1041                                         request->rcvdVendorSpecificHeaderOptions,
1042                                         (OCObserveAction)request->observationOption,
1043                                         (OCObservationId)0,
1044                                         request->coapID);
1045     VERIFY_SUCCESS(result);
1046
1047     // At this point we know for sure that defaultDeviceHandler exists
1048     ehResult = defaultDeviceHandler(OC_REQUEST_FLAG, &ehRequest,
1049                                   (char*) request->resourceUrl, defaultDeviceHandlerCallbackParameter);
1050     if(ehResult == OC_EH_SLOW)
1051     {
1052         OIC_LOG(INFO, TAG, "This is a slow resource");
1053         request->slowFlag = 1;
1054     }
1055     else if(ehResult == OC_EH_ERROR)
1056     {
1057         FindAndDeleteServerRequest(request);
1058     }
1059     result = EntityHandlerCodeToOCStackCode(ehResult);
1060 exit:
1061     OCPayloadDestroy(ehRequest.payload);
1062     return result;
1063 }
1064
1065 static OCStackResult
1066 HandleResourceWithEntityHandler (OCServerRequest *request,
1067                                  OCResource *resource,
1068                                  uint8_t collectionResource)
1069 {
1070     OC_UNUSED(collectionResource);
1071
1072     if(!request || ! resource)
1073     {
1074         return OC_STACK_INVALID_PARAM;
1075     }
1076
1077     OCStackResult result = OC_STACK_ERROR;
1078     OCEntityHandlerResult ehResult = OC_EH_ERROR;
1079     OCEntityHandlerFlag ehFlag = OC_REQUEST_FLAG;
1080     ResourceObserver *resObs = NULL;
1081
1082     OCEntityHandlerRequest ehRequest = {0};
1083
1084     OIC_LOG(INFO, TAG, "Entering HandleResourceWithEntityHandler");
1085     OCPayloadType type = PAYLOAD_TYPE_REPRESENTATION;
1086     // check the security resource
1087     if (request && request->resourceUrl && SRMIsSecurityResourceURI(request->resourceUrl))
1088     {
1089         type = PAYLOAD_TYPE_SECURITY;
1090
1091     }
1092
1093     result = FormOCEntityHandlerRequest(&ehRequest,
1094                                         (OCRequestHandle)request,
1095                                         request->method,
1096                                         &request->devAddr,
1097                                         (OCResourceHandle)resource,
1098                                         request->query,
1099                                         type,
1100                                         request->payload,
1101                                         request->payloadSize,
1102                                         request->numRcvdVendorSpecificHeaderOptions,
1103                                         request->rcvdVendorSpecificHeaderOptions,
1104                                         (OCObserveAction)request->observationOption,
1105                                         0,
1106                                         request->coapID);
1107     VERIFY_SUCCESS(result);
1108
1109     if(ehRequest.obsInfo.action == OC_OBSERVE_NO_OPTION)
1110     {
1111         OIC_LOG(INFO, TAG, "No observation requested");
1112         ehFlag = OC_REQUEST_FLAG;
1113     }
1114     else if(ehRequest.obsInfo.action == OC_OBSERVE_REGISTER)
1115     {
1116         OIC_LOG(INFO, TAG, "Observation registration requested");
1117
1118         ResourceObserver *obs = GetObserverUsingToken (request->requestToken,
1119                                     request->tokenLength);
1120
1121         if (obs)
1122         {
1123             OIC_LOG (INFO, TAG, "Observer with this token already present");
1124             OIC_LOG (INFO, TAG, "Possibly re-transmitted CON OBS request");
1125             OIC_LOG (INFO, TAG, "Not adding observer. Not responding to client");
1126             OIC_LOG (INFO, TAG, "The first request for this token is already ACKED.");
1127
1128             // server requests are usually free'd when the response is sent out
1129             // for the request in ocserverrequest.c : HandleSingleResponse()
1130             // Since we are making an early return and not responding, the server request
1131             // needs to be deleted.
1132             FindAndDeleteServerRequest (request);
1133             return OC_STACK_OK;
1134         }
1135
1136         result = GenerateObserverId(&ehRequest.obsInfo.obsId);
1137         VERIFY_SUCCESS(result);
1138
1139         result = AddObserver ((const char*)(request->resourceUrl),
1140                 (const char *)(request->query),
1141                 ehRequest.obsInfo.obsId, request->requestToken, request->tokenLength,
1142                 resource, request->qos, request->acceptFormat,
1143                 &request->devAddr);
1144
1145         if(result == OC_STACK_OK)
1146         {
1147             OIC_LOG(INFO, TAG, "Added observer successfully");
1148             request->observeResult = OC_STACK_OK;
1149             ehFlag = (OCEntityHandlerFlag)(OC_REQUEST_FLAG | OC_OBSERVE_FLAG);
1150         }
1151         else if (result == OC_STACK_RESOURCE_ERROR)
1152         {
1153             OIC_LOG(INFO, TAG, "The Resource is not active, discoverable or observable");
1154             request->observeResult = OC_STACK_ERROR;
1155             ehFlag = OC_REQUEST_FLAG;
1156         }
1157         else
1158         {
1159             // The error in observeResult for the request will be used when responding to this
1160             // request by omitting the observation option/sequence number.
1161             request->observeResult = OC_STACK_ERROR;
1162             OIC_LOG(ERROR, TAG, "Observer Addition failed");
1163             ehFlag = OC_REQUEST_FLAG;
1164             FindAndDeleteServerRequest(request);
1165             goto exit;
1166         }
1167
1168     }
1169     else if(ehRequest.obsInfo.action == OC_OBSERVE_DEREGISTER)
1170     {
1171         OIC_LOG(INFO, TAG, "Deregistering observation requested");
1172
1173         resObs = GetObserverUsingToken (request->requestToken, request->tokenLength);
1174
1175         if (NULL == resObs)
1176         {
1177             // Stack does not contain this observation request
1178             // Either token is incorrect or observation list is corrupted
1179             result = OC_STACK_ERROR;
1180             goto exit;
1181         }
1182         ehRequest.obsInfo.obsId = resObs->observeId;
1183         ehFlag = (OCEntityHandlerFlag)(ehFlag | OC_OBSERVE_FLAG);
1184
1185         result = DeleteObserverUsingToken (request->requestToken, request->tokenLength);
1186
1187         if(result == OC_STACK_OK)
1188         {
1189             OIC_LOG(INFO, TAG, "Removed observer successfully");
1190             request->observeResult = OC_STACK_OK;
1191             // There should be no observe option header for de-registration response.
1192             // Set as an invalid value here so we can detect it later and remove the field in response.
1193             request->observationOption = MAX_SEQUENCE_NUMBER + 1;
1194         }
1195         else
1196         {
1197             request->observeResult = OC_STACK_ERROR;
1198             OIC_LOG(ERROR, TAG, "Observer Removal failed");
1199             FindAndDeleteServerRequest(request);
1200             goto exit;
1201         }
1202     }
1203     else
1204     {
1205         result = OC_STACK_ERROR;
1206         goto exit;
1207     }
1208
1209     ehResult = resource->entityHandler(ehFlag, &ehRequest, resource->entityHandlerCallbackParam);
1210     if(ehResult == OC_EH_SLOW)
1211     {
1212         OIC_LOG(INFO, TAG, "This is a slow resource");
1213         request->slowFlag = 1;
1214     }
1215     else if(ehResult == OC_EH_ERROR)
1216     {
1217         FindAndDeleteServerRequest(request);
1218     }
1219     result = EntityHandlerCodeToOCStackCode(ehResult);
1220 exit:
1221     OCPayloadDestroy(ehRequest.payload);
1222     return result;
1223 }
1224
1225 static OCStackResult
1226 HandleCollectionResourceDefaultEntityHandler (OCServerRequest *request,
1227                                               OCResource *resource)
1228 {
1229     if(!request || !resource)
1230     {
1231         return OC_STACK_INVALID_PARAM;
1232     }
1233
1234     OCStackResult result = OC_STACK_ERROR;
1235     OCEntityHandlerRequest ehRequest = {0};
1236
1237     result = FormOCEntityHandlerRequest(&ehRequest,
1238                                         (OCRequestHandle)request,
1239                                         request->method,
1240                                         &request->devAddr,
1241                                         (OCResourceHandle)resource,
1242                                         request->query,
1243                                         PAYLOAD_TYPE_REPRESENTATION,
1244                                         request->payload,
1245                                         request->payloadSize,
1246                                         request->numRcvdVendorSpecificHeaderOptions,
1247                                         request->rcvdVendorSpecificHeaderOptions,
1248                                         (OCObserveAction)request->observationOption,
1249                                         (OCObservationId)0,
1250                                         request->coapID);
1251     if(result == OC_STACK_OK)
1252     {
1253         result = DefaultCollectionEntityHandler (OC_REQUEST_FLAG, &ehRequest);
1254     }
1255
1256     OCPayloadDestroy(ehRequest.payload);
1257     return result;
1258 }
1259
1260 OCStackResult
1261 ProcessRequest(ResourceHandling resHandling, OCResource *resource, OCServerRequest *request)
1262 {
1263     OCStackResult ret = OC_STACK_OK;
1264
1265     switch (resHandling)
1266     {
1267         case OC_RESOURCE_VIRTUAL:
1268         {
1269             ret = HandleVirtualResource (request, resource);
1270             break;
1271         }
1272         case OC_RESOURCE_DEFAULT_DEVICE_ENTITYHANDLER:
1273         {
1274             ret = HandleDefaultDeviceEntityHandler(request);
1275             break;
1276         }
1277         case OC_RESOURCE_NOT_COLLECTION_DEFAULT_ENTITYHANDLER:
1278         {
1279             OIC_LOG(INFO, TAG, "OC_RESOURCE_NOT_COLLECTION_DEFAULT_ENTITYHANDLER");
1280             return OC_STACK_ERROR;
1281         }
1282         case OC_RESOURCE_NOT_COLLECTION_WITH_ENTITYHANDLER:
1283         {
1284             ret = HandleResourceWithEntityHandler (request, resource, 0);
1285             break;
1286         }
1287         case OC_RESOURCE_COLLECTION_WITH_ENTITYHANDLER:
1288         {
1289             ret = HandleResourceWithEntityHandler (request, resource, 1);
1290             break;
1291         }
1292         case OC_RESOURCE_COLLECTION_DEFAULT_ENTITYHANDLER:
1293         {
1294             ret = HandleCollectionResourceDefaultEntityHandler (request, resource);
1295             break;
1296         }
1297         case OC_RESOURCE_NOT_SPECIFIED:
1298         {
1299             ret = OC_STACK_NO_RESOURCE;
1300             break;
1301         }
1302         default:
1303         {
1304             OIC_LOG(INFO, TAG, "Invalid Resource Determination");
1305             return OC_STACK_ERROR;
1306         }
1307     }
1308     return ret;
1309 }
1310
1311 OCStackResult OCSetPlatformInfo(OCPlatformInfo info)
1312 {
1313     OCResource *resource = NULL;
1314     if (!info.platformID || !info.manufacturerName)
1315     {
1316         OIC_LOG(ERROR, TAG, "No value specified.");
1317         goto exit;
1318     }
1319     if (0 == strlen(info.platformID) || 0 == strlen(info.manufacturerName))
1320     {
1321         OIC_LOG(ERROR, TAG, "The passed value cannot be empty");
1322         goto exit;
1323     }
1324     if ((info.manufacturerName && strlen(info.manufacturerName) > MAX_PLATFORM_NAME_LENGTH) ||
1325         (info.manufacturerUrl && strlen(info.manufacturerUrl) > MAX_PLATFORM_URL_LENGTH) ||
1326         (info.modelNumber && strlen(info.modelNumber) > MAX_PLATFORM_NAME_LENGTH) ||
1327         (info.platformVersion && strlen(info.platformVersion) > MAX_PLATFORM_NAME_LENGTH) ||
1328         (info.operatingSystemVersion && strlen(info.operatingSystemVersion) > MAX_PLATFORM_NAME_LENGTH) ||
1329         (info.hardwareVersion && strlen(info.hardwareVersion) > MAX_PLATFORM_NAME_LENGTH) ||
1330         (info.firmwareVersion && strlen(info.firmwareVersion) > MAX_PLATFORM_NAME_LENGTH) ||
1331         (info.supportUrl && strlen(info.supportUrl) > MAX_PLATFORM_URL_LENGTH))
1332     {
1333         OIC_LOG(ERROR, TAG, "The passed value is bigger than permitted.");
1334         goto exit;
1335     }
1336
1337     resource = FindResourceByUri(OC_RSRVD_PLATFORM_URI);
1338     if (!resource)
1339     {
1340         OIC_LOG(ERROR, TAG, "Platform Resource does not exist.");
1341         goto exit;
1342     }
1343     OIC_LOG(INFO, TAG, "Entering OCSetPlatformInfo");
1344     VERIFY_SUCCESS(OCSetPropertyValue(PAYLOAD_TYPE_PLATFORM, OC_RSRVD_PLATFORM_ID, info.platformID));
1345     VERIFY_SUCCESS(OCSetPropertyValue(PAYLOAD_TYPE_PLATFORM, OC_RSRVD_MFG_NAME, info.manufacturerName));
1346     OCSetPropertyValue(PAYLOAD_TYPE_PLATFORM, OC_RSRVD_MFG_URL, info.manufacturerUrl);
1347     OCSetPropertyValue(PAYLOAD_TYPE_PLATFORM, OC_RSRVD_MODEL_NUM, info.modelNumber);
1348     OCSetPropertyValue(PAYLOAD_TYPE_PLATFORM, OC_RSRVD_MFG_DATE, info.dateOfManufacture);
1349     OCSetPropertyValue(PAYLOAD_TYPE_PLATFORM, OC_RSRVD_PLATFORM_VERSION, info.platformVersion);
1350     OCSetPropertyValue(PAYLOAD_TYPE_PLATFORM, OC_RSRVD_OS_VERSION, info.operatingSystemVersion);
1351     OCSetPropertyValue(PAYLOAD_TYPE_PLATFORM, OC_RSRVD_HARDWARE_VERSION, info.hardwareVersion);
1352     OCSetPropertyValue(PAYLOAD_TYPE_PLATFORM, OC_RSRVD_FIRMWARE_VERSION, info.firmwareVersion);
1353     OCSetPropertyValue(PAYLOAD_TYPE_PLATFORM, OC_RSRVD_SUPPORT_URL, info.supportUrl);
1354     OCSetPropertyValue(PAYLOAD_TYPE_PLATFORM, OC_RSRVD_SYSTEM_TIME, info.systemTime);
1355     OIC_LOG(INFO, TAG, "Platform parameter initialized successfully.");
1356     return OC_STACK_OK;
1357
1358 exit:
1359     return OC_STACK_INVALID_PARAM;
1360 }
1361
1362 OCStackResult OCSetDeviceInfo(OCDeviceInfo info)
1363 {
1364     OCStringLL *dataModelVersion = NULL;
1365     OCResource *resource = FindResourceByUri(OC_RSRVD_DEVICE_URI);
1366     if (!resource)
1367     {
1368         OIC_LOG(ERROR, TAG, "Device Resource does not exist.");
1369         goto exit;
1370     }
1371     if (!info.deviceName || info.deviceName[0] == '\0')
1372     {
1373         OIC_LOG(ERROR, TAG, "Null or empty device name.");
1374        return OC_STACK_INVALID_PARAM;
1375     }
1376
1377     if (OCGetServerInstanceIDString() == NULL)
1378     {
1379         OIC_LOG(INFO, TAG, "Device ID generation failed");
1380         goto exit;
1381     }
1382
1383     VERIFY_SUCCESS(OCSetPropertyValue(PAYLOAD_TYPE_DEVICE, OC_RSRVD_DEVICE_NAME, info.deviceName));
1384     for (OCStringLL *temp = info.types; temp; temp = temp->next)
1385     {
1386         if (temp->value)
1387         {
1388             VERIFY_SUCCESS(OCBindResourceTypeToResource(resource, temp->value));
1389         }
1390     }
1391     VERIFY_SUCCESS(OCSetPropertyValue(PAYLOAD_TYPE_DEVICE, OC_RSRVD_SPEC_VERSION, info.specVersion ?
1392         info.specVersion: OC_SPEC_VERSION));
1393     if (info.dataModelVersions)
1394     {
1395         VERIFY_SUCCESS(OCSetPropertyValue(PAYLOAD_TYPE_DEVICE, OC_RSRVD_DATA_MODEL_VERSION, info.dataModelVersions));
1396     }
1397     else
1398     {
1399         dataModelVersion = OCCreateOCStringLL(OC_DATA_MODEL_VERSION);
1400         VERIFY_SUCCESS(OCSetPropertyValue(PAYLOAD_TYPE_DEVICE, OC_RSRVD_DATA_MODEL_VERSION, dataModelVersion));
1401     }
1402     OIC_LOG(INFO, TAG, "Device parameter initialized successfully.");
1403     return OC_STACK_OK;
1404
1405 exit:
1406     if (dataModelVersion)
1407     {
1408         OCFreeOCStringLL(dataModelVersion);
1409     }
1410     return OC_STACK_ERROR;
1411 }
1412
1413 OCStackResult OCGetAttribute(const OCResource *resource, const char *attribute, void **value)
1414 {
1415     if (!resource || !attribute)
1416     {
1417         return OC_STACK_INVALID_PARAM;
1418     }
1419     if (0 == strlen(attribute))
1420     {
1421         return OC_STACK_INVALID_PARAM;
1422     }
1423     for (OCAttribute *temp = resource->rsrcAttributes; temp; temp = temp->next)
1424     {
1425         if (0 == strcmp(attribute, temp->attrName))
1426         {
1427             // A special case as this type return OCStringLL
1428             if (0 == strcmp(OC_RSRVD_DATA_MODEL_VERSION, attribute))
1429             {
1430                 *value = CloneOCStringLL((OCStringLL *)temp->attrValue);
1431                 return OC_STACK_OK;
1432             }
1433             else
1434             {
1435                 *value = OICStrdup((char *)temp->attrValue);
1436                 return OC_STACK_OK;
1437             }
1438         }
1439     }
1440     return OC_STACK_NO_RESOURCE;
1441 }
1442
1443 OCStackResult OCGetPropertyValue(OCPayloadType type, const char *prop, void **value)
1444 {
1445     if (!prop || *value)
1446     {
1447         return OC_STACK_INVALID_PARAM;
1448     }
1449     if (strlen(prop) == 0)
1450     {
1451         return OC_STACK_INVALID_PARAM;
1452     }
1453     OCStackResult res =  OC_STACK_NO_RESOURCE;
1454     if (PAYLOAD_TYPE_DEVICE == type || PAYLOAD_TYPE_PLATFORM == type)
1455     {
1456         const char *pathType = (type == PAYLOAD_TYPE_DEVICE) ? OC_RSRVD_DEVICE_URI : OC_RSRVD_PLATFORM_URI;
1457         OCResource *resource = FindResourceByUri(pathType);
1458         if (!resource)
1459         {
1460             return OC_STACK_NO_RESOURCE;
1461         }
1462
1463         res = OCGetAttribute(resource, prop, value);
1464     }
1465     return res;
1466 }
1467
1468 OCStackResult OCSetAttribute(OCResource* resource, const char* attribute, const void* value)
1469 {
1470     OCAttribute *resAttrib = (OCAttribute *)OICCalloc(1, sizeof(OCAttribute));
1471     VERIFY_PARAM_NON_NULL(TAG, resAttrib, "Failed allocation OCAttribute");
1472     resAttrib->attrName = OICStrdup(attribute);
1473     VERIFY_PARAM_NON_NULL(TAG, resAttrib->attrName, "Failed allocating attribute name");
1474     // A special case when value is of type OCStringLL
1475     if (0 == strcmp(OC_RSRVD_DATA_MODEL_VERSION, attribute))
1476     {
1477         resAttrib->attrValue = CloneOCStringLL((OCStringLL *)value);
1478     }
1479     else
1480     {
1481         resAttrib->attrValue = OICStrdup((char *)value);
1482     }
1483     VERIFY_PARAM_NON_NULL(TAG, resAttrib->attrValue, "Failed allocating attribute value");
1484     resAttrib->next = NULL;
1485
1486     if (!resource->rsrcAttributes)
1487     {
1488         resource->rsrcAttributes = resAttrib;
1489     }
1490     else
1491     {
1492         OCAttribute *temp = resource->rsrcAttributes;
1493         for (; temp->next; temp = temp->next)
1494         {
1495             if (0 == strcmp(attribute, temp->attrName))
1496             {
1497                 if (0 == strcmp(OC_RSRVD_DATA_MODEL_VERSION, temp->attrName))
1498                 {
1499                     OCFreeOCStringLL((OCStringLL *)temp->attrValue);
1500                 }
1501                  {
1502                     OICFree((char *)temp->attrValue);
1503                 }
1504                 break;
1505             }
1506         }
1507         temp->next = resAttrib;
1508     }
1509     return OC_STACK_OK;
1510
1511 exit:
1512     OCDeleteResourceAttributes(resAttrib);
1513     return OC_STACK_NO_MEMORY;
1514
1515 }
1516
1517 OCStackResult OCSetPropertyValue(OCPayloadType type, const char *prop, const void *value)
1518 {
1519     if (!prop)
1520     {
1521         return OC_STACK_INVALID_PARAM;
1522     }
1523     if (strlen(prop) == 0)
1524     {
1525         return OC_STACK_INVALID_PARAM;
1526     }
1527
1528     OCStackResult res = OC_STACK_ERROR;
1529     if (PAYLOAD_TYPE_DEVICE == type || PAYLOAD_TYPE_PLATFORM == type)
1530     {
1531         const char *pathType = (type == PAYLOAD_TYPE_DEVICE) ? OC_RSRVD_DEVICE_URI : OC_RSRVD_PLATFORM_URI;
1532         OCResource *resource = FindResourceByUri(pathType);
1533         if (!resource)
1534         {
1535             OIC_LOG(ERROR, TAG, "Resource does not exist.");
1536         }
1537         if (value)
1538         {
1539             res = OCSetAttribute(resource, prop, value);
1540         }
1541     }
1542
1543     return res;
1544 }