Update change log and spec for wrt-plugins-tizen_0.4.9
[platform/framework/web/wrt-plugins-tizen.git] / src / Bluetooth / BluetoothServiceHandlerManager.cpp
1 //
2 // Tizen Web Device API
3 // Copyright (c) 2012 Samsung Electronics Co., Ltd.
4 //
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
8 //
9 // http://www.apache.org/licenses/LICENSE-2.0
10 //
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.
16 //
17
18 #include "BluetoothServiceHandlerManager.h"
19 #include "BluetoothCBManager.h"
20 #include <dpl/assert.h>
21 #include "BluetoothProperty.h"
22 #include "BTDevice.h"
23
24
25 namespace DeviceAPI {
26 namespace Bluetooth {
27
28 BluetoothServiceHandlerManager::BluetoothServiceHandlerManager()
29 {
30         LogDebug("Enter");
31 }
32
33 BluetoothServiceHandlerManager::~BluetoothServiceHandlerManager() 
34 {
35         LogDebug("Enter");
36
37         BluetoothCBManagerSingleton::Instance().removeConnectionStateCallback(m_Uuid, BluetoothCBManager::FROM_SERVICE);
38 }
39
40
41
42 void BluetoothServiceHandlerManager::connectionStateChangedEmit(int result, 
43         bt_socket_connection_state_e connection_state,  bt_socket_connection_s *connection)
44 {
45         LogDebug("OK" << connection_state);
46
47         EventBTServiceOnConnectPtr event(new EventBTServiceOnConnect());
48
49         if (result == BT_ERROR_NONE)
50         {
51                 if (connection_state == BT_SOCKET_CONNECTED)
52                 {
53         
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;
63                         m_Connected = true;
64                         event->setExceptionCode(WrtDeviceApis::Commons::ExceptionCodes::None);
65                         event->setSocketData(socketdata);
66                 }
67                 else 
68                 {
69                         m_Connected = false;
70                         return;
71                 }
72         }
73         else
74         {
75                 event->setExceptionCode(WrtDeviceApis::Commons::ExceptionCodes::UnknownException);
76         }
77         m_emitter->emit(event);
78 }
79
80 void BluetoothServiceHandlerManager::setRegisterSocket(int socket)
81 {
82         m_registerSocket = socket;
83 }
84
85 void BluetoothServiceHandlerManager::setName(std::string name)
86 {
87         m_Name = name;
88 }
89
90 void BluetoothServiceHandlerManager::setIsConnected(bool connected)
91 {
92         m_Connected =  connected;
93
94 }
95
96 std::string BluetoothServiceHandlerManager::getName()
97 {
98         return m_Name;
99 }
100
101 bool BluetoothServiceHandlerManager::getIsConnected()
102 {
103         return m_Connected;
104 }
105
106 void BluetoothServiceHandlerManager::setServiceHandlerListener(
107         const EventBTServiceOnConnectEmitterPtr& emitter)
108 {
109         // pass uuid for server socket
110         BluetoothCBManagerSingleton::Instance().addConnectionStateCallback(m_Uuid, 
111                                 (void*)this, BluetoothCBManager::FROM_SERVICE);
112
113         LogDebug("setServiceHandlerListener - OK");
114         m_emitter = emitter;
115 }
116
117 EventBTServiceOnConnectEmitterPtr BluetoothServiceHandlerManager::getServiceHandlerListener()
118 {
119         return m_emitter;
120 }
121
122 std::string BluetoothServiceHandlerManager::getUUID()
123 {
124         return m_Uuid;
125 }
126
127 void BluetoothServiceHandlerManager::setUUID(std::string uuid)
128 {
129         m_Uuid = uuid;
130 }
131
132 void BluetoothServiceHandlerManager::unRegister(const EventBTUnregisterRFCOMMServicePtr &event)
133 {
134         LogDebug("Enter");
135         WrtDeviceApis::Commons::EventRequestReceiver<EventBTUnregisterRFCOMMService>::PostRequest(event);
136 }
137
138 void BluetoothServiceHandlerManager::OnRequestReceived(const EventBTUnregisterRFCOMMServicePtr& event) 
139 {
140         LogDebug("Enter");
141         
142         try 
143         {
144                 std::map<std::string, BluetoothSocketData>::iterator it;
145                 
146                 if (m_registerSocket < 0)
147                 {
148                         ThrowMsg(WrtDeviceApis::Commons::UnknownException, "no service socket");
149                 }
150
151                 if (bt_socket_destroy_rfcomm(m_registerSocket) != BT_ERROR_NONE)
152                 {
153                         ThrowMsg(WrtDeviceApis::Commons::UnknownException, "socket destroy error");
154                 }
155                 event->setExceptionCode(WrtDeviceApis::Commons::ExceptionCodes::None);
156                 
157                 BluetoothCBManagerSingleton::Instance().unregisterSocket(m_Uuid);
158                 BluetoothCBManagerSingleton::Instance().removeConnectionStateCallback(m_Uuid, BluetoothCBManager::FROM_SERVICE);
159                 
160         }
161         catch (const WrtDeviceApis::Commons::Exception& ex) 
162         {
163                 LogError("Exception: " << ex.GetMessage());
164         event->setExceptionCode(ex.getCode());
165         }       
166         bt_adapter_unset_state_changed_cb();
167
168 }
169
170 }
171 }