• 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 "command_dispatch.h"
17 #include "dm_log.h"
18 #include "dm_constants.h"
19 #include "securec.h"
20 #include "dm_device_info.h"
21 #include "dm_subscribe_info.h"
22 #include "ipc_get_trustdevice_req.h"
23 #include "ipc_start_discovery_req.h"
24 #include "ipc_stop_discovery_req.h"
25 #include "ipc_set_useroperation_req.h"
26 #include "ipc_authenticate_device_req.h"
27 #include "ipc_verify_authenticate_req.h"
28 #include "ipc_get_trustdevice_rsp.h"
29 #include "device_manager_service.h"
30 #include "ipc_get_local_device_info_rsp.h"
31 #include "ipc_get_info_by_network_rsp.h"
32 #include "ipc_get_info_by_network_req.h"
33 #include "ipc_unauthenticate_device_req.h"
34 #include "ipc_get_dmfaparam_rsp.h"
35 
36 namespace OHOS {
37 namespace DistributedHardware {
38 IMPLEMENT_SINGLE_INSTANCE(CommandDispatch);
39 
MessageSendCmd(int32_t cmdCode,const std::shared_ptr<IpcReq> & req,const std::shared_ptr<IpcRsp> & rsp)40 int32_t CommandDispatch::MessageSendCmd(int32_t cmdCode, const std::shared_ptr<IpcReq> &req,
41                                         const std::shared_ptr<IpcRsp> &rsp)
42 {
43     if (req == nullptr || rsp == nullptr) {
44         LOGE("Message req or rsp is null.");
45         return ERR_DM_INPUT_PARA_INVALID;
46     }
47     uint32_t i = 0;
48     for (i = 0; i < (sizeof(g_cmdMap) / sizeof(g_cmdMap[0])); i++) {
49         if (g_cmdMap[i].cmdCode == cmdCode) {
50             break;
51         }
52     }
53     int32_t ret = g_cmdMap[i].MsgProcess(req, rsp);
54     if (ret != DM_OK) {
55         LOGE("MessageSendCmd Failed with ret: %d", ret);
56         return ret;
57     }
58     return DM_OK;
59 }
60 
AddPkgName(const std::string & pkgName)61 void CommandDispatch::AddPkgName(const std::string &pkgName)
62 {
63     dmPkgName_.push_back(pkgName);
64 }
65 
DeletePkgName(const std::string & pkgName)66 void CommandDispatch::DeletePkgName(const std::string &pkgName)
67 {
68     dmPkgName_.remove(pkgName);
69 }
70 
GetPkgNameList() const71 const std::list<std::string>& CommandDispatch::GetPkgNameList() const
72 {
73     return dmPkgName_;
74 }
75 
GetTrustedDeviceList(const std::shared_ptr<IpcReq> & req,const std::shared_ptr<IpcRsp> & rsp)76 static int32_t GetTrustedDeviceList(const std::shared_ptr<IpcReq> &req, const std::shared_ptr<IpcRsp> &rsp)
77 {
78     std::shared_ptr<IpcGetTrustDeviceReq> pReq = std::static_pointer_cast<IpcGetTrustDeviceReq>(req);
79     std::shared_ptr<IpcGetTrustDeviceRsp> prsp = std::static_pointer_cast<IpcGetTrustDeviceRsp>(rsp);
80     std::string pkgName = pReq->GetPkgName();
81     std::string extra = pReq->GetExtra();
82 
83     LOGI("enter GetTrustedDeviceList");
84     std::vector<DmDeviceInfo> deviceList;
85     int32_t ret = DeviceManagerService::GetInstance().GetTrustedDeviceList(pkgName, extra, deviceList);
86     prsp->SetDeviceVec(deviceList);
87     prsp->SetErrCode(ret);
88     return ret;
89 }
90 
GetLocalDeviceInfo(const std::shared_ptr<IpcReq> & req,const std::shared_ptr<IpcRsp> & rsp)91 static int32_t GetLocalDeviceInfo(const std::shared_ptr<IpcReq> &req, const std::shared_ptr<IpcRsp> &rsp)
92 {
93     std::shared_ptr<IpcGetLocalDeviceInfoRsp> pRsp = std::static_pointer_cast<IpcGetLocalDeviceInfoRsp>(rsp);
94     DmDeviceInfo dmDeviceInfo = {0};
95     int32_t ret = DeviceManagerService::GetInstance().GetLocalDeviceInfo(dmDeviceInfo);
96     DmDeviceInfo *Info = &dmDeviceInfo;
97     if (Info != nullptr) {
98         pRsp->SetLocalDeviceInfo(dmDeviceInfo);
99     }
100     pRsp->SetErrCode(ret);
101     return ret;
102 }
103 
GetUdidByNetworkId(const std::shared_ptr<IpcReq> & req,const std::shared_ptr<IpcRsp> & rsp)104 static int32_t GetUdidByNetworkId(const std::shared_ptr<IpcReq> &req, const std::shared_ptr<IpcRsp> &rsp)
105 {
106     std::shared_ptr<IpcGetInfoByNetWorkReq> pReq = std::static_pointer_cast<IpcGetInfoByNetWorkReq>(req);
107     std::shared_ptr<IpcGetInfoByNetWorkRsp> pRsp = std::static_pointer_cast<IpcGetInfoByNetWorkRsp>(rsp);
108     std::string pkgName = pReq->GetPkgName();
109     std::string netWorkId = pReq->GetNetWorkId();
110     std::string udid;
111 
112     int32_t ret = DeviceManagerService::GetInstance().GetUdidByNetworkId(pkgName, netWorkId, udid);
113     pRsp->SetUdid(udid);
114     pRsp->SetErrCode(ret);
115     return ret;
116 }
117 
GetUuidByNetworkId(const std::shared_ptr<IpcReq> & req,const std::shared_ptr<IpcRsp> & rsp)118 static int32_t GetUuidByNetworkId(const std::shared_ptr<IpcReq> &req, const std::shared_ptr<IpcRsp> &rsp)
119 {
120     std::shared_ptr<IpcGetInfoByNetWorkReq> pReq = std::static_pointer_cast<IpcGetInfoByNetWorkReq>(req);
121     std::shared_ptr<IpcGetInfoByNetWorkRsp> pRsp = std::static_pointer_cast<IpcGetInfoByNetWorkRsp>(rsp);
122     std::string pkgName = pReq->GetPkgName();
123     std::string netWorkId = pReq->GetNetWorkId();
124     std::string uuid;
125 
126     int32_t ret = DeviceManagerService::GetInstance().GetUuidByNetworkId(pkgName, netWorkId, uuid);
127     pRsp->SetUuid(uuid);
128     pRsp->SetErrCode(ret);
129     return ret;
130 }
131 
StartDeviceDiscovery(const std::shared_ptr<IpcReq> & req,const std::shared_ptr<IpcRsp> & rsp)132 static int32_t StartDeviceDiscovery(const std::shared_ptr<IpcReq> &req, const std::shared_ptr<IpcRsp> &rsp)
133 {
134     std::shared_ptr<IpcStartDiscoveryReq> pReq = std::static_pointer_cast<IpcStartDiscoveryReq>(req);
135     std::string pkgName = pReq->GetPkgName();
136     std::string extra = pReq->GetExtra();
137     const DmSubscribeInfo dmSubscribeInfo = pReq->GetSubscribeInfo();
138     LOGI("StartDeviceDiscovery service");
139 
140     int32_t ret = DeviceManagerService::GetInstance().StartDeviceDiscovery(pkgName, dmSubscribeInfo, extra);
141     rsp->SetErrCode(ret);
142     return ret;
143 }
144 
StopDeviceDiscovery(const std::shared_ptr<IpcReq> & req,const std::shared_ptr<IpcRsp> & rsp)145 static int32_t StopDeviceDiscovery(const std::shared_ptr<IpcReq> &req, const std::shared_ptr<IpcRsp> &rsp)
146 {
147     LOGI("StopDeviceDiscovery service");
148     std::shared_ptr<IpcStopDiscoveryReq> pReq = std::static_pointer_cast<IpcStopDiscoveryReq>(req);
149     std::string pkgName = pReq->GetPkgName();
150     uint16_t subscribeId = pReq->GetSubscribeId();
151 
152     int32_t ret = DeviceManagerService::GetInstance().StopDeviceDiscovery(pkgName, subscribeId);
153     rsp->SetErrCode(ret);
154     return ret;
155 }
156 
SetUserOperation(const std::shared_ptr<IpcReq> & req,const std::shared_ptr<IpcRsp> & rsp)157 static int32_t SetUserOperation(const std::shared_ptr<IpcReq> &req, const std::shared_ptr<IpcRsp> &rsp)
158 {
159     std::shared_ptr<IpcGetOperationReq> pReq = std::static_pointer_cast<IpcGetOperationReq>(req);
160     std::string pkgName = pReq->GetPkgName();
161     int32_t action = pReq->GetOperation();
162 
163     LOGI("enter server user authorization operation.");
164 
165     int32_t ret = DeviceManagerService::GetInstance().SetUserOperation(pkgName, action);
166     rsp->SetErrCode(ret);
167     return ret;
168 }
169 
GetFaParam(const std::shared_ptr<IpcReq> & req,const std::shared_ptr<IpcRsp> & rsp)170 static int32_t GetFaParam(const std::shared_ptr<IpcReq> &req, const std::shared_ptr<IpcRsp> &rsp)
171 {
172     std::shared_ptr<IpcReq> pReq = std::static_pointer_cast<IpcReq>(req);
173     std::shared_ptr<IpcGetDmFaParamRsp> pRsp = std::static_pointer_cast<IpcGetDmFaParamRsp>(rsp);
174     std::string pkgName = pReq->GetPkgName();
175     DmAuthParam authParam = {
176         .authToken = "",
177         .packageName = "",
178         .appName = "",
179         .appDescription = "",
180         .authType = 0,
181         .business = 0,
182         .pincode = 0,
183         .direction = 0,
184         .pinToken = 0
185     };
186 
187     LOGI("DeviceManagerStub::GET_AUTHENTCATION_INFO:pkgName:%s", pkgName.c_str());
188 
189     int32_t ret = DeviceManagerService::GetInstance().GetFaParam(pkgName, authParam);
190     pRsp->SetDmAuthParam(authParam);
191     pRsp->SetErrCode(ret);
192     return ret;
193 }
194 
AuthenticateDevice(const std::shared_ptr<IpcReq> & req,const std::shared_ptr<IpcRsp> & rsp)195 static int32_t AuthenticateDevice(const std::shared_ptr<IpcReq> &req, const std::shared_ptr<IpcRsp> &rsp)
196 {
197     std::shared_ptr<IpcAuthenticateDeviceReq> pReq = std::static_pointer_cast<IpcAuthenticateDeviceReq>(req);
198     std::string pkgName = pReq->GetPkgName();
199     std::string extra = pReq->GetExtra();
200     DmDeviceInfo deviceInfo = pReq->GetDeviceInfo();
201     int32_t authType = pReq->GetAuthType();
202     std::string deviceId = deviceInfo.deviceId;
203 
204     LOGI("DeviceManagerStub::AUTHENTCATION_DEVICE:pkgName:%s", pkgName.c_str());
205 
206     int32_t ret = DeviceManagerService::GetInstance().AuthenticateDevice(pkgName, authType, deviceId, extra);
207     rsp->SetErrCode(ret);
208     return ret;
209 }
210 
UnAuthenticateDevice(const std::shared_ptr<IpcReq> & req,const std::shared_ptr<IpcRsp> & rsp)211 static int32_t UnAuthenticateDevice(const std::shared_ptr<IpcReq> &req, const std::shared_ptr<IpcRsp> &rsp)
212 {
213     std::shared_ptr<IpcUnAuthenticateDeviceReq> pReq = std::static_pointer_cast<IpcUnAuthenticateDeviceReq>(req);
214     std::string pkgName = pReq->GetPkgName();
215     DmDeviceInfo deviceInfo = pReq->GetDeviceInfo();
216     std::string deviceId = deviceInfo.deviceId;
217 
218     LOGI("enter server user authorization operation.");
219     int32_t ret = DeviceManagerService::GetInstance().UnAuthenticateDevice(pkgName, deviceId);
220     rsp->SetErrCode(ret);
221     return ret;
222 }
223 
VerifyAuthentication(const std::shared_ptr<IpcReq> & req,const std::shared_ptr<IpcRsp> & rsp)224 static int32_t VerifyAuthentication(const std::shared_ptr<IpcReq> &req, const std::shared_ptr<IpcRsp> &rsp)
225 {
226     std::shared_ptr<IpcVerifyAuthenticateReq> pReq = std::static_pointer_cast<IpcVerifyAuthenticateReq>(req);
227     std::string pkgName = pReq->GetPkgName();
228     std::string authParam = pReq->GetAuthPara();
229 
230     LOGI("DeviceManagerStub::VERIFY_AUTHENTCATION:pkgName:%s", pkgName.c_str());
231 
232     int32_t ret = DeviceManagerService::GetInstance().VerifyAuthentication(authParam);
233     rsp->SetErrCode(ret);
234     return ret;
235 }
236 } // namespace DistributedHardware
237 } // namespace OHOS
238