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
28 #include "occollection.h"
31 #include "ocstackinternal.h"
32 #include "ocresourcehandler.h"
41 #define WITH_GROUPACTION 1
45 #define TAG PCF("occollection")
47 #define NUM_PARAM_IN_QUERY 2 // The expected number of parameters in a query
48 #define NUM_FIELDS_IN_QUERY 2 // The expected number of fields in a query
50 static OCStackResult CheckRTParamSupport(const OCResource* resource, const char* rtPtr)
52 if(!resource || !rtPtr)
54 return OC_STACK_INVALID_PARAM;
57 OCResourceType* rTPointer = resource->rsrcType;
60 if( strcmp (rTPointer->resourcetypename, rtPtr) == 0)
65 rTPointer = rTPointer->next;
67 return OC_STACK_ERROR;
70 static OCStackResult CheckIFParamSupport(const OCResource* resource, const char* ifPtr)
72 if(!resource || !ifPtr)
74 return OC_STACK_INVALID_PARAM;
77 OCResourceInterface* iFPointer = resource->rsrcInterface;
80 if( strcmp (iFPointer->name, ifPtr) == 0)
85 iFPointer = iFPointer->next;
87 return OC_STACK_ERROR;
91 ValidateQuery (const char *query, OCResourceHandle resource,
92 OCStackIfTypes *ifParam, char **rtParam)
94 uint8_t numFields = 0;
97 //TODO: Query and URL validation is being done for virtual resource case
98 // using ValidateUrlQuery function. We should be able to merge it with this
100 OC_LOG(INFO, TAG, PCF("Entering ValidateQuery"));
103 return OC_STACK_ERROR;
105 if(!ifParam || !rtParam)
107 return OC_STACK_INVALID_PARAM;
112 // Query string is empty
113 OC_LOG_V(INFO, TAG, PCF("Empty query string, use default IF and RT"));
114 *ifParam = STACK_IF_DEFAULT;
115 *rtParam = (char *) OCGetResourceTypeName (resource, 0);
119 // Break the query string to validate it and determine IF and RT parameters
120 // Validate there are atmost 2 parameters in string and that one is 'if' and other 'rt'
121 char *endStr, *ifPtr = NULL, *rtPtr = NULL;
122 char *token = strtok_r ((char *)query, "&", &endStr);
124 // External loop breaks query string into fields using the & separator
125 while (token != NULL)
129 char *innerToken = strtok_r (token, "=", &endToken);
132 // Internal loop parses the field to extract values (parameters) assigned to each field
133 while (innerToken != NULL)
136 if (strncmp (innerToken, OC_RSRVD_INTERFACE, sizeof(OC_RSRVD_INTERFACE)) == 0)
138 // Determine the value of IF parameter
139 innerToken = strtok_r (NULL, "=", &endToken);
142 else if (strcmp (innerToken, OC_RSRVD_RESOURCE_TYPE) == 0)
144 // Determine the value of RT parameter
145 innerToken = strtok_r (NULL, "=", &endToken);
150 innerToken = strtok_r (NULL, "=", &endToken);
153 if (numParam != NUM_PARAM_IN_QUERY)
155 // Query parameter should be of the form if=<string>. String should not have & or =
156 return OC_STACK_INVALID_QUERY;
158 token = strtok_r (NULL, "&", &endStr);
160 if (numFields > NUM_FIELDS_IN_QUERY)
162 // current release supports one IF value, one RT value and no other params
163 return OC_STACK_INVALID_QUERY;
168 if(CheckIFParamSupport((OCResource *)resource, ifPtr) != OC_STACK_OK)
170 return OC_STACK_INVALID_QUERY;
172 if (strcmp (ifPtr, OC_RSRVD_INTERFACE_DEFAULT) == 0)
174 *ifParam = STACK_IF_DEFAULT;
176 else if (strcmp (ifPtr, OC_RSRVD_INTERFACE_LL) == 0)
178 *ifParam = STACK_IF_LL;
180 else if (strcmp (ifPtr, OC_RSRVD_INTERFACE_BATCH) == 0)
182 *ifParam = STACK_IF_BATCH;
184 else if(strcmp (ifPtr, OC_RSRVD_INTERFACE_GROUP) == 0)
186 *ifParam = STACK_IF_GROUP;
190 return OC_STACK_ERROR;
195 // IF not specified in query, use default IF
196 *ifParam = STACK_IF_DEFAULT;
201 if (CheckRTParamSupport((OCResource *)resource, rtPtr) == OC_STACK_OK)
207 return OC_STACK_INVALID_QUERY;
212 // RT not specified in query. Use the first resource type for the resource as default.
213 *rtParam = (char *) OCGetResourceTypeName (resource, 0);
215 OC_LOG_V(INFO, TAG, "Query params: IF = %d, RT = %s", *ifParam, *rtParam);
221 static OCStackResult BuildRootResourceJSON(OCResource *resource,
222 char * bufferPtr, uint16_t *remaining)
224 OCStackResult ret = OC_STACK_ERROR;
225 cJSON *resObj = NULL;
226 char *jsonStr = NULL;
229 OC_LOG(INFO, TAG, PCF("Entering BuildRootResourceJSON"));
230 resObj = cJSON_CreateObject();
234 ret = OC_STACK_NO_MEMORY;
238 cJSON_AddItemToObject (resObj, OC_RSRVD_HREF, cJSON_CreateString(resource->uri));
239 jsonStr = cJSON_PrintUnformatted (resObj);
243 cJSON_Delete(resObj);
244 return OC_STACK_NO_MEMORY;
247 jsonLen = strlen(jsonStr);
248 if (jsonLen < *remaining)
250 strncpy(bufferPtr, jsonStr, jsonLen);
251 *remaining -= jsonLen;
252 bufferPtr += jsonLen;
258 ret = OC_STACK_INVALID_PARAM;
261 cJSON_Delete (resObj);
269 HandleLinkedListInterface(OCEntityHandlerRequest *ehRequest,
270 uint8_t filterOn, char *filterValue)
274 return OC_STACK_INVALID_PARAM;
277 OCStackResult ret = OC_STACK_ERROR;
278 char jsonbuffer[MAX_RESPONSE_LENGTH] = {};
279 size_t jsonbufferLength = 0;
280 uint16_t remaining = 0;
282 OCResource * collResource = (OCResource *) ehRequest->resource;
285 remaining = MAX_RESPONSE_LENGTH;
287 ret = BuildRootResourceJSON(collResource, ptr, &remaining);
289 if (ret == OC_STACK_OK && remaining >= (sizeof(OC_JSON_SEPARATOR) + 1))
291 ptr += strlen((char*)ptr);
292 *ptr = OC_JSON_SEPARATOR;
298 ret = OC_STACK_ERROR;
301 if (ret == OC_STACK_OK)
303 for (int i = 0; i < MAX_CONTAINED_RESOURCES; i++)
305 OCResource* temp = collResource->rsrcResources[i];
308 //TODO : Update needed here to get correct connectivity type
309 //from ServerRequest data structure.
311 // Function will return error if not enough space in buffer.
312 ret = BuildVirtualResourceResponse(temp, filterOn, filterValue,
313 (char*)ptr, &remaining, CA_IPV4 );
314 if (ret != OC_STACK_OK)
318 ptr += strlen((char*)ptr);
320 // Check if we have added all resources.
321 if ((i + 1) == MAX_CONTAINED_RESOURCES)
325 // Add separator if more resources and enough space present.
326 if (collResource->rsrcResources[i+1] && remaining > sizeof(OC_JSON_SEPARATOR))
328 *ptr = OC_JSON_SEPARATOR;
332 // No point continuing as no more space on buffer
333 // and/or no more resources.
346 jsonbufferLength = strlen((const char *)jsonbuffer);
347 if(ret == OC_STACK_OK && jsonbufferLength)
349 OCEntityHandlerResponse response = {};
350 response.ehResult = OC_EH_OK;
351 response.payload = jsonbuffer;
352 response.payloadSize = jsonbufferLength + 1;
353 response.persistentBufferFlag = 0;
354 response.requestHandle = (OCRequestHandle) ehRequest->requestHandle;
355 response.resourceHandle = (OCResourceHandle) collResource;
356 ret = OCDoResponse(&response);
362 HandleBatchInterface(OCEntityHandlerRequest *ehRequest)
364 OCStackResult stackRet = OC_STACK_ERROR;
365 OCEntityHandlerResult ehResult = OC_EH_ERROR;
366 char jsonbuffer[MAX_RESPONSE_LENGTH] = {0};
367 size_t jsonbufferLength = 0;
368 uint16_t remaining = 0;
370 OCResource * collResource = (OCResource *) ehRequest->resource;
373 remaining = MAX_RESPONSE_LENGTH;
375 stackRet = BuildRootResourceJSON(collResource, ptr, &remaining);
376 ptr += strlen((char*)ptr);
378 jsonbufferLength = strlen((const char *)jsonbuffer);
381 OCEntityHandlerResponse response = {};
382 response.ehResult = OC_EH_OK;
383 response.payload = jsonbuffer;
384 response.payloadSize = jsonbufferLength + 1;
385 response.persistentBufferFlag = 0;
386 response.requestHandle = (OCRequestHandle) ehRequest->requestHandle;
387 response.resourceHandle = (OCResourceHandle) collResource;
388 stackRet = OCDoResponse(&response);
391 if (stackRet == OC_STACK_OK)
393 for (int i = 0; i < MAX_CONTAINED_RESOURCES; i++)
395 OCResource* temp = collResource->rsrcResources[i];
398 // Note that all entity handlers called through a collection
399 // will get the same pointer to ehRequest, the only difference
400 // is ehRequest->resource
401 ehRequest->resource = (OCResourceHandle) temp;
403 ehResult = temp->entityHandler(OC_REQUEST_FLAG, ehRequest);
405 // The default collection handler is returning as OK
406 if(stackRet != OC_STACK_SLOW_RESOURCE)
408 stackRet = OC_STACK_OK;
410 // if a single resource is slow, then entire response will be treated
412 if(ehResult == OC_EH_SLOW)
414 OC_LOG(INFO, TAG, PCF("This is a slow resource"));
415 ((OCServerRequest *)ehRequest->requestHandle)->slowFlag = 1;
416 stackRet = EntityHandlerCodeToOCStackCode(ehResult);
424 ehRequest->resource = (OCResourceHandle) collResource;
429 uint8_t GetNumOfResourcesInCollection (OCResource *resource)
434 for (int i = 0; i < MAX_CONTAINED_RESOURCES; i++)
436 if (resource->rsrcResources[i])
450 OCStackResult DefaultCollectionEntityHandler (OCEntityHandlerFlag flag,
451 OCEntityHandlerRequest *ehRequest)
453 if(!ehRequest || !ehRequest->query)
455 return OC_STACK_INVALID_PARAM;
458 OCStackResult result = OC_STACK_ERROR;
459 OCStackIfTypes ifQueryParam = STACK_IF_INVALID;
460 char *rtQueryParam = NULL;
462 OC_LOG_V(INFO, TAG, "DefaultCollectionEntityHandler with query %s", ehRequest->query);
464 if (flag != OC_REQUEST_FLAG)
466 return OC_STACK_ERROR;
469 result = ValidateQuery (ehRequest->query,
470 ehRequest->resource, &ifQueryParam, &rtQueryParam);
472 if (result != OC_STACK_OK)
477 if(!((ehRequest->method == OC_REST_GET) ||
478 (ehRequest->method == OC_REST_PUT) ||
479 (ehRequest->method == OC_REST_POST)))
481 return OC_STACK_ERROR;
484 if (ehRequest->method == OC_REST_GET)
486 switch (ifQueryParam)
488 case STACK_IF_DEFAULT:
489 // Get attributes of collection resource and properties of contined resource
490 // M1 release does not support attributes for collection resource, so the GET
491 // operation is same as the GET on LL interface.
492 OC_LOG(INFO, TAG, PCF("STACK_IF_DEFAULT"));
493 return HandleLinkedListInterface(ehRequest, STACK_RES_DISCOVERY_NOFILTER, NULL);
496 OC_LOG(INFO, TAG, PCF("STACK_IF_LL"));
497 return HandleLinkedListInterface(ehRequest, STACK_RES_DISCOVERY_NOFILTER, NULL);
500 OC_LOG(INFO, TAG, PCF("STACK_IF_BATCH"));
501 ((OCServerRequest *)ehRequest->requestHandle)->ehResponseHandler =
502 HandleAggregateResponse;
503 ((OCServerRequest *)ehRequest->requestHandle)->numResponses =
504 GetNumOfResourcesInCollection((OCResource *)ehRequest->resource) + 1;
505 return HandleBatchInterface(ehRequest);
507 return BuildCollectionGroupActionJSONResponse(OC_REST_GET/*flag*/,
508 (OCResource *) ehRequest->resource, ehRequest);
510 return OC_STACK_ERROR;
513 else if (ehRequest->method == OC_REST_PUT)
515 switch (ifQueryParam)
517 case STACK_IF_DEFAULT:
518 // M1 release does not support PUT on default interface
519 return OC_STACK_ERROR;
522 // LL interface only supports GET
523 return OC_STACK_ERROR;
526 ((OCServerRequest *)ehRequest->requestHandle)->ehResponseHandler =
527 HandleAggregateResponse;
528 ((OCServerRequest *)ehRequest->requestHandle)->numResponses =
529 GetNumOfResourcesInCollection((OCResource *)ehRequest->resource) + 1;
530 return HandleBatchInterface(ehRequest);
534 OC_LOG_V(INFO, TAG, "IF_COLLECTION PUT with request ::\n%s\n ",
535 ehRequest->reqJSONPayload);
536 return BuildCollectionGroupActionJSONResponse(OC_REST_PUT/*flag*/,
537 (OCResource *) ehRequest->resource, ehRequest);
540 return OC_STACK_ERROR;
543 else if (ehRequest->method == OC_REST_POST)
546 switch (ifQueryParam)
550 OC_LOG_V(INFO, TAG, "IF_COLLECTION POST with request :: \n%s\n ",
551 ehRequest->reqJSONPayload);
552 return BuildCollectionGroupActionJSONResponse(OC_REST_POST/*flag*/,
553 (OCResource *) ehRequest->resource, ehRequest);
556 return OC_STACK_ERROR;
559 else if (ehRequest->method == OC_REST_POST)
562 if(ifQueryParam == STACK_IF_GROUP)
564 OC_LOG_V(INFO, TAG, "IF_COLLECTION POST with request :: \n%s\n ",
565 ehRequest->reqJSONPayload);
566 return BuildCollectionGroupActionJSONResponse(OC_REST_POST/*flag*/,
567 (OCResource *) ehRequest->resource, ehRequest);
571 return OC_STACK_ERROR;