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 #define _POSIX_C_SOURCE 200112L
24 #include "ocstackinternal.h"
25 #include "ocresourcehandler.h"
31 #define WITH_GROUPACTION 1
35 #define TAG PCF("occollection")
37 #define NUM_PARAM_IN_QUERY 2
39 static OCStackResult CheckRTParamSupport(const OCResource* resource, const char* rtPtr)
41 OCResourceType* rTPointer = resource->rsrcType;
44 if( strcmp (rTPointer->resourcetypename, rtPtr) == 0)
47 rTPointer = rTPointer->next;
49 return OC_STACK_ERROR;
52 static OCStackResult CheckIFParamSupport(const OCResource* resource, const char* ifPtr)
54 OCResourceInterface* iFPointer = resource->rsrcInterface;
57 if( strcmp (iFPointer->name, ifPtr) == 0)
60 iFPointer = iFPointer->next;
62 return OC_STACK_ERROR;
66 ValidateQuery (const unsigned char *query, OCResourceHandle resource,
67 OCStackIfTypes *ifParam, char **rtParam)
69 uint8_t numFields = 0, numParam;
71 //TODO: Query and URL validation is being done for virtual resource case
72 // using ValidateUrlQuery function. We should be able to merge it with this
74 OC_LOG(INFO, TAG, PCF("Entering ValidateQuery"));
77 return OC_STACK_ERROR;
81 // Query string is empty
82 OC_LOG_V(INFO, TAG, PCF("Empty query string, use default IF and RT"));
83 *ifParam = STACK_IF_DEFAULT;
84 *rtParam = (char *) OCGetResourceTypeName (resource, 0);
88 // Break the query string to validate it and determine IF and RT parameters
89 // Validate there are atmost 2 parameters in string and that one is 'if' and other 'rt'
90 char *endStr, *ifPtr = NULL, *rtPtr = NULL;
91 char *token = strtok_r ((char *)query, "&", &endStr);
93 // External loop breaks query string into fields using the & separator
98 char *innerToken = strtok_r (token, "=", &endToken);
101 // Internal loop parses the field to extract values (parameters) assigned to each field
102 while (innerToken != NULL)
105 if (strcmp (innerToken, OC_RSRVD_INTERFACE) == 0)
107 // Determine the value of IF parameter
108 innerToken = strtok_r (NULL, "=", &endToken);
110 } else if (strcmp (innerToken, OC_RSRVD_RESOURCE_TYPE) == 0) {
111 // Determine the value of RT parameter
112 innerToken = strtok_r (NULL, "=", &endToken);
115 innerToken = strtok_r (NULL, "=", &endToken);
120 // Query parameter should be of the form if=<string>. String should not have & or =
121 return OC_STACK_INVALID_QUERY;
123 token = strtok_r (NULL, "&", &endStr);
125 if (numFields > NUM_PARAM_IN_QUERY)
127 // M1 release supports one IF value, one RT value and no other params
128 return OC_STACK_INVALID_QUERY;
133 if(CheckIFParamSupport((OCResource *)resource, ifPtr) != OC_STACK_OK)
135 return OC_STACK_INVALID_QUERY;
137 if (strcmp (ifPtr, OC_RSRVD_INTERFACE_DEFAULT) == 0)
139 *ifParam = STACK_IF_DEFAULT;
141 else if (strcmp (ifPtr, OC_RSRVD_INTERFACE_LL) == 0)
143 *ifParam = STACK_IF_LL;
145 else if (strcmp (ifPtr, OC_RSRVD_INTERFACE_BATCH) == 0)
147 *ifParam = STACK_IF_BATCH;
149 else if(strcmp (ifPtr, OC_RSRVD_INTERFACE_GROUP) == 0)
151 *ifParam = STACK_IF_GROUP;
155 return OC_STACK_ERROR;
160 // IF not specified in query, use default IF
161 *ifParam = STACK_IF_DEFAULT;
166 if (CheckRTParamSupport((OCResource *)resource, rtPtr) == OC_STACK_OK)
172 return OC_STACK_INVALID_QUERY;
177 // RT not specified in query. Use the first resource type for the resource as default.
178 *rtParam = (char *) OCGetResourceTypeName (resource, 0);
180 OC_LOG_V(INFO, TAG, "Query params: IF = %d, RT = %s", *ifParam, *rtParam);
186 static OCStackResult BuildRootResourceJSON(OCResource *resource,
187 unsigned char * bufferPtr, uint16_t *remaining)
189 OCStackResult ret = OC_STACK_ERROR;
194 OC_LOG(INFO, TAG, PCF("Entering BuildRootResourceJSON"));
195 resObj = cJSON_CreateObject();
199 cJSON_AddItemToObject (resObj, OC_RSRVD_HREF, cJSON_CreateString(resource->uri));
201 jsonStr = cJSON_PrintUnformatted (resObj);
202 jsonLen = strlen(jsonStr);
203 if (jsonLen < *remaining)
205 strcpy((char*) bufferPtr, jsonStr);
206 *remaining -= jsonLen;
207 bufferPtr += jsonLen;
211 cJSON_Delete (resObj);
219 HandleLinkedListInterface(OCEntityHandlerRequest *ehRequest,
220 uint8_t filterOn, char *filterValue)
222 OCStackResult ret = OC_STACK_ERROR;
223 unsigned char jsonbuffer[MAX_RESPONSE_LENGTH] = {0};
224 size_t jsonbufferLength = 0;
225 uint16_t remaining = 0;
226 unsigned char * ptr = NULL;
227 OCResource * collResource = (OCResource *) ehRequest->resource;
230 remaining = MAX_RESPONSE_LENGTH;
232 ret = BuildRootResourceJSON(collResource, ptr, &remaining);
233 ptr += strlen((char*)ptr);
235 if (ret == OC_STACK_OK && remaining >= (sizeof(OC_JSON_SEPARATOR) + 1))
237 *ptr = OC_JSON_SEPARATOR;
243 ret = OC_STACK_ERROR;
247 if (ret == OC_STACK_OK)
249 for (int i = 0; i < MAX_CONTAINED_RESOURCES; i++)
251 OCResource* temp = collResource->rsrcResources[i];
254 //TODO : Update needed here to get correct connectivity type
255 //from ServerRequest data structure
256 ret = BuildVirtualResourceResponse(temp, filterOn, filterValue,
257 (char*)ptr, &remaining, CA_WIFI );
258 if (ret != OC_STACK_OK)
262 ptr += strlen((char*)ptr);
263 if (collResource->rsrcResources[i+1] && remaining > sizeof(OC_JSON_SEPARATOR))
265 *ptr = OC_JSON_SEPARATOR;
278 jsonbufferLength = strlen((const char *)jsonbuffer);
279 if(ret == OC_STACK_OK && jsonbufferLength)
281 OCEntityHandlerResponse response = {0};
282 response.ehResult = OC_EH_OK;
283 response.payload = jsonbuffer;
284 response.payloadSize = jsonbufferLength + 1;
285 response.persistentBufferFlag = 0;
286 response.requestHandle = (OCRequestHandle) ehRequest->requestHandle;
287 response.resourceHandle = (OCResourceHandle) collResource;
288 ret = OCDoResponse(&response);
294 HandleBatchInterface(OCEntityHandlerRequest *ehRequest)
296 OCStackResult stackRet = OC_STACK_ERROR;
297 OCEntityHandlerResult ehResult = OC_EH_ERROR;
298 unsigned char jsonbuffer[MAX_RESPONSE_LENGTH] = {0};
299 size_t jsonbufferLength = 0;
300 uint16_t remaining = 0;
301 unsigned char * ptr = NULL;
302 OCResource * collResource = (OCResource *) ehRequest->resource;
305 remaining = MAX_RESPONSE_LENGTH;
307 stackRet = BuildRootResourceJSON(collResource, ptr, &remaining);
308 ptr += strlen((char*)ptr);
311 jsonbufferLength = strlen((const char *)jsonbuffer);
314 OCEntityHandlerResponse response = {0};
315 response.ehResult = OC_EH_OK;
316 response.payload = jsonbuffer;
317 response.payloadSize = jsonbufferLength + 1;
318 response.persistentBufferFlag = 0;
319 response.requestHandle = (OCRequestHandle) ehRequest->requestHandle;
320 response.resourceHandle = (OCResourceHandle) collResource;
321 stackRet = OCDoResponse(&response);
324 if (stackRet == OC_STACK_OK)
326 for (int i = 0; i < MAX_CONTAINED_RESOURCES; i++)
328 OCResource* temp = collResource->rsrcResources[i];
331 // Note that all entity handlers called through a collection
332 // will get the same pointer to ehRequest, the only difference
333 // is ehRequest->resource
334 ehRequest->resource = (OCResourceHandle) temp;
336 ehResult = temp->entityHandler(OC_REQUEST_FLAG, ehRequest);
338 // The default collection handler is returning as OK
339 if(stackRet != OC_STACK_SLOW_RESOURCE)
341 stackRet = OC_STACK_OK;
343 // if a single resource is slow, then entire response will be treated
345 if(ehResult == OC_EH_SLOW)
347 OC_LOG(INFO, TAG, PCF("This is a slow resource"));
348 ((OCServerRequest *)ehRequest->requestHandle)->slowFlag = 1;
349 stackRet = EntityHandlerCodeToOCStackCode(ehResult);
357 ehRequest->resource = (OCResourceHandle) collResource;
362 uint8_t GetNumOfResourcesInCollection (OCResource *resource)
365 for (int i = 0; i < MAX_CONTAINED_RESOURCES; i++)
367 if (resource->rsrcResources[i])
376 OCStackResult DefaultCollectionEntityHandler (OCEntityHandlerFlag flag,
377 OCEntityHandlerRequest *ehRequest)
379 OCStackResult result = OC_STACK_ERROR;
380 OCStackIfTypes ifQueryParam = STACK_IF_INVALID;
381 char *rtQueryParam = NULL;
383 OC_LOG_V(INFO, TAG, "DefaultCollectionEntityHandler with query %s", ehRequest->query);
385 if (flag != OC_REQUEST_FLAG)
386 return OC_STACK_ERROR;
388 result = ValidateQuery ((const unsigned char *)ehRequest->query,
389 ehRequest->resource, &ifQueryParam, &rtQueryParam);
391 if (result != OC_STACK_OK)
395 if(!((ehRequest->method == OC_REST_GET) ||
396 (ehRequest->method == OC_REST_PUT) ||
397 (ehRequest->method == OC_REST_POST)))
398 return OC_STACK_ERROR;
400 if (ehRequest->method == OC_REST_GET)
402 switch (ifQueryParam)
404 case STACK_IF_DEFAULT:
405 // Get attributes of collection resource and properties of contined resource
406 // M1 release does not support attributes for collection resource, so the GET
407 // operation is same as the GET on LL interface.
408 OC_LOG(INFO, TAG, PCF("STACK_IF_DEFAULT"));
409 return HandleLinkedListInterface(ehRequest, STACK_RES_DISCOVERY_NOFILTER, NULL);
412 OC_LOG(INFO, TAG, PCF("STACK_IF_LL"));
413 return HandleLinkedListInterface(ehRequest, STACK_RES_DISCOVERY_NOFILTER, NULL);
416 OC_LOG(INFO, TAG, PCF("STACK_IF_BATCH"));
417 ((OCServerRequest *)ehRequest->requestHandle)->ehResponseHandler = HandleAggregateResponse;
418 ((OCServerRequest *)ehRequest->requestHandle)->numResponses =
419 GetNumOfResourcesInCollection((OCResource *)ehRequest->resource) + 1;
420 return HandleBatchInterface(ehRequest);
422 return BuildCollectionGroupActionJSONResponse(OC_REST_GET/*flag*/,
423 (OCResource *) ehRequest->resource, ehRequest);
425 return OC_STACK_ERROR;
427 } else if (ehRequest->method == OC_REST_PUT) {
428 switch (ifQueryParam)
430 case STACK_IF_DEFAULT:
431 // M1 release does not support PUT on default interface
432 return OC_STACK_ERROR;
435 // LL interface only supports GET
436 return OC_STACK_ERROR;
439 ((OCServerRequest *)ehRequest->requestHandle)->ehResponseHandler = HandleAggregateResponse;
440 ((OCServerRequest *)ehRequest->requestHandle)->numResponses =
441 GetNumOfResourcesInCollection((OCResource *)ehRequest->resource) + 1;
442 return HandleBatchInterface(ehRequest);
446 OC_LOG_V(INFO, TAG, "IF_COLLECTION PUT with request ::\n%s\n ",
447 ehRequest->reqJSONPayload);
448 return BuildCollectionGroupActionJSONResponse(OC_REST_PUT/*flag*/,
449 (OCResource *) ehRequest->resource, ehRequest);
452 return OC_STACK_ERROR;
455 else if (ehRequest->method == OC_REST_POST)
458 switch (ifQueryParam)
462 OC_LOG_V(INFO, TAG, "IF_COLLECTION POST with request :: \n%s\n ",
463 ehRequest->reqJSONPayload);
464 return BuildCollectionGroupActionJSONResponse(OC_REST_POST/*flag*/,
465 (OCResource *) ehRequest->resource, ehRequest);
468 return OC_STACK_ERROR;