IoT-243 Connectivity Abstraction single-threaded module missing definition for CASend...
[platform/upstream/iotivity.git] / resource / csdk / connectivity / src / cainterfacecontroller.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 "cainterfacecontroller.h"
27 #include "caethernetadapter.h"
28 #include "caedradapter.h"
29 #include "caleadapter.h"
30 #include "cawifiadapter.h"
31 #include "canetworkconfigurator.h"
32 #include "caremotehandler.h"
33 #include "oic_malloc.h"
34 #include "logger.h"
35 #include "uthreadpool.h"
36
37 #define TAG PCF("CA")
38
39 #define CA_MEMORY_ALLOC_CHECK(arg) {if (arg == NULL) \
40     {OIC_LOG_V(DEBUG, TAG, "memory error");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, "CARegisterCallback - Entry");
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, register complete!", cType);
82 }
83
84 static void CAReceivedPacketCallback(CARemoteEndpoint_t *endpoint, void *data,
85                                      uint32_t dataLen)
86 {
87     OIC_LOG(DEBUG, TAG, "receivedPacketCallback in interface controller");
88
89     // Call the callback.
90     if (gNetworkPacketReceivedCallback != NULL)
91     {
92         gNetworkPacketReceivedCallback(endpoint, data, dataLen);
93     }
94 }
95
96 static void CANetworkChangedCallback(CALocalConnectivity_t *info,
97                                      CANetworkStatus_t status)
98 {
99     OIC_LOG(DEBUG, TAG, "Network Changed callback");
100
101     // Call the callback.
102     if (gNetworkChangeCallback != NULL)
103     {
104         gNetworkChangeCallback(info, status);
105     }
106 }
107
108 void CAInitializeAdapters(u_thread_pool_t handle)
109 {
110     OIC_LOG(DEBUG, TAG, "initialize adapters..");
111
112     memset(gAdapterHandler, 0, sizeof(CAConnectivityHandler_t) * CA_CONNECTIVITY_TYPE_NUM);
113
114     // Initialize adapters and register callback.
115 #ifdef ETHERNET_ADAPTER
116     CAInitializeEthernet(CARegisterCallback, CAReceivedPacketCallback, CANetworkChangedCallback,
117                          handle);
118 #endif /* ETHERNET_ADAPTER */
119
120 #ifdef WIFI_ADAPTER
121     CAInitializeWifi(CARegisterCallback, CAReceivedPacketCallback, CANetworkChangedCallback,
122                      handle);
123 #endif /* WIFI_ADAPTER */
124
125 #ifdef EDR_ADAPTER
126     CAInitializeEDR(CARegisterCallback, CAReceivedPacketCallback, CANetworkChangedCallback,
127                     handle);
128 #endif /* EDR_ADAPTER */
129
130 #ifdef LE_ADAPTER
131     CAInitializeLE(CARegisterCallback, CAReceivedPacketCallback, CANetworkChangedCallback,
132                    handle);
133 #endif /* LE_ADAPTER */
134
135 }
136
137 void CASetPacketReceivedCallback(CANetworkPacketReceivedCallback callback)
138 {
139     OIC_LOG(DEBUG, TAG, "Set packet received callback");
140
141     gNetworkPacketReceivedCallback = callback;
142 }
143
144 void CASetNetworkChangeCallback(CANetworkChangeCallback callback)
145 {
146     OIC_LOG(DEBUG, TAG, "Set network change callback");
147
148     gNetworkChangeCallback = callback;
149 }
150
151 void CAStartAdapter(CAConnectivityType_t cType)
152 {
153     OIC_LOG_V(DEBUG, TAG, "Start the adapter of CAConnectivityType[%d]", cType);
154
155     int8_t index = -1;
156
157     index = CAGetAdapterIndex(cType);
158
159     if (index == -1)
160     {
161         OIC_LOG(DEBUG, TAG, "unknown connectivity type!");
162         return;
163     }
164
165     if (gAdapterHandler[index].startAdapter != NULL)
166     {
167         gAdapterHandler[index].startAdapter();
168     }
169 }
170
171 void CAStopAdapter(CAConnectivityType_t cType)
172 {
173     OIC_LOG_V(DEBUG, TAG, "Stop the adapter of CAConnectivityType[%d]", cType);
174
175     int8_t index = -1;
176
177     index = CAGetAdapterIndex(cType);
178
179     if (index == -1)
180     {
181         OIC_LOG(DEBUG, TAG, "unknown connectivity type!");
182         return;
183     }
184
185     if (gAdapterHandler[index].stopAdapter != NULL)
186     {
187         gAdapterHandler[index].stopAdapter();
188     }
189 }
190
191 CAResult_t CAGetNetworkInfo(CALocalConnectivity_t **info, uint32_t *size)
192 {
193     CAResult_t res = CA_STATUS_FAILED;
194     int8_t index = 0;
195     int8_t i = 0;
196
197     CALocalConnectivity_t *resInfo = NULL;
198     uint32_t resSize = 0;
199
200     CALocalConnectivity_t *tempInfo[CA_CONNECTIVITY_TYPE_NUM];
201     uint32_t tempSize[CA_CONNECTIVITY_TYPE_NUM];
202
203     memset(tempInfo, 0, sizeof(CALocalConnectivity_t *) * CA_CONNECTIVITY_TYPE_NUM);
204     memset(tempSize, 0, sizeof(uint32_t) * CA_CONNECTIVITY_TYPE_NUM);
205
206     // #1. get information each adapter
207     for (index = 0; index < CA_CONNECTIVITY_TYPE_NUM; index++)
208     {
209         if (gAdapterHandler[index].GetnetInfo != NULL)
210         {
211             res = gAdapterHandler[index].GetnetInfo(&tempInfo[index], &tempSize[index]);
212
213             OIC_LOG_V(DEBUG, TAG, "%d adapter network info size is %d res:%d", index,
214                       tempSize[index], res);
215         }
216     }
217
218     resSize = 0;
219     for (index = 0; index < CA_CONNECTIVITY_TYPE_NUM; index++)
220     {
221         // check information
222         if (tempInfo[index] == NULL || tempSize[index] <= 0)
223         {
224             continue;
225         }
226
227         // #2. total size
228         resSize += tempSize[index];
229     }
230
231     OIC_LOG_V(DEBUG, TAG, "network info total size is %d!", resSize);
232
233     if (resSize <= 0)
234     {
235         res = CA_STATUS_FAILED;
236         return res;
237     }
238
239     // #3. add data into result
240     // memory allocation
241     resInfo = (CALocalConnectivity_t *) OICMalloc(sizeof(CALocalConnectivity_t) * resSize);
242     CA_MEMORY_ALLOC_CHECK(resInfo);
243     memset(resInfo, 0, sizeof(CALocalConnectivity_t) * resSize);
244
245     i = 0;
246     for (index = 0; index < CA_CONNECTIVITY_TYPE_NUM; index++)
247     {
248         // check information
249         if (tempInfo[index] == NULL || tempSize[index] <= 0)
250         {
251             continue;
252         }
253
254         memcpy(resInfo + i, tempInfo[index], sizeof(CALocalConnectivity_t) * tempSize[index]);
255
256         i += tempSize[index];
257
258         // free adapter data
259         OICFree(tempInfo[index]);
260     }
261
262     // #5. save data
263     *info = resInfo;
264     *size = resSize;
265     res = CA_STATUS_OK;
266
267     OIC_LOG_V(DEBUG, TAG, "each network info save success!");
268
269     return res;
270
271     // memory error label.
272 memory_error_exit:
273
274     return CA_MEMORY_ALLOC_FAILED;
275 }
276
277 CAResult_t CASendUnicastData(const CARemoteEndpoint_t *endpoint, void *data, uint32_t length)
278 {
279     OIC_LOG(DEBUG, TAG, "Send unicast data to enabled interface..");
280
281     int8_t index = -1;
282     CAResult_t res = CA_STATUS_FAILED;
283     uint32_t sentDataLen = 0;
284
285     if (endpoint == NULL)
286     {
287         OIC_LOG_V(DEBUG, TAG, "Invalid endpoint");
288         return CA_STATUS_INVALID_PARAM;
289     }
290
291     CAConnectivityType_t type = endpoint->connectivityType;
292
293     index = CAGetAdapterIndex(type);
294
295     if (index == -1)
296     {
297         OIC_LOG(DEBUG, TAG, "unknown connectivity type!");
298         return CA_STATUS_INVALID_PARAM;
299     }
300
301     if (gAdapterHandler[index].sendData != NULL)
302     {
303         sentDataLen = gAdapterHandler[index].sendData(endpoint, data, length);
304     }
305
306     if (sentDataLen == length)
307     {
308         res = CA_STATUS_OK;
309     }
310     return res;
311 }
312
313 CAResult_t CASendMulticastData(void *data, uint32_t length)
314 {
315     OIC_LOG(DEBUG, TAG, "Send multicast data to enabled interface..");
316
317     uint8_t i = 0;
318     CAConnectivityType_t connType;
319     int8_t index = -1;
320     uint32_t sentDataLen = 0;
321     CAResult_t res = CA_STATUS_FAILED;
322     u_arraylist_t *list = CAGetSelectedNetworkList();
323
324     if (!list)
325     {
326         OIC_LOG(DEBUG, TAG, "No selected network");
327         return CA_STATUS_FAILED;
328     }
329
330     for (i = 0; i < u_arraylist_length(list); i++)
331     {
332         void* ptrType = u_arraylist_get(list, i);
333
334         if(ptrType == NULL)
335         {
336             continue;
337         }
338
339         connType = *(CAConnectivityType_t *) ptrType;
340
341         index = CAGetAdapterIndex(connType);
342
343         if (index == -1)
344         {
345             OIC_LOG(DEBUG, TAG, "unknown connectivity type!");
346             continue;
347         }
348
349         if (gAdapterHandler[index].sendDataToAll != NULL)
350         {
351             void *payload = (void *) OICMalloc(length);
352             if (!payload)
353             {
354                 OIC_LOG_V(ERROR, TAG, "Out of memory!");
355                 return CA_MEMORY_ALLOC_FAILED;
356             }
357             memcpy(payload, data, length);
358             sentDataLen = gAdapterHandler[index].sendDataToAll(payload, length);
359             OICFree(payload);
360         }
361         if (sentDataLen == length)
362         {
363            res = CA_STATUS_OK;
364         }
365     }
366     return res;
367 }
368
369 CAResult_t CAStartListeningServerAdapters()
370 {
371     OIC_LOG(DEBUG, TAG, "Start listening server from adapters..");
372
373     uint8_t i = 0;
374     CAConnectivityType_t connType;
375     int8_t index = -1;
376     u_arraylist_t *list = CAGetSelectedNetworkList();
377
378     if (!list)
379     {
380         OIC_LOG(DEBUG, TAG, "No selected network");
381         return CA_STATUS_FAILED;
382     }
383
384     for (i = 0; i < u_arraylist_length(list); i++)
385     {
386         void* ptrType = u_arraylist_get(list, i);
387
388         if(ptrType == NULL)
389         {
390             continue;
391         }
392
393         connType = *(CAConnectivityType_t *) ptrType;
394
395         index = CAGetAdapterIndex(connType);
396         if (index == -1)
397         {
398             OIC_LOG(DEBUG, TAG, "unknown connectivity type!");
399             continue;
400         }
401
402         if (gAdapterHandler[index].startListenServer != NULL)
403         {
404             gAdapterHandler[index].startListenServer();
405         }
406     }
407
408     return CA_STATUS_OK;
409 }
410
411 CAResult_t CAStartDiscoveryServerAdapters()
412 {
413     OIC_LOG(DEBUG, TAG, "Start discovery server from adapters..");
414
415     uint8_t i = 0;
416     CAConnectivityType_t connType;
417     int8_t index = -1;
418     u_arraylist_t *list = CAGetSelectedNetworkList();
419
420     if (!list)
421     {
422         OIC_LOG(DEBUG, TAG, "No selected network");
423         return CA_STATUS_FAILED;
424     }
425
426     for (i = 0; i < u_arraylist_length(list); i++)
427     {
428         void* ptrType = u_arraylist_get(list, i);
429
430         if(ptrType == NULL)
431         {
432             continue;
433         }
434
435         connType = *(CAConnectivityType_t *) ptrType;
436
437         index = CAGetAdapterIndex(connType);
438
439         if (index == -1)
440         {
441             OIC_LOG_V(DEBUG, TAG, "unknown connectivity type!");
442             continue;
443         }
444
445         if (gAdapterHandler[index].startDiscoverServer != NULL)
446         {
447             gAdapterHandler[index].startDiscoverServer();
448         }
449     }
450
451     return CA_STATUS_OK;
452 }
453
454 void CATerminateAdapters()
455 {
456     OIC_LOG(DEBUG, TAG, "terminate all adapters..");
457
458     uint8_t index;
459
460     for (index = 0; index < CA_CONNECTIVITY_TYPE_NUM; index++)
461     {
462         if (gAdapterHandler[index].stopAdapter != NULL)
463         {
464             gAdapterHandler[index].stopAdapter();
465         }
466         if (gAdapterHandler[index].terminate != NULL)
467         {
468             gAdapterHandler[index].terminate();
469         }
470     }
471 }