• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2021-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_server_frame.h"
17 
18 #include <dlfcn.h>
19 #include "auth_apply_key_manager.h"
20 #include "auth_interface.h"
21 #include "auth_uk_manager.h"
22 #include "br_proxy_server_manager.h"
23 #include "bus_center_manager.h"
24 #include "conn_log.h"
25 #include "disc_event_manager.h"
26 #include "g_enhance_conn_func.h"
27 #include "g_enhance_conn_func_pack.h"
28 #include "g_enhance_lnn_func.h"
29 #include "g_enhance_trans_func.h"
30 #include "g_enhance_disc_func.h"
31 #include "g_enhance_adapter_func.h"
32 #include "g_enhance_auth_func.h"
33 #include "instant_statistics.h"
34 #include "lnn_bus_center_ipc.h"
35 #include "lnn_init_monitor.h"
36 #include "lnn_sle_monitor.h"
37 #include "softbus_adapter_bt_common.h"
38 #include "softbus_disc_server.h"
39 #include "softbus_feature_config.h"
40 #include "legacy/softbus_hidumper_interface.h"
41 #include "legacy/softbus_hisysevt_common.h"
42 #include "softbus_utils.h"
43 #include "softbus_lnn_init.h"
44 #include "softbus_conn_init.h"
45 #include "softbus_adapter_init.h"
46 #include "softbus_authentication_init.h"
47 #include "softbus_disc_init.h"
48 #include "softbus_trans_init.h"
49 #include "softbus_ddos.h"
50 #include "softbus_init_common.h"
51 #include "trans_session_service.h"
52 #include "wifi_direct_manager.h"
53 
54 static bool g_isInit = false;
55 
ServerStubInit(void)56 int __attribute__((weak)) ServerStubInit(void)
57 {
58     COMM_LOGW(COMM_SVC, "softbus server stub init(weak function).");
59     return SOFTBUS_OK;
60 }
61 
ServerModuleDeinit(void)62 static void ServerModuleDeinit(void)
63 {
64     DiscEventManagerDeinit();
65     DiscServerDeinit();
66     ConnServerDeinit();
67     TransServerDeinit();
68     BusCenterServerDeinit();
69     AuthDeinit();
70     SoftBusTimerDeInit();
71     LooperDeinit();
72     SoftBusHiDumperDeinit();
73     DeinitSoftbusSysEvt();
74     DeinitDdos();
75     LnnDeinitSle();
76 }
77 
GetServerIsInit(void)78 bool GetServerIsInit(void)
79 {
80     return g_isInit;
81 }
82 
softbusServerOpenFuncInit(void * soHandle)83 static int32_t softbusServerOpenFuncInit(void *soHandle)
84 {
85     if (soHandle == NULL) {
86         return SOFTBUS_NETWORK_DLOPEN_FAILED;
87     }
88     if (LnnOpenFuncInit(soHandle) != SOFTBUS_OK) {
89         COMM_LOGE(COMM_SVC, "softbus Lnn Open Func init failed.");
90         return SOFTBUS_NETWORK_LNN_OPEN_FUNC_INIT_FAILED;
91     }
92     if (ConnOpenFuncInit(soHandle) != SOFTBUS_OK) {
93         COMM_LOGE(COMM_SVC, "softbus Conn Open Func init failed.");
94         return SOFTBUS_NETWORK_CONN_OPEN_FUNC_INIT_FAILED;
95     }
96     if (AdapterOpenFuncInit(soHandle) != SOFTBUS_OK) {
97         COMM_LOGE(COMM_SVC, "softbus Adapter Open Func init failed.");
98         return SOFTBUS_NETWORK_ADAPTER_OPEN_FUNC_INIT_FAILED;
99     }
100     if (AuthOpenFuncInit(soHandle) != SOFTBUS_OK) {
101         COMM_LOGE(COMM_SVC, "softbus Auth Open Func init failed.");
102         return SOFTBUS_NETWORK_AUTH_OPEN_FUNC_INIT_FAILED;
103     }
104     if (DiscOpenFuncInit(soHandle) != SOFTBUS_OK) {
105         COMM_LOGE(COMM_SVC, "softbus Disc Open Func init failed.");
106         return SOFTBUS_NETWORK_DISC_OPEN_FUNC_INIT_FAILED;
107     }
108     if (TransOpenFuncInit(soHandle) != SOFTBUS_OK) {
109         COMM_LOGE(COMM_SVC, "softbus Trans Open Func init failed.");
110         return SOFTBUS_NETWORK_TRANS_ENHANCE_FUNC_INIT_FAILED;
111     }
112     return SOFTBUS_OK;
113 }
114 
softbusServerEnhanceFuncInit(void * soHandle)115 static int32_t softbusServerEnhanceFuncInit(void *soHandle)
116 {
117     if (soHandle == NULL) {
118         return SOFTBUS_NETWORK_DLOPEN_FAILED;
119     }
120     if (LnnRegisterEnhanceFunc(soHandle) != SOFTBUS_OK) {
121         COMM_LOGE(COMM_SVC, "softbus Lnn Enhance Func init failed.");
122         return SOFTBUS_NETWORK_LNN_ENHANCE_FUNC_INIT_FAILED;
123     }
124     if (ConnRegisterEnhanceFunc(soHandle) != SOFTBUS_OK) {
125         COMM_LOGE(COMM_SVC, "softbus Conn Enhance Func init failed.");
126         return SOFTBUS_NETWORK_CONN_ENHANCE_FUNC_INIT_FAILED;
127     }
128     if (AdapterRegisterEnhanceFunc(soHandle) != SOFTBUS_OK) {
129         COMM_LOGE(COMM_SVC, "softbus Adapter Enhance Func init failed.");
130         return SOFTBUS_NETWORK_ADAPTER_ENHANCE_FUNC_INIT_FAILED;
131     }
132     if (AuthRegisterEnhanceFunc(soHandle) != SOFTBUS_OK) {
133         COMM_LOGE(COMM_SVC, "softbus Auth Enhance Func init failed.");
134         return SOFTBUS_NETWORK_AUTH_ENHANCE_FUNC_INIT_FAILED;
135     }
136     if (DiscRegisterEnhanceFunc(soHandle) != SOFTBUS_OK) {
137         COMM_LOGE(COMM_SVC, "softbus Disc Enhance Func init failed.");
138         return SOFTBUS_NETWORK_DISC_ENHANCE_FUNC_INIT_FAILED;
139     }
140     if (TransRegisterEnhanceFunc(soHandle) != SOFTBUS_OK) {
141         COMM_LOGE(COMM_SVC, "softbus Trans Enhance Func init failed.");
142         return SOFTBUS_NETWORK_TRANS_ENHANCE_FUNC_INIT_FAILED;
143     }
144     return SOFTBUS_OK;
145 }
146 
ServerFuncInit(void)147 static void ServerFuncInit(void)
148 {
149     int ret = SOFTBUS_OK;
150     void *pluginServerSoHandle = NULL;
151     (void)SoftBusDlopen(SOFTBUS_HANDLE_SERVER_PLUGIN, &pluginServerSoHandle);
152     if (pluginServerSoHandle == NULL) {
153         COMM_LOGE(COMM_SVC, "dlopen libdsoftbus_server_plugin.z.so failed.");
154         return;
155     }
156 
157     ret = softbusServerOpenFuncInit(pluginServerSoHandle);
158     if (ret != SOFTBUS_OK) {
159         COMM_LOGE(COMM_SVC, "init softbus server Open func failed");
160     }
161 
162     ret = softbusServerEnhanceFuncInit(pluginServerSoHandle);
163     if (ret != SOFTBUS_OK) {
164         COMM_LOGE(COMM_SVC, "init softbus server Enhance func failed");
165     }
166 }
167 
InitServicesAndModules(void)168 static int32_t InitServicesAndModules(void)
169 {
170     COMM_CHECK_AND_RETURN_RET_LOGE(ConnServerInit() == SOFTBUS_OK,
171         SOFTBUS_CONN_SERVER_INIT_FAILED, COMM_SVC, "softbus conn server init failed.");
172 
173     COMM_CHECK_AND_RETURN_RET_LOGE(AuthInit() == SOFTBUS_OK,
174         SOFTBUS_AUTH_INIT_FAIL, COMM_SVC, "softbus auth init failed.");
175 
176     COMM_CHECK_AND_RETURN_RET_LOGE(DiscServerInit() == SOFTBUS_OK,
177         SOFTBUS_DISC_SERVER_INIT_FAILED, COMM_SVC, "softbus disc server init failed.");
178 
179     COMM_CHECK_AND_RETURN_RET_LOGE(BusCenterServerInit() == SOFTBUS_OK,
180         SOFTBUS_CENTER_SERVER_INIT_FAILED, COMM_SVC, "softbus buscenter server init failed.");
181 
182     COMM_CHECK_AND_RETURN_RET_LOGE(TransServerInit() == SOFTBUS_OK,
183         SOFTBUS_TRANS_SERVER_INIT_FAILED, COMM_SVC, "softbus trans server init failed.");
184 
185     COMM_CHECK_AND_RETURN_RET_LOGE(DiscEventManagerInit() == SOFTBUS_OK,
186         SOFTBUS_DISCOVER_MANAGER_INIT_FAIL, COMM_SVC, "softbus disc event manager init failed.");
187 
188     COMM_CHECK_AND_RETURN_RET_LOGE(GetWifiDirectManager()->init() == SOFTBUS_OK,
189         SOFTBUS_WIFI_DIRECT_INIT_FAILED, COMM_SVC, "softbus wifi direct init failed.");
190 
191     COMM_CHECK_AND_RETURN_RET_LOGE(ConnBleDirectInitPacked() == SOFTBUS_OK,
192         SOFTBUS_CONN_BLE_DIRECT_INIT_FAILED, COMM_SVC, "softbus ble direct init failed.");
193     if (ConnPagingConnectInitPacked() != SOFTBUS_OK) {
194         COMM_LOGE(COMM_SVC, "paging connect init failed.");
195     }
196     if (InitSoftbusSysEvt() != SOFTBUS_OK || SoftBusHiDumperInit() != SOFTBUS_OK) {
197         COMM_LOGE(COMM_SVC, "softbus dfx init failed.");
198         return SOFTBUS_DFX_INIT_FAILED;
199     }
200     if (LnnInitSle() != SOFTBUS_OK) {
201         COMM_LOGE(COMM_SVC, "softbus lnn sle init failed.");
202     }
203     InstRegister(NULL);
204     return SOFTBUS_OK;
205 }
206 
InitSoftBusServer(void)207 void InitSoftBusServer(void)
208 {
209     ServerFuncInit();
210     SoftbusConfigInit();
211     LnnInitMonitorInit();
212     if (ServerStubInit() != SOFTBUS_OK) {
213         COMM_LOGE(COMM_SVC, "server stub init failed.");
214         return;
215     }
216 
217     if (SoftBusTimerInit() != SOFTBUS_OK) {
218         COMM_LOGE(COMM_SVC, "softbus timer init failed.");
219         return;
220     }
221 
222     if (LooperInit() != SOFTBUS_OK) {
223         COMM_LOGE(COMM_SVC, "softbus looper init failed.");
224         return;
225     }
226 
227     if (InitDdos() != SOFTBUS_OK) {
228         COMM_LOGE(COMM_SVC, "softbus ddos init failed.");
229     }
230 
231     int32_t ret = InitServicesAndModules();
232     if (ret != SOFTBUS_OK) {
233         ServerModuleDeinit();
234         COMM_LOGE(COMM_SVC, "softbus framework init failed, err = %{public}d", ret);
235         return;
236     }
237     ret = SoftBusBtInit();
238     if (ret != SOFTBUS_OK) {
239         LnnInitModuleReturnSet(INIT_DEPS_BLUETOOTH, ret);
240         LnnInitModuleStatusSet(INIT_DEPS_BLUETOOTH, DEPS_STATUS_FAILED);
241         ServerModuleDeinit();
242         COMM_LOGE(COMM_SVC, "softbus bt init failed, err = %{public}d", ret);
243         return;
244     }
245     LnnInitModuleStatusSet(INIT_DEPS_BLUETOOTH, DEPS_STATUS_SUCCESS);
246     LnnModuleInitMonitorCheckStart();
247     UkNegotiateSessionInit();
248     InitApplyKeyManager();
249     g_isInit = true;
250     COMM_LOGI(COMM_SVC, "softbus framework init success.");
251 }
252 
ClientDeathCallback(const char * pkgName,int32_t pid)253 void ClientDeathCallback(const char *pkgName, int32_t pid)
254 {
255     DiscServerDeathCallback(pkgName, pid);
256     TransServerDeathCallback(pkgName, pid);
257     BusCenterServerDeathCallback(pkgName);
258     AuthServerDeathCallback(pkgName, pid);
259     ConnDeathCallback(pkgName, pid);
260     BrProxyClientDeathClearResource(pid);
261 }
262