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, Common::SecurityAccessor* accessor)
36 Common::SecurityAccessor();
37 Common::SecurityAccessor::copyAceCheckAccessFunction(accessor);
39 mConnectedSocket = connection->socket_fd;
40 mUUID = std::string(connection->service_uuid);
43 bt_device_info_s *deviceInfo = NULL;
44 if(bt_adapter_get_bonded_device_info(connection->remote_address, &deviceInfo) == BT_ERROR_NONE && deviceInfo != NULL) {
45 BluetoothDeviceSharedPtr device(new BluetoothDevice(deviceInfo));
46 device->copyAceCheckAccessFunction(accessor);
48 bt_adapter_free_device_info(deviceInfo);
52 BluetoothSocket::~BluetoothSocket()
55 if(bt_socket_disconnect_rfcomm(mConnectedSocket) != BT_ERROR_NONE) {
56 LoggerW("Already disconnected");
59 if(!BluetoothAdapter::getInstance()->closeConnectedSocket(mConnectedSocket)) {
60 LoggerW("Already done");
65 bool BluetoothSocket::setOnMessage(JSContextRef context, JSObjectRef onMessage)
67 MultiCallbackUserDataPtr callback(
68 new MultiCallbackUserData(GlobalContextManager::getInstance()->getGlobalContext(context)));
70 LoggerW("Can't create MultiCallbackUserData");
73 callback->setCallback("onmessage", onMessage);
74 mOnMessage = callback;
76 return mLocalProperty.setProperty(context, BLUETOOTH_SOCKET_ONMESSAGE, onMessage);
79 bool BluetoothSocket::setOnClose(JSContextRef context, JSObjectRef onClose)
81 MultiCallbackUserDataPtr callback(
82 new MultiCallbackUserData(GlobalContextManager::getInstance()->getGlobalContext(context)));
84 LoggerW("Can't create MultiCallbackUserData");
87 callback->setCallback("onclose", onClose);
90 return mLocalProperty.setProperty(context, BLUETOOTH_SOCKET_ONCLOSE, onClose);
93 bool BluetoothSocket::setOnError(JSContextRef context, JSObjectRef onError)
95 MultiCallbackUserDataPtr callback(
96 new MultiCallbackUserData(GlobalContextManager::getInstance()->getGlobalContext(context)));
98 LoggerW("Can't create MultiCallbackUserData");
101 callback->setCallback("onerror", onError);
104 return mLocalProperty.setProperty(context, BLUETOOTH_SOCKET_ONERROR, onError);
107 std::string BluetoothSocket::getUUID() const
112 void BluetoothSocket::setConnectionState(bool isConnected)
114 mIsConnected = isConnected;
117 bool BluetoothSocket::getConnectionState()
122 JSValueRef BluetoothSocket::getPeer(JSContextRef context)
125 JSValueRef peer = mLocalProperty.getProperty(context, BLUETOOTH_SOCKET_PEER);
126 if(peer == NULL && mPeer != NULL) {
127 peer = JSBluetoothDevice::createJSObject(context, mPeer);
128 mLocalProperty.setProperty(context, BLUETOOTH_DEVICE_DEVICE_CLASS, peer);
133 return JSBluetoothDevice::createJSObject(context, mPeer);
136 MultiCallbackUserDataPtr BluetoothSocket::getOnMessage() const
141 JSValueRef BluetoothSocket::getOnMessage(JSContextRef context)
143 JSValueRef onMessage = mLocalProperty.getProperty(context, BLUETOOTH_SOCKET_ONMESSAGE);
144 if(onMessage == NULL) {
145 LoggerD("onmessage is null");
146 return JSValueMakeNull(context);
152 MultiCallbackUserDataPtr BluetoothSocket::getOnClose() const
157 JSValueRef BluetoothSocket::getOnClose(JSContextRef context)
159 JSValueRef onClose = mLocalProperty.getProperty(context, BLUETOOTH_SOCKET_ONCLOSE);
160 if(onClose == NULL) {
161 LoggerD("onclose is null");
162 return JSValueMakeNull(context);
168 MultiCallbackUserDataPtr BluetoothSocket::getOnError() const
173 JSValueRef BluetoothSocket::getOnError(JSContextRef context)
175 JSValueRef onError = mLocalProperty.getProperty(context, BLUETOOTH_SOCKET_ONERROR);
176 if(onError == NULL) {
177 LoggerD("onerror is null");
178 return JSValueMakeNull(context);
184 unsigned long BluetoothSocket::writeData(char* data, unsigned long size)
186 unsigned long ret = 0;
187 TIME_TRACER_ITEM_BEGIN("writeData::bt_socket_send_data", 1);
188 if(bt_socket_send_data(mConnectedSocket, data, static_cast<int>(size)) == BT_ERROR_NONE) {
189 TIME_TRACER_ITEM_END("writeData::bt_socket_send_data", 1);
190 LoggerD("bt_socket_send_data() succeeded");
194 TIME_TRACER_ITEM_END("writeData::bt_socket_send_data", 1);
195 throw UnknownException("Unknown error");
202 void BluetoothSocket::storeRecivedData(char *data, unsigned long size)
204 for(unsigned long i = 0; i < size; i++) {
205 mReceivedData.push_back(static_cast<signed char>(data[i]));
209 std::vector<signed char> BluetoothSocket::readData()
211 std::vector<signed char> result (mReceivedData);
212 mReceivedData.clear();
217 void BluetoothSocket::close()
220 LoggerD("Already disconnected");
224 TIME_TRACER_ITEM_BEGIN("close::bt_socket_disconnect_rfcomm", 1);
225 if(bt_socket_disconnect_rfcomm(mConnectedSocket) != BT_ERROR_NONE) {
226 TIME_TRACER_ITEM_END("close::bt_socket_disconnect_rfcomm", 1);
227 LoggerE("bt_socket_disconnect_rfcomm() failed");
228 throw UnknownException("Unknown error");
230 TIME_TRACER_ITEM_END("close::bt_socket_disconnect_rfcomm", 1);
232 mIsConnected = false;