1 /*
2 * Copyright (c) 2021-2024 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_proxy_frame.h"
17
18 #include <thread>
19 #include "bus_center_server_proxy.h"
20 #include "client_bus_center_manager.h"
21 #include "general_client_connection.h"
22 #include "client_trans_socket_manager.h"
23 #include "general_connection_server_proxy.h"
24 #include "comm_log.h"
25 #include "g_enhance_sdk_func.h"
26 #include "ipc_skeleton.h"
27 #include "softbus_adapter_mem.h"
28 #include "softbus_client_death_recipient.h"
29 #include "softbus_client_frame_manager.h"
30 #include "softbus_client_stub_interface.h"
31 #include "softbus_server_ipc_interface_code.h"
32 #include "softbus_server_proxy_standard.h"
33 #include "trans_server_proxy.h"
34 #include "g_enhance_sdk_func.h"
35
36 namespace {
37 OHOS::sptr<OHOS::IRemoteObject> g_serverProxy = nullptr;
38 OHOS::sptr<OHOS::IRemoteObject> g_oldServerProxy = nullptr;
39 OHOS::sptr<OHOS::IRemoteObject::DeathRecipient> g_clientDeath = nullptr;
40 std::mutex g_mutex;
41 constexpr uint32_t WAIT_SERVER_INTERVAL = 50;
42 constexpr uint32_t SOFTBUS_MAX_RETRY_TIMES = 25;
43 uint32_t g_getSystemAbilityId = 2;
44 uint32_t g_printRequestFailedCount = 0;
45 constexpr int32_t RANDOM_RANGE_MAX = 501; // range of random numbers is (0, 500ms)
46 constexpr uint32_t PRINT_INTERVAL = 200;
47 constexpr int32_t CYCLE_NUMBER_MAX = 100;
48 const std::u16string SAMANAGER_INTERFACE_TOKEN = u"ohos.samgr.accessToken";
49 }
50
InnerRegisterService(ListNode * sessionServerInfoList)51 static int InnerRegisterService(ListNode *sessionServerInfoList)
52 {
53 srand(time(nullptr));
54 int32_t randomNum = rand();
55 int32_t scaledNum = randomNum % RANDOM_RANGE_MAX;
56
57 // Prevent high-concurrency conflicts
58 std::this_thread::sleep_for(std::chrono::milliseconds(scaledNum));
59 if (g_serverProxy == nullptr) {
60 COMM_LOGE(COMM_SDK, "g_serverProxy is nullptr!");
61 return SOFTBUS_INVALID_PARAM;
62 }
63 OHOS::sptr<OHOS::SoftBusServerProxyFrame> serverProxyFrame =
64 new (std::nothrow) OHOS::SoftBusServerProxyFrame(g_serverProxy);
65 if (serverProxyFrame == nullptr) {
66 COMM_LOGE(COMM_SDK, "serverProxyFrame is nullptr!");
67 return SOFTBUS_INVALID_PARAM;
68 }
69 char *clientName[SOFTBUS_PKGNAME_MAX_NUM] = {0};
70 uint32_t clientNameNum = GetSoftBusClientNameList(clientName, SOFTBUS_PKGNAME_MAX_NUM);
71 if (clientNameNum == 0) {
72 COMM_LOGE(COMM_SDK, "get client name failed");
73 return SOFTBUS_TRANS_GET_CLIENT_NAME_FAILED;
74 }
75 for (uint32_t i = 0; i < clientNameNum; i++) {
76 while (serverProxyFrame->SoftbusRegisterService(clientName[i], nullptr) != SOFTBUS_OK) {
77 SoftBusSleepMs(WAIT_SERVER_READY_INTERVAL);
78 }
79 SoftBusFree(clientName[i]);
80 }
81 int32_t ret = ReCreateSessionServerToServer(sessionServerInfoList);
82 if (ret != SOFTBUS_OK) {
83 COMM_LOGE(COMM_SDK, "ReCreateSessionServerToServer failed!");
84 return ret;
85 }
86 COMM_LOGD(COMM_SDK, "softbus server register service success!");
87 return SOFTBUS_OK;
88 }
89
GetSystemAbility()90 static OHOS::sptr<OHOS::IRemoteObject> GetSystemAbility()
91 {
92 OHOS::MessageParcel data;
93 if (!data.WriteInterfaceToken(SAMANAGER_INTERFACE_TOKEN)) {
94 COMM_LOGE(COMM_EVENT, "write interface token failed!");
95 return nullptr;
96 }
97
98 data.WriteInt32(SOFTBUS_SERVER_SA_ID_INNER);
99 OHOS::MessageParcel reply;
100 OHOS::MessageOption option;
101 OHOS::sptr<OHOS::IRemoteObject> samgr = OHOS::IPCSkeleton::GetContextObject();
102 if (samgr == nullptr) {
103 COMM_LOGE(COMM_EVENT, "Get samgr failed!");
104 return nullptr;
105 }
106 int32_t err = samgr->SendRequest(g_getSystemAbilityId, data, reply, option);
107 if (err != 0) {
108 if ((++g_printRequestFailedCount) % PRINT_INTERVAL == 0) {
109 COMM_LOGD(COMM_EVENT, "Get GetSystemAbility failed!");
110 }
111 return nullptr;
112 }
113 return reply.ReadRemoteObject();
114 }
115
ServerProxyInit(void)116 static int32_t ServerProxyInit(void)
117 {
118 std::lock_guard<std::mutex> lock(g_mutex);
119 if (g_serverProxy == nullptr) {
120 g_serverProxy = GetSystemAbility();
121 if (g_serverProxy == nullptr) {
122 return SOFTBUS_IPC_ERR;
123 }
124
125 if (g_serverProxy == g_oldServerProxy) {
126 g_serverProxy = nullptr;
127 COMM_LOGE(COMM_SDK, "g_serverProxy not update");
128 return SOFTBUS_IPC_ERR;
129 }
130
131 g_clientDeath =
132 OHOS::sptr<OHOS::IRemoteObject::DeathRecipient>(new (std::nothrow) OHOS::SoftBusClientDeathRecipient());
133 if (g_clientDeath == nullptr) {
134 COMM_LOGE(COMM_SDK, "DeathRecipient object is nullptr");
135 return SOFTBUS_TRANS_DEATH_RECIPIENT_INVALID;
136 }
137 if (!g_serverProxy->AddDeathRecipient(g_clientDeath)) {
138 COMM_LOGE(COMM_SDK, "AddDeathRecipient failed");
139 return SOFTBUS_TRANS_ADD_DEATH_RECIPIENT_FAILED;
140 }
141 }
142 return SOFTBUS_OK;
143 }
144
145 static RestartEventCallback g_restartAuthParaCallback = nullptr;
146
RestartAuthParaNotify(void)147 static void RestartAuthParaNotify(void)
148 {
149 if (g_restartAuthParaCallback == nullptr) {
150 COMM_LOGI(COMM_SDK, "Restart AuthPara notify is not used");
151 return;
152 }
153 if (g_restartAuthParaCallback() != SOFTBUS_OK) {
154 RestartAuthParaCallbackUnregister();
155 COMM_LOGE(COMM_SDK, "Restart AuthPara notify failed!");
156 return;
157 }
158 COMM_LOGI(COMM_SDK, "Restart AuthPara notify success!");
159 }
160
DiscRecoveryPolicyPacked(void)161 static int32_t DiscRecoveryPolicyPacked(void)
162 {
163 ClientEnhanceFuncList *pfnClientEnhanceFuncList = ClientEnhanceFuncListGet();
164 ClientRegisterEnhanceFuncCheck((void *)pfnClientEnhanceFuncList->discRecoveryPolicy);
165 if (pfnClientEnhanceFuncList->discRecoveryPolicy == nullptr) {
166 COMM_LOGE(COMM_SDK, "discRecoveryPolicy func not register");
167 return SOFTBUS_OK;
168 }
169 return pfnClientEnhanceFuncList->discRecoveryPolicy();
170 }
171
ClientDeathProcTask(void)172 void ClientDeathProcTask(void)
173 {
174 {
175 std::lock_guard<std::mutex> lock(g_mutex);
176 g_oldServerProxy = g_serverProxy;
177 if (g_serverProxy != nullptr && g_clientDeath != nullptr) {
178 g_serverProxy->RemoveDeathRecipient(g_clientDeath);
179 }
180 g_serverProxy.clear();
181 }
182 TransServerProxyClear();
183 BusCenterServerProxyDeInit();
184 ConnectionServerProxyDeInit();
185 ConnectionDeathNotify();
186
187 ListNode sessionServerInfoList;
188 ListInit(&sessionServerInfoList);
189 ClientCleanAllSessionWhenServerDeath(&sessionServerInfoList);
190
191 int32_t cnt = 0;
192 for (cnt = 0; cnt < CYCLE_NUMBER_MAX; cnt++) {
193 if (ServerProxyInit() == SOFTBUS_OK) {
194 break;
195 }
196 SoftBusSleepMs(WAIT_SERVER_INTERVAL);
197 }
198 if (cnt == CYCLE_NUMBER_MAX) {
199 COMM_LOGE(COMM_SDK, "server proxy init reached the maximum count=%{public}d", cnt);
200 return;
201 }
202 TransServerProxyInit();
203 BusCenterServerProxyInit();
204 ConnectionServerProxyInit();
205 InnerRegisterService(&sessionServerInfoList);
206 RestartAuthParaNotify();
207 DiscRecoveryPublish();
208 DiscRecoverySubscribe();
209 DiscRecoveryPolicyPacked();
210 RestartRegDataLevelChange();
211 }
212
RestartAuthParaCallbackUnregister(void)213 void RestartAuthParaCallbackUnregister(void)
214 {
215 g_restartAuthParaCallback = nullptr;
216 }
217
218 extern "C" {
RestartAuthParaCallbackRegister(RestartEventCallback callback)219 int32_t RestartAuthParaCallbackRegister(RestartEventCallback callback)
220 {
221 if (callback == nullptr) {
222 COMM_LOGE(COMM_SDK, "Restart OpenAuthSessionWithPara callback register param is invalid!");
223 return SOFTBUS_INVALID_PARAM;
224 }
225 g_restartAuthParaCallback = callback;
226 COMM_LOGI(COMM_SDK, "Restart event callback register success!");
227 return SOFTBUS_OK;
228 }
229 }
230
ClientStubInit(void)231 int32_t ClientStubInit(void)
232 {
233 if (ServerProxyInit() != SOFTBUS_OK) {
234 COMM_LOGE(COMM_SDK, "ServerProxyInit failed");
235 return SOFTBUS_NO_INIT;
236 }
237 return SOFTBUS_OK;
238 }
239
ClientRegisterService(const char * pkgName)240 int ClientRegisterService(const char *pkgName)
241 {
242 if (g_serverProxy == nullptr) {
243 COMM_LOGE(COMM_SDK, "g_serverProxy is nullptr!");
244 return SOFTBUS_INVALID_PARAM;
245 }
246 OHOS::sptr<OHOS::SoftBusServerProxyFrame> serverProxyFrame =
247 new (std::nothrow) OHOS::SoftBusServerProxyFrame(g_serverProxy);
248 if (serverProxyFrame == nullptr) {
249 COMM_LOGE(COMM_SDK, "serverProxyFrame is nullptr!");
250 return SOFTBUS_INVALID_PARAM;
251 }
252 uint32_t sleepCnt = 0;
253 while (serverProxyFrame->SoftbusRegisterService(pkgName, nullptr) != SOFTBUS_OK) {
254 SoftBusSleepMs(WAIT_SERVER_READY_INTERVAL);
255 sleepCnt++;
256 if (sleepCnt >= SOFTBUS_MAX_RETRY_TIMES) {
257 return SOFTBUS_SERVER_NOT_INIT;
258 }
259 }
260
261 COMM_LOGD(COMM_SDK, "softbus server register service success! pkgName=%{public}s", pkgName);
262 return SOFTBUS_OK;
263 }
264
ClientRegisterBrProxyService(const char * pkgName)265 int32_t ClientRegisterBrProxyService(const char *pkgName)
266 {
267 if (g_serverProxy == nullptr) {
268 COMM_LOGE(COMM_SDK, "g_serverProxy is nullptr!");
269 return SOFTBUS_INVALID_PARAM;
270 }
271 OHOS::sptr<OHOS::SoftBusServerProxyFrame> serverProxyFrame =
272 new (std::nothrow) OHOS::SoftBusServerProxyFrame(g_serverProxy);
273 if (serverProxyFrame == nullptr) {
274 COMM_LOGE(COMM_SDK, "serverProxyFrame is nullptr!");
275 return SOFTBUS_INVALID_PARAM;
276 }
277 uint32_t sleepCnt = 0;
278 int32_t ret = SOFTBUS_SERVER_NOT_INIT;
279 while (ret != SOFTBUS_OK) {
280 ret = serverProxyFrame->RegisterBrProxyService(pkgName, nullptr);
281 SoftBusSleepMs(WAIT_SERVER_READY_INTERVAL);
282 sleepCnt++;
283 if (sleepCnt >= SOFTBUS_MAX_RETRY_TIMES) {
284 return ret;
285 }
286 }
287
288 COMM_LOGD(COMM_SDK, "softbus server register service success! pkgName=%{public}s", pkgName);
289 return SOFTBUS_OK;
290 }