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
16 #include "bluetooth_remote_device_observer_stub.h"
17 #include "bluetooth_log.h"
18
19 namespace OHOS {
20 namespace Bluetooth {
21 constexpr int32_t UUID_SIZE_MAX = 1024;
22 const std::map<uint32_t, std::function<ErrCode(BluetoothRemoteDeviceObserverstub *, MessageParcel &, MessageParcel &)>>
23 BluetoothRemoteDeviceObserverstub::memberFuncMap_ = {
24 {IBluetoothRemoteDeviceObserver::Code::BT_REMOTE_DEVICE_OBSERVER_PSIR_STATUS,
25 std::bind(&BluetoothRemoteDeviceObserverstub::OnPairStatusChangedInner, std::placeholders::_1,
26 std::placeholders::_2, std::placeholders::_3)},
27 {IBluetoothRemoteDeviceObserver::Code::BT_REMOTE_DEVICE_OBSERVER_REMOTE_UUID,
28 std::bind(&BluetoothRemoteDeviceObserverstub::OnRemoteNameUuidChangedInner, std::placeholders::_1,
29 std::placeholders::_2, std::placeholders::_3)},
30 {IBluetoothRemoteDeviceObserver::Code::BT_REMOTE_DEVICE_OBSERVER_REMOTE_NAME,
31 std::bind(&BluetoothRemoteDeviceObserverstub::OnRemoteNameChangedInner, std::placeholders::_1,
32 std::placeholders::_2, std::placeholders::_3)},
33 {IBluetoothRemoteDeviceObserver::Code::BT_REMOTE_DEVICE_OBSERVER_ALIAS_CHANGED,
34 std::bind(&BluetoothRemoteDeviceObserverstub::OnRemoteAliasChangedInner, std::placeholders::_1,
35 std::placeholders::_2, std::placeholders::_3)},
36 {IBluetoothRemoteDeviceObserver::Code::BT_REMOTE_DEVICE_OBSERVER_REMOTE_COD,
37 std::bind(&BluetoothRemoteDeviceObserverstub::OnRemoteCodChangedInner, std::placeholders::_1,
38 std::placeholders::_2, std::placeholders::_3)},
39 {IBluetoothRemoteDeviceObserver::Code::BT_REMOTE_DEVICE_OBSERVER_REMOTE_BATTERY_LEVEL,
40 std::bind(&BluetoothRemoteDeviceObserverstub::OnRemoteBatteryLevelChangedInner, std::placeholders::_1,
41 std::placeholders::_2, std::placeholders::_3)},
42 };
43
BluetoothRemoteDeviceObserverstub()44 BluetoothRemoteDeviceObserverstub::BluetoothRemoteDeviceObserverstub()
45 {
46 HILOGD("%{public}s start.", __func__);
47 }
48
~BluetoothRemoteDeviceObserverstub()49 BluetoothRemoteDeviceObserverstub::~BluetoothRemoteDeviceObserverstub()
50 {
51 HILOGD("%{public}s start.", __func__);
52 }
53
OnRemoteRequest(uint32_t code,MessageParcel & data,MessageParcel & reply,MessageOption & option)54 int BluetoothRemoteDeviceObserverstub::OnRemoteRequest(
55 uint32_t code, MessageParcel &data, MessageParcel &reply, MessageOption &option)
56 {
57 HILOGD("BluetoothRemoteDeviceObserverstub::OnRemoteRequest, cmd = %d, flags= %d", code, option.GetFlags());
58 std::u16string descriptor = BluetoothRemoteDeviceObserverstub::GetDescriptor();
59 std::u16string remoteDescriptor = data.ReadInterfaceToken();
60 if (descriptor != remoteDescriptor) {
61 HILOGE("BluetoothHostStub::OnRemoteRequest, local descriptor is not equal to remote");
62 return ERR_INVALID_STATE;
63 }
64 auto itFunc = memberFuncMap_.find(code);
65 if (itFunc != memberFuncMap_.end()) {
66 auto memberFunc = itFunc->second;
67 if (memberFunc != nullptr) {
68 return memberFunc(this, data, reply);
69 }
70 }
71 return IPCObjectStub::OnRemoteRequest(code, data, reply, option);
72 }
73
OnPairStatusChangedInner(MessageParcel & data,MessageParcel & reply)74 ErrCode BluetoothRemoteDeviceObserverstub::OnPairStatusChangedInner(MessageParcel &data, MessageParcel &reply)
75 {
76 int32_t transport = data.ReadInt32();
77 sptr<BluetoothRawAddress> result = data.ReadParcelable<BluetoothRawAddress>();
78 int32_t status = data.ReadInt32();
79 OnPairStatusChanged(transport, *result, status);
80 return NO_ERROR;
81 }
82
OnRemoteNameUuidChangedInner(MessageParcel & data,MessageParcel & reply)83 ErrCode BluetoothRemoteDeviceObserverstub::OnRemoteNameUuidChangedInner(MessageParcel &data, MessageParcel &reply)
84 {
85 sptr<BluetoothRawAddress> result = data.ReadParcelable<BluetoothRawAddress>();
86 int32_t uuidSize = data.ReadInt32();
87 if (uuidSize > UUID_SIZE_MAX || uuidSize < 0) {
88 HILOGE("%{public}s, uuidSize = %{public}d exceeds the maximum 512.", __func__, uuidSize);
89 return TRANSACTION_ERR;
90 }
91 std::vector<bluetooth::Uuid> uuids;
92 for (int i = 0; i < uuidSize; ++i) {
93 bluetooth::Uuid uuid = ParcelBtUuid::ReadFromParcel(data);
94 uuids.push_back(uuid);
95 }
96 OnRemoteUuidChanged(*result, uuids);
97 return NO_ERROR;
98 }
99
OnRemoteNameChangedInner(MessageParcel & data,MessageParcel & reply)100 ErrCode BluetoothRemoteDeviceObserverstub::OnRemoteNameChangedInner(MessageParcel &data, MessageParcel &reply)
101 {
102 sptr<BluetoothRawAddress> result = data.ReadParcelable<BluetoothRawAddress>();
103 std::string deviceName = data.ReadString();
104 OnRemoteNameChanged(*result, deviceName);
105 return NO_ERROR;
106 }
107
OnRemoteAliasChangedInner(MessageParcel & data,MessageParcel & reply)108 ErrCode BluetoothRemoteDeviceObserverstub::OnRemoteAliasChangedInner(MessageParcel &data, MessageParcel &reply)
109 {
110 sptr<BluetoothRawAddress> result = data.ReadParcelable<BluetoothRawAddress>();
111 std::string alias = data.ReadString();
112 OnRemoteAliasChanged(*result, alias);
113 return NO_ERROR;
114 }
115
OnRemoteCodChangedInner(MessageParcel & data,MessageParcel & reply)116 ErrCode BluetoothRemoteDeviceObserverstub::OnRemoteCodChangedInner(MessageParcel &data, MessageParcel &reply)
117 {
118 sptr<BluetoothRawAddress> result = data.ReadParcelable<BluetoothRawAddress>();
119 int32_t cod = data.ReadInt32();
120 OnRemoteCodChanged(*result, cod);
121 return NO_ERROR;
122 }
123
OnRemoteBatteryLevelChangedInner(MessageParcel & data,MessageParcel & reply)124 ErrCode BluetoothRemoteDeviceObserverstub::OnRemoteBatteryLevelChangedInner(MessageParcel &data, MessageParcel &reply)
125 {
126 sptr<BluetoothRawAddress> result = data.ReadParcelable<BluetoothRawAddress>();
127 int32_t batteryLevel = data.ReadInt32();
128 OnRemoteBatteryLevelChanged(*result, batteryLevel);
129 return NO_ERROR;
130 }
131 } // namespace Bluetooth
132 } // namespace OHOS