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