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"
28 #include "cainterface.h"
32 #define VERIFY_NON_NULL(arg) { if (!arg) {OC_LOG(FATAL, TAG, #arg " is NULL"); goto exit;} }
34 #define TAG PCF("ocserverrequest")
36 static struct OCServerRequest * serverRequestList = NULL;
37 static struct OCServerResponse * serverResponseList = NULL;
39 OCServerRequest * GetServerRequestUsingToken (const OCCoAPToken token)
41 OCServerRequest * out = NULL;
42 LL_FOREACH (serverRequestList, out)
44 OC_LOG(INFO, TAG,PCF("comparing tokens"));
45 OC_LOG_BUFFER(INFO, TAG, token.token, token.tokenLength);
46 OC_LOG_BUFFER(INFO, TAG, out->requestToken.token, out->requestToken.tokenLength);
47 if((out->requestToken.tokenLength == token.tokenLength) &&
48 (memcmp(out->requestToken.token, token.token, token.tokenLength) == 0))
53 OC_LOG(INFO, TAG, PCF("Server Request not found!!"));
57 OCServerRequest * GetServerRequestUsingHandle (const OCServerRequest * handle)
59 OCServerRequest * out = NULL;
60 LL_FOREACH (serverRequestList, out)
67 OC_LOG(INFO, TAG, PCF("Server Request not found!!"));
71 OCServerResponse * GetServerResponseUsingHandle (const OCServerRequest * handle)
73 OCServerResponse * out = NULL;
74 LL_FOREACH (serverResponseList, out)
76 if(out->requestHandle == handle)
81 OC_LOG(INFO, TAG, PCF("Server Response not found!!"));
85 OCStackResult AddServerRequest (OCServerRequest ** request, uint16_t coapID,
86 uint8_t delayedResNeeded, uint8_t secured, uint8_t notificationFlag, OCMethod method,
87 uint8_t numRcvdVendorSpecificHeaderOptions, uint32_t observationOption,
88 OCQualityOfService qos, unsigned char * query,
89 OCHeaderOption * rcvdVendorSpecificHeaderOptions,
90 unsigned char * reqJSONPayload, OCCoAPToken * requestToken,
91 OCDevAddr * requesterAddr, unsigned char * resourceUrl, size_t reqTotalSize)
93 OCServerRequest * serverRequest = NULL;
95 //Note: OCServerRequest includes 1 byte for the JSON Payload. payloadSize is calculated
96 //as the required length of the string, so this will result in enough room for the
97 //null terminator as well.
98 serverRequest = (OCServerRequest *) OCCalloc(1, sizeof(OCServerRequest) + reqTotalSize - 1);
99 VERIFY_NON_NULL(serverRequest);
101 serverRequest->coapID = coapID;
102 serverRequest->delayedResNeeded = delayedResNeeded;
103 serverRequest->secured = secured;
104 serverRequest->notificationFlag = notificationFlag;
106 serverRequest->method = method;
107 serverRequest->numRcvdVendorSpecificHeaderOptions = numRcvdVendorSpecificHeaderOptions;
108 serverRequest->observationOption = observationOption;
109 serverRequest->observeResult = OC_STACK_ERROR;
110 serverRequest->qos = qos;
111 serverRequest->ehResponseHandler = HandleSingleResponse;
112 serverRequest->numResponses = 1;
115 memcpy(serverRequest->query, query, strlen((const char *)query) + 1);
117 if(rcvdVendorSpecificHeaderOptions)
119 memcpy(serverRequest->rcvdVendorSpecificHeaderOptions, rcvdVendorSpecificHeaderOptions,
120 MAX_HEADER_OPTIONS * sizeof(OCHeaderOption));
124 // destination is at least 1 greater than the source, so a NULL always exists in the
126 strncpy((char*)serverRequest->reqJSONPayload,
127 (const char*)reqJSONPayload, reqTotalSize - 1);
129 serverRequest->requestComplete = 0;
132 memcpy(&serverRequest->requestToken, requestToken, sizeof(OCCoAPToken));
136 memcpy(&serverRequest->requesterAddr, requesterAddr, sizeof(OCDevAddr));
140 memcpy(serverRequest->resourceUrl, resourceUrl, strlen((const char *)resourceUrl) + 1);
143 *request = serverRequest;
144 OC_LOG(INFO, TAG, PCF("Server Request Added!!"));
145 LL_APPEND (serverRequestList, serverRequest);
151 OCFree(serverRequest);
152 serverRequest = NULL;
155 return OC_STACK_NO_MEMORY;
159 OCStackResult AddServerCARequest (OCServerRequest ** request, uint16_t coapID,
160 uint8_t delayedResNeeded, uint8_t secured, uint8_t notificationFlag, OCMethod method,
161 uint8_t numRcvdVendorSpecificHeaderOptions, uint32_t observationOption,
162 OCQualityOfService qos, unsigned char * query,
163 OCHeaderOption * rcvdVendorSpecificHeaderOptions,
164 unsigned char * reqJSONPayload, OCCoAPToken * requestToken,
165 OCDevAddr * requesterAddr, unsigned char * resourceUrl, size_t reqTotalSize,
166 CAAddress_t *addressInfo, CAConnectivityType_t connectivityType, char *token)
168 OCServerRequest * serverRequest = NULL;
170 //Note: OCServerRequest includes 1 byte for the JSON Payload. payloadSize is calculated
171 //as the required length of the string, so this will result in enough room for the
172 //null terminator as well.
173 serverRequest = (OCServerRequest *) OCCalloc(1, sizeof(OCServerRequest) + reqTotalSize - 1);
174 VERIFY_NON_NULL(serverRequest);
176 serverRequest->coapID = coapID;
177 serverRequest->delayedResNeeded = delayedResNeeded;
178 serverRequest->secured = secured;
179 serverRequest->notificationFlag = notificationFlag;
181 serverRequest->method = method;
182 serverRequest->numRcvdVendorSpecificHeaderOptions = numRcvdVendorSpecificHeaderOptions;
183 serverRequest->observationOption = observationOption;
184 serverRequest->observeResult = OC_STACK_ERROR;
185 serverRequest->qos = qos;
186 serverRequest->ehResponseHandler = HandleSingleResponse;
187 serverRequest->numResponses = 1;
190 memcpy(serverRequest->query, query, strlen((const char *)query) + 1);
192 if(rcvdVendorSpecificHeaderOptions)
194 memcpy(serverRequest->rcvdVendorSpecificHeaderOptions, rcvdVendorSpecificHeaderOptions,
195 MAX_HEADER_OPTIONS * sizeof(OCHeaderOption));
199 // destination is at least 1 greater than the source, so a NULL always exists in the
201 strncpy((char*)serverRequest->reqJSONPayload,
202 (const char*)reqJSONPayload, reqTotalSize - 1);
204 serverRequest->requestComplete = 0;
207 memcpy(&serverRequest->requestToken, requestToken, sizeof(OCCoAPToken));
211 memcpy(&serverRequest->requesterAddr, requesterAddr, sizeof(OCDevAddr));
215 memcpy(serverRequest->resourceUrl, resourceUrl, strlen((const char *)resourceUrl) + 1);
220 serverRequest->addressInfo = *addressInfo;
222 serverRequest->connectivityType = connectivityType;
225 strncpy(serverRequest->token, token, CA_MAX_TOKEN_LEN);
228 *request = serverRequest;
229 OC_LOG(INFO, TAG, PCF("Server Request Added!!"));
230 LL_APPEND (serverRequestList, serverRequest);
236 OCFree(serverRequest);
237 serverRequest = NULL;
240 return OC_STACK_NO_MEMORY;
244 OCStackResult AddServerResponse (OCServerResponse ** response, OCRequestHandle requestHandle)
246 OCServerResponse * serverResponse = NULL;
248 serverResponse = (OCServerResponse *) OCCalloc(1, sizeof(OCServerResponse));
249 VERIFY_NON_NULL(serverResponse);
251 serverResponse->payload = (unsigned char *) OCMalloc(MAX_RESPONSE_LENGTH);
252 VERIFY_NON_NULL(serverResponse->payload);
253 memset(serverResponse->payload, 0, sizeof(MAX_RESPONSE_LENGTH));
255 serverResponse->remainingPayloadSize = MAX_RESPONSE_LENGTH;
256 serverResponse->requestHandle = requestHandle;
258 *response = serverResponse;
259 OC_LOG(INFO, TAG, PCF("Server Response Added!!"));
260 LL_APPEND (serverResponseList, serverResponse);
266 OCFree(serverResponse);
267 serverResponse = NULL;
270 return OC_STACK_NO_MEMORY;
273 // Form the OCEntityHandlerRequest struct
274 OCStackResult FormOCEntityHandlerRequest(OCEntityHandlerRequest * entityHandlerRequest, OCRequestHandle request,
275 OCMethod method, OCResourceHandle resource, unsigned char * queryBuf, unsigned char * bufReqPayload,
276 uint8_t numVendorOptions, OCHeaderOption * vendorOptions, OCObserveAction observeAction,
277 OCObservationId observeID)
279 if (entityHandlerRequest)
281 memset(entityHandlerRequest, 0, sizeof(OCEntityHandlerRequest));
282 entityHandlerRequest->requestHandle = request;
283 entityHandlerRequest->method = method;
284 entityHandlerRequest->resource = (OCResourceHandle) resource;
285 entityHandlerRequest->query = queryBuf;
286 entityHandlerRequest->reqJSONPayload = bufReqPayload;
287 entityHandlerRequest->numRcvdVendorSpecificHeaderOptions = numVendorOptions;
288 entityHandlerRequest->rcvdVendorSpecificHeaderOptions = vendorOptions;
290 entityHandlerRequest->obsInfo.action = observeAction;
291 entityHandlerRequest->obsInfo.obsId = observeID;
295 return OC_STACK_INVALID_PARAM;
298 void FindAndDeleteServerResponse(OCServerResponse * serverResponse)
300 OCServerResponse* tmp;
303 LL_FOREACH(serverResponseList, tmp)
305 if (serverResponse == tmp)
307 DeleteServerResponse(tmp);
314 void DeleteServerResponse(OCServerResponse * serverResponse)
317 LL_DELETE(serverResponseList, serverResponse);
318 OCFree(serverResponse->payload);
319 OCFree(serverResponse);
320 OC_LOG(INFO, TAG, PCF("Server Response Removed!!"));
324 void FindAndDeleteServerRequest(OCServerRequest * serverRequest)
326 OCServerRequest* tmp;
329 LL_FOREACH(serverRequestList, tmp)
331 if (serverRequest == tmp)
333 DeleteServerRequest(tmp);
340 void DeleteServerRequest(OCServerRequest * serverRequest)
343 LL_DELETE(serverRequestList, serverRequest);
344 OCFree(serverRequest);
345 serverRequest = NULL;
346 OC_LOG(INFO, TAG, PCF("Server Request Removed!!"));
350 OCStackResult HandleSingleResponse(OCEntityHandlerResponse * ehResponse)
353 OCStackResult result = OC_STACK_ERROR;
354 CARemoteEndpoint_t responseEndpoint;
355 CAResponseInfo_t responseInfo;
356 CAHeaderOption_t* optionsPointer;
358 OC_LOG_V(INFO, TAG, "Inside HandleSingleResponse: %s", ehResponse->payload);
360 OCServerRequest *serverRequest = (OCServerRequest *)ehResponse->requestHandle;
363 responseEndpoint.resourceUri = serverRequest->resourceUrl;
364 responseEndpoint.addressInfo = serverRequest->addressInfo;
365 responseEndpoint.connectivityType = serverRequest->connectivityType;
366 responseEndpoint.isSecured = serverRequest->secured;
369 switch (ehResponse->ehResult)
372 responseInfo.result = CA_SUCCESS;
375 responseInfo.result = CA_BAD_REQ;
377 case OC_EH_RESOURCE_CREATED:
378 responseInfo.result = CA_CREATED;
380 case OC_EH_RESOURCE_DELETED:
381 responseInfo.result = CA_DELETED;
384 responseInfo.result = CA_SUCCESS;
386 case OC_EH_FORBIDDEN:
387 responseInfo.result = CA_BAD_REQ;
390 responseInfo.result = CA_BAD_REQ;
394 // TODO-CA: Need to do something with a slow response if a confirmed request was sent
397 // TODO-CA: Need to handle CA_MSG_RESET and CA_MSG_ACKNOWLEDGE
398 switch (serverRequest->qos)
401 responseInfo.info.type = CA_MSG_NONCONFIRM;
404 responseInfo.info.type = CA_MSG_NONCONFIRM;
407 responseInfo.info.type = CA_MSG_CONFIRM;
410 responseInfo.info.type = CA_MSG_NONCONFIRM;
413 responseInfo.info.type = CA_MSG_NONCONFIRM;
417 responseInfo.info.token = serverRequest->token;
419 if(serverRequest->observeResult == OC_STACK_OK)
421 responseInfo.info.numOptions = ehResponse->numSendVendorSpecificHeaderOptions + 1;
425 responseInfo.info.numOptions = ehResponse->numSendVendorSpecificHeaderOptions;
428 responseInfo.info.options = (CAHeaderOption_t *)
429 malloc(sizeof(CAHeaderOption_t) * responseInfo.info.numOptions);
431 optionsPointer = responseInfo.info.options;
433 if(serverRequest->observeResult == OC_STACK_OK)
435 responseInfo.info.numOptions = ehResponse->numSendVendorSpecificHeaderOptions + 1;
438 // TODO-CA Revisit this logic
439 if(serverRequest->observeResult == OC_STACK_OK)
441 responseInfo.info.options[0].protocolID = CA_COAP_ID;
442 responseInfo.info.options[0].optionID = COAP_OPTION_OBSERVE;
443 // TODO-CA Remove the magic number 4
444 responseInfo.info.options[0].optionLength = 4;
445 memcpy(responseInfo.info.options[0].optionData, &(serverRequest->observationOption), 4);
447 // Point to the next header option before copying vender specific header options
451 if (ehResponse->numSendVendorSpecificHeaderOptions)
453 memcpy(optionsPointer, ehResponse->sendVendorSpecificHeaderOptions,
454 sizeof(OCHeaderOption) * ehResponse->numSendVendorSpecificHeaderOptions);
457 // Allocate memory for the payload.
458 char *payload = (char *)OCMalloc(MAX_RESPONSE_LENGTH);
461 return OC_STACK_NO_MEMORY;
463 memset(payload, 0, MAX_RESPONSE_LENGTH);
464 // Put the JSON prefix and suffix around the payload
465 strcpy(payload, (const char *)OC_JSON_PREFIX);
466 strcat(payload, (const char *)ehResponse->payload);
467 strcat(payload, (const char *)OC_JSON_SUFFIX);
468 responseInfo.info.payload = (CAPayload_t)payload;
470 CAResult_t caResult = CASendResponse(&responseEndpoint, &responseInfo);
471 if(caResult != CA_STATUS_OK)
473 OC_LOG(ERROR, TAG, PCF("CASendResponse error"));
477 result = OC_STACK_OK;
482 FindAndDeleteServerRequest(serverRequest);
485 OCStackResult result = OC_STACK_ERROR;
486 OCServerProtocolResponse protocolResponse;
487 memset(&protocolResponse, 0, sizeof(OCServerProtocolResponse));
489 OC_LOG_V(INFO, TAG, "Inside HandleSingleResponse: %s", ehResponse->payload);
491 OCServerRequest *serverRequest = (OCServerRequest *)ehResponse->requestHandle;
492 // Format protocol response structure with data needed for
493 // sending the response
494 protocolResponse.qos = serverRequest->qos;
496 if((OCResource *)ehResponse->resourceHandle &&
497 ((OCResource *)ehResponse->resourceHandle)->resourceProperties == (OCResourceProperty) 0)
499 ehResponse->ehResult = OC_EH_RESOURCE_DELETED;
501 protocolResponse.result = EntityHandlerCodeToOCStackCode(ehResponse->ehResult);
502 protocolResponse.requesterAddr = &serverRequest->requesterAddr;
503 protocolResponse.requestToken = &serverRequest->requestToken;
504 protocolResponse.numSendVendorSpecificHeaderOptions = ehResponse->numSendVendorSpecificHeaderOptions;
505 protocolResponse.sendVendorSpecificHeaderOptions = ehResponse->sendVendorSpecificHeaderOptions;
506 protocolResponse.resourceUri = ehResponse->resourceUri;
507 protocolResponse.delayedResNeeded = serverRequest->delayedResNeeded;
508 protocolResponse.secured = serverRequest->secured;
509 protocolResponse.slowFlag = serverRequest->slowFlag;
510 protocolResponse.notificationFlag = serverRequest->notificationFlag;
512 //should we put the prefix and suffix here?
513 protocolResponse.payload = (unsigned char *) OCMalloc(MAX_RESPONSE_LENGTH);
514 if(!protocolResponse.payload)
516 return OC_STACK_NO_MEMORY;
518 strcpy((char *)protocolResponse.payload, (const char *)OC_JSON_PREFIX);
519 strcat((char *)protocolResponse.payload, (const char *)ehResponse->payload);
520 strcat((char *)protocolResponse.payload, (const char *)OC_JSON_SUFFIX);
521 protocolResponse.payloadSize = strlen((const char *)protocolResponse.payload) + 1;
522 protocolResponse.resourceUri = ehResponse->resourceUri;
524 //revise the following
525 protocolResponse.coapID = serverRequest->coapID;
526 if(serverRequest->observeResult == OC_STACK_OK)
528 protocolResponse.observationOption = serverRequest->observationOption;
532 protocolResponse.observationOption = OC_OBSERVE_NO_OPTION;
534 // Make call to OCCoAP layer
535 result = OCDoCoAPResponse(&protocolResponse);
537 OCFree(protocolResponse.payload);
539 FindAndDeleteServerRequest(serverRequest);
544 OCStackResult HandleAggregateResponse(OCEntityHandlerResponse * ehResponse)
546 OCStackResult stackRet = OC_STACK_ERROR;
547 OCServerRequest * serverRequest = NULL;
548 OCServerResponse * serverResponse = NULL;
550 OC_LOG_V(INFO, TAG, "Inside HandleAggregateResponse: %s", ehResponse->payload);
552 serverRequest = GetServerRequestUsingHandle((OCServerRequest *)ehResponse->requestHandle);
553 serverResponse = GetServerResponseUsingHandle((OCServerRequest *)ehResponse->requestHandle);
559 OC_LOG(INFO, TAG, PCF("This is the first response fragment"));
560 stackRet = AddServerResponse(&serverResponse, ehResponse->requestHandle);
561 if (OC_STACK_OK != stackRet)
563 OC_LOG(ERROR, TAG, PCF("Error adding server response"));
566 VERIFY_NON_NULL(serverResponse);
567 VERIFY_NON_NULL(serverResponse->payload);
570 if((serverResponse->remainingPayloadSize >= ehResponse->payloadSize + 1 &&
571 serverRequest->numResponses == 1) ||
572 (serverResponse->remainingPayloadSize >= ehResponse->payloadSize + 2 &&
573 serverRequest->numResponses > 1))
575 OC_LOG(INFO, TAG, PCF("There is room in response buffer"));
577 sprintf((char *)serverResponse->payload, "%s%s", (char *)serverResponse->payload, (char *)ehResponse->payload);
578 OC_LOG_V(INFO, TAG, "Current aggregated response ...%s", serverResponse->payload);
579 serverResponse->remainingPayloadSize -= ehResponse->payloadSize;
580 (serverRequest->numResponses)--;
581 if(serverRequest->numResponses == 0)
583 OC_LOG(INFO, TAG, PCF("This is the last response fragment"));
584 ehResponse->payload = serverResponse->payload;
585 ehResponse->payloadSize = strlen((char *) serverResponse->payload) + 1;
586 stackRet = HandleSingleResponse(ehResponse);
587 //Delete the request and response
588 FindAndDeleteServerRequest(serverRequest);
589 FindAndDeleteServerResponse(serverResponse);
593 OC_LOG(INFO, TAG, PCF("More response fragment to come"));
594 // TODO: we should consider using strcat rather than setting a char by char here!
595 sprintf((char *)serverResponse->payload, "%s%c", (char *)serverResponse->payload,OC_JSON_SEPARATOR);
596 OC_LOG_V(INFO, TAG, "Current aggregated response ...%s", serverResponse->payload);
597 (serverResponse->remainingPayloadSize)--;
598 stackRet = OC_STACK_OK;
603 OC_LOG(INFO, TAG, PCF("No room in response buffer"));
604 //Delete the request and response
605 FindAndDeleteServerRequest(serverRequest);
606 FindAndDeleteServerResponse(serverResponse);
607 stackRet = OC_STACK_NO_MEMORY;