1 /*
2 * Copyright (c) 2024 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 "bluetooth_ble_gattServer.h"
17
18 #include "bluetooth_ble_common.h"
19 #include "bluetooth_gatt_characteristic.h"
20 #include "bluetooth_gatt_descriptor.h"
21 #include "bluetooth_log.h"
22
23 namespace OHOS {
24 namespace CJSystemapi {
25 namespace CJBluetoothBle {
26 using Bluetooth::BTConnectState;
27 using Bluetooth::GattCharacteristic;
28 using Bluetooth::GattDescriptor;
29
FfiGattServerCallback()30 FfiGattServerCallback::FfiGattServerCallback()
31 {
32 }
33
OnCharacteristicReadRequest(const BluetoothRemoteDevice & device,GattCharacteristic & characteristic,int requestId)34 void FfiGattServerCallback::OnCharacteristicReadRequest(const BluetoothRemoteDevice &device,
35 GattCharacteristic &characteristic, int requestId)
36 {
37 int transId_ = requestId;
38 std::string deviceAddr_ = device.GetDeviceAddr();
39
40 NativeCharacteristicReadRequest request{};
41 request.deviceId = MallocCString(deviceAddr_.c_str());
42 request.transId = transId_;
43 request.offset = 0;
44 request.characteristicUuid = MallocCString(characteristic.GetUuid().ToString().c_str());
45 if (characteristic.GetService() != nullptr) {
46 request.serviceUuid = MallocCString(characteristic.GetService()->GetUuid().ToString().c_str());
47 }
48
49 if (characteristicReadFunc != nullptr) {
50 characteristicReadFunc(request);
51 }
52 free(request.deviceId);
53 free(request.characteristicUuid);
54 free(request.serviceUuid);
55 request.deviceId = nullptr;
56 request.characteristicUuid = nullptr;
57 request.serviceUuid = nullptr;
58 }
59
OnCharacteristicWriteRequest(const BluetoothRemoteDevice & device,GattCharacteristic & characteristic,int requestId)60 void FfiGattServerCallback::OnCharacteristicWriteRequest(const BluetoothRemoteDevice &device,
61 GattCharacteristic &characteristic, int requestId)
62 {
63 int transId_ = requestId;
64 std::string deviceAddr_ = device.GetDeviceAddr();
65
66 NativeCharacteristicWriteRequest request{};
67 request.deviceId = MallocCString(deviceAddr_.c_str());
68 request.transId = transId_;
69 request.offset = 0;
70 request.isPrepared = false;
71 request.needRsp = characteristic.GetWriteType() == GattCharacteristic::WriteType::DEFAULT;
72
73 size_t valueSize;
74 uint8_t *valueData = characteristic.GetValue(&valueSize).get();
75
76 CArrUI8 arr{};
77 arr.head = valueData;
78 arr.size = static_cast<int64_t>(valueSize);
79 request.value = arr;
80
81 request.characteristicUuid = MallocCString(characteristic.GetUuid().ToString().c_str());
82 if (characteristic.GetService() != nullptr) {
83 request.serviceUuid = MallocCString(characteristic.GetService()->GetUuid().ToString().c_str());
84 }
85 if (characteristicWriteFunc != nullptr) {
86 characteristicWriteFunc(request);
87 }
88 free(request.deviceId);
89 free(request.characteristicUuid);
90 free(request.serviceUuid);
91 request.deviceId = nullptr;
92 request.characteristicUuid = nullptr;
93 request.serviceUuid = nullptr;
94 }
95
OnDescriptorReadRequest(const BluetoothRemoteDevice & device,GattDescriptor & descriptor,int requestId)96 void FfiGattServerCallback::OnDescriptorReadRequest(const BluetoothRemoteDevice &device, GattDescriptor &descriptor,
97 int requestId)
98 {
99 int transId_ = requestId;
100 std::string deviceAddr_ = device.GetDeviceAddr();
101
102 NativeDescriptorReadRequest request{};
103
104 request.deviceId = MallocCString(deviceAddr_.c_str());
105 request.transId = transId_;
106 request.offset = 0;
107 request.descriptorUuid = MallocCString(descriptor.GetUuid().ToString().c_str());
108 request.characteristicUuid = MallocCString(descriptor.GetCharacteristic()->GetUuid().ToString().c_str());
109
110 if (descriptor.GetCharacteristic()->GetService() != nullptr) {
111 request.serviceUuid = MallocCString(descriptor.GetCharacteristic()->GetService()->GetUuid().ToString().c_str());
112 }
113
114 if (descriptorReadFunc != nullptr) {
115 descriptorReadFunc(request);
116 }
117 free(request.deviceId);
118 free(request.descriptorUuid);
119 free(request.characteristicUuid);
120 free(request.serviceUuid);
121 request.deviceId = nullptr;
122 request.descriptorUuid = nullptr;
123 request.characteristicUuid = nullptr;
124 request.serviceUuid = nullptr;
125 }
126
OnDescriptorWriteRequest(const BluetoothRemoteDevice & device,GattDescriptor & descriptor,int requestId)127 void FfiGattServerCallback::OnDescriptorWriteRequest(const BluetoothRemoteDevice &device, GattDescriptor &descriptor,
128 int requestId)
129 {
130 int transId_ = requestId;
131 std::string deviceAddr_ = device.GetDeviceAddr();
132
133 NativeDescriptorWriteRequest request{};
134
135 request.deviceId = MallocCString(deviceAddr_.c_str());
136 request.transId = transId_;
137 request.offset = 0;
138 request.isPrepared = false;
139 request.needRsp = true;
140
141 size_t valueSize;
142 uint8_t *valueData = descriptor.GetValue(&valueSize).get();
143
144 CArrUI8 arr{};
145 arr.head = valueData;
146 arr.size = static_cast<int64_t>(valueSize);
147 request.value = arr;
148
149 request.descriptorUuid = MallocCString(descriptor.GetUuid().ToString().c_str());
150 if (descriptor.GetCharacteristic() != nullptr) {
151 request.characteristicUuid = MallocCString(descriptor.GetCharacteristic()->GetUuid().ToString().c_str());
152 if (descriptor.GetCharacteristic()->GetService() != nullptr) {
153 request.serviceUuid =
154 MallocCString(descriptor.GetCharacteristic()->GetService()->GetUuid().ToString().c_str());
155 }
156 }
157
158 if (descriptorWriteFunc != nullptr) {
159 descriptorWriteFunc(request);
160 }
161 free(request.deviceId);
162 free(request.descriptorUuid);
163 free(request.characteristicUuid);
164 free(request.serviceUuid);
165 request.deviceId = nullptr;
166 request.descriptorUuid = nullptr;
167 request.characteristicUuid = nullptr;
168 request.serviceUuid = nullptr;
169 }
170
OnConnectionStateUpdate(const BluetoothRemoteDevice & device,int state)171 void FfiGattServerCallback::OnConnectionStateUpdate(const BluetoothRemoteDevice &device, int state)
172 {
173 std::lock_guard<std::mutex> lock(FfiGattServer::deviceListMutex_);
174 if (state == static_cast<int>(BTConnectState::CONNECTED)) {
175 HILOGI("connected");
176 bool hasAddr = false;
177 for (auto it = FfiGattServer::deviceList_.begin(); it != FfiGattServer::deviceList_.end(); ++it) {
178 if (*it == device.GetDeviceAddr()) {
179 hasAddr = true;
180 break;
181 }
182 }
183 if (!hasAddr) {
184 HILOGI("add devices");
185 FfiGattServer::deviceList_.push_back(device.GetDeviceAddr());
186 }
187 } else if (state == static_cast<int>(BTConnectState::DISCONNECTED)) {
188 HILOGI("disconnected");
189 for (auto it = FfiGattServer::deviceList_.begin(); it != FfiGattServer::deviceList_.end(); ++it) {
190 if (*it == device.GetDeviceAddr()) {
191 HILOGI("romove device");
192 FfiGattServer::deviceList_.erase(it);
193 break;
194 }
195 }
196 }
197
198 std::string deviceAddr_ = device.GetDeviceAddr();
199 int connectState_ = GetProfileConnectionState(state);
200
201 NativeBLEConnectionChangeState changeState{};
202
203 changeState.deviceId = MallocCString(deviceAddr_.c_str());
204 changeState.state = connectState_;
205
206 if (connectionStateChangeFunc != nullptr) {
207 connectionStateChangeFunc(changeState);
208 }
209 free(changeState.deviceId);
210 changeState.deviceId = nullptr;
211 }
212
OnMtuUpdate(const BluetoothRemoteDevice & device,int mtu)213 void FfiGattServerCallback::OnMtuUpdate(const BluetoothRemoteDevice &device, int mtu)
214 {
215 if (bleMtuChangeFunc != nullptr) {
216 bleMtuChangeFunc(mtu);
217 }
218 }
219
RegisterCharacteristicReadFunc(std::function<void (NativeCharacteristicReadRequest)> cjCallback)220 void FfiGattServerCallback::RegisterCharacteristicReadFunc(
221 std::function<void(NativeCharacteristicReadRequest)> cjCallback)
222 {
223 characteristicReadFunc = cjCallback;
224 }
225
RegisterCharacteristicWriteFunc(std::function<void (NativeCharacteristicWriteRequest)> cjCallback)226 void FfiGattServerCallback::RegisterCharacteristicWriteFunc(
227 std::function<void(NativeCharacteristicWriteRequest)> cjCallback)
228 {
229 characteristicWriteFunc = cjCallback;
230 }
231
RegisterDescriptorReadFunc(std::function<void (NativeDescriptorReadRequest)> cjCallback)232 void FfiGattServerCallback::RegisterDescriptorReadFunc(std::function<void(NativeDescriptorReadRequest)> cjCallback)
233 {
234 descriptorReadFunc = cjCallback;
235 }
236
RegisterDescriptorWriteFunc(std::function<void (NativeDescriptorWriteRequest)> cjCallback)237 void FfiGattServerCallback::RegisterDescriptorWriteFunc(std::function<void(NativeDescriptorWriteRequest)> cjCallback)
238 {
239 descriptorWriteFunc = cjCallback;
240 }
241
RegisterConnectionStateChangeFunc(std::function<void (NativeBLEConnectionChangeState)> cjCallback)242 void FfiGattServerCallback::RegisterConnectionStateChangeFunc(
243 std::function<void(NativeBLEConnectionChangeState)> cjCallback)
244 {
245 connectionStateChangeFunc = cjCallback;
246 }
247
RegisterBLEMtuChangeFunc(std::function<void (int32_t)> cjCallback)248 void FfiGattServerCallback::RegisterBLEMtuChangeFunc(std::function<void(int32_t)> cjCallback)
249 {
250 bleMtuChangeFunc = cjCallback;
251 }
252
253 } // namespace CJBluetoothBle
254 } // namespace CJSystemapi
255 } // namespace OHOS