replace : iotivity -> iotivity-sec
[platform/upstream/iotivity.git] / resource / csdk / stack / src / occollection.c
1 //******************************************************************
2 //
3 // Copyright 2014 Intel Mobile Communications GmbH All Rights Reserved.
4 //
5 //-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
6 //
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
10 //
11 //      http://www.apache.org/licenses/LICENSE-2.0
12 //
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.
18 //
19 //-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
20
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
29 #include "occollection.h"
30 #include "ocpayload.h"
31 #include "ocstack.h"
32 #include "oicgroup.h"
33 #include "oic_string.h"
34 #include "payload_logging.h"
35
36 #define TAG "OIC_RI_COLLECTION"
37
38 static bool AddRTSBaslinePayload(OCRepPayload **linkArray, int size, OCRepPayload **colPayload)
39 {
40     size_t arraySize = 0;
41     for (int j = 0; j < size; j++)
42     {
43         size_t rtDim[MAX_REP_ARRAY_DEPTH] = {0};
44         char **rt = NULL;
45         OCRepPayloadGetStringArray(linkArray[j], OC_RSRVD_RESOURCE_TYPE, &rt, rtDim);
46         arraySize += rtDim[0];
47         for (size_t l = 0; l < rtDim[0]; l++)
48         {
49             OICFree(rt[l]);
50         }
51         OICFree(rt);
52     }
53
54     for (OCStringLL *rsrcType = (*colPayload)->types; rsrcType; rsrcType = rsrcType->next, arraySize++);
55
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);
59     if (!rts)
60     {
61         OIC_LOG(ERROR, TAG, "Memory allocation failed!");
62         return OC_STACK_NO_MEMORY;
63     }
64     int k = 0;
65     for (int j = 0; j < size; j++)
66     {
67         size_t rtDim[MAX_REP_ARRAY_DEPTH] = {0};
68         char **rt = NULL;
69         OCRepPayloadGetStringArray(linkArray[j], OC_RSRVD_RESOURCE_TYPE, &rt, rtDim);
70         for (size_t l = 0; l < rtDim[0]; l++)
71         {
72             rts[k++] = OICStrdup(rt[l]);
73             OICFree(rt[l]);
74         }
75         OICFree(rt);
76     }
77     for (OCStringLL *rsrcType = (*colPayload)->types; rsrcType; rsrcType = rsrcType->next, size++)
78     {
79         rts[k++] = OICStrdup(rsrcType->value);
80     }
81
82     return OCRepPayloadSetStringArrayAsOwner(*colPayload, OC_RSRVD_RTS, rts, dim);
83 }
84
85 static OCStackResult SendResponse(const OCRepPayload *payload, const OCEntityHandlerRequest *ehRequest,
86     const OCResource* collResource, OCEntityHandlerResult ehResult)
87 {
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);
95 }
96
97 uint8_t GetNumOfResourcesInCollection(const OCResource *collResource)
98 {
99     uint8_t size = 0;
100     for (OCChildResource *tempChildResource = collResource->rsrcChildResourcesHead;
101         tempChildResource; tempChildResource = tempChildResource->next)
102     {
103         size++;
104     }
105     return size;
106 }
107
108 static OCStackResult HandleLinkedListInterface(OCEntityHandlerRequest *ehRequest, char *ifQueryParam)
109 {
110     if (!ehRequest)
111     {
112         return OC_STACK_INVALID_PARAM;
113     }
114
115     OCResource *collResource = (OCResource *)ehRequest->resource;
116     if (!collResource)
117     {
118         return OC_STACK_INVALID_PARAM;
119     }
120
121     uint8_t size = GetNumOfResourcesInCollection(collResource);
122     OCRepPayload *colPayload = NULL;
123     OCEntityHandlerResult ehResult = OC_EH_ERROR;
124     int i = 0;
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");
129
130     for (OCChildResource *tempChildResource = collResource->rsrcChildResourcesHead;
131         tempChildResource && ret == OC_STACK_OK; tempChildResource = tempChildResource->next)
132     {
133         OCResource* temp = tempChildResource->rsrcResource;
134         if (temp)
135         {
136             ret = BuildResponseRepresentation(temp, &linkArr[i++], &ehRequest->devAddr);
137         }
138     }
139
140     if (ret == OC_STACK_OK)
141     {
142         colPayload = OCRepPayloadCreate();
143         if (colPayload)
144         {
145             if (0 == strcmp(OC_RSRVD_INTERFACE_DEFAULT, ifQueryParam))
146             {
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)
151                 {
152                     if (0 != strcmp(OC_RSRVD_RESOURCE_TYPE_COLLECTION, types->resourcetypename))
153                     {
154                         OCRepPayloadAddResourceType(colPayload, types->resourcetypename);
155                     }
156                 }
157                 for (OCResourceInterface *itf = collResource->rsrcInterface; itf; itf = itf->next)
158                 {
159                     OCRepPayloadAddInterface(colPayload, itf->name);
160                 }
161                 AddRTSBaslinePayload(linkArr, i, &colPayload);
162             }
163             OCRepPayloadSetPropObjectArrayAsOwner(colPayload, OC_RSRVD_LINKS, linkArr, dim);
164         }
165     }
166
167 exit:
168     if (ret == OC_STACK_OK)
169     {
170         ehResult = OC_EH_OK;
171     }
172     else
173     {
174         ehResult = (ret == OC_STACK_NO_RESOURCE) ? OC_EH_RESOURCE_NOT_FOUND : OC_EH_ERROR;
175     }
176     ret = SendResponse(colPayload, ehRequest, collResource, ehResult);
177     OIC_LOG_PAYLOAD(DEBUG, (OCPayload *)colPayload);
178     OCRepPayloadDestroy(colPayload);
179     return ret;
180 }
181
182 static OCStackResult HandleBatchInterface(OCEntityHandlerRequest *ehRequest)
183 {
184     if (!ehRequest)
185     {
186         return OC_STACK_INVALID_PARAM;
187     }
188
189     OCStackResult stackRet = OC_STACK_OK;
190     char *storeQuery = NULL;
191     OCResource *collResource = (OCResource *)ehRequest->resource;
192
193     if (stackRet == OC_STACK_OK)
194     {
195
196         if (collResource->rsrcChildResourcesHead)
197         {
198             storeQuery = ehRequest->query;
199             ehRequest->query = NULL;
200             OIC_LOG_V(DEBUG, TAG, "Query : %s", ehRequest->query);
201         }
202
203         uint8_t numRes = 0;
204         for (OCChildResource *tempChildResource = collResource->rsrcChildResourcesHead;
205             tempChildResource; tempChildResource = tempChildResource->next, numRes++)
206         {
207             OCResource* tempRsrcResource = tempChildResource->rsrcResource;
208             if (tempRsrcResource)
209             {
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);
216
217                 // The default collection handler is returning as OK
218                 if (stackRet != OC_STACK_SLOW_RESOURCE)
219                 {
220                     stackRet = OC_STACK_OK;
221                 }
222                 // if a single resource is slow, then entire response will be treated
223                 // as slow response
224                 if (ehResult == OC_EH_SLOW)
225                 {
226                     OIC_LOG(INFO, TAG, "This is a slow resource");
227                     OCServerRequest *request =
228                             GetServerRequestUsingHandle(ehRequest->requestHandle);
229                     if (request)
230                     {
231                         request->slowFlag = 1;
232                     }
233                     stackRet = EntityHandlerCodeToOCStackCode(ehResult);
234                 }
235             }
236             else
237             {
238                 break;
239             }
240         }
241         ehRequest->resource = (OCResourceHandle) collResource;
242     }
243     ehRequest->query = storeQuery;
244     return stackRet;
245 }
246
247 OCStackResult DefaultCollectionEntityHandler(OCEntityHandlerFlag flag, OCEntityHandlerRequest *ehRequest)
248 {
249     if (!ehRequest || !ehRequest->query)
250     {
251         return OC_STACK_INVALID_PARAM;
252     }
253     // Delete is not supported for any interface query method.
254     if (ehRequest->method == OC_REST_DELETE || flag != OC_REQUEST_FLAG)
255     {
256         return OC_STACK_ERROR;
257     }
258     OIC_LOG_V(INFO, TAG, "DefaultCollectionEntityHandler with query %s", ehRequest->query);
259
260     char *ifQueryParam = NULL;
261     char *rtQueryParam = NULL;
262     OCStackResult result = ExtractFiltersFromQuery(ehRequest->query, &ifQueryParam, &rtQueryParam);
263     if (result != OC_STACK_OK)
264     {
265         result = OC_STACK_NO_RESOURCE;
266         goto exit;
267     }
268     if (!ifQueryParam)
269     {
270         ifQueryParam = OICStrdup(OC_RSRVD_INTERFACE_LL);
271     }
272
273     VERIFY_PARAM_NON_NULL(TAG, ifQueryParam, "Invalid Parameter root");
274
275     if (0 == strcmp(ifQueryParam, OC_RSRVD_INTERFACE_LL) || 0 == strcmp (ifQueryParam, OC_RSRVD_INTERFACE_DEFAULT))
276     {
277         if (ehRequest->method == OC_REST_PUT || ehRequest->method == OC_REST_POST)
278         {
279             result =  OC_STACK_ERROR;
280         }
281         else
282         {
283             result = HandleLinkedListInterface(ehRequest, ifQueryParam);
284         }
285     }
286     else if (0 == strcmp(ifQueryParam, OC_RSRVD_INTERFACE_BATCH))
287     {
288         OCServerRequest *request = GetServerRequestUsingHandle(ehRequest->requestHandle);
289         if (request)
290         {
291             request->numResponses = GetNumOfResourcesInCollection((OCResource *)ehRequest->resource);
292             request->ehResponseHandler = HandleAggregateResponse;
293             result = HandleBatchInterface(ehRequest);
294         }
295     }
296     else if (0 == strcmp(ifQueryParam, OC_RSRVD_INTERFACE_GROUP))
297     {
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);
301     }
302 exit:
303     if (result != OC_STACK_OK)
304     {
305         result = SendResponse(NULL, ehRequest, (OCResource *)ehRequest->resource, OC_EH_BAD_REQ);
306     }
307     OICFree(ifQueryParam);
308     OICFree(rtQueryParam);
309     return result;
310 }