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)
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()
52 if(bt_socket_disconnect_rfcomm(mConnectedSocket) != BT_ERROR_NONE) {
53 LogWarning("Already disconnected");
56 if(!BluetoothAdapter::getInstance()->closeConnectedSocket(mConnectedSocket)) {
57 LogWarning("Already done");
62 bool BluetoothSocket::setOnMessage(JSContextRef context, JSObjectRef onMessage)
65 MultiCallbackUserDataPtr callback(
66 new MultiCallbackUserData(GlobalContextManager::getInstance()->getGlobalContext(context)));
68 LogWarning("Can't create MultiCallbackUserData");
71 callback->setCallback("onmessage", onMessage);
72 mOnMessage = callback;
74 return mLocalProperty.setProperty(context, BLUETOOTH_SOCKET_ONMESSAGE, onMessage);
77 bool BluetoothSocket::setOnClose(JSContextRef context, JSObjectRef onClose)
80 MultiCallbackUserDataPtr callback(
81 new MultiCallbackUserData(GlobalContextManager::getInstance()->getGlobalContext(context)));
83 LogWarning("Can't create MultiCallbackUserData");
86 callback->setCallback("onclose", onClose);
89 return mLocalProperty.setProperty(context, BLUETOOTH_SOCKET_ONCLOSE, onClose);
92 bool BluetoothSocket::setOnError(JSContextRef context, JSObjectRef onError)
95 MultiCallbackUserDataPtr callback(
96 new MultiCallbackUserData(GlobalContextManager::getInstance()->getGlobalContext(context)));
98 LogWarning("Can't create MultiCallbackUserData");
101 callback->setCallback("onerror", onError);
104 return mLocalProperty.setProperty(context, BLUETOOTH_SOCKET_ONERROR, onError);
107 std::string BluetoothSocket::getUUID() const
113 void BluetoothSocket::setConnectionState(bool isConnected)
116 mIsConnected = isConnected;
119 bool BluetoothSocket::getConnectionState()
125 JSValueRef BluetoothSocket::getPeer(JSContextRef context)
129 JSValueRef peer = mLocalProperty.getProperty(context, BLUETOOTH_SOCKET_PEER);
130 if(peer == NULL && mPeer != NULL) {
131 peer = JSBluetoothDevice::createJSObject(context, mPeer);
132 mLocalProperty.setProperty(context, BLUETOOTH_DEVICE_DEVICE_CLASS, peer);
137 return JSBluetoothDevice::createJSObject(context, mPeer);
140 MultiCallbackUserDataPtr BluetoothSocket::getOnMessage() const
145 JSValueRef BluetoothSocket::getOnMessage(JSContextRef context)
148 JSValueRef onMessage = mLocalProperty.getProperty(context, BLUETOOTH_SOCKET_ONMESSAGE);
149 if(onMessage == NULL) {
150 LogDebug("onmessage is null");
151 return JSValueMakeNull(context);
157 MultiCallbackUserDataPtr BluetoothSocket::getOnClose() const
162 JSValueRef BluetoothSocket::getOnClose(JSContextRef context)
166 JSValueRef onClose = mLocalProperty.getProperty(context, BLUETOOTH_SOCKET_ONCLOSE);
167 if(onClose == NULL) {
168 LogDebug("onclose is null");
169 return JSValueMakeNull(context);
175 MultiCallbackUserDataPtr BluetoothSocket::getOnError() const
180 JSValueRef BluetoothSocket::getOnError(JSContextRef context)
184 JSValueRef onError = mLocalProperty.getProperty(context, BLUETOOTH_SOCKET_ONERROR);
185 if(onError == NULL) {
186 LogDebug("onerror is null");
187 return JSValueMakeNull(context);
193 unsigned long BluetoothSocket::writeData(char* data, unsigned long size)
197 unsigned long ret = 0;
198 if(bt_socket_send_data(mConnectedSocket, data, static_cast<int>(size)) == BT_ERROR_NONE) {
199 LogDebug("bt_socket_send_data() succeeded");
203 throw UnknownException("Unknown error");
210 void BluetoothSocket::storeRecivedData(char *data, unsigned long size)
214 for(unsigned long i = 0; i < size; i++) {
215 mReceivedData.push_back(static_cast<signed char>(data[i]));
219 std::vector<signed char> BluetoothSocket::readData()
223 std::vector<signed char> result (mReceivedData);
224 mReceivedData.clear();
229 void BluetoothSocket::close()
234 LogDebug("Already disconnected");
238 if(bt_socket_disconnect_rfcomm(mConnectedSocket) != BT_ERROR_NONE) {
239 LogError("bt_socket_disconnect_rfcomm() failed");
243 mIsConnected = false;
244 if(!BluetoothAdapter::getInstance()->closeConnectedSocket(mConnectedSocket)) {
245 throw UnknownException("Unknown error");