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 <GlobalContextManager.h>
19 #include <PlatformException.h>
21 #include "BluetoothSocket.h"
22 #include "BluetoothAdapter.h"
23 #include "plugin_config.h"
24 #include "JSBluetoothDevice.h"
27 #include <TimeTracer.h>
29 using namespace DeviceAPI::Common;
34 BluetoothSocket::BluetoothSocket(bt_socket_connection_s *connection)
36 mConnectedSocket = connection->socket_fd;
37 mUUID = std::string(connection->service_uuid);
40 bt_device_info_s *deviceInfo = NULL;
41 if(bt_adapter_get_bonded_device_info(connection->remote_address, &deviceInfo) == BT_ERROR_NONE && deviceInfo != NULL) {
42 BluetoothDeviceSharedPtr device(new BluetoothDevice(deviceInfo));
44 bt_adapter_free_device_info(deviceInfo);
48 BluetoothSocket::~BluetoothSocket()
51 if(bt_socket_disconnect_rfcomm(mConnectedSocket) != BT_ERROR_NONE) {
52 LoggerW("Already disconnected");
55 if(!BluetoothAdapter::getInstance()->closeConnectedSocket(mConnectedSocket)) {
56 LoggerW("Already done");
61 bool BluetoothSocket::setOnMessage(JSContextRef context, JSObjectRef onMessage)
63 MultiCallbackUserDataPtr callback(
64 new MultiCallbackUserData(GlobalContextManager::getInstance()->getGlobalContext(context)));
66 LoggerW("Can't create MultiCallbackUserData");
69 callback->setCallback("onmessage", onMessage);
70 mOnMessage = callback;
72 return mLocalProperty.setProperty(context, BLUETOOTH_SOCKET_ONMESSAGE, onMessage);
75 bool BluetoothSocket::setOnClose(JSContextRef context, JSObjectRef onClose)
77 MultiCallbackUserDataPtr callback(
78 new MultiCallbackUserData(GlobalContextManager::getInstance()->getGlobalContext(context)));
80 LoggerW("Can't create MultiCallbackUserData");
83 callback->setCallback("onclose", onClose);
86 return mLocalProperty.setProperty(context, BLUETOOTH_SOCKET_ONCLOSE, onClose);
89 bool BluetoothSocket::setOnError(JSContextRef context, JSObjectRef onError)
91 MultiCallbackUserDataPtr callback(
92 new MultiCallbackUserData(GlobalContextManager::getInstance()->getGlobalContext(context)));
94 LoggerW("Can't create MultiCallbackUserData");
97 callback->setCallback("onerror", onError);
100 return mLocalProperty.setProperty(context, BLUETOOTH_SOCKET_ONERROR, onError);
103 std::string BluetoothSocket::getUUID() const
108 void BluetoothSocket::setConnectionState(bool isConnected)
110 mIsConnected = isConnected;
113 bool BluetoothSocket::getConnectionState()
118 JSValueRef BluetoothSocket::getPeer(JSContextRef context)
121 JSValueRef peer = mLocalProperty.getProperty(context, BLUETOOTH_SOCKET_PEER);
122 if(peer == NULL && mPeer != NULL) {
123 peer = JSBluetoothDevice::createJSObject(context, mPeer);
124 mLocalProperty.setProperty(context, BLUETOOTH_DEVICE_DEVICE_CLASS, peer);
129 return JSBluetoothDevice::createJSObject(context, mPeer);
132 MultiCallbackUserDataPtr BluetoothSocket::getOnMessage() const
137 JSValueRef BluetoothSocket::getOnMessage(JSContextRef context)
139 JSValueRef onMessage = mLocalProperty.getProperty(context, BLUETOOTH_SOCKET_ONMESSAGE);
140 if(onMessage == NULL) {
141 LoggerD("onmessage is null");
142 return JSValueMakeNull(context);
148 MultiCallbackUserDataPtr BluetoothSocket::getOnClose() const
153 JSValueRef BluetoothSocket::getOnClose(JSContextRef context)
155 JSValueRef onClose = mLocalProperty.getProperty(context, BLUETOOTH_SOCKET_ONCLOSE);
156 if(onClose == NULL) {
157 LoggerD("onclose is null");
158 return JSValueMakeNull(context);
164 MultiCallbackUserDataPtr BluetoothSocket::getOnError() const
169 JSValueRef BluetoothSocket::getOnError(JSContextRef context)
171 JSValueRef onError = mLocalProperty.getProperty(context, BLUETOOTH_SOCKET_ONERROR);
172 if(onError == NULL) {
173 LoggerD("onerror is null");
174 return JSValueMakeNull(context);
180 unsigned long BluetoothSocket::writeData(char* data, unsigned long size)
182 unsigned long ret = 0;
183 TIME_TRACER_ITEM_BEGIN("writeData::bt_socket_send_data", 1);
184 if(bt_socket_send_data(mConnectedSocket, data, static_cast<int>(size)) == BT_ERROR_NONE) {
185 TIME_TRACER_ITEM_END("writeData::bt_socket_send_data", 1);
186 LoggerD("bt_socket_send_data() succeeded");
190 TIME_TRACER_ITEM_END("writeData::bt_socket_send_data", 1);
191 throw UnknownException("Unknown error");
198 void BluetoothSocket::storeRecivedData(char *data, unsigned long size)
200 for(unsigned long i = 0; i < size; i++) {
201 mReceivedData.push_back(static_cast<signed char>(data[i]));
205 std::vector<signed char> BluetoothSocket::readData()
207 std::vector<signed char> result (mReceivedData);
208 mReceivedData.clear();
213 void BluetoothSocket::close()
216 LoggerD("Already disconnected");
220 TIME_TRACER_ITEM_BEGIN("close::bt_socket_disconnect_rfcomm", 1);
221 if(bt_socket_disconnect_rfcomm(mConnectedSocket) != BT_ERROR_NONE) {
222 TIME_TRACER_ITEM_END("close::bt_socket_disconnect_rfcomm", 1);
223 LoggerE("bt_socket_disconnect_rfcomm() failed");
224 throw UnknownException("Unknown error");
226 TIME_TRACER_ITEM_END("close::bt_socket_disconnect_rfcomm", 1);
228 mIsConnected = false;