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