1 /*
2 * Copyright (C) 2021-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_socket_proxy"
17 #endif
18
19 #include "bluetooth_socket_proxy.h"
20 #include "bluetooth_bt_uuid.h"
21 #include "bluetooth_log.h"
22 #include "bluetooth_errorcode.h"
23
24 namespace OHOS {
25 namespace Bluetooth {
Connect(ConnectSocketParam & param,int & fd)26 int BluetoothSocketProxy::Connect(ConnectSocketParam ¶m, int &fd)
27 {
28 MessageParcel data;
29 CHECK_AND_RETURN_LOG_RET(data.WriteInterfaceToken(BluetoothSocketProxy::GetDescriptor()),
30 BT_ERR_IPC_TRANS_FAILED, "WriteInterfaceToken error");
31 CHECK_AND_RETURN_LOG_RET(data.WriteString(param.addr), BT_ERR_IPC_TRANS_FAILED, "write param.addr error");
32 BluetoothUuid btUuid(param.uuid);
33 CHECK_AND_RETURN_LOG_RET(data.WriteParcelable(&btUuid), BT_ERR_IPC_TRANS_FAILED, "write btUuid error");
34 CHECK_AND_RETURN_LOG_RET(
35 data.WriteInt32(param.securityFlag), BT_ERR_IPC_TRANS_FAILED, "write param.securityFlag error");
36 CHECK_AND_RETURN_LOG_RET(data.WriteInt32(param.type), BT_ERR_IPC_TRANS_FAILED, "write param.type error");
37 CHECK_AND_RETURN_LOG_RET(data.WriteInt32(param.psm), BT_ERR_IPC_TRANS_FAILED, "write param.psm error");
38
39 MessageParcel reply;
40 MessageOption option(MessageOption::TF_SYNC);
41
42 SEND_IPC_REQUEST_RETURN_RESULT(BluetoothSocketInterfaceCode::SOCKET_CONNECT,
43 data, reply, option, BT_ERR_IPC_TRANS_FAILED);
44
45 int errorCode = reply.ReadInt32();
46 if (errorCode == NO_ERROR) {
47 fd = reply.ReadFileDescriptor();
48 }
49 return errorCode;
50 }
51
Listen(ListenSocketParam & param,int & fd)52 int BluetoothSocketProxy::Listen(ListenSocketParam ¶m, int &fd)
53 {
54 fd = BT_INVALID_SOCKET_FD;
55 MessageParcel data;
56 CHECK_AND_RETURN_LOG_RET(data.WriteInterfaceToken(BluetoothSocketProxy::GetDescriptor()),
57 BT_ERR_IPC_TRANS_FAILED, "WriteInterfaceToken error");
58 CHECK_AND_RETURN_LOG_RET(data.WriteString(param.name), BT_ERR_IPC_TRANS_FAILED, "write param.name error");
59 BluetoothUuid btUuid(param.uuid);
60 CHECK_AND_RETURN_LOG_RET(data.WriteParcelable(&btUuid), BT_ERR_IPC_TRANS_FAILED, "write btUuid error");
61 CHECK_AND_RETURN_LOG_RET(
62 data.WriteInt32(param.securityFlag), BT_ERR_IPC_TRANS_FAILED, "write param.securityFlag error");
63 CHECK_AND_RETURN_LOG_RET(data.WriteInt32(param.type), BT_ERR_IPC_TRANS_FAILED, "write param.type error");
64 CHECK_AND_RETURN_LOG_RET(data.WriteInt32(param.psm), BT_ERR_IPC_TRANS_FAILED, "write param.psm error");
65 CHECK_AND_RETURN_LOG_RET(
66 data.WriteRemoteObject(param.observer->AsObject()), BT_ERR_IPC_TRANS_FAILED, "write object errorr");
67
68 MessageParcel reply;
69 MessageOption option(MessageOption::TF_SYNC);
70
71 SEND_IPC_REQUEST_RETURN_RESULT(BluetoothSocketInterfaceCode::SOCKET_LISTEN,
72 data, reply, option, BT_ERR_IPC_TRANS_FAILED);
73
74 int errorCode = reply.ReadInt32();
75 if (errorCode == NO_ERROR) {
76 fd = reply.ReadFileDescriptor();
77 }
78
79 return errorCode;
80 }
81
DeregisterServerObserver(const sptr<IBluetoothServerSocketObserver> & observer)82 int BluetoothSocketProxy::DeregisterServerObserver(const sptr<IBluetoothServerSocketObserver> &observer)
83 {
84 MessageParcel data;
85 CHECK_AND_RETURN_LOG_RET(data.WriteInterfaceToken(BluetoothSocketProxy::GetDescriptor()),
86 BT_ERR_IPC_TRANS_FAILED, "WriteInterfaceToken error");
87 CHECK_AND_RETURN_LOG_RET(data.WriteRemoteObject(observer->AsObject()),
88 BT_ERR_IPC_TRANS_FAILED, "write object errorr");
89
90 MessageParcel reply;
91 MessageOption option(MessageOption::TF_SYNC);
92
93 SEND_IPC_REQUEST_RETURN_RESULT(BluetoothSocketInterfaceCode::DEREGISTER_SERVER_OBSERVER,
94 data, reply, option, BT_ERR_IPC_TRANS_FAILED);
95
96 return reply.ReadInt32();
97 }
98
RegisterClientObserver(const BluetoothRawAddress & dev,const bluetooth::Uuid uuid,const sptr<IBluetoothClientSocketObserver> & observer)99 int BluetoothSocketProxy::RegisterClientObserver(const BluetoothRawAddress &dev, const bluetooth::Uuid uuid,
100 const sptr<IBluetoothClientSocketObserver> &observer)
101 {
102 MessageParcel data;
103 CHECK_AND_RETURN_LOG_RET(data.WriteInterfaceToken(BluetoothSocketProxy::GetDescriptor()),
104 BT_ERR_IPC_TRANS_FAILED, "WriteInterfaceToken error");
105 CHECK_AND_RETURN_LOG_RET(data.WriteParcelable(&dev), BT_ERR_IPC_TRANS_FAILED, "write dev error");
106 BluetoothUuid btUuid(uuid);
107 CHECK_AND_RETURN_LOG_RET(data.WriteParcelable(&btUuid), BT_ERR_IPC_TRANS_FAILED, "write btUuid error");
108 CHECK_AND_RETURN_LOG_RET(
109 data.WriteRemoteObject(observer->AsObject()), BT_ERR_IPC_TRANS_FAILED, "write object errorr");
110
111 MessageParcel reply;
112 MessageOption option(MessageOption::TF_SYNC);
113
114 SEND_IPC_REQUEST_RETURN_RESULT(BluetoothSocketInterfaceCode::REGISTER_CLIENT_OBSERVER,
115 data, reply, option, BT_ERR_IPC_TRANS_FAILED);
116
117 return reply.ReadInt32();
118 }
119
DeregisterClientObserver(const BluetoothRawAddress & dev,const bluetooth::Uuid uuid,const sptr<IBluetoothClientSocketObserver> & observer)120 int BluetoothSocketProxy::DeregisterClientObserver(const BluetoothRawAddress &dev, const bluetooth::Uuid uuid,
121 const sptr<IBluetoothClientSocketObserver> &observer)
122 {
123 MessageParcel data;
124 CHECK_AND_RETURN_LOG_RET(data.WriteInterfaceToken(BluetoothSocketProxy::GetDescriptor()),
125 BT_ERR_IPC_TRANS_FAILED, "WriteInterfaceToken error");
126 CHECK_AND_RETURN_LOG_RET(data.WriteParcelable(&dev), BT_ERR_IPC_TRANS_FAILED, "write dev error");
127
128 BluetoothUuid btUuid(uuid);
129 CHECK_AND_RETURN_LOG_RET(data.WriteParcelable(&btUuid), BT_ERR_IPC_TRANS_FAILED, "write btUuid error");
130 CHECK_AND_RETURN_LOG_RET(
131 data.WriteRemoteObject(observer->AsObject()), BT_ERR_IPC_TRANS_FAILED, "write object error");
132
133 MessageParcel reply;
134 MessageOption option(MessageOption::TF_SYNC);
135
136 SEND_IPC_REQUEST_RETURN_RESULT(BluetoothSocketInterfaceCode::DEREGISTER_CLIENT_OBSERVER,
137 data, reply, option, BT_ERR_IPC_TRANS_FAILED);
138
139 return reply.ReadInt32();
140 }
141
UpdateCocConnectionParams(const BluetoothSocketCocInfo & info)142 int BluetoothSocketProxy::UpdateCocConnectionParams(const BluetoothSocketCocInfo &info)
143 {
144 MessageParcel data;
145 CHECK_AND_RETURN_LOG_RET(data.WriteInterfaceToken(BluetoothSocketProxy::GetDescriptor()),
146 BT_ERR_IPC_TRANS_FAILED, "WriteInterfaceToken error");
147 CHECK_AND_RETURN_LOG_RET(data.WriteParcelable(&info), BT_ERR_IPC_TRANS_FAILED, "write info error");
148
149 MessageParcel reply;
150 MessageOption option(MessageOption::TF_SYNC);
151
152 SEND_IPC_REQUEST_RETURN_RESULT(BluetoothSocketInterfaceCode::SOCKET_UPDATE_COC_PARAMS,
153 data, reply, option, BT_ERR_IPC_TRANS_FAILED);
154
155 return reply.ReadInt32();
156 }
157
IsAllowSocketConnect(int socketType,const std::string & addr,bool & isAllowed)158 int BluetoothSocketProxy::IsAllowSocketConnect(int socketType, const std::string &addr, bool &isAllowed)
159 {
160 MessageParcel data;
161 CHECK_AND_RETURN_LOG_RET(data.WriteInterfaceToken(BluetoothSocketProxy::GetDescriptor()),
162 BT_ERR_IPC_TRANS_FAILED, "WriteInterfaceToken error");
163 CHECK_AND_RETURN_LOG_RET(data.WriteInt32(socketType), BT_ERR_IPC_TRANS_FAILED, "write socketType error");
164 CHECK_AND_RETURN_LOG_RET(data.WriteString(addr), BT_ERR_IPC_TRANS_FAILED, "write addr error");
165
166 MessageParcel reply;
167 MessageOption option(MessageOption::TF_SYNC);
168
169 SEND_IPC_REQUEST_RETURN_RESULT(BluetoothSocketInterfaceCode::SOCKET_IS_ALLOW_CONNECT,
170 data, reply, option, BT_ERR_IPC_TRANS_FAILED);
171
172 int32_t exception = reply.ReadInt32();
173 if (exception == BT_NO_ERROR) {
174 isAllowed = reply.ReadBool();
175 }
176 return exception;
177 }
178 } // namespace Bluetooth
179 } // namespace OHOS