• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2021 Huawei Device Co., Ltd.
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at
6  *
7  *     http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  */
15 #include <uv.h>
16 #include "bluetooth_log.h"
17 #include "napi_bluetooth_gatt_client.h"
18 #include "napi_bluetooth_gatt_client_callback.h"
19 
20 namespace OHOS {
21 namespace Bluetooth {
OnCharacteristicChanged(const GattCharacteristic & characteristic)22 void NapiGattClientCallback::OnCharacteristicChanged(const GattCharacteristic &characteristic)
23 {
24     HILOGI("NapiGattClientCallback::OnCharacteristicChanged called");
25 
26     if (!callbackInfos_[STR_BT_GATT_CLIENT_CALLBACK_BLE_CHARACTERISTIC_CHANGE]) {
27         HILOGW("NapiGattClientCallback::OnCharacteristicChanged: This callback is not registered by ability.");
28         return;
29     }
30     HILOGI("NapiGattClientCallback::OnCharacteristicChanged: %{public}s is registered by ability",
31         STR_BT_GATT_CLIENT_CALLBACK_BLE_CHARACTERISTIC_CHANGE.c_str());
32     std::shared_ptr<GattCharacteristicCallbackInfo> callbackInfo =
33         std::static_pointer_cast<GattCharacteristicCallbackInfo>(callbackInfos_[STR_BT_GATT_CLIENT_CALLBACK_BLE_CHARACTERISTIC_CHANGE]);
34     HILOGI("uuid is %{public}s", characteristic.GetUuid().ToString().c_str());
35     callbackInfo->characteristic_ = characteristic;
36     uv_loop_s *loop = nullptr;
37     napi_get_uv_event_loop(callbackInfo->env_, &loop);
38     uv_work_t *work = new uv_work_t;
39     work->data = (void*)callbackInfo.get();
40     uv_queue_work(
41         loop,
42         work,
43         [](uv_work_t *work) {},
44         [](uv_work_t *work, int status) {
45             GattCharacteristicCallbackInfo *callbackInfo = (GattCharacteristicCallbackInfo *)work->data;
46             napi_value result = nullptr;
47             napi_create_object(callbackInfo->env_, &result);
48             ConvertBLECharacteristicToJS(callbackInfo->env_, result, callbackInfo->characteristic_);
49             napi_value callback = nullptr;
50             napi_value undefined = nullptr;
51             napi_value callResult = nullptr;
52             napi_get_undefined(callbackInfo->env_, &undefined);
53             napi_get_reference_value(callbackInfo->env_, callbackInfo->callback_, &callback);
54             napi_call_function(callbackInfo->env_, undefined, callback, ARGS_SIZE_ONE, &result, &callResult);
55             delete work;
56             work = nullptr;
57         }
58     );
59 }
60 
OnCharacteristicReadResult(const GattCharacteristic & characteristic,int ret)61 void NapiGattClientCallback::OnCharacteristicReadResult(const GattCharacteristic &characteristic, int ret)
62 {
63     HILOGI("NapiGattClientCallback::OnCharacteristicChanged called");
64 
65     ReadCharacteristicValueCallbackInfo *callbackInfo = client_->readCharacteristicValueCallbackInfo_;
66     if (!callbackInfo) {
67         return;
68     }
69     if (callbackInfo->asyncState_ == ASYNC_START) {
70         callbackInfo->ret = ret;
71         callbackInfo->outputCharacteristic_ = &characteristic;
72         callbackInfo->asyncState_ = ASYNC_DONE;
73     }
74 }
75 
OnDescriptorReadResult(const GattDescriptor & descriptor,int ret)76 void NapiGattClientCallback::OnDescriptorReadResult(const GattDescriptor &descriptor, int ret)
77 {
78     HILOGI("NapiGattClientCallback::OnDescriptorReadResult called");
79 
80     ReadDescriptorValueCallbackInfo *callbackInfo = client_->readDescriptorValueCallbackInfo_;
81     if (!callbackInfo) {
82         return;
83     }
84     if (callbackInfo->asyncState_ == ASYNC_START) {
85         callbackInfo->ret = ret;
86         callbackInfo->outputDescriptor_ = &descriptor;
87         callbackInfo->asyncState_ = ASYNC_DONE;
88     }
89 }
90 
OnConnectionStateChanged(int connectionState,int ret)91 void NapiGattClientCallback::OnConnectionStateChanged(int connectionState, int ret)
92 {
93     HILOGI("NapiGattClientCallback::OnConnectionStateChanged called");
94 
95     if (!callbackInfos_[STR_BT_GATT_CLIENT_CALLBACK_BLE_CONNECTIION_STATE_CHANGE]) {
96         HILOGW("NapiGattClientCallback::OnConnectionStateChanged: This callback is not registered by ability.");
97         return;
98     }
99     HILOGI("NapiGattClientCallback::OnConnectionStateChanged: %{public}s is registered by ability",
100         STR_BT_GATT_CLIENT_CALLBACK_BLE_CONNECTIION_STATE_CHANGE.c_str());
101     std::shared_ptr<BluetoothCallbackInfo> callbackInfo =
102         callbackInfos_[STR_BT_GATT_CLIENT_CALLBACK_BLE_CONNECTIION_STATE_CHANGE];
103 
104     callbackInfo->state_ = connectionState;
105     callbackInfo->deviceId_ = client_->GetDevice()->GetDeviceAddr();
106     uv_loop_s *loop = nullptr;
107     napi_get_uv_event_loop(callbackInfo->env_, &loop);
108     uv_work_t *work = new uv_work_t;
109     work->data = (void*)callbackInfo.get();
110 
111     uv_queue_work(
112         loop,
113         work,
114         [](uv_work_t *work) {},
115         [](uv_work_t *work, int status) {
116             BluetoothCallbackInfo *callbackInfo = (BluetoothCallbackInfo *)work->data;
117             napi_value result = nullptr;
118             napi_create_object(callbackInfo->env_, &result);
119             ConvertStateChangeParamToJS(callbackInfo->env_, result, callbackInfo->deviceId_, callbackInfo->state_);
120             napi_value callback = nullptr;
121             napi_value undefined = nullptr;
122             napi_value callResult = nullptr;
123             napi_get_undefined(callbackInfo->env_, &undefined);
124             napi_get_reference_value(callbackInfo->env_, callbackInfo->callback_, &callback);
125             napi_call_function(callbackInfo->env_, undefined, callback, ARGS_SIZE_ONE, &result, &callResult);
126             delete work;
127             work = nullptr;
128         }
129     );
130 }
131 
OnServicesDiscovered(int status)132 void NapiGattClientCallback::OnServicesDiscovered(int status)
133 {
134     HILOGI("NapiGattClientCallback::OnServicesDiscovered called");
135     DiscoverServicesCallbackInfo *callbackInfo = client_->discoverServicesCallbackInfo_;
136     if (!callbackInfo) {
137         return;
138     }
139     if (callbackInfo->asyncState_ == ASYNC_START) {
140         callbackInfo->status_ = status;
141         callbackInfo->asyncState_ = ASYNC_DONE;
142     }
143 }
144 } // namespace Bluetooth
145 } // namespace OHOS
146