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.
18 #include "BluetoothServiceHandlerManager.h"
19 #include "BluetoothCBManager.h"
20 #include <dpl/assert.h>
21 #include "BluetoothProperty.h"
28 BluetoothServiceHandlerManager::BluetoothServiceHandlerManager()
33 BluetoothServiceHandlerManager::~BluetoothServiceHandlerManager()
37 BluetoothCBManagerSingleton::Instance().removeConnectionStateCallback(m_Uuid, BluetoothCBManager::FROM_SERVICE);
42 void BluetoothServiceHandlerManager::connectionStateChangedEmit(int result,
43 bt_socket_connection_state_e connection_state, bt_socket_connection_s *connection)
45 LogDebug("OK" << connection_state);
47 EventBTServiceOnConnectPtr event(new EventBTServiceOnConnect());
49 if (result == BT_ERROR_NONE)
51 if (connection_state == BT_SOCKET_CONNECTED)
54 BluetoothSocketData socketdata;
55 socketdata.uuid = m_Uuid;
56 socketdata.isServer = true;
57 socketdata.protocol = PROTOCOL_TYPE_RFCOMM_VALUE_INT;
58 socketdata.state = SOCKET_STATE_OPEN_VALUE_INT;
59 socketdata.connectedSocket = connection->socket_fd;
60 socketdata.registeredSocket = m_registerSocket;
61 socketdata.peerDevice.address = connection->remote_address;
62 socketdata.peerDevice.isConnected = true;
64 event->setExceptionCode(WrtDeviceApis::Commons::ExceptionCodes::None);
65 event->setSocketData(socketdata);
75 event->setExceptionCode(WrtDeviceApis::Commons::ExceptionCodes::UnknownException);
77 m_emitter->emit(event);
80 void BluetoothServiceHandlerManager::setRegisterSocket(int socket)
82 m_registerSocket = socket;
85 void BluetoothServiceHandlerManager::setName(std::string name)
90 void BluetoothServiceHandlerManager::setIsConnected(bool connected)
92 m_Connected = connected;
96 std::string BluetoothServiceHandlerManager::getName()
101 bool BluetoothServiceHandlerManager::getIsConnected()
106 void BluetoothServiceHandlerManager::setServiceHandlerListener(
107 const EventBTServiceOnConnectEmitterPtr& emitter)
109 // pass uuid for server socket
110 BluetoothCBManagerSingleton::Instance().addConnectionStateCallback(m_Uuid,
111 (void*)this, BluetoothCBManager::FROM_SERVICE);
113 LogDebug("setServiceHandlerListener - OK");
117 EventBTServiceOnConnectEmitterPtr BluetoothServiceHandlerManager::getServiceHandlerListener()
122 std::string BluetoothServiceHandlerManager::getUUID()
127 void BluetoothServiceHandlerManager::setUUID(std::string uuid)
132 void BluetoothServiceHandlerManager::unRegister(const EventBTUnregisterRFCOMMServicePtr &event)
135 WrtDeviceApis::Commons::EventRequestReceiver<EventBTUnregisterRFCOMMService>::PostRequest(event);
138 void BluetoothServiceHandlerManager::OnRequestReceived(const EventBTUnregisterRFCOMMServicePtr& event)
144 std::map<std::string, BluetoothSocketData>::iterator it;
146 if (m_registerSocket < 0)
148 ThrowMsg(WrtDeviceApis::Commons::UnknownException, "no service socket");
151 if (bt_socket_destroy_rfcomm(m_registerSocket) != BT_ERROR_NONE)
153 ThrowMsg(WrtDeviceApis::Commons::UnknownException, "socket destroy error");
155 event->setExceptionCode(WrtDeviceApis::Commons::ExceptionCodes::None);
157 BluetoothCBManagerSingleton::Instance().unregisterSocket(m_Uuid);
158 BluetoothCBManagerSingleton::Instance().removeConnectionStateCallback(m_Uuid, BluetoothCBManager::FROM_SERVICE);
161 catch (const WrtDeviceApis::Commons::Exception& ex)
163 LogError("Exception: " << ex.GetMessage());
164 event->setExceptionCode(ex.getCode());
166 bt_adapter_unset_state_changed_cb();