1  /*
2   * Copyright (C) 2021-2022 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_src_observer_proxy.h"
17  #include "bluetooth_log.h"
18  
19  namespace OHOS {
20  namespace Bluetooth {
OnConnectionStateChanged(const RawAddress & device,int state,int cause)21  void BluetoothA2dpSrcObserverProxy::OnConnectionStateChanged(const RawAddress &device, int state, int cause)
22  {
23      MessageParcel data;
24      if (!data.WriteInterfaceToken(BluetoothA2dpSrcObserverProxy::GetDescriptor())) {
25          HILOGE("BluetoothA2dpSrcObserverProxy::OnConnectionStateChanged WriteInterfaceToken error");
26          return;
27      }
28      if (!data.WriteString(device.GetAddress())) {
29          HILOGE("BluetoothA2dpSrcObserverProxy::OnConnectionStateChanged write device error");
30          return;
31      }
32      if (!data.WriteInt32(state)) {
33          HILOGE("BluetoothA2dpSrcObserverProxy::OnConnectionStateChanged state error");
34          return;
35      }
36      if (!data.WriteInt32(cause)) {
37          HILOGE("BluetoothA2dpSrcObserverProxy::OnConnectionStateChanged cause error");
38          return;
39      }
40  
41      MessageParcel reply;
42      MessageOption option {
43          MessageOption::TF_ASYNC
44      };
45  
46      ErrCode result = InnerTransact(BT_A2DP_SRC_OBSERVER_CONNECTION_STATE_CHANGED, option, data, reply);
47      if (result != NO_ERROR) {
48          HILOGE("BluetoothA2dpSrcObserverProxy::OnConnectionStateChanged done fail, error: %{public}d", result);
49          return;
50      }
51  }
52  
OnPlayingStatusChanged(const RawAddress & device,int playingState,int error)53  void BluetoothA2dpSrcObserverProxy::OnPlayingStatusChanged(const RawAddress &device, int playingState, int error)
54  {
55      MessageParcel data;
56      if (!data.WriteInterfaceToken(BluetoothA2dpSrcObserverProxy::GetDescriptor())) {
57          HILOGE("BluetoothA2dpSrcObserverProxy::OnPlayingStatusChanged WriteInterfaceToken error");
58          return;
59      }
60      if (!data.WriteString(device.GetAddress())) {
61          HILOGE("BluetoothA2dpSrcObserverProxy::OnPlayingStatusChanged write device error");
62          return;
63      }
64      if (!data.WriteInt32(playingState)) {
65          HILOGE("BluetoothA2dpSrcObserverProxy::OnPlayingStatusChanged playingState error");
66          return;
67      }
68  
69      if (!data.WriteInt32(error)) {
70          HILOGE("BluetoothA2dpSrcObserverProxy::OnPlayingStatusChanged error error");
71          return;
72      }
73  
74      MessageParcel reply;
75      MessageOption option {
76          MessageOption::TF_ASYNC
77      };
78  
79      ErrCode result = InnerTransact(BT_A2DP_SRC_OBSERVER_PLAYING_STATUS_CHANGED, option, data, reply);
80      if (result != NO_ERROR) {
81          HILOGE("BluetoothA2dpSrcObserverProxy::OnPlayingStatusChanged done fail, error: %{public}d", result);
82          return;
83      }
84  }
85  
OnConfigurationChanged(const RawAddress & device,const BluetoothA2dpCodecInfo & info,int error)86  void BluetoothA2dpSrcObserverProxy::OnConfigurationChanged(
87      const RawAddress &device, const BluetoothA2dpCodecInfo &info, int error)
88  {
89      MessageParcel data;
90      if (!data.WriteInterfaceToken(BluetoothA2dpSrcObserverProxy::GetDescriptor())) {
91          HILOGE("BluetoothA2dpSrcObserverProxy::OnConfigurationChanged WriteInterfaceToken error");
92          return;
93      }
94      if (!data.WriteString(device.GetAddress())) {
95          HILOGE("BluetoothA2dpSrcObserverProxy::OnConfigurationChanged write device error");
96          return;
97      }
98      if (!data.WriteParcelable(&info)) {
99          HILOGE("BluetoothA2dpSrcObserverProxy::OnConfigurationChanged transport error");
100          return;
101      }
102      if (!data.WriteInt32(error)) {
103          HILOGE("BluetoothA2dpSrcObserverProxy::OnConfigurationChanged error error");
104          return;
105      }
106  
107      MessageParcel reply;
108      MessageOption option {
109          MessageOption::TF_ASYNC
110      };
111  
112      ErrCode result = InnerTransact(BT_A2DP_SRC_OBSERVER_CONFIGURATION_CHANGED, option, data, reply);
113      if (result != NO_ERROR) {
114          HILOGE("BluetoothA2dpSrcObserverProxy::OnConfigurationChanged done fail, error: %{public}d", result);
115          return;
116      }
117  }
118  
OnMediaStackChanged(const RawAddress & device,int action)119  void BluetoothA2dpSrcObserverProxy::OnMediaStackChanged(
120      const RawAddress &device, int action)
121  {
122      MessageParcel data;
123      if (!data.WriteInterfaceToken(BluetoothA2dpSrcObserverProxy::GetDescriptor())) {
124          HILOGE("BluetoothA2dpSrcObserverProxy::OnMediaStackChanged WriteInterfaceToken error");
125          return;
126      }
127      if (!data.WriteString(device.GetAddress())) {
128          HILOGE("BluetoothA2dpSrcObserverProxy::OnMediaStackChanged write device error");
129          return;
130      }
131      if (!data.WriteInt32(action)) {
132          HILOGE("BluetoothA2dpSrcObserverProxy::OnMediaStackChanged error error");
133          return;
134      }
135  
136      MessageParcel reply;
137      MessageOption option {
138          MessageOption::TF_ASYNC
139      };
140  
141      ErrCode result = InnerTransact(BT_A2DP_SRC_OBSERVER_MEDIASTACK_CHANGED, option, data, reply);
142      if (result != NO_ERROR) {
143          HILOGE("BluetoothA2dpSrcObserverProxy::OnMediaStackChanged done fail, error: %{public}d", result);
144          return;
145      }
146  }
147  
InnerTransact(uint32_t code,MessageOption & flags,MessageParcel & data,MessageParcel & reply)148  ErrCode BluetoothA2dpSrcObserverProxy::InnerTransact(
149      uint32_t code, MessageOption &flags, MessageParcel &data, MessageParcel &reply)
150  {
151      auto remote = Remote();
152      if (remote == nullptr) {
153          HILOGE("[InnerTransact] fail: get Remote fail code %{public}d", code);
154          return ERR_DEAD_OBJECT;
155      }
156      int err = remote->SendRequest(code, data, reply, flags);
157      switch (err) {
158          case NO_ERROR: {
159              return ERR_OK;
160          }
161          case DEAD_OBJECT: {
162              HILOGE("[InnerTransact] fail: ipcErr=%{public}d code %{public}d", err, code);
163              return ERR_DEAD_OBJECT;
164          }
165          default: {
166              HILOGE("[InnerTransact] fail: ipcErr=%{public}d code %{public}d", err, code);
167              return TRANSACTION_ERR;
168          }
169      }
170  }
171  
OnVirtualDeviceChanged(int32_t action,std::string address)172  void BluetoothA2dpSrcObserverProxy::OnVirtualDeviceChanged(int32_t action, std::string address)
173  {
174      MessageParcel data;
175      if (!data.WriteInterfaceToken(BluetoothA2dpSrcObserverProxy::GetDescriptor())) {
176          HILOGE("BluetoothA2dpSrcObserverProxy::OnVirtualDeviceChanged WriteInterfaceToken error");
177          return;
178      }
179      if (!data.WriteInt32(action)) {
180          HILOGE("BluetoothA2dpSrcObserverProxy::OnVirtualDeviceChanged WriteInt32 error");
181          return;
182      }
183      if (!data.WriteString(address)) {
184          HILOGE("BluetoothA2dpSrcObserverProxy::OnVirtualDeviceChanged WriteString error");
185          return;
186      }
187      MessageParcel reply;
188      MessageOption option {
189          MessageOption::TF_ASYNC
190      };
191      ErrCode result = InnerTransact(BT_A2DP_SRC_OBSERVER_VIRTUALDEVICE_CHANGED, option, data, reply);
192      if (result != NO_ERROR) {
193          HILOGE("BluetoothA2dpSrcObserverProxy::OnVirtualDeviceChanged done fail, error: %{public}d", result);
194          return;
195      }
196  }
197  }  // namespace Bluetooth
198  }  // namespace OHOS