1 /*
2 * Copyright (C) 2025 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
16 #include "ohos.bluetooth.ble.proj.hpp"
17 #include "ohos.bluetooth.ble.impl.hpp"
18 #include "taihe/runtime.hpp"
19 #include "stdexcept"
20
21 #include "bluetooth_ble_advertiser.h"
22 #include "bluetooth_ble_central_manager.h"
23 #include "bluetooth_remote_device.h"
24 #include "bluetooth_gatt_client.h"
25 #include "bluetooth_errorcode.h"
26 #include "bluetooth_log.h"
27
28 #include "taihe_bluetooth_ble_advertise_callback.h"
29 #include "taihe_bluetooth_gatt_client_callback.h"
30 #include "taihe_bluetooth_gatt_server_callback.h"
31 #include "taihe_bluetooth_ble_utils.h"
32 #include "taihe_bluetooth_ble_central_manager_callback.h"
33
34 using namespace taihe;
35 using namespace ohos::bluetooth::ble;
36
37 #ifndef ANI_BT_ASSERT_RETURN
38 #define ANI_BT_ASSERT_RETURN(cond, errCode, errMsg) \
39 do { \
40 if (!(cond)) { \
41 set_business_error(errCode, errMsg); \
42 HILOGE("bluetoothManager ani assert failed."); \
43 return; \
44 } \
45 } while (0)
46 #endif
47
48 namespace {
49
BleAdvertiserGetInstance(void)50 std::shared_ptr<OHOS::Bluetooth::BleAdvertiser> BleAdvertiserGetInstance(void)
51 {
52 static auto instance = OHOS::Bluetooth::BleAdvertiser::CreateInstance();
53 return instance;
54 }
55
BleCentralManagerGetInstance(void)56 OHOS::Bluetooth::BleCentralManager *BleCentralManagerGetInstance(void)
57 {
58 static OHOS::Bluetooth::BleCentralManager
59 instance(OHOS::Bluetooth::TaiheBluetoothBleCentralManagerCallback::GetInstance());
60 return &instance;
61 }
62
63 class GattClientDeviceImpl {
64 public:
GattClientDeviceImpl(string_view deviceId)65 explicit GattClientDeviceImpl(string_view deviceId)
66 {
67 HILOGI("enter");
68 std::string remoteAddr = std::string(deviceId);
69 device_ = std::make_shared<OHOS::Bluetooth::BluetoothRemoteDevice>(remoteAddr, 1);
70 client_ = std::make_shared<OHOS::Bluetooth::GattClient>(*device_);
71 client_->Init();
72 callback_ = std::make_shared<OHOS::Bluetooth::TaiheGattClientCallback>();
73 callback_->SetDeviceAddr(remoteAddr);
74 }
75 ~GattClientDeviceImpl() = default;
76
SetBLEMtuSize(double mtu)77 void SetBLEMtuSize(double mtu)
78 {
79 HILOGI("enter");
80 ANI_BT_ASSERT_RETURN(client_ != nullptr, OHOS::Bluetooth::BT_ERR_INTERNAL_ERROR,
81 "SetBLEMtuSize ani assert failed");
82
83 int ret = client_->RequestBleMtuSize(mtu);
84 HILOGI("ret: %{public}d", ret);
85 ANI_BT_ASSERT_RETURN(ret == OHOS::Bluetooth::BT_NO_ERROR, ret, "SetBLEMtuSize return error");
86 }
87
Connect()88 void Connect()
89 {
90 HILOGI("enter");
91 ANI_BT_ASSERT_RETURN(client_ != nullptr, OHOS::Bluetooth::BT_ERR_INTERNAL_ERROR,
92 "Connect ani assert failed");
93 ANI_BT_ASSERT_RETURN(callback_ != nullptr, OHOS::Bluetooth::BT_ERR_INTERNAL_ERROR,
94 "Connect ani assert failed");
95
96 int ret = client_->Connect(callback_, false, OHOS::Bluetooth::GATT_TRANSPORT_TYPE_LE);
97 HILOGI("ret: %{public}d", ret);
98 ANI_BT_ASSERT_RETURN(ret == OHOS::Bluetooth::BT_NO_ERROR, ret, "Connect return error");
99 }
100
Disconnect()101 void Disconnect()
102 {
103 HILOGI("enter");
104 ANI_BT_ASSERT_RETURN(client_ != nullptr, OHOS::Bluetooth::BT_ERR_INTERNAL_ERROR,
105 "Disconnect ani assert failed");
106
107 int ret = client_->Disconnect();
108 HILOGI("ret: %{public}d", ret);
109 ANI_BT_ASSERT_RETURN(ret == OHOS::Bluetooth::BT_NO_ERROR, ret, "Disconnect return error");
110 }
111
Close()112 void Close()
113 {
114 HILOGI("enter");
115 ANI_BT_ASSERT_RETURN(client_ != nullptr, OHOS::Bluetooth::BT_ERR_INTERNAL_ERROR, "Close ani assert failed");
116 int ret = client_->Close();
117 HILOGI("ret: %{public}d", ret);
118
119 ANI_BT_ASSERT_RETURN(ret == OHOS::Bluetooth::BT_NO_ERROR, ret, "Close return error");
120 }
121 private:
122 std::shared_ptr<OHOS::Bluetooth::GattClient> client_ = nullptr;
123 std::shared_ptr<OHOS::Bluetooth::TaiheGattClientCallback> callback_;
124 std::shared_ptr<OHOS::Bluetooth::BluetoothRemoteDevice> device_ = nullptr;
125 };
126
127 class GattServerImpl {
128 public:
GattServerImpl()129 GattServerImpl()
130 {
131 HILOGI("enter");
132 callback_ = std::make_shared<OHOS::Bluetooth::TaiheGattServerCallback>();
133 std::shared_ptr<OHOS::Bluetooth::GattServerCallback> tmp =
134 std::static_pointer_cast<OHOS::Bluetooth::GattServerCallback>(callback_);
135 server_ = OHOS::Bluetooth::GattServer::CreateInstance(tmp);
136 }
137 ~GattServerImpl() = default;
138
Close()139 void Close()
140 {
141 HILOGI("enter");
142 ANI_BT_ASSERT_RETURN(server_ != nullptr, OHOS::Bluetooth::BT_ERR_INTERNAL_ERROR, "Close ani assert failed");
143 int ret = server_->Close();
144 HILOGI("ret: %{public}d", ret);
145
146 ANI_BT_ASSERT_RETURN(ret == OHOS::Bluetooth::BT_NO_ERROR, ret, "Close return error");
147 }
148 private:
149 std::shared_ptr<OHOS::Bluetooth::GattServer> server_ = nullptr;
150 std::shared_ptr<OHOS::Bluetooth::TaiheGattServerCallback> callback_;
151 };
152
153 class BleScannerImpl {
154 public:
BleScannerImpl()155 BleScannerImpl()
156 {
157 HILOGI("enter");
158 callback_ = std::make_shared<OHOS::Bluetooth::TaiheBluetoothBleCentralManagerCallback>(true);
159 bleCentralManager_ = std::make_shared<OHOS::Bluetooth::BleCentralManager>(callback_);
160 }
161 ~BleScannerImpl() = default;
162
163 private:
164 std::shared_ptr<OHOS::Bluetooth::BleCentralManager> bleCentralManager_ = nullptr;
165 std::shared_ptr<OHOS::Bluetooth::TaiheBluetoothBleCentralManagerCallback> callback_ = nullptr;
166 };
167
StopAdvertising()168 void StopAdvertising()
169 {
170 HILOGI("enter");
171 std::shared_ptr<OHOS::Bluetooth::BleAdvertiser> bleAdvertiser = BleAdvertiserGetInstance();
172 ANI_BT_ASSERT_RETURN(bleAdvertiser != nullptr, OHOS::Bluetooth::BT_ERR_INTERNAL_ERROR,
173 "bleAdvertiser ani assert failed");
174
175 std::vector<std::shared_ptr<OHOS::Bluetooth::BleAdvertiseCallback>> callbacks = bleAdvertiser->GetAdvObservers();
176 if (callbacks.empty()) {
177 int ret = bleAdvertiser->StopAdvertising(OHOS::Bluetooth::TaiheBluetoothBleAdvertiseCallback::GetInstance());
178 ANI_BT_ASSERT_RETURN(ret == OHOS::Bluetooth::BT_NO_ERROR, ret, "StopAdvertising return error");
179 }
180 }
181
StopBLEScan()182 void StopBLEScan()
183 {
184 HILOGI("enter");
185 int ret = BleCentralManagerGetInstance()->StopScan();
186 ANI_BT_ASSERT_RETURN(ret == OHOS::Bluetooth::BT_NO_ERROR, ret, "StopBLEScan return error");
187 }
188
CreateGattClientDevice(string_view deviceId)189 GattClientDevice CreateGattClientDevice(string_view deviceId)
190 {
191 std::string remoteAddr = std::string(deviceId);
192 return make_holder<GattClientDeviceImpl, GattClientDevice>(remoteAddr);
193 }
194
CreateGattServer()195 GattServer CreateGattServer()
196 {
197 return make_holder<GattServerImpl, GattServer>();
198 }
199
CreateBleScanner()200 BleScanner CreateBleScanner()
201 {
202 return make_holder<BleScannerImpl, BleScanner>();
203 }
204 } // namespace
205
206 // Since these macros are auto-generate, lint will cause false positive.
207 // NOLINTBEGIN
208 TH_EXPORT_CPP_API_CreateGattClientDevice(CreateGattClientDevice);
209 TH_EXPORT_CPP_API_CreateGattServer(CreateGattServer);
210 TH_EXPORT_CPP_API_CreateBleScanner(CreateBleScanner);
211 TH_EXPORT_CPP_API_StopAdvertising(StopAdvertising);
212 TH_EXPORT_CPP_API_StopBLEScan(StopBLEScan);
213 // NOLINTEND