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 "wrapper_br_interface.h"
17
18 #include "c_header/ohos_bt_def.h"
19 #include "c_header/ohos_bt_gap.h"
20 #include "c_header/ohos_bt_spp.h"
21 #include "c_header/ohos_bt_socket.h"
22 #include "conn_log.h"
23 #include "securec.h"
24 #include "softbus_errcode.h"
25 #include "string.h"
26
27 #define IS_BR_ENCRYPT false
28
Init(const struct tagSppSocketDriver * sppDriver)29 static void Init(const struct tagSppSocketDriver *sppDriver)
30 {
31 (void)sppDriver;
32 }
33
OpenSppServer(const char * name,int32_t nameLen,const char * uuid,int32_t isSecure)34 static int32_t OpenSppServer(const char *name, int32_t nameLen, const char *uuid, int32_t isSecure)
35 {
36 if (name == NULL || nameLen <= 0) {
37 CONN_LOGW(CONN_BR, "OpenSppServer invalid param");
38 return SOFTBUS_INVALID_PARAM;
39 }
40 (void)isSecure;
41
42 BtCreateSocketPara socketPara;
43 (void)memset_s((char *)&socketPara, sizeof(socketPara), 0, sizeof(socketPara));
44 socketPara.uuid.uuid = (char *)uuid;
45 socketPara.uuid.uuidLen = strlen(uuid);
46 socketPara.socketType = OHOS_SPP_SOCKET_RFCOMM;
47 socketPara.isEncrypt = IS_BR_ENCRYPT;
48 return SppServerCreate(&socketPara, name, nameLen);
49 }
50
CloseSppServer(int32_t serverFd)51 static void CloseSppServer(int32_t serverFd)
52 {
53 CONN_LOGI(CONN_BR, "[CloseServer Connect, and serverFd=%{public}d]", serverFd);
54 SppServerClose(serverFd);
55 }
56
ConnectByPort(const char * uuid,const BT_ADDR mac,const int socketPsmValue,void * connectCallback)57 static int32_t ConnectByPort(const char *uuid, const BT_ADDR mac, const int socketPsmValue, void *connectCallback)
58 {
59 if (mac == NULL) {
60 return SOFTBUS_INVALID_PARAM;
61 }
62 BluetoothCreateSocketPara socketPara;
63 (void)memset_s((char *)&socketPara, sizeof(socketPara), 0, sizeof(socketPara));
64 socketPara.uuid.uuid = (char *)uuid;
65 socketPara.uuid.uuidLen = strlen(uuid);
66 socketPara.socketType = OHOS_SOCKET_SPP_RFCOMM;
67 socketPara.isEncrypt = IS_BR_ENCRYPT;
68
69 BdAddr bdAddr;
70 (void)memset_s((char *)&bdAddr, sizeof(bdAddr), 0, sizeof(bdAddr));
71 if (memcpy_s((char *)bdAddr.addr, OHOS_BD_ADDR_LEN, mac, BT_ADDR_LEN) != EOK) {
72 CONN_LOGE(CONN_BR, "Connect memcpy_s failed");
73 return SOFTBUS_MEM_ERR;
74 }
75 int ret = SocketConnectEx(&socketPara, &bdAddr, socketPsmValue, (BtSocketConnectionCallback *)connectCallback);
76 if (ret < 0) {
77 CONN_LOGE(CONN_BR, "connect failed, ret=%{public}d", ret);
78 return SOFTBUS_CONN_BR_SOCKET_CONNECT_ERR;
79 }
80 CONN_LOGI(CONN_BR, "SocketConnect ok. clientId=%{public}d", ret);
81 return ret;
82 }
83
Connect(const char * uuid,const BT_ADDR mac,void * connectCallback)84 static int32_t Connect(const char *uuid, const BT_ADDR mac, void *connectCallback)
85 {
86 return ConnectByPort(uuid, mac, -1, connectCallback);
87 }
88
DisConnect(int32_t clientFd)89 static int32_t DisConnect(int32_t clientFd)
90 {
91 CONN_LOGI(CONN_BR, "[DisConnect, and clientFd=%{public}d]", clientFd);
92 return SppDisconnect(clientFd);
93 }
94
IsConnected(int32_t clientFd)95 static bool IsConnected(int32_t clientFd)
96 {
97 CONN_LOGI(CONN_BR, "[get connected state from bt, clientFd=%{public}d]", clientFd);
98 return IsSppConnected(clientFd);
99 }
100
Accept(int32_t serverFd)101 static int32_t Accept(int32_t serverFd)
102 {
103 CONN_LOGI(CONN_BR, "[Accept remote device to connect, and serverFd=%{public}d]", serverFd);
104 int32_t ret = SppServerAccept(serverFd);
105 if (ret == BT_SPP_INVALID_ID) {
106 CONN_LOGE(CONN_BR, "Accept spp server failed");
107 return SOFTBUS_CONN_BR_SPP_SERVER_ERR;
108 }
109 return ret;
110 }
111
Write(int32_t clientFd,const uint8_t * buf,const int32_t len)112 static int32_t Write(int32_t clientFd, const uint8_t *buf, const int32_t len)
113 {
114 return SppWrite(clientFd, (const char *)buf, len);
115 }
116
Read(int32_t clientFd,uint8_t * buf,const int32_t len)117 static int32_t Read(int32_t clientFd, uint8_t *buf, const int32_t len)
118 {
119 int32_t ret = SppRead(clientFd, (char *)buf, len);
120 if (ret == BT_SPP_READ_SOCKET_CLOSED) {
121 return BR_READ_SOCKET_CLOSED;
122 } else if (ret == BT_SPP_READ_FAILED) {
123 return BR_READ_FAILED;
124 }
125 return ret;
126 }
127
GetRemoteDeviceInfo(int32_t clientFd,const BluetoothRemoteDevice * device)128 static int32_t GetRemoteDeviceInfo(int32_t clientFd, const BluetoothRemoteDevice *device)
129 {
130 CONN_LOGI(CONN_BR, "[to get remotedeviceinfo, clientFd=%{public}d]", clientFd);
131 BdAddr bdAddr;
132 (void)memset_s((char *)&bdAddr, sizeof(bdAddr), 0, sizeof(bdAddr));
133 (void)SppGetRemoteAddr(clientFd, &bdAddr);
134 if (memcpy_s((char *)device->mac, BT_ADDR_LEN, (char *)bdAddr.addr, OHOS_BD_ADDR_LEN) != EOK) {
135 CONN_LOGE(CONN_BR, "GetRemoteDeviceInfo memcpy_s failed");
136 return SOFTBUS_MEM_ERR;
137 }
138
139 return SOFTBUS_OK;
140 }
GetSppServerPort(int serverId)141 static int32_t GetSppServerPort(int serverId)
142 {
143 return SocketGetScn(serverId);
144 }
145
146 static SppSocketDriver g_sppSocketDriver = {
147 .Init = Init,
148 .OpenSppServer = OpenSppServer,
149 .CloseSppServer = CloseSppServer,
150 .Connect = Connect,
151 .ConnectByPort = ConnectByPort,
152 .DisConnect = DisConnect,
153 .IsConnected = IsConnected,
154 .Accept = Accept,
155 .Write = Write,
156 .Read = Read,
157 .GetRemoteDeviceInfo = GetRemoteDeviceInfo,
158 .GetSppServerPort = GetSppServerPort
159 };
160
IsAclConnected(const BT_ADDR mac)161 bool IsAclConnected(const BT_ADDR mac)
162 {
163 CONN_LOGW(CONN_BR, "IsAclConnected not implement");
164 return false;
165 }
166
InitSppSocketDriver()167 SppSocketDriver *InitSppSocketDriver()
168 {
169 CONN_LOGI(CONN_INIT, "[InitSppSocketDriver]");
170 Init(&g_sppSocketDriver);
171 return &g_sppSocketDriver;
172 }
173