Modifying version number for building on tizen 3.0
[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 "caedradapter.h"
28 #include "caleadapter.h"
29 #include "cawifiethernetadapter.h"
30 #include "canetworkconfigurator.h"
31 #include "logger.h"
32
33 #define TAG PCF("CA")
34
35 #define CA_CONNECTIVITY_TYPE_NUM   4
36
37 static CAConnectivityHandler_t gAdapterHandler[CA_CONNECTIVITY_TYPE_NUM];
38
39 static CANetworkPacketReceivedCallback gNetworkPacketReceivedCallback = NULL;
40
41 static int8_t CAGetAdapterIndex(CAConnectivityType_t cType)
42 {
43     switch (cType)
44     {
45         case CA_ETHERNET:
46             return 0;
47         case CA_WIFI:
48             return 1;
49         case CA_EDR:
50             return 2;
51         case CA_LE:
52             return 3;
53     }
54     return -1;
55 }
56
57 static void CARegisterCallback(CAConnectivityHandler_t handler, CAConnectivityType_t cType)
58 {
59     int8_t index = -1;
60
61     index = CAGetAdapterIndex(cType);
62
63     if (index == -1)
64     {
65         OIC_LOG(DEBUG, TAG, "unknown connectivity type!");
66         return;
67     }
68
69     memcpy(&gAdapterHandler[index], &handler, sizeof(CAConnectivityHandler_t));
70
71     OIC_LOG_V(DEBUG, TAG, "%d type adapter, register complete!", cType);
72 }
73
74 static void CAReceivedPacketCallback(CARemoteEndpoint_t* endpoint, void* data, uint32_t dataLen)
75 {
76     OIC_LOG(DEBUG, TAG, "receivedPacketCallback in interface controller");
77
78     // Call the callback.
79     if (gNetworkPacketReceivedCallback != NULL)
80     {
81         gNetworkPacketReceivedCallback(endpoint, data, dataLen);
82     }
83 }
84
85 static void CANetworkChangedCallback(CALocalConnectivityt_t* info, CANetworkStatus_t status)
86 {
87     OIC_LOG(DEBUG, TAG, "Network Changed callback");
88 }
89
90 void CAInitializeAdapters()
91 {
92     OIC_LOG(DEBUG, TAG, "initialize adapters..");
93
94     memset(gAdapterHandler, 0, sizeof(CAConnectivityHandler_t) * CA_CONNECTIVITY_TYPE_NUM);
95
96     // Initialize adapters and register callback.
97 #ifdef ETHERNET_ADAPTER
98     CAInitializeEthernet(CARegisterCallback, CAReceivedPacketCallback, CANetworkChangedCallback);
99 #endif /* ETHERNET_ADAPTER */
100
101 #ifdef WIFI_ADAPTER
102     CAInitializeWifi(CARegisterCallback, CAReceivedPacketCallback, CANetworkChangedCallback);
103 #endif /* WIFI_ADAPTER */
104
105 #ifdef EDR_ADAPTER
106     CAInitializeEDR(CARegisterCallback, CAReceivedPacketCallback, CANetworkChangedCallback);
107 #endif /* EDR_ADAPTER */
108
109 #ifdef LE_ADAPTER
110     CAInitializeLE(CARegisterCallback, CAReceivedPacketCallback, CANetworkChangedCallback);
111 #endif /* LE_ADAPTER */
112
113 }
114
115 void CASetPacketReceivedCallback(CANetworkPacketReceivedCallback callback)
116 {
117     OIC_LOG(DEBUG, TAG, "Set packet received callback");
118
119     gNetworkPacketReceivedCallback = callback;
120 }
121
122 void CAStartAdapter(CAConnectivityType_t cType)
123 {
124     OIC_LOG_V(DEBUG, TAG, "Start the adapter of CAConnectivityType[%d]", cType);
125
126     int8_t index = -1;
127
128     index = CAGetAdapterIndex(cType);
129
130     if (index == -1)
131     {
132         OIC_LOG(DEBUG, TAG, "unknown connectivity type!");
133         return;
134     }
135
136     if (gAdapterHandler[index].startAdapter != NULL)
137     {
138         gAdapterHandler[index].startAdapter();
139     }
140 }
141
142 void CAStopAdapter(CAConnectivityType_t cType)
143 {
144     OIC_LOG_V(DEBUG, TAG, "Stop the adapter of CAConnectivityType[%d]", cType);
145
146     int8_t index = -1;
147
148     index = CAGetAdapterIndex(cType);
149
150     if (index == -1)
151     {
152         OIC_LOG(DEBUG, TAG, "unknown connectivity type!");
153         return;
154     }
155
156     if (gAdapterHandler[index].stopAdapter != NULL)
157     {
158         gAdapterHandler[index].stopAdapter();
159     }
160 }
161
162 CAResult_t CASendUnicastData(const CARemoteEndpoint_t* endpoint, void* data, uint32_t length)
163 {
164     OIC_LOG(DEBUG, TAG, "Send unicast data to enabled interface..");
165
166     int8_t index = -1;
167     CAResult_t res = CA_STATUS_FAILED;
168
169     if (endpoint == NULL)
170     {
171         OIC_LOG_V(DEBUG, TAG, "Invalid endpoint");
172         return CA_STATUS_INVALID_PARAM;
173     }
174
175     CAConnectivityType_t type = endpoint->connectivityType;
176
177     index = CAGetAdapterIndex(type);
178
179     if (index == -1)
180     {
181         OIC_LOG(DEBUG, TAG, "unknown connectivity type!");
182         return CA_STATUS_INVALID_PARAM;
183     }
184
185     if (gAdapterHandler[index].sendData != NULL)
186     {
187         res = gAdapterHandler[index].sendData(endpoint, data, length);
188     }
189
190     return res;
191 }
192
193 CAResult_t CASendMulticastData(void* data, uint32_t length)
194 {
195     OIC_LOG(DEBUG, TAG, "Send multicast data to enabled interface..");
196
197     uint8_t i, type;
198     int8_t index = -1;
199     CAResult_t res = CA_STATUS_FAILED;
200     u_arraylist_t *list = CAGetSelectedNetworkList();
201
202     if (!list)
203     {
204         OIC_LOG(DEBUG, TAG, "No selected network");
205         return CA_STATUS_FAILED;
206     }
207
208     for (i = 0; i < u_arraylist_length(list); i++)
209     {
210         type = *(int*) u_arraylist_get(list, i);
211
212         index = CAGetAdapterIndex(type);
213
214         if (index == -1)
215         {
216             OIC_LOG(DEBUG, TAG, "unknown connectivity type!");
217             continue;
218         }
219
220         if (gAdapterHandler[index].sendDataToAll != NULL)
221         {
222             res = gAdapterHandler[index].sendDataToAll(data, length);
223         }
224     }
225
226     return res;
227 }
228
229 CAResult_t CAStartListeningServerAdapters()
230 {
231     OIC_LOG(DEBUG, TAG, "Start listening server from adapters..");
232
233     uint8_t i, type;
234     int8_t index = -1;
235     u_arraylist_t *list = CAGetSelectedNetworkList();
236
237     if (!list)
238     {
239         OIC_LOG(DEBUG, TAG, "No selected network");
240         return CA_STATUS_FAILED;
241     }
242
243     for (i = 0; i < u_arraylist_length(list); i++)
244     {
245         type = *(int*) u_arraylist_get(list, i);
246
247         index = CAGetAdapterIndex(type);
248
249         if (index == -1)
250         {
251             OIC_LOG(DEBUG, TAG, "unknown connectivity type!");
252             continue;
253         }
254
255         if (gAdapterHandler[index].startListenServer != NULL)
256         {
257             gAdapterHandler[index].startListenServer();
258         }
259     }
260
261     return CA_STATUS_OK;
262 }
263
264 CAResult_t CAStartDiscoveryServerAdapters()
265 {
266     OIC_LOG(DEBUG, TAG, "Start discovery server from adapters..");
267
268     uint8_t i, type;
269     int8_t index = -1;
270     u_arraylist_t *list = CAGetSelectedNetworkList();
271
272     if (!list)
273     {
274         OIC_LOG(DEBUG, TAG, "No selected network");
275         return CA_STATUS_FAILED;
276     }
277
278     for (i = 0; i < u_arraylist_length(list); i++)
279     {
280         type = *(int*) u_arraylist_get(list, i);
281
282         index = CAGetAdapterIndex(type);
283
284         if (index == -1)
285         {
286             OIC_LOG_V(DEBUG, TAG, "unknown connectivity type!");
287             continue;
288         }
289
290         if (gAdapterHandler[index].startDiscoverServer != NULL)
291         {
292             gAdapterHandler[index].startDiscoverServer();
293         }
294     }
295
296     return CA_STATUS_OK;
297 }
298
299 void CATerminateAdapters()
300 {
301     OIC_LOG(DEBUG, TAG, "terminate all adapters..");
302
303     uint8_t index;
304
305     for (index = 0; index < CA_CONNECTIVITY_TYPE_NUM; index++)
306     {
307         if (gAdapterHandler[index].terminate != NULL)
308         {
309             gAdapterHandler[index].terminate();
310         }
311     }
312 }