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 #ifndef BLUETOOTH_CONNECTION_IMPL_H
16 #define BLUETOOTH_CONNECTION_IMPL_H
17 
18 #include "bluetooth_connection_ffi.h"
19 #include "cj_common_ffi.h"
20 #include "bluetooth_host.h"
21 #include "native/ffi_remote_data.h"
22 
23 namespace OHOS {
24 namespace CJSystemapi {
25 namespace CJBluetoothConnection {
26 using Bluetooth::BluetoothHostObserver;
27 using Bluetooth::BluetoothRemoteDevice;
28 using Bluetooth::BluetoothRemoteDeviceObserver;
29 using Bluetooth::ParcelUuid;
30 using Bluetooth::BluetoothDeviceClass;
31 using Bluetooth::DeviceBatteryInfo;
32 
33 class ConnectionImpl {
34 public:
35     ConnectionImpl() = default;
36     ~ConnectionImpl() = default;
37 
38     static void PairDevice(std::string deviceId, int32_t* errCode);
39     static char* GetRemoteDeviceName(std::string deviceId, int32_t* errCode);
40     static DeviceClass GetRemoteDeviceClass(std::string deviceId, int32_t* errCode);
41     static CArrString GetRemoteProfileUuids(std::string deviceId, int32_t* errCode);
42     static char* GetLocalName(int32_t* errCode);
43     static CArrString GetPairedDevices(int32_t* errCode);
44     static int32_t GetPairState(std::string deviceId, int32_t* errCode);
45     static int32_t GetProfileConnectionState(int32_t profileId, int32_t* errCode);
46     static void SetDevicePairingConfirmation(std::string deviceId, bool accept, int32_t* errCode);
47     static void SetDevicePinCode(std::string deviceId, std::string code, int32_t* errCode);
48     static void SetLocalName(std::string localName, int32_t* errCode);
49     static void SetBluetoothScanMode(int32_t mode, int32_t duration, int32_t* errCode);
50     static int32_t GetBluetoothScanMode(int32_t* errCode);
51     static void StartBluetoothDiscovery(int32_t* errCode);
52     static void StoptBluetoothDiscovery(int32_t* errCode);
53     static bool IsBluetoothDiscovering(int32_t* errCode);
54     static void SetRemoteDeviceName(std::string deviceId, std::string name, int32_t* errCode);
55     static CBatteryInfo GetRemoteDeviceBatteryInfo(std::string deviceId, int32_t* errCode);
56     static void RegisterConnectionObserver(int32_t callbackType, void (*callback)(), int32_t* errCode);
57 };
58 
59 class CjBluetoothConnectionObserver : public BluetoothHostObserver {
60 public:
61     CjBluetoothConnectionObserver();
62     ~CjBluetoothConnectionObserver() override = default;
63 
64     void OnStateChanged(const int transport, const int status) override{};
65     void OnDiscoveryStateChanged(int status) override{};
66     void OnDiscoveryResult(
67         const BluetoothRemoteDevice &device, int rssi, const std::string deviceName, int deviceClass) override;
68     void OnPairRequested(const BluetoothRemoteDevice &device) override{};
69     void OnPairConfirmed(const BluetoothRemoteDevice &device, int reqType, int number) override;
70     void OnScanModeChanged(int mode) override{};
71     void OnDeviceNameChanged(const std::string &deviceName) override{};
72     void OnDeviceAddrChanged(const std::string &address) override{};
73 
74     void RegisterDeviceFindFunc(std::function<void(CArrString)> cjCallback);
75     void RegisterPinRequestFunc(std::function<void(CPinRequiredParam)> cjCallback);
76 
77 private:
78     std::function<void(CArrString)> deviceFindFunc{nullptr};
79     std::function<void(CPinRequiredParam)> pinRequestFunc{nullptr};
80 };
81 
82 class CjBluetoothRemoteDeviceObserver : public BluetoothRemoteDeviceObserver {
83 public:
84     CjBluetoothRemoteDeviceObserver();
85     ~CjBluetoothRemoteDeviceObserver() override = default;
86 
87     void OnAclStateChanged(const BluetoothRemoteDevice &device, int state, unsigned int reason) override{};
88     void OnPairStatusChanged(const BluetoothRemoteDevice &device, int status, int cause) override;
89     void OnRemoteUuidChanged(const BluetoothRemoteDevice &device, const std::vector<ParcelUuid> &uuids) override{};
90     void OnRemoteNameChanged(const BluetoothRemoteDevice &device, const std::string &deviceName) override{};
91     void OnRemoteAliasChanged(const BluetoothRemoteDevice &device, const std::string &alias) override{};
92     void OnRemoteCodChanged(const BluetoothRemoteDevice &device, const BluetoothDeviceClass &cod) override{};
93     void OnRemoteBatteryLevelChanged(const BluetoothRemoteDevice &device, int batteryLevel) override{};
94     void OnReadRemoteRssiEvent(const BluetoothRemoteDevice &device, int rssi, int status) override{};
95     void OnRemoteBatteryChanged(const BluetoothRemoteDevice &device, const DeviceBatteryInfo &batteryInfo) override;
96 
97     void RegisterBondStateFunc(std::function<void(CBondStateParam)> cjCallback);
98     void RegisterBatteryChangeFunc(std::function<void(CBatteryInfo)> cjCallback);
99 
100 private:
101     std::function<void(CBondStateParam)> bondStateFunc{nullptr};
102     std::function<void(CBatteryInfo)> batteryChangeFunc{nullptr};
103 };
104 } // namespace BluetoothConnection
105 } // namespace CJSystemapi
106 } // namespace OHOS
107 
108 #endif // BLUETOOTH_CONNECTION_IMPL_H