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 #ifndef LOG_TAG
16 #define LOG_TAG "bt_ipc_a2dp_src_observer_stub"
17 #endif
18 
19 #include "bluetooth_a2dp_src_observer_stub.h"
20 #include "bluetooth_errorcode.h"
21 #include "bluetooth_log.h"
22 #include "raw_address.h"
23 
24 namespace OHOS {
25 namespace Bluetooth {
26 using namespace OHOS::bluetooth;
BluetoothA2dpSrcObserverStub()27 BluetoothA2dpSrcObserverStub::BluetoothA2dpSrcObserverStub()
28 {
29     HILOGD("start.");
30     memberFuncMap_[static_cast<uint32_t>(
31         BluetoothA2dpSourceObserverInterfaceCode::BT_A2DP_SRC_OBSERVER_CONNECTION_STATE_CHANGED)] =
32         BluetoothA2dpSrcObserverStub::OnConnectionStateChangedInner;
33     memberFuncMap_[static_cast<uint32_t>(
34         BluetoothA2dpSourceObserverInterfaceCode::BT_A2DP_SRC_OBSERVER_PLAYING_STATUS_CHANGED)] =
35         BluetoothA2dpSrcObserverStub::OnPlayingStatusChangedInner;
36     memberFuncMap_[static_cast<uint32_t>(
37         BluetoothA2dpSourceObserverInterfaceCode::BT_A2DP_SRC_OBSERVER_CONFIGURATION_CHANGED)] =
38         BluetoothA2dpSrcObserverStub::OnConfigurationChangedInner;
39     memberFuncMap_[static_cast<uint32_t>(
40         BluetoothA2dpSourceObserverInterfaceCode::BT_A2DP_SRC_OBSERVER_MEDIASTACK_CHANGED)] =
41         BluetoothA2dpSrcObserverStub::OnMediaStackChangedInner;
42     memberFuncMap_[static_cast<uint32_t>(
43         BluetoothA2dpSourceObserverInterfaceCode::BT_A2DP_SRC_OBSERVER_VIRTUALDEVICE_CHANGED)] =
44         BluetoothA2dpSrcObserverStub::OnVirtualDeviceChangedInner;
45 }
46 
~BluetoothA2dpSrcObserverStub()47 BluetoothA2dpSrcObserverStub::~BluetoothA2dpSrcObserverStub()
48 {
49     HILOGD("start.");
50     memberFuncMap_.clear();
51 }
52 
OnRemoteRequest(uint32_t code,MessageParcel & data,MessageParcel & reply,MessageOption & option)53 int BluetoothA2dpSrcObserverStub::OnRemoteRequest(
54     uint32_t code, MessageParcel &data, MessageParcel &reply, MessageOption &option)
55 {
56     HILOGD("cmd = %{public}d, flags= %{public}d", code, option.GetFlags());
57     if (BluetoothA2dpSrcObserverStub::GetDescriptor() != data.ReadInterfaceToken()) {
58         HILOGI("local descriptor is not equal to remote");
59         return ERR_INVALID_STATE;
60     }
61     auto itFunc = memberFuncMap_.find(code);
62     if (itFunc != memberFuncMap_.end()) {
63         auto memberFunc = itFunc->second;
64         if (memberFunc != nullptr) {
65             return memberFunc(this, data, reply);
66         }
67     }
68     HILOGW("default case, need check.");
69     return IPCObjectStub::OnRemoteRequest(code, data, reply, option);
70 }
71 
OnConnectionStateChangedInner(BluetoothA2dpSrcObserverStub * stub,MessageParcel & data,MessageParcel & reply)72 int32_t BluetoothA2dpSrcObserverStub::OnConnectionStateChangedInner(
73     BluetoothA2dpSrcObserverStub *stub, MessageParcel &data, MessageParcel &reply)
74 {
75     std::string addr = data.ReadString();
76     int state = data.ReadInt32();
77     int cause = data.ReadInt32();
78 
79     stub->OnConnectionStateChanged(RawAddress(addr), state, cause);
80 
81     return NO_ERROR;
82 }
83 
OnPlayingStatusChangedInner(BluetoothA2dpSrcObserverStub * stub,MessageParcel & data,MessageParcel & reply)84 int32_t BluetoothA2dpSrcObserverStub::OnPlayingStatusChangedInner(
85     BluetoothA2dpSrcObserverStub *stub, MessageParcel &data, MessageParcel &reply)
86 {
87     std::string addr = data.ReadString();
88     int playingState = data.ReadInt32();
89     int error = data.ReadInt32();
90 
91     stub->OnPlayingStatusChanged(RawAddress(addr), playingState, error);
92 
93     return NO_ERROR;
94 }
95 
OnConfigurationChangedInner(BluetoothA2dpSrcObserverStub * stub,MessageParcel & data,MessageParcel & reply)96 int32_t BluetoothA2dpSrcObserverStub::OnConfigurationChangedInner(
97     BluetoothA2dpSrcObserverStub *stub, MessageParcel &data, MessageParcel &reply)
98 {
99     std::string addr = data.ReadString();
100     std::shared_ptr<BluetoothA2dpCodecInfo> info(data.ReadParcelable<BluetoothA2dpCodecInfo>());
101     if (!info) {
102         return BT_ERR_IPC_TRANS_FAILED;
103     }
104     int error = data.ReadInt32();
105 
106     stub->OnConfigurationChanged(RawAddress(addr), *info, error);
107     return NO_ERROR;
108 }
109 
OnMediaStackChangedInner(BluetoothA2dpSrcObserverStub * stub,MessageParcel & data,MessageParcel & reply)110 int32_t BluetoothA2dpSrcObserverStub::OnMediaStackChangedInner(
111     BluetoothA2dpSrcObserverStub *stub, MessageParcel &data, MessageParcel &reply)
112 {
113     std::string addr = data.ReadString();
114     int action = data.ReadInt32();
115     stub->OnMediaStackChanged(RawAddress(addr), action);
116     return BT_NO_ERROR;
117 }
118 
OnVirtualDeviceChangedInner(BluetoothA2dpSrcObserverStub * stub,MessageParcel & data,MessageParcel & reply)119 ErrCode BluetoothA2dpSrcObserverStub::OnVirtualDeviceChangedInner(
120     BluetoothA2dpSrcObserverStub *stub, MessageParcel &data, MessageParcel &reply)
121 {
122     int action = data.ReadInt32();
123     std::string addr = data.ReadString();
124 
125     stub->OnVirtualDeviceChanged(action, addr);
126 
127     return BT_NO_ERROR;
128 }
129 }  // namespace Bluetooth
130 }  // namespace OHOS