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 ******************************************************************/
20 #include "caleadapter.h"
23 #include <arpa/inet.h>
24 #include <sys/socket.h>
29 #endif //#ifdef __TIZEN__
34 #include "cableserver.h"
35 #include "cableclient.h"
38 #include "BLEAdapterArduino.h"
39 #include "caadapterutils.h"
40 #endif //#ifdef __TIZEN__
41 #define CALEADAPTER_TAG "CA_BLE_ADAPTER"
43 static CANetworkChangeCallback gNetworkCallback = NULL;
44 static char gLocalBLEAddress[16] =
47 static int gIsServer = 0;
49 int CALERegisterNetworkNotifications(CANetworkChangeCallback netCallback);
52 int CALEDeviceStateChangedCb(int result, bt_adapter_state_e adapter_state, void *user_data);
53 #endif //#ifdef __TIZEN__
55 pthread_mutex_t gBleIsServerMutex = PTHREAD_RECURSIVE_MUTEX_INITIALIZER_NP;
57 pthread_mutex_t gBleNetworkCbMutex = PTHREAD_RECURSIVE_MUTEX_INITIALIZER_NP;
59 pthread_mutex_t gBleLocalAddressMutex = PTHREAD_RECURSIVE_MUTEX_INITIALIZER_NP;
61 CAResult_t CAInitializeLE(CARegisterConnectivityCallback registerCallback,
62 CANetworkPacketReceivedCallback reqRespCallback, CANetworkChangeCallback netCallback)
64 OCLog(DEBUG, CALEADAPTER_TAG, "IN");
67 VERIFY_NON_NULL(registerCallback, NULL, "RegisterConnectivity callback is null");
68 VERIFY_NON_NULL(reqRespCallback, NULL, "PacketReceived Callback is null");
69 VERIFY_NON_NULL(netCallback, NULL, "NetworkChange Callback is null");
73 int ret = bt_initialize();
76 OCLog(ERROR, CALEADAPTER_TAG, "bt_initialize failed!");
77 return CA_STATUS_FAILED;
80 #endif //#ifdef __TIZEN__
81 CASetBLEReqRescallback(reqRespCallback);
82 CALERegisterNetworkNotifications(netCallback);
84 CAConnectivityHandler_t connHandler;
85 connHandler.startListenServer = CAStartLEListeningServer;
86 connHandler.startDiscoverServer = CAStartLEDiscoveryServer;
87 connHandler.sendData = CASendLEUnicastData;
88 connHandler.sendDataToAll = CASendLEMulticastData;
89 connHandler.startNotifyServer = CAStartLENotifyServer;
90 connHandler.sendNotification = CASendLENotification;
91 connHandler.GetnetInfo = CAGetLEInterfaceInformation;
92 connHandler.readData = CAReadLEData;
93 connHandler.terminate = CATerminateLE;
94 registerCallback(connHandler, CA_LE);
96 OCLog(DEBUG, CALEADAPTER_TAG, "OUT");
103 OCLog(DEBUG, CALEADAPTER_TAG, "IN");
105 CASetBLEReqRescallback(NULL);
106 CALERegisterNetworkNotifications(NULL);
108 pthread_mutex_lock(&gBleIsServerMutex);
111 CAStopBleGattServer();
115 CAStopBLEGattClient();
117 pthread_mutex_unlock(&gBleIsServerMutex);
119 OCLog(DEBUG, CALEADAPTER_TAG, "OUT");
123 void CALEServerInitThreadFunc(void *param)
125 OCLog(DEBUG, CALEADAPTER_TAG, "[CALEServerInitThreadFunc]IN");
127 CAStartBleGattServer();
129 OCLog(DEBUG, CALEADAPTER_TAG, "[CALEServerInitThreadFunc] OUT");
132 void CALEClientInitThreadFunc(void *param)
134 OCLog(DEBUG, CALEADAPTER_TAG, "[CALEClientInitThreadFunc]IN");
136 CAStartBLEGattClient();
138 OCLog(DEBUG, CALEADAPTER_TAG, "[CALEClientInitThreadFunc] OUT");
141 CAResult_t CAStartLEListeningServer()
143 OCLog(DEBUG, CALEADAPTER_TAG, "IN");
145 int init_pthread_status = 0;
146 pthread_t pthread_id = 0;
148 pthread_attr_init(&attr);
149 pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED);
151 init_pthread_status = pthread_create(&pthread_id, &attr, CALEServerInitThreadFunc, NULL);
153 if (init_pthread_status != 0)
155 OCLog(ERROR, CALEADAPTER_TAG, "pthread_create failed!");
156 return CA_STATUS_FAILED;
159 OCLog(DEBUG, CALEADAPTER_TAG, "OUT");
160 pthread_mutex_lock(&gBleIsServerMutex);
162 pthread_mutex_unlock(&gBleIsServerMutex);
166 CAResult_t CAStartLEDiscoveryServer()
168 OCLog(DEBUG, CALEADAPTER_TAG, "IN");
170 int init_pthread_status = 0;
171 pthread_t pthread_id = 0;
173 pthread_attr_init(&attr);
174 pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED);
176 init_pthread_status = pthread_create(&pthread_id, &attr, CALEClientInitThreadFunc, NULL);
178 if (init_pthread_status != 0)
180 OCLog(ERROR, CALEADAPTER_TAG, "pthread_create failed!");
181 return CA_STATUS_FAILED;
184 OCLog(DEBUG, CALEADAPTER_TAG, "OUT");
185 pthread_mutex_lock(&gBleIsServerMutex);
187 pthread_mutex_unlock(&gBleIsServerMutex);
191 CAResult_t CAStartLENotifyServer()
193 OCLog(DEBUG, CALEADAPTER_TAG, "IN");
195 OCLog(DEBUG, CALEADAPTER_TAG, "OUT");
200 uint32_t CASendLENotification(const CARemoteEndpoint_t *endpoint, void *data, uint32_t dataLen)
202 OCLog(DEBUG, CALEADAPTER_TAG, "IN");
204 OCLog(DEBUG, CALEADAPTER_TAG, "OUT");
209 CAResult_t CAReadLEData()
211 OCLog(DEBUG, CALEADAPTER_TAG, "IN");
213 OCLog(DEBUG, CALEADAPTER_TAG, "OUT");
218 uint32_t CASendLEUnicastData(const CARemoteEndpoint_t *endpoint, void *data, uint32_t dataLen)
220 OCLog(DEBUG, CALEADAPTER_TAG, "IN");
223 VERIFY_NON_NULL(endpoint, NULL, "Remote endpoint is null");
224 VERIFY_NON_NULL(data, NULL, "Data is null");
226 CAResult_t result = CA_STATUS_FAILED;
229 pthread_mutex_lock(&gBleIsServerMutex);
232 result = CAUpdateCharacteristicsInGattServer(data, dataLen);
233 if (CA_STATUS_OK != result)
235 OCLogv(ERROR, CALEADAPTER_TAG,
236 "[SendLEUnicastData] sending unicast data to [%s] failed\n", endpoint->addressInfo.BT.btMacAddress);
237 pthread_mutex_unlock(&gBleIsServerMutex);
244 result = CAUpdateCharacteristicsToGattServer(endpoint->addressInfo.BT.btMacAddress, data,
245 dataLen, UNICAST, 0);
246 if (CA_STATUS_OK != result)
248 OCLogv(ERROR, CALEADAPTER_TAG,
249 "[SendLEUnicastData] sending unicast data to [%s] failed\n", endpoint->addressInfo.BT.btMacAddress);
250 pthread_mutex_unlock(&gBleIsServerMutex);
254 pthread_mutex_unlock(&gBleIsServerMutex);
256 char *tempPath = "temp_path";
257 updateCharacteristicsInGattServer(tempPath, (char *) data, dataLen);
258 #endif //#ifdef __TIZEN__
259 OCLog(DEBUG, CALEADAPTER_TAG, "OUT");
264 uint32_t CASendLEMulticastData(void *data, uint32_t dataLen)
266 OCLog(DEBUG, CALEADAPTER_TAG, "IN");
269 VERIFY_NON_NULL(data, NULL, "Data is null");
273 OCLog(ERROR, CALEADAPTER_TAG, "Invalid Parameter");
277 CAResult_t result = CA_STATUS_FAILED;
279 pthread_mutex_lock(&gBleIsServerMutex);
282 result = CAUpdateCharacteristicsInGattServer(data, dataLen);
283 if (CA_STATUS_OK != result)
285 OCLogv(ERROR, CALEADAPTER_TAG,
286 "[CASendLEMulticastData] updating data in server is failed");
287 pthread_mutex_unlock(&gBleIsServerMutex);
293 result = CAUpdateCharacteristicsToAllGattServers(data, dataLen);
294 if (CA_STATUS_OK != result)
296 OCLogv(ERROR, CALEADAPTER_TAG,
297 "[SendLEMulticastDataToAll] multicasting data to servers failed" );
298 pthread_mutex_unlock(&gBleIsServerMutex);
302 pthread_mutex_unlock(&gBleIsServerMutex);
304 char *tempPath = "temp_path";
305 updateCharacteristicsInGattServer(tempPath, (char *) data, dataLen);
306 #endif //#ifdef __TIZEN__
307 OCLog(DEBUG, CALEADAPTER_TAG, "OUT");
311 CAResult_t CAGetLEInterfaceInformation(CALocalConnectivityt_t **info, uint32_t *size)
313 OCLog(DEBUG, CALEADAPTER_TAG, "IN");
315 VERIFY_NON_NULL(info, NULL, "CALocalConnectivity info is null");
317 #ifdef OIC_ARDUINODUE
318 OCLog(DEBUG, CALEADAPTER_TAG, "Info from ARDUINO");
319 //1: call corresponding Arduino API
324 char *local_address = NULL;
326 bt_adapter_get_address(&local_address);
327 if (NULL == local_address)
329 OCLog(ERROR, CALEADAPTER_TAG, "Get local bt adapter address failed");
330 return CA_STATUS_FAILED;
333 #endif //#if ARDUINODUE
335 (*info) = (CALocalConnectivityt_t *) OICMalloc(sizeof(CALocalConnectivityt_t));
338 OCLog(ERROR, CALEADAPTER_TAG, "Malloc failure!");
339 return CA_STATUS_FAILED;
341 memset((*info), 0x0, sizeof(CALocalConnectivityt_t));
343 strncpy((*info)->addressInfo.BT.btMacAddress, local_address, strlen(local_address));
344 pthread_mutex_lock(&gBleLocalAddressMutex);
345 strncpy(gLocalBLEAddress, local_address, sizeof(gLocalBLEAddress));
346 pthread_mutex_unlock(&gBleLocalAddressMutex);
348 (*info)->type = CA_LE;
350 OICFree(local_address);
352 OCLog(DEBUG, CALEADAPTER_TAG, "OUT");
356 int CALERegisterNetworkNotifications(CANetworkChangeCallback netCallback)
358 OCLog(DEBUG, CALEADAPTER_TAG, "IN");
360 pthread_mutex_lock(&gBleNetworkCbMutex);
361 gNetworkCallback = netCallback;
362 pthread_mutex_unlock(&gBleNetworkCbMutex);
367 ret = bt_adapter_set_state_changed_cb(CALEDeviceStateChangedCb, NULL);
370 OCLog(ERROR, CALEADAPTER_TAG, "bt_adapter_set_state_changed_cb failed!");
375 ret = bt_adapter_unset_state_changed_cb();
378 OCLog(ERROR, CALEADAPTER_TAG, "bt_adapter_set_state_changed_cb failed!");
381 #endif //#ifdef __TIZEN__
382 OCLog(DEBUG, CALEADAPTER_TAG, "OUT");
388 int CALEDeviceStateChangedCb(int result, bt_adapter_state_e adapter_state, void *user_data)
390 OCLog(DEBUG, CALEADAPTER_TAG, "IN");
392 bt_adapter_state_e btAdaptorState = BT_ADAPTER_DISABLED;
393 if (BT_ADAPTER_ENABLED == adapter_state)
395 btAdaptorState = BT_ADAPTER_ENABLED;
398 CALocalConnectivityt_t localEndpoint;
400 pthread_mutex_lock(&gBleLocalAddressMutex);
401 strncpy(localEndpoint.addressInfo.BT.btMacAddress, gLocalBLEAddress, strlen(gLocalBLEAddress));
402 pthread_mutex_unlock(&gBleLocalAddressMutex);
404 pthread_mutex_lock(&gBleNetworkCbMutex);
405 if(NULL != gNetworkCallback)
407 gNetworkCallback(&localEndpoint, adapter_state);
411 OCLog(ERROR, CALEADAPTER_TAG, "gNetworkCallback is NULL");
413 pthread_mutex_unlock(&gBleNetworkCbMutex);
414 OCLog(DEBUG, CALEADAPTER_TAG, "OUT");
417 #endif //#ifdef OIC_TIZEN