1 /*
2 * Copyright (c) 2022-2023 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 "ipc_server_stub.h"
17
18 #include "device_manager_service.h"
19 #include "dm_constants.h"
20 #include "dm_log.h"
21 #include "dm_subscribe_info.h"
22 #include "ipc_cmd_register.h"
23 #include "ipc_def.h"
24 #include "ipc_server_listenermgr.h"
25 #include "iproxy_server.h"
26 #include "ipc_skeleton.h"
27 #include "ohos_init.h"
28 #include "samgr_lite.h"
29 #include "securec.h"
30
31 namespace {
32 const int32_t WAIT_FOR_SERVER = 2;
33 const int32_t STACK_SIZE = 0x1000;
34 const int32_t QUEUE_SIZE = 32;
35 const int32_t MALLOC_MAX_LEN = 2 * 1024 * 1024;
36 } // namespace
37
38 using namespace OHOS::DistributedHardware;
39
40 struct DefaultFeatureApi {
41 INHERIT_SERVER_IPROXY;
42 };
43
44 struct DeviceManagerSamgrService {
45 INHERIT_SERVICE;
46 INHERIT_IUNKNOWNENTRY(DefaultFeatureApi);
47 Identity identity;
48 };
49
DeathCb(void * arg)50 static void DeathCb(void *arg)
51 {
52 if (arg == nullptr) {
53 LOGE("package name is NULL.");
54 return;
55 }
56 CommonSvcId svcId = {0};
57 std::string pkgName = reinterpret_cast<const char*>(arg);
58 if (IpcServerListenermgr::GetInstance().GetListenerByPkgName(pkgName, &svcId) != DM_OK) {
59 LOGE("not found client by package name.");
60 free(arg);
61 return;
62 }
63 IpcServerListenermgr::GetInstance().UnregisterListener(pkgName);
64 free(arg);
65 SvcIdentity sid = {0};
66 sid.handle = svcId.handle;
67 sid.token = svcId.token;
68 sid.cookie = svcId.cookie;
69 ReleaseSvc(sid);
70 }
71
RegisterDeviceManagerListener(IpcIo * req,IpcIo * reply)72 int32_t RegisterDeviceManagerListener(IpcIo *req, IpcIo *reply)
73 {
74 LOGI("register service listener.");
75 size_t len = 0;
76 uint8_t *name = ReadString(req, &len);
77 SvcIdentity svc;
78 bool ret = ReadRemoteObject(req, &svc);
79 if (!ret || name == NULL || len == 0) {
80 LOGE("get para failed");
81 return ERR_DM_INPUT_PARA_INVALID;
82 }
83
84 CommonSvcId svcId = {0};
85 svcId.handle = svc.handle;
86 svcId.token = svc.token;
87 svcId.cookie = svc.cookie;
88
89 if (len == 0 || len > MALLOC_MAX_LEN) {
90 LOGE("malloc length invalid!");
91 return ERR_DM_MALLOC_FAILED;
92 }
93 char *pkgName = new char[len+1];
94 if (pkgName == nullptr) {
95 LOGE("malloc failed!");
96 return ERR_DM_MALLOC_FAILED;
97 }
98 if (strcpy_s(pkgName, len + 1, reinterpret_cast<const char*>(name)) != DM_OK) {
99 LOGE("strcpy_s failed!");
100 delete[] pkgName;
101 return ERR_DM_IPC_COPY_FAILED;
102 }
103 uint32_t cbId = 0;
104 AddDeathRecipient(svc, DeathCb, pkgName, &cbId);
105 svcId.cbId = cbId;
106 std::string strPkgName = reinterpret_cast<const char*>(name);
107 return IpcServerListenermgr::GetInstance().RegisterListener(strPkgName, &svcId);
108 }
109
UnRegisterDeviceManagerListener(IpcIo * req,IpcIo * reply)110 int32_t UnRegisterDeviceManagerListener(IpcIo *req, IpcIo *reply)
111 {
112 LOGI("unregister service listener.");
113 size_t len = 0;
114 std::string pkgName = reinterpret_cast<const char*>(ReadString(req, &len));
115 if (pkgName == "" || len == 0) {
116 LOGE("get para failed");
117 return ERR_DM_FAILED;
118 }
119 CommonSvcId svcId;
120 if (IpcServerListenermgr::GetInstance().GetListenerByPkgName(pkgName, &svcId) != DM_OK) {
121 LOGE("not found listener by package name.");
122 return ERR_DM_FAILED;
123 }
124 int32_t ret = IpcServerListenermgr::GetInstance().UnregisterListener(pkgName);
125 if (ret == DM_OK) {
126 SvcIdentity sid;
127 sid.handle = svcId.handle;
128 sid.token = svcId.token;
129 sid.cookie = svcId.cookie;
130 ReleaseSvc(sid);
131 }
132 return ret;
133 }
134
GetName(Service * service)135 static const char *GetName(Service *service)
136 {
137 (void)service;
138 return DEVICE_MANAGER_SERVICE_NAME;
139 }
140
Initialize(Service * service,Identity identity)141 static BOOL Initialize(Service *service, Identity identity)
142 {
143 if (service == nullptr) {
144 LOGW("invalid param");
145 return FALSE;
146 }
147
148 DeviceManagerSamgrService *mgrService = reinterpret_cast<DeviceManagerSamgrService *>(service);
149 mgrService->identity = identity;
150 return TRUE;
151 }
152
MessageHandle(Service * service,Request * request)153 static BOOL MessageHandle(Service *service, Request *request)
154 {
155 if ((service == nullptr) || (request == nullptr)) {
156 LOGW("invalid param");
157 return FALSE;
158 }
159 return TRUE;
160 }
161
GetTaskConfig(Service * service)162 static TaskConfig GetTaskConfig(Service *service)
163 {
164 (void)service;
165 TaskConfig config = {LEVEL_HIGH, PRI_BELOW_NORMAL, STACK_SIZE, QUEUE_SIZE, SHARED_TASK};
166 return config;
167 }
168
OnRemoteRequestLite(IServerProxy * iProxy,int32_t funcId,void * origin,IpcIo * req,IpcIo * reply)169 static int32_t OnRemoteRequestLite(IServerProxy *iProxy, int32_t funcId, void *origin, IpcIo *req, IpcIo *reply)
170 {
171 LOGI("Receive funcId:%d", funcId);
172 (void)origin;
173 return IpcCmdRegister::GetInstance().OnIpcServerCmd(funcId, *req, *reply);
174 }
175
HOS_SystemInit(void)176 static void HOS_SystemInit(void)
177 {
178 SAMGR_Bootstrap();
179 return;
180 }
181
IpcServerStubInit(void)182 int32_t IpcServerStubInit(void)
183 {
184 HOS_SystemInit();
185 return DM_OK;
186 }
187
DevMgrSvcInit(void)188 static void DevMgrSvcInit(void)
189 {
190 sleep(WAIT_FOR_SERVER);
191 static DeviceManagerSamgrService service = {
192 .GetName = GetName,
193 .Initialize = Initialize,
194 .MessageHandle = MessageHandle,
195 .GetTaskConfig = GetTaskConfig,
196 SERVER_IPROXY_IMPL_BEGIN,
197 .Invoke = OnRemoteRequestLite,
198 IPROXY_END,
199 };
200
201 if (!SAMGR_GetInstance()->RegisterService((Service *)&service)) {
202 LOGE("%s, RegisterService failed", DEVICE_MANAGER_SERVICE_NAME);
203 return;
204 }
205 if (!SAMGR_GetInstance()->RegisterDefaultFeatureApi(DEVICE_MANAGER_SERVICE_NAME, GET_IUNKNOWN(service))) {
206 LOGE("%s, RegisterDefaultFeatureApi failed", DEVICE_MANAGER_SERVICE_NAME);
207 return;
208 }
209 LOGI("%s, init success", DEVICE_MANAGER_SERVICE_NAME);
210 }
211
212 SYSEX_SERVICE_INIT(DevMgrSvcInit);
213