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 //-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
23 #include "ocserverrequest.h"
24 #include "ocresourcehandler.h"
28 #include "cainterface.h"
34 #define VERIFY_NON_NULL(arg) { if (!arg) {OC_LOG(FATAL, TAG, #arg " is NULL"); goto exit;} }
36 #define TAG PCF("ocserverrequest")
38 static struct OCServerRequest * serverRequestList = NULL;
39 static struct OCServerResponse * serverResponseList = NULL;
41 OCServerRequest * GetServerRequestUsingToken (const CAToken_t token)
43 OCServerRequest * out = NULL;
44 LL_FOREACH (serverRequestList, out)
46 OC_LOG(INFO, TAG,PCF("comparing tokens"));
47 OC_LOG_BUFFER(INFO, TAG, (const uint8_t *)token, CA_MAX_TOKEN_LEN);
48 OC_LOG_BUFFER(INFO, TAG, (const uint8_t *)out->requestToken, CA_MAX_TOKEN_LEN);
49 if(memcmp(out->requestToken, token, CA_MAX_TOKEN_LEN) == 0)
54 OC_LOG(INFO, TAG, PCF("Server Request not found!!"));
58 OCServerRequest * GetServerRequestUsingHandle (const OCServerRequest * handle)
60 OCServerRequest * out = NULL;
61 LL_FOREACH (serverRequestList, out)
68 OC_LOG(INFO, TAG, PCF("Server Request not found!!"));
72 OCServerResponse * GetServerResponseUsingHandle (const OCServerRequest * handle)
74 OCServerResponse * out = NULL;
75 LL_FOREACH (serverResponseList, out)
77 if(out->requestHandle == handle)
82 OC_LOG(INFO, TAG, PCF("Server Response not found!!"));
86 OCStackResult AddServerRequest (OCServerRequest ** request, uint16_t coapID,
87 uint8_t delayedResNeeded, uint8_t secured, uint8_t notificationFlag, OCMethod method,
88 uint8_t numRcvdVendorSpecificHeaderOptions, uint32_t observationOption,
89 OCQualityOfService qos, unsigned char * query,
90 OCHeaderOption * rcvdVendorSpecificHeaderOptions,
91 unsigned char * reqJSONPayload, CAToken_t * requestToken,
92 OCDevAddr * requesterAddr, unsigned char * resourceUrl, size_t reqTotalSize,
93 CAAddress_t *addressInfo, CAConnectivityType_t connectivityType)
95 OCServerRequest * serverRequest = NULL;
97 //Note: OCServerRequest includes 1 byte for the JSON Payload. payloadSize is calculated
98 //as the required length of the string, so this will result in enough room for the
99 //null terminator as well.
100 serverRequest = (OCServerRequest *) OCCalloc(1, sizeof(OCServerRequest) + reqTotalSize - 1);
101 VERIFY_NON_NULL(serverRequest);
103 serverRequest->coapID = coapID;
104 serverRequest->delayedResNeeded = delayedResNeeded;
105 serverRequest->secured = secured;
106 serverRequest->notificationFlag = notificationFlag;
108 serverRequest->method = method;
109 serverRequest->numRcvdVendorSpecificHeaderOptions = numRcvdVendorSpecificHeaderOptions;
110 serverRequest->observationOption = observationOption;
111 serverRequest->observeResult = OC_STACK_ERROR;
112 serverRequest->qos = qos;
113 serverRequest->ehResponseHandler = HandleSingleResponse;
114 serverRequest->numResponses = 1;
117 memcpy(serverRequest->query, query, strlen((const char *)query) + 1);
119 if(rcvdVendorSpecificHeaderOptions)
121 memcpy(serverRequest->rcvdVendorSpecificHeaderOptions, rcvdVendorSpecificHeaderOptions,
122 MAX_HEADER_OPTIONS * sizeof(OCHeaderOption));
126 // destination is at least 1 greater than the source, so a NULL always exists in the
128 strncpy((char*)serverRequest->reqJSONPayload,
129 (const char*)reqJSONPayload, reqTotalSize - 1);
131 serverRequest->requestComplete = 0;
134 serverRequest->requestToken = (CAToken_t)OCMalloc(CA_MAX_TOKEN_LEN+1);
135 VERIFY_NON_NULL (serverRequest->requestToken);
136 memset(serverRequest->requestToken, 0, CA_MAX_TOKEN_LEN + 1);
137 memcpy(serverRequest->requestToken, *requestToken, CA_MAX_TOKEN_LEN);
141 memcpy(&serverRequest->requesterAddr, requesterAddr, sizeof(OCDevAddr));
145 memcpy(serverRequest->resourceUrl, resourceUrl, strlen((const char *)resourceUrl) + 1);
149 serverRequest->addressInfo = *addressInfo;
151 serverRequest->connectivityType = connectivityType;
153 *request = serverRequest;
154 OC_LOG(INFO, TAG, PCF("Server Request Added!!"));
155 LL_APPEND (serverRequestList, serverRequest);
161 OCFree(serverRequest);
162 serverRequest = NULL;
165 return OC_STACK_NO_MEMORY;
168 OCStackResult AddServerResponse (OCServerResponse ** response, OCRequestHandle requestHandle)
170 OCServerResponse * serverResponse = NULL;
172 serverResponse = (OCServerResponse *) OCCalloc(1, sizeof(OCServerResponse));
173 VERIFY_NON_NULL(serverResponse);
175 serverResponse->payload = (unsigned char *) OCMalloc(MAX_RESPONSE_LENGTH);
176 VERIFY_NON_NULL(serverResponse->payload);
177 memset(serverResponse->payload, 0, MAX_RESPONSE_LENGTH);
179 serverResponse->remainingPayloadSize = MAX_RESPONSE_LENGTH;
180 serverResponse->requestHandle = requestHandle;
182 *response = serverResponse;
183 OC_LOG(INFO, TAG, PCF("Server Response Added!!"));
184 LL_APPEND (serverResponseList, serverResponse);
190 OCFree(serverResponse);
191 serverResponse = NULL;
194 return OC_STACK_NO_MEMORY;
197 // Form the OCEntityHandlerRequest struct
198 OCStackResult FormOCEntityHandlerRequest(OCEntityHandlerRequest * entityHandlerRequest, OCRequestHandle request,
199 OCMethod method, OCResourceHandle resource, unsigned char * queryBuf, unsigned char * bufReqPayload,
200 uint8_t numVendorOptions, OCHeaderOption * vendorOptions, OCObserveAction observeAction,
201 OCObservationId observeID)
203 if (entityHandlerRequest)
205 memset(entityHandlerRequest, 0, sizeof(OCEntityHandlerRequest));
206 entityHandlerRequest->requestHandle = request;
207 entityHandlerRequest->method = method;
208 entityHandlerRequest->resource = (OCResourceHandle) resource;
209 entityHandlerRequest->query = queryBuf;
210 entityHandlerRequest->reqJSONPayload = bufReqPayload;
211 entityHandlerRequest->numRcvdVendorSpecificHeaderOptions = numVendorOptions;
212 entityHandlerRequest->rcvdVendorSpecificHeaderOptions = vendorOptions;
214 entityHandlerRequest->obsInfo.action = observeAction;
215 entityHandlerRequest->obsInfo.obsId = observeID;
219 return OC_STACK_INVALID_PARAM;
222 void FindAndDeleteServerResponse(OCServerResponse * serverResponse)
224 OCServerResponse* tmp;
227 LL_FOREACH(serverResponseList, tmp)
229 if (serverResponse == tmp)
231 DeleteServerResponse(tmp);
238 void DeleteServerResponse(OCServerResponse * serverResponse)
241 LL_DELETE(serverResponseList, serverResponse);
242 OCFree(serverResponse->payload);
243 OCFree(serverResponse);
244 OC_LOG(INFO, TAG, PCF("Server Response Removed!!"));
248 void FindAndDeleteServerRequest(OCServerRequest * serverRequest)
250 OCServerRequest* tmp;
253 LL_FOREACH(serverRequestList, tmp)
255 if (serverRequest == tmp)
257 DeleteServerRequest(tmp);
264 void DeleteServerRequest(OCServerRequest * serverRequest)
267 LL_DELETE(serverRequestList, serverRequest);
268 OCFree(serverRequest);
269 serverRequest = NULL;
270 OC_LOG(INFO, TAG, PCF("Server Request Removed!!"));
274 OCStackResult HandleSingleResponse(OCEntityHandlerResponse * ehResponse)
276 OCStackResult result = OC_STACK_ERROR;
277 CARemoteEndpoint_t responseEndpoint = {0};
278 CAResponseInfo_t responseInfo = {0};
279 CAHeaderOption_t* optionsPointer;
281 OC_LOG_V(INFO, TAG, "Inside HandleSingleResponse: %s", ehResponse->payload);
283 OCServerRequest *serverRequest = (OCServerRequest *)ehResponse->requestHandle;
286 responseEndpoint.resourceUri = (CAURI_t) serverRequest->resourceUrl;
287 responseEndpoint.addressInfo = serverRequest->addressInfo;
288 responseEndpoint.connectivityType = serverRequest->connectivityType;
289 responseEndpoint.isSecured = (CABool_t) serverRequest->secured;
292 switch (ehResponse->ehResult)
295 responseInfo.result = CA_SUCCESS;
298 responseInfo.result = CA_BAD_REQ;
300 case OC_EH_RESOURCE_CREATED:
301 responseInfo.result = CA_CREATED;
303 case OC_EH_RESOURCE_DELETED:
304 responseInfo.result = CA_DELETED;
307 responseInfo.result = CA_SUCCESS;
309 case OC_EH_FORBIDDEN:
310 responseInfo.result = CA_BAD_REQ;
313 responseInfo.result = CA_BAD_REQ;
317 // TODO-CA: Need to do something with a slow response if a confirmed request was sent
320 // TODO-CA: Need to handle CA_MSG_RESET and CA_MSG_ACKNOWLEDGE
321 switch (serverRequest->qos)
324 responseInfo.info.type = CA_MSG_NONCONFIRM;
327 responseInfo.info.type = CA_MSG_NONCONFIRM;
330 responseInfo.info.type = CA_MSG_CONFIRM;
333 responseInfo.info.type = CA_MSG_NONCONFIRM;
336 responseInfo.info.type = CA_MSG_NONCONFIRM;
340 responseInfo.info.token = (CAToken_t)OCMalloc(CA_MAX_TOKEN_LEN+1);
341 if (!responseInfo.info.token)
343 OC_LOG(FATAL, TAG, "Response Info Token is NULL");
346 memset(responseInfo.info.token, 0, CA_MAX_TOKEN_LEN + 1);
347 memcpy(responseInfo.info.token, serverRequest->requestToken, CA_MAX_TOKEN_LEN);
349 if(serverRequest->observeResult == OC_STACK_OK)
351 responseInfo.info.numOptions = ehResponse->numSendVendorSpecificHeaderOptions + 1;
355 responseInfo.info.numOptions = ehResponse->numSendVendorSpecificHeaderOptions;
358 responseInfo.info.options = (CAHeaderOption_t *)
359 malloc(sizeof(CAHeaderOption_t) * responseInfo.info.numOptions);
361 optionsPointer = responseInfo.info.options;
363 if(serverRequest->observeResult == OC_STACK_OK)
365 responseInfo.info.numOptions = ehResponse->numSendVendorSpecificHeaderOptions + 1;
368 // TODO-CA Revisit this logic
369 if(serverRequest->observeResult == OC_STACK_OK)
371 responseInfo.info.options[0].protocolID = CA_COAP_ID;
372 responseInfo.info.options[0].optionID = COAP_OPTION_OBSERVE;
373 responseInfo.info.options[0].optionLength = sizeof(uint32_t);
374 memcpy(responseInfo.info.options[0].optionData,
375 &(serverRequest->observationOption), sizeof(uint32_t));
377 // Point to the next header option before copying vender specific header options
381 if (ehResponse->numSendVendorSpecificHeaderOptions)
383 memcpy(optionsPointer, ehResponse->sendVendorSpecificHeaderOptions,
384 sizeof(OCHeaderOption) * ehResponse->numSendVendorSpecificHeaderOptions);
387 // Allocate memory for the payload.
388 char *payload = (char *)OCMalloc(MAX_RESPONSE_LENGTH);
391 return OC_STACK_NO_MEMORY;
393 memset(payload, 0, MAX_RESPONSE_LENGTH);
394 // Put the JSON prefix and suffix around the payload
395 strcpy(payload, (const char *)OC_JSON_PREFIX);
396 strcat(payload, (const char *)ehResponse->payload);
397 strcat(payload, (const char *)OC_JSON_SUFFIX);
398 responseInfo.info.payload = (CAPayload_t)payload;
400 CAResult_t caResult = CASendResponse(&responseEndpoint, &responseInfo);
401 if(caResult != CA_STATUS_OK)
403 OC_LOG(ERROR, TAG, PCF("CASendResponse error"));
407 result = OC_STACK_OK;
412 FindAndDeleteServerRequest(serverRequest);
416 OCStackResult HandleAggregateResponse(OCEntityHandlerResponse * ehResponse)
418 OCStackResult stackRet = OC_STACK_ERROR;
419 OCServerRequest * serverRequest = NULL;
420 OCServerResponse * serverResponse = NULL;
422 OC_LOG_V(INFO, TAG, "Inside HandleAggregateResponse: %s", ehResponse->payload);
424 serverRequest = GetServerRequestUsingHandle((OCServerRequest *)ehResponse->requestHandle);
425 serverResponse = GetServerResponseUsingHandle((OCServerRequest *)ehResponse->requestHandle);
431 OC_LOG(INFO, TAG, PCF("This is the first response fragment"));
432 stackRet = AddServerResponse(&serverResponse, ehResponse->requestHandle);
433 if (OC_STACK_OK != stackRet)
435 OC_LOG(ERROR, TAG, PCF("Error adding server response"));
438 VERIFY_NON_NULL(serverResponse);
439 VERIFY_NON_NULL(serverResponse->payload);
442 if((serverResponse->remainingPayloadSize >= ehResponse->payloadSize + 1 &&
443 serverRequest->numResponses == 1) ||
444 (serverResponse->remainingPayloadSize >= ehResponse->payloadSize + 2 &&
445 serverRequest->numResponses > 1))
447 OC_LOG(INFO, TAG, PCF("There is room in response buffer"));
449 snprintf((char *)serverResponse->payload, serverResponse->remainingPayloadSize, "%s%s", (char *)serverResponse->payload, (char *)ehResponse->payload);
450 OC_LOG_V(INFO, TAG, "Current aggregated response ...%s", serverResponse->payload);
451 serverResponse->remainingPayloadSize -= ehResponse->payloadSize;
452 (serverRequest->numResponses)--;
453 if(serverRequest->numResponses == 0)
455 OC_LOG(INFO, TAG, PCF("This is the last response fragment"));
456 ehResponse->payload = serverResponse->payload;
457 ehResponse->payloadSize = strlen((char *) serverResponse->payload) + 1;
458 stackRet = HandleSingleResponse(ehResponse);
459 //Delete the request and response
460 FindAndDeleteServerRequest(serverRequest);
461 FindAndDeleteServerResponse(serverResponse);
465 OC_LOG(INFO, TAG, PCF("More response fragment to come"));
466 // TODO: we should consider using strcat rather than setting a char by char here!
467 snprintf((char *)serverResponse->payload, serverResponse->remainingPayloadSize, "%s%c", (char *)serverResponse->payload,OC_JSON_SEPARATOR);
468 OC_LOG_V(INFO, TAG, "Current aggregated response ...%s", serverResponse->payload);
469 (serverResponse->remainingPayloadSize)--;
470 stackRet = OC_STACK_OK;
475 OC_LOG(INFO, TAG, PCF("No room in response buffer"));
476 //Delete the request and response
477 FindAndDeleteServerRequest(serverRequest);
478 FindAndDeleteServerResponse(serverResponse);
479 stackRet = OC_STACK_NO_MEMORY;