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, sizeof(serverRequest->token) - 1);
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;
368 switch (ehResponse->ehResult)
371 responseInfo.result = CA_SUCCESS;
374 responseInfo.result = CA_BAD_REQ;
376 case OC_EH_RESOURCE_CREATED:
377 responseInfo.result = CA_CREATED;
379 case OC_EH_RESOURCE_DELETED:
380 responseInfo.result = CA_DELETED;
383 responseInfo.result = CA_SUCCESS;
385 case OC_EH_FORBIDDEN:
386 responseInfo.result = CA_BAD_REQ;
389 responseInfo.result = CA_BAD_REQ;
393 // TODO-CA: Need to do something with a slow response if a confirmed request was sent
396 // TODO-CA: Need to handle CA_MSG_RESET and CA_MSG_ACKNOWLEDGE
397 switch (serverRequest->qos)
400 responseInfo.info.type = CA_MSG_NONCONFIRM;
403 responseInfo.info.type = CA_MSG_NONCONFIRM;
406 responseInfo.info.type = CA_MSG_CONFIRM;
409 responseInfo.info.type = CA_MSG_NONCONFIRM;
412 responseInfo.info.type = CA_MSG_NONCONFIRM;
416 responseInfo.info.token = serverRequest->token;
418 if(serverRequest->observeResult == OC_STACK_OK)
420 responseInfo.info.numOptions = ehResponse->numSendVendorSpecificHeaderOptions + 1;
424 responseInfo.info.numOptions = ehResponse->numSendVendorSpecificHeaderOptions;
427 responseInfo.info.options = (CAHeaderOption_t *)
428 malloc(sizeof(CAHeaderOption_t) * responseInfo.info.numOptions);
430 optionsPointer = responseInfo.info.options;
432 if(serverRequest->observeResult == OC_STACK_OK)
434 responseInfo.info.numOptions = ehResponse->numSendVendorSpecificHeaderOptions + 1;
437 // TODO-CA Revisit this logic
438 if(serverRequest->observeResult == OC_STACK_OK)
440 responseInfo.info.options[0].protocolID = CA_COAP_ID;
441 responseInfo.info.options[0].optionID = COAP_OPTION_OBSERVE;
442 // TODO-CA Remove the magic number 4
443 responseInfo.info.options[0].optionLength = 4;
444 memcpy(responseInfo.info.options[0].optionData, &(serverRequest->observationOption), 4);
446 // Point to the next header option before copying vender specific header options
450 if (ehResponse->numSendVendorSpecificHeaderOptions)
452 memcpy(optionsPointer, ehResponse->sendVendorSpecificHeaderOptions,
453 sizeof(OCHeaderOption) * ehResponse->numSendVendorSpecificHeaderOptions);
456 // Allocate memory for the payload.
457 char *payload = (char *)OCMalloc(MAX_RESPONSE_LENGTH);
460 return OC_STACK_NO_MEMORY;
462 memset(payload, 0, MAX_RESPONSE_LENGTH);
463 // Put the JSON prefix and suffix around the payload
464 strcpy(payload, (const char *)OC_JSON_PREFIX);
465 strcat(payload, (const char *)ehResponse->payload);
466 strcat(payload, (const char *)OC_JSON_SUFFIX);
467 responseInfo.info.payload = (CAPayload_t)payload;
469 CAResult_t caResult = CASendResponse(&responseEndpoint, &responseInfo);
470 if(caResult != CA_STATUS_OK)
472 OC_LOG(ERROR, TAG, PCF("CASendResponse error"));
476 result = OC_STACK_OK;
481 FindAndDeleteServerRequest(serverRequest);
484 OCStackResult result = OC_STACK_ERROR;
485 OCServerProtocolResponse protocolResponse;
486 memset(&protocolResponse, 0, sizeof(OCServerProtocolResponse));
488 OC_LOG_V(INFO, TAG, "Inside HandleSingleResponse: %s", ehResponse->payload);
490 OCServerRequest *serverRequest = (OCServerRequest *)ehResponse->requestHandle;
491 // Format protocol response structure with data needed for
492 // sending the response
493 protocolResponse.qos = serverRequest->qos;
495 if((OCResource *)ehResponse->resourceHandle &&
496 ((OCResource *)ehResponse->resourceHandle)->resourceProperties == (OCResourceProperty) 0)
498 ehResponse->ehResult = OC_EH_RESOURCE_DELETED;
500 protocolResponse.result = EntityHandlerCodeToOCStackCode(ehResponse->ehResult);
501 protocolResponse.requesterAddr = &serverRequest->requesterAddr;
502 protocolResponse.requestToken = &serverRequest->requestToken;
503 protocolResponse.numSendVendorSpecificHeaderOptions = ehResponse->numSendVendorSpecificHeaderOptions;
504 protocolResponse.sendVendorSpecificHeaderOptions = ehResponse->sendVendorSpecificHeaderOptions;
505 protocolResponse.resourceUri = ehResponse->resourceUri;
506 protocolResponse.delayedResNeeded = serverRequest->delayedResNeeded;
507 protocolResponse.secured = serverRequest->secured;
508 protocolResponse.slowFlag = serverRequest->slowFlag;
509 protocolResponse.notificationFlag = serverRequest->notificationFlag;
511 //should we put the prefix and suffix here?
512 protocolResponse.payload = (unsigned char *) OCMalloc(MAX_RESPONSE_LENGTH);
513 if(!protocolResponse.payload)
515 return OC_STACK_NO_MEMORY;
517 strcpy((char *)protocolResponse.payload, (const char *)OC_JSON_PREFIX);
518 strcat((char *)protocolResponse.payload, (const char *)ehResponse->payload);
519 strcat((char *)protocolResponse.payload, (const char *)OC_JSON_SUFFIX);
520 protocolResponse.payloadSize = strlen((const char *)protocolResponse.payload) + 1;
521 protocolResponse.resourceUri = ehResponse->resourceUri;
523 //revise the following
524 protocolResponse.coapID = serverRequest->coapID;
525 if(serverRequest->observeResult == OC_STACK_OK)
527 protocolResponse.observationOption = serverRequest->observationOption;
531 protocolResponse.observationOption = OC_OBSERVE_NO_OPTION;
533 // Make call to OCCoAP layer
534 result = OCDoCoAPResponse(&protocolResponse);
536 OCFree(protocolResponse.payload);
538 FindAndDeleteServerRequest(serverRequest);
543 OCStackResult HandleAggregateResponse(OCEntityHandlerResponse * ehResponse)
545 OCStackResult stackRet = OC_STACK_ERROR;
546 OCServerRequest * serverRequest = NULL;
547 OCServerResponse * serverResponse = NULL;
549 OC_LOG_V(INFO, TAG, "Inside HandleAggregateResponse: %s", ehResponse->payload);
551 serverRequest = GetServerRequestUsingHandle((OCServerRequest *)ehResponse->requestHandle);
552 serverResponse = GetServerResponseUsingHandle((OCServerRequest *)ehResponse->requestHandle);
558 OC_LOG(INFO, TAG, PCF("This is the first response fragment"));
559 stackRet = AddServerResponse(&serverResponse, ehResponse->requestHandle);
560 if (OC_STACK_OK != stackRet)
562 OC_LOG(ERROR, TAG, PCF("Error adding server response"));
565 VERIFY_NON_NULL(serverResponse);
566 VERIFY_NON_NULL(serverResponse->payload);
569 if((serverResponse->remainingPayloadSize >= ehResponse->payloadSize + 1 &&
570 serverRequest->numResponses == 1) ||
571 (serverResponse->remainingPayloadSize >= ehResponse->payloadSize + 2 &&
572 serverRequest->numResponses > 1))
574 OC_LOG(INFO, TAG, PCF("There is room in response buffer"));
576 sprintf((char *)serverResponse->payload, "%s%s", (char *)serverResponse->payload, (char *)ehResponse->payload);
577 OC_LOG_V(INFO, TAG, "Current aggregated response ...%s", serverResponse->payload);
578 serverResponse->remainingPayloadSize -= ehResponse->payloadSize;
579 (serverRequest->numResponses)--;
580 if(serverRequest->numResponses == 0)
582 OC_LOG(INFO, TAG, PCF("This is the last response fragment"));
583 ehResponse->payload = serverResponse->payload;
584 ehResponse->payloadSize = strlen((char *) serverResponse->payload) + 1;
585 stackRet = HandleSingleResponse(ehResponse);
586 //Delete the request and response
587 FindAndDeleteServerRequest(serverRequest);
588 FindAndDeleteServerResponse(serverResponse);
592 OC_LOG(INFO, TAG, PCF("More response fragment to come"));
593 // TODO: we should consider using strcat rather than setting a char by char here!
594 sprintf((char *)serverResponse->payload, "%s%c", (char *)serverResponse->payload,OC_JSON_SEPARATOR);
595 OC_LOG_V(INFO, TAG, "Current aggregated response ...%s", serverResponse->payload);
596 (serverResponse->remainingPayloadSize)--;
597 stackRet = OC_STACK_OK;
602 OC_LOG(INFO, TAG, PCF("No room in response buffer"));
603 //Delete the request and response
604 FindAndDeleteServerRequest(serverRequest);
605 FindAndDeleteServerResponse(serverResponse);
606 stackRet = OC_STACK_NO_MEMORY;