[IOT-583] fixed Option Parameter is not support
[platform/upstream/iotivity.git] / resource / csdk / connectivity / src / cainterfacecontroller_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 "cainterfacecontroller_singlethread.h"
22
23 #include <stdio.h>
24 #include <stdlib.h>
25 #include <string.h>
26 #include <stdint.h>
27
28 #include "caipadapter_singlethread.h"
29 #include "caedradapter_singlethread.h"
30 #include "caleadapter_singlethread.h"
31 #include "caadapterutils.h"
32
33 #include "canetworkconfigurator.h"
34 #include "oic_malloc.h"
35 #include "logger.h"
36
37 #define TAG "CAIFCNT_ST"
38
39 #define CA_MEMORY_ALLOC_CHECK(arg) { if (arg == NULL) {OIC_LOG(ERROR, TAG, "Out of memory");\
40     goto memory_error_exit;} }
41
42 #define CA_CONNECTIVITY_TYPE_NUM   4
43
44 static CAConnectivityHandler_t g_adapterHandler[CA_CONNECTIVITY_TYPE_NUM];
45
46 static CANetworkPacketReceivedCallback g_networkPacketReceivedCallback = NULL;
47
48 static CANetworkChangeCallback g_networkChangeCallback = NULL;
49
50 static CAErrorHandleCallback g_errorHandleCallback = NULL;
51
52 static int CAGetAdapterIndex(CATransportAdapter_t cType)
53 {
54     switch (cType)
55     {
56         case CA_ADAPTER_IP:
57             return 0;
58         case CA_ADAPTER_GATT_BTLE:
59             return 1;
60         case CA_ADAPTER_RFCOMM_BTEDR:
61             return 2;
62     }
63
64     OIC_LOG(DEBUG, TAG, "CA_CONNECTIVITY_TYPE_NUM is not 4");
65
66     return -1;
67 }
68
69 static void CARegisterCallback(CAConnectivityHandler_t handler, CATransportAdapter_t cType)
70 {
71     OIC_LOG(DEBUG, TAG, "IN");
72
73     if(handler.startAdapter == NULL ||
74         handler.startListenServer == NULL ||
75         handler.startDiscoveryServer == NULL ||
76         handler.sendData == NULL ||
77         handler.sendDataToAll == NULL ||
78         handler.GetnetInfo == NULL ||
79         handler.readData == NULL ||
80         handler.stopAdapter == NULL ||
81         handler.terminate == NULL)
82     {
83         OIC_LOG(ERROR, TAG, "connectivity handler is not enough to be used!");
84         return;
85     }
86
87     int index = CAGetAdapterIndex(cType);
88
89     if (index == -1)
90     {
91         OIC_LOG(DEBUG, TAG, "unknown connectivity type!");
92         return;
93     }
94
95     g_adapterHandler[index] = handler;
96
97     OIC_LOG_V(DEBUG, TAG, "%d type adapter", cType);
98     OIC_LOG(DEBUG, TAG, "OUT");
99 }
100
101 static void CAReceivedPacketCallback(CAEndpoint_t *endpoint, void *data,
102     uint32_t dataLen)
103 {
104     OIC_LOG(DEBUG, TAG, "IN");
105
106     // Call the callback.
107     if (g_networkPacketReceivedCallback != NULL)
108     {
109         g_networkPacketReceivedCallback(endpoint, data, dataLen);
110     }
111     OIC_LOG(DEBUG, TAG, "OUT");
112 }
113
114 void CASetErrorHandleCallback(CAErrorHandleCallback errorCallback)
115 {
116     OIC_LOG(DEBUG, TAG, "Set error handle callback");
117     g_errorHandleCallback = errorCallback;
118 }
119
120 static void CANetworkChangedCallback(CAEndpoint_t *info, CANetworkStatus_t status)
121 {
122     OIC_LOG(DEBUG, TAG, "IN");
123
124     // Call the callback.
125     if (g_networkChangeCallback != NULL)
126     {
127         g_networkChangeCallback(info, status);
128     }
129     OIC_LOG(DEBUG, TAG, "OUT");
130 }
131
132 void CAInitializeAdapters()
133 {
134     OIC_LOG(DEBUG, TAG, "IN");
135
136     memset(g_adapterHandler, 0, sizeof(CAConnectivityHandler_t) * CA_CONNECTIVITY_TYPE_NUM);
137
138     // Initialize adapters and register callback.
139 #ifdef IP_ADAPTER
140     CAInitializeIP(CARegisterCallback, CAReceivedPacketCallback, CANetworkChangedCallback);
141 #endif /* IP_ADAPTER */
142
143 #ifdef EDR_ADAPTER
144     CAInitializeEDR(CARegisterCallback, CAReceivedPacketCallback, CANetworkChangedCallback);
145 #endif /* EDR_ADAPTER */
146
147 #ifdef LE_ADAPTER
148     CAInitializeLE(CARegisterCallback, CAReceivedPacketCallback, CANetworkChangedCallback);
149 #endif /* LE_ADAPTER */
150
151     OIC_LOG(DEBUG, TAG, "OUT");
152 }
153
154 void CASetPacketReceivedCallback(CANetworkPacketReceivedCallback callback)
155 {
156     OIC_LOG(DEBUG, TAG, "IN");
157
158     g_networkPacketReceivedCallback = callback;
159     OIC_LOG(DEBUG, TAG, "OUT");
160 }
161
162 void CASetNetworkChangeCallback(CANetworkChangeCallback callback)
163 {
164     OIC_LOG(DEBUG, TAG, "IN");
165
166     g_networkChangeCallback = callback;
167     OIC_LOG(DEBUG, TAG, "OUT");
168 }
169
170 CAResult_t CAStartAdapter(CATransportAdapter_t transportType)
171 {
172     OIC_LOG_V(DEBUG, TAG, "transportType[%d]", transportType);
173
174     int index = CAGetAdapterIndex(transportType);
175
176     if (index == -1)
177     {
178         OIC_LOG(DEBUG, TAG, "unknown connectivity type!");
179         return CA_STATUS_FAILED;
180     }
181
182     if (g_adapterHandler[index].startAdapter != NULL)
183     {
184         g_adapterHandler[index].startAdapter();
185     }
186     OIC_LOG(DEBUG, TAG, "OUT");
187     return CA_STATUS_OK;
188 }
189
190 void CAStopAdapter(CATransportAdapter_t transportType)
191 {
192     OIC_LOG_V(DEBUG, TAG, "transportType[%d]", transportType);
193
194     int index = CAGetAdapterIndex(transportType);
195
196     if (index == -1)
197     {
198         OIC_LOG(DEBUG, TAG, "unknown connectivity type!");
199         return;
200     }
201
202     if (g_adapterHandler[index].stopAdapter != NULL)
203     {
204         g_adapterHandler[index].stopAdapter();
205     }
206     OIC_LOG(DEBUG, TAG, "OUT");
207 }
208
209 CAResult_t CAGetNetworkInfo(CAEndpoint_t **info, uint32_t *size)
210 {
211     OIC_LOG(DEBUG, TAG, "IN");
212     VERIFY_NON_NULL(info, TAG, "info");
213     VERIFY_NON_NULL(size, TAG, "size");
214
215     CAEndpoint_t *tempInfo[CA_CONNECTIVITY_TYPE_NUM] = { 0 };
216     uint32_t tempSize[CA_CONNECTIVITY_TYPE_NUM] = { 0 };
217
218     CAResult_t res = CA_STATUS_FAILED;
219     // #1. get information each adapter
220     for (uint8_t index = 0; index < CA_CONNECTIVITY_TYPE_NUM; index++)
221     {
222         if (g_adapterHandler[index].GetnetInfo != NULL)
223         {
224             res = g_adapterHandler[index].GetnetInfo(&tempInfo[index], &tempSize[index]);
225
226             OIC_LOG_V (DEBUG, TAG, "%d adapter network info size is %d", index, tempSize[index]);
227         }
228     }
229
230     uint32_t resSize = 0;
231     for (uint8_t index = 0; index < CA_CONNECTIVITY_TYPE_NUM; index++)
232     {
233         // check information
234         if (tempInfo[index] == NULL || tempSize[index] <= 0)
235         {
236             continue;
237         }
238
239         // #2. total size
240         resSize += tempSize[index];
241     }
242
243     OIC_LOG_V(DEBUG, TAG, "network info total size is %d!", resSize);
244
245     if (resSize <= 0)
246     {
247         if (CA_ADAPTER_NOT_ENABLED == res || CA_NOT_SUPPORTED == res)
248         {
249             return res;
250         }
251         return CA_STATUS_FAILED;
252     }
253
254     // #3. add data into result
255     // memory allocation
256     CAEndpoint_t *resInfo = (CAEndpoint_t *)OICCalloc(resSize, sizeof(CAEndpoint_t));
257     CA_MEMORY_ALLOC_CHECK(resInfo);
258
259     uint8_t i = 0;
260     for (uint8_t index = 0; index < CA_CONNECTIVITY_TYPE_NUM; index++)
261     {
262         // check information
263         if (tempInfo[index] == NULL || tempSize[index] <= 0)
264         {
265             continue;
266         }
267
268         memcpy(resInfo + i, tempInfo[index], sizeof(CAEndpoint_t) * tempSize[index]);
269
270         i += tempSize[index];
271
272         // free adapter data
273         OICFree(tempInfo[index]);
274     }
275
276     // #5. save data
277     *info = resInfo;
278     *size = resSize;
279
280     OIC_LOG(DEBUG, TAG, "OUT");
281
282     return res;
283
284     // memory error label.
285 memory_error_exit:
286
287     for (uint8_t index = 0; index < CA_CONNECTIVITY_TYPE_NUM; index++)
288     {
289
290         OICFree(tempInfo[index]);
291     }
292
293     return CA_MEMORY_ALLOC_FAILED;
294 }
295
296 CAResult_t CASendUnicastData(const CAEndpoint_t *endpoint, const void *data, uint32_t length)
297 {
298     OIC_LOG(DEBUG, TAG, "IN");
299
300     CAResult_t res = CA_STATUS_FAILED;
301
302     if (endpoint == NULL)
303     {
304         OIC_LOG(DEBUG, TAG, "Endpoint is NULL");
305         return CA_STATUS_INVALID_PARAM;
306     }
307
308     CATransportAdapter_t type = endpoint->adapter;
309
310     int index = CAGetAdapterIndex(type);
311
312     if (index == -1)
313     {
314         OIC_LOG(DEBUG, TAG, "unknown connectivity type!");
315         return CA_STATUS_INVALID_PARAM;
316     }
317
318     uint32_t sentDataLen = 0;
319     if (g_adapterHandler[index].sendData != NULL)
320     {
321         sentDataLen = g_adapterHandler[index].sendData(endpoint, data, length);
322     }
323
324     if (sentDataLen == length)
325     {
326         res = CA_STATUS_OK;
327     }
328
329     OIC_LOG(DEBUG, TAG, "OUT");
330     return res;
331 }
332
333 CAResult_t CASendMulticastData(const CAEndpoint_t *endpoint, const void *data, uint32_t length)
334 {
335     OIC_LOG(DEBUG, TAG, "IN");
336
337     CAResult_t res = CA_STATUS_FAILED;
338     u_arraylist_t *list = CAGetSelectedNetworkList();
339
340     if (!list)
341     {
342         OIC_LOG(DEBUG, TAG, "No selected network");
343         return res;
344     }
345
346     for (uint8_t i = 0; i < u_arraylist_length(list); i++)
347     {
348         void* ptrType = u_arraylist_get(list, i);
349         if (NULL == ptrType)
350         {
351             continue;
352         }
353
354         CATransportAdapter_t connType = *(CATransportAdapter_t *) ptrType;
355
356         int index = CAGetAdapterIndex(connType);
357
358         if (index == -1)
359         {
360             OIC_LOG(DEBUG, TAG, "unknown connectivity type!");
361             continue;
362         }
363
364         uint32_t sentDataLen = 0;
365         if (g_adapterHandler[index].sendDataToAll != NULL)
366         {
367             sentDataLen = g_adapterHandler[index].sendDataToAll(endpoint, data, length);
368         }
369
370         if (sentDataLen == length)
371         {
372             res = CA_STATUS_OK;
373         }
374     }
375     OIC_LOG(DEBUG, TAG, "OUT");
376     return res;
377 }
378
379 CAResult_t CAStartListeningServerAdapters()
380 {
381     OIC_LOG(DEBUG, TAG, "IN");
382
383     u_arraylist_t *list = CAGetSelectedNetworkList();
384
385     if (!list)
386     {
387         OIC_LOG(DEBUG, TAG, "No selected network");
388         return CA_STATUS_FAILED;
389     }
390
391     for (uint8_t i = 0; i < u_arraylist_length(list); i++)
392     {
393         void* ptrType = u_arraylist_get(list, i);
394         if (NULL == ptrType)
395         {
396             OIC_LOG(ERROR, TAG, "Invalid conn type");
397             continue;
398         }
399
400         CATransportAdapter_t connType = *(CATransportAdapter_t *) ptrType;
401
402         int index = CAGetAdapterIndex(connType);
403
404         if (index == -1)
405         {
406             OIC_LOG(DEBUG, TAG, "unknown connectivity type!");
407             continue;
408         }
409
410         if (g_adapterHandler[index].startListenServer != NULL)
411         {
412             g_adapterHandler[index].startListenServer();
413         }
414     }
415     OIC_LOG(DEBUG, TAG, "OUT");
416     return CA_STATUS_OK;
417 }
418
419 CAResult_t CAStartDiscoveryServerAdapters()
420 {
421     OIC_LOG(DEBUG, TAG, "IN");
422
423     u_arraylist_t *list = CAGetSelectedNetworkList();
424
425     if (!list)
426     {
427         OIC_LOG(DEBUG, TAG, "No selected network");
428         return CA_STATUS_FAILED;
429     }
430
431     for (uint8_t i = 0; i < u_arraylist_length(list); i++)
432     {
433         void* ptrType = u_arraylist_get(list, i);
434         if (NULL == ptrType)
435         {
436             continue;
437         }
438
439         CATransportAdapter_t connType = *(CATransportAdapter_t *) ptrType;
440
441         int index = CAGetAdapterIndex(connType);
442
443         if (index == -1)
444         {
445             OIC_LOG(DEBUG, TAG, "unknown connectivity type!");
446             continue;
447         }
448
449         if (g_adapterHandler[index].startDiscoveryServer != NULL)
450         {
451             g_adapterHandler[index].startDiscoveryServer();
452         }
453     }
454     OIC_LOG(DEBUG, TAG, "OUT");
455     return CA_STATUS_OK;
456 }
457
458 void CATerminateAdapters()
459 {
460     OIC_LOG(DEBUG, TAG, "IN");
461
462     uint8_t index;
463
464     for (index = 0; index < CA_CONNECTIVITY_TYPE_NUM; index++)
465     {
466         if (g_adapterHandler[index].stopAdapter != NULL)
467         {
468             g_adapterHandler[index].stopAdapter();
469         }
470         if (g_adapterHandler[index].terminate != NULL)
471         {
472             g_adapterHandler[index].terminate();
473         }
474     }
475
476     OIC_LOG(DEBUG, TAG, "OUT");
477 }
478
479 CAResult_t CAReadData()
480 {
481     OIC_LOG(DEBUG, TAG, "IN");
482     u_arraylist_t *list = CAGetSelectedNetworkList();
483
484     if (!list)
485     {
486         return CA_STATUS_FAILED;
487     }
488
489     for (uint8_t i = 0; i < u_arraylist_length(list); i++)
490     {
491         void *ptrType = u_arraylist_get(list, i);
492         if (NULL == ptrType)
493         {
494             OIC_LOG(ERROR, TAG, "get list fail");
495             return CA_STATUS_FAILED;
496         }
497
498         CATransportAdapter_t connType = *(CATransportAdapter_t *)ptrType;
499
500         int index = CAGetAdapterIndex(connType);
501
502         if (-1 == index)
503         {
504             OIC_LOG(DEBUG, TAG, "unknown connectivity type!");
505             continue;
506         }
507
508         if (g_adapterHandler[index].readData != NULL)
509         {
510             g_adapterHandler[index].readData();
511         }
512     }
513
514     OIC_LOG(DEBUG, TAG, "OUT");
515     return CA_STATUS_OK;
516 }
517