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