• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 #include "proxy_observer.h"
16 
17 #include "bluetooth_def.h"
18 #include "bluetooth_hfp_ag.h"
19 #include "bluetooth_host.h"
20 #include "conn_log.h"
21 #include "softbus_adapter_crypto.h"
22 #include "softbus_common.h"
23 #include "softbus_conn_common.h"
24 #include "softbus_error_code.h"
25 #include "softbus_utils.h"
26 
27 namespace OHOS::SoftBus {
28 class ProxyObserver : public OHOS::Bluetooth::HandsFreeAudioGatewayObserver {
29 public:
ProxyObserver(const ProxyListener listener)30     explicit ProxyObserver(const ProxyListener listener)
31     {
32         listener_ = listener;
33     }
~ProxyObserver()34     ~ProxyObserver() {}
35     // HFP reconnection callback
36     void OnConnectionStateChanged(const Bluetooth::BluetoothRemoteDevice &device,
37         int32_t state, int32_t cause) override;
OnScoStateChanged(const Bluetooth::BluetoothRemoteDevice & device,int32_t state,int32_t reason)38     void OnScoStateChanged(const Bluetooth::BluetoothRemoteDevice &device,
39         int32_t state, int32_t reason) override {}
OnActiveDeviceChanged(const Bluetooth::BluetoothRemoteDevice & device)40     void OnActiveDeviceChanged(const Bluetooth::BluetoothRemoteDevice &device) override {}
OnHfEnhancedDriverSafetyChanged(const Bluetooth::BluetoothRemoteDevice & device,int32_t indValue)41     void OnHfEnhancedDriverSafetyChanged(const Bluetooth::BluetoothRemoteDevice &device,
42         int32_t indValue) override {}
OnHfpStackChanged(const Bluetooth::BluetoothRemoteDevice & device,int32_t action)43     void OnHfpStackChanged(const Bluetooth::BluetoothRemoteDevice &device, int32_t action) override {}
44 
45 private:
46     ProxyListener listener_;
47 };
48 
OnConnectionStateChanged(const OHOS::Bluetooth::BluetoothRemoteDevice & device,int32_t state,int32_t cause)49 void ProxyObserver::OnConnectionStateChanged(const OHOS::Bluetooth::BluetoothRemoteDevice &device,
50     int32_t state, int32_t cause)
51 {
52     std::string address = device.GetDeviceAddr();
53     char anomizeAddress[BT_MAC_LEN] = {0};
54     ConvertAnonymizeMacAddress(anomizeAddress, BT_MAC_LEN, address.c_str(), BT_MAC_LEN);
55     CONN_LOGW(CONN_PROXY, "hfp OnConnectionStateChanged %{public}s to %{public}d", anomizeAddress, state);
56     if (state == (int)Bluetooth::BTConnectState::CONNECTED) {
57         // the Bluetooth bottom layer has been reconnected
58         if (listener_ != nullptr) {
59             listener_(address.c_str(), SOFTBUS_HFP_CONNECTED);
60         }
61     }
62 }
63 
64 class ProxyPairStatusObserver : public Bluetooth::BluetoothRemoteDeviceObserver  {
65 public:
ProxyPairStatusObserver(const ProxyListener listener)66     explicit ProxyPairStatusObserver(const ProxyListener listener)
67     {
68         listener_ = listener;
69     }
~ProxyPairStatusObserver()70     ~ProxyPairStatusObserver() {}
OnAclStateChanged(const Bluetooth::BluetoothRemoteDevice & device,int state,unsigned int reason)71     void OnAclStateChanged(const Bluetooth::BluetoothRemoteDevice& device, int state, unsigned int reason) override {};
72     void OnPairStatusChanged(const Bluetooth::BluetoothRemoteDevice& device, int status, int cause) override;
OnRemoteUuidChanged(const Bluetooth::BluetoothRemoteDevice & device,const std::vector<Bluetooth::ParcelUuid> & uuids)73     void OnRemoteUuidChanged(const Bluetooth::BluetoothRemoteDevice& device,
74         const std::vector<Bluetooth::ParcelUuid>& uuids) override {};
OnRemoteNameChanged(const Bluetooth::BluetoothRemoteDevice & device,const std::string & deviceName)75     void OnRemoteNameChanged(const Bluetooth::BluetoothRemoteDevice& device, const std::string& deviceName) override {};
OnRemoteAliasChanged(const Bluetooth::BluetoothRemoteDevice & device,const std::string & alias)76     void OnRemoteAliasChanged(const Bluetooth::BluetoothRemoteDevice& device, const std::string& alias) override {};
OnRemoteCodChanged(const Bluetooth::BluetoothRemoteDevice & device,const Bluetooth::BluetoothDeviceClass & cod)77     void OnRemoteCodChanged(const Bluetooth::BluetoothRemoteDevice& device,
78         const Bluetooth::BluetoothDeviceClass& cod) override {};
OnRemoteBatteryLevelChanged(const Bluetooth::BluetoothRemoteDevice & device,int batteryLevel)79     void OnRemoteBatteryLevelChanged(const Bluetooth::BluetoothRemoteDevice& device, int batteryLevel) override {};
OnReadRemoteRssiEvent(const Bluetooth::BluetoothRemoteDevice & device,int rssi,int status)80     void OnReadRemoteRssiEvent(const Bluetooth::BluetoothRemoteDevice& device, int rssi, int status) override {};
OnRemoteBatteryChanged(const Bluetooth::BluetoothRemoteDevice & device,const Bluetooth::DeviceBatteryInfo & batteryInfo)81     void OnRemoteBatteryChanged(const Bluetooth::BluetoothRemoteDevice& device,
82         const Bluetooth::DeviceBatteryInfo& batteryInfo) override {};
83 private:
84     ProxyListener listener_;
85 };
86 
OnPairStatusChanged(const Bluetooth::BluetoothRemoteDevice & device,int status,int cause)87 void ProxyPairStatusObserver::OnPairStatusChanged(const Bluetooth::BluetoothRemoteDevice& device, int status, int cause)
88 {
89     std::string address = device.GetDeviceAddr();
90     char anomizeAddress[BT_MAC_LEN] = {0};
91     ConvertAnonymizeMacAddress(anomizeAddress, BT_MAC_LEN, address.c_str(), BT_MAC_LEN);
92     CONN_LOGW(CONN_PROXY, "pair status changed %{public}s to %{public}d, cause=%{public}d",
93         anomizeAddress, status, cause);
94     if (status ==  OHOS::Bluetooth::PAIR_NONE) {
95         if (listener_ != nullptr) {
96             listener_(address.c_str(), SOFTBUS_DEVICE_UNPAIRED);
97         }
98     }
99 }
100 }
101 
RegisterHfpListener(const ProxyListener listener)102 int32_t RegisterHfpListener(const ProxyListener listener)
103 {
104     CONN_CHECK_AND_RETURN_RET_LOGE(listener != nullptr, SOFTBUS_INVALID_PARAM, CONN_PROXY, "listener is null");
105     std::shared_ptr<OHOS::SoftBus::ProxyObserver> observer =
106         std::make_shared<OHOS::SoftBus::ProxyObserver>(listener);
107     OHOS::Bluetooth::HandsFreeAudioGateway::GetProfile()->RegisterObserver(observer);
108     std::shared_ptr<OHOS::SoftBus::ProxyPairStatusObserver> pairStatusobserver =
109         std::make_shared<OHOS::SoftBus::ProxyPairStatusObserver>(listener);
110     OHOS::Bluetooth::BluetoothHost::GetDefaultHost().RegisterRemoteDeviceObserver(pairStatusobserver);
111     return SOFTBUS_OK;
112 }
113 
ConvertRealMacToHashMac(const std::string addr)114 static std::string ConvertRealMacToHashMac(const std::string addr)
115 {
116     uint8_t hashAddr[SHA_256_HASH_LEN] = { 0 };
117     int32_t ret = SoftBusGenerateStrHash(reinterpret_cast<const unsigned char *>(addr.c_str()),
118         addr.length(), hashAddr);
119     CONN_CHECK_AND_RETURN_RET_LOGE(ret == SOFTBUS_OK, "", CONN_PROXY, "hash failed ret=%{public}d", ret);
120     char hashAddrStr[HEXIFY_LEN(SHA_256_HASH_LEN)] = {0};
121     ret = ConvertBytesToHexString(hashAddrStr, sizeof(hashAddrStr), hashAddr, sizeof(hashAddr));
122     CONN_CHECK_AND_RETURN_RET_LOGE(ret == SOFTBUS_OK, "", CONN_PROXY, "convert hex string failed, ret=%{public}d", ret);
123     return std::string(hashAddrStr).substr(SHA_256_HASH_LEN, SHA_256_HASH_LEN);
124 }
125 
IsPairedDevice(const char * addr,bool isRealMac)126 bool IsPairedDevice(const char *addr, bool isRealMac)
127 {
128     CONN_CHECK_AND_RETURN_RET_LOGE(addr != nullptr, false, CONN_PROXY, "addr is null");
129     std::vector<OHOS::Bluetooth::BluetoothRemoteDevice> remoteDeviceLists;
130     int32_t ret = OHOS::Bluetooth::BluetoothHost::GetDefaultHost().GetPairedDevices(OHOS::Bluetooth::BT_TRANSPORT_BREDR,
131         remoteDeviceLists);
132     CONN_CHECK_AND_RETURN_RET_LOGE(ret == 0, false, CONN_PROXY, "GetPairedDevices failed, ret=%{public}d", ret);
133     for (const auto &device : remoteDeviceLists) {
134         int32_t state = 0;
135         device.GetPairState(state);
136         CONN_LOGI(CONN_PROXY, "pair state=%{public}d", state);
137         if (state == OHOS::Bluetooth::PAIR_PAIRED &&
138             ((isRealMac && StrCmpIgnoreCase(device.GetDeviceAddr().c_str(), addr) == 0) ||
139             (!isRealMac && StrCmpIgnoreCase(ConvertRealMacToHashMac(device.GetDeviceAddr()).c_str(), addr) == 0))) {
140             return true;
141         }
142     }
143     return false;
144 }
145 
GetRealMac(char * realAddr,uint32_t realAddrLen,const char * hashAddr)146 int32_t GetRealMac(char *realAddr, uint32_t realAddrLen, const char *hashAddr)
147 {
148     CONN_CHECK_AND_RETURN_RET_LOGE(realAddr != nullptr, SOFTBUS_INVALID_PARAM, CONN_PROXY, "realAddr is null");
149     CONN_CHECK_AND_RETURN_RET_LOGE(hashAddr != nullptr, SOFTBUS_INVALID_PARAM, CONN_PROXY, "hashAddr is null");
150     std::vector<OHOS::Bluetooth::BluetoothRemoteDevice> remoteDeviceLists;
151     int32_t ret = OHOS::Bluetooth::BluetoothHost::GetDefaultHost().GetPairedDevices(OHOS::Bluetooth::BT_TRANSPORT_BREDR,
152         remoteDeviceLists);
153     CONN_CHECK_AND_RETURN_RET_LOGE(ret == 0, SOFTBUS_CONN_PROXY_INTERNAL_ERR,
154         CONN_PROXY, "GetPairedDevices failed, ret=%{public}d", ret);
155     for (const auto &device : remoteDeviceLists) {
156         int32_t state = 0;
157         device.GetPairState(state);
158         CONN_LOGD(CONN_PROXY, "pair state=%{public}d", state);
159         if (state == OHOS::Bluetooth::PAIR_PAIRED &&
160             StrCmpIgnoreCase(ConvertRealMacToHashMac(device.GetDeviceAddr()).c_str(), hashAddr) == 0) {
161             if (strncpy_s(realAddr, realAddrLen, device.GetDeviceAddr().c_str(), realAddrLen - 1) != EOK) {
162                 CONN_LOGE(CONN_PROXY, "copy real mac address failed");
163                 return SOFTBUS_STRCPY_ERR;
164             }
165             return SOFTBUS_OK;
166         }
167     }
168     CONN_LOGE(CONN_PROXY, "not found matching addr");
169     return SOFTBUS_NOT_FIND;
170 }