Integrated singlethread and multithread files of BLE adapter.
[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.h"
29 #include "caedradapter_singlethread.h"
30 #include "caleadapter.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   3
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 3");
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                    NULL, NULL);
142 #endif /* IP_ADAPTER */
143
144 #ifdef EDR_ADAPTER
145     CAInitializeEDR(CARegisterCallback, CAReceivedPacketCallback, CANetworkChangedCallback);
146 #endif /* EDR_ADAPTER */
147
148 #ifdef LE_ADAPTER
149     CAInitializeLE(CARegisterCallback, CAReceivedPacketCallback, CANetworkChangedCallback,
150                    NULL, NULL);
151 #endif /* LE_ADAPTER */
152
153     OIC_LOG(DEBUG, TAG, "OUT");
154 }
155
156 void CASetPacketReceivedCallback(CANetworkPacketReceivedCallback callback)
157 {
158     OIC_LOG(DEBUG, TAG, "IN");
159
160     g_networkPacketReceivedCallback = callback;
161     OIC_LOG(DEBUG, TAG, "OUT");
162 }
163
164 void CASetNetworkChangeCallback(CANetworkChangeCallback callback)
165 {
166     OIC_LOG(DEBUG, TAG, "IN");
167
168     g_networkChangeCallback = callback;
169     OIC_LOG(DEBUG, TAG, "OUT");
170 }
171
172 CAResult_t CAStartAdapter(CATransportAdapter_t transportType)
173 {
174     OIC_LOG_V(DEBUG, TAG, "transportType[%d]", transportType);
175
176     int index = CAGetAdapterIndex(transportType);
177
178     if (index == -1)
179     {
180         OIC_LOG(DEBUG, TAG, "unknown connectivity type!");
181         return CA_STATUS_FAILED;
182     }
183
184     if (g_adapterHandler[index].startAdapter != NULL)
185     {
186         g_adapterHandler[index].startAdapter();
187     }
188     OIC_LOG(DEBUG, TAG, "OUT");
189     return CA_STATUS_OK;
190 }
191
192 void CAStopAdapter(CATransportAdapter_t transportType)
193 {
194     OIC_LOG_V(DEBUG, TAG, "transportType[%d]", transportType);
195
196     int index = CAGetAdapterIndex(transportType);
197
198     if (index == -1)
199     {
200         OIC_LOG(DEBUG, TAG, "unknown connectivity type!");
201         return;
202     }
203
204     if (g_adapterHandler[index].stopAdapter != NULL)
205     {
206         g_adapterHandler[index].stopAdapter();
207     }
208     OIC_LOG(DEBUG, TAG, "OUT");
209 }
210
211 CAResult_t CAGetNetworkInfo(CAEndpoint_t **info, uint32_t *size)
212 {
213     OIC_LOG(DEBUG, TAG, "IN");
214     VERIFY_NON_NULL(info, TAG, "info");
215     VERIFY_NON_NULL(size, TAG, "size");
216
217     CAEndpoint_t *tempInfo[CA_CONNECTIVITY_TYPE_NUM] = { 0 };
218     uint32_t tempSize[CA_CONNECTIVITY_TYPE_NUM] = { 0 };
219
220     CAResult_t res = CA_STATUS_FAILED;
221     // #1. get information each adapter
222     for (uint8_t index = 0; index < CA_CONNECTIVITY_TYPE_NUM; index++)
223     {
224         if (g_adapterHandler[index].GetnetInfo != NULL)
225         {
226             res = g_adapterHandler[index].GetnetInfo(&tempInfo[index], &tempSize[index]);
227
228             OIC_LOG_V (DEBUG, TAG, "%d adapter network info size is %d", index, tempSize[index]);
229         }
230     }
231
232     uint32_t resSize = 0;
233     for (uint8_t index = 0; index < CA_CONNECTIVITY_TYPE_NUM; index++)
234     {
235         // check information
236         if (tempInfo[index] == NULL || tempSize[index] <= 0)
237         {
238             continue;
239         }
240
241         // #2. total size
242         resSize += tempSize[index];
243     }
244
245     OIC_LOG_V(DEBUG, TAG, "network info total size is %d!", resSize);
246
247     if (resSize <= 0)
248     {
249         if (CA_ADAPTER_NOT_ENABLED == res || CA_NOT_SUPPORTED == res)
250         {
251             return res;
252         }
253         return CA_STATUS_FAILED;
254     }
255
256     // #3. add data into result
257     // memory allocation
258     CAEndpoint_t *resInfo = (CAEndpoint_t *)OICCalloc(resSize, sizeof(CAEndpoint_t));
259     CA_MEMORY_ALLOC_CHECK(resInfo);
260
261     uint8_t i = 0;
262     for (uint8_t index = 0; index < CA_CONNECTIVITY_TYPE_NUM; index++)
263     {
264         // check information
265         if (tempInfo[index] == NULL || tempSize[index] <= 0)
266         {
267             continue;
268         }
269
270         memcpy(resInfo + i, tempInfo[index], sizeof(CAEndpoint_t) * tempSize[index]);
271
272         i += tempSize[index];
273
274         // free adapter data
275         OICFree(tempInfo[index]);
276     }
277
278     // #5. save data
279     *info = resInfo;
280     *size = resSize;
281
282     OIC_LOG(DEBUG, TAG, "OUT");
283
284     return res;
285
286     // memory error label.
287 memory_error_exit:
288
289     for (uint8_t index = 0; index < CA_CONNECTIVITY_TYPE_NUM; index++)
290     {
291
292         OICFree(tempInfo[index]);
293     }
294
295     return CA_MEMORY_ALLOC_FAILED;
296 }
297
298 CAResult_t CASendUnicastData(const CAEndpoint_t *endpoint, const void *data, uint32_t length)
299 {
300     OIC_LOG(DEBUG, TAG, "IN");
301
302     CAResult_t res = CA_STATUS_FAILED;
303
304     if (endpoint == NULL)
305     {
306         OIC_LOG(DEBUG, TAG, "Endpoint is NULL");
307         return CA_STATUS_INVALID_PARAM;
308     }
309
310     CATransportAdapter_t type = endpoint->adapter;
311
312     int index = CAGetAdapterIndex(type);
313
314     if (index == -1)
315     {
316         OIC_LOG(DEBUG, TAG, "unknown connectivity type!");
317         return CA_STATUS_INVALID_PARAM;
318     }
319
320     uint32_t sentDataLen = 0;
321     if (g_adapterHandler[index].sendData != NULL)
322     {
323         sentDataLen = g_adapterHandler[index].sendData(endpoint, data, length);
324     }
325
326     if (sentDataLen == length)
327     {
328         res = CA_STATUS_OK;
329     }
330
331     OIC_LOG(DEBUG, TAG, "OUT");
332     return res;
333 }
334
335 CAResult_t CASendMulticastData(const CAEndpoint_t *endpoint, const void *data, uint32_t length)
336 {
337     OIC_LOG(DEBUG, TAG, "IN");
338
339     CAResult_t res = CA_STATUS_FAILED;
340     u_arraylist_t *list = CAGetSelectedNetworkList();
341
342     if (!list)
343     {
344         OIC_LOG(DEBUG, TAG, "No selected network");
345         return res;
346     }
347
348     for (uint8_t i = 0; i < u_arraylist_length(list); i++)
349     {
350         void* ptrType = u_arraylist_get(list, i);
351         if (NULL == ptrType)
352         {
353             continue;
354         }
355
356         CATransportAdapter_t connType = *(CATransportAdapter_t *) ptrType;
357
358         int index = CAGetAdapterIndex(connType);
359
360         if (index == -1)
361         {
362             OIC_LOG(DEBUG, TAG, "unknown connectivity type!");
363             continue;
364         }
365
366         uint32_t sentDataLen = 0;
367         if (g_adapterHandler[index].sendDataToAll != NULL)
368         {
369             sentDataLen = g_adapterHandler[index].sendDataToAll(endpoint, data, length);
370         }
371
372         if (sentDataLen == length)
373         {
374             res = CA_STATUS_OK;
375         }
376     }
377     OIC_LOG(DEBUG, TAG, "OUT");
378     return res;
379 }
380
381 CAResult_t CAStartListeningServerAdapters()
382 {
383     OIC_LOG(DEBUG, TAG, "IN");
384
385     u_arraylist_t *list = CAGetSelectedNetworkList();
386
387     if (!list)
388     {
389         OIC_LOG(DEBUG, TAG, "No selected network");
390         return CA_STATUS_FAILED;
391     }
392
393     for (uint8_t i = 0; i < u_arraylist_length(list); i++)
394     {
395         void* ptrType = u_arraylist_get(list, i);
396         if (NULL == ptrType)
397         {
398             OIC_LOG(ERROR, TAG, "Invalid conn type");
399             continue;
400         }
401
402         CATransportAdapter_t connType = *(CATransportAdapter_t *) ptrType;
403
404         int index = CAGetAdapterIndex(connType);
405
406         if (index == -1)
407         {
408             OIC_LOG(DEBUG, TAG, "unknown connectivity type!");
409             continue;
410         }
411
412         if (g_adapterHandler[index].startListenServer != NULL)
413         {
414             g_adapterHandler[index].startListenServer();
415         }
416     }
417     OIC_LOG(DEBUG, TAG, "OUT");
418     return CA_STATUS_OK;
419 }
420
421 CAResult_t CAStartDiscoveryServerAdapters()
422 {
423     OIC_LOG(DEBUG, TAG, "IN");
424
425     u_arraylist_t *list = CAGetSelectedNetworkList();
426
427     if (!list)
428     {
429         OIC_LOG(DEBUG, TAG, "No selected network");
430         return CA_STATUS_FAILED;
431     }
432
433     for (uint8_t i = 0; i < u_arraylist_length(list); i++)
434     {
435         void* ptrType = u_arraylist_get(list, i);
436         if (NULL == ptrType)
437         {
438             continue;
439         }
440
441         CATransportAdapter_t connType = *(CATransportAdapter_t *) ptrType;
442
443         int index = CAGetAdapterIndex(connType);
444
445         if (index == -1)
446         {
447             OIC_LOG(DEBUG, TAG, "unknown connectivity type!");
448             continue;
449         }
450
451         if (g_adapterHandler[index].startDiscoveryServer != NULL)
452         {
453             g_adapterHandler[index].startDiscoveryServer();
454         }
455     }
456     OIC_LOG(DEBUG, TAG, "OUT");
457     return CA_STATUS_OK;
458 }
459
460 void CATerminateAdapters()
461 {
462     OIC_LOG(DEBUG, TAG, "IN");
463
464     uint8_t index;
465
466     for (index = 0; index < CA_CONNECTIVITY_TYPE_NUM; index++)
467     {
468         if (g_adapterHandler[index].stopAdapter != NULL)
469         {
470             g_adapterHandler[index].stopAdapter();
471         }
472         if (g_adapterHandler[index].terminate != NULL)
473         {
474             g_adapterHandler[index].terminate();
475         }
476     }
477
478     OIC_LOG(DEBUG, TAG, "OUT");
479 }
480
481 CAResult_t CAReadData()
482 {
483     OIC_LOG(DEBUG, TAG, "IN");
484     u_arraylist_t *list = CAGetSelectedNetworkList();
485
486     if (!list)
487     {
488         return CA_STATUS_FAILED;
489     }
490
491     for (uint8_t i = 0; i < u_arraylist_length(list); i++)
492     {
493         void *ptrType = u_arraylist_get(list, i);
494         if (NULL == ptrType)
495         {
496             OIC_LOG(ERROR, TAG, "get list fail");
497             return CA_STATUS_FAILED;
498         }
499
500         CATransportAdapter_t connType = *(CATransportAdapter_t *)ptrType;
501
502         int index = CAGetAdapterIndex(connType);
503
504         if (-1 == index)
505         {
506             OIC_LOG(DEBUG, TAG, "unknown connectivity type!");
507             continue;
508         }
509
510         if (g_adapterHandler[index].readData != NULL)
511         {
512             g_adapterHandler[index].readData();
513         }
514     }
515
516     OIC_LOG(DEBUG, TAG, "OUT");
517     return CA_STATUS_OK;
518 }
519