Implementation of following functionality
[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
266     OIC_LOG_V(DEBUG, TAG, "each network info save success!");
267
268     return res;
269
270     // memory error label.
271 memory_error_exit:
272
273     return CA_MEMORY_ALLOC_FAILED;
274 }
275
276 CAResult_t CASendUnicastData(const CARemoteEndpoint_t *endpoint, void *data, uint32_t length)
277 {
278     OIC_LOG(DEBUG, TAG, "Send unicast data to enabled interface..");
279
280     int8_t index = -1;
281     CAResult_t res = CA_STATUS_FAILED;
282
283     if (endpoint == NULL)
284     {
285         OIC_LOG_V(DEBUG, TAG, "Invalid endpoint");
286         return CA_STATUS_INVALID_PARAM;
287     }
288
289     CAConnectivityType_t type = endpoint->connectivityType;
290
291     index = CAGetAdapterIndex(type);
292
293     if (index == -1)
294     {
295         OIC_LOG(DEBUG, TAG, "unknown connectivity type!");
296         return CA_STATUS_INVALID_PARAM;
297     }
298
299     if (gAdapterHandler[index].sendData != NULL)
300     {
301         res = gAdapterHandler[index].sendData(endpoint, data, length);
302     }
303
304     return res;
305 }
306
307 CAResult_t CASendMulticastData(void *data, uint32_t length)
308 {
309     OIC_LOG(DEBUG, TAG, "Send multicast data to enabled interface..");
310
311     uint8_t i, type;
312     int8_t index = -1;
313     CAResult_t res = CA_STATUS_FAILED;
314     u_arraylist_t *list = CAGetSelectedNetworkList();
315
316     if (!list)
317     {
318         OIC_LOG(DEBUG, TAG, "No selected network");
319         return CA_STATUS_FAILED;
320     }
321
322     for (i = 0; i < u_arraylist_length(list); i++)
323     {
324         type = *(uint8_t *) u_arraylist_get(list, i);
325
326         index = CAGetAdapterIndex(type);
327
328         if (index == -1)
329         {
330             OIC_LOG(DEBUG, TAG, "unknown connectivity type!");
331             continue;
332         }
333
334         if (gAdapterHandler[index].sendDataToAll != NULL)
335         {
336             void *payload = (void *) OICMalloc(length);
337             if (!payload)
338             {
339                 OIC_LOG_V(ERROR, TAG, "Out of memory!");
340                 return CA_MEMORY_ALLOC_FAILED;
341             }
342             memcpy(payload, data, length);
343             res = gAdapterHandler[index].sendDataToAll(payload, length);
344         }
345     }
346     return res;
347 }
348
349 CAResult_t CAStartListeningServerAdapters()
350 {
351     OIC_LOG(DEBUG, TAG, "Start listening server from adapters..");
352
353     uint8_t i, type;
354     int8_t index = -1;
355     u_arraylist_t *list = CAGetSelectedNetworkList();
356
357     if (!list)
358     {
359         OIC_LOG(DEBUG, TAG, "No selected network");
360         return CA_STATUS_FAILED;
361     }
362
363     for (i = 0; i < u_arraylist_length(list); i++)
364     {
365         type = *(uint8_t *) u_arraylist_get(list, i);
366
367         index = CAGetAdapterIndex(type);
368
369         if (index == -1)
370         {
371             OIC_LOG(DEBUG, TAG, "unknown connectivity type!");
372             continue;
373         }
374
375         if (gAdapterHandler[index].startListenServer != NULL)
376         {
377             gAdapterHandler[index].startListenServer();
378         }
379     }
380
381     return CA_STATUS_OK;
382 }
383
384 CAResult_t CAStartDiscoveryServerAdapters()
385 {
386     OIC_LOG(DEBUG, TAG, "Start discovery server from adapters..");
387
388     uint8_t i, type;
389     int8_t index = -1;
390     u_arraylist_t *list = CAGetSelectedNetworkList();
391
392     if (!list)
393     {
394         OIC_LOG(DEBUG, TAG, "No selected network");
395         return CA_STATUS_FAILED;
396     }
397
398     for (i = 0; i < u_arraylist_length(list); i++)
399     {
400         type = *(uint8_t *) u_arraylist_get(list, i);
401
402         index = CAGetAdapterIndex(type);
403
404         if (index == -1)
405         {
406             OIC_LOG_V(DEBUG, TAG, "unknown connectivity type!");
407             continue;
408         }
409
410         if (gAdapterHandler[index].startDiscoverServer != NULL)
411         {
412             gAdapterHandler[index].startDiscoverServer();
413         }
414     }
415
416     return CA_STATUS_OK;
417 }
418
419 void CATerminateAdapters()
420 {
421     OIC_LOG(DEBUG, TAG, "terminate all adapters..");
422
423     uint8_t index;
424
425     for (index = 0; index < CA_CONNECTIVITY_TYPE_NUM; index++)
426     {
427         if (gAdapterHandler[index].terminate != NULL)
428         {
429             gAdapterHandler[index].stopAdapter();
430             gAdapterHandler[index].terminate();
431         }
432     }
433 }