2 // Tizen Web Device API
3 // Copyright (c) 2012 Samsung Electronics Co., Ltd.
5 // Licensed under the Apache License, Version 2.0 (the License);
6 // you may not use this file except in compliance with the License.
7 // You may obtain a copy of the License at
9 // http://www.apache.org/licenses/LICENSE-2.0
11 // Unless required by applicable law or agreed to in writing, software
12 // distributed under the License is distributed on an "AS IS" BASIS,
13 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 // See the License for the specific language governing permissions and
15 // limitations under the License.
17 #include "BluetoothGATTBase.h"
18 #include <PlatformException.h>
19 #include <MultiCallbackUserData.h>
22 #include <JSWebAPIErrorFactory.h>
23 #include <bits/unique_ptr.h>
24 #include "BluetoothUtil.h"
29 BluetoothGATTBase::BluetoothGATTBase(bt_gatt_h _handle) :m_handle(_handle) {
33 BluetoothGATTBase::~BluetoothGATTBase() {
37 struct ReadValueData {
38 Common::MultiCallbackUserData* pCallback;
42 void BluetoothGATTBase::readValue(Common::MultiCallbackUserData* pCallback) {
44 int ret = bt_gatt_client_read_value(m_handle,
45 readNativeCallback, static_cast<void*>(pCallback));
46 if(ret != BT_ERROR_NONE) {
47 if (ret == BT_ERROR_NOT_ENABLED) {
48 LOGE("LE is currently not enabled");
49 throw Common::InvalidStateException("LE is currently not enabled");
51 LOGE("Couldn't register callback for read value");
52 postError(pCallback, ret);
56 void BluetoothGATTBase::postError(Common::MultiCallbackUserData* _pCallback, int _errorCode) {
57 JSObjectRef errorobj = Common::JSWebAPIErrorFactory::makeErrorObject(
58 _pCallback->getContext(),
59 Common::JSWebAPIErrorFactory::UNKNOWN_ERROR,
60 BluetoothUtil::getBluetoothErrorMessage(_errorCode));
61 JSValueProtect(_pCallback->getContext(), errorobj);
62 ReadValueData* pData = new ReadValueData();
63 pData->pCallback = _pCallback;
64 pData->errorobj = errorobj;
66 guint error = g_idle_add( asyncError, static_cast<void*>(pData));
68 LOGE("Failed to add error callback to idle");
74 gboolean BluetoothGATTBase::asyncError(gpointer _pUserData) {
75 auto pReadValueData = static_cast<ReadValueData*>(_pUserData);
76 pReadValueData->pCallback->invokeCallback("error", pReadValueData->errorobj);
77 JSValueUnprotect(pReadValueData->pCallback->getContext(), pReadValueData->errorobj);
78 delete pReadValueData->pCallback;
79 delete pReadValueData;
80 return G_SOURCE_REMOVE;
83 void BluetoothGATTBase::readNativeCallback(int result,
84 bt_gatt_h request_handle, void *user_data) {
86 auto pCallback = static_cast<Common::MultiCallbackUserData*>(user_data);
87 if(result != BT_ERROR_NONE) {
88 postError(pCallback, result);
93 int ret = bt_gatt_get_value(request_handle, &value, &length);
94 if(ret != BT_ERROR_NONE) {
95 postError(pCallback, ret);
98 std::vector<signed char> valueVect;
99 for(int i = 0; i < length; ++i) {
100 valueVect.push_back(value[i]);
102 JSValueRef jsResult = Common::JSUtil::toJSValueRef_<signed char>(pCallback->getContext(),valueVect);
103 pCallback->invokeCallback("onread", jsResult);
109 void BluetoothGATTBase::writeNativeCallback(int result,
110 bt_gatt_h request_handle, void *user_data) {
112 auto pCallback = static_cast<Common::MultiCallbackUserData*>(user_data);
113 if(result == BT_ERROR_NONE) {
114 pCallback->invokeCallback("onsuccess");
118 postError(pCallback, result);
122 void BluetoothGATTBase::writeValue(std::unique_ptr<char[]> data, int dataSize, Common::MultiCallbackUserData* pCallback) {
125 int ret = bt_gatt_set_value(m_handle, data.get(), dataSize);
127 if(ret != BT_ERROR_NONE){
128 if (ret == BT_ERROR_NOT_ENABLED) {
129 LOGE("LE is currently not enabled");
130 throw Common::InvalidStateException("LE is currently not enabled");
132 LOGE("Set value failed with %d", ret);
133 postError(pCallback, ret);
137 int check = bt_gatt_client_write_value(m_handle,
138 writeNativeCallback, static_cast<void*>(pCallback));
139 if(check != BT_ERROR_NONE) {
140 LOGE("Could not write value %d", check);
141 postError(pCallback, check);
145 } // namespace Bluetooth
146 } // namespace DeviceAPI