• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 #include "ipc_def.h"
19 #include "ipc_notify_auth_result_req.h"
20 #include "ipc_notify_check_auth_result_req.h"
21 #include "ipc_notify_device_found_req.h"
22 #include "ipc_notify_device_state_req.h"
23 #include "ipc_notify_discover_result_req.h"
24 #include "ipc_server_adapter.h"
25 #include "ipc_server_stub.h"
26 #include "ipc_notify_dmfa_result_req.h"
27 
28 #include "device_manager_errno.h"
29 #include "device_manager_log.h"
30 
31 namespace OHOS {
32 namespace DistributedHardware {
ON_IPC_SET_REQUEST(SERVER_DEVICE_STATE_NOTIFY,std::shared_ptr<IpcReq> pBaseReq,MessageParcel & data)33 ON_IPC_SET_REQUEST(SERVER_DEVICE_STATE_NOTIFY, std::shared_ptr<IpcReq> pBaseReq, MessageParcel &data)
34 {
35     std::shared_ptr<IpcNotifyDeviceStateReq> pReq = std::static_pointer_cast<IpcNotifyDeviceStateReq>(pBaseReq);
36     std::string pkgName = pReq->GetPkgName();
37     int32_t deviceState = pReq->GetDeviceState();
38     DmDeviceInfo deviceInfo = pReq->GetDeviceInfo();
39     if (!data.WriteString(pkgName)) {
40         DMLOG(DM_LOG_ERROR, "write pkgName failed");
41         return DEVICEMANAGER_FLATTEN_OBJECT;
42     }
43     if (!data.WriteInt32(deviceState)) {
44         DMLOG(DM_LOG_ERROR, "write state failed");
45         return DEVICEMANAGER_FLATTEN_OBJECT;
46     }
47     if (!data.WriteRawData(&deviceInfo, sizeof(DmDeviceInfo))) {
48         DMLOG(DM_LOG_ERROR, "write deviceInfo failed");
49         return DEVICEMANAGER_FLATTEN_OBJECT;
50     }
51     return DEVICEMANAGER_OK;
52 }
53 
ON_IPC_READ_RESPONSE(SERVER_DEVICE_STATE_NOTIFY,MessageParcel & reply,std::shared_ptr<IpcRsp> pBaseRsp)54 ON_IPC_READ_RESPONSE(SERVER_DEVICE_STATE_NOTIFY, MessageParcel &reply, std::shared_ptr<IpcRsp> pBaseRsp)
55 {
56     pBaseRsp->SetErrCode(reply.ReadInt32());
57     return DEVICEMANAGER_OK;
58 }
59 
ON_IPC_SET_REQUEST(SERVER_DEVICE_FOUND,std::shared_ptr<IpcReq> pBaseReq,MessageParcel & data)60 ON_IPC_SET_REQUEST(SERVER_DEVICE_FOUND, std::shared_ptr<IpcReq> pBaseReq, MessageParcel &data)
61 {
62     std::shared_ptr<IpcNotifyDeviceFoundReq> pReq = std::static_pointer_cast<IpcNotifyDeviceFoundReq>(pBaseReq);
63     std::string pkgName = pReq->GetPkgName();
64     uint16_t subscribeId = pReq->GetSubscribeId();
65     DmDeviceInfo deviceInfo = pReq->GetDeviceInfo();
66     if (!data.WriteString(pkgName)) {
67         DMLOG(DM_LOG_ERROR, "write pkgName failed");
68         return DEVICEMANAGER_FLATTEN_OBJECT;
69     }
70     if (!data.WriteInt16(subscribeId)) {
71         DMLOG(DM_LOG_ERROR, "write subscribeId failed");
72         return DEVICEMANAGER_FLATTEN_OBJECT;
73     }
74     if (!data.WriteRawData(&deviceInfo, sizeof(DmDeviceInfo))) {
75         DMLOG(DM_LOG_ERROR, "write deviceInfo failed");
76         return DEVICEMANAGER_FLATTEN_OBJECT;
77     }
78     return DEVICEMANAGER_OK;
79 }
80 
ON_IPC_READ_RESPONSE(SERVER_DEVICE_FOUND,MessageParcel & reply,std::shared_ptr<IpcRsp> pBaseRsp)81 ON_IPC_READ_RESPONSE(SERVER_DEVICE_FOUND, MessageParcel &reply, std::shared_ptr<IpcRsp> pBaseRsp)
82 {
83     pBaseRsp->SetErrCode(reply.ReadInt32());
84     return DEVICEMANAGER_OK;
85 }
86 
ON_IPC_SET_REQUEST(SERVER_DISCOVER_FINISH,std::shared_ptr<IpcReq> pBaseReq,MessageParcel & data)87 ON_IPC_SET_REQUEST(SERVER_DISCOVER_FINISH, std::shared_ptr<IpcReq> pBaseReq, MessageParcel &data)
88 {
89     std::shared_ptr<IpcNotifyDiscoverResultReq> pReq = std::static_pointer_cast<IpcNotifyDiscoverResultReq>(pBaseReq);
90     std::string pkgName = pReq->GetPkgName();
91     uint16_t subscribeId = pReq->GetSubscribeId();
92     int32_t result = pReq->GetResult();
93     if (!data.WriteString(pkgName)) {
94         DMLOG(DM_LOG_ERROR, "write pkgName failed");
95         return DEVICEMANAGER_FLATTEN_OBJECT;
96     }
97     if (!data.WriteInt16(subscribeId)) {
98         DMLOG(DM_LOG_ERROR, "write subscribeId failed");
99         return DEVICEMANAGER_FLATTEN_OBJECT;
100     }
101     if (!data.WriteInt32(result)) {
102         DMLOG(DM_LOG_ERROR, "write result failed");
103         return DEVICEMANAGER_FLATTEN_OBJECT;
104     }
105     return DEVICEMANAGER_OK;
106 }
107 
ON_IPC_READ_RESPONSE(SERVER_DISCOVER_FINISH,MessageParcel & reply,std::shared_ptr<IpcRsp> pBaseRsp)108 ON_IPC_READ_RESPONSE(SERVER_DISCOVER_FINISH, MessageParcel &reply, std::shared_ptr<IpcRsp> pBaseRsp)
109 {
110     pBaseRsp->SetErrCode(reply.ReadInt32());
111     return DEVICEMANAGER_OK;
112 }
113 
ON_IPC_SET_REQUEST(SERVER_AUTH_RESULT,std::shared_ptr<IpcReq> pBaseReq,MessageParcel & data)114 ON_IPC_SET_REQUEST(SERVER_AUTH_RESULT, std::shared_ptr<IpcReq> pBaseReq, MessageParcel &data)
115 {
116     std::shared_ptr<IpcNotifyAuthResultReq> pReq = std::static_pointer_cast<IpcNotifyAuthResultReq>(pBaseReq);
117     std::string pkgName = pReq->GetPkgName();
118     std::string deviceId = pReq->GetDeviceId();
119     int32_t pinToken = pReq->GetPinToken();
120     int32_t status = pReq->GetStatus();
121     int32_t reason = pReq->GetReason();
122     if (!data.WriteString(pkgName)) {
123         DMLOG(DM_LOG_ERROR, "write pkgName failed");
124         return DEVICEMANAGER_FLATTEN_OBJECT;
125     }
126     if (!data.WriteString(deviceId)) {
127         DMLOG(DM_LOG_ERROR, "write deviceId failed");
128         return DEVICEMANAGER_FLATTEN_OBJECT;
129     }
130     if (!data.WriteInt32(pinToken)) {
131         DMLOG(DM_LOG_ERROR, "write pinToken failed");
132         return DEVICEMANAGER_FLATTEN_OBJECT;
133     }
134     if (!data.WriteInt32(status)) {
135         DMLOG(DM_LOG_ERROR, "write status failed");
136         return DEVICEMANAGER_FLATTEN_OBJECT;
137     }
138     if (!data.WriteInt32(reason)) {
139         DMLOG(DM_LOG_ERROR, "write reason failed");
140         return DEVICEMANAGER_FLATTEN_OBJECT;
141     }
142     return DEVICEMANAGER_OK;
143 }
144 
ON_IPC_READ_RESPONSE(SERVER_AUTH_RESULT,MessageParcel & reply,std::shared_ptr<IpcRsp> pBaseRsp)145 ON_IPC_READ_RESPONSE(SERVER_AUTH_RESULT, MessageParcel &reply, std::shared_ptr<IpcRsp> pBaseRsp)
146 {
147     pBaseRsp->SetErrCode(reply.ReadInt32());
148     return DEVICEMANAGER_OK;
149 }
150 
ON_IPC_SET_REQUEST(SERVER_CHECK_AUTH_RESULT,std::shared_ptr<IpcReq> pBaseReq,MessageParcel & data)151 ON_IPC_SET_REQUEST(SERVER_CHECK_AUTH_RESULT, std::shared_ptr<IpcReq> pBaseReq, MessageParcel &data)
152 {
153     std::shared_ptr<IpcNotifyCheckAuthResultReq> pReq = std::static_pointer_cast<IpcNotifyCheckAuthResultReq>(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     if (!data.WriteString(pkgName)) {
159         DMLOG(DM_LOG_ERROR, "write pkgName failed");
160         return DEVICEMANAGER_FLATTEN_OBJECT;
161     }
162     if (!data.WriteString(deviceId)) {
163         DMLOG(DM_LOG_ERROR, "write deviceId failed");
164         return DEVICEMANAGER_FLATTEN_OBJECT;
165     }
166     if (!data.WriteInt32(result)) {
167         DMLOG(DM_LOG_ERROR, "write result failed");
168         return DEVICEMANAGER_FLATTEN_OBJECT;
169     }
170     if (!data.WriteInt32(flag)) {
171         DMLOG(DM_LOG_ERROR, "write flag failed");
172         return DEVICEMANAGER_FLATTEN_OBJECT;
173     }
174     return DEVICEMANAGER_OK;
175 }
176 
ON_IPC_READ_RESPONSE(SERVER_CHECK_AUTH_RESULT,MessageParcel & reply,std::shared_ptr<IpcRsp> pBaseRsp)177 ON_IPC_READ_RESPONSE(SERVER_CHECK_AUTH_RESULT, MessageParcel &reply, std::shared_ptr<IpcRsp> pBaseRsp)
178 {
179     pBaseRsp->SetErrCode(reply.ReadInt32());
180     return DEVICEMANAGER_OK;
181 }
182 
ON_IPC_CMD(GET_TRUST_DEVICE_LIST,MessageParcel & data,MessageParcel & reply)183 ON_IPC_CMD(GET_TRUST_DEVICE_LIST, MessageParcel &data, MessageParcel &reply)
184 {
185     std::string pkgName = data.ReadString();
186     std::string extra = data.ReadString();
187     DMLOG(DM_LOG_INFO, "pkgName:%s, extra:%s", pkgName.c_str(), extra.c_str());
188     DmDeviceInfo *info = nullptr;
189     int32_t infoNum = 0;
190     int32_t result = IpcServerAdapter::GetInstance().GetTrustedDeviceList(pkgName, extra, &info, &infoNum);
191     reply.WriteInt32(infoNum);
192     if (infoNum > 0 && info != nullptr) {
193         if (!reply.WriteRawData(info, sizeof(DmDeviceInfo) * infoNum)) {
194             DMLOG(DM_LOG_ERROR, "write subscribeInfo failed");
195         }
196         free(info);
197     }
198     if (!reply.WriteInt32(result)) {
199         DMLOG(DM_LOG_ERROR, "write result failed");
200         return DEVICEMANAGER_WRITE_FAILED;
201     }
202     return DEVICEMANAGER_OK;
203 }
204 
ON_IPC_CMD(REGISTER_DEVICE_MANAGER_LISTENER,MessageParcel & data,MessageParcel & reply)205 ON_IPC_CMD(REGISTER_DEVICE_MANAGER_LISTENER, MessageParcel &data, MessageParcel &reply)
206 {
207     std::string pkgName = data.ReadString();
208     sptr<IRemoteObject> listener = data.ReadRemoteObject();
209     int32_t result = IpcServerStub::GetInstance().RegisterDeviceManagerListener(pkgName, listener);
210     if (!reply.WriteInt32(result)) {
211         DMLOG(DM_LOG_ERROR, "write result failed");
212         return DEVICEMANAGER_WRITE_FAILED;
213     }
214     return DEVICEMANAGER_OK;
215 }
216 
ON_IPC_CMD(UNREGISTER_DEVICE_MANAGER_LISTENER,MessageParcel & data,MessageParcel & reply)217 ON_IPC_CMD(UNREGISTER_DEVICE_MANAGER_LISTENER, MessageParcel &data, MessageParcel &reply)
218 {
219     std::string pkgName = data.ReadString();
220     int32_t result = IpcServerStub::GetInstance().UnRegisterDeviceManagerListener(pkgName);
221     if (!reply.WriteInt32(result)) {
222         DMLOG(DM_LOG_ERROR, "write result failed");
223         return DEVICEMANAGER_WRITE_FAILED;
224     }
225     return DEVICEMANAGER_OK;
226 }
227 
ON_IPC_CMD(START_DEVICE_DISCOVER,MessageParcel & data,MessageParcel & reply)228 ON_IPC_CMD(START_DEVICE_DISCOVER, MessageParcel &data, MessageParcel &reply)
229 {
230     std::string pkgName = data.ReadString();
231     DmSubscribeInfo *subscribeInfo = (DmSubscribeInfo *)data.ReadRawData(sizeof(DmSubscribeInfo));
232     int32_t result = DEVICEMANAGER_NULLPTR;
233 
234     if (subscribeInfo != nullptr) {
235         DMLOG(DM_LOG_INFO, "pkgName:%s, subscribeId: %d", pkgName.c_str(), subscribeInfo->subscribeId);
236         result = IpcServerAdapter::GetInstance().StartDeviceDiscovery(pkgName, *subscribeInfo);
237     }
238     if (!reply.WriteInt32(result)) {
239         DMLOG(DM_LOG_ERROR, "write result failed");
240         return DEVICEMANAGER_WRITE_FAILED;
241     }
242     return DEVICEMANAGER_OK;
243 }
244 
ON_IPC_CMD(STOP_DEVICE_DISCOVER,MessageParcel & data,MessageParcel & reply)245 ON_IPC_CMD(STOP_DEVICE_DISCOVER, MessageParcel &data, MessageParcel &reply)
246 {
247     std::string pkgName = data.ReadString();
248     uint16_t subscribeId = data.ReadInt32();
249     DMLOG(DM_LOG_INFO, "pkgName:%s, subscribeId: %d", pkgName.c_str(), subscribeId);
250     int32_t result = IpcServerAdapter::GetInstance().StopDiscovery(pkgName, subscribeId);
251     if (!reply.WriteInt32(result)) {
252         DMLOG(DM_LOG_ERROR, "write result failed");
253         return DEVICEMANAGER_WRITE_FAILED;
254     }
255     return DEVICEMANAGER_OK;
256 }
257 
ON_IPC_CMD(AUTHENTICATE_DEVICE,MessageParcel & data,MessageParcel & reply)258 ON_IPC_CMD(AUTHENTICATE_DEVICE, MessageParcel &data, MessageParcel &reply)
259 {
260     std::string pkgName = data.ReadString();
261     std::string extra = data.ReadString();
262     DmDeviceInfo *deviceInfo = (DmDeviceInfo *)data.ReadRawData(sizeof(DmDeviceInfo));
263     int32_t appIconLen = data.ReadInt32();
264     int32_t appThumbnailLen = data.ReadInt32();
265     uint8_t *appIcon = appIconLen > 0? (uint8_t *)data.ReadRawData(appIconLen) : nullptr;
266     uint8_t *appThumbnail = appThumbnailLen > 0? (uint8_t *)data.ReadRawData(appThumbnailLen) : nullptr;
267 
268     DmAppImageInfo imageInfo(appIcon, appIconLen, appThumbnail, appThumbnailLen);
269     int32_t result = DEVICEMANAGER_OK;
270 
271     if (deviceInfo != nullptr) {
272         result = IpcServerAdapter::GetInstance().AuthenticateDevice(pkgName, *deviceInfo, imageInfo, extra);
273     }
274     if (!reply.WriteInt32(result)) {
275         DMLOG(DM_LOG_ERROR, "write result failed");
276         return DEVICEMANAGER_WRITE_FAILED;
277     }
278     return DEVICEMANAGER_OK;
279 }
280 
ON_IPC_CMD(CHECK_AUTHENTICATION,MessageParcel & data,MessageParcel & reply)281 ON_IPC_CMD(CHECK_AUTHENTICATION, MessageParcel &data, MessageParcel &reply)
282 {
283     std::string authPara = data.ReadString();
284     int32_t result = IpcServerAdapter::GetInstance().CheckAuthentication(authPara);
285     if (!reply.WriteInt32(result)) {
286         DMLOG(DM_LOG_ERROR, "write result failed");
287         return DEVICEMANAGER_WRITE_FAILED;
288     }
289     return DEVICEMANAGER_OK;
290 }
291 
ON_IPC_CMD(SERVER_GET_AUTHENTCATION_INFO,MessageParcel & data,MessageParcel & reply)292 ON_IPC_CMD(SERVER_GET_AUTHENTCATION_INFO, MessageParcel &data, MessageParcel &reply)
293 {
294     std::string packName = data.ReadString();
295     DmAuthParam authParam;
296     int32_t ret = DEVICEMANAGER_OK;
297     DMLOG(DM_LOG_ERROR, "DeviceManagerStub:: GET_AUTHENTCATION_INFO:pkgName:%s", packName.c_str());
298     IpcServerAdapter::GetInstance().GetAuthenticationParam(packName, authParam);
299     if (authParam.direction == AUTH_SESSION_SIDE_CLIENT) {
300         if (!reply.WriteInt32(authParam.direction) || !reply.WriteInt32(authParam.authType) ||
301             !reply.WriteInt32(authParam.pinToken)) {
302             DMLOG(DM_LOG_ERROR, "DeviceManagerStub::wirte client fail");
303             ret = DEVICEMANAGER_WRITE_FAILED;
304         }
305         return ret;
306     }
307 
308     int32_t appIconLen = authParam.imageinfo.GetAppIconLen();
309     int32_t appThumbnailLen = authParam.imageinfo.GetAppThumbnailLen();
310     if (!reply.WriteInt32(authParam.direction) || !reply.WriteInt32(authParam.authType) ||
311         !reply.WriteString(authParam.packageName) || !reply.WriteString(authParam.appName) ||
312         !reply.WriteString(authParam.appDescription) || !reply.WriteInt32(authParam.business) ||
313         !reply.WriteInt32(authParam.pincode) || !reply.WriteInt32(appIconLen) ||
314         !reply.WriteInt32(appThumbnailLen)) {
315         DMLOG(DM_LOG_ERROR, "write reply failed");
316         return DEVICEMANAGER_WRITE_FAILED;
317     }
318 
319     if (appIconLen > 0 && authParam.imageinfo.GetAppIcon() != nullptr) {
320         if (!reply.WriteRawData(authParam.imageinfo.GetAppIcon(), appIconLen)) {
321             DMLOG(DM_LOG_ERROR, "write appIcon failed");
322             return DEVICEMANAGER_WRITE_FAILED;
323         }
324     }
325 
326     if (appThumbnailLen > 0 && authParam.imageinfo.GetAppThumbnail() != nullptr) {
327         if (!reply.WriteRawData(authParam.imageinfo.GetAppThumbnail(), appThumbnailLen)) {
328             DMLOG(DM_LOG_ERROR, "write appThumbnail failed");
329             return DEVICEMANAGER_WRITE_FAILED;
330         }
331     }
332 
333     return DEVICEMANAGER_OK;
334 }
335 
ON_IPC_CMD(SERVER_USER_AUTHORIZATION_OPERATION,MessageParcel & data,MessageParcel & reply)336 ON_IPC_CMD(SERVER_USER_AUTHORIZATION_OPERATION, MessageParcel &data, MessageParcel &reply)
337 {
338     std::string packageName = data.ReadString();
339     int32_t action = data.ReadInt32();
340     int result = IpcServerAdapter::GetInstance().SetUserOperation(packageName, action);
341 
342     if (!reply.WriteInt32(action)) {
343         DMLOG(DM_LOG_ERROR, "write result failed");
344         return DEVICEMANAGER_WRITE_FAILED;
345     }
346     return result;
347 }
348 
ON_IPC_SET_REQUEST(SERVER_DEVICEMANAGER_FA_NOTIFY,std::shared_ptr<IpcReq> pBaseReq,MessageParcel & data)349 ON_IPC_SET_REQUEST(SERVER_DEVICEMANAGER_FA_NOTIFY, std::shared_ptr<IpcReq> pBaseReq, MessageParcel& data)
350 {
351     DMLOG(DM_LOG_INFO, "OnFaCallBack");
352     std::shared_ptr<IpcNotifyDMFAResultReq> pReq = std::static_pointer_cast<IpcNotifyDMFAResultReq>(pBaseReq);
353     std::string packagname = pReq->GetPkgName();
354     std::string paramJson = pReq->GetJsonParam();
355     if (!data.WriteString(packagname)) {
356         DMLOG(DM_LOG_ERROR, "write pkgName failed");
357         return DEVICEMANAGER_FLATTEN_OBJECT;
358     }
359     if (!data.WriteString(paramJson)) {
360         DMLOG(DM_LOG_ERROR, "write paramJson failed");
361         return DEVICEMANAGER_FLATTEN_OBJECT;
362     }
363     return DEVICEMANAGER_OK;
364 }
365 
ON_IPC_READ_RESPONSE(SERVER_DEVICEMANAGER_FA_NOTIFY,MessageParcel & reply,std::shared_ptr<IpcRsp> pBaseRsp)366 ON_IPC_READ_RESPONSE(SERVER_DEVICEMANAGER_FA_NOTIFY, MessageParcel& reply, std::shared_ptr<IpcRsp> pBaseRsp)
367 {
368     pBaseRsp->SetErrCode(reply.ReadInt32());
369     return DEVICEMANAGER_OK;
370 }
371 } // namespace DistributedHardware
372 } // namespace OHOS
373