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(
65 data.WriteRemoteObject(param.observer->AsObject()), BT_ERR_IPC_TRANS_FAILED, "write object errorr");
66
67 MessageParcel reply;
68 MessageOption option(MessageOption::TF_SYNC);
69
70 SEND_IPC_REQUEST_RETURN_RESULT(BluetoothSocketInterfaceCode::SOCKET_LISTEN,
71 data, reply, option, BT_ERR_IPC_TRANS_FAILED);
72
73 int errorCode = reply.ReadInt32();
74 if (errorCode == NO_ERROR) {
75 fd = reply.ReadFileDescriptor();
76 }
77
78 return errorCode;
79 }
80
DeregisterServerObserver(const sptr<IBluetoothServerSocketObserver> & observer)81 int BluetoothSocketProxy::DeregisterServerObserver(const sptr<IBluetoothServerSocketObserver> &observer)
82 {
83 MessageParcel data;
84 CHECK_AND_RETURN_LOG_RET(data.WriteInterfaceToken(BluetoothSocketProxy::GetDescriptor()),
85 BT_ERR_IPC_TRANS_FAILED, "WriteInterfaceToken error");
86 CHECK_AND_RETURN_LOG_RET(data.WriteRemoteObject(observer->AsObject()),
87 BT_ERR_IPC_TRANS_FAILED, "write object errorr");
88
89 MessageParcel reply;
90 MessageOption option(MessageOption::TF_SYNC);
91
92 SEND_IPC_REQUEST_RETURN_RESULT(BluetoothSocketInterfaceCode::DEREGISTER_SERVER_OBSERVER,
93 data, reply, option, BT_ERR_IPC_TRANS_FAILED);
94
95 return reply.ReadInt32();
96 }
97
RegisterClientObserver(const BluetoothRawAddress & dev,const bluetooth::Uuid uuid,const sptr<IBluetoothClientSocketObserver> & observer)98 int BluetoothSocketProxy::RegisterClientObserver(const BluetoothRawAddress &dev, const bluetooth::Uuid uuid,
99 const sptr<IBluetoothClientSocketObserver> &observer)
100 {
101 MessageParcel data;
102 CHECK_AND_RETURN_LOG_RET(data.WriteInterfaceToken(BluetoothSocketProxy::GetDescriptor()),
103 BT_ERR_IPC_TRANS_FAILED, "WriteInterfaceToken error");
104 CHECK_AND_RETURN_LOG_RET(data.WriteParcelable(&dev), BT_ERR_IPC_TRANS_FAILED, "write dev error");
105 BluetoothUuid btUuid(uuid);
106 CHECK_AND_RETURN_LOG_RET(data.WriteParcelable(&btUuid), BT_ERR_IPC_TRANS_FAILED, "write btUuid error");
107 CHECK_AND_RETURN_LOG_RET(
108 data.WriteRemoteObject(observer->AsObject()), BT_ERR_IPC_TRANS_FAILED, "write object errorr");
109
110 MessageParcel reply;
111 MessageOption option(MessageOption::TF_SYNC);
112
113 SEND_IPC_REQUEST_RETURN_RESULT(BluetoothSocketInterfaceCode::REGISTER_CLIENT_OBSERVER,
114 data, reply, option, BT_ERR_IPC_TRANS_FAILED);
115
116 return reply.ReadInt32();
117 }
118
DeregisterClientObserver(const BluetoothRawAddress & dev,const bluetooth::Uuid uuid,const sptr<IBluetoothClientSocketObserver> & observer)119 int BluetoothSocketProxy::DeregisterClientObserver(const BluetoothRawAddress &dev, const bluetooth::Uuid uuid,
120 const sptr<IBluetoothClientSocketObserver> &observer)
121 {
122 MessageParcel data;
123 CHECK_AND_RETURN_LOG_RET(data.WriteInterfaceToken(BluetoothSocketProxy::GetDescriptor()),
124 BT_ERR_IPC_TRANS_FAILED, "WriteInterfaceToken error");
125 CHECK_AND_RETURN_LOG_RET(data.WriteParcelable(&dev), BT_ERR_IPC_TRANS_FAILED, "write dev error");
126
127 BluetoothUuid btUuid(uuid);
128 CHECK_AND_RETURN_LOG_RET(data.WriteParcelable(&btUuid), BT_ERR_IPC_TRANS_FAILED, "write btUuid error");
129 CHECK_AND_RETURN_LOG_RET(
130 data.WriteRemoteObject(observer->AsObject()), BT_ERR_IPC_TRANS_FAILED, "write object error");
131
132 MessageParcel reply;
133 MessageOption option(MessageOption::TF_SYNC);
134
135 SEND_IPC_REQUEST_RETURN_RESULT(BluetoothSocketInterfaceCode::DEREGISTER_CLIENT_OBSERVER,
136 data, reply, option, BT_ERR_IPC_TRANS_FAILED);
137
138 return reply.ReadInt32();
139 }
140
UpdateCocConnectionParams(const BluetoothSocketCocInfo & info)141 int BluetoothSocketProxy::UpdateCocConnectionParams(const BluetoothSocketCocInfo &info)
142 {
143 MessageParcel data;
144 CHECK_AND_RETURN_LOG_RET(data.WriteInterfaceToken(BluetoothSocketProxy::GetDescriptor()),
145 BT_ERR_IPC_TRANS_FAILED, "WriteInterfaceToken error");
146 CHECK_AND_RETURN_LOG_RET(data.WriteParcelable(&info), BT_ERR_IPC_TRANS_FAILED, "write info error");
147
148 MessageParcel reply;
149 MessageOption option(MessageOption::TF_SYNC);
150
151 SEND_IPC_REQUEST_RETURN_RESULT(BluetoothSocketInterfaceCode::SOCKET_UPDATE_COC_PARAMS,
152 data, reply, option, BT_ERR_IPC_TRANS_FAILED);
153
154 return reply.ReadInt32();
155 }
156 } // namespace Bluetooth
157 } // namespace OHOS