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[] =
64 //-----------------------------------------------------------------------------
65 // Default resource entity handler function
66 //-----------------------------------------------------------------------------
67 OCEntityHandlerResult defaultResourceEHandler(OCEntityHandlerFlag flag,
68 OCEntityHandlerRequest * request)
70 //TODO ("Implement me!!!!");
71 // TODO: remove silence unused param warnings
74 return OC_EH_OK; // Making sure that the Default EH and the Vendor EH have matching signatures
77 /* This method will retrieve the port at which the secure resource is hosted */
78 static OCStackResult GetSecurePortInfo(CAConnectivityType_t connType, uint16_t *port)
80 CALocalConnectivity_t* info = NULL;
82 OCStackResult ret = OC_STACK_ERROR;
84 CAResult_t caResult = CAGetNetworkInformation(&info, &size);
85 if ((caResult == CA_STATUS_OK) && info && size)
89 if (info[size].isSecured && info[size].type == connType)
91 if (info[size].type == CA_ETHERNET ||
92 info[size].type == CA_WIFI)
94 *port = info[size].addressInfo.IP.port;
106 static OCStackResult ValidateUrlQuery (char *url, char *query,
107 uint8_t *filterOn, char **filterValue)
109 if(!filterOn || !filterValue)
111 return OC_STACK_INVALID_PARAM;
114 char *filterParam = NULL;
116 OC_LOG(INFO, TAG, PCF("Entering ValidateUrlQuery"));
119 return OC_STACK_INVALID_URI;
122 if (strcmp ((char *)url, GetVirtualResourceUri(OC_WELL_KNOWN_URI)) == 0 ||
123 strcmp ((char *)url, GetVirtualResourceUri(OC_DEVICE_URI)) == 0)
125 *filterOn = STACK_RES_DISCOVERY_NOFILTER;
128 char* strTokPtr = NULL;
129 filterParam = strtok_r((char *)query, "=", &strTokPtr);
130 *filterValue = strtok_r(NULL, " ", &strTokPtr);
132 if (!(*filterValue) || ! filterParam)
134 return OC_STACK_INVALID_QUERY;
136 else if (strcmp (filterParam, OC_RSRVD_INTERFACE) == 0)
138 // Resource discovery with interface filter
139 *filterOn = STACK_RES_DISCOVERY_IF_FILTER;
141 else if (strcmp (filterParam, OC_RSRVD_RESOURCE_TYPE) == 0)
143 // Resource discovery with resource type filter
144 *filterOn = STACK_RES_DISCOVERY_RT_FILTER;
146 else if (strcmp (filterParam, OC_RSRVD_DEVICE_ID) == 0)
149 *filterOn = STACK_DEVICE_DISCOVERY_DI_FILTER;
151 else if (strcmp (filterParam, OC_RSRVD_DEVICE_NAME) == 0)
154 *filterOn = STACK_DEVICE_DISCOVERY_DN_FILTER;
158 // Other filter types not supported
159 return OC_STACK_INVALID_QUERY;
164 else if (strcmp((char *)url, GetVirtualResourceUri(OC_PRESENCE)) == 0)
166 //Nothing needs to be done, except for pass a OC_PRESENCE query through as OC_STACK_OK.
167 OC_LOG(INFO, TAG, PCF("OC_PRESENCE Request"));
168 *filterOn = STACK_RES_DISCOVERY_NOFILTER;
173 // Other URIs not yet supported
174 return OC_STACK_INVALID_URI;
176 OC_LOG(INFO, TAG, PCF("Exiting ValidateUrlQuery"));
182 BuildVirtualResourceResponse(const OCResource *resourcePtr, uint8_t filterOn,
183 const char *filterValue, char *out, uint16_t *remaining,
184 CAConnectivityType_t connType )
186 if(!resourcePtr || !out || !remaining)
188 return OC_STACK_INVALID_PARAM;
191 OCResourceType *resourceTypePtr = NULL;
192 OCResourceInterface *interfacePtr = NULL;
193 cJSON *resObj = NULL;
194 cJSON *propObj = NULL;
195 cJSON *rtArray = NULL;
196 char *jsonStr = NULL;
197 uint8_t encodeRes = 0;
198 OCStackResult ret = OC_STACK_OK;
199 uint16_t jsonLen = 0;
201 OC_LOG(INFO, TAG, PCF("Entering BuildVirtualResourceResponse"));
202 resObj = cJSON_CreateObject();
207 if ((filterOn == STACK_RES_DISCOVERY_RT_FILTER) && filterValue)
209 resourceTypePtr = resourcePtr->rsrcType;
210 while (resourceTypePtr)
212 if (strcmp (resourceTypePtr->resourcetypename, filterValue) == 0)
217 resourceTypePtr = resourceTypePtr->next;
220 else if ((filterOn == STACK_RES_DISCOVERY_IF_FILTER) && filterValue)
222 interfacePtr = resourcePtr->rsrcInterface;
225 if (strcmp (interfacePtr->name, filterValue) == 0)
230 interfacePtr = interfacePtr->next;
233 else if (filterOn == STACK_RES_DISCOVERY_NOFILTER)
239 //TODO: Unsupported query filter
240 return OC_STACK_INVALID_QUERY;
246 cJSON_AddItemToObject (resObj, OC_RSRVD_HREF, cJSON_CreateString(resourcePtr->uri));
248 // Add server instance id
249 cJSON_AddItemToObject (resObj,
250 OC_RSRVD_SERVER_INSTANCE_ID,
251 cJSON_CreateString(OCGetServerInstanceIDString()));
253 cJSON_AddItemToObject (resObj, OC_RSRVD_PROPERTY, propObj = cJSON_CreateObject());
254 // Add resource types
255 cJSON_AddItemToObject (propObj, OC_RSRVD_RESOURCE_TYPE, rtArray = cJSON_CreateArray());
256 resourceTypePtr = resourcePtr->rsrcType;
257 while (resourceTypePtr)
259 cJSON_AddItemToArray (rtArray,
260 cJSON_CreateString(resourceTypePtr->resourcetypename));
261 resourceTypePtr = resourceTypePtr->next;
263 // Add interface types
264 cJSON_AddItemToObject (propObj, OC_RSRVD_INTERFACE, rtArray = cJSON_CreateArray());
265 interfacePtr = resourcePtr->rsrcInterface;
268 cJSON_AddItemToArray (rtArray, cJSON_CreateString(interfacePtr->name));
269 interfacePtr = interfacePtr->next;
271 // If resource is observable, set observability flag.
272 // Resources that are not observable will not have the flag.
273 if (resourcePtr->resourceProperties & OC_OBSERVABLE)
275 cJSON_AddItemToObject (propObj, OC_RSRVD_OBSERVABLE,
276 cJSON_CreateNumber(OC_RESOURCE_OBSERVABLE));
278 // Set secure flag for secure resources
279 if (resourcePtr->resourceProperties & OC_SECURE)
281 cJSON_AddNumberToObject (propObj, OC_RSRVD_SECURE, OC_RESOURCE_SECURE);
282 //Set the IP port also as secure resources are hosted on a different port
284 if (GetSecurePortInfo (connType, &port) == OC_STACK_OK)
286 cJSON_AddNumberToObject (propObj, OC_RSRVD_HOSTING_PORT, port);
292 jsonStr = cJSON_PrintUnformatted (resObj);
294 jsonLen = strlen(jsonStr);
295 if (jsonLen < *remaining)
297 strcpy(out, jsonStr);
298 *remaining = *remaining - jsonLen;
302 ret = OC_STACK_ERROR;
304 cJSON_Delete (resObj);
307 OC_LOG(INFO, TAG, PCF("Exiting BuildVirtualResourceResponse"));
311 OCStackResult BuildVirtualResourceResponseForDevice(uint8_t filterOn, char *filterValue,
312 char *out, uint16_t *remaining)
314 if(!out || !remaining)
316 return OC_STACK_INVALID_PARAM;
319 OCStackResult ret = OC_STACK_ERROR;
321 if (savedDeviceInfo != NULL)
323 char *jsonStr = NULL;
324 uint16_t jsonLen = 0;
325 cJSON *repObj = cJSON_GetObjectItem(savedDeviceInfo, OC_RSRVD_REPRESENTATION);
327 OC_LOG(INFO, TAG, PCF("Entering BuildVirtualResourceResponseForDevice"));
329 if ((filterOn == STACK_DEVICE_DISCOVERY_DI_FILTER) && filterValue)
331 if((cJSON_GetObjectItem(repObj,OC_RSRVD_DEVICE_ID) != NULL) &&
332 strcmp(cJSON_GetObjectItem(repObj,OC_RSRVD_DEVICE_ID)->valuestring, filterValue)
338 else if ((filterOn == STACK_DEVICE_DISCOVERY_DN_FILTER) && filterValue)
340 if((cJSON_GetObjectItem(repObj,OC_RSRVD_DEVICE_NAME) != NULL) &&
341 strcmp(cJSON_GetObjectItem(repObj,OC_RSRVD_DEVICE_NAME)->valuestring,
347 else if (filterOn == STACK_RES_DISCOVERY_NOFILTER)
353 ret = OC_STACK_INVALID_QUERY;
356 if (ret == OC_STACK_OK)
358 jsonStr = cJSON_PrintUnformatted (savedDeviceInfo);
362 jsonLen = strlen(jsonStr);
364 if (jsonLen < *remaining)
366 strncpy(out, jsonStr, (jsonLen + 1));
367 *remaining = *remaining - jsonLen;
372 ret = OC_STACK_ERROR;
379 ret = OC_STACK_ERROR;
384 ret = OC_STACK_INVALID_DEVICE_INFO;
389 //error so that stack won't respond with empty payload
390 ret = OC_STACK_INVALID_DEVICE_INFO;
393 OC_LOG(INFO, TAG, PCF("Exiting BuildVirtualResourceResponseForDevice"));
397 const char * GetVirtualResourceUri( OCVirtualResources resource)
399 if (resource < OC_MAX_VIRTUAL_RESOURCES)
401 return VIRTUAL_RSRCS[resource];
407 bool IsVirtualResource(const char* resourceUri)
414 for (int i = 0; i < OC_MAX_VIRTUAL_RESOURCES; i++)
416 if (strcmp(resourceUri, GetVirtualResourceUri((OCVirtualResources)i)) == 0)
424 uint8_t IsCollectionResource (OCResource *resource)
431 for (int i = 0; i < MAX_CONTAINED_RESOURCES; i++)
433 if (resource->rsrcResources[i])
441 OCResource *FindResourceByUri(const char* resourceUri)
448 OCResource * pointer = headResource;
451 if (strcmp(resourceUri, pointer->uri) == 0)
455 pointer = pointer->next;
457 OC_LOG(INFO, TAG, PCF("Resource not found"));
462 OCStackResult DetermineResourceHandling (const OCServerRequest *request,
463 ResourceHandling *handling,
464 OCResource **resource)
466 if(!request || !handling || !resource)
468 return OC_STACK_INVALID_PARAM;
471 OC_LOG(INFO, TAG, PCF("Entering DetermineResourceHandling"));
473 // Check if virtual resource
474 if (IsVirtualResource((const char*)request->resourceUrl))
476 *handling = OC_RESOURCE_VIRTUAL;
477 *resource = headResource;
480 if (NULL == request->resourceUrl || (strlen((const char*)(request->resourceUrl)) == 0))
482 // Resource URL not specified
483 *handling = OC_RESOURCE_NOT_SPECIFIED;
484 return OC_STACK_NO_RESOURCE;
488 OCResource *resourcePtr = NULL;
489 resourcePtr = FindResourceByUri((const char*)request->resourceUrl);
490 *resource = resourcePtr;
493 if(defaultDeviceHandler)
495 *handling = OC_RESOURCE_DEFAULT_DEVICE_ENTITYHANDLER;
499 // Resource does not exist
500 // and default device handler does not exist
501 *handling = OC_RESOURCE_NOT_SPECIFIED;
502 return OC_STACK_NO_RESOURCE;
505 // secure resource will entertain only authorized requests
506 if ((resourcePtr->resourceProperties & OC_SECURE) && (request->secured == 0))
508 OC_LOG(ERROR, TAG, PCF("Un-authorized request. Ignoring"));
509 return OC_STACK_RESOURCE_ERROR;
512 if (IsCollectionResource (resourcePtr))
514 // Collection resource
515 if (resourcePtr->entityHandler != defaultResourceEHandler)
517 *handling = OC_RESOURCE_COLLECTION_WITH_ENTITYHANDLER;
522 *handling = OC_RESOURCE_COLLECTION_DEFAULT_ENTITYHANDLER;
528 // Resource not a collection
529 if (resourcePtr->entityHandler != defaultResourceEHandler)
531 *handling = OC_RESOURCE_NOT_COLLECTION_WITH_ENTITYHANDLER;
536 *handling = OC_RESOURCE_NOT_COLLECTION_DEFAULT_ENTITYHANDLER;
543 OCStackResult EntityHandlerCodeToOCStackCode(OCEntityHandlerResult ehResult)
545 OCStackResult result;
550 result = OC_STACK_OK;
553 result = OC_STACK_SLOW_RESOURCE;
556 result = OC_STACK_ERROR;
558 case OC_EH_FORBIDDEN:
559 result = OC_STACK_RESOURCE_ERROR;
561 case OC_EH_RESOURCE_CREATED:
562 result = OC_STACK_RESOURCE_CREATED;
564 case OC_EH_RESOURCE_DELETED:
565 result = OC_STACK_RESOURCE_DELETED;
567 case OC_EH_RESOURCE_NOT_FOUND:
568 result = OC_STACK_NO_RESOURCE;
571 result = OC_STACK_ERROR;
578 HandleVirtualResource (OCServerRequest *request, OCResource* resource)
580 if(!request || !resource)
582 return OC_STACK_INVALID_PARAM;
585 OCStackResult result = OC_STACK_ERROR;
586 char *filterValue = NULL;
587 uint8_t filterOn = 0;
588 uint16_t remaining = 0;
590 uint8_t firstLoopDone = 0;
591 char discoveryResBuf[MAX_RESPONSE_LENGTH] = {};
593 OC_LOG(INFO, TAG, PCF("Entering HandleVirtualResource"));
595 result = ValidateUrlQuery (request->resourceUrl,
596 request->query, &filterOn,
599 if (result == OC_STACK_OK)
601 if (strcmp ((char *)request->resourceUrl, GetVirtualResourceUri(OC_WELL_KNOWN_URI)) == 0)
603 ptr = discoveryResBuf;
604 remaining = MAX_RESPONSE_LENGTH;
606 // Check if valid resource and enough space in buffer for atleast
607 // the null character.
608 while(resource && (remaining > 1))
610 if((resource->resourceProperties & OC_ACTIVE)
611 && (resource->resourceProperties & OC_DISCOVERABLE))
613 // if there is data on the buffer, we have already added a response,
614 // so we need to add a comma before we do anything
616 && remaining >= (sizeof(OC_JSON_SEPARATOR)+1))
618 *ptr = OC_JSON_SEPARATOR;
623 result = BuildVirtualResourceResponse(resource, filterOn, filterValue,
624 (char*)ptr, &remaining, request->connectivityType );
626 if (result != OC_STACK_OK)
628 // if this failed, we need to remove the comma added above.
637 ptr += strlen((char *)ptr);
639 resource = resource->next;
642 if(strlen((const char *)discoveryResBuf) > 0)
644 OCEntityHandlerResponse response = {};
646 response.ehResult = OC_EH_OK;
647 response.payload = discoveryResBuf;
648 response.payloadSize = strlen((const char *)discoveryResBuf) + 1;
649 response.persistentBufferFlag = 0;
650 response.requestHandle = (OCRequestHandle) request;
651 response.resourceHandle = (OCResourceHandle) resource;
653 result = OCDoResponse(&response);
656 else if (strcmp ((char *)request->resourceUrl, GetVirtualResourceUri(OC_DEVICE_URI)) == 0)
658 remaining = MAX_RESPONSE_LENGTH;
659 ptr = discoveryResBuf;
661 result = BuildVirtualResourceResponseForDevice(filterOn, filterValue,
662 (char*)ptr, &remaining);
664 if(result == OC_STACK_OK)
666 ptr += strlen((char*)ptr);
669 if(remaining < MAX_RESPONSE_LENGTH)
671 OCEntityHandlerResponse response = {0};
673 response.ehResult = OC_EH_OK;
674 response.payload = discoveryResBuf;
675 response.payloadSize = strlen((const char *)discoveryResBuf) + 1;
676 response.persistentBufferFlag = 0;
677 response.requestHandle = (OCRequestHandle) request;
678 response.resourceHandle = (OCResourceHandle) resource;
680 result = OCDoResponse(&response);
683 else if (strcmp ((char *)request->resourceUrl, GetVirtualResourceUri(OC_DEVICE_URI)) == 0)
685 remaining = MAX_RESPONSE_LENGTH;
686 ptr = discoveryResBuf;
688 result = BuildVirtualResourceResponseForDevice(filterOn, filterValue,
689 (char*)ptr, &remaining);
691 if(result == OC_STACK_OK)
693 ptr += strlen((char*)ptr);
696 if(remaining < MAX_RESPONSE_LENGTH)
698 OCEntityHandlerResponse response = {0};
700 response.ehResult = OC_EH_OK;
701 response.payload = discoveryResBuf;
702 response.payloadSize = strlen((const char *)discoveryResBuf) + 1;
703 response.persistentBufferFlag = 0;
704 response.requestHandle = (OCRequestHandle) request;
705 response.resourceHandle = (OCResourceHandle) resource;
707 result = OCDoResponse(&response);
713 if(resource->resourceProperties & OC_ACTIVE){
714 SendPresenceNotification(NULL);
719 result = OC_STACK_OK;
724 HandleDefaultDeviceEntityHandler (OCServerRequest *request)
728 return OC_STACK_INVALID_PARAM;
731 OCStackResult result = OC_STACK_OK;
732 OCEntityHandlerResult ehResult = OC_EH_ERROR;
733 OCEntityHandlerRequest ehRequest = {};
735 OC_LOG(INFO, TAG, PCF("Entering HandleResourceWithDefaultDeviceEntityHandler"));
736 result = FormOCEntityHandlerRequest(&ehRequest, (OCRequestHandle) request,
737 request->method, (OCResourceHandle) NULL, request->query,
738 request->reqJSONPayload, request->numRcvdVendorSpecificHeaderOptions,
739 request->rcvdVendorSpecificHeaderOptions,
740 (OCObserveAction)request->observationOption, (OCObservationId)0);
741 VERIFY_SUCCESS(result, OC_STACK_OK);
743 // At this point we know for sure that defaultDeviceHandler exists
744 ehResult = defaultDeviceHandler(OC_REQUEST_FLAG, &ehRequest,
745 (char*) request->resourceUrl);
746 if(ehResult == OC_EH_SLOW)
748 OC_LOG(INFO, TAG, PCF("This is a slow resource"));
749 request->slowFlag = 1;
751 else if(ehResult == OC_EH_ERROR)
753 FindAndDeleteServerRequest(request);
755 result = EntityHandlerCodeToOCStackCode(ehResult);
761 HandleResourceWithEntityHandler (OCServerRequest *request,
762 OCResource *resource,
763 uint8_t collectionResource)
765 if(!request || ! resource)
767 return OC_STACK_INVALID_PARAM;
770 OCStackResult result = OC_STACK_ERROR;
771 OCEntityHandlerResult ehResult = OC_EH_ERROR;
772 OCEntityHandlerFlag ehFlag = OC_REQUEST_FLAG;
773 ResourceObserver *resObs = NULL;
775 OCEntityHandlerRequest ehRequest = {};
777 OC_LOG(INFO, TAG, PCF("Entering HandleResourceWithEntityHandler"));
778 result = FormOCEntityHandlerRequest(&ehRequest, (OCRequestHandle) request,
779 request->method, (OCResourceHandle) resource, request->query,
780 request->reqJSONPayload, request->numRcvdVendorSpecificHeaderOptions,
781 request->rcvdVendorSpecificHeaderOptions,
782 (OCObserveAction)request->observationOption, 0);
783 VERIFY_SUCCESS(result, OC_STACK_OK);
785 if(ehRequest.obsInfo.action == OC_OBSERVE_NO_OPTION)
787 OC_LOG(INFO, TAG, PCF("No observation requested"));
788 ehFlag = OC_REQUEST_FLAG;
790 else if(ehRequest.obsInfo.action == OC_OBSERVE_REGISTER &&
793 OC_LOG(INFO, TAG, PCF("Registering observation requested"));
794 result = GenerateObserverId(&ehRequest.obsInfo.obsId);
795 VERIFY_SUCCESS(result, OC_STACK_OK);
797 result = AddObserver ((const char*)(request->resourceUrl),
798 (const char *)(request->query),
799 ehRequest.obsInfo.obsId, request->requestToken, request->tokenLength,
800 resource, request->qos,
801 &request->addressInfo, request->connectivityType);
803 if(result == OC_STACK_OK)
805 OC_LOG(INFO, TAG, PCF("Added observer successfully"));
806 request->observeResult = OC_STACK_OK;
807 ehFlag = (OCEntityHandlerFlag)(OC_REQUEST_FLAG | OC_OBSERVE_FLAG);
811 result = OC_STACK_OK;
812 // The error in observeResult for the request will be
813 // used when responding to this request by omitting
814 // the observation option/sequence number.
815 request->observeResult = OC_STACK_ERROR;
816 OC_LOG(ERROR, TAG, PCF("Observer Addition failed"));
817 ehFlag = OC_REQUEST_FLAG;
821 else if(ehRequest.obsInfo.action == OC_OBSERVE_DEREGISTER &&
824 OC_LOG(INFO, TAG, PCF("Deregistering observation requested"));
826 resObs = GetObserverUsingToken (request->requestToken, request->tokenLength);
830 // Stack does not contain this observation request
831 // Either token is incorrect or observation list is corrupted
832 result = OC_STACK_ERROR;
835 ehRequest.obsInfo.obsId = resObs->observeId;
836 ehFlag = (OCEntityHandlerFlag)(ehFlag | OC_OBSERVE_FLAG);
838 result = DeleteObserverUsingToken (request->requestToken, request->tokenLength);
840 if(result == OC_STACK_OK)
842 OC_LOG(INFO, TAG, PCF("Removed observer successfully"));
843 request->observeResult = OC_STACK_OK;
847 result = OC_STACK_OK;
848 request->observeResult = OC_STACK_ERROR;
849 OC_LOG(ERROR, TAG, PCF("Observer Removal failed"));
854 result = OC_STACK_ERROR;
858 ehResult = resource->entityHandler(ehFlag, &ehRequest);
859 if(ehResult == OC_EH_SLOW)
861 OC_LOG(INFO, TAG, PCF("This is a slow resource"));
862 request->slowFlag = 1;
864 else if(ehResult == OC_EH_ERROR)
866 FindAndDeleteServerRequest(request);
868 result = EntityHandlerCodeToOCStackCode(ehResult);
874 HandleCollectionResourceDefaultEntityHandler (OCServerRequest *request,
875 OCResource *resource)
877 if(!request || !resource)
879 return OC_STACK_INVALID_PARAM;
882 OCStackResult result = OC_STACK_ERROR;
883 OCEntityHandlerRequest ehRequest = {};
885 result = FormOCEntityHandlerRequest(&ehRequest, (OCRequestHandle) request,
886 request->method, (OCResourceHandle) resource, request->query,
887 request->reqJSONPayload, request->numRcvdVendorSpecificHeaderOptions,
888 request->rcvdVendorSpecificHeaderOptions,
889 (OCObserveAction)request->observationOption, (OCObservationId) 0);
890 if(result != OC_STACK_OK)
895 return (DefaultCollectionEntityHandler (OC_REQUEST_FLAG, &ehRequest));
899 ProcessRequest(ResourceHandling resHandling, OCResource *resource, OCServerRequest *request)
901 OCStackResult ret = OC_STACK_OK;
905 case OC_RESOURCE_VIRTUAL:
907 ret = HandleVirtualResource (request, resource);
910 case OC_RESOURCE_DEFAULT_DEVICE_ENTITYHANDLER:
912 ret = HandleDefaultDeviceEntityHandler(request);
915 case OC_RESOURCE_NOT_COLLECTION_DEFAULT_ENTITYHANDLER:
917 OC_LOG(INFO, TAG, PCF("OC_RESOURCE_NOT_COLLECTION_DEFAULT_ENTITYHANDLER"));
918 return OC_STACK_ERROR;
920 case OC_RESOURCE_NOT_COLLECTION_WITH_ENTITYHANDLER:
922 ret = HandleResourceWithEntityHandler (request, resource, 0);
925 case OC_RESOURCE_COLLECTION_WITH_ENTITYHANDLER:
927 ret = HandleResourceWithEntityHandler (request, resource, 1);
930 case OC_RESOURCE_COLLECTION_DEFAULT_ENTITYHANDLER:
932 ret = HandleCollectionResourceDefaultEntityHandler (request, resource);
935 case OC_RESOURCE_NOT_SPECIFIED:
937 ret = OC_STACK_NO_RESOURCE;
942 OC_LOG(INFO, TAG, PCF("Invalid Resource Determination"));
943 return OC_STACK_ERROR;
949 void DeleteDeviceInfo()
953 cJSON_Delete(savedDeviceInfo);
957 OCStackResult SaveDeviceInfo(OCDeviceInfo deviceInfo)
961 savedDeviceInfo = cJSON_CreateObject();
962 cJSON *repObj = NULL;
964 cJSON_AddItemToObject (savedDeviceInfo, OC_RSRVD_HREF,
965 cJSON_CreateString(GetVirtualResourceUri(OC_DEVICE_URI)));
967 cJSON_AddItemToObject (savedDeviceInfo, OC_RSRVD_REPRESENTATION, repObj = cJSON_CreateObject());
969 if (deviceInfo.contentType)
971 cJSON_AddItemToObject (repObj, OC_RSRVD_CONTENT_TYPE,
972 cJSON_CreateString(deviceInfo.contentType));
975 if (deviceInfo.dateOfManufacture)
977 cJSON_AddItemToObject (repObj, OC_RSRVD_MFG_DATE,
978 cJSON_CreateString(deviceInfo.dateOfManufacture));
981 if (deviceInfo.deviceName)
983 cJSON_AddItemToObject (repObj, OC_RSRVD_DEVICE_NAME,
984 cJSON_CreateString(deviceInfo.deviceName));
987 if (deviceInfo.deviceUUID)
989 cJSON_AddItemToObject (repObj, OC_RSRVD_DEVICE_ID,
990 cJSON_CreateString(deviceInfo.deviceUUID));
993 if (deviceInfo.firmwareVersion)
995 cJSON_AddItemToObject (repObj, OC_RSRVD_FW_VERSION,
996 cJSON_CreateString(deviceInfo.firmwareVersion));
999 if (deviceInfo.hostName)
1001 cJSON_AddItemToObject (repObj, OC_RSRVD_HOST_NAME,
1002 cJSON_CreateString(deviceInfo.hostName));
1005 if (deviceInfo.manufacturerName)
1007 if(strlen(deviceInfo.manufacturerName) > MAX_MANUFACTURER_NAME_LENGTH)
1010 return OC_STACK_INVALID_PARAM;
1013 cJSON_AddItemToObject (repObj, OC_RSRVD_MFG_NAME,
1014 cJSON_CreateString(deviceInfo.manufacturerName));
1017 if (deviceInfo.manufacturerUrl)
1019 if(strlen(deviceInfo.manufacturerUrl) > MAX_MANUFACTURER_URL_LENGTH)
1022 return OC_STACK_INVALID_PARAM;
1025 cJSON_AddItemToObject (repObj, OC_RSRVD_MFG_URL,
1026 cJSON_CreateString(deviceInfo.manufacturerUrl));
1029 if (deviceInfo.modelNumber)
1031 cJSON_AddItemToObject (repObj, OC_RSRVD_MODEL_NUM,
1032 cJSON_CreateString(deviceInfo.modelNumber));
1035 if (deviceInfo.platformVersion)
1037 cJSON_AddItemToObject (repObj, OC_RSRVD_PLATFORM_VERSION,
1038 cJSON_CreateString(deviceInfo.platformVersion));
1041 if (deviceInfo.supportUrl)
1043 cJSON_AddItemToObject (repObj, OC_RSRVD_SUPPORT_URL,
1044 cJSON_CreateString(deviceInfo.supportUrl));
1047 if (deviceInfo.version)
1049 cJSON_AddItemToObject (repObj, OC_RSRVD_VERSION,
1050 cJSON_CreateString(deviceInfo.version));