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 <ohos_init.h>
17
18 #include "bus_center_server_stub.h"
19 #include "comm_log.h"
20 #include "ipc_skeleton.h"
21 #include "iproxy_server.h"
22 #include "lnn_bus_center_ipc.h"
23 #include "samgr_lite.h"
24 #include "securec.h"
25 #include "softbus_adapter_mem.h"
26 #include "softbus_client_info_manager.h"
27 #include "softbus_disc_server.h"
28 #include "softbus_def.h"
29 #include "softbus_errcode.h"
30 #include "softbus_server_ipc_interface_code.h"
31 #include "softbus_permission.h"
32 #include "softbus_server_frame.h"
33 #include "trans_server_stub.h"
34 #include "trans_session_service.h"
35
36 #define STACK_SIZE 0x800
37 #define QUEUE_SIZE 20
38 #define WAIT_FOR_SERVER 2
39 typedef struct {
40 INHERIT_SERVER_IPROXY;
41 } DefaultFeatureApi;
42
43 typedef struct {
44 INHERIT_SERVICE;
45 INHERIT_IUNKNOWNENTRY(DefaultFeatureApi);
46 Identity identity;
47 } SoftbusSamgrService;
48
GetName(Service * service)49 static const char *GetName(Service *service)
50 {
51 (void)service;
52 return SOFTBUS_SERVICE;
53 }
54
Initialize(Service * service,Identity identity)55 static BOOL Initialize(Service *service, Identity identity)
56 {
57 if (service == NULL) {
58 COMM_LOGE(COMM_SVC, "invalid param");
59 return FALSE;
60 }
61
62 SoftbusSamgrService *samgrService = (SoftbusSamgrService *)service;
63 samgrService->identity = identity;
64 return TRUE;
65 }
66
MessageHandle(Service * service,Request * msg)67 static BOOL MessageHandle(Service *service, Request *msg)
68 {
69 if (service == NULL || msg == NULL) {
70 COMM_LOGE(COMM_SVC, "invalid param");
71 return TRUE;
72 }
73 return FALSE;
74 }
75
GetTaskConfig(Service * service)76 static TaskConfig GetTaskConfig(Service *service)
77 {
78 (void)service;
79 TaskConfig config = { LEVEL_HIGH, PRI_BELOW_NORMAL, STACK_SIZE, QUEUE_SIZE, SHARED_TASK };
80 return config;
81 }
82
ComponentDeathCallback(const char * pkgName,int32_t pid)83 static void ComponentDeathCallback(const char *pkgName, int32_t pid)
84 {
85 DiscServerDeathCallback(pkgName);
86 TransServerDeathCallback(pkgName, pid);
87 BusCenterServerDeathCallback(pkgName);
88 }
89
90 typedef struct DeathCbArg {
91 char *pkgName;
92 int32_t pid;
93 } DeathCbArg;
94
ClientDeathCb(void * arg)95 static void ClientDeathCb(void *arg)
96 {
97 if (arg == NULL) {
98 COMM_LOGE(COMM_SVC, "arg is NULL.");
99 return;
100 }
101 DeathCbArg* argStrcut = (DeathCbArg*)arg;
102 struct CommonScvId svcId = {0};
103 if (SERVER_GetIdentityByPkgName((const char *)argStrcut->pkgName, &svcId) != SOFTBUS_OK) {
104 COMM_LOGE(COMM_SVC, "not found client by package name.");
105 SoftBusFree(argStrcut->pkgName);
106 SoftBusFree(argStrcut);
107 return;
108 }
109 SERVER_UnregisterService((const char *)argStrcut->pkgName);
110 ComponentDeathCallback((const char *)argStrcut->pkgName, argStrcut->pid);
111 SoftBusFree(argStrcut->pkgName);
112 SoftBusFree(argStrcut);
113 SvcIdentity sid = {0};
114 sid.handle = (int32_t)svcId.handle;
115 sid.token = (uintptr_t)svcId.token;
116 sid.cookie = (uintptr_t)svcId.cookie;
117 ReleaseSvc(sid);
118 }
119
ServerRegisterService(IpcIo * req,IpcIo * reply)120 static int32_t ServerRegisterService(IpcIo *req, IpcIo *reply)
121 {
122 COMM_LOGI(COMM_SVC, "register service ipc server pop.");
123 size_t len = 0;
124 int ret = SOFTBUS_ERR;
125 struct CommonScvId svcId = {0};
126
127 const char *name = (const char*)ReadString(req, &len);
128 SvcIdentity svc;
129 if ((name == NULL) || (len == 0)) {
130 COMM_LOGE(COMM_SVC, "ServerRegisterService read name or len fail");
131 goto EXIT;
132 }
133 int32_t callingUid = GetCallingUid();
134 if (!CheckBusCenterPermission(callingUid, name)) {
135 COMM_LOGE(COMM_SVC, "ServerRegisterService no permission.");
136 goto EXIT;
137 }
138 bool value = ReadRemoteObject(req, &svc);
139
140 svcId.handle = svc.handle;
141 svcId.token = svc.token;
142 svcId.cookie = svc.cookie;
143
144 char *pkgName = (char *)SoftBusMalloc(len + 1);
145 if (pkgName == NULL) {
146 COMM_LOGE(COMM_SVC, "softbus pkgName malloc failed!");
147 goto EXIT;
148 }
149 if (strcpy_s(pkgName, len + 1, name) != EOK) {
150 COMM_LOGE(COMM_SVC, "softbus strcpy_s failed!");
151 SoftBusFree(pkgName);
152 goto EXIT;
153 }
154
155 DeathCbArg *argStrcut = (DeathCbArg*)SoftBusMalloc(sizeof(DeathCbArg));
156 if (argStrcut == NULL) {
157 COMM_LOGE(COMM_SVC, "softbus argStrcut malloc failed!");
158 SoftBusFree(pkgName);
159 goto EXIT;
160 }
161 argStrcut->pkgName = pkgName;
162 argStrcut->pid = GetCallingPid();
163
164 uint32_t cbId = 0;
165 AddDeathRecipient(svc, ClientDeathCb, argStrcut, &cbId);
166 svcId.cbId = cbId;
167 ret = SERVER_RegisterService(name, &svcId);
168 EXIT:
169 WriteInt32(reply, ret);
170 return SOFTBUS_OK;
171 }
172
173 typedef struct {
174 enum SoftBusFuncId id;
175 int32_t (*func)(IpcIo *req, IpcIo *reply);
176 } ServerInvokeCmd;
177
178 const ServerInvokeCmd g_serverInvokeCmdTbl[] = {
179 { MANAGE_REGISTER_SERVICE, ServerRegisterService },
180 { SERVER_JOIN_LNN, ServerJoinLNN },
181 { SERVER_JOIN_METANODE, ServerJoinMetaNode },
182 { SERVER_LEAVE_LNN, ServerLeaveLNN },
183 { SERVER_LEAVE_METANODE, ServerLeaveMetaNode },
184 { SERVER_GET_ALL_ONLINE_NODE_INFO, ServerGetAllOnlineNodeInfo },
185 { SERVER_GET_LOCAL_DEVICE_INFO, ServerGetLocalDeviceInfo },
186 { SERVER_GET_NODE_KEY_INFO, ServerGetNodeKeyInfo },
187 { SERVER_START_TIME_SYNC, ServerStartTimeSync },
188 { SERVER_STOP_TIME_SYNC, ServerStopTimeSync },
189 { SERVER_PUBLISH_LNN, ServerPublishLNN },
190 { SERVER_STOP_PUBLISH_LNN, ServerStopPublishLNN },
191 { SERVER_REFRESH_LNN, ServerRefreshLNN },
192 { SERVER_STOP_REFRESH_LNN, ServerStopRefreshLNN },
193 { SERVER_ACTIVE_META_NODE, ServerActiveMetaNode},
194 { SERVER_DEACTIVE_META_NODE, ServerDeactiveMetaNode },
195 { SERVER_GET_ALL_META_NODE_INFO, ServerGetAllMetaNodeInfo },
196 { SERVER_SHIFT_LNN_GEAR, ServerShiftLnnGear },
197 { SERVER_CREATE_SESSION_SERVER, ServerCreateSessionServer },
198 { SERVER_REMOVE_SESSION_SERVER, ServerRemoveSessionServer },
199 { SERVER_OPEN_SESSION, ServerOpenSession },
200 { SERVER_OPEN_AUTH_SESSION, ServerOpenAuthSession},
201 { SERVER_NOTIFY_AUTH_SUCCESS, ServerNotifyAuthSuccess},
202 { SERVER_CLOSE_CHANNEL, ServerCloseChannel },
203 { SERVER_SESSION_SENDMSG, ServerSendSessionMsg },
204 { SERVER_SET_NODE_DATA_CHANGE_FLAG, ServerSetNodeDataChangeFlag },
205 { SERVER_REG_DATA_LEVEL_CHANGE_CB, ServerRegDataLevelChangeCb },
206 { SERVER_UNREG_DATA_LEVEL_CHANGE_CB, ServerUnregDataLevelChangeCb },
207 { SERVER_SET_DATA_LEVEL, ServerSetDataLevel },
208 { SERVER_RELEASE_RESOURCES, ServerReleaseResources },
209 };
210
Invoke(IServerProxy * iProxy,int funcId,void * origin,IpcIo * req,IpcIo * reply)211 static int32_t Invoke(IServerProxy *iProxy, int funcId, void *origin, IpcIo *req, IpcIo *reply)
212 {
213 COMM_LOGI(COMM_SVC, "RECEIVE FUNCID. funcId=%{public}d", funcId);
214 if (GetServerIsInit() == false) {
215 COMM_LOGE(COMM_SVC, "server not init");
216 WriteInt32(reply, SOFTBUS_SERVER_NOT_INIT);
217 return SOFTBUS_ERR;
218 }
219 int tblSize = sizeof(g_serverInvokeCmdTbl) / sizeof(ServerInvokeCmd);
220 for (int i = 0; i < tblSize; i++) {
221 if (funcId == g_serverInvokeCmdTbl[i].id) {
222 return g_serverInvokeCmdTbl[i].func(req, reply);
223 }
224 }
225 COMM_LOGE(COMM_SVC, "not support func. funcId=%{public}d", funcId);
226 return SOFTBUS_ERR;
227 }
228
229 static SoftbusSamgrService g_samgrService = {
230 .GetName = GetName,
231 .Initialize = Initialize,
232 .MessageHandle = MessageHandle,
233 .GetTaskConfig = GetTaskConfig,
234 SERVER_IPROXY_IMPL_BEGIN,
235 .Invoke = Invoke,
236 IPROXY_END,
237 };
238
HOS_SystemInit(void)239 void __attribute__((weak)) HOS_SystemInit(void)
240 {
241 SAMGR_Bootstrap();
242 return;
243 }
244
ServerStubInit(void)245 int ServerStubInit(void)
246 {
247 HOS_SystemInit();
248
249 if (LnnIpcInit() != SOFTBUS_OK) {
250 COMM_LOGE(COMM_SVC, "Center Ipc init failed.");
251 return SOFTBUS_ERR;
252 }
253
254 if (SERVER_InitClient() != SOFTBUS_OK) {
255 COMM_LOGE(COMM_SVC, "client manager init failed.");
256 LnnIpcDeinit();
257 return SOFTBUS_ERR;
258 }
259 return SOFTBUS_OK;
260 }
261
Init(void)262 static void Init(void)
263 {
264 sleep(WAIT_FOR_SERVER);
265 SAMGR_GetInstance()->RegisterService((Service *)&g_samgrService);
266 SAMGR_GetInstance()->RegisterDefaultFeatureApi(SOFTBUS_SERVICE, GET_IUNKNOWN(g_samgrService));
267 COMM_LOGI(COMM_SVC, "Init success SOFTBUS_SERVICE=%{public}s", SOFTBUS_SERVICE);
268 }
269 SYSEX_SERVICE_INIT(Init);