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