Merge branch 'master' into connectivity-abstraction
[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 "cawifiadapter.h"
28 #include "caethernetadapter.h"
29 #include "caedradapter.h"
30 #include "caleadapter.h"
31 #include "cawifiadapter.h"
32 #include "caethernetadapter.h"
33 #include "canetworkconfigurator.h"
34 #include "caremotehandler.h"
35 #include "oic_malloc.h"
36 #include "logger.h"
37 #include "uthreadpool.h"
38
39 #define TAG PCF("CA")
40
41 #define CA_MEMORY_ALLOC_CHECK(arg) { if (arg == NULL) {OIC_LOG_V(DEBUG, TAG, "memory error"); goto memory_error_exit;} }
42
43 #define CA_CONNECTIVITY_TYPE_NUM   4
44
45 static CAConnectivityHandler_t gAdapterHandler[CA_CONNECTIVITY_TYPE_NUM];
46
47 static CANetworkPacketReceivedCallback gNetworkPacketReceivedCallback = NULL;
48
49 static CANetworkChangeCallback gNetworkChangeCallback = NULL;
50
51 static int8_t CAGetAdapterIndex(CAConnectivityType_t cType)
52 {
53     switch (cType)
54     {
55         case CA_ETHERNET:
56             return 0;
57         case CA_WIFI:
58             return 1;
59         case CA_EDR:
60             return 2;
61         case CA_LE:
62             return 3;
63     }
64     return -1;
65 }
66
67 static void CARegisterCallback(CAConnectivityHandler_t handler, CAConnectivityType_t cType)
68 {
69     OIC_LOG(DEBUG, TAG, "CARegisterCallback - Entry");
70     int8_t index = -1;
71
72     index = CAGetAdapterIndex(cType);
73
74     if (index == -1)
75     {
76         OIC_LOG(DEBUG, TAG, "unknown connectivity type!");
77         return;
78     }
79
80     memcpy(&gAdapterHandler[index], &handler, sizeof(CAConnectivityHandler_t));
81
82     OIC_LOG_V(DEBUG, TAG, "%d type adapter, register complete!", cType);
83 }
84
85 static void CAReceivedPacketCallback(CARemoteEndpoint_t *endpoint, void *data, 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, CANetworkStatus_t status)
97 {
98     OIC_LOG(DEBUG, TAG, "Network Changed callback");
99
100     // Call the callback.
101     if (gNetworkChangeCallback != NULL)
102     {
103         gNetworkChangeCallback(info, status);
104     }
105 }
106
107 void CAInitializeAdapters(u_thread_pool_t handle)
108 {
109     OIC_LOG(DEBUG, TAG, "initialize adapters..");
110
111     memset(gAdapterHandler, 0, sizeof(CAConnectivityHandler_t) * CA_CONNECTIVITY_TYPE_NUM);
112
113     // Initialize adapters and register callback.
114 #ifdef ETHERNET_ADAPTER
115     CAInitializeEthernet(CARegisterCallback, CAReceivedPacketCallback, CANetworkChangedCallback,
116                          handle);
117 #endif /* ETHERNET_ADAPTER */
118
119 #ifdef WIFI_ADAPTER
120     CAInitializeWifi(CARegisterCallback, CAReceivedPacketCallback, CANetworkChangedCallback, handle);
121 #endif /* WIFI_ADAPTER */
122
123 #ifdef EDR_ADAPTER
124     CAInitializeEDR(CARegisterCallback, CAReceivedPacketCallback, CANetworkChangedCallback, handle);
125 #endif /* EDR_ADAPTER */
126
127 #ifdef LE_ADAPTER
128     CAInitializeLE(CARegisterCallback, CAReceivedPacketCallback, CANetworkChangedCallback, handle);
129 #endif /* LE_ADAPTER */
130
131 }
132
133 void CASetPacketReceivedCallback(CANetworkPacketReceivedCallback callback)
134 {
135     OIC_LOG(DEBUG, TAG, "Set packet received callback");
136
137     gNetworkPacketReceivedCallback = callback;
138 }
139
140 void CASetNetworkChangeCallback(CANetworkChangeCallback callback)
141 {
142     OIC_LOG(DEBUG, TAG, "Set network change callback");
143
144     gNetworkChangeCallback = callback;
145 }
146
147 void CAStartAdapter(CAConnectivityType_t cType)
148 {
149     OIC_LOG_V(DEBUG, TAG, "Start the adapter of CAConnectivityType[%d]", cType);
150
151     int8_t index = -1;
152
153     index = CAGetAdapterIndex(cType);
154
155     if (index == -1)
156     {
157         OIC_LOG(DEBUG, TAG, "unknown connectivity type!");
158         return;
159     }
160
161     if (gAdapterHandler[index].startAdapter != NULL)
162     {
163         gAdapterHandler[index].startAdapter();
164     }
165 }
166
167 void CAStopAdapter(CAConnectivityType_t cType)
168 {
169     OIC_LOG_V(DEBUG, TAG, "Stop the adapter of CAConnectivityType[%d]", cType);
170
171     int8_t index = -1;
172
173     index = CAGetAdapterIndex(cType);
174
175     if (index == -1)
176     {
177         OIC_LOG(DEBUG, TAG, "unknown connectivity type!");
178         return;
179     }
180
181     if (gAdapterHandler[index].stopAdapter != NULL)
182     {
183         gAdapterHandler[index].stopAdapter();
184     }
185 }
186
187 CAResult_t CAGetNetworkInfo(CALocalConnectivity_t **info, uint32_t *size)
188 {
189     CAResult_t res = CA_STATUS_FAILED;
190     int8_t index = 0;
191     int8_t i = 0;
192
193     CALocalConnectivity_t *resInfo = NULL;
194     uint32_t resSize = 0;
195
196     CALocalConnectivity_t *tempInfo[CA_CONNECTIVITY_TYPE_NUM];
197     uint32_t tempSize[CA_CONNECTIVITY_TYPE_NUM];
198
199     memset(tempInfo, 0, sizeof(CALocalConnectivity_t *) * CA_CONNECTIVITY_TYPE_NUM);
200     memset(tempSize, 0, sizeof(uint32_t) * CA_CONNECTIVITY_TYPE_NUM);
201
202     // #1. get information each adapter
203     for (index = 0; index < CA_CONNECTIVITY_TYPE_NUM; index++)
204     {
205         if (gAdapterHandler[index].GetnetInfo != NULL)
206         {
207             res = gAdapterHandler[index].GetnetInfo(&tempInfo[index], &tempSize[index]);
208
209             OIC_LOG_V(DEBUG, TAG, "%d adapter network info size is %d res:%d", index,
210                     tempSize[index], res);
211         }
212     }
213
214     resSize = 0;
215     for (index = 0; index < CA_CONNECTIVITY_TYPE_NUM; index++)
216     {
217         // check information
218         if (tempInfo[index] == NULL || tempSize[index] <= 0)
219         {
220             continue;
221         }
222
223         // #2. total size
224         resSize += tempSize[index];
225     }
226
227     OIC_LOG_V(DEBUG, TAG, "network info total size is %d!", resSize);
228
229     if (resSize <= 0)
230     {
231         res = CA_STATUS_FAILED;
232         return res;
233     }
234
235     // #3. add data into result
236     // memory allocation
237     resInfo = (CALocalConnectivity_t *) OICMalloc(sizeof(CALocalConnectivity_t) * resSize);
238     CA_MEMORY_ALLOC_CHECK(resInfo);
239     memset(resInfo, 0, sizeof(CALocalConnectivity_t) * resSize);
240
241     i = 0;
242     for (index = 0; index < CA_CONNECTIVITY_TYPE_NUM; index++)
243     {
244         // check information
245         if (tempInfo[index] == NULL || tempSize[index] <= 0)
246         {
247             continue;
248         }
249
250         memcpy(resInfo + i, tempInfo[index], sizeof(CALocalConnectivity_t) * tempSize[index]);
251
252         i += tempSize[index];
253
254         // free adapter data
255         OICFree(tempInfo[index]);
256     }
257
258     // #5. save data
259     *info = resInfo;
260     *size = resSize;
261
262     OIC_LOG_V(DEBUG, TAG, "each network info save success!");
263
264     return res;
265
266     // memory error label.
267 memory_error_exit:
268
269     return CA_MEMORY_ALLOC_FAILED;
270 }
271
272 CAResult_t CASendUnicastData(CARemoteEndpoint_t* endpoint, void* data, uint32_t length)
273 {
274     OIC_LOG(DEBUG, TAG, "Send unicast data to enabled interface..");
275
276     int8_t index = -1;
277     CAResult_t res = CA_STATUS_FAILED;
278
279     if (endpoint == NULL)
280     {
281         OIC_LOG_V(DEBUG, TAG, "Invalid endpoint");
282         return CA_STATUS_INVALID_PARAM;
283     }
284
285     CAConnectivityType_t type = endpoint->connectivityType;
286
287     index = CAGetAdapterIndex(type);
288
289     if (index == -1)
290     {
291         OIC_LOG(DEBUG, TAG, "unknown connectivity type!");
292         return CA_STATUS_INVALID_PARAM;
293     }
294
295     if (gAdapterHandler[index].sendData != NULL)
296     {
297         res = gAdapterHandler[index].sendData(endpoint, data, length);
298     }
299     //For Unicast , data will be deleted by adapters
300
301     CADestroyRemoteEndpointInternal(endpoint);
302
303     return res;
304 }
305
306 CAResult_t CASendMulticastData(void *data, uint32_t length)
307 {
308     OIC_LOG(DEBUG, TAG, "Send multicast data to enabled interface..");
309
310     uint8_t i, type;
311     int8_t index = -1;
312     CAResult_t res = CA_STATUS_FAILED;
313     u_arraylist_t *list = CAGetSelectedNetworkList();
314
315     if (!list)
316     {
317         OIC_LOG(DEBUG, TAG, "No selected network");
318         return CA_STATUS_FAILED;
319     }
320
321     for (i = 0; i < u_arraylist_length(list); i++)
322     {
323         type = *(uint8_t*) u_arraylist_get(list, i);
324
325         index = CAGetAdapterIndex(type);
326
327         if (index == -1)
328         {
329             OIC_LOG(DEBUG, TAG, "unknown connectivity type!");
330             continue;
331         }
332
333         if (gAdapterHandler[index].sendDataToAll != NULL)
334         {
335             void* payload = (void*) OICMalloc(length);
336             memcpy(payload, data, length);
337             res = gAdapterHandler[index].sendDataToAll(payload, length);
338         }
339     }
340     return res;
341 }
342
343 CAResult_t CAStartListeningServerAdapters()
344 {
345     OIC_LOG(DEBUG, TAG, "Start listening server from adapters..");
346
347     uint8_t i, type;
348     int8_t index = -1;
349     u_arraylist_t *list = CAGetSelectedNetworkList();
350
351     if (!list)
352     {
353         OIC_LOG(DEBUG, TAG, "No selected network");
354         return CA_STATUS_FAILED;
355     }
356
357     for (i = 0; i < u_arraylist_length(list); i++)
358     {
359         type = *(uint8_t*) u_arraylist_get(list, i);
360
361         index = CAGetAdapterIndex(type);
362
363         if (index == -1)
364         {
365             OIC_LOG(DEBUG, TAG, "unknown connectivity type!");
366             continue;
367         }
368
369         if (gAdapterHandler[index].startListenServer != NULL)
370         {
371             gAdapterHandler[index].startListenServer();
372         }
373     }
374
375     return CA_STATUS_OK;
376 }
377
378 CAResult_t CAStartDiscoveryServerAdapters()
379 {
380     OIC_LOG(DEBUG, TAG, "Start discovery server from adapters..");
381
382     uint8_t i, type;
383     int8_t index = -1;
384     u_arraylist_t *list = CAGetSelectedNetworkList();
385
386     if (!list)
387     {
388         OIC_LOG(DEBUG, TAG, "No selected network");
389         return CA_STATUS_FAILED;
390     }
391
392     for (i = 0; i < u_arraylist_length(list); i++)
393     {
394         type = *(uint8_t*) u_arraylist_get(list, i);
395
396         index = CAGetAdapterIndex(type);
397
398         if (index == -1)
399         {
400             OIC_LOG_V(DEBUG, TAG, "unknown connectivity type!");
401             continue;
402         }
403
404         if (gAdapterHandler[index].startDiscoverServer != NULL)
405         {
406             gAdapterHandler[index].startDiscoverServer();
407         }
408     }
409
410     return CA_STATUS_OK;
411 }
412
413 void CATerminateAdapters()
414 {
415     OIC_LOG(DEBUG, TAG, "terminate all adapters..");
416
417     uint8_t index;
418
419     for (index = 0; index < CA_CONNECTIVITY_TYPE_NUM; index++)
420     {
421         if (gAdapterHandler[index].terminate != NULL)
422         {
423             gAdapterHandler[index].terminate();
424         }
425     }
426 }