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