1 /*
2  * Copyright (C) 2023 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_log.h"
17 #include "bluetooth_errorcode.h"
18 #include "bluetooth_map_mse_proxy.h"
19 
20 namespace OHOS {
21 namespace Bluetooth {
22 const int32_t MAP_MSE_READ_DEVICE_MAX_SIZE = 0x100;
GetDeviceState(const BluetoothRawAddress &device, int32_t &state)23 int32_t BluetoothMapMseProxy::GetDeviceState(const BluetoothRawAddress &device, int32_t &state)
24 {
25     MessageParcel data;
26     CHECK_AND_RETURN_LOG_RET(data.WriteInterfaceToken(BluetoothMapMseProxy::GetDescriptor()),
27         BT_ERR_IPC_TRANS_FAILED, "WriteInterfaceToken error");
28     CHECK_AND_RETURN_LOG_RET(data.WriteParcelable(&device), BT_ERR_IPC_TRANS_FAILED, "Write device error");
29 
30     MessageParcel reply;
31     MessageOption option(MessageOption::TF_SYNC);
32 
33     SEND_IPC_REQUEST_RETURN_RESULT(BluetoothMapMseInterfaceCode::MSE_GET_DEVICE_STATE,
34         data, reply, option, BT_ERR_IPC_TRANS_FAILED);
35 
36     int32_t ret = reply.ReadInt32();
37     CHECK_AND_RETURN_LOG_RET((ret == BT_NO_ERROR), ret, "reply errCode: %{public}d", ret);
38     state = reply.ReadInt32();
39     return BT_NO_ERROR;
40 }
41 
GetDevicesByStates( const std::vector<int32_t> &states, std::vector<BluetoothRawAddress> &rawDevices)42 int32_t BluetoothMapMseProxy::GetDevicesByStates(
43     const std::vector<int32_t> &states, std::vector<BluetoothRawAddress> &rawDevices)
44 {
45     MessageParcel data;
46     CHECK_AND_RETURN_LOG_RET(data.WriteInterfaceToken(BluetoothMapMseProxy::GetDescriptor()),
47         BT_ERR_IPC_TRANS_FAILED, "WriteInterfaceToken error");
48     CHECK_AND_RETURN_LOG_RET(data.WriteInt32Vector(states), BT_ERR_IPC_TRANS_FAILED, "WriteInt32Vector error");
49 
50     MessageParcel reply;
51     MessageOption option(MessageOption::TF_SYNC);
52 
53     SEND_IPC_REQUEST_RETURN_RESULT(BluetoothMapMseInterfaceCode::MSE_GET_DEVICES_BY_STATES,
54         data, reply, option, BT_ERR_IPC_TRANS_FAILED);
55 
56     int32_t ret = reply.ReadInt32();
57     CHECK_AND_RETURN_LOG_RET((ret == BT_NO_ERROR), ret, "reply errCode: %{public}d", ret);
58     int32_t DevNum = reply.ReadInt32();
59     CHECK_AND_RETURN_LOG_RET((DevNum >= 0 && DevNum < MAP_MSE_READ_DEVICE_MAX_SIZE),
60         BT_ERR_INTERNAL_ERROR, "Invalid DevNum: %{public}d", DevNum);
61 
62     for (int32_t i = 0; i < DevNum; i++) {
63         std::shared_ptr<BluetoothRawAddress> address(reply.ReadParcelable<BluetoothRawAddress>());
64         CHECK_AND_RETURN_LOG_RET((address != nullptr), BT_ERR_INTERNAL_ERROR, "address is nullptr");
65         rawDevices.push_back(*address);
66     }
67     return BT_NO_ERROR;
68 }
69 
Disconnect(const BluetoothRawAddress &device)70 int32_t BluetoothMapMseProxy::Disconnect(const BluetoothRawAddress &device)
71 {
72     MessageParcel data;
73     CHECK_AND_RETURN_LOG_RET(data.WriteInterfaceToken(BluetoothMapMseProxy::GetDescriptor()),
74         BT_ERR_IPC_TRANS_FAILED, "WriteInterfaceToken error");
75     CHECK_AND_RETURN_LOG_RET(data.WriteParcelable(&device), BT_ERR_IPC_TRANS_FAILED, "Write device error");
76 
77     MessageParcel reply;
78     MessageOption option(MessageOption::TF_SYNC);
79 
80     SEND_IPC_REQUEST_RETURN_RESULT(BluetoothMapMseInterfaceCode::MSE_DISCONNECT,
81         data, reply, option, BT_ERR_IPC_TRANS_FAILED);
82 
83     return reply.ReadInt32();
84 }
85 
SetConnectionStrategy(const BluetoothRawAddress &device, int32_t strategy)86 int32_t BluetoothMapMseProxy::SetConnectionStrategy(const BluetoothRawAddress &device, int32_t strategy)
87 {
88     MessageParcel data;
89     CHECK_AND_RETURN_LOG_RET(data.WriteInterfaceToken(BluetoothMapMseProxy::GetDescriptor()),
90         BT_ERR_IPC_TRANS_FAILED, "WriteInterfaceToken error");
91     CHECK_AND_RETURN_LOG_RET(data.WriteParcelable(&device), BT_ERR_IPC_TRANS_FAILED, "Write device error");
92     CHECK_AND_RETURN_LOG_RET(data.WriteInt32(strategy), BT_ERR_IPC_TRANS_FAILED, "Write strategy error");
93 
94     MessageParcel reply;
95     MessageOption option(MessageOption::TF_SYNC);
96 
97     SEND_IPC_REQUEST_RETURN_RESULT(BluetoothMapMseInterfaceCode::MSE_SET_CONNECTION_STRATEGY,
98         data, reply, option, BT_ERR_IPC_TRANS_FAILED);
99 
100     return reply.ReadInt32();
101 }
102 
GetConnectionStrategy(const BluetoothRawAddress &device, int32_t &strategy)103 int32_t BluetoothMapMseProxy::GetConnectionStrategy(const BluetoothRawAddress &device, int32_t &strategy)
104 {
105     MessageParcel data;
106     CHECK_AND_RETURN_LOG_RET(data.WriteInterfaceToken(BluetoothMapMseProxy::GetDescriptor()),
107         BT_ERR_IPC_TRANS_FAILED, "WriteInterfaceToken error");
108     CHECK_AND_RETURN_LOG_RET(data.WriteParcelable(&device), BT_ERR_IPC_TRANS_FAILED, "Write device error");
109 
110     MessageParcel reply;
111     MessageOption option(MessageOption::TF_SYNC);
112 
113     SEND_IPC_REQUEST_RETURN_RESULT(BluetoothMapMseInterfaceCode::MSE_GET_CONNECTION_STRATEGY,
114         data, reply, option, BT_ERR_IPC_TRANS_FAILED);
115 
116     int32_t ret = reply.ReadInt32();
117     CHECK_AND_RETURN_LOG_RET((ret == BT_NO_ERROR), ret, "reply errCode: %{public}d", ret);
118     strategy = reply.ReadInt32();
119     return BT_NO_ERROR;
120 }
121 
SetMessageAccessAuthorization(const BluetoothRawAddress &device, int32_t accessAuthorization)122 int32_t BluetoothMapMseProxy::SetMessageAccessAuthorization(const BluetoothRawAddress &device,
123     int32_t accessAuthorization)
124 {
125     MessageParcel data;
126     CHECK_AND_RETURN_LOG_RET(data.WriteInterfaceToken(BluetoothMapMseProxy::GetDescriptor()),
127         BT_ERR_IPC_TRANS_FAILED, "WriteInterfaceToken error");
128     CHECK_AND_RETURN_LOG_RET(data.WriteParcelable(&device), BT_ERR_IPC_TRANS_FAILED, "Write device error");
129     CHECK_AND_RETURN_LOG_RET(data.WriteInt32(accessAuthorization),
130         BT_ERR_IPC_TRANS_FAILED, "Write accessAuthorization error");
131 
132     MessageParcel reply;
133     MessageOption option(MessageOption::TF_SYNC);
134 
135     SEND_IPC_REQUEST_RETURN_RESULT(BluetoothMapMseInterfaceCode::MSE_SET_ACCESS_AUTHORIZATION,
136         data, reply, option, BT_ERR_IPC_TRANS_FAILED);
137 
138     return reply.ReadInt32();
139 }
140 
GetMessageAccessAuthorization(const BluetoothRawAddress &device, int32_t &accessAuthorization)141 int32_t BluetoothMapMseProxy::GetMessageAccessAuthorization(const BluetoothRawAddress &device,
142     int32_t &accessAuthorization)
143 {
144     MessageParcel data;
145     CHECK_AND_RETURN_LOG_RET(data.WriteInterfaceToken(BluetoothMapMseProxy::GetDescriptor()),
146         BT_ERR_INTERNAL_ERROR, "WriteInterfaceToken error");
147     CHECK_AND_RETURN_LOG_RET(data.WriteParcelable(&device), BT_ERR_INTERNAL_ERROR, "Write device error");
148 
149     MessageParcel reply;
150     MessageOption option(MessageOption::TF_SYNC);
151 
152     SEND_IPC_REQUEST_RETURN_RESULT(BluetoothMapMseInterfaceCode::MSE_GET_ACCESS_AUTHORIZATION,
153         data, reply, option, BT_ERR_IPC_TRANS_FAILED);
154 
155     int32_t ret = reply.ReadInt32();
156     CHECK_AND_RETURN_LOG_RET((ret == BT_NO_ERROR), ret, "reply errCode: %{public}d", ret);
157     accessAuthorization = reply.ReadInt32();
158     return BT_NO_ERROR;
159 }
160 
RegisterObserver(const sptr<IBluetoothMapMseObserver> &observer)161 void BluetoothMapMseProxy::RegisterObserver(const sptr<IBluetoothMapMseObserver> &observer)
162 {
163     MessageParcel data;
164     CHECK_AND_RETURN_LOG(data.WriteInterfaceToken(BluetoothMapMseProxy::GetDescriptor()), "WriteInterfaceToken error");
165     CHECK_AND_RETURN_LOG(data.WriteRemoteObject(observer->AsObject()), "Write object error");
166 
167     MessageParcel reply;
168     MessageOption option(MessageOption::TF_ASYNC);
169 
170     SEND_IPC_REQUEST_RETURN(BluetoothMapMseInterfaceCode::MSE_REGISTER_OBSERVER, data, reply, option);
171 }
172 
DeregisterObserver(const sptr<IBluetoothMapMseObserver> &observer)173 void BluetoothMapMseProxy::DeregisterObserver(const sptr<IBluetoothMapMseObserver> &observer)
174 {
175     MessageParcel data;
176     CHECK_AND_RETURN_LOG(data.WriteInterfaceToken(BluetoothMapMseProxy::GetDescriptor()), "WriteInterfaceToken error");
177     CHECK_AND_RETURN_LOG(data.WriteRemoteObject(observer->AsObject()), "Write object error");
178 
179     MessageParcel reply;
180     MessageOption option(MessageOption::TF_ASYNC);
181 
182     SEND_IPC_REQUEST_RETURN(BluetoothMapMseInterfaceCode::MSE_DEREGISTER_OBSERVER, data, reply, option);
183 }
184 }  // namespace Bluetooth
185 }  // namespace OHOS