• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2021 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_sink_stub.h"
17 #include "bluetooth_log.h"
18 #include "ipc_types.h"
19 #include "parcel_bt_uuid.h"
20 #include "raw_address.h"
21 
22 namespace OHOS {
23 namespace Bluetooth {
24 using namespace bluetooth;
BluetoothA2dpSinkStub()25 BluetoothA2dpSinkStub::BluetoothA2dpSinkStub()
26 {
27     HILOGD("%{public}s start.", __func__);
28     memberFuncMap_[static_cast<uint32_t>(IBluetoothA2dpSink::Code::BT_A2DP_SINK_CONNECT)] =
29         &BluetoothA2dpSinkStub::ConnectInner;
30     memberFuncMap_[static_cast<uint32_t>(IBluetoothA2dpSink::Code::BT_A2DP_SINK_DISCONNECT)] =
31         &BluetoothA2dpSinkStub::DisconnectInner;
32     memberFuncMap_[static_cast<uint32_t>(IBluetoothA2dpSink::Code::BT_A2DP_SINK_REGISTER_OBSERVER)] =
33         &BluetoothA2dpSinkStub::RegisterObserverInner;
34     memberFuncMap_[static_cast<uint32_t>(IBluetoothA2dpSink::Code::BT_A2DP_SINK_DEREGISTER_OBSERVER)] =
35         &BluetoothA2dpSinkStub::DeregisterObserverInner;
36     memberFuncMap_[static_cast<uint32_t>(IBluetoothA2dpSink::Code::BT_A2DP_SINK_GET_DEVICE_BY_STATES)] =
37         &BluetoothA2dpSinkStub::GetDevicesByStatesInner;
38     memberFuncMap_[static_cast<uint32_t>(IBluetoothA2dpSink::Code::BT_A2DP_SINK_GET_DEVICE_STATE)] =
39         &BluetoothA2dpSinkStub::GetDeviceStateInner;
40     memberFuncMap_[static_cast<uint32_t>(IBluetoothA2dpSink::Code::BT_A2DP_SINK_GET_PLAYING_STATE)] =
41         &BluetoothA2dpSinkStub::GetPlayingStateInner;
42     memberFuncMap_[static_cast<uint32_t>(IBluetoothA2dpSink::Code::BT_A2DP_SINK_SET_CONNECT_STRATEGY)] =
43         &BluetoothA2dpSinkStub::SetConnectStrategyInner;
44     memberFuncMap_[static_cast<uint32_t>(IBluetoothA2dpSink::Code::BT_A2DP_SINK_GET_CONNECT_STRATEGY)] =
45         &BluetoothA2dpSinkStub::GetConnectStrategyInner;
46 }
47 
~BluetoothA2dpSinkStub()48 BluetoothA2dpSinkStub::~BluetoothA2dpSinkStub()
49 {
50     HILOGD("%{public}s start.", __func__);
51     memberFuncMap_.clear();
52 }
53 
OnRemoteRequest(uint32_t code,MessageParcel & data,MessageParcel & reply,MessageOption & option)54 int BluetoothA2dpSinkStub::OnRemoteRequest(
55     uint32_t code, MessageParcel &data, MessageParcel &reply, MessageOption &option)
56 {
57     HILOGD("BluetoothA2dpSinkStub::OnRemoteRequest, cmd = %{public}d, flags= %{public}d", code, option.GetFlags());
58     std::u16string descriptor = BluetoothA2dpSinkStub::GetDescriptor();
59     std::u16string remoteDescriptor = data.ReadInterfaceToken();
60     if (descriptor != remoteDescriptor) {
61         HILOGI("local descriptor is not equal to remote");
62         return ERR_INVALID_STATE;
63     }
64     auto itFunc = memberFuncMap_.find(code);
65     if (itFunc != memberFuncMap_.end()) {
66         auto memberFunc = itFunc->second;
67         if (memberFunc != nullptr) {
68             return (this->*memberFunc)(data, reply);
69         }
70     }
71     HILOGW("BluetoothA2dpSinkStub::OnRemoteRequest, default case, need check.");
72     return IPCObjectStub::OnRemoteRequest(code, data, reply, option);
73 }
74 
ConnectInner(MessageParcel & data,MessageParcel & reply)75 ErrCode BluetoothA2dpSinkStub::ConnectInner(MessageParcel &data, MessageParcel &reply)
76 {
77     std::string addr = data.ReadString();
78 
79     int result = Connect(RawAddress(addr));
80 
81     bool ret = reply.WriteInt32(result);
82     if (!ret) {
83         HILOGE("BluetoothA2dpSinkStub: ConnectInner reply writing failed in: %{public}s.", __func__);
84         return TRANSACTION_ERR;
85     }
86 
87     return NO_ERROR;
88 }
89 
DisconnectInner(MessageParcel & data,MessageParcel & reply)90 ErrCode BluetoothA2dpSinkStub::DisconnectInner(MessageParcel &data, MessageParcel &reply)
91 {
92     std::string addr = data.ReadString();
93 
94     int result = Disconnect(RawAddress(addr));
95 
96     bool ret = reply.WriteInt32(result);
97     if (!ret) {
98         HILOGE("BluetoothA2dpSinkStub: DisconnectInner reply writing failed in: %{public}s.", __func__);
99         return TRANSACTION_ERR;
100     }
101 
102     return NO_ERROR;
103 }
104 
RegisterObserverInner(MessageParcel & data,MessageParcel & reply)105 ErrCode BluetoothA2dpSinkStub::RegisterObserverInner(MessageParcel &data, MessageParcel &reply)
106 {
107     sptr<IRemoteObject> remote = data.ReadRemoteObject();
108     const sptr<IBluetoothA2dpSinkObserver> observer = OHOS::iface_cast<IBluetoothA2dpSinkObserver>(remote);
109     RegisterObserver(observer);
110 
111     return NO_ERROR;
112 }
113 
DeregisterObserverInner(MessageParcel & data,MessageParcel & reply)114 ErrCode BluetoothA2dpSinkStub::DeregisterObserverInner(MessageParcel &data, MessageParcel &reply)
115 {
116     sptr<IRemoteObject> remote = data.ReadRemoteObject();
117     const sptr<IBluetoothA2dpSinkObserver> observer = OHOS::iface_cast<IBluetoothA2dpSinkObserver>(remote);
118     DeregisterObserver(observer);
119 
120     return NO_ERROR;
121 }
122 
GetDevicesByStatesInner(MessageParcel & data,MessageParcel & reply)123 ErrCode BluetoothA2dpSinkStub::GetDevicesByStatesInner(MessageParcel &data, MessageParcel &reply)
124 {
125     std::vector<int32_t> states = {};
126     int32_t stateSize = data.ReadInt32();
127 
128     for (int i = 0; i < stateSize; i++) {
129         int32_t state = data.ReadInt32();
130         states.push_back(state);
131     }
132 
133     std::vector<RawAddress> rawAdds = GetDevicesByStates(states);
134     reply.WriteInt32(rawAdds.size());
135 
136     for (auto rawAdd : rawAdds) {
137         if (!reply.WriteString(rawAdd.GetAddress())) {
138             HILOGE("GetConnectedDevicesInner: write WriteString failed");
139             return ERR_INVALID_STATE;
140         }
141     }
142     return NO_ERROR;
143 }
144 
GetDeviceStateInner(MessageParcel & data,MessageParcel & reply)145 ErrCode BluetoothA2dpSinkStub::GetDeviceStateInner(MessageParcel &data, MessageParcel &reply)
146 {
147     std::string addr = data.ReadString();
148 
149     int result = GetDeviceState(RawAddress(addr));
150 
151     bool ret = reply.WriteInt32(result);
152     if (!ret) {
153         HILOGE("BluetoothA2dpSinkStub: GetDeviceStateInner reply writing failed in: %{public}s.", __func__);
154         return TRANSACTION_ERR;
155     }
156 
157     return NO_ERROR;
158 }
159 
GetPlayingStateInner(MessageParcel & data,MessageParcel & reply)160 ErrCode BluetoothA2dpSinkStub::GetPlayingStateInner(MessageParcel &data, MessageParcel &reply)
161 {
162     std::string addr = data.ReadString();
163 
164     int result = GetPlayingState(RawAddress(addr));
165 
166     bool ret = reply.WriteInt32(result);
167     if (!ret) {
168         HILOGE("BluetoothA2dpSinkStub: GetPlayingStateInner reply writing failed in: %{public}s.", __func__);
169         return TRANSACTION_ERR;
170     }
171 
172     return NO_ERROR;
173 }
174 
SetConnectStrategyInner(MessageParcel & data,MessageParcel & reply)175 ErrCode BluetoothA2dpSinkStub::SetConnectStrategyInner(MessageParcel &data, MessageParcel &reply)
176 {
177     std::string addr = data.ReadString();
178     int strategy = data.ReadInt32();
179 
180     int result = SetConnectStrategy(RawAddress(addr), strategy);
181 
182     bool ret = reply.WriteInt32(result);
183     if (!ret) {
184         HILOGE("BluetoothA2dpSinkStub: SetConnectStrategyInner reply writing failed in: %{public}s.", __func__);
185         return TRANSACTION_ERR;
186     }
187 
188     return NO_ERROR;
189 }
190 
GetConnectStrategyInner(MessageParcel & data,MessageParcel & reply)191 ErrCode BluetoothA2dpSinkStub::GetConnectStrategyInner(MessageParcel &data, MessageParcel &reply)
192 {
193     std::string addr = data.ReadString();
194     int result = GetConnectStrategy(RawAddress(addr));
195 
196     bool ret = reply.WriteInt32(result);
197     if (!ret) {
198         HILOGE("BluetoothA2dpSinkStub: GetConnectStrategyInner reply writing failed in: %{public}s.", __func__);
199         return TRANSACTION_ERR;
200     }
201 
202     return NO_ERROR;
203 }
204 }  // namespace Bluetooth
205 }  // namespace OHOS