1 /******************************************************************
3 * Copyright 2014 Samsung Electronics All Rights Reserved.
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
11 * http://www.apache.org/licenses/LICENSE-2.0
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.
19 ******************************************************************/
26 #include "cainterfacecontroller.h"
27 #include "caedradapter.h"
28 #include "caleadapter.h"
29 #include "cawifiethernetadapter.h"
30 #include "canetworkconfigurator.h"
35 #define CA_CONNECTIVITY_TYPE_NUM 4
37 static CAConnectivityHandler_t gAdapterHandler[CA_CONNECTIVITY_TYPE_NUM];
39 static CANetworkPacketReceivedCallback gNetworkPacketReceivedCallback = NULL;
41 static int8_t CAGetAdapterIndex(CAConnectivityType_t cType)
57 static void CARegisterCallback(CAConnectivityHandler_t handler, CAConnectivityType_t cType)
61 index = CAGetAdapterIndex(cType);
65 OIC_LOG(DEBUG, TAG, "unknown connectivity type!");
69 memcpy(&gAdapterHandler[index], &handler, sizeof(CAConnectivityHandler_t));
71 OIC_LOG_V(DEBUG, TAG, "%d type adapter, register complete!", cType);
74 static void CAReceivedPacketCallback(CARemoteEndpoint_t* endpoint, void* data, uint32_t dataLen)
76 OIC_LOG(DEBUG, TAG, "receivedPacketCallback in interface controller");
79 if (gNetworkPacketReceivedCallback != NULL)
81 gNetworkPacketReceivedCallback(endpoint, data, dataLen);
85 static void CANetworkChangedCallback(CALocalConnectivityt_t* info, CANetworkStatus_t status)
87 OIC_LOG(DEBUG, TAG, "Network Changed callback");
90 void CAInitializeAdapters()
92 OIC_LOG(DEBUG, TAG, "initialize adapters..");
94 memset(gAdapterHandler, 0, sizeof(CAConnectivityHandler_t) * CA_CONNECTIVITY_TYPE_NUM);
96 // Initialize adapters and register callback.
97 #ifdef ETHERNET_ADAPTER
98 CAInitializeEthernet(CARegisterCallback, CAReceivedPacketCallback, CANetworkChangedCallback);
99 #endif /* ETHERNET_ADAPTER */
102 CAInitializeWifi(CARegisterCallback, CAReceivedPacketCallback, CANetworkChangedCallback);
103 #endif /* WIFI_ADAPTER */
106 CAInitializeEDR(CARegisterCallback, CAReceivedPacketCallback, CANetworkChangedCallback);
107 #endif /* EDR_ADAPTER */
110 CAInitializeLE(CARegisterCallback, CAReceivedPacketCallback, CANetworkChangedCallback);
111 #endif /* LE_ADAPTER */
115 void CASetPacketReceivedCallback(CANetworkPacketReceivedCallback callback)
117 OIC_LOG(DEBUG, TAG, "Set packet received callback");
119 gNetworkPacketReceivedCallback = callback;
122 void CAStartAdapter(CAConnectivityType_t cType)
124 OIC_LOG_V(DEBUG, TAG, "Start the adapter of CAConnectivityType[%d]", cType);
128 index = CAGetAdapterIndex(cType);
132 OIC_LOG(DEBUG, TAG, "unknown connectivity type!");
136 if (gAdapterHandler[index].startAdapter != NULL)
138 gAdapterHandler[index].startAdapter();
142 void CAStopAdapter(CAConnectivityType_t cType)
144 OIC_LOG_V(DEBUG, TAG, "Stop the adapter of CAConnectivityType[%d]", cType);
148 index = CAGetAdapterIndex(cType);
152 OIC_LOG(DEBUG, TAG, "unknown connectivity type!");
156 if (gAdapterHandler[index].stopAdapter != NULL)
158 gAdapterHandler[index].stopAdapter();
162 CAResult_t CASendUnicastData(const CARemoteEndpoint_t* endpoint, void* data, uint32_t length)
164 OIC_LOG(DEBUG, TAG, "Send unicast data to enabled interface..");
167 CAResult_t res = CA_STATUS_FAILED;
169 if (endpoint == NULL)
171 OIC_LOG_V(DEBUG, TAG, "Invalid endpoint");
172 return CA_STATUS_INVALID_PARAM;
175 CAConnectivityType_t type = endpoint->connectivityType;
177 index = CAGetAdapterIndex(type);
181 OIC_LOG(DEBUG, TAG, "unknown connectivity type!");
182 return CA_STATUS_INVALID_PARAM;
185 if (gAdapterHandler[index].sendData != NULL)
187 res = gAdapterHandler[index].sendData(endpoint, data, length);
193 CAResult_t CASendMulticastData(void* data, uint32_t length)
195 OIC_LOG(DEBUG, TAG, "Send multicast data to enabled interface..");
199 CAResult_t res = CA_STATUS_FAILED;
200 u_arraylist_t *list = CAGetSelectedNetworkList();
204 OIC_LOG(DEBUG, TAG, "No selected network");
205 return CA_STATUS_FAILED;
208 for (i = 0; i < u_arraylist_length(list); i++)
210 type = *(int*) u_arraylist_get(list, i);
212 index = CAGetAdapterIndex(type);
216 OIC_LOG(DEBUG, TAG, "unknown connectivity type!");
220 if (gAdapterHandler[index].sendDataToAll != NULL)
222 res = gAdapterHandler[index].sendDataToAll(data, length);
229 CAResult_t CAStartListeningServerAdapters()
231 OIC_LOG(DEBUG, TAG, "Start listening server from adapters..");
235 u_arraylist_t *list = CAGetSelectedNetworkList();
239 OIC_LOG(DEBUG, TAG, "No selected network");
240 return CA_STATUS_FAILED;
243 for (i = 0; i < u_arraylist_length(list); i++)
245 type = *(int*) u_arraylist_get(list, i);
247 index = CAGetAdapterIndex(type);
251 OIC_LOG(DEBUG, TAG, "unknown connectivity type!");
255 if (gAdapterHandler[index].startListenServer != NULL)
257 gAdapterHandler[index].startListenServer();
264 CAResult_t CAStartDiscoveryServerAdapters()
266 OIC_LOG(DEBUG, TAG, "Start discovery server from adapters..");
270 u_arraylist_t *list = CAGetSelectedNetworkList();
274 OIC_LOG(DEBUG, TAG, "No selected network");
275 return CA_STATUS_FAILED;
278 for (i = 0; i < u_arraylist_length(list); i++)
280 type = *(int*) u_arraylist_get(list, i);
282 index = CAGetAdapterIndex(type);
286 OIC_LOG_V(DEBUG, TAG, "unknown connectivity type!");
290 if (gAdapterHandler[index].startDiscoverServer != NULL)
292 gAdapterHandler[index].startDiscoverServer();
299 void CATerminateAdapters()
301 OIC_LOG(DEBUG, TAG, "terminate all adapters..");
305 for (index = 0; index < CA_CONNECTIVITY_TYPE_NUM; index++)
307 if (gAdapterHandler[index].terminate != NULL)
309 gAdapterHandler[index].terminate();