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;
563 case OC_EH_RESOURCE_NOT_FOUND:
564 result = OC_STACK_NO_RESOURCE;
567 result = OC_STACK_ERROR;
574 HandleVirtualResource (OCServerRequest *request, OCResource* resource)
576 if(!request || !resource)
578 return OC_STACK_INVALID_PARAM;
581 OCStackResult result = OC_STACK_ERROR;
582 char *filterValue = NULL;
583 uint8_t filterOn = 0;
584 uint16_t remaining = 0;
586 uint8_t firstLoopDone = 0;
587 char discoveryResBuf[MAX_RESPONSE_LENGTH] = {};
589 OC_LOG(INFO, TAG, PCF("Entering HandleVirtualResource"));
591 result = ValidateUrlQuery (request->resourceUrl,
592 request->query, &filterOn,
595 if (result == OC_STACK_OK)
597 if (strcmp ((char *)request->resourceUrl, GetVirtualResourceUri(OC_WELL_KNOWN_URI)) == 0)
599 ptr = discoveryResBuf;
600 remaining = MAX_RESPONSE_LENGTH;
602 // Check if valid resource and enough space in buffer for atleast
603 // the null character.
604 while(resource && (remaining > 1))
606 if((resource->resourceProperties & OC_ACTIVE)
607 && (resource->resourceProperties & OC_DISCOVERABLE))
609 // if there is data on the buffer, we have already added a response,
610 // so we need to add a comma before we do anything
612 && remaining >= (sizeof(OC_JSON_SEPARATOR)+1))
614 *ptr = OC_JSON_SEPARATOR;
619 result = BuildVirtualResourceResponse(resource, filterOn, filterValue,
620 (char*)ptr, &remaining, request->connectivityType );
622 if (result != OC_STACK_OK)
624 // if this failed, we need to remove the comma added above.
633 ptr += strlen((char *)ptr);
635 resource = resource->next;
638 if(strlen((const char *)discoveryResBuf) > 0)
640 OCEntityHandlerResponse response = {};
642 response.ehResult = OC_EH_OK;
643 response.payload = discoveryResBuf;
644 response.payloadSize = strlen((const char *)discoveryResBuf) + 1;
645 response.persistentBufferFlag = 0;
646 response.requestHandle = (OCRequestHandle) request;
647 response.resourceHandle = (OCResourceHandle) resource;
649 result = OCDoResponse(&response);
652 else if (strcmp ((char *)request->resourceUrl, GetVirtualResourceUri(OC_DEVICE_URI)) == 0)
654 remaining = MAX_RESPONSE_LENGTH;
655 ptr = discoveryResBuf;
657 result = BuildVirtualResourceResponseForDevice(filterOn, filterValue,
658 (char*)ptr, &remaining);
660 if(result == OC_STACK_OK)
662 ptr += strlen((char*)ptr);
665 if(remaining < MAX_RESPONSE_LENGTH)
667 OCEntityHandlerResponse response = {0};
669 response.ehResult = OC_EH_OK;
670 response.payload = discoveryResBuf;
671 response.payloadSize = strlen((const char *)discoveryResBuf) + 1;
672 response.persistentBufferFlag = 0;
673 response.requestHandle = (OCRequestHandle) request;
674 response.resourceHandle = (OCResourceHandle) resource;
676 result = OCDoResponse(&response);
682 if(resource->resourceProperties & OC_ACTIVE){
683 SendPresenceNotification(NULL);
688 result = OC_STACK_OK;
693 HandleDefaultDeviceEntityHandler (OCServerRequest *request)
697 return OC_STACK_INVALID_PARAM;
700 OCStackResult result = OC_STACK_OK;
701 OCEntityHandlerResult ehResult = OC_EH_ERROR;
702 OCEntityHandlerRequest ehRequest = {};
704 OC_LOG(INFO, TAG, PCF("Entering HandleResourceWithDefaultDeviceEntityHandler"));
705 result = FormOCEntityHandlerRequest(&ehRequest, (OCRequestHandle) request,
706 request->method, (OCResourceHandle) NULL, request->query,
707 request->reqJSONPayload, request->numRcvdVendorSpecificHeaderOptions,
708 request->rcvdVendorSpecificHeaderOptions,
709 (OCObserveAction)request->observationOption, (OCObservationId)0);
710 VERIFY_SUCCESS(result, OC_STACK_OK);
712 // At this point we know for sure that defaultDeviceHandler exists
713 ehResult = defaultDeviceHandler(OC_REQUEST_FLAG, &ehRequest,
714 (char*) request->resourceUrl);
715 if(ehResult == OC_EH_SLOW)
717 OC_LOG(INFO, TAG, PCF("This is a slow resource"));
718 request->slowFlag = 1;
720 else if(ehResult == OC_EH_ERROR)
722 FindAndDeleteServerRequest(request);
724 result = EntityHandlerCodeToOCStackCode(ehResult);
730 HandleResourceWithEntityHandler (OCServerRequest *request,
731 OCResource *resource,
732 uint8_t collectionResource)
734 if(!request || ! resource)
736 return OC_STACK_INVALID_PARAM;
739 OCStackResult result = OC_STACK_ERROR;
740 OCEntityHandlerResult ehResult = OC_EH_ERROR;
741 OCEntityHandlerFlag ehFlag = OC_REQUEST_FLAG;
742 ResourceObserver *resObs = NULL;
744 OCEntityHandlerRequest ehRequest = {};
746 OC_LOG(INFO, TAG, PCF("Entering HandleResourceWithEntityHandler"));
747 result = FormOCEntityHandlerRequest(&ehRequest, (OCRequestHandle) request,
748 request->method, (OCResourceHandle) resource, request->query,
749 request->reqJSONPayload, request->numRcvdVendorSpecificHeaderOptions,
750 request->rcvdVendorSpecificHeaderOptions,
751 (OCObserveAction)request->observationOption, 0);
752 VERIFY_SUCCESS(result, OC_STACK_OK);
754 if(ehRequest.obsInfo.action == OC_OBSERVE_NO_OPTION)
756 OC_LOG(INFO, TAG, PCF("No observation requested"));
757 ehFlag = OC_REQUEST_FLAG;
759 else if(ehRequest.obsInfo.action == OC_OBSERVE_REGISTER &&
762 OC_LOG(INFO, TAG, PCF("Registering observation requested"));
763 result = GenerateObserverId(&ehRequest.obsInfo.obsId);
764 VERIFY_SUCCESS(result, OC_STACK_OK);
766 result = AddObserver ((const char*)(request->resourceUrl),
767 (const char *)(request->query),
768 ehRequest.obsInfo.obsId, request->requestToken, request->tokenLength,
769 resource, request->qos,
770 &request->addressInfo, request->connectivityType);
772 if(result == OC_STACK_OK)
774 OC_LOG(INFO, TAG, PCF("Added observer successfully"));
775 request->observeResult = OC_STACK_OK;
776 ehFlag = (OCEntityHandlerFlag)(OC_REQUEST_FLAG | OC_OBSERVE_FLAG);
780 result = OC_STACK_OK;
781 // The error in observeResult for the request will be
782 // used when responding to this request by omitting
783 // the observation option/sequence number.
784 request->observeResult = OC_STACK_ERROR;
785 OC_LOG(ERROR, TAG, PCF("Observer Addition failed"));
786 ehFlag = OC_REQUEST_FLAG;
790 else if(ehRequest.obsInfo.action == OC_OBSERVE_DEREGISTER &&
793 OC_LOG(INFO, TAG, PCF("Deregistering observation requested"));
795 resObs = GetObserverUsingToken (request->requestToken, request->tokenLength);
799 // Stack does not contain this observation request
800 // Either token is incorrect or observation list is corrupted
801 result = OC_STACK_ERROR;
804 ehRequest.obsInfo.obsId = resObs->observeId;
805 ehFlag = (OCEntityHandlerFlag)(ehFlag | OC_OBSERVE_FLAG);
807 result = DeleteObserverUsingToken (request->requestToken, request->tokenLength);
809 if(result == OC_STACK_OK)
811 OC_LOG(INFO, TAG, PCF("Removed observer successfully"));
812 request->observeResult = OC_STACK_OK;
816 result = OC_STACK_OK;
817 request->observeResult = OC_STACK_ERROR;
818 OC_LOG(ERROR, TAG, PCF("Observer Removal failed"));
823 result = OC_STACK_ERROR;
827 ehResult = resource->entityHandler(ehFlag, &ehRequest);
828 if(ehResult == OC_EH_SLOW)
830 OC_LOG(INFO, TAG, PCF("This is a slow resource"));
831 request->slowFlag = 1;
833 else if(ehResult == OC_EH_ERROR)
835 FindAndDeleteServerRequest(request);
837 result = EntityHandlerCodeToOCStackCode(ehResult);
843 HandleCollectionResourceDefaultEntityHandler (OCServerRequest *request,
844 OCResource *resource)
846 if(!request || !resource)
848 return OC_STACK_INVALID_PARAM;
851 OCStackResult result = OC_STACK_ERROR;
852 OCEntityHandlerRequest ehRequest = {};
854 result = FormOCEntityHandlerRequest(&ehRequest, (OCRequestHandle) request,
855 request->method, (OCResourceHandle) resource, request->query,
856 request->reqJSONPayload, request->numRcvdVendorSpecificHeaderOptions,
857 request->rcvdVendorSpecificHeaderOptions,
858 (OCObserveAction)request->observationOption, (OCObservationId) 0);
859 if(result != OC_STACK_OK)
864 return (DefaultCollectionEntityHandler (OC_REQUEST_FLAG, &ehRequest));
868 ProcessRequest(ResourceHandling resHandling, OCResource *resource, OCServerRequest *request)
870 OCStackResult ret = OC_STACK_OK;
874 case OC_RESOURCE_VIRTUAL:
876 ret = HandleVirtualResource (request, resource);
879 case OC_RESOURCE_DEFAULT_DEVICE_ENTITYHANDLER:
881 ret = HandleDefaultDeviceEntityHandler(request);
884 case OC_RESOURCE_NOT_COLLECTION_DEFAULT_ENTITYHANDLER:
886 OC_LOG(INFO, TAG, PCF("OC_RESOURCE_NOT_COLLECTION_DEFAULT_ENTITYHANDLER"));
887 return OC_STACK_ERROR;
889 case OC_RESOURCE_NOT_COLLECTION_WITH_ENTITYHANDLER:
891 ret = HandleResourceWithEntityHandler (request, resource, 0);
894 case OC_RESOURCE_COLLECTION_WITH_ENTITYHANDLER:
896 ret = HandleResourceWithEntityHandler (request, resource, 1);
899 case OC_RESOURCE_COLLECTION_DEFAULT_ENTITYHANDLER:
901 ret = HandleCollectionResourceDefaultEntityHandler (request, resource);
904 case OC_RESOURCE_NOT_SPECIFIED:
906 ret = OC_STACK_NO_RESOURCE;
911 OC_LOG(INFO, TAG, PCF("Invalid Resource Determination"));
912 return OC_STACK_ERROR;
918 void DeleteDeviceInfo()
922 cJSON_Delete(savedDeviceInfo);
926 OCStackResult SaveDeviceInfo(OCDeviceInfo deviceInfo)
930 savedDeviceInfo = cJSON_CreateObject();
931 cJSON *repObj = NULL;
933 cJSON_AddItemToObject (savedDeviceInfo, OC_RSRVD_HREF,
934 cJSON_CreateString(GetVirtualResourceUri(OC_DEVICE_URI)));
936 cJSON_AddItemToObject (savedDeviceInfo, OC_RSRVD_REPRESENTATION, repObj = cJSON_CreateObject());
938 if (deviceInfo.contentType)
940 cJSON_AddItemToObject (repObj, OC_RSRVD_CONTENT_TYPE,
941 cJSON_CreateString(deviceInfo.contentType));
944 if (deviceInfo.dateOfManufacture)
946 cJSON_AddItemToObject (repObj, OC_RSRVD_MFG_DATE,
947 cJSON_CreateString(deviceInfo.dateOfManufacture));
950 if (deviceInfo.deviceName)
952 cJSON_AddItemToObject (repObj, OC_RSRVD_DEVICE_NAME,
953 cJSON_CreateString(deviceInfo.deviceName));
956 if (deviceInfo.deviceUUID)
958 cJSON_AddItemToObject (repObj, OC_RSRVD_DEVICE_ID,
959 cJSON_CreateString(deviceInfo.deviceUUID));
962 if (deviceInfo.firmwareVersion)
964 cJSON_AddItemToObject (repObj, OC_RSRVD_FW_VERSION,
965 cJSON_CreateString(deviceInfo.firmwareVersion));
968 if (deviceInfo.hostName)
970 cJSON_AddItemToObject (repObj, OC_RSRVD_HOST_NAME,
971 cJSON_CreateString(deviceInfo.hostName));
974 if (deviceInfo.manufacturerName)
976 if(strlen(deviceInfo.manufacturerName) > MAX_MANUFACTURER_NAME_LENGTH)
979 return OC_STACK_INVALID_PARAM;
982 cJSON_AddItemToObject (repObj, OC_RSRVD_MFG_NAME,
983 cJSON_CreateString(deviceInfo.manufacturerName));
986 if (deviceInfo.manufacturerUrl)
988 if(strlen(deviceInfo.manufacturerUrl) > MAX_MANUFACTURER_URL_LENGTH)
991 return OC_STACK_INVALID_PARAM;
994 cJSON_AddItemToObject (repObj, OC_RSRVD_MFG_URL,
995 cJSON_CreateString(deviceInfo.manufacturerUrl));
998 if (deviceInfo.modelNumber)
1000 cJSON_AddItemToObject (repObj, OC_RSRVD_MODEL_NUM,
1001 cJSON_CreateString(deviceInfo.modelNumber));
1004 if (deviceInfo.platformVersion)
1006 cJSON_AddItemToObject (repObj, OC_RSRVD_PLATFORM_VERSION,
1007 cJSON_CreateString(deviceInfo.platformVersion));
1010 if (deviceInfo.supportUrl)
1012 cJSON_AddItemToObject (repObj, OC_RSRVD_SUPPORT_URL,
1013 cJSON_CreateString(deviceInfo.supportUrl));
1016 if (deviceInfo.version)
1018 cJSON_AddItemToObject (repObj, OC_RSRVD_VERSION,
1019 cJSON_CreateString(deviceInfo.version));