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 "BluetoothSocketManager.h"
18 #include <dpl/assert.h>
20 using namespace TizenApis::Api;
27 using namespace Platform;
29 BluetoothSocketManager::BluetoothSocketManager()
32 m_socketData.connectedSocket = -1;
33 m_socketData.registeredSocket = -1;
36 BluetoothSocketManager::~BluetoothSocketManager()
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)
48 ((BluetoothSocketManager*)(user_data))->connectionStateChangedEmit(result, connection_state, connection);
51 static void capi_callback_bt_data_received(bt_socket_received_data_s *data, void *user_data)
53 ((BluetoothSocketManager*)(user_data))->setDataReceivedEmit(data);
58 void BluetoothSocketManager::setReadData(EventBTReadDataType readData)
60 DPL::Mutex::ScopedLock lock(&m_mtx);
61 m_readData = readData;
64 void BluetoothSocketManager::setDataReceivedEmit(bt_socket_received_data_s *data)
66 assert(m_socketData.connectedSocket == data->socket_fd);
67 assert(data->data != NULL);
68 assert(data->data_size != 0);
71 LogDebug("OK" << data->data[0] << data->data[1] << " :" << data->data_size);
73 DPL::Mutex::ScopedLock lock(&m_mtx);
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);
82 void BluetoothSocketManager::connectionStateChangedEmit(int result,
83 bt_socket_connection_state_e connection_state, bt_socket_connection_s *connection)
85 assert(m_socketData.connectedSocket == connection->socket_fd);
86 assert(strcmp(m_socketData.peerDevice.address.data(), connection->remote_address) == 0);
90 EventBTSocketNotificationPtr event(new EventBTSocketNotification());
91 event->setConnectionState(EventBTSocketNotification::SOCKETERROR);
93 if (result == BT_ERROR_NONE)
95 if (connection_state == BT_SOCKET_DISCONNECTED)
97 event->setConnectionState(EventBTSocketNotification::DISCONNECTED);
101 event->setSocketData(m_socketData);
102 m_EventBTSocketNotificationEmitterPtr->emit(event);
105 void BluetoothSocketManager::setSocketData(BluetoothSocketData socketData)
107 m_socketData = socketData;
111 int BluetoothSocketManager::setSocketNotifier(EventBTSocketNotificationEmitterPtr emitter)
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)
116 LogDebug("callback set error");
117 return BT_ERROR_NOT_INITIALIZED;
120 m_EventBTSocketNotificationEmitterPtr = emitter;
122 return BT_ERROR_NONE;
125 int BluetoothSocketManager::writeData(const char *data, const unsigned int length)
129 if (m_socketData.connectedSocket < 0)
131 return BT_ERROR_NOT_INITIALIZED;
135 if (data == NULL || length == 0)
137 return BT_ERROR_INVALID_PARAMETER;
141 return bt_socket_send_data(m_socketData.connectedSocket, data, length);
144 EventBTReadDataType BluetoothSocketManager::readData()
146 DPL::Mutex::ScopedLock lock(&m_mtx);
152 int BluetoothSocketManager::close()
154 DPL::Mutex::ScopedLock lock(&m_mtx);
158 if (m_socketData.isServer == true)
160 ret = bt_socket_destroy_rfcomm(m_socketData.registeredSocket);
164 ret = bt_socket_disconnect_rfcomm(m_socketData.connectedSocket);
170 std::string BluetoothSocketManager::getUUID()
172 return m_socketData.uuid;
174 unsigned int BluetoothSocketManager::getProtocol()
176 return m_socketData.protocol;
178 unsigned int BluetoothSocketManager::getState()
180 return m_socketData.state;
182 BluetoothDeviceData BluetoothSocketManager::getPeer()
184 return m_socketData.peerDevice;