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 "ocresource.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 *) OCMalloc(sizeof(OCServerRequest) + reqTotalSize - 1);
90 VERIFY_NON_NULL(serverRequest);
92 memset(serverRequest, 0, sizeof(OCServerRequest) + reqTotalSize - 1);
94 serverRequest->coapID = coapID;
95 serverRequest->delayedResNeeded = delayedResNeeded;
96 serverRequest->secured = secured;
97 serverRequest->notificationFlag = notificationFlag;
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;
108 memcpy(serverRequest->query, query, strlen((const char *)query) + 1);
110 if(rcvdVendorSpecificHeaderOptions)
112 memcpy(serverRequest->rcvdVendorSpecificHeaderOptions, rcvdVendorSpecificHeaderOptions,
113 MAX_HEADER_OPTIONS * sizeof(OCHeaderOption));
117 memcpy((void *)serverRequest->reqJSONPayload, (void *)reqJSONPayload,
118 strlen((const char *)reqJSONPayload) + 1);
120 serverRequest->requestComplete = 0;
123 memcpy(&serverRequest->requestToken, requestToken, sizeof(OCCoAPToken));
127 memcpy(&serverRequest->requesterAddr, requesterAddr, sizeof(OCDevAddr));
131 memcpy(serverRequest->resourceUrl, resourceUrl, strlen((const char *)resourceUrl) + 1);
133 *request = serverRequest;
134 OC_LOG(INFO, TAG, PCF("Server Request Added!!"));
135 LL_APPEND (serverRequestList, serverRequest);
141 OCFree(serverRequest);
142 serverRequest = NULL;
145 return OC_STACK_NO_MEMORY;
148 OCStackResult AddServerResponse (OCServerResponse ** response, OCRequestHandle requestHandle)
150 OCServerResponse * serverResponse = NULL;
152 serverResponse = (OCServerResponse *) OCMalloc(sizeof(OCServerResponse));
153 VERIFY_NON_NULL(serverResponse);
154 memset(serverResponse, 0, sizeof(OCServerResponse));
156 serverResponse->payload = (unsigned char *) OCMalloc(MAX_RESPONSE_LENGTH);
157 VERIFY_NON_NULL(serverResponse->payload);
158 memset(serverResponse->payload, 0, sizeof(MAX_RESPONSE_LENGTH));
160 serverResponse->remainingPayloadSize = MAX_RESPONSE_LENGTH;
161 serverResponse->requestHandle = requestHandle;
163 *response = serverResponse;
164 OC_LOG(INFO, TAG, PCF("Server Response Added!!"));
165 LL_APPEND (serverResponseList, serverResponse);
171 OCFree(serverResponse);
172 serverResponse = NULL;
175 return OC_STACK_NO_MEMORY;
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)
184 if (entityHandlerRequest)
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;
195 entityHandlerRequest->obsInfo.action = observeAction;
196 entityHandlerRequest->obsInfo.obsId = observeID;
200 return OC_STACK_INVALID_PARAM;
203 void FindAndDeleteServerResponse(OCServerResponse * serverResponse)
205 OCServerResponse* tmp;
208 LL_FOREACH(serverResponseList, tmp)
210 if (serverResponse == tmp)
212 DeleteServerResponse(tmp);
219 void DeleteServerResponse(OCServerResponse * serverResponse)
222 LL_DELETE(serverResponseList, serverResponse);
223 OCFree(serverResponse->payload);
224 OCFree(serverResponse);
225 OC_LOG(INFO, TAG, PCF("Server Response Removed!!"));
229 void FindAndDeleteServerRequest(OCServerRequest * serverRequest)
231 OCServerRequest* tmp;
234 LL_FOREACH(serverRequestList, tmp)
236 if (serverRequest == tmp)
238 DeleteServerRequest(tmp);
245 void DeleteServerRequest(OCServerRequest * serverRequest)
248 LL_DELETE(serverRequestList, serverRequest);
249 OCFree(serverRequest);
250 serverRequest = NULL;
251 OC_LOG(INFO, TAG, PCF("Server Request Removed!!"));
255 OCStackResult HandleSingleResponse(OCEntityHandlerResponse * ehResponse)
257 OCStackResult result = OC_STACK_ERROR;
258 OCServerProtocolResponse protocolResponse;
259 memset(&protocolResponse, 0, sizeof(OCServerProtocolResponse));
261 OC_LOG_V(INFO, TAG, "Inside HandleSingleResponse: %s", ehResponse->payload);
263 OCServerRequest *serverRequest = (OCServerRequest *)ehResponse->requestHandle;
264 // Format protocol response structure with data needed for
265 // sending the response
266 protocolResponse.qos = serverRequest->qos;
268 if((OCResource *)ehResponse->resourceHandle &&
269 ((OCResource *)ehResponse->resourceHandle)->resourceProperties == (OCResourceProperty) 0)
271 ehResponse->ehResult = OC_EH_RESOURCE_DELETED;
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;
284 //should we put the prefix and suffix here?
285 protocolResponse.payload = (unsigned char *) OCMalloc(MAX_RESPONSE_LENGTH);
286 if(!protocolResponse.payload)
288 return OC_STACK_NO_MEMORY;
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;
296 //revise the following
297 protocolResponse.coapID = serverRequest->coapID;
298 if(serverRequest->observeResult == OC_STACK_OK)
300 protocolResponse.observationOption = serverRequest->observationOption;
304 protocolResponse.observationOption = OC_OBSERVE_NO_OPTION;
306 // Make call to OCCoAP layer
307 result = OCDoCoAPResponse(&protocolResponse);
309 OCFree(protocolResponse.payload);
311 FindAndDeleteServerRequest(serverRequest);
315 OCStackResult HandleAggregateResponse(OCEntityHandlerResponse * ehResponse)
317 OCStackResult stackRet = OC_STACK_ERROR;
318 OCServerRequest * serverRequest = NULL;
319 OCServerResponse * serverResponse = NULL;
321 OC_LOG_V(INFO, TAG, "Inside HandleAggregateResponse: %s", ehResponse->payload);
323 serverRequest = GetServerRequestUsingHandle((OCServerRequest *)ehResponse->requestHandle);
324 serverResponse = GetServerResponseUsingHandle((OCServerRequest *)ehResponse->requestHandle);
330 OC_LOG(INFO, TAG, PCF("This is the first response fragment"));
331 stackRet = AddServerResponse(&serverResponse, ehResponse->requestHandle);
332 if (OC_STACK_OK != stackRet)
334 OC_LOG(ERROR, TAG, PCF("Error adding server response"));
337 VERIFY_NON_NULL(serverResponse);
338 VERIFY_NON_NULL(serverResponse->payload);
341 if((serverResponse->remainingPayloadSize >= ehResponse->payloadSize + 1 &&
342 serverRequest->numResponses == 1) ||
343 (serverResponse->remainingPayloadSize >= ehResponse->payloadSize + 2 &&
344 serverRequest->numResponses > 1))
346 OC_LOG(INFO, TAG, PCF("There is room in response buffer"));
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)
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);
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;
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;