2 // Tizen Web Device API
3 // Copyright (c) 2012 Samsung Electronics Co., Ltd.
5 // Licensed under the Apache License, Version 2.0 (the License);
6 // you may not use this file except in compliance with the License.
7 // You may obtain a copy of the License at
9 // http://www.apache.org/licenses/LICENSE-2.0
11 // Unless required by applicable law or agreed to in writing, software
12 // distributed under the License is distributed on an "AS IS" BASIS,
13 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 // See the License for the specific language governing permissions and
15 // limitations under the License.
19 #include <bluetooth.h>
20 #include "BluetoothCBManager.h"
21 #include "BluetoothAdapterManager.h"
22 #include "BluetoothDeviceManager.h"
23 #include "BluetoothSocketManager.h"
24 #include "BluetoothServiceHandlerManager.h"
25 #include <dpl/singleton_impl.h>
26 #include <dpl/scoped_array.h>
29 #include <Commons/Exception.h>
32 IMPLEMENT_SINGLETON(DeviceAPI::Bluetooth::BluetoothCBManager);
39 static void capi_callback_bt_state_changed(int result, bt_adapter_state_e adapter_state, void* user_data)
41 if( result == BT_ERROR_NONE && user_data != NULL)
43 LogDebug("State Changed" << adapter_state);
44 ((BluetoothAdapterManager*)user_data)->setPoweredManualAnswer();
49 LogDebug("Error from platform");
53 static void capi_callback_bt_name_changed(char *device_name, void *user_data)
55 ((BluetoothAdapterManager*)user_data)->setAdapterNameManualAnswer(device_name);
58 static void capi_callback_bt_discovery_state_changed(int result,
59 bt_adapter_device_discovery_state_e discovery_state, bt_adapter_device_discovery_info_s *discovery_info, void *user_data)
61 LogDebug("Discovery state" << discovery_state);
63 if (discovery_state == BT_ADAPTER_DEVICE_DISCOVERY_FOUND && discovery_info != NULL)
65 LogDebug("found device" << discovery_info->remote_name << " " << discovery_info->remote_address);
68 LogDebug("Discovery state" << discovery_state);
70 if (user_data != NULL)
72 ((BluetoothAdapterManager*)user_data)->discoveryDevicesCallbackEmit(result, discovery_state, discovery_info);
76 LogDebug("Error from platform");
81 static void capi_callback_bt_device_bond_created(int result, bt_device_info_s *device_info, void *user_data)
83 if (user_data != NULL)
85 ((BluetoothAdapterManager*)user_data)->createBondingManualAnswer(result, device_info);
89 LogDebug("Error from platform");
94 static void capi_callback_bt_device_bond_destroyed(int result, char* remote_address, void *user_data)
96 if (user_data != NULL)
98 ((BluetoothAdapterManager*)user_data)->destroyBondingManualAnswer(result);
102 LogDebug("Error from platform");
106 static void capi_bt_device_service_searched(int result, bt_device_sdp_info_s* sdp_info, void* user_data)
108 if (user_data != NULL && sdp_info != NULL && sdp_info->remote_address != NULL)
110 ((BluetoothCBManager*)user_data)->serviceSearchRouter(result, sdp_info, user_data);
114 LogDebug("Error from platform");
119 static void capi_callback_bt_socket_connection_state_changed(int result, bt_socket_connection_state_e connection_state,
120 bt_socket_connection_s *connection, void *user_data)
122 if (user_data != NULL && connection != NULL && connection->remote_address != NULL)
124 ((BluetoothCBManager*)user_data)->connectionStateRouter(result, connection_state, connection);
128 LogDebug("Error from platform");
133 static void capi_callback_bt_data_received(bt_socket_received_data_s *data, void *user_data)
135 if (user_data != NULL && data != NULL)
137 ((BluetoothCBManager*)(user_data))->socketDataReceivedRouter(data);
141 LogDebug("Error from platform");
152 BluetoothCBManager::BluetoothCBManager()
154 LogDebug("BluetoothCBManager singleton");
158 BluetoothCBManager::~BluetoothCBManager()
160 LogDebug("BluetoothCBManager singleton removed");
161 disconnectAllSockets();
167 void BluetoothCBManager::addAdapterCallback(adapterCallback cb, void *passData, std::string address)
169 std::vector<adapterCallback>::iterator it;
171 for (it = m_adapterCallback.begin(); it < m_adapterCallback.end(); ++it)
177 if (cb == ADAPTER_DESTROYBOND)
179 LogDebug("ADAPTER_DESTROYBOND");
182 if (it == m_adapterCallback.end())
187 case ADAPTER_SETPOWERED:
188 if( bt_adapter_set_state_changed_cb(capi_callback_bt_state_changed, passData) != BT_ERROR_NONE )
190 ThrowMsg(WrtDeviceApis::Commons::PlatformException, "call back set error");
193 case ADAPTER_SETNAME:
194 if (bt_adapter_set_name_changed_cb(capi_callback_bt_name_changed, passData) != BT_ERROR_NONE)
196 ThrowMsg(WrtDeviceApis::Commons::PlatformException, "callback set error");
199 case ADAPTER_DISCOVERY:
200 if (bt_adapter_set_device_discovery_state_changed_cb(capi_callback_bt_discovery_state_changed,
201 passData)!= BT_ERROR_NONE)
203 ThrowMsg(WrtDeviceApis::Commons::PlatformException, "callback set error");
206 case ADAPTER_SERVICESEARCH:
207 addServiceSearchCallback(address, passData, FROM_ADAPTER);
209 case ADAPTER_CREATEBOND:
210 if (bt_device_set_bond_created_cb(capi_callback_bt_device_bond_created, passData) != BT_ERROR_NONE )
212 ThrowMsg(WrtDeviceApis::Commons::PlatformException, "callback set error");
215 case ADAPTER_DESTROYBOND:
216 if (bt_device_set_bond_destroyed_cb (capi_callback_bt_device_bond_destroyed, passData) != BT_ERROR_NONE )
218 ThrowMsg(WrtDeviceApis::Commons::PlatformException, "callback set error");
221 case ADAPTER_REGISTER:
222 LogDebug("do nothing to set callback");
226 m_adapterCallback.push_back(cb);
227 LogDebug("operation call back is set successfully");
231 LogDebug("same operation now, throws error");
232 ThrowMsg(WrtDeviceApis::Commons::AlreadyInUseException, "same operation now");
238 void BluetoothCBManager::removeAdapterCallback(adapterCallback cb, std::string address)
240 std::vector<adapterCallback>::iterator it;
242 for (it = m_adapterCallback.begin(); it != m_adapterCallback.end(); ++it)
250 if (it == m_adapterCallback.end())
252 LogDebug("there is no such callback");
256 m_adapterCallback.erase(it);
257 LogDebug("operation call back is unset successfully");
261 case ADAPTER_SETPOWERED:
262 bt_adapter_unset_state_changed_cb();
264 case ADAPTER_SETNAME:
265 bt_adapter_unset_name_changed_cb();
267 case ADAPTER_DISCOVERY:
268 bt_adapter_unset_device_discovery_state_changed_cb();
270 case ADAPTER_SERVICESEARCH:
271 removeServiceSearchCallback(address);
273 case ADAPTER_CREATEBOND:
274 bt_device_unset_bond_created_cb();
276 case ADAPTER_DESTROYBOND:
277 bt_device_unset_bond_destroyed_cb();
279 case ADAPTER_REGISTER:
280 LogDebug("do nothing to set callback");
288 void BluetoothCBManager::registerSocket(std::string uuid, int socket)
290 std::map<std::string, int>::iterator it = m_serverSocketUuidMap.find(uuid);
292 if (it != m_serverSocketUuidMap.end())
294 ThrowMsg(WrtDeviceApis::Commons::AlreadyInUseException, "same uuid exist");
297 m_serverSocketUuidMap[uuid] = socket;
300 void BluetoothCBManager::checkUUIDAvailable(std::string uuid)
302 std::map<std::string, int>::iterator it = m_serverSocketUuidMap.find(uuid);
304 if (it != m_serverSocketUuidMap.end())
306 ThrowMsg(WrtDeviceApis::Commons::AlreadyInUseException, "same uuid exist");
310 void BluetoothCBManager::unregisterSocket(std::string uuid)
312 std::map<std::string, int>::iterator it = m_serverSocketUuidMap.find(uuid);
314 if (it != m_serverSocketUuidMap.end())
316 LogDebug("no such uuid");
319 m_serverSocketUuidMap.erase (uuid);
323 void BluetoothCBManager::serviceSearchRouter(int result, bt_device_sdp_info_s* sdp_info, void* user_data)
325 std::map<std::string, void*>::iterator it =
326 m_serviceSearchMap.find(sdp_info->remote_address);
327 std::map<std::string, managerType>::iterator managerIt =
328 m_serviceSearchManagerMap.find(sdp_info->remote_address);
330 LogDebug(sdp_info->remote_address);
332 if (it != m_serviceSearchMap.end() && managerIt != m_serviceSearchManagerMap.end())
334 if (managerIt->second == FROM_DEVICE)
336 ((BluetoothDeviceManager*)(it->second))->serviceSearchManualAnswer(result);
338 else if(managerIt->second == FROM_ADAPTER)
340 ((BluetoothAdapterManager*)(it->second))->serviceSearchManualAnswer(result);
345 LogDebug("manager is not registered");
349 void BluetoothCBManager::connectionStateRouter(int result, bt_socket_connection_state_e connection_state,
350 bt_socket_connection_s *connection)
352 std::map<std::string, void*>::iterator it;
353 std::map<std::string, managerType>::iterator managerIt;
355 if (connection->service_uuid == NULL)
357 LogDebug("Error service uuid NULL");
361 LogDebug(connection->service_uuid);
363 std::string uuid = connection->service_uuid;
365 if (connection->local_role == BT_SOCKET_SERVER)
367 LogDebug("Route servicehandler");
369 it = m_devicePrivateObjectMap.find(uuid);
370 managerIt = m_devicePrivateObjectManagerMap.find(uuid);
372 if (it != m_devicePrivateObjectMap.end() && managerIt != m_devicePrivateObjectManagerMap.end())
374 if(managerIt->second == FROM_SERVICE)
376 ((BluetoothServiceHandlerManager*)(it->second))->connectionStateChangedEmit(result, connection_state, connection);
382 it = m_devicePrivateObjectMap.find(connection->remote_address);
383 managerIt = m_devicePrivateObjectManagerMap.find(connection->remote_address);
385 if (it != m_devicePrivateObjectMap.end() && managerIt != m_devicePrivateObjectManagerMap.end())
387 if (managerIt->second == FROM_DEVICE)
389 LogDebug("Route device");
390 ((BluetoothDeviceManager*)(it->second))->connectToServiceByUUIDManualAnswer(result, connection_state, connection);
392 else if(managerIt->second == FROM_SOCKET)
394 LogDebug("Route socket");
395 ((BluetoothSocketManager*)(it->second))->connectionStateChangedEmit(result, connection_state, connection);
401 void BluetoothCBManager::socketDataReceivedRouter(bt_socket_received_data_s *data)
403 std::map<int, void*>::iterator it = m_socketPrivateObjectMap.find(data->socket_fd);
405 if (it != m_socketPrivateObjectMap.end())
407 ((BluetoothSocketManager*)(it->second))->setDataReceivedEmit(data);
411 LogDebug("manager is not registered");
416 void BluetoothCBManager::addServiceSearchCallback(std::string deviceAddress, void *data, managerType type)
418 if (m_serviceSearchMap.size() != 0)
420 ThrowMsg(WrtDeviceApis::Commons::AlreadyInUseException, "same operation now");
423 std::map<std::string, void*>::iterator it =
424 m_serviceSearchMap.find(deviceAddress);
426 if (it != m_serviceSearchMap.end())
428 ThrowMsg(WrtDeviceApis::Commons::AlreadyInUseException, "searvice search same device address");
431 if (bt_device_set_service_searched_cb(capi_bt_device_service_searched, this) != BT_ERROR_NONE)
433 ThrowMsg(WrtDeviceApis::Commons::UnknownException, "callback set error");
436 LogDebug(deviceAddress);
438 m_serviceSearchMap[deviceAddress] = data;
439 m_serviceSearchManagerMap[deviceAddress] = type;
443 void BluetoothCBManager::removeServiceSearchCallback(std::string deviceAddress)
445 std::map<std::string, void*>::iterator it =
446 m_serviceSearchMap.find(deviceAddress);
448 if (it == m_serviceSearchMap.end())
450 LogDebug("there is no such address");
455 m_serviceSearchMap.erase(deviceAddress);
456 m_serviceSearchManagerMap.erase(deviceAddress);
458 if (m_serviceSearchMap.size() == 0)
460 bt_device_unset_service_searched_cb();
464 void BluetoothCBManager::addConnectionStateCallback(std::string deviceAddress, void *data, managerType type)
466 std::map<std::string, void*>::iterator it =
467 m_devicePrivateObjectMap.find(deviceAddress);
469 if (it != m_devicePrivateObjectMap.end())
471 ThrowMsg(WrtDeviceApis::Commons::AlreadyInUseException, "connecting to same device address");
474 if (m_devicePrivateObjectMap.size() == 0)
476 if (bt_socket_set_connection_state_changed_cb(capi_callback_bt_socket_connection_state_changed, this) != BT_ERROR_NONE)
478 LogDebug("callback set error");
479 ThrowMsg(WrtDeviceApis::Commons::UnknownException, "callback set error");
482 LogDebug("connection callback for " << deviceAddress);
483 m_devicePrivateObjectMap[deviceAddress] = data;
484 m_devicePrivateObjectManagerMap[deviceAddress] = type;
487 void BluetoothCBManager::removeConnectionStateCallback(std::string deviceAddress, managerType type)
489 std::map<std::string, void*>::iterator it =
490 m_devicePrivateObjectMap.find(deviceAddress);
492 if (it == m_devicePrivateObjectMap.end())
494 LogDebug("there is no such address");
498 if (type == m_devicePrivateObjectManagerMap[deviceAddress])
500 m_devicePrivateObjectMap.erase(deviceAddress);
501 m_devicePrivateObjectManagerMap.erase(deviceAddress);
502 LogDebug("remove connection" << deviceAddress);
506 LogDebug("type different");
509 if (m_devicePrivateObjectMap.size() == 0)
511 LogDebug("remove connection state call back");
512 bt_socket_unset_connection_state_changed_cb();
516 void BluetoothCBManager::addSocketDataReceivedCallback(int socket, void *data)
518 std::map<int, void*>::iterator it = m_socketPrivateObjectMap.find(socket);
520 if (it != m_socketPrivateObjectMap.end())
522 ThrowMsg(WrtDeviceApis::Commons::AlreadyInUseException, "there is same socket");
525 if (m_socketPrivateObjectMap.size() == 0)
527 LogDebug("set data receive call back");
528 if (bt_socket_set_data_received_cb(capi_callback_bt_data_received, this) != BT_ERROR_NONE)
530 LogDebug("callback set error");
531 ThrowMsg(WrtDeviceApis::Commons::UnknownException, "callback set error");
535 m_socketPrivateObjectMap[socket] = data;
536 LogDebug(socket << "add socket (" << data << ")");
540 void BluetoothCBManager::removeSocketDataReceivedCallback(int socket)
542 std::map<int, void*>::iterator it = m_socketPrivateObjectMap.find(socket);
544 if (it == m_socketPrivateObjectMap.end())
546 LogDebug("there is no such socket");
550 LogDebug(socket << "remove socket " << m_socketPrivateObjectMap[socket] );
551 m_socketPrivateObjectMap.erase(socket);
553 if (m_socketPrivateObjectMap.size() == 0)
555 LogDebug("remove data receive call back");
556 bt_socket_unset_data_received_cb();
560 void BluetoothCBManager::clearAll()
562 m_adapterCallback.clear();
563 m_serverSocketUuidMap.clear();
564 m_devicePrivateObjectMap.clear();
565 m_devicePrivateObjectManagerMap.clear();
566 m_serviceSearchMap.clear();
567 m_serviceSearchManagerMap.clear();
568 m_socketPrivateObjectMap.clear();
570 void BluetoothCBManager::disconnectAllSockets()
572 // close connection socket
573 for (std::map<int, void*>::iterator it = m_socketPrivateObjectMap.begin(); it != m_socketPrivateObjectMap.end(); ++it)
575 bt_socket_disconnect_rfcomm((*it).first);
578 // close server socket
579 for (std::map<std::string, int>::iterator it = m_serverSocketUuidMap.begin(); it != m_serverSocketUuidMap.end(); ++it)
581 bt_socket_destroy_rfcomm((*it).second);