1 /*
2 * Copyright (c) 2021 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 <mutex>
19 #include "client_trans_session_manager.h"
20 #include "ipc_skeleton.h"
21 #include "iremote_broker.h"
22 #include "iremote_object.h"
23 #include "iremote_proxy.h"
24 #include "softbus_adapter_mem.h"
25 #include "softbus_adapter_timer.h"
26 #include "softbus_client_death_recipient.h"
27 #include "softbus_client_frame_manager.h"
28 #include "softbus_client_stub_interface.h"
29 #include "softbus_def.h"
30 #include "softbus_errcode.h"
31 #include "softbus_server_ipc_interface_code.h"
32 #include "softbus_log.h"
33 #include "softbus_server_proxy_standard.h"
34
35 using namespace OHOS;
36
37 namespace {
38 sptr<IRemoteObject> g_serverProxy = nullptr;
39 sptr<IRemoteObject::DeathRecipient> g_clientDeath = nullptr;
40 std::mutex g_mutex;
41 uint32_t g_waitServerInterval = 2;
42 uint32_t g_getSystemAbilityId = 2;
43 const std::u16string SAMANAGER_INTERFACE_TOKEN = u"ohos.samgr.accessToken";
44 }
45
InnerRegisterService(void)46 static int InnerRegisterService(void)
47 {
48 if (g_serverProxy == nullptr) {
49 SoftBusLog(SOFTBUS_LOG_COMM, SOFTBUS_LOG_ERROR, "g_serverProxy is nullptr!");
50 return SOFTBUS_INVALID_PARAM;
51 }
52 sptr<SoftBusServerProxyFrame> serverProxyFrame = new (std::nothrow) SoftBusServerProxyFrame(g_serverProxy);
53 if (serverProxyFrame == nullptr) {
54 SoftBusLog(SOFTBUS_LOG_COMM, SOFTBUS_LOG_ERROR, "serverProxyFrame is nullptr!");
55 return SOFTBUS_INVALID_PARAM;
56 }
57 char *clientName[SOFTBUS_PKGNAME_MAX_NUM] = {0};
58 uint32_t clientNameNum = GetSoftBusClientNameList(clientName, SOFTBUS_PKGNAME_MAX_NUM);
59 if (clientNameNum == 0) {
60 SoftBusLog(SOFTBUS_LOG_COMM, SOFTBUS_LOG_ERROR, "get client name failed");
61 return SOFTBUS_ERR;
62 }
63 for (uint32_t i = 0; i < clientNameNum; i++) {
64 while (serverProxyFrame->SoftbusRegisterService(clientName[i], nullptr) != SOFTBUS_OK) {
65 SoftBusSleepMs(WAIT_SERVER_READY_INTERVAL);
66 }
67 SoftBusFree(clientName[i]);
68 }
69 int32_t ret = ReCreateSessionServerToServer();
70 if (ret != SOFTBUS_OK) {
71 SoftBusLog(SOFTBUS_LOG_COMM, SOFTBUS_LOG_ERROR, "ReCreateSessionServerToServer failed!\n");
72 return ret;
73 }
74 SoftBusLog(SOFTBUS_LOG_COMM, SOFTBUS_LOG_INFO, "softbus server register service success!\n");
75 return SOFTBUS_OK;
76 }
77
GetSystemAbility()78 static sptr<IRemoteObject> GetSystemAbility()
79 {
80 MessageParcel data;
81
82 if (!data.WriteInterfaceToken(SAMANAGER_INTERFACE_TOKEN)) {
83 return nullptr;
84 }
85
86 data.WriteInt32(SOFTBUS_SERVER_SA_ID_INNER);
87 MessageParcel reply;
88 MessageOption option;
89 sptr<IRemoteObject> samgr = IPCSkeleton::GetContextObject();
90 int32_t err = samgr->SendRequest(g_getSystemAbilityId, data, reply, option);
91 if (err != 0) {
92 SoftBusLog(SOFTBUS_LOG_COMM, SOFTBUS_LOG_ERROR, "Get GetSystemAbility failed!\n");
93 return nullptr;
94 }
95 return reply.ReadRemoteObject();
96 }
97
ServerProxyInit(void)98 static int32_t ServerProxyInit(void)
99 {
100 std::lock_guard<std::mutex> lock(g_mutex);
101 if (g_serverProxy == nullptr) {
102 g_serverProxy = GetSystemAbility();
103 if (g_serverProxy == nullptr) {
104 SoftBusLog(SOFTBUS_LOG_COMM, SOFTBUS_LOG_ERROR, "Get remote softbus object failed!\n");
105 return SOFTBUS_ERR;
106 }
107 g_clientDeath = sptr<IRemoteObject::DeathRecipient>(new (std::nothrow) SoftBusClientDeathRecipient());
108 if (g_clientDeath == nullptr) {
109 SoftBusLog(SOFTBUS_LOG_COMM, SOFTBUS_LOG_ERROR, "DeathRecipient object is nullptr\n");
110 return SOFTBUS_ERR;
111 }
112 if (!g_serverProxy->AddDeathRecipient(g_clientDeath)) {
113 SoftBusLog(SOFTBUS_LOG_COMM, SOFTBUS_LOG_ERROR, "AddDeathRecipient failed\n");
114 return SOFTBUS_ERR;
115 }
116 }
117 return SOFTBUS_OK;
118 }
119
ClientDeathProcTask(void)120 void ClientDeathProcTask(void)
121 {
122 {
123 std::lock_guard<std::mutex> lock(g_mutex);
124 if (g_serverProxy != nullptr && g_clientDeath != nullptr) {
125 g_serverProxy->RemoveDeathRecipient(g_clientDeath);
126 }
127 g_serverProxy = nullptr;
128 }
129 ClientCleanAllSessionWhenServerDeath();
130
131 while (g_serverProxy == nullptr) {
132 SoftBusSleepMs(g_waitServerInterval);
133 ServerProxyInit();
134 if (g_serverProxy != nullptr) {
135 break;
136 }
137 }
138 InnerRegisterService();
139 }
140
ClientStubInit(void)141 int32_t ClientStubInit(void)
142 {
143 if (ServerProxyInit() != SOFTBUS_OK) {
144 SoftBusLog(SOFTBUS_LOG_COMM, SOFTBUS_LOG_ERROR, "ServerProxyInit failed\n");
145 return SOFTBUS_ERR;
146 }
147 return SOFTBUS_OK;
148 }
149
ClientRegisterService(const char * pkgName)150 int ClientRegisterService(const char *pkgName)
151 {
152 if (g_serverProxy == nullptr) {
153 SoftBusLog(SOFTBUS_LOG_COMM, SOFTBUS_LOG_ERROR, "g_serverProxy is nullptr!");
154 return SOFTBUS_INVALID_PARAM;
155 }
156 sptr<SoftBusServerProxyFrame> serverProxyFrame = new (std::nothrow) SoftBusServerProxyFrame(g_serverProxy);
157 if (serverProxyFrame == nullptr) {
158 SoftBusLog(SOFTBUS_LOG_COMM, SOFTBUS_LOG_ERROR, "serverProxyFrame is nullptr!");
159 return SOFTBUS_INVALID_PARAM;
160 }
161 while (serverProxyFrame->SoftbusRegisterService(pkgName, nullptr) != SOFTBUS_OK) {
162 SoftBusSleepMs(WAIT_SERVER_READY_INTERVAL);
163 }
164
165 SoftBusLog(SOFTBUS_LOG_COMM, SOFTBUS_LOG_INFO, "%s softbus server register service success!\n", pkgName);
166 return SOFTBUS_OK;
167 }