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)
45 OC_LOG(ERROR, TAG, PCF("Invalid Parameter Token"));
49 OCServerRequest * out = NULL;
50 LL_FOREACH (serverRequestList, out)
52 OC_LOG(INFO, TAG,PCF("comparing tokens"));
53 OC_LOG_BUFFER(INFO, TAG, (const uint8_t *)token, CA_MAX_TOKEN_LEN);
54 OC_LOG_BUFFER(INFO, TAG, (const uint8_t *)out->requestToken, CA_MAX_TOKEN_LEN);
55 if(memcmp(out->requestToken, token, CA_MAX_TOKEN_LEN) == 0)
60 OC_LOG(ERROR, TAG, PCF("Server Request not found!!"));
64 OCServerRequest * GetServerRequestUsingHandle (const OCServerRequest * handle)
66 OCServerRequest * out = NULL;
67 LL_FOREACH (serverRequestList, out)
74 OC_LOG(ERROR, TAG, PCF("Server Request not found!!"));
78 OCServerResponse * GetServerResponseUsingHandle (const OCServerRequest * handle)
80 OCServerResponse * out = NULL;
81 LL_FOREACH (serverResponseList, out)
83 if(out->requestHandle == handle)
88 OC_LOG(ERROR, TAG, PCF("Server Response not found!!"));
92 OCStackResult AddServerRequest (OCServerRequest ** request, uint16_t coapID,
93 uint8_t delayedResNeeded, uint8_t secured, uint8_t notificationFlag, OCMethod method,
94 uint8_t numRcvdVendorSpecificHeaderOptions, uint32_t observationOption,
95 OCQualityOfService qos, char * query,
96 OCHeaderOption * rcvdVendorSpecificHeaderOptions,
97 char * reqJSONPayload, CAToken_t * requestToken,
98 char * resourceUrl, size_t reqTotalSize,
99 CAAddress_t *addressInfo, CAConnectivityType_t connectivityType)
101 OCServerRequest * serverRequest = NULL;
103 //Note: OCServerRequest includes 1 byte for the JSON Payload. payloadSize is calculated
104 //as the required length of the string, so this will result in enough room for the
105 //null terminator as well.
106 serverRequest = (OCServerRequest *) OCCalloc(1, sizeof(OCServerRequest) +
107 (reqTotalSize ? reqTotalSize : 1) - 1);
108 VERIFY_NON_NULL(serverRequest);
110 serverRequest->coapID = coapID;
111 serverRequest->delayedResNeeded = delayedResNeeded;
112 serverRequest->secured = secured;
113 serverRequest->notificationFlag = notificationFlag;
115 serverRequest->method = method;
116 serverRequest->numRcvdVendorSpecificHeaderOptions = numRcvdVendorSpecificHeaderOptions;
117 serverRequest->observationOption = observationOption;
118 serverRequest->observeResult = OC_STACK_ERROR;
119 serverRequest->qos = qos;
120 serverRequest->ehResponseHandler = HandleSingleResponse;
121 serverRequest->numResponses = 1;
125 strncpy((char*)serverRequest->query,
126 (const char*)query, sizeof(serverRequest->query) - 1);
129 if(rcvdVendorSpecificHeaderOptions)
131 memcpy(serverRequest->rcvdVendorSpecificHeaderOptions, rcvdVendorSpecificHeaderOptions,
132 MAX_HEADER_OPTIONS * sizeof(OCHeaderOption));
134 if(reqJSONPayload && reqTotalSize)
136 // destination is at least 1 greater than the source, so a NULL always exists in the
138 strncpy((char*)serverRequest->reqJSONPayload,
139 (const char*)reqJSONPayload, reqTotalSize - 1);
141 serverRequest->requestComplete = 0;
144 serverRequest->requestToken = (CAToken_t)OCMalloc(CA_MAX_TOKEN_LEN+1);
145 VERIFY_NON_NULL (serverRequest->requestToken);
146 memcpy(serverRequest->requestToken, *requestToken, CA_MAX_TOKEN_LEN);
147 serverRequest->requestToken[CA_MAX_TOKEN_LEN]='\0';
152 strncpy((char*)serverRequest->resourceUrl,
153 (const char*)resourceUrl, sizeof(serverRequest->resourceUrl) - 1);
158 serverRequest->addressInfo = *addressInfo;
160 serverRequest->connectivityType = connectivityType;
162 *request = serverRequest;
163 OC_LOG(INFO, TAG, PCF("Server Request Added!!"));
164 LL_APPEND (serverRequestList, serverRequest);
170 OCFree(serverRequest);
171 serverRequest = NULL;
174 return OC_STACK_NO_MEMORY;
177 OCStackResult AddServerResponse (OCServerResponse ** response, OCRequestHandle requestHandle)
179 OCServerResponse * serverResponse = NULL;
181 serverResponse = (OCServerResponse *) OCCalloc(1, sizeof(OCServerResponse));
182 VERIFY_NON_NULL(serverResponse);
184 serverResponse->payload = (char *) OCCalloc(1, MAX_RESPONSE_LENGTH);
185 VERIFY_NON_NULL(serverResponse->payload);
187 serverResponse->remainingPayloadSize = MAX_RESPONSE_LENGTH;
188 serverResponse->requestHandle = requestHandle;
190 *response = serverResponse;
191 OC_LOG(INFO, TAG, PCF("Server Response Added!!"));
192 LL_APPEND (serverResponseList, serverResponse);
198 OCFree(serverResponse);
199 serverResponse = NULL;
202 return OC_STACK_NO_MEMORY;
205 // Form the OCEntityHandlerRequest struct
206 OCStackResult FormOCEntityHandlerRequest(
207 OCEntityHandlerRequest * entityHandlerRequest,
208 OCRequestHandle request,
210 OCResourceHandle resource,
212 char * bufReqPayload,
213 uint8_t numVendorOptions,
214 OCHeaderOption * vendorOptions,
215 OCObserveAction observeAction,
216 OCObservationId observeID)
218 if (entityHandlerRequest)
220 memset(entityHandlerRequest, 0, sizeof(OCEntityHandlerRequest));
221 entityHandlerRequest->requestHandle = request;
222 entityHandlerRequest->method = method;
223 entityHandlerRequest->resource = (OCResourceHandle) resource;
224 entityHandlerRequest->query = queryBuf;
225 entityHandlerRequest->reqJSONPayload = bufReqPayload;
226 entityHandlerRequest->numRcvdVendorSpecificHeaderOptions = numVendorOptions;
227 entityHandlerRequest->rcvdVendorSpecificHeaderOptions = vendorOptions;
229 entityHandlerRequest->obsInfo.action = observeAction;
230 entityHandlerRequest->obsInfo.obsId = observeID;
234 return OC_STACK_INVALID_PARAM;
237 void FindAndDeleteServerResponse(OCServerResponse * serverResponse)
239 OCServerResponse* tmp;
242 LL_FOREACH(serverResponseList, tmp)
244 if (serverResponse == tmp)
246 DeleteServerResponse(tmp);
253 void DeleteServerResponse(OCServerResponse * serverResponse)
257 LL_DELETE(serverResponseList, serverResponse);
258 OCFree(serverResponse->payload);
259 OCFree(serverResponse);
260 OC_LOG(INFO, TAG, PCF("Server Response Removed!!"));
264 void FindAndDeleteServerRequest(OCServerRequest * serverRequest)
266 OCServerRequest* tmp;
269 LL_FOREACH(serverRequestList, tmp)
271 if (serverRequest == tmp)
273 DeleteServerRequest(tmp);
280 void DeleteServerRequest(OCServerRequest * serverRequest)
284 LL_DELETE(serverRequestList, serverRequest);
285 OCFree(serverRequest);
286 serverRequest = NULL;
287 OC_LOG(INFO, TAG, PCF("Server Request Removed!!"));
291 OCStackResult HandleSingleResponse(OCEntityHandlerResponse * ehResponse)
293 OCStackResult result = OC_STACK_ERROR;
294 CARemoteEndpoint_t responseEndpoint = {};
295 CAResponseInfo_t responseInfo = {};
296 CAHeaderOption_t* optionsPointer = NULL;
300 OC_LOG(ERROR, TAG, PCF("HandleSingleResponse invalid parameters"));
301 return OC_STACK_INVALID_PARAM;
304 OC_LOG_V(INFO, TAG, "Inside HandleSingleResponse: %s", ehResponse->payload);
306 OCServerRequest *serverRequest = (OCServerRequest *)ehResponse->requestHandle;
309 responseEndpoint.resourceUri = (CAURI_t) serverRequest->resourceUrl;
310 responseEndpoint.addressInfo = serverRequest->addressInfo;
311 responseEndpoint.connectivityType = serverRequest->connectivityType;
312 responseEndpoint.isSecured = serverRequest->secured;
314 switch (ehResponse->ehResult)
317 responseInfo.result = CA_SUCCESS;
320 responseInfo.result = CA_BAD_REQ;
322 case OC_EH_RESOURCE_CREATED:
323 responseInfo.result = CA_CREATED;
325 case OC_EH_RESOURCE_DELETED:
326 responseInfo.result = CA_DELETED;
329 responseInfo.result = CA_SUCCESS;
331 case OC_EH_FORBIDDEN:
332 responseInfo.result = CA_BAD_REQ;
335 responseInfo.result = CA_BAD_REQ;
338 responseInfo.info.type = qualityOfServiceToMessageType(serverRequest->qos);
339 char token[CA_MAX_TOKEN_LEN + 1] = {};
340 responseInfo.info.token = token;
341 memcpy(responseInfo.info.token, serverRequest->requestToken, CA_MAX_TOKEN_LEN);
343 if(serverRequest->observeResult == OC_STACK_OK)
345 responseInfo.info.numOptions = ehResponse->numSendVendorSpecificHeaderOptions + 1;
349 responseInfo.info.numOptions = ehResponse->numSendVendorSpecificHeaderOptions;
352 if(responseInfo.info.numOptions > 0)
354 responseInfo.info.options = (CAHeaderOption_t *)
355 OCCalloc(responseInfo.info.numOptions,
356 sizeof(CAHeaderOption_t));
358 if(!responseInfo.info.options)
360 OC_LOG(FATAL, TAG, PCF("options is NULL"));
361 return OC_STACK_NO_MEMORY;
364 optionsPointer = responseInfo.info.options;
366 // TODO: This exposes CoAP specific details. At some point, this should be
367 // re-factored and handled in the CA layer.
368 if(serverRequest->observeResult == OC_STACK_OK)
370 responseInfo.info.options[0].protocolID = CA_COAP_ID;
371 responseInfo.info.options[0].optionID = COAP_OPTION_OBSERVE;
372 responseInfo.info.options[0].optionLength = sizeof(uint32_t);
373 memcpy(responseInfo.info.options[0].optionData,
374 &(serverRequest->observationOption), sizeof(uint32_t));
376 // Point to the next header option before copying vender specific header options
380 if (ehResponse->numSendVendorSpecificHeaderOptions)
382 memcpy(optionsPointer, ehResponse->sendVendorSpecificHeaderOptions,
383 sizeof(OCHeaderOption) *
384 ehResponse->numSendVendorSpecificHeaderOptions);
389 responseInfo.info.options = NULL;
392 char payload[MAX_RESPONSE_LENGTH] = {};
394 // Put the JSON prefix and suffix around the payload
395 strcpy(payload, (const char *)OC_JSON_PREFIX);
396 strncat(payload, (const char *)ehResponse->payload, ehResponse->payloadSize);
397 strcat(payload, (const char *)OC_JSON_SUFFIX);
398 responseInfo.info.payload = (CAPayload_t)payload;
401 //TODO: Add other connectivity types to CAConnTypes[] when enabled
402 CAConnectivityType_t CAConnTypes[] = {CA_ETHERNET, CA_WIFI};
403 const char * connTypes[] = {"ethernet", "wifi"};
404 int size = sizeof(CAConnTypes)/ sizeof(CAConnectivityType_t);
405 CAConnectivityType_t connType = responseEndpoint.connectivityType;
406 CAResult_t caResult = CA_STATUS_FAILED;
407 result = OC_STACK_OK;
409 //Sending response on all n/w interfaces
410 for(int i = 0; i < size; i++ )
412 responseEndpoint.connectivityType = (CAConnectivityType_t)(connType & CAConnTypes[i]);
413 if(responseEndpoint.connectivityType)
415 //The result is set to OC_STACK_OK only if CASendResponse succeeds in sending the
416 //response on all the n/w interfaces else it is set to OC_STACK_ERROR
417 caResult = CASendResponse(&responseEndpoint, &responseInfo);
418 if(caResult != CA_STATUS_OK)
420 OC_LOG_V(ERROR, TAG, "CASendResponse failed on %s", connTypes[i]);
421 result = CAResultToOCResult(caResult);
425 OC_LOG_V(INFO, TAG, "CASendResponse succeeded on %s", connTypes[i]);
430 CAResult_t caResult = CASendResponse(&responseEndpoint, &responseInfo);
431 if(caResult != CA_STATUS_OK)
433 OC_LOG(ERROR, TAG, PCF("CASendResponse failed"));
434 result = CAResultToOCResult(caResult);
438 result = OC_STACK_OK;
442 OCFree(responseInfo.info.options);
444 FindAndDeleteServerRequest(serverRequest);
448 OCStackResult HandleAggregateResponse(OCEntityHandlerResponse * ehResponse)
450 OCStackResult stackRet = OC_STACK_ERROR;
451 OCServerRequest * serverRequest = NULL;
452 OCServerResponse * serverResponse = NULL;
453 uint16_t bufferNeeded = 0;
455 if(!ehResponse || !ehResponse->payload)
457 OC_LOG(ERROR, TAG, PCF("HandleAggregateResponse invalid parameters"));
458 return OC_STACK_INVALID_PARAM;
461 OC_LOG_V(INFO, TAG, "Inside HandleAggregateResponse: %s", ehResponse->payload);
463 serverRequest = GetServerRequestUsingHandle((OCServerRequest *)ehResponse->requestHandle);
464 serverResponse = GetServerResponseUsingHandle((OCServerRequest *)ehResponse->requestHandle);
470 OC_LOG(INFO, TAG, PCF("This is the first response fragment"));
471 stackRet = AddServerResponse(&serverResponse, ehResponse->requestHandle);
472 if (OC_STACK_OK != stackRet)
474 OC_LOG(ERROR, TAG, PCF("Error adding server response"));
477 VERIFY_NON_NULL(serverResponse);
478 VERIFY_NON_NULL(serverResponse->payload);
481 // If there is more than 1 response, then we need to allow for a null-termination
482 // in the server response payload buffer AND the JSON response separator
483 bufferNeeded = ehResponse->payloadSize + 1;
484 if (serverRequest->numResponses > 1)
486 bufferNeeded += strlen(OC_JSON_SEPARATOR_STR);
488 if(serverResponse->remainingPayloadSize >= bufferNeeded)
490 OC_LOG(ERROR, TAG, PCF("There is room in response buffer"));
492 strncat((char *)serverResponse->payload,
493 (char *)ehResponse->payload,
494 serverResponse->remainingPayloadSize);
495 OC_LOG_V(INFO, TAG, "Current aggregated response ...%s", serverResponse->payload);
496 serverResponse->remainingPayloadSize -= strlen((char *)ehResponse->payload);
497 (serverRequest->numResponses)--;
498 if(serverRequest->numResponses == 0)
500 OC_LOG(INFO, TAG, PCF("This is the last response fragment"));
501 ehResponse->payload = serverResponse->payload;
502 ehResponse->payloadSize = strlen((char *) serverResponse->payload) + 1;
503 stackRet = HandleSingleResponse(ehResponse);
504 //Delete the request and response
505 FindAndDeleteServerRequest(serverRequest);
506 FindAndDeleteServerResponse(serverResponse);
510 OC_LOG(INFO, TAG, PCF("More response fragments to come"));
511 strncat((char *)serverResponse->payload,
512 OC_JSON_SEPARATOR_STR,
513 serverResponse->remainingPayloadSize);
514 OC_LOG_V(INFO, TAG, "Current aggregated response ...%s", serverResponse->payload);
515 serverResponse->remainingPayloadSize -= strlen(OC_JSON_SEPARATOR_STR);
516 stackRet = OC_STACK_OK;
521 OC_LOG(ERROR, TAG, PCF("No room in response buffer"));
522 //Delete the request and response
523 FindAndDeleteServerRequest(serverRequest);
524 FindAndDeleteServerResponse(serverResponse);
525 stackRet = OC_STACK_NO_MEMORY;