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 "ohos_bt_spp.h"
17 #include "ohos_bt_socket.h"
18
19 #include <iostream>
20 #include <cstring>
21 #include <vector>
22
23 #include "ohos_bt_adapter_utils.h"
24 #include "bluetooth_socket.h"
25 #include "bluetooth_host.h"
26 #include "bluetooth_log.h"
27 #include "bluetooth_utils.h"
28
29 #ifdef __cplusplus
30 extern "C" {
31 #endif
32
33 using namespace std;
34
35 namespace OHOS {
36 namespace Bluetooth {
37
38 /**
39 * @brief Creates an server listening socket based on the service record.
40 *
41 * @param socketPara The parameters to create a server socket.
42 * @param name The service's name.
43 * @param len The length of the service's name.
44 * @return Returns a server ID, if create fail return {@link BT_SPP_INVALID_ID}.
45 */
SppServerCreate(BtCreateSocketPara * socketPara,const char * name,unsigned int len)46 int SppServerCreate(BtCreateSocketPara *socketPara, const char *name, unsigned int len)
47 {
48 HILOGI("start!");
49 if (socketPara == nullptr) {
50 HILOGI("socketPara is invalid!");
51 return BT_SPP_INVALID_ID;
52 }
53 BluetoothCreateSocketPara btsocketPara;
54 btsocketPara.isEncrypt = socketPara->isEncrypt;
55 btsocketPara.socketType = BluetoothSocketType(socketPara->socketType);
56 btsocketPara.uuid.uuidLen = socketPara->uuid.uuidLen;
57 btsocketPara.uuid.uuid = socketPara->uuid.uuid;
58
59 return SocketServerCreate(&btsocketPara, name);
60 }
61
62 /**
63 * @brief Waits for a remote device to connect to this server socket.
64 *
65 * This method return a client ID indicates a client socket
66 * can be used to read data from and write data to remote device.
67 *
68 * @param serverId The relative ID used to identify the current server socket, obtain the value by calling
69 * {@link SppServerCreate}.
70 * @return Returns a client ID, if accept fail return {@link BT_SPP_INVALID_ID}.
71 */
SppServerAccept(int serverId)72 int SppServerAccept(int serverId)
73 {
74 HILOGI("start, serverId: %{public}d", serverId);
75 return SocketServerAccept(serverId);
76 }
77
78 /**
79 * @brief Disables an spp server socket and releases related resources.
80 *
81 * @param serverId The relative ID used to identify the current server socket, obtain the value by calling
82 * {@link SppServerCreate}.
83 * @return Returns the operation result status {@link BtStatus}.
84 */
SppServerClose(int serverId)85 int SppServerClose(int serverId)
86 {
87 HILOGI("serverId: %{public}d", serverId);
88 return SocketServerClose(serverId);
89 }
90
91 /**
92 * @brief Connects to a remote device over the socket.
93 *
94 * @param socketPara The param to create a client socket and connect to a remote device.
95 * @return Returns a client ID, if connect fail return {@link BT_SPP_INVALID_ID}.
96 */
SppConnect(BtCreateSocketPara * socketPara,const BdAddr * bdAddr)97 int SppConnect(BtCreateSocketPara *socketPara, const BdAddr *bdAddr)
98 {
99 HILOGI("SppConnect start");
100 if (socketPara == nullptr) {
101 HILOGI("socketPara is invalid!");
102 return BT_SPP_INVALID_ID;
103 }
104 BluetoothCreateSocketPara btsocketPara;
105 btsocketPara.isEncrypt = socketPara->isEncrypt;
106 btsocketPara.socketType = BluetoothSocketType(socketPara->socketType);
107 btsocketPara.uuid.uuidLen = socketPara->uuid.uuidLen;
108 btsocketPara.uuid.uuid = socketPara->uuid.uuid;
109 return SocketConnect(&btsocketPara, bdAddr, SPP_SOCKET_PSM_VALUE);
110 }
111
112 /**
113 * @brief Disables a connection and releases related resources.
114 *
115 * @param clientId The relative ID used to identify the current client socket.
116 * @return Returns the operation result status {@link BtStatus}.
117 */
SppDisconnect(int clientId)118 int SppDisconnect(int clientId)
119 {
120 HILOGI("clientId: %{public}d", clientId);
121 return SocketDisconnect(clientId);
122 }
123
124 /**
125 * @brief Spp get remote device's address.
126 *
127 * @param clientId The relative ID used to identify the current client socket.
128 * @param remoteAddr Remote device's address, memory allocated by caller.
129 * @return Returns the operation result status {@link BtStatus}.
130 */
SppGetRemoteAddr(int clientId,BdAddr * remoteAddr)131 int SppGetRemoteAddr(int clientId, BdAddr *remoteAddr)
132 {
133 HILOGI("clientId: %{public}d", clientId);
134 return SocketGetRemoteAddr(clientId, remoteAddr);
135 }
136
137 /**
138 * @brief Get the connection status of this socket.
139 *
140 * @param clientId The relative ID used to identify the current client socket.
141 * @return Returns true is connected or false is not connected.
142 */
IsSppConnected(int clientId)143 bool IsSppConnected(int clientId)
144 {
145 HILOGI("clientId: %{public}d", clientId);
146 return IsSocketConnected(clientId);
147 }
148
149 /**
150 * @brief Read data from socket.
151 *
152 * @param clientId The relative ID used to identify the current client socket.
153 * @param buf Indicate the buffer which read in, memory allocated by caller.
154 * @param bufLen Indicate the buffer length.
155 * @return Returns the length greater than 0 as read the actual length.
156 * Returns {@link BT_SPP_READ_SOCKET_CLOSED} if the socket is closed.
157 * Returns {@link BT_SPP_READ_FAILED} if the operation failed.
158 */
SppRead(int clientId,char * buf,const unsigned int bufLen)159 int SppRead(int clientId, char *buf, const unsigned int bufLen)
160 {
161 return SocketRead(clientId, reinterpret_cast<uint8_t*>(buf), bufLen);
162 }
163
164 /**
165 * @brief Client write data to socket.
166 *
167 * @param clientId The relative ID used to identify the current client socket.
168 * @param data Indicate the data to be written.
169 * @return Returns the actual write length.
170 * Returns {@link BT_SPP_WRITE_FAILED} if the operation failed.
171 */
SppWrite(int clientId,const char * data,const unsigned int len)172 int SppWrite(int clientId, const char *data, const unsigned int len)
173 {
174 HILOGI("start, clientId: %{public}d, len: %{public}d", clientId, len);
175 return SocketWrite(clientId, reinterpret_cast<const uint8_t*>(data), len);
176 }
177
178 /**
179 * @brief Adjust the socket send and recv buffer size, limit range is 4KB to 50KB
180 *
181 * @param clientId The relative ID used to identify the current client socket.
182 * @param bufferSize The buffer size want to set, unit is byte.
183 * @return Returns the operation result status {@link BtStatus}.
184 */
SppSetSocketBufferSize(int clientId,int bufferSize)185 int SppSetSocketBufferSize(int clientId, int bufferSize)
186 {
187 HILOGI("start, clientId: %{public}d, size: %{public}d", clientId, bufferSize);
188 if (bufferSize < 0) {
189 return OHOS_BT_STATUS_PARM_INVALID;
190 }
191 return SetSocketBufferSize(clientId, bufferSize);
192 }
193 } // namespace Bluetooth
194 } // namespace OHOS
195 #ifdef __cplusplus
196 }
197 #endif