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 //-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
22 #include "ocserverrequest.h"
23 #include "ocresourcehandler.h"
26 #define VERIFY_NON_NULL(arg) { if (!arg) {OC_LOG(FATAL, TAG, #arg " is NULL"); goto exit;} }
28 #define TAG PCF("ocserverrequest")
30 static struct OCServerRequest * serverRequestList = NULL;
31 static struct OCServerResponse * serverResponseList = NULL;
33 OCServerRequest * GetServerRequestUsingToken (const OCCoAPToken token)
35 OCServerRequest * out = NULL;
36 LL_FOREACH (serverRequestList, out)
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))
47 OC_LOG(INFO, TAG, PCF("Server Request not found!!"));
51 OCServerRequest * GetServerRequestUsingHandle (const OCServerRequest * handle)
53 OCServerRequest * out = NULL;
54 LL_FOREACH (serverRequestList, out)
61 OC_LOG(INFO, TAG, PCF("Server Request not found!!"));
65 OCServerResponse * GetServerResponseUsingHandle (const OCServerRequest * handle)
67 OCServerResponse * out = NULL;
68 LL_FOREACH (serverResponseList, out)
70 if(out->requestHandle == handle)
75 OC_LOG(INFO, TAG, PCF("Server Response not found!!"));
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)
87 OCServerRequest * serverRequest = NULL;
89 serverRequest = (OCServerRequest *) OCCalloc(1, sizeof(OCServerRequest) + reqTotalSize - 1);
90 VERIFY_NON_NULL(serverRequest);
92 serverRequest->coapID = coapID;
93 serverRequest->delayedResNeeded = delayedResNeeded;
94 serverRequest->secured = secured;
95 serverRequest->notificationFlag = notificationFlag;
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;
106 memcpy(serverRequest->query, query, strlen((const char *)query) + 1);
108 if(rcvdVendorSpecificHeaderOptions)
110 memcpy(serverRequest->rcvdVendorSpecificHeaderOptions, rcvdVendorSpecificHeaderOptions,
111 MAX_HEADER_OPTIONS * sizeof(OCHeaderOption));
115 memcpy((void *)serverRequest->reqJSONPayload, (void *)reqJSONPayload,
116 strlen((const char *)reqJSONPayload) + 1);
118 serverRequest->requestComplete = 0;
121 memcpy(&serverRequest->requestToken, requestToken, sizeof(OCCoAPToken));
125 memcpy(&serverRequest->requesterAddr, requesterAddr, sizeof(OCDevAddr));
129 memcpy(serverRequest->resourceUrl, resourceUrl, strlen((const char *)resourceUrl) + 1);
131 *request = serverRequest;
132 OC_LOG(INFO, TAG, PCF("Server Request Added!!"));
133 LL_APPEND (serverRequestList, serverRequest);
139 OCFree(serverRequest);
140 serverRequest = NULL;
143 return OC_STACK_NO_MEMORY;
146 OCStackResult AddServerResponse (OCServerResponse ** response, OCRequestHandle requestHandle)
148 OCServerResponse * serverResponse = NULL;
150 serverResponse = (OCServerResponse *) OCCalloc(1, sizeof(OCServerResponse));
151 VERIFY_NON_NULL(serverResponse);
153 serverResponse->payload = (unsigned char *) OCMalloc(MAX_RESPONSE_LENGTH);
154 VERIFY_NON_NULL(serverResponse->payload);
155 memset(serverResponse->payload, 0, sizeof(MAX_RESPONSE_LENGTH));
157 serverResponse->remainingPayloadSize = MAX_RESPONSE_LENGTH;
158 serverResponse->requestHandle = requestHandle;
160 *response = serverResponse;
161 OC_LOG(INFO, TAG, PCF("Server Response Added!!"));
162 LL_APPEND (serverResponseList, serverResponse);
168 OCFree(serverResponse);
169 serverResponse = NULL;
172 return OC_STACK_NO_MEMORY;
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)
181 if (entityHandlerRequest)
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;
192 entityHandlerRequest->obsInfo.action = observeAction;
193 entityHandlerRequest->obsInfo.obsId = observeID;
197 return OC_STACK_INVALID_PARAM;
200 void FindAndDeleteServerResponse(OCServerResponse * serverResponse)
202 OCServerResponse* tmp;
205 LL_FOREACH(serverResponseList, tmp)
207 if (serverResponse == tmp)
209 DeleteServerResponse(tmp);
216 void DeleteServerResponse(OCServerResponse * serverResponse)
219 LL_DELETE(serverResponseList, serverResponse);
220 OCFree(serverResponse->payload);
221 OCFree(serverResponse);
222 OC_LOG(INFO, TAG, PCF("Server Response Removed!!"));
226 void FindAndDeleteServerRequest(OCServerRequest * serverRequest)
228 OCServerRequest* tmp;
231 LL_FOREACH(serverRequestList, tmp)
233 if (serverRequest == tmp)
235 DeleteServerRequest(tmp);
242 void DeleteServerRequest(OCServerRequest * serverRequest)
245 LL_DELETE(serverRequestList, serverRequest);
246 OCFree(serverRequest);
247 serverRequest = NULL;
248 OC_LOG(INFO, TAG, PCF("Server Request Removed!!"));
252 OCStackResult HandleSingleResponse(OCEntityHandlerResponse * ehResponse)
254 OCStackResult result = OC_STACK_ERROR;
255 OCServerProtocolResponse protocolResponse = {0};
257 OC_LOG_V(INFO, TAG, "Inside HandleSingleResponse: %s", ehResponse->payload);
259 OCServerRequest *serverRequest = (OCServerRequest *)ehResponse->requestHandle;
260 // Format protocol response structure with data needed for
261 // sending the response
262 protocolResponse.qos = serverRequest->qos;
264 if((OCResource *)ehResponse->resourceHandle &&
265 ((OCResource *)ehResponse->resourceHandle)->resourceProperties == (OCResourceProperty) 0)
267 ehResponse->ehResult = OC_EH_RESOURCE_DELETED;
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;
280 //should we put the prefix and suffix here?
281 protocolResponse.payload = (unsigned char *) OCMalloc(MAX_RESPONSE_LENGTH);
282 if(!protocolResponse.payload)
284 return OC_STACK_NO_MEMORY;
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;
292 //revise the following
293 protocolResponse.coapID = serverRequest->coapID;
294 if(serverRequest->observeResult == OC_STACK_OK)
296 protocolResponse.observationOption = serverRequest->observationOption;
300 protocolResponse.observationOption = OC_OBSERVE_NO_OPTION;
302 // Make call to OCCoAP layer
303 result = OCDoCoAPResponse(&protocolResponse);
305 OCFree(protocolResponse.payload);
307 FindAndDeleteServerRequest(serverRequest);
311 OCStackResult HandleAggregateResponse(OCEntityHandlerResponse * ehResponse)
313 OCStackResult stackRet = OC_STACK_ERROR;
314 OCServerRequest * serverRequest = NULL;
315 OCServerResponse * serverResponse = NULL;
317 OC_LOG_V(INFO, TAG, "Inside HandleAggregateResponse: %s", ehResponse->payload);
319 serverRequest = GetServerRequestUsingHandle((OCServerRequest *)ehResponse->requestHandle);
320 serverResponse = GetServerResponseUsingHandle((OCServerRequest *)ehResponse->requestHandle);
326 OC_LOG(INFO, TAG, PCF("This is the first response fragment"));
327 stackRet = AddServerResponse(&serverResponse, ehResponse->requestHandle);
328 if (OC_STACK_OK != stackRet)
330 OC_LOG(ERROR, TAG, PCF("Error adding server response"));
333 VERIFY_NON_NULL(serverResponse);
334 VERIFY_NON_NULL(serverResponse->payload);
337 if((serverResponse->remainingPayloadSize >= ehResponse->payloadSize + 1 &&
338 serverRequest->numResponses == 1) ||
339 (serverResponse->remainingPayloadSize >= ehResponse->payloadSize + 2 &&
340 serverRequest->numResponses > 1))
342 OC_LOG(INFO, TAG, PCF("There is room in response buffer"));
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)
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);
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;
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;