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