OCStackResult result = OC_STACK_ERROR;
CARemoteEndpoint_t responseEndpoint = {};
CAResponseInfo_t responseInfo = {};
- CAHeaderOption_t* optionsPointer;
+ CAHeaderOption_t* optionsPointer = NULL;
OC_LOG_V(INFO, TAG, "Inside HandleSingleResponse: %s", ehResponse->payload);
break;
}
responseInfo.info.type = qualityOfServiceToMessageType(serverRequest->qos);
- responseInfo.info.token = (CAToken_t) OCCalloc(1, CA_MAX_TOKEN_LEN + 1);
- if (!responseInfo.info.token)
- {
- OC_LOG(FATAL, TAG, "Response Info Token is NULL");
- return result;
- }
+ char token[CA_MAX_TOKEN_LEN + 1] = {};
+ responseInfo.info.token = token;
memcpy(responseInfo.info.token, serverRequest->requestToken, CA_MAX_TOKEN_LEN);
if(serverRequest->observeResult == OC_STACK_OK)
responseInfo.info.numOptions = ehResponse->numSendVendorSpecificHeaderOptions;
}
- responseInfo.info.options = (CAHeaderOption_t *)
- OCMalloc(sizeof(CAHeaderOption_t) * responseInfo.info.numOptions);
+ if(responseInfo.info.numOptions > 0)
+ {
+ responseInfo.info.options = (CAHeaderOption_t *)
+ OCCalloc(responseInfo.info.numOptions,
+ sizeof(CAHeaderOption_t));
- optionsPointer = responseInfo.info.options;
+ if(!responseInfo.info.options)
+ {
+ OC_LOG(FATAL, TAG, PCF("options is NULL"));
+ return OC_STACK_NO_MEMORY;
+ }
- // TODO: This exposes CoAP specific details. At some point, this should be
- // re-factored and handled in the CA layer.
- if(serverRequest->observeResult == OC_STACK_OK)
- {
- responseInfo.info.options[0].protocolID = CA_COAP_ID;
- responseInfo.info.options[0].optionID = COAP_OPTION_OBSERVE;
- responseInfo.info.options[0].optionLength = sizeof(uint32_t);
- memcpy(responseInfo.info.options[0].optionData,
- &(serverRequest->observationOption), sizeof(uint32_t));
-
- // Point to the next header option before copying vender specific header options
- optionsPointer += 1;
- }
+ optionsPointer = responseInfo.info.options;
- if (ehResponse->numSendVendorSpecificHeaderOptions)
- {
- memcpy(optionsPointer, ehResponse->sendVendorSpecificHeaderOptions,
- sizeof(OCHeaderOption) * ehResponse->numSendVendorSpecificHeaderOptions);
- }
+ // TODO: This exposes CoAP specific details. At some point, this should be
+ // re-factored and handled in the CA layer.
+ if(serverRequest->observeResult == OC_STACK_OK)
+ {
+ responseInfo.info.options[0].protocolID = CA_COAP_ID;
+ responseInfo.info.options[0].optionID = COAP_OPTION_OBSERVE;
+ responseInfo.info.options[0].optionLength = sizeof(uint32_t);
+ memcpy(responseInfo.info.options[0].optionData,
+ &(serverRequest->observationOption), sizeof(uint32_t));
+
+ // Point to the next header option before copying vender specific header options
+ optionsPointer += 1;
+ }
- // Allocate memory for the payload.
- char *payload = (char *)OCCalloc(1, MAX_RESPONSE_LENGTH);
- if(!payload)
+ if (ehResponse->numSendVendorSpecificHeaderOptions)
+ {
+ memcpy(optionsPointer, ehResponse->sendVendorSpecificHeaderOptions,
+ sizeof(OCHeaderOption) *
+ ehResponse->numSendVendorSpecificHeaderOptions);
+ }
+ }
+ else
{
- return OC_STACK_NO_MEMORY;
+ responseInfo.info.options = NULL;
}
+ char payload[MAX_RESPONSE_LENGTH] = {};
+
// Put the JSON prefix and suffix around the payload
strcpy(payload, (const char *)OC_JSON_PREFIX);
strcat(payload, (const char *)ehResponse->payload);
if(caResult != CA_STATUS_OK)
{
OC_LOG_V(ERROR, TAG, "CASendResponse failed on %s", connTypes[i]);
- result = OC_STACK_ERROR;
+ result = CAResultToOCResult(caResult);
}
else
{
if(caResult != CA_STATUS_OK)
{
OC_LOG(ERROR, TAG, PCF("CASendResponse failed"));
+ result = CAResultToOCResult(caResult);
}
else
{
}
#endif
- OCFree(payload);
+ OCFree(responseInfo.info.options);
//Delete the request
FindAndDeleteServerRequest(serverRequest);
return result;