1 //******************************************************************
3 // Copyright 2014 Intel Mobile Communications GmbH All Rights Reserved.
5 //-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
7 // Licensed under the Apache License, Version 2.0 (the "License");
8 // you may not use this file except in compliance with the License.
9 // You may obtain a copy of the License at
11 // http://www.apache.org/licenses/LICENSE-2.0
13 // Unless required by applicable law or agreed to in writing, software
14 // distributed under the License is distributed on an "AS IS" BASIS,
15 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 // See the License for the specific language governing permissions and
17 // limitations under the License.
19 //-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
21 // 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
30 #include "ocstackconfig.h"
31 #include "ocstackinternal.h"
32 #include "ocresourcehandler.h"
33 #include "ocobserve.h"
34 #include "occollection.h"
40 #include "cainterface.h"
44 #define TAG PCF("ocresource")
45 #define VERIFY_SUCCESS(op, successCode) { if (op != successCode) \
46 {OC_LOG_V(FATAL, TAG, "%s failed!!", #op); goto exit;} }
48 #define VERIFY_NON_NULL(arg, logLevel, retVal) { if (!(arg)) { OC_LOG((logLevel), \
49 TAG, PCF(#arg " is NULL")); return (retVal); } }
51 extern OCResource *headResource;
52 static cJSON *savedDeviceInfo = NULL;
54 static const char * VIRTUAL_RSRCS[] = {
63 //-----------------------------------------------------------------------------
64 // Default resource entity handler function
65 //-----------------------------------------------------------------------------
66 OCEntityHandlerResult defaultResourceEHandler(OCEntityHandlerFlag flag,
67 OCEntityHandlerRequest * request) {
68 //TODO ("Implement me!!!!");
69 // TODO: remove silence unused param warnings
72 return OC_EH_OK; // Making sure that the Default EH and the Vendor EH have matching signatures
75 /* This method will retrieve the port at which the secure resource is hosted */
76 static OCStackResult GetSecurePortInfo(CAConnectivityType_t connType, uint16_t *port)
78 CALocalConnectivity_t* info = NULL;
80 OCStackResult ret = OC_STACK_ERROR;
82 CAResult_t caResult = CAGetNetworkInformation(&info, &size);
83 if ((caResult == CA_STATUS_OK) && info && size)
87 if (info[size].isSecured && info[size].type == connType)
89 if (info[size].type == CA_ETHERNET ||
90 info[size].type == CA_WIFI)
92 *port = info[size].addressInfo.IP.port;
104 static OCStackResult ValidateUrlQuery (char *url, char *query,
105 uint8_t *filterOn, char **filterValue)
107 if(!filterOn || !filterValue)
109 return OC_STACK_INVALID_PARAM;
112 char *filterParam = NULL;
114 OC_LOG(INFO, TAG, PCF("Entering ValidateUrlQuery"));
117 return OC_STACK_INVALID_URI;
120 if (strcmp ((char *)url, GetVirtualResourceUri(OC_WELL_KNOWN_URI)) == 0 ||
121 strcmp ((char *)url, GetVirtualResourceUri(OC_DEVICE_URI)) == 0)
123 *filterOn = STACK_RES_DISCOVERY_NOFILTER;
126 char* strTokPtr = NULL;
127 filterParam = strtok_r((char *)query, "=", &strTokPtr);
128 *filterValue = strtok_r(NULL, " ", &strTokPtr);
130 if (!(*filterValue) || ! filterParam)
132 return OC_STACK_INVALID_QUERY;
134 else if (strcmp (filterParam, OC_RSRVD_INTERFACE) == 0)
136 // Resource discovery with interface filter
137 *filterOn = STACK_RES_DISCOVERY_IF_FILTER;
139 else if (strcmp (filterParam, OC_RSRVD_RESOURCE_TYPE) == 0)
141 // Resource discovery with resource type filter
142 *filterOn = STACK_RES_DISCOVERY_RT_FILTER;
144 else if (strcmp (filterParam, OC_RSRVD_DEVICE_ID) == 0)
147 *filterOn = STACK_DEVICE_DISCOVERY_DI_FILTER;
149 else if (strcmp (filterParam, OC_RSRVD_DEVICE_NAME) == 0)
152 *filterOn = STACK_DEVICE_DISCOVERY_DN_FILTER;
156 // Other filter types not supported
157 return OC_STACK_INVALID_QUERY;
162 else if (strcmp((char *)url, GetVirtualResourceUri(OC_PRESENCE)) == 0)
164 //Nothing needs to be done, except for pass a OC_PRESENCE query through as OC_STACK_OK.
165 OC_LOG(INFO, TAG, PCF("OC_PRESENCE Request"));
166 *filterOn = STACK_RES_DISCOVERY_NOFILTER;
171 // Other URIs not yet supported
172 return OC_STACK_INVALID_URI;
174 OC_LOG(INFO, TAG, PCF("Exiting ValidateUrlQuery"));
180 BuildVirtualResourceResponse(const OCResource *resourcePtr, uint8_t filterOn,
181 const char *filterValue, char *out, uint16_t *remaining,
182 CAConnectivityType_t connType )
184 if(!resourcePtr || !out || !remaining)
186 return OC_STACK_INVALID_PARAM;
189 OCResourceType *resourceTypePtr = NULL;
190 OCResourceInterface *interfacePtr = NULL;
191 cJSON *resObj = NULL;
192 cJSON *propObj = NULL;
193 cJSON *rtArray = NULL;
194 char *jsonStr = NULL;
195 uint8_t encodeRes = 0;
196 OCStackResult ret = OC_STACK_OK;
197 uint16_t jsonLen = 0;
199 OC_LOG(INFO, TAG, PCF("Entering BuildVirtualResourceResponse"));
200 resObj = cJSON_CreateObject();
205 if ((filterOn == STACK_RES_DISCOVERY_RT_FILTER) && filterValue)
207 resourceTypePtr = resourcePtr->rsrcType;
208 while (resourceTypePtr)
210 if (strcmp (resourceTypePtr->resourcetypename, filterValue) == 0)
215 resourceTypePtr = resourceTypePtr->next;
218 else if ((filterOn == STACK_RES_DISCOVERY_IF_FILTER) && filterValue)
220 interfacePtr = resourcePtr->rsrcInterface;
223 if (strcmp (interfacePtr->name, filterValue) == 0)
228 interfacePtr = interfacePtr->next;
231 else if (filterOn == STACK_RES_DISCOVERY_NOFILTER)
237 //TODO: Unsupported query filter
238 return OC_STACK_INVALID_QUERY;
244 cJSON_AddItemToObject (resObj, OC_RSRVD_HREF, cJSON_CreateString(resourcePtr->uri));
246 // Add server instance id
247 cJSON_AddItemToObject (resObj,
248 OC_RSRVD_SERVER_INSTANCE_ID,
249 cJSON_CreateString(OCGetServerInstanceIDString()));
251 cJSON_AddItemToObject (resObj, OC_RSRVD_PROPERTY, propObj = cJSON_CreateObject());
252 // Add resource types
253 cJSON_AddItemToObject (propObj, OC_RSRVD_RESOURCE_TYPE, rtArray = cJSON_CreateArray());
254 resourceTypePtr = resourcePtr->rsrcType;
255 while (resourceTypePtr)
257 cJSON_AddItemToArray (rtArray,
258 cJSON_CreateString(resourceTypePtr->resourcetypename));
259 resourceTypePtr = resourceTypePtr->next;
261 // Add interface types
262 cJSON_AddItemToObject (propObj, OC_RSRVD_INTERFACE, rtArray = cJSON_CreateArray());
263 interfacePtr = resourcePtr->rsrcInterface;
266 cJSON_AddItemToArray (rtArray, cJSON_CreateString(interfacePtr->name));
267 interfacePtr = interfacePtr->next;
269 // If resource is observable, set observability flag.
270 // Resources that are not observable will not have the flag.
271 if (resourcePtr->resourceProperties & OC_OBSERVABLE)
273 cJSON_AddItemToObject (propObj, OC_RSRVD_OBSERVABLE,
274 cJSON_CreateNumber(OC_RESOURCE_OBSERVABLE));
276 // Set secure flag for secure resources
277 if (resourcePtr->resourceProperties & OC_SECURE)
279 cJSON_AddNumberToObject (propObj, OC_RSRVD_SECURE, OC_RESOURCE_SECURE);
280 //Set the IP port also as secure resources are hosted on a different port
282 if (GetSecurePortInfo (connType, &port) == OC_STACK_OK)
284 cJSON_AddNumberToObject (propObj, OC_RSRVD_HOSTING_PORT, port);
290 jsonStr = cJSON_PrintUnformatted (resObj);
292 jsonLen = strlen(jsonStr);
293 if (jsonLen < *remaining)
295 strcpy(out, jsonStr);
296 *remaining = *remaining - jsonLen;
300 ret = OC_STACK_ERROR;
302 cJSON_Delete (resObj);
305 OC_LOG(INFO, TAG, PCF("Exiting BuildVirtualResourceResponse"));
309 OCStackResult BuildVirtualResourceResponseForDevice(uint8_t filterOn, char *filterValue,
310 char *out, uint16_t *remaining)
312 if(!out || !remaining)
314 return OC_STACK_INVALID_PARAM;
317 OCStackResult ret = OC_STACK_ERROR;
319 if (savedDeviceInfo != NULL)
321 char *jsonStr = NULL;
322 uint16_t jsonLen = 0;
323 cJSON *repObj = cJSON_GetObjectItem(savedDeviceInfo, OC_RSRVD_REPRESENTATION);
325 OC_LOG(INFO, TAG, PCF("Entering BuildVirtualResourceResponseForDevice"));
327 if ((filterOn == STACK_DEVICE_DISCOVERY_DI_FILTER) && filterValue)
329 if((cJSON_GetObjectItem(repObj,OC_RSRVD_DEVICE_ID) != NULL) &&
330 strcmp(cJSON_GetObjectItem(repObj,OC_RSRVD_DEVICE_ID)->valuestring, filterValue)
336 else if ((filterOn == STACK_DEVICE_DISCOVERY_DN_FILTER) && filterValue)
338 if((cJSON_GetObjectItem(repObj,OC_RSRVD_DEVICE_NAME) != NULL) &&
339 strcmp(cJSON_GetObjectItem(repObj,OC_RSRVD_DEVICE_NAME)->valuestring,
345 else if (filterOn == STACK_RES_DISCOVERY_NOFILTER)
351 ret = OC_STACK_INVALID_QUERY;
354 if (ret == OC_STACK_OK)
356 jsonStr = cJSON_PrintUnformatted (savedDeviceInfo);
360 jsonLen = strlen(jsonStr);
362 if (jsonLen < *remaining)
364 strncpy(out, jsonStr, (jsonLen + 1));
365 *remaining = *remaining - jsonLen;
370 ret = OC_STACK_ERROR;
377 ret = OC_STACK_ERROR;
382 ret = OC_STACK_INVALID_DEVICE_INFO;
387 //error so that stack won't respond with empty payload
388 ret = OC_STACK_INVALID_DEVICE_INFO;
391 OC_LOG(INFO, TAG, PCF("Exiting BuildVirtualResourceResponseForDevice"));
395 const char * GetVirtualResourceUri( OCVirtualResources resource)
397 if (resource < OC_MAX_VIRTUAL_RESOURCES)
399 return VIRTUAL_RSRCS[resource];
405 bool IsVirtualResource(const char* resourceUri)
412 for (int i = 0; i < OC_MAX_VIRTUAL_RESOURCES; i++)
414 if (strcmp(resourceUri, GetVirtualResourceUri((OCVirtualResources)i)) == 0)
422 uint8_t IsCollectionResource (OCResource *resource)
429 for (int i = 0; i < MAX_CONTAINED_RESOURCES; i++)
431 if (resource->rsrcResources[i])
439 OCResource *FindResourceByUri(const char* resourceUri)
446 OCResource * pointer = headResource;
448 if (strcmp(resourceUri, pointer->uri) == 0) {
451 pointer = pointer->next;
453 OC_LOG(INFO, TAG, PCF("Resource not found"));
458 OCStackResult DetermineResourceHandling (const OCServerRequest *request,
459 ResourceHandling *handling,
460 OCResource **resource)
462 if(!request || !handling || !resource)
464 return OC_STACK_INVALID_PARAM;
467 OC_LOG(INFO, TAG, PCF("Entering DetermineResourceHandling"));
469 // Check if virtual resource
470 if (IsVirtualResource((const char*)request->resourceUrl))
472 *handling = OC_RESOURCE_VIRTUAL;
473 *resource = headResource;
476 if (NULL == request->resourceUrl || (strlen((const char*)(request->resourceUrl)) == 0))
478 // Resource URL not specified
479 *handling = OC_RESOURCE_NOT_SPECIFIED;
480 return OC_STACK_NO_RESOURCE;
484 OCResource *resourcePtr = NULL;
485 resourcePtr = FindResourceByUri((const char*)request->resourceUrl);
486 *resource = resourcePtr;
489 if(defaultDeviceHandler)
491 *handling = OC_RESOURCE_DEFAULT_DEVICE_ENTITYHANDLER;
495 // Resource does not exist
496 // and default device handler does not exist
497 *handling = OC_RESOURCE_NOT_SPECIFIED;
498 return OC_STACK_NO_RESOURCE;
501 // secure resource will entertain only authorized requests
502 if ((resourcePtr->resourceProperties & OC_SECURE) && (request->secured == 0))
504 OC_LOG(ERROR, TAG, PCF("Un-authorized request. Ignoring"));
505 return OC_STACK_RESOURCE_ERROR;
508 if (IsCollectionResource (resourcePtr))
510 // Collection resource
511 if (resourcePtr->entityHandler != defaultResourceEHandler)
513 *handling = OC_RESOURCE_COLLECTION_WITH_ENTITYHANDLER;
518 *handling = OC_RESOURCE_COLLECTION_DEFAULT_ENTITYHANDLER;
524 // Resource not a collection
525 if (resourcePtr->entityHandler != defaultResourceEHandler)
527 *handling = OC_RESOURCE_NOT_COLLECTION_WITH_ENTITYHANDLER;
532 *handling = OC_RESOURCE_NOT_COLLECTION_DEFAULT_ENTITYHANDLER;
539 OCStackResult EntityHandlerCodeToOCStackCode(OCEntityHandlerResult ehResult)
541 OCStackResult result;
546 result = OC_STACK_OK;
549 result = OC_STACK_SLOW_RESOURCE;
552 result = OC_STACK_ERROR;
554 case OC_EH_FORBIDDEN:
555 result = OC_STACK_RESOURCE_ERROR;
557 case OC_EH_RESOURCE_CREATED:
558 result = OC_STACK_RESOURCE_CREATED;
560 case OC_EH_RESOURCE_DELETED:
561 result = OC_STACK_RESOURCE_DELETED;
564 result = OC_STACK_ERROR;
571 HandleVirtualResource (OCServerRequest *request, OCResource* resource)
573 if(!request || !resource)
575 return OC_STACK_INVALID_PARAM;
578 OCStackResult result = OC_STACK_ERROR;
579 char *filterValue = NULL;
580 uint8_t filterOn = 0;
581 uint16_t remaining = 0;
583 uint8_t firstLoopDone = 0;
584 char discoveryResBuf[MAX_RESPONSE_LENGTH] = {};
586 OC_LOG(INFO, TAG, PCF("Entering HandleVirtualResource"));
588 result = ValidateUrlQuery (request->resourceUrl,
589 request->query, &filterOn,
592 if (result == OC_STACK_OK)
594 if (strcmp ((char *)request->resourceUrl, GetVirtualResourceUri(OC_WELL_KNOWN_URI)) == 0)
596 ptr = discoveryResBuf;
597 remaining = MAX_RESPONSE_LENGTH;
599 // Check if valid resource and enough space in buffer for atleast
600 // the null character.
601 while(resource && (remaining > 1))
603 if((resource->resourceProperties & OC_ACTIVE)
604 && (resource->resourceProperties & OC_DISCOVERABLE))
606 // if there is data on the buffer, we have already added a response,
607 // so we need to add a comma before we do anything
609 && remaining >= (sizeof(OC_JSON_SEPARATOR)+1))
611 *ptr = OC_JSON_SEPARATOR;
616 result = BuildVirtualResourceResponse(resource, filterOn, filterValue,
617 (char*)ptr, &remaining, request->connectivityType );
619 if (result != OC_STACK_OK)
621 // if this failed, we need to remove the comma added above.
630 ptr += strlen((char *)ptr);
632 resource = resource->next;
635 if(strlen((const char *)discoveryResBuf) > 0)
637 OCEntityHandlerResponse response = {};
639 response.ehResult = OC_EH_OK;
640 response.payload = discoveryResBuf;
641 response.payloadSize = strlen((const char *)discoveryResBuf) + 1;
642 response.persistentBufferFlag = 0;
643 response.requestHandle = (OCRequestHandle) request;
644 response.resourceHandle = (OCResourceHandle) resource;
646 result = OCDoResponse(&response);
649 else if (strcmp ((char *)request->resourceUrl, GetVirtualResourceUri(OC_DEVICE_URI)) == 0)
651 remaining = MAX_RESPONSE_LENGTH;
652 ptr = discoveryResBuf;
654 result = BuildVirtualResourceResponseForDevice(filterOn, filterValue,
655 (char*)ptr, &remaining);
657 if(result == OC_STACK_OK)
659 ptr += strlen((char*)ptr);
662 if(remaining < MAX_RESPONSE_LENGTH)
664 OCEntityHandlerResponse response = {0};
666 response.ehResult = OC_EH_OK;
667 response.payload = discoveryResBuf;
668 response.payloadSize = strlen((const char *)discoveryResBuf) + 1;
669 response.persistentBufferFlag = 0;
670 response.requestHandle = (OCRequestHandle) request;
671 response.resourceHandle = (OCResourceHandle) resource;
673 result = OCDoResponse(&response);
679 if(resource->resourceProperties & OC_ACTIVE){
680 SendPresenceNotification(NULL);
685 result = OC_STACK_VIRTUAL_DO_NOT_HANDLE;
690 HandleDefaultDeviceEntityHandler (OCServerRequest *request)
694 return OC_STACK_INVALID_PARAM;
697 OCStackResult result = OC_STACK_OK;
698 OCEntityHandlerResult ehResult = OC_EH_ERROR;
699 OCEntityHandlerRequest ehRequest = {};
701 OC_LOG(INFO, TAG, PCF("Entering HandleResourceWithDefaultDeviceEntityHandler"));
702 result = FormOCEntityHandlerRequest(&ehRequest, (OCRequestHandle) request,
703 request->method, (OCResourceHandle) NULL, request->query,
704 request->reqJSONPayload, request->numRcvdVendorSpecificHeaderOptions,
705 request->rcvdVendorSpecificHeaderOptions,
706 (OCObserveAction)request->observationOption, (OCObservationId)0);
707 VERIFY_SUCCESS(result, OC_STACK_OK);
709 // At this point we know for sure that defaultDeviceHandler exists
710 ehResult = defaultDeviceHandler(OC_REQUEST_FLAG, &ehRequest,
711 (char*) request->resourceUrl);
712 if(ehResult == OC_EH_SLOW)
714 OC_LOG(INFO, TAG, PCF("This is a slow resource"));
715 request->slowFlag = 1;
717 else if(ehResult == OC_EH_ERROR)
719 FindAndDeleteServerRequest(request);
721 result = EntityHandlerCodeToOCStackCode(ehResult);
727 HandleResourceWithEntityHandler (OCServerRequest *request,
728 OCResource *resource,
729 uint8_t collectionResource)
731 if(!request || ! resource)
733 return OC_STACK_INVALID_PARAM;
736 OCStackResult result = OC_STACK_ERROR;
737 OCEntityHandlerResult ehResult = OC_EH_ERROR;
738 OCEntityHandlerFlag ehFlag = OC_REQUEST_FLAG;
739 ResourceObserver *resObs = NULL;
741 OCEntityHandlerRequest ehRequest = {};
743 OC_LOG(INFO, TAG, PCF("Entering HandleResourceWithEntityHandler"));
744 result = FormOCEntityHandlerRequest(&ehRequest, (OCRequestHandle) request,
745 request->method, (OCResourceHandle) resource, request->query,
746 request->reqJSONPayload, request->numRcvdVendorSpecificHeaderOptions,
747 request->rcvdVendorSpecificHeaderOptions,
748 (OCObserveAction)request->observationOption, 0);
749 VERIFY_SUCCESS(result, OC_STACK_OK);
751 if(ehRequest.obsInfo.action == OC_OBSERVE_NO_OPTION)
753 OC_LOG(INFO, TAG, PCF("No observation requested"));
754 ehFlag = OC_REQUEST_FLAG;
756 else if(ehRequest.obsInfo.action == OC_OBSERVE_REGISTER &&
759 OC_LOG(INFO, TAG, PCF("Registering observation requested"));
760 result = GenerateObserverId(&ehRequest.obsInfo.obsId);
761 VERIFY_SUCCESS(result, OC_STACK_OK);
763 result = AddObserver ((const char*)(request->resourceUrl),
764 (const char *)(request->query),
765 ehRequest.obsInfo.obsId, &request->requestToken, request->tokenLength,
766 resource, request->qos,
767 &request->addressInfo, request->connectivityType);
769 if(result == OC_STACK_OK)
771 OC_LOG(INFO, TAG, PCF("Added observer successfully"));
772 request->observeResult = OC_STACK_OK;
773 ehFlag = (OCEntityHandlerFlag)(OC_REQUEST_FLAG | OC_OBSERVE_FLAG);
777 result = OC_STACK_OK;
778 // The error in observeResult for the request will be
779 // used when responding to this request by omitting
780 // the observation option/sequence number.
781 request->observeResult = OC_STACK_ERROR;
782 OC_LOG(ERROR, TAG, PCF("Observer Addition failed"));
783 ehFlag = OC_REQUEST_FLAG;
787 else if(ehRequest.obsInfo.action == OC_OBSERVE_DEREGISTER &&
790 OC_LOG(INFO, TAG, PCF("Deregistering observation requested"));
792 resObs = GetObserverUsingToken (&request->requestToken, request->tokenLength);
796 // Stack does not contain this observation request
797 // Either token is incorrect or observation list is corrupted
798 result = OC_STACK_ERROR;
801 ehRequest.obsInfo.obsId = resObs->observeId;
802 ehFlag = (OCEntityHandlerFlag)(ehFlag | OC_OBSERVE_FLAG);
804 result = DeleteObserverUsingToken (&request->requestToken, request->tokenLength);
806 if(result == OC_STACK_OK)
808 OC_LOG(INFO, TAG, PCF("Removed observer successfully"));
809 request->observeResult = OC_STACK_OK;
813 result = OC_STACK_OK;
814 request->observeResult = OC_STACK_ERROR;
815 OC_LOG(ERROR, TAG, PCF("Observer Removal failed"));
820 result = OC_STACK_ERROR;
824 ehResult = resource->entityHandler(ehFlag, &ehRequest);
825 if(ehResult == OC_EH_SLOW)
827 OC_LOG(INFO, TAG, PCF("This is a slow resource"));
828 request->slowFlag = 1;
830 else if(ehResult == OC_EH_ERROR)
832 FindAndDeleteServerRequest(request);
834 result = EntityHandlerCodeToOCStackCode(ehResult);
840 HandleCollectionResourceDefaultEntityHandler (OCServerRequest *request,
841 OCResource *resource)
843 if(!request || !resource)
845 return OC_STACK_INVALID_PARAM;
848 OCStackResult result = OC_STACK_ERROR;
849 OCEntityHandlerRequest ehRequest = {};
851 result = FormOCEntityHandlerRequest(&ehRequest, (OCRequestHandle) request,
852 request->method, (OCResourceHandle) resource, request->query,
853 request->reqJSONPayload, request->numRcvdVendorSpecificHeaderOptions,
854 request->rcvdVendorSpecificHeaderOptions,
855 (OCObserveAction)request->observationOption, (OCObservationId) 0);
856 if(result != OC_STACK_OK)
861 return (DefaultCollectionEntityHandler (OC_REQUEST_FLAG, &ehRequest));
865 ProcessRequest(ResourceHandling resHandling, OCResource *resource, OCServerRequest *request)
867 OCStackResult ret = OC_STACK_OK;
871 case OC_RESOURCE_VIRTUAL:
873 ret = HandleVirtualResource (request, resource);
876 case OC_RESOURCE_DEFAULT_DEVICE_ENTITYHANDLER:
878 ret = HandleDefaultDeviceEntityHandler(request);
881 case OC_RESOURCE_NOT_COLLECTION_DEFAULT_ENTITYHANDLER:
883 OC_LOG(INFO, TAG, PCF("OC_RESOURCE_NOT_COLLECTION_DEFAULT_ENTITYHANDLER"));
884 return OC_STACK_ERROR;
886 case OC_RESOURCE_NOT_COLLECTION_WITH_ENTITYHANDLER:
888 ret = HandleResourceWithEntityHandler (request, resource, 0);
891 case OC_RESOURCE_COLLECTION_WITH_ENTITYHANDLER:
893 ret = HandleResourceWithEntityHandler (request, resource, 1);
896 case OC_RESOURCE_COLLECTION_DEFAULT_ENTITYHANDLER:
898 ret = HandleCollectionResourceDefaultEntityHandler (request, resource);
901 case OC_RESOURCE_NOT_SPECIFIED:
903 ret = OC_STACK_NO_RESOURCE;
908 OC_LOG(INFO, TAG, PCF("Invalid Resource Determination"));
909 return OC_STACK_ERROR;
915 void DeleteDeviceInfo()
919 cJSON_Delete(savedDeviceInfo);
923 OCStackResult SaveDeviceInfo(OCDeviceInfo deviceInfo)
927 savedDeviceInfo = cJSON_CreateObject();
928 cJSON *repObj = NULL;
930 cJSON_AddItemToObject (savedDeviceInfo, OC_RSRVD_HREF,
931 cJSON_CreateString(GetVirtualResourceUri(OC_DEVICE_URI)));
933 cJSON_AddItemToObject (savedDeviceInfo, OC_RSRVD_REPRESENTATION, repObj = cJSON_CreateObject());
935 if (deviceInfo.contentType)
937 cJSON_AddItemToObject (repObj, OC_RSRVD_CONTENT_TYPE,
938 cJSON_CreateString(deviceInfo.contentType));
941 if (deviceInfo.dateOfManufacture)
943 cJSON_AddItemToObject (repObj, OC_RSRVD_MFG_DATE,
944 cJSON_CreateString(deviceInfo.dateOfManufacture));
947 if (deviceInfo.deviceName)
949 cJSON_AddItemToObject (repObj, OC_RSRVD_DEVICE_NAME,
950 cJSON_CreateString(deviceInfo.deviceName));
953 if (deviceInfo.deviceUUID)
955 cJSON_AddItemToObject (repObj, OC_RSRVD_DEVICE_ID,
956 cJSON_CreateString(deviceInfo.deviceUUID));
959 if (deviceInfo.firmwareVersion)
961 cJSON_AddItemToObject (repObj, OC_RSRVD_FW_VERSION,
962 cJSON_CreateString(deviceInfo.firmwareVersion));
965 if (deviceInfo.hostName)
967 cJSON_AddItemToObject (repObj, OC_RSRVD_HOST_NAME,
968 cJSON_CreateString(deviceInfo.hostName));
971 if (deviceInfo.manufacturerName)
973 if(strlen(deviceInfo.manufacturerName) > MAX_MANUFACTURER_NAME_LENGTH)
976 return OC_STACK_INVALID_PARAM;
979 cJSON_AddItemToObject (repObj, OC_RSRVD_MFG_NAME,
980 cJSON_CreateString(deviceInfo.manufacturerName));
983 if (deviceInfo.manufacturerUrl)
985 if(strlen(deviceInfo.manufacturerUrl) > MAX_MANUFACTURER_URL_LENGTH)
988 return OC_STACK_INVALID_PARAM;
991 cJSON_AddItemToObject (repObj, OC_RSRVD_MFG_URL,
992 cJSON_CreateString(deviceInfo.manufacturerUrl));
995 if (deviceInfo.modelNumber)
997 cJSON_AddItemToObject (repObj, OC_RSRVD_MODEL_NUM,
998 cJSON_CreateString(deviceInfo.modelNumber));
1001 if (deviceInfo.platformVersion)
1003 cJSON_AddItemToObject (repObj, OC_RSRVD_PLATFORM_VERSION,
1004 cJSON_CreateString(deviceInfo.platformVersion));
1007 if (deviceInfo.supportUrl)
1009 cJSON_AddItemToObject (repObj, OC_RSRVD_SUPPORT_URL,
1010 cJSON_CreateString(deviceInfo.supportUrl));
1013 if (deviceInfo.version)
1015 cJSON_AddItemToObject (repObj, OC_RSRVD_VERSION,
1016 cJSON_CreateString(deviceInfo.version));