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.h"
30 #include "caprotocolmessage.h"
31 #include "caretransmission.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 CAEndpoint_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;
61 static CAErrorCallback g_errorHandler = NULL;
63 static void CATimeoutCallback(const CAEndpoint_t *endpoint, const void *pdu, uint32_t size)
65 OIC_LOG(DEBUG, TAG, "IN");
66 CAEndpoint_t* ep = CACloneEndpoint(endpoint);
69 OIC_LOG(ERROR, TAG, "clone failed");
73 CAResponseInfo_t* resInfo = (CAResponseInfo_t*) OICCalloc(1, sizeof(CAResponseInfo_t));
77 OIC_LOG(ERROR, TAG, "calloc failed");
82 resInfo->result = CA_RETRANSMIT_TIMEOUT;
83 resInfo->info.type = CAGetMessageTypeFromPduBinaryData(pdu, size);
84 resInfo->info.messageId = CAGetMessageIdFromPduBinaryData(pdu, size);
86 if (g_responseHandler)
88 g_responseHandler(ep, resInfo);
94 OIC_LOG(DEBUG, TAG, "OUT");
96 static void CAProcessData(const CAData_t *data)
98 OIC_LOG(DEBUG, TAG, "IN");
99 VERIFY_NON_NULL_VOID(data, TAG, "data");
100 VERIFY_NON_NULL_VOID(data->remoteEndpoint, TAG, "remoteEndpoint");
102 CAResult_t res = CA_STATUS_FAILED;
104 CASendDataType_t type = data->type;
106 if (SEND_TYPE_UNICAST == type)
108 OIC_LOG(DEBUG,TAG,"Unicast Message");
109 coap_pdu_t *pdu = NULL;
111 if (NULL != data->requestInfo)
113 OIC_LOG(DEBUG, TAG, "reqInfo avlbl");
115 pdu = (coap_pdu_t *)CAGeneratePDU(data->requestInfo->method, &data->requestInfo->info);
117 else if (NULL != data->responseInfo)
119 OIC_LOG(DEBUG, TAG, "resInfo avlbl");
121 pdu = (coap_pdu_t *)CAGeneratePDU(data->responseInfo->result, &data->responseInfo->info);
125 OIC_LOG(DEBUG, TAG, "request info, response info is empty");
129 // interface controller function call.
134 res = CASendUnicastData(data->remoteEndpoint, pdu->hdr, pdu->length);
135 if (CA_STATUS_OK != res)
137 OIC_LOG_V(ERROR, TAG, "send failed:%d", res);
138 coap_delete_pdu(pdu);
141 // for retransmission
142 res = CARetransmissionSentData(&g_retransmissionContext, data->remoteEndpoint, pdu->hdr,
144 if (CA_STATUS_OK != res)
146 OIC_LOG_V(INFO, TAG, "retransmissions will not be working: %d", res);
147 coap_delete_pdu(pdu);
151 coap_delete_pdu(pdu);
155 OIC_LOG(ERROR,TAG, "Failed to Generate Unicast PDU");
159 else if (SEND_TYPE_MULTICAST == type)
161 OIC_LOG(DEBUG,TAG,"Multicast Message");
162 if (NULL != data->requestInfo)
164 OIC_LOG(DEBUG, TAG, "reqInfo avlbl");
166 CAInfo_t *info = &data->requestInfo->info;
168 info->options = data->options;
169 info->numOptions = data->numOptions;
171 coap_pdu_t *pdu = (coap_pdu_t *)CAGeneratePDU(CA_GET, info);
176 res = CASendMulticastData(data->remoteEndpoint, pdu->hdr, pdu->length);
177 if(CA_STATUS_OK != res)
179 OIC_LOG_V(ERROR, TAG, "send failed:%d", res);
180 coap_delete_pdu(pdu);
183 coap_delete_pdu(pdu);
187 OIC_LOG(ERROR,TAG,"Failed to Generate Multicast PDU");
192 OIC_LOG(ERROR,TAG,"requestInfo is empty");
196 OIC_LOG(DEBUG, TAG, "OUT");
199 static void CAReceivedPacketCallback(CAEndpoint_t *endpoint, void *data, uint32_t dataLen)
201 OIC_LOG(DEBUG, TAG, "IN");
202 VERIFY_NON_NULL_VOID(data, TAG, "data");
204 uint32_t code = CA_NOT_FOUND;
205 coap_pdu_t *pdu = (coap_pdu_t *) CAParsePDU((const char *) data, dataLen, &code);
209 OIC_LOG(ERROR, TAG, "Parse PDU failed");
213 char uri[CA_MAX_URI_LENGTH] = { };
215 if (CA_GET == code || CA_POST == code || CA_PUT == code || CA_DELETE == code)
217 CARequestInfo_t *ReqInfo = (CARequestInfo_t *) OICCalloc(1, sizeof(CARequestInfo_t));
220 OIC_LOG(ERROR, TAG, "CAReceivedPacketCallback, Memory allocation failed!");
221 coap_delete_pdu(pdu);
225 CAResult_t res = CAGetRequestInfoFromPDU(pdu, ReqInfo);
226 if (CA_STATUS_OK != res)
228 OIC_LOG_V(ERROR, TAG, "CAGetRequestInfoFromPDU failed : %d", res);
230 coap_delete_pdu(pdu);
234 if (NULL != ReqInfo->info.options)
236 for (uint32_t i = 0; i < ReqInfo->info.numOptions; i++)
238 OIC_LOG_V(DEBUG, TAG, "optionID: %d", ReqInfo->info.options[i].optionID);
240 OIC_LOG_V(DEBUG, TAG, "list: %s", ReqInfo->info.options[i].optionData);
244 if (NULL != ReqInfo->info.payload)
246 OIC_LOG_V(DEBUG, TAG, "Request- payload: %s", ReqInfo->info.payload);
249 OIC_LOG_V(DEBUG, TAG, "code: %d", ReqInfo->method);
250 OIC_LOG(DEBUG, TAG, "token:");
251 OIC_LOG_BUFFER(DEBUG, TAG, (const uint8_t *) ReqInfo->info.token, CA_MAX_TOKEN_LEN);
253 if (g_requestHandler)
255 g_requestHandler(endpoint, ReqInfo);
258 CADestroyRequestInfoInternal(ReqInfo);
262 CAResponseInfo_t *ResInfo = (CAResponseInfo_t *) OICCalloc(1, sizeof(CAResponseInfo_t));
265 OIC_LOG(ERROR, TAG, "CAReceivedPacketCallback, Memory allocation failed!");
266 coap_delete_pdu(pdu);
270 CAResult_t res = CAGetResponseInfoFromPDU(pdu, ResInfo);
271 if (CA_STATUS_OK != res)
273 OIC_LOG_V(ERROR, TAG, "CAGetResponseInfoFromPDU failed : %d", res);
275 coap_delete_pdu(pdu);
279 if (NULL != ResInfo->info.options)
281 for (uint32_t i = 0; i < ResInfo->info.numOptions; i++)
283 OIC_LOG_V(DEBUG, TAG, "optionID: %d", ResInfo->info.options[i].optionID);
285 OIC_LOG_V(DEBUG, TAG, "list: %s", ResInfo->info.options[i].optionData);
289 if (NULL != ResInfo->info.payload)
291 OIC_LOG_V(DEBUG, TAG, "payload: %s", ResInfo->info.payload);
293 OIC_LOG_V(DEBUG, TAG, "code: %d", ResInfo->result);
295 // for retransmission
296 void *retransmissionPdu = NULL;
297 CARetransmissionReceivedData(&g_retransmissionContext, endpoint, pdu->hdr, pdu->length,
300 // get token from saved data in retransmission list
301 if (retransmissionPdu && CA_EMPTY == code)
303 CAResult_t res = CAGetTokenFromPDU((const coap_hdr_t *)retransmissionPdu,
305 if(CA_STATUS_OK != res)
307 OIC_LOG(ERROR, TAG, "fail to get Token from retransmission list");
308 OICFree(ResInfo->info.token);
311 OICFree(retransmissionPdu);
315 if (g_responseHandler)
317 g_responseHandler(endpoint, ResInfo);
319 CADestroyResponseInfoInternal(ResInfo);
325 coap_delete_pdu(pdu);
327 OIC_LOG(DEBUG, TAG, "OUT");
330 static void CANetworkChangedCallback(CAEndpoint_t *info, CANetworkStatus_t status)
332 OIC_LOG(DEBUG, TAG, "IN");
334 OIC_LOG(DEBUG, TAG, "OUT");
337 void CAHandleRequestResponseCallbacks()
340 CARetransmissionBaseRoutine((void *)&g_retransmissionContext);
343 CAResult_t CADetachRequestMessage(const CAEndpoint_t *object, const CARequestInfo_t *request)
345 OIC_LOG(DEBUG, TAG, "IN");
347 VERIFY_NON_NULL(object, TAG, "object");
348 VERIFY_NON_NULL(request, TAG, "request");
350 // If max retransmission queue is reached, then don't handle new request
351 if (CA_MAX_RT_ARRAY_SIZE == u_arraylist_length(g_retransmissionContext.dataList))
353 OIC_LOG(ERROR, TAG, "max RT queue size reached!");
354 return CA_SEND_FAILED;
357 // allocate & initialize
358 CAData_t *data = (CAData_t *) OICCalloc(1, sizeof(CAData_t));
359 CA_MEMORY_ALLOC_CHECK(data);
362 data->type = request->isMulticast ? SEND_TYPE_MULTICAST : SEND_TYPE_UNICAST;
363 data->remoteEndpoint = object;
364 data->requestInfo = request;
365 data->responseInfo = NULL;
369 OIC_LOG(DEBUG, TAG, "OUT");
372 // memory error label.
375 OIC_LOG(DEBUG, TAG, "OUT");
376 return CA_MEMORY_ALLOC_FAILED;
379 CAResult_t CADetachResponseMessage(const CAEndpoint_t *object,
380 const CAResponseInfo_t *response)
382 OIC_LOG(DEBUG, TAG, "IN");
383 VERIFY_NON_NULL(object, TAG, "object");
384 VERIFY_NON_NULL(response, TAG, "response");
386 // allocate & initialize
387 CAData_t *data = (CAData_t *) OICCalloc(1, sizeof(CAData_t));
388 CA_MEMORY_ALLOC_CHECK(data);
391 data->type = SEND_TYPE_UNICAST;
392 data->remoteEndpoint = object;
393 data->requestInfo = NULL;
394 data->responseInfo = response;
399 OIC_LOG(DEBUG, TAG, "OUT");
402 // memory error label.
405 OIC_LOG(DEBUG, TAG, "OUT");
407 return CA_MEMORY_ALLOC_FAILED;
410 void CASetInterfaceCallbacks(CARequestCallback ReqHandler,
411 CAResponseCallback RespHandler, CAErrorCallback errorHandler)
413 OIC_LOG(DEBUG, TAG, "IN");
414 g_requestHandler = ReqHandler;
415 g_responseHandler = RespHandler;
416 g_errorHandler = errorHandler;
417 OIC_LOG(DEBUG, TAG, "OUT");
420 CAResult_t CAInitializeMessageHandler()
422 OIC_LOG(DEBUG, TAG, "IN");
423 CASetPacketReceivedCallback(CAReceivedPacketCallback);
425 CASetNetworkChangeCallback(CANetworkChangedCallback);
427 // retransmission initialize
428 CARetransmissionInitialize(&g_retransmissionContext, NULL, CASendUnicastData,
429 CATimeoutCallback, NULL);
431 CAInitializeAdapters(NULL);
432 OIC_LOG(DEBUG, TAG, "OUT");
436 void CATerminateMessageHandler()
438 OIC_LOG(DEBUG, TAG, "IN");
439 // terminate interface adapters by controller
440 CATerminateAdapters();
442 // stop retransmission
443 CARetransmissionStop(&g_retransmissionContext);
444 CARetransmissionDestroy(&g_retransmissionContext);
446 OIC_LOG(DEBUG, TAG, "OUT");
449 void CALogPDUInfo(coap_pdu_t *pdu)
451 VERIFY_NON_NULL_VOID(pdu, TAG, "pdu");
453 OIC_LOG_V(DEBUG, TAG, "PDU Maker - payload : %s", pdu->data);
455 OIC_LOG_V(DEBUG, TAG, "PDU Maker - type : %d", pdu->hdr->type);
457 OIC_LOG_V(DEBUG, TAG, "PDU Maker - code : %d", pdu->hdr->code);
459 OIC_LOG_V(DEBUG, TAG, "PDU Maker - id : %d", ntohs(pdu->hdr->id));
461 OIC_LOG(DEBUG, TAG, "PDU Maker - token :");
463 OIC_LOG_BUFFER(DEBUG, TAG, pdu->hdr->token, pdu->hdr->token_length);