1 /******************************************************************
3 * Copyright 2014 Samsung Electronics All Rights Reserved.
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 ******************************************************************/
26 #include "cainterface.h"
27 #include "camessagehandler_singlethread.h"
28 #include "caremotehandler.h"
29 #include "cainterfacecontroller_singlethread.h"
30 #include "caprotocolmessage.h"
31 #include "caretransmission_singlethread.h"
33 #include "config.h" /* for coap protocol */
34 #include "oic_malloc.h"
38 #define CA_MAX_RT_ARRAY_SIZE 3
42 SEND_TYPE_MULTICAST = 0, SEND_TYPE_UNICAST
47 CASendDataType_t type;
48 CARemoteEndpoint_t *remoteEndpoint;
49 CARequestInfo_t *requestInfo;
50 CAResponseInfo_t *responseInfo;
51 CAHeaderOption_t *options;
56 static CARetransmission_t g_retransmissionContext;
59 static CARequestCallback g_requestHandler = NULL;
60 static CAResponseCallback g_responseHandler = NULL;
62 static void CATimeoutCallback(const CARemoteEndpoint_t *endpoint, const void *pdu, uint32_t size)
64 OIC_LOG(DEBUG, TAG, "IN");
65 CARemoteEndpoint_t* ep = CACloneRemoteEndpoint(endpoint);
68 OIC_LOG(ERROR, TAG, "clone failed");
72 CAResponseInfo_t* resInfo = (CAResponseInfo_t*) OICCalloc(1, sizeof(CAResponseInfo_t));
76 OIC_LOG(ERROR, TAG, "calloc failed");
77 CADestroyRemoteEndpointInternal(ep);
81 resInfo->result = CA_RETRANSMIT_TIMEOUT;
82 resInfo->info.type = CAGetMessageTypeFromPduBinaryData(pdu, size);
83 resInfo->info.messageId = CAGetMessageIdFromPduBinaryData(pdu, size);
85 if (g_responseHandler)
87 g_responseHandler(ep, resInfo);
90 CADestroyRemoteEndpointInternal(ep);
93 OIC_LOG(DEBUG, TAG, "OUT");
95 static void CAProcessData(const CAData_t *data)
97 OIC_LOG(DEBUG, TAG, "IN");
98 VERIFY_NON_NULL_VOID(data, TAG, "data");
99 VERIFY_NON_NULL_VOID(data->remoteEndpoint, TAG, "remoteEndpoint");
101 CAResult_t res = CA_STATUS_FAILED;
103 CASendDataType_t type = data->type;
105 if (SEND_TYPE_UNICAST == type)
107 coap_pdu_t *pdu = NULL;
109 if (NULL != data->requestInfo)
111 OIC_LOG(DEBUG, TAG, "reqInfo avlbl");
113 pdu = (coap_pdu_t *) CAGeneratePDU(data->remoteEndpoint->resourceUri,
114 data->requestInfo->method,
115 data->requestInfo->info);
117 else if (NULL != data->responseInfo)
119 OIC_LOG(DEBUG, TAG, "resInfo avlbl");
121 pdu = (coap_pdu_t *) CAGeneratePDU(data->remoteEndpoint->resourceUri,
122 data->responseInfo->result,
123 data->responseInfo->info);
127 OIC_LOG(DEBUG, TAG, "request info, response info is empty");
130 // interface controller function call.
135 res = CASendUnicastData(data->remoteEndpoint, pdu->hdr, pdu->length);
136 if (CA_STATUS_OK != res)
138 OIC_LOG_V(ERROR, TAG, "send failed:%d", res);
139 coap_delete_pdu(pdu);
142 // for retransmission
143 res = CARetransmissionSentData(&g_retransmissionContext, data->remoteEndpoint, pdu->hdr,
145 if (CA_STATUS_OK != res)
147 OIC_LOG_V(INFO, TAG, "retransmissions will not be working: %d", res);
148 coap_delete_pdu(pdu);
152 coap_delete_pdu(pdu);
155 else if (SEND_TYPE_MULTICAST == type)
157 OIC_LOG(DEBUG, TAG, "both requestInfo & responseInfo is not available");
159 CAInfo_t info = data->requestInfo->info;
161 info.options = data->options;
162 info.numOptions = data->numOptions;
164 coap_pdu_t *pdu = (coap_pdu_t *) CAGeneratePDU(data->remoteEndpoint->resourceUri, CA_GET,
170 res = CASendMulticastData(pdu->hdr, pdu->length);
171 if(CA_STATUS_OK != res)
173 OIC_LOG_V(ERROR, TAG, "send failed:%d", res);
174 coap_delete_pdu(pdu);
177 coap_delete_pdu(pdu);
181 OIC_LOG(DEBUG, TAG, "OUT");
184 static void CAReceivedPacketCallback(CARemoteEndpoint_t *endpoint, void *data, uint32_t dataLen)
186 OIC_LOG(DEBUG, TAG, "IN");
187 VERIFY_NON_NULL_VOID(data, TAG, "data");
189 uint32_t code = CA_NOT_FOUND;
190 coap_pdu_t *pdu = (coap_pdu_t *) CAParsePDU((const char *) data, dataLen, &code);
194 OIC_LOG(ERROR, TAG, "Parse PDU failed");
198 char uri[CA_MAX_URI_LENGTH] = { 0, };
199 uint32_t bufLen = sizeof(uri);
201 if (CA_GET == code || CA_POST == code || CA_PUT == code || CA_DELETE == code)
203 CARequestInfo_t *ReqInfo = (CARequestInfo_t *) OICCalloc(1, sizeof(CARequestInfo_t));
206 OIC_LOG(ERROR, TAG, "CAReceivedPacketCallback, Memory allocation failed!");
207 coap_delete_pdu(pdu);
211 CAResult_t res = CAGetRequestInfoFromPDU(pdu, ReqInfo, uri, bufLen);
212 if (CA_STATUS_OK != res)
214 OIC_LOG_V(ERROR, TAG, "CAGetRequestInfoFromPDU failed : %d", res);
216 coap_delete_pdu(pdu);
220 if (NULL != ReqInfo->info.options)
222 for (uint32_t i = 0; i < ReqInfo->info.numOptions; i++)
224 OIC_LOG_V(DEBUG, TAG, "optionID: %d", ReqInfo->info.options[i].optionID);
226 OIC_LOG_V(DEBUG, TAG, "list: %s", ReqInfo->info.options[i].optionData);
230 if (NULL != ReqInfo->info.payload)
232 OIC_LOG_V(DEBUG, TAG, "Request- payload: %s", ReqInfo->info.payload);
235 OIC_LOG_V(DEBUG, TAG, "code: %d", ReqInfo->method);
236 OIC_LOG(DEBUG, TAG, "token:");
237 OIC_LOG_BUFFER(DEBUG, TAG, (const uint8_t *) ReqInfo->info.token, CA_MAX_TOKEN_LEN);
238 if (NULL != endpoint)
240 endpoint->resourceUri = (char *) OICMalloc(bufLen + 1);
241 if (NULL == endpoint->resourceUri)
243 OIC_LOG(ERROR, TAG, "CAReceivedPacketCallback, Memory allocation failed!");
245 coap_delete_pdu(pdu);
248 memcpy(endpoint->resourceUri, uri, bufLen);
249 endpoint->resourceUri[bufLen] = '\0';
250 OIC_LOG_V(DEBUG, TAG, "URI : %s", endpoint->resourceUri);
255 if (g_requestHandler)
257 g_requestHandler(endpoint, ReqInfo);
260 CADestroyRequestInfoInternal(ReqInfo);
265 CAResponseInfo_t *ResInfo = (CAResponseInfo_t *) OICCalloc(1, sizeof(CAResponseInfo_t));
268 OIC_LOG(ERROR, TAG, "CAReceivedPacketCallback, Memory allocation failed!");
269 coap_delete_pdu(pdu);
273 CAResult_t res = CAGetResponseInfoFromPDU(pdu, ResInfo, uri, bufLen);
274 if (CA_STATUS_OK != res)
276 OIC_LOG_V(ERROR, TAG, "CAGetResponseInfoFromPDU failed : %d", res);
278 coap_delete_pdu(pdu);
282 if (NULL != ResInfo->info.options)
284 for (uint32_t i = 0; i < ResInfo->info.numOptions; i++)
286 OIC_LOG_V(DEBUG, TAG, "optionID: %d", ResInfo->info.options[i].optionID);
288 OIC_LOG_V(DEBUG, TAG, "list: %s", ResInfo->info.options[i].optionData);
292 if (NULL != ResInfo->info.payload)
294 OIC_LOG_V(DEBUG, TAG, "payload: %s", ResInfo->info.payload);
296 OIC_LOG_V(DEBUG, TAG, "code: %d", ResInfo->result);
298 if (NULL != endpoint)
300 endpoint->resourceUri = (char *) OICMalloc(bufLen + 1);
301 if (NULL == endpoint->resourceUri)
303 OIC_LOG(ERROR, TAG, "CAReceivedPacketCallback, Memory allocation failed !");
305 coap_delete_pdu(pdu);
308 memcpy(endpoint->resourceUri, uri, bufLen);
309 endpoint->resourceUri[bufLen] = '\0';
310 OIC_LOG_V(DEBUG, TAG, "URI : %s", endpoint->resourceUri);
313 // for retransmission
314 void *retransmissionPdu = NULL;
315 CARetransmissionReceivedData(&g_retransmissionContext, endpoint, pdu->hdr, pdu->length,
318 // get token from saved data in retransmission list
319 if (retransmissionPdu && CA_EMPTY == code)
321 CAResult_t res = CAGetTokenFromPDU((const coap_hdr_t *)retransmissionPdu,
323 if(CA_STATUS_OK != res)
325 OIC_LOG(ERROR, TAG, "fail to get Token from retransmission list");
326 OICFree(ResInfo->info.token);
329 OICFree(retransmissionPdu);
333 if (g_responseHandler)
335 g_responseHandler(endpoint, ResInfo);
337 CADestroyResponseInfoInternal(ResInfo);
341 if (endpoint && endpoint->resourceUri)
343 OICFree(endpoint->resourceUri);
344 endpoint->resourceUri = NULL;
348 coap_delete_pdu(pdu);
350 OIC_LOG(DEBUG, TAG, "OUT");
353 static void CANetworkChangedCallback(CALocalConnectivity_t *info, CANetworkStatus_t status)
355 OIC_LOG(DEBUG, TAG, "IN");
357 OIC_LOG(DEBUG, TAG, "OUT");
360 void CAHandleRequestResponseCallbacks()
363 CARetransmissionBaseRoutine((void *)&g_retransmissionContext);
366 CAResult_t CADetachRequestMessage(const CARemoteEndpoint_t *object, const CARequestInfo_t *request)
368 OIC_LOG(DEBUG, TAG, "IN");
370 VERIFY_NON_NULL(object, TAG, "object");
371 VERIFY_NON_NULL(request, TAG, "request");
373 // If max retransmission queue is reached, then don't handle new request
374 if (CA_MAX_RT_ARRAY_SIZE == u_arraylist_length(g_retransmissionContext.dataList))
376 OIC_LOG(ERROR, TAG, "max RT queue size reached!");
377 return CA_SEND_FAILED;
380 // allocate & initialize
381 CAData_t *data = (CAData_t *) OICCalloc(1, sizeof(CAData_t));
382 CA_MEMORY_ALLOC_CHECK(data);
385 data->type = SEND_TYPE_UNICAST;
386 data->remoteEndpoint = object;
387 data->requestInfo = request;
388 data->responseInfo = NULL;
392 OIC_LOG(DEBUG, TAG, "OUT");
395 // memory error label.
398 OIC_LOG(DEBUG, TAG, "OUT");
399 return CA_MEMORY_ALLOC_FAILED;
402 CAResult_t CADetachRequestToAllMessage(const CAGroupEndpoint_t *object,
403 const CARequestInfo_t *request)
405 OIC_LOG(DEBUG, TAG, "IN");
407 if (NULL == object || NULL == request || NULL == object->resourceUri)
409 return CA_STATUS_INVALID_PARAM;
412 if ((request->method < CA_GET) || (request->method > CA_DELETE))
414 OIC_LOG(ERROR, TAG, "Invalid method type!");
416 return CA_STATUS_INVALID_PARAM;
419 // allocate & initialize
420 CAData_t *data = (CAData_t *) OICCalloc(1, sizeof(CAData_t));
421 CA_MEMORY_ALLOC_CHECK(data);
423 CAAddress_t addr = {0};
424 CARemoteEndpoint_t *remoteEndpoint = CACreateRemoteEndpointInternal(object->resourceUri, addr,
425 object->transportType);
428 data->type = SEND_TYPE_MULTICAST;
429 data->remoteEndpoint = remoteEndpoint;
430 data->requestInfo = request;
431 data->responseInfo = NULL;
434 CADestroyRemoteEndpointInternal(remoteEndpoint);
437 OIC_LOG(DEBUG, TAG, "OUT");
440 // memory error label.
444 OIC_LOG(DEBUG, TAG, "OUT");
445 return CA_MEMORY_ALLOC_FAILED;
448 CAResult_t CADetachResponseMessage(const CARemoteEndpoint_t *object,
449 const CAResponseInfo_t *response)
451 OIC_LOG(DEBUG, TAG, "IN");
452 VERIFY_NON_NULL(object, TAG, "object");
453 VERIFY_NON_NULL(response, TAG, "response");
455 // allocate & initialize
456 CAData_t *data = (CAData_t *) OICCalloc(1, sizeof(CAData_t));
457 CA_MEMORY_ALLOC_CHECK(data);
460 data->type = SEND_TYPE_UNICAST;
461 data->remoteEndpoint = object;
462 data->requestInfo = NULL;
463 data->responseInfo = response;
468 OIC_LOG(DEBUG, TAG, "OUT");
471 // memory error label.
474 OIC_LOG(DEBUG, TAG, "OUT");
476 return CA_MEMORY_ALLOC_FAILED;
479 CAResult_t CADetachMessageResourceUri(const CAURI_t resourceUri, const CAToken_t token,
480 uint8_t tokenLength, const CAHeaderOption_t *options,
483 OIC_LOG(DEBUG, TAG, "IN");
484 VERIFY_NON_NULL(resourceUri, TAG, "resourceUri is NULL");
485 VERIFY_NON_NULL(token, TAG, "Token is NULL");
487 // allocate & initialize
488 CAData_t *data = (CAData_t *) OICCalloc(1, sizeof(CAData_t));
489 CA_MEMORY_ALLOC_CHECK(data);
491 CAAddress_t addr = {0};
492 CARemoteEndpoint_t *remoteEndpoint =
493 CACreateRemoteEndpointInternal(resourceUri, addr, CA_IPV4 | CA_EDR | CA_LE);
495 // create request info
496 CARequestInfo_t *reqInfo = (CARequestInfo_t *) OICCalloc(1, sizeof(CARequestInfo_t));
497 CA_MEMORY_ALLOC_CHECK(reqInfo);
499 // save request info data
500 reqInfo->method = CA_GET;
501 reqInfo->info.type = CA_MSG_NONCONFIRM;
503 reqInfo->info.token = token;
504 reqInfo->info.tokenLength = tokenLength;
507 data->type = SEND_TYPE_MULTICAST;
508 data->remoteEndpoint = remoteEndpoint;
509 data->requestInfo = reqInfo;
511 data->responseInfo = NULL;
512 data->options = NULL;
513 data->numOptions = 0;
514 CAHeaderOption_t *headerOption = NULL;
515 if (NULL != options && numOptions > 0)
518 headerOption = (CAHeaderOption_t *) OICMalloc(sizeof(CAHeaderOption_t) * numOptions);
519 CA_MEMORY_ALLOC_CHECK(headerOption);
520 memcpy(headerOption, options, sizeof(CAHeaderOption_t) * numOptions);
522 data->options = headerOption;
523 data->numOptions = numOptions;
528 CADestroyRemoteEndpoint(remoteEndpoint);
529 OICFree(headerOption);
532 OIC_LOG(DEBUG, TAG, "OUT");
535 // memory error label.
538 CADestroyRemoteEndpointInternal(remoteEndpoint);
542 OIC_LOG(DEBUG, TAG, "OUT");
543 return CA_MEMORY_ALLOC_FAILED;
546 void CASetRequestResponseCallbacks(CARequestCallback ReqHandler, CAResponseCallback RespHandler)
548 OIC_LOG(DEBUG, TAG, "IN");
549 g_requestHandler = ReqHandler;
550 g_responseHandler = RespHandler;
551 OIC_LOG(DEBUG, TAG, "OUT");
554 CAResult_t CAInitializeMessageHandler()
556 OIC_LOG(DEBUG, TAG, "IN");
557 CASetPacketReceivedCallback(CAReceivedPacketCallback);
559 CASetNetworkChangeCallback(CANetworkChangedCallback);
561 // retransmission initialize
562 CARetransmissionInitialize(&g_retransmissionContext, CASendUnicastData,
563 CATimeoutCallback, NULL);
565 CAInitializeAdapters();
566 OIC_LOG(DEBUG, TAG, "OUT");
570 void CATerminateMessageHandler()
572 OIC_LOG(DEBUG, TAG, "IN");
573 // terminate interface adapters by controller
574 CATerminateAdapters();
576 // stop retransmission
577 CARetransmissionStop(&g_retransmissionContext);
578 CARetransmissionDestroy(&g_retransmissionContext);
580 OIC_LOG(DEBUG, TAG, "OUT");
583 void CALogPDUInfo(coap_pdu_t *pdu)
585 VERIFY_NON_NULL_VOID(pdu, TAG, "pdu");
587 OIC_LOG_V(DEBUG, TAG, "PDU Maker - payload : %s", pdu->data);
589 OIC_LOG_V(DEBUG, TAG, "PDU Maker - type : %d", pdu->hdr->type);
591 OIC_LOG_V(DEBUG, TAG, "PDU Maker - code : %d", pdu->hdr->code);
593 OIC_LOG_V(DEBUG, TAG, "PDU Maker - id : %d", ntohs(pdu->hdr->id));
595 OIC_LOG(DEBUG, TAG, "PDU Maker - token :");
597 OIC_LOG_BUFFER(DEBUG, TAG, pdu->hdr->token, pdu->hdr->token_length);