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_ag_observer_stub"
17 #endif
18 
19 #include "bluetooth_errorcode.h"
20 #include "bluetooth_hfp_ag_observer_stub.h"
21 #include "bluetooth_log.h"
22 
23 namespace OHOS {
24 namespace Bluetooth {
BluetoothHfpAgObserverStub()25 BluetoothHfpAgObserverStub::BluetoothHfpAgObserverStub()
26 {
27     HILOGD("start.");
28     memberFuncMap_[static_cast<uint32_t>(
29         BluetoothHfpAgObserverInterfaceCode::BT_HFP_AG_OBSERVER_CONNECTION_STATE_CHANGED)] =
30         &BluetoothHfpAgObserverStub::OnConnectionStateChangedInner;
31     memberFuncMap_[static_cast<uint32_t>(
32         BluetoothHfpAgObserverInterfaceCode::BT_HFP_AG_OBSERVER_SCO_STATE_CHANGED)] =
33         &BluetoothHfpAgObserverStub::OnScoStateChangedInner;
34     memberFuncMap_[static_cast<uint32_t>(
35         BluetoothHfpAgObserverInterfaceCode::BT_HFP_AG_OBSERVER_ACTIVE_DEVICE_CHANGED)] =
36         &BluetoothHfpAgObserverStub::OnActiveDeviceChangedInner;
37     memberFuncMap_[static_cast<uint32_t>(
38         BluetoothHfpAgObserverInterfaceCode::BT_HFP_AG_OBSERVER_HF_ENHANCED_DRIVER_SAFETY_CHANGED)] =
39         &BluetoothHfpAgObserverStub::OnHfEnhancedDriverSafetyChangedInner;
40     memberFuncMap_[static_cast<uint32_t>(
41         BluetoothHfpAgObserverInterfaceCode::BT_HFP_AG_OBSERVER_HFP_STACK_CHANGED)] =
42         &BluetoothHfpAgObserverStub::OnHfpStackChangedInner;
43     memberFuncMap_[static_cast<uint32_t>(
44         BluetoothHfpAgObserverInterfaceCode::BT_HFP_AG_OBSERVER_VIRTUALDEVICE_CHANGED)] =
45         &BluetoothHfpAgObserverStub::OnVirtualDeviceChangedInner;
46 
47     HILOGD("ends.");
48 }
49 
~BluetoothHfpAgObserverStub()50 BluetoothHfpAgObserverStub::~BluetoothHfpAgObserverStub()
51 {
52     HILOGD("start.");
53     memberFuncMap_.clear();
54 }
55 
OnRemoteRequest(uint32_t code,MessageParcel & data,MessageParcel & reply,MessageOption & option)56 int BluetoothHfpAgObserverStub::OnRemoteRequest(uint32_t code, MessageParcel &data, MessageParcel &reply,
57     MessageOption &option)
58 {
59     HILOGD("BluetoothHfpAgObserverStub::OnRemoteRequest, cmd = %{public}d, flags= %{public}d", code, option.GetFlags());
60     if (BluetoothHfpAgObserverStub::GetDescriptor() != data.ReadInterfaceToken()) {
61         HILOGI("local descriptor is not equal to remote");
62         return ERR_INVALID_STATE;
63     }
64 
65     auto itFunc = memberFuncMap_.find(code);
66     if (itFunc != memberFuncMap_.end()) {
67         auto memberFunc = itFunc->second;
68         if (memberFunc != nullptr) {
69             return (this->*memberFunc)(data, reply);
70         }
71     }
72     HILOGW("BluetoothHfpAgObserverStub::OnRemoteRequest, default case, need check.");
73     return IPCObjectStub::OnRemoteRequest(code, data, reply, option);
74 }
75 
OnConnectionStateChangedInner(MessageParcel & data,MessageParcel & reply)76 ErrCode BluetoothHfpAgObserverStub::OnConnectionStateChangedInner(MessageParcel &data, MessageParcel &reply)
77 {
78     std::shared_ptr<BluetoothRawAddress> device(data.ReadParcelable<BluetoothRawAddress>());
79     if (!device) {
80         return TRANSACTION_ERR;
81     }
82     int state = data.ReadInt32();
83     int cause = data.ReadInt32();
84     OnConnectionStateChanged(*device, state, cause);
85     return BT_NO_ERROR;
86 }
87 
OnScoStateChangedInner(MessageParcel & data,MessageParcel & reply)88 ErrCode BluetoothHfpAgObserverStub::OnScoStateChangedInner(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 reason = data.ReadInt32();
96     OnScoStateChanged(*device, state, reason);
97     return BT_NO_ERROR;
98 }
99 
OnActiveDeviceChangedInner(MessageParcel & data,MessageParcel & reply)100 ErrCode BluetoothHfpAgObserverStub::OnActiveDeviceChangedInner(MessageParcel &data, MessageParcel &reply)
101 {
102     std::shared_ptr<BluetoothRawAddress> device(data.ReadParcelable<BluetoothRawAddress>());
103     if (!device) {
104         return TRANSACTION_ERR;
105     }
106     OnActiveDeviceChanged(*device);
107     return BT_NO_ERROR;
108 }
109 
OnHfEnhancedDriverSafetyChangedInner(MessageParcel & data,MessageParcel & reply)110 ErrCode BluetoothHfpAgObserverStub::OnHfEnhancedDriverSafetyChangedInner(MessageParcel &data, MessageParcel &reply)
111 {
112     std::shared_ptr<BluetoothRawAddress> device(data.ReadParcelable<BluetoothRawAddress>());
113     if (!device) {
114         return TRANSACTION_ERR;
115     }
116     int indValue = data.ReadInt32();
117     OnScoStateChanged(*device, indValue);
118     return BT_NO_ERROR;
119 }
120 
OnHfpStackChangedInner(MessageParcel & data,MessageParcel & reply)121 int32_t BluetoothHfpAgObserverStub::OnHfpStackChangedInner(MessageParcel &data, MessageParcel &reply)
122 {
123     std::shared_ptr<BluetoothRawAddress> device(data.ReadParcelable<BluetoothRawAddress>());
124     CHECK_AND_RETURN_LOG_RET(device, BT_ERR_INTERNAL_ERROR,
125         "BluetoothHfpAgObserverStub::OnHfpStackChangedInnerInner error, service is null");
126     int action = data.ReadInt32();
127     OnHfpStackChanged(*device, action);
128     return BT_NO_ERROR;
129 }
130 
OnVirtualDeviceChangedInner(MessageParcel & data,MessageParcel & reply)131 ErrCode BluetoothHfpAgObserverStub::OnVirtualDeviceChangedInner(MessageParcel &data, MessageParcel &reply)
132 {
133     int32_t action = data.ReadInt32();
134     std::string addr = data.ReadString();
135 
136     OnVirtualDeviceChanged(action, addr);
137 
138     return BT_NO_ERROR;
139 }
140 }  // namespace Bluetooth
141 }  // namespace OHOS
142