diff options
Diffstat (limited to 'mobile_src/Bluetooth')
60 files changed, 9678 insertions, 0 deletions
diff --git a/mobile_src/Bluetooth/BluetoothAdapter.cpp b/mobile_src/Bluetooth/BluetoothAdapter.cpp new file mode 100644 index 0000000..fff8eff --- /dev/null +++ b/mobile_src/Bluetooth/BluetoothAdapter.cpp @@ -0,0 +1,1624 @@ +// +// Tizen Web Device API +// Copyright (c) 2012 Samsung Electronics Co., Ltd. +// +// Licensed under the Apache License, Version 2.0 (the License); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. +// + +#include <pcrecpp.h> + +#include <Logger.h> +#include <JSWebAPIErrorFactory.h> +#include <JSUtil.h> +#include <TimeTracer.h> +#include "plugin_config_impl.h" +#include "BluetoothAdapter.h" +#include "BluetoothCallbackUtil.h" +#include "JSBluetoothDevice.h" +#include "JSBluetoothServiceHandler.h" +#include "JSBluetoothSocket.h" +#include "GlobalContextManager.h" + +using namespace DeviceAPI::Common; + +namespace DeviceAPI { +namespace Bluetooth { + +void BluetoothAdapter::onStateChangedCB(int result, bt_adapter_state_e adapterState, void *userData) +{ + TIME_TRACER_ITEM_BEGIN(__FUNCTION__, 1); + + BluetoothAdapterPtr object = static_cast<BluetoothAdapterPtr>(userData); + if(!object) { + LoggerW("userData is NULL"); + return; + } + + bool previousState = object->mEnabled; + object->mEnabled = (adapterState == BT_ADAPTER_ENABLED) ? true : false; + if(object->mEnabled == true) { + object->mVisible = object->getVisible(); + } + + // call onstatechanged in ChangeListener + if(previousState != object->mEnabled && result == BT_ERROR_NONE && object->mChangeListener != NULL) { + LoggerD("call onstatechanged in ChangeListener"); + object->mChangeListener->invokeCallback("onstatechanged", JSUtil::toJSValueRef(object->mChangeListener->getContext(), object->mEnabled)); + } + + // call a result callback of setPowered() + if(object->mUserDataList[SET_POWERED] != NULL) { // requested event + LoggerD("call a result callback of setPowered()"); + if(object->mRequestedState != object->mEnabled) { + LoggerW("Requested state is not equal to current state"); + return; + } + + MultiCallbackUserDataPtr callback = static_cast<MultiCallbackUserDataPtr>(object->mUserDataList[SET_POWERED]); + object->mUserDataList[SET_POWERED].reset(); + + if(result == BT_ERROR_NONE) { + if(callback) + callback->invokeCallback("success"); + } + else if(result == BT_ERROR_RESOURCE_BUSY) { + if(callback) { + JSContextRef context = callback->getContext(); + ServiceNotAvailableException error("Bluetooth device is busy"); + callback->invokeCallback("error", JSWebAPIErrorFactory::makeErrorObject(context, error)); + } + } + else { + if(callback) { + JSContextRef context = callback->getContext(); + UnknownException error("Unknown error"); + callback->invokeCallback("error", JSWebAPIErrorFactory::makeErrorObject(context, error)); + } + } + } + else { // unexpected event + LoggerW("Bluetooth state is changed unexpectedly"); + } + + TIME_TRACER_ITEM_END(__FUNCTION__, 1); +} + +void BluetoothAdapter::onNameChangedCB(char *name, void *userData) +{ + TIME_TRACER_ITEM_BEGIN(__FUNCTION__, 1); + + BluetoothAdapterPtr object = static_cast<BluetoothAdapterPtr>(userData); + if(!object) { + LoggerW("userData is NULL"); + return; + } + + LoggerD("changed name: " << std::string(name)); + // call onnamechanged in ChangeListener + if(object->mChangeListener != NULL) { + LoggerD("call onnamechanged in ChangeListener"); + object->mChangeListener->invokeCallback("onnamechanged", JSUtil::toJSValueRef(object->mChangeListener->getContext(), std::string(name))); + } + + // call a result callback of setName() + if(object->mUserDataList[SET_NAME] != NULL && !strcmp(object->mRequestedName.c_str(), name)) { // requested event + MultiCallbackUserDataPtr callback = static_cast<MultiCallbackUserDataPtr>(object->mUserDataList[SET_NAME]); + object->mUserDataList[SET_NAME].reset(); + if(callback) + callback->invokeCallback("success"); + + //bt_adapter_unset_name_changed_cb(); + } + else { // unexpected event + LoggerW("Bluetooth name is changed unexpectedly"); + } + + TIME_TRACER_ITEM_END(__FUNCTION__, 1); +} + +void BluetoothAdapter::onVisibilityChangedCB(int result, bt_adapter_visibility_mode_e visibilityMode, void *userData) +{ + TIME_TRACER_ITEM_BEGIN(__FUNCTION__, 1); + + BluetoothAdapterPtr object = static_cast<BluetoothAdapterPtr>(userData); + if(!object) { + LoggerW("userData is NULL"); + return; + } + + bool previousVisible = object->mVisible; + object->mVisible = (visibilityMode != BT_ADAPTER_VISIBILITY_MODE_NON_DISCOVERABLE) ? true : false; + + // call onvisibilitychanged in ChangeListener + if(object->mChangeListener != NULL) { + if(previousVisible != object->mVisible) { + LoggerD("call onvisibilitychanged in ChangeListener"); + object->mChangeListener->invokeCallback("onvisibilitychanged", JSUtil::toJSValueRef(object->mChangeListener->getContext(), object->mVisible)); + } + } + + // call a result callback of setVisible() + if(object->mUserDataList[SET_VISIBLE] != NULL) { // requested event + //bool visibility = (visibilityMode == BT_ADAPTER_VISIBILITY_MODE_NON_DISCOVERABLE) ? false : true; + if(object->mRequestedVisibility != visibilityMode) { + LoggerW("Requested visibility is not same to current visibility"); + return; + } + + MultiCallbackUserDataPtr callback = static_cast<MultiCallbackUserDataPtr>(object->mUserDataList[SET_VISIBLE]); + object->mUserDataList[SET_VISIBLE].reset(); + + if(result == BT_ERROR_NONE) { + if(callback) + callback->invokeCallback("success"); + } + else { + if(callback) { + JSContextRef context = callback->getContext(); + UnknownException error("Unknown error"); + callback->invokeCallback("error", JSWebAPIErrorFactory::makeErrorObject(context, error)); + } + } + + //bt_adapter_unset_visibility_mode_changed_cb(); + } + else { // unexpected event + LoggerW("Bluetooth visibility is changed unexpectedly"); + } + + TIME_TRACER_ITEM_END(__FUNCTION__, 1); +} + +void BluetoothAdapter::onDiscoveryStateChangedCB(int result, bt_adapter_device_discovery_state_e discoveryState, + bt_adapter_device_discovery_info_s *discoveryInfo, void *userData) +{ + BluetoothAdapterPtr object = static_cast<BluetoothAdapterPtr>(userData); + if(!object) { + LoggerW("userData is NULL"); + return; + } + + switch(discoveryState) { + case BT_ADAPTER_DEVICE_DISCOVERY_STARTED: + { + LoggerD("Discovery started"); + if(object->mUserDataList[DISCOVER_DEVICES] != NULL) { // requested event + MultiCallbackUserDataPtr callback = + static_cast<MultiCallbackUserDataPtr>(object->mUserDataList[DISCOVER_DEVICES]); + + if(result == BT_ERROR_NONE) { + + // store MAC address of previously found device into mDisappearedDevices + object->mDisappearedDevices.clear(); + for(std::vector<BluetoothDeviceSharedPtr>::iterator iter = object->mFoundDevices.begin(); + iter != object->mFoundDevices.end(); iter++) { + BluetoothDeviceSharedPtr foundDevice = *iter; + object->mDisappearedDevices.push_back(foundDevice->getAddress()); + } + + object->mFoundDevices.clear(); + if(callback) + callback->invokeCallback("onstarted"); + } + else { + if(callback) { + object->mUserDataList[DISCOVER_DEVICES].reset(); + + JSContextRef context = callback->getContext(); + UnknownException error("Unknown error"); + callback->invokeCallback("error", JSWebAPIErrorFactory::makeErrorObject(context, error)); + } + + /* + if(object->mUserDataList[STOP_DISCOVERY] == NULL) // because same core API callback is used + bt_adapter_unset_device_discovery_state_changed_cb(); + */ + } + } + else { // unexpected event + LoggerW("Unexpected discovery"); + } + break; + } + case BT_ADAPTER_DEVICE_DISCOVERY_FINISHED: + { + LoggerD("Discovery finished"); + if(result == BT_ERROR_NONE || result == BT_ERROR_CANCELLED) { + // in case of discoverDevices() + if(object->mUserDataList[DISCOVER_DEVICES] != NULL) { + MultiCallbackUserDataPtr callback = + static_cast<MultiCallbackUserDataPtr>(object->mUserDataList[DISCOVER_DEVICES]); + + if(callback) { + if(object->mDisappearedDevices.size() > 0) { + LoggerD("There are disappeared devices"); + for(std::vector<std::string>::iterator iter = object->mDisappearedDevices.begin(); + iter != object->mDisappearedDevices.end(); iter++) { + + callback->invokeCallback("ondevicedisappeared", + JSUtil::toJSValueRef(callback->getContext(), *iter)); + } + } + + if(object->mFoundDevices.size() > 0) { // There are found devices + LoggerD("There are found devices"); + int num = object->mFoundDevices.size(); + JSObjectRef devices[num]; + for(int i = 0; i < num; i++) { + JSObjectRef deviceObj = JSBluetoothDevice::createJSObject(callback->getContext(), object->mFoundDevices[i]); + devices[i] = deviceObj; + } + + object->mUserDataList[DISCOVER_DEVICES].reset(); + + callback->invokeCallback( + "onfinished", + JSObjectMakeArray(callback->getContext(), num, devices, NULL) ); + } + else { // There is no found device + LoggerD("There is no found device"); + object->mUserDataList[DISCOVER_DEVICES].reset(); + + callback->invokeCallback( + "onfinished", + JSObjectMakeArray(callback->getContext(), 0, NULL, NULL) ); + } + } + } + + // in case of stopDiscovery() + if(object->mUserDataList[STOP_DISCOVERY] != NULL) { + MultiCallbackUserDataPtr callback = + static_cast<MultiCallbackUserDataPtr>(object->mUserDataList[STOP_DISCOVERY]); + + if(callback) { + LoggerD("Call successCallback of stopDiscovery()"); + object->mUserDataList[STOP_DISCOVERY].reset(); + callback->invokeCallback("success"); + } + } + + //bt_adapter_unset_device_discovery_state_changed_cb(); + } + else { + LoggerW("Unexpected result of discovery finish"); + } + break; + } + case BT_ADAPTER_DEVICE_DISCOVERY_FOUND: + { + LoggerD("A device is found"); + if(!discoveryInfo) { + LoggerW("No information about found device"); + return; + } + + if(object->mUserDataList[DISCOVER_DEVICES] != NULL) { // requested event + MultiCallbackUserDataPtr callback = + static_cast<MultiCallbackUserDataPtr>(object->mUserDataList[DISCOVER_DEVICES]); + + if(result == BT_ERROR_NONE) { + // create BluetoothDevice + BluetoothDeviceSharedPtr device(new BluetoothDevice(discoveryInfo)); + device->copyAceCheckAccessFunction(getInstance()); + JSContextRef context = callback->getContext(); + JSObjectRef deviceObj = JSBluetoothDevice::createJSObject(context, device); + object->mFoundDevices.push_back(device); + + // remove MAC address of found device from mDisappearedDevices + for(std::vector<std::string>::iterator iter = object->mDisappearedDevices.begin(); + iter != object->mDisappearedDevices.end(); iter++) { + if(!strcmp(discoveryInfo->remote_address, (*iter).c_str())) { + object->mDisappearedDevices.erase(iter); + break; + } + } + + if(callback) + callback->invokeCallback("ondevicefound", deviceObj); + } + else { + LoggerW("Unexpected result of discovery"); + } + } + else { // unexpected event + LoggerW("Unexpected discovery"); + } + break; + } + default: + { + LoggerW("Unknown state"); + } + } +} + +bool BluetoothAdapter::foreachBondedDevicesCB(bt_device_info_s *deviceInfo, void *userData) +{ + BluetoothAdapterPtr adapter = static_cast<BluetoothAdapterPtr>(userData); + if(!adapter) { + LoggerW("userData is NULL"); + return true; + } + + if(deviceInfo == NULL) { + LoggerW("deviceInfo is NULL"); + return true; + } + + std::vector<BluetoothDeviceSharedPtr>::iterator iter; + for(iter = adapter->knownDevices.begin(); iter != adapter->knownDevices.end(); ++iter) { + BluetoothDeviceSharedPtr foundDevice = *iter; + + if(!strcmp(foundDevice->getAddress().c_str(), deviceInfo->remote_address)) { + foundDevice->updateInfo(deviceInfo); + break; + } + } + + if(iter == adapter->knownDevices.end()) { + BluetoothDeviceSharedPtr device(new BluetoothDevice(deviceInfo)); + device->copyAceCheckAccessFunction(getInstance()); + adapter->knownDevices.push_back(device); + } + + return true; +} + +void BluetoothAdapter::onBondCreatedCB(int result, bt_device_info_s *deviceInfo, void *userData) +{ + TIME_TRACER_ITEM_BEGIN(__FUNCTION__, 1); + + BluetoothAdapterPtr object = static_cast<BluetoothAdapterPtr>(userData); + if(!object) { + LoggerW("userData is NULL"); + return; + } + + if(!deviceInfo) { + LoggerW("deviceInfo is NULL"); + return; + } + + if(object->mUserDataList[CREATE_BONDING] != NULL && + !strcmp(object->mCreateBondingAddress.c_str(), deviceInfo->remote_address)) { // requested event + + MultiCallbackUserDataPtr callback = static_cast<MultiCallbackUserDataPtr>(object->mUserDataList[CREATE_BONDING]); + object->mUserDataList[CREATE_BONDING].reset(); + + if(result == BT_ERROR_NONE && deviceInfo != NULL) { + if(callback) { + BluetoothDeviceSharedPtr device(new BluetoothDevice(deviceInfo)); + device->copyAceCheckAccessFunction(getInstance()); + JSContextRef context = callback->getContext(); + JSObjectRef deviceObj = JSBluetoothDevice::createJSObject(context, device); + callback->invokeCallback("success", deviceObj); + } + } + else if(result == BT_ERROR_REMOTE_DEVICE_NOT_FOUND) { + if(callback) { + LoggerE("Not found"); + JSContextRef context = callback->getContext(); + NotFoundException error("Not found"); + callback->invokeCallback("error", JSWebAPIErrorFactory::makeErrorObject(context, error)); + } + } + else { + if(callback) { + JSContextRef context = callback->getContext(); + UnknownException error("Unknown error"); + callback->invokeCallback("error", JSWebAPIErrorFactory::makeErrorObject(context, error)); + } + } + + bt_device_unset_bond_created_cb(); + object->mCreateBondingAddress.clear(); + } + else { // unexpected event + LoggerW("A bonding is created unexpectedly"); + } + + TIME_TRACER_ITEM_END(__FUNCTION__, 1); +} + +void BluetoothAdapter::onBondDestroyedCB(int result, char *remoteAddress, void *userData) +{ + TIME_TRACER_ITEM_BEGIN(__FUNCTION__, 1); + + BluetoothAdapterPtr object = static_cast<BluetoothAdapterPtr>(userData); + if(!object) { + LoggerW("userData is NULL"); + return; + } + + if(object->mUserDataList[DESTROY_BONDING] != NULL && + !strcmp(object->mDestroyBondingAddress.c_str(), remoteAddress)) { // requested event + + MultiCallbackUserDataPtr callback = static_cast<MultiCallbackUserDataPtr>(object->mUserDataList[DESTROY_BONDING]); + object->mUserDataList[DESTROY_BONDING].reset(); + + if(result == BT_ERROR_NONE) { + if(callback) + callback->invokeCallback("success"); + } + else { + if(callback) { + JSContextRef context = callback->getContext(); + UnknownException error("Unknown error"); + callback->invokeCallback("error", JSWebAPIErrorFactory::makeErrorObject(context, error)); + } + } + + bt_device_unset_bond_destroyed_cb(); + object->mDestroyBondingAddress.clear(); + } + else { // unexpected event + LoggerW("A bonding is destroyed unexpectedly"); + } + + TIME_TRACER_ITEM_END(__FUNCTION__, 1); +} + +void BluetoothAdapter::onSocketConnected(int result, bt_socket_connection_state_e state, bt_socket_connection_s *connection, void *userData) +{ + TIME_TRACER_ITEM_BEGIN(__FUNCTION__, 1); + + BluetoothAdapterPtr object = static_cast<BluetoothAdapterPtr>(userData); + if(!object) { + LoggerW("userData is NULL"); + return; + } + + if(!connection) { + LoggerW("connection is NULL"); + return; + } + + if(connection->local_role == BT_SOCKET_SERVER) { + RegisteredUUIDMapT::iterator iter = object->mRegisteredUUID.find(connection->service_uuid); + if(iter == object->mRegisteredUUID.end()) { + LoggerW("Connection state is changed unexpectedly"); + return; + } + + if(state == BT_SOCKET_CONNECTED) { // connected when Server + if(result == BT_ERROR_NONE) { + // Update BluetoothServiceHandler + BluetoothServiceHandlerPtr service = iter->second; + service->setConnectionState(true); + + // Call BluetoothServiceHandler.onconnect + BluetoothSocketPtr socket = new BluetoothSocket(connection, getInstance()); + MultiCallbackUserDataPtr callback = service->getOnConnect(); + JSContextRef context = callback->getContext(); + JSObjectRef socketObj = JSBluetoothSocket::createJSObject(context, socket); + if(callback) + callback->invokeCallback("onconnect", socketObj); + + // Update mConnectedSocket + object->mConnectedSocket.insert(std::pair<int, BluetoothSocketPtr>(connection->socket_fd, socket)); + bt_socket_set_data_received_cb(onSocketReceivedCB, userData); + } + else { + LoggerW("Establishing a connection failed"); + } + return; + } + else { // disconnected when Server + if(result == BT_ERROR_NONE) { + // Update BluetoothServiceHandler + BluetoothServiceHandlerPtr service = iter->second; + service->setConnectionState(false); + + // call BluetoothSocket.onclose; + ConnectedSocketMapT::iterator i = object->mConnectedSocket.find(connection->socket_fd); + if(i == object->mConnectedSocket.end()) { + LoggerW("Unknown connected socket"); + return; + } + //BluetoothSocketSharedPtr socket = i->second; + BluetoothSocketPtr socket = i->second; + socket->setConnectionState(false); + MultiCallbackUserDataPtr callback = socket->getOnClose(); + if(callback) + callback->invokeCallback("onclose"); + + // Update mConnectedSocket + object->mConnectedSocket.erase(i); + } + else { + LoggerW("Disconnecting a connection failed"); + } + } + } + else if(connection->local_role == BT_SOCKET_CLIENT) { + + if(state == BT_SOCKET_CONNECTED) { // connected when Client + std::string remoteAddress(connection->remote_address); + ConnReqMultiMapT::iterator iter; + do { + iter = object->mConnReqMap.find(remoteAddress); + if(iter != object->mConnReqMap.end() && !strcmp(iter->second->mUUID.c_str(), connection->service_uuid)) { + break; + } + } while(iter != object->mConnReqMap.end()); + + if(iter == object->mConnReqMap.end()) { + LoggerW("Connection state is changed unexpectedly"); + return; + } + + MultiCallbackUserDataPtr callback = static_cast<MultiCallbackUserDataPtr>(iter->second->mUserData); + + if(result == BT_ERROR_NONE) { + // Update mConnectedSocket + BluetoothSocketPtr socket = new BluetoothSocket(connection, getInstance()); + + object->mConnectedSocket.insert(std::pair<int, BluetoothSocketPtr>(connection->socket_fd, socket)); + bt_socket_set_data_received_cb(onSocketReceivedCB, userData); + + // Call successcallback of connectToServiceByUUID + JSContextRef context = callback->getContext(); + JSObjectRef socketObj = JSBluetoothSocket::createJSObject(context, socket); + if(callback) + callback->invokeCallback("success", socketObj); + + // Update mConnReqMap + object->mConnReqMap.erase(iter); + } + else { + // Call errorcallback of connectToServiceByUUID + JSContextRef context = callback->getContext(); + NotFoundException error("Not found"); + if(callback) + callback->invokeCallback("error", JSWebAPIErrorFactory::makeErrorObject(context, error)); + + // Update mConnReqMap + object->mConnReqMap.erase(iter); + } + return; + } + else { // disconnected when Client + if(result == BT_ERROR_NONE) { + // call BluetoothSocket.onclose; + ConnectedSocketMapT::iterator i = object->mConnectedSocket.find(connection->socket_fd); + if(i == object->mConnectedSocket.end()) { + LoggerW("Unknown connected socket"); + return; + } + + BluetoothSocketPtr socket = i->second; + socket->setConnectionState(false); + MultiCallbackUserDataPtr callback = socket->getOnClose(); + if(callback) + callback->invokeCallback("onclose"); + + // Update mConnectedSocket + object->mConnectedSocket.erase(i); + } + else { + LoggerW("Disconnecting a connection failed"); + } + } + } + else { + LoggerW("Unknown role"); + return; + } + + if(object->mConnectedSocket.size() == 0) { + bt_socket_unset_data_received_cb(); + } + + if(object->mRegisteredUUID.size() == 0 && object->mConnReqMap.size() == 0 && object->mConnectedSocket.size() == 0) { + bt_socket_unset_connection_state_changed_cb(); + } + + TIME_TRACER_ITEM_END(__FUNCTION__, 1); +} + +void BluetoothAdapter::onSocketReceivedCB(bt_socket_received_data_s *data, void *userData) +{ + BluetoothAdapterPtr object = static_cast<BluetoothAdapterPtr>(userData); + if(!object) { + LoggerW("userData is NULL"); + return; + } + + if(!data) { + LoggerW("data is NULL"); + return; + } + + ConnectedSocketMapT::iterator i = object->mConnectedSocket.find(data->socket_fd); + if(i == object->mConnectedSocket.end()) { + LoggerW("Unknown connected socket"); + return; + } + + // Store received data + BluetoothSocketPtr socket = i->second; + socket->storeRecivedData(data->data, static_cast<unsigned long>(data->data_size)); + + // Call BluetoothSocket.onmessage + MultiCallbackUserDataPtr callback = socket->getOnMessage(); + if(callback) + callback->invokeCallback("onmessage"); +} + +BluetoothAdapter::BluetoothAdapter(): + mEnabled(false), mVisible(false) +{ + Common::SecurityAccessor(); + + bt_adapter_state_e state; + if (bt_adapter_get_state(&state) == BT_ERROR_NONE) { + if (state == BT_ADAPTER_ENABLED) { + mEnabled = true; + } + } + + bt_adapter_visibility_mode_e mode; + if (bt_adapter_get_visibility(&mode, NULL) == BT_ERROR_NONE) { + if (mode != BT_ADAPTER_VISIBILITY_MODE_NON_DISCOVERABLE) { + mVisible = true; + } + } + + if(bt_adapter_set_state_changed_cb(onStateChangedCB, this) != BT_ERROR_NONE) { + LoggerE("bt_adapter_set_state_changed_cb() failed"); + } + + if(bt_adapter_set_device_discovery_state_changed_cb(onDiscoveryStateChangedCB, this) != BT_ERROR_NONE) { + LoggerE("bt_adapter_set_device_discovery_state_changed_cb() failed"); + } + + if(bt_adapter_set_name_changed_cb(onNameChangedCB, this) != BT_ERROR_NONE) { + LoggerE("bt_adapter_set_name_changed_cb() failed"); + } + + if(bt_adapter_set_visibility_mode_changed_cb(onVisibilityChangedCB, this) != BT_ERROR_NONE) { + LoggerE("bt_adapter_set_visibility_mode_changed_cb() failed"); + } +} + +BluetoothAdapter::~BluetoothAdapter() +{ + // unset platform callback + bt_adapter_unset_state_changed_cb(); + bt_adapter_unset_name_changed_cb(); + bt_adapter_unset_visibility_mode_changed_cb(); + bt_adapter_unset_device_discovery_state_changed_cb(); + bt_device_unset_bond_created_cb(); + bt_device_unset_bond_destroyed_cb(); + bt_socket_unset_connection_state_changed_cb(); + bt_socket_unset_data_received_cb(); + bt_deinitialize(); + + for(int i = 0; i <= DESTROY_BONDING; i++) { + mUserDataList[i].reset(); + } + mRegisteredUUID.clear(); + mConnReqMap.clear(); + mFoundDevices.clear(); + mConnectedSocket.clear(); + + +} + +void BluetoothAdapter::unloadFrame(JSContextRef context) +{ + LoggerD("Clean mUserDataList"); + for(int i = 0; i <= DESTROY_BONDING; i++) { + if(mUserDataList[i]) { + MultiCallbackUserDataPtr callback = mUserDataList[i]; + if(!GlobalContextManager::getInstance()->isAliveGlobalContext(callback->getContext())) { + mUserDataList[i].reset(); + } + } + } + + LoggerD("Clean mConnReqMap"); + for(ConnReqMultiMapT::iterator iter = mConnReqMap.begin(); iter != mConnReqMap.end(); ) { + ConnReqMultiMapT::iterator temp = iter++; + MultiCallbackUserDataPtr callback = temp->second->mUserData; + if(!callback && !GlobalContextManager::getInstance()->isAliveGlobalContext(callback->getContext())) { + mConnReqMap.erase(temp); + } + } +} + +void BluetoothAdapter::unregisterUUID(std::string &uuid) +{ + mRegisteredUUID.erase(mRegisteredUUID.find(uuid)); + if(mRegisteredUUID.size() == 0 && mConnReqMap.size() == 0 && mConnectedSocket.size() == 0) { + bt_socket_unset_connection_state_changed_cb(); + } +} + +bool BluetoothAdapter::closeConnectedSocket(int socket) +{ + if(mEnabled == true) { + ConnectedSocketMapT::iterator iter = mConnectedSocket.find(socket); + if(iter == mConnectedSocket.end()) { + LoggerW("Already disconnected"); + return true; + } + + mConnectedSocket.erase(iter); + if(mConnectedSocket.size() == 0) { + bt_socket_unset_data_received_cb(); + } + + if(mRegisteredUUID.size() == 0 && mConnReqMap.size() == 0 && mConnectedSocket.size() == 0) { + bt_socket_unset_connection_state_changed_cb(); + } + + return true; + } + else { + LoggerE("Bluetooth is not powered"); + return false; + } +} + +void BluetoothAdapter::removeConnReq(std::string &remoteAddress) +{ + mConnReqMap.erase(remoteAddress); + + if(mRegisteredUUID.size() == 0 && mConnReqMap.size() == 0 && mConnectedSocket.size() == 0) { + if(bt_socket_unset_connection_state_changed_cb() != BT_ERROR_NONE) { + LoggerW("Unsetting connection event callback failed"); + } + } +} + +BluetoothAdapter* BluetoothAdapter::getInstance() +{ + static BluetoothAdapter instance; + return &instance; +} + +bool BluetoothAdapter::isValidAddress(std::string &address) +{ + pcrecpp::RE re("(([0-9a-zA-Z]+):)+([0-9a-zA-Z]+)"); + std::string compareAddress = "00:12:47:08:9A:A6"; + + if (!re.FullMatch(address)) { + LoggerE("Invalid address"); + return false; + } + + if (address.size() != compareAddress.size()) + { + LoggerE("Invalid size"); + return false; + } + + return true; +} + +bool BluetoothAdapter::isValidUUID(std::string &uuid) +{ + pcrecpp::RE re("(([0-9a-zA-Z]+)-)+([0-9a-zA-Z]+)"); + std::string compareUUID = "00001101-0000-1000-8000-00805F9B34FB"; + + if (!re.FullMatch(uuid)) + { + LoggerE("Invalid UUID"); + return false; + } + + if (uuid.size() != compareUUID.size()) + { + LoggerE("Invalid size"); + return false; + } + + return true; +} + +std::string BluetoothAdapter::getName() const +{ + char* name = NULL; + std::string str = ""; + + TIME_TRACER_ITEM_BEGIN("getName::bt_adapter_get_name", 1); + if(bt_adapter_get_name(&name) == BT_ERROR_NONE) { + TIME_TRACER_ITEM_END("getName::bt_adapter_get_name", 1); + if (name != NULL) + { + str = name; + free(name); + } + } + else { + TIME_TRACER_ITEM_END("getName::bt_adapter_get_name", 1); + LoggerE("bt_adapter_get_name() failed"); + } + + return str; +} + +void BluetoothAdapter::setName(std::string &name, MultiCallbackUserDataPtr userData) +{ + if(mEnabled == true) { + std::string adapterName = getName(); + if(adapterName == name) { // in case of same name + LoggerD("same name"); + BluetoothCallbackUtil::syncToAsyncSuccessCallback(userData); + return; + } + + if(mUserDataList[SET_NAME] == NULL) { + mUserDataList[SET_NAME] = userData; + } else { + LoggerE("Already requested"); + UnknownException *error = new UnknownException("Already requested"); + BluetoothCallbackUtil::syncToAsyncErrorCallback(userData, error); + return; + } + + TIME_TRACER_ITEM_BEGIN("setName::bt_adapter_set_name", 1); + int ret = bt_adapter_set_name(name.c_str()); + TIME_TRACER_ITEM_END("setName::bt_adapter_set_name", 1); + + switch(ret) { + case BT_ERROR_NONE: + { + LoggerD("bt_adapter_set_name() succeeded"); + mRequestedName = name; + return; + } + case BT_ERROR_INVALID_PARAMETER: + { + LoggerE("Invalid value"); + InvalidValuesException *error = new InvalidValuesException("Invalid value"); + BluetoothCallbackUtil::syncToAsyncErrorCallback(userData, error); + break; + } + default: + { + LoggerE("Invalid value"); + UnknownException *error = new UnknownException("Unknown exception"); + BluetoothCallbackUtil::syncToAsyncErrorCallback(userData, error); + } + } + + //TIME_TRACER_ITEM_BEGIN("setName::bt_adapter_unset_name_changed_cb", 1); + //bt_adapter_unset_name_changed_cb(); + //TIME_TRACER_ITEM_END("setName::bt_adapter_unset_name_changed_cb", 1); + mUserDataList[SET_NAME].reset(); + } else { // Not enabled + LoggerE("Bluetooth device is turned off"); + ServiceNotAvailableException *error = new ServiceNotAvailableException("Bluetooth device is turned off"); + BluetoothCallbackUtil::syncToAsyncErrorCallback(userData, error); + } +} + +std::string BluetoothAdapter::getAddress() const +{ + char* address = NULL; + std::string str = ""; + + if (bt_adapter_get_address(&address) == BT_ERROR_NONE) { + if (address != NULL) { + str = address; + free(address); + } + else { + LoggerW("address is NULL"); + } + } + else { + LoggerE("bt_adapter_get_address() failed"); + } + + return str; +} + +bool BluetoothAdapter::getPowered() const +{ + return mEnabled; +} + +void BluetoothAdapter::setPowered(bool powered, MultiCallbackUserDataPtr userData) +{ + TIME_TRACER_ITEM_BEGIN("setPowered::check current state", 1); + + if(powered == mEnabled) { + LoggerD("same state"); + BluetoothCallbackUtil::syncToAsyncSuccessCallback(userData); + return; + } + + if(mUserDataList[SET_POWERED] == NULL) { + mUserDataList[SET_POWERED] = userData; + } else { + // Already requested + LoggerE("Already requested"); + ServiceNotAvailableException *error = new ServiceNotAvailableException("Already requested"); + BluetoothCallbackUtil::syncToAsyncErrorCallback(userData, error); + return; + } + + TIME_TRACER_ITEM_END("setPowered::check current state", 1); + + mRequestedState = powered; + if(powered == true) { + TIME_TRACER_ITEM_BEGIN("setPowered::bt_adapter_enable", 1); + int ret = bt_adapter_enable(); + TIME_TRACER_ITEM_END("setPowered::bt_adapter_enable", 1); + + switch(ret) { + case BT_ERROR_NONE: + { + LoggerD("bt_adapter_enable() succeeded"); + return; + } + case BT_ERROR_ALREADY_DONE: + { + // call successCallback + LoggerD("BT_ERROR_ALREADY_DONE"); + BluetoothCallbackUtil::syncToAsyncSuccessCallback(userData); + break; + } + case BT_ERROR_NOW_IN_PROGRESS: + { + LoggerD("BT_ERROR_NOW_IN_PROGRESS"); + ServiceNotAvailableException *error = new ServiceNotAvailableException("Bluetooth device is busy"); + BluetoothCallbackUtil::syncToAsyncErrorCallback(userData, error); + break; + } + default: + { + LoggerD("Unknown error"); + UnknownException *error = new UnknownException("Unknown error"); + BluetoothCallbackUtil::syncToAsyncErrorCallback(userData, error); + } + } + } else { + TIME_TRACER_ITEM_BEGIN("setPowered::bt_adapter_disable", 1); + int ret = bt_adapter_disable(); + TIME_TRACER_ITEM_END("setPowered::bt_adapter_disable", 1); + + switch(ret) { + case BT_ERROR_NONE: + { + LoggerD("bt_adapter_disable() succeeded"); + return; + } + case BT_ERROR_NOT_ENABLED: + { + // call successCallback + LoggerD("Already disabled"); + BluetoothCallbackUtil::syncToAsyncSuccessCallback(userData); + break; + } + case BT_ERROR_NOW_IN_PROGRESS: + { + LoggerD("BT_ERROR_NOW_IN_PROGRESS"); + ServiceNotAvailableException *error = new ServiceNotAvailableException("Bluetooth device is busy"); + BluetoothCallbackUtil::syncToAsyncErrorCallback(userData, error); + break; + } + default: + { + LoggerD("Unknown error"); + UnknownException *error = new UnknownException("Unknown error"); + BluetoothCallbackUtil::syncToAsyncErrorCallback(userData, error); + } + } + } + + mUserDataList[SET_POWERED].reset(); +} + +bool BluetoothAdapter::getVisible() const +{ + bt_adapter_visibility_mode_e mode; + + if (bt_adapter_get_visibility(&mode, NULL) == BT_ERROR_NONE) { + if (mode != BT_ADAPTER_VISIBILITY_MODE_NON_DISCOVERABLE) { + return true; + } + } + + return false; +} + +void BluetoothAdapter::setVisible(bool visible, unsigned int timeout, MultiCallbackUserDataPtr userData) +{ + if(mEnabled == true) { + bt_adapter_visibility_mode_e discoverable_mode = BT_ADAPTER_VISIBILITY_MODE_NON_DISCOVERABLE; + if(visible == true) { + if(timeout == 0) + discoverable_mode = BT_ADAPTER_VISIBILITY_MODE_GENERAL_DISCOVERABLE; + else + discoverable_mode = BT_ADAPTER_VISIBILITY_MODE_LIMITED_DISCOVERABLE; + } + + bt_adapter_visibility_mode_e current = BT_ADAPTER_VISIBILITY_MODE_NON_DISCOVERABLE; + int time = 0; + + TIME_TRACER_ITEM_BEGIN("setVisible::bt_adapter_get_visibility", 1); + if(bt_adapter_get_visibility(¤t , &time) != BT_ERROR_NONE) { + LoggerE("bt_adapter_get_visibility() failed"); + UnknownException *error = new UnknownException("Can't get current visibility"); + BluetoothCallbackUtil::syncToAsyncErrorCallback(userData, error); + return; + } + TIME_TRACER_ITEM_END("setVisible::bt_adapter_get_visibility", 1); + + if(discoverable_mode == current) { + if(discoverable_mode != BT_ADAPTER_VISIBILITY_MODE_LIMITED_DISCOVERABLE) { + LoggerD("same visibility"); + BluetoothCallbackUtil::syncToAsyncSuccessCallback(userData); + return; + } + else if((unsigned int)time == timeout) { + LoggerD("same visibility"); + BluetoothCallbackUtil::syncToAsyncSuccessCallback(userData); + return; + } + } + + if(mUserDataList[SET_VISIBLE] == NULL) { + mUserDataList[SET_VISIBLE] = userData; + } else { + UnknownException *error = new UnknownException("Already requested"); + BluetoothCallbackUtil::syncToAsyncErrorCallback(userData, error); + return; + } + + mRequestedVisibility = discoverable_mode; + TIME_TRACER_ITEM_BEGIN("setVisible::bt_adapter_set_visibility", 1); + int ret = bt_adapter_set_visibility(discoverable_mode, timeout); + TIME_TRACER_ITEM_END("setVisible::bt_adapter_set_visibility", 1); + switch(ret) { + case BT_ERROR_NONE: + { + LoggerD("bt_adapter_set_visibility() succeeded"); + return; + } + case BT_ERROR_INVALID_PARAMETER: + { + InvalidValuesException *error = new InvalidValuesException("Invalid value"); + BluetoothCallbackUtil::syncToAsyncErrorCallback(userData, error); + break; + } + default: + { + UnknownException *error = new UnknownException("Unknown error"); + BluetoothCallbackUtil::syncToAsyncErrorCallback(userData, error); + } + } + + /* + TIME_TRACER_ITEM_BEGIN("setVisible::bt_adapter_unset_visibility_mode_changed_cb", 1); + bt_adapter_unset_visibility_mode_changed_cb(); + TIME_TRACER_ITEM_END("setVisible::bt_adapter_unset_visibility_mode_changed_cb", 1); + */ + mUserDataList[SET_VISIBLE].reset(); + } else { // Not enabled + ServiceNotAvailableException *error = new ServiceNotAvailableException("Bluetooth device is turned off"); + BluetoothCallbackUtil::syncToAsyncErrorCallback(userData, error); + } + +} + +void BluetoothAdapter::discoverDevices(MultiCallbackUserDataPtr userData) +{ + TIME_TRACER_ITEM_BEGIN("discoverDevices::current state", 1); + + if(mUserDataList[DISCOVER_DEVICES] == NULL) { + mUserDataList[DISCOVER_DEVICES] = userData; + + /* + if(mUserDataList[STOP_DISCOVERY] == NULL) + bt_adapter_set_device_discovery_state_changed_cb(onDiscoveryStateChangedCB, this); + */ + } else { + LoggerE("Already requested"); + UnknownException *error = new UnknownException("Already requested"); + BluetoothCallbackUtil::syncToAsyncErrorCallback(userData, error); + return; + } + + TIME_TRACER_ITEM_END("discoverDevices::current state", 1); + + if(mEnabled == true) { + TIME_TRACER_ITEM_BEGIN("discoverDevices::bt_adapter_start_device_discovery", 1); + int ret = bt_adapter_start_discover_devices(BT_ADAPTER_DEVICE_DISCOVERY_LE_BREDR); + TIME_TRACER_ITEM_END("discoverDevices::bt_adapter_start_device_discovery", 1); + switch(ret) { + case BT_ERROR_NONE: + { + LoggerD("bt_adapter_start_device_discovery() succeeded"); + return; + } + default: + { + LoggerE("Unknown exception"); + UnknownException *error = new UnknownException("Unknown error"); + BluetoothCallbackUtil::syncToAsyncErrorCallback(userData, error); + } + } + } else { // Not enabled + LoggerE("Bluetooth device is turned off"); + ServiceNotAvailableException *error = new ServiceNotAvailableException("Bluetooth device is turned off"); + BluetoothCallbackUtil::syncToAsyncErrorCallback(userData, error); + } + + mUserDataList[DISCOVER_DEVICES].reset(); + /* + if(mUserDataList[STOP_DISCOVERY] == NULL) { + bt_adapter_unset_device_discovery_state_changed_cb(); + } + */ +} + +void BluetoothAdapter::stopDiscovery(MultiCallbackUserDataPtr userData) +{ + if(mEnabled == true) { + + bool isDiscovering = false; + bt_adapter_is_discovering(&isDiscovering); + if(!isDiscovering) { + BluetoothCallbackUtil::syncToAsyncSuccessCallback(userData); + return; + } + + if(mUserDataList[STOP_DISCOVERY] == NULL) { + mUserDataList[STOP_DISCOVERY] = userData; + + /* + if(mUserDataList[DISCOVER_DEVICES] == NULL) + bt_adapter_set_device_discovery_state_changed_cb(onDiscoveryStateChangedCB, this); + */ + } else { + LoggerD("Already requested"); + UnknownException *error = new UnknownException("Already requested"); + BluetoothCallbackUtil::syncToAsyncErrorCallback(userData, error); + return; + } + + TIME_TRACER_ITEM_BEGIN("stopDiscovery::bt_adapter_stop_device_discovery", 1); + int ret = bt_adapter_stop_device_discovery(); + TIME_TRACER_ITEM_END("stopDiscovery::bt_adapter_stop_device_discovery", 1); + switch(ret) { + case BT_ERROR_NONE: + { + LoggerD("bt_adapter_stop_device_discovery() succeeded"); + return; + } + default: + { + UnknownException *error = new UnknownException("Unknown error"); + BluetoothCallbackUtil::syncToAsyncErrorCallback(userData, error); + } + } + + mUserDataList[STOP_DISCOVERY].reset(); + /* + if(mUserDataList[DISCOVER_DEVICES] == NULL) { + bt_adapter_unset_device_discovery_state_changed_cb(); + } + */ + } else { // Not enabled + ServiceNotAvailableException *error = new ServiceNotAvailableException("Bluetooth device is turned off"); + BluetoothCallbackUtil::syncToAsyncErrorCallback(userData, error); + } +} + +void BluetoothAdapter::getKnownDevices(MultiCallbackUserDataPtr userData) +{ + BluetoothCallbackUtil::syncToAsyncDeviceArrayCallback(userData); +} + +void BluetoothAdapter::getDevice(std::string &address, MultiCallbackUserDataPtr userData) +{ + BluetoothCallbackUtil::syncToAsyncDeviceCallback(userData, address); +} + +void BluetoothAdapter::createBonding(std::string &address, MultiCallbackUserDataPtr userData) +{ + if(!isValidAddress(address)) { + LoggerE("Wrong address"); + NotFoundException *error = new NotFoundException("Wrong address"); + BluetoothCallbackUtil::syncToAsyncErrorCallback(userData, error); + return; + } + + if(mUserDataList[CREATE_BONDING] == NULL) { + TIME_TRACER_ITEM_BEGIN("createBonding::bt_device_set_bond_created_cb", 1); + if(bt_device_set_bond_created_cb(onBondCreatedCB, this) != BT_ERROR_NONE) { + LoggerW("bt_device_set_bond_created_cb() failed"); + } + TIME_TRACER_ITEM_END("createBonding::bt_device_set_bond_created_cb", 1); + mCreateBondingAddress = address; + mUserDataList[CREATE_BONDING] = userData; + } else { + LoggerE("Already requested"); + UnknownException *error = new UnknownException("Already requested"); + BluetoothCallbackUtil::syncToAsyncErrorCallback(userData, error); + return; + } + + if(mEnabled == true) { + TIME_TRACER_ITEM_BEGIN("createBonding::bt_device_create_bond", 1); + int ret = bt_device_create_bond(address.c_str()); + TIME_TRACER_ITEM_END("createBonding::bt_device_create_bond", 1); + + switch(ret) { + case BT_ERROR_NONE: + { + LoggerD("bt_device_create_bond() succeeded"); + return; + } + case BT_ERROR_INVALID_PARAMETER: + { + LoggerE("Not found"); + NotFoundException *error = new NotFoundException("Not found"); + BluetoothCallbackUtil::syncToAsyncErrorCallback(userData, error); + break; + } + default: + { + LoggerE("Unknown exception"); + UnknownException *error = new UnknownException("Unknown error"); + BluetoothCallbackUtil::syncToAsyncErrorCallback(userData, error); + } + } + } else { // Not enabled + LoggerE("Bluetooth device is turned off"); + ServiceNotAvailableException *error = new ServiceNotAvailableException("Bluetooth device is turned off"); + BluetoothCallbackUtil::syncToAsyncErrorCallback(userData, error); + } + + TIME_TRACER_ITEM_BEGIN("createBonding::bt_device_unset_bond_created_cb", 1); + bt_device_unset_bond_created_cb(); + TIME_TRACER_ITEM_END("createBonding::bt_device_unset_bond_created_cb", 1); + mCreateBondingAddress.clear(); + mUserDataList[CREATE_BONDING].reset(); +} + +void BluetoothAdapter::destroyBonding(std::string &address, MultiCallbackUserDataPtr userData) +{ + if(!isValidAddress(address)) { + LoggerE("Wrong address"); + NotFoundException *error = new NotFoundException("Wrong address"); + BluetoothCallbackUtil::syncToAsyncErrorCallback(userData, error); + return; + } + + if(mUserDataList[DESTROY_BONDING] == NULL) { + TIME_TRACER_ITEM_BEGIN("destroyBonding::bt_device_set_bond_destroyed_cb", 1); + if(bt_device_set_bond_destroyed_cb(onBondDestroyedCB, this) != BT_ERROR_NONE) { + LoggerW("bt_device_set_bond_destroyed_cb() failed"); + } + TIME_TRACER_ITEM_END("destroyBonding::bt_device_set_bond_destroyed_cb", 1); + mDestroyBondingAddress = address; + mUserDataList[DESTROY_BONDING] = userData; + } else { + LoggerD("Already requested"); + UnknownException *error = new UnknownException("Already requested"); + BluetoothCallbackUtil::syncToAsyncErrorCallback(userData, error); + return; + } + + if(mEnabled == true) { + bt_device_info_s *deviceInfo = NULL; + TIME_TRACER_ITEM_BEGIN("destroyBonding::bt_adapter_get_bonded_device_info", 1); + if(bt_adapter_get_bonded_device_info(address.c_str(), &deviceInfo) != BT_ERROR_NONE || deviceInfo == NULL) { + TIME_TRACER_ITEM_END("destroyBonding::bt_adapter_get_bonded_device_info", 1); + LoggerD("There is no bonding"); + NotFoundException *error = new NotFoundException("Not found"); + BluetoothCallbackUtil::syncToAsyncErrorCallback(userData, error); + } + else { + TIME_TRACER_ITEM_END("destroyBonding::bt_adapter_get_bonded_device_info", 1); + bt_adapter_free_device_info(deviceInfo); + TIME_TRACER_ITEM_BEGIN("destroyBonding::bt_device_destroy_bond", 1); + int ret = bt_device_destroy_bond(address.c_str()); + TIME_TRACER_ITEM_END("destroyBonding::bt_device_destroy_bond", 1); + + switch(ret) { + case BT_ERROR_NONE: + { + LoggerD("bt_device_destroy_bond() succeeded"); + return; + } + case BT_ERROR_INVALID_PARAMETER: + { + NotFoundException *error = new NotFoundException("Not found"); + BluetoothCallbackUtil::syncToAsyncErrorCallback(userData, error); + break; + } + default: + { + UnknownException *error = new UnknownException("Unknown error"); + BluetoothCallbackUtil::syncToAsyncErrorCallback(userData, error); + } + } + } + } else { // Not enabled + ServiceNotAvailableException *error = new ServiceNotAvailableException("Bluetooth device is turned off"); + BluetoothCallbackUtil::syncToAsyncErrorCallback(userData, error); + } + + TIME_TRACER_ITEM_BEGIN("destroyBonding::bt_device_unset_bond_destroyed_cb", 1); + bt_device_unset_bond_destroyed_cb(); + TIME_TRACER_ITEM_END("destroyBonding::bt_device_unset_bond_destroyed_cb", 1); + + mDestroyBondingAddress.clear(); + mUserDataList[DESTROY_BONDING].reset(); +} + +void BluetoothAdapter::registerRFCOMMServiceByUUID(std::string &uuid, std::string &name, MultiCallbackUserDataPtr userData) +{ + BluetoothCallbackUtil::syncToAsyncServiceCallback(userData, uuid, name); +} + +void BluetoothAdapter::connectToServiceByUUID(std::string &remoteAddress, std::string &uuid, Common::MultiCallbackUserDataPtr userData) +{ + if(!isValidUUID(uuid)) { + LoggerE("Wrong UUID"); + InvalidValuesException *error = new InvalidValuesException("Wrong UUID"); + BluetoothCallbackUtil::syncToAsyncErrorCallback(userData, error); + return; + } + + + if(mEnabled == true) { + TIME_TRACER_ITEM_BEGIN("connectToServiceByUUID::bt_socket_connect_rfcomm", 1); + int ret = bt_socket_connect_rfcomm(remoteAddress.c_str(), uuid.c_str()); + TIME_TRACER_ITEM_END("connectToServiceByUUID::bt_socket_connect_rfcomm", 1); + + switch(ret) { + case BT_ERROR_NONE: + { + LoggerD("bt_socket_connect_rfcomm() succeeded"); + TIME_TRACER_ITEM_BEGIN("connectToServiceByUUID::bt_socket_set_connection_state_changed_cb", 1); + bt_socket_set_connection_state_changed_cb(onSocketConnected, this); + TIME_TRACER_ITEM_END("connectToServiceByUUID::bt_socket_set_connection_state_changed_cb", 1); + + BluetoothConnReqPtr connReq = new BluetoothConnReq(uuid, userData); + mConnReqMap.insert(std::pair<std::string, BluetoothConnReqPtr>(remoteAddress, connReq)); + break; + } + case BT_ERROR_INVALID_PARAMETER: + case BT_ERROR_REMOTE_DEVICE_NOT_BONDED: + { + InvalidValuesException *error = new InvalidValuesException("Invalid value"); + BluetoothCallbackUtil::syncToAsyncErrorCallback(userData, error); + break; + } + default: + { + UnknownException *error = new UnknownException("Unknown error"); + BluetoothCallbackUtil::syncToAsyncErrorCallback(userData, error); + } + } + } else { // Not enabled + ServiceNotAvailableException *error = new ServiceNotAvailableException("Bluetooth device is turned off"); + BluetoothCallbackUtil::syncToAsyncErrorCallback(userData, error); + } +} + +void BluetoothAdapter::returnKnownDevices(Common::MultiCallbackUserDataPtr userData) +{ + if(mEnabled == true) { + knownDevices = mFoundDevices; + if(bt_adapter_foreach_bonded_device(foreachBondedDevicesCB, this) == BT_ERROR_NONE) { + if(knownDevices.size() > 0) { // There are found devices + + int num = knownDevices.size(); + LoggerD("There are found devices " << num); + JSObjectRef devices[num]; + for(int i = 0; i < num; i++) { + JSObjectRef deviceObj = JSBluetoothDevice::createJSObject(userData->getContext(), knownDevices[i]); + devices[i] = deviceObj; + } + + userData->invokeCallback("success", JSObjectMakeArray(userData->getContext(), num, devices, NULL)); + } + else { // There is no found device + userData->invokeCallback("success", JSObjectMakeArray(userData->getContext(), 0, NULL, NULL) ); + } + } + else { + LoggerE("Unknown exception"); + userData->invokeCallback( + "error", + JSWebAPIErrorFactory::makeErrorObject(userData->getContext(), UnknownException("Unknown exception")) + ); + } + } else { // Not enabled + LoggerE("Bluetooth device is turned off"); + userData->invokeCallback( + "error", + JSWebAPIErrorFactory::makeErrorObject(userData->getContext(), ServiceNotAvailableException("Bluetooth device is turned off")) + ); + } +} + +void BluetoothAdapter::returnDevice(std::string &address, Common::MultiCallbackUserDataPtr userData) +{ + TIME_TRACER_ITEM_BEGIN(__FUNCTION__, 1); + + if(!isValidAddress(address)) { + LoggerE("Wrong address"); + userData->invokeCallback("error", JSWebAPIErrorFactory::makeErrorObject(userData->getContext(), NotFoundException("Wrong address"))); + return; + } + + if(mEnabled == true) { + bt_device_info_s *deviceInfo = NULL; + + TIME_TRACER_ITEM_BEGIN("returnDevice::bt_adapter_get_bonded_device_info", 1); + if(bt_adapter_get_bonded_device_info(address.c_str(), &deviceInfo) == BT_ERROR_NONE && + deviceInfo != NULL) { + TIME_TRACER_ITEM_END("returnDevice::bt_adapter_get_bonded_device_info", 1); + BluetoothDeviceSharedPtr device(new BluetoothDevice(deviceInfo)); + device->copyAceCheckAccessFunction(getInstance()); + bt_adapter_free_device_info(deviceInfo); + + LoggerD("invoke successCallback"); + userData->invokeCallback("success", JSBluetoothDevice::createJSObject(userData->getContext(), device)); + TIME_TRACER_ITEM_END("returnDevice::bt_adapter_get_bonded_device_info", 1); + return; + } + + std::vector<BluetoothDeviceSharedPtr>::iterator iter; + for(iter = mFoundDevices.begin(); iter != mFoundDevices.end(); ++iter) { + BluetoothDeviceSharedPtr foundDevice = *iter; + if(!strcmp(foundDevice->getAddress().c_str(), address.c_str())) { + LoggerD("Found in mFoundDevices"); + userData->invokeCallback("success", JSBluetoothDevice::createJSObject(userData->getContext(), foundDevice)); + break; + } + } + + if(iter == mFoundDevices.end()) { + LoggerE("Can't find this device"); + + userData->invokeCallback( + "error", + JSWebAPIErrorFactory::makeErrorObject(userData->getContext(), NotFoundException("There is no device with the given address")) + ); + } + + } else { // Not enabled + LoggerE("Bluetooth device is turned off"); + userData->invokeCallback( + "error", + JSWebAPIErrorFactory::makeErrorObject(userData->getContext(), ServiceNotAvailableException("Bluetooth device is turned off")) + ); + } + + TIME_TRACER_ITEM_END(__FUNCTION__, 1); +} + +void BluetoothAdapter::returnRegisteredService(std::string &uuid, std::string &name, Common::MultiCallbackUserDataPtr userData) +{ + TIME_TRACER_ITEM_BEGIN(__FUNCTION__, 1); + + if(!isValidUUID(uuid)) { + LoggerE("Wrong UUID"); + userData->invokeCallback("error", JSWebAPIErrorFactory::makeErrorObject(userData->getContext(), InvalidValuesException("Wrong UUID"))); + return; + } + + if(mEnabled == true) { + + bool isRegistered; + TIME_TRACER_ITEM_BEGIN("returnRegisteredService::bt_adapter_is_service_used", 1); + if(bt_adapter_is_service_used(uuid.c_str(), &isRegistered) == BT_ERROR_NONE && isRegistered == true) { + TIME_TRACER_ITEM_END("returnRegisteredService::bt_adapter_is_service_used", 1); + LoggerD("Already registered"); + userData->invokeCallback("error", JSWebAPIErrorFactory::makeErrorObject(userData->getContext(), InvalidValuesException("Already registered"))); + return; + } + TIME_TRACER_ITEM_END("returnRegisteredService::bt_adapter_is_service_used", 1); + + int socket = -1; + TIME_TRACER_ITEM_BEGIN("returnRegisteredService::bt_socket_create_rfcomm", 1); + int ret = bt_socket_create_rfcomm(uuid.c_str(), &socket); + TIME_TRACER_ITEM_END("returnRegisteredService::bt_socket_create_rfcomm", 1); + + switch(ret) { + case BT_ERROR_NONE: + { + LoggerD("bt_socket_create_rfcomm() succeeded"); + TIME_TRACER_ITEM_BEGIN("returnRegisteredService::bt_socket_listen_and_accept_rfcomm", 1); + int ret = bt_socket_listen_and_accept_rfcomm(socket, 0); + TIME_TRACER_ITEM_END("returnRegisteredService::bt_socket_listen_and_accept_rfcomm", 1); + switch(ret) { + case BT_ERROR_NONE: + { + LoggerD("bt_socket_listen() succeeded"); + bt_socket_set_connection_state_changed_cb(onSocketConnected, this); + + BluetoothServiceHandlerPtr serviceHandler = new BluetoothServiceHandler(uuid, name, socket); + serviceHandler->copyAceCheckAccessFunction(getInstance()); + mRegisteredUUID.insert(std::pair<std::string, BluetoothServiceHandlerPtr>(uuid, serviceHandler)); + + JSObjectRef serviceObj = JSBluetoothServiceHandler::createJSObject(userData->getContext(), serviceHandler); + userData->invokeCallback("success", serviceObj); + break; + } + case BT_ERROR_INVALID_PARAMETER: + { + LoggerD("Invalid value"); + userData->invokeCallback("error", JSWebAPIErrorFactory::makeErrorObject(userData->getContext(), InvalidValuesException("Invalid value"))); + break; + } + default: + { + LoggerD("Unknown exception"); + userData->invokeCallback("error", JSWebAPIErrorFactory::makeErrorObject(userData->getContext(), UnknownException("Unknown exception"))); + } + } + + break; + } + case BT_ERROR_INVALID_PARAMETER: + { + LoggerD("Invalid value"); + userData->invokeCallback("error", JSWebAPIErrorFactory::makeErrorObject(userData->getContext(), InvalidValuesException("Invalid value"))); + break; + } + default: + { + LoggerD("Unknown exception"); + userData->invokeCallback("error", JSWebAPIErrorFactory::makeErrorObject(userData->getContext(), UnknownException("Unknown exception"))); + } + } + } else { // Not enabled + LoggerE("Bluetooth device is turned off"); + userData->invokeCallback( + "error", + JSWebAPIErrorFactory::makeErrorObject(userData->getContext(), ServiceNotAvailableException("Bluetooth device is turned off")) + ); + } + + TIME_TRACER_ITEM_BEGIN(__FUNCTION__, 1); +} + +void BluetoothAdapter::setChangeListener(MultiCallbackUserDataPtr userData) +{ + TIME_TRACER_ITEM_BEGIN(__FUNCTION__, 1); + + LoggerD("Enter"); + mChangeListener = userData; + + TIME_TRACER_ITEM_BEGIN(__FUNCTION__, 1); +} + +void BluetoothAdapter::unsetChangeListener() +{ + TIME_TRACER_ITEM_BEGIN(__FUNCTION__, 1); + + LoggerD("Enter"); + mChangeListener.reset(); + + TIME_TRACER_ITEM_BEGIN(__FUNCTION__, 1); +} + + + + + +} // Bluetooth +} // DeviceAPI diff --git a/mobile_src/Bluetooth/BluetoothAdapter.h b/mobile_src/Bluetooth/BluetoothAdapter.h new file mode 100644 index 0000000..731b81e --- /dev/null +++ b/mobile_src/Bluetooth/BluetoothAdapter.h @@ -0,0 +1,137 @@ +// +// Tizen Web Device API +// Copyright (c) 2012 Samsung Electronics Co., Ltd. +// +// Licensed under the Apache License, Version 2.0 (the License); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. +// + +#ifndef __TIZEN_BLUETOOTH_ADAPTER_H__ +#define __TIZEN_BLUETOOTH_ADAPTER_H__ + +#include <string> +#include <vector> +#include <map> + +#include <bluetooth.h> +#include <Security.h> +#include <MultiCallbackUserData.h> +#include <PlatformException.h> +#include "BluetoothDevice.h" +#include "BluetoothServiceHandler.h" +#include "BluetoothSocket.h" + +namespace DeviceAPI { +namespace Bluetooth { + +class BluetoothConnReq +{ +public: + BluetoothConnReq(std::string uuid, Common::MultiCallbackUserDataPtr userData) + { + mUUID = uuid; + mUserData = userData; + }; + + std::string mUUID; + Common::MultiCallbackUserDataPtr mUserData; +}; +typedef BluetoothConnReq* BluetoothConnReqPtr; + +class BluetoothAdapter : public Common::SecurityAccessor +{ +public: + enum adapterAsyncE + { + SET_POWERED = 0, + SET_NAME, + SET_VISIBLE, + DISCOVER_DEVICES, + STOP_DISCOVERY, + CREATE_BONDING, + DESTROY_BONDING, + }; + + bool getPowered() const; + void setPowered(bool powered, Common::MultiCallbackUserDataPtr userData); + std::string getName() const; + void setName(std::string &name, Common::MultiCallbackUserDataPtr userData); + std::string getAddress() const; + bool getVisible() const; + void setVisible(bool visible, unsigned int timeout, Common::MultiCallbackUserDataPtr userData); + void discoverDevices(Common::MultiCallbackUserDataPtr userData); + void stopDiscovery(Common::MultiCallbackUserDataPtr userData); + void getKnownDevices(Common::MultiCallbackUserDataPtr userData); + void getDevice(std::string &address, Common::MultiCallbackUserDataPtr userData); + void createBonding(std::string &address, Common::MultiCallbackUserDataPtr userData); + void destroyBonding(std::string &address, Common::MultiCallbackUserDataPtr userData); + void registerRFCOMMServiceByUUID(std::string &uuid, std::string &name, Common::MultiCallbackUserDataPtr userData); + void connectToServiceByUUID(std::string &remoteAddress, std::string &uuid, Common::MultiCallbackUserDataPtr userData); + + void returnKnownDevices(Common::MultiCallbackUserDataPtr userData); + void returnDevice(std::string &address, Common::MultiCallbackUserDataPtr userData); + void returnRegisteredService(std::string &uuid, std::string &name, Common::MultiCallbackUserDataPtr userData); + + void unregisterUUID(std::string &uuid); + bool closeConnectedSocket(int socket); + void removeConnReq(std::string &remoteAddress); + + void setChangeListener(Common::MultiCallbackUserDataPtr userData); + void unsetChangeListener(); + + static BluetoothAdapter* getInstance(); + static bool isValidAddress(std::string &address); + static bool isValidUUID(std::string &uuid); + void unloadFrame(JSContextRef context); +private: + BluetoothAdapter(); + virtual ~BluetoothAdapter(); + + static void onStateChangedCB(int result, bt_adapter_state_e adapterState, void *userData); + static void onNameChangedCB(char *name, void *userData); + static void onVisibilityChangedCB(int result, bt_adapter_visibility_mode_e visibilityMode, void *userData); + static void onDiscoveryStateChangedCB(int result, bt_adapter_device_discovery_state_e discoveryState, + bt_adapter_device_discovery_info_s *discoveryInfo, void *userData); + static bool foreachBondedDevicesCB(bt_device_info_s *deviceInfo, void *userData); + static void onBondCreatedCB(int result, bt_device_info_s *deviceInfo, void *userData); + static void onBondDestroyedCB(int result, char *remoteAddress, void *userData); + static void onSocketConnected(int result, bt_socket_connection_state_e state, bt_socket_connection_s *connection, void *userData); + static void onSocketReceivedCB(bt_socket_received_data_s *data, void *userData); + + typedef std::multimap<std::string, BluetoothConnReqPtr> ConnReqMultiMapT; // <remoteAddress, BluetoothConnReqPtr> + typedef std::map<std::string, BluetoothServiceHandlerPtr> RegisteredUUIDMapT; // <UUID, BluetoothServiceHandlerPtr> + typedef std::map<int, BluetoothSocketPtr> ConnectedSocketMapT; // <socketFD, BluetoothSocketPtr> + + bool mEnabled; + bool mVisible; + ConnReqMultiMapT mConnReqMap; + RegisteredUUIDMapT mRegisteredUUID; + ConnectedSocketMapT mConnectedSocket; + bool mRequestedState; + std::string mRequestedName; + bt_adapter_visibility_mode_e mRequestedVisibility; + std::string mCreateBondingAddress; + std::string mDestroyBondingAddress; + Common::MultiCallbackUserDataPtr mUserDataList[DESTROY_BONDING + 1]; + std::vector<std::string> mDisappearedDevices; + std::vector<BluetoothDeviceSharedPtr> mFoundDevices; + std::vector<BluetoothDeviceSharedPtr> knownDevices; + Common::MultiCallbackUserDataPtr mChangeListener; + +}; + +typedef BluetoothAdapter* BluetoothAdapterPtr; + +} // Bluetooth +} // DeviceAPI + +#endif // __TIZEN_BLUETOOTH_ADAPTER_H__ diff --git a/mobile_src/Bluetooth/BluetoothCallback.cpp b/mobile_src/Bluetooth/BluetoothCallback.cpp new file mode 100644 index 0000000..211c541 --- /dev/null +++ b/mobile_src/Bluetooth/BluetoothCallback.cpp @@ -0,0 +1,102 @@ +// +// Tizen Web Device API +// Copyright (c) 2012 Samsung Electronics Co., Ltd. +// +// Licensed under the Apache License, Version 2.0 (the License); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. +// + +#include <Ecore.h> + +#include <JSUtil.h> +#include <JSWebAPIErrorFactory.h> +#include <Logger.h> + +#include "BluetoothCallback.h" + + +using namespace DeviceAPI::Common; + +namespace DeviceAPI { +namespace Bluetooth { + + +class BluetoothCallbackData +{ +public: + BluetoothCallbackData(MultiCallbackUserDataPtr callback) + { + mCallback = callback; + mError = NULL; + }; + + BluetoothCallbackData(MultiCallbackUserDataPtr callback, Common::BasePlatformException *error) + { + mCallback = callback; + mError = error; + }; + + virtual ~BluetoothCallbackData() + { + if(mError) + delete mError; + }; + + MultiCallbackUserDataPtr mCallback; + Common::BasePlatformException *mError; +}; + +typedef BluetoothCallbackData* BluetoothCallbackDataPtr; + + +static Eina_Bool jobCompleteCB(void *userData){ + BluetoothCallbackDataPtr data = static_cast<BluetoothCallbackDataPtr>(userData); + + if(!data) { + LoggerW("BluetoothCallbackDataPtr is NULL"); + return false; + } + + if(!(data->mCallback)) { + LoggerW("MulticallbackUserData is NULL"); + delete data; + return false; + } + + if(data->mError == NULL) { // Success Callback + LoggerD("Success Callback"); + data->mCallback->invokeCallback("success"); + } + else { // Error Callback + LoggerD("BT_CB_ERROR"); + data->mCallback->invokeCallback("error", JSWebAPIErrorFactory::makeErrorObject(data->mCallback->getContext(), *(data->mError))); + } + + delete data; + return false; +} + +void BluetoothCallback::syncToAsyncSuccessCB(MultiCallbackUserDataPtr callback) +{ + BluetoothCallbackDataPtr data = new BluetoothCallbackData(callback); + ecore_idler_add(jobCompleteCB, data); +} + +void BluetoothCallback::syncToAsyncErrorCB(MultiCallbackUserDataPtr callback, Common::BasePlatformException *error) +{ + BluetoothCallbackDataPtr data = new BluetoothCallbackData(callback, error); + ecore_idler_add(jobCompleteCB, data); +} + + +} // Bluetooth +} // DeviceAPI diff --git a/mobile_src/Bluetooth/BluetoothCallback.h b/mobile_src/Bluetooth/BluetoothCallback.h new file mode 100644 index 0000000..b642b23 --- /dev/null +++ b/mobile_src/Bluetooth/BluetoothCallback.h @@ -0,0 +1,44 @@ +// +// Tizen Web Device API +// Copyright (c) 2012 Samsung Electronics Co., Ltd. +// +// Licensed under the Apache License, Version 2.0 (the License); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. +// + +#ifndef __TIZEN_BLUETOOTH_CALLBACK_H__ +#define __TIZEN_BLUETOOTH_CALLBACK_H__ + +#include <string> +#include <MultiCallbackUserData.h> +#include <PlatformException.h> + + +namespace DeviceAPI { +namespace Bluetooth { + + +class BluetoothCallback +{ +public: + static void syncToAsyncSuccessCB(DeviceAPI::Common::MultiCallbackUserDataPtr userData); + static void syncToAsyncErrorCB(DeviceAPI::Common::MultiCallbackUserDataPtr userData, Common::BasePlatformException *error); +}; + + +} // Bluetooth +} // DeviceAPI + + + +#endif // __TIZEN_BLUETOOTH_CALLBACK_H__ + diff --git a/mobile_src/Bluetooth/BluetoothCallbackUtil.cpp b/mobile_src/Bluetooth/BluetoothCallbackUtil.cpp new file mode 100644 index 0000000..9082954 --- /dev/null +++ b/mobile_src/Bluetooth/BluetoothCallbackUtil.cpp @@ -0,0 +1,120 @@ +// +// Tizen Web Device API +// Copyright (c) 2012 Samsung Electronics Co., Ltd. +// +// Licensed under the Apache License, Version 2.0 (the License); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. +// + +#include <Ecore.h> + +#include <JSUtil.h> +#include <JSWebAPIErrorFactory.h> + +#include "BluetoothCallbackUtil.h" +#include "BluetoothAdapter.h" + +#include <Logger.h> + +using namespace DeviceAPI::Common; + +namespace DeviceAPI { +namespace Bluetooth { + +static Eina_Bool jobCompleteCB(void *userData){ + BluetoothCallbackUserDataPtr data = static_cast<BluetoothCallbackUserDataPtr>(userData); + + if(!data) { + LoggerW("BluetoothCallbackUserDataPtr is NULL"); + return false; + } + + if(!(data->mUserData)) { + LoggerW("MulticallbackUserData is NULL"); + delete data; + return false; + } + + switch(data->mCBType) { + case BluetoothCallbackUserData::BT_CB_SUCCESS: + { + LoggerD("BT_CB_SUCCESS"); + data->mUserData->invokeCallback("success"); + break; + } + case BluetoothCallbackUserData::BT_CB_ERROR: + { + LoggerD("BT_CB_ERROR"); + data->mUserData->invokeCallback("error", JSWebAPIErrorFactory::makeErrorObject(data->mUserData->getContext(), *(data->mError))); + break; + } + case BluetoothCallbackUserData::BT_CB_DEVICE: + { + LoggerD("BT_CB_DEVICE"); + BluetoothAdapter::getInstance()->returnDevice(data->mAddress, data->mUserData); + break; + } + case BluetoothCallbackUserData::BT_CB_DEVICES: + { + LoggerD("BT_CB_DEVICES"); + BluetoothAdapter::getInstance()->returnKnownDevices(data->mUserData); + break; + } + case BluetoothCallbackUserData::BT_CB_SERVICE: + { + LoggerD("BT_CB_SERVICE"); + BluetoothAdapter::getInstance()->returnRegisteredService(data->mUUID, data->mName, data->mUserData); + break; + } + default: + { + LoggerW("Unknown callback type"); + } + } + + delete data; + return false; +} + +void BluetoothCallbackUtil::syncToAsyncSuccessCallback(Common::MultiCallbackUserDataPtr userData) +{ + BluetoothCallbackUserDataPtr data = new BluetoothCallbackUserData(userData, BluetoothCallbackUserData::BT_CB_SUCCESS); + ecore_idler_add(jobCompleteCB, data); +} + +void BluetoothCallbackUtil::syncToAsyncErrorCallback(DeviceAPI::Common::MultiCallbackUserDataPtr userData, Common::BasePlatformException *error) +{ + BluetoothCallbackUserDataPtr data = new BluetoothCallbackUserData(userData, error); + ecore_idler_add(jobCompleteCB, data); +} + +void BluetoothCallbackUtil::syncToAsyncDeviceCallback(Common::MultiCallbackUserDataPtr userData, std::string &address) +{ + BluetoothCallbackUserDataPtr data = new BluetoothCallbackUserData(userData, address); + ecore_idler_add(jobCompleteCB, data); +} + +void BluetoothCallbackUtil::syncToAsyncDeviceArrayCallback(Common::MultiCallbackUserDataPtr userData) +{ + BluetoothCallbackUserDataPtr data = new BluetoothCallbackUserData(userData, BluetoothCallbackUserData::BT_CB_DEVICES); + ecore_idler_add(jobCompleteCB, data); +} + +void BluetoothCallbackUtil::syncToAsyncServiceCallback(Common::MultiCallbackUserDataPtr userData, std::string &uuid, std::string &name) +{ + BluetoothCallbackUserDataPtr data = new BluetoothCallbackUserData(userData, uuid, name); + ecore_idler_add(jobCompleteCB, data); +} + + +} // Bluetooth +} // DeviceAPI diff --git a/mobile_src/Bluetooth/BluetoothCallbackUtil.h b/mobile_src/Bluetooth/BluetoothCallbackUtil.h new file mode 100644 index 0000000..76926d6 --- /dev/null +++ b/mobile_src/Bluetooth/BluetoothCallbackUtil.h @@ -0,0 +1,105 @@ +// +// Tizen Web Device API +// Copyright (c) 2012 Samsung Electronics Co., Ltd. +// +// Licensed under the Apache License, Version 2.0 (the License); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. +// + +#ifndef __TIZEN_BLUETOOTH_CALLBACK_UTIL_H__ +#define __TIZEN_BLUETOOTH_CALLBACK_UTIL_H__ + +#include <string> +#include <JavaScriptCore/JavaScript.h> +#include <MultiCallbackUserData.h> +#include <PlatformException.h> + + +namespace DeviceAPI { +namespace Bluetooth { + + +class BluetoothCallbackUserData +{ +public: + typedef enum { + BT_CB_SUCCESS, + BT_CB_ERROR, + BT_CB_DEVICE, + BT_CB_DEVICES, + BT_CB_SERVICE + } callbackTypeE; + + + BluetoothCallbackUserData(DeviceAPI::Common::MultiCallbackUserDataPtr userData, callbackTypeE type) + { + mUserData = userData; + mCBType = type; + }; + + BluetoothCallbackUserData(DeviceAPI::Common::MultiCallbackUserDataPtr userData, Common::BasePlatformException *error) + { + mUserData = userData; + mError = error; + mCBType = BT_CB_ERROR; + }; + + BluetoothCallbackUserData(DeviceAPI::Common::MultiCallbackUserDataPtr userData, std::string &uuid, std::string &name) + { + mUserData = userData; + mCBType = BT_CB_SERVICE; + mUUID = uuid; + mName = name; + }; + + BluetoothCallbackUserData(DeviceAPI::Common::MultiCallbackUserDataPtr userData, std::string &address) + { + mUserData = userData; + mCBType = BT_CB_DEVICE; + mAddress = address; + }; + + virtual ~BluetoothCallbackUserData() + { + if(mCBType == BT_CB_ERROR) + delete mError; + }; + + + callbackTypeE mCBType; + DeviceAPI::Common::MultiCallbackUserDataPtr mUserData; + Common::BasePlatformException *mError; + std::string mUUID; + std::string mName; + std::string mAddress; +}; + +typedef BluetoothCallbackUserData* BluetoothCallbackUserDataPtr; + + +class BluetoothCallbackUtil +{ +public: + static void syncToAsyncSuccessCallback(DeviceAPI::Common::MultiCallbackUserDataPtr userData); + static void syncToAsyncErrorCallback(DeviceAPI::Common::MultiCallbackUserDataPtr userData, Common::BasePlatformException *error); + static void syncToAsyncDeviceCallback(DeviceAPI::Common::MultiCallbackUserDataPtr userData, std::string &address); + static void syncToAsyncDeviceArrayCallback(DeviceAPI::Common::MultiCallbackUserDataPtr userData); + static void syncToAsyncServiceCallback(DeviceAPI::Common::MultiCallbackUserDataPtr userData, std::string &uuid, std::string &name); +}; + + +} // Bluetooth +} // DeviceAPI + + + +#endif // __TIZEN_BLUETOOTH_CALLBACK_UTIL_H__ diff --git a/mobile_src/Bluetooth/BluetoothClass.cpp b/mobile_src/Bluetooth/BluetoothClass.cpp new file mode 100644 index 0000000..6486574 --- /dev/null +++ b/mobile_src/Bluetooth/BluetoothClass.cpp @@ -0,0 +1,83 @@ +// +// Tizen Web Device API +// Copyright (c) 2012 Samsung Electronics Co., Ltd. +// +// Licensed under the Apache License, Version 2.0 (the License); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. +// + +#include <PlatformException.h> +#include <JSUtil.h> + +#include "plugin_config.h" +#include "BluetoothClass.h" +#include "BluetoothClassDeviceMajor.h" +#include "BluetoothClassDeviceMinor.h" +#include "BluetoothClassDeviceService.h" +#include "JSBluetoothClassDeviceService.h" + +#include <Logger.h> + +using namespace DeviceAPI::Common; + +namespace DeviceAPI { +namespace Bluetooth { + +BluetoothClass::BluetoothClass(bt_class_s bluetoothClass) +{ + Common::SecurityAccessor(); + mMajor = BluetoothClassDeviceMajor::getInstance()->getMajorValue(bluetoothClass.major_device_class); + mMinor = BluetoothClassDeviceMinor::getInstance()->getMinorValue(bluetoothClass.minor_device_class); + mServices= BluetoothClassDeviceService::getInstance()->getServiceValues(bluetoothClass.major_service_class_mask); +} + +BluetoothClass::~BluetoothClass() +{ +} + +unsigned long BluetoothClass::getMajor() const +{ + return mMajor; +} + +unsigned long BluetoothClass::getMinor() const +{ + return mMinor; +} + +JSValueRef BluetoothClass::getServices(JSContextRef context) +{ +/* + JSValueRef service = mLocalProperty.getProperty(context, BLUETOOTH_CLASS_SERVICES); + if(service == NULL) { + service = JSUtil::toJSValueRef_(context, mServices); + mLocalProperty.setProperty(context, BLUETOOTH_CLASS_SERVICES, service); + } + + return service; +*/ + return JSUtil::toJSValueRef_(context, mServices); +} + +bool BluetoothClass::hasService(unsigned long service) +{ + for(std::vector<unsigned long>::iterator iter = mServices.begin(); iter != mServices.end(); ++iter) { + if((*iter) == service) { + return true; + } + } + return false; +} + + +} // Bluetooth +} // DeviceAPI diff --git a/mobile_src/Bluetooth/BluetoothClass.h b/mobile_src/Bluetooth/BluetoothClass.h new file mode 100644 index 0000000..d1d18dd --- /dev/null +++ b/mobile_src/Bluetooth/BluetoothClass.h @@ -0,0 +1,57 @@ +// +// Tizen Web Device API +// Copyright (c) 2012 Samsung Electronics Co., Ltd. +// +// Licensed under the Apache License, Version 2.0 (the License); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. +// + +#ifndef __TIZEN_BLUETOOTH_CLASS_H__ +#define __TIZEN_BLUETOOTH_CLASS_H__ + +#include <string> +#include <vector> + +#include <JavaScriptCore/JavaScript.h> +#include <bluetooth.h> +#include <Security.h> +#include <boost/shared_ptr.hpp> +#include <PropertyBag.h> + +namespace DeviceAPI { +namespace Bluetooth { + +class BluetoothClass : public Common::SecurityAccessor +{ +public: + BluetoothClass(bt_class_s bluetoothClass); + virtual ~BluetoothClass(); + + unsigned long getMajor() const; + unsigned long getMinor() const; + JSValueRef getServices(JSContextRef context); + + bool hasService(unsigned long service); + +private: + unsigned long mMajor; + unsigned long mMinor; + std::vector<unsigned long> mServices; + //Common::PropertyBag mLocalProperty; +}; + +typedef boost::shared_ptr<BluetoothClass> BluetoothClassSharedPtr; + +} // Bluetooth +} // DeviceAPI + +#endif // __TIZEN_BLUETOOTH_CLASS_H__ diff --git a/mobile_src/Bluetooth/BluetoothClassDeviceMajor.cpp b/mobile_src/Bluetooth/BluetoothClassDeviceMajor.cpp new file mode 100644 index 0000000..fb7f03d --- /dev/null +++ b/mobile_src/Bluetooth/BluetoothClassDeviceMajor.cpp @@ -0,0 +1,76 @@ +// +// Tizen Web Device API +// Copyright (c) 2012 Samsung Electronics Co., Ltd. +// +// Licensed under the Apache License, Version 2.0 (the License); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. +// + +#include <PlatformException.h> + +#include "BluetoothClassDeviceMajor.h" + +#include <Logger.h> + +namespace DeviceAPI { +namespace Bluetooth { + +BluetoothClassDeviceMajor::BluetoothClassDeviceMajor() +{ + mMajorStringMap.insert(std::pair<std::string, unsigned long>("MISC", 0x00)); + mMajorStringMap.insert(std::pair<std::string, unsigned long>("COMPUTER", 0x01)); + mMajorStringMap.insert(std::pair<std::string, unsigned long>("PHONE", 0x02)); + mMajorStringMap.insert(std::pair<std::string, unsigned long>("NETWORK", 0x03)); + mMajorStringMap.insert(std::pair<std::string, unsigned long>("AUDIO_VIDEO", 0x04)); + mMajorStringMap.insert(std::pair<std::string, unsigned long>("PERIPHERAL", 0x05)); + mMajorStringMap.insert(std::pair<std::string, unsigned long>("IMAGING", 0x06)); + mMajorStringMap.insert(std::pair<std::string, unsigned long>("WEARABLE", 0x07)); + mMajorStringMap.insert(std::pair<std::string, unsigned long>("TOY", 0x08)); + mMajorStringMap.insert(std::pair<std::string, unsigned long>("HEALTH", 0x09)); + mMajorStringMap.insert(std::pair<std::string, unsigned long>("UNCATEGORIZED", 0x1F)); + + mMajorEnumMap.insert(std::pair<bt_major_device_class_e, unsigned long>(BT_MAJOR_DEVICE_CLASS_MISC, 0x00)); + mMajorEnumMap.insert(std::pair<bt_major_device_class_e, unsigned long>(BT_MAJOR_DEVICE_CLASS_COMPUTER, 0x01)); + mMajorEnumMap.insert(std::pair<bt_major_device_class_e, unsigned long>(BT_MAJOR_DEVICE_CLASS_PHONE, 0x02)); + mMajorEnumMap.insert(std::pair<bt_major_device_class_e, unsigned long>(BT_MAJOR_DEVICE_CLASS_LAN_NETWORK_ACCESS_POINT, 0x03)); + mMajorEnumMap.insert(std::pair<bt_major_device_class_e, unsigned long>(BT_MAJOR_DEVICE_CLASS_AUDIO_VIDEO, 0x04)); + mMajorEnumMap.insert(std::pair<bt_major_device_class_e, unsigned long>(BT_MAJOR_DEVICE_CLASS_PERIPHERAL, 0x05)); + mMajorEnumMap.insert(std::pair<bt_major_device_class_e, unsigned long>(BT_MAJOR_DEVICE_CLASS_IMAGING, 0x06)); + mMajorEnumMap.insert(std::pair<bt_major_device_class_e, unsigned long>(BT_MAJOR_DEVICE_CLASS_WEARABLE, 0x07)); + mMajorEnumMap.insert(std::pair<bt_major_device_class_e, unsigned long>(BT_MAJOR_DEVICE_CLASS_TOY, 0x08)); + mMajorEnumMap.insert(std::pair<bt_major_device_class_e, unsigned long>(BT_MAJOR_DEVICE_CLASS_HEALTH, 0x09)); + mMajorEnumMap.insert(std::pair<bt_major_device_class_e, unsigned long>(BT_MAJOR_DEVICE_CLASS_UNCATEGORIZED, 0x1F)); +} + +BluetoothClassDeviceMajor::~BluetoothClassDeviceMajor() +{ +} + +BluetoothClassDeviceMajor* BluetoothClassDeviceMajor::getInstance() +{ + static BluetoothClassDeviceMajor instance; + return &instance; +} + +unsigned long BluetoothClassDeviceMajor::getMajorValue(std::string major) +{ + return mMajorStringMap.find(major)->second; +} + +unsigned long BluetoothClassDeviceMajor::getMajorValue(bt_major_device_class_e major) +{ + return mMajorEnumMap.find(major)->second; +} + + +} // Bluetooth +} // DeviceAPI diff --git a/mobile_src/Bluetooth/BluetoothClassDeviceMajor.h b/mobile_src/Bluetooth/BluetoothClassDeviceMajor.h new file mode 100644 index 0000000..dbc3b9c --- /dev/null +++ b/mobile_src/Bluetooth/BluetoothClassDeviceMajor.h @@ -0,0 +1,47 @@ +// +// Tizen Web Device API +// Copyright (c) 2012 Samsung Electronics Co., Ltd. +// +// Licensed under the Apache License, Version 2.0 (the License); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. +// + +#ifndef __TIZEN_BLUETOOTH_CLASS_DEVICE_MAJOR_H__ +#define __TIZEN_BLUETOOTH_CLASS_DEVICE_MAJOR_H__ + +#include <string> +#include <map> +#include <bluetooth.h> + +namespace DeviceAPI { +namespace Bluetooth { + +class BluetoothClassDeviceMajor +{ +public: + + BluetoothClassDeviceMajor(); + virtual ~BluetoothClassDeviceMajor(); + + static BluetoothClassDeviceMajor* getInstance(); + unsigned long getMajorValue(std::string major); + unsigned long getMajorValue(bt_major_device_class_e major); + +private: + std::map<std::string, unsigned long> mMajorStringMap; + std::map<bt_major_device_class_e, unsigned long> mMajorEnumMap; +}; + +} // Bluetooth +} // DeviceAPI + +#endif // __TIZEN_BLUETOOTH_CLASS_DEVICE_MAJOR_H__ diff --git a/mobile_src/Bluetooth/BluetoothClassDeviceMinor.cpp b/mobile_src/Bluetooth/BluetoothClassDeviceMinor.cpp new file mode 100644 index 0000000..3396761 --- /dev/null +++ b/mobile_src/Bluetooth/BluetoothClassDeviceMinor.cpp @@ -0,0 +1,203 @@ +// +// Tizen Web Device API +// Copyright (c) 2012 Samsung Electronics Co., Ltd. +// +// Licensed under the Apache License, Version 2.0 (the License); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. +// + +#include <PlatformException.h> + +#include "BluetoothClassDeviceMinor.h" + +#include <Logger.h> + +namespace DeviceAPI { +namespace Bluetooth { + +BluetoothClassDeviceMinor::BluetoothClassDeviceMinor() +{ + mMinorStringMap.insert(std::pair<std::string, unsigned long>("COMPUTER_UNCATEGORIZED", 0x00)); + mMinorStringMap.insert(std::pair<std::string, unsigned long>("COMPUTER_DESKTOP", 0x01)); + mMinorStringMap.insert(std::pair<std::string, unsigned long>("COMPUTER_SERVER", 0x02)); + mMinorStringMap.insert(std::pair<std::string, unsigned long>("COMPUTER_LAPTOP", 0x03)); + mMinorStringMap.insert(std::pair<std::string, unsigned long>("COMPUTER_HANDHELD_PC_OR_PDA", 0x04)); + mMinorStringMap.insert(std::pair<std::string, unsigned long>("COMPUTER_PALM_PC_OR_PDA", 0x5)); + mMinorStringMap.insert(std::pair<std::string, unsigned long>("COMPUTER_WEARABLE", 0x06)); + mMinorStringMap.insert(std::pair<std::string, unsigned long>("PHONE_UNCATEGORIZED", 0x00)); + mMinorStringMap.insert(std::pair<std::string, unsigned long>("PHONE_CELLULAR", 0x01)); + mMinorStringMap.insert(std::pair<std::string, unsigned long>("PHONE_CORDLESS", 0x02)); + mMinorStringMap.insert(std::pair<std::string, unsigned long>("PHONE_SMARTPHONE", 0x03)); + mMinorStringMap.insert(std::pair<std::string, unsigned long>("PHONE_MODEM_OR_GATEWAY", 0x04)); + mMinorStringMap.insert(std::pair<std::string, unsigned long>("PHONE_ISDN", 0x05)); + mMinorStringMap.insert(std::pair<std::string, unsigned long>("AV_UNRECOGNIZED", 0x00)); + mMinorStringMap.insert(std::pair<std::string, unsigned long>("AV_WEARABLE_HEADSET", 0x01)); + mMinorStringMap.insert(std::pair<std::string, unsigned long>("AV_HANDSFREE", 0x02)); + mMinorStringMap.insert(std::pair<std::string, unsigned long>("AV_MICROPHONE", 0x04)); + mMinorStringMap.insert(std::pair<std::string, unsigned long>("AV_LOUDSPEAKER", 0x05)); + mMinorStringMap.insert(std::pair<std::string, unsigned long>("AV_HEADPHONES", 0x06)); + mMinorStringMap.insert(std::pair<std::string, unsigned long>("AV_PORTABLE_AUDIO", 0x07)); + mMinorStringMap.insert(std::pair<std::string, unsigned long>("AV_CAR_AUDIO", 0x08)); + mMinorStringMap.insert(std::pair<std::string, unsigned long>("AV_SETTOP_BOX", 0x09)); + mMinorStringMap.insert(std::pair<std::string, unsigned long>("AV_HIFI", 0x0a)); + mMinorStringMap.insert(std::pair<std::string, unsigned long>("AV_VCR", 0x0b)); + mMinorStringMap.insert(std::pair<std::string, unsigned long>("AV_VIDEO_CAMERA", 0x0c)); + mMinorStringMap.insert(std::pair<std::string, unsigned long>("AV_CAMCORDER", 0x0d)); + mMinorStringMap.insert(std::pair<std::string, unsigned long>("AV_MONITOR", 0x0e)); + mMinorStringMap.insert(std::pair<std::string, unsigned long>("AV_DISPLAY_AND_LOUDSPEAKER", 0x0f)); + mMinorStringMap.insert(std::pair<std::string, unsigned long>("AV_VIDEO_CONFERENCING", 0x10)); + mMinorStringMap.insert(std::pair<std::string, unsigned long>("AV_GAMING_TOY", 0x12)); + mMinorStringMap.insert(std::pair<std::string, unsigned long>("PERIPHERAL_UNCATEGORIZED", 0)); + mMinorStringMap.insert(std::pair<std::string, unsigned long>("PERIPHERAL_KEYBOARD", 0x10)); + mMinorStringMap.insert(std::pair<std::string, unsigned long>("PERIPHERAL_POINTING_DEVICE", 0x20)); + mMinorStringMap.insert(std::pair<std::string, unsigned long>("PERIPHERAL_KEYBOARD_AND_POINTING_DEVICE", 0x30)); + mMinorStringMap.insert(std::pair<std::string, unsigned long>("PERIPHERAL_JOYSTICK", 0x01)); + mMinorStringMap.insert(std::pair<std::string, unsigned long>("PERIPHERAL_GAMEPAD", 0x02)); + mMinorStringMap.insert(std::pair<std::string, unsigned long>("PERIPHERAL_REMOTE_CONTROL", 0x03)); + mMinorStringMap.insert(std::pair<std::string, unsigned long>("PERIPHERAL_SENSING_DEVICE", 0x04)); + mMinorStringMap.insert(std::pair<std::string, unsigned long>("PERIPHERAL_DEGITIZER_TABLET", 0x05)); + mMinorStringMap.insert(std::pair<std::string, unsigned long>("PERIPHERAL_CARD_READER", 0x06)); + mMinorStringMap.insert(std::pair<std::string, unsigned long>("PERIPHERAL_DIGITAL_PEN", 0x07)); + mMinorStringMap.insert(std::pair<std::string, unsigned long>("PERIPHERAL_HANDHELD_SCANNER", 0x08)); + mMinorStringMap.insert(std::pair<std::string, unsigned long>("PERIPHERAL_HANDHELD_INPUT_DEVICE", 0x09)); + mMinorStringMap.insert(std::pair<std::string, unsigned long>("IMAGING_UNCATEGORIZED", 0x00)); + mMinorStringMap.insert(std::pair<std::string, unsigned long>("IMAGING_DISPLAY", 0x04)); + mMinorStringMap.insert(std::pair<std::string, unsigned long>("IMAGING_CAMERA", 0x08)); + mMinorStringMap.insert(std::pair<std::string, unsigned long>("IMAGING_SCANNER", 0x10)); + mMinorStringMap.insert(std::pair<std::string, unsigned long>("IMAGING_PRINTER", 0x20)); + mMinorStringMap.insert(std::pair<std::string, unsigned long>("WEARABLE_WRITST_WATCH", 0x01)); + mMinorStringMap.insert(std::pair<std::string, unsigned long>("WEARABLE_PAGER", 0x02)); + mMinorStringMap.insert(std::pair<std::string, unsigned long>("WEARABLE_JACKET", 0x03)); + mMinorStringMap.insert(std::pair<std::string, unsigned long>("WEARABLE_HELMET", 0x04)); + mMinorStringMap.insert(std::pair<std::string, unsigned long>("WEARABLE_GLASSES", 0x05)); + mMinorStringMap.insert(std::pair<std::string, unsigned long>("TOY_ROBOT", 0x01)); + mMinorStringMap.insert(std::pair<std::string, unsigned long>("TOY_VEHICLE", 0x02)); + mMinorStringMap.insert(std::pair<std::string, unsigned long>("TOY_DOLL", 0x03)); + mMinorStringMap.insert(std::pair<std::string, unsigned long>("TOY_CONTROLLER", 0x04)); + mMinorStringMap.insert(std::pair<std::string, unsigned long>("TOY_GAME", 0x05)); + mMinorStringMap.insert(std::pair<std::string, unsigned long>("HEALTH_UNDEFINED", 0x00)); + mMinorStringMap.insert(std::pair<std::string, unsigned long>("HEALTH_BLOOD_PRESSURE_MONITOR", 0x01)); + mMinorStringMap.insert(std::pair<std::string, unsigned long>("HEALTH_THERMOMETER", 0x02)); + mMinorStringMap.insert(std::pair<std::string, unsigned long>("HEALTH_WEIGHING_SCALE", 0x03)); + mMinorStringMap.insert(std::pair<std::string, unsigned long>("HEALTH_GLUCOSE_METER", 0x04)); + mMinorStringMap.insert(std::pair<std::string, unsigned long>("HEALTH_PULSE_OXIMETER", 0x05)); + mMinorStringMap.insert(std::pair<std::string, unsigned long>("HEALTH_PULSE_RATE_MONITOR", 0x06)); + mMinorStringMap.insert(std::pair<std::string, unsigned long>("HEALTH_DATA_DISPLAY", 0x07)); + mMinorStringMap.insert(std::pair<std::string, unsigned long>("HEALTH_STEP_COUNTER", 0x08)); + mMinorStringMap.insert(std::pair<std::string, unsigned long>("HEALTH_BODY_COMPOSITION_ANALYZER", 0x09)); + mMinorStringMap.insert(std::pair<std::string, unsigned long>("HEALTH_PEAK_FLOW_MONITOR", 0x0a)); + mMinorStringMap.insert(std::pair<std::string, unsigned long>("HEALTH_MEDICATION_MONITOR", 0x0b)); + mMinorStringMap.insert(std::pair<std::string, unsigned long>("HEALTH_KNEE_PROSTHESIS", 0x0c)); + mMinorStringMap.insert(std::pair<std::string, unsigned long>("HEALTH_ANKLE_PROSTHESIS", 0x0d)); + + mMinorEnumMap.insert(std::pair<bt_minor_device_class_e, unsigned long>(BT_MINOR_DEVICE_CLASS_COMPUTER_UNCATEGORIZED, 0x00)); + mMinorEnumMap.insert(std::pair<bt_minor_device_class_e, unsigned long>(BT_MINOR_DEVICE_CLASS_COMPUTER_DESKTOP_WORKSTATION , 0x01)); + mMinorEnumMap.insert(std::pair<bt_minor_device_class_e, unsigned long>(BT_MINOR_DEVICE_CLASS_COMPUTER_SERVER_CLASS , 0x02)); + mMinorEnumMap.insert(std::pair<bt_minor_device_class_e, unsigned long>(BT_MINOR_DEVICE_CLASS_COMPUTER_LAPTOP , 0x03)); + mMinorEnumMap.insert(std::pair<bt_minor_device_class_e, unsigned long>(BT_MINOR_DEVICE_CLASS_COMPUTER_HANDHELD_PC_OR_PDA , 0x04)); + mMinorEnumMap.insert(std::pair<bt_minor_device_class_e, unsigned long>(BT_MINOR_DEVICE_CLASS_COMPUTER_PALM_SIZED_PC_OR_PDA, 0x5)); + mMinorEnumMap.insert(std::pair<bt_minor_device_class_e, unsigned long>(BT_MINOR_DEVICE_CLASS_COMPUTER_WEARABLE_COMPUTER , 0x06)); + mMinorEnumMap.insert(std::pair<bt_minor_device_class_e, unsigned long>(BT_MINOR_DEVICE_CLASS_PHONE_UNCATEGORIZED , 0x00)); + mMinorEnumMap.insert(std::pair<bt_minor_device_class_e, unsigned long>(BT_MINOR_DEVICE_CLASS_PHONE_CELLULAR , 0x01)); + mMinorEnumMap.insert(std::pair<bt_minor_device_class_e, unsigned long>(BT_MINOR_DEVICE_CLASS_PHONE_CORDLESS , 0x02)); + mMinorEnumMap.insert(std::pair<bt_minor_device_class_e, unsigned long>(BT_MINOR_DEVICE_CLASS_PHONE_SMART_PHONE , 0x03)); + mMinorEnumMap.insert(std::pair<bt_minor_device_class_e, unsigned long>(BT_MINOR_DEVICE_CLASS_PHONE_WIRED_MODEM_OR_VOICE_GATEWAY , 0x04)); + mMinorEnumMap.insert(std::pair<bt_minor_device_class_e, unsigned long>(BT_MINOR_DEVICE_CLASS_PHONE_COMMON_ISDN_ACCESS , 0x05)); + mMinorEnumMap.insert(std::pair<bt_minor_device_class_e, unsigned long>(BT_MINOR_DEVICE_CLASS_AUDIO_VIDEO_UNCATEGORIZED , 0x00)); + mMinorEnumMap.insert(std::pair<bt_minor_device_class_e, unsigned long>(BT_MINOR_DEVICE_CLASS_AUDIO_VIDEO_WEARABLE_HEADSET , 0x01)); + mMinorEnumMap.insert(std::pair<bt_minor_device_class_e, unsigned long>(BT_MINOR_DEVICE_CLASS_AUDIO_VIDEO_HANDS_FREE , 0x02)); + mMinorEnumMap.insert(std::pair<bt_minor_device_class_e, unsigned long>(BT_MINOR_DEVICE_CLASS_AUDIO_VIDEO_MICROPHONE , 0x04)); + mMinorEnumMap.insert(std::pair<bt_minor_device_class_e, unsigned long>(BT_MINOR_DEVICE_CLASS_AUDIO_VIDEO_LOUDSPEAKER , 0x05)); + mMinorEnumMap.insert(std::pair<bt_minor_device_class_e, unsigned long>(BT_MINOR_DEVICE_CLASS_AUDIO_VIDEO_HEADPHONES , 0x06)); + mMinorEnumMap.insert(std::pair<bt_minor_device_class_e, unsigned long>(BT_MINOR_DEVICE_CLASS_AUDIO_VIDEO_PORTABLE_AUDIO , 0x07)); + mMinorEnumMap.insert(std::pair<bt_minor_device_class_e, unsigned long>(BT_MINOR_DEVICE_CLASS_AUDIO_VIDEO_CAR_AUDIO , 0x08)); + mMinorEnumMap.insert(std::pair<bt_minor_device_class_e, unsigned long>(BT_MINOR_DEVICE_CLASS_AUDIO_VIDEO_SET_TOP_BOX , 0x09)); + mMinorEnumMap.insert(std::pair<bt_minor_device_class_e, unsigned long>(BT_MINOR_DEVICE_CLASS_AUDIO_VIDEO_HIFI_AUDIO_DEVICE , 0x0a)); + mMinorEnumMap.insert(std::pair<bt_minor_device_class_e, unsigned long>(BT_MINOR_DEVICE_CLASS_AUDIO_VIDEO_VCR , 0x0b)); + mMinorEnumMap.insert(std::pair<bt_minor_device_class_e, unsigned long>(BT_MINOR_DEVICE_CLASS_AUDIO_VIDEO_VIDEO_CAMERA , 0x0c)); + mMinorEnumMap.insert(std::pair<bt_minor_device_class_e, unsigned long>(BT_MINOR_DEVICE_CLASS_AUDIO_VIDEO_CAMCORDER , 0x0d)); + mMinorEnumMap.insert(std::pair<bt_minor_device_class_e, unsigned long>(BT_MINOR_DEVICE_CLASS_AUDIO_VIDEO_VIDEO_MONITOR , 0x0e)); + mMinorEnumMap.insert(std::pair<bt_minor_device_class_e, unsigned long>(BT_MINOR_DEVICE_CLASS_AUDIO_VIDEO_VIDEO_DISPLAY_LOUDSPEAKER , 0x0f)); + mMinorEnumMap.insert(std::pair<bt_minor_device_class_e, unsigned long>(BT_MINOR_DEVICE_CLASS_AUDIO_VIDEO_VIDEO_CONFERENCING , 0x10)); + mMinorEnumMap.insert(std::pair<bt_minor_device_class_e, unsigned long>(BT_MINOR_DEVICE_CLASS_AUDIO_VIDEO_GAMING_TOY , 0x12)); + mMinorEnumMap.insert(std::pair<bt_minor_device_class_e, unsigned long>(BT_MINOR_DEVICE_CLASS_PERIPHERA_UNCATEGORIZED , 0)); + mMinorEnumMap.insert(std::pair<bt_minor_device_class_e, unsigned long>(BT_MINOR_DEVICE_CLASS_PERIPHERAL_KEY_BOARD , 0x10)); + mMinorEnumMap.insert(std::pair<bt_minor_device_class_e, unsigned long>(BT_MINOR_DEVICE_CLASS_PERIPHERAL_POINTING_DEVICE , 0x20)); + mMinorEnumMap.insert(std::pair<bt_minor_device_class_e, unsigned long>(BT_MINOR_DEVICE_CLASS_PERIPHERAL_COMBO_KEYBOARD_POINTING_DEVICE , 0x30)); + mMinorEnumMap.insert(std::pair<bt_minor_device_class_e, unsigned long>(BT_MINOR_DEVICE_CLASS_PERIPHERAL_JOYSTICK , 0x01)); + mMinorEnumMap.insert(std::pair<bt_minor_device_class_e, unsigned long>(BT_MINOR_DEVICE_CLASS_PERIPHERAL_GAME_PAD , 0x02)); + mMinorEnumMap.insert(std::pair<bt_minor_device_class_e, unsigned long>(BT_MINOR_DEVICE_CLASS_PERIPHERAL_REMOTE_CONTROL , 0x03)); + mMinorEnumMap.insert(std::pair<bt_minor_device_class_e, unsigned long>(BT_MINOR_DEVICE_CLASS_PERIPHERAL_SENSING_DEVICE , 0x04)); + mMinorEnumMap.insert(std::pair<bt_minor_device_class_e, unsigned long>(BT_MINOR_DEVICE_CLASS_PERIPHERAL_DIGITIZER_TABLET , 0x05)); + mMinorEnumMap.insert(std::pair<bt_minor_device_class_e, unsigned long>(BT_MINOR_DEVICE_CLASS_PERIPHERAL_CARD_READER , 0x06)); + mMinorEnumMap.insert(std::pair<bt_minor_device_class_e, unsigned long>(BT_MINOR_DEVICE_CLASS_PERIPHERAL_DIGITAL_PEN , 0x07)); + mMinorEnumMap.insert(std::pair<bt_minor_device_class_e, unsigned long>(BT_MINOR_DEVICE_CLASS_PERIPHERAL_HANDHELD_SCANNER , 0x08)); + mMinorEnumMap.insert(std::pair<bt_minor_device_class_e, unsigned long>(BT_MINOR_DEVICE_CLASS_PERIPHERAL_HANDHELD_GESTURAL_INPUT_DEVICE , 0x09)); + mMinorEnumMap.insert(std::pair<bt_minor_device_class_e, unsigned long>(BT_MINOR_DEVICE_CLASS_IMAGING_DISPLAY , 0x04)); + mMinorEnumMap.insert(std::pair<bt_minor_device_class_e, unsigned long>(BT_MINOR_DEVICE_CLASS_IMAGING_CAMERA , 0x08)); + mMinorEnumMap.insert(std::pair<bt_minor_device_class_e, unsigned long>(BT_MINOR_DEVICE_CLASS_IMAGING_SCANNER , 0x10)); + mMinorEnumMap.insert(std::pair<bt_minor_device_class_e, unsigned long>(BT_MINOR_DEVICE_CLASS_IMAGING_PRINTER, 0x20)); + mMinorEnumMap.insert(std::pair<bt_minor_device_class_e, unsigned long>(BT_MINOR_DEVICE_CLASS_WEARABLE_WRIST_WATCH , 0x01)); + mMinorEnumMap.insert(std::pair<bt_minor_device_class_e, unsigned long>(BT_MINOR_DEVICE_CLASS_WEARABLE_PAGER , 0x02)); + mMinorEnumMap.insert(std::pair<bt_minor_device_class_e, unsigned long>(BT_MINOR_DEVICE_CLASS_WEARABLE_JACKET , 0x03)); + mMinorEnumMap.insert(std::pair<bt_minor_device_class_e, unsigned long>(BT_MINOR_DEVICE_CLASS_WEARABLE_HELMET , 0x04)); + mMinorEnumMap.insert(std::pair<bt_minor_device_class_e, unsigned long>(BT_MINOR_DEVICE_CLASS_WEARABLE_GLASSES , 0x05)); + mMinorEnumMap.insert(std::pair<bt_minor_device_class_e, unsigned long>(BT_MINOR_DEVICE_CLASS_TOY_ROBOT , 0x01)); + mMinorEnumMap.insert(std::pair<bt_minor_device_class_e, unsigned long>(BT_MINOR_DEVICE_CLASS_TOY_VEHICLE , 0x02)); + mMinorEnumMap.insert(std::pair<bt_minor_device_class_e, unsigned long>(BT_MINOR_DEVICE_CLASS_TOY_DOLL_ACTION , 0x03)); + mMinorEnumMap.insert(std::pair<bt_minor_device_class_e, unsigned long>(BT_MINOR_DEVICE_CLASS_TOY_CONTROLLER , 0x04)); + mMinorEnumMap.insert(std::pair<bt_minor_device_class_e, unsigned long>(BT_MINOR_DEVICE_CLASS_TOY_GAME , 0x05)); + mMinorEnumMap.insert(std::pair<bt_minor_device_class_e, unsigned long>(BT_MINOR_DEVICE_CLASS_HEATH_UNCATEGORIZED , 0x00)); + mMinorEnumMap.insert(std::pair<bt_minor_device_class_e, unsigned long>(BT_MINOR_DEVICE_CLASS_HEATH_BLOOD_PRESSURE_MONITOR , 0x01)); + mMinorEnumMap.insert(std::pair<bt_minor_device_class_e, unsigned long>(BT_MINOR_DEVICE_CLASS_HEATH_THERMOMETER , 0x02)); + mMinorEnumMap.insert(std::pair<bt_minor_device_class_e, unsigned long>(BT_MINOR_DEVICE_CLASS_HEATH_WEIGHING_SCALE , 0x03)); + mMinorEnumMap.insert(std::pair<bt_minor_device_class_e, unsigned long>(BT_MINOR_DEVICE_CLASS_HEATH_GLUCOSE_METER , 0x04)); + mMinorEnumMap.insert(std::pair<bt_minor_device_class_e, unsigned long>(BT_MINOR_DEVICE_CLASS_HEATH_PULSE_OXIMETER , 0x05)); + mMinorEnumMap.insert(std::pair<bt_minor_device_class_e, unsigned long>(BT_MINOR_DEVICE_CLASS_HEATH_HEART_PULSE_RATE_MONITOR , 0x06)); + mMinorEnumMap.insert(std::pair<bt_minor_device_class_e, unsigned long>(BT_MINOR_DEVICE_CLASS_HEATH_DATA_DISPLAY , 0x07)); + mMinorEnumMap.insert(std::pair<bt_minor_device_class_e, unsigned long>(BT_MINOR_DEVICE_CLASS_HEATH_STEP_COUNTER , 0x08)); + mMinorEnumMap.insert(std::pair<bt_minor_device_class_e, unsigned long>(BT_MINOR_DEVICE_CLASS_HEATH_BODY_COMPOSITION_ANALYZER , 0x09)); + mMinorEnumMap.insert(std::pair<bt_minor_device_class_e, unsigned long>(BT_MINOR_DEVICE_CLASS_HEATH_PEAK_FLOW_MONITOR , 0x0a)); + mMinorEnumMap.insert(std::pair<bt_minor_device_class_e, unsigned long>(BT_MINOR_DEVICE_CLASS_HEATH_MEDICATION_MONITOR , 0x0b)); + mMinorEnumMap.insert(std::pair<bt_minor_device_class_e, unsigned long>(BT_MINOR_DEVICE_CLASS_HEATH_KNEE_PROSTHESIS , 0x0c)); + mMinorEnumMap.insert(std::pair<bt_minor_device_class_e, unsigned long>(BT_MINOR_DEVICE_CLASS_HEATH_ANKLE_PROSTHESIS , 0x0d)); +} + +BluetoothClassDeviceMinor::~BluetoothClassDeviceMinor() +{ +} + +BluetoothClassDeviceMinor* BluetoothClassDeviceMinor::getInstance() +{ + static BluetoothClassDeviceMinor instance; + return &instance; +} + +unsigned long BluetoothClassDeviceMinor::getMinorValue(std::string minor) +{ + return mMinorStringMap.find(minor)->second; +} + +unsigned long BluetoothClassDeviceMinor::getMinorValue(bt_minor_device_class_e minor) +{ + // F/W provides more minor deivce class + std::map<bt_minor_device_class_e, unsigned long>::iterator iter = mMinorEnumMap.find(minor); + if(iter != mMinorEnumMap.end()) { + return iter->second; + } + + return 0; +} + + +} // Bluetooth +} // DeviceAPI diff --git a/mobile_src/Bluetooth/BluetoothClassDeviceMinor.h b/mobile_src/Bluetooth/BluetoothClassDeviceMinor.h new file mode 100644 index 0000000..7107568 --- /dev/null +++ b/mobile_src/Bluetooth/BluetoothClassDeviceMinor.h @@ -0,0 +1,46 @@ +// +// Tizen Web Device API +// Copyright (c) 2012 Samsung Electronics Co., Ltd. +// +// Licensed under the Apache License, Version 2.0 (the License); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. +// + +#ifndef __TIZEN_BLUETOOTH_CLASS_DEVICE_MINOR_H__ +#define __TIZEN_BLUETOOTH_CLASS_DEVICE_MINOR_H__ + +#include <string> +#include <map> +#include <bluetooth.h> + +namespace DeviceAPI { +namespace Bluetooth { + +class BluetoothClassDeviceMinor +{ +public: + BluetoothClassDeviceMinor(); + virtual ~BluetoothClassDeviceMinor(); + + static BluetoothClassDeviceMinor* getInstance(); + unsigned long getMinorValue(std::string minor); + unsigned long getMinorValue(bt_minor_device_class_e minor); + +private: + std::map<std::string, unsigned long> mMinorStringMap; + std::map<bt_minor_device_class_e, unsigned long> mMinorEnumMap; +}; + +} // Bluetooth +} // DeviceAPI + +#endif // __TIZEN_BLUETOOTH_CLASS_DEVICE_MINOR_H__ diff --git a/mobile_src/Bluetooth/BluetoothClassDeviceService.cpp b/mobile_src/Bluetooth/BluetoothClassDeviceService.cpp new file mode 100644 index 0000000..aae9a2d --- /dev/null +++ b/mobile_src/Bluetooth/BluetoothClassDeviceService.cpp @@ -0,0 +1,84 @@ +// +// Tizen Web Device API +// Copyright (c) 2012 Samsung Electronics Co., Ltd. +// +// Licensed under the Apache License, Version 2.0 (the License); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. +// + +#include <PlatformException.h> + +#include "BluetoothClassDeviceService.h" + +#include <Logger.h> + +namespace DeviceAPI { +namespace Bluetooth { + +BluetoothClassDeviceService::BluetoothClassDeviceService() +{ + mServiceStringMap.insert(std::pair<std::string, unsigned long>("LIMITED_DISCOVERABILITY", 0x0001)); + mServiceStringMap.insert(std::pair<std::string, unsigned long>("POSITIONING", 0x0008)); + mServiceStringMap.insert(std::pair<std::string, unsigned long>("NETWORKING", 0x0010)); + mServiceStringMap.insert(std::pair<std::string, unsigned long>("RENDERING", 0x0020)); + mServiceStringMap.insert(std::pair<std::string, unsigned long>("CAPTURING", 0x0040)); + mServiceStringMap.insert(std::pair<std::string, unsigned long>("OBJECT_TRANSFER", 0x0080)); + mServiceStringMap.insert(std::pair<std::string, unsigned long>("AUDIO", 0x0100)); + mServiceStringMap.insert(std::pair<std::string, unsigned long>("TELEPHONY", 0x0200)); + mServiceStringMap.insert(std::pair<std::string, unsigned long>("INFORMATION", 0x0400)); + + mServiceEnumMap.insert(std::pair<bt_major_service_class_e, unsigned long>(BT_MAJOR_SERVICE_CLASS_LIMITED_DISCOVERABLE_MODE, 0x0001)); + mServiceEnumMap.insert(std::pair<bt_major_service_class_e, unsigned long>(BT_MAJOR_SERVICE_CLASS_POSITIONING, 0x0008)); + mServiceEnumMap.insert(std::pair<bt_major_service_class_e, unsigned long>(BT_MAJOR_SERVICE_CLASS_NETWORKING, 0x0010)); + mServiceEnumMap.insert(std::pair<bt_major_service_class_e, unsigned long>(BT_MAJOR_SERVICE_CLASS_RENDERING, 0x0020)); + mServiceEnumMap.insert(std::pair<bt_major_service_class_e, unsigned long>(BT_MAJOR_SERVICE_CLASS_CAPTURING, 0x0040)); + mServiceEnumMap.insert(std::pair<bt_major_service_class_e, unsigned long>(BT_MAJOR_SERVICE_CLASS_OBJECT_TRANSFER, 0x0080)); + mServiceEnumMap.insert(std::pair<bt_major_service_class_e, unsigned long>(BT_MAJOR_SERVICE_CLASS_AUDIO, 0x0100)); + mServiceEnumMap.insert(std::pair<bt_major_service_class_e, unsigned long>(BT_MAJOR_SERVICE_CLASS_TELEPHONY, 0x0200)); + mServiceEnumMap.insert(std::pair<bt_major_service_class_e, unsigned long>(BT_MAJOR_SERVICE_CLASS_INFORMATION, 0x0400)); +} + +BluetoothClassDeviceService::~BluetoothClassDeviceService() +{ +} + +BluetoothClassDeviceService* BluetoothClassDeviceService::getInstance() +{ + static BluetoothClassDeviceService instance; + return &instance; +} + +unsigned long BluetoothClassDeviceService::getServiceValue(std::string service) +{ + return mServiceStringMap.find(service)->second; +} + +unsigned long BluetoothClassDeviceService::getServiceValue(bt_major_service_class_e service) +{ + return mServiceEnumMap.find(service)->second; +} + +std::vector<unsigned long> BluetoothClassDeviceService::getServiceValues(int serviceMask) +{ + std::vector<unsigned long> ret; + for(std::map<bt_major_service_class_e, unsigned long>::iterator iter = mServiceEnumMap.begin(); iter != mServiceEnumMap.end(); iter++) { + if(iter->first & serviceMask) { + ret.push_back(iter->second); + } + } + + return ret; +} + + +} // Bluetooth +} // DeviceAPI diff --git a/mobile_src/Bluetooth/BluetoothClassDeviceService.h b/mobile_src/Bluetooth/BluetoothClassDeviceService.h new file mode 100644 index 0000000..713eecb --- /dev/null +++ b/mobile_src/Bluetooth/BluetoothClassDeviceService.h @@ -0,0 +1,48 @@ +// +// Tizen Web Device API +// Copyright (c) 2012 Samsung Electronics Co., Ltd. +// +// Licensed under the Apache License, Version 2.0 (the License); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. +// + +#ifndef __TIZEN_BLUETOOTH_CLASS_DEVICE_SERVICE_H__ +#define __TIZEN_BLUETOOTH_CLASS_DEVICE_SERVICE_H__ + +#include <string> +#include <map> +#include <vector> +#include <bluetooth.h> + +namespace DeviceAPI { +namespace Bluetooth { + +class BluetoothClassDeviceService +{ +public: + BluetoothClassDeviceService(); + virtual ~BluetoothClassDeviceService(); + + static BluetoothClassDeviceService* getInstance(); + unsigned long getServiceValue(std::string service); + unsigned long getServiceValue(bt_major_service_class_e service); + std::vector<unsigned long> getServiceValues(int serviceMask); + +private: + std::map<std::string, unsigned long> mServiceStringMap; + std::map<bt_major_service_class_e, unsigned long> mServiceEnumMap; +}; + +} // Bluetooth +} // DeviceAPI + +#endif // __TIZEN_BLUETOOTH_CLASS_DEVICE_SERVICE_H__ diff --git a/mobile_src/Bluetooth/BluetoothDevice.cpp b/mobile_src/Bluetooth/BluetoothDevice.cpp new file mode 100644 index 0000000..c87d099 --- /dev/null +++ b/mobile_src/Bluetooth/BluetoothDevice.cpp @@ -0,0 +1,154 @@ +// +// Tizen Web Device API +// Copyright (c) 2012 Samsung Electronics Co., Ltd. +// +// Licensed under the Apache License, Version 2.0 (the License); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. +// + +#include <PlatformException.h> +#include <JSUtil.h> + +#include "plugin_config.h" +#include "BluetoothDevice.h" +#include "BluetoothAdapter.h" +#include "JSBluetoothClass.h" + +#include <Logger.h> + +using namespace DeviceAPI::Common; + +namespace DeviceAPI { +namespace Bluetooth { + +BluetoothDevice::BluetoothDevice(bt_adapter_device_discovery_info_s *discoveryInfo) +{ + Common::SecurityAccessor(); + mName = std::string(discoveryInfo->remote_name); + mAddress = std::string(discoveryInfo->remote_address); + mDeviceClass = BluetoothClassSharedPtr(new BluetoothClass(discoveryInfo->bt_class)); + for(int i = 0; i < discoveryInfo->service_count; i++) { + mUUIDs.push_back(std::string(discoveryInfo->service_uuid[i])); + } + isUpdated = true; +} + +BluetoothDevice::BluetoothDevice(bt_device_info_s *deviceInfo) +{ + Common::SecurityAccessor(); + mName = std::string(deviceInfo->remote_name); + mAddress = std::string(deviceInfo->remote_address); + mDeviceClass = BluetoothClassSharedPtr(new BluetoothClass(deviceInfo->bt_class)); + + for(int i = 0; i < deviceInfo->service_count; i++) { + mUUIDs.push_back(std::string(deviceInfo->service_uuid[i])); + } + isUpdated = true; +} + +BluetoothDevice::~BluetoothDevice() +{ + BluetoothAdapter::getInstance()->removeConnReq(mAddress); +} + +void BluetoothDevice::updateInfo(bt_device_info_s *deviceInfo) +{ + mName = std::string(deviceInfo->remote_name); + mUUIDs.clear(); + for(int i = 0; i < deviceInfo->service_count; i++) { + mUUIDs.push_back(std::string(deviceInfo->service_uuid[i])); + } + isUpdated = true; +} + +std::string BluetoothDevice::getName() const +{ + return mName; +} + +std::string BluetoothDevice::getAddress() const +{ + return mAddress; +} + +void BluetoothDevice::copyAceCheckAccessFunction(const Common::SecurityAccessor *securityAccessor) +{ + Common::SecurityAccessor::copyAceCheckAccessFunction(securityAccessor); + mDeviceClass->copyAceCheckAccessFunction(securityAccessor); +} + +JSValueRef BluetoothDevice::getDeviceClass(JSContextRef context) +{ + /* + JSValueRef deviceClass = mLocalProperty.getProperty(context, BLUETOOTH_DEVICE_DEVICE_CLASS); + if(deviceClass == NULL) { + deviceClass = JSBluetoothClass::createJSObject(context, mDeviceClass); + mLocalProperty.setProperty(context, BLUETOOTH_DEVICE_DEVICE_CLASS, deviceClass); + } + + return deviceClass; + */ + return JSBluetoothClass::createJSObject(context, mDeviceClass); +} + +bool BluetoothDevice::isBonded() const +{ + bool ret = false; + bt_device_info_s *deviceInfo = NULL; + if(bt_adapter_get_bonded_device_info(mAddress.c_str(), &deviceInfo) == BT_ERROR_NONE && deviceInfo != NULL) { + ret = deviceInfo->is_bonded; + bt_adapter_free_device_info(deviceInfo); + } + + return ret; +} + +bool BluetoothDevice::isTrusted() const +{ + bool ret = false; + bt_device_info_s *deviceInfo = NULL; + if(bt_adapter_get_bonded_device_info(mAddress.c_str(), &deviceInfo) == BT_ERROR_NONE && deviceInfo != NULL) { + ret = deviceInfo->is_authorized; + bt_adapter_free_device_info(deviceInfo); + } + + return ret; +} + +bool BluetoothDevice::isConnected() const +{ + bool ret = false; + bt_device_info_s *deviceInfo = NULL; + if(bt_adapter_get_bonded_device_info(mAddress.c_str(), &deviceInfo) == BT_ERROR_NONE && deviceInfo != NULL) { + ret = deviceInfo->is_connected; + bt_adapter_free_device_info(deviceInfo); + } + + return ret; +} + +JSValueRef BluetoothDevice::getUUIDs(JSContextRef context) +{ +/* + if(isUpdated == true) { + mLocalProperty.setProperty(context, BLUETOOTH_DEVICE_UUIDS, JSUtil::toJSValueRef(context, mUUIDs)); + isUpdated = false; + } + + return mLocalProperty.getProperty(context, BLUETOOTH_DEVICE_UUIDS); +*/ + return JSUtil::toJSValueRef(context, mUUIDs); +} + + +} // Bluetooth +} // DeviceAPI diff --git a/mobile_src/Bluetooth/BluetoothDevice.h b/mobile_src/Bluetooth/BluetoothDevice.h new file mode 100644 index 0000000..e434bbc --- /dev/null +++ b/mobile_src/Bluetooth/BluetoothDevice.h @@ -0,0 +1,64 @@ +// +// Tizen Web Device API +// Copyright (c) 2012 Samsung Electronics Co., Ltd. +// +// Licensed under the Apache License, Version 2.0 (the License); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. +// + +#ifndef __TIZEN_BLUETOOTH_DEVICE_H__ +#define __TIZEN_BLUETOOTH_DEVICE_H__ + +#include <string> +#include <vector> + +#include <JavaScriptCore/JavaScript.h> +#include <bluetooth.h> +#include <boost/shared_ptr.hpp> +#include <PropertyBag.h> +#include <Security.h> +#include "BluetoothClass.h" + +namespace DeviceAPI { +namespace Bluetooth { + +class BluetoothDevice : public Common::SecurityAccessor +{ +public: + BluetoothDevice(bt_adapter_device_discovery_info_s *discoveryInfo); + BluetoothDevice(bt_device_info_s *deviceInfo); + virtual ~BluetoothDevice(); + + void updateInfo(bt_device_info_s *deviceInfo); + std::string getName() const; + std::string getAddress() const; + JSValueRef getDeviceClass(JSContextRef context); + bool isBonded() const; + bool isTrusted() const; + bool isConnected() const; + JSValueRef getUUIDs(JSContextRef context); + void copyAceCheckAccessFunction(const Common::SecurityAccessor *securityAccessor); +private: + std::string mName; + std::string mAddress; + BluetoothClassSharedPtr mDeviceClass; + std::vector<std::string> mUUIDs; + //Common::PropertyBag mLocalProperty; + bool isUpdated; +}; + +typedef boost::shared_ptr<BluetoothDevice> BluetoothDeviceSharedPtr; + +} // Bluetooth +} // DeviceAPI + +#endif // __TIZEN_BLUETOOTH_DEVICE_H__ diff --git a/mobile_src/Bluetooth/BluetoothHealthApplication.cpp b/mobile_src/Bluetooth/BluetoothHealthApplication.cpp new file mode 100644 index 0000000..8fb9d5d --- /dev/null +++ b/mobile_src/Bluetooth/BluetoothHealthApplication.cpp @@ -0,0 +1,106 @@ +// +// Tizen Web Device API +// Copyright (c) 2012 Samsung Electronics Co., Ltd. +// +// Licensed under the Apache License, Version 2.0 (the License); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. +// + +#include "BluetoothHealthApplication.h" +#include "plugin_config.h" + +#include <bluetooth.h> +#include <Logger.h> +#include <GlobalContextManager.h> + +using namespace DeviceAPI::Common; + +namespace DeviceAPI { +namespace Bluetooth { + +BluetoothHealthApplication::BluetoothHealthApplication(std::string appID, std::string name, unsigned short dataType) +{ + Common::SecurityAccessor(); + mAppID = appID; + mName = name; + mDataType = dataType; + mIsRegistered = true; +} + +BluetoothHealthApplication::~BluetoothHealthApplication() +{ + if(mIsRegistered) + bt_hdp_unregister_sink_app(mAppID.c_str()); +} + +std::string BluetoothHealthApplication::getAppID() const +{ + return mAppID; +} + +unsigned short BluetoothHealthApplication::getDataType() const +{ + return mDataType; +} + +std::string BluetoothHealthApplication::getName() const +{ + return mName; +} + +bool BluetoothHealthApplication::setOnConnect(JSContextRef context, JSObjectRef onConnect) +{ + LoggerD("Enter"); + + MultiCallbackUserDataPtr callback( + new MultiCallbackUserData(GlobalContextManager::getInstance()->getGlobalContext(context))); + if(!callback){ + LoggerW("Can't create MultiCallbackUserData"); + return false; + } + callback->setCallback("onconnect", onConnect); + mOnConnect = callback; + + return mLocalProperty.setProperty(context, BLUETOOTH_HEALTH_APPLICATION_ONCONNECT, onConnect); +} + +MultiCallbackUserDataPtr BluetoothHealthApplication::getOnConnect() const +{ + return mOnConnect; +} + +JSValueRef BluetoothHealthApplication::getOnConnect(JSContextRef context) +{ + LoggerD("Enter"); + + JSValueRef onConnect = mLocalProperty.getProperty(context, BLUETOOTH_HEALTH_APPLICATION_ONCONNECT); + if(onConnect == NULL) { + LoggerD("onconnect is null"); + return JSValueMakeNull(context); + } + + return onConnect; +} + +bool BluetoothHealthApplication::getRegistrationState() const +{ + return mIsRegistered; +} + +void BluetoothHealthApplication::setRegistrationState(bool isRegistered) +{ + mIsRegistered = isRegistered; +} + + +} // Bluetooth +} // DeviceAPI diff --git a/mobile_src/Bluetooth/BluetoothHealthApplication.h b/mobile_src/Bluetooth/BluetoothHealthApplication.h new file mode 100644 index 0000000..f823bea --- /dev/null +++ b/mobile_src/Bluetooth/BluetoothHealthApplication.h @@ -0,0 +1,63 @@ +// +// Tizen Web Device API +// Copyright (c) 2012 Samsung Electronics Co., Ltd. +// +// Licensed under the Apache License, Version 2.0 (the License); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. +// + +#ifndef __TIZEN_BLUETOOTH_HEALTH_APPLICATION_H__ +#define __TIZEN_BLUETOOTH_HEALTH_APPLICATION_H__ + +#include <string> + +#include <JavaScriptCore/JavaScript.h> +#include <boost/shared_ptr.hpp> + +#include <MultiCallbackUserData.h> +#include <PropertyBag.h> +#include <Security.h> + +namespace DeviceAPI { +namespace Bluetooth { + +class BluetoothHealthApplication : public Common::SecurityAccessor +{ +public: + BluetoothHealthApplication(std::string appID, std::string name, unsigned short dataType); + virtual ~BluetoothHealthApplication(); + + std::string getAppID() const; + unsigned short getDataType() const; + std::string getName() const; + bool getRegistrationState() const; + void setRegistrationState(bool isRegistered); + Common::MultiCallbackUserDataPtr getOnConnect() const; + JSValueRef getOnConnect(JSContextRef context); + bool setOnConnect(JSContextRef context, JSObjectRef onConnect); + + +private: + std::string mAppID; + std::string mName; + bool mIsRegistered; + Common::MultiCallbackUserDataPtr mOnConnect; + unsigned short mDataType; + Common::PropertyBag mLocalProperty; +}; + +typedef boost::shared_ptr<BluetoothHealthApplication> BluetoothHealthApplicationSharedPtr; + +} // Bluetooth +} // DeviceAPI + +#endif // __TIZEN_BLUETOOTH_HEALTH_APPLICATION_H__ diff --git a/mobile_src/Bluetooth/BluetoothHealthChannel.cpp b/mobile_src/Bluetooth/BluetoothHealthChannel.cpp new file mode 100644 index 0000000..f038d33 --- /dev/null +++ b/mobile_src/Bluetooth/BluetoothHealthChannel.cpp @@ -0,0 +1,145 @@ +// Tizen Web Device API +// Copyright (c) 2012 Samsung Electronics Co., Ltd. +// +// Licensed under the Apache License, Version 2.0 (the License); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. +// + +#include <GlobalContextManager.h> +#include <PlatformException.h> + +#include "BluetoothHealthChannel.h" +#include "JSBluetoothDevice.h" +#include "JSBluetoothHealthApplication.h" + +#include <Logger.h> +#include <TimeTracer.h> + +using namespace DeviceAPI::Common; + +namespace DeviceAPI { +namespace Bluetooth { + +BluetoothHealthChannel::BluetoothHealthChannel(unsigned int channel, BluetoothDeviceSharedPtr remoteDevice, + bt_hdp_channel_type_e type, BluetoothHealthApplicationSharedPtr application) +{ + Common::SecurityAccessor(); + mChannel = channel; + mRemoteDevice = remoteDevice; + if(type == BT_HDP_CHANNEL_TYPE_RELIABLE) { + mChannelTypeStr = "RELIABLE"; + } + else { + mChannelTypeStr = "STREAMING"; + } + mChannelType = type; + mApp = application; + mIsConnected = true; +} + +BluetoothHealthChannel::~BluetoothHealthChannel() +{ + if(mIsConnected) { + bt_hdp_disconnect(mRemoteDevice->getAddress().c_str(), mChannel); + } +} + +bool BluetoothHealthChannel::getConnectionState() const +{ + return mIsConnected; +} + +void BluetoothHealthChannel::setConnectionState(bool isConnected) +{ + mIsConnected = isConnected; +} + +unsigned int BluetoothHealthChannel::getChannel() const +{ + return mChannel; +} + +std::string BluetoothHealthChannel::getChannelTypeStr() const +{ + return mChannelTypeStr; +} + +bt_hdp_channel_type_e BluetoothHealthChannel::getChannelType() const +{ + return mChannelType; +} + +JSValueRef BluetoothHealthChannel::getApp(JSContextRef context) +{ + return JSBluetoothHealthApplication::createJSObject(context, mApp); +} + +JSValueRef BluetoothHealthChannel::getPeer(JSContextRef context) +{ + return JSBluetoothDevice::createJSObject(context, mRemoteDevice); +} + +Common::MultiCallbackUserDataPtr BluetoothHealthChannel::getListener() const +{ + return mListener; +} + +unsigned long BluetoothHealthChannel::sendData(char* data, unsigned long size) +{ + unsigned long ret = 0; + TIME_TRACER_ITEM_BEGIN("sendData::bt_hdp_send_data", 1); + if(bt_hdp_send_data(mChannel, data, static_cast<unsigned int>(size)) == BT_ERROR_NONE) { + TIME_TRACER_ITEM_END("sendData::bt_hdp_send_data", 1); + LoggerD("bt_hdp_send_data() succeeded"); + ret = size; + } + else { + throw UnknownException("Unknown error"); + } + + //delete data; + return ret; +} + +void BluetoothHealthChannel::close() +{ + LoggerD("Enter"); + if(!mIsConnected) { + LoggerD("Already disconnected"); + return; + } + + TIME_TRACER_ITEM_BEGIN("close::bt_hdp_disconnect", 1); + if(bt_hdp_disconnect(mRemoteDevice->getAddress().c_str(), mChannel) != BT_ERROR_NONE) { + LoggerE("bt_hdp_disconnect() failed"); + throw UnknownException("Unknown error"); + } + TIME_TRACER_ITEM_END("close::bt_hdp_disconnect", 1); + + mIsConnected = false; + + LoggerD("End"); +} + +void BluetoothHealthChannel::setListener(Common::MultiCallbackUserDataPtr callback) +{ + mListener = callback; +} + +void BluetoothHealthChannel::unsetListener() +{ + mListener.reset(); +} + + +} // Bluetooth +} // DeviceAPI diff --git a/mobile_src/Bluetooth/BluetoothHealthChannel.h b/mobile_src/Bluetooth/BluetoothHealthChannel.h new file mode 100644 index 0000000..87ea7b4 --- /dev/null +++ b/mobile_src/Bluetooth/BluetoothHealthChannel.h @@ -0,0 +1,68 @@ +// +// Tizen Web Device API +// Copyright (c) 2012 Samsung Electronics Co., Ltd. +// +// Licensed under the Apache License, Version 2.0 (the License); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. +// + +#ifndef __TIZEN_BLUETOOTH_HEALTH_CHANNEL_H__ +#define __TIZEN_BLUETOOTH_HEALTH_CHANNEL_H__ + +#include <string> +#include <bluetooth.h> +#include <JavaScriptCore/JavaScript.h> +#include <Security.h> +#include <MultiCallbackUserData.h> +#include "BluetoothDevice.h" +#include "BluetoothHealthApplication.h" + +namespace DeviceAPI { +namespace Bluetooth { + +class BluetoothHealthChannel : public Common::SecurityAccessor +{ +public: + BluetoothHealthChannel(unsigned int channel, BluetoothDeviceSharedPtr remoteDevice, bt_hdp_channel_type_e type, BluetoothHealthApplicationSharedPtr application); + virtual ~BluetoothHealthChannel(); + + bool getConnectionState() const; + void setConnectionState(bool isConnected); + unsigned int getChannel() const; + std::string getChannelTypeStr() const; + bt_hdp_channel_type_e getChannelType() const; + JSValueRef getApp(JSContextRef context); + JSValueRef getPeer(JSContextRef context); + Common::MultiCallbackUserDataPtr getListener() const; + + unsigned long sendData(char* data, unsigned long size); + void close(); + void setListener(Common::MultiCallbackUserDataPtr callback); + void unsetListener(); + +private: + unsigned int mChannel; + bool mIsConnected; + std::string mChannelTypeStr; + bt_hdp_channel_type_e mChannelType; + BluetoothDeviceSharedPtr mRemoteDevice; + BluetoothHealthApplicationSharedPtr mApp; + + Common::MultiCallbackUserDataPtr mListener; +}; + +typedef BluetoothHealthChannel* BluetoothHealthChannelPtr; + +} // Bluetooth +} // DeviceAPI + +#endif // __TIZEN_BLUETOOTH_HEALTH_CHANNEL_H__ diff --git a/mobile_src/Bluetooth/BluetoothHealthProfileHandler.cpp b/mobile_src/Bluetooth/BluetoothHealthProfileHandler.cpp new file mode 100644 index 0000000..8299baa --- /dev/null +++ b/mobile_src/Bluetooth/BluetoothHealthProfileHandler.cpp @@ -0,0 +1,411 @@ +// +// Tizen Web Device API +// Copyright (c) 2012 Samsung Electronics Co., Ltd. +// +// Licensed under the Apache License, Version 2.0 (the License); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. +// + + +#include <Logger.h> +#include <JSWebAPIErrorFactory.h> +#include <JSUtil.h> +#include <TimeTracer.h> + +#include "BluetoothHealthProfileHandler.h" +#include "BluetoothHealthProfileHandlerCallback.h" +#include "JSBluetoothHealthApplication.h" +#include "JSBluetoothDevice.h" +#include "JSBluetoothHealthChannel.h" +#include "BluetoothHealthChannel.h" + +using namespace DeviceAPI::Common; + +namespace DeviceAPI { +namespace Bluetooth { + +void BluetoothHealthProfileHandler::onConnected(int result, const char *remote_address, const char *app_id, + bt_hdp_channel_type_e type, unsigned int channel, void *userData) +{ + LoggerD("Enter"); + TIME_TRACER_ITEM_BEGIN(__FUNCTION__, 1); + + HealthProfileHandlerPtr object = static_cast<HealthProfileHandlerPtr>(userData); + if(!object) { + LoggerW("userData is NULL"); + return; + } + + if(result != BT_ERROR_NONE) { + LoggerD("Not BT_ERROR_NONE"); + } + + LoggerD("Connected app: " << app_id); + LoggerD("Connected channel: " << channel); + + std::string appID(app_id); + RegisteredHealthAppMapT::iterator iter = object->mRegisteredHealthAppMap.find(appID); + if(iter == object->mRegisteredHealthAppMap.end()) { + LoggerW("This app is not registered"); + return; + } + BluetoothHealthApplicationSharedPtr application = iter->second; + + bool isChannelInserted = false; + BluetoothHealthChannelPtr healthChannel = NULL; + + // call BluetoothHealthApplication.onconnect + if(result == BT_ERROR_NONE) { + Common::MultiCallbackUserDataPtr callback = application->getOnConnect(); + if(callback) { + bt_device_info_s *deviceInfo = NULL; + if(bt_adapter_get_bonded_device_info(remote_address, &deviceInfo) == BT_ERROR_NONE && + deviceInfo != NULL) { + BluetoothDeviceSharedPtr device(new BluetoothDevice(deviceInfo)); + // need to check + device->copyAceCheckAccessFunction(getInstance()); + bt_adapter_free_device_info(deviceInfo); + + LoggerD("invoke BluetoothHealthApplication.onconnect"); + healthChannel = new BluetoothHealthChannel(channel, device, type, application); + healthChannel->copyAceCheckAccessFunction(getInstance()); + object->mConnectedSocketMap.insert(std::pair<unsigned int, BluetoothHealthChannelPtr>(channel, healthChannel)); + isChannelInserted = true; + callback->invokeCallback("onconnect", JSBluetoothHealthChannel::createJSObject(callback->getContext(), healthChannel)); + } + else { + LoggerE("Can't call BluetoothHealthApplication.onconnect because failed to get device info"); + } + } + else { + LoggerD("BluetoothHealthApplication.onconnect is not set"); + } + } + + // in case of connectToSource() + HealthConnReqMapT::iterator i = object->mHealthConnReqMap.find(application); + if(i != object->mHealthConnReqMap.end()) { + LoggerD("Requested connection"); + Common::MultiCallbackUserDataPtr callback = i->second->mUserData; + if(callback) { + if(result == BT_ERROR_NONE) { + if(isChannelInserted == false) { + healthChannel = new BluetoothHealthChannel(channel, i->second->mRemoteDevice, type, application); + healthChannel->copyAceCheckAccessFunction(getInstance()); + object->mConnectedSocketMap.insert(std::pair<unsigned int, BluetoothHealthChannelPtr>(channel, healthChannel)); + } + callback->invokeCallback("success", JSBluetoothHealthChannel::createJSObject(callback->getContext(), healthChannel)); + } + else { + LoggerE("Failed to establish a connection with health profile"); + callback->invokeCallback("error", + JSWebAPIErrorFactory::makeErrorObject(callback->getContext(), UnknownException("Failed to establish a connection with health profile"))); + } + } + + // Update mHealthConnReqMap + object->mHealthConnReqMap.erase(i); + } + else { + LoggerD("There is no connection request"); + } + + TIME_TRACER_ITEM_END(__FUNCTION__, 1); +} + +void BluetoothHealthProfileHandler::onDisconnected(int result, const char *remote_address, unsigned int channel, void *userData) +{ + LoggerD("Enter"); + TIME_TRACER_ITEM_BEGIN(__FUNCTION__, 1); + + HealthProfileHandlerPtr object = static_cast<HealthProfileHandlerPtr>(userData); + if(!object) { + LoggerW("userData is NULL"); + return; + } + + LoggerD("Disconnected channel: " << channel); + HealthConnectedSocketMapT::iterator iter = object->mConnectedSocketMap.find(channel); + if(iter == object->mConnectedSocketMap.end()) { + LoggerW("Unexpected health disconnection event"); + return; + } + + if(result == BT_ERROR_NONE) { + BluetoothHealthChannelPtr healthChannel = iter->second; + object->mConnectedSocketMap.erase(iter); + + healthChannel->setConnectionState(false); + MultiCallbackUserDataPtr callback = healthChannel->getListener(); + if(callback) + callback->invokeCallback("onclose"); + } + else { + LoggerW("Failed to disconnect a connection"); + } + + TIME_TRACER_ITEM_END(__FUNCTION__, 1); + LoggerD("End"); +} + +void BluetoothHealthProfileHandler::onDataReceivedCB(unsigned int channel, const char *data, unsigned int size, void *userData) +{ + LoggerD("Enter"); + + TIME_TRACER_ITEM_BEGIN(__FUNCTION__, 1); + + HealthProfileHandlerPtr object = static_cast<HealthProfileHandlerPtr>(userData); + if(!object) { + LoggerW("userData is NULL"); + return; + } + + LoggerD("data channel: " << channel); + LoggerD("sent data size: " << size); + HealthConnectedSocketMapT::iterator iter = object->mConnectedSocketMap.find(channel); + if(iter == object->mConnectedSocketMap.end()) { + LoggerW("Unexpected health data received event"); + return; + } + + BluetoothHealthChannelPtr healthChannel = iter->second; + MultiCallbackUserDataPtr callback = healthChannel->getListener(); + if(callback) { + std::vector<signed char> receivedData; + for(unsigned int i = 0; i < size; i++) { + receivedData.push_back(static_cast<signed char>(data[i])); + } + callback->invokeCallback("onmessage", JSUtil::toJSValueRef_(callback->getContext(), receivedData)); + } + + TIME_TRACER_ITEM_END(__FUNCTION__, 1); +} + +BluetoothHealthProfileHandler* BluetoothHealthProfileHandler::getInstance() +{ + static BluetoothHealthProfileHandler instance; + return &instance; +} + +BluetoothHealthProfileHandler::BluetoothHealthProfileHandler() +{ + Common::SecurityAccessor(); + + if(bt_hdp_set_connection_state_changed_cb(onConnected, onDisconnected, this) != BT_ERROR_NONE) { + LoggerE("bt_hdp_set_connection_state_changed_cb() failed"); + } + + if(bt_hdp_set_data_received_cb(onDataReceivedCB, this) != BT_ERROR_NONE) { + LoggerE("bt_hdp_set_data_received_cb() failed"); + } +} + + +BluetoothHealthProfileHandler::~BluetoothHealthProfileHandler() +{ + // unset platform callback + bt_hdp_unset_connection_state_changed_cb(); + bt_hdp_unset_data_received_cb(); + + mHealthConnReqMap.clear(); + mConnectedSocketMap.clear(); + mRegisteredHealthAppMap.clear(); +} + +void BluetoothHealthProfileHandler::registerSinkApp(unsigned short dataType, std::string name, Common::MultiCallbackUserDataPtr callback) +{ + TIME_TRACER_ITEM_BEGIN(__FUNCTION__, 1); + BluetoothHealthProfileHandlerCallback::syncToAsyncRegisterCB(callback, dataType, name); + TIME_TRACER_ITEM_END(__FUNCTION__, 1); +} + +void BluetoothHealthProfileHandler::returnRegisteringSinkAppResult(unsigned short dataType, std::string name, Common::MultiCallbackUserDataPtr callback) +{ + LoggerD("Enter"); + TIME_TRACER_ITEM_BEGIN(__FUNCTION__, 1); + + char *app_id = NULL; + int ret = bt_hdp_register_sink_app(dataType, &app_id); + switch(ret) { + case BT_ERROR_NONE: + { + LoggerD("Registered app: " << app_id); + std::string appID(app_id); + //free(app_id); + BluetoothHealthApplicationSharedPtr application(new BluetoothHealthApplication(appID, name, dataType)); + application->copyAceCheckAccessFunction(getInstance()); + + mRegisteredHealthAppMap.insert(std::pair<std::string, BluetoothHealthApplicationSharedPtr>(appID, application)); + if(callback) + callback->invokeCallback("success", JSBluetoothHealthApplication::createJSObject(callback->getContext(), application)); + break; + } + case BT_ERROR_NOT_ENABLED: + { + if(callback) { + callback->invokeCallback("error", + JSWebAPIErrorFactory::makeErrorObject(callback->getContext(), ServiceNotAvailableException("Bluetooth device is turned off"))); + } + break; + } + default: + { + if(callback) { + callback->invokeCallback("error", + JSWebAPIErrorFactory::makeErrorObject(callback->getContext(), UnknownException("Unknown error"))); + } + } + } + + TIME_TRACER_ITEM_END(__FUNCTION__, 1); +} + +/* +void BluetoothHealthProfileHandler::unregisterSinkApplication(JSObjectRef appObj, Common::MultiCallbackUserDataPtr callback) +{ + TIME_TRACER_ITEM_BEGIN(__FUNCTION__, 1); + BluetoothHealthProfileHandlerCallback::syncToAsyncUnregisterCB(callback, appObj); + TIME_TRACER_ITEM_END(__FUNCTION__, 1); +} + +void BluetoothHealthProfileHandler::returnUnregisteringResult(JSObjectRef appObj, Common::MultiCallbackUserDataPtr callback) +{ + TIME_TRACER_ITEM_BEGIN(__FUNCTION__, 1); + + BluetoothHealthApplicationSharedPtr app = JSBluetoothHealthApplication::toBluetoothHealthApplication(appObj); + int ret = bt_hdp_unregister_sink_app(app->getAppID().c_str()); + switch(ret) { + case BT_ERROR_NONE: + { + //app->setRegistrationState(false); + BluetoothHealthProfileHandlerCallback::syncToAsyncSuccessCB(callback); + break; + } + case BT_ERROR_NOT_ENABLED: + { + ServiceNotAvailableException *error = new ServiceNotAvailableException("Bluetooth device is turned off"); + BluetoothHealthProfileHandlerCallback::syncToAsyncErrorCB(callback, error); + break; + } + default: + { + UnknownException *error = new UnknownException("Unknown error"); + BluetoothHealthProfileHandlerCallback::syncToAsyncErrorCB(callback, error); + } + } + + TIME_TRACER_ITEM_END(__FUNCTION__, 1); +} +*/ + +void BluetoothHealthProfileHandler::unregisterApp(std::string appID, Common::MultiCallbackUserDataPtr callback) +{ + TIME_TRACER_ITEM_BEGIN(__FUNCTION__, 1); + BluetoothHealthProfileHandlerCallback::syncToAsyncUnregisterCB(callback, appID); + TIME_TRACER_ITEM_END(__FUNCTION__, 1); +} + +void BluetoothHealthProfileHandler::returnUnregisteringAppResult(std::string appID, Common::MultiCallbackUserDataPtr callback) +{ + TIME_TRACER_ITEM_BEGIN(__FUNCTION__, 1); + + RegisteredHealthAppMapT::iterator iter = mRegisteredHealthAppMap.find(appID); + BluetoothHealthApplicationSharedPtr application; + if(iter != mRegisteredHealthAppMap.end()) { + LoggerE("registered Health Application is found"); + application = iter->second; + } + else { + LoggerD("Already unregistered"); + if(callback) + callback->invokeCallback("success"); + return; + } + + int ret = bt_hdp_unregister_sink_app(appID.c_str()); + switch(ret) { + case BT_ERROR_NONE: + { + mRegisteredHealthAppMap.erase(iter); + application->setRegistrationState(false); + if(callback) + callback->invokeCallback("success"); + break; + } + case BT_ERROR_NOT_ENABLED: + { + if(callback) { + callback->invokeCallback("error", + JSWebAPIErrorFactory::makeErrorObject(callback->getContext(), ServiceNotAvailableException("Bluetooth device is turned off"))); + } + break; + } + default: + { + if(callback) { + callback->invokeCallback("error", + JSWebAPIErrorFactory::makeErrorObject(callback->getContext(), UnknownException("Unknown error"))); + } + } + } + + TIME_TRACER_ITEM_END(__FUNCTION__, 1); +} + +void BluetoothHealthProfileHandler::connectToSource(JSObjectRef remoteDeviceObj, JSObjectRef appObj, Common::MultiCallbackUserDataPtr callback) +{ + LoggerD("Enter"); + + TIME_TRACER_ITEM_BEGIN(__FUNCTION__, 1); + + BluetoothDeviceSharedPtr device = JSBluetoothDevice::toBluetoothDevice(remoteDeviceObj); + BluetoothHealthApplicationSharedPtr app = JSBluetoothHealthApplication::toBluetoothHealthApplication(appObj); + LoggerD("address: " << device->getAddress().c_str()); + LoggerD("app ID: " << app->getAppID().c_str()); + int ret = bt_hdp_connect_to_source(device->getAddress().c_str(), app->getAppID().c_str()); + switch(ret) { + case BT_ERROR_NONE: + { + LoggerD("NONE"); + HealthConnReqPtr connReq = new HealthConnReq(device, callback); + mHealthConnReqMap.insert(std::pair<BluetoothHealthApplicationSharedPtr, HealthConnReqPtr>(app, connReq)); + break; + } + case BT_ERROR_NOT_ENABLED: + { + LoggerD("Not Enabled"); + ServiceNotAvailableException *error = new ServiceNotAvailableException("Bluetooth device is turned off"); + BluetoothHealthProfileHandlerCallback::syncToAsyncErrorCB(callback, error); + break; + } + case BT_ERROR_INVALID_PARAMETER: + case BT_ERROR_REMOTE_DEVICE_NOT_BONDED: + { + LoggerD("invalid value"); + InvalidValuesException *error = new InvalidValuesException("Invalid value"); + BluetoothHealthProfileHandlerCallback::syncToAsyncErrorCB(callback, error); + break; + } + default: + { + LoggerD("Unknown error"); + UnknownException *error = new UnknownException("Unknown error"); + BluetoothHealthProfileHandlerCallback::syncToAsyncErrorCB(callback, error); + } + } + + TIME_TRACER_ITEM_END(__FUNCTION__, 1); +} + + +} // Bluetooth +} // DeviceAPI diff --git a/mobile_src/Bluetooth/BluetoothHealthProfileHandler.h b/mobile_src/Bluetooth/BluetoothHealthProfileHandler.h new file mode 100644 index 0000000..9573473 --- /dev/null +++ b/mobile_src/Bluetooth/BluetoothHealthProfileHandler.h @@ -0,0 +1,89 @@ +// +// Tizen Web Device API +// Copyright (c) 2012 Samsung Electronics Co., Ltd. +// +// Licensed under the Apache License, Version 2.0 (the License); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. +// + +#ifndef __TIZEN_HEALTH_PROFILE_HANDLER_H__ +#define __TIZEN_HEALTH_PROFILE_HANDLER_H__ + +#include <string> +#include <map> +#include <vector> + +#include <JavaScriptCore/JavaScript.h> + +#include <MultiCallbackUserData.h> +#include <PlatformException.h> +#include <bluetooth.h> + +#include "BluetoothHealthChannel.h" +#include "BluetoothHealthApplication.h" +#include "BluetoothDevice.h" + + +namespace DeviceAPI { +namespace Bluetooth { + +class HealthConnReq +{ +public: + HealthConnReq(BluetoothDeviceSharedPtr remoteDevice, Common::MultiCallbackUserDataPtr userData) + { + mRemoteDevice = remoteDevice; + mUserData = userData; + }; + + BluetoothDeviceSharedPtr mRemoteDevice; + Common::MultiCallbackUserDataPtr mUserData; +}; +typedef HealthConnReq* HealthConnReqPtr; + +class BluetoothHealthProfileHandler : public Common::SecurityAccessor +{ +public: + static BluetoothHealthProfileHandler* getInstance(); + void registerSinkApp(unsigned short dataType, std::string name, Common::MultiCallbackUserDataPtr callback); + void returnRegisteringSinkAppResult(unsigned short dataType, std::string name, Common::MultiCallbackUserDataPtr callback); +// void unregisterSinkApplication(JSObjectRef appObj, Common::MultiCallbackUserDataPtr callback); +// void returnUnregisteringResult(JSObjectRef appObj, Common::MultiCallbackUserDataPtr callback); + void unregisterApp(std::string appID, Common::MultiCallbackUserDataPtr callback); + void returnUnregisteringAppResult(std::string appID, Common::MultiCallbackUserDataPtr callback); + void connectToSource(JSObjectRef remoteDeviceObj, JSObjectRef appObj, Common::MultiCallbackUserDataPtr callback); + +private: + BluetoothHealthProfileHandler(); + virtual ~BluetoothHealthProfileHandler(); + + static void onConnected(int result, const char *remote_address, const char *app_id, + bt_hdp_channel_type_e type, unsigned int channel, void *userData); + static void onDisconnected(int result, const char *remote_address, unsigned int channel, void *userData); + static void onDataReceivedCB(unsigned int channel, const char *data, unsigned int size, void *userData); + + typedef std::map<BluetoothHealthApplicationSharedPtr, HealthConnReqPtr> HealthConnReqMapT; + typedef std::map<unsigned int, BluetoothHealthChannelPtr> HealthConnectedSocketMapT; // <channel, BluetoothHealthChannelPtr> + typedef std::map<std::string, BluetoothHealthApplicationSharedPtr> RegisteredHealthAppMapT; // <app_id, BluetoothHealthApplicationSharedPtr> + + + HealthConnReqMapT mHealthConnReqMap; + HealthConnectedSocketMapT mConnectedSocketMap; + RegisteredHealthAppMapT mRegisteredHealthAppMap; +}; + +typedef BluetoothHealthProfileHandler* HealthProfileHandlerPtr; + +} // Bluetooth +} // DeviceAPI + +#endif // __TIZEN_HEALTH_PROFILE_HANDLER_H__ diff --git a/mobile_src/Bluetooth/BluetoothHealthProfileHandlerCallback.cpp b/mobile_src/Bluetooth/BluetoothHealthProfileHandlerCallback.cpp new file mode 100644 index 0000000..242496c --- /dev/null +++ b/mobile_src/Bluetooth/BluetoothHealthProfileHandlerCallback.cpp @@ -0,0 +1,114 @@ +// +// Tizen Web Device API +// Copyright (c) 2012 Samsung Electronics Co., Ltd. +// +// Licensed under the Apache License, Version 2.0 (the License); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. +// + +#include <Ecore.h> + +#include <JSUtil.h> +#include <Logger.h> + +#include "BluetoothHealthProfileHandlerCallback.h" +#include "BluetoothHealthProfileHandler.h" + + +using namespace DeviceAPI::Common; + +namespace DeviceAPI { +namespace Bluetooth { + + +class HealthProfileHandlerCallbackData +{ +public: + HealthProfileHandlerCallbackData(MultiCallbackUserDataPtr callback, unsigned short dataType, std::string name) + { + mCallback = callback; + mDataType = dataType; + mName = name; + mIsRegistering = true; + }; + + HealthProfileHandlerCallbackData(MultiCallbackUserDataPtr callback, std::string appID) + { + mCallback = callback; + mAppID = appID; + mIsRegistering = false; + }; + + virtual ~HealthProfileHandlerCallbackData() + { + // Do nothing + }; + + MultiCallbackUserDataPtr mCallback; + unsigned short mDataType; + std::string mName; + std::string mAppID; + bool mIsRegistering; +}; + +typedef HealthProfileHandlerCallbackData* HealthProfileHandlerCallbackDataPtr; + + +static Eina_Bool idlerCallback(void *userData){ + HealthProfileHandlerCallbackDataPtr data = static_cast<HealthProfileHandlerCallbackDataPtr>(userData); + + if(!data) { + LoggerW("BluetoothCallbackDataPtr is NULL"); + return false; + } + + if(!(data->mCallback)) { + LoggerW("MulticallbackUserData is NULL"); + delete data; + return false; + } + + if(data->mIsRegistering) { // registerSinkApplication + LoggerD("In case of registerSinkApplication"); + BluetoothHealthProfileHandler::getInstance()->returnRegisteringSinkAppResult(data->mDataType, data->mName, data->mCallback); + } + else { // unregisterSinkApplication + LoggerD("In case of unregisterSinkApplication"); + BluetoothHealthProfileHandler::getInstance()->returnUnregisteringAppResult(data->mAppID, data->mCallback); + } + + delete data; + return false; +} + +void BluetoothHealthProfileHandlerCallback::syncToAsyncRegisterCB(MultiCallbackUserDataPtr callback, unsigned short dataType, std::string name) +{ + HealthProfileHandlerCallbackDataPtr data = new HealthProfileHandlerCallbackData(callback, dataType, name); + ecore_idler_add(idlerCallback, data); +} + +/* +void BluetoothHealthProfileHandlerCallback::syncToAsyncUnregisterCB(MultiCallbackUserDataPtr callback, JSObjectRef application) +{ + HealthProfileHandlerCallbackDataPtr data = new HealthProfileHandlerCallbackData(callback, application); + ecore_idler_add(idlerCallback, data); +} +*/ +void BluetoothHealthProfileHandlerCallback::syncToAsyncUnregisterCB(MultiCallbackUserDataPtr callback, std::string appID) +{ + HealthProfileHandlerCallbackDataPtr data = new HealthProfileHandlerCallbackData(callback, appID); + ecore_idler_add(idlerCallback, data); +} + + +} // Bluetooth +} // DeviceAPI diff --git a/mobile_src/Bluetooth/BluetoothHealthProfileHandlerCallback.h b/mobile_src/Bluetooth/BluetoothHealthProfileHandlerCallback.h new file mode 100644 index 0000000..b201206 --- /dev/null +++ b/mobile_src/Bluetooth/BluetoothHealthProfileHandlerCallback.h @@ -0,0 +1,45 @@ +// +// Tizen Web Device API +// Copyright (c) 2012 Samsung Electronics Co., Ltd. +// +// Licensed under the Apache License, Version 2.0 (the License); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. +// + +#ifndef __TIZEN_HEALTH_PROFILE_HANDLER_CALLBACK_H__ +#define __TIZEN_HEALTH_PROFILE_HANDLER_CALLBACK_H__ + +#include <string> +#include <MultiCallbackUserData.h> +#include <JavaScriptCore/JavaScript.h> + +#include "BluetoothCallback.h" + +namespace DeviceAPI { +namespace Bluetooth { + + +class BluetoothHealthProfileHandlerCallback : public BluetoothCallback +{ +public: + static void syncToAsyncRegisterCB(DeviceAPI::Common::MultiCallbackUserDataPtr callback, unsigned short dataType, std::string name); + //static void syncToAsyncUnregisterCB(DeviceAPI::Common::MultiCallbackUserDataPtr callback, JSObjectRef application); + static void syncToAsyncUnregisterCB(DeviceAPI::Common::MultiCallbackUserDataPtr callback, std::string appID); +}; + + +} // Bluetooth +} // DeviceAPI + + + +#endif // __TIZEN_HEALTH_PROFILE_HANDLER_CALLBACK_H__ diff --git a/mobile_src/Bluetooth/BluetoothServiceHandler.cpp b/mobile_src/Bluetooth/BluetoothServiceHandler.cpp new file mode 100644 index 0000000..d325b37 --- /dev/null +++ b/mobile_src/Bluetooth/BluetoothServiceHandler.cpp @@ -0,0 +1,131 @@ +// +// Tizen Web Device API +// Copyright (c) 2012 Samsung Electronics Co., Ltd. +// +// Licensed under the Apache License, Version 2.0 (the License); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. +// + +#include <GlobalContextManager.h> +#include <PlatformException.h> + +#include "BluetoothServiceHandler.h" +#include "BluetoothAdapter.h" +#include "BluetoothCallbackUtil.h" +#include "plugin_config.h" + +#include <Logger.h> +#include <TimeTracer.h> + +using namespace DeviceAPI::Common; + +namespace DeviceAPI { +namespace Bluetooth { + +BluetoothServiceHandler::BluetoothServiceHandler(std::string uuid, std::string name, int registeredSocket) +{ + Common::SecurityAccessor(); + + mUUID = uuid; + mName = name; + mRegisteredSocket = registeredSocket; + mIsRegistered = true; + mIsConnected = false; +} + +BluetoothServiceHandler::~BluetoothServiceHandler() +{ + if(mIsRegistered) { + BluetoothAdapter::getInstance()->unregisterUUID(mUUID); + if(bt_socket_destroy_rfcomm(mRegisteredSocket) != BT_ERROR_NONE) { + LoggerW("Already destroyed"); + } + } +} + +bool BluetoothServiceHandler::setOnConnect(JSContextRef context, JSObjectRef onConnect) +{ + MultiCallbackUserDataPtr callback( + new MultiCallbackUserData(GlobalContextManager::getInstance()->getGlobalContext(context))); + if(!callback){ + LoggerW("Can't create MultiCallbackUserData"); + return false; + } + callback->setCallback("onconnect", onConnect); + mOnConnect = callback; + + return mLocalProperty.setProperty(context, BLUETOOTH_SERVICE_HANDLER_ONCONNECT, onConnect); +} + +std::string BluetoothServiceHandler::getUUID() const +{ + return mUUID; +} + +std::string BluetoothServiceHandler::getName() const +{ + return mName; +} + +int BluetoothServiceHandler::getRegisteredSocket() const +{ + return mRegisteredSocket; +} + +MultiCallbackUserDataPtr BluetoothServiceHandler::getOnConnect() const +{ + return mOnConnect; +} + +JSValueRef BluetoothServiceHandler::getOnConnect(JSContextRef context) +{ + JSValueRef onConnect = mLocalProperty.getProperty(context, BLUETOOTH_SERVICE_HANDLER_ONCONNECT); + if(onConnect == NULL) { + LoggerD("onconnect is null"); + return JSValueMakeNull(context); + } + + return onConnect; +} + +void BluetoothServiceHandler::setConnectionState(bool isConnected) +{ + mIsConnected = isConnected; +} + +bool BluetoothServiceHandler::getConnectionState() const +{ + return mIsConnected; +} + +void BluetoothServiceHandler::unregister(MultiCallbackUserDataPtr userData) +{ + if(BluetoothAdapter::getInstance()->getPowered() == true) { + TIME_TRACER_ITEM_BEGIN("unregister::bt_socket_destroy_rfcomm", 1); + if(bt_socket_destroy_rfcomm(mRegisteredSocket) == BT_ERROR_NONE) { + TIME_TRACER_ITEM_END("unregister::bt_socket_destroy_rfcomm", 1); + mIsRegistered = false; + BluetoothAdapter::getInstance()->unregisterUUID(mUUID); + BluetoothCallbackUtil::syncToAsyncSuccessCallback(userData); + } + else { + UnknownException *error = new UnknownException("Unknown exception"); + BluetoothCallbackUtil::syncToAsyncErrorCallback(userData, error); + } + } else { // Not enabled + ServiceNotAvailableException *error = new ServiceNotAvailableException("Bluetooth device is turned off"); + BluetoothCallbackUtil::syncToAsyncErrorCallback(userData, error); + } +} + +} // Bluetooth +} // DeviceAPI diff --git a/mobile_src/Bluetooth/BluetoothServiceHandler.h b/mobile_src/Bluetooth/BluetoothServiceHandler.h new file mode 100644 index 0000000..fd30b1a --- /dev/null +++ b/mobile_src/Bluetooth/BluetoothServiceHandler.h @@ -0,0 +1,65 @@ +// +// Tizen Web Device API +// Copyright (c) 2012 Samsung Electronics Co., Ltd. +// +// Licensed under the Apache License, Version 2.0 (the License); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. +// + +#ifndef __TIZEN_BLUETOOTH_SERVICE_HANDLER_H__ +#define __TIZEN_BLUETOOTH_SERVICE_HANDLER_H__ + +#include <string> +#include <JavaScriptCore/JavaScript.h> + +//#include <boost/shared_ptr.hpp> +#include <MultiCallbackUserData.h> +#include <PropertyBag.h> +#include <Security.h> + +namespace DeviceAPI { +namespace Bluetooth { + +class BluetoothServiceHandler : public Common::SecurityAccessor +{ +public: + BluetoothServiceHandler(std::string uuid, std::string name, int registeredSocket); + virtual ~BluetoothServiceHandler(); + + std::string getUUID() const; + std::string getName() const; + int getRegisteredSocket() const; + bool getConnectionState() const; + Common::MultiCallbackUserDataPtr getOnConnect() const; + JSValueRef getOnConnect(JSContextRef context); + + void setConnectionState(bool isConnected); + bool setOnConnect(JSContextRef context, JSObjectRef onConnect); + + void unregister(Common::MultiCallbackUserDataPtr userData); + +private: + std::string mUUID; + std::string mName; + int mRegisteredSocket; + bool mIsConnected; + bool mIsRegistered; + Common::MultiCallbackUserDataPtr mOnConnect; + Common::PropertyBag mLocalProperty; +}; + +typedef BluetoothServiceHandler* BluetoothServiceHandlerPtr; + +} // Bluetooth +} // DeviceAPI + +#endif // __TIZEN_BLUETOOTH_SERVICE_HANDLER_H__ diff --git a/mobile_src/Bluetooth/BluetoothSocket.cpp b/mobile_src/Bluetooth/BluetoothSocket.cpp new file mode 100644 index 0000000..78b8596 --- /dev/null +++ b/mobile_src/Bluetooth/BluetoothSocket.cpp @@ -0,0 +1,236 @@ +// +// Tizen Web Device API +// Copyright (c) 2012 Samsung Electronics Co., Ltd. +// +// Licensed under the Apache License, Version 2.0 (the License); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. +// + +#include <GlobalContextManager.h> +#include <PlatformException.h> + +#include "BluetoothSocket.h" +#include "BluetoothAdapter.h" +#include "plugin_config.h" +#include "JSBluetoothDevice.h" + +#include <Logger.h> +#include <TimeTracer.h> + +using namespace DeviceAPI::Common; + +namespace DeviceAPI { +namespace Bluetooth { + +BluetoothSocket::BluetoothSocket(bt_socket_connection_s *connection, Common::SecurityAccessor* accessor) +{ + Common::SecurityAccessor(); + Common::SecurityAccessor::copyAceCheckAccessFunction(accessor); + + mConnectedSocket = connection->socket_fd; + mUUID = std::string(connection->service_uuid); + mIsConnected = true; + + bt_device_info_s *deviceInfo = NULL; + if(bt_adapter_get_bonded_device_info(connection->remote_address, &deviceInfo) == BT_ERROR_NONE && deviceInfo != NULL) { + BluetoothDeviceSharedPtr device(new BluetoothDevice(deviceInfo)); + device->copyAceCheckAccessFunction(accessor); + mPeer = device; + bt_adapter_free_device_info(deviceInfo); + } +} + +BluetoothSocket::~BluetoothSocket() +{ + if(mIsConnected) { + if(bt_socket_disconnect_rfcomm(mConnectedSocket) != BT_ERROR_NONE) { + LoggerW("Already disconnected"); + } + + if(!BluetoothAdapter::getInstance()->closeConnectedSocket(mConnectedSocket)) { + LoggerW("Already done"); + } + } +} + +bool BluetoothSocket::setOnMessage(JSContextRef context, JSObjectRef onMessage) +{ + MultiCallbackUserDataPtr callback( + new MultiCallbackUserData(GlobalContextManager::getInstance()->getGlobalContext(context))); + if(!callback){ + LoggerW("Can't create MultiCallbackUserData"); + return false; + } + callback->setCallback("onmessage", onMessage); + mOnMessage = callback; + + return mLocalProperty.setProperty(context, BLUETOOTH_SOCKET_ONMESSAGE, onMessage); +} + +bool BluetoothSocket::setOnClose(JSContextRef context, JSObjectRef onClose) +{ + MultiCallbackUserDataPtr callback( + new MultiCallbackUserData(GlobalContextManager::getInstance()->getGlobalContext(context))); + if(!callback){ + LoggerW("Can't create MultiCallbackUserData"); + return false; + } + callback->setCallback("onclose", onClose); + mOnClose = callback; + + return mLocalProperty.setProperty(context, BLUETOOTH_SOCKET_ONCLOSE, onClose); +} + +bool BluetoothSocket::setOnError(JSContextRef context, JSObjectRef onError) +{ + MultiCallbackUserDataPtr callback( + new MultiCallbackUserData(GlobalContextManager::getInstance()->getGlobalContext(context))); + if(!callback){ + LoggerW("Can't create MultiCallbackUserData"); + return false; + } + callback->setCallback("onerror", onError); + mOnError = callback; + + return mLocalProperty.setProperty(context, BLUETOOTH_SOCKET_ONERROR, onError); +} + +std::string BluetoothSocket::getUUID() const +{ + return mUUID; +} + +void BluetoothSocket::setConnectionState(bool isConnected) +{ + mIsConnected = isConnected; +} + +bool BluetoothSocket::getConnectionState() +{ + return mIsConnected; +} + +JSValueRef BluetoothSocket::getPeer(JSContextRef context) +{ + /* + JSValueRef peer = mLocalProperty.getProperty(context, BLUETOOTH_SOCKET_PEER); + if(peer == NULL && mPeer != NULL) { + peer = JSBluetoothDevice::createJSObject(context, mPeer); + mLocalProperty.setProperty(context, BLUETOOTH_DEVICE_DEVICE_CLASS, peer); + } + + return peer; + */ + return JSBluetoothDevice::createJSObject(context, mPeer); +} + +MultiCallbackUserDataPtr BluetoothSocket::getOnMessage() const +{ + return mOnMessage; +} + +JSValueRef BluetoothSocket::getOnMessage(JSContextRef context) +{ + JSValueRef onMessage = mLocalProperty.getProperty(context, BLUETOOTH_SOCKET_ONMESSAGE); + if(onMessage == NULL) { + LoggerD("onmessage is null"); + return JSValueMakeNull(context); + } + + return onMessage; +} + +MultiCallbackUserDataPtr BluetoothSocket::getOnClose() const +{ + return mOnClose; +} + +JSValueRef BluetoothSocket::getOnClose(JSContextRef context) +{ + JSValueRef onClose = mLocalProperty.getProperty(context, BLUETOOTH_SOCKET_ONCLOSE); + if(onClose == NULL) { + LoggerD("onclose is null"); + return JSValueMakeNull(context); + } + + return onClose; +} + +MultiCallbackUserDataPtr BluetoothSocket::getOnError() const +{ + return mOnError; +} + +JSValueRef BluetoothSocket::getOnError(JSContextRef context) +{ + JSValueRef onError = mLocalProperty.getProperty(context, BLUETOOTH_SOCKET_ONERROR); + if(onError == NULL) { + LoggerD("onerror is null"); + return JSValueMakeNull(context); + } + + return onError; +} + +unsigned long BluetoothSocket::writeData(char* data, unsigned long size) +{ + unsigned long ret = 0; + TIME_TRACER_ITEM_BEGIN("writeData::bt_socket_send_data", 1); + if(bt_socket_send_data(mConnectedSocket, data, static_cast<int>(size)) == BT_ERROR_NONE) { + TIME_TRACER_ITEM_END("writeData::bt_socket_send_data", 1); + LoggerD("bt_socket_send_data() succeeded"); + ret = size; + } + else { + TIME_TRACER_ITEM_END("writeData::bt_socket_send_data", 1); + throw UnknownException("Unknown error"); + } + + //delete data; + return ret; +} + +void BluetoothSocket::storeRecivedData(char *data, unsigned long size) +{ + for(unsigned long i = 0; i < size; i++) { + mReceivedData.push_back(static_cast<signed char>(data[i])); + } +} + +std::vector<signed char> BluetoothSocket::readData() +{ + std::vector<signed char> result (mReceivedData); + mReceivedData.clear(); + + return result; +} + +void BluetoothSocket::close() +{ + if(!mIsConnected) { + LoggerD("Already disconnected"); + return; + } + + TIME_TRACER_ITEM_BEGIN("close::bt_socket_disconnect_rfcomm", 1); + if(bt_socket_disconnect_rfcomm(mConnectedSocket) != BT_ERROR_NONE) { + TIME_TRACER_ITEM_END("close::bt_socket_disconnect_rfcomm", 1); + LoggerE("bt_socket_disconnect_rfcomm() failed"); + throw UnknownException("Unknown error"); + } + TIME_TRACER_ITEM_END("close::bt_socket_disconnect_rfcomm", 1); + + mIsConnected = false; +} + +} // Bluetooth +} // DeviceAPI diff --git a/mobile_src/Bluetooth/BluetoothSocket.h b/mobile_src/Bluetooth/BluetoothSocket.h new file mode 100644 index 0000000..4ff9c38 --- /dev/null +++ b/mobile_src/Bluetooth/BluetoothSocket.h @@ -0,0 +1,76 @@ +// +// Tizen Web Device API +// Copyright (c) 2012 Samsung Electronics Co., Ltd. +// +// Licensed under the Apache License, Version 2.0 (the License); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. +// + +#ifndef __TIZEN_BLUETOOTH_SOCKET_H__ +#define __TIZEN_BLUETOOTH_SOCKET_H__ + +#include <string> +#include <bluetooth.h> +#include <JavaScriptCore/JavaScript.h> + +//#include <boost/shared_ptr.hpp> +#include <MultiCallbackUserData.h> +#include <PropertyBag.h> +#include <Security.h> +#include "BluetoothDevice.h" + +namespace DeviceAPI { +namespace Bluetooth { + +class BluetoothSocket : public Common::SecurityAccessor +{ +public: + BluetoothSocket(bt_socket_connection_s *connection, Common::SecurityAccessor* accessor); + virtual ~BluetoothSocket(); + + std::string getUUID() const; + bool getConnectionState(); + JSValueRef getPeer(JSContextRef context); + Common::MultiCallbackUserDataPtr getOnMessage() const; + JSValueRef getOnMessage(JSContextRef context); + Common::MultiCallbackUserDataPtr getOnClose() const; + JSValueRef getOnClose(JSContextRef context); + Common::MultiCallbackUserDataPtr getOnError() const; + JSValueRef getOnError(JSContextRef context); + bool setOnMessage(JSContextRef context, JSObjectRef onMessage); + bool setOnClose(JSContextRef context, JSObjectRef onClose); + bool setOnError(JSContextRef context, JSObjectRef onError); + void storeRecivedData(char *data, unsigned long size); + void setConnectionState(bool isConnected); + + unsigned long writeData(char* data, unsigned long size); + std::vector<signed char> readData(); + void close(); + +private: + std::string mUUID; + int mConnectedSocket; + bool mIsConnected; + BluetoothDeviceSharedPtr mPeer; + std::vector<signed char> mReceivedData; + Common::MultiCallbackUserDataPtr mOnMessage; + Common::MultiCallbackUserDataPtr mOnClose; + Common::MultiCallbackUserDataPtr mOnError; + Common::PropertyBag mLocalProperty; +}; + +typedef BluetoothSocket* BluetoothSocketPtr; + +} // Bluetooth +} // DeviceAPI + +#endif // __TIZEN_BLUETOOTH_SOCKET_H__ diff --git a/mobile_src/Bluetooth/CMakeLists.txt b/mobile_src/Bluetooth/CMakeLists.txt new file mode 100644 index 0000000..c4bb7f7 --- /dev/null +++ b/mobile_src/Bluetooth/CMakeLists.txt @@ -0,0 +1,75 @@ +SET(TARGET_NAME ${bluetooth_target}) +SET(DESTINATION_NAME ${bluetooth_dest}) +SET(TARGET_IMPL_NAME ${bluetooth_impl}) + +IF(ENABLE_OPTIONAL_BT) +PKG_SEARCH_MODULE(bluetooth REQUIRED capi-network-bluetooth) +PKG_SEARCH_MODULE(system-info REQUIRED capi-system-info) + +SET(CMAKE_INSTALL_RPATH + ${CMAKE_INSTALL_RPATH} + ${CMAKE_INSTALL_PREFIX}/${DESTINATION_LIB_PREFIX}/${DESTINATION_NAME} +) + +SET(SRCS_IMPL + JSBluetoothManager.cpp + JSBluetoothAdapter.cpp + JSBluetoothDevice.cpp + JSBluetoothSocket.cpp + JSBluetoothClass.cpp + JSBluetoothClassDeviceMajor.cpp + JSBluetoothClassDeviceMinor.cpp + JSBluetoothClassDeviceService.cpp + JSBluetoothServiceHandler.cpp + JSBluetoothHealthApplication.cpp + JSBluetoothHealthChannel.cpp + JSBluetoothProfileHandler.cpp + JSBluetoothHealthProfileHandler.cpp + BluetoothAdapter.cpp + BluetoothDevice.cpp + BluetoothSocket.cpp + BluetoothClass.cpp + BluetoothClassDeviceMajor.cpp + BluetoothClassDeviceMinor.cpp + BluetoothClassDeviceService.cpp + BluetoothServiceHandler.cpp + BluetoothCallbackUtil.cpp + BluetoothCallback.cpp + BluetoothHealthApplication.cpp + BluetoothHealthChannel.cpp + BluetoothHealthProfileHandlerCallback.cpp + BluetoothHealthProfileHandler.cpp +) + +INCLUDE_DIRECTORIES( + ${INCLUDE_COMMON} + ${bluetooth_INCLUDE_DIRS} + ${system-info_INCLUDE_DIRS} +) + +ADD_LIBRARY(${TARGET_IMPL_NAME} SHARED ${SRCS_IMPL}) + +TARGET_LINK_LIBRARIES(${TARGET_IMPL_NAME} + ${LIBS_COMMON} + ${bluetooth_LIBRARIES} + ${system-info_LIBRARIES} +) + +SET(SRCS + plugin_config.cpp + plugin_initializer.cpp +) + +ADD_LIBRARY(${TARGET_NAME} SHARED ${SRCS}) + +TARGET_LINK_LIBRARIES(${TARGET_NAME} + ${TARGET_IMPL_NAME} +) + +INSTALL(TARGETS ${TARGET_NAME} ${TARGET_IMPL_NAME} LIBRARY DESTINATION ${DESTINATION_LIB_PREFIX}/${DESTINATION_NAME}) +INSTALL(FILES ${CMAKE_CURRENT_SOURCE_DIR}/config.xml DESTINATION ${DESTINATION_LIB_PREFIX}/${DESTINATION_NAME}) +ENDIF(ENABLE_OPTIONAL_BT) +INSTALL( + DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}/ DESTINATION ${DESTINATION_HEADER_PREFIX}/bluetooth + FILES_MATCHING PATTERN "*.h" PATTERN "CMakeFiles" EXCLUDE +) diff --git a/mobile_src/Bluetooth/JSBluetoothAdapter.cpp b/mobile_src/Bluetooth/JSBluetoothAdapter.cpp new file mode 100644 index 0000000..2e492a2 --- /dev/null +++ b/mobile_src/Bluetooth/JSBluetoothAdapter.cpp @@ -0,0 +1,763 @@ +// +// Tizen Web Device API +// Copyright (c) 2012 Samsung Electronics Co., Ltd. +// +// Licensed under the Apache License, Version 2.0 (the License); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. +// + +#include <SecurityExceptions.h> + +#include <JSUtil.h> +#include <ArgumentValidator.h> +#include <GlobalContextManager.h> + +#include "plugin_config.h" +#include "JSBluetoothAdapter.h" +#include "BluetoothAdapter.h" +#include "BluetoothHealthProfileHandler.h" +#include "JSBluetoothHealthProfileHandler.h" + +#include <system_info.h> +#include <TimeTracer.h> +#include <Logger.h> +#include "plugin_config_impl.h" +using namespace WrtDeviceApis::Commons; +using namespace DeviceAPI::Common; + +namespace DeviceAPI { +namespace Bluetooth { + +JSClassDefinition JSBluetoothAdapter::m_classInfo = { + 0, + kJSClassAttributeNone, + "BluetoothAdapter", + NULL, //ParentClass + m_property, //StaticValues + m_function, //StaticFunctions + initialize, //Initialize + finalize, //Finalize + NULL, //HasProperty, + NULL, //GetProperty, + NULL, //SetProperty, + NULL, //DeleteProperty, + NULL, //GetPropertyNames, + NULL, //CallAsFunction, + NULL, //CallAsConstructor, + NULL, //HasInstance, + NULL //ConvertToType +}; + +JSStaticValue JSBluetoothAdapter::m_property[] = { + { BLUETOOTH_ADAPTER_NAME, getProperty, NULL, kJSPropertyAttributeNone|kJSPropertyAttributeReadOnly|kJSPropertyAttributeDontDelete }, + { BLUETOOTH_ADAPTER_POWERED, getProperty, NULL, kJSPropertyAttributeNone|kJSPropertyAttributeReadOnly|kJSPropertyAttributeDontDelete }, + { BLUETOOTH_ADAPTER_VISIBLE, getProperty, NULL, kJSPropertyAttributeNone|kJSPropertyAttributeReadOnly|kJSPropertyAttributeDontDelete }, + { BLUETOOTH_ADAPTER_ADDRESS, getProperty, NULL, kJSPropertyAttributeNone|kJSPropertyAttributeReadOnly|kJSPropertyAttributeDontDelete }, + { 0, 0, 0, 0 } +}; + +JSStaticFunction JSBluetoothAdapter::m_function[] = { + { BLUETOOTH_ADAPTER_API_SET_NAME, setName, kJSPropertyAttributeNone }, + { BLUETOOTH_ADAPTER_API_SET_POWERED, setPowered, kJSPropertyAttributeNone }, + { BLUETOOTH_ADAPTER_API_SET_VISIBLE, setVisible, kJSPropertyAttributeNone }, + { BLUETOOTH_ADAPTER_API_DISCOVER_DEVICES, discoverDevices, kJSPropertyAttributeNone }, + { BLUETOOTH_ADAPTER_API_STOP_DISCOVERY, stopDiscovery, kJSPropertyAttributeNone }, + { BLUETOOTH_ADAPTER_API_GET_KNOWN_DEVICES, getKnownDevices, kJSPropertyAttributeNone }, + { BLUETOOTH_ADAPTER_API_GET_DEVICE, getDevice, kJSPropertyAttributeNone }, + { BLUETOOTH_ADAPTER_API_CREATE_BONDING, createBonding, kJSPropertyAttributeNone }, + { BLUETOOTH_ADAPTER_API_DESTROY_BONDING, destroyBonding, kJSPropertyAttributeNone }, + { BLUETOOTH_ADAPTER_API_REGISTER_RFCOMMSERVICE_BY_UUID, registerRFCOMMServiceByUUID, kJSPropertyAttributeNone }, + { BLUETOOTH_ADAPTER_API_GET_BLUETOOTH_PROFILE_HANDLER, getBluetoothProfileHandler, kJSPropertyAttributeNone }, + { BLUETOOTH_ADAPTER_API_SET_CHANGE_LISTENER, setChangeListener, kJSPropertyAttributeNone }, + { BLUETOOTH_ADAPTER_API_UNSET_CHANGE_LISTENER, unsetChangeListener, kJSPropertyAttributeNone }, + { 0, 0, 0 } +}; + +JSClassRef JSBluetoothAdapter::m_jsClassRef = JSClassCreate(JSBluetoothAdapter::getClassInfo()); + +const JSClassRef JSBluetoothAdapter::getClassRef() +{ + if (!m_jsClassRef) { + m_jsClassRef = JSClassCreate(&m_classInfo); + } + return m_jsClassRef; +} + +const JSClassDefinition* JSBluetoothAdapter::getClassInfo() +{ + return &m_classInfo; +} + +JSObjectRef JSBluetoothAdapter::createJSObject(JSContextRef context) +{ + return JSObjectMake(context, getClassRef(), NULL); +} + +void JSBluetoothAdapter::initialize(JSContextRef context, JSObjectRef object) +{ + // do nothing +} + + +void JSBluetoothAdapter::finalize(JSObjectRef object) +{ + // do nothing +} + +JSValueRef JSBluetoothAdapter::getProperty(JSContextRef context, + JSObjectRef object, + JSStringRef propertyName, + JSValueRef* exception) +{ + try { + if (JSStringIsEqualToUTF8CString(propertyName, BLUETOOTH_ADAPTER_NAME)) { + std::string name = BluetoothAdapter::getInstance()->getName(); + return JSUtil::toJSValueRef(context, name); + } + else if (JSStringIsEqualToUTF8CString(propertyName, BLUETOOTH_ADAPTER_POWERED)) { + return JSUtil::toJSValueRef(context, BluetoothAdapter::getInstance()->getPowered()); + } + else if (JSStringIsEqualToUTF8CString(propertyName, BLUETOOTH_ADAPTER_VISIBLE)) { + return JSUtil::toJSValueRef(context, BluetoothAdapter::getInstance()->getVisible()); + } + else if (JSStringIsEqualToUTF8CString(propertyName, BLUETOOTH_DEVICE_ADDRESS)) { + std::string address = BluetoothAdapter::getInstance()->getAddress(); + return JSUtil::toJSValueRef(context, address); + } + } catch (const BasePlatformException &err) { + LoggerW("Getting property is failed" << err.getMessage().c_str()); + } + + return NULL; +} + +JSValueRef JSBluetoothAdapter::setName(JSContextRef context, + JSObjectRef object, + JSObjectRef thisObject, + size_t argumentCount, + const JSValueRef arguments[], + JSValueRef* exception) +{ + TIME_TRACER_ITEM_BEGIN(__FUNCTION__, 1); + + // Access Check + TIME_TRACER_ITEM_BEGIN("setName::ACE", 1); + TIZEN_CHECK_ACCESS(context, exception, BluetoothAdapter::getInstance(), BLUETOOTH_ADAPTER_API_SET_NAME); + TIME_TRACER_ITEM_END("setName::ACE", 1); + + + try { + ArgumentValidator validator(context, argumentCount, arguments); + std::string name = validator.toString(0); // name + JSObjectRef successCallback = validator.toFunction(1, true); // successCallback + JSObjectRef errorCallback = validator.toFunction(2, true); // errorCallback + + // perform + MultiCallbackUserDataPtr callback( + new MultiCallbackUserData(GlobalContextManager::getInstance()->getGlobalContext(context))); + if(!callback){ + LoggerW("Can't create MultiCallbackUserData"); + } + else { + callback->setCallback("success", successCallback); + callback->setCallback("error", errorCallback); + } + + BluetoothAdapter::getInstance()->setName(name, callback); + TIME_TRACER_ITEM_END(__FUNCTION__, 1); + + return JSValueMakeUndefined(context); + } catch (const BasePlatformException &err) { + return JSWebAPIErrorFactory::postException(context, exception, err); + } catch (...) { + DeviceAPI::Common::UnknownException err("Unknown Error in BluetoothAdapter.setName()."); + return JSWebAPIErrorFactory::postException(context, exception, err); + } +} + +JSValueRef JSBluetoothAdapter::setPowered(JSContextRef context, + JSObjectRef object, + JSObjectRef thisObject, + size_t argumentCount, + const JSValueRef arguments[], + JSValueRef* exception) +{ + LoggerD("Enter"); + + TIME_TRACER_ITEM_BEGIN(__FUNCTION__, 1); + + // Access Check + TIME_TRACER_ITEM_BEGIN("setPowered::ACE", 1); + TIZEN_CHECK_ACCESS(context, exception, BluetoothAdapter::getInstance(), BLUETOOTH_ADAPTER_API_SET_POWERED); + TIME_TRACER_ITEM_END("setPowered::ACE", 1); + + try { + // Check whether BT is supported or not + bool supported = false; + if(system_info_get_platform_bool("tizen.org/feature/network.bluetooth", &supported) != SYSTEM_INFO_ERROR_NONE) { + LoggerW("Can't check BT is supported or not"); + } + + if(supported == false) { + LoggerW("BT is not supported"); + throw DeviceAPI::Common::NotSupportedException("Bluetooth is not supported"); + } + else { + LoggerD("BT is supported"); + } + + // Validate arguments + ArgumentValidator validator(context, argumentCount, arguments); + bool state = validator.toBool(0); // state + JSObjectRef successCallback = validator.toFunction(1, true); // successCallback + JSObjectRef errorCallback = validator.toFunction(2, true); // errorCallback + + // perform + MultiCallbackUserDataPtr callback( + new MultiCallbackUserData(GlobalContextManager::getInstance()->getGlobalContext(context))); + if(!callback){ + LoggerW("Can't create MultiCallbackUserData"); + } + else { + callback->setCallback("success", successCallback); + callback->setCallback("error", errorCallback); + } + + BluetoothAdapter::getInstance()->setPowered(state, callback); + TIME_TRACER_ITEM_END(__FUNCTION__, 1); + + return JSValueMakeUndefined(context); + } catch (const BasePlatformException &err) { + return JSWebAPIErrorFactory::postException(context, exception, err); + } catch (...) { + DeviceAPI::Common::UnknownException err("Unknown Error in BluetoothAdapter.setPowered()."); + return JSWebAPIErrorFactory::postException(context, exception, err); + } +} + +JSValueRef JSBluetoothAdapter::setVisible(JSContextRef context, + JSObjectRef object, + JSObjectRef thisObject, + size_t argumentCount, + const JSValueRef arguments[], + JSValueRef* exception) +{ + TIME_TRACER_ITEM_BEGIN(__FUNCTION__, 1); + + // Access Check + TIME_TRACER_ITEM_BEGIN("setVisible::ACE", 1); + TIZEN_CHECK_ACCESS(context, exception, BluetoothAdapter::getInstance(), BLUETOOTH_ADAPTER_API_SET_VISIBLE); + TIME_TRACER_ITEM_END("setVisible::ACE", 1); + + + try { + // Validate arguments + ArgumentValidator validator(context, argumentCount, arguments); + bool mode = validator.toBool(0); // mode + JSObjectRef successCallback = validator.toFunction(1, true); // successCallback + JSObjectRef errorCallback = validator.toFunction(2, true); // errorCallback + unsigned long timeout = validator.toULong(3, true, 180); // timeout + if(timeout > 65535) + timeout = 180; + + // perform + MultiCallbackUserDataPtr callback( + new MultiCallbackUserData(GlobalContextManager::getInstance()->getGlobalContext(context))); + if(!callback){ + LoggerW("Can't create MultiCallbackUserData"); + } + else { + callback->setCallback("success", successCallback); + callback->setCallback("error", errorCallback); + } + + BluetoothAdapter::getInstance()->setVisible(mode, timeout, callback); + TIME_TRACER_ITEM_END(__FUNCTION__, 1); + + return JSValueMakeUndefined(context); + } catch (const BasePlatformException &err) { + return JSWebAPIErrorFactory::postException(context, exception, err); + } catch (...) { + DeviceAPI::Common::UnknownException err("Unknown Error in BluetoothAdapter.setVisible()."); + return JSWebAPIErrorFactory::postException(context, exception, err); + } +} + +JSValueRef JSBluetoothAdapter::discoverDevices(JSContextRef context, + JSObjectRef object, + JSObjectRef thisObject, + size_t argumentCount, + const JSValueRef arguments[], + JSValueRef* exception) +{ + TIME_TRACER_ITEM_BEGIN(__FUNCTION__, 1); + + // Access Check + TIME_TRACER_ITEM_BEGIN("discoverDevices::ACE", 1); + TIZEN_CHECK_ACCESS(context, exception, BluetoothAdapter::getInstance(), BLUETOOTH_ADAPTER_API_DISCOVER_DEVICES); + TIME_TRACER_ITEM_END("discoverDevices::ACE", 1); + + + try { + TIME_TRACER_ITEM_BEGIN("discoverDevices::parameter", 1); + + // Validate arguments + ArgumentValidator validator(context, argumentCount, arguments); + + // successCallback + JSObjectRef successCallback = validator.toCallbackObject(0, false, "onstarted", "ondevicefound", "ondevicedisappeared", "onfinished", NULL); + + // errorCallback + JSObjectRef errorCallback = validator.toFunction(1, true); + + MultiCallbackUserDataPtr callback( + new MultiCallbackUserData(GlobalContextManager::getInstance()->getGlobalContext(context))); + if(!callback){ + LoggerW("Can't create MultiMultiCallbackUserData"); + } + else { + // onstarted + JSValueRef onstartedValue = JSUtil::getProperty(context , successCallback, "onstarted"); + if(!JSValueIsUndefined(context, onstartedValue)) { + LoggerD("There is a onstarted()"); + callback->setCallback("onstarted", JSUtil::JSValueToObject(context, onstartedValue)); + } + + // ondevicefound + JSValueRef ondevicefoundValue = JSUtil::getProperty(context , successCallback, "ondevicefound"); + if(!JSValueIsUndefined(context, ondevicefoundValue)) { + LoggerD("There is a ondevicefound()"); + callback->setCallback("ondevicefound", JSUtil::JSValueToObject(context, ondevicefoundValue)); + } + + // ondevicedisappeared + JSValueRef ondevicedisappearedValue = JSUtil::getProperty(context , successCallback, "ondevicedisappeared"); + if(!JSValueIsUndefined(context, ondevicedisappearedValue)) { + LoggerD("There is a ondevicedisappeared()"); + callback->setCallback("ondevicedisappeared", JSUtil::JSValueToObject(context, ondevicedisappearedValue)); + } + + // onfinished + JSValueRef onfinishedValue = JSUtil::getProperty(context , successCallback, "onfinished"); + if(!JSValueIsUndefined(context, onfinishedValue)) { + LoggerD("There is a onfinished()"); + callback->setCallback("onfinished", JSUtil::JSValueToObject(context, onfinishedValue)); + } + + callback->setCallback("error", errorCallback); + } + TIME_TRACER_ITEM_END("discoverDevices::parameter", 1); + + // perform + BluetoothAdapter::getInstance()->discoverDevices(callback); + TIME_TRACER_ITEM_END(__FUNCTION__, 1); + + return JSValueMakeUndefined(context); + } catch (const BasePlatformException &err) { + return JSWebAPIErrorFactory::postException(context, exception, err); + } catch (...) { + DeviceAPI::Common::UnknownException err("Unknown Error in BluetoothAdapter.discoverDevices()."); + return JSWebAPIErrorFactory::postException(context, exception, err); + } +} + +JSValueRef JSBluetoothAdapter::stopDiscovery(JSContextRef context, + JSObjectRef object, + JSObjectRef thisObject, + size_t argumentCount, + const JSValueRef arguments[], + JSValueRef* exception) +{ + TIME_TRACER_ITEM_BEGIN(__FUNCTION__, 1); + + // Access Check + TIME_TRACER_ITEM_BEGIN("stopDiscovery::ACE", 1); + TIZEN_CHECK_ACCESS(context, exception, BluetoothAdapter::getInstance(), BLUETOOTH_ADAPTER_API_STOP_DISCOVERY); + TIME_TRACER_ITEM_END("stopDiscovery::ACE", 1); + + + try { + // Validate arguments + ArgumentValidator validator(context, argumentCount, arguments); + JSObjectRef successCallback = validator.toFunction(0, true); // successCallback + JSObjectRef errorCallback = validator.toFunction(1, true); // errorCallback + + // perform + MultiCallbackUserDataPtr callback( + new MultiCallbackUserData(GlobalContextManager::getInstance()->getGlobalContext(context))); + if(!callback){ + LoggerW("Can't create MultiCallbackUserData"); + } + else { + callback->setCallback("success", successCallback); + callback->setCallback("error", errorCallback); + } + + BluetoothAdapter::getInstance()->stopDiscovery(callback); + TIME_TRACER_ITEM_END(__FUNCTION__, 1); + + return JSValueMakeUndefined(context); + } catch (const BasePlatformException &err) { + return JSWebAPIErrorFactory::postException(context, exception, err); + } catch (...) { + DeviceAPI::Common::UnknownException err("Unknown Error in BluetoothAdapter.stopDiscovery()."); + return JSWebAPIErrorFactory::postException(context, exception, err); + } +} + +JSValueRef JSBluetoothAdapter::getKnownDevices(JSContextRef context, + JSObjectRef object, + JSObjectRef thisObject, + size_t argumentCount, + const JSValueRef arguments[], + JSValueRef* exception) +{ + TIME_TRACER_ITEM_BEGIN(__FUNCTION__, 1); + + // Access Check + TIME_TRACER_ITEM_BEGIN("getKnownDevices::ACE", 1); + TIZEN_CHECK_ACCESS(context, exception, BluetoothAdapter::getInstance(), BLUETOOTH_ADAPTER_API_GET_KNOWN_DEVICES); + TIME_TRACER_ITEM_END("getKnownDevices::ACE", 1); + + + try { + // Validate arguments + ArgumentValidator validator(context, argumentCount, arguments); + JSObjectRef successCallback = validator.toFunction(0); // successCallback + JSObjectRef errorCallback = validator.toFunction(1, true); // errorCallback + + // perform + MultiCallbackUserDataPtr callback( + new MultiCallbackUserData(GlobalContextManager::getInstance()->getGlobalContext(context))); + if(!callback){ + LoggerW("Can't create MultiCallbackUserData"); + } + else { + callback->setCallback("success", successCallback); + callback->setCallback("error", errorCallback); + } + + BluetoothAdapter::getInstance()->getKnownDevices(callback); + TIME_TRACER_ITEM_END(__FUNCTION__, 1); + + return JSValueMakeUndefined(context); + } catch (const BasePlatformException &err) { + return JSWebAPIErrorFactory::postException(context, exception, err); + } catch (...) { + DeviceAPI::Common::UnknownException err("Unknown Error in BluetoothAdapter.getKnownDevices()."); + return JSWebAPIErrorFactory::postException(context, exception, err); + } +} + +JSValueRef JSBluetoothAdapter::getDevice(JSContextRef context, + JSObjectRef object, + JSObjectRef thisObject, + size_t argumentCount, + const JSValueRef arguments[], + JSValueRef* exception) +{ + TIME_TRACER_ITEM_BEGIN(__FUNCTION__, 1); + + // Access Check + TIME_TRACER_ITEM_BEGIN("getDevice::ACE", 1); + TIZEN_CHECK_ACCESS(context, exception, BluetoothAdapter::getInstance(), BLUETOOTH_ADAPTER_API_GET_DEVICE); + TIME_TRACER_ITEM_END("getDevice::ACE", 1); + + + try { + // Validate arguments + ArgumentValidator validator(context, argumentCount, arguments); + std::string address = validator.toString(0); // address + JSObjectRef successCallback = validator.toFunction(1); // successCallback + JSObjectRef errorCallback = validator.toFunction(2, true); // errorCallback + + // perform + MultiCallbackUserDataPtr callback( + new MultiCallbackUserData(GlobalContextManager::getInstance()->getGlobalContext(context))); + if(!callback){ + LoggerW("Can't create MultiCallbackUserData"); + } + else { + callback->setCallback("success", successCallback); + callback->setCallback("error", errorCallback); + } + + BluetoothAdapter::getInstance()->getDevice(address, callback); + TIME_TRACER_ITEM_END(__FUNCTION__, 1); + + return JSValueMakeUndefined(context); + } catch (const BasePlatformException &err) { + return JSWebAPIErrorFactory::postException(context, exception, err); + } catch (...) { + DeviceAPI::Common::UnknownException err("Unknown Error in BluetoothAdapter.getDevice()."); + return JSWebAPIErrorFactory::postException(context, exception, err); + } +} + +JSValueRef JSBluetoothAdapter::createBonding(JSContextRef context, + JSObjectRef object, + JSObjectRef thisObject, + size_t argumentCount, + const JSValueRef arguments[], + JSValueRef* exception) +{ + TIME_TRACER_ITEM_BEGIN(__FUNCTION__, 1); + + // Access Check + TIME_TRACER_ITEM_BEGIN("createBonding::ACE", 1); + TIZEN_CHECK_ACCESS(context, exception, BluetoothAdapter::getInstance(), BLUETOOTH_ADAPTER_API_CREATE_BONDING); + TIME_TRACER_ITEM_END("createBonding::ACE", 1); + + + try { + // Validate arguments + ArgumentValidator validator(context, argumentCount, arguments); + std::string address = validator.toString(0); // address + JSObjectRef successCallback = validator.toFunction(1); // successCallback + JSObjectRef errorCallback = validator.toFunction(2, true); // errorCallback + + // perform + MultiCallbackUserDataPtr callback( + new MultiCallbackUserData(GlobalContextManager::getInstance()->getGlobalContext(context))); + if(!callback){ + LoggerW("Can't create MultiCallbackUserData"); + } + else { + callback->setCallback("success", successCallback); + callback->setCallback("error", errorCallback); + } + + BluetoothAdapter::getInstance()->createBonding(address, callback); + TIME_TRACER_ITEM_END(__FUNCTION__, 1); + + return JSValueMakeUndefined(context); + } catch (const BasePlatformException &err) { + return JSWebAPIErrorFactory::postException(context, exception, err); + } catch (...) { + DeviceAPI::Common::UnknownException err("Unknown Error in BluetoothAdapter.createBonding()."); + return JSWebAPIErrorFactory::postException(context, exception, err); + } +} + +JSValueRef JSBluetoothAdapter::destroyBonding(JSContextRef context, + JSObjectRef object, + JSObjectRef thisObject, + size_t argumentCount, + const JSValueRef arguments[], + JSValueRef* exception) +{ + TIME_TRACER_ITEM_BEGIN(__FUNCTION__, 1); + + // Access Check + TIME_TRACER_ITEM_BEGIN("destroyBonding::ACE", 1); + TIZEN_CHECK_ACCESS(context, exception, BluetoothAdapter::getInstance(), BLUETOOTH_ADAPTER_API_DESTROY_BONDING); + TIME_TRACER_ITEM_END("destroyBonding::ACE", 1); + + + try { + // Validate arguments + ArgumentValidator validator(context, argumentCount, arguments); + std::string address = validator.toString(0); // address + JSObjectRef successCallback = validator.toFunction(1, true); // successCallback + JSObjectRef errorCallback = validator.toFunction(2, true); // errorCallback + + // perform + MultiCallbackUserDataPtr callback( + new MultiCallbackUserData(GlobalContextManager::getInstance()->getGlobalContext(context))); + if(!callback){ + LoggerW("Can't create MultiCallbackUserData"); + } + else { + callback->setCallback("success", successCallback); + callback->setCallback("error", errorCallback); + } + + BluetoothAdapter::getInstance()->destroyBonding(address, callback); + TIME_TRACER_ITEM_END(__FUNCTION__, 1); + + return JSValueMakeUndefined(context); + } catch (const BasePlatformException &err) { + return JSWebAPIErrorFactory::postException(context, exception, err); + } catch (...) { + DeviceAPI::Common::UnknownException err("Unknown Error in BluetoothAdapter.destroyBonding()."); + return JSWebAPIErrorFactory::postException(context, exception, err); + } +} + +JSValueRef JSBluetoothAdapter::registerRFCOMMServiceByUUID(JSContextRef context, + JSObjectRef object, + JSObjectRef thisObject, + size_t argumentCount, + const JSValueRef arguments[], + JSValueRef* exception) +{ + TIME_TRACER_ITEM_BEGIN(__FUNCTION__, 1); + + // Access Check + TIME_TRACER_ITEM_BEGIN("registerRFCOMMServiceByUUID::ACE", 1); + TIZEN_CHECK_ACCESS(context, exception, BluetoothAdapter::getInstance(), BLUETOOTH_ADAPTER_API_REGISTER_RFCOMMSERVICE_BY_UUID); + TIME_TRACER_ITEM_END("registerRFCOMMServiceByUUID::ACE", 1); + + + try { + // Validate arguments + ArgumentValidator validator(context, argumentCount, arguments); + std::string uuid = validator.toString(0); // uuid + std::string name = validator.toString(1); // name + JSObjectRef successCallback = validator.toFunction(2); // successCallback + JSObjectRef errorCallback = validator.toFunction(3, true); // errorCallback + + // perform + MultiCallbackUserDataPtr callback( + new MultiCallbackUserData(GlobalContextManager::getInstance()->getGlobalContext(context))); + if(!callback){ + LoggerW("Can't create MultiCallbackUserData"); + } + else { + callback->setCallback("success", successCallback); + callback->setCallback("error", errorCallback); + } + + BluetoothAdapter::getInstance()->registerRFCOMMServiceByUUID(uuid, name, callback); + TIME_TRACER_ITEM_END(__FUNCTION__, 1); + + return JSValueMakeUndefined(context); + } catch (const BasePlatformException &err) { + return JSWebAPIErrorFactory::postException(context, exception, err); + } catch (...) { + DeviceAPI::Common::UnknownException err("Unknown Error in BluetoothAdapter.registerRFCOMMServiceByUUID()."); + return JSWebAPIErrorFactory::postException(context, exception, err); + } +} + +JSValueRef JSBluetoothAdapter::getBluetoothProfileHandler(JSContextRef context, + JSObjectRef object, + JSObjectRef thisObject, + size_t argumentCount, + const JSValueRef arguments[], + JSValueRef* exception) +{ + LoggerD("Enter"); + TIME_TRACER_ITEM_BEGIN(__FUNCTION__, 1); + + try { + // Validate arguments + ArgumentValidator validator(context, argumentCount, arguments); + std::string type = validator.toString(0); // profileType + + // perform + JSObjectRef profileHandler; + bool isCorrectParameter = false; + if(type.compare("HEALTH") == 0) { + isCorrectParameter = true; + BluetoothHealthProfileHandler::getInstance()->copyAceCheckAccessFunction(BluetoothAdapter::getInstance()); + + profileHandler = JSBluetoothHealthProfileHandler::createJSObject(context); + } + TIME_TRACER_ITEM_END(__FUNCTION__, 1); + + if(!isCorrectParameter) { + throw TypeMismatchException("Type Mismatch"); + } + + return profileHandler; + } catch (const BasePlatformException &err) { + return JSWebAPIErrorFactory::postException(context, exception, err); + } catch (...) { + DeviceAPI::Common::UnknownException err("Unknown Error in BluetoothAdapter.registerRFCOMMServiceByUUID()."); + return JSWebAPIErrorFactory::postException(context, exception, err); + } +} + +JSValueRef JSBluetoothAdapter::setChangeListener(JSContextRef context, + JSObjectRef object, + JSObjectRef thisObject, + size_t argumentCount, + const JSValueRef arguments[], + JSValueRef* exception) +{ + LoggerD("Enter"); + TIME_TRACER_ITEM_BEGIN(__FUNCTION__, 1); + + try { + // Validate arguments + ArgumentValidator validator(context, argumentCount, arguments); + JSObjectRef changeCallbackObj = validator.toCallbackObject(0, false, "onstatechanged", "onnamechanged", "onvisibilitychanged", NULL); + + MultiCallbackUserDataPtr callback( + new MultiCallbackUserData(GlobalContextManager::getInstance()->getGlobalContext(context))); + if(!callback){ + LoggerW("Can't create MultiMultiCallbackUserData"); + } + else { + // onstatechanged + JSValueRef onstatechangedValue = JSUtil::getProperty(context , changeCallbackObj, "onstatechanged"); + if(!JSValueIsUndefined(context, onstatechangedValue)) { + LoggerD("There is a onstatechanged()"); + callback->setCallback("onstatechanged", JSUtil::JSValueToObject(context, onstatechangedValue)); + } + + // onnamechanged + JSValueRef onnamechangedValue = JSUtil::getProperty(context , changeCallbackObj, "onnamechanged"); + if(!JSValueIsUndefined(context, onnamechangedValue)) { + LoggerD("There is a onnamechanged()"); + callback->setCallback("onnamechanged", JSUtil::JSValueToObject(context, onnamechangedValue)); + } + + // onvisibilitychanged + JSValueRef onvisibilitychangedValue = JSUtil::getProperty(context , changeCallbackObj, "onvisibilitychanged"); + if(!JSValueIsUndefined(context, onvisibilitychangedValue)) { + LoggerD("There is a onvisibilitychanged()"); + callback->setCallback("onvisibilitychanged", JSUtil::JSValueToObject(context, onvisibilitychangedValue)); + } + } + + // perform + BluetoothAdapter::getInstance()->setChangeListener(callback); + TIME_TRACER_ITEM_END(__FUNCTION__, 1); + + return JSValueMakeUndefined(context); + } catch (const BasePlatformException &err) { + return JSWebAPIErrorFactory::postException(context, exception, err); + } catch (...) { + DeviceAPI::Common::UnknownException err("Unknown Error in BluetoothAdapter.registerRFCOMMServiceByUUID()."); + return JSWebAPIErrorFactory::postException(context, exception, err); + } +} + +JSValueRef JSBluetoothAdapter::unsetChangeListener(JSContextRef context, + JSObjectRef object, + JSObjectRef thisObject, + size_t argumentCount, + const JSValueRef arguments[], + JSValueRef* exception) +{ + LoggerD("Enter"); + TIME_TRACER_ITEM_BEGIN(__FUNCTION__, 1); + + try { + // perform + BluetoothAdapter::getInstance()->unsetChangeListener(); + TIME_TRACER_ITEM_END(__FUNCTION__, 1); + + return JSValueMakeUndefined(context); + } catch (const BasePlatformException &err) { + return JSWebAPIErrorFactory::postException(context, exception, err); + } catch (...) { + DeviceAPI::Common::UnknownException err("Unknown Error in BluetoothAdapter.registerRFCOMMServiceByUUID()."); + return JSWebAPIErrorFactory::postException(context, exception, err); + } +} + + +} // Bluetooth +} // DeviceAPI diff --git a/mobile_src/Bluetooth/JSBluetoothAdapter.h b/mobile_src/Bluetooth/JSBluetoothAdapter.h new file mode 100644 index 0000000..5306888 --- /dev/null +++ b/mobile_src/Bluetooth/JSBluetoothAdapter.h @@ -0,0 +1,169 @@ +// +// Tizen Web Device API +// Copyright (c) 2012 Samsung Electronics Co., Ltd. +// +// Licensed under the Apache License, Version 2.0 (the License); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. +// + +#ifndef __TIZEN_JS_BLUETOOTH_ADAPTER_H__ +#define __TIZEN_JS_BLUETOOTH_ADAPTER_H__ + +#include <JavaScriptCore/JavaScript.h> +#include <Security.h> + +namespace DeviceAPI { +namespace Bluetooth { + +class JSBluetoothAdapter +{ +public: + static const JSClassDefinition* getClassInfo(); + static const JSClassRef getClassRef(); + static JSObjectRef createJSObject(JSContextRef context); + +private: + + /** + * The callback invoked when an object is first created. + */ + static void initialize(JSContextRef context, + JSObjectRef object); + + /** + * The callback invoked when an object is finalized. + */ + static void finalize(JSObjectRef object); + + static JSValueRef getProperty(JSContextRef context, + JSObjectRef object, + JSStringRef propertyName, + JSValueRef* exception); + + static JSValueRef setName(JSContextRef context, + JSObjectRef object, + JSObjectRef thisObject, + size_t argumentCount, + const JSValueRef arguments[], + JSValueRef* exception); + + static JSValueRef setPowered(JSContextRef context, + JSObjectRef object, + JSObjectRef thisObject, + size_t argumentCount, + const JSValueRef arguments[], + JSValueRef* exception); + + static JSValueRef setVisible(JSContextRef context, + JSObjectRef object, + JSObjectRef thisObject, + size_t argumentCount, + const JSValueRef arguments[], + JSValueRef* exception); + + static JSValueRef discoverDevices(JSContextRef context, + JSObjectRef object, + JSObjectRef thisObject, + size_t argumentCount, + const JSValueRef arguments[], + JSValueRef* exception); + + static JSValueRef stopDiscovery(JSContextRef context, + JSObjectRef object, + JSObjectRef thisObject, + size_t argumentCount, + const JSValueRef arguments[], + JSValueRef* exception); + + static JSValueRef getKnownDevices(JSContextRef context, + JSObjectRef object, + JSObjectRef thisObject, + size_t argumentCount, + const JSValueRef arguments[], + JSValueRef* exception); + + static JSValueRef getDevice(JSContextRef context, + JSObjectRef object, + JSObjectRef thisObject, + size_t argumentCount, + const JSValueRef arguments[], + JSValueRef* exception); + + static JSValueRef createBonding(JSContextRef context, + JSObjectRef object, + JSObjectRef thisObject, + size_t argumentCount, + const JSValueRef arguments[], + JSValueRef* exception); + + static JSValueRef destroyBonding(JSContextRef context, + JSObjectRef object, + JSObjectRef thisObject, + size_t argumentCount, + const JSValueRef arguments[], + JSValueRef* exception); + + static JSValueRef registerRFCOMMServiceByUUID(JSContextRef context, + JSObjectRef object, + JSObjectRef thisObject, + size_t argumentCount, + const JSValueRef arguments[], + JSValueRef* exception); + + static JSValueRef getBluetoothProfileHandler(JSContextRef context, + JSObjectRef object, + JSObjectRef thisObject, + size_t argumentCount, + const JSValueRef arguments[], + JSValueRef* exception); + + static JSValueRef setChangeListener(JSContextRef context, + JSObjectRef object, + JSObjectRef thisObject, + size_t argumentCount, + const JSValueRef arguments[], + JSValueRef* exception); + + static JSValueRef unsetChangeListener(JSContextRef context, + JSObjectRef object, + JSObjectRef thisObject, + size_t argumentCount, + const JSValueRef arguments[], + JSValueRef* exception); + + /** + * This member variable contains the values which has to be passed + * when the this class is embedded into JS Engine. + */ + static JSClassDefinition m_classInfo; + + /** + * This structure describes a statically declared function property. + */ + static JSStaticFunction m_function[]; + + /** + * This member variable contains the initialization values for the + * properties of this class. The values are given according to + * the data structure JSPropertySpec + */ + static JSStaticValue m_property[]; + + static JSClassRef m_jsClassRef; +}; + + + +} // Bluetooth +} // DeviceAPI + +#endif // __TIZEN_JS_DOWNLOAD_MANAGER_H__ diff --git a/mobile_src/Bluetooth/JSBluetoothClass.cpp b/mobile_src/Bluetooth/JSBluetoothClass.cpp new file mode 100644 index 0000000..22c04ba --- /dev/null +++ b/mobile_src/Bluetooth/JSBluetoothClass.cpp @@ -0,0 +1,174 @@ +// +// Tizen Web Device API +// Copyright (c) 2012 Samsung Electronics Co., Ltd. +// +// Licensed under the Apache License, Version 2.0 (the License); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. +// + +#include <SecurityExceptions.h> + +#include <JSUtil.h> +#include <ArgumentValidator.h> +#include <GlobalContextManager.h> +#include <PlatformException.h> + +#include "plugin_config.h" + +#include "JSBluetoothClass.h" +#include "BluetoothAdapter.h" + +#include <TimeTracer.h> +#include <Logger.h> + +using namespace WrtDeviceApis::Commons; +using namespace DeviceAPI::Common; + +namespace DeviceAPI { +namespace Bluetooth { + +JSClassDefinition JSBluetoothClass::m_classInfo = { + 0, + kJSClassAttributeNone, + "BluetoothClass", + NULL, //ParentClass + m_property, //StaticValues + m_function, //StaticFunctions + initialize, //Initialize + finalize, //Finalize + NULL, //HasProperty, + NULL, //GetProperty, + NULL, //SetProperty, + NULL, //DeleteProperty, + NULL, //GetPropertyNames, + NULL, //CallAsFunction, + NULL, //CallAsConstructor, + NULL, //HasInstance, + NULL //ConvertToType +}; + +JSStaticValue JSBluetoothClass::m_property[] = { + { BLUETOOTH_CLASS_MAJOR, getProperty, NULL, kJSPropertyAttributeNone|kJSPropertyAttributeReadOnly|kJSPropertyAttributeDontDelete }, + { BLUETOOTH_CLASS_MINOR, getProperty, NULL, kJSPropertyAttributeNone|kJSPropertyAttributeReadOnly|kJSPropertyAttributeDontDelete }, + { BLUETOOTH_CLASS_SERVICES, getProperty, NULL, kJSPropertyAttributeNone|kJSPropertyAttributeReadOnly|kJSPropertyAttributeDontDelete }, + { 0, 0, 0, 0 } +}; + +JSStaticFunction JSBluetoothClass::m_function[] = { + { BLUETOOTH_CLASS_API_HAS_SERVICE, hasService, kJSPropertyAttributeNone }, + { 0, 0, 0 } +}; + +JSClassRef JSBluetoothClass::m_jsClassRef = JSClassCreate(JSBluetoothClass::getClassInfo()); + +const JSClassRef JSBluetoothClass::getClassRef() +{ + if (!m_jsClassRef) { + m_jsClassRef = JSClassCreate(&m_classInfo); + } + return m_jsClassRef; +} + +const JSClassDefinition* JSBluetoothClass::getClassInfo() +{ + return &m_classInfo; +} + +JSObjectRef JSBluetoothClass::createJSObject(JSContextRef context, BluetoothClassSharedPtr bluetoothClass) +{ + BluetoothClassHolderPtr holder = new BluetoothClassHolder(bluetoothClass); + return JSObjectMake(context, getClassRef(), static_cast<void*>(holder)); +} + +void JSBluetoothClass::initialize(JSContextRef context, JSObjectRef object) +{ + // do nothing +} + +void JSBluetoothClass::finalize(JSObjectRef object) +{ + BluetoothClassHolderPtr priv = static_cast<BluetoothClassHolderPtr>(JSObjectGetPrivate(object)); + if (priv) { + JSObjectSetPrivate(object, NULL); + delete priv; + } +} + +JSValueRef JSBluetoothClass::getProperty(JSContextRef context, + JSObjectRef object, + JSStringRef propertyName, + JSValueRef* exception) +{ + try { + BluetoothClassHolderPtr priv = static_cast<BluetoothClassHolderPtr>(JSObjectGetPrivate(object)); + if (!priv) { + throw TypeMismatchException("Private object is NULL"); + } + + if (JSStringIsEqualToUTF8CString(propertyName, BLUETOOTH_CLASS_MAJOR)) { + return JSUtil::toJSValueRef(context, priv->mClass->getMajor()); + } + else if (JSStringIsEqualToUTF8CString(propertyName, BLUETOOTH_CLASS_MINOR)) { + return JSUtil::toJSValueRef(context, priv->mClass->getMinor()); + } + else if (JSStringIsEqualToUTF8CString(propertyName, BLUETOOTH_CLASS_SERVICES)) { + return priv->mClass->getServices(context); + } + } catch (const BasePlatformException &err) { + LoggerW("Getting property is failed: " << err.getMessage().c_str()); + } + + return NULL; +} + +JSValueRef JSBluetoothClass::hasService(JSContextRef context, + JSObjectRef object, + JSObjectRef thisObject, + size_t argumentCount, + const JSValueRef arguments[], + JSValueRef* exception) +{ + TIME_TRACER_ITEM_BEGIN(__FUNCTION__, 1); + + try { + // Private Object + BluetoothClassHolderPtr priv = static_cast<BluetoothClassHolderPtr>(JSObjectGetPrivate(thisObject)); + if (!priv) { + throw TypeMismatchException("Private object is NULL."); + } + + // Access Check + BluetoothClass* btclass = priv->mClass.get(); + TIME_TRACER_ITEM_BEGIN("hasService::ACE", 1); + TIZEN_CHECK_ACCESS(context, exception, btclass, BLUETOOTH_CLASS_API_HAS_SERVICE); + TIME_TRACER_ITEM_END("hasService::ACE", 1); + + + ArgumentValidator validator(context, argumentCount, arguments); + unsigned long service = validator.toULong(0); // uuid + + JSValueRef result = JSUtil::toJSValueRef(context, priv->mClass->hasService(service)); + TIME_TRACER_ITEM_END(__FUNCTION__, 1); + + return result; + } catch (const BasePlatformException &err) { + return JSWebAPIErrorFactory::postException(context, exception, err); + } catch (...) { + DeviceAPI::Common::UnknownException err("Unknown Error in BluetoothClass.hasService()."); + return JSWebAPIErrorFactory::postException(context, exception, err); + } +} + + + +} // Bluetooth +} // DeviceAPI diff --git a/mobile_src/Bluetooth/JSBluetoothClass.h b/mobile_src/Bluetooth/JSBluetoothClass.h new file mode 100644 index 0000000..ff5a646 --- /dev/null +++ b/mobile_src/Bluetooth/JSBluetoothClass.h @@ -0,0 +1,93 @@ +// +// Tizen Web Device API +// Copyright (c) 2012 Samsung Electronics Co., Ltd. +// +// Licensed under the Apache License, Version 2.0 (the License); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. +// + +#ifndef __TIZEN_JS_BLUETOOTH_CLASS_H__ +#define __TIZEN_JS_BLUETOOTH_CLASS_H__ + +#include <JavaScriptCore/JavaScript.h> +#include "BluetoothClass.h" + +namespace DeviceAPI { +namespace Bluetooth { + +class BluetoothClassHolder +{ +public: + BluetoothClassHolder(BluetoothClassSharedPtr bluetoothClass) { mClass = bluetoothClass;} + BluetoothClassSharedPtr mClass; +}; +typedef BluetoothClassHolder* BluetoothClassHolderPtr; + +class JSBluetoothClass +{ +public: + static const JSClassDefinition* getClassInfo(); + static const JSClassRef getClassRef(); + static JSObjectRef createJSObject(JSContextRef context, BluetoothClassSharedPtr bluetoothClass); + +private: + + /** + * The callback invoked when an object is first created. + */ + static void initialize(JSContextRef context, + JSObjectRef object); + + /** + * The callback invoked when an object is finalized. + */ + static void finalize(JSObjectRef object); + + static JSValueRef getProperty(JSContextRef context, + JSObjectRef object, + JSStringRef propertyName, + JSValueRef* exception); + + static JSValueRef hasService(JSContextRef context, + JSObjectRef object, + JSObjectRef thisObject, + size_t argumentCount, + const JSValueRef arguments[], + JSValueRef* exception); + + /** + * This member variable contains the values which has to be passed + * when the this class is embedded into JS Engine. + */ + static JSClassDefinition m_classInfo; + + /** + * This structure describes a statically declared function property. + */ + static JSStaticFunction m_function[]; + + /** + * This member variable contains the initialization values for the + * properties of this class. The values are given according to + * the data structure JSPropertySpec + */ + static JSStaticValue m_property[]; + + static JSClassRef m_jsClassRef; +}; + + + +} // Bluetooth +} // DeviceAPI + +#endif // __TIZEN_JS_DOWNLOAD_MANAGER_H__ diff --git a/mobile_src/Bluetooth/JSBluetoothClassDeviceMajor.cpp b/mobile_src/Bluetooth/JSBluetoothClassDeviceMajor.cpp new file mode 100644 index 0000000..2e38412 --- /dev/null +++ b/mobile_src/Bluetooth/JSBluetoothClassDeviceMajor.cpp @@ -0,0 +1,114 @@ +// +// Tizen Web Device API +// Copyright (c) 2012 Samsung Electronics Co., Ltd. +// +// Licensed under the Apache License, Version 2.0 (the License); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. +// + +#include <JSUtil.h> +#include "plugin_config.h" +#include "JSBluetoothClassDeviceMajor.h" +#include "BluetoothClassDeviceMajor.h" + +#include <Logger.h> + +using namespace WrtDeviceApis::Commons; +using namespace DeviceAPI::Common; + +namespace DeviceAPI { +namespace Bluetooth { + +JSClassDefinition JSBluetoothClassDeviceMajor::m_classInfo = { + 0, + kJSClassAttributeNone, + "BluetoothClassDeviceMajor", + NULL, //ParentClass + m_property, //StaticValues + NULL, //StaticFunctions + initialize, //Initialize + finalize, //Finalize + NULL, //HasProperty, + NULL, //GetProperty, + NULL, //SetProperty, + NULL, //DeleteProperty, + NULL, //GetPropertyNames, + NULL, //CallAsFunction, + NULL, //CallAsConstructor, + NULL, //HasInstance, + NULL //ConvertToType +}; + +JSClassRef JSBluetoothClassDeviceMajor::m_jsClassRef = JSClassCreate(JSBluetoothClassDeviceMajor::getClassInfo()); + +JSStaticValue JSBluetoothClassDeviceMajor::m_property[] = { + { "MISC", getProperty, NULL, kJSPropertyAttributeNone|kJSPropertyAttributeReadOnly|kJSPropertyAttributeDontDelete}, + { "COMPUTER", getProperty, NULL, kJSPropertyAttributeNone|kJSPropertyAttributeReadOnly|kJSPropertyAttributeDontDelete}, + { "PHONE", getProperty, NULL, kJSPropertyAttributeNone|kJSPropertyAttributeReadOnly|kJSPropertyAttributeDontDelete}, + { "NETWORK", getProperty, NULL, kJSPropertyAttributeNone|kJSPropertyAttributeReadOnly|kJSPropertyAttributeDontDelete}, + { "AUDIO_VIDEO", getProperty, NULL, kJSPropertyAttributeNone|kJSPropertyAttributeReadOnly|kJSPropertyAttributeDontDelete}, + { "PERIPHERAL", getProperty, NULL, kJSPropertyAttributeNone|kJSPropertyAttributeReadOnly|kJSPropertyAttributeDontDelete}, + { "IMAGING", getProperty, NULL, kJSPropertyAttributeNone|kJSPropertyAttributeReadOnly|kJSPropertyAttributeDontDelete}, + { "WEARABLE", getProperty, NULL, kJSPropertyAttributeNone|kJSPropertyAttributeReadOnly|kJSPropertyAttributeDontDelete}, + { "TOY", getProperty, NULL, kJSPropertyAttributeNone|kJSPropertyAttributeReadOnly|kJSPropertyAttributeDontDelete}, + { "HEALTH", getProperty, NULL, kJSPropertyAttributeNone|kJSPropertyAttributeReadOnly|kJSPropertyAttributeDontDelete}, + { "UNCATEGORIZED", getProperty, NULL, kJSPropertyAttributeNone|kJSPropertyAttributeReadOnly|kJSPropertyAttributeDontDelete}, + { 0, 0, 0, 0 } +}; + + +const JSClassRef JSBluetoothClassDeviceMajor::getClassRef() +{ + if (!m_jsClassRef) { + m_jsClassRef = JSClassCreate(&m_classInfo); + } + return m_jsClassRef; +} + +const JSClassDefinition* JSBluetoothClassDeviceMajor::getClassInfo() +{ + return &m_classInfo; +} + +JSObjectRef JSBluetoothClassDeviceMajor::createJSObject(JSContextRef context) +{ + return JSObjectMake(context, getClassRef(), NULL); +} + +void JSBluetoothClassDeviceMajor::initialize(JSContextRef context, JSObjectRef object) +{ + // Do nothing +} + +void JSBluetoothClassDeviceMajor::finalize(JSObjectRef object) +{ + // Do nothing +} + +JSValueRef JSBluetoothClassDeviceMajor::getProperty(JSContextRef context, + JSObjectRef object, + JSStringRef propertyName, + JSValueRef* exception) +{ + try { + std::string name = JSUtil::JSStringToString(context, propertyName); + return JSUtil::toJSValueRef(context, BluetoothClassDeviceMajor::getInstance()->getMajorValue(name)); + } catch (const BasePlatformException &err) { + LoggerW("Getting property is failed: " << err.getMessage().c_str()); + } + + return NULL; +} + + +} // Bluetooth +} // DeviceAPI diff --git a/mobile_src/Bluetooth/JSBluetoothClassDeviceMajor.h b/mobile_src/Bluetooth/JSBluetoothClassDeviceMajor.h new file mode 100644 index 0000000..b61d84f --- /dev/null +++ b/mobile_src/Bluetooth/JSBluetoothClassDeviceMajor.h @@ -0,0 +1,73 @@ +// +// Tizen Web Device API +// Copyright (c) 2012 Samsung Electronics Co., Ltd. +// +// Licensed under the Apache License, Version 2.0 (the License); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. +// + +#ifndef __TIZEN_JS_BLUETOOTH_CLASS_DEVICE_MAJOR_H__ +#define __TIZEN_JS_BLUETOOTH_CLASS_DEVICE_MAJOR_H__ + +#include <JavaScriptCore/JavaScript.h> + +namespace DeviceAPI { +namespace Bluetooth { + +class JSBluetoothClassDeviceMajor +{ +public: + static const JSClassDefinition* getClassInfo(); + static const JSClassRef getClassRef(); + static JSObjectRef createJSObject(JSContextRef context); + +private: + + /** + * The callback invoked when an object is first created. + */ + static void initialize(JSContextRef context, + JSObjectRef object); + + /** + * The callback invoked when an object is finalized. + */ + static void finalize(JSObjectRef object); + + static JSValueRef getProperty(JSContextRef context, + JSObjectRef object, + JSStringRef propertyName, + JSValueRef* exception); + + /** + * This member variable contains the values which has to be passed + * when the this class is embedded into JS Engine. + */ + static JSClassDefinition m_classInfo; + + + /** + * This member variable contains the initialization values for the + * properties of this class. The values are given according to + * the data structure JSPropertySpec + */ + static JSStaticValue m_property[]; + + static JSClassRef m_jsClassRef; +}; + + + +} // Bluetooth +} // DeviceAPI + +#endif // __TIZEN_JS_DOWNLOAD_MANAGER_H__ diff --git a/mobile_src/Bluetooth/JSBluetoothClassDeviceMinor.cpp b/mobile_src/Bluetooth/JSBluetoothClassDeviceMinor.cpp new file mode 100644 index 0000000..d17729f --- /dev/null +++ b/mobile_src/Bluetooth/JSBluetoothClassDeviceMinor.cpp @@ -0,0 +1,175 @@ +// +// Tizen Web Device API +// Copyright (c) 2012 Samsung Electronics Co., Ltd. +// +// Licensed under the Apache License, Version 2.0 (the License); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. +// + +#include <JSUtil.h> +#include "plugin_config.h" +#include "JSBluetoothClassDeviceMinor.h" +#include "BluetoothClassDeviceMinor.h" + +#include <Logger.h> + +using namespace WrtDeviceApis::Commons; +using namespace DeviceAPI::Common; + +namespace DeviceAPI { +namespace Bluetooth { + +JSClassDefinition JSBluetoothClassDeviceMinor::m_classInfo = { + 0, + kJSClassAttributeNone, + "BluetoothClassDeviceMinor", + NULL, //ParentClass + m_property, //StaticValues + NULL, //StaticFunctions + initialize, //Initialize + finalize, //Finalize + NULL, //HasProperty, + NULL, //GetProperty, + NULL, //SetProperty, + NULL, //DeleteProperty, + NULL, //GetPropertyNames, + NULL, //CallAsFunction, + NULL, //CallAsConstructor, + NULL, //HasInstance, + NULL //ConvertToType +}; + + +JSClassRef JSBluetoothClassDeviceMinor::m_jsClassRef = JSClassCreate(JSBluetoothClassDeviceMinor::getClassInfo()); + +JSStaticValue JSBluetoothClassDeviceMinor::m_property[] = { + {"COMPUTER_UNCATEGORIZED", getProperty, NULL, kJSPropertyAttributeNone|kJSPropertyAttributeReadOnly|kJSPropertyAttributeDontDelete}, + {"COMPUTER_DESKTOP", getProperty, NULL, kJSPropertyAttributeNone|kJSPropertyAttributeReadOnly|kJSPropertyAttributeDontDelete}, + {"COMPUTER_SERVER", getProperty, NULL, kJSPropertyAttributeNone|kJSPropertyAttributeReadOnly|kJSPropertyAttributeDontDelete}, + {"COMPUTER_LAPTOP", getProperty, NULL, kJSPropertyAttributeNone|kJSPropertyAttributeReadOnly|kJSPropertyAttributeDontDelete}, + {"COMPUTER_HANDHELD_PC_OR_PDA", getProperty, NULL, kJSPropertyAttributeNone|kJSPropertyAttributeReadOnly|kJSPropertyAttributeDontDelete}, + {"COMPUTER_PALM_PC_OR_PDA", getProperty, NULL, kJSPropertyAttributeNone|kJSPropertyAttributeReadOnly|kJSPropertyAttributeDontDelete}, + {"COMPUTER_WEARABLE", getProperty, NULL, kJSPropertyAttributeNone|kJSPropertyAttributeReadOnly|kJSPropertyAttributeDontDelete}, + {"PHONE_UNCATEGORIZED", getProperty, NULL, kJSPropertyAttributeNone|kJSPropertyAttributeReadOnly|kJSPropertyAttributeDontDelete}, + {"PHONE_CELLULAR", getProperty, NULL, kJSPropertyAttributeNone|kJSPropertyAttributeReadOnly|kJSPropertyAttributeDontDelete}, + {"PHONE_CORDLESS", getProperty, NULL, kJSPropertyAttributeNone|kJSPropertyAttributeReadOnly|kJSPropertyAttributeDontDelete}, + {"PHONE_SMARTPHONE", getProperty, NULL, kJSPropertyAttributeNone|kJSPropertyAttributeReadOnly|kJSPropertyAttributeDontDelete}, + {"PHONE_MODEM_OR_GATEWAY", getProperty, NULL, kJSPropertyAttributeNone|kJSPropertyAttributeReadOnly|kJSPropertyAttributeDontDelete}, + {"PHONE_ISDN", getProperty, NULL, kJSPropertyAttributeNone|kJSPropertyAttributeReadOnly|kJSPropertyAttributeDontDelete}, + {"AV_UNRECOGNIZED", getProperty, NULL, kJSPropertyAttributeNone|kJSPropertyAttributeReadOnly|kJSPropertyAttributeDontDelete}, + {"AV_WEARABLE_HEADSET", getProperty, NULL, kJSPropertyAttributeNone|kJSPropertyAttributeReadOnly|kJSPropertyAttributeDontDelete}, + {"AV_HANDSFREE", getProperty, NULL, kJSPropertyAttributeNone|kJSPropertyAttributeReadOnly|kJSPropertyAttributeDontDelete}, + {"AV_MICROPHONE", getProperty, NULL, kJSPropertyAttributeNone|kJSPropertyAttributeReadOnly|kJSPropertyAttributeDontDelete}, + {"AV_LOUDSPEAKER", getProperty, NULL, kJSPropertyAttributeNone|kJSPropertyAttributeReadOnly|kJSPropertyAttributeDontDelete}, + {"AV_HEADPHONES", getProperty, NULL, kJSPropertyAttributeNone|kJSPropertyAttributeReadOnly|kJSPropertyAttributeDontDelete}, + {"AV_PORTABLE_AUDIO", getProperty, NULL, kJSPropertyAttributeNone|kJSPropertyAttributeReadOnly|kJSPropertyAttributeDontDelete}, + {"AV_CAR_AUDIO", getProperty, NULL, kJSPropertyAttributeNone|kJSPropertyAttributeReadOnly|kJSPropertyAttributeDontDelete}, + {"AV_SETTOP_BOX", getProperty, NULL, kJSPropertyAttributeNone|kJSPropertyAttributeReadOnly|kJSPropertyAttributeDontDelete}, + {"AV_HIFI", getProperty, NULL, kJSPropertyAttributeNone|kJSPropertyAttributeReadOnly|kJSPropertyAttributeDontDelete}, + {"AV_VCR", getProperty, NULL, kJSPropertyAttributeNone|kJSPropertyAttributeReadOnly|kJSPropertyAttributeDontDelete}, + {"AV_VIDEO_CAMERA", getProperty, NULL, kJSPropertyAttributeNone|kJSPropertyAttributeReadOnly|kJSPropertyAttributeDontDelete}, + {"AV_CAMCORDER", getProperty, NULL, kJSPropertyAttributeNone|kJSPropertyAttributeReadOnly|kJSPropertyAttributeDontDelete}, + {"AV_MONITOR", getProperty, NULL, kJSPropertyAttributeNone|kJSPropertyAttributeReadOnly|kJSPropertyAttributeDontDelete}, + {"AV_DISPLAY_AND_LOUDSPEAKER", getProperty, NULL, kJSPropertyAttributeNone|kJSPropertyAttributeReadOnly|kJSPropertyAttributeDontDelete}, + {"AV_VIDEO_CONFERENCING", getProperty, NULL, kJSPropertyAttributeNone|kJSPropertyAttributeReadOnly|kJSPropertyAttributeDontDelete}, + {"AV_GAMING_TOY", getProperty, NULL, kJSPropertyAttributeNone|kJSPropertyAttributeReadOnly|kJSPropertyAttributeDontDelete}, + {"PERIPHERAL_UNCATEGORIZED", getProperty, NULL, kJSPropertyAttributeNone|kJSPropertyAttributeReadOnly|kJSPropertyAttributeDontDelete}, + {"PERIPHERAL_KEYBOARD", getProperty, NULL, kJSPropertyAttributeNone|kJSPropertyAttributeReadOnly|kJSPropertyAttributeDontDelete}, + {"PERIPHERAL_POINTING_DEVICE", getProperty, NULL, kJSPropertyAttributeNone|kJSPropertyAttributeReadOnly|kJSPropertyAttributeDontDelete}, + {"PERIPHERAL_KEYBOARD_AND_POINTING_DEVICE", getProperty, NULL, kJSPropertyAttributeNone|kJSPropertyAttributeReadOnly|kJSPropertyAttributeDontDelete}, + {"PERIPHERAL_JOYSTICK", getProperty, NULL, kJSPropertyAttributeNone|kJSPropertyAttributeReadOnly|kJSPropertyAttributeDontDelete}, + {"PERIPHERAL_GAMEPAD", getProperty, NULL, kJSPropertyAttributeNone|kJSPropertyAttributeReadOnly|kJSPropertyAttributeDontDelete}, + {"PERIPHERAL_REMOTE_CONTROL", getProperty, NULL, kJSPropertyAttributeNone|kJSPropertyAttributeReadOnly|kJSPropertyAttributeDontDelete}, + {"PERIPHERAL_SENSING_DEVICE", getProperty, NULL, kJSPropertyAttributeNone|kJSPropertyAttributeReadOnly|kJSPropertyAttributeDontDelete}, + {"PERIPHERAL_DEGITIZER_TABLET", getProperty, NULL, kJSPropertyAttributeNone|kJSPropertyAttributeReadOnly|kJSPropertyAttributeDontDelete}, + {"PERIPHERAL_CARD_READER", getProperty, NULL, kJSPropertyAttributeNone|kJSPropertyAttributeReadOnly|kJSPropertyAttributeDontDelete}, + {"PERIPHERAL_DIGITAL_PEN", getProperty, NULL, kJSPropertyAttributeNone|kJSPropertyAttributeReadOnly|kJSPropertyAttributeDontDelete}, + {"PERIPHERAL_HANDHELD_SCANNER", getProperty, NULL, kJSPropertyAttributeNone|kJSPropertyAttributeReadOnly|kJSPropertyAttributeDontDelete}, + {"PERIPHERAL_HANDHELD_INPUT_DEVICE", getProperty, NULL, kJSPropertyAttributeNone|kJSPropertyAttributeReadOnly|kJSPropertyAttributeDontDelete}, + {"IMAGING_UNCATEGORIZED", getProperty, NULL, kJSPropertyAttributeNone|kJSPropertyAttributeReadOnly|kJSPropertyAttributeDontDelete}, + {"IMAGING_DISPLAY", getProperty, NULL, kJSPropertyAttributeNone|kJSPropertyAttributeReadOnly|kJSPropertyAttributeDontDelete}, + {"IMAGING_CAMERA", getProperty, NULL, kJSPropertyAttributeNone|kJSPropertyAttributeReadOnly|kJSPropertyAttributeDontDelete}, + {"IMAGING_SCANNER", getProperty, NULL, kJSPropertyAttributeNone|kJSPropertyAttributeReadOnly|kJSPropertyAttributeDontDelete}, + {"IMAGING_PRINTER", getProperty, NULL, kJSPropertyAttributeNone|kJSPropertyAttributeReadOnly|kJSPropertyAttributeDontDelete}, + {"WEARABLE_WRITST_WATCH", getProperty, NULL, kJSPropertyAttributeNone|kJSPropertyAttributeReadOnly|kJSPropertyAttributeDontDelete}, + {"WEARABLE_PAGER", getProperty, NULL, kJSPropertyAttributeNone|kJSPropertyAttributeReadOnly|kJSPropertyAttributeDontDelete}, + {"WEARABLE_JACKET", getProperty, NULL, kJSPropertyAttributeNone|kJSPropertyAttributeReadOnly|kJSPropertyAttributeDontDelete}, + {"WEARABLE_HELMET", getProperty, NULL, kJSPropertyAttributeNone|kJSPropertyAttributeReadOnly|kJSPropertyAttributeDontDelete}, + {"WEARABLE_GLASSES", getProperty, NULL, kJSPropertyAttributeNone|kJSPropertyAttributeReadOnly|kJSPropertyAttributeDontDelete}, + {"TOY_ROBOT", getProperty, NULL, kJSPropertyAttributeNone|kJSPropertyAttributeReadOnly|kJSPropertyAttributeDontDelete}, + {"TOY_VEHICLE", getProperty, NULL, kJSPropertyAttributeNone|kJSPropertyAttributeReadOnly|kJSPropertyAttributeDontDelete}, + {"TOY_DOLL", getProperty, NULL, kJSPropertyAttributeNone|kJSPropertyAttributeReadOnly|kJSPropertyAttributeDontDelete}, + {"TOY_CONTROLLER", getProperty, NULL, kJSPropertyAttributeNone|kJSPropertyAttributeReadOnly|kJSPropertyAttributeDontDelete}, + {"TOY_GAME", getProperty, NULL, kJSPropertyAttributeNone|kJSPropertyAttributeReadOnly|kJSPropertyAttributeDontDelete}, + {"HEALTH_UNDEFINED", getProperty, NULL, kJSPropertyAttributeNone|kJSPropertyAttributeReadOnly|kJSPropertyAttributeDontDelete}, + {"HEALTH_BLOOD_PRESSURE_MONITOR", getProperty, NULL, kJSPropertyAttributeNone|kJSPropertyAttributeReadOnly|kJSPropertyAttributeDontDelete}, + {"HEALTH_THERMOMETER", getProperty, NULL, kJSPropertyAttributeNone|kJSPropertyAttributeReadOnly|kJSPropertyAttributeDontDelete}, + {"HEALTH_WEIGHING_SCALE", getProperty, NULL, kJSPropertyAttributeNone|kJSPropertyAttributeReadOnly|kJSPropertyAttributeDontDelete}, + {"HEALTH_GLUCOSE_METER", getProperty, NULL, kJSPropertyAttributeNone|kJSPropertyAttributeReadOnly|kJSPropertyAttributeDontDelete}, + {"HEALTH_PULSE_OXIMETER", getProperty, NULL, kJSPropertyAttributeNone|kJSPropertyAttributeReadOnly|kJSPropertyAttributeDontDelete}, + {"HEALTH_PULSE_RATE_MONITOR", getProperty, NULL, kJSPropertyAttributeNone|kJSPropertyAttributeReadOnly|kJSPropertyAttributeDontDelete}, + {"HEALTH_DATA_DISPLAY", getProperty, NULL, kJSPropertyAttributeNone|kJSPropertyAttributeReadOnly|kJSPropertyAttributeDontDelete}, + {"HEALTH_STEP_COUNTER", getProperty, NULL, kJSPropertyAttributeNone|kJSPropertyAttributeReadOnly|kJSPropertyAttributeDontDelete}, + {"HEALTH_BODY_COMPOSITION_ANALYZER", getProperty, NULL, kJSPropertyAttributeNone|kJSPropertyAttributeReadOnly|kJSPropertyAttributeDontDelete}, + {"HEALTH_PEAK_FLOW_MONITOR", getProperty, NULL, kJSPropertyAttributeNone|kJSPropertyAttributeReadOnly|kJSPropertyAttributeDontDelete}, + {"HEALTH_MEDICATION_MONITOR", getProperty, NULL, kJSPropertyAttributeNone|kJSPropertyAttributeReadOnly|kJSPropertyAttributeDontDelete}, + {"HEALTH_KNEE_PROSTHESIS", getProperty, NULL, kJSPropertyAttributeNone|kJSPropertyAttributeReadOnly|kJSPropertyAttributeDontDelete}, + {"HEALTH_ANKLE_PROSTHESIS", getProperty, NULL, kJSPropertyAttributeNone|kJSPropertyAttributeReadOnly|kJSPropertyAttributeDontDelete}, + { 0, 0, 0, 0 } +}; + +const JSClassRef JSBluetoothClassDeviceMinor::getClassRef() +{ + if (!m_jsClassRef) { + m_jsClassRef = JSClassCreate(&m_classInfo); + } + return m_jsClassRef; +} + +const JSClassDefinition* JSBluetoothClassDeviceMinor::getClassInfo() +{ + return &m_classInfo; +} + +JSObjectRef JSBluetoothClassDeviceMinor::createJSObject(JSContextRef context) +{ + return JSObjectMake(context, getClassRef(), NULL); +} + +void JSBluetoothClassDeviceMinor::initialize(JSContextRef context, JSObjectRef object) +{ + // do nothing +} + +void JSBluetoothClassDeviceMinor::finalize(JSObjectRef object) +{ + // do nothing +} + +JSValueRef JSBluetoothClassDeviceMinor::getProperty(JSContextRef context, + JSObjectRef object, + JSStringRef propertyName, + JSValueRef* exception) +{ + try { + std::string name = JSUtil::JSStringToString(context, propertyName); + return JSUtil::toJSValueRef(context, BluetoothClassDeviceMinor::getInstance()->getMinorValue(name)); + } catch (const BasePlatformException &err) { + LoggerW("Getting property is failed: " << err.getMessage().c_str()); + } + + return NULL; +} + + +} // Bluetooth +} // DeviceAPI diff --git a/mobile_src/Bluetooth/JSBluetoothClassDeviceMinor.h b/mobile_src/Bluetooth/JSBluetoothClassDeviceMinor.h new file mode 100644 index 0000000..1877cc5 --- /dev/null +++ b/mobile_src/Bluetooth/JSBluetoothClassDeviceMinor.h @@ -0,0 +1,73 @@ +// +// Tizen Web Device API +// Copyright (c) 2012 Samsung Electronics Co., Ltd. +// +// Licensed under the Apache License, Version 2.0 (the License); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. +// + +#ifndef __TIZEN_JS_BLUETOOTH_CLASS_DEVICE_MINOR_H__ +#define __TIZEN_JS_BLUETOOTH_CLASS_DEVICE_MINOR_H__ + +#include <JavaScriptCore/JavaScript.h> + +namespace DeviceAPI { +namespace Bluetooth { + +class JSBluetoothClassDeviceMinor +{ +public: + static const JSClassDefinition* getClassInfo(); + static const JSClassRef getClassRef(); + static JSObjectRef createJSObject(JSContextRef context); + +private: + + /** + * The callback invoked when an object is first created. + */ + static void initialize(JSContextRef context, + JSObjectRef object); + + /** + * The callback invoked when an object is finalized. + */ + static void finalize(JSObjectRef object); + + static JSValueRef getProperty(JSContextRef context, + JSObjectRef object, + JSStringRef propertyName, + JSValueRef* exception); + + /** + * This member variable contains the values which has to be passed + * when the this class is embedded into JS Engine. + */ + static JSClassDefinition m_classInfo; + + + /** + * This member variable contains the initialization values for the + * properties of this class. The values are given according to + * the data structure JSPropertySpec + */ + static JSStaticValue m_property[]; + + static JSClassRef m_jsClassRef; +}; + + + +} // Bluetooth +} // DeviceAPI + +#endif // __TIZEN_JS_DOWNLOAD_MANAGER_H__ diff --git a/mobile_src/Bluetooth/JSBluetoothClassDeviceService.cpp b/mobile_src/Bluetooth/JSBluetoothClassDeviceService.cpp new file mode 100644 index 0000000..cbf363b --- /dev/null +++ b/mobile_src/Bluetooth/JSBluetoothClassDeviceService.cpp @@ -0,0 +1,113 @@ +// +// Tizen Web Device API +// Copyright (c) 2012 Samsung Electronics Co., Ltd. +// +// Licensed under the Apache License, Version 2.0 (the License); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. +// + +#include <JSUtil.h> +#include "plugin_config.h" +#include "JSBluetoothClassDeviceService.h" +#include "BluetoothClassDeviceService.h" + +#include <Logger.h> + +using namespace WrtDeviceApis::Commons; +using namespace DeviceAPI::Common; + +namespace DeviceAPI { +namespace Bluetooth { + +JSClassDefinition JSBluetoothClassDeviceService::m_classInfo = { + 0, + kJSClassAttributeNone, + "BluetoothClassDeviceService", + NULL, //ParentClass + m_property, //StaticValues + NULL, //StaticFunctions + initialize, //Initialize + finalize, //Finalize + NULL, //HasProperty, + NULL, //GetProperty, + NULL, //SetProperty, + NULL, //DeleteProperty, + NULL, //GetPropertyNames, + NULL, //CallAsFunction, + NULL, //CallAsConstructor, + NULL, //HasInstance, + NULL //ConvertToType +}; + + + +JSClassRef JSBluetoothClassDeviceService::m_jsClassRef = JSClassCreate(JSBluetoothClassDeviceService::getClassInfo()); + +JSStaticValue JSBluetoothClassDeviceService::m_property[] = { + { "LIMITED_DISCOVERABILITY", getProperty, NULL, kJSPropertyAttributeNone|kJSPropertyAttributeReadOnly|kJSPropertyAttributeDontDelete}, + { "POSITIONING", getProperty, NULL, kJSPropertyAttributeNone|kJSPropertyAttributeReadOnly|kJSPropertyAttributeDontDelete}, + { "NETWORKING", getProperty, NULL, kJSPropertyAttributeNone|kJSPropertyAttributeReadOnly|kJSPropertyAttributeDontDelete}, + { "RENDERING", getProperty, NULL, kJSPropertyAttributeNone|kJSPropertyAttributeReadOnly|kJSPropertyAttributeDontDelete}, + { "CAPTURING", getProperty, NULL, kJSPropertyAttributeNone|kJSPropertyAttributeReadOnly|kJSPropertyAttributeDontDelete}, + { "OBJECT_TRANSFER", getProperty, NULL, kJSPropertyAttributeNone|kJSPropertyAttributeReadOnly|kJSPropertyAttributeDontDelete}, + { "AUDIO", getProperty, NULL, kJSPropertyAttributeNone|kJSPropertyAttributeReadOnly|kJSPropertyAttributeDontDelete}, + { "TELEPHONY", getProperty, NULL, kJSPropertyAttributeNone|kJSPropertyAttributeReadOnly|kJSPropertyAttributeDontDelete}, + { "INFORMATION", getProperty, NULL, kJSPropertyAttributeNone|kJSPropertyAttributeReadOnly|kJSPropertyAttributeDontDelete}, + { 0, 0, 0, 0 } +}; + +const JSClassRef JSBluetoothClassDeviceService::getClassRef() +{ + if (!m_jsClassRef) { + m_jsClassRef = JSClassCreate(&m_classInfo); + } + return m_jsClassRef; +} + +const JSClassDefinition* JSBluetoothClassDeviceService::getClassInfo() +{ + return &m_classInfo; +} + +JSObjectRef JSBluetoothClassDeviceService::createJSObject(JSContextRef context) +{ + return JSObjectMake(context, getClassRef(), NULL); +} + +void JSBluetoothClassDeviceService::initialize(JSContextRef context, JSObjectRef object) +{ + // do nothing +} + +void JSBluetoothClassDeviceService::finalize(JSObjectRef object) +{ + // do nothing +} + +JSValueRef JSBluetoothClassDeviceService::getProperty(JSContextRef context, + JSObjectRef object, + JSStringRef propertyName, + JSValueRef* exception) +{ + try { + std::string name = JSUtil::JSStringToString(context, propertyName); + return JSUtil::toJSValueRef(context, BluetoothClassDeviceService::getInstance()->getServiceValue(name)); + } catch (const BasePlatformException &err) { + LoggerW("Getting property is failed: " << err.getMessage().c_str()); + } + + return NULL; +} + + +} // Bluetooth +} // DeviceAPI diff --git a/mobile_src/Bluetooth/JSBluetoothClassDeviceService.h b/mobile_src/Bluetooth/JSBluetoothClassDeviceService.h new file mode 100644 index 0000000..5ae6fce --- /dev/null +++ b/mobile_src/Bluetooth/JSBluetoothClassDeviceService.h @@ -0,0 +1,73 @@ +// +// Tizen Web Device API +// Copyright (c) 2012 Samsung Electronics Co., Ltd. +// +// Licensed under the Apache License, Version 2.0 (the License); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. +// + +#ifndef __TIZEN_JS_BLUETOOTH_CLASS_DEVICE_SERVICE_H__ +#define __TIZEN_JS_BLUETOOTH_CLASS_DEVICE_SERVICE_H__ + +#include <JavaScriptCore/JavaScript.h> + +namespace DeviceAPI { +namespace Bluetooth { + +class JSBluetoothClassDeviceService +{ +public: + static const JSClassDefinition* getClassInfo(); + static const JSClassRef getClassRef(); + static JSObjectRef createJSObject(JSContextRef context); + +private: + + /** + * The callback invoked when an object is first created. + */ + static void initialize(JSContextRef context, + JSObjectRef object); + + /** + * The callback invoked when an object is finalized. + */ + static void finalize(JSObjectRef object); + + static JSValueRef getProperty(JSContextRef context, + JSObjectRef object, + JSStringRef propertyName, + JSValueRef* exception); + + /** + * This member variable contains the values which has to be passed + * when the this class is embedded into JS Engine. + */ + static JSClassDefinition m_classInfo; + + + /** + * This member variable contains the initialization values for the + * properties of this class. The values are given according to + * the data structure JSPropertySpec + */ + static JSStaticValue m_property[]; + + static JSClassRef m_jsClassRef; +}; + + + +} // Bluetooth +} // DeviceAPI + +#endif // __TIZEN_JS_DOWNLOAD_MANAGER_H__ diff --git a/mobile_src/Bluetooth/JSBluetoothDevice.cpp b/mobile_src/Bluetooth/JSBluetoothDevice.cpp new file mode 100644 index 0000000..61e43fb --- /dev/null +++ b/mobile_src/Bluetooth/JSBluetoothDevice.cpp @@ -0,0 +1,208 @@ +// +// Tizen Web Device API +// Copyright (c) 2012 Samsung Electronics Co., Ltd. +// +// Licensed under the Apache License, Version 2.0 (the License); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. +// + +#include <SecurityExceptions.h> + +#include <JSUtil.h> +#include <ArgumentValidator.h> +#include <GlobalContextManager.h> +#include <PlatformException.h> +#include <MultiCallbackUserData.h> + +#include "plugin_config.h" +#include "JSBluetoothDevice.h" +#include "BluetoothDevice.h" +#include "BluetoothAdapter.h" + +#include <TimeTracer.h> +#include <Logger.h> + +using namespace WrtDeviceApis::Commons; +using namespace DeviceAPI::Common; + +namespace DeviceAPI { +namespace Bluetooth { + +JSClassDefinition JSBluetoothDevice::m_classInfo = { + 0, + kJSClassAttributeNone, + "BluetoothDevice", + NULL, //ParentClass + m_property, //StaticValues + m_function, //StaticFunctions + initialize, //Initialize + finalize, //Finalize + NULL, //HasProperty, + NULL, //GetProperty, + NULL, //SetProperty, + NULL, //DeleteProperty, + NULL, //GetPropertyNames, + NULL, //CallAsFunction, + NULL, //CallAsConstructor, + NULL, //HasInstance, + NULL //ConvertToType +}; + +JSStaticValue JSBluetoothDevice::m_property[] = { + { BLUETOOTH_DEVICE_NAME, getProperty, NULL, kJSPropertyAttributeNone|kJSPropertyAttributeReadOnly|kJSPropertyAttributeDontDelete }, + { BLUETOOTH_DEVICE_ADDRESS, getProperty, NULL, kJSPropertyAttributeNone|kJSPropertyAttributeReadOnly|kJSPropertyAttributeDontDelete }, + { BLUETOOTH_DEVICE_DEVICE_CLASS, getProperty, NULL, kJSPropertyAttributeNone|kJSPropertyAttributeReadOnly|kJSPropertyAttributeDontDelete }, + { BLUETOOTH_DEVICE_IS_BONDED, getProperty, NULL, kJSPropertyAttributeNone|kJSPropertyAttributeReadOnly|kJSPropertyAttributeDontDelete }, + { BLUETOOTH_DEVICE_IS_TRUSTED, getProperty, NULL, kJSPropertyAttributeNone|kJSPropertyAttributeReadOnly|kJSPropertyAttributeDontDelete }, + { BLUETOOTH_DEVICE_IS_CONNECTED, getProperty, NULL, kJSPropertyAttributeNone|kJSPropertyAttributeReadOnly|kJSPropertyAttributeDontDelete }, + { BLUETOOTH_DEVICE_UUIDS, getProperty, NULL, kJSPropertyAttributeNone|kJSPropertyAttributeReadOnly|kJSPropertyAttributeDontDelete }, + { 0, 0, 0, 0 } +}; + +JSStaticFunction JSBluetoothDevice::m_function[] = { + { BLUETOOTH_DEVICE_API_CONNECT_TO_SERVICE_BY_UUID, connectToServiceByUUID, kJSPropertyAttributeNone }, + { 0, 0, 0 } +}; + +JSClassRef JSBluetoothDevice::m_jsClassRef = JSClassCreate(JSBluetoothDevice::getClassInfo()); + +const JSClassRef JSBluetoothDevice::getClassRef() +{ + if (!m_jsClassRef) { + m_jsClassRef = JSClassCreate(&m_classInfo); + } + return m_jsClassRef; +} + +const JSClassDefinition* JSBluetoothDevice::getClassInfo() +{ + return &m_classInfo; +} + +JSObjectRef JSBluetoothDevice::createJSObject(JSContextRef context, BluetoothDeviceSharedPtr device) +{ + BluetoothDeviceHolderPtr holder = new BluetoothDeviceHolder(device); + return JSObjectMake(context, getClassRef(), static_cast<void*>(holder)); +} + +BluetoothDeviceSharedPtr JSBluetoothDevice::toBluetoothDevice(JSObjectRef deviceObj) +{ + BluetoothDeviceHolderPtr priv = static_cast<BluetoothDeviceHolderPtr>(JSObjectGetPrivate(deviceObj)); + return priv->mDevice; +} + +void JSBluetoothDevice::initialize(JSContextRef context, JSObjectRef object) +{ + // Do nothing +} + +void JSBluetoothDevice::finalize(JSObjectRef object) +{ + BluetoothDeviceHolderPtr priv = static_cast<BluetoothDeviceHolderPtr>(JSObjectGetPrivate(object)); + if (priv) { + JSObjectSetPrivate(object, NULL); + delete priv; + } +} + +JSValueRef JSBluetoothDevice::getProperty(JSContextRef context, + JSObjectRef object, + JSStringRef propertyName, + JSValueRef* exception) +{ + try { + BluetoothDeviceHolderPtr priv = static_cast<BluetoothDeviceHolderPtr>(JSObjectGetPrivate(object)); + if (!priv) { + throw TypeMismatchException("Private object is NULL"); + } + + if (JSStringIsEqualToUTF8CString(propertyName, BLUETOOTH_DEVICE_NAME)) { + return JSUtil::toJSValueRef(context, priv->mDevice->getName()); + } + else if (JSStringIsEqualToUTF8CString(propertyName, BLUETOOTH_DEVICE_ADDRESS)) { + return JSUtil::toJSValueRef(context, priv->mDevice->getAddress()); + } + else if (JSStringIsEqualToUTF8CString(propertyName, BLUETOOTH_DEVICE_DEVICE_CLASS)) { + return priv->mDevice->getDeviceClass(context); + } + else if (JSStringIsEqualToUTF8CString(propertyName, BLUETOOTH_DEVICE_IS_BONDED)) { + return JSUtil::toJSValueRef(context, priv->mDevice->isBonded()); + } + else if (JSStringIsEqualToUTF8CString(propertyName, BLUETOOTH_DEVICE_IS_TRUSTED)) { + return JSUtil::toJSValueRef(context, priv->mDevice->isTrusted()); + } + else if (JSStringIsEqualToUTF8CString(propertyName, BLUETOOTH_DEVICE_IS_CONNECTED)) { + return JSUtil::toJSValueRef(context, priv->mDevice->isConnected()); + } + else if (JSStringIsEqualToUTF8CString(propertyName, BLUETOOTH_DEVICE_UUIDS)) { + return priv->mDevice->getUUIDs(context); + } + } catch (const BasePlatformException &err) { + LoggerW("Getting property is failed: " << err.getMessage().c_str()); + } + + return NULL; +} + +JSValueRef JSBluetoothDevice::connectToServiceByUUID(JSContextRef context, + JSObjectRef object, + JSObjectRef thisObject, + size_t argumentCount, + const JSValueRef arguments[], + JSValueRef* exception) +{ + TIME_TRACER_ITEM_BEGIN(__FUNCTION__, 1); + + try { + // Private Object + BluetoothDeviceHolderPtr priv = static_cast<BluetoothDeviceHolderPtr>(JSObjectGetPrivate(thisObject)); + if (!priv) { + throw TypeMismatchException("Private object is NULL."); + } + // Access Check + TIME_TRACER_ITEM_BEGIN("connectToServiceByUUID::ACE", 1); + TIZEN_CHECK_ACCESS(context, exception, priv->mDevice.get(), BLUETOOTH_DEVICE_API_CONNECT_TO_SERVICE_BY_UUID); + TIME_TRACER_ITEM_END("connectToServiceByUUID::ACE", 1); + + ArgumentValidator validator(context, argumentCount, arguments); + std::string uuid = validator.toString(0); // uuid + JSObjectRef successCallback = validator.toFunction(1); // successCallback + JSObjectRef errorCallback = validator.toFunction(2, true); // errorCallback + std::string remoteAddress = priv->mDevice->getAddress(); // remote address + + // perform + MultiCallbackUserDataPtr callback( + new MultiCallbackUserData(GlobalContextManager::getInstance()->getGlobalContext(context))); + if(!callback){ + LoggerW("Can't create MultiCallbackUserData"); + } + else { + callback->setCallback("success", successCallback); + callback->setCallback("error", errorCallback); + } + + BluetoothAdapter::getInstance()->connectToServiceByUUID(remoteAddress, uuid, callback); + TIME_TRACER_ITEM_END(__FUNCTION__, 1); + + return JSValueMakeUndefined(context); + } catch (const BasePlatformException &err) { + return JSWebAPIErrorFactory::postException(context, exception, err); + } catch (...) { + DeviceAPI::Common::UnknownException err("Unknown Error in BluetoothDevice.connectToServiceByUUID()."); + return JSWebAPIErrorFactory::postException(context, exception, err); + } +} + + + +} // Bluetooth +} // DeviceAPI diff --git a/mobile_src/Bluetooth/JSBluetoothDevice.h b/mobile_src/Bluetooth/JSBluetoothDevice.h new file mode 100644 index 0000000..cede559 --- /dev/null +++ b/mobile_src/Bluetooth/JSBluetoothDevice.h @@ -0,0 +1,94 @@ +// +// Tizen Web Device API +// Copyright (c) 2012 Samsung Electronics Co., Ltd. +// +// Licensed under the Apache License, Version 2.0 (the License); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. +// + +#ifndef __TIZEN_JS_BLUETOOTH_DEVICE_H__ +#define __TIZEN_JS_BLUETOOTH_DEVICE_H__ + +#include <JavaScriptCore/JavaScript.h> +#include "BluetoothDevice.h" + +namespace DeviceAPI { +namespace Bluetooth { + +class BluetoothDeviceHolder +{ +public: + BluetoothDeviceHolder(BluetoothDeviceSharedPtr device) : mDevice(device) {} + BluetoothDeviceSharedPtr mDevice; +}; +typedef BluetoothDeviceHolder* BluetoothDeviceHolderPtr; + +class JSBluetoothDevice +{ +public: + static const JSClassDefinition* getClassInfo(); + static const JSClassRef getClassRef(); + static JSObjectRef createJSObject(JSContextRef context, BluetoothDeviceSharedPtr device); + static BluetoothDeviceSharedPtr toBluetoothDevice(JSObjectRef deviceObj); + +private: + + /** + * The callback invoked when an object is first created. + */ + static void initialize(JSContextRef context, + JSObjectRef object); + + /** + * The callback invoked when an object is finalized. + */ + static void finalize(JSObjectRef object); + + static JSValueRef getProperty(JSContextRef context, + JSObjectRef object, + JSStringRef propertyName, + JSValueRef* exception); + + static JSValueRef connectToServiceByUUID(JSContextRef context, + JSObjectRef object, + JSObjectRef thisObject, + size_t argumentCount, + const JSValueRef arguments[], + JSValueRef* exception); + + /** + * This member variable contains the values which has to be passed + * when the this class is embedded into JS Engine. + */ + static JSClassDefinition m_classInfo; + + /** + * This structure describes a statically declared function property. + */ + static JSStaticFunction m_function[]; + + /** + * This member variable contains the initialization values for the + * properties of this class. The values are given according to + * the data structure JSPropertySpec + */ + static JSStaticValue m_property[]; + + static JSClassRef m_jsClassRef; +}; + + + +} // Bluetooth +} // DeviceAPI + +#endif // __TIZEN_JS_DOWNLOAD_MANAGER_H__ diff --git a/mobile_src/Bluetooth/JSBluetoothHealthApplication.cpp b/mobile_src/Bluetooth/JSBluetoothHealthApplication.cpp new file mode 100644 index 0000000..f31ad8f --- /dev/null +++ b/mobile_src/Bluetooth/JSBluetoothHealthApplication.cpp @@ -0,0 +1,242 @@ +// +// Tizen Web Device API +// Copyright (c) 2012 Samsung Electronics Co., Ltd. +// +// Licensed under the Apache License, Version 2.0 (the License); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. +// + +#include <SecurityExceptions.h> + +#include <JSUtil.h> +#include <ArgumentValidator.h> +#include <GlobalContextManager.h> +#include <PlatformException.h> +#include <MultiCallbackUserData.h> + +#include "plugin_config.h" +#include "JSBluetoothHealthApplication.h" +#include "BluetoothHealthProfileHandler.h" + +#include <TimeTracer.h> +#include <Logger.h> + +using namespace WrtDeviceApis::Commons; +using namespace DeviceAPI::Common; + +namespace DeviceAPI { +namespace Bluetooth { + +JSClassDefinition JSBluetoothHealthApplication::m_classInfo = { + 0, + kJSClassAttributeNone, + "BluetoothHealthApplication", + NULL, //ParentClass + m_property, //StaticValues + m_function, //StaticFunctions + initialize, //Initialize + finalize, //Finalize + NULL, //HasProperty, + NULL, //GetProperty, + NULL, //SetProperty, + NULL, //DeleteProperty, + NULL, //GetPropertyNames, + NULL, //CallAsFunction, + NULL, //CallAsConstructor, + NULL, //HasInstance, + NULL //ConvertToType +}; + +JSStaticValue JSBluetoothHealthApplication::m_property[] = { + { BLUETOOTH_HEALTH_APPLICATION_DATA_TYPE, getProperty, NULL, kJSPropertyAttributeNone|kJSPropertyAttributeReadOnly|kJSPropertyAttributeDontDelete }, + { BLUETOOTH_HEALTH_APPLICATION_NAME, getProperty, NULL, kJSPropertyAttributeNone|kJSPropertyAttributeReadOnly|kJSPropertyAttributeDontDelete }, + { BLUETOOTH_HEALTH_APPLICATION_ONCONNECT, getProperty, setProperty, kJSPropertyAttributeNone|kJSPropertyAttributeDontDelete }, + { 0, 0, 0, 0 } +}; + +JSStaticFunction JSBluetoothHealthApplication::m_function[] = { + { "unregister", unregister, kJSPropertyAttributeNone }, + { 0, 0, 0 } +}; + +JSClassRef JSBluetoothHealthApplication::m_jsClassRef = JSClassCreate(JSBluetoothHealthApplication::getClassInfo()); + +const JSClassRef JSBluetoothHealthApplication::getClassRef() +{ + if (!m_jsClassRef) { + m_jsClassRef = JSClassCreate(&m_classInfo); + } + return m_jsClassRef; +} + +const JSClassDefinition* JSBluetoothHealthApplication::getClassInfo() +{ + return &m_classInfo; +} + +JSObjectRef JSBluetoothHealthApplication::createJSObject(JSContextRef context, BluetoothHealthApplicationSharedPtr app) +{ + BluetoothHealthApplicationHolderPtr holder = new BluetoothHealthApplicationHolder(app); + return JSObjectMake(context, getClassRef(), static_cast<void*>(holder)); +} + +BluetoothHealthApplicationSharedPtr JSBluetoothHealthApplication::toBluetoothHealthApplication(JSObjectRef appObj) +{ + BluetoothHealthApplicationHolderPtr priv = static_cast<BluetoothHealthApplicationHolderPtr>(JSObjectGetPrivate(appObj)); + return priv->mApp; +} + +void JSBluetoothHealthApplication::initialize(JSContextRef context, JSObjectRef object) +{ + // Do nothing +} + +void JSBluetoothHealthApplication::finalize(JSObjectRef object) +{ + BluetoothHealthApplicationHolderPtr priv = static_cast<BluetoothHealthApplicationHolderPtr>(JSObjectGetPrivate(object)); + if (priv) { + JSObjectSetPrivate(object, NULL); + delete priv; + } +} + +JSValueRef JSBluetoothHealthApplication::getProperty(JSContextRef context, + JSObjectRef object, + JSStringRef propertyName, + JSValueRef* exception) +{ + LoggerD("Enter"); + + try { + BluetoothHealthApplicationHolderPtr priv = static_cast<BluetoothHealthApplicationHolderPtr>(JSObjectGetPrivate(object)); + if (!priv) { + throw TypeMismatchException("Private object is NULL"); + } + + if (JSStringIsEqualToUTF8CString(propertyName, BLUETOOTH_HEALTH_APPLICATION_DATA_TYPE)) { + return JSValueMakeNumber(context, priv->mApp->getDataType()); + } + else if (JSStringIsEqualToUTF8CString(propertyName, BLUETOOTH_HEALTH_APPLICATION_NAME)) { + return JSUtil::toJSValueRef(context, priv->mApp->getName()); + } + else if (JSStringIsEqualToUTF8CString(propertyName, BLUETOOTH_HEALTH_APPLICATION_ONCONNECT)) { + LoggerD("get onconnect"); + return priv->mApp->getOnConnect(context); + } + /* + else if (JSStringIsEqualToUTF8CString(propertyName, BLUETOOTH_HEALTH_APPLICATION_IS_REGISTERED)) { + return JSUtil::toJSValueRef(context, priv->mApp->getRegistrationState()); + } + */ + } catch (const BasePlatformException &err) { + LoggerW("Getting property is failed: " << err.getMessage().c_str()); + } + + return NULL; +} + +bool JSBluetoothHealthApplication::setProperty(JSContextRef context, + JSObjectRef object, + JSStringRef propertyName, + JSValueRef value, + JSValueRef* exception) +{ + LoggerD("Enter"); + + try { + BluetoothHealthApplicationHolderPtr priv = static_cast<BluetoothHealthApplicationHolderPtr>(JSObjectGetPrivate(object)); + if (!priv) { + throw TypeMismatchException("Private object is NULL"); + } + + if (JSStringIsEqualToUTF8CString(propertyName, BLUETOOTH_HEALTH_APPLICATION_ONCONNECT)) { + LoggerD("Set onconnect"); + JSObjectRef onconnectObj = NULL; + if(!JSValueIsNull(context, value)) { + if(!JSValueIsObject(context, value)) { + throw TypeMismatchException("Value is not Object"); + } + + onconnectObj = JSUtil::JSValueToObject(context, value); + + if(!JSObjectIsFunction(context, onconnectObj)) { + throw TypeMismatchException("Not function"); + } + } + else { + LoggerD("onconnect() is NULL"); + } + + return priv->mApp->setOnConnect(context, onconnectObj); + } + } catch (const BasePlatformException &err) { + JSWebAPIErrorFactory::postException(context, exception, err); + } + + return false; +} + + +JSValueRef JSBluetoothHealthApplication::unregister(JSContextRef context, + JSObjectRef object, + JSObjectRef thisObject, + size_t argumentCount, + const JSValueRef arguments[], + JSValueRef* exception) +{ + LoggerD("Enter"); + + TIME_TRACER_ITEM_BEGIN(__FUNCTION__, 1); + + // Access Check + + + try { + BluetoothHealthApplicationHolderPtr priv = static_cast<BluetoothHealthApplicationHolderPtr>(JSObjectGetPrivate(thisObject)); + if (!priv) { + throw TypeMismatchException("Private object is NULL"); + } + TIME_TRACER_ITEM_BEGIN("unregister::ACE", 1); + TIZEN_CHECK_ACCESS(context, exception, priv->mApp.get(), BLUETOOTH_HEALTH_APPLICATION_API_UNREGISTER); + TIME_TRACER_ITEM_END("unregister::ACE", 1); + + ArgumentValidator validator(context, argumentCount, arguments); + JSObjectRef successCallback = validator.toFunction(0, true); // successCallback + JSObjectRef errorCallback = validator.toFunction(1, true); // errorCallback + + // perform + MultiCallbackUserDataPtr callback( + new MultiCallbackUserData(GlobalContextManager::getInstance()->getGlobalContext(context))); + if(!callback){ + LoggerW("Can't create MultiCallbackUserData"); + } + else { + callback->setCallback("success", successCallback); + callback->setCallback("error", errorCallback); + } + + BluetoothHealthProfileHandler::getInstance()->unregisterApp(priv->mApp->getAppID(), callback); + TIME_TRACER_ITEM_END(__FUNCTION__, 1); + + return JSValueMakeUndefined(context); + } catch (const BasePlatformException &err) { + TIME_TRACER_ITEM_END(__FUNCTION__, 1); + return JSWebAPIErrorFactory::postException(context, exception, err); + } catch (...) { + DeviceAPI::Common::UnknownException err("Unknown Error in BluetoothAdapter.setName()."); + return JSWebAPIErrorFactory::postException(context, exception, err); + } +} + + +} // Bluetooth +} // DeviceAPI diff --git a/mobile_src/Bluetooth/JSBluetoothHealthApplication.h b/mobile_src/Bluetooth/JSBluetoothHealthApplication.h new file mode 100644 index 0000000..375e738 --- /dev/null +++ b/mobile_src/Bluetooth/JSBluetoothHealthApplication.h @@ -0,0 +1,99 @@ +// +// Tizen Web Device API +// Copyright (c) 2012 Samsung Electronics Co., Ltd. +// +// Licensed under the Apache License, Version 2.0 (the License); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. +// + +#ifndef __TIZEN_JS_BLUETOOTH_HEALTH_APPLICATION_H__ +#define __TIZEN_JS_BLUETOOTH_HEALTH_APPLICATION_H__ + +#include <JavaScriptCore/JavaScript.h> +#include "BluetoothHealthApplication.h" + +namespace DeviceAPI { +namespace Bluetooth { + +class BluetoothHealthApplicationHolder +{ +public: + BluetoothHealthApplicationHolder(BluetoothHealthApplicationSharedPtr app){mApp = app;} + BluetoothHealthApplicationSharedPtr mApp; +}; +typedef BluetoothHealthApplicationHolder* BluetoothHealthApplicationHolderPtr; + +class JSBluetoothHealthApplication +{ +public: + static const JSClassDefinition* getClassInfo(); + static const JSClassRef getClassRef(); + static JSObjectRef createJSObject(JSContextRef context, BluetoothHealthApplicationSharedPtr app); + static BluetoothHealthApplicationSharedPtr toBluetoothHealthApplication(JSObjectRef appObj); + +private: + + /** + * The callback invoked when an object is first created. + */ + static void initialize(JSContextRef context, + JSObjectRef object); + + /** + * The callback invoked when an object is finalized. + */ + static void finalize(JSObjectRef object); + + static JSValueRef getProperty(JSContextRef context, + JSObjectRef object, + JSStringRef propertyName, + JSValueRef* exception); + + static bool setProperty(JSContextRef context, + JSObjectRef object, + JSStringRef propertyName, + JSValueRef value, + JSValueRef* exception); + + static JSValueRef unregister(JSContextRef context, + JSObjectRef object, + JSObjectRef thisObject, + size_t argumentCount, + const JSValueRef arguments[], + JSValueRef* exception); + + /** + * This member variable contains the values which has to be passed + * when the this class is embedded into JS Engine. + */ + static JSClassDefinition m_classInfo; + + /** + * This structure describes a statically declared function property. + */ + static JSStaticFunction m_function[]; + + /** + * This member variable contains the initialization values for the + * properties of this class. The values are given according to + * the data structure JSPropertySpec + */ + static JSStaticValue m_property[]; + + static JSClassRef m_jsClassRef; +}; + + +} // Bluetooth +} // DeviceAPI + +#endif // __TIZEN_JS_BLUETOOTH_HEALTH_APPLICATION_H__ diff --git a/mobile_src/Bluetooth/JSBluetoothHealthChannel.cpp b/mobile_src/Bluetooth/JSBluetoothHealthChannel.cpp new file mode 100644 index 0000000..3ee4bf3 --- /dev/null +++ b/mobile_src/Bluetooth/JSBluetoothHealthChannel.cpp @@ -0,0 +1,325 @@ +// +// Tizen Web Device API +// Copyright (c) 2012 Samsung Electronics Co., Ltd. +// +// Licensed under the Apache License, Version 2.0 (the License); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. +// + +#include <vector> + +#include <SecurityExceptions.h> +#include <JSUtil.h> +#include <ArgumentValidator.h> +#include <GlobalContextManager.h> +#include <PlatformException.h> +#include <MultiCallbackUserData.h> + +#include "plugin_config.h" +#include "JSBluetoothHealthChannel.h" +#include "BluetoothHealthProfileHandler.h" + +#include <TimeTracer.h> +#include <Logger.h> + +using namespace WrtDeviceApis::Commons; +using namespace DeviceAPI::Common; + +namespace DeviceAPI { +namespace Bluetooth { + +JSClassDefinition JSBluetoothHealthChannel::m_classInfo = { + 0, + kJSClassAttributeNone, + "BluetoothHealthChannel", + NULL, //ParentClass + m_property, //StaticValues + m_function, //StaticFunctions + initialize, //Initialize + finalize, //Finalize + NULL, //HasProperty, + NULL, //GetProperty, + NULL, //SetProperty, + NULL, //DeleteProperty, + NULL, //GetPropertyNames, + NULL, //CallAsFunction, + NULL, //CallAsConstructor, + NULL, //HasInstance, + NULL //ConvertToType +}; + +JSStaticValue JSBluetoothHealthChannel::m_property[] = { + { BLUETOOTH_HEALTH_CHANNEL_PEER, getProperty, NULL, kJSPropertyAttributeNone|kJSPropertyAttributeReadOnly|kJSPropertyAttributeDontDelete }, + { BLUETOOTH_HEALTH_CHANNEL_TYPE, getProperty, NULL, kJSPropertyAttributeNone|kJSPropertyAttributeReadOnly|kJSPropertyAttributeDontDelete }, + { BLUETOOTH_HEALTH_CHANNEL_APP, getProperty, NULL, kJSPropertyAttributeNone|kJSPropertyAttributeReadOnly|kJSPropertyAttributeDontDelete }, + { BLUETOOTH_HEALTH_CHANNEL_IS_CONNECTED, getProperty, NULL, kJSPropertyAttributeNone|kJSPropertyAttributeDontDelete }, + { 0, 0, 0, 0 } +}; + +JSStaticFunction JSBluetoothHealthChannel::m_function[] = { + { BLUETOOTH_HEALTH_CHANNEL_API_SEND_DATA, sendData, kJSPropertyAttributeNone }, + { "close", close, kJSPropertyAttributeNone }, + { BLUETOOTH_HEALTH_CHANNEL_API_SET_LISTENER, setListener, kJSPropertyAttributeNone }, + { BLUETOOTH_HEALTH_CHANNEL_API_UNSET_LISTENER, unsetListener, kJSPropertyAttributeNone }, + { 0, 0, 0 } +}; + +JSClassRef JSBluetoothHealthChannel::m_jsClassRef = JSClassCreate(JSBluetoothHealthChannel::getClassInfo()); + +const JSClassRef JSBluetoothHealthChannel::getClassRef() +{ + if (!m_jsClassRef) { + m_jsClassRef = JSClassCreate(&m_classInfo); + } + return m_jsClassRef; +} + +const JSClassDefinition* JSBluetoothHealthChannel::getClassInfo() +{ + return &m_classInfo; +} + +JSObjectRef JSBluetoothHealthChannel::createJSObject(JSContextRef context, BluetoothHealthChannelPtr channel) +{ + return JSObjectMake(context, getClassRef(), static_cast<void*>(channel)); +} + +void JSBluetoothHealthChannel::initialize(JSContextRef context, JSObjectRef object) +{ + // do nothing +} + +void JSBluetoothHealthChannel::finalize(JSObjectRef object) +{ + BluetoothHealthChannelPtr priv = static_cast<BluetoothHealthChannelPtr>(JSObjectGetPrivate(object)); + if (priv) { + JSObjectSetPrivate(object, NULL); + delete priv; + } +} + +JSValueRef JSBluetoothHealthChannel::getProperty(JSContextRef context, + JSObjectRef object, + JSStringRef propertyName, + JSValueRef* exception) +{ + try { + BluetoothHealthChannelPtr priv = static_cast<BluetoothHealthChannelPtr>(JSObjectGetPrivate(object)); + if (!priv) { + throw TypeMismatchException("Private object is NULL"); + } + + if (JSStringIsEqualToUTF8CString(propertyName, BLUETOOTH_HEALTH_CHANNEL_PEER)) { + return priv->getPeer(context); + } + else if (JSStringIsEqualToUTF8CString(propertyName, BLUETOOTH_HEALTH_CHANNEL_TYPE)) { + return JSUtil::toJSValueRef(context, priv->getChannelTypeStr()); + } + else if (JSStringIsEqualToUTF8CString(propertyName, BLUETOOTH_HEALTH_CHANNEL_APP)) { + return priv->getApp(context); + } + else if (JSStringIsEqualToUTF8CString(propertyName, BLUETOOTH_HEALTH_CHANNEL_IS_CONNECTED)) { + return JSUtil::toJSValueRef(context, priv->getConnectionState()); + } + } catch (const BasePlatformException &err) { + LoggerW("Getting property is failed: " << err.getMessage().c_str()); + } + + return NULL; +} + +JSValueRef JSBluetoothHealthChannel::sendData(JSContextRef context, + JSObjectRef object, + JSObjectRef thisObject, + size_t argumentCount, + const JSValueRef arguments[], + JSValueRef* exception) +{ + LoggerD("Enter"); + + TIME_TRACER_ITEM_BEGIN(__FUNCTION__, 1);; + + try { + // Private Object + BluetoothHealthChannelPtr priv = static_cast<BluetoothHealthChannelPtr>(JSObjectGetPrivate(thisObject)); + if (!priv) { + throw DeviceAPI::Common::UnknownException("Private object is NULL."); + } + // Access Check + TIME_TRACER_ITEM_BEGIN("sendData::ACE", 1);; + TIZEN_CHECK_ACCESS(context, exception, priv, BLUETOOTH_HEALTH_CHANNEL_API_SEND_DATA); + TIME_TRACER_ITEM_END("sendData::ACE", 1);; + + ArgumentValidator validator(context, argumentCount, arguments); + + JSObjectRef dataArrayObj = validator.toArrayObject(0); // data + size_t size = JSGetArrayLength(context, dataArrayObj); + char *buffer = new char[size]; + for(size_t i = 0; i < size; ++i) { + JSValueRef element = JSGetArrayElement(context, dataArrayObj, i); + buffer[i] = static_cast<char>(JSUtil::JSValueToByte(context, element)); + } + + JSValueRef result = JSUtil::toJSValueRef(context, priv->sendData(buffer, size)); + delete buffer; + TIME_TRACER_ITEM_END(__FUNCTION__, 1);; + + return result; + } catch (const BasePlatformException &err) { + return JSWebAPIErrorFactory::postException(context, exception, err); + } catch (...) { + DeviceAPI::Common::UnknownException err("Unknown Error"); + return JSWebAPIErrorFactory::postException(context, exception, err); + } +} + +JSValueRef JSBluetoothHealthChannel::close(JSContextRef context, + JSObjectRef object, + JSObjectRef thisObject, + size_t argumentCount, + const JSValueRef arguments[], + JSValueRef* exception) +{ + LoggerD("Enter"); + + TIME_TRACER_ITEM_BEGIN(__FUNCTION__, 1); + + + + try { + // Private Object + BluetoothHealthChannelPtr priv = static_cast<BluetoothHealthChannelPtr>(JSObjectGetPrivate(thisObject)); + if (!priv) { + throw DeviceAPI::Common::UnknownException("Private object is NULL."); + } + // Access Check + TIME_TRACER_ITEM_BEGIN("close::ACE", 1);; + TIZEN_CHECK_ACCESS(context, exception, priv, BLUETOOTH_HEALTH_CHANNEL_API_CLOSE); + TIME_TRACER_ITEM_END("close::ACE", 1);; + + priv->close(); + TIME_TRACER_ITEM_END(__FUNCTION__, 1);; + + return JSValueMakeUndefined(context); + } catch (const BasePlatformException &err) { + return JSWebAPIErrorFactory::postException(context, exception, err); + } catch (...) { + DeviceAPI::Common::UnknownException err("Unknown Error"); + return JSWebAPIErrorFactory::postException(context, exception, err); + } +} + +JSValueRef JSBluetoothHealthChannel::setListener(JSContextRef context, + JSObjectRef object, + JSObjectRef thisObject, + size_t argumentCount, + const JSValueRef arguments[], + JSValueRef* exception) +{ + LoggerD("Enter"); + + TIME_TRACER_ITEM_BEGIN(__FUNCTION__, 1);; + + + + try { + // Private Object + BluetoothHealthChannelPtr priv = static_cast<BluetoothHealthChannelPtr>(JSObjectGetPrivate(thisObject)); + if (!priv) { + throw DeviceAPI::Common::UnknownException("Private object is NULL."); + } + + // Access Check + TIME_TRACER_ITEM_BEGIN("setListener::ACE", 1);; + TIZEN_CHECK_ACCESS(context, exception, priv, BLUETOOTH_HEALTH_CHANNEL_API_SET_LISTENER); + TIME_TRACER_ITEM_END("setListener::ACE", 1);; + + + // Validate arguments + ArgumentValidator validator(context, argumentCount, arguments); + + // successCallback + JSObjectRef successCallback = validator.toCallbackObject(0, false, "onmessage", "onclose", NULL); + + MultiCallbackUserDataPtr callback( + new MultiCallbackUserData(GlobalContextManager::getInstance()->getGlobalContext(context))); + if(!callback){ + LoggerW("Can't create MultiMultiCallbackUserData"); + } + else { + // onmessage + JSValueRef onmessageValue = JSUtil::getProperty(context , successCallback, "onmessage"); + if(!JSValueIsUndefined(context, onmessageValue)) { + LoggerD("There is a onmessage()"); + callback->setCallback("onmessage", JSUtil::JSValueToObject(context, onmessageValue)); + } + + // onclose + JSValueRef oncloseValue = JSUtil::getProperty(context , successCallback, "onclose"); + if(!JSValueIsUndefined(context, oncloseValue)) { + LoggerD("There is a onclose()"); + callback->setCallback("onclose", JSUtil::JSValueToObject(context, oncloseValue)); + } + } + + priv->setListener(callback); + TIME_TRACER_ITEM_END(__FUNCTION__, 1);; + + return JSValueMakeUndefined(context); + } catch (const BasePlatformException &err) { + return JSWebAPIErrorFactory::postException(context, exception, err); + } catch (...) { + DeviceAPI::Common::UnknownException err("Unknown Error"); + return JSWebAPIErrorFactory::postException(context, exception, err); + } +} + +JSValueRef JSBluetoothHealthChannel::unsetListener(JSContextRef context, + JSObjectRef object, + JSObjectRef thisObject, + size_t argumentCount, + const JSValueRef arguments[], + JSValueRef* exception) +{ + LoggerD("Enter"); + + TIME_TRACER_ITEM_BEGIN(__FUNCTION__, 1);; + + + try { + // Private Object + BluetoothHealthChannelPtr priv = static_cast<BluetoothHealthChannelPtr>(JSObjectGetPrivate(thisObject)); + if (!priv) { + throw DeviceAPI::Common::UnknownException("Private object is NULL."); + } + // Access Check + TIME_TRACER_ITEM_BEGIN("unsetListener::ACE", 1); + TIZEN_CHECK_ACCESS(context, exception, priv, BLUETOOTH_HEALTH_CHANNEL_API_UNSET_LISTENER); + TIME_TRACER_ITEM_END("unsetListener::ACE", 1); + + + priv->unsetListener(); + TIME_TRACER_ITEM_END(__FUNCTION__, 1);; + + return JSValueMakeUndefined(context); + } catch (const BasePlatformException &err) { + return JSWebAPIErrorFactory::postException(context, exception, err); + } catch (...) { + DeviceAPI::Common::UnknownException err("Unknown Error"); + return JSWebAPIErrorFactory::postException(context, exception, err); + } +} + + + +} // Bluetooth +} // DeviceAPI diff --git a/mobile_src/Bluetooth/JSBluetoothHealthChannel.h b/mobile_src/Bluetooth/JSBluetoothHealthChannel.h new file mode 100644 index 0000000..3e35488 --- /dev/null +++ b/mobile_src/Bluetooth/JSBluetoothHealthChannel.h @@ -0,0 +1,106 @@ +// +// Tizen Web Device API +// Copyright (c) 2012 Samsung Electronics Co., Ltd. +// +// Licensed under the Apache License, Version 2.0 (the License); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. +// + +#ifndef __TIZEN_JS_BLUETOOTH_HEALTH_CHANNEL_H__ +#define __TIZEN_JS_BLUETOOTH_HEALTH_CHANNEL_H__ + +#include <JavaScriptCore/JavaScript.h> + +#include "BluetoothHealthChannel.h" + +namespace DeviceAPI { +namespace Bluetooth { + +class JSBluetoothHealthChannel +{ +public: + static const JSClassDefinition* getClassInfo(); + static const JSClassRef getClassRef(); + static JSObjectRef createJSObject(JSContextRef context, BluetoothHealthChannelPtr channel); + +private: + + /** + * The callback invoked when an object is first created. + */ + static void initialize(JSContextRef context, + JSObjectRef object); + + /** + * The callback invoked when an object is finalized. + */ + static void finalize(JSObjectRef object); + + static JSValueRef getProperty(JSContextRef context, + JSObjectRef object, + JSStringRef propertyName, + JSValueRef* exception); + + static JSValueRef close(JSContextRef context, + JSObjectRef object, + JSObjectRef thisObject, + size_t argumentCount, + const JSValueRef arguments[], + JSValueRef* exception); + + static JSValueRef sendData(JSContextRef context, + JSObjectRef object, + JSObjectRef thisObject, + size_t argumentCount, + const JSValueRef arguments[], + JSValueRef* exception); + + static JSValueRef setListener(JSContextRef context, + JSObjectRef object, + JSObjectRef thisObject, + size_t argumentCount, + const JSValueRef arguments[], + JSValueRef* exception); + + static JSValueRef unsetListener(JSContextRef context, + JSObjectRef object, + JSObjectRef thisObject, + size_t argumentCount, + const JSValueRef arguments[], + JSValueRef* exception); + + /** + * This member variable contains the values which has to be passed + * when the this class is embedded into JS Engine. + */ + static JSClassDefinition m_classInfo; + + /** + * This structure describes a statically declared function property. + */ + static JSStaticFunction m_function[]; + + /** + * This member variable contains the initialization values for the + * properties of this class. The values are given according to + * the data structure JSPropertySpec + */ + static JSStaticValue m_property[]; + + static JSClassRef m_jsClassRef; +}; + + +} // Bluetooth +} // DeviceAPI + +#endif // __TIZEN_JS_BLUETOOTH_HEALTH_CHANNEL_H__ diff --git a/mobile_src/Bluetooth/JSBluetoothHealthProfileHandler.cpp b/mobile_src/Bluetooth/JSBluetoothHealthProfileHandler.cpp new file mode 100644 index 0000000..343ee45 --- /dev/null +++ b/mobile_src/Bluetooth/JSBluetoothHealthProfileHandler.cpp @@ -0,0 +1,202 @@ +// +// Tizen Web Device API +// Copyright (c) 2012 Samsung Electronics Co., Ltd. +// +// Licensed under the Apache License, Version 2.0 (the License); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. +// + +#include <SecurityExceptions.h> + +#include <JSUtil.h> +#include <ArgumentValidator.h> +#include <GlobalContextManager.h> + +#include "plugin_config.h" +#include "JSBluetoothHealthProfileHandler.h" +#include "JSBluetoothProfileHandler.h" +#include "JSBluetoothHealthApplication.h" +#include "JSBluetoothDevice.h" +#include "BluetoothHealthProfileHandler.h" + +#include <TimeTracer.h> +#include <Logger.h> + +using namespace WrtDeviceApis::Commons; +using namespace DeviceAPI::Common; + +namespace DeviceAPI { +namespace Bluetooth { + +JSClassDefinition JSBluetoothHealthProfileHandler::m_classInfo = { + 0, + kJSClassAttributeNone, + "BluetoothHealthProfileHandler", + JSBluetoothProfileHandler::getClassRef(), //ParentClass + NULL, //StaticValues + m_function, //StaticFunctions + initialize, //Initialize + finalize, //Finalize + NULL, //HasProperty, + NULL, //GetProperty, + NULL, //SetProperty, + NULL, //DeleteProperty, + NULL, //GetPropertyNames, + NULL, //CallAsFunction, + NULL, //CallAsConstructor, + NULL, //HasInstance, + NULL //ConvertToType +}; + +JSStaticFunction JSBluetoothHealthProfileHandler::m_function[] = { + { BLUETOOTH_HEALTH_PROFILE_HANDLER_API_REGISTER_SINK_APPLICATION, registerSinkApplication, kJSPropertyAttributeNone }, + { BLUETOOTH_HEALTH_PROFILE_HANDLER_API_CONNECT_TO_SOURCE, connectToSource, kJSPropertyAttributeNone }, + { 0, 0, 0 } +}; + +JSClassRef JSBluetoothHealthProfileHandler::m_jsClassRef = JSClassCreate(JSBluetoothHealthProfileHandler::getClassInfo()); + +const JSClassRef JSBluetoothHealthProfileHandler::getClassRef() +{ + if (!m_jsClassRef) { + m_jsClassRef = JSClassCreate(&m_classInfo); + } + return m_jsClassRef; +} + +const JSClassDefinition* JSBluetoothHealthProfileHandler::getClassInfo() +{ + return &m_classInfo; +} + +JSObjectRef JSBluetoothHealthProfileHandler::createJSObject(JSContextRef context) +{ + return JSObjectMake(context, getClassRef(), NULL); +} + +void JSBluetoothHealthProfileHandler::initialize(JSContextRef context, JSObjectRef object) +{ + // do nothing +} + + +void JSBluetoothHealthProfileHandler::finalize(JSObjectRef object) +{ + // do nothing +} + +JSValueRef JSBluetoothHealthProfileHandler::registerSinkApplication(JSContextRef context, + JSObjectRef object, + JSObjectRef thisObject, + size_t argumentCount, + const JSValueRef arguments[], + JSValueRef* exception) +{ + LoggerD("Enter"); + + TIME_TRACER_ITEM_BEGIN(__FUNCTION__, 1); + // Access Check + TIME_TRACER_ITEM_BEGIN("registerSinkApplication::ACE", 1); + TIZEN_CHECK_ACCESS(context, exception, BluetoothHealthProfileHandler::getInstance(), BLUETOOTH_HEALTH_PROFILE_HANDLER_API_REGISTER_SINK_APPLICATION); + TIME_TRACER_ITEM_END("registerSinkApplication::ACE", 1); + + + try { + ArgumentValidator validator(context, argumentCount, arguments); + unsigned short dataType = static_cast<unsigned short>(validator.toULong(0)); // dataType + std::string name = validator.toString(1); // name + JSObjectRef successCallback = validator.toFunction(2); // successCallback + JSObjectRef errorCallback = validator.toFunction(3, true); // errorCallback + + // perform + MultiCallbackUserDataPtr callback( + new MultiCallbackUserData(GlobalContextManager::getInstance()->getGlobalContext(context))); + if(!callback){ + LoggerW("Can't create MultiCallbackUserData"); + } + else { + callback->setCallback("success", successCallback); + callback->setCallback("error", errorCallback); + } + + BluetoothHealthProfileHandler::getInstance()->registerSinkApp(dataType, name, callback); + TIME_TRACER_ITEM_END(__FUNCTION__, 1); + + return JSValueMakeUndefined(context); + } catch (const BasePlatformException &err) { + TIME_TRACER_ITEM_END(__FUNCTION__, 1); + return JSWebAPIErrorFactory::postException(context, exception, err); + } catch (...) { + DeviceAPI::Common::UnknownException err("Unknown Error in BluetoothAdapter.setName()."); + return JSWebAPIErrorFactory::postException(context, exception, err); + } +} + +JSValueRef JSBluetoothHealthProfileHandler::connectToSource(JSContextRef context, + JSObjectRef object, + JSObjectRef thisObject, + size_t argumentCount, + const JSValueRef arguments[], + JSValueRef* exception) +{ + LoggerD("Enter"); + + TIME_TRACER_ITEM_BEGIN(__FUNCTION__, 1); + + // Access Check + TIME_TRACER_ITEM_BEGIN("connectToSource::ACE", 1); + TIZEN_CHECK_ACCESS(context, exception, BluetoothHealthProfileHandler::getInstance(), BLUETOOTH_HEALTH_PROFILE_HANDLER_API_CONNECT_TO_SOURCE); + TIME_TRACER_ITEM_END("connectToSource::ACE", 1); + + + try { + ArgumentValidator validator(context, argumentCount, arguments); + JSObjectRef remoteObj = validator.toObject(0); // remoteDevice + if(!JSValueIsObjectOfClass(context, remoteObj, JSBluetoothDevice::getClassRef())) { + throw TypeMismatchException("remoteDevice is not a BluetoothDevice object"); + } + + JSObjectRef appObj = validator.toObject(1); // application + if(!JSValueIsObjectOfClass(context, appObj, JSBluetoothHealthApplication::getClassRef())) { + throw TypeMismatchException("application is not a BluetoothHealthApplication object"); + } + + JSObjectRef successCallback = validator.toFunction(2); // successCallback + JSObjectRef errorCallback = validator.toFunction(3, true); // errorCallback + + // perform + MultiCallbackUserDataPtr callback( + new MultiCallbackUserData(GlobalContextManager::getInstance()->getGlobalContext(context))); + if(!callback){ + LoggerW("Can't create MultiCallbackUserData"); + } + else { + callback->setCallback("success", successCallback); + callback->setCallback("error", errorCallback); + } + + BluetoothHealthProfileHandler::getInstance()->connectToSource(remoteObj, appObj, callback); + TIME_TRACER_ITEM_END(__FUNCTION__, 1); + + return JSValueMakeUndefined(context); + } catch (const BasePlatformException &err) { + TIME_TRACER_ITEM_END(__FUNCTION__, 1); + return JSWebAPIErrorFactory::postException(context, exception, err); + } catch (...) { + DeviceAPI::Common::UnknownException err("Unknown Error in BluetoothAdapter.setName()."); + return JSWebAPIErrorFactory::postException(context, exception, err); + } +} + + +} // Bluetooth +} // DeviceAPI diff --git a/mobile_src/Bluetooth/JSBluetoothHealthProfileHandler.h b/mobile_src/Bluetooth/JSBluetoothHealthProfileHandler.h new file mode 100644 index 0000000..07b1792 --- /dev/null +++ b/mobile_src/Bluetooth/JSBluetoothHealthProfileHandler.h @@ -0,0 +1,78 @@ +// +// Tizen Web Device API +// Copyright (c) 2012 Samsung Electronics Co., Ltd. +// +// Licensed under the Apache License, Version 2.0 (the License); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. +// + +#ifndef __TIZEN_JS_HEALTH_PROFILE_HANDLER_H__ +#define __TIZEN_JS_HEALTH_PROFILE_HANDLER_H__ + +#include <JavaScriptCore/JavaScript.h> + +namespace DeviceAPI { +namespace Bluetooth { + +class JSBluetoothHealthProfileHandler +{ +public: + static const JSClassDefinition* getClassInfo(); + static const JSClassRef getClassRef(); + static JSObjectRef createJSObject(JSContextRef context); + +private: + + /** + * The callback invoked when an object is first created. + */ + static void initialize(JSContextRef context, + JSObjectRef object); + + /** + * The callback invoked when an object is finalized. + */ + static void finalize(JSObjectRef object); + + static JSValueRef registerSinkApplication(JSContextRef context, + JSObjectRef object, + JSObjectRef thisObject, + size_t argumentCount, + const JSValueRef arguments[], + JSValueRef* exception); + + static JSValueRef connectToSource(JSContextRef context, + JSObjectRef object, + JSObjectRef thisObject, + size_t argumentCount, + const JSValueRef arguments[], + JSValueRef* exception); + + /** + * This member variable contains the values which has to be passed + * when the this class is embedded into JS Engine. + */ + static JSClassDefinition m_classInfo; + + /** + * This structure describes a statically declared function property. + */ + static JSStaticFunction m_function[]; + + static JSClassRef m_jsClassRef; +}; + + +} // Bluetooth +} // DeviceAPI + +#endif // __TIZEN_JS_HEALTH_PROFILE_HANDLER_H__ diff --git a/mobile_src/Bluetooth/JSBluetoothManager.cpp b/mobile_src/Bluetooth/JSBluetoothManager.cpp new file mode 100644 index 0000000..b0a1422 --- /dev/null +++ b/mobile_src/Bluetooth/JSBluetoothManager.cpp @@ -0,0 +1,188 @@ +// +// Tizen Web Device API +// Copyright (c) 2012 Samsung Electronics Co., Ltd. +// +// Licensed under the Apache License, Version 2.0 (the License); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. +// + +#include <SecurityExceptions.h> + +#include <JSUtil.h> +#include <ArgumentValidator.h> +#include <GlobalContextManager.h> +#include <PlatformException.h> +#include <PropertyBag.h> +#include <bluetooth.h> + +#include "plugin_config_impl.h" +#include "JSBluetoothManager.h" +#include "JSBluetoothClassDeviceMajor.h" +#include "JSBluetoothClassDeviceMinor.h" +#include "JSBluetoothClassDeviceService.h" +#include "JSBluetoothAdapter.h" +#include "BluetoothAdapter.h" + +#include <TimeTracer.h> +#include <Logger.h> + +using namespace WrtDeviceApis::Commons; +using namespace DeviceAPI::Common; + +namespace DeviceAPI { +namespace Bluetooth { + +JSClassDefinition JSBluetoothManager::m_classInfo = { + 0, + kJSClassAttributeNone, + "BluetoothManager", + NULL, //ParentClass + m_property, //StaticValues + m_function, //StaticFunctions + initialize, //Initialize + finalize, //Finalize + NULL, //HasProperty, + NULL, //GetProperty, + NULL, //SetProperty, + NULL, //DeleteProperty, + NULL, //GetPropertyNames, + NULL, //CallAsFunction, + NULL, //CallAsConstructor, + NULL, //HasInstance, + NULL //ConvertToType +}; + +JSStaticValue JSBluetoothManager::m_property[] = { + { BLUETOOTH_MANAGER_DEVICE_MAJOR, getReadOnlyProperty, NULL, kJSPropertyAttributeNone|kJSPropertyAttributeReadOnly|kJSPropertyAttributeDontDelete}, + { BLUETOOTH_MANAGER_DEVICE_MINOR, getReadOnlyProperty, NULL, kJSPropertyAttributeNone|kJSPropertyAttributeReadOnly|kJSPropertyAttributeDontDelete}, + { BLUETOOTH_MANAGER_DEVICE_SERVICE, getReadOnlyProperty, NULL, kJSPropertyAttributeNone|kJSPropertyAttributeReadOnly|kJSPropertyAttributeDontDelete}, + { 0, 0, 0, 0 } +}; + +JSStaticFunction JSBluetoothManager::m_function[] = { + { BLUETOOTH_MANAGER_API_GET_DEFAULT_ADAPTER, getDefaultAdapter, kJSPropertyAttributeNone }, + { 0, 0, 0 } +}; + +JSClassRef JSBluetoothManager::m_jsClassRef = JSClassCreate(JSBluetoothManager::getClassInfo()); + +const JSClassRef JSBluetoothManager::getClassRef() +{ + if (!m_jsClassRef) { + m_jsClassRef = JSClassCreate(&m_classInfo); + } + return m_jsClassRef; +} + +const JSClassDefinition* JSBluetoothManager::getClassInfo() +{ + return &m_classInfo; +} + +void JSBluetoothManager::initialize(JSContextRef context, JSObjectRef object) +{ + LoggerD("initialize"); + + if (!JSObjectGetPrivate(object)) { + JSBluetoothManagerPriv* priv = new JSBluetoothManagerPriv(); + + if(priv) { + // deviceMajor + priv->setProperty(context, BLUETOOTH_MANAGER_DEVICE_MAJOR, + JSBluetoothClassDeviceMajor::createJSObject(context)); + + // deviceMinor + priv->setProperty(context, BLUETOOTH_MANAGER_DEVICE_MINOR, + JSBluetoothClassDeviceMinor::createJSObject(context)); + + // deviceService + priv->setProperty(context, BLUETOOTH_MANAGER_DEVICE_SERVICE, + JSBluetoothClassDeviceService::createJSObject(context)); + + if (!JSObjectSetPrivate(object, static_cast<void*>(priv))) { + LoggerW("Failed to set private data"); + delete priv; + } + LoggerD("Private creation ok"); + } + else { + LoggerW("Failed to create private data"); + } + } + else { + LoggerW("already has private data"); + } +} + +void JSBluetoothManager::finalize(JSObjectRef object) +{ + JSBluetoothManagerPriv *priv = static_cast<JSBluetoothManagerPriv*>(JSObjectGetPrivate(object)); + if (priv) { + JSObjectSetPrivate(object, NULL); + delete priv; + } +} + +JSValueRef JSBluetoothManager::getReadOnlyProperty(JSContextRef context, JSObjectRef object, JSStringRef propertyName, JSValueRef* exception) { + JSBluetoothManagerPriv *priv = static_cast<JSBluetoothManagerPriv*>(JSObjectGetPrivate(object)); + if(!priv) { + LoggerW("There is no private data"); + return NULL; + } + + std::string name = JSUtil::JSStringToString(context, propertyName); + return priv->getProperty(context, propertyName); +} + +JSValueRef JSBluetoothManager::getDefaultAdapter(JSContextRef context, + JSObjectRef object, + JSObjectRef thisObject, + size_t argumentCount, + const JSValueRef arguments[], + JSValueRef* exception) +{ + JSBluetoothManagerPriv *priv = static_cast<JSBluetoothManagerPriv*>(JSObjectGetPrivate(thisObject)); + + if (!priv) + { + DeviceAPI::Common::TypeMismatchException err("Private Object is null"); + return JSWebAPIErrorFactory::postException(context, exception, err); + } + + TIME_TRACER_ITEM_BEGIN("getDefaultAdapter", 1); + TIZEN_CHECK_ACCESS(context, exception, priv, BLUETOOTH_MANAGER_API_GET_DEFAULT_ADAPTER); + TIME_TRACER_ITEM_END("getDefaultAdapter::ACE", 1); + + + try { + + if(bt_initialize() != BT_ERROR_NONE) { + LoggerE("bt_initialize() failed"); + } + + BluetoothAdapter::getInstance()->copyAceCheckAccessFunction(priv); + JSObjectRef adapter = JSBluetoothAdapter::createJSObject(context); + + TIME_TRACER_ITEM_END("getDefaultAdapter", 1); + return adapter; + + } catch (const BasePlatformException &err) { + return JSWebAPIErrorFactory::postException(context, exception, err); + } catch (...) { + DeviceAPI::Common::UnknownException err("Unknown Error in BluetoothManager.getDefaultAdapter()."); + return JSWebAPIErrorFactory::postException(context, exception, err); + } +} + + +} // Bluetooth +} // DeviceAPI diff --git a/mobile_src/Bluetooth/JSBluetoothManager.h b/mobile_src/Bluetooth/JSBluetoothManager.h new file mode 100644 index 0000000..87c8b75 --- /dev/null +++ b/mobile_src/Bluetooth/JSBluetoothManager.h @@ -0,0 +1,93 @@ +// +// Tizen Web Device API +// Copyright (c) 2012 Samsung Electronics Co., Ltd. +// +// Licensed under the Apache License, Version 2.0 (the License); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. +// + +#ifndef __TIZEN_JS_BLUETOOTH_MANAGER_H__ +#define __TIZEN_JS_BLUETOOTH_MANAGER_H__ + +#include <JavaScriptCore/JavaScript.h> +#include <PropertyBag.h> +#include <memory> +#include <Security.h> + +namespace DeviceAPI { +namespace Bluetooth { + + +class JSBluetoothManagerPriv : + public DeviceAPI::Common::SecurityAccessor, + public DeviceAPI::Common::PropertyBag +{ +public: + JSBluetoothManagerPriv() : + DeviceAPI::Common::SecurityAccessor(), + DeviceAPI::Common::PropertyBag() + { + } + virtual ~JSBluetoothManagerPriv() {} +}; + +class JSBluetoothManager +{ +public: + static const JSClassDefinition* getClassInfo(); + static const JSClassRef getClassRef(); + +private: + + /** + * The callback invoked when an object is first created. + */ + static void initialize(JSContextRef context, + JSObjectRef object); + + /** + * The callback invoked when an object is finalized. + */ + static void finalize(JSObjectRef object); + + static JSValueRef getReadOnlyProperty(JSContextRef context, JSObjectRef object, JSStringRef propertyName, JSValueRef* exception); + + static JSValueRef getDefaultAdapter(JSContextRef context, + JSObjectRef object, + JSObjectRef thisObject, + size_t argumentCount, + const JSValueRef arguments[], + JSValueRef* exception); + + /** + * This member variable contains the values which has to be passed + * when the this class is embedded into JS Engine. + */ + static JSClassDefinition m_classInfo; + + /** + * This structure describes a statically declared function property. + */ + static JSStaticFunction m_function[]; + + static JSStaticValue m_property[]; + + static JSClassRef m_jsClassRef; + +}; + + + +} // Bluetooth +} // DeviceAPI + +#endif // __TIZEN_JS_DOWNLOAD_MANAGER_H__ diff --git a/mobile_src/Bluetooth/JSBluetoothProfileHandler.cpp b/mobile_src/Bluetooth/JSBluetoothProfileHandler.cpp new file mode 100644 index 0000000..5f50cdc --- /dev/null +++ b/mobile_src/Bluetooth/JSBluetoothProfileHandler.cpp @@ -0,0 +1,113 @@ +// +// Tizen Web Device API +// Copyright (c) 2012 Samsung Electronics Co., Ltd. +// +// Licensed under the Apache License, Version 2.0 (the License); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. +// + +#include <SecurityExceptions.h> + +#include <JSUtil.h> +#include <ArgumentValidator.h> +#include <GlobalContextManager.h> +#include <PlatformException.h> + +#include "plugin_config.h" +#include "JSBluetoothProfileHandler.h" +#include "JSBluetoothHealthProfileHandler.h" + +#include <TimeTracer.h> +#include <Logger.h> + +using namespace WrtDeviceApis::Commons; +using namespace DeviceAPI::Common; + +namespace DeviceAPI { +namespace Bluetooth { + +JSClassDefinition JSBluetoothProfileHandler::m_classInfo = { + 0, + kJSClassAttributeNone, + "BluetoothProfileHandler", + NULL, //ParentClass + m_property, //StaticValues + NULL, //StaticFunctions + initialize, //Initialize + finalize, //Finalize + NULL, //HasProperty, + NULL, //GetProperty, + NULL, //SetProperty, + NULL, //DeleteProperty, + NULL, //GetPropertyNames, + NULL, //CallAsFunction, + NULL, //CallAsConstructor, + NULL, //HasInstance, + NULL //ConvertToType +}; + +JSStaticValue JSBluetoothProfileHandler::m_property[] = { + { BLUETOOTH_PROFILE_TYPE, getProperty, NULL, kJSPropertyAttributeNone|kJSPropertyAttributeReadOnly|kJSPropertyAttributeDontDelete}, + { 0, 0, 0, 0 } +}; + +JSClassRef JSBluetoothProfileHandler::m_jsClassRef = JSClassCreate(JSBluetoothProfileHandler::getClassInfo()); + +const JSClassRef JSBluetoothProfileHandler::getClassRef() +{ + if (!m_jsClassRef) { + m_jsClassRef = JSClassCreate(&m_classInfo); + } + return m_jsClassRef; +} + +const JSClassDefinition* JSBluetoothProfileHandler::getClassInfo() +{ + return &m_classInfo; +} + +void JSBluetoothProfileHandler::initialize(JSContextRef context, JSObjectRef object) +{ + // Do nothing +} + +void JSBluetoothProfileHandler::finalize(JSObjectRef object) +{ + // Do nothing +} + +JSValueRef JSBluetoothProfileHandler::getProperty(JSContextRef context, JSObjectRef object, JSStringRef propertyName, JSValueRef* exception) { + LoggerD("Enter"); + try { + if (JSStringIsEqualToUTF8CString(propertyName, BLUETOOTH_PROFILE_TYPE)) { + if(object) { + if(JSValueIsObjectOfClass(context, object, JSBluetoothHealthProfileHandler::getClassRef())) { + std::string profileType("HEALTH"); + LoggerD("profileType: " << profileType.c_str()); + return JSUtil::toJSValueRef(context, profileType); + } + } + else { + LoggerE("object is NULL"); + } + } + } catch (const BasePlatformException &err) { + LoggerW("Getting property is failed" << err.getMessage().c_str()); + } + + return NULL; +} + + + +} // Bluetooth +} // DeviceAPI diff --git a/mobile_src/Bluetooth/JSBluetoothProfileHandler.h b/mobile_src/Bluetooth/JSBluetoothProfileHandler.h new file mode 100644 index 0000000..38c9370 --- /dev/null +++ b/mobile_src/Bluetooth/JSBluetoothProfileHandler.h @@ -0,0 +1,67 @@ +// +// Tizen Web Device API +// Copyright (c) 2012 Samsung Electronics Co., Ltd. +// +// Licensed under the Apache License, Version 2.0 (the License); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. +// + +#ifndef __TIZEN_JS_BLUETOOTH_PROFILE_HANDLER_H__ +#define __TIZEN_JS_BLUETOOTH_PROFILE_HANDLER_H__ + +#include <JavaScriptCore/JavaScript.h> + +namespace DeviceAPI { +namespace Bluetooth { + +class JSBluetoothProfileHandler +{ +public: + static const JSClassDefinition* getClassInfo(); + static const JSClassRef getClassRef(); + +private: + + /** + * The callback invoked when an object is first created. + */ + static void initialize(JSContextRef context, + JSObjectRef object); + + /** + * The callback invoked when an object is finalized. + */ + static void finalize(JSObjectRef object); + + static JSValueRef getProperty(JSContextRef context, + JSObjectRef object, + JSStringRef propertyName, + JSValueRef* exception); + + /** + * This member variable contains the values which has to be passed + * when the this class is embedded into JS Engine. + */ + static JSClassDefinition m_classInfo; + + static JSStaticValue m_property[]; + + static JSClassRef m_jsClassRef; + +}; + + + +} // Bluetooth +} // DeviceAPI + +#endif // __TIZEN_JS_BLUETOOTH_PROFILE_HANDLER_H__ diff --git a/mobile_src/Bluetooth/JSBluetoothServiceHandler.cpp b/mobile_src/Bluetooth/JSBluetoothServiceHandler.cpp new file mode 100644 index 0000000..68d70d0 --- /dev/null +++ b/mobile_src/Bluetooth/JSBluetoothServiceHandler.cpp @@ -0,0 +1,228 @@ +// +// Tizen Web Device API +// Copyright (c) 2012 Samsung Electronics Co., Ltd. +// +// Licensed under the Apache License, Version 2.0 (the License); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. +// + +#include <SecurityExceptions.h> + +#include <JSUtil.h> +#include <ArgumentValidator.h> +#include <GlobalContextManager.h> +#include <PlatformException.h> +#include <MultiCallbackUserData.h> + +#include "plugin_config.h" +#include "JSBluetoothServiceHandler.h" +#include "BluetoothAdapter.h" + +#include <TimeTracer.h> +#include <Logger.h> + +using namespace WrtDeviceApis::Commons; +using namespace DeviceAPI::Common; + +namespace DeviceAPI { +namespace Bluetooth { + +JSClassDefinition JSBluetoothServiceHandler::m_classInfo = { + 0, + kJSClassAttributeNone, + "BluetoothServiceHandler", + NULL, //ParentClass + m_property, //StaticValues + m_function, //StaticFunctions + initialize, //Initialize + finalize, //Finalize + NULL, //HasProperty, + NULL, //GetProperty, + NULL, //SetProperty, + NULL, //DeleteProperty, + NULL, //GetPropertyNames, + NULL, //CallAsFunction, + NULL, //CallAsConstructor, + NULL, //HasInstance, + NULL //ConvertToType +}; + +JSStaticValue JSBluetoothServiceHandler::m_property[] = { + { BLUETOOTH_SERVICE_HANDLER_UUID, getProperty, NULL, kJSPropertyAttributeNone|kJSPropertyAttributeReadOnly|kJSPropertyAttributeDontDelete }, + { BLUETOOTH_SERVICE_HANDLER_NAME, getProperty, NULL, kJSPropertyAttributeNone|kJSPropertyAttributeReadOnly|kJSPropertyAttributeDontDelete }, + { BLUETOOTH_SERVICE_HANDLER_IS_CONNECTED, getProperty, NULL, kJSPropertyAttributeNone|kJSPropertyAttributeReadOnly|kJSPropertyAttributeDontDelete }, + { BLUETOOTH_SERVICE_HANDLER_ONCONNECT, getProperty, setProperty, kJSPropertyAttributeNone|kJSPropertyAttributeDontDelete }, + { 0, 0, 0, 0 } +}; + +JSStaticFunction JSBluetoothServiceHandler::m_function[] = { + { BLUETOOTH_SERVICE_HANDLER_API_UNREGISTER, unregister, kJSPropertyAttributeNone }, + { 0, 0, 0 } +}; + +JSClassRef JSBluetoothServiceHandler::m_jsClassRef = JSClassCreate(JSBluetoothServiceHandler::getClassInfo()); + +const JSClassRef JSBluetoothServiceHandler::getClassRef() +{ + if (!m_jsClassRef) { + m_jsClassRef = JSClassCreate(&m_classInfo); + } + return m_jsClassRef; +} + +const JSClassDefinition* JSBluetoothServiceHandler::getClassInfo() +{ + return &m_classInfo; +} + +JSObjectRef JSBluetoothServiceHandler::createJSObject(JSContextRef context, BluetoothServiceHandlerPtr service) +{ + return JSObjectMake(context, getClassRef(), static_cast<void*>(service)); +} + +void JSBluetoothServiceHandler::initialize(JSContextRef context, JSObjectRef object) +{ + // do nothing +} + +void JSBluetoothServiceHandler::finalize(JSObjectRef object) +{ + BluetoothServiceHandlerPtr priv = static_cast<BluetoothServiceHandlerPtr>(JSObjectGetPrivate(object)); + if (priv) { + JSObjectSetPrivate(object, NULL); + delete priv; + } +} + +JSValueRef JSBluetoothServiceHandler::getProperty(JSContextRef context, + JSObjectRef object, + JSStringRef propertyName, + JSValueRef* exception) +{ + try { + BluetoothServiceHandlerPtr priv = static_cast<BluetoothServiceHandlerPtr>(JSObjectGetPrivate(object)); + if (!priv) { + throw TypeMismatchException("Private object is NULL"); + } + + if (JSStringIsEqualToUTF8CString(propertyName, BLUETOOTH_SERVICE_HANDLER_UUID)) { + return JSUtil::toJSValueRef(context, priv->getUUID()); + } + else if (JSStringIsEqualToUTF8CString(propertyName, BLUETOOTH_SERVICE_HANDLER_NAME)) { + return JSUtil::toJSValueRef(context, priv->getName()); + } + else if (JSStringIsEqualToUTF8CString(propertyName, BLUETOOTH_SERVICE_HANDLER_IS_CONNECTED)) { + return JSUtil::toJSValueRef(context, priv->getConnectionState()); + } + else if (JSStringIsEqualToUTF8CString(propertyName, BLUETOOTH_SERVICE_HANDLER_ONCONNECT)) { + return priv->getOnConnect(context); + } + } catch (const BasePlatformException &err) { + LoggerW("Getting property is failed: " << err.getMessage().c_str()); + } + + return NULL; +} + +bool JSBluetoothServiceHandler::setProperty(JSContextRef context, + JSObjectRef object, + JSStringRef propertyName, + JSValueRef value, + JSValueRef* exception) +{ + try { + BluetoothServiceHandlerPtr priv = static_cast<BluetoothServiceHandlerPtr>(JSObjectGetPrivate(object)); + if (!priv) { + throw TypeMismatchException("Private object is NULL"); + } + + if (JSStringIsEqualToUTF8CString(propertyName, BLUETOOTH_SERVICE_HANDLER_ONCONNECT)) { + JSObjectRef object = NULL; + if(!JSValueIsNull(context, value)) { + if(!JSValueIsObject(context, value)) { + throw TypeMismatchException("Value is not Object"); + } + + object = JSUtil::JSValueToObject(context, value); + + if(!JSObjectIsFunction(context, object)) { + throw TypeMismatchException("Not function"); + } + } + else { + LoggerD("onconnect() is NULL"); + } + + return priv->setOnConnect(context, object); + } + } catch (const BasePlatformException &err) { + JSWebAPIErrorFactory::postException(context, exception, err); + } + + return false; +} + +JSValueRef JSBluetoothServiceHandler::unregister(JSContextRef context, + JSObjectRef object, + JSObjectRef thisObject, + size_t argumentCount, + const JSValueRef arguments[], + JSValueRef* exception) +{ + TIME_TRACER_ITEM_BEGIN(__FUNCTION__, 1); + + + + try { + // Private Object + BluetoothServiceHandlerPtr priv = static_cast<BluetoothServiceHandlerPtr>(JSObjectGetPrivate(thisObject)); + if (!priv) { + throw TypeMismatchException("Private object is NULL."); + } + + // Access Check + TIME_TRACER_ITEM_BEGIN("unregister::ACE", 1); + TIZEN_CHECK_ACCESS(context, exception, priv, BLUETOOTH_SERVICE_HANDLER_API_UNREGISTER); + TIME_TRACER_ITEM_END("unregister::ACE", 1); + + + ArgumentValidator validator(context, argumentCount, arguments); + JSObjectRef successCallback = validator.toFunction(0, true); // successCallback + JSObjectRef errorCallback = validator.toFunction(1, true); // errorCallback + + // perform + MultiCallbackUserDataPtr callback( + new MultiCallbackUserData(GlobalContextManager::getInstance()->getGlobalContext(context))); + if(!callback){ + LoggerW("Can't create MultiCallbackUserData"); + } + else { + callback->setCallback("success", successCallback); + callback->setCallback("error", errorCallback); + } + + priv->unregister(callback); + TIME_TRACER_ITEM_END(__FUNCTION__, 1); + + return JSValueMakeUndefined(context); + } catch (const BasePlatformException &err) { + return JSWebAPIErrorFactory::postException(context, exception, err); + } catch (...) { + DeviceAPI::Common::UnknownException err("Unknown Error in BluetoothServiceHandler.unregister()."); + return JSWebAPIErrorFactory::postException(context, exception, err); + } +} + + + +} // Bluetooth +} // DeviceAPI diff --git a/mobile_src/Bluetooth/JSBluetoothServiceHandler.h b/mobile_src/Bluetooth/JSBluetoothServiceHandler.h new file mode 100644 index 0000000..c59540d --- /dev/null +++ b/mobile_src/Bluetooth/JSBluetoothServiceHandler.h @@ -0,0 +1,91 @@ +// +// Tizen Web Device API +// Copyright (c) 2012 Samsung Electronics Co., Ltd. +// +// Licensed under the Apache License, Version 2.0 (the License); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. +// + +#ifndef __TIZEN_JS_BLUETOOTH_SERVICE_HANDLER_H__ +#define __TIZEN_JS_BLUETOOTH_SERVICE_HANDLER_H__ + +#include <JavaScriptCore/JavaScript.h> +#include "BluetoothServiceHandler.h" + +namespace DeviceAPI { +namespace Bluetooth { + +class JSBluetoothServiceHandler +{ +public: + static const JSClassDefinition* getClassInfo(); + static const JSClassRef getClassRef(); + static JSObjectRef createJSObject(JSContextRef context, BluetoothServiceHandlerPtr service); + +private: + + /** + * The callback invoked when an object is first created. + */ + static void initialize(JSContextRef context, + JSObjectRef object); + + /** + * The callback invoked when an object is finalized. + */ + static void finalize(JSObjectRef object); + + static JSValueRef getProperty(JSContextRef context, + JSObjectRef object, + JSStringRef propertyName, + JSValueRef* exception); + + static bool setProperty(JSContextRef context, + JSObjectRef object, + JSStringRef propertyName, + JSValueRef value, + JSValueRef* exception); + + static JSValueRef unregister(JSContextRef context, + JSObjectRef object, + JSObjectRef thisObject, + size_t argumentCount, + const JSValueRef arguments[], + JSValueRef* exception); + + /** + * This member variable contains the values which has to be passed + * when the this class is embedded into JS Engine. + */ + static JSClassDefinition m_classInfo; + + /** + * This structure describes a statically declared function property. + */ + static JSStaticFunction m_function[]; + + /** + * This member variable contains the initialization values for the + * properties of this class. The values are given according to + * the data structure JSPropertySpec + */ + static JSStaticValue m_property[]; + + static JSClassRef m_jsClassRef; +}; + + + +} // Bluetooth +} // DeviceAPI + +#endif // __TIZEN_JS_DOWNLOAD_MANAGER_H__ diff --git a/mobile_src/Bluetooth/JSBluetoothSocket.cpp b/mobile_src/Bluetooth/JSBluetoothSocket.cpp new file mode 100644 index 0000000..f8b3a0a --- /dev/null +++ b/mobile_src/Bluetooth/JSBluetoothSocket.cpp @@ -0,0 +1,351 @@ +// +// Tizen Web Device API +// Copyright (c) 2012 Samsung Electronics Co., Ltd. +// +// Licensed under the Apache License, Version 2.0 (the License); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. +// + +#include <vector> + +#include <SecurityExceptions.h> +#include <JSUtil.h> +#include <ArgumentValidator.h> +#include <GlobalContextManager.h> +#include <PlatformException.h> +#include <MultiCallbackUserData.h> + +#include "plugin_config.h" +#include "JSBluetoothSocket.h" +#include "BluetoothSocket.h" +#include "BluetoothAdapter.h" + +#include <TimeTracer.h> +#include <Logger.h> + +using namespace WrtDeviceApis::Commons; +using namespace DeviceAPI::Common; + +namespace DeviceAPI { +namespace Bluetooth { + +JSClassDefinition JSBluetoothSocket::m_classInfo = { + 0, + kJSClassAttributeNone, + "BluetoothSocket", + NULL, //ParentClass + m_property, //StaticValues + m_function, //StaticFunctions + initialize, //Initialize + finalize, //Finalize + NULL, //HasProperty, + NULL, //GetProperty, + NULL, //SetProperty, + NULL, //DeleteProperty, + NULL, //GetPropertyNames, + NULL, //CallAsFunction, + NULL, //CallAsConstructor, + NULL, //HasInstance, + NULL //ConvertToType +}; + +JSStaticValue JSBluetoothSocket::m_property[] = { + { BLUETOOTH_SOCKET_UUID, getProperty, NULL, kJSPropertyAttributeNone|kJSPropertyAttributeReadOnly|kJSPropertyAttributeDontDelete }, + { BLUETOOTH_SOCKET_STATE, getProperty, NULL, kJSPropertyAttributeNone|kJSPropertyAttributeReadOnly|kJSPropertyAttributeDontDelete }, + { BLUETOOTH_SOCKET_PEER, getProperty, NULL, kJSPropertyAttributeNone|kJSPropertyAttributeReadOnly|kJSPropertyAttributeDontDelete }, + { BLUETOOTH_SOCKET_ONMESSAGE, getProperty, setProperty, kJSPropertyAttributeNone|kJSPropertyAttributeDontDelete }, + { BLUETOOTH_SOCKET_ONCLOSE, getProperty, setProperty, kJSPropertyAttributeNone|kJSPropertyAttributeDontDelete }, + { BLUETOOTH_SOCKET_ONERROR, getProperty, setProperty, kJSPropertyAttributeNone|kJSPropertyAttributeDontDelete }, + { 0, 0, 0, 0 } +}; + +JSStaticFunction JSBluetoothSocket::m_function[] = { + { BLUETOOTH_SOCKET_API_WRITE_DATA, writeData, kJSPropertyAttributeNone }, + { BLUETOOTH_SOCKET_API_READ_DATA, readData, kJSPropertyAttributeNone }, + { BLUETOOTH_SOCKET_API_CLOSE, close, kJSPropertyAttributeNone }, + { 0, 0, 0 } +}; + +JSClassRef JSBluetoothSocket::m_jsClassRef = JSClassCreate(JSBluetoothSocket::getClassInfo()); + +const JSClassRef JSBluetoothSocket::getClassRef() +{ + if (!m_jsClassRef) { + m_jsClassRef = JSClassCreate(&m_classInfo); + } + return m_jsClassRef; +} + +const JSClassDefinition* JSBluetoothSocket::getClassInfo() +{ + return &m_classInfo; +} + +JSObjectRef JSBluetoothSocket::createJSObject(JSContextRef context, BluetoothSocketPtr socket) +{ + return JSObjectMake(context, getClassRef(), static_cast<void*>(socket)); +} + +void JSBluetoothSocket::initialize(JSContextRef context, JSObjectRef object) +{ + // do nothing +} + +void JSBluetoothSocket::finalize(JSObjectRef object) +{ + BluetoothSocketPtr priv = static_cast<BluetoothSocketPtr>(JSObjectGetPrivate(object)); + if (priv) { + JSObjectSetPrivate(object, NULL); + delete priv; + } +} + +JSValueRef JSBluetoothSocket::getProperty(JSContextRef context, + JSObjectRef object, + JSStringRef propertyName, + JSValueRef* exception) +{ + try { + BluetoothSocketPtr priv = static_cast<BluetoothSocketPtr>(JSObjectGetPrivate(object)); + if (!priv) { + throw TypeMismatchException("Private object is NULL"); + } + + if (JSStringIsEqualToUTF8CString(propertyName, BLUETOOTH_SOCKET_UUID)) { + return JSUtil::toJSValueRef(context, priv->getUUID()); + } + else if (JSStringIsEqualToUTF8CString(propertyName, BLUETOOTH_SOCKET_STATE)) { + LoggerD("get state"); + std::string state; + if(priv->getConnectionState()) { + state = "OPEN"; + } + else { + state = "CLOSED"; + } + LoggerD("state: " << state); + return JSUtil::toJSValueRef(context, state); + } + else if (JSStringIsEqualToUTF8CString(propertyName, BLUETOOTH_SOCKET_PEER)) { + return priv->getPeer(context); + } + else if (JSStringIsEqualToUTF8CString(propertyName, BLUETOOTH_SOCKET_ONMESSAGE)) { + return priv->getOnMessage(context); + } + else if (JSStringIsEqualToUTF8CString(propertyName, BLUETOOTH_SOCKET_ONCLOSE)) { + return priv->getOnClose(context); + } + else if (JSStringIsEqualToUTF8CString(propertyName, BLUETOOTH_SOCKET_ONERROR)) { + return priv->getOnError(context); + } + } catch (const BasePlatformException &err) { + LoggerW("Getting property is failed: " << err.getMessage().c_str()); + } + + return NULL; +} + +bool JSBluetoothSocket::setProperty(JSContextRef context, + JSObjectRef object, + JSStringRef propertyName, + JSValueRef value, + JSValueRef* exception) +{ + LoggerD("Enter"); + + try { + BluetoothSocketPtr priv = static_cast<BluetoothSocketPtr>(JSObjectGetPrivate(object)); + if (!priv) { + throw TypeMismatchException("Private object is NULL"); + } + + if (JSStringIsEqualToUTF8CString(propertyName, BLUETOOTH_SOCKET_ONMESSAGE)) { + JSObjectRef object = NULL; + if(!JSValueIsNull(context, value)) { + if(!JSValueIsObject(context, value)) { + throw TypeMismatchException("Value is not Object"); + } + + object = JSUtil::JSValueToObject(context, value); + + if(!JSObjectIsFunction(context, object)) { + throw TypeMismatchException("Not function"); + } + } + else { + LoggerD("onmessage() is NULL"); + } + + return priv->setOnMessage(context, object); + } + else if (JSStringIsEqualToUTF8CString(propertyName, BLUETOOTH_SOCKET_ONCLOSE)) { + JSObjectRef object = NULL; + if(!JSValueIsNull(context, value)) { + if(!JSValueIsObject(context, value)) { + throw TypeMismatchException("Value is not Object"); + } + + object = JSUtil::JSValueToObject(context, value); + + if(!JSObjectIsFunction(context, object)) { + throw TypeMismatchException("Not function"); + } + } + else { + LoggerD("onclose() is NULL"); + } + + return priv->setOnClose(context, object); + } + else if (JSStringIsEqualToUTF8CString(propertyName, BLUETOOTH_SOCKET_ONERROR)) { + JSObjectRef object = NULL; + if(!JSValueIsNull(context, value)) { + if(!JSValueIsObject(context, value)) { + throw TypeMismatchException("Value is not Object"); + } + + object = JSUtil::JSValueToObject(context, value); + + if(!JSObjectIsFunction(context, object)) { + throw TypeMismatchException("Not function"); + } + } + else { + LoggerD("onerror() is NULL"); + } + + return priv->setOnError(context, object); + } + } catch (const BasePlatformException &err) { + JSWebAPIErrorFactory::postException(context, exception, err); + } + + return false; +} + +JSValueRef JSBluetoothSocket::writeData(JSContextRef context, + JSObjectRef object, + JSObjectRef thisObject, + size_t argumentCount, + const JSValueRef arguments[], + JSValueRef* exception) +{ + TIME_TRACER_ITEM_BEGIN(__FUNCTION__, 1); + + try { + // Private Object + BluetoothSocketPtr priv = static_cast<BluetoothSocketPtr>(JSObjectGetPrivate(thisObject)); + if (!priv) { + throw DeviceAPI::Common::UnknownException("Private object is NULL."); + } + + // Access Check + TIME_TRACER_ITEM_BEGIN("writeData::ACE", 1); + TIZEN_CHECK_ACCESS(context, exception, priv, BLUETOOTH_SOCKET_API_WRITE_DATA); + TIME_TRACER_ITEM_END("writeData::ACE", 1); + + ArgumentValidator validator(context, argumentCount, arguments); + + JSObjectRef dataArrayObj = validator.toArrayObject(0); // data + size_t size = JSGetArrayLength(context, dataArrayObj); + char *buffer = new char[size]; + for(size_t i = 0; i < size; ++i) { + JSValueRef element = JSGetArrayElement(context, dataArrayObj, i); + buffer[i] = static_cast<char>(JSUtil::JSValueToByte(context, element)); + } + + JSValueRef result = JSUtil::toJSValueRef(context, priv->writeData(buffer, size)); + delete buffer; + TIME_TRACER_ITEM_END(__FUNCTION__, 1);; + + return result; + } catch (const BasePlatformException &err) { + return JSWebAPIErrorFactory::postException(context, exception, err); + } catch (...) { + DeviceAPI::Common::UnknownException err("Unknown Error in BluetoothSocket.writeData()."); + return JSWebAPIErrorFactory::postException(context, exception, err); + } +} + +JSValueRef JSBluetoothSocket::readData(JSContextRef context, + JSObjectRef object, + JSObjectRef thisObject, + size_t argumentCount, + const JSValueRef arguments[], + JSValueRef* exception) +{ + TIME_TRACER_ITEM_BEGIN(__FUNCTION__, 1); + + + try { + // Private Object + BluetoothSocketPtr priv = static_cast<BluetoothSocketPtr>(JSObjectGetPrivate(thisObject)); + if (!priv) { + throw DeviceAPI::Common::UnknownException("Private object is NULL."); + } + + // Access Check + TIME_TRACER_ITEM_BEGIN("readData::ACE", 1); + TIZEN_CHECK_ACCESS(context, exception, priv, BLUETOOTH_SOCKET_API_READ_DATA); + TIME_TRACER_ITEM_END("readData::ACE", 1); + + + std::vector<signed char> data = priv->readData(); + TIME_TRACER_ITEM_END(__FUNCTION__, 1); + + return JSUtil::toJSValueRef_(context, data); + } catch (const BasePlatformException &err) { + return JSWebAPIErrorFactory::postException(context, exception, err); + } catch (...) { + DeviceAPI::Common::UnknownException err("Unknown Error in BluetoothSocket.readData()."); + return JSWebAPIErrorFactory::postException(context, exception, err); + } +} + +JSValueRef JSBluetoothSocket::close(JSContextRef context, + JSObjectRef object, + JSObjectRef thisObject, + size_t argumentCount, + const JSValueRef arguments[], + JSValueRef* exception) +{ + TIME_TRACER_ITEM_BEGIN(__FUNCTION__, 1);; + + try { + // Private Object + BluetoothSocketPtr priv = static_cast<BluetoothSocketPtr>(JSObjectGetPrivate(thisObject)); + if (!priv) { + throw DeviceAPI::Common::UnknownException("Private object is NULL."); + } + + // Access Check + TIME_TRACER_ITEM_BEGIN("close::ACE", 1); + TIZEN_CHECK_ACCESS(context, exception, priv, BLUETOOTH_SOCKET_API_CLOSE); + TIME_TRACER_ITEM_END("close::ACE", 1); + + + priv->close(); + TIME_TRACER_ITEM_END(__FUNCTION__, 1);; + + return JSValueMakeUndefined(context); + } catch (const BasePlatformException &err) { + return JSWebAPIErrorFactory::postException(context, exception, err); + } catch (...) { + DeviceAPI::Common::UnknownException err("Unknown Error in BluetoothSocket.close()."); + return JSWebAPIErrorFactory::postException(context, exception, err); + } +} + + + +} // Bluetooth +} // DeviceAPI diff --git a/mobile_src/Bluetooth/JSBluetoothSocket.h b/mobile_src/Bluetooth/JSBluetoothSocket.h new file mode 100644 index 0000000..7b98871 --- /dev/null +++ b/mobile_src/Bluetooth/JSBluetoothSocket.h @@ -0,0 +1,105 @@ +// +// Tizen Web Device API +// Copyright (c) 2012 Samsung Electronics Co., Ltd. +// +// Licensed under the Apache License, Version 2.0 (the License); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. +// + +#ifndef __TIZEN_JS_BLUETOOTH_SOCKET_H__ +#define __TIZEN_JS_BLUETOOTH_SOCKET_H__ + +#include <JavaScriptCore/JavaScript.h> +#include "BluetoothSocket.h" + +namespace DeviceAPI { +namespace Bluetooth { + +class JSBluetoothSocket +{ +public: + static const JSClassDefinition* getClassInfo(); + static const JSClassRef getClassRef(); + static JSObjectRef createJSObject(JSContextRef context, BluetoothSocketPtr socket); + +private: + + /** + * The callback invoked when an object is first created. + */ + static void initialize(JSContextRef context, + JSObjectRef object); + + /** + * The callback invoked when an object is finalized. + */ + static void finalize(JSObjectRef object); + + static JSValueRef getProperty(JSContextRef context, + JSObjectRef object, + JSStringRef propertyName, + JSValueRef* exception); + + static bool setProperty(JSContextRef context, + JSObjectRef object, + JSStringRef propertyName, + JSValueRef value, + JSValueRef* exception); + + static JSValueRef writeData(JSContextRef context, + JSObjectRef object, + JSObjectRef thisObject, + size_t argumentCount, + const JSValueRef arguments[], + JSValueRef* exception); + + static JSValueRef readData(JSContextRef context, + JSObjectRef object, + JSObjectRef thisObject, + size_t argumentCount, + const JSValueRef arguments[], + JSValueRef* exception); + + static JSValueRef close(JSContextRef context, + JSObjectRef object, + JSObjectRef thisObject, + size_t argumentCount, + const JSValueRef arguments[], + JSValueRef* exception); + + /** + * This member variable contains the values which has to be passed + * when the this class is embedded into JS Engine. + */ + static JSClassDefinition m_classInfo; + + /** + * This structure describes a statically declared function property. + */ + static JSStaticFunction m_function[]; + + /** + * This member variable contains the initialization values for the + * properties of this class. The values are given according to + * the data structure JSPropertySpec + */ + static JSStaticValue m_property[]; + + static JSClassRef m_jsClassRef; +}; + + + +} // Bluetooth +} // DeviceAPI + +#endif // __TIZEN_JS_DOWNLOAD_MANAGER_H__ diff --git a/mobile_src/Bluetooth/config.xml b/mobile_src/Bluetooth/config.xml new file mode 100644 index 0000000..e3c942f --- /dev/null +++ b/mobile_src/Bluetooth/config.xml @@ -0,0 +1,31 @@ +<?xml version="1.0" ?> +<!DOCTYPE plugin-properties SYSTEM "/usr/etc/tizen-apis/config.dtd"> +<plugin-properties> + <library-name>libwrt-plugins-tizen-bluetooth.so</library-name> + <feature-install-uri>bluetooth.install.uri</feature-install-uri> + <api-feature> + <name>http://tizen.org/privilege/bluetooth.gap</name> + <device-capability>bluetooth.gap</device-capability> + </api-feature> + + <api-feature> + <name>http://tizen.org/privilege/bluetooth.admin</name> + <device-capability>bluetooth.admin</device-capability> + </api-feature> + + <api-feature> + <name>http://tizen.org/privilege/bluetoothmanager</name> + <device-capability>bluetoothmanager</device-capability> + </api-feature> + + <api-feature> + <name>http://tizen.org/privilege/bluetooth.spp</name> + <device-capability>bluetooth.spp</device-capability> + </api-feature> + + <api-feature> + <name>http://tizen.org/privilege/bluetooth.health</name> + <device-capability>bluetooth.health</device-capability> + </api-feature> + +</plugin-properties> diff --git a/mobile_src/Bluetooth/plugin_config.cpp b/mobile_src/Bluetooth/plugin_config.cpp new file mode 100644 index 0000000..dbab123 --- /dev/null +++ b/mobile_src/Bluetooth/plugin_config.cpp @@ -0,0 +1,398 @@ +// +// Tizen Web Device API +// Copyright (c) 2012 Samsung Electronics Co., Ltd. +// +// Licensed under the Apache License, Version 2.0 (the License); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. +// + + +#include <Commons/FunctionDefinition.h> +#include <Commons/FunctionDeclaration.h> +#include <map> + +#include "plugin_config.h" + +#define BLUETOOTH_FEATURE_API_ADMIN "http://tizen.org/privilege/bluetooth.admin" +#define BLUETOOTH_FEATURE_API_GAP "http://tizen.org/privilege/bluetooth.gap" +#define BLUETOOTH_FEATURE_API_SPP "http://tizen.org/privilege/bluetooth.spp" +#define BLUETOOTH_FEATURE_API_MANAGER "http://tizen.org/privilege/bluetoothmanager" +#define BLUETOOTH_FEATURE_API_HDP "http://tizen.org/privilege/bluetooth.health" + +#define BLUETOOTH_DEVICE_CAP_ADMIN "bluetooth.admin" +#define BLUETOOTH_DEVICE_CAP_GAP "bluetooth.gap" +#define BLUETOOTH_DEVICE_CAP_SPP "bluetooth.spp" +#define BLUETOOTH_DEVICE_CAP_MANAGER "bluetoothmanager" +#define BLUETOOTH_DEVICE_CAP_HDP "bluetooth.health" + + +using namespace WrtDeviceApis::Commons; + +namespace DeviceAPI { +namespace Bluetooth { + +static FunctionMapping createBluetoothFunctions(); +static FunctionMapping BluetoothFunctions = createBluetoothFunctions(); + +#pragma GCC visibility push(default) + +DEFINE_FUNCTION_GETTER(Bluetooth, BluetoothFunctions); + +#pragma GCC visibility pop + +static FunctionMapping createBluetoothFunctions() +{ + /** + * Device capabilities + */ + ACE_CREATE_DEVICE_CAP(DEVICE_CAP_BLUETOOTH_ADMIN, BLUETOOTH_DEVICE_CAP_ADMIN); + ACE_CREATE_DEVICE_CAPS_LIST(DEVICE_LIST_BLUETOOTH_ADMIN); + ACE_ADD_DEVICE_CAP(DEVICE_LIST_BLUETOOTH_ADMIN, DEVICE_CAP_BLUETOOTH_ADMIN); + + ACE_CREATE_DEVICE_CAP(DEVICE_CAP_BLUETOOTH_GAP, BLUETOOTH_DEVICE_CAP_GAP); + ACE_CREATE_DEVICE_CAPS_LIST(DEVICE_LIST_BLUETOOTH_GAP); + ACE_ADD_DEVICE_CAP(DEVICE_LIST_BLUETOOTH_GAP, DEVICE_CAP_BLUETOOTH_GAP); + + ACE_CREATE_DEVICE_CAP(DEVICE_CAP_BLUETOOTH_SPP, BLUETOOTH_DEVICE_CAP_SPP); + ACE_CREATE_DEVICE_CAPS_LIST(DEVICE_LIST_BLUETOOTH_SPP); + ACE_ADD_DEVICE_CAP(DEVICE_LIST_BLUETOOTH_SPP, DEVICE_CAP_BLUETOOTH_SPP); + + ACE_CREATE_DEVICE_CAP(DEVICE_CAP_BLUETOOTH_MANAGER, BLUETOOTH_DEVICE_CAP_MANAGER); + ACE_CREATE_DEVICE_CAPS_LIST(DEVICE_LIST_BLUETOOTH_MANAGER); + ACE_ADD_DEVICE_CAP(DEVICE_LIST_BLUETOOTH_MANAGER, DEVICE_CAP_BLUETOOTH_MANAGER); + + ACE_CREATE_DEVICE_CAP(DEVICE_CAP_BLUETOOTH_HDP, BLUETOOTH_DEVICE_CAP_HDP); + ACE_CREATE_DEVICE_CAPS_LIST(DEVICE_LIST_BLUETOOTH_HDP); + ACE_ADD_DEVICE_CAP(DEVICE_LIST_BLUETOOTH_HDP, DEVICE_CAP_BLUETOOTH_HDP); + + + /** + * Api Features + */ + ACE_CREATE_FEATURE(FEATURE_ADMIN, BLUETOOTH_FEATURE_API_ADMIN); + ACE_CREATE_FEATURE_LIST(BLUETOOTH_FEATURES_BLUETOOTH_ADMIN); + ACE_ADD_API_FEATURE(BLUETOOTH_FEATURES_BLUETOOTH_ADMIN, FEATURE_ADMIN); + + ACE_CREATE_FEATURE(FEATURE_GAP, BLUETOOTH_FEATURE_API_GAP); + ACE_CREATE_FEATURE_LIST(BLUETOOTH_FEATURES_BLUETOOTH_GAP); + ACE_ADD_API_FEATURE(BLUETOOTH_FEATURES_BLUETOOTH_GAP, FEATURE_GAP); + + ACE_CREATE_FEATURE(FEATURE_SPP, BLUETOOTH_FEATURE_API_SPP); + ACE_CREATE_FEATURE_LIST(BLUETOOTH_FEATURES_BLUETOOTH_SPP); + ACE_ADD_API_FEATURE(BLUETOOTH_FEATURES_BLUETOOTH_SPP, FEATURE_SPP); + + ACE_CREATE_FEATURE(FEATURE_MANAGER, BLUETOOTH_FEATURE_API_MANAGER); + ACE_CREATE_FEATURE_LIST(BLUETOOTH_FEATURES_BLUETOOTH_MANAGER); + ACE_ADD_API_FEATURE(BLUETOOTH_FEATURES_BLUETOOTH_MANAGER, FEATURE_MANAGER); + ACE_ADD_API_FEATURE(BLUETOOTH_FEATURES_BLUETOOTH_MANAGER, FEATURE_ADMIN); // for backward compatibility + + ACE_CREATE_FEATURE(FEATURE_HDP, BLUETOOTH_FEATURE_API_HDP); + ACE_CREATE_FEATURE_LIST(BLUETOOTH_FEATURES_BLUETOOTH_HDP); + ACE_ADD_API_FEATURE(BLUETOOTH_FEATURES_BLUETOOTH_HDP, FEATURE_HDP); + + + /** + * Functions + */ + FunctionMapping BluetoothMapping; + + // getDefaultAdapter() + AceFunction getDefaultAdapterFunc = ACE_CREATE_FUNCTION( + FUNCTION_BLUETOOTH_MANAGER_API_GET_DEFAULT_ADAPTER, + BLUETOOTH_MANAGER_API_GET_DEFAULT_ADAPTER, + BLUETOOTH_FEATURES_BLUETOOTH_GAP, + DEVICE_LIST_BLUETOOTH_GAP); + + BluetoothMapping.insert(std::make_pair( + BLUETOOTH_MANAGER_API_GET_DEFAULT_ADAPTER, + getDefaultAdapterFunc)); + + // setName() + AceFunction setNameFunc = ACE_CREATE_FUNCTION( + FUNCTION_BLUETOOTH_ADAPTER_API_SET_NAME, + BLUETOOTH_ADAPTER_API_SET_NAME, + BLUETOOTH_FEATURES_BLUETOOTH_ADMIN, + DEVICE_LIST_BLUETOOTH_ADMIN); + + BluetoothMapping.insert(std::make_pair( + BLUETOOTH_ADAPTER_API_SET_NAME, + setNameFunc)); + + // setPowered() + AceFunction setPoweredFunc = ACE_CREATE_FUNCTION( + FUNCTION_BLUETOOTH_ADAPTER_API_SET_POWERED, + BLUETOOTH_ADAPTER_API_SET_POWERED, + BLUETOOTH_FEATURES_BLUETOOTH_ADMIN, + DEVICE_LIST_BLUETOOTH_ADMIN); + + BluetoothMapping.insert(std::make_pair( + BLUETOOTH_ADAPTER_API_SET_POWERED, + setPoweredFunc)); + + // setVisible() + AceFunction setVisibleFunc = ACE_CREATE_FUNCTION( + FUNCTION_BLUETOOTH_ADAPTER_API_SET_VISIBLE, + BLUETOOTH_ADAPTER_API_SET_VISIBLE, + BLUETOOTH_FEATURES_BLUETOOTH_MANAGER, + DEVICE_LIST_BLUETOOTH_MANAGER); + + BluetoothMapping.insert(std::make_pair( + BLUETOOTH_ADAPTER_API_SET_VISIBLE, + setVisibleFunc)); + + // setChangeListener() + AceFunction setChangeListenerFunc = ACE_CREATE_FUNCTION( + FUNCTION_BLUETOOTH_ADAPTER_API_SET_CHANGE_LISTENER, + BLUETOOTH_ADAPTER_API_SET_CHANGE_LISTENER, + BLUETOOTH_FEATURES_BLUETOOTH_ADMIN, + DEVICE_LIST_BLUETOOTH_ADMIN); + + BluetoothMapping.insert(std::make_pair( + BLUETOOTH_ADAPTER_API_SET_CHANGE_LISTENER, + setChangeListenerFunc)); + + // unsetChangeListener() + AceFunction unsetChangeListenerFunc = ACE_CREATE_FUNCTION( + FUNCTION_BLUETOOTH_ADAPTER_API_UNSET_CHANGE_LISTENER, + BLUETOOTH_ADAPTER_API_UNSET_CHANGE_LISTENER, + BLUETOOTH_FEATURES_BLUETOOTH_ADMIN, + DEVICE_LIST_BLUETOOTH_ADMIN); + + BluetoothMapping.insert(std::make_pair( + BLUETOOTH_ADAPTER_API_UNSET_CHANGE_LISTENER, + unsetChangeListenerFunc)); + + // discoverDevices() + AceFunction discoverDevicesFunc = ACE_CREATE_FUNCTION( + FUNCTION_BLUETOOTH_ADAPTER_API_DISCOVER_DEVICES, + BLUETOOTH_ADAPTER_API_DISCOVER_DEVICES, + BLUETOOTH_FEATURES_BLUETOOTH_GAP, + DEVICE_LIST_BLUETOOTH_GAP); + + BluetoothMapping.insert(std::make_pair( + BLUETOOTH_ADAPTER_API_DISCOVER_DEVICES, + discoverDevicesFunc)); + + // stopDiscovery() + AceFunction stopDiscoveryFunc = ACE_CREATE_FUNCTION( + FUNCTION_BLUETOOTH_ADAPTER_API_STOP_DISCOVERY, + BLUETOOTH_ADAPTER_API_STOP_DISCOVERY, + BLUETOOTH_FEATURES_BLUETOOTH_GAP, + DEVICE_LIST_BLUETOOTH_GAP); + + BluetoothMapping.insert(std::make_pair( + BLUETOOTH_ADAPTER_API_STOP_DISCOVERY, + stopDiscoveryFunc)); + + // getKnownDevices() + AceFunction getKnownDevicesFunc = ACE_CREATE_FUNCTION( + FUNCTION_BLUETOOTH_ADAPTER_API_GET_KNOWN_DEVICES, + BLUETOOTH_ADAPTER_API_GET_KNOWN_DEVICES, + BLUETOOTH_FEATURES_BLUETOOTH_GAP, + DEVICE_LIST_BLUETOOTH_GAP); + + BluetoothMapping.insert(std::make_pair( + BLUETOOTH_ADAPTER_API_GET_KNOWN_DEVICES, + getKnownDevicesFunc)); + + // getDevice() + AceFunction getDeviceFunc = ACE_CREATE_FUNCTION( + FUNCTION_BLUETOOTH_ADAPTER_API_GET_DEVICE, + BLUETOOTH_ADAPTER_API_GET_DEVICE, + BLUETOOTH_FEATURES_BLUETOOTH_GAP, + DEVICE_LIST_BLUETOOTH_GAP); + + BluetoothMapping.insert(std::make_pair( + BLUETOOTH_ADAPTER_API_GET_DEVICE, + getDeviceFunc)); + + // createBonding() + AceFunction createBondingFunc = ACE_CREATE_FUNCTION( + FUNCTION_BLUETOOTH_ADAPTER_API_CREATE_BONDING, + BLUETOOTH_ADAPTER_API_CREATE_BONDING, + BLUETOOTH_FEATURES_BLUETOOTH_GAP, + DEVICE_LIST_BLUETOOTH_GAP); + + BluetoothMapping.insert(std::make_pair( + BLUETOOTH_ADAPTER_API_CREATE_BONDING, + createBondingFunc)); + + // destroyBonding() + AceFunction destroyBondingFunc = ACE_CREATE_FUNCTION( + FUNCTION_BLUETOOTH_ADAPTER_API_DESTROY_BONDING, + BLUETOOTH_ADAPTER_API_DESTROY_BONDING, + BLUETOOTH_FEATURES_BLUETOOTH_GAP, + DEVICE_LIST_BLUETOOTH_GAP); + + BluetoothMapping.insert(std::make_pair( + BLUETOOTH_ADAPTER_API_DESTROY_BONDING, + destroyBondingFunc)); + + // registerRFCOMMServiceByUUID() + AceFunction registerRFCOMMServiceByUUIDFunc = ACE_CREATE_FUNCTION( + FUNCTION_BLUETOOTH_ADAPTER_API_REGISTER_RFCOMMSERVICE_BY_UUID, + BLUETOOTH_ADAPTER_API_REGISTER_RFCOMMSERVICE_BY_UUID, + BLUETOOTH_FEATURES_BLUETOOTH_SPP, + DEVICE_LIST_BLUETOOTH_SPP); + + BluetoothMapping.insert(std::make_pair( + BLUETOOTH_ADAPTER_API_REGISTER_RFCOMMSERVICE_BY_UUID, + registerRFCOMMServiceByUUIDFunc)); + + // connectToServiceByUUID() + AceFunction connectToServiceByUUIDFunc = ACE_CREATE_FUNCTION( + FUNCTION_BLUETOOTH_DEVICE_API_CONNECT_TO_SERVICE_BY_UUID, + BLUETOOTH_DEVICE_API_CONNECT_TO_SERVICE_BY_UUID, + BLUETOOTH_FEATURES_BLUETOOTH_SPP, + DEVICE_LIST_BLUETOOTH_SPP); + + BluetoothMapping.insert(std::make_pair( + BLUETOOTH_DEVICE_API_CONNECT_TO_SERVICE_BY_UUID, + connectToServiceByUUIDFunc)); + + // writeData() + AceFunction writeDataFunc = ACE_CREATE_FUNCTION( + FUNCTION_BLUETOOTH_SOCKET_API_WRITE_DATA, + BLUETOOTH_SOCKET_API_WRITE_DATA, + BLUETOOTH_FEATURES_BLUETOOTH_SPP, + DEVICE_LIST_BLUETOOTH_SPP); + + BluetoothMapping.insert(std::make_pair( + BLUETOOTH_SOCKET_API_WRITE_DATA, + writeDataFunc)); + + // readData() + AceFunction readDataFunc = ACE_CREATE_FUNCTION( + FUNCTION_BLUETOOTH_SOCKET_API_READ_DATA, + BLUETOOTH_SOCKET_API_READ_DATA, + BLUETOOTH_FEATURES_BLUETOOTH_SPP, + DEVICE_LIST_BLUETOOTH_SPP); + + BluetoothMapping.insert(std::make_pair( + BLUETOOTH_SOCKET_API_READ_DATA, + readDataFunc)); + + // close() + AceFunction closeFunc = ACE_CREATE_FUNCTION( + FUNCTION_BLUETOOTH_SOCKET_API_CLOSE, + BLUETOOTH_SOCKET_API_CLOSE, + BLUETOOTH_FEATURES_BLUETOOTH_SPP, + DEVICE_LIST_BLUETOOTH_SPP); + + BluetoothMapping.insert(std::make_pair( + BLUETOOTH_SOCKET_API_CLOSE, + closeFunc)); + + // hasService() + AceFunction hasServiceFunc = ACE_CREATE_FUNCTION( + FUNCTION_BLUETOOTH_CLASS_API_HAS_SERVICE, + BLUETOOTH_CLASS_API_HAS_SERVICE, + BLUETOOTH_FEATURES_BLUETOOTH_GAP, + DEVICE_LIST_BLUETOOTH_GAP); + + BluetoothMapping.insert(std::make_pair( + BLUETOOTH_CLASS_API_HAS_SERVICE, + hasServiceFunc)); + + // unregister() + AceFunction unregisterFunc = ACE_CREATE_FUNCTION( + FUNCTION_BLUETOOTH_SERVICE_HANDLER_API_UNREGISTER, + BLUETOOTH_SERVICE_HANDLER_API_UNREGISTER, + BLUETOOTH_FEATURES_BLUETOOTH_SPP, + DEVICE_LIST_BLUETOOTH_SPP); + + BluetoothMapping.insert(std::make_pair( + BLUETOOTH_SERVICE_HANDLER_API_UNREGISTER, + unregisterFunc)); + + // registerSinkApplication() + AceFunction registerSinkApplicationFunc = ACE_CREATE_FUNCTION( + FUNCTION_BLUETOOTH_HEALTH_PROFILE_HANDLER_API_REGISTER_SINK_APPLICATION, + BLUETOOTH_HEALTH_PROFILE_HANDLER_API_REGISTER_SINK_APPLICATION, + BLUETOOTH_FEATURES_BLUETOOTH_HDP, + DEVICE_LIST_BLUETOOTH_HDP); + + BluetoothMapping.insert(std::make_pair( + BLUETOOTH_HEALTH_PROFILE_HANDLER_API_REGISTER_SINK_APPLICATION, + registerSinkApplicationFunc)); + + // connectToSource() + AceFunction connectToSourceFunc = ACE_CREATE_FUNCTION( + FUNCTION_BLUETOOTH_HEALTH_PROFILE_HANDLER_API_CONNECT_TO_SOURCE, + BLUETOOTH_HEALTH_PROFILE_HANDLER_API_CONNECT_TO_SOURCE, + BLUETOOTH_FEATURES_BLUETOOTH_HDP, + DEVICE_LIST_BLUETOOTH_HDP); + + BluetoothMapping.insert(std::make_pair( + BLUETOOTH_HEALTH_PROFILE_HANDLER_API_CONNECT_TO_SOURCE, + connectToSourceFunc)); + + // unregister() + AceFunction unregisterHDPFunc = ACE_CREATE_FUNCTION( + FUNCTION_BLUETOOTH_HEALTH_APPLICATION_API_UNREGISTER, + BLUETOOTH_HEALTH_APPLICATION_API_UNREGISTER, + BLUETOOTH_FEATURES_BLUETOOTH_HDP, + DEVICE_LIST_BLUETOOTH_HDP); + + BluetoothMapping.insert(std::make_pair( + BLUETOOTH_HEALTH_APPLICATION_API_UNREGISTER, + unregisterHDPFunc)); + + // close() + AceFunction closeHDPFunc = ACE_CREATE_FUNCTION( + FUNCTION_BLUETOOTH_HEALTH_CHANNEL_API_CLOSE, + BLUETOOTH_HEALTH_CHANNEL_API_CLOSE, + BLUETOOTH_FEATURES_BLUETOOTH_HDP, + DEVICE_LIST_BLUETOOTH_HDP); + + BluetoothMapping.insert(std::make_pair( + BLUETOOTH_HEALTH_CHANNEL_API_CLOSE, + closeHDPFunc)); + + // sendData() + AceFunction sendDataFunc = ACE_CREATE_FUNCTION( + FUNCTION_BLUETOOTH_HEALTH_CHANNEL_API_SEND_DATA, + BLUETOOTH_HEALTH_CHANNEL_API_SEND_DATA, + BLUETOOTH_FEATURES_BLUETOOTH_HDP, + DEVICE_LIST_BLUETOOTH_HDP); + + BluetoothMapping.insert(std::make_pair( + BLUETOOTH_HEALTH_CHANNEL_API_SEND_DATA, + sendDataFunc)); + + // setListener() + AceFunction setListenerFunc = ACE_CREATE_FUNCTION( + FUNCTION_BLUETOOTH_HEALTH_CHANNEL_API_SET_LISTENER, + BLUETOOTH_HEALTH_CHANNEL_API_SET_LISTENER, + BLUETOOTH_FEATURES_BLUETOOTH_HDP, + DEVICE_LIST_BLUETOOTH_HDP); + + BluetoothMapping.insert(std::make_pair( + BLUETOOTH_HEALTH_CHANNEL_API_SET_LISTENER, + setListenerFunc)); + + // unsetListener() + AceFunction unsetListenerFunc = ACE_CREATE_FUNCTION( + FUNCTION_BLUETOOTH_HEALTH_CHANNEL_API_UNSET_LISTENER, + BLUETOOTH_HEALTH_CHANNEL_API_UNSET_LISTENER, + BLUETOOTH_FEATURES_BLUETOOTH_HDP, + DEVICE_LIST_BLUETOOTH_HDP); + + BluetoothMapping.insert(std::make_pair( + BLUETOOTH_HEALTH_CHANNEL_API_UNSET_LISTENER, + unsetListenerFunc)); + + return BluetoothMapping; +} + +} // Bluetooth +} // DeviceAPI diff --git a/mobile_src/Bluetooth/plugin_config.h b/mobile_src/Bluetooth/plugin_config.h new file mode 100644 index 0000000..a44485a --- /dev/null +++ b/mobile_src/Bluetooth/plugin_config.h @@ -0,0 +1,38 @@ +// +// Tizen Web Device API +// Copyright (c) 2012 Samsung Electronics Co., Ltd. +// +// Licensed under the Apache License, Version 2.0 (the License); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. +// + + +#ifndef _BLUETOOTH_PLUGIN_CONFIG_H_ +#define _BLUETOOTH_PLUGIN_CONFIG_H_ + +#include <Commons/FunctionDeclaration.h> +#include "plugin_config_impl.h" + +namespace DeviceAPI { +namespace Bluetooth { + +DECLARE_FUNCTION_GETTER(Bluetooth); + +#define BLUETOOTH_CHECK_ACCESS(functionName) \ + aceCheckAccess<AceFunctionGetter, DefaultArgsVerifier<> >( \ + getBluetoothFunctionData, \ + functionName) + +} +} + +#endif // _BLUETOOTH_PLUGIN_CONFIG_H_
\ No newline at end of file diff --git a/mobile_src/Bluetooth/plugin_config_impl.h b/mobile_src/Bluetooth/plugin_config_impl.h new file mode 100644 index 0000000..4d7668a --- /dev/null +++ b/mobile_src/Bluetooth/plugin_config_impl.h @@ -0,0 +1,75 @@ +#ifndef _BLUETOOTH_PLUGIN_CONFIG_IMPL_H_
+#define _BLUETOOTH_PLUGIN_CONFIG_IMPL_H_
+
+namespace DeviceAPI {
+namespace Bluetooth {
+
+// attributes
+#define BLUETOOTH_MANAGER_DEVICE_MAJOR "deviceMajor"
+#define BLUETOOTH_MANAGER_DEVICE_MINOR "deviceMinor"
+#define BLUETOOTH_MANAGER_DEVICE_SERVICE "deviceService"
+#define BLUETOOTH_ADAPTER_NAME "name"
+#define BLUETOOTH_ADAPTER_ADDRESS "address"
+#define BLUETOOTH_ADAPTER_POWERED "powered"
+#define BLUETOOTH_ADAPTER_VISIBLE "visible"
+#define BLUETOOTH_DEVICE_NAME "name"
+#define BLUETOOTH_DEVICE_ADDRESS "address"
+#define BLUETOOTH_DEVICE_DEVICE_CLASS "deviceClass"
+#define BLUETOOTH_DEVICE_IS_BONDED "isBonded"
+#define BLUETOOTH_DEVICE_IS_TRUSTED "isTrusted"
+#define BLUETOOTH_DEVICE_IS_CONNECTED "isConnected"
+#define BLUETOOTH_DEVICE_UUIDS "uuids"
+#define BLUETOOTH_SOCKET_UUID "uuid"
+#define BLUETOOTH_SOCKET_STATE "state"
+#define BLUETOOTH_SOCKET_PEER "peer"
+#define BLUETOOTH_SOCKET_ONMESSAGE "onmessage"
+#define BLUETOOTH_SOCKET_ONCLOSE "onclose"
+#define BLUETOOTH_SOCKET_ONERROR "onerror"
+#define BLUETOOTH_CLASS_MAJOR "major"
+#define BLUETOOTH_CLASS_MINOR "minor"
+#define BLUETOOTH_CLASS_SERVICES "services"
+#define BLUETOOTH_SERVICE_HANDLER_UUID "uuid"
+#define BLUETOOTH_SERVICE_HANDLER_NAME "name"
+#define BLUETOOTH_SERVICE_HANDLER_IS_CONNECTED "isConnected"
+#define BLUETOOTH_SERVICE_HANDLER_ONCONNECT "onconnect"
+#define BLUETOOTH_PROFILE_TYPE "profileType"
+#define BLUETOOTH_HEALTH_APPLICATION_DATA_TYPE "dataType"
+#define BLUETOOTH_HEALTH_APPLICATION_NAME "name"
+#define BLUETOOTH_HEALTH_APPLICATION_ONCONNECT "onconnect"
+#define BLUETOOTH_HEALTH_CHANNEL_PEER "peer"
+#define BLUETOOTH_HEALTH_CHANNEL_TYPE "channelType"
+#define BLUETOOTH_HEALTH_CHANNEL_APP "application"
+#define BLUETOOTH_HEALTH_CHANNEL_IS_CONNECTED "isConnected"
+
+
+// functions
+#define BLUETOOTH_MANAGER_API_GET_DEFAULT_ADAPTER "getDefaultAdapter"
+#define BLUETOOTH_ADAPTER_API_SET_NAME "setName"
+#define BLUETOOTH_ADAPTER_API_SET_POWERED "setPowered"
+#define BLUETOOTH_ADAPTER_API_SET_VISIBLE "setVisible"
+#define BLUETOOTH_ADAPTER_API_DISCOVER_DEVICES "discoverDevices"
+#define BLUETOOTH_ADAPTER_API_STOP_DISCOVERY "stopDiscovery"
+#define BLUETOOTH_ADAPTER_API_GET_KNOWN_DEVICES "getKnownDevices"
+#define BLUETOOTH_ADAPTER_API_GET_DEVICE "getDevice"
+#define BLUETOOTH_ADAPTER_API_CREATE_BONDING "createBonding"
+#define BLUETOOTH_ADAPTER_API_DESTROY_BONDING "destroyBonding"
+#define BLUETOOTH_ADAPTER_API_REGISTER_RFCOMMSERVICE_BY_UUID "registerRFCOMMServiceByUUID"
+#define BLUETOOTH_ADAPTER_API_GET_BLUETOOTH_PROFILE_HANDLER "getBluetoothProfileHandler"
+#define BLUETOOTH_ADAPTER_API_SET_CHANGE_LISTENER "setChangeListener"
+#define BLUETOOTH_ADAPTER_API_UNSET_CHANGE_LISTENER "unsetChangeListener"
+#define BLUETOOTH_DEVICE_API_CONNECT_TO_SERVICE_BY_UUID "connectToServiceByUUID"
+#define BLUETOOTH_SOCKET_API_WRITE_DATA "writeData"
+#define BLUETOOTH_SOCKET_API_READ_DATA "readData"
+#define BLUETOOTH_SOCKET_API_CLOSE "close"
+#define BLUETOOTH_CLASS_API_HAS_SERVICE "hasService"
+#define BLUETOOTH_SERVICE_HANDLER_API_UNREGISTER "unregister"
+#define BLUETOOTH_HEALTH_PROFILE_HANDLER_API_REGISTER_SINK_APPLICATION "registerSinkApplication"
+#define BLUETOOTH_HEALTH_APPLICATION_API_UNREGISTER "healthUnregister"
+#define BLUETOOTH_HEALTH_PROFILE_HANDLER_API_CONNECT_TO_SOURCE "connectToSource"
+#define BLUETOOTH_HEALTH_CHANNEL_API_SEND_DATA "sendData"
+#define BLUETOOTH_HEALTH_CHANNEL_API_SET_LISTENER "setListener"
+#define BLUETOOTH_HEALTH_CHANNEL_API_UNSET_LISTENER "unsetListener"
+#define BLUETOOTH_HEALTH_CHANNEL_API_CLOSE "healthClose"
+}
+}
+#endif
diff --git a/mobile_src/Bluetooth/plugin_initializer.cpp b/mobile_src/Bluetooth/plugin_initializer.cpp new file mode 100644 index 0000000..d0270f5 --- /dev/null +++ b/mobile_src/Bluetooth/plugin_initializer.cpp @@ -0,0 +1,108 @@ +// +// Tizen Web Device API +// Copyright (c) 2012 Samsung Electronics Co., Ltd. +// +// Licensed under the Apache License, Version 2.0 (the License); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. +// + + +#include <Commons/plugin_initializer_def.h> +#include <Commons/WrtAccess/WrtAccess.h> +#include <Security.h> +#include <GlobalContextManager.h> +#include "plugin_config.h" +#include "JSBluetoothManager.h" +#include "BluetoothAdapter.h" + +#include <TimeTracer.h> +#include <Logger.h> + +using namespace WrtDeviceApis; +using namespace WrtDeviceApis::Commons; +using namespace DeviceAPI::Common; + +namespace DeviceAPI { +namespace Bluetooth { + +AceSecurityStatus bluetoothAceCheckAccessFunction(const char* functionName) +{ + return BLUETOOTH_CHECK_ACCESS(functionName); +} + +DEFINE_GLOBAL_SECURITY_ACCESSOR(gSecurityAccessor); +DEFINE_SECURITY_ACCESSOR_SETTER(AceCheckerBluetoothSetter, JSBluetoothManagerPriv, gSecurityAccessor); + +class_definition_options_t BluetoothOptions = +{ + JS_CLASS, + CREATE_INSTANCE, + ALWAYS_NOTICE, + USE_OVERLAYED, + AceCheckerBluetoothSetter, + NULL +}; + + + +void on_widget_start_callback(int widgetId) +{ + LoggerD("[Tizen\\Bluetooth] on_widget_start_callback (%d)", widgetId); + TIME_TRACER_INIT(); + try { + WrtAccessSingleton::Instance().initialize(widgetId); + } catch (...) { + LoggerE("WrtAccess initialization failed"); + } + INITAILIZE_GLOBAL_SECURITY_ACCESSOR(gSecurityAccessor, bluetoothAceCheckAccessFunction); +} + +void on_widget_stop_callback(int widgetId) +{ + LoggerD("[Tizen\\Bluetooth] on_widget_stop_callback (%d)", widgetId); + TIME_TRACER_EXPORT_REPORT_TO(TIME_TRACER_EXPORT_FILE,"Bluetooth"); + TIME_TRACER_RELEASE(); + try { + WrtAccessSingleton::Instance().deinitialize(widgetId); + } catch (...) { + LoggerE("WrtAccess deinitialization failed"); + } + FINALIZE_GLOBAL_SECURITY_ACCESSOR(gSecurityAccessor); +} + +void on_frame_load_callback(const void *context) +{ + LoggerD("[Tizen\\Bluetooth] on_frame_load_callback (%p)", context); + GlobalContextManager::getInstance()->addGlobalContext(static_cast<JSContextRef>(context)); +} + +void on_frame_unload_callback(const void *context) +{ + LoggerD("[Tizen\\Bluetooth] on_frame_unload_callback (%p)", context); + GlobalContextManager::getInstance()->removeGlobalContext(static_cast<JSContextRef>(context)); + BluetoothAdapter::getInstance()->unloadFrame(static_cast<JSContextRef>(context)); +} + +PLUGIN_ON_WIDGET_START(on_widget_start_callback) +PLUGIN_ON_WIDGET_STOP(on_widget_stop_callback) +PLUGIN_ON_FRAME_LOAD(on_frame_load_callback) +PLUGIN_ON_FRAME_UNLOAD(on_frame_unload_callback) + +PLUGIN_CLASS_MAP_BEGIN +PLUGIN_CLASS_MAP_ADD_CLASS(WRT_JS_EXTENSION_OBJECT_TIZEN, + "bluetooth", + (js_class_template_getter)JSBluetoothManager::getClassRef, + &BluetoothOptions) +PLUGIN_CLASS_MAP_END + +} // Bluetooth +} // DeviceAPI |