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 #ifndef LOG_TAG
16 #define LOG_TAG "bt_cj_hfp_ag"
17 #endif
18
19 #include "bluetooth_hfp_ag_impl.h"
20
21 #include <vector>
22
23 #include "bluetooth_errorcode.h"
24 #include "bluetooth_utils.h"
25 #include "cj_lambda.h"
26 #include "napi_bluetooth_utils.h"
27
28 using namespace OHOS::FFI;
29
30 namespace OHOS {
31 namespace Bluetooth {
32 std::shared_ptr<HandsFreeUnitObserverImpl> HandsFreeAudioGatewayImpl::observer_ =
33 std::make_shared<HandsFreeUnitObserverImpl>();
34 bool HandsFreeAudioGatewayImpl::isRegistered_ = false;
35
GetConnectionDevices(int32_t * errCode)36 CArrString HandsFreeAudioGatewayImpl::GetConnectionDevices(int32_t* errCode)
37 {
38 HILOGD("enter");
39 HandsFreeAudioGateway* profile = HandsFreeAudioGateway::GetProfile();
40 if (profile == nullptr) {
41 HILOGE("profile is nullptr.");
42 *errCode = BT_ERR_INTERNAL_ERROR;
43 return CArrString { 0 };
44 }
45 std::vector<BluetoothRemoteDevice> devices;
46 int errorCode = profile->GetConnectedDevices(devices);
47 if (errorCode != BT_NO_ERROR) {
48 HILOGE("bluetooth assert failed.");
49 *errCode = static_cast<int32_t>(errorCode);
50 return CArrString { 0 };
51 }
52 if (devices.size() <= 0) {
53 return CArrString { 0 };
54 }
55 char** deviceVector = static_cast<char**>(malloc(sizeof(char*) * devices.size()));
56 if (deviceVector == nullptr) {
57 HILOGE("deviceVector malloc failed.");
58 *errCode = BT_ERR_INTERNAL_ERROR;
59 return CArrString { 0 };
60 }
61 int64_t i = 0;
62 for (auto& device : devices) {
63 deviceVector[i] = MallocCString(device.GetDeviceAddr());
64 i++;
65 }
66 CArrString ret = CArrString { .head = deviceVector, .size = i };
67 return ret;
68 }
69
GetConnectionState(char * device,int32_t * errCode)70 int32_t HandsFreeAudioGatewayImpl::GetConnectionState(char* device, int32_t* errCode)
71 {
72 HILOGD("enter");
73 int32_t profileState = ProfileConnectionState::STATE_DISCONNECTED;
74 std::string remoteAddr = device;
75 if (!IsValidAddress(remoteAddr)) {
76 HILOGE("Invalid addr");
77 *errCode = static_cast<int32_t>(BT_ERR_INVALID_PARAM);
78 return profileState;
79 }
80 HandsFreeAudioGateway* profile = HandsFreeAudioGateway::GetProfile();
81 if (profile == nullptr) {
82 HILOGE("profile is nullptr.");
83 *errCode = BT_ERR_INTERNAL_ERROR;
84 return profileState;
85 }
86 BluetoothRemoteDevice remoteDevice(remoteAddr, BT_TRANSPORT_BREDR);
87 int32_t state = static_cast<int32_t>(BTConnectState::DISCONNECTED);
88 int32_t errorCode = profile->GetDeviceState(remoteDevice, state);
89 if (errorCode != BT_NO_ERROR) {
90 HILOGE("bluetooth assert failed.");
91 *errCode = errorCode;
92 return profileState;
93 }
94 profileState = GetProfileConnectionState(state);
95 return profileState;
96 }
97
On(int32_t type,int64_t id,int32_t * errCode)98 void HandsFreeAudioGatewayImpl::On(int32_t type, int64_t id, int32_t* errCode)
99 {
100 if (observer_) {
101 auto observerFunc = CJLambda::Create(reinterpret_cast<void (*)(StateChangeParam)>(id));
102 if (!observerFunc) {
103 HILOGE("Register state change event failed");
104 *errCode = BT_ERR_INVALID_PARAM;
105 return;
106 }
107 observer_->RegisterStateChangeFunc(observerFunc);
108 }
109
110 if (!isRegistered_) {
111 HandsFreeAudioGateway* profile = HandsFreeAudioGateway::GetProfile();
112 if (profile == nullptr) {
113 HILOGE("profile is nullptr.");
114 *errCode = BT_ERR_INVALID_PARAM;
115 return;
116 }
117 profile->RegisterObserver(observer_);
118 isRegistered_ = true;
119 }
120 return;
121 }
122 } // namespace Bluetooth
123 } // namespace OHOS