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