• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 "softbus_conn_ipc.h"
17 #include "softbus_conn_general_connection.h"
18 
19 #include "general_connection_client_proxy.h"
20 #include "conn_log.h"
21 #include "softbus_connection.h"
22 
GeneralDataReceived(GeneralConnectionParam * info,uint32_t generalHandle,const uint8_t * data,uint32_t dataLen)23 static void GeneralDataReceived(GeneralConnectionParam *info,
24     uint32_t generalHandle, const uint8_t *data, uint32_t dataLen)
25 {
26     ClientIpcOnDataReceived(info->pkgName, info->pid, generalHandle, data, dataLen);
27 }
28 
GeneralConnectionDisconnected(GeneralConnectionParam * info,uint32_t generalHandle,int32_t reason)29 static void GeneralConnectionDisconnected(GeneralConnectionParam *info, uint32_t generalHandle, int32_t reason)
30 {
31     ClientIpcOnConnectionStateChange(info->pkgName, info->pid, generalHandle, CONNECTION_STATE_DISCONNECTED, reason);
32 }
33 
GeneralAcceptConnect(GeneralConnectionParam * info,uint32_t generalHandle)34 static void GeneralAcceptConnect(GeneralConnectionParam *info, uint32_t generalHandle)
35 {
36     ClientIpcOnAcceptConnect(info->pkgName, info->pid, info->name, generalHandle);
37 }
38 
GeneralConnectFail(GeneralConnectionParam * info,uint32_t generalHandle,int32_t reason)39 static void GeneralConnectFail(GeneralConnectionParam *info, uint32_t generalHandle, int32_t reason)
40 {
41     ClientIpcOnConnectionStateChange(info->pkgName, info->pid, generalHandle,
42         CONNECTION_STATE_CONNECTED_FAILED, reason);
43 }
44 
GeneralConnectSuccess(GeneralConnectionParam * info,uint32_t generalHandle)45 static void GeneralConnectSuccess(GeneralConnectionParam *info, uint32_t generalHandle)
46 {
47     ClientIpcOnConnectionStateChange(info->pkgName, info->pid, generalHandle, CONNECTION_STATE_CONNECTED_SUCCESS, 0);
48 }
49 
50 GeneralConnectionListener g_baseListener = {
51     .onConnectSuccess = GeneralConnectSuccess,
52     .onConnectFailed = GeneralConnectFail,
53     .onAcceptConnect = GeneralAcceptConnect,
54     .onDataReceived = GeneralDataReceived,
55     .onConnectionDisconnected = GeneralConnectionDisconnected,
56 };
57 
ClearGeneralConnection(const char * pkgName,int32_t pid)58 void ClearGeneralConnection(const char *pkgName, int32_t pid)
59 {
60     GeneralConnectionManager *manager = GetGeneralConnectionManager();
61     if (manager == NULL) {
62         COMM_LOGE(CONN_COMMON, "manager is null");
63         return;
64     }
65     manager->cleanupGeneralConnection(pkgName, pid);
66 }
67 
InitGeneralConnection(void)68 int32_t InitGeneralConnection(void)
69 {
70     int32_t ret = InitGeneralConnectionManager();
71     if (ret != SOFTBUS_OK) {
72         COMM_LOGE(CONN_COMMON, "init general manager fail, err=%{public}d", ret);
73         return SOFTBUS_NO_INIT;
74     }
75 
76     GeneralConnectionManager *manager = GetGeneralConnectionManager();
77     if (manager == NULL) {
78         COMM_LOGE(CONN_COMMON, "manager is null");
79         return SOFTBUS_NO_INIT;
80     }
81     ret = manager->registerListener(&g_baseListener);
82     if (ret != SOFTBUS_OK) {
83         COMM_LOGE(CONN_COMMON, "init general manager fail, err=%{public}d", ret);
84         return SOFTBUS_NO_INIT;
85     }
86     COMM_LOGI(CONN_COMMON, "init and refister listener success");
87     return SOFTBUS_OK;
88 }