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