• 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_event.h"
18 #include "napi_bluetooth_gatt_client.h"
19 #include "napi_bluetooth_gatt_client_callback.h"
20 
21 namespace OHOS {
22 namespace Bluetooth {
23 std::shared_mutex NapiGattClientCallback::g_gattClientCallbackInfosMutex;
24 
OnCharacteristicChanged(const GattCharacteristic & characteristic)25 void NapiGattClientCallback::OnCharacteristicChanged(const GattCharacteristic &characteristic)
26 {
27     std::unique_lock<std::shared_mutex> guard(g_gattClientCallbackInfosMutex);
28     std::map<std::string, std::shared_ptr<BluetoothCallbackInfo>>::iterator it =
29         callbackInfos_.find(STR_BT_GATT_CLIENT_CALLBACK_BLE_CHARACTERISTIC_CHANGE);
30     if (it == callbackInfos_.end() || it->second == nullptr) {
31         HILOGI("This callback is not registered by ability.");
32         return;
33     }
34     std::shared_ptr<BluetoothCallbackInfo> callbackInfo = it->second;
35     HILOGI("uuid is %{public}s", characteristic.GetUuid().ToString().c_str());
36 
37     NapiEvent::CheckAndNotify(callbackInfo, characteristic);
38 }
39 
OnCharacteristicReadResult(const GattCharacteristic & characteristic,int ret)40 void NapiGattClientCallback::OnCharacteristicReadResult(const GattCharacteristic &characteristic, int ret)
41 {
42     ReadCharacteristicValueCallbackInfo *callbackInfo = client_->readCharacteristicValueCallbackInfo_;
43     if (!callbackInfo) {
44         HILOGE("CallbackInfo does not exist");
45         return;
46     }
47     HILOGI("asyncState: %{public}d", callbackInfo->asyncState_);
48     if (callbackInfo->asyncState_ == ASYNC_START) {
49         callbackInfo->ret = ret;
50         callbackInfo->outputCharacteristic_ = &characteristic;
51         callbackInfo->asyncState_ = ASYNC_DONE;
52     }
53 }
54 
OnDescriptorReadResult(const GattDescriptor & descriptor,int ret)55 void NapiGattClientCallback::OnDescriptorReadResult(const GattDescriptor &descriptor, int ret)
56 {
57     ReadDescriptorValueCallbackInfo *callbackInfo = client_->readDescriptorValueCallbackInfo_;
58     if (!callbackInfo) {
59         HILOGE("CallbackInfo does not exist");
60         return;
61     }
62     HILOGI("asyncState: %{public}d", callbackInfo->asyncState_);
63     if (callbackInfo->asyncState_ == ASYNC_START) {
64         callbackInfo->ret = ret;
65         callbackInfo->outputDescriptor_ = &descriptor;
66         callbackInfo->asyncState_ = ASYNC_DONE;
67     }
68 }
69 
OnConnectionStateChanged(int connectionState,int ret)70 void NapiGattClientCallback::OnConnectionStateChanged(int connectionState, int ret)
71 {
72     std::unique_lock<std::shared_mutex> guard(g_gattClientCallbackInfosMutex);
73     std::map<std::string, std::shared_ptr<BluetoothCallbackInfo>>::iterator it =
74         callbackInfos_.find(STR_BT_GATT_CLIENT_CALLBACK_BLE_CONNECTIION_STATE_CHANGE);
75     if (it == callbackInfos_.end() || it->second == nullptr) {
76         HILOGI("This callback is not registered by ability.");
77         return;
78     }
79     std::shared_ptr<BluetoothCallbackInfo> callbackInfo = it->second;
80     HILOGI("connectionState:%{public}d, ret:%{public}d", connectionState, ret);
81     callbackInfo->state_ = connectionState;
82     callbackInfo->deviceId_ = client_->GetDevice()->GetDeviceAddr();
83     NapiEvent::CheckAndNotify(callbackInfo, callbackInfo->state_);
84 }
85 
OnServicesDiscovered(int status)86 void NapiGattClientCallback::OnServicesDiscovered(int status)
87 {
88     HILOGI("status:%{public}d", status);
89 }
90 } // namespace Bluetooth
91 } // namespace OHOS
92