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 "constants.h"
17 #include "ipc_cmd_register.h"
18
19 #include "device_manager_errno.h"
20 #include "device_manager_log.h"
21
22 #include "ipc_def.h"
23 #include "ipc_notify_auth_result_req.h"
24 #include "ipc_notify_check_auth_result_req.h"
25 #include "ipc_notify_device_found_req.h"
26 #include "ipc_notify_device_state_req.h"
27 #include "ipc_notify_discover_result_req.h"
28 #include "ipc_notify_dmfa_result_req.h"
29 #include "ipc_server_adapter.h"
30 #include "ipc_server_stub.h"
31
32 namespace OHOS {
33 namespace DistributedHardware {
ON_IPC_SET_REQUEST(SERVER_DEVICE_STATE_NOTIFY,std::shared_ptr<IpcReq> pBaseReq,IpcIo & request,uint8_t * buffer,size_t buffLen)34 ON_IPC_SET_REQUEST(SERVER_DEVICE_STATE_NOTIFY, std::shared_ptr<IpcReq> pBaseReq, IpcIo &request,
35 uint8_t *buffer, size_t buffLen)
36 {
37 std::shared_ptr<IpcNotifyDeviceStateReq> pReq = std::static_pointer_cast<IpcNotifyDeviceStateReq>(pBaseReq);
38 std::string pkgName = pReq->GetPkgName();
39 int32_t deviceState = pReq->GetDeviceState();
40 DmDeviceInfo deviceInfo = pReq->GetDeviceInfo();
41
42 IpcIoInit(&request, buffer, buffLen, 0);
43 IpcIoPushString(&request, pkgName.c_str());
44 IpcIoPushInt32(&request, deviceState);
45 IpcIoPushFlatObj(&request, &deviceInfo, sizeof(DmDeviceInfo));
46 return DEVICEMANAGER_OK;
47 }
48
ON_IPC_READ_RESPONSE(SERVER_DEVICE_STATE_NOTIFY,IpcIo & reply,std::shared_ptr<IpcRsp> pBaseRsp)49 ON_IPC_READ_RESPONSE(SERVER_DEVICE_STATE_NOTIFY, IpcIo &reply, std::shared_ptr<IpcRsp> pBaseRsp)
50 {
51 pBaseRsp->SetErrCode(IpcIoPopInt32(&reply));
52 return DEVICEMANAGER_OK;
53 }
54
ON_IPC_SET_REQUEST(SERVER_DEVICE_FOUND,std::shared_ptr<IpcReq> pBaseReq,IpcIo & request,uint8_t * buffer,size_t buffLen)55 ON_IPC_SET_REQUEST(SERVER_DEVICE_FOUND, std::shared_ptr<IpcReq> pBaseReq, IpcIo &request,
56 uint8_t *buffer, size_t buffLen)
57 {
58 std::shared_ptr<IpcNotifyDeviceFoundReq> pReq = std::static_pointer_cast<IpcNotifyDeviceFoundReq>(pBaseReq);
59 std::string pkgName = pReq->GetPkgName();
60 uint16_t subscribeId = pReq->GetSubscribeId();
61 DmDeviceInfo deviceInfo = pReq->GetDeviceInfo();
62
63 IpcIoInit(&request, buffer, buffLen, 0);
64 IpcIoPushString(&request, pkgName.c_str());
65 IpcIoPushUint16(&request, subscribeId);
66 IpcIoPushFlatObj(&request, &deviceInfo, sizeof(DmDeviceInfo));
67 return DEVICEMANAGER_OK;
68 }
69
ON_IPC_READ_RESPONSE(SERVER_DEVICE_FOUND,IpcIo & reply,std::shared_ptr<IpcRsp> pBaseRsp)70 ON_IPC_READ_RESPONSE(SERVER_DEVICE_FOUND, IpcIo &reply, std::shared_ptr<IpcRsp> pBaseRsp)
71 {
72 pBaseRsp->SetErrCode(IpcIoPopInt32(&reply));
73 return DEVICEMANAGER_OK;
74 }
75
ON_IPC_SET_REQUEST(SERVER_DISCOVER_FINISH,std::shared_ptr<IpcReq> pBaseReq,IpcIo & request,uint8_t * buffer,size_t buffLen)76 ON_IPC_SET_REQUEST(SERVER_DISCOVER_FINISH, std::shared_ptr<IpcReq> pBaseReq, IpcIo &request,
77 uint8_t *buffer, size_t buffLen)
78 {
79 std::shared_ptr<IpcNotifyDiscoverResultReq> pReq = std::static_pointer_cast<IpcNotifyDiscoverResultReq>(pBaseReq);
80 std::string pkgName = pReq->GetPkgName();
81 uint16_t subscribeId = pReq->GetSubscribeId();
82 int32_t result = pReq->GetResult();
83
84 IpcIoInit(&request, buffer, buffLen, 0);
85 IpcIoPushString(&request, pkgName.c_str());
86 IpcIoPushUint16(&request, subscribeId);
87 IpcIoPushInt32(&request, result);
88 return DEVICEMANAGER_OK;
89 }
90
ON_IPC_READ_RESPONSE(SERVER_DISCOVER_FINISH,IpcIo & reply,std::shared_ptr<IpcRsp> pBaseRsp)91 ON_IPC_READ_RESPONSE(SERVER_DISCOVER_FINISH, IpcIo &reply, std::shared_ptr<IpcRsp> pBaseRsp)
92 {
93 pBaseRsp->SetErrCode(IpcIoPopInt32(&reply));
94 return DEVICEMANAGER_OK;
95 }
96
ON_IPC_SET_REQUEST(SERVER_AUTH_RESULT,std::shared_ptr<IpcReq> pBaseReq,IpcIo & request,uint8_t * buffer,size_t buffLen)97 ON_IPC_SET_REQUEST(SERVER_AUTH_RESULT, std::shared_ptr<IpcReq> pBaseReq, IpcIo &request,
98 uint8_t *buffer, size_t buffLen)
99 {
100 std::shared_ptr<IpcNotifyAuthResultReq> pReq = std::static_pointer_cast<IpcNotifyAuthResultReq>(pBaseReq);
101 std::string pkgName = pReq->GetPkgName();
102 std::string deviceId = pReq->GetDeviceId();
103 int32_t pinToken = pReq->GetPinToken();
104 int32_t status = pReq->GetStatus();
105 int32_t reason = pReq->GetReason();
106
107 IpcIoInit(&request, buffer, buffLen, 0);
108 IpcIoPushString(&request, pkgName.c_str());
109 IpcIoPushString(&request, deviceId.c_str());
110 IpcIoPushInt32(&request, pinToken);
111 IpcIoPushInt32(&request, status);
112 IpcIoPushInt32(&request, reason);
113 return DEVICEMANAGER_OK;
114 }
115
ON_IPC_READ_RESPONSE(SERVER_AUTH_RESULT,IpcIo & reply,std::shared_ptr<IpcRsp> pBaseRsp)116 ON_IPC_READ_RESPONSE(SERVER_AUTH_RESULT, IpcIo &reply, std::shared_ptr<IpcRsp> pBaseRsp)
117 {
118 pBaseRsp->SetErrCode(IpcIoPopInt32(&reply));
119 return DEVICEMANAGER_OK;
120 }
121
ON_IPC_SET_REQUEST(SERVER_CHECK_AUTH_RESULT,std::shared_ptr<IpcReq> pBaseReq,IpcIo & request,uint8_t * buffer,size_t buffLen)122 ON_IPC_SET_REQUEST(SERVER_CHECK_AUTH_RESULT, std::shared_ptr<IpcReq> pBaseReq, IpcIo &request,
123 uint8_t *buffer, size_t buffLen)
124 {
125 std::shared_ptr<IpcNotifyCheckAuthResultReq> pReq = std::static_pointer_cast<IpcNotifyCheckAuthResultReq>(pBaseReq);
126 std::string pkgName = pReq->GetPkgName();
127 std::string deviceId = pReq->GetDeviceId();
128 int32_t result = pReq->GetResult();
129 int32_t flag = pReq->GetFlag();
130
131 IpcIoInit(&request, buffer, buffLen, 0);
132 IpcIoPushString(&request, pkgName.c_str());
133 IpcIoPushString(&request, deviceId.c_str());
134 IpcIoPushInt32(&request, result);
135 IpcIoPushInt32(&request, flag);
136 return DEVICEMANAGER_OK;
137 }
138
ON_IPC_READ_RESPONSE(SERVER_CHECK_AUTH_RESULT,IpcIo & reply,std::shared_ptr<IpcRsp> pBaseRsp)139 ON_IPC_READ_RESPONSE(SERVER_CHECK_AUTH_RESULT, IpcIo &reply, std::shared_ptr<IpcRsp> pBaseRsp)
140 {
141 pBaseRsp->SetErrCode(IpcIoPopInt32(&reply));
142 return DEVICEMANAGER_OK;
143 }
144
ON_IPC_SERVER_CMD(GET_TRUST_DEVICE_LIST,IpcIo & req,IpcIo & reply)145 ON_IPC_SERVER_CMD(GET_TRUST_DEVICE_LIST, IpcIo &req, IpcIo &reply)
146 {
147 DMLOG(DM_LOG_INFO, "enter GetTrustedDeviceList.");
148 size_t len = 0;
149 std::string pkgName = (const char *)IpcIoPopString(&req, &len);
150 std::string extra = (const char *)IpcIoPopString(&req, &len);
151 DmDeviceInfo *info = nullptr;
152 int32_t infoNum = 0;
153 int32_t ret = IpcServerAdapter::GetInstance().GetTrustedDeviceList(pkgName, extra, &info, &infoNum);
154 IpcIoPushInt32(&reply, infoNum);
155 if (infoNum > 0) {
156 IpcIoPushFlatObj(&reply, info, sizeof(DmDeviceInfo) * infoNum);
157 free(info);
158 }
159 IpcIoPushInt32(&reply, ret);
160 }
161
ON_IPC_SERVER_CMD(REGISTER_DEVICE_MANAGER_LISTENER,IpcIo & req,IpcIo & reply)162 ON_IPC_SERVER_CMD(REGISTER_DEVICE_MANAGER_LISTENER, IpcIo &req, IpcIo &reply)
163 {
164 int32_t errCode = RegisterDeviceManagerListener(&req, &reply);
165 IpcIoPushInt32(&reply, errCode);
166 }
167
ON_IPC_SERVER_CMD(UNREGISTER_DEVICE_MANAGER_LISTENER,IpcIo & req,IpcIo & reply)168 ON_IPC_SERVER_CMD(UNREGISTER_DEVICE_MANAGER_LISTENER, IpcIo &req, IpcIo &reply)
169 {
170 int32_t errCode = UnRegisterDeviceManagerListener(&req, &reply);
171 IpcIoPushInt32(&reply, errCode);
172 }
173
ON_IPC_SERVER_CMD(START_DEVICE_DISCOVER,IpcIo & req,IpcIo & reply)174 ON_IPC_SERVER_CMD(START_DEVICE_DISCOVER, IpcIo &req, IpcIo &reply)
175 {
176 DMLOG(DM_LOG_INFO, "StartDeviceDiscovery service listener.");
177 size_t len = 0;
178 std::string pkgName = (const char *)IpcIoPopString(&req, &len);
179 uint32_t size = 0;
180 DmSubscribeInfo *pDmSubscribeInfo = (DmSubscribeInfo*)IpcIoPopFlatObj(&req, &size);
181
182 int32_t ret = IpcServerAdapter::GetInstance().StartDeviceDiscovery(pkgName, *pDmSubscribeInfo);
183 IpcIoPushInt32(&reply, ret);
184 }
185
ON_IPC_SERVER_CMD(STOP_DEVICE_DISCOVER,IpcIo & req,IpcIo & reply)186 ON_IPC_SERVER_CMD(STOP_DEVICE_DISCOVER, IpcIo &req, IpcIo &reply)
187 {
188 DMLOG(DM_LOG_INFO, "StopDeviceDiscovery service listener.");
189 size_t len = 0;
190 std::string pkgName = (const char *)IpcIoPopString(&req, &len);
191 uint16_t subscribeId = IpcIoPopUint16(&req);
192 int32_t ret = IpcServerAdapter::GetInstance().StopDiscovery(pkgName, subscribeId);
193 IpcIoPushInt32(&reply, ret);
194 }
195
ON_IPC_SERVER_CMD(AUTHENTICATE_DEVICE,IpcIo & req,IpcIo & reply)196 ON_IPC_SERVER_CMD(AUTHENTICATE_DEVICE, IpcIo &req, IpcIo &reply)
197 {
198 DMLOG(DM_LOG_INFO, "AuthenticateDevice service listener.");
199 size_t len = 0;
200 std::string pkgName = (const char *)IpcIoPopString(&req, &len);
201 size_t extraLen = 0;
202 std::string extra = (const char *)IpcIoPopString(&req, &extraLen);
203 uint32_t size;
204 DmDeviceInfo *deviceInfo = (DmDeviceInfo*)IpcIoPopFlatObj(&req, &size);
205 DmAppImageInfo imageInfo(nullptr, 0, nullptr, 0);
206 int32_t ret = IpcServerAdapter::GetInstance().AuthenticateDevice(pkgName, *deviceInfo, imageInfo, extra);
207 IpcIoPushInt32(&reply, ret);
208 }
209
ON_IPC_SERVER_CMD(CHECK_AUTHENTICATION,IpcIo & req,IpcIo & reply)210 ON_IPC_SERVER_CMD(CHECK_AUTHENTICATION, IpcIo &req, IpcIo &reply)
211 {
212 DMLOG(DM_LOG_INFO, "CheckAuthentication service listener.");
213 size_t authParaLen = 0;
214 std::string authPara = (const char *)IpcIoPopString(&req, &authParaLen);
215 int32_t ret = IpcServerAdapter::GetInstance().CheckAuthentication(authPara);
216 IpcIoPushInt32(&reply, ret);
217 }
218
ON_IPC_SERVER_CMD(SERVER_GET_AUTHENTCATION_INFO,IpcIo & req,IpcIo & reply)219 ON_IPC_SERVER_CMD(SERVER_GET_AUTHENTCATION_INFO, IpcIo &req, IpcIo &reply)
220 {
221 size_t len = 0;
222 std::string packName = (const char *)IpcIoPopString(&req, &len);
223 DmAuthParam authParam = {0};
224 DMLOG(DM_LOG_ERROR, "DeviceManagerStub:: GET_AUTHENTCATION_INFO:pkgName:%s", packName.c_str());
225 IpcServerAdapter::GetInstance().GetAuthenticationParam(packName, authParam);
226 if (authParam.direction == AUTH_SESSION_SIDE_CLIENT) {
227 IpcIoPushInt32(&reply, authParam.direction);
228 IpcIoPushInt32(&reply, authParam.authType);
229 IpcIoPushInt32(&reply, authParam.pinToken);
230 DMLOG(DM_LOG_DEBUG, "DeviceManagerStub::is Client so just return direction");
231 return;
232 }
233
234 int32_t appIconLen = authParam.imageinfo.GetAppIconLen();
235 int32_t appThumbnailLen = authParam.imageinfo.GetAppThumbnailLen();
236
237 IpcIoPushInt32(&reply, authParam.direction);
238 IpcIoPushInt32(&reply, authParam.authType);
239 IpcIoPushString(&reply, authParam.packageName.c_str());
240 IpcIoPushString(&reply, authParam.appName.c_str());
241 IpcIoPushString(&reply, authParam.appDescription.c_str());
242 IpcIoPushInt32(&reply, authParam.business);
243 IpcIoPushInt32(&reply, authParam.pincode);
244 IpcIoPushInt32(&reply, appIconLen);
245 IpcIoPushInt32(&reply, appThumbnailLen);
246
247 if (appIconLen > 0 && authParam.imageinfo.GetAppIcon() != nullptr) {
248 IpcIoPushFlatObj(&reply, authParam.imageinfo.GetAppIcon(), appIconLen);
249 }
250
251 if (appThumbnailLen > 0 && authParam.imageinfo.GetAppThumbnail() != nullptr) {
252 IpcIoPushFlatObj(&reply, authParam.imageinfo.GetAppThumbnail(), appThumbnailLen);
253 }
254 }
255
ON_IPC_SERVER_CMD(SERVER_USER_AUTHORIZATION_OPERATION,IpcIo & req,IpcIo & reply)256 ON_IPC_SERVER_CMD(SERVER_USER_AUTHORIZATION_OPERATION, IpcIo &req, IpcIo &reply)
257 {
258 size_t len = 0;
259 std::string packName = (const char *)IpcIoPopString(&req, &len);
260 int32_t action = IpcIoPopInt32(&reply);
261 IpcServerAdapter::GetInstance().SetUserOperation(packName, action);
262
263 IpcIoPushInt32(&reply, action);
264 }
265
ON_IPC_SET_REQUEST(SERVER_DEVICEMANAGER_FA_NOTIFY,std::shared_ptr<IpcReq> pBaseReq,IpcIo & request,uint8_t * buffer,size_t buffLen)266 ON_IPC_SET_REQUEST(SERVER_DEVICEMANAGER_FA_NOTIFY, std::shared_ptr<IpcReq> pBaseReq, IpcIo& request,
267 uint8_t *buffer, size_t buffLen)
268 {
269 std::shared_ptr<IpcNotifyDMFAResultReq> pReq = std::static_pointer_cast<IpcNotifyDMFAResultReq>(pBaseReq);
270 std::string packagname = pReq->GetPkgName();
271 std::string paramJson = pReq->GetJsonParam();
272 IpcIoPushString(&request, packagname.c_str());
273 IpcIoPushString(&request, paramJson.c_str());
274 return DEVICEMANAGER_OK;
275 }
276
ON_IPC_READ_RESPONSE(SERVER_DEVICEMANAGER_FA_NOTIFY,IpcIo & reply,std::shared_ptr<IpcRsp> pBaseRsp)277 ON_IPC_READ_RESPONSE(SERVER_DEVICEMANAGER_FA_NOTIFY, IpcIo& reply, std::shared_ptr<IpcRsp> pBaseRsp)
278 {
279 pBaseRsp->SetErrCode(IpcIoPopInt32(&reply));
280 return DEVICEMANAGER_OK;
281 }
282 } // namespace DistributedHardware
283 } // namespace OHOS
284