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 <ohos_init.h>
17
18 #include "bus_center_server_stub.h"
19 #include "disc_server_stub.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_errcode.h"
29 #include "softbus_ipc_def.h"
30 #include "softbus_log.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 SoftBusLog(SOFTBUS_LOG_COMM, SOFTBUS_LOG_ERROR, "invalid param");
59 return TRUE;
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 SoftBusLog(SOFTBUS_LOG_COMM, SOFTBUS_LOG_ERROR, "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)83 static void ComponentDeathCallback(const char *pkgName)
84 {
85 DiscServerDeathCallback(pkgName);
86 TransServerDeathCallback(pkgName);
87 BusCenterServerDeathCallback(pkgName);
88 }
89
ClientDeathCb(void * arg)90 static void ClientDeathCb(void *arg)
91 {
92 if (arg == NULL) {
93 SoftBusLog(SOFTBUS_LOG_COMM, SOFTBUS_LOG_ERROR, "package name is NULL.");
94 return;
95 }
96 struct CommonScvId svcId = {0};
97 if (SERVER_GetIdentityByPkgName((const char *)arg, &svcId) != SOFTBUS_OK) {
98 SoftBusLog(SOFTBUS_LOG_COMM, SOFTBUS_LOG_ERROR, "not found client by package name.");
99 SoftBusFree(arg);
100 arg = NULL;
101 return;
102 }
103 SERVER_UnregisterService((const char *)arg);
104 ComponentDeathCallback((const char *)arg);
105 SoftBusFree(arg);
106 arg = NULL;
107 SvcIdentity sid = {0};
108 sid.handle = svcId.handle;
109 sid.token = svcId.token;
110 sid.cookie = svcId.cookie;
111 ReleaseSvc(sid);
112 }
113
ServerRegisterService(IpcIo * req,IpcIo * reply)114 static int32_t ServerRegisterService(IpcIo *req, IpcIo *reply)
115 {
116 SoftBusLog(SOFTBUS_LOG_COMM, SOFTBUS_LOG_INFO, "register service ipc server pop.");
117 size_t len = 0;
118 int ret = SOFTBUS_ERR;
119 struct CommonScvId svcId = {0};
120
121 const char *name = (const char*)ReadString(req, &len);
122 SvcIdentity svc;
123 bool value = ReadRemoteObject(req, &svc);
124 if (!value || name == NULL || len == 0) {
125 SoftBusLog(SOFTBUS_LOG_COMM, SOFTBUS_LOG_ERROR, "get data fail");
126 goto EXIT;
127 }
128 int32_t callingUid = GetCallingUid();
129 if (!CheckBusCenterPermission(callingUid, name)) {
130 SoftBusLog(SOFTBUS_LOG_COMM, SOFTBUS_LOG_ERROR, "ServerRegisterService no permission.");
131 goto EXIT;
132 }
133 svcId.handle = svc.handle;
134 svcId.token = svc.token;
135 svcId.cookie = svc.cookie;
136
137 char *pkgName = (char *)SoftBusMalloc(len + 1);
138 if (pkgName == NULL) {
139 SoftBusLog(SOFTBUS_LOG_COMM, SOFTBUS_LOG_ERROR, "softbus malloc failed!");
140 goto EXIT;
141 }
142 if (strcpy_s(pkgName, len + 1, name) != EOK) {
143 SoftBusLog(SOFTBUS_LOG_COMM, SOFTBUS_LOG_ERROR, "softbus strcpy_s failed!");
144 SoftBusFree(pkgName);
145 goto EXIT;
146 }
147 uint32_t cbId = 0;
148 AddDeathRecipient(svc, ClientDeathCb, pkgName, &cbId);
149 svcId.cbId = cbId;
150 ret = SERVER_RegisterService(name, &svcId);
151 EXIT:
152 WriteInt32(reply, ret);
153 return SOFTBUS_OK;
154 }
155
156 typedef struct {
157 enum SoftBusFuncId id;
158 int32_t (*func)(IpcIo *req, IpcIo *reply);
159 } ServerInvokeCmd;
160
161 ServerInvokeCmd g_serverInvokeCmdTbl[] = {
162 { MANAGE_REGISTER_SERVICE, ServerRegisterService },
163 { SERVER_PUBLISH_SERVICE, ServerPublishService },
164 { SERVER_UNPUBLISH_SERVICE, ServerUnPublishService },
165 { SERVER_START_DISCOVERY, ServerStartDiscovery },
166 { SERVER_STOP_DISCOVERY, ServerStopDiscovery },
167 { SERVER_JOIN_LNN, ServerJoinLNN },
168 { SERVER_JOIN_LNN, ServerJoinMetaNode },
169 { SERVER_LEAVE_LNN, ServerLeaveLNN },
170 { SERVER_LEAVE_LNN, ServerLeaveMetaNode },
171 { SERVER_GET_ALL_ONLINE_NODE_INFO, ServerGetAllOnlineNodeInfo },
172 { SERVER_GET_LOCAL_DEVICE_INFO, ServerGetLocalDeviceInfo },
173 { SERVER_GET_NODE_KEY_INFO, ServerGetNodeKeyInfo },
174 { SERVER_START_TIME_SYNC, ServerStartTimeSync },
175 { SERVER_STOP_TIME_SYNC, ServerStopTimeSync },
176 { SERVER_PUBLISH_LNN, ServerPublishLNN },
177 { SERVER_STOP_PUBLISH_LNN, ServerStopPublishLNN },
178 { SERVER_REFRESH_LNN, ServerRefreshLNN },
179 { SERVER_STOP_REFRESH_LNN, ServerStopRefreshLNN },
180 { SERVER_ACTIVE_META_NODE, ServerActiveMetaNode},
181 { SERVER_DEACTIVE_META_NODE, ServerDeactiveMetaNode },
182 { SERVER_GET_ALL_META_NODE_INFO, ServerGetAllMetaNodeInfo },
183 { SERVER_SHIFT_LNN_GEAR, ServerShiftLnnGear },
184 { SERVER_CREATE_SESSION_SERVER, ServerCreateSessionServer },
185 { SERVER_REMOVE_SESSION_SERVER, ServerRemoveSessionServer },
186 { SERVER_OPEN_SESSION, ServerOpenSession },
187 { SERVER_OPEN_AUTH_SESSION, ServerOpenAuthSession},
188 { SERVER_NOTIFY_AUTH_SUCCESS, ServerNotifyAuthSuccess},
189 { SERVER_CLOSE_CHANNEL, ServerCloseChannel },
190 { SERVER_SESSION_SENDMSG, ServerSendSessionMsg },
191 { SERVER_SET_NODE_DATA_CHANGE_FLAG, ServerSetNodeDataChangeFlag},
192 };
193
Invoke(IServerProxy * iProxy,int funcId,void * origin,IpcIo * req,IpcIo * reply)194 static int32_t Invoke(IServerProxy *iProxy, int funcId, void *origin, IpcIo *req, IpcIo *reply)
195 {
196 SoftBusLog(SOFTBUS_LOG_COMM, SOFTBUS_LOG_INFO, "RECEIVE FUNCID:%d", funcId);
197 if (GetServerIsInit() == false) {
198 SoftBusLog(SOFTBUS_LOG_COMM, SOFTBUS_LOG_ERROR, "server not init");
199 WriteInt32(reply, SOFTBUS_SERVER_NOT_INIT);
200 return SOFTBUS_ERR;
201 }
202 int tblSize = sizeof(g_serverInvokeCmdTbl) / sizeof(ServerInvokeCmd);
203 for (int i = 0; i < tblSize; i++) {
204 if (funcId == g_serverInvokeCmdTbl[i].id) {
205 return g_serverInvokeCmdTbl[i].func(req, reply);
206 }
207 }
208 SoftBusLog(SOFTBUS_LOG_COMM, SOFTBUS_LOG_ERROR, "not support func[%d]", funcId);
209 return SOFTBUS_ERR;
210 }
211
212 static SoftbusSamgrService g_samgrService = {
213 .GetName = GetName,
214 .Initialize = Initialize,
215 .MessageHandle = MessageHandle,
216 .GetTaskConfig = GetTaskConfig,
217 SERVER_IPROXY_IMPL_BEGIN,
218 .Invoke = Invoke,
219 IPROXY_END,
220 };
221
HOS_SystemInit(void)222 void __attribute__((weak)) HOS_SystemInit(void)
223 {
224 SAMGR_Bootstrap();
225 return;
226 }
227
ServerStubInit(void)228 int ServerStubInit(void)
229 {
230 HOS_SystemInit();
231
232 if (LnnIpcInit() != SOFTBUS_OK) {
233 SoftBusLog(SOFTBUS_LOG_COMM, SOFTBUS_LOG_ERROR, "Center Ipc init failed.");
234 return SOFTBUS_ERR;
235 }
236
237 if (SERVER_InitClient() != SOFTBUS_OK) {
238 SoftBusLog(SOFTBUS_LOG_COMM, SOFTBUS_LOG_ERROR, "client manager init failed.");
239 return SOFTBUS_ERR;
240 }
241 return SOFTBUS_OK;
242 }
243
Init(void)244 static void Init(void)
245 {
246 sleep(WAIT_FOR_SERVER);
247 SAMGR_GetInstance()->RegisterService((Service *)&g_samgrService);
248 SAMGR_GetInstance()->RegisterDefaultFeatureApi(SOFTBUS_SERVICE, GET_IUNKNOWN(g_samgrService));
249 SoftBusLog(SOFTBUS_LOG_COMM, SOFTBUS_LOG_INFO, "Init success %s", SOFTBUS_SERVICE);
250 }
251 SYSEX_SERVICE_INIT(Init);