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 coap_pdu_t *pdu = NULL;
110 if (NULL != data->requestInfo)
112 OIC_LOG(DEBUG, TAG, "reqInfo avlbl");
114 pdu = (coap_pdu_t *)CAGeneratePDU(data->requestInfo->method, &data->requestInfo->info);
116 else if (NULL != data->responseInfo)
118 OIC_LOG(DEBUG, TAG, "resInfo avlbl");
120 pdu = (coap_pdu_t *)CAGeneratePDU(data->responseInfo->result, &data->responseInfo->info);
124 OIC_LOG(DEBUG, TAG, "request info, response info is empty");
127 // interface controller function call.
132 res = CASendUnicastData(data->remoteEndpoint, pdu->hdr, pdu->length);
133 if (CA_STATUS_OK != res)
135 OIC_LOG_V(ERROR, TAG, "send failed:%d", res);
136 coap_delete_pdu(pdu);
139 // for retransmission
140 res = CARetransmissionSentData(&g_retransmissionContext, data->remoteEndpoint, pdu->hdr,
142 if (CA_STATUS_OK != res)
144 OIC_LOG_V(INFO, TAG, "retransmissions will not be working: %d", res);
145 coap_delete_pdu(pdu);
149 coap_delete_pdu(pdu);
152 else if (SEND_TYPE_MULTICAST == type)
154 OIC_LOG(DEBUG, TAG, "both requestInfo & responseInfo is not available");
156 CAInfo_t *info = &data->requestInfo->info;
158 info->options = data->options;
159 info->numOptions = data->numOptions;
161 coap_pdu_t *pdu = (coap_pdu_t *)CAGeneratePDU(CA_GET, info);
166 res = CASendMulticastData(data->remoteEndpoint, pdu->hdr, pdu->length);
167 if(CA_STATUS_OK != res)
169 OIC_LOG_V(ERROR, TAG, "send failed:%d", res);
170 coap_delete_pdu(pdu);
173 coap_delete_pdu(pdu);
177 OIC_LOG(DEBUG, TAG, "OUT");
180 static void CAReceivedPacketCallback(CAEndpoint_t *endpoint, void *data, uint32_t dataLen)
182 OIC_LOG(DEBUG, TAG, "IN");
183 VERIFY_NON_NULL_VOID(data, TAG, "data");
185 uint32_t code = CA_NOT_FOUND;
186 coap_pdu_t *pdu = (coap_pdu_t *) CAParsePDU((const char *) data, dataLen, &code);
190 OIC_LOG(ERROR, TAG, "Parse PDU failed");
194 char uri[CA_MAX_URI_LENGTH] = { };
196 if (CA_GET == code || CA_POST == code || CA_PUT == code || CA_DELETE == code)
198 CARequestInfo_t *ReqInfo = (CARequestInfo_t *) OICCalloc(1, sizeof(CARequestInfo_t));
201 OIC_LOG(ERROR, TAG, "CAReceivedPacketCallback, Memory allocation failed!");
202 coap_delete_pdu(pdu);
206 CAResult_t res = CAGetRequestInfoFromPDU(pdu, ReqInfo);
207 if (CA_STATUS_OK != res)
209 OIC_LOG_V(ERROR, TAG, "CAGetRequestInfoFromPDU failed : %d", res);
211 coap_delete_pdu(pdu);
215 if (NULL != ReqInfo->info.options)
217 for (uint32_t i = 0; i < ReqInfo->info.numOptions; i++)
219 OIC_LOG_V(DEBUG, TAG, "optionID: %d", ReqInfo->info.options[i].optionID);
221 OIC_LOG_V(DEBUG, TAG, "list: %s", ReqInfo->info.options[i].optionData);
225 if (NULL != ReqInfo->info.payload)
227 OIC_LOG_V(DEBUG, TAG, "Request- payload: %s", ReqInfo->info.payload);
230 OIC_LOG_V(DEBUG, TAG, "code: %d", ReqInfo->method);
231 OIC_LOG(DEBUG, TAG, "token:");
232 OIC_LOG_BUFFER(DEBUG, TAG, (const uint8_t *) ReqInfo->info.token, CA_MAX_TOKEN_LEN);
234 if (g_requestHandler)
236 g_requestHandler(endpoint, ReqInfo);
239 CADestroyRequestInfoInternal(ReqInfo);
243 CAResponseInfo_t *ResInfo = (CAResponseInfo_t *) OICCalloc(1, sizeof(CAResponseInfo_t));
246 OIC_LOG(ERROR, TAG, "CAReceivedPacketCallback, Memory allocation failed!");
247 coap_delete_pdu(pdu);
251 CAResult_t res = CAGetResponseInfoFromPDU(pdu, ResInfo);
252 if (CA_STATUS_OK != res)
254 OIC_LOG_V(ERROR, TAG, "CAGetResponseInfoFromPDU failed : %d", res);
256 coap_delete_pdu(pdu);
260 if (NULL != ResInfo->info.options)
262 for (uint32_t i = 0; i < ResInfo->info.numOptions; i++)
264 OIC_LOG_V(DEBUG, TAG, "optionID: %d", ResInfo->info.options[i].optionID);
266 OIC_LOG_V(DEBUG, TAG, "list: %s", ResInfo->info.options[i].optionData);
270 if (NULL != ResInfo->info.payload)
272 OIC_LOG_V(DEBUG, TAG, "payload: %s", ResInfo->info.payload);
274 OIC_LOG_V(DEBUG, TAG, "code: %d", ResInfo->result);
276 // for retransmission
277 void *retransmissionPdu = NULL;
278 CARetransmissionReceivedData(&g_retransmissionContext, endpoint, pdu->hdr, pdu->length,
281 // get token from saved data in retransmission list
282 if (retransmissionPdu && CA_EMPTY == code)
284 CAResult_t res = CAGetTokenFromPDU((const coap_hdr_t *)retransmissionPdu,
286 if(CA_STATUS_OK != res)
288 OIC_LOG(ERROR, TAG, "fail to get Token from retransmission list");
289 OICFree(ResInfo->info.token);
292 OICFree(retransmissionPdu);
296 if (g_responseHandler)
298 g_responseHandler(endpoint, ResInfo);
300 CADestroyResponseInfoInternal(ResInfo);
306 coap_delete_pdu(pdu);
308 OIC_LOG(DEBUG, TAG, "OUT");
311 static void CANetworkChangedCallback(CAEndpoint_t *info, CANetworkStatus_t status)
313 OIC_LOG(DEBUG, TAG, "IN");
315 OIC_LOG(DEBUG, TAG, "OUT");
318 void CAHandleRequestResponseCallbacks()
321 CARetransmissionBaseRoutine((void *)&g_retransmissionContext);
324 CAResult_t CADetachRequestMessage(const CAEndpoint_t *object, const CARequestInfo_t *request)
326 OIC_LOG(DEBUG, TAG, "IN");
328 VERIFY_NON_NULL(object, TAG, "object");
329 VERIFY_NON_NULL(request, TAG, "request");
331 // If max retransmission queue is reached, then don't handle new request
332 if (CA_MAX_RT_ARRAY_SIZE == u_arraylist_length(g_retransmissionContext.dataList))
334 OIC_LOG(ERROR, TAG, "max RT queue size reached!");
335 return CA_SEND_FAILED;
338 // allocate & initialize
339 CAData_t *data = (CAData_t *) OICCalloc(1, sizeof(CAData_t));
340 CA_MEMORY_ALLOC_CHECK(data);
343 data->type = request->isMulticast ? SEND_TYPE_MULTICAST : SEND_TYPE_UNICAST;
344 data->remoteEndpoint = object;
345 data->requestInfo = request;
346 data->responseInfo = NULL;
350 OIC_LOG(DEBUG, TAG, "OUT");
353 // memory error label.
356 OIC_LOG(DEBUG, TAG, "OUT");
357 return CA_MEMORY_ALLOC_FAILED;
360 CAResult_t CADetachResponseMessage(const CAEndpoint_t *object,
361 const CAResponseInfo_t *response)
363 OIC_LOG(DEBUG, TAG, "IN");
364 VERIFY_NON_NULL(object, TAG, "object");
365 VERIFY_NON_NULL(response, TAG, "response");
367 // allocate & initialize
368 CAData_t *data = (CAData_t *) OICCalloc(1, sizeof(CAData_t));
369 CA_MEMORY_ALLOC_CHECK(data);
372 data->type = SEND_TYPE_UNICAST;
373 data->remoteEndpoint = object;
374 data->requestInfo = NULL;
375 data->responseInfo = response;
380 OIC_LOG(DEBUG, TAG, "OUT");
383 // memory error label.
386 OIC_LOG(DEBUG, TAG, "OUT");
388 return CA_MEMORY_ALLOC_FAILED;
391 void CASetInterfaceCallbacks(CARequestCallback ReqHandler,
392 CAResponseCallback RespHandler, CAErrorCallback errorHandler)
394 OIC_LOG(DEBUG, TAG, "IN");
395 g_requestHandler = ReqHandler;
396 g_responseHandler = RespHandler;
397 g_errorHandler = errorHandler;
398 OIC_LOG(DEBUG, TAG, "OUT");
401 CAResult_t CAInitializeMessageHandler()
403 OIC_LOG(DEBUG, TAG, "IN");
404 CASetPacketReceivedCallback(CAReceivedPacketCallback);
406 CASetNetworkChangeCallback(CANetworkChangedCallback);
408 // retransmission initialize
409 CARetransmissionInitialize(&g_retransmissionContext, NULL, CASendUnicastData,
410 CATimeoutCallback, NULL);
412 CAInitializeAdapters(NULL);
413 OIC_LOG(DEBUG, TAG, "OUT");
417 void CATerminateMessageHandler()
419 OIC_LOG(DEBUG, TAG, "IN");
420 // terminate interface adapters by controller
421 CATerminateAdapters();
423 // stop retransmission
424 CARetransmissionStop(&g_retransmissionContext);
425 CARetransmissionDestroy(&g_retransmissionContext);
427 OIC_LOG(DEBUG, TAG, "OUT");
430 void CALogPDUInfo(coap_pdu_t *pdu)
432 VERIFY_NON_NULL_VOID(pdu, TAG, "pdu");
434 OIC_LOG_V(DEBUG, TAG, "PDU Maker - payload : %s", pdu->data);
436 OIC_LOG_V(DEBUG, TAG, "PDU Maker - type : %d", pdu->hdr->type);
438 OIC_LOG_V(DEBUG, TAG, "PDU Maker - code : %d", pdu->hdr->code);
440 OIC_LOG_V(DEBUG, TAG, "PDU Maker - id : %d", ntohs(pdu->hdr->id));
442 OIC_LOG(DEBUG, TAG, "PDU Maker - token :");
444 OIC_LOG_BUFFER(DEBUG, TAG, pdu->hdr->token, pdu->hdr->token_length);