Merge branch 'master' into resource-manipulation
[platform/upstream/iotivity.git] / resource / csdk / connectivity / src / camessagehandler_singlethread.c
1 /******************************************************************
2  *
3  * Copyright 2014 Samsung Electronics All Rights Reserved.
4  *
5  *
6  *
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
10  *
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  *
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.
18  *
19  ******************************************************************/
20
21 #include <stdio.h>
22 #include <stdlib.h>
23 #include <string.h>
24 #include <stdint.h>
25
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"
32 #include "logger.h"
33 #include "config.h" /* for coap protocol */
34 #include "oic_malloc.h"
35
36 #define TAG "CAMH_ST"
37
38 #define CA_MAX_RT_ARRAY_SIZE    3
39
40 typedef enum
41 {
42     SEND_TYPE_MULTICAST = 0, SEND_TYPE_UNICAST
43 } CASendDataType_t;
44
45 typedef struct
46 {
47     CASendDataType_t type;
48     CAEndpoint_t *remoteEndpoint;
49     CARequestInfo_t *requestInfo;
50     CAResponseInfo_t *responseInfo;
51     CAHeaderOption_t *options;
52     uint8_t numOptions;
53 } CAData_t;
54
55
56 static CARetransmission_t g_retransmissionContext;
57
58 // handler field
59 static CARequestCallback g_requestHandler = NULL;
60 static CAResponseCallback g_responseHandler = NULL;
61 static CAErrorCallback g_errorHandler = NULL;
62
63 static void CATimeoutCallback(const CAEndpoint_t *endpoint, const void *pdu, uint32_t size)
64 {
65     OIC_LOG(DEBUG, TAG, "IN");
66     CAEndpoint_t* ep = CACloneEndpoint(endpoint);
67     if (NULL == ep)
68     {
69         OIC_LOG(ERROR, TAG, "clone failed");
70         return;
71     }
72
73     CAResponseInfo_t* resInfo = (CAResponseInfo_t*) OICCalloc(1, sizeof(CAResponseInfo_t));
74
75     if (NULL == resInfo)
76     {
77         OIC_LOG(ERROR, TAG, "calloc failed");
78         CAFreeEndpoint(ep);
79         return;
80     }
81
82     resInfo->result = CA_RETRANSMIT_TIMEOUT;
83     resInfo->info.type = CAGetMessageTypeFromPduBinaryData(pdu, size);
84     resInfo->info.messageId = CAGetMessageIdFromPduBinaryData(pdu, size);
85
86     if (g_responseHandler)
87     {
88         g_responseHandler(ep, resInfo);
89     }
90
91     CAFreeEndpoint(ep);
92     OICFree(resInfo);
93
94     OIC_LOG(DEBUG, TAG, "OUT");
95 }
96 static void CAProcessData(const CAData_t *data)
97 {
98     OIC_LOG(DEBUG, TAG, "IN");
99     VERIFY_NON_NULL_VOID(data, TAG, "data");
100     VERIFY_NON_NULL_VOID(data->remoteEndpoint, TAG, "remoteEndpoint");
101
102     CAResult_t res = CA_STATUS_FAILED;
103
104     CASendDataType_t type = data->type;
105
106     if (SEND_TYPE_UNICAST == type)
107     {
108         coap_pdu_t *pdu = NULL;
109
110         if (NULL != data->requestInfo)
111         {
112             OIC_LOG(DEBUG, TAG, "reqInfo avlbl");
113
114             pdu = (coap_pdu_t *)CAGeneratePDU(data->requestInfo->method, &data->requestInfo->info);
115         }
116         else if (NULL != data->responseInfo)
117         {
118             OIC_LOG(DEBUG, TAG, "resInfo avlbl");
119
120             pdu = (coap_pdu_t *)CAGeneratePDU(data->responseInfo->result, &data->responseInfo->info);
121         }
122         else
123         {
124             OIC_LOG(DEBUG, TAG, "request info, response info is empty");
125         }
126
127         // interface controller function call.
128         if (NULL != pdu)
129         {
130             CALogPDUInfo(pdu);
131
132             res = CASendUnicastData(data->remoteEndpoint, pdu->hdr, pdu->length);
133             if (CA_STATUS_OK != res)
134             {
135                 OIC_LOG_V(ERROR, TAG, "send failed:%d", res);
136                 coap_delete_pdu(pdu);
137                 return;
138             }
139             // for retransmission
140             res = CARetransmissionSentData(&g_retransmissionContext, data->remoteEndpoint, pdu->hdr,
141                                            pdu->length);
142             if (CA_STATUS_OK != res)
143             {
144                 OIC_LOG_V(INFO, TAG, "retransmissions will not be working: %d", res);
145                 coap_delete_pdu(pdu);
146                 return;
147             }
148
149             coap_delete_pdu(pdu);
150         }
151     }
152     else if (SEND_TYPE_MULTICAST == type)
153     {
154         OIC_LOG(DEBUG, TAG, "both requestInfo & responseInfo is not available");
155
156         CAInfo_t *info = &data->requestInfo->info;
157
158         info->options = data->options;
159         info->numOptions = data->numOptions;
160
161         coap_pdu_t *pdu = (coap_pdu_t *)CAGeneratePDU(CA_GET, info);
162
163         if (NULL != pdu)
164         {
165             CALogPDUInfo(pdu);
166             res = CASendMulticastData(data->remoteEndpoint, pdu->hdr, pdu->length);
167             if(CA_STATUS_OK != res)
168             {
169                 OIC_LOG_V(ERROR, TAG, "send failed:%d", res);
170                 coap_delete_pdu(pdu);
171                 return;
172             }
173             coap_delete_pdu(pdu);
174         }
175     }
176
177     OIC_LOG(DEBUG, TAG, "OUT");
178 }
179
180 static void CAReceivedPacketCallback(CAEndpoint_t *endpoint, void *data, uint32_t dataLen)
181 {
182     OIC_LOG(DEBUG, TAG, "IN");
183     VERIFY_NON_NULL_VOID(data, TAG, "data");
184
185     uint32_t code = CA_NOT_FOUND;
186     coap_pdu_t *pdu = (coap_pdu_t *) CAParsePDU((const char *) data, dataLen, &code);
187     OICFree(data);
188     if (NULL == pdu)
189     {
190         OIC_LOG(ERROR, TAG, "Parse PDU failed");
191         return;
192     }
193
194     char uri[CA_MAX_URI_LENGTH] = { };
195
196     if (CA_GET == code || CA_POST == code || CA_PUT == code || CA_DELETE == code)
197     {
198         CARequestInfo_t *ReqInfo = (CARequestInfo_t *) OICCalloc(1, sizeof(CARequestInfo_t));
199         if (NULL == ReqInfo)
200         {
201             OIC_LOG(ERROR, TAG, "CAReceivedPacketCallback, Memory allocation failed!");
202             coap_delete_pdu(pdu);
203             return;
204         }
205
206         CAResult_t res = CAGetRequestInfoFromPDU(pdu, ReqInfo);
207         if (CA_STATUS_OK != res)
208         {
209             OIC_LOG_V(ERROR, TAG, "CAGetRequestInfoFromPDU failed : %d", res);
210             OICFree(ReqInfo);
211             coap_delete_pdu(pdu);
212             return;
213         }
214
215         if (NULL != ReqInfo->info.options)
216         {
217             for (uint32_t i = 0; i < ReqInfo->info.numOptions; i++)
218             {
219                 OIC_LOG_V(DEBUG, TAG, "optionID: %d", ReqInfo->info.options[i].optionID);
220
221                 OIC_LOG_V(DEBUG, TAG, "list: %s", ReqInfo->info.options[i].optionData);
222             }
223         }
224
225         if (NULL != ReqInfo->info.payload)
226         {
227             OIC_LOG_V(DEBUG, TAG, "Request- payload: %s", ReqInfo->info.payload);
228         }
229
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);
233
234         if (g_requestHandler)
235         {
236             g_requestHandler(endpoint, ReqInfo);
237         }
238
239         CADestroyRequestInfoInternal(ReqInfo);
240     }
241     else
242     {
243         CAResponseInfo_t *ResInfo = (CAResponseInfo_t *) OICCalloc(1, sizeof(CAResponseInfo_t));
244         if (NULL == ResInfo)
245         {
246             OIC_LOG(ERROR, TAG, "CAReceivedPacketCallback, Memory allocation failed!");
247             coap_delete_pdu(pdu);
248             return;
249         }
250
251         CAResult_t res = CAGetResponseInfoFromPDU(pdu, ResInfo);
252         if (CA_STATUS_OK != res)
253         {
254             OIC_LOG_V(ERROR, TAG, "CAGetResponseInfoFromPDU failed : %d", res);
255             OICFree(ResInfo);
256             coap_delete_pdu(pdu);
257             return;
258         }
259
260         if (NULL != ResInfo->info.options)
261         {
262             for (uint32_t i = 0; i < ResInfo->info.numOptions; i++)
263             {
264                 OIC_LOG_V(DEBUG, TAG, "optionID: %d", ResInfo->info.options[i].optionID);
265
266                 OIC_LOG_V(DEBUG, TAG, "list: %s", ResInfo->info.options[i].optionData);
267             }
268         }
269
270         if (NULL != ResInfo->info.payload)
271         {
272             OIC_LOG_V(DEBUG, TAG, "payload: %s", ResInfo->info.payload);
273         }
274         OIC_LOG_V(DEBUG, TAG, "code: %d", ResInfo->result);
275
276         // for retransmission
277         void *retransmissionPdu = NULL;
278         CARetransmissionReceivedData(&g_retransmissionContext, endpoint, pdu->hdr, pdu->length,
279                                      &retransmissionPdu);
280
281         // get token from saved data in retransmission list
282         if (retransmissionPdu && CA_EMPTY == code)
283         {
284             CAResult_t res = CAGetTokenFromPDU((const coap_hdr_t *)retransmissionPdu,
285                                                &(ResInfo->info));
286             if(CA_STATUS_OK != res)
287             {
288                 OIC_LOG(ERROR, TAG, "fail to get Token from retransmission list");
289                 OICFree(ResInfo->info.token);
290             }
291         }
292         OICFree(retransmissionPdu);
293
294         if (NULL != ResInfo)
295         {
296             if (g_responseHandler)
297             {
298                 g_responseHandler(endpoint, ResInfo);
299             }
300             CADestroyResponseInfoInternal(ResInfo);
301         }
302     }
303
304     if (pdu)
305     {
306         coap_delete_pdu(pdu);
307     }
308     OIC_LOG(DEBUG, TAG, "OUT");
309 }
310
311 static void CANetworkChangedCallback(CAEndpoint_t *info, CANetworkStatus_t status)
312 {
313     OIC_LOG(DEBUG, TAG, "IN");
314
315     OIC_LOG(DEBUG, TAG, "OUT");
316 }
317
318 void CAHandleRequestResponseCallbacks()
319 {
320     CAReadData();
321     CARetransmissionBaseRoutine((void *)&g_retransmissionContext);
322 }
323
324 CAResult_t CADetachRequestMessage(const CAEndpoint_t *object, const CARequestInfo_t *request)
325 {
326     OIC_LOG(DEBUG, TAG, "IN");
327
328     VERIFY_NON_NULL(object, TAG, "object");
329     VERIFY_NON_NULL(request, TAG, "request");
330
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))
333     {
334         OIC_LOG(ERROR, TAG, "max RT queue size reached!");
335         return CA_SEND_FAILED;
336     }
337
338     // allocate & initialize
339     CAData_t *data = (CAData_t *) OICCalloc(1, sizeof(CAData_t));
340     CA_MEMORY_ALLOC_CHECK(data);
341
342     // save data
343     data->type = request->isMulticast ? SEND_TYPE_MULTICAST : SEND_TYPE_UNICAST;
344     data->remoteEndpoint = object;
345     data->requestInfo = request;
346     data->responseInfo = NULL;
347
348     CAProcessData(data);
349     OICFree(data);
350     OIC_LOG(DEBUG, TAG, "OUT");
351     return CA_STATUS_OK;
352
353 // memory error label.
354 memory_error_exit:
355     OICFree(data);
356     OIC_LOG(DEBUG, TAG, "OUT");
357     return CA_MEMORY_ALLOC_FAILED;
358 }
359
360 CAResult_t CADetachResponseMessage(const CAEndpoint_t *object,
361                                    const CAResponseInfo_t *response)
362 {
363     OIC_LOG(DEBUG, TAG, "IN");
364     VERIFY_NON_NULL(object, TAG, "object");
365     VERIFY_NON_NULL(response, TAG, "response");
366
367     // allocate & initialize
368     CAData_t *data = (CAData_t *) OICCalloc(1, sizeof(CAData_t));
369     CA_MEMORY_ALLOC_CHECK(data);
370
371     // save data
372     data->type = SEND_TYPE_UNICAST;
373     data->remoteEndpoint = object;
374     data->requestInfo = NULL;
375     data->responseInfo = response;
376
377     CAProcessData(data);
378
379     OICFree(data);
380     OIC_LOG(DEBUG, TAG, "OUT");
381     return CA_STATUS_OK;
382
383 // memory error label.
384 memory_error_exit:
385     OICFree(data);
386     OIC_LOG(DEBUG, TAG, "OUT");
387
388     return CA_MEMORY_ALLOC_FAILED;
389 }
390
391 void CASetInterfaceCallbacks(CARequestCallback ReqHandler,
392                 CAResponseCallback RespHandler, CAErrorCallback errorHandler)
393 {
394     OIC_LOG(DEBUG, TAG, "IN");
395     g_requestHandler = ReqHandler;
396     g_responseHandler = RespHandler;
397     g_errorHandler = errorHandler;
398     OIC_LOG(DEBUG, TAG, "OUT");
399 }
400
401 CAResult_t CAInitializeMessageHandler()
402 {
403     OIC_LOG(DEBUG, TAG, "IN");
404     CASetPacketReceivedCallback(CAReceivedPacketCallback);
405
406     CASetNetworkChangeCallback(CANetworkChangedCallback);
407
408     // retransmission initialize
409     CARetransmissionInitialize(&g_retransmissionContext, NULL, CASendUnicastData,
410                                CATimeoutCallback, NULL);
411
412     CAInitializeAdapters(NULL);
413     OIC_LOG(DEBUG, TAG, "OUT");
414     return CA_STATUS_OK;
415 }
416
417 void CATerminateMessageHandler()
418 {
419     OIC_LOG(DEBUG, TAG, "IN");
420     // terminate interface adapters by controller
421     CATerminateAdapters();
422
423     // stop retransmission
424     CARetransmissionStop(&g_retransmissionContext);
425     CARetransmissionDestroy(&g_retransmissionContext);
426
427     OIC_LOG(DEBUG, TAG, "OUT");
428 }
429
430 void CALogPDUInfo(coap_pdu_t *pdu)
431 {
432     VERIFY_NON_NULL_VOID(pdu, TAG, "pdu");
433
434     OIC_LOG_V(DEBUG, TAG, "PDU Maker - payload : %s", pdu->data);
435
436     OIC_LOG_V(DEBUG, TAG, "PDU Maker - type : %d", pdu->hdr->type);
437
438     OIC_LOG_V(DEBUG, TAG, "PDU Maker - code : %d", pdu->hdr->code);
439
440     OIC_LOG_V(DEBUG, TAG, "PDU Maker - id : %d", ntohs(pdu->hdr->id));
441
442     OIC_LOG(DEBUG, TAG, "PDU Maker - token :");
443
444     OIC_LOG_BUFFER(DEBUG, TAG, pdu->hdr->token, pdu->hdr->token_length);
445 }
446