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 ******************************************************************/
21 #include "camessagehandler_singlethread.h"
28 #include "cainterface.h"
29 #include "caremotehandler.h"
30 #include "cainterfacecontroller_singlethread.h"
31 #include "caprotocolmessage_singlethread.h"
33 #include "config.h" /* for coap protocol */
35 #include "oic_malloc.h"
36 #include "caadapterutils.h"
40 #define CA_MEMORY_ALLOC_CHECK(arg) { if (arg == NULL) {OIC_LOG(ERROR, TAG, "Out of memory");\
41 goto memory_error_exit;} }
43 #define CA_MAX_RT_ARRAY_SIZE 3
47 SEND_TYPE_MULTICAST = 0, SEND_TYPE_UNICAST
52 CASendDataType_t type;
53 CARemoteEndpoint_t *remoteEndpoint;
54 CARequestInfo_t *requestInfo;
55 CAResponseInfo_t *responseInfo;
56 CAHeaderOption_t *options;
61 static CARetransmission_t g_retransmissionContext;
64 static CARequestCallback g_requestHandler = NULL;
65 static CAResponseCallback g_responseHandler = NULL;
67 static void CAProcessData(CAData_t *data)
69 OIC_LOG(DEBUG, TAG, "IN");
70 VERIFY_NON_NULL_VOID(data, TAG, "data");
71 VERIFY_NON_NULL_VOID(data->remoteEndpoint, TAG, "remoteendpoint");
73 CAResult_t res = CA_STATUS_FAILED;
75 CASendDataType_t type = data->type;
77 if (type == SEND_TYPE_UNICAST)
79 coap_pdu_t *pdu = NULL;
81 if (data->requestInfo != NULL)
83 OIC_LOG(DEBUG, TAG, "reqInfo avlbl");
85 pdu = (coap_pdu_t *) CAGeneratePdu(data->remoteEndpoint->resourceUri,
86 data->requestInfo->method, data->requestInfo->info);
88 else if (data->responseInfo != NULL)
90 OIC_LOG(DEBUG, TAG, "resInfo avlbl");
92 pdu = (coap_pdu_t *) CAGeneratePdu(data->remoteEndpoint->resourceUri,
93 data->responseInfo->result,
94 data->responseInfo->info);
98 OIC_LOG(DEBUG, TAG, "request info, response info is empty");
101 // interface controller function call.
106 res = CASendUnicastData(data->remoteEndpoint, pdu->hdr, pdu->length);
107 if(CA_STATUS_OK != res)
109 OIC_LOG_V(ERROR, TAG, "send failed:%d", res);
110 coap_delete_pdu(pdu);
113 // for retransmission
114 CARetransmissionSentData(&g_retransmissionContext, data->remoteEndpoint, pdu->hdr,
117 coap_delete_pdu(pdu);
119 else if (type == SEND_TYPE_MULTICAST)
121 OIC_LOG(DEBUG, TAG, "both requestInfo & responseInfo is not available");
123 coap_pdu_t *pdu = NULL;
124 CAInfo_t info = { 0 };
126 info.options = data->options;
127 info.numOptions = data->numOptions;
128 info.token = data->requestInfo->info.token;
129 info.tokenLength = data->requestInfo->info.tokenLength;
130 info.type = data->requestInfo->info.type;
132 pdu = (coap_pdu_t *) CAGeneratePdu(data->remoteEndpoint->resourceUri, CA_GET, info);
137 res = CASendMulticastData(pdu->hdr, pdu->length);
138 if(CA_STATUS_OK != res)
140 OIC_LOG_V(ERROR, TAG, "send failed:%d", res);
141 coap_delete_pdu(pdu);
145 coap_delete_pdu(pdu);
148 OIC_LOG(DEBUG, TAG, "OUT");
151 static void CATimeoutCallback(const CARemoteEndpoint_t *endpoint, const void *pdu, uint32_t size)
153 OIC_LOG(DEBUG, TAG, "IN");
154 CARemoteEndpoint_t* ep = CACloneRemoteEndpoint(endpoint);
157 OIC_LOG(ERROR, TAG, "clone failed");
161 CAResponseInfo_t* resInfo = (CAResponseInfo_t*) OICCalloc(1, sizeof(CAResponseInfo_t));
165 OIC_LOG(ERROR, TAG, "calloc failed");
166 CADestroyRemoteEndpointInternal(ep);
170 resInfo->result = CA_RETRANSMIT_TIMEOUT;
171 resInfo->info.type = CAGetMessageTypeFromPduBinaryData(pdu, size);;
172 resInfo->info.messageId = CAGetMessageIdFromPduBinaryData(pdu, size);;
174 if (g_responseHandler)
176 g_responseHandler(ep, resInfo);
179 CADestroyRemoteEndpointInternal(ep);
181 OIC_LOG(DEBUG, TAG, "OUT");
184 static void CAReceivedPacketCallback(CARemoteEndpoint_t *endpoint, void *data,
187 OIC_LOG(DEBUG, TAG, "IN");
188 VERIFY_NON_NULL_VOID(data, TAG, "data");
190 uint32_t code = CA_NOT_FOUND;
191 coap_pdu_t *pdu = (coap_pdu_t *) CAParsePDU((const char *) data, dataLen, &code);
194 OIC_LOG(ERROR, TAG, "Parse PDU failed");
199 char uri[CA_MAX_URI_LENGTH] = { 0, };
200 uint32_t bufLen = sizeof(uri);
202 if (CA_GET == code || CA_POST == code || CA_PUT == code || CA_DELETE == code )
204 CARequestInfo_t *ReqInfo = (CARequestInfo_t *) OICCalloc(1, sizeof(CARequestInfo_t));
207 OIC_LOG(DEBUG, TAG, "CAReceivedPacketCallback, Memory allocation failed!");
208 coap_delete_pdu(pdu);
212 CAGetRequestInfoFromPdu(pdu, ReqInfo, uri, bufLen);
213 if (NULL != ReqInfo->info.options)
216 for (i = 0; i < ReqInfo->info.numOptions; i++)
218 OIC_LOG_V(DEBUG, TAG, "Request- optionID: %d", ReqInfo->info.options[i].optionID);
220 OIC_LOG_V(DEBUG, TAG, "Request- list: %s", ReqInfo->info.options[i].optionData);
224 if (NULL != ReqInfo->info.payload)
226 OIC_LOG_V(DEBUG, TAG, "Request- payload: %s", ReqInfo->info.payload);
229 OIC_LOG_V(DEBUG, TAG, "Request- code: %d", ReqInfo->method);
230 OIC_LOG(DEBUG, TAG, "Request- token:");
231 OIC_LOG_BUFFER(DEBUG, TAG, (const uint8_t *) ReqInfo->info.token, CA_MAX_TOKEN_LEN);
233 if (NULL != endpoint)
235 endpoint->resourceUri = (char *) OICCalloc(bufLen + 1, sizeof(char));
236 if (endpoint->resourceUri == NULL)
238 OIC_LOG(DEBUG, TAG, "CAReceivedPacketCallback, Memory allocation failed!");
239 coap_delete_pdu(pdu);
243 memcpy(endpoint->resourceUri, uri, bufLen);
244 OIC_LOG_V(DEBUG, TAG, "URI : %s", endpoint->resourceUri);
249 if (g_requestHandler)
251 g_requestHandler(endpoint, ReqInfo);
254 CADestroyRequestInfoInternal(ReqInfo);
259 CAResponseInfo_t *ResInfo = (CAResponseInfo_t *) OICCalloc(1, sizeof(CAResponseInfo_t));
262 OIC_LOG(DEBUG, TAG, "CAReceivedPacketCallback, Memory allocation failed!");
263 coap_delete_pdu(pdu);
267 CAGetResponseInfoFromPdu(pdu, ResInfo, uri, bufLen);
269 if (NULL != ResInfo->info.options)
272 for (i = 0; i < ResInfo->info.numOptions; i++)
274 OIC_LOG_V(DEBUG, TAG, "optionID: %d", ResInfo->info.options[i].optionID);
276 OIC_LOG_V(DEBUG, TAG, "list: %s", ResInfo->info.options[i].optionData);
279 if (NULL != ResInfo->info.payload)
281 OIC_LOG_V(DEBUG, TAG, "payload: %s", ResInfo->info.payload);
284 OIC_LOG_V(DEBUG, TAG, "code: %d", ResInfo->result);
287 if (NULL != endpoint)
289 endpoint->resourceUri = (char *) OICCalloc(bufLen + 1, sizeof(char));
290 if (endpoint->resourceUri == NULL)
292 OIC_LOG(DEBUG, TAG, "CAReceivedPacketCallback, Memory allocation failed!");
293 coap_delete_pdu(pdu);
297 memcpy(endpoint->resourceUri, uri, bufLen);
298 OIC_LOG_V(DEBUG, TAG, "URI : %s", endpoint->resourceUri);
303 if (g_responseHandler)
305 g_responseHandler(endpoint, ResInfo);
307 // for retransmission
308 CARetransmissionReceivedData(&g_retransmissionContext, endpoint, pdu->hdr,
311 CADestroyResponseInfoInternal(ResInfo);
315 if (endpoint && endpoint->resourceUri)
317 OICFree(endpoint->resourceUri);
318 endpoint->resourceUri = NULL;
320 coap_delete_pdu(pdu);
321 OIC_LOG(DEBUG, TAG, "OUT");
324 static void CANetworkChangedCallback(CALocalConnectivity_t *info, CANetworkStatus_t status)
326 OIC_LOG(DEBUG, TAG, "IN");
327 OIC_LOG(DEBUG, TAG, "OUT");
330 void CAHandleRequestResponseCallbacks()
333 CARetransmissionBaseRoutine((void *)&g_retransmissionContext);
336 CAResult_t CADetachRequestMessage(const CARemoteEndpoint_t *object,
337 const CARequestInfo_t *request)
339 OIC_LOG(DEBUG, TAG, "IN");
341 VERIFY_NON_NULL(object, TAG, "object");
342 VERIFY_NON_NULL(request, TAG, "request");
344 // If max retransmission queue is reached, then don't handle new request
345 if (CA_MAX_RT_ARRAY_SIZE == u_arraylist_length(g_retransmissionContext.dataList))
347 OIC_LOG(ERROR, TAG, "max RT queue size reached!");
348 return CA_SEND_FAILED;
351 // allocate & initialize
352 CAData_t *data = (CAData_t *) OICCalloc(1, sizeof(CAData_t));
353 CA_MEMORY_ALLOC_CHECK(data);
356 data->type = SEND_TYPE_UNICAST;
357 data->remoteEndpoint = object;
358 data->requestInfo = request;
359 data->responseInfo = NULL;
363 OIC_LOG(DEBUG, TAG, "OUT");
366 // memory error label.
368 OIC_LOG(DEBUG, TAG, "OUT");
369 return CA_MEMORY_ALLOC_FAILED;
372 CAResult_t CADetachRequestToAllMessage(const CAGroupEndpoint_t *object,
373 const CARequestInfo_t *request)
375 OIC_LOG(DEBUG, TAG, "IN");
377 if (object == NULL || request == NULL)
379 return CA_STATUS_FAILED;
382 // allocate & initialize
383 CAData_t *data = (CAData_t *) OICCalloc(1, sizeof(CAData_t));
384 CA_MEMORY_ALLOC_CHECK(data);
386 CAAddress_t addr = {0};
387 CARemoteEndpoint_t *remoteEndpoint = CACreateRemoteEndpointInternal(object->resourceUri, addr,
388 object->connectivityType);
391 data->type = SEND_TYPE_MULTICAST;
392 data->remoteEndpoint = remoteEndpoint;
393 data->requestInfo = request;
394 data->responseInfo = NULL;
397 CADestroyRemoteEndpoint(remoteEndpoint);
399 OIC_LOG(DEBUG, TAG, "OUT");
402 // memory error label.
405 OIC_LOG(DEBUG, TAG, "OUT");
406 return CA_MEMORY_ALLOC_FAILED;
409 CAResult_t CADetachResponseMessage(const CARemoteEndpoint_t *object,
410 const CAResponseInfo_t *response)
412 OIC_LOG(DEBUG, TAG, "IN");
413 VERIFY_NON_NULL(object, TAG, "object");
414 VERIFY_NON_NULL(response, TAG, "response");
416 // allocate & initialize
417 CAData_t *data = (CAData_t *) OICCalloc(1, sizeof(CAData_t));
418 CA_MEMORY_ALLOC_CHECK(data);
421 data->type = SEND_TYPE_UNICAST;
422 data->remoteEndpoint = object;
423 data->requestInfo = NULL;
424 data->responseInfo = response;
428 OIC_LOG(DEBUG, TAG, "OUT");
431 // memory error label.
433 OIC_LOG(DEBUG, TAG, "OUT");
434 return CA_MEMORY_ALLOC_FAILED;
437 CAResult_t CADetachMessageResourceUri(const CAURI_t resourceUri, const CAToken_t token,
438 uint8_t tokenLength, const CAHeaderOption_t *options,
441 OIC_LOG(DEBUG, TAG, "IN");
442 VERIFY_NON_NULL(resourceUri, TAG, "resourceUri is NULL");
443 VERIFY_NON_NULL(token, TAG, "Token is NULL");
445 // allocate & initialize
446 CAData_t *data = (CAData_t *) OICCalloc(1, sizeof(CAData_t));
447 CA_MEMORY_ALLOC_CHECK(data);
449 CAAddress_t addr = {0};
450 CARemoteEndpoint_t *remoteEndpoint =
451 CACreateRemoteEndpointInternal(resourceUri, addr,
452 CA_ETHERNET | CA_WIFI | CA_EDR | CA_LE);
454 // create request info
455 CARequestInfo_t *reqInfo = (CARequestInfo_t *) OICCalloc(1, sizeof(CARequestInfo_t));
456 CA_MEMORY_ALLOC_CHECK(reqInfo);
458 // save request info data
459 reqInfo->method = CA_GET;
460 reqInfo->info.type = CA_MSG_NONCONFIRM;
462 reqInfo->info.token = token;
463 reqInfo->info.tokenLength = tokenLength;
466 data->type = SEND_TYPE_MULTICAST;
467 data->remoteEndpoint = remoteEndpoint;
468 data->requestInfo = reqInfo;
470 data->responseInfo = NULL;
471 data->options = NULL;
472 data->numOptions = 0;
473 CAHeaderOption_t *headerOption = NULL;
474 if (NULL != options && numOptions > 0)
477 headerOption = (CAHeaderOption_t *) OICMalloc(sizeof(CAHeaderOption_t) * numOptions);
478 CA_MEMORY_ALLOC_CHECK(headerOption);
479 memcpy(headerOption, options, sizeof(CAHeaderOption_t) * numOptions);
481 data->options = headerOption;
482 data->numOptions = numOptions;
487 CADestroyRemoteEndpoint(remoteEndpoint);
488 OICFree(headerOption);
491 OIC_LOG(DEBUG, TAG, "OUT");
494 // memory error label.
497 CADestroyRemoteEndpointInternal(remoteEndpoint);
500 OIC_LOG(DEBUG, TAG, "OUT");
501 return CA_MEMORY_ALLOC_FAILED;
504 void CASetRequestResponseCallbacks(CARequestCallback ReqHandler,
505 CAResponseCallback RespHandler)
507 OIC_LOG(DEBUG, TAG, "IN");
508 g_requestHandler = ReqHandler;
509 g_responseHandler = RespHandler;
510 OIC_LOG(DEBUG, TAG, "OUT");
513 CAResult_t CAInitializeMessageHandler()
515 OIC_LOG(DEBUG, TAG, "IN");
516 CASetPacketReceivedCallback(CAReceivedPacketCallback);
518 CASetNetworkChangeCallback(CANetworkChangedCallback);
522 // retransmission initialize
523 CARetransmissionInitialize(&g_retransmissionContext, CASendUnicastData, CATimeoutCallback, NULL);
525 CAInitializeAdapters();
526 OIC_LOG(DEBUG, TAG, "OUT");
530 void CATerminateMessageHandler()
532 OIC_LOG(DEBUG, TAG, "IN");
533 // terminate interface adapters by controller
534 CATerminateAdapters();
536 // stop retransmission
537 CARetransmissionStop(&g_retransmissionContext);
538 CARetransmissionDestroy(&g_retransmissionContext);
540 OIC_LOG(DEBUG, TAG, "OUT");