• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 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_c_adapter_socket"
17 #endif
18 
19 #include "ohos_bt_socket.h"
20 
21 #include <iostream>
22 #include <cstring>
23 #include <vector>
24 
25 #include "ohos_bt_adapter_utils.h"
26 #include "bluetooth_def.h"
27 #include "bluetooth_gatt_client.h"
28 #include "bluetooth_socket.h"
29 #include "bluetooth_host.h"
30 #include "bluetooth_log.h"
31 #include "bluetooth_utils.h"
32 #include "bluetooth_object_map.h"
33 
34 #ifdef __cplusplus
35 extern "C" {
36 #endif
37 
38 using namespace std;
39 
40 namespace OHOS {
41 namespace Bluetooth {
42 
43 const int MAX_OBJECT_NUM = 10000;
44 
45 static BluetoothObjectMap<std::shared_ptr<ServerSocket>, MAX_OBJECT_NUM> g_serverMap;
46 static BluetoothObjectMap<std::shared_ptr<ClientSocket>, MAX_OBJECT_NUM> g_clientMap;
47 
48 class BluetoothConnectionObserverWapper : public BluetoothConnectionObserver {
49 public:
BluetoothConnectionObserverWapper(BtSocketConnectionCallback & callback)50     explicit BluetoothConnectionObserverWapper(BtSocketConnectionCallback &callback)
51     {
52         socektConnectCallback = callback;
53     }
54 
55     __attribute__((no_sanitize("cfi")))
OnConnectionStateChanged(const CallbackConnectParam & param)56     void OnConnectionStateChanged(const CallbackConnectParam &param) override
57     {
58         if (socektConnectCallback.connStateCb == nullptr && socektConnectCallback.bleConnStateCb == nullptr) {
59             HILOGE("callback is null");
60             return;
61         }
62         BdAddr addr;
63         GetAddrFromString(param.addr.GetDeviceAddr(), addr.addr);
64         BtUuid btUuid;
65         string strUuid = param.uuid.ToString();
66         btUuid.uuid = (char *)strUuid.c_str();
67         btUuid.uuidLen = strUuid.size();
68         if (param.type == OHOS_SOCKET_SPP_RFCOMM && socektConnectCallback.connStateCb != nullptr) {
69             socektConnectCallback.connStateCb(&addr, btUuid, param.status, param.result);
70         }
71         if (param.type == OHOS_SOCKET_L2CAP_LE && socektConnectCallback.bleConnStateCb != nullptr) {
72             socektConnectCallback.bleConnStateCb(&addr, param.psm, param.status, param.result);
73         }
74     }
75 
76     BtSocketConnectionCallback socektConnectCallback;
77 };
78 
79 using ClientCbIterator = std::map<int, std::shared_ptr<BluetoothConnectionObserverWapper>>::iterator;
80 
GetSocketUuidPara(const BluetoothCreateSocketPara * socketPara,UUID & serverUuid)81 static bool GetSocketUuidPara(const BluetoothCreateSocketPara *socketPara, UUID &serverUuid)
82 {
83     if (socketPara->socketType == OHOS_SOCKET_SPP_RFCOMM) {
84         if (socketPara->uuid.uuid == nullptr || strlen(socketPara->uuid.uuid) != socketPara->uuid.uuidLen) {
85             HILOGE("param uuid invalid!");
86             return false;
87         }
88         string tmpUuid(socketPara->uuid.uuid);
89         if (!IsValidUuid(tmpUuid)) {
90             HILOGE("match the UUID faild.");
91             return false;
92         }
93         serverUuid = UUID::FromString(tmpUuid);
94     } else if (socketPara->socketType == OHOS_SOCKET_L2CAP_LE) {
95         if (socketPara->uuid.uuid == nullptr || strlen(socketPara->uuid.uuid) != socketPara->uuid.uuidLen) {
96             serverUuid = UUID::RandomUUID();
97         } else {
98             string tmpUuid(socketPara->uuid.uuid);
99             if (!IsValidUuid(tmpUuid)) {
100                 HILOGE("match the UUID faild.");
101                 return false;
102             }
103             serverUuid = UUID::FromString(tmpUuid);
104         }
105     } else {
106         HILOGE("param socketType invalid. socketType: %{public}d", socketPara->socketType);
107         return false;
108     }
109     return true;
110 }
111 
112 /**
113  * @brief Creates an server listening socket based on the service record.
114  *
115  * @param socketPara The parameters to create a server socket.
116  * @param name The service's name.
117  * @return Returns a server ID, if create fail return {@link BT_SOCKET_INVALID_ID}.
118  */
SocketServerCreate(const BluetoothCreateSocketPara * socketPara,const char * name)119 int SocketServerCreate(const BluetoothCreateSocketPara *socketPara, const char *name)
120 {
121     HILOGD("SocketServerCreate start!");
122     if (socketPara == nullptr || name == nullptr) {
123         HILOGE("socketPara or name is null.");
124         return BT_SOCKET_INVALID_ID;
125     }
126 
127     if (socketPara->socketType == OHOS_SOCKET_L2CAP_LE) {
128         CHECK_AND_RETURN_LOG_RET(IS_BLE_ENABLED(), BT_SOCKET_INVALID_ID, "BLE is not TURN_ON");
129     } else {
130         CHECK_AND_RETURN_LOG_RET(IS_BT_ENABLED(), BT_SOCKET_INVALID_ID, "BR is not TURN_ON");
131     }
132 
133     UUID serverUuid;
134     if (!GetSocketUuidPara(socketPara, serverUuid)) {
135         return BT_SOCKET_INVALID_ID;
136     }
137 
138     string serverName(name);
139     std::shared_ptr<ServerSocket> server = std::make_shared<ServerSocket>(serverName, serverUuid,
140         BtSocketType(socketPara->socketType), socketPara->isEncrypt);
141     int result = server->Listen();
142     if (result != BT_NO_ERROR) {
143         HILOGE("SocketServerCreate fail, result: %{public}d", result);
144         server->Close();
145         HILOGE("SocketServerCreate closed.");
146         return BT_SOCKET_INVALID_ID;
147     }
148     int serverId = g_serverMap.AddObject(server);
149     HILOGI("success, serverId: %{public}d, socketType: %{public}d, isEncrypt: %{public}d", serverId,
150         socketPara->socketType, socketPara->isEncrypt);
151     return serverId;
152 }
153 
154 /**
155  * @brief Waits for a remote device to connect to this server socket.
156  *
157  * This method return a client ID indicates a client socket
158  * can be used to read data from and write data to remote device.
159  * This method will block until a connection is established.
160  *
161  * @param serverId The relative ID used to identify the current server socket, obtain the value by calling
162  * {@link SocketServerCreate}.
163  * @return Returns a client ID, if accept fail return {@link BT_SOCKET_INVALID_ID}.
164  */
SocketServerAccept(int serverId)165 int SocketServerAccept(int serverId)
166 {
167     HILOGI("SocketServerAccept start, serverId: %{public}d", serverId);
168     std::shared_ptr<ServerSocket> server = g_serverMap.GetObject(serverId);
169     if (server == nullptr) {
170         HILOGD("server is null!");
171         return BT_SOCKET_INVALID_ID;
172     }
173 
174     std::shared_ptr<ClientSocket> client = server->Accept(0);
175     if (client == nullptr) {
176         HILOGE("client is null!");
177         return BT_SOCKET_INVALID_ID;
178     }
179     int clientId = g_clientMap.AddObject(client);
180     HILOGI("success, clientId: %{public}d", clientId);
181     return clientId;
182 }
183 
184 /**
185  * @brief Disables an socket server socket and releases related resources.
186  *
187  * @param serverId The relative ID used to identify the current server socket, obtain the value by calling
188  * {@link SocketServerCreate}.
189  * @return Returns the operation result status {@link BtStatus}.
190  */
SocketServerClose(int serverId)191 int SocketServerClose(int serverId)
192 {
193     HILOGI("SocketServerClose start, serverId: %{public}d", serverId);
194     std::shared_ptr<ServerSocket> server = g_serverMap.GetObject(serverId);
195     if (server == nullptr) {
196         HILOGE("server is null!");
197         return OHOS_BT_STATUS_FAIL;
198     }
199     server->Close();
200     g_serverMap.RemoveObject(serverId);
201     return OHOS_BT_STATUS_SUCCESS;
202 }
203 
204 /**
205  * @brief Set fast connection flag
206  *
207  * @param bdAddr The remote device address to connect.
208  * @return Returns the operation result status {@link BtStatus}.
209  */
SocketSetFastConnection(const BdAddr * bdAddr)210 int SocketSetFastConnection(const BdAddr *bdAddr)
211 {
212     string strAddress;
213     int leType = 1;
214     if (bdAddr == nullptr) {
215         HILOGE("bdAddr is nullptr.");
216         return OHOS_BT_STATUS_PARM_INVALID;
217     }
218     ConvertAddr(bdAddr->addr, strAddress);
219     // create a client to reuse requestfastestconn.
220     std::shared_ptr<GattClient> client = nullptr;
221     BluetoothRemoteDevice device(strAddress, leType);
222     client = std::make_shared<GattClient>(device);
223     client->Init();
224     int result = client->RequestFastestConn();
225     if (result != OHOS_BT_STATUS_SUCCESS) {
226         HILOGE("request fastest connect fail.");
227         return OHOS_BT_STATUS_FAIL;
228     }
229     return OHOS_BT_STATUS_SUCCESS;
230 }
231 
232 /**
233  * @brief Connects to a remote device over the socket.
234  * This method will block until a connection is made or the connection fails.
235  *
236  * @param socketPara The param to create a client socket and connect to a remote device.
237  * @param bdAddr The remote device address to connect.
238  * @param psm BluetoothSocketType is {@link OHOS_SOCKET_L2CAP_LE} use dynamic PSM value from remote device.
239  * BluetoothSocketType is {@link OHOS_SOCKET_SPP_RFCOMM} use -1.
240  * @return Returns a client ID, if connect fail return {@link BT_SOCKET_INVALID_ID}.
241  */
SocketConnect(const BluetoothCreateSocketPara * socketPara,const BdAddr * bdAddr,int psm)242 int SocketConnect(const BluetoothCreateSocketPara *socketPara, const BdAddr *bdAddr, int psm)
243 {
244     HILOGI("SocketConnect start.");
245     if (socketPara == nullptr || bdAddr == nullptr) {
246         HILOGE("socketPara is nullptr, or bdAddr is nullptr");
247         return BT_SOCKET_INVALID_ID;
248     }
249 
250     string strAddress;
251     ConvertAddr(bdAddr->addr, strAddress);
252     std::shared_ptr<BluetoothRemoteDevice> device = std::make_shared<BluetoothRemoteDevice>(strAddress, 0);
253 
254     UUID serverUuid;
255     if (!GetSocketUuidPara(socketPara, serverUuid)) {
256         return BT_SOCKET_INVALID_ID;
257     }
258 
259     std::shared_ptr<ClientSocket> client = std::make_shared<ClientSocket>(*device, serverUuid,
260         BtSocketType(socketPara->socketType), socketPara->isEncrypt);
261     HILOGI("socketType: %{public}d, isEncrypt: %{public}d", socketPara->socketType, socketPara->isEncrypt);
262     if (socketPara->socketType != OHOS_SOCKET_L2CAP_LE && !client->IsAllowSocketConnect(socketPara->socketType)) {
263         HILOGE("SocketConnect fail due to limited resources, addr: %{public}s", GetEncryptAddr(strAddress).c_str());
264         return BT_SOCKET_LIMITED_RESOURCES;
265     }
266     client->Init();
267     int result = client->Connect(psm);
268     if (result != OHOS_BT_STATUS_SUCCESS) {
269         HILOGE("SocketConnect fail, result: %{public}d", result);
270         client->Close();
271         HILOGE("SocketConnect closed.");
272         if (result == BT_ERR_SPP_CONNECT_FAILED) {
273             return BT_SOCEKET_CONNECT_FAILED;
274         }
275         return BT_SOCKET_INVALID_ID;
276     }
277     int clientId = g_clientMap.AddObject(client);
278     HILOGI("SocketConnect success, clientId: %{public}d", clientId);
279     return clientId;
280 }
281 
282 /**
283  * @brief Obtain the device random address based on the device real address and tokenId.
284  *
285  * @param realAddr The remote device real address.
286  * @param randomAddr The remote device random address.
287  * @param tokenId The relative ID used to identify the current client socket.
288  * @return Returns the operation result status {@link BtStatus}.
289  */
GetRandomAddress(const BdAddr * realAddr,BdAddr * randomAddr,uint64_t tokenId)290 int GetRandomAddress(const BdAddr *realAddr, BdAddr *randomAddr, uint64_t tokenId)
291 {
292     if (realAddr == nullptr || randomAddr == nullptr) {
293         HILOGE("realAddr is nullptr, or randomAddr is nullptr");
294         return OHOS_BT_STATUS_FAIL;
295     }
296 
297     string strRealAddr;
298     ConvertAddr(realAddr->addr, strRealAddr);
299     string strRandomAddr;
300     BluetoothHost *host = &BluetoothHost::GetDefaultHost();
301     int result = host->GetRandomAddress(strRealAddr, strRandomAddr, tokenId);
302     if (result == OHOS_BT_STATUS_SUCCESS) {
303         GetAddrFromString(strRandomAddr, randomAddr->addr);
304     }
305     return result;
306 }
307 
308 /**
309  * @brief Connects to a remote device over the socket.
310  * This method will block until a connection is made or the connection fails.
311  * @param socketPara The param to create a client socket and connect to a remote device.
312  * @param bdAddr The remote device address to connect.
313  * @param psm BluetoothSocketType is {@link OHOS_SOCKET_L2CAP_LE} use dynamic PSM value from remote device.
314  * BluetoothSocketType is {@link OHOS_SOCKET_SPP_RFCOMM} use -1.
315  * @param callback Reference to the socket state observer
316  * @return Returns a client ID, if connect fail return {@link BT_SOCKET_INVALID_ID}.
317  */
SocketConnectEx(const BluetoothCreateSocketPara * socketPara,const BdAddr * bdAddr,int psm,BtSocketConnectionCallback * callback)318 int SocketConnectEx(const BluetoothCreateSocketPara *socketPara, const BdAddr *bdAddr, int psm,
319     BtSocketConnectionCallback *callback)
320 {
321     HILOGI("SocketConnect start.");
322     if (socketPara == nullptr || bdAddr == nullptr || callback == nullptr) {
323         HILOGE("socketPara is nullptr, or bdAddr is nullptr, or callback is nullptr");
324         return BT_SOCKET_INVALID_ID;
325     }
326 
327     string strAddress;
328     ConvertAddr(bdAddr->addr, strAddress);
329     std::shared_ptr<BluetoothRemoteDevice> device = std::make_shared<BluetoothRemoteDevice>(strAddress, 0);
330 
331     UUID serverUuid;
332     if (!GetSocketUuidPara(socketPara, serverUuid)) {
333         return BT_SOCKET_INVALID_ID;
334     }
335 
336     if (socketPara->socketType != OHOS_SOCKET_SPP_RFCOMM && socketPara->socketType != OHOS_SOCKET_L2CAP_LE) {
337         HILOGE("SocketType is not support");
338         return BT_SOCKET_INVALID_TYPE;
339     }
340 
341     std::shared_ptr<BluetoothConnectionObserverWapper> connWrapper =
342         std::make_shared<BluetoothConnectionObserverWapper>(*callback);
343     std::shared_ptr<ClientSocket> client = std::make_shared<ClientSocket>(*device, serverUuid,
344         BtSocketType(socketPara->socketType), socketPara->isEncrypt, connWrapper);
345     HILOGI("socketType: %{public}d, isEncrypt: %{public}d", socketPara->socketType, socketPara->isEncrypt);
346     if (socketPara->socketType != OHOS_SOCKET_L2CAP_LE && !client->IsAllowSocketConnect(socketPara->socketType)) {
347         HILOGE("SocketConnect fail due to limited resources, addr: %{public}s", GetEncryptAddr(strAddress).c_str());
348         return BT_SOCKET_LIMITED_RESOURCES;
349     }
350     client->Init();
351     int result = client->Connect(psm);
352     if (result != OHOS_BT_STATUS_SUCCESS) {
353         HILOGE("SocketConnect fail, result: %{public}d", result);
354         client->Close();
355         HILOGE("SocketConnect closed.");
356         return BT_SOCKET_INVALID_ID;
357     }
358     int clientId = g_clientMap.AddObject(client);
359     HILOGI("SocketConnect success, clientId: %{public}d", clientId);
360     return clientId;
361 }
362 
363 /**
364  * @brief Disables a connection and releases related resources.
365  *
366  * @param clientId The relative ID used to identify the current client socket.
367  * @return Returns the operation result status {@link BtStatus}.
368  */
SocketDisconnect(int clientId)369 int SocketDisconnect(int clientId)
370 {
371     HILOGI("SocketDisconnect start, clientId: %{public}d", clientId);
372     std::shared_ptr<ClientSocket> client = g_clientMap.GetObject(clientId);
373     if (client == nullptr) {
374         HILOGE("client is null, clientId: %{public}d", clientId);
375         return OHOS_BT_STATUS_FAIL;
376     }
377     client->Close();
378     g_clientMap.RemoveObject(clientId);
379     HILOGI("SocketDisConnect success, clientId: %{public}d", clientId);
380     return OHOS_BT_STATUS_SUCCESS;
381 }
382 
383 /**
384  * @brief Socket get remote device's address.
385  *
386  * @param clientId The relative ID used to identify the current client socket.
387  * @param remoteAddr Remote device's address, memory allocated by caller.
388  * @return Returns the operation result status {@link BtStatus}.
389  */
SocketGetRemoteAddr(int clientId,BdAddr * remoteAddr)390 int SocketGetRemoteAddr(int clientId, BdAddr *remoteAddr)
391 {
392     HILOGI("SocketGetRemoteAddr clientId: %{public}d", clientId);
393     if (remoteAddr == nullptr) {
394         HILOGE("remoteAddr is null, clientId: %{public}d", clientId);
395         return OHOS_BT_STATUS_PARM_INVALID;
396     }
397     std::shared_ptr<ClientSocket> client = g_clientMap.GetObject(clientId);
398     if (client == nullptr) {
399         HILOGE("client is null, clientId: %{public}d", clientId);
400         return OHOS_BT_STATUS_FAIL;
401     }
402     string tmpAddr = client->GetRemoteDevice().GetDeviceAddr();
403     GetAddrFromString(tmpAddr, remoteAddr->addr);
404     HILOGI("device: %{public}s", GetEncryptAddr(tmpAddr).c_str());
405     return OHOS_BT_STATUS_SUCCESS;
406 }
407 
408 /**
409  * @brief Get the connection status of this socket.
410  *
411  * @param clientId The relative ID used to identify the current client socket.
412  * @return Returns true is connected or false is not connected.
413  */
IsSocketConnected(int clientId)414 bool IsSocketConnected(int clientId)
415 {
416     HILOGI("IsSocketConnected clientId: %{public}d", clientId);
417     std::shared_ptr<ClientSocket> client = g_clientMap.GetObject(clientId);
418     if (client == nullptr) {
419         HILOGE("client is null, clientId: %{public}d", clientId);
420         return false;
421     }
422     bool isConnected = client->IsConnected();
423     HILOGI("clientId: %{public}d, isConnected: %{public}d", clientId, isConnected);
424     return isConnected;
425 }
426 
427 /**
428  * @brief Read data from socket.
429  * This method blocks until input data is available.
430  *
431  * @param clientId The relative ID used to identify the current client socket.
432  * @param buf Indicate the buffer which read in, memory allocated by caller.
433  * @param bufLen Indicate the buffer length.
434  * @return Returns the length greater than 0 as read the actual length.
435  * Returns {@link BT_SOCKET_READ_SOCKET_CLOSED} if the socket is closed.
436  * Returns {@link BT_SOCKET_READ_FAILED} if the operation failed.
437  */
SocketRead(int clientId,uint8_t * buf,uint32_t bufLen)438 int SocketRead(int clientId, uint8_t *buf, uint32_t bufLen)
439 {
440     HILOGD("SocketRead start, clientId: %{public}d, bufLen: %{public}d", clientId, bufLen);
441     if (buf == nullptr || bufLen == 0) {
442         HILOGE("buf is null or bufLen is 0! clientId: %{public}d", clientId);
443         return BT_SOCKET_READ_FAILED;
444     }
445     std::shared_ptr<ClientSocket> client = g_clientMap.GetObject(clientId);
446     if (client == nullptr) {
447         HILOGE("client is null, clientId: %{public}d", clientId);
448         return BT_SOCKET_READ_FAILED;
449     }
450     if (client->GetInputStream() == nullptr) {
451         HILOGE("inputStream is null, clientId: %{public}d", clientId);
452         return BT_SOCKET_READ_FAILED;
453     }
454     int readLen = client->GetInputStream()->Read(buf, bufLen);
455     HILOGD("SocketRead ret, clientId: %{public}d, readLen: %{public}d", clientId, readLen);
456     return readLen;
457 }
458 
459 /**
460  * @brief Client write data to socket.
461  *
462  * @param clientId The relative ID used to identify the current client socket.
463  * @param data Indicate the data to be written.
464  * @return Returns the actual write length.
465  * Returns {@link BT_SOCKET_WRITE_FAILED} if the operation failed.
466  */
SocketWrite(int clientId,const uint8_t * data,uint32_t len)467 int SocketWrite(int clientId, const uint8_t *data, uint32_t len)
468 {
469     HILOGD("SocketWrite start, clientId: %{public}d, len: %{public}d", clientId, len);
470     if (data == nullptr || len == 0) {
471         HILOGE("data is null or len is 0! clientId: %{public}d", clientId);
472         return BT_SOCKET_WRITE_FAILED;
473     }
474     std::shared_ptr<ClientSocket> client = g_clientMap.GetObject(clientId);
475     if (client == nullptr) {
476         HILOGE("client is null! clientId: %{public}d", clientId);
477         return BT_SOCKET_WRITE_FAILED;
478     }
479     if (client->GetOutputStream() == nullptr) {
480         HILOGE("outputStream is null, clientId: %{public}d", clientId);
481         return BT_SOCKET_READ_FAILED;
482     }
483     int writeLen = client->GetOutputStream()->Write(data, len);
484     HILOGD("end, writeLen: %{public}d", writeLen);
485     return writeLen;
486 }
487 
488 /**
489  * @brief Get dynamic PSM value for OHOS_SOCKET_L2CAP.
490  *
491  * @param serverId The relative ID used to identify the current server socket, obtain the value by calling
492  * {@link SocketServerCreate}.
493  * @return Returns the PSM value.
494  * Returns {@link BT_SOCKET_INVALID_PSM} if the operation failed.
495  */
SocketGetPsm(int serverId)496 int SocketGetPsm(int serverId)
497 {
498     HILOGI("serverId: %{public}d", serverId);
499     std::shared_ptr<ServerSocket> server = g_serverMap.GetObject(serverId);
500     CHECK_AND_RETURN_LOG_RET(server, BT_SOCKET_INVALID_PSM, "server is null!");
501     return server->GetL2capPsm();
502 }
503 
504 /**
505  * @brief Get server channel number for OHOS_SOCKET_RFCOMM.
506  *
507  * @param serverId The relative ID used to identify the current server socket, obtain the value by calling
508  * {@link SocketServerCreate}.
509  * @return Returns the scn.
510  * Returns {@link BT_SOCKET_INVALID_PSM} if the operation failed.
511  */
SocketGetScn(int serverId)512 int SocketGetScn(int serverId)
513 {
514     HILOGI("serverId: %{public}d", serverId);
515     std::shared_ptr<ServerSocket> server = g_serverMap.GetObject(serverId);
516     CHECK_AND_RETURN_LOG_RET(server, BT_SOCKET_INVALID_SCN, "server is null!");
517     return server->GetRfcommScn();
518 }
519 
520 /**
521  * @brief Adjust the socket send and recv buffer size, limit range is 4KB to 50KB
522  *
523  * @param clientId The relative ID used to identify the current client socket.
524  * @param bufferSize The buffer size want to set, unit is byte.
525  * @return  Returns the operation result status {@link BtStatus}.
526  */
SetSocketBufferSize(int clientId,uint32_t bufferSize)527 int SetSocketBufferSize(int clientId, uint32_t bufferSize)
528 {
529     HILOGI("start, clientId: %{public}d, bufferSize: %{public}d", clientId, bufferSize);
530     std::shared_ptr<ClientSocket> client = g_clientMap.GetObject(clientId);
531     if (client == nullptr) {
532         HILOGE("client is null! clientId: %{public}d", clientId);
533         return OHOS_BT_STATUS_FAIL;
534     }
535     int ret = client->SetBufferSize(bufferSize);
536     if (ret == RET_BAD_PARAM) {
537         return OHOS_BT_STATUS_PARM_INVALID;
538     } else if (ret == RET_BAD_STATUS) {
539         return OHOS_BT_STATUS_FAIL;
540     }
541     return OHOS_BT_STATUS_SUCCESS;
542 }
543 /**
544  * @brief Update the coc connection params
545  *
546  * @param param CocUpdateSocketParam instance for carry params.
547  * @param bdAddr The remote device address to connect.
548  * @return Returns the operation result status {@link BtStatus}.
549  */
SocketUpdateCocConnectionParams(BluetoothCocUpdateSocketParam * param,const BdAddr * bdAddr)550 int SocketUpdateCocConnectionParams(BluetoothCocUpdateSocketParam* param, const BdAddr *bdAddr)
551 {
552     CocUpdateSocketParam params;
553 
554     HILOGI("Socket update coc params start");
555     CHECK_AND_RETURN_LOG_RET(param, OHOS_BT_STATUS_FAIL, "param is null");
556     CHECK_AND_RETURN_LOG_RET(bdAddr, OHOS_BT_STATUS_FAIL, "bdAddr is null");
557     ConvertAddr(bdAddr->addr, params.addr);
558     params.minInterval = param->minInterval;
559     params.maxInterval = param->maxInterval;
560     params.peripheralLatency = param->peripheralLatency;
561     params.supervisionTimeout = param->supervisionTimeout;
562     params.minConnEventLen = param->minConnEventLen;
563     params.maxConnEventLen = param->maxConnEventLen;
564 
565     std::shared_ptr<BluetoothRemoteDevice> device = std::make_shared<BluetoothRemoteDevice>(params.addr,
566         OHOS_SOCKET_SPP_RFCOMM);
567     std::shared_ptr<ClientSocket> client = std::make_shared<ClientSocket>(*device, UUID::RandomUUID(),
568         TYPE_L2CAP_LE, false);
569     CHECK_AND_RETURN_LOG_RET(client, OHOS_BT_STATUS_FAIL, "client is null");
570     return client->UpdateCocConnectionParams(params);
571 }
572 
573 }  // namespace Bluetooth
574 }  // namespace OHOS
575 #ifdef __cplusplus
576 }
577 #endif