• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2021-2022 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     HILOGI("Connect starts");
26     MessageParcel data;
27 
28     if (!data.WriteInterfaceToken(BluetoothSocketProxy::GetDescriptor())) {
29         HILOGE("Connect WriteInterfaceToken error");
30         return BT_ERR_INTERNAL_ERROR;
31     }
32     if (!data.WriteString(param.addr)) {
33         HILOGE("Connect write addr error");
34         return BT_ERR_INTERNAL_ERROR;
35     }
36 
37     BluetoothUuid btUuid(param.uuid);
38     if (!data.WriteParcelable(&btUuid)) {
39         HILOGE("Connect write uuid error");
40         return BT_ERR_INTERNAL_ERROR;
41     }
42 
43     if (!data.WriteInt32(param.securityFlag)) {
44         HILOGE("Connect write securityFlag error");
45         return BT_ERR_INTERNAL_ERROR;
46     }
47 
48     if (!data.WriteInt32(param.type)) {
49         HILOGE("Connect write type error");
50         return BT_ERR_INTERNAL_ERROR;
51     }
52 
53     if (!data.WriteInt32(param.psm)) {
54         HILOGE("Connect write psm error");
55         return BT_ERR_INTERNAL_ERROR;
56     }
57 
58     MessageParcel reply;
59     MessageOption option {
60         MessageOption::TF_SYNC
61     };
62 
63     int error = Remote()->SendRequest(BluetoothSocketInterfaceCode::SOCKET_CONNECT, data, reply, option);
64     if (error != NO_ERROR) {
65         HILOGE("Connect done fail, error: %{public}d", error);
66         return BT_ERR_INTERNAL_ERROR;
67     }
68 
69     int errorCode = reply.ReadInt32();
70     if (errorCode == NO_ERROR) {
71         fd = reply.ReadFileDescriptor();
72     }
73     return errorCode;
74 }
75 
Listen(ListenSocketParam & param,int & fd)76 int BluetoothSocketProxy::Listen(ListenSocketParam &param, int &fd)
77 {
78     HILOGI("starts");
79     fd = BT_INVALID_SOCKET_FD;
80     MessageParcel data;
81 
82     if (!data.WriteInterfaceToken(BluetoothSocketProxy::GetDescriptor())) {
83         HILOGE("WriteInterfaceToken error");
84         return BT_ERR_INTERNAL_ERROR;
85     }
86     if (!data.WriteString(param.name)) {
87         HILOGE("write name error");
88         return BT_ERR_INTERNAL_ERROR;
89     }
90     BluetoothUuid btUuid(param.uuid);
91     if (!data.WriteParcelable(&btUuid)) {
92         HILOGE("write uuid error");
93         return BT_ERR_INTERNAL_ERROR;
94     }
95 
96     if (!data.WriteInt32(param.securityFlag)) {
97         HILOGE("write securityFlag error");
98         return BT_ERR_INTERNAL_ERROR;
99     }
100 
101     if (!data.WriteInt32(param.type)) {
102         HILOGE("write type error");
103         return BT_ERR_INTERNAL_ERROR;
104     }
105 
106     MessageParcel reply;
107     MessageOption option {
108         MessageOption::TF_SYNC
109     };
110 
111     int error = Remote()->SendRequest(BluetoothSocketInterfaceCode::SOCKET_LISTEN, data, reply, option);
112     if (error != NO_ERROR) {
113         HILOGE("Listen done fail, error: %{public}d", error);
114         return BT_ERR_INTERNAL_ERROR;
115     }
116 
117     int errorCode = reply.ReadInt32();
118     if (errorCode == NO_ERROR) {
119         fd = reply.ReadFileDescriptor();
120     }
121 
122     return errorCode;
123 }
124 }  // namespace Bluetooth
125 }  // namespace OHOS