• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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_clientDevice.h"
17 
18 #include "bluetooth_ble_common.h"
19 #include "bluetooth_log.h"
20 
21 namespace OHOS {
22 namespace CJSystemapi {
23 namespace CJBluetoothBle {
24 
FfiGattClientCallback()25 FfiGattClientCallback::FfiGattClientCallback()
26 {
27 }
28 
OnReadRemoteRssiValueResult(int rssi,int status)29 void FfiGattClientCallback::OnReadRemoteRssiValueResult(int rssi, int status)
30 {
31     if (getRssiValueFunc != nullptr) {
32         RetDataI32 res{};
33         res.code = status;
34         res.data = rssi;
35         getRssiValueFunc(res);
36     }
37 }
38 
OnCharacteristicChanged(const GattCharacteristic & characteristic)39 void FfiGattClientCallback::OnCharacteristicChanged(const GattCharacteristic &characteristic)
40 {
41     NativeBLECharacteristic outCharacteristic{};
42 
43     GattCharacteristic character_ = const_cast<GattCharacteristic &>(characteristic);
44 
45     outCharacteristic.characteristicUuid = MallocCString(character_.GetUuid().ToString().c_str());
46     if (character_.GetService() != nullptr) {
47         outCharacteristic.serviceUuid = MallocCString(character_.GetService()->GetUuid().ToString().c_str());
48     }
49     size_t valueSize = 0;
50     uint8_t *valueData = character_.GetValue(&valueSize).get();
51 
52     CArrUI8 arr{};
53     arr.head = valueData;
54     arr.size = static_cast<int64_t>(valueSize);
55     outCharacteristic.characteristicValue = arr;
56 
57     outCharacteristic.properties = ConvertGattPropertiesToCJ(character_.GetProperties());
58     outCharacteristic.descriptors = Convert2CArrBLEDescriptor(character_.GetDescriptors());
59 
60     if (bleCharacteristicChangeFunc != nullptr) {
61         bleCharacteristicChangeFunc(outCharacteristic);
62     }
63     FreeNativeBLECharacteristic(outCharacteristic);
64 }
65 
OnCharacteristicReadResult(const GattCharacteristic & characteristic,int ret)66 void FfiGattClientCallback::OnCharacteristicReadResult(const GattCharacteristic &characteristic, int ret)
67 {
68     HILOGI("UUID: %{public}s, ret: %{public}d", characteristic.GetUuid().ToString().c_str(), ret);
69     RetNativeBLECharacteristic res{};
70     res.data = ConvertBLECharacteristicToCJ(const_cast<GattCharacteristic &>(characteristic));
71     res.code = ret;
72     if (readCharacteristicFunc != nullptr) {
73         readCharacteristicFunc(res);
74     }
75     FreeNativeBLECharacteristic(res.data);
76 }
77 
OnCharacteristicWriteResult(const GattCharacteristic & characteristic,int ret)78 void FfiGattClientCallback::OnCharacteristicWriteResult(const GattCharacteristic &characteristic, int ret)
79 {
80     HILOGI("UUID: %{public}s, ret: %{public}d", characteristic.GetUuid().ToString().c_str(), ret);
81     if (writeCharacteristicFunc != nullptr) {
82         writeCharacteristicFunc(ret);
83     }
84 }
85 
OnDescriptorReadResult(const GattDescriptor & descriptor,int ret)86 void FfiGattClientCallback::OnDescriptorReadResult(const GattDescriptor &descriptor, int ret)
87 {
88     HILOGI("UUID: %{public}s, ret: %{public}d", descriptor.GetUuid().ToString().c_str(), ret);
89     RetNativeBLEDescriptor res{};
90     res.data = ConvertBLEDescriptorToCJ(const_cast<GattDescriptor &>(descriptor));
91     res.code = ret;
92     if (readDescriptorFunc != nullptr) {
93         readDescriptorFunc(res);
94     }
95     FreeNativeBLEDescriptor(res.data);
96 }
97 
OnDescriptorWriteResult(const GattDescriptor & descriptor,int ret)98 void FfiGattClientCallback::OnDescriptorWriteResult(const GattDescriptor &descriptor, int ret)
99 {
100     HILOGI("UUID: %{public}s, ret: %{public}d", descriptor.GetUuid().ToString().c_str(), ret);
101     if (writeDescriptorValueFunc != nullptr) {
102         writeDescriptorValueFunc(ret);
103     }
104 }
105 
OnConnectionStateChanged(int connectionState,int ret)106 void FfiGattClientCallback::OnConnectionStateChanged(int connectionState, int ret)
107 {
108     int connectState_ = connectionState;
109 
110     NativeBLEConnectionChangeState outState{};
111 
112     outState.deviceId = MallocCString(deviceAddr_.c_str());
113     outState.state = GetProfileConnectionState(connectState_);
114 
115     if (bleConnectionStateChangeFunc != nullptr) {
116         bleConnectionStateChangeFunc(outState);
117     }
118     free(outState.deviceId);
119     outState.deviceId = nullptr;
120 }
121 
OnMtuUpdate(int mtu,int ret)122 void FfiGattClientCallback::OnMtuUpdate(int mtu, int ret)
123 {
124     if (bleMtuChangeFunc != nullptr) {
125         bleMtuChangeFunc(mtu);
126     }
127 }
128 
RegisterBLECharacteristicChangeFunc(std::function<void (NativeBLECharacteristic)> cjCallback)129 void FfiGattClientCallback::RegisterBLECharacteristicChangeFunc(std::function<void(NativeBLECharacteristic)> cjCallback)
130 {
131     bleCharacteristicChangeFunc = cjCallback;
132 }
133 
RegisterBLEConnectionStateChangeFunc(std::function<void (NativeBLEConnectionChangeState)> cjCallback)134 void FfiGattClientCallback::RegisterBLEConnectionStateChangeFunc(
135     std::function<void(NativeBLEConnectionChangeState)> cjCallback)
136 {
137     bleConnectionStateChangeFunc = cjCallback;
138 }
139 
RegisterBLEMtuChangeFunc(std::function<void (int32_t)> cjCallback)140 void FfiGattClientCallback::RegisterBLEMtuChangeFunc(std::function<void(int32_t)> cjCallback)
141 {
142     bleMtuChangeFunc = cjCallback;
143 }
144 
RegisterGetRemoteRssicallback(std::function<void (RetDataI32)> cjCallback)145 void FfiGattClientCallback::RegisterGetRemoteRssicallback(std::function<void(RetDataI32)> cjCallback)
146 {
147     getRssiValueFunc = cjCallback;
148 }
149 
RegisterReadCharacteristicCallback(std::function<void (RetNativeBLECharacteristic)> cjCallback)150 void FfiGattClientCallback::RegisterReadCharacteristicCallback(
151     std::function<void(RetNativeBLECharacteristic)> cjCallback)
152 {
153     readCharacteristicFunc = cjCallback;
154 }
155 
RegisterReadDescriptorCallback(std::function<void (RetNativeBLEDescriptor)> cjCallback)156 void FfiGattClientCallback::RegisterReadDescriptorCallback(std::function<void(RetNativeBLEDescriptor)> cjCallback)
157 {
158     readDescriptorFunc = cjCallback;
159 }
RegisterWriteCharacteristicCallback(std::function<void (int32_t)> cjCallback)160 void FfiGattClientCallback::RegisterWriteCharacteristicCallback(std::function<void(int32_t)> cjCallback)
161 {
162     writeCharacteristicFunc = cjCallback;
163 }
164 
RegisterWriteDescriptorCallback(std::function<void (int32_t)> cjCallback)165 void FfiGattClientCallback::RegisterWriteDescriptorCallback(std::function<void(int32_t)> cjCallback)
166 {
167     writeDescriptorValueFunc = cjCallback;
168 }
169 
170 } // namespace CJBluetoothBle
171 } // namespace CJSystemapi
172 } // namespace OHOS