• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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_LOCAL_DEVICE_NETWORKID,IpcIo & req,IpcIo & reply)133 ON_IPC_SERVER_CMD(GET_LOCAL_DEVICE_NETWORKID, IpcIo &req, IpcIo &reply)
134 {
135     LOGI("enter GetLocalDeviceNetworkId.");
136     (void)req;
137     std::string networkId;
138     int32_t ret = DeviceManagerService::GetInstance().GetLocalDeviceNetworkId(networkId);
139     WriteString(&reply, networkId.c_str());
140     WriteInt32(&reply, ret);
141 }
142 
ON_IPC_SERVER_CMD(GET_LOCAL_DEVICEID,IpcIo & req,IpcIo & reply)143 ON_IPC_SERVER_CMD(GET_LOCAL_DEVICEID, IpcIo &req, IpcIo &reply)
144 {
145     LOGI("enter GetLocalDeviceId.");
146     std::string pkgName = (const char *)ReadString(&req, nullptr);
147     std::string deviceId;
148     int32_t ret = DeviceManagerService::GetInstance().GetLocalDeviceId(pkgName, deviceId);
149     WriteString(&reply, deviceId.c_str());
150     WriteInt32(&reply, ret);
151 }
152 
ON_IPC_SERVER_CMD(GET_LOCAL_DEVICE_NAME,IpcIo & req,IpcIo & reply)153 ON_IPC_SERVER_CMD(GET_LOCAL_DEVICE_NAME, IpcIo &req, IpcIo &reply)
154 {
155     LOGI("enter GetLocalDeviceName.");
156     std::string deviceName;
157     int32_t ret = DeviceManagerService::GetInstance().GetLocalDeviceName(deviceName);
158     WriteString(&reply, deviceName.c_str());
159     WriteInt32(&reply, ret);
160 }
161 
ON_IPC_SERVER_CMD(GET_LOCAL_DEVICE_TYPE,IpcIo & req,IpcIo & reply)162 ON_IPC_SERVER_CMD(GET_LOCAL_DEVICE_TYPE, IpcIo &req, IpcIo &reply)
163 {
164     LOGI("enter GetLocalDeviceType.");
165     int32_t deviceType = 0;
166     int32_t ret = DeviceManagerService::GetInstance().GetLocalDeviceType(deviceType);
167     WriteInt32(&reply, deviceType);
168     WriteInt32(&reply, ret);
169 }
170 
ON_IPC_SERVER_CMD(GET_DEVICE_INFO,IpcIo & req,IpcIo & reply)171 ON_IPC_SERVER_CMD(GET_DEVICE_INFO, IpcIo &req, IpcIo &reply)
172 {
173     LOGI("enter GetDeviceInfo.");
174     std::string pkgName = (const char*)ReadString(&req, nullptr);
175     std::string networkId = (const char*)ReadString(&req, nullptr);
176     DmDeviceInfo deviceInfo;
177     int32_t ret = DeviceManagerService::GetInstance().GetDeviceInfo(networkId, deviceInfo);
178     EncodeDmDeviceInfo(deviceInfo, reply);
179     WriteInt32(&reply, ret);
180 }
181 
ON_IPC_SERVER_CMD(GET_AVAILABLE_DEVICE_LIST,IpcIo & req,IpcIo & reply)182 ON_IPC_SERVER_CMD(GET_AVAILABLE_DEVICE_LIST, IpcIo &req, IpcIo &reply)
183 {
184     LOGI("enter get available device list.");
185     std::string pkgName = (const char *)ReadString(&req, nullptr);
186     std::vector<DmDeviceBasicInfo> deviceList;
187     int32_t ret = DeviceManagerService::GetInstance().GetAvailableDeviceList(pkgName, deviceList);
188     WriteInt32(&reply, ret);
189     WriteInt32(&reply, deviceList.size());
190     if (ret == DM_OK && deviceList.size() > 0) {
191         WriteRawData(&reply, deviceList.data(), sizeof(DmDeviceBasicInfo) * deviceList.size());
192     }
193 }
194 
ON_IPC_SERVER_CMD(GET_TRUST_DEVICE_LIST,IpcIo & req,IpcIo & reply)195 ON_IPC_SERVER_CMD(GET_TRUST_DEVICE_LIST, IpcIo &req, IpcIo &reply)
196 {
197     LOGI("enter get trust device list.");
198     std::string pkgName = (const char *)ReadString(&req, nullptr);
199     std::string extra = (const char *)ReadString(&req, nullptr);
200     std::vector<DmDeviceInfo> deviceList;
201     int32_t ret = DeviceManagerService::GetInstance().GetTrustedDeviceList(pkgName, extra, deviceList);
202     WriteInt32(&reply, ret);
203     WriteInt32(&reply, deviceList.size());
204     if (ret == DM_OK && deviceList.size() > 0) {
205         for (const auto &devInfo : deviceList) {
206             if (!EncodeDmDeviceInfo(devInfo, reply)) {
207                 LOGE("write dm device info failed");
208             }
209         }
210     }
211 }
212 
ON_IPC_SERVER_CMD(START_DEVICE_DISCOVERY,IpcIo & req,IpcIo & reply)213 ON_IPC_SERVER_CMD(START_DEVICE_DISCOVERY, IpcIo &req, IpcIo &reply)
214 {
215     LOGI("StartDeviceDiscovery service listener.");
216     std::string pkgName = (const char *)ReadString(&req, nullptr);
217     std::string extra = (const char *)ReadString(&req, nullptr);
218     uint16_t subscribeId = 0;
219     ReadUint16(&req, &subscribeId);
220     int32_t ret = DeviceManagerService::GetInstance().StartDeviceDiscovery(pkgName, subscribeId, extra);
221     WriteInt32(&reply, ret);
222 }
223 
ON_IPC_SERVER_CMD(STOP_DEVICE_DISCOVER,IpcIo & req,IpcIo & reply)224 ON_IPC_SERVER_CMD(STOP_DEVICE_DISCOVER, IpcIo &req, IpcIo &reply)
225 {
226     LOGI("StopDeviceDiscovery service listener.");
227     std::string pkgName = (const char *)ReadString(&req, nullptr);
228     uint16_t subscribeId = 0;
229     ReadUint16(&req, &subscribeId);
230     int32_t ret = DeviceManagerService::GetInstance().StopDeviceDiscovery(pkgName, subscribeId);
231     WriteInt32(&reply, ret);
232 }
233 
ON_IPC_SERVER_CMD(REQUEST_CREDENTIAL,IpcIo & req,IpcIo & reply)234 ON_IPC_SERVER_CMD(REQUEST_CREDENTIAL, IpcIo &req, IpcIo &reply)
235 {
236     LOGI("request credential service listener.");
237     std::string pkgName = (const char *)ReadString(&req, nullptr);
238     std::string reqParaStr = (const char *)ReadString(&req, nullptr);
239     std::map<std::string, std::string> requestParam;
240     ParseMapFromJsonString(reqParaStr, requestParam);
241     std::string returnJsonStr;
242     int32_t ret = DM_OK;
243     if (requestParam[DM_CREDENTIAL_TYPE] == DM_TYPE_MINE) {
244         DeviceManagerService::GetInstance().MineRequestCredential(pkgName, returnJsonStr);
245     }
246     WriteInt32(&reply, ret);
247     if (ret == DM_OK) {
248         WriteString(&reply, returnJsonStr.c_str());
249     }
250 }
251 
ON_IPC_SERVER_CMD(SERVER_GET_DMFA_INFO,IpcIo & req,IpcIo & reply)252 ON_IPC_SERVER_CMD(SERVER_GET_DMFA_INFO, IpcIo &req, IpcIo &reply)
253 {
254     LOGI("check credential service listener.");
255     std::string pkgName = (const char *)ReadString(&req, nullptr);
256     std::string reqJsonStr = (const char *)ReadString(&req, nullptr);
257     std::string returnJsonStr;
258     int32_t ret = DeviceManagerService::GetInstance().CheckCredential(pkgName, reqJsonStr, returnJsonStr);
259     WriteInt32(&reply, ret);
260     if (ret == DM_OK) {
261         WriteString(&reply, returnJsonStr.c_str());
262     }
263 }
264 
ON_IPC_SERVER_CMD(IMPORT_CREDENTIAL,IpcIo & req,IpcIo & reply)265 ON_IPC_SERVER_CMD(IMPORT_CREDENTIAL, IpcIo &req, IpcIo &reply)
266 {
267     LOGI("import credential service listener.");
268     std::string pkgName = (const char *)ReadString(&req, nullptr);
269     std::string reqParaStr = (const char *)ReadString(&req, nullptr);
270     std::map<std::string, std::string> requestParam;
271     ParseMapFromJsonString(reqParaStr, requestParam);
272     std::string returnJsonStr;
273     std::string outParamStr;
274     int32_t ret = DM_OK;
275     if (requestParam[DM_CREDENTIAL_TYPE] == DM_TYPE_MINE) {
276         DeviceManagerService::GetInstance().ImportCredential(pkgName, requestParam[DM_CREDENTIAL_REQJSONSTR],
277                                                              returnJsonStr);
278         std::map<std::string, std::string> outputResult;
279         outputResult.emplace(DM_CREDENTIAL_TYPE, DM_TYPE_MINE);
280         outputResult.emplace(DM_CREDENTIAL_RETURNJSONSTR, returnJsonStr);
281         outParamStr = ConvertMapToJsonString(outputResult);
282     }
283     WriteInt32(&reply, ret);
284     if (ret == DM_OK) {
285         WriteString(&reply, outParamStr.c_str());
286     }
287 }
288 
ON_IPC_SERVER_CMD(DELETE_CREDENTIAL,IpcIo & req,IpcIo & reply)289 ON_IPC_SERVER_CMD(DELETE_CREDENTIAL, IpcIo &req, IpcIo &reply)
290 {
291     LOGI("import credential service listener.");
292     std::string pkgName = (const char *)ReadString(&req, nullptr);
293     std::string reqParaStr = (const char *)ReadString(&req, nullptr);
294     std::map<std::string, std::string> requestParam;
295     ParseMapFromJsonString(reqParaStr, requestParam);
296     std::string returnJsonStr;
297     std::string outParamStr;
298     int32_t ret = DM_OK;
299     if (requestParam[DM_CREDENTIAL_TYPE] == DM_TYPE_MINE) {
300         DeviceManagerService::GetInstance().DeleteCredential(pkgName, requestParam[DM_CREDENTIAL_REQJSONSTR],
301                                                              returnJsonStr);
302         std::map<std::string, std::string> outputResult;
303         outputResult.emplace(DM_CREDENTIAL_TYPE, DM_TYPE_MINE);
304         outputResult.emplace(DM_CREDENTIAL_RETURNJSONSTR, returnJsonStr);
305         outParamStr = ConvertMapToJsonString(outputResult);
306     }
307     WriteInt32(&reply, ret);
308     if (ret == DM_OK) {
309         WriteString(&reply, outParamStr.c_str());
310     }
311 }
312 } // namespace DistributedHardware
313 } // namespace OHOS
314