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 = { 0 };
161 info.options = data->options;
162 info.numOptions = data->numOptions;
163 info.token = data->requestInfo->info.token;
164 info.tokenLength = data->requestInfo->info.tokenLength;
165 info.type = data->requestInfo->info.type;
166 info.messageId = data->requestInfo->info.messageId;
167 info.payload = data->requestInfo->info.payload;
169 coap_pdu_t *pdu = (coap_pdu_t *) CAGeneratePDU(data->remoteEndpoint->resourceUri, CA_GET,
175 res = CASendMulticastData(pdu->hdr, pdu->length);
176 if(CA_STATUS_OK != res)
178 OIC_LOG_V(ERROR, TAG, "send failed:%d", res);
179 coap_delete_pdu(pdu);
182 coap_delete_pdu(pdu);
186 OIC_LOG(DEBUG, TAG, "OUT");
189 static void CAReceivedPacketCallback(CARemoteEndpoint_t *endpoint, void *data, uint32_t dataLen)
191 OIC_LOG(DEBUG, TAG, "IN");
192 VERIFY_NON_NULL_VOID(data, TAG, "data");
194 uint32_t code = CA_NOT_FOUND;
195 coap_pdu_t *pdu = (coap_pdu_t *) CAParsePDU((const char *) data, dataLen, &code);
199 OIC_LOG(ERROR, TAG, "Parse PDU failed");
203 char uri[CA_MAX_URI_LENGTH] = { 0, };
204 uint32_t bufLen = sizeof(uri);
206 if (CA_GET == code || CA_POST == code || CA_PUT == code || CA_DELETE == code)
208 CARequestInfo_t *ReqInfo = (CARequestInfo_t *) OICCalloc(1, sizeof(CARequestInfo_t));
211 OIC_LOG(ERROR, TAG, "CAReceivedPacketCallback, Memory allocation failed!");
212 coap_delete_pdu(pdu);
216 CAResult_t res = CAGetRequestInfoFromPDU(pdu, ReqInfo, uri, bufLen);
217 if (CA_STATUS_OK != res)
219 OIC_LOG_V(ERROR, TAG, "CAGetRequestInfoFromPDU failed : %d", res);
221 coap_delete_pdu(pdu);
225 if (NULL != ReqInfo->info.options)
227 for (uint32_t i = 0; i < ReqInfo->info.numOptions; i++)
229 OIC_LOG_V(DEBUG, TAG, "optionID: %d", ReqInfo->info.options[i].optionID);
231 OIC_LOG_V(DEBUG, TAG, "list: %s", ReqInfo->info.options[i].optionData);
235 if (NULL != ReqInfo->info.payload)
237 OIC_LOG_V(DEBUG, TAG, "Request- payload: %s", ReqInfo->info.payload);
240 OIC_LOG_V(DEBUG, TAG, "code: %d", ReqInfo->method);
241 OIC_LOG(DEBUG, TAG, "token:");
242 OIC_LOG_BUFFER(DEBUG, TAG, (const uint8_t *) ReqInfo->info.token, CA_MAX_TOKEN_LEN);
243 if (NULL != endpoint)
245 endpoint->resourceUri = (char *) OICMalloc(bufLen + 1);
246 if (NULL == endpoint->resourceUri)
248 OIC_LOG(ERROR, TAG, "CAReceivedPacketCallback, Memory allocation failed!");
250 coap_delete_pdu(pdu);
253 memcpy(endpoint->resourceUri, uri, bufLen);
254 endpoint->resourceUri[bufLen] = '\0';
255 OIC_LOG_V(DEBUG, TAG, "URI : %s", endpoint->resourceUri);
260 if (g_requestHandler)
262 g_requestHandler(endpoint, ReqInfo);
265 CADestroyRequestInfoInternal(ReqInfo);
270 CAResponseInfo_t *ResInfo = (CAResponseInfo_t *) OICCalloc(1, sizeof(CAResponseInfo_t));
273 OIC_LOG(ERROR, TAG, "CAReceivedPacketCallback, Memory allocation failed!");
274 coap_delete_pdu(pdu);
278 CAResult_t res = CAGetResponseInfoFromPDU(pdu, ResInfo, uri, bufLen);
279 if (CA_STATUS_OK != res)
281 OIC_LOG_V(ERROR, TAG, "CAGetResponseInfoFromPDU failed : %d", res);
283 coap_delete_pdu(pdu);
287 if (NULL != ResInfo->info.options)
289 for (uint32_t i = 0; i < ResInfo->info.numOptions; i++)
291 OIC_LOG_V(DEBUG, TAG, "optionID: %d", ResInfo->info.options[i].optionID);
293 OIC_LOG_V(DEBUG, TAG, "list: %s", ResInfo->info.options[i].optionData);
297 if (NULL != ResInfo->info.payload)
299 OIC_LOG_V(DEBUG, TAG, "payload: %s", ResInfo->info.payload);
301 OIC_LOG_V(DEBUG, TAG, "code: %d", ResInfo->result);
303 if (NULL != endpoint)
305 endpoint->resourceUri = (char *) OICMalloc(bufLen + 1);
306 if (NULL == endpoint->resourceUri)
308 OIC_LOG(ERROR, TAG, "CAReceivedPacketCallback, Memory allocation failed !");
310 coap_delete_pdu(pdu);
313 memcpy(endpoint->resourceUri, uri, bufLen);
314 endpoint->resourceUri[bufLen] = '\0';
315 OIC_LOG_V(DEBUG, TAG, "URI : %s", endpoint->resourceUri);
318 // for retransmission
319 void *retransmissionPdu = NULL;
320 CARetransmissionReceivedData(&g_retransmissionContext, endpoint, pdu->hdr, pdu->length,
323 // get token from saved data in retransmission list
324 if (retransmissionPdu && CA_EMPTY == code)
326 CAResult_t res = CAGetTokenFromPDU((const coap_hdr_t *)retransmissionPdu,
328 if(res != CA_STATUS_OK)
330 OIC_LOG(ERROR, TAG, "fail to get Token from retransmission list");
331 OICFree(ResInfo->info.token);
334 OICFree(retransmissionPdu);
338 if (g_responseHandler)
340 g_responseHandler(endpoint, ResInfo);
342 CADestroyResponseInfoInternal(ResInfo);
346 if (endpoint && endpoint->resourceUri)
348 OICFree(endpoint->resourceUri);
349 endpoint->resourceUri = NULL;
353 coap_delete_pdu(pdu);
355 OIC_LOG(DEBUG, TAG, "OUT");
358 static void CANetworkChangedCallback(CALocalConnectivity_t *info, CANetworkStatus_t status)
360 OIC_LOG(DEBUG, TAG, "IN");
362 OIC_LOG(DEBUG, TAG, "OUT");
365 void CAHandleRequestResponseCallbacks()
368 CARetransmissionBaseRoutine((void *)&g_retransmissionContext);
371 CAResult_t CADetachRequestMessage(const CARemoteEndpoint_t *object, const CARequestInfo_t *request)
373 OIC_LOG(DEBUG, TAG, "IN");
375 VERIFY_NON_NULL(object, TAG, "object");
376 VERIFY_NON_NULL(request, TAG, "request");
378 // If max retransmission queue is reached, then don't handle new request
379 if (CA_MAX_RT_ARRAY_SIZE == u_arraylist_length(g_retransmissionContext.dataList))
381 OIC_LOG(ERROR, TAG, "max RT queue size reached!");
382 return CA_SEND_FAILED;
385 // allocate & initialize
386 CAData_t *data = (CAData_t *) OICCalloc(1, sizeof(CAData_t));
387 CA_MEMORY_ALLOC_CHECK(data);
390 data->type = SEND_TYPE_UNICAST;
391 data->remoteEndpoint = object;
392 data->requestInfo = request;
393 data->responseInfo = NULL;
397 OIC_LOG(DEBUG, TAG, "OUT");
400 // memory error label.
403 OIC_LOG(DEBUG, TAG, "OUT");
404 return CA_MEMORY_ALLOC_FAILED;
407 CAResult_t CADetachRequestToAllMessage(const CAGroupEndpoint_t *object,
408 const CARequestInfo_t *request)
410 OIC_LOG(DEBUG, TAG, "IN");
412 if (NULL == object || NULL == request || NULL == object->resourceUri)
414 return CA_STATUS_INVALID_PARAM;
417 if ((request->method < CA_GET) || (request->method > CA_DELETE))
419 OIC_LOG(ERROR, TAG, "Invalid method type!");
421 return CA_STATUS_INVALID_PARAM;
424 // allocate & initialize
425 CAData_t *data = (CAData_t *) OICCalloc(1, sizeof(CAData_t));
426 CA_MEMORY_ALLOC_CHECK(data);
428 CAAddress_t addr = {0};
429 CARemoteEndpoint_t *remoteEndpoint = CACreateRemoteEndpointInternal(object->resourceUri, addr,
430 object->connectivityType);
433 data->type = SEND_TYPE_MULTICAST;
434 data->remoteEndpoint = remoteEndpoint;
435 data->requestInfo = request;
436 data->responseInfo = NULL;
439 CADestroyRemoteEndpointInternal(remoteEndpoint);
442 OIC_LOG(DEBUG, TAG, "OUT");
445 // memory error label.
449 OIC_LOG(DEBUG, TAG, "OUT");
450 return CA_MEMORY_ALLOC_FAILED;
453 CAResult_t CADetachResponseMessage(const CARemoteEndpoint_t *object,
454 const CAResponseInfo_t *response)
456 OIC_LOG(DEBUG, TAG, "IN");
457 VERIFY_NON_NULL(object, TAG, "object");
458 VERIFY_NON_NULL(response, TAG, "response");
460 // allocate & initialize
461 CAData_t *data = (CAData_t *) OICCalloc(1, sizeof(CAData_t));
462 CA_MEMORY_ALLOC_CHECK(data);
465 data->type = SEND_TYPE_UNICAST;
466 data->remoteEndpoint = object;
467 data->requestInfo = NULL;
468 data->responseInfo = response;
473 OIC_LOG(DEBUG, TAG, "OUT");
476 // memory error label.
479 OIC_LOG(DEBUG, TAG, "OUT");
481 return CA_MEMORY_ALLOC_FAILED;
484 CAResult_t CADetachMessageResourceUri(const CAURI_t resourceUri, const CAToken_t token,
485 uint8_t tokenLength, const CAHeaderOption_t *options,
488 OIC_LOG(DEBUG, TAG, "IN");
489 VERIFY_NON_NULL(resourceUri, TAG, "resourceUri is NULL");
490 VERIFY_NON_NULL(token, TAG, "Token is NULL");
492 // allocate & initialize
493 CAData_t *data = (CAData_t *) OICCalloc(1, sizeof(CAData_t));
494 CA_MEMORY_ALLOC_CHECK(data);
496 CAAddress_t addr = {0};
497 CARemoteEndpoint_t *remoteEndpoint =
498 CACreateRemoteEndpointInternal(resourceUri, addr,
499 CA_ETHERNET | CA_WIFI | CA_EDR | CA_LE);
501 // create request info
502 CARequestInfo_t *reqInfo = (CARequestInfo_t *) OICCalloc(1, sizeof(CARequestInfo_t));
503 CA_MEMORY_ALLOC_CHECK(reqInfo);
505 // save request info data
506 reqInfo->method = CA_GET;
507 reqInfo->info.type = CA_MSG_NONCONFIRM;
509 reqInfo->info.token = token;
510 reqInfo->info.tokenLength = tokenLength;
513 data->type = SEND_TYPE_MULTICAST;
514 data->remoteEndpoint = remoteEndpoint;
515 data->requestInfo = reqInfo;
517 data->responseInfo = NULL;
518 data->options = NULL;
519 data->numOptions = 0;
520 CAHeaderOption_t *headerOption = NULL;
521 if (NULL != options && numOptions > 0)
524 headerOption = (CAHeaderOption_t *) OICMalloc(sizeof(CAHeaderOption_t) * numOptions);
525 CA_MEMORY_ALLOC_CHECK(headerOption);
526 memcpy(headerOption, options, sizeof(CAHeaderOption_t) * numOptions);
528 data->options = headerOption;
529 data->numOptions = numOptions;
534 CADestroyRemoteEndpoint(remoteEndpoint);
535 OICFree(headerOption);
538 OIC_LOG(DEBUG, TAG, "OUT");
541 // memory error label.
544 CADestroyRemoteEndpointInternal(remoteEndpoint);
548 OIC_LOG(DEBUG, TAG, "OUT");
549 return CA_MEMORY_ALLOC_FAILED;
552 void CASetRequestResponseCallbacks(CARequestCallback ReqHandler, CAResponseCallback RespHandler)
554 OIC_LOG(DEBUG, TAG, "IN");
555 g_requestHandler = ReqHandler;
556 g_responseHandler = RespHandler;
557 OIC_LOG(DEBUG, TAG, "OUT");
560 CAResult_t CAInitializeMessageHandler()
562 OIC_LOG(DEBUG, TAG, "IN");
563 CASetPacketReceivedCallback(CAReceivedPacketCallback);
565 CASetNetworkChangeCallback(CANetworkChangedCallback);
567 // retransmission initialize
568 CARetransmissionInitialize(&g_retransmissionContext, CASendUnicastData,
569 CATimeoutCallback, NULL);
571 CAInitializeAdapters();
572 OIC_LOG(DEBUG, TAG, "OUT");
576 void CATerminateMessageHandler()
578 OIC_LOG(DEBUG, TAG, "IN");
579 // terminate interface adapters by controller
580 CATerminateAdapters();
582 // stop retransmission
583 CARetransmissionStop(&g_retransmissionContext);
584 CARetransmissionDestroy(&g_retransmissionContext);
586 OIC_LOG(DEBUG, TAG, "OUT");
589 void CALogPDUInfo(coap_pdu_t *pdu)
591 VERIFY_NON_NULL_VOID(pdu, TAG, "pdu");
593 OIC_LOG_V(DEBUG, TAG, "PDU Maker - payload : %s", pdu->data);
595 OIC_LOG_V(DEBUG, TAG, "PDU Maker - type : %d", pdu->hdr->type);
597 OIC_LOG_V(DEBUG, TAG, "PDU Maker - code : %d", pdu->hdr->code);
599 OIC_LOG_V(DEBUG, TAG, "PDU Maker - id : %d", ntohs(pdu->hdr->id));
601 OIC_LOG(DEBUG, TAG, "PDU Maker - token :");
603 OIC_LOG_BUFFER(DEBUG, TAG, pdu->hdr->token, pdu->hdr->token_length);