2 * Copyright (c) 2011 Samsung Electronics Co., Ltd All Rights Reserved
4 * Licensed under the Apache License, Version 2.0 (the License);
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an AS IS BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
17 #include "BluetoothServiceHandlerManager.h"
18 #include <dpl/assert.h>
19 #include <API/Bluetooth/BluetoothProperty.h>
20 #include <API/Bluetooth/BTDevice.h>
27 using namespace Platform;
29 BluetoothServiceHandlerManager::BluetoothServiceHandlerManager()
34 BluetoothServiceHandlerManager::~BluetoothServiceHandlerManager()
42 static void capi_callback_bt_connection_state_changed(int result, bt_socket_connection_state_e connection_state,
43 bt_socket_connection_s *connection, void *user_data)
46 ((BluetoothServiceHandlerManager*)(user_data))->connectionStateChangedEmit(result, connection_state, connection);
52 void BluetoothServiceHandlerManager::connectionStateChangedEmit(int result,
53 bt_socket_connection_state_e connection_state, bt_socket_connection_s *connection)
57 Api::Bluetooth::EventBTServiceOnConnectPtr event(new Api::Bluetooth::EventBTServiceOnConnect());
59 if (result == BT_ERROR_NONE)
61 Api::Bluetooth::BluetoothSocketData socketdata;
62 socketdata.uuid = m_Uuid;
63 socketdata.isServer = true;
64 socketdata.protocol = PROTOCOL_TYPE_RFCOMM_VALUE_INT;
65 socketdata.state = SOCKET_STATE_OPEN_VALUE_INT;
66 socketdata.connectedSocket = connection->socket_fd;
67 socketdata.registeredSocket = m_registerSocket;
68 socketdata.peerDevice.address = connection->remote_address;
69 socketdata.peerDevice.isConnected = true;
71 event->setExceptionCode(WrtDeviceApis::Commons::ExceptionCodes::None);
72 event->setSocketData(socketdata);
76 event->setExceptionCode(WrtDeviceApis::Commons::ExceptionCodes::UnknownException);
78 m_emitter->emit(event);
81 void BluetoothServiceHandlerManager::setRegisterSocket(int socket)
83 m_registerSocket = socket;
86 void BluetoothServiceHandlerManager::setName(std::string name)
91 void BluetoothServiceHandlerManager::setIsConnected(bool connected)
93 m_Connected = connected;
97 std::string BluetoothServiceHandlerManager::getName()
102 bool BluetoothServiceHandlerManager::getIsConnected()
107 int BluetoothServiceHandlerManager::setServiceHandlerListener(
108 const Api::Bluetooth::EventBTServiceOnConnectEmitterPtr& emitter)
110 if (bt_socket_set_connection_state_changed_cb(capi_callback_bt_connection_state_changed, this)
113 LogDebug("callback set error");
114 return BT_ERROR_NOT_INITIALIZED;
116 LogDebug("setServiceHandlerListener - OK");
119 return BT_ERROR_NONE;
123 std::string BluetoothServiceHandlerManager::getUUID()
128 void BluetoothServiceHandlerManager::setUUID(std::string uuid)
133 void BluetoothServiceHandlerManager::unRegister(const Api::Bluetooth::EventBTUnregisterRFCOMMServicePtr &event)
136 WrtDeviceApis::Commons::EventRequestReceiver<Api::Bluetooth::EventBTUnregisterRFCOMMService>::PostRequest(event);
139 void BluetoothServiceHandlerManager::OnRequestReceived(const Api::Bluetooth::EventBTUnregisterRFCOMMServicePtr& event)
145 std::map<std::string, Api::Bluetooth::BluetoothSocketData>::iterator it;
147 if (m_registerSocket < 0)
149 ThrowMsg(WrtDeviceApis::Commons::UnsupportedException, "no service socket");
152 if (bt_socket_destroy_rfcomm(m_registerSocket) != BT_ERROR_NONE)
154 ThrowMsg(WrtDeviceApis::Commons::UnsupportedException, "socket destroy error");
156 event->setExceptionCode(WrtDeviceApis::Commons::ExceptionCodes::None);
158 catch (const WrtDeviceApis::Commons::Exception& ex)
160 LogError("Exception: " << ex.GetMessage());
161 event->setExceptionCode(ex.getCode());
163 bt_adapter_unset_state_changed_cb();