Git Init
[profile/ivi/wrt-plugins-tizen.git] / src / platform / Tizen / Bluetooth / BluetoothSocketManager.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 "BluetoothSocketManager.h"
18 #include <dpl/assert.h>
19
20 using namespace TizenApis::Api;
21
22
23 namespace TizenApis {
24 namespace Platform {
25 namespace Bluetooth {
26
27 using namespace Platform;
28
29 BluetoothSocketManager::BluetoothSocketManager()
30 {
31         LogDebug("Enter");
32         m_socketData.connectedSocket = -1;
33         m_socketData.registeredSocket = -1;
34 }
35
36 BluetoothSocketManager::~BluetoothSocketManager() 
37 {
38         LogDebug("Enter");
39 }
40
41
42 namespace {
43
44 static void capi_callback_bt_connection_state_changed(int result, bt_socket_connection_state_e connection_state, 
45         bt_socket_connection_s *connection, void *user_data)
46 {
47         LogDebug("OK");
48         ((BluetoothSocketManager*)(user_data))->connectionStateChangedEmit(result, connection_state, connection);
49 }
50
51 static void capi_callback_bt_data_received(bt_socket_received_data_s *data, void *user_data)
52 {
53         ((BluetoothSocketManager*)(user_data))->setDataReceivedEmit(data);
54 }
55
56 }
57
58 void BluetoothSocketManager::setReadData(EventBTReadDataType readData)
59 {
60         DPL::Mutex::ScopedLock lock(&m_mtx);
61         m_readData = readData;
62 }
63
64 void BluetoothSocketManager::setDataReceivedEmit(bt_socket_received_data_s *data)
65 {
66         assert(m_socketData.connectedSocket == data->socket_fd);
67         assert(data->data != NULL);
68         assert(data->data_size != 0);
69
70
71         LogDebug("OK" << data->data[0] << data->data[1] << " :" << data->data_size);
72
73         DPL::Mutex::ScopedLock lock(&m_mtx);
74                 
75         EventBTSocketNotificationPtr event(new EventBTSocketNotification());
76         event->setSocketData(m_socketData);
77         event->setReadData(data->data, data->data_size);
78         event->setConnectionState(EventBTSocketNotification::DATARECEIVED);
79         m_EventBTSocketNotificationEmitterPtr->emit(event);
80 }
81
82 void BluetoothSocketManager::connectionStateChangedEmit(int result, 
83         bt_socket_connection_state_e connection_state,  bt_socket_connection_s *connection)
84 {
85         assert(m_socketData.connectedSocket == connection->socket_fd);
86         assert(strcmp(m_socketData.peerDevice.address.data(), connection->remote_address) == 0);
87
88         LogDebug("OK");
89         
90         EventBTSocketNotificationPtr event(new EventBTSocketNotification());
91         event->setConnectionState(EventBTSocketNotification::SOCKETERROR);              
92
93         if (result == BT_ERROR_NONE)
94         {
95                 if (connection_state == BT_SOCKET_DISCONNECTED)
96                 {
97                         event->setConnectionState(EventBTSocketNotification::DISCONNECTED);
98                 }
99         }
100
101         event->setSocketData(m_socketData);
102         m_EventBTSocketNotificationEmitterPtr->emit(event);
103
104 }
105 void BluetoothSocketManager::setSocketData(BluetoothSocketData socketData)
106 {
107         m_socketData = socketData;
108 }
109
110
111 int BluetoothSocketManager::setSocketNotifier(EventBTSocketNotificationEmitterPtr emitter)
112 {
113         if (bt_socket_set_data_received_cb(capi_callback_bt_data_received, this) != BT_ERROR_NONE ||
114                 bt_socket_set_connection_state_changed_cb(capi_callback_bt_connection_state_changed, this) != BT_ERROR_NONE)
115         {
116                 LogDebug("callback set error");
117                 return BT_ERROR_NOT_INITIALIZED;
118         }
119
120         m_EventBTSocketNotificationEmitterPtr = emitter;
121         
122         return BT_ERROR_NONE;
123         
124 }
125 int BluetoothSocketManager::writeData(const char *data, const unsigned int length)
126 {
127         LogDebug(length);
128
129         if (m_socketData.connectedSocket < 0)
130         {
131                 return BT_ERROR_NOT_INITIALIZED;
132         }
133
134                         
135         if (data == NULL || length == 0)
136         {
137                 return BT_ERROR_INVALID_PARAMETER;
138         }
139
140
141         return  bt_socket_send_data(m_socketData.connectedSocket, data, length);
142 }
143
144 EventBTReadDataType BluetoothSocketManager::readData()
145 {
146         DPL::Mutex::ScopedLock lock(&m_mtx);
147         
148         return m_readData;
149 }
150
151
152 int BluetoothSocketManager::close()
153 {
154         DPL::Mutex::ScopedLock lock(&m_mtx);
155         int ret = 0;
156         
157
158         if (m_socketData.isServer == true)
159         {
160                 ret = bt_socket_destroy_rfcomm(m_socketData.registeredSocket);
161         }
162         else
163         {
164                 ret = bt_socket_disconnect_rfcomm(m_socketData.connectedSocket);
165         }
166         
167         return ret;
168 }
169
170 std::string BluetoothSocketManager::getUUID()
171 {
172         return m_socketData.uuid;
173 }
174 unsigned int BluetoothSocketManager::getProtocol()
175 {
176         return m_socketData.protocol;
177 }
178 unsigned int BluetoothSocketManager::getState()
179 {
180         return m_socketData.state;
181 }
182 BluetoothDeviceData BluetoothSocketManager::getPeer()
183 {
184         return m_socketData.peerDevice;
185 }
186 }
187 }
188 }