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
29 #include "occollection.h"
30 #include "ocpayload.h"
33 #include "oic_string.h"
34 #include "payload_logging.h"
36 #define TAG "OIC_RI_COLLECTION"
38 static bool AddRTSBaslinePayload(OCRepPayload **linkArray, int size, OCRepPayload **colPayload)
41 for (int j = 0; j < size; j++)
43 size_t rtDim[MAX_REP_ARRAY_DEPTH] = {0};
45 OCRepPayloadGetStringArray(linkArray[j], OC_RSRVD_RESOURCE_TYPE, &rt, rtDim);
46 arraySize += rtDim[0];
47 for (size_t l = 0; l < rtDim[0]; l++)
54 for (OCStringLL *rsrcType = (*colPayload)->types; rsrcType; rsrcType = rsrcType->next, arraySize++);
56 OIC_LOG_V(DEBUG, TAG, "Number of RTS elements : %zd", arraySize);
57 size_t dim[MAX_REP_ARRAY_DEPTH] = {arraySize, 0, 0};
58 char **rts = (char **)OICMalloc(sizeof(char *) * arraySize);
61 OIC_LOG(ERROR, TAG, "Memory allocation failed!");
62 return OC_STACK_NO_MEMORY;
65 for (int j = 0; j < size; j++)
67 size_t rtDim[MAX_REP_ARRAY_DEPTH] = {0};
69 OCRepPayloadGetStringArray(linkArray[j], OC_RSRVD_RESOURCE_TYPE, &rt, rtDim);
70 for (size_t l = 0; l < rtDim[0]; l++)
72 rts[k++] = OICStrdup(rt[l]);
77 for (OCStringLL *rsrcType = (*colPayload)->types; rsrcType; rsrcType = rsrcType->next, size++)
79 rts[k++] = OICStrdup(rsrcType->value);
82 return OCRepPayloadSetStringArrayAsOwner(*colPayload, OC_RSRVD_RTS, rts, dim);
85 static OCStackResult SendResponse(const OCRepPayload *payload, const OCEntityHandlerRequest *ehRequest,
86 const OCResource* collResource, OCEntityHandlerResult ehResult)
88 OCEntityHandlerResponse response = {0};
89 response.ehResult = ehResult;
90 response.payload = (OCPayload*)payload;
91 response.persistentBufferFlag = 0;
92 response.requestHandle = (OCRequestHandle) ehRequest->requestHandle;
93 response.resourceHandle = (OCResourceHandle) collResource;
94 return OCDoResponse(&response);
97 uint8_t GetNumOfResourcesInCollection(const OCResource *collResource)
100 for (OCChildResource *tempChildResource = collResource->rsrcChildResourcesHead;
101 tempChildResource; tempChildResource = tempChildResource->next)
108 static OCStackResult HandleLinkedListInterface(OCEntityHandlerRequest *ehRequest, char *ifQueryParam)
112 return OC_STACK_INVALID_PARAM;
115 OCResource *collResource = (OCResource *)ehRequest->resource;
118 return OC_STACK_INVALID_PARAM;
121 uint8_t size = GetNumOfResourcesInCollection(collResource);
122 OCRepPayload *colPayload = NULL;
123 OCEntityHandlerResult ehResult = OC_EH_ERROR;
125 OCStackResult ret = OC_STACK_OK;
126 size_t dim[MAX_REP_ARRAY_DEPTH] = {size, 0, 0};
127 OCRepPayload **linkArr = (OCRepPayload **)OICCalloc(size, sizeof(OCRepPayload *));
128 VERIFY_PARAM_NON_NULL(TAG, linkArr, "Failed creating LinkArray");
130 for (OCChildResource *tempChildResource = collResource->rsrcChildResourcesHead;
131 tempChildResource && ret == OC_STACK_OK; tempChildResource = tempChildResource->next)
133 OCResource* temp = tempChildResource->rsrcResource;
136 ret = BuildResponseRepresentation(temp, &linkArr[i++], &ehRequest->devAddr);
140 if (ret == OC_STACK_OK)
142 colPayload = OCRepPayloadCreate();
145 if (0 == strcmp(OC_RSRVD_INTERFACE_DEFAULT, ifQueryParam))
147 //TODO : Add resource type filtering once collections
148 // start supporting queries.
149 OCRepPayloadAddResourceType(colPayload, OC_RSRVD_RESOURCE_TYPE_COLLECTION);
150 for (OCResourceType *types = collResource->rsrcType; types; types = types->next)
152 if (0 != strcmp(OC_RSRVD_RESOURCE_TYPE_COLLECTION, types->resourcetypename))
154 OCRepPayloadAddResourceType(colPayload, types->resourcetypename);
157 for (OCResourceInterface *itf = collResource->rsrcInterface; itf; itf = itf->next)
159 OCRepPayloadAddInterface(colPayload, itf->name);
161 AddRTSBaslinePayload(linkArr, i, &colPayload);
163 OCRepPayloadSetPropObjectArrayAsOwner(colPayload, OC_RSRVD_LINKS, linkArr, dim);
168 if (ret == OC_STACK_OK)
174 ehResult = (ret == OC_STACK_NO_RESOURCE) ? OC_EH_RESOURCE_NOT_FOUND : OC_EH_ERROR;
176 ret = SendResponse(colPayload, ehRequest, collResource, ehResult);
177 OIC_LOG_PAYLOAD(DEBUG, (OCPayload *)colPayload);
178 OCRepPayloadDestroy(colPayload);
182 static OCStackResult HandleBatchInterface(OCEntityHandlerRequest *ehRequest)
186 return OC_STACK_INVALID_PARAM;
189 OCStackResult stackRet = OC_STACK_OK;
190 char *storeQuery = NULL;
191 OCResource *collResource = (OCResource *)ehRequest->resource;
193 if (stackRet == OC_STACK_OK)
196 if (collResource->rsrcChildResourcesHead)
198 storeQuery = ehRequest->query;
199 ehRequest->query = NULL;
200 OIC_LOG_V(DEBUG, TAG, "Query : %s", ehRequest->query);
204 for (OCChildResource *tempChildResource = collResource->rsrcChildResourcesHead;
205 tempChildResource; tempChildResource = tempChildResource->next, numRes++)
207 OCResource* tempRsrcResource = tempChildResource->rsrcResource;
208 if (tempRsrcResource)
210 // Note that all entity handlers called through a collection
211 // will get the same pointer to ehRequest, the only difference
212 // is ehRequest->resource
213 ehRequest->resource = (OCResourceHandle) tempRsrcResource;
214 OCEntityHandlerResult ehResult = tempRsrcResource->entityHandler(OC_REQUEST_FLAG,
215 ehRequest, tempRsrcResource->entityHandlerCallbackParam);
217 // The default collection handler is returning as OK
218 if (stackRet != OC_STACK_SLOW_RESOURCE)
220 stackRet = OC_STACK_OK;
222 // if a single resource is slow, then entire response will be treated
224 if (ehResult == OC_EH_SLOW)
226 OIC_LOG(INFO, TAG, "This is a slow resource");
227 OCServerRequest *request =
228 GetServerRequestUsingHandle(ehRequest->requestHandle);
231 request->slowFlag = 1;
233 stackRet = EntityHandlerCodeToOCStackCode(ehResult);
241 ehRequest->resource = (OCResourceHandle) collResource;
243 ehRequest->query = storeQuery;
247 OCStackResult DefaultCollectionEntityHandler(OCEntityHandlerFlag flag, OCEntityHandlerRequest *ehRequest)
249 if (!ehRequest || !ehRequest->query)
251 return OC_STACK_INVALID_PARAM;
253 // Delete is not supported for any interface query method.
254 if (ehRequest->method == OC_REST_DELETE || flag != OC_REQUEST_FLAG)
256 return OC_STACK_ERROR;
258 OIC_LOG_V(INFO, TAG, "DefaultCollectionEntityHandler with query %s", ehRequest->query);
260 char *ifQueryParam = NULL;
261 char *rtQueryParam = NULL;
262 OCStackResult result = ExtractFiltersFromQuery(ehRequest->query, &ifQueryParam, &rtQueryParam);
263 if (result != OC_STACK_OK)
265 result = OC_STACK_NO_RESOURCE;
270 ifQueryParam = OICStrdup(OC_RSRVD_INTERFACE_LL);
273 VERIFY_PARAM_NON_NULL(TAG, ifQueryParam, "Invalid Parameter root");
275 if (0 == strcmp(ifQueryParam, OC_RSRVD_INTERFACE_LL) || 0 == strcmp (ifQueryParam, OC_RSRVD_INTERFACE_DEFAULT))
277 if (ehRequest->method == OC_REST_PUT || ehRequest->method == OC_REST_POST)
279 result = OC_STACK_ERROR;
283 result = HandleLinkedListInterface(ehRequest, ifQueryParam);
286 else if (0 == strcmp(ifQueryParam, OC_RSRVD_INTERFACE_BATCH))
288 OCServerRequest *request = GetServerRequestUsingHandle(ehRequest->requestHandle);
291 request->numResponses = GetNumOfResourcesInCollection((OCResource *)ehRequest->resource);
292 request->ehResponseHandler = HandleAggregateResponse;
293 result = HandleBatchInterface(ehRequest);
296 else if (0 == strcmp(ifQueryParam, OC_RSRVD_INTERFACE_GROUP))
298 OIC_LOG_V(INFO, TAG, "IF_COLLECTION %d with request ::\n", ehRequest->method);
299 OIC_LOG_PAYLOAD(INFO, ehRequest->payload);
300 result = BuildCollectionGroupActionCBORResponse(ehRequest->method, (OCResource *) ehRequest->resource, ehRequest);
303 if (result != OC_STACK_OK)
305 result = SendResponse(NULL, ehRequest, (OCResource *)ehRequest->resource, OC_EH_BAD_REQ);
307 OICFree(ifQueryParam);
308 OICFree(rtQueryParam);