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 "bus_center_server_proxy.h"
26 #include "comm_log.h"
27 #include "disc_server_proxy.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
43 namespace {
44 OHOS::sptr<OHOS::IRemoteObject> g_serverProxy = nullptr;
45 OHOS::sptr<OHOS::IRemoteObject::DeathRecipient> g_clientDeath = nullptr;
46 std::mutex g_mutex;
47 uint32_t g_waitServerInterval = 2;
48 uint32_t g_getSystemAbilityId = 2;
49 uint32_t g_printRequestFailedCount = 0;
50 int32_t g_randomMax = 501; // range of random numbers is (0, 500ms)
51 constexpr uint32_t g_printInterval = 200;
52 const std::u16string SAMANAGER_INTERFACE_TOKEN = u"ohos.samgr.accessToken";
53 }
54
InnerRegisterService(void)55 static int InnerRegisterService(void)
56 {
57 srand(time(nullptr));
58 int32_t randomNum = rand();
59 int32_t scaledNum = randomNum % g_randomMax;
60
61 // Prevent high-concurrency conflicts
62 std::this_thread::sleep_for(std::chrono::milliseconds(scaledNum));
63 if (g_serverProxy == nullptr) {
64 COMM_LOGE(COMM_SDK, "g_serverProxy is nullptr!");
65 return SOFTBUS_INVALID_PARAM;
66 }
67 OHOS::sptr<OHOS::SoftBusServerProxyFrame> serverProxyFrame =
68 new (std::nothrow) OHOS::SoftBusServerProxyFrame(g_serverProxy);
69 if (serverProxyFrame == nullptr) {
70 COMM_LOGE(COMM_SDK, "serverProxyFrame is nullptr!");
71 return SOFTBUS_INVALID_PARAM;
72 }
73 char *clientName[SOFTBUS_PKGNAME_MAX_NUM] = {0};
74 uint32_t clientNameNum = GetSoftBusClientNameList(clientName, SOFTBUS_PKGNAME_MAX_NUM);
75 if (clientNameNum == 0) {
76 COMM_LOGE(COMM_SDK, "get client name failed");
77 return SOFTBUS_ERR;
78 }
79 for (uint32_t i = 0; i < clientNameNum; i++) {
80 while (serverProxyFrame->SoftbusRegisterService(clientName[i], nullptr) != SOFTBUS_OK) {
81 SoftBusSleepMs(WAIT_SERVER_READY_INTERVAL);
82 }
83 SoftBusFree(clientName[i]);
84 }
85 int32_t ret = ReCreateSessionServerToServer();
86 if (ret != SOFTBUS_OK) {
87 COMM_LOGE(COMM_SDK, "ReCreateSessionServerToServer failed!\n");
88 return ret;
89 }
90 COMM_LOGI(COMM_SDK, "softbus server register service success!\n");
91 return SOFTBUS_OK;
92 }
93
GetSystemAbility()94 static OHOS::sptr<OHOS::IRemoteObject> GetSystemAbility()
95 {
96 OHOS::MessageParcel data;
97 if (!data.WriteInterfaceToken(SAMANAGER_INTERFACE_TOKEN)) {
98 COMM_LOGE(COMM_EVENT, "write interface token failed!");
99 return nullptr;
100 }
101
102 data.WriteInt32(SOFTBUS_SERVER_SA_ID_INNER);
103 OHOS::MessageParcel reply;
104 OHOS::MessageOption option;
105 OHOS::sptr<OHOS::IRemoteObject> samgr = OHOS::IPCSkeleton::GetContextObject();
106 if (samgr == nullptr) {
107 COMM_LOGE(COMM_EVENT, "Get samgr failed!");
108 return nullptr;
109 }
110 int32_t err = samgr->SendRequest(g_getSystemAbilityId, data, reply, option);
111 if (err != 0) {
112 if ((++g_printRequestFailedCount) % g_printInterval == 0) {
113 COMM_LOGD(COMM_EVENT, "Get GetSystemAbility failed!");
114 }
115 return nullptr;
116 }
117 return reply.ReadRemoteObject();
118 }
119
ServerProxyInit(void)120 static int32_t ServerProxyInit(void)
121 {
122 std::lock_guard<std::mutex> lock(g_mutex);
123 if (g_serverProxy == nullptr) {
124 g_serverProxy = GetSystemAbility();
125 if (g_serverProxy == nullptr) {
126 return SOFTBUS_ERR;
127 }
128 g_clientDeath =
129 OHOS::sptr<OHOS::IRemoteObject::DeathRecipient>(new (std::nothrow) OHOS::SoftBusClientDeathRecipient());
130 if (g_clientDeath == nullptr) {
131 COMM_LOGE(COMM_SDK, "DeathRecipient object is nullptr\n");
132 return SOFTBUS_ERR;
133 }
134 if (!g_serverProxy->AddDeathRecipient(g_clientDeath)) {
135 COMM_LOGE(COMM_SDK, "AddDeathRecipient failed\n");
136 return SOFTBUS_ERR;
137 }
138 }
139 return SOFTBUS_OK;
140 }
141
ClientDeathProcTask(void)142 void ClientDeathProcTask(void)
143 {
144 {
145 std::lock_guard<std::mutex> lock(g_mutex);
146 if (g_serverProxy != nullptr && g_clientDeath != nullptr) {
147 g_serverProxy->RemoveDeathRecipient(g_clientDeath);
148 }
149 g_serverProxy.clear();
150 }
151 DiscServerProxyDeInit();
152 TransServerProxyDeInit();
153 BusCenterServerProxyDeInit();
154
155 ClientCleanAllSessionWhenServerDeath();
156
157 while (true) {
158 if (ServerProxyInit() == SOFTBUS_OK) {
159 break;
160 }
161 SoftBusSleepMs(g_waitServerInterval);
162 }
163 DiscServerProxyInit();
164 TransServerProxyInit();
165 BusCenterServerProxyInit();
166 InnerRegisterService();
167 TransBroadCastReInit();
168 DiscRecoveryPublish();
169 DiscRecoverySubscribe();
170 }
171
ClientStubInit(void)172 int32_t ClientStubInit(void)
173 {
174 if (ServerProxyInit() != SOFTBUS_OK) {
175 COMM_LOGE(COMM_SDK, "ServerProxyInit failed\n");
176 return SOFTBUS_ERR;
177 }
178 return SOFTBUS_OK;
179 }
180
ClientRegisterService(const char * pkgName)181 int ClientRegisterService(const char *pkgName)
182 {
183 if (g_serverProxy == nullptr) {
184 COMM_LOGE(COMM_SDK, "g_serverProxy is nullptr!");
185 return SOFTBUS_INVALID_PARAM;
186 }
187 OHOS::sptr<OHOS::SoftBusServerProxyFrame> serverProxyFrame =
188 new (std::nothrow) OHOS::SoftBusServerProxyFrame(g_serverProxy);
189 if (serverProxyFrame == nullptr) {
190 COMM_LOGE(COMM_SDK, "serverProxyFrame is nullptr!");
191 return SOFTBUS_INVALID_PARAM;
192 }
193 while (serverProxyFrame->SoftbusRegisterService(pkgName, nullptr) != SOFTBUS_OK) {
194 SoftBusSleepMs(WAIT_SERVER_READY_INTERVAL);
195 }
196
197 COMM_LOGI(COMM_SDK, "softbus server register service success! pkgName=%{public}s\n", pkgName);
198 return SOFTBUS_OK;
199 }
200