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"
35 #include "oic_malloc.h"
36 #include "oic_string.h"
37 #include "ocpayload.h"
42 #define WITH_GROUPACTION 1
46 #define TAG PCF("occollection")
48 #define NUM_PARAM_IN_QUERY 2 // The expected number of parameters in a query
49 #define NUM_FIELDS_IN_QUERY 2 // The expected number of fields in a query
51 static OCStackResult CheckRTParamSupport(const OCResource* resource, const char* rtPtr)
53 if(!resource || !rtPtr)
55 return OC_STACK_INVALID_PARAM;
58 OCResourceType* rTPointer = resource->rsrcType;
61 if( strcmp (rTPointer->resourcetypename, rtPtr) == 0)
66 rTPointer = rTPointer->next;
68 return OC_STACK_ERROR;
71 static OCStackResult CheckIFParamSupport(const OCResource* resource, const char* ifPtr)
73 if(!resource || !ifPtr)
75 return OC_STACK_INVALID_PARAM;
78 OCResourceInterface* iFPointer = resource->rsrcInterface;
81 if( strcmp (iFPointer->name, ifPtr) == 0)
86 iFPointer = iFPointer->next;
88 return OC_STACK_ERROR;
92 ValidateQuery (const char *query, OCResourceHandle resource,
93 OCStackIfTypes *ifParam, char **rtParam)
95 uint8_t numFields = 0;
98 //TODO: Query and URL validation is being done for virtual resource case
99 // using ValidateUrlQuery function. We should be able to merge it with this
101 OC_LOG(INFO, TAG, PCF("Entering ValidateQuery"));
104 return OC_STACK_ERROR;
106 if(!ifParam || !rtParam)
108 return OC_STACK_INVALID_PARAM;
113 // Query string is empty
114 OC_LOG_V(INFO, TAG, PCF("Empty query string, use default IF and RT"));
115 *ifParam = STACK_IF_DEFAULT;
116 *rtParam = (char *) OCGetResourceTypeName (resource, 0);
120 // Break the query string to validate it and determine IF and RT parameters
121 // Validate there are atmost 2 parameters in string and that one is 'if' and other 'rt'
122 // separated by token '&' or ';'. Stack will accept both the versions.
124 char *endStr, *ifPtr = NULL, *rtPtr = NULL;
125 char *token = strtok_r ((char *)query, OC_QUERY_SEPARATOR , &endStr);
127 // External loop breaks query string into fields using the & separator
128 while (token != NULL)
132 char *innerToken = strtok_r (token, "=", &endToken);
135 // Internal loop parses the field to extract values (parameters) assigned to each field
136 while (innerToken != NULL)
139 if (strncmp (innerToken, OC_RSRVD_INTERFACE, sizeof(OC_RSRVD_INTERFACE)) == 0)
141 // Determine the value of IF parameter
142 innerToken = strtok_r (NULL, "=", &endToken);
145 else if (strcmp (innerToken, OC_RSRVD_RESOURCE_TYPE) == 0)
147 // Determine the value of RT parameter
148 innerToken = strtok_r (NULL, "=", &endToken);
153 innerToken = strtok_r (NULL, "=", &endToken);
156 if (numParam != NUM_PARAM_IN_QUERY)
158 // Query parameter should be of the form if=<string>. String should not have & or =
159 return OC_STACK_INVALID_QUERY;
161 token = strtok_r (NULL, OC_QUERY_SEPARATOR, &endStr);
163 if (numFields > NUM_FIELDS_IN_QUERY)
165 // current release supports one IF value, one RT value and no other params
166 return OC_STACK_INVALID_QUERY;
171 if(CheckIFParamSupport((OCResource *)resource, ifPtr) != OC_STACK_OK)
173 return OC_STACK_INVALID_QUERY;
175 if (strcmp (ifPtr, OC_RSRVD_INTERFACE_DEFAULT) == 0)
177 *ifParam = STACK_IF_DEFAULT;
179 else if (strcmp (ifPtr, OC_RSRVD_INTERFACE_LL) == 0)
181 *ifParam = STACK_IF_LL;
183 else if (strcmp (ifPtr, OC_RSRVD_INTERFACE_BATCH) == 0)
185 *ifParam = STACK_IF_BATCH;
187 else if(strcmp (ifPtr, OC_RSRVD_INTERFACE_GROUP) == 0)
189 *ifParam = STACK_IF_GROUP;
193 return OC_STACK_ERROR;
198 // IF not specified in query, use default IF
199 *ifParam = STACK_IF_DEFAULT;
204 if (CheckRTParamSupport((OCResource *)resource, rtPtr) == OC_STACK_OK)
210 return OC_STACK_INVALID_QUERY;
215 // RT not specified in query. Use the first resource type for the resource as default.
216 *rtParam = (char *) OCGetResourceTypeName (resource, 0);
218 OC_LOG_V(INFO, TAG, "Query params: IF = %d, RT = %s", *ifParam, *rtParam);
224 HandleLinkedListInterface(OCEntityHandlerRequest *ehRequest,
232 return OC_STACK_INVALID_PARAM;
235 OCStackResult ret = OC_STACK_OK;
236 OCResource *collResource = (OCResource *)ehRequest->resource;
238 OCRepPayload* payload = NULL;
240 if(ret == OC_STACK_OK)
242 ret = BuildResponseRepresentation(collResource, &payload);
245 if (ret == OC_STACK_OK)
247 for (int i = 0; i < MAX_CONTAINED_RESOURCES && ret == OC_STACK_OK; i++)
249 OCResource* temp = collResource->rsrcResources[i];
252 //TODO : Add resource type filtering once collections
253 // start supporting queries.
254 ret = BuildResponseRepresentation(temp, &payload);
259 if(ret == OC_STACK_OK)
261 OCEntityHandlerResponse response = {0};
262 response.ehResult = OC_EH_OK;
263 response.payload = (OCPayload*)payload;
264 response.persistentBufferFlag = 0;
265 response.requestHandle = (OCRequestHandle) ehRequest->requestHandle;
266 response.resourceHandle = (OCResourceHandle) collResource;
267 ret = OCDoResponse(&response);
269 OCRepPayloadDestroy(payload);
274 HandleBatchInterface(OCEntityHandlerRequest *ehRequest)
276 OCStackResult stackRet = OC_STACK_OK;
277 OCEntityHandlerResult ehResult = OC_EH_ERROR;
278 OCResource * collResource = (OCResource *) ehRequest->resource;
280 OCRepPayload* payload = OCRepPayloadCreate();
283 stackRet = OC_STACK_NO_MEMORY;
286 if(stackRet == OC_STACK_OK)
288 OCRepPayloadSetUri(payload, collResource->uri);
291 if(stackRet == OC_STACK_OK)
293 OCEntityHandlerResponse response = {0};
294 response.ehResult = OC_EH_OK;
295 response.payload = (OCPayload*)payload;
296 response.persistentBufferFlag = 0;
297 response.requestHandle = (OCRequestHandle) ehRequest->requestHandle;
298 response.resourceHandle = (OCResourceHandle) collResource;
299 stackRet = OCDoResponse(&response);
302 if (stackRet == OC_STACK_OK)
304 for (int i = 0; i < MAX_CONTAINED_RESOURCES; i++)
306 OCResource* temp = collResource->rsrcResources[i];
309 // Note that all entity handlers called through a collection
310 // will get the same pointer to ehRequest, the only difference
311 // is ehRequest->resource
312 ehRequest->resource = (OCResourceHandle) temp;
314 ehResult = temp->entityHandler(OC_REQUEST_FLAG, ehRequest,
315 temp->entityHandlerCallbackParam);
317 // The default collection handler is returning as OK
318 if(stackRet != OC_STACK_SLOW_RESOURCE)
320 stackRet = OC_STACK_OK;
322 // if a single resource is slow, then entire response will be treated
324 if(ehResult == OC_EH_SLOW)
326 OC_LOG(INFO, TAG, PCF("This is a slow resource"));
327 ((OCServerRequest *)ehRequest->requestHandle)->slowFlag = 1;
328 stackRet = EntityHandlerCodeToOCStackCode(ehResult);
336 ehRequest->resource = (OCResourceHandle) collResource;
341 uint8_t GetNumOfResourcesInCollection (OCResource *resource)
346 for (int i = 0; i < MAX_CONTAINED_RESOURCES; i++)
348 if (resource->rsrcResources[i])
362 OCStackResult DefaultCollectionEntityHandler (OCEntityHandlerFlag flag,
363 OCEntityHandlerRequest *ehRequest)
365 if(!ehRequest || !ehRequest->query)
367 return OC_STACK_INVALID_PARAM;
370 OCStackResult result = OC_STACK_ERROR;
371 OCStackIfTypes ifQueryParam = STACK_IF_INVALID;
372 char *rtQueryParam = NULL;
374 OC_LOG_V(INFO, TAG, "DefaultCollectionEntityHandler with query %s", ehRequest->query);
376 if (flag != OC_REQUEST_FLAG)
378 return OC_STACK_ERROR;
381 result = ValidateQuery (ehRequest->query,
382 ehRequest->resource, &ifQueryParam, &rtQueryParam);
384 if (result != OC_STACK_OK)
389 if(!((ehRequest->method == OC_REST_GET) ||
390 (ehRequest->method == OC_REST_PUT) ||
391 (ehRequest->method == OC_REST_POST)))
393 return OC_STACK_ERROR;
396 if (ehRequest->method == OC_REST_GET)
398 switch (ifQueryParam)
400 case STACK_IF_DEFAULT:
401 // Get attributes of collection resource and properties of contined resource
402 // M1 release does not support attributes for collection resource, so the GET
403 // operation is same as the GET on LL interface.
404 OC_LOG(INFO, TAG, PCF("STACK_IF_DEFAULT"));
405 return HandleLinkedListInterface(ehRequest, STACK_RES_DISCOVERY_NOFILTER, NULL);
408 OC_LOG(INFO, TAG, PCF("STACK_IF_LL"));
409 return HandleLinkedListInterface(ehRequest, STACK_RES_DISCOVERY_NOFILTER, NULL);
412 OC_LOG(INFO, TAG, PCF("STACK_IF_BATCH"));
413 ((OCServerRequest *)ehRequest->requestHandle)->ehResponseHandler =
414 HandleAggregateResponse;
416 ((OCServerRequest *)ehRequest->requestHandle)->numResponses =
417 GetNumOfResourcesInCollection((OCResource *)ehRequest->resource) + 1;
419 return HandleBatchInterface(ehRequest);
422 return BuildCollectionGroupActionJSONResponse(OC_REST_GET/*flag*/,
423 (OCResource *) ehRequest->resource, ehRequest);
425 return OC_STACK_ERROR;
428 else if (ehRequest->method == OC_REST_PUT)
430 switch (ifQueryParam)
432 case STACK_IF_DEFAULT:
433 // M1 release does not support PUT on default interface
434 return OC_STACK_ERROR;
437 // LL interface only supports GET
438 return OC_STACK_ERROR;
441 ((OCServerRequest *)ehRequest->requestHandle)->ehResponseHandler =
442 HandleAggregateResponse;
443 ((OCServerRequest *)ehRequest->requestHandle)->numResponses =
444 GetNumOfResourcesInCollection((OCResource *)ehRequest->resource) + 1;
445 return HandleBatchInterface(ehRequest);
449 OC_LOG(INFO, TAG, PCF("IF_COLLECTION PUT with request ::\n"));
450 OC_LOG_PAYLOAD(INFO, TAG, ehRequest->payload);
451 return BuildCollectionGroupActionJSONResponse(OC_REST_PUT/*flag*/,
452 (OCResource *) ehRequest->resource, ehRequest);
455 return OC_STACK_ERROR;
458 else if (ehRequest->method == OC_REST_POST)
461 switch (ifQueryParam)
465 OC_LOG(INFO, TAG, PCF("IF_COLLECTION POST with request ::\n"));
466 OC_LOG_PAYLOAD(INFO, TAG, ehRequest->payload);
467 return BuildCollectionGroupActionJSONResponse(OC_REST_POST/*flag*/,
468 (OCResource *) ehRequest->resource, ehRequest);
471 return OC_STACK_ERROR;
474 else if (ehRequest->method == OC_REST_POST)
477 if(ifQueryParam == STACK_IF_GROUP)
479 OC_LOG(INFO, TAG, PCF("IF_COLLECTION POST with request ::\n"));
480 OC_LOG_PAYLOAD(INFO, TAG, ehRequest->payload);
481 return BuildCollectionGroupActionJSONResponse(OC_REST_POST/*flag*/,
482 (OCResource *) ehRequest->resource, ehRequest);
486 return OC_STACK_ERROR;