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