1 /*
2 * Copyright (c) 2025 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 "general_client_connection.h"
17
18 #include <string.h>
19 #include "conn_log.h"
20 #include "general_connection_server_proxy.h"
21 #include "softbus_adapter_thread.h"
22 #include "softbus_client_stub_interface.h"
23 #include "softbus_connection.h"
24 #include "softbus_utils.h"
25 #include "softbus_client_frame_manager.h"
26
27 static IGeneralListener *g_connectionListener = NULL;
28 static SoftBusMutex g_connectionListenerLock;
29 const char *g_limitPkgName = "ohos.distributedschedule.dms";
30
IsValidListener(IGeneralListener * listener)31 static bool IsValidListener(IGeneralListener *listener)
32 {
33 if (listener == NULL || listener->OnAcceptConnect == NULL || listener->OnConnectionStateChange == NULL ||
34 listener->OnDataReceived == NULL || listener->OnServiceDied == NULL) {
35 CONN_LOGE(CONN_INIT, "listener is invalid");
36 return false;
37 }
38 return true;
39 }
40
GeneralRegisterListener(IGeneralListener * listener)41 int32_t GeneralRegisterListener(IGeneralListener *listener)
42 {
43 if (!IsValidListener(listener)) {
44 CONN_LOGE(CONN_INIT, "listener is invalid");
45 return SOFTBUS_INVALID_PARAM;
46 }
47 if (SoftBusMutexInit(&g_connectionListenerLock, NULL) != SOFTBUS_OK) {
48 CONN_LOGE(CONN_INIT, "mutex init failed");
49 return SOFTBUS_LOCK_ERR;
50 }
51 int32_t ret = SoftBusMutexLock(&g_connectionListenerLock);
52 if (ret != SOFTBUS_OK) {
53 CONN_LOGE(CONN_INIT, "lock failed");
54 return ret;
55 }
56 g_connectionListener = listener;
57 (void)SoftBusMutexUnlock(&g_connectionListenerLock);
58 CONN_LOGI(CONN_INIT, "GeneralRegisterListener success");
59 return SOFTBUS_OK;
60 }
61
GeneralUnregisterListener(void)62 int32_t GeneralUnregisterListener(void)
63 {
64 if (g_connectionListener == NULL) {
65 CONN_LOGW(CONN_INIT, "listener has not registered");
66 return SOFTBUS_OK;
67 }
68 int32_t ret = SoftBusMutexLock(&g_connectionListenerLock);
69 if (ret != SOFTBUS_OK) {
70 CONN_LOGE(CONN_INIT, "lock failed");
71 return ret;
72 }
73 g_connectionListener = NULL;
74 (void)SoftBusMutexUnlock(&g_connectionListenerLock);
75 (void)SoftBusMutexDestroy(&g_connectionListenerLock);
76 CONN_LOGI(CONN_INIT, "GeneralUnregisterListener success");
77 return SOFTBUS_OK;
78 }
79
CheckNameIsValid(const char * pkgName,const char * name)80 static int32_t CheckNameIsValid(const char *pkgName, const char *name)
81 {
82 if (!IsValidString(pkgName, PKG_NAME_SIZE_MAX - 1)) {
83 CONN_LOGE(CONN_COMMON, "invalid package name");
84 return SOFTBUS_INVALID_PARAM;
85 }
86 if (!IsValidString(name, SESSION_NAME_SIZE_MAX - 1)) {
87 CONN_LOGE(CONN_COMMON, "invalid name");
88 return SOFTBUS_INVALID_PARAM;
89 }
90 if (strcmp(pkgName, g_limitPkgName) != 0) {
91 CONN_LOGE(CONN_COMMON, "invalid package name");
92 return SOFTBUS_INVALID_PARAM;
93 }
94 return SOFTBUS_OK;
95 }
96
GeneralCreateServer(const char * pkgName,const char * name)97 int32_t GeneralCreateServer(const char *pkgName, const char *name)
98 {
99 int32_t ret = CheckNameIsValid(pkgName, name);
100 if (ret != SOFTBUS_OK) {
101 CONN_LOGE(CONN_COMMON, "invalid param");
102 return ret;
103 }
104 ret = InitSoftBus(pkgName);
105 if (ret != SOFTBUS_OK) {
106 CONN_LOGE(CONN_COMMON, "register service failed");
107 return ret;
108 }
109 ret = ServerIpcCreateServer(pkgName, name);
110 if (ret != SOFTBUS_OK) {
111 CONN_LOGE(CONN_COMMON, "create server failed");
112 return ret;
113 }
114 CONN_LOGI(CONN_COMMON, "create server success");
115 return ret;
116 }
117
GeneralRemoveServer(const char * pkgName,const char * name)118 int32_t GeneralRemoveServer(const char *pkgName, const char *name)
119 {
120 int32_t ret = CheckNameIsValid(pkgName, name);
121 if (ret != SOFTBUS_OK) {
122 CONN_LOGE(CONN_COMMON, "invalid param");
123 return ret;
124 }
125 ret = ServerIpcRemoveServer(pkgName, name);
126 if (ret != SOFTBUS_OK) {
127 CONN_LOGE(CONN_COMMON, "remove server failed");
128 return ret;
129 }
130 CONN_LOGI(CONN_COMMON, "remove server success");
131 return ret;
132 }
133
GeneralConnect(const char * pkgName,const char * name,const Address * address)134 int32_t GeneralConnect(const char *pkgName, const char *name, const Address *address)
135 {
136 int32_t ret = CheckNameIsValid(pkgName, name);
137 if (ret != SOFTBUS_OK) {
138 CONN_LOGE(CONN_COMMON, "invalid param");
139 return ret;
140 }
141 if (address == NULL) {
142 CONN_LOGE(CONN_COMMON, "address is null");
143 return SOFTBUS_INVALID_PARAM;
144 }
145 ret = InitSoftBus(pkgName);
146 if (ret != SOFTBUS_OK) {
147 CONN_LOGE(CONN_COMMON, "register service failed");
148 return ret;
149 }
150 int32_t handle = ServerIpcConnect(pkgName, name, address);
151 if (handle <= 0) {
152 CONN_LOGE(CONN_COMMON, "connect failed, error=%{public}d", handle);
153 return handle;
154 }
155 CONN_LOGI(CONN_COMMON, "connect success, handle=%{public}d", handle);
156 return handle;
157 }
158
GeneralDisconnect(uint32_t handle)159 int32_t GeneralDisconnect(uint32_t handle)
160 {
161 CONN_LOGI(CONN_COMMON, "sdk disconnect, handle=%{public}u", handle);
162 if (handle <= 0) {
163 CONN_LOGE(CONN_COMMON, "invalid handle");
164 return SOFTBUS_INVALID_PARAM;
165 }
166 int32_t ret = ServerIpcDisconnect(handle);
167 if (ret != SOFTBUS_OK) {
168 CONN_LOGE(CONN_COMMON, "disconnect failed, ret=%{public}d", ret);
169 return ret;
170 }
171 CONN_LOGI(CONN_COMMON, "disconnect success, handle=%{public}u", handle);
172 return SOFTBUS_OK;
173 }
174
GeneralSend(uint32_t handle,const uint8_t * data,uint32_t len)175 int32_t GeneralSend(uint32_t handle, const uint8_t *data, uint32_t len)
176 {
177 CONN_LOGI(CONN_COMMON, "sdk send, handle=%{public}u, len=%{public}u", handle, len);
178 if (handle <= 0) {
179 CONN_LOGE(CONN_COMMON, "invalid handle");
180 return SOFTBUS_INVALID_PARAM;
181 }
182 if (data == NULL) {
183 CONN_LOGE(CONN_COMMON, "data is null");
184 return SOFTBUS_INVALID_PARAM;
185 }
186 if (len == 0 || len > GENERAL_SEND_DATA_MAX_LEN) {
187 CONN_LOGE(CONN_COMMON, "len is invalid");
188 return SOFTBUS_INVALID_PARAM;
189 }
190 int32_t ret = ServerIpcSend(handle, data, len);
191 if (ret != SOFTBUS_OK) {
192 CONN_LOGE(CONN_COMMON, "send failed");
193 return ret;
194 }
195 CONN_LOGI(CONN_COMMON, "send success, handle=%{public}u, len=%{public}u", handle, len);
196 return ret;
197 }
198
GeneralGetPeerDeviceId(uint32_t handle,char * deviceId,uint32_t len)199 int32_t GeneralGetPeerDeviceId(uint32_t handle, char *deviceId, uint32_t len)
200 {
201 CONN_LOGI(CONN_COMMON, "sdk get peer device id, handle=%{public}u", handle);
202 if (handle <= 0) {
203 CONN_LOGE(CONN_COMMON, "invalid handle");
204 return SOFTBUS_INVALID_PARAM;
205 }
206 if (deviceId == NULL) {
207 CONN_LOGE(CONN_COMMON, "deviceId is null");
208 return SOFTBUS_INVALID_PARAM;
209 }
210 if (len == 0 || len > BT_MAC_LEN) {
211 CONN_LOGE(CONN_COMMON, "len is 0 or too long");
212 return SOFTBUS_INVALID_PARAM;
213 }
214 int32_t ret = ServerIpcGetPeerDeviceId(handle, deviceId, len);
215 if (ret != SOFTBUS_OK) {
216 CONN_LOGE(CONN_COMMON, "get peer device id failed, ret=%{public}d", ret);
217 return ret;
218 }
219 CONN_LOGI(CONN_COMMON, "get peer device id success, handle=%{public}u", handle);
220 return SOFTBUS_OK;
221 }
222
ConnectionStateChange(uint32_t handle,int32_t state,int32_t reason)223 int32_t ConnectionStateChange(uint32_t handle, int32_t state, int32_t reason)
224 {
225 CONN_LOGI(CONN_COMMON, "sdk connection state change, handle=%{public}u, state=%{public}d, reason=%{public}d",
226 handle, state, reason);
227 if (SoftBusMutexLock(&g_connectionListenerLock) != SOFTBUS_OK) {
228 CONN_LOGE(CONN_INIT, "lock failed");
229 return SOFTBUS_LOCK_ERR;
230 }
231 if (g_connectionListener == NULL || g_connectionListener->OnConnectionStateChange == NULL) {
232 (void)SoftBusMutexUnlock(&g_connectionListenerLock);
233 CONN_LOGE(CONN_COMMON, "notify connection state change failed, listener is null.");
234 return SOFTBUS_NO_INIT;
235 }
236 int32_t ret = g_connectionListener->OnConnectionStateChange(handle, state, reason);
237 (void)SoftBusMutexUnlock(&g_connectionListenerLock);
238 if (ret != SOFTBUS_OK) {
239 CONN_LOGE(CONN_COMMON, "notify connection state change failed, ret=%{public}d", ret);
240 return ret;
241 }
242 CONN_LOGI(CONN_COMMON, "notify connection state change success");
243 return SOFTBUS_OK;
244 }
245
AcceptConnect(const char * name,uint32_t handle)246 int32_t AcceptConnect(const char *name, uint32_t handle)
247 {
248 CONN_LOGI(CONN_COMMON, "sdk accept connect, handle=%{public}u", handle);
249 if (SoftBusMutexLock(&g_connectionListenerLock) != SOFTBUS_OK) {
250 CONN_LOGE(CONN_INIT, "lock failed");
251 return SOFTBUS_LOCK_ERR;
252 }
253 if (g_connectionListener == NULL || g_connectionListener->OnAcceptConnect == NULL) {
254 (void)SoftBusMutexUnlock(&g_connectionListenerLock);
255 CONN_LOGE(CONN_COMMON, "notify accept connect failed, listener is null.");
256 return SOFTBUS_NO_INIT;
257 }
258 int32_t ret = g_connectionListener->OnAcceptConnect(name, handle);
259 (void)SoftBusMutexUnlock(&g_connectionListenerLock);
260 if (ret != SOFTBUS_OK) {
261 CONN_LOGE(CONN_COMMON, "accept connect failed, ret=%{public}d", ret);
262 return ret;
263 }
264 CONN_LOGI(CONN_COMMON, "notify accept connect success");
265 return SOFTBUS_OK;
266 }
267
DataReceived(uint32_t handle,const uint8_t * data,uint32_t len)268 void DataReceived(uint32_t handle, const uint8_t *data, uint32_t len)
269 {
270 CONN_LOGI(CONN_COMMON, "sdk data received, handle=%{public}u, len=%{public}u", handle, len);
271 if (SoftBusMutexLock(&g_connectionListenerLock) != SOFTBUS_OK) {
272 CONN_LOGE(CONN_INIT, "lock failed");
273 return;
274 }
275 if (g_connectionListener == NULL || g_connectionListener->OnDataReceived == NULL) {
276 (void)SoftBusMutexUnlock(&g_connectionListenerLock);
277 CONN_LOGE(CONN_COMMON, "notify data received failed, listener is null.");
278 return;
279 }
280 g_connectionListener->OnDataReceived(handle, data, len);
281 (void)SoftBusMutexUnlock(&g_connectionListenerLock);
282 CONN_LOGI(CONN_COMMON, "notify data received success");
283 }
284
ConnectionDeathNotify(void)285 void ConnectionDeathNotify(void)
286 {
287 CONN_LOGI(CONN_COMMON, "connection death notify.");
288 if (g_connectionListener == NULL) {
289 CONN_LOGI(CONN_COMMON, "listener has not registered, no need to notify.");
290 return;
291 }
292 if (SoftBusMutexLock(&g_connectionListenerLock) != SOFTBUS_OK) {
293 CONN_LOGE(CONN_INIT, "lock failed");
294 return;
295 }
296 if (g_connectionListener->OnConnectionStateChange != NULL) {
297 (void)g_connectionListener->OnConnectionStateChange(0, CONNECTION_STATE_DISCONNECTED, SOFTBUS_CONN_FAIL);
298 }
299 if (g_connectionListener->OnServiceDied != NULL) {
300 g_connectionListener->OnServiceDied();
301 }
302 (void)SoftBusMutexUnlock(&g_connectionListenerLock);
303 CONN_LOGI(CONN_COMMON, "connection death notify success.");
304 }