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