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"
28 using namespace DeviceAPI::Common;
33 BluetoothSocket::BluetoothSocket(bt_socket_connection_s *connection)
35 mConnectedSocket = connection->socket_fd;
36 mUUID = std::string(connection->service_uuid);
39 bt_device_info_s *deviceInfo = NULL;
40 if(bt_adapter_get_bonded_device_info(connection->remote_address, &deviceInfo) == BT_ERROR_NONE && deviceInfo != NULL) {
41 BluetoothDeviceSharedPtr device(new BluetoothDevice(deviceInfo));
43 bt_adapter_free_device_info(deviceInfo);
47 BluetoothSocket::~BluetoothSocket()
50 if(bt_socket_disconnect_rfcomm(mConnectedSocket) != BT_ERROR_NONE) {
51 LoggerW("Already disconnected");
54 if(!BluetoothAdapter::getInstance()->closeConnectedSocket(mConnectedSocket)) {
55 LoggerW("Already done");
60 bool BluetoothSocket::setOnMessage(JSContextRef context, JSObjectRef onMessage)
62 MultiCallbackUserDataPtr callback(
63 new MultiCallbackUserData(GlobalContextManager::getInstance()->getGlobalContext(context)));
65 LoggerW("Can't create MultiCallbackUserData");
68 callback->setCallback("onmessage", onMessage);
69 mOnMessage = callback;
71 return mLocalProperty.setProperty(context, BLUETOOTH_SOCKET_ONMESSAGE, onMessage);
74 bool BluetoothSocket::setOnClose(JSContextRef context, JSObjectRef onClose)
76 MultiCallbackUserDataPtr callback(
77 new MultiCallbackUserData(GlobalContextManager::getInstance()->getGlobalContext(context)));
79 LoggerW("Can't create MultiCallbackUserData");
82 callback->setCallback("onclose", onClose);
85 return mLocalProperty.setProperty(context, BLUETOOTH_SOCKET_ONCLOSE, onClose);
88 bool BluetoothSocket::setOnError(JSContextRef context, JSObjectRef onError)
90 MultiCallbackUserDataPtr callback(
91 new MultiCallbackUserData(GlobalContextManager::getInstance()->getGlobalContext(context)));
93 LoggerW("Can't create MultiCallbackUserData");
96 callback->setCallback("onerror", onError);
99 return mLocalProperty.setProperty(context, BLUETOOTH_SOCKET_ONERROR, onError);
102 std::string BluetoothSocket::getUUID() const
107 void BluetoothSocket::setConnectionState(bool isConnected)
109 mIsConnected = isConnected;
112 bool BluetoothSocket::getConnectionState()
117 JSValueRef BluetoothSocket::getPeer(JSContextRef context)
120 JSValueRef peer = mLocalProperty.getProperty(context, BLUETOOTH_SOCKET_PEER);
121 if(peer == NULL && mPeer != NULL) {
122 peer = JSBluetoothDevice::createJSObject(context, mPeer);
123 mLocalProperty.setProperty(context, BLUETOOTH_DEVICE_DEVICE_CLASS, peer);
128 return JSBluetoothDevice::createJSObject(context, mPeer);
131 MultiCallbackUserDataPtr BluetoothSocket::getOnMessage() const
136 JSValueRef BluetoothSocket::getOnMessage(JSContextRef context)
138 JSValueRef onMessage = mLocalProperty.getProperty(context, BLUETOOTH_SOCKET_ONMESSAGE);
139 if(onMessage == NULL) {
140 LoggerD("onmessage is null");
141 return JSValueMakeNull(context);
147 MultiCallbackUserDataPtr BluetoothSocket::getOnClose() const
152 JSValueRef BluetoothSocket::getOnClose(JSContextRef context)
154 JSValueRef onClose = mLocalProperty.getProperty(context, BLUETOOTH_SOCKET_ONCLOSE);
155 if(onClose == NULL) {
156 LoggerD("onclose is null");
157 return JSValueMakeNull(context);
163 MultiCallbackUserDataPtr BluetoothSocket::getOnError() const
168 JSValueRef BluetoothSocket::getOnError(JSContextRef context)
170 JSValueRef onError = mLocalProperty.getProperty(context, BLUETOOTH_SOCKET_ONERROR);
171 if(onError == NULL) {
172 LoggerD("onerror is null");
173 return JSValueMakeNull(context);
179 unsigned long BluetoothSocket::writeData(char* data, unsigned long size)
181 unsigned long ret = 0;
182 if(bt_socket_send_data(mConnectedSocket, data, static_cast<int>(size)) == BT_ERROR_NONE) {
183 LoggerD("bt_socket_send_data() succeeded");
187 throw UnknownException("Unknown error");
194 void BluetoothSocket::storeRecivedData(char *data, unsigned long size)
196 for(unsigned long i = 0; i < size; i++) {
197 mReceivedData.push_back(static_cast<signed char>(data[i]));
201 std::vector<signed char> BluetoothSocket::readData()
203 std::vector<signed char> result (mReceivedData);
204 mReceivedData.clear();
209 void BluetoothSocket::close()
212 LoggerD("Already disconnected");
216 if(bt_socket_disconnect_rfcomm(mConnectedSocket) != BT_ERROR_NONE) {
217 LoggerE("bt_socket_disconnect_rfcomm() failed");
218 throw UnknownException("Unknown error");
221 mIsConnected = false;