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 #include <hdf_log.h>
16 #include "raw_address.h"
17 #include "bluetooth_a2dp_audio_src_observer_stub.h"
18 
19 namespace OHOS {
20 namespace Bluetooth {
21 using namespace OHOS::bluetooth;
BluetoothA2dpAudioSrcObserverStub()22 BluetoothA2dpAudioSrcObserverStub::BluetoothA2dpAudioSrcObserverStub()
23 {
24     HDF_LOGI("%{public}s start.", __func__);
25     funcMap_[static_cast<uint32_t>(IBluetoothA2dpSourceObserver::Code::BT_A2DP_SRC_OBSERVER_CONNECTION_STATE_CHANGED)] =
26         &BluetoothA2dpAudioSrcObserverStub::OnConnectionStateChangedInner;
27     funcMap_[static_cast<uint32_t>(IBluetoothA2dpSourceObserver::Code::BT_A2DP_SRC_OBSERVER_PLAYING_STATUS_CHANGED)] =
28         &BluetoothA2dpAudioSrcObserverStub::OnPlayingStatusChangedInner;
29     funcMap_[static_cast<uint32_t>(IBluetoothA2dpSourceObserver::Code::BT_A2DP_SRC_OBSERVER_CONFIGURATION_CHANGED)] =
30         &BluetoothA2dpAudioSrcObserverStub::OnConfigurationChangedInner;
31     funcMap_[static_cast<uint32_t>(IBluetoothA2dpSourceObserver::Code::BT_A2DP_SRC_OBSERVER_MEDIASTACK_CHANGED)] =
32         &BluetoothA2dpAudioSrcObserverStub::OnMediaStackChangedInner;
33 }
34 
~BluetoothA2dpAudioSrcObserverStub()35 BluetoothA2dpAudioSrcObserverStub::~BluetoothA2dpAudioSrcObserverStub()
36 {
37     HDF_LOGI("%{public}s start.", __func__);
38     funcMap_.clear();
39 }
40 
OnRemoteRequest(uint32_t code,MessageParcel & data,MessageParcel & reply,MessageOption & option)41 int BluetoothA2dpAudioSrcObserverStub::OnRemoteRequest(
42     uint32_t code, MessageParcel &data, MessageParcel &reply, MessageOption &option)
43 {
44     HDF_LOGI("BluetoothA2dpAudioSrcObserverStub::OnRemoteRequest, cmd=%{public}d, flags= %d", code, option.GetFlags());
45     std::u16string descriptor = BluetoothA2dpAudioSrcObserverStub::GetDescriptor();
46     std::u16string remoteDescriptor = data.ReadInterfaceToken();
47     if (descriptor != remoteDescriptor) {
48         HDF_LOGE("local descriptor is not equal to remote");
49         return ERR_INVALID_STATE;
50     }
51     auto itFunc = funcMap_.find(code);
52     if (itFunc != funcMap_.end()) {
53         auto memberFunc = itFunc->second;
54         if (memberFunc != nullptr) {
55             return (this->*memberFunc)(data, reply);
56         }
57     }
58     HDF_LOGI("BluetoothA2dpAudioSrcObserverStub::OnRemoteRequest, default case, need check.");
59     return IPCObjectStub::OnRemoteRequest(code, data, reply, option);
60 }
61 
OnConnectionStateChangedInner(MessageParcel & data,MessageParcel & reply)62 ErrCode BluetoothA2dpAudioSrcObserverStub::OnConnectionStateChangedInner(MessageParcel &data, MessageParcel &reply)
63 {
64     std::string addr = data.ReadString();
65     int state = data.ReadInt32();
66     int cause = data.ReadInt32();
67     HDF_LOGI("BluetoothA2dpAudioSrcObserverStub::OnConnectionStateChangedInner");
68     OnConnectionStateChanged(RawAddress(addr), state, cause);
69 
70     return NO_ERROR;
71 }
72 
OnPlayingStatusChangedInner(MessageParcel & data,MessageParcel & reply)73 ErrCode BluetoothA2dpAudioSrcObserverStub::OnPlayingStatusChangedInner(MessageParcel &data, MessageParcel &reply)
74 {
75     std::string addr = data.ReadString();
76     int playingState = data.ReadInt32();
77     int error = data.ReadInt32();
78     HDF_LOGI("BluetoothA2dpAudioSrcObserverStub::OnPlayingStatusChangedInner");
79     OnPlayingStatusChanged(RawAddress(addr), playingState, error);
80 
81     return NO_ERROR;
82 }
83 
OnConfigurationChangedInner(MessageParcel & data,MessageParcel & reply)84 ErrCode BluetoothA2dpAudioSrcObserverStub::OnConfigurationChangedInner(MessageParcel &data, MessageParcel &reply)
85 {
86     std::string addr = data.ReadString();
87     std::shared_ptr<BluetoothA2dpCodecInfo> info(data.ReadParcelable<BluetoothA2dpCodecInfo>());
88     if (info == nullptr) {
89         HDF_LOGE("Read a2dp code info failed");
90         return TRANSACTION_ERR;
91     }
92     int error = data.ReadInt32();
93     HDF_LOGI("BluetoothA2dpAudioSrcObserverStub::OnConfigurationChangedInner");
94     OnConfigurationChanged(RawAddress(addr), *info, error);
95 
96     return NO_ERROR;
97 }
98 
OnMediaStackChangedInner(MessageParcel & data,MessageParcel & reply)99 int32_t BluetoothA2dpAudioSrcObserverStub::OnMediaStackChangedInner(MessageParcel &data, MessageParcel &reply)
100 {
101     std::string addr = data.ReadString();
102     int action = data.ReadInt32();
103     HDF_LOGI("BluetoothA2dpAudioSrcObserverStub::OnMediaStackChangedInner");
104     OnMediaStackChanged(RawAddress(addr), action);
105 
106     return NO_ERROR;
107 }
108 }
109 }