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_anonymous.h"
19 #include "dm_constants.h"
20 #include "dm_log.h"
21 #include "ipc_cmd_register.h"
22 #include "ipc_def.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_server_stub.h"
27 
28 namespace OHOS {
29 namespace DistributedHardware {
SetRspErrCode(IpcIo & reply,std::shared_ptr<IpcRsp> pBaseRsp)30 int32_t SetRspErrCode(IpcIo &reply, std::shared_ptr<IpcRsp> pBaseRsp)
31 {
32     if (pBaseRsp == nullptr) {
33         LOGE("pBaseRsp is null");
34         return ERR_DM_FAILED;
35     }
36     int32_t ret = 0;
37     ReadInt32(&reply, &ret);
38     pBaseRsp->SetErrCode(ret);
39     return DM_OK;
40 }
41 
EncodeDmDeviceInfo(const DmDeviceInfo & devInfo,IpcIo & reply)42 bool EncodeDmDeviceInfo(const DmDeviceInfo &devInfo, IpcIo &reply)
43 {
44     bool bRet = true;
45     std::string deviceIdStr(devInfo.deviceId);
46     bRet = (bRet && WriteString(&reply, deviceIdStr.c_str()));
47     std::string deviceNameStr(devInfo.deviceName);
48     bRet = (bRet && WriteString(&reply, deviceNameStr.c_str()));
49     bRet = (bRet && WriteUint16(&reply, devInfo.deviceTypeId));
50     std::string networkIdStr(devInfo.networkId);
51     bRet = (bRet && WriteString(&reply, networkIdStr.c_str()));
52     bRet = (bRet && WriteInt32(&reply, devInfo.range));
53     bRet = (bRet && WriteInt32(&reply, devInfo.networkType));
54     bRet = (bRet && WriteInt32(&reply, devInfo.authForm));
55     bRet = (bRet && WriteString(&reply, devInfo.extraData.c_str()));
56     return bRet;
57 }
58 
ON_IPC_SET_REQUEST(SERVER_DEVICE_STATE_NOTIFY,std::shared_ptr<IpcReq> pBaseReq,IpcIo & request,uint8_t * buffer,size_t buffLen)59 ON_IPC_SET_REQUEST(SERVER_DEVICE_STATE_NOTIFY, std::shared_ptr<IpcReq> pBaseReq, IpcIo &request, uint8_t *buffer,
60                    size_t buffLen)
61 {
62     std::shared_ptr<IpcNotifyDeviceStateReq> pReq = std::static_pointer_cast<IpcNotifyDeviceStateReq>(pBaseReq);
63     std::string pkgName = pReq->GetPkgName();
64     int32_t deviceState = pReq->GetDeviceState();
65     DmDeviceInfo deviceInfo = pReq->GetDeviceInfo();
66 
67     IpcIoInit(&request, buffer, buffLen, 0);
68     WriteString(&request, pkgName.c_str());
69     WriteInt32(&request, deviceState);
70     EncodeDmDeviceInfo(deviceInfo, request);
71     return DM_OK;
72 }
73 
ON_IPC_READ_RESPONSE(SERVER_DEVICE_STATE_NOTIFY,IpcIo & reply,std::shared_ptr<IpcRsp> pBaseRsp)74 ON_IPC_READ_RESPONSE(SERVER_DEVICE_STATE_NOTIFY, IpcIo &reply, std::shared_ptr<IpcRsp> pBaseRsp)
75 {
76     return SetRspErrCode(reply, pBaseRsp);
77 }
78 
ON_IPC_SET_REQUEST(SERVER_DEVICE_FOUND,std::shared_ptr<IpcReq> pBaseReq,IpcIo & request,uint8_t * buffer,size_t buffLen)79 ON_IPC_SET_REQUEST(SERVER_DEVICE_FOUND, std::shared_ptr<IpcReq> pBaseReq, IpcIo &request, uint8_t *buffer,
80                    size_t buffLen)
81 {
82     std::shared_ptr<IpcNotifyDeviceFoundReq> pReq = std::static_pointer_cast<IpcNotifyDeviceFoundReq>(pBaseReq);
83     std::string pkgName = pReq->GetPkgName();
84     uint16_t subscribeId = pReq->GetSubscribeId();
85     DmDeviceInfo deviceInfo = pReq->GetDeviceInfo();
86 
87     IpcIoInit(&request, buffer, buffLen, 0);
88     WriteString(&request, pkgName.c_str());
89     WriteUint16(&request, subscribeId);
90     EncodeDmDeviceInfo(deviceInfo, request);
91     return DM_OK;
92 }
93 
ON_IPC_READ_RESPONSE(SERVER_DEVICE_FOUND,IpcIo & reply,std::shared_ptr<IpcRsp> pBaseRsp)94 ON_IPC_READ_RESPONSE(SERVER_DEVICE_FOUND, IpcIo &reply, std::shared_ptr<IpcRsp> pBaseRsp)
95 {
96     return SetRspErrCode(reply, pBaseRsp);
97 }
98 
ON_IPC_SET_REQUEST(SERVER_DISCOVER_FINISH,std::shared_ptr<IpcReq> pBaseReq,IpcIo & request,uint8_t * buffer,size_t buffLen)99 ON_IPC_SET_REQUEST(SERVER_DISCOVER_FINISH, std::shared_ptr<IpcReq> pBaseReq, IpcIo &request, uint8_t *buffer,
100                    size_t buffLen)
101 {
102     std::shared_ptr<IpcNotifyDiscoverResultReq> pReq = std::static_pointer_cast<IpcNotifyDiscoverResultReq>(pBaseReq);
103     std::string pkgName = pReq->GetPkgName();
104     uint16_t subscribeId = pReq->GetSubscribeId();
105     int32_t result = pReq->GetResult();
106 
107     IpcIoInit(&request, buffer, buffLen, 0);
108     WriteString(&request, pkgName.c_str());
109     WriteUint16(&request, subscribeId);
110     WriteInt32(&request, result);
111     return DM_OK;
112 }
113 
ON_IPC_READ_RESPONSE(SERVER_DISCOVER_FINISH,IpcIo & reply,std::shared_ptr<IpcRsp> pBaseRsp)114 ON_IPC_READ_RESPONSE(SERVER_DISCOVER_FINISH, IpcIo &reply, std::shared_ptr<IpcRsp> pBaseRsp)
115 {
116     return SetRspErrCode(reply, pBaseRsp);
117 }
118 
ON_IPC_SERVER_CMD(REGISTER_DEVICE_MANAGER_LISTENER,IpcIo & req,IpcIo & reply)119 ON_IPC_SERVER_CMD(REGISTER_DEVICE_MANAGER_LISTENER, IpcIo &req, IpcIo &reply)
120 {
121     LOGI("start to register device manager service listener.");
122     int32_t errCode = RegisterDeviceManagerListener(&req, &reply);
123     WriteInt32(&reply, errCode);
124 }
125 
ON_IPC_SERVER_CMD(UNREGISTER_DEVICE_MANAGER_LISTENER,IpcIo & req,IpcIo & reply)126 ON_IPC_SERVER_CMD(UNREGISTER_DEVICE_MANAGER_LISTENER, IpcIo &req, IpcIo &reply)
127 {
128     LOGI("start to unregister device manager service listener.");
129     int32_t errCode = UnRegisterDeviceManagerListener(&req, &reply);
130     WriteInt32(&reply, errCode);
131 }
132 
ON_IPC_SERVER_CMD(GET_DEVICE_INFO,IpcIo & req,IpcIo & reply)133 ON_IPC_SERVER_CMD(GET_DEVICE_INFO, IpcIo &req, IpcIo &reply)
134 {
135     LOGI("enter GetDeviceInfo.");
136     std::string pkgName = (const char*)ReadString(&req, nullptr);
137     std::string networkId = (const char*)ReadString(&req, nullptr);
138     DmDeviceInfo deviceInfo;
139     int32_t ret = DeviceManagerService::GetInstance().GetDeviceInfo(networkId, deviceInfo);
140     EncodeDmDeviceInfo(deviceInfo, reply);
141     WriteInt32(&reply, ret);
142 }
143 
ON_IPC_SERVER_CMD(GET_TRUST_DEVICE_LIST,IpcIo & req,IpcIo & reply)144 ON_IPC_SERVER_CMD(GET_TRUST_DEVICE_LIST, IpcIo &req, IpcIo &reply)
145 {
146     LOGI("enter get trust device list.");
147     std::string pkgName = (const char *)ReadString(&req, nullptr);
148     std::string extra = (const char *)ReadString(&req, nullptr);
149     std::vector<DmDeviceInfo> deviceList;
150     int32_t ret = DeviceManagerService::GetInstance().GetTrustedDeviceList(pkgName, extra, deviceList);
151     WriteInt32(&reply, ret);
152     WriteInt32(&reply, deviceList.size());
153     if (ret == DM_OK && deviceList.size() > 0) {
154         for (const auto &devInfo : deviceList) {
155             if (!EncodeDmDeviceInfo(devInfo, reply)) {
156                 LOGE("write dm device info failed");
157             }
158         }
159     }
160 }
161 
ON_IPC_SERVER_CMD(START_DEVICE_DISCOVERY,IpcIo & req,IpcIo & reply)162 ON_IPC_SERVER_CMD(START_DEVICE_DISCOVERY, IpcIo &req, IpcIo &reply)
163 {
164     LOGI("StartDeviceDiscovery service listener.");
165     std::string pkgName = (const char *)ReadString(&req, nullptr);
166     std::string extra = (const char *)ReadString(&req, nullptr);
167     uint16_t subscribeId = 0;
168     ReadUint16(&req, &subscribeId);
169     int32_t ret = DeviceManagerService::GetInstance().StartDeviceDiscovery(pkgName, subscribeId, extra);
170     WriteInt32(&reply, ret);
171 }
172 
ON_IPC_SERVER_CMD(STOP_DEVICE_DISCOVER,IpcIo & req,IpcIo & reply)173 ON_IPC_SERVER_CMD(STOP_DEVICE_DISCOVER, IpcIo &req, IpcIo &reply)
174 {
175     LOGI("StopDeviceDiscovery service listener.");
176     std::string pkgName = (const char *)ReadString(&req, nullptr);
177     uint16_t subscribeId = 0;
178     ReadUint16(&req, &subscribeId);
179     int32_t ret = DeviceManagerService::GetInstance().StopDeviceDiscovery(pkgName, subscribeId);
180     WriteInt32(&reply, ret);
181 }
182 
ON_IPC_SERVER_CMD(REQUEST_CREDENTIAL,IpcIo & req,IpcIo & reply)183 ON_IPC_SERVER_CMD(REQUEST_CREDENTIAL, IpcIo &req, IpcIo &reply)
184 {
185     LOGI("request credential service listener.");
186     std::string pkgName = (const char *)ReadString(&req, nullptr);
187     std::string reqParaStr = (const char *)ReadString(&req, nullptr);
188     std::map<std::string, std::string> requestParam;
189     ParseMapFromJsonString(reqParaStr, requestParam);
190     std::string returnJsonStr;
191     int32_t ret = DM_OK;
192     if (requestParam[DM_CREDENTIAL_TYPE] == DM_TYPE_MINE) {
193         DeviceManagerService::GetInstance().MineRequestCredential(pkgName, returnJsonStr);
194     }
195     WriteInt32(&reply, ret);
196     if (ret == DM_OK) {
197         WriteString(&reply, returnJsonStr.c_str());
198     }
199 }
200 
ON_IPC_SERVER_CMD(SERVER_GET_DMFA_INFO,IpcIo & req,IpcIo & reply)201 ON_IPC_SERVER_CMD(SERVER_GET_DMFA_INFO, IpcIo &req, IpcIo &reply)
202 {
203     LOGI("check credential service listener.");
204     std::string pkgName = (const char *)ReadString(&req, nullptr);
205     std::string reqJsonStr = (const char *)ReadString(&req, nullptr);
206     std::string returnJsonStr;
207     int32_t ret = DeviceManagerService::GetInstance().CheckCredential(pkgName, reqJsonStr, returnJsonStr);
208     WriteInt32(&reply, ret);
209     if (ret == DM_OK) {
210         WriteString(&reply, returnJsonStr.c_str());
211     }
212 }
213 
ON_IPC_SERVER_CMD(IMPORT_CREDENTIAL,IpcIo & req,IpcIo & reply)214 ON_IPC_SERVER_CMD(IMPORT_CREDENTIAL, IpcIo &req, IpcIo &reply)
215 {
216     LOGI("import credential service listener.");
217     std::string pkgName = (const char *)ReadString(&req, nullptr);
218     std::string reqParaStr = (const char *)ReadString(&req, nullptr);
219     std::map<std::string, std::string> requestParam;
220     ParseMapFromJsonString(reqParaStr, requestParam);
221     std::string returnJsonStr;
222     std::string outParamStr;
223     int32_t ret = DM_OK;
224     if (requestParam[DM_CREDENTIAL_TYPE] == DM_TYPE_MINE) {
225         DeviceManagerService::GetInstance().ImportCredential(pkgName, requestParam[DM_CREDENTIAL_REQJSONSTR],
226                                                              returnJsonStr);
227         std::map<std::string, std::string> outputResult;
228         outputResult.emplace(DM_CREDENTIAL_TYPE, DM_TYPE_MINE);
229         outputResult.emplace(DM_CREDENTIAL_RETURNJSONSTR, returnJsonStr);
230         outParamStr = ConvertMapToJsonString(outputResult);
231     }
232     WriteInt32(&reply, ret);
233     if (ret == DM_OK) {
234         WriteString(&reply, outParamStr.c_str());
235     }
236 }
237 
ON_IPC_SERVER_CMD(DELETE_CREDENTIAL,IpcIo & req,IpcIo & reply)238 ON_IPC_SERVER_CMD(DELETE_CREDENTIAL, IpcIo &req, IpcIo &reply)
239 {
240     LOGI("import credential service listener.");
241     std::string pkgName = (const char *)ReadString(&req, nullptr);
242     std::string reqParaStr = (const char *)ReadString(&req, nullptr);
243     std::map<std::string, std::string> requestParam;
244     ParseMapFromJsonString(reqParaStr, requestParam);
245     std::string returnJsonStr;
246     std::string outParamStr;
247     int32_t ret = DM_OK;
248     if (requestParam[DM_CREDENTIAL_TYPE] == DM_TYPE_MINE) {
249         DeviceManagerService::GetInstance().DeleteCredential(pkgName, requestParam[DM_CREDENTIAL_REQJSONSTR],
250                                                              returnJsonStr);
251         std::map<std::string, std::string> outputResult;
252         outputResult.emplace(DM_CREDENTIAL_TYPE, DM_TYPE_MINE);
253         outputResult.emplace(DM_CREDENTIAL_RETURNJSONSTR, returnJsonStr);
254         outParamStr = ConvertMapToJsonString(outputResult);
255     }
256     WriteInt32(&reply, ret);
257     if (ret == DM_OK) {
258         WriteString(&reply, outParamStr.c_str());
259     }
260 }
261 } // namespace DistributedHardware
262 } // namespace OHOS
263