iotivity 0.9.0
[platform/upstream/iotivity.git] / resource / csdk / stack / src / ocserverrequest.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 #include "ocstack.h"
22 #include "ocserverrequest.h"
23 #include "ocresourcehandler.h"
24
25 // Module Name
26 #define VERIFY_NON_NULL(arg) { if (!arg) {OC_LOG(FATAL, TAG, #arg " is NULL"); goto exit;} }
27
28 #define TAG  PCF("ocserverrequest")
29
30 static struct OCServerRequest * serverRequestList = NULL;
31 static struct OCServerResponse * serverResponseList = NULL;
32
33 OCServerRequest * GetServerRequestUsingToken (const OCCoAPToken token)
34 {
35     OCServerRequest * out = NULL;
36     LL_FOREACH (serverRequestList, out)
37     {
38         OC_LOG(INFO, TAG,PCF("comparing tokens"));
39         OC_LOG_BUFFER(INFO, TAG, token.token, token.tokenLength);
40         OC_LOG_BUFFER(INFO, TAG, out->requestToken.token, out->requestToken.tokenLength);
41         if((out->requestToken.tokenLength == token.tokenLength) &&
42                 (memcmp(out->requestToken.token, token.token, token.tokenLength) == 0))
43         {
44             return out;
45         }
46     }
47     OC_LOG(INFO, TAG, PCF("Server Request not found!!"));
48     return NULL;
49 }
50
51 OCServerRequest * GetServerRequestUsingHandle (const OCServerRequest * handle)
52 {
53     OCServerRequest * out = NULL;
54     LL_FOREACH (serverRequestList, out)
55     {
56         if(out == handle)
57         {
58             return out;
59         }
60     }
61     OC_LOG(INFO, TAG, PCF("Server Request not found!!"));
62     return NULL;
63 }
64
65 OCServerResponse * GetServerResponseUsingHandle (const OCServerRequest * handle)
66 {
67     OCServerResponse * out = NULL;
68     LL_FOREACH (serverResponseList, out)
69     {
70         if(out->requestHandle == handle)
71         {
72             return out;
73         }
74     }
75     OC_LOG(INFO, TAG, PCF("Server Response not found!!"));
76     return NULL;
77 }
78
79 OCStackResult AddServerRequest (OCServerRequest ** request, uint16_t coapID,
80         uint8_t delayedResNeeded, uint8_t secured, uint8_t notificationFlag, OCMethod method,
81         uint8_t numRcvdVendorSpecificHeaderOptions, uint32_t observationOption,
82         OCQualityOfService qos, unsigned char * query,
83         OCHeaderOption * rcvdVendorSpecificHeaderOptions,
84         unsigned char * reqJSONPayload, OCCoAPToken * requestToken,
85         OCDevAddr * requesterAddr, unsigned char * resourceUrl, uint32_t reqTotalSize)
86 {
87     OCServerRequest * serverRequest = NULL;
88
89     serverRequest = (OCServerRequest *) OCCalloc(1, sizeof(OCServerRequest) + reqTotalSize - 1);
90     VERIFY_NON_NULL(serverRequest);
91
92     serverRequest->coapID = coapID;
93     serverRequest->delayedResNeeded = delayedResNeeded;
94     serverRequest->secured = secured;
95     serverRequest->notificationFlag = notificationFlag;
96
97     serverRequest->method = method;
98     serverRequest->numRcvdVendorSpecificHeaderOptions = numRcvdVendorSpecificHeaderOptions;
99     serverRequest->observationOption = observationOption;
100     serverRequest->observeResult = OC_STACK_ERROR;
101     serverRequest->qos = qos;
102     serverRequest->ehResponseHandler = HandleSingleResponse;
103     serverRequest->numResponses = 1;
104     if(query)
105     {
106         memcpy(serverRequest->query, query, strlen((const char *)query) + 1);
107     }
108     if(rcvdVendorSpecificHeaderOptions)
109     {
110         memcpy(serverRequest->rcvdVendorSpecificHeaderOptions, rcvdVendorSpecificHeaderOptions,
111             MAX_HEADER_OPTIONS * sizeof(OCHeaderOption));
112     }
113     if(reqJSONPayload)
114     {
115         memcpy((void *)serverRequest->reqJSONPayload, (void *)reqJSONPayload,
116             strlen((const char *)reqJSONPayload) + 1);
117     }
118     serverRequest->requestComplete = 0;
119     if(requestToken)
120     {
121         memcpy(&serverRequest->requestToken, requestToken, sizeof(OCCoAPToken));
122     }
123     if(requesterAddr)
124     {
125         memcpy(&serverRequest->requesterAddr, requesterAddr, sizeof(OCDevAddr));
126     }
127     if(resourceUrl)
128     {
129         memcpy(serverRequest->resourceUrl, resourceUrl, strlen((const char *)resourceUrl) + 1);
130     }
131     *request = serverRequest;
132     OC_LOG(INFO, TAG, PCF("Server Request Added!!"));
133     LL_APPEND (serverRequestList, serverRequest);
134     return OC_STACK_OK;
135
136 exit:
137     if (serverRequest)
138     {
139         OCFree(serverRequest);
140         serverRequest = NULL;
141     }
142     *request = NULL;
143     return OC_STACK_NO_MEMORY;
144 }
145
146 OCStackResult AddServerResponse (OCServerResponse ** response, OCRequestHandle requestHandle)
147 {
148     OCServerResponse * serverResponse = NULL;
149
150     serverResponse = (OCServerResponse *) OCCalloc(1, sizeof(OCServerResponse));
151     VERIFY_NON_NULL(serverResponse);
152
153     serverResponse->payload = (unsigned char *) OCMalloc(MAX_RESPONSE_LENGTH);
154     VERIFY_NON_NULL(serverResponse->payload);
155     memset(serverResponse->payload, 0, sizeof(MAX_RESPONSE_LENGTH));
156
157     serverResponse->remainingPayloadSize = MAX_RESPONSE_LENGTH;
158     serverResponse->requestHandle = requestHandle;
159
160     *response = serverResponse;
161     OC_LOG(INFO, TAG, PCF("Server Response Added!!"));
162     LL_APPEND (serverResponseList, serverResponse);
163     return OC_STACK_OK;
164
165 exit:
166     if (serverResponse)
167     {
168         OCFree(serverResponse);
169         serverResponse = NULL;
170     }
171     *response = NULL;
172     return OC_STACK_NO_MEMORY;
173 }
174
175 // Form the OCEntityHandlerRequest struct
176 OCStackResult FormOCEntityHandlerRequest(OCEntityHandlerRequest * entityHandlerRequest, OCRequestHandle request,
177         OCMethod method, OCResourceHandle resource, unsigned char * queryBuf, unsigned char * bufReqPayload,
178         uint8_t numVendorOptions, OCHeaderOption * vendorOptions, OCObserveAction observeAction,
179         OCObservationId observeID)
180 {
181     if (entityHandlerRequest)
182     {
183         memset(entityHandlerRequest, 0, sizeof(OCEntityHandlerRequest));
184         entityHandlerRequest->requestHandle = request;
185         entityHandlerRequest->method = method;
186         entityHandlerRequest->resource = (OCResourceHandle) resource;
187         entityHandlerRequest->query = queryBuf;
188         entityHandlerRequest->reqJSONPayload = bufReqPayload;
189         entityHandlerRequest->numRcvdVendorSpecificHeaderOptions = numVendorOptions;
190         entityHandlerRequest->rcvdVendorSpecificHeaderOptions = vendorOptions;
191
192         entityHandlerRequest->obsInfo.action = observeAction;
193         entityHandlerRequest->obsInfo.obsId = observeID;
194         return OC_STACK_OK;
195     }
196
197     return OC_STACK_INVALID_PARAM;
198 }
199
200 void FindAndDeleteServerResponse(OCServerResponse * serverResponse)
201 {
202     OCServerResponse* tmp;
203     if(serverResponse)
204     {
205         LL_FOREACH(serverResponseList, tmp)
206         {
207             if (serverResponse == tmp)
208             {
209                 DeleteServerResponse(tmp);
210                 return;
211             }
212         }
213     }
214 }
215
216 void DeleteServerResponse(OCServerResponse * serverResponse)
217 {
218     if(serverResponse) {
219         LL_DELETE(serverResponseList, serverResponse);
220         OCFree(serverResponse->payload);
221         OCFree(serverResponse);
222         OC_LOG(INFO, TAG, PCF("Server Response Removed!!"));
223     }
224 }
225
226 void FindAndDeleteServerRequest(OCServerRequest * serverRequest)
227 {
228     OCServerRequest* tmp;
229     if(serverRequest)
230     {
231         LL_FOREACH(serverRequestList, tmp)
232         {
233             if (serverRequest == tmp)
234             {
235                 DeleteServerRequest(tmp);
236                 return;
237             }
238         }
239     }
240 }
241
242 void DeleteServerRequest(OCServerRequest * serverRequest)
243 {
244     if(serverRequest) {
245         LL_DELETE(serverRequestList, serverRequest);
246         OCFree(serverRequest);
247         serverRequest = NULL;
248         OC_LOG(INFO, TAG, PCF("Server Request Removed!!"));
249     }
250 }
251
252 OCStackResult HandleSingleResponse(OCEntityHandlerResponse * ehResponse)
253 {
254     OCStackResult result = OC_STACK_ERROR;
255     OCServerProtocolResponse protocolResponse = {0};
256
257     OC_LOG_V(INFO, TAG, "Inside HandleSingleResponse: %s", ehResponse->payload);
258
259     OCServerRequest *serverRequest = (OCServerRequest *)ehResponse->requestHandle;
260     // Format protocol response structure with data needed for
261     // sending the response
262     protocolResponse.qos = serverRequest->qos;
263
264     if((OCResource *)ehResponse->resourceHandle &&
265             ((OCResource *)ehResponse->resourceHandle)->resourceProperties == (OCResourceProperty) 0)
266     {
267         ehResponse->ehResult = OC_EH_RESOURCE_DELETED;
268     }
269     protocolResponse.result = EntityHandlerCodeToOCStackCode(ehResponse->ehResult);
270     protocolResponse.requesterAddr = &serverRequest->requesterAddr;
271     protocolResponse.requestToken = &serverRequest->requestToken;
272     protocolResponse.numSendVendorSpecificHeaderOptions = ehResponse->numSendVendorSpecificHeaderOptions;
273     protocolResponse.sendVendorSpecificHeaderOptions = ehResponse->sendVendorSpecificHeaderOptions;
274     protocolResponse.resourceUri = ehResponse->resourceUri;
275     protocolResponse.delayedResNeeded = serverRequest->delayedResNeeded;
276     protocolResponse.secured = serverRequest->secured;
277     protocolResponse.slowFlag = serverRequest->slowFlag;
278     protocolResponse.notificationFlag = serverRequest->notificationFlag;
279
280     //should we put the prefix and suffix here?
281     protocolResponse.payload = (unsigned char *) OCMalloc(MAX_RESPONSE_LENGTH);
282     if(!protocolResponse.payload)
283     {
284         return OC_STACK_NO_MEMORY;
285     }
286     strcpy((char *)protocolResponse.payload, (const char *)OC_JSON_PREFIX);
287     strcat((char *)protocolResponse.payload, (const char *)ehResponse->payload);
288     strcat((char *)protocolResponse.payload, (const char *)OC_JSON_SUFFIX);
289     protocolResponse.payloadSize = strlen((const char *)protocolResponse.payload) + 1;
290     protocolResponse.resourceUri = ehResponse->resourceUri;
291
292     //revise the following
293     protocolResponse.coapID = serverRequest->coapID;
294     if(serverRequest->observeResult == OC_STACK_OK)
295     {
296         protocolResponse.observationOption = serverRequest->observationOption;
297     }
298     else
299     {
300         protocolResponse.observationOption = OC_OBSERVE_NO_OPTION;
301     }
302     // Make call to OCCoAP layer
303     result = OCDoCoAPResponse(&protocolResponse);
304
305     OCFree(protocolResponse.payload);
306     //Delete the request
307     FindAndDeleteServerRequest(serverRequest);
308     return result;
309 }
310
311 OCStackResult HandleAggregateResponse(OCEntityHandlerResponse * ehResponse)
312 {
313     OCStackResult stackRet = OC_STACK_ERROR;
314     OCServerRequest * serverRequest = NULL;
315     OCServerResponse * serverResponse = NULL;
316
317     OC_LOG_V(INFO, TAG, "Inside HandleAggregateResponse: %s", ehResponse->payload);
318
319     serverRequest = GetServerRequestUsingHandle((OCServerRequest *)ehResponse->requestHandle);
320     serverResponse = GetServerResponseUsingHandle((OCServerRequest *)ehResponse->requestHandle);
321
322     if(serverRequest)
323     {
324         if(!serverResponse)
325         {
326             OC_LOG(INFO, TAG, PCF("This is the first response fragment"));
327             stackRet = AddServerResponse(&serverResponse, ehResponse->requestHandle);
328             if (OC_STACK_OK != stackRet)
329             {
330                 OC_LOG(ERROR, TAG, PCF("Error adding server response"));
331                 return stackRet;
332             }
333             VERIFY_NON_NULL(serverResponse);
334             VERIFY_NON_NULL(serverResponse->payload);
335         }
336
337         if((serverResponse->remainingPayloadSize >= ehResponse->payloadSize + 1 &&
338                 serverRequest->numResponses == 1) ||
339                 (serverResponse->remainingPayloadSize >= ehResponse->payloadSize + 2 &&
340                         serverRequest->numResponses > 1))
341         {
342             OC_LOG(INFO, TAG, PCF("There is room in response buffer"));
343             // append
344             snprintf((char *)serverResponse->payload, serverResponse->remainingPayloadSize, "%s%s", (char *)serverResponse->payload, (char *)ehResponse->payload);
345             OC_LOG_V(INFO, TAG, "Current aggregated response  ...%s", serverResponse->payload);
346             serverResponse->remainingPayloadSize -= ehResponse->payloadSize;
347             (serverRequest->numResponses)--;
348             if(serverRequest->numResponses == 0)
349             {
350                 OC_LOG(INFO, TAG, PCF("This is the last response fragment"));
351                 ehResponse->payload = serverResponse->payload;
352                 ehResponse->payloadSize = strlen((char *) serverResponse->payload) + 1;
353                 stackRet = HandleSingleResponse(ehResponse);
354                 //Delete the request and response
355                 FindAndDeleteServerRequest(serverRequest);
356                 FindAndDeleteServerResponse(serverResponse);
357             }
358             else
359             {
360                 OC_LOG(INFO, TAG, PCF("More response fragment to come"));
361                 // TODO: we should consider using strcat rather than setting a char by char here!
362                 snprintf((char *)serverResponse->payload, serverResponse->remainingPayloadSize, "%s%c", (char *)serverResponse->payload,OC_JSON_SEPARATOR);
363                 OC_LOG_V(INFO, TAG, "Current aggregated response  ...%s", serverResponse->payload);
364                 (serverResponse->remainingPayloadSize)--;
365                 stackRet = OC_STACK_OK;
366             }
367         }
368         else
369         {
370             OC_LOG(INFO, TAG, PCF("No room in response buffer"));
371             //Delete the request and response
372             FindAndDeleteServerRequest(serverRequest);
373             FindAndDeleteServerResponse(serverResponse);
374             stackRet = OC_STACK_NO_MEMORY;
375         }
376     }
377 exit:
378     return stackRet;
379 }