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 "device_manager_ipc_interface_code.h"
17 #include "device_manager_service.h"
18 #include "dm_constants.h"
19 #include "dm_log.h"
20 #include "ipc_cmd_register.h"
21 #include "ipc_def.h"
22 #include "ipc_notify_auth_result_req.h"
23 #include "ipc_notify_device_found_req.h"
24 #include "ipc_notify_device_state_req.h"
25 #include "ipc_notify_discover_result_req.h"
26 #include "ipc_notify_verify_auth_result_req.h"
27 #include "ipc_server_stub.h"
28
29 namespace OHOS {
30 namespace DistributedHardware {
ON_IPC_SET_REQUEST(SERVER_DEVICE_STATE_NOTIFY,std::shared_ptr<IpcReq> pBaseReq,IpcIo & request,uint8_t * buffer,size_t buffLen)31 ON_IPC_SET_REQUEST(SERVER_DEVICE_STATE_NOTIFY, std::shared_ptr<IpcReq> pBaseReq, IpcIo &request, uint8_t *buffer,
32 size_t buffLen)
33 {
34 std::shared_ptr<IpcNotifyDeviceStateReq> pReq = std::static_pointer_cast<IpcNotifyDeviceStateReq>(pBaseReq);
35 std::string pkgName = pReq->GetPkgName();
36 int32_t deviceState = pReq->GetDeviceState();
37 DmDeviceInfo deviceInfo = pReq->GetDeviceInfo();
38
39 IpcIoInit(&request, buffer, buffLen, 0);
40 WriteString(&request, pkgName.c_str());
41 WriteInt32(&request, deviceState);
42 bool ret = WriteRawData(&request, &deviceInfo, sizeof(DmDeviceInfo));
43 if (!ret) {
44 return ERR_DM_FAILED;
45 }
46 return DM_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 if (pBaseRsp == nullptr) {
52 LOGE("pBaseRsp is null");
53 return ERR_DM_FAILED;
54 }
55 int32_t ret = 0;
56 ReadInt32(&reply, &ret);
57 pBaseRsp->SetErrCode(ret);
58 return DM_OK;
59 }
60
ON_IPC_SET_REQUEST(SERVER_DEVICE_FOUND,std::shared_ptr<IpcReq> pBaseReq,IpcIo & request,uint8_t * buffer,size_t buffLen)61 ON_IPC_SET_REQUEST(SERVER_DEVICE_FOUND, std::shared_ptr<IpcReq> pBaseReq, IpcIo &request, uint8_t *buffer,
62 size_t buffLen)
63 {
64 std::shared_ptr<IpcNotifyDeviceFoundReq> pReq = std::static_pointer_cast<IpcNotifyDeviceFoundReq>(pBaseReq);
65 std::string pkgName = pReq->GetPkgName();
66 uint16_t subscribeId = pReq->GetSubscribeId();
67 DmDeviceInfo deviceInfo = pReq->GetDeviceInfo();
68
69 IpcIoInit(&request, buffer, buffLen, 0);
70 WriteString(&request, pkgName.c_str());
71 WriteUint16(&request, subscribeId);
72 bool ret = WriteRawData(&request, &deviceInfo, sizeof(DmDeviceInfo));
73 if (!ret) {
74 return ERR_DM_FAILED;
75 }
76 return DM_OK;
77 }
78
ON_IPC_READ_RESPONSE(SERVER_DEVICE_FOUND,IpcIo & reply,std::shared_ptr<IpcRsp> pBaseRsp)79 ON_IPC_READ_RESPONSE(SERVER_DEVICE_FOUND, IpcIo &reply, std::shared_ptr<IpcRsp> pBaseRsp)
80 {
81 if (pBaseRsp == nullptr) {
82 LOGE("pBaseRsp is null");
83 return ERR_DM_FAILED;
84 }
85 int32_t ret = 0;
86 ReadInt32(&reply, &ret);
87 pBaseRsp->SetErrCode(ret);
88 return DM_OK;
89 }
90
ON_IPC_SET_REQUEST(SERVER_DISCOVER_FINISH,std::shared_ptr<IpcReq> pBaseReq,IpcIo & request,uint8_t * buffer,size_t buffLen)91 ON_IPC_SET_REQUEST(SERVER_DISCOVER_FINISH, std::shared_ptr<IpcReq> pBaseReq, IpcIo &request, uint8_t *buffer,
92 size_t buffLen)
93 {
94 std::shared_ptr<IpcNotifyDiscoverResultReq> pReq = std::static_pointer_cast<IpcNotifyDiscoverResultReq>(pBaseReq);
95 std::string pkgName = pReq->GetPkgName();
96 uint16_t subscribeId = pReq->GetSubscribeId();
97 int32_t result = pReq->GetResult();
98
99 IpcIoInit(&request, buffer, buffLen, 0);
100 WriteString(&request, pkgName.c_str());
101 WriteUint16(&request, subscribeId);
102 WriteInt32(&request, result);
103 return DM_OK;
104 }
105
ON_IPC_READ_RESPONSE(SERVER_DISCOVER_FINISH,IpcIo & reply,std::shared_ptr<IpcRsp> pBaseRsp)106 ON_IPC_READ_RESPONSE(SERVER_DISCOVER_FINISH, IpcIo &reply, std::shared_ptr<IpcRsp> pBaseRsp)
107 {
108 if (pBaseRsp == nullptr) {
109 LOGE("pBaseRsp is null");
110 return ERR_DM_FAILED;
111 }
112 int32_t ret = 0;
113 ReadInt32(&reply, &ret);
114 pBaseRsp->SetErrCode(ret);
115 return DM_OK;
116 }
117
ON_IPC_SET_REQUEST(SERVER_AUTH_RESULT,std::shared_ptr<IpcReq> pBaseReq,IpcIo & request,uint8_t * buffer,size_t buffLen)118 ON_IPC_SET_REQUEST(SERVER_AUTH_RESULT, std::shared_ptr<IpcReq> pBaseReq, IpcIo &request, uint8_t *buffer,
119 size_t buffLen)
120 {
121 std::shared_ptr<IpcNotifyAuthResultReq> pReq = std::static_pointer_cast<IpcNotifyAuthResultReq>(pBaseReq);
122 std::string pkgName = pReq->GetPkgName();
123 std::string deviceId = pReq->GetDeviceId();
124 std::string token = pReq->GetPinToken();
125 int32_t status = pReq->GetStatus();
126 int32_t reason = pReq->GetReason();
127
128 IpcIoInit(&request, buffer, buffLen, 0);
129 WriteString(&request, pkgName.c_str());
130 WriteString(&request, deviceId.c_str());
131 WriteString(&request, token.c_str());
132 WriteInt32(&request, status);
133 WriteInt32(&request, reason);
134 return DM_OK;
135 }
136
ON_IPC_READ_RESPONSE(SERVER_AUTH_RESULT,IpcIo & reply,std::shared_ptr<IpcRsp> pBaseRsp)137 ON_IPC_READ_RESPONSE(SERVER_AUTH_RESULT, IpcIo &reply, std::shared_ptr<IpcRsp> pBaseRsp)
138 {
139 if (pBaseRsp == nullptr) {
140 LOGE("pBaseRsp is null");
141 return ERR_DM_FAILED;
142 }
143 int32_t ret = 0;
144 ReadInt32(&reply, &ret);
145 pBaseRsp->SetErrCode(ret);
146 return DM_OK;
147 }
148
ON_IPC_SET_REQUEST(SERVER_VERIFY_AUTH_RESULT,std::shared_ptr<IpcReq> pBaseReq,IpcIo & request,uint8_t * buffer,size_t buffLen)149 ON_IPC_SET_REQUEST(SERVER_VERIFY_AUTH_RESULT, std::shared_ptr<IpcReq> pBaseReq, IpcIo &request, uint8_t *buffer,
150 size_t buffLen)
151 {
152 std::shared_ptr<IpcNotifyVerifyAuthResultReq> pReq =
153 std::static_pointer_cast<IpcNotifyVerifyAuthResultReq>(pBaseReq);
154 std::string pkgName = pReq->GetPkgName();
155 std::string deviceId = pReq->GetDeviceId();
156 int32_t result = pReq->GetResult();
157 int32_t flag = pReq->GetFlag();
158
159 IpcIoInit(&request, buffer, buffLen, 0);
160 WriteString(&request, pkgName.c_str());
161 WriteString(&request, deviceId.c_str());
162 WriteInt32(&request, result);
163 WriteInt32(&request, flag);
164 return DM_OK;
165 }
166
ON_IPC_READ_RESPONSE(SERVER_VERIFY_AUTH_RESULT,IpcIo & reply,std::shared_ptr<IpcRsp> pBaseRsp)167 ON_IPC_READ_RESPONSE(SERVER_VERIFY_AUTH_RESULT, IpcIo &reply, std::shared_ptr<IpcRsp> pBaseRsp)
168 {
169 if (pBaseRsp == nullptr) {
170 LOGE("pBaseRsp is null");
171 return ERR_DM_FAILED;
172 }
173 int32_t ret = 0;
174 ReadInt32(&reply, &ret);
175 pBaseRsp->SetErrCode(ret);
176 return DM_OK;
177 }
178
ON_IPC_SERVER_CMD(REGISTER_DEVICE_MANAGER_LISTENER,IpcIo & req,IpcIo & reply)179 ON_IPC_SERVER_CMD(REGISTER_DEVICE_MANAGER_LISTENER, IpcIo &req, IpcIo &reply)
180 {
181 int32_t errCode = RegisterDeviceManagerListener(&req, &reply);
182 WriteInt32(&reply, errCode);
183 }
184
ON_IPC_SERVER_CMD(UNREGISTER_DEVICE_MANAGER_LISTENER,IpcIo & req,IpcIo & reply)185 ON_IPC_SERVER_CMD(UNREGISTER_DEVICE_MANAGER_LISTENER, IpcIo &req, IpcIo &reply)
186 {
187 int32_t errCode = UnRegisterDeviceManagerListener(&req, &reply);
188 WriteInt32(&reply, errCode);
189 }
190
ON_IPC_SERVER_CMD(GET_TRUST_DEVICE_LIST,IpcIo & req,IpcIo & reply)191 ON_IPC_SERVER_CMD(GET_TRUST_DEVICE_LIST, IpcIo &req, IpcIo &reply)
192 {
193 LOGI("enter GetTrustedDeviceList.");
194 std::string pkgName = (const char *)ReadString(&req, nullptr);
195 std::string extra = (const char *)ReadString(&req, nullptr);
196
197 std::vector<DmDeviceInfo> deviceList;
198 int32_t ret = DeviceManagerService::GetInstance().GetTrustedDeviceList(pkgName, extra, deviceList);
199 WriteInt32(&reply, deviceList.size());
200 if (deviceList.size() > 0) {
201 bool value = WriteRawData(&reply, deviceList.data(), sizeof(DmDeviceInfo) * deviceList.size());
202 if (!value) {
203 return;
204 }
205 }
206 WriteInt32(&reply, ret);
207 }
208
ON_IPC_SERVER_CMD(GET_LOCAL_DEVICE_INFO,IpcIo & req,IpcIo & reply)209 ON_IPC_SERVER_CMD(GET_LOCAL_DEVICE_INFO, IpcIo &req, IpcIo &reply)
210 {
211 LOGI("enter GetLocalDeviceInfo.");
212 DmDeviceInfo dmDeviceInfo;
213 int32_t ret = DeviceManagerService::GetInstance().GetLocalDeviceInfo(dmDeviceInfo);
214 bool value = WriteRawData(&reply, &dmDeviceInfo, sizeof(DmDeviceInfo));
215 if (!value) {
216 return;
217 }
218 WriteInt32(&reply, ret);
219 }
220
ON_IPC_SERVER_CMD(START_DEVICE_DISCOVER,IpcIo & req,IpcIo & reply)221 ON_IPC_SERVER_CMD(START_DEVICE_DISCOVER, IpcIo &req, IpcIo &reply)
222 {
223 LOGI("StartDeviceDiscovery service listener.");
224 std::string pkgName = (const char *)ReadString(&req, nullptr);
225 std::string extra = (const char *)ReadString(&req, nullptr);
226 DmSubscribeInfo *pDmSubscribeInfo = (DmSubscribeInfo *)ReadRawData(&req, sizeof(DmSubscribeInfo));
227 int32_t ret = DeviceManagerService::GetInstance().StartDeviceDiscovery(pkgName, *pDmSubscribeInfo, extra);
228 WriteInt32(&reply, ret);
229 }
230
ON_IPC_SERVER_CMD(STOP_DEVICE_DISCOVER,IpcIo & req,IpcIo & reply)231 ON_IPC_SERVER_CMD(STOP_DEVICE_DISCOVER, IpcIo &req, IpcIo &reply)
232 {
233 LOGI("StopDeviceDiscovery service listener.");
234 std::string pkgName = (const char *)ReadString(&req, nullptr);
235 uint16_t subscribeId = 0;
236 ReadUint16(&reply, &subscribeId);
237 int32_t ret = DeviceManagerService::GetInstance().StopDeviceDiscovery(pkgName, subscribeId);
238 WriteInt32(&reply, ret);
239 }
240
ON_IPC_SERVER_CMD(AUTHENTICATE_DEVICE,IpcIo & req,IpcIo & reply)241 ON_IPC_SERVER_CMD(AUTHENTICATE_DEVICE, IpcIo &req, IpcIo &reply)
242 {
243 LOGI("AuthenticateDevice service listener.");
244 std::string pkgName = (const char *)ReadString(&req, nullptr);
245 std::string extra = (const char *)ReadString(&req, nullptr);
246 std::string deviceId = (const char *)ReadString(&req, nullptr);
247 int32_t authType = 0;
248 ReadInt32(&reply, &authType);
249 int32_t ret = DeviceManagerService::GetInstance().AuthenticateDevice(pkgName, authType, deviceId, extra);
250 WriteInt32(&reply, ret);
251 }
252
ON_IPC_SERVER_CMD(UNAUTHENTICATE_DEVICE,IpcIo & req,IpcIo & reply)253 ON_IPC_SERVER_CMD(UNAUTHENTICATE_DEVICE, IpcIo &req, IpcIo &reply)
254 {
255 LOGI("UnAuthenticateDevice service listener.");
256 std::string pkgName = (const char *)ReadString(&req, nullptr);
257 std::string deviceId = (const char *)ReadString(&req, nullptr);
258
259 int32_t ret = DeviceManagerService::GetInstance().UnAuthenticateDevice(pkgName, deviceId);
260 WriteInt32(&reply, ret);
261 }
262
ON_IPC_SERVER_CMD(VERIFY_AUTHENTICATION,IpcIo & req,IpcIo & reply)263 ON_IPC_SERVER_CMD(VERIFY_AUTHENTICATION, IpcIo &req, IpcIo &reply)
264 {
265 LOGI("VerifyAuthentication service listener.");
266 std::string authParam = (const char *)ReadString(&req, nullptr);
267 int32_t ret = DeviceManagerService::GetInstance().VerifyAuthentication(authParam);
268 WriteInt32(&reply, ret);
269 }
270
ON_IPC_SERVER_CMD(SERVER_USER_AUTH_OPERATION,IpcIo & req,IpcIo & reply)271 ON_IPC_SERVER_CMD(SERVER_USER_AUTH_OPERATION, IpcIo &req, IpcIo &reply)
272 {
273 size_t len = 0;
274 std::string packName = (const char *)ReadString(&req, &len);
275 int32_t action = 0;
276 ReadInt32(&reply, &action);
277 DeviceManagerService::GetInstance().SetUserOperation(packName, action);
278 WriteInt32(&reply, action);
279 }
280
ON_IPC_SET_REQUEST(SERVER_DEVICE_FA_NOTIFY,std::shared_ptr<IpcReq> pBaseReq,IpcIo & request,uint8_t * buffer,size_t buffLen)281 ON_IPC_SET_REQUEST(SERVER_DEVICE_FA_NOTIFY, std::shared_ptr<IpcReq> pBaseReq, IpcIo &request, uint8_t *buffer,
282 size_t buffLen)
283 {
284 std::shared_ptr<IpcNotifyDMFAResultReq> pReq = std::static_pointer_cast<IpcNotifyDMFAResultReq>(pBaseReq);
285 std::string packagname = pReq->GetPkgName();
286 std::string paramJson = pReq->GetJsonParam();
287 WriteString(&request, packagname.c_str());
288 WriteString(&request, paramJson.c_str());
289 return DM_OK;
290 }
291 } // namespace DistributedHardware
292 } // namespace OHOS
293