Removed Token ASCII-char limit and length limit
[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 "camessagehandler_singlethread.h"
22
23 #include <stdio.h>
24 #include <stdlib.h>
25 #include <string.h>
26 #include <stdint.h>
27
28 #include "cainterface.h"
29 #include "caremotehandler.h"
30 #include "cainterfacecontroller_singlethread.h"
31 #include "caprotocolmessage_singlethread.h"
32 #include "logger.h"
33 #include "config.h" /* for coap protocol */
34 #include "coap.h"
35 #include "oic_malloc.h"
36 #include "caadapterutils.h"
37
38 #define TAG "CAMH_ST"
39
40 #define CA_MEMORY_ALLOC_CHECK(arg) { if (arg == NULL) {OIC_LOG(ERROR, TAG, "Out of memory");\
41     goto memory_error_exit;} }
42
43 #define CA_MAX_RT_ARRAY_SIZE    3
44
45 typedef enum
46 {
47     SEND_TYPE_MULTICAST = 0, SEND_TYPE_UNICAST
48 } CASendDataType_t;
49
50 typedef struct
51 {
52     CASendDataType_t type;
53     CARemoteEndpoint_t *remoteEndpoint;
54     CARequestInfo_t *requestInfo;
55     CAResponseInfo_t *responseInfo;
56     CAHeaderOption_t *options;
57     uint8_t numOptions;
58 } CAData_t;
59
60
61 static CARetransmission_t g_retransmissionContext;
62
63 // handler field
64 static CARequestCallback g_requestHandler = NULL;
65 static CAResponseCallback g_responseHandler = NULL;
66
67 static void CAProcessData(CAData_t *data)
68 {
69     OIC_LOG(DEBUG, TAG, "IN");
70     VERIFY_NON_NULL_VOID(data, TAG, "data");
71     VERIFY_NON_NULL_VOID(data->remoteEndpoint, TAG, "remoteendpoint");
72
73     CAResult_t res = CA_STATUS_FAILED;
74
75     CASendDataType_t type = data->type;
76
77     if (type == SEND_TYPE_UNICAST)
78     {
79         coap_pdu_t *pdu = NULL;
80
81         if (data->requestInfo != NULL)
82         {
83             OIC_LOG(DEBUG, TAG, "reqInfo avlbl");
84
85             pdu = (coap_pdu_t *) CAGeneratePdu(data->remoteEndpoint->resourceUri,
86                                                data->requestInfo->method, data->requestInfo->info);
87         }
88         else if (data->responseInfo != NULL)
89         {
90             OIC_LOG(DEBUG, TAG, "resInfo avlbl");
91
92             pdu = (coap_pdu_t *) CAGeneratePdu(data->remoteEndpoint->resourceUri,
93                                                data->responseInfo->result,
94                                                data->responseInfo->info);
95         }
96         else
97         {
98             OIC_LOG(DEBUG, TAG, "request info, response info is empty");
99         }
100
101         // interface controller function call.
102         if (NULL != pdu)
103         {
104             CALogPDUData(pdu);
105
106             res = CASendUnicastData(data->remoteEndpoint, pdu->hdr, pdu->length);
107             if(CA_STATUS_OK != res)
108             {
109                 OIC_LOG_V(ERROR, TAG, "send failed:%d", res);
110                 coap_delete_pdu(pdu);
111                 return;
112             }
113             // for retransmission
114             CARetransmissionSentData(&g_retransmissionContext, data->remoteEndpoint, pdu->hdr,
115                                      pdu->length);
116         }
117         coap_delete_pdu(pdu);
118     }
119     else if (type == SEND_TYPE_MULTICAST)
120     {
121         OIC_LOG(DEBUG, TAG, "both requestInfo & responseInfo is not available");
122
123         coap_pdu_t *pdu = NULL;
124         CAInfo_t info = { 0 };
125
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;
131
132         pdu = (coap_pdu_t *) CAGeneratePdu(data->remoteEndpoint->resourceUri, CA_GET, info);
133
134         if (NULL != pdu)
135         {
136             CALogPDUData(pdu);
137             res = CASendMulticastData(pdu->hdr, pdu->length);
138             if(CA_STATUS_OK != res)
139             {
140                 OIC_LOG_V(ERROR, TAG, "send failed:%d", res);
141                 coap_delete_pdu(pdu);
142                 return;
143             }
144         }
145         coap_delete_pdu(pdu);
146     }
147
148     OIC_LOG(DEBUG, TAG, "OUT");
149 }
150
151 static void CATimeoutCallback(const CARemoteEndpoint_t *endpoint, const void *pdu, uint32_t size)
152 {
153     OIC_LOG(DEBUG, TAG, "IN");
154     CARemoteEndpoint_t* ep = CACloneRemoteEndpoint(endpoint);
155     if (ep == NULL)
156     {
157         OIC_LOG(ERROR, TAG, "clone failed");
158         return;
159     }
160
161     CAResponseInfo_t* resInfo = (CAResponseInfo_t*) OICCalloc(1, sizeof(CAResponseInfo_t));
162
163     if (resInfo == NULL)
164     {
165         OIC_LOG(ERROR, TAG, "calloc failed");
166         CADestroyRemoteEndpointInternal(ep);
167         return;
168     }
169
170     resInfo->result = CA_RETRANSMIT_TIMEOUT;
171     resInfo->info.type = CAGetMessageTypeFromPduBinaryData(pdu, size);;
172     resInfo->info.messageId = CAGetMessageIdFromPduBinaryData(pdu, size);;
173
174     if (g_responseHandler)
175     {
176         g_responseHandler(ep, resInfo);
177     }
178
179     CADestroyRemoteEndpointInternal(ep);
180     OICFree(resInfo);
181     OIC_LOG(DEBUG, TAG, "OUT");
182 }
183
184 static void CAReceivedPacketCallback(CARemoteEndpoint_t *endpoint, void *data,
185     uint32_t dataLen)
186 {
187     OIC_LOG(DEBUG, TAG, "IN");
188     VERIFY_NON_NULL_VOID(data, TAG, "data");
189
190     uint32_t code = CA_NOT_FOUND;
191     coap_pdu_t *pdu = (coap_pdu_t *) CAParsePDU((const char *) data, dataLen, &code);
192     if (NULL == pdu)
193     {
194         OIC_LOG(ERROR, TAG, "Parse PDU failed");
195         return;
196     }
197     //OICFree(data);
198
199     char uri[CA_MAX_URI_LENGTH] = { 0, };
200     uint32_t bufLen = sizeof(uri);
201
202     if (CA_GET == code || CA_POST == code  || CA_PUT == code  || CA_DELETE == code )
203     {
204         CARequestInfo_t *ReqInfo = (CARequestInfo_t *) OICCalloc(1, sizeof(CARequestInfo_t));
205         if (ReqInfo == NULL)
206         {
207             OIC_LOG(DEBUG, TAG, "CAReceivedPacketCallback, Memory allocation failed!");
208             coap_delete_pdu(pdu);
209             return;
210         }
211
212         CAGetRequestInfoFromPdu(pdu, ReqInfo, uri, bufLen);
213         if (NULL != ReqInfo->info.options)
214         {
215             uint32_t i;
216             for (i = 0; i < ReqInfo->info.numOptions; i++)
217             {
218                 OIC_LOG_V(DEBUG, TAG, "Request- optionID: %d", ReqInfo->info.options[i].optionID);
219
220                 OIC_LOG_V(DEBUG, TAG, "Request- list: %s", ReqInfo->info.options[i].optionData);
221             }
222         }
223
224         if (NULL != ReqInfo->info.payload)
225         {
226             OIC_LOG_V(DEBUG, TAG, "Request- payload: %s", ReqInfo->info.payload);
227         }
228
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);
232
233         if (NULL != endpoint)
234         {
235             endpoint->resourceUri = (char *) OICCalloc(bufLen + 1, sizeof(char));
236             if (endpoint->resourceUri == NULL)
237             {
238                 OIC_LOG(DEBUG, TAG, "CAReceivedPacketCallback, Memory allocation failed!");
239                 coap_delete_pdu(pdu);
240                 OICFree(ReqInfo);
241                 return;
242             }
243             memcpy(endpoint->resourceUri, uri, bufLen);
244             OIC_LOG_V(DEBUG, TAG, "URI : %s", endpoint->resourceUri);
245         }
246
247         if (ReqInfo)
248         {
249             if (g_requestHandler)
250             {
251                 g_requestHandler(endpoint, ReqInfo);
252             }
253
254             CADestroyRequestInfoInternal(ReqInfo);
255         }
256     }
257     else
258     {
259         CAResponseInfo_t *ResInfo = (CAResponseInfo_t *) OICCalloc(1, sizeof(CAResponseInfo_t));
260         if (ResInfo == NULL)
261         {
262             OIC_LOG(DEBUG, TAG, "CAReceivedPacketCallback, Memory allocation failed!");
263             coap_delete_pdu(pdu);
264             return;
265         }
266
267         CAGetResponseInfoFromPdu(pdu, ResInfo, uri, bufLen);
268
269         if (NULL != ResInfo->info.options)
270         {
271             uint32_t i;
272             for (i = 0; i < ResInfo->info.numOptions; i++)
273             {
274                 OIC_LOG_V(DEBUG, TAG, "optionID: %d", ResInfo->info.options[i].optionID);
275
276                 OIC_LOG_V(DEBUG, TAG, "list: %s", ResInfo->info.options[i].optionData);
277             }
278
279             if (NULL != ResInfo->info.payload)
280             {
281                 OIC_LOG_V(DEBUG, TAG, "payload: %s", ResInfo->info.payload);
282             }
283
284             OIC_LOG_V(DEBUG, TAG, "code: %d", ResInfo->result);
285         }
286
287         if (NULL != endpoint)
288         {
289             endpoint->resourceUri = (char *) OICCalloc(bufLen + 1, sizeof(char));
290             if (endpoint->resourceUri == NULL)
291             {
292                 OIC_LOG(DEBUG, TAG, "CAReceivedPacketCallback, Memory allocation failed!");
293                 coap_delete_pdu(pdu);
294                 OICFree(ResInfo);
295                 return;
296             }
297             memcpy(endpoint->resourceUri, uri, bufLen);
298             OIC_LOG_V(DEBUG, TAG, "URI : %s", endpoint->resourceUri);
299         }
300
301         if (ResInfo != NULL)
302         {
303             if (g_responseHandler)
304             {
305                 g_responseHandler(endpoint, ResInfo);
306
307                 // for retransmission
308                 CARetransmissionReceivedData(&g_retransmissionContext, endpoint, pdu->hdr,
309                                              pdu->length);
310             }
311             CADestroyResponseInfoInternal(ResInfo);
312         }
313     }
314
315     if (endpoint && endpoint->resourceUri)
316     {
317         OICFree(endpoint->resourceUri);
318         endpoint->resourceUri = NULL;
319     }
320     coap_delete_pdu(pdu);
321     OIC_LOG(DEBUG, TAG, "OUT");
322 }
323
324 static void CANetworkChangedCallback(CALocalConnectivity_t *info, CANetworkStatus_t status)
325 {
326     OIC_LOG(DEBUG, TAG, "IN");
327     OIC_LOG(DEBUG, TAG, "OUT");
328 }
329
330 void CAHandleRequestResponseCallbacks()
331 {
332     CAReadData();
333     CARetransmissionBaseRoutine((void *)&g_retransmissionContext);
334 }
335
336 CAResult_t CADetachRequestMessage(const CARemoteEndpoint_t *object,
337     const CARequestInfo_t *request)
338 {
339     OIC_LOG(DEBUG, TAG, "IN");
340
341     VERIFY_NON_NULL(object, TAG, "object");
342     VERIFY_NON_NULL(request, TAG, "request");
343
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))
346     {
347         OIC_LOG(ERROR, TAG, "max RT queue size reached!");
348         return CA_SEND_FAILED;
349     }
350
351     // allocate & initialize
352     CAData_t *data = (CAData_t *) OICCalloc(1, sizeof(CAData_t));
353     CA_MEMORY_ALLOC_CHECK(data);
354
355     // save data
356     data->type = SEND_TYPE_UNICAST;
357     data->remoteEndpoint = object;
358     data->requestInfo = request;
359     data->responseInfo = NULL;
360
361     CAProcessData(data);
362     OICFree(data);
363     OIC_LOG(DEBUG, TAG, "OUT");
364     return CA_STATUS_OK;
365
366     // memory error label.
367 memory_error_exit:
368     OIC_LOG(DEBUG, TAG, "OUT");
369     return CA_MEMORY_ALLOC_FAILED;
370 }
371
372 CAResult_t CADetachRequestToAllMessage(const CAGroupEndpoint_t *object,
373                                        const CARequestInfo_t *request)
374 {
375     OIC_LOG(DEBUG, TAG, "IN");
376
377     if (object == NULL || request == NULL)
378     {
379         return CA_STATUS_FAILED;
380     }
381
382     // allocate & initialize
383     CAData_t *data = (CAData_t *) OICCalloc(1, sizeof(CAData_t));
384     CA_MEMORY_ALLOC_CHECK(data);
385
386     CAAddress_t addr = {0};
387     CARemoteEndpoint_t *remoteEndpoint = CACreateRemoteEndpointInternal(object->resourceUri, addr,
388                                                                         object->connectivityType);
389
390     // save data
391     data->type = SEND_TYPE_MULTICAST;
392     data->remoteEndpoint = remoteEndpoint;
393     data->requestInfo = request;
394     data->responseInfo = NULL;
395
396     CAProcessData(data);
397     CADestroyRemoteEndpoint(remoteEndpoint);
398     OICFree(data);
399     OIC_LOG(DEBUG, TAG, "OUT");
400     return CA_STATUS_OK;
401
402     // memory error label.
403 memory_error_exit:
404
405     OIC_LOG(DEBUG, TAG, "OUT");
406     return CA_MEMORY_ALLOC_FAILED;
407 }
408
409 CAResult_t CADetachResponseMessage(const CARemoteEndpoint_t *object,
410                                    const CAResponseInfo_t *response)
411 {
412     OIC_LOG(DEBUG, TAG, "IN");
413     VERIFY_NON_NULL(object, TAG, "object");
414     VERIFY_NON_NULL(response, TAG, "response");
415
416     // allocate & initialize
417     CAData_t *data = (CAData_t *) OICCalloc(1, sizeof(CAData_t));
418     CA_MEMORY_ALLOC_CHECK(data);
419
420     // save data
421     data->type = SEND_TYPE_UNICAST;
422     data->remoteEndpoint = object;
423     data->requestInfo = NULL;
424     data->responseInfo = response;
425
426     CAProcessData(data);
427     OICFree(data);
428     OIC_LOG(DEBUG, TAG, "OUT");
429     return CA_STATUS_OK;
430
431     // memory error label.
432 memory_error_exit:
433     OIC_LOG(DEBUG, TAG, "OUT");
434     return CA_MEMORY_ALLOC_FAILED;
435 }
436
437 CAResult_t CADetachMessageResourceUri(const CAURI_t resourceUri, const CAToken_t token,
438                                       uint8_t tokenLength, const CAHeaderOption_t *options,
439                                       uint8_t numOptions)
440 {
441     OIC_LOG(DEBUG, TAG, "IN");
442     VERIFY_NON_NULL(resourceUri, TAG, "resourceUri is NULL");
443     VERIFY_NON_NULL(token, TAG, "Token is NULL");
444
445     // allocate & initialize
446     CAData_t *data = (CAData_t *) OICCalloc(1, sizeof(CAData_t));
447     CA_MEMORY_ALLOC_CHECK(data);
448
449     CAAddress_t addr = {0};
450     CARemoteEndpoint_t *remoteEndpoint =
451             CACreateRemoteEndpointInternal(resourceUri, addr,
452                                            CA_ETHERNET | CA_WIFI | CA_EDR | CA_LE);
453
454     // create request info
455     CARequestInfo_t *reqInfo = (CARequestInfo_t *) OICCalloc(1, sizeof(CARequestInfo_t));
456     CA_MEMORY_ALLOC_CHECK(reqInfo);
457
458     // save request info data
459     reqInfo->method = CA_GET;
460     reqInfo->info.type = CA_MSG_NONCONFIRM;
461
462     reqInfo->info.token = token;
463     reqInfo->info.tokenLength = tokenLength;
464
465     // save data
466     data->type = SEND_TYPE_MULTICAST;
467     data->remoteEndpoint = remoteEndpoint;
468     data->requestInfo = reqInfo;
469
470     data->responseInfo = NULL;
471     data->options = NULL;
472     data->numOptions = 0;
473     CAHeaderOption_t *headerOption = NULL;
474     if (NULL != options && numOptions > 0)
475     {
476         // copy data
477         headerOption = (CAHeaderOption_t *) OICMalloc(sizeof(CAHeaderOption_t) * numOptions);
478         CA_MEMORY_ALLOC_CHECK(headerOption);
479         memcpy(headerOption, options, sizeof(CAHeaderOption_t) * numOptions);
480
481         data->options = headerOption;
482         data->numOptions = numOptions;
483     }
484
485     CAProcessData(data);
486
487     CADestroyRemoteEndpoint(remoteEndpoint);
488     OICFree(headerOption);
489     OICFree(data);
490     OICFree(reqInfo);
491     OIC_LOG(DEBUG, TAG, "OUT");
492     return CA_STATUS_OK;
493
494     // memory error label.
495 memory_error_exit:
496
497     CADestroyRemoteEndpointInternal(remoteEndpoint);
498
499     OICFree(data);
500     OIC_LOG(DEBUG, TAG, "OUT");
501     return CA_MEMORY_ALLOC_FAILED;
502 }
503
504 void CASetRequestResponseCallbacks(CARequestCallback ReqHandler,
505     CAResponseCallback RespHandler)
506 {
507     OIC_LOG(DEBUG, TAG, "IN");
508     g_requestHandler = ReqHandler;
509     g_responseHandler = RespHandler;
510     OIC_LOG(DEBUG, TAG, "OUT");
511 }
512
513 CAResult_t CAInitializeMessageHandler()
514 {
515     OIC_LOG(DEBUG, TAG, "IN");
516     CASetPacketReceivedCallback(CAReceivedPacketCallback);
517
518     CASetNetworkChangeCallback(CANetworkChangedCallback);
519
520     CAResult_t res;
521
522     // retransmission initialize
523     CARetransmissionInitialize(&g_retransmissionContext, CASendUnicastData, CATimeoutCallback, NULL);
524
525     CAInitializeAdapters();
526     OIC_LOG(DEBUG, TAG, "OUT");
527     return CA_STATUS_OK;
528 }
529
530 void CATerminateMessageHandler()
531 {
532     OIC_LOG(DEBUG, TAG, "IN");
533     // terminate interface adapters by controller
534     CATerminateAdapters();
535
536     // stop retransmission
537     CARetransmissionStop(&g_retransmissionContext);
538     CARetransmissionDestroy(&g_retransmissionContext);
539
540     OIC_LOG(DEBUG, TAG, "OUT");
541 }
542
543