• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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_error_code.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, pid);
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 
139     bool value = ReadRemoteObject(req, &svc);
140     if (!value) {
141         COMM_LOGE(COMM_SVC, "softbus read remote obj failed!");
142         goto EXIT;
143     }
144 
145     svcId.handle = svc.handle;
146     svcId.token = svc.token;
147     svcId.cookie = svc.cookie;
148 
149     char *pkgName = (char *)SoftBusMalloc(len + 1);
150     if (pkgName == NULL) {
151         COMM_LOGE(COMM_SVC, "softbus pkgName malloc failed!");
152         goto EXIT;
153     }
154     if (strcpy_s(pkgName, len + 1, name) != EOK) {
155         COMM_LOGE(COMM_SVC, "softbus strcpy_s failed!");
156         SoftBusFree(pkgName);
157         goto EXIT;
158     }
159 
160     DeathCbArg *argStrcut = (DeathCbArg*)SoftBusMalloc(sizeof(DeathCbArg));
161     if (argStrcut == NULL) {
162         COMM_LOGE(COMM_SVC, "softbus argStrcut malloc failed!");
163         SoftBusFree(pkgName);
164         goto EXIT;
165     }
166     argStrcut->pkgName = pkgName;
167     argStrcut->pid = GetCallingPid();
168 
169     uint32_t cbId = 0;
170     AddDeathRecipient(svc, ClientDeathCb, argStrcut, &cbId);
171     svcId.cbId = cbId;
172     ret = SERVER_RegisterService(name, &svcId);
173 EXIT:
174     WriteInt32(reply, ret);
175     return SOFTBUS_OK;
176 }
177 
178 typedef struct {
179     enum SoftBusFuncId id;
180     int32_t (*func)(IpcIo *req, IpcIo *reply);
181 } ServerInvokeCmd;
182 
183 const ServerInvokeCmd g_serverInvokeCmdTbl[] = {
184     { MANAGE_REGISTER_SERVICE, ServerRegisterService },
185     { SERVER_JOIN_LNN, ServerJoinLNN },
186     { SERVER_JOIN_METANODE, ServerJoinMetaNode },
187     { SERVER_LEAVE_LNN, ServerLeaveLNN },
188     { SERVER_LEAVE_METANODE, ServerLeaveMetaNode },
189     { SERVER_GET_ALL_ONLINE_NODE_INFO, ServerGetAllOnlineNodeInfo },
190     { SERVER_GET_LOCAL_DEVICE_INFO, ServerGetLocalDeviceInfo },
191     { SERVER_GET_NODE_KEY_INFO, ServerGetNodeKeyInfo },
192     { SERVER_START_TIME_SYNC, ServerStartTimeSync },
193     { SERVER_STOP_TIME_SYNC, ServerStopTimeSync },
194     { SERVER_PUBLISH_LNN, ServerPublishLNN },
195     { SERVER_STOP_PUBLISH_LNN, ServerStopPublishLNN },
196     { SERVER_REFRESH_LNN, ServerRefreshLNN },
197     { SERVER_STOP_REFRESH_LNN, ServerStopRefreshLNN },
198     { SERVER_ACTIVE_META_NODE, ServerActiveMetaNode},
199     { SERVER_DEACTIVE_META_NODE, ServerDeactiveMetaNode },
200     { SERVER_GET_ALL_META_NODE_INFO, ServerGetAllMetaNodeInfo },
201     { SERVER_SHIFT_LNN_GEAR, ServerShiftLnnGear },
202     { SERVER_TRIGGER_RANGE_FOR_MSDP, ServerTriggerRangeForMsdp },
203     { SERVER_REG_RANGE_CB_FOR_MSDP, ServerRegRangeCbForMsdp },
204     { SERVER_UNREG_RANGE_CB_FOR_MSDP, ServerUnregRangeCbForMsdp },
205     { SERVER_CREATE_SESSION_SERVER, ServerCreateSessionServer },
206     { SERVER_REMOVE_SESSION_SERVER, ServerRemoveSessionServer },
207     { SERVER_OPEN_SESSION, ServerOpenSession },
208     { SERVER_OPEN_AUTH_SESSION, ServerOpenAuthSession},
209     { SERVER_NOTIFY_AUTH_SUCCESS, ServerNotifyAuthSuccess},
210     { SERVER_CLOSE_CHANNEL, ServerCloseChannel },
211     { SERVER_SESSION_SENDMSG, ServerSendSessionMsg },
212     { SERVER_SET_NODE_DATA_CHANGE_FLAG, ServerSetNodeDataChangeFlag },
213     { SERVER_REG_DATA_LEVEL_CHANGE_CB, ServerRegDataLevelChangeCb },
214     { SERVER_UNREG_DATA_LEVEL_CHANGE_CB, ServerUnregDataLevelChangeCb },
215     { SERVER_SET_DATA_LEVEL, ServerSetDataLevel },
216     { SERVER_RELEASE_RESOURCES, ServerReleaseResources },
217     { SERVER_PROCESS_INNER_EVENT, ServerProcessInnerEvent },
218     { SERVER_PRIVILEGE_CLOSE_CHANNEL, ServerPrivilegeCloseChannel },
219 };
220 
Invoke(IServerProxy * iProxy,int funcId,void * origin,IpcIo * req,IpcIo * reply)221 static int32_t Invoke(IServerProxy *iProxy, int funcId, void *origin, IpcIo *req, IpcIo *reply)
222 {
223     COMM_LOGI(COMM_SVC, "RECEIVE FUNCID. funcId=%{public}d", funcId);
224     if (GetServerIsInit() == false) {
225         COMM_LOGE(COMM_SVC, "server not init");
226         WriteInt32(reply, SOFTBUS_SERVER_NOT_INIT);
227         return SOFTBUS_ERR;
228     }
229     int tblSize = sizeof(g_serverInvokeCmdTbl) / sizeof(ServerInvokeCmd);
230     for (int i = 0; i < tblSize; i++) {
231         if (funcId == g_serverInvokeCmdTbl[i].id) {
232             return g_serverInvokeCmdTbl[i].func(req, reply);
233         }
234     }
235     COMM_LOGE(COMM_SVC, "not support func. funcId=%{public}d", funcId);
236     return SOFTBUS_ERR;
237 }
238 
239 static SoftbusSamgrService g_samgrService = {
240     .GetName = GetName,
241     .Initialize = Initialize,
242     .MessageHandle = MessageHandle,
243     .GetTaskConfig = GetTaskConfig,
244     SERVER_IPROXY_IMPL_BEGIN,
245     .Invoke = Invoke,
246     IPROXY_END,
247 };
248 
HOS_SystemInit(void)249 void __attribute__((weak)) HOS_SystemInit(void)
250 {
251     SAMGR_Bootstrap();
252     return;
253 }
254 
ServerStubInit(void)255 int ServerStubInit(void)
256 {
257     HOS_SystemInit();
258 
259     if (LnnIpcInit() != SOFTBUS_OK) {
260         COMM_LOGE(COMM_SVC, "Center Ipc init failed.");
261         return SOFTBUS_ERR;
262     }
263 
264     if (SERVER_InitClient() != SOFTBUS_OK) {
265         COMM_LOGE(COMM_SVC, "client manager init failed.");
266         LnnIpcDeinit();
267         return SOFTBUS_ERR;
268     }
269     return SOFTBUS_OK;
270 }
271 
Init(void)272 static void Init(void)
273 {
274     sleep(WAIT_FOR_SERVER);
275     SAMGR_GetInstance()->RegisterService((Service *)&g_samgrService);
276     SAMGR_GetInstance()->RegisterDefaultFeatureApi(SOFTBUS_SERVICE, GET_IUNKNOWN(g_samgrService));
277     COMM_LOGI(COMM_SVC, "Init success SOFTBUS_SERVICE=%{public}s", SOFTBUS_SERVICE);
278 }
279 SYSEX_SERVICE_INIT(Init);