Merge "[CA] [All] URI support , Optimization , License verification" into connectivit...
[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
284     if (endpoint == NULL)
285     {
286         OIC_LOG_V(DEBUG, TAG, "Invalid endpoint");
287         return CA_STATUS_INVALID_PARAM;
288     }
289
290     CAConnectivityType_t type = endpoint->connectivityType;
291
292     index = CAGetAdapterIndex(type);
293
294     if (index == -1)
295     {
296         OIC_LOG(DEBUG, TAG, "unknown connectivity type!");
297         return CA_STATUS_INVALID_PARAM;
298     }
299
300     if (gAdapterHandler[index].sendData != NULL)
301     {
302         res = gAdapterHandler[index].sendData(endpoint, data, length);
303     }
304
305     return res;
306 }
307
308 CAResult_t CASendMulticastData(void *data, uint32_t length)
309 {
310     OIC_LOG(DEBUG, TAG, "Send multicast data to enabled interface..");
311
312     uint8_t i, type;
313     int8_t index = -1;
314     CAResult_t res = CA_STATUS_FAILED;
315     u_arraylist_t *list = CAGetSelectedNetworkList();
316
317     if (!list)
318     {
319         OIC_LOG(DEBUG, TAG, "No selected network");
320         return CA_STATUS_FAILED;
321     }
322
323     for (i = 0; i < u_arraylist_length(list); i++)
324     {
325         void* cType = u_arraylist_get(list, i);
326
327         if(cType == NULL)
328         {
329             continue;
330         }
331
332         type = *(uint8_t *) cType;
333
334         index = CAGetAdapterIndex(type);
335
336         if (index == -1)
337         {
338             OIC_LOG(DEBUG, TAG, "unknown connectivity type!");
339             continue;
340         }
341
342         if (gAdapterHandler[index].sendDataToAll != NULL)
343         {
344             void *payload = (void *) OICMalloc(length);
345             if (!payload)
346             {
347                 OIC_LOG_V(ERROR, TAG, "Out of memory!");
348                 return CA_MEMORY_ALLOC_FAILED;
349             }
350             memcpy(payload, data, length);
351             res = gAdapterHandler[index].sendDataToAll(payload, length);
352         }
353     }
354     return res;
355 }
356
357 CAResult_t CAStartListeningServerAdapters()
358 {
359     OIC_LOG(DEBUG, TAG, "Start listening server from adapters..");
360
361     uint8_t i, type;
362     int8_t index = -1;
363     u_arraylist_t *list = CAGetSelectedNetworkList();
364
365     if (!list)
366     {
367         OIC_LOG(DEBUG, TAG, "No selected network");
368         return CA_STATUS_FAILED;
369     }
370
371     for (i = 0; i < u_arraylist_length(list); i++)
372     {
373         void* cType = u_arraylist_get(list, i);
374
375         if(cType == NULL)
376         {
377             continue;
378         }
379
380         type = *(uint8_t *) cType;
381
382         index = CAGetAdapterIndex(type);
383
384         if (index == -1)
385         {
386             OIC_LOG(DEBUG, TAG, "unknown connectivity type!");
387             continue;
388         }
389
390         if (gAdapterHandler[index].startListenServer != NULL)
391         {
392             gAdapterHandler[index].startListenServer();
393         }
394     }
395
396     return CA_STATUS_OK;
397 }
398
399 CAResult_t CAStartDiscoveryServerAdapters()
400 {
401     OIC_LOG(DEBUG, TAG, "Start discovery server from adapters..");
402
403     uint8_t i, type;
404     int8_t index = -1;
405     u_arraylist_t *list = CAGetSelectedNetworkList();
406
407     if (!list)
408     {
409         OIC_LOG(DEBUG, TAG, "No selected network");
410         return CA_STATUS_FAILED;
411     }
412
413     for (i = 0; i < u_arraylist_length(list); i++)
414     {
415         void* cType = u_arraylist_get(list, i);
416
417         if(cType == NULL)
418         {
419             continue;
420         }
421
422         type = *(uint8_t *) cType;
423
424         index = CAGetAdapterIndex(type);
425
426         if (index == -1)
427         {
428             OIC_LOG_V(DEBUG, TAG, "unknown connectivity type!");
429             continue;
430         }
431
432         if (gAdapterHandler[index].startDiscoverServer != NULL)
433         {
434             gAdapterHandler[index].startDiscoverServer();
435         }
436     }
437
438     return CA_STATUS_OK;
439 }
440
441 void CATerminateAdapters()
442 {
443     OIC_LOG(DEBUG, TAG, "terminate all adapters..");
444
445     uint8_t index;
446
447     for (index = 0; index < CA_CONNECTIVITY_TYPE_NUM; index++)
448     {
449         if (gAdapterHandler[index].terminate != NULL)
450         {
451             gAdapterHandler[index].stopAdapter();
452             gAdapterHandler[index].terminate();
453         }
454     }
455 }