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_hfp_hf_observer_stub"
17 #endif
18 
19 #include "bluetooth_hfp_hf_observer_stub.h"
20 #include "bluetooth_log.h"
21 
22 namespace OHOS {
23 namespace Bluetooth {
BluetoothHfpHfObserverStub()24 BluetoothHfpHfObserverStub::BluetoothHfpHfObserverStub()
25 {
26     HILOGD("start.");
27     memberFuncMap_[static_cast<uint32_t>(
28         BluetoothHfpHfObserverInterfaceCode::BT_HFP_HF_OBSERVER_CONNECTION_STATE_CHANGED)] =
29         BluetoothHfpHfObserverStub::OnConnectionStateChangedInner;
30     memberFuncMap_[static_cast<uint32_t>(
31         BluetoothHfpHfObserverInterfaceCode::BT_HFP_HF_OBSERVER_SCO_STATE_CHANGED)] =
32         BluetoothHfpHfObserverStub::OnScoStateChangedInner;
33     memberFuncMap_[static_cast<uint32_t>(
34         BluetoothHfpHfObserverInterfaceCode::BT_HFP_HF_OBSERVER_CALL_CHANGED)] =
35         BluetoothHfpHfObserverStub::OnCallChangedInner;
36     memberFuncMap_[static_cast<uint32_t>(
37         BluetoothHfpHfObserverInterfaceCode::BT_HFP_HF_OBSERVER_SIGNAL_STRENGTH_CHANGED)] =
38         BluetoothHfpHfObserverStub::OnSignalStrengthChangedInner;
39     memberFuncMap_[static_cast<uint32_t>(
40         BluetoothHfpHfObserverInterfaceCode::BT_HFP_HF_OBSERVER_REGISTRATION_STATUS_CHANGED)] =
41         BluetoothHfpHfObserverStub::OnRegistrationStatusChangedInner;
42     memberFuncMap_[static_cast<uint32_t>(
43         BluetoothHfpHfObserverInterfaceCode::BT_HFP_HF_OBSERVER_ROAMING_STATUS_CHANGED)] =
44         BluetoothHfpHfObserverStub::OnRoamingStatusChangedInner;
45     memberFuncMap_[static_cast<uint32_t>(
46         BluetoothHfpHfObserverInterfaceCode::BT_HFP_HF_OBSERVER_OPERATOR_SELECTION_CHANGED)] =
47         BluetoothHfpHfObserverStub::OnOperatorSelectionChangedInner;
48     memberFuncMap_[static_cast<uint32_t>(
49         BluetoothHfpHfObserverInterfaceCode::BT_HFP_HF_OBSERVER_SUBSCRIBER_NUMBER_CHANGED)] =
50         BluetoothHfpHfObserverStub::OnSubscriberNumberChangedInner;
51     memberFuncMap_[static_cast<uint32_t>(
52         BluetoothHfpHfObserverInterfaceCode::BT_HFP_HF_OBSERVER_VOICE_RECOGNITION_STATUS_CHANGED)] =
53         BluetoothHfpHfObserverStub::OnVoiceRecognitionStatusChangedInner;
54     memberFuncMap_[static_cast<uint32_t>(
55         BluetoothHfpHfObserverInterfaceCode::BT_HFP_HF_OBSERVER_IN_BAND_RING_TONE_CHANGED)] =
56         BluetoothHfpHfObserverStub::OnInBandRingToneChangedInner;
57 
58     HILOGI("ends.");
59 }
60 
~BluetoothHfpHfObserverStub()61 BluetoothHfpHfObserverStub::~BluetoothHfpHfObserverStub()
62 {
63     HILOGD("start.");
64     memberFuncMap_.clear();
65 }
66 
OnRemoteRequest(uint32_t code,MessageParcel & data,MessageParcel & reply,MessageOption & option)67 int BluetoothHfpHfObserverStub::OnRemoteRequest(
68     uint32_t code, MessageParcel &data, MessageParcel &reply, MessageOption &option)
69 {
70     HILOGD("BluetoothHfpHfObserverStub::OnRemoteRequest, cmd = %{public}d, flags= %{public}d", code, option.GetFlags());
71     if (BluetoothHfpHfObserverStub::GetDescriptor() != data.ReadInterfaceToken()) {
72         HILOGI("local descriptor is not equal to remote");
73         return ERR_INVALID_STATE;
74     }
75 
76     auto itFunc = memberFuncMap_.find(code);
77     if (itFunc != memberFuncMap_.end()) {
78         auto memberFunc = itFunc->second;
79         if (memberFunc != nullptr) {
80             return memberFunc(this, data, reply);
81         }
82     }
83     HILOGW("BluetoothHfpHfObserverStub::OnRemoteRequest, default case, need check.");
84     return IPCObjectStub::OnRemoteRequest(code, data, reply, option);
85 }
86 
OnConnectionStateChangedInner(BluetoothHfpHfObserverStub * stub,MessageParcel & data,MessageParcel & reply)87 ErrCode BluetoothHfpHfObserverStub::OnConnectionStateChangedInner(
88     BluetoothHfpHfObserverStub *stub, MessageParcel &data, MessageParcel &reply)
89 {
90     std::shared_ptr<BluetoothRawAddress> device(data.ReadParcelable<BluetoothRawAddress>());
91     if (!device) {
92         return TRANSACTION_ERR;
93     }
94     int state = data.ReadInt32();
95     int cause = data.ReadInt32();
96     stub->OnConnectionStateChanged(*device, state, cause);
97     return NO_ERROR;
98 }
99 
OnScoStateChangedInner(BluetoothHfpHfObserverStub * stub,MessageParcel & data,MessageParcel & reply)100 ErrCode BluetoothHfpHfObserverStub::OnScoStateChangedInner(
101     BluetoothHfpHfObserverStub *stub, MessageParcel &data, MessageParcel &reply)
102 {
103     std::shared_ptr<BluetoothRawAddress> device(data.ReadParcelable<BluetoothRawAddress>());
104     if (!device) {
105         return TRANSACTION_ERR;
106     }
107     int state = data.ReadInt32();
108     stub->OnScoStateChanged(*device, state);
109     return NO_ERROR;
110 }
111 
OnCallChangedInner(BluetoothHfpHfObserverStub * stub,MessageParcel & data,MessageParcel & reply)112 ErrCode BluetoothHfpHfObserverStub::OnCallChangedInner(
113     BluetoothHfpHfObserverStub *stub, MessageParcel &data, MessageParcel &reply)
114 {
115     std::shared_ptr<BluetoothRawAddress> device(data.ReadParcelable<BluetoothRawAddress>());
116     if (!device) {
117         return TRANSACTION_ERR;
118     }
119     sptr<BluetoothHfpHfCall> call = data.ReadParcelable<BluetoothHfpHfCall>();
120     if (!call) {
121         return TRANSACTION_ERR;
122     }
123     stub->OnCallChanged(*device, *call);
124     return NO_ERROR;
125 }
126 
OnSignalStrengthChangedInner(BluetoothHfpHfObserverStub * stub,MessageParcel & data,MessageParcel & reply)127 ErrCode BluetoothHfpHfObserverStub::OnSignalStrengthChangedInner(
128     BluetoothHfpHfObserverStub *stub, MessageParcel &data, MessageParcel &reply)
129 {
130     std::shared_ptr<BluetoothRawAddress> device(data.ReadParcelable<BluetoothRawAddress>());
131     if (!device) {
132         return TRANSACTION_ERR;
133     }
134     int signal = data.ReadInt32();
135     stub->OnSignalStrengthChanged(*device, signal);
136     return NO_ERROR;
137 }
138 
OnRegistrationStatusChangedInner(BluetoothHfpHfObserverStub * stub,MessageParcel & data,MessageParcel & reply)139 ErrCode BluetoothHfpHfObserverStub::OnRegistrationStatusChangedInner(
140     BluetoothHfpHfObserverStub *stub, MessageParcel &data, MessageParcel &reply)
141 {
142     std::shared_ptr<BluetoothRawAddress> device(data.ReadParcelable<BluetoothRawAddress>());
143     if (!device) {
144         return TRANSACTION_ERR;
145     }
146     int status = data.ReadInt32();
147     stub->OnSignalStrengthChanged(*device, status);
148     return NO_ERROR;
149 }
150 
OnRoamingStatusChangedInner(BluetoothHfpHfObserverStub * stub,MessageParcel & data,MessageParcel & reply)151 ErrCode BluetoothHfpHfObserverStub::OnRoamingStatusChangedInner(
152     BluetoothHfpHfObserverStub *stub, MessageParcel &data, MessageParcel &reply)
153 {
154     std::shared_ptr<BluetoothRawAddress> device(data.ReadParcelable<BluetoothRawAddress>());
155     if (!device) {
156         return TRANSACTION_ERR;
157     }
158     int status = data.ReadInt32();
159     stub->OnRoamingStatusChanged(*device, status);
160     return NO_ERROR;
161 }
162 
OnOperatorSelectionChangedInner(BluetoothHfpHfObserverStub * stub,MessageParcel & data,MessageParcel & reply)163 ErrCode BluetoothHfpHfObserverStub::OnOperatorSelectionChangedInner(
164     BluetoothHfpHfObserverStub *stub, MessageParcel &data, MessageParcel &reply)
165 {
166     std::shared_ptr<BluetoothRawAddress> device(data.ReadParcelable<BluetoothRawAddress>());
167     if (!device) {
168         return TRANSACTION_ERR;
169     }
170     std::string name = data.ReadString();
171     stub->OnOperatorSelectionChanged(*device, name);
172     return NO_ERROR;
173 }
174 
OnSubscriberNumberChangedInner(BluetoothHfpHfObserverStub * stub,MessageParcel & data,MessageParcel & reply)175 ErrCode BluetoothHfpHfObserverStub::OnSubscriberNumberChangedInner(
176     BluetoothHfpHfObserverStub *stub, MessageParcel &data, MessageParcel &reply)
177 {
178     std::shared_ptr<BluetoothRawAddress> device(data.ReadParcelable<BluetoothRawAddress>());
179     if (!device) {
180         return TRANSACTION_ERR;
181     }
182     std::string number = data.ReadString();
183     stub->OnSubscriberNumberChanged(*device, number);
184     return NO_ERROR;
185 }
186 
OnVoiceRecognitionStatusChangedInner(BluetoothHfpHfObserverStub * stub,MessageParcel & data,MessageParcel & reply)187 ErrCode BluetoothHfpHfObserverStub::OnVoiceRecognitionStatusChangedInner(
188     BluetoothHfpHfObserverStub *stub, MessageParcel &data, MessageParcel &reply)
189 {
190     std::shared_ptr<BluetoothRawAddress> device(data.ReadParcelable<BluetoothRawAddress>());
191     if (!device) {
192         return TRANSACTION_ERR;
193     }
194     int status = data.ReadInt32();
195     stub->OnVoiceRecognitionStatusChanged(*device, status);
196     return NO_ERROR;
197 }
198 
OnInBandRingToneChangedInner(BluetoothHfpHfObserverStub * stub,MessageParcel & data,MessageParcel & reply)199 ErrCode BluetoothHfpHfObserverStub::OnInBandRingToneChangedInner(
200     BluetoothHfpHfObserverStub *stub, MessageParcel &data, MessageParcel &reply)
201 {
202     std::shared_ptr<BluetoothRawAddress> device(data.ReadParcelable<BluetoothRawAddress>());
203     if (!device) {
204         return TRANSACTION_ERR;
205     }
206     int status = data.ReadInt32();
207     stub->OnInBandRingToneChanged(*device, status);
208     return NO_ERROR;
209 }
210 
211 }  // namespace Bluetooth
212 }  // namespace OHOS
213