1 /*
2 * Copyright (c) 2021 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 <sstream>
17 #include "nlohmann/json.hpp"
18
19 #include "dm_ability_manager.h"
20 #include "request_session.h"
21 #include "constants.h"
22 #include "device_manager_log.h"
23 #include "msg_codec.h"
24 #include "device_manager_errno.h"
25 #include "ipc_server_adapter.h"
26 #include "encrypt_utils.h"
27 #include "softbus_adapter.h"
28 #include "ipc_server_listener_adapter.h"
29 #include "hichain_connector.h"
30 #include "softbus_session.h"
31
32 namespace OHOS {
33 namespace DistributedHardware {
RequestSession(std::string & hostPkgName,std::string & targetPkgName,const DmDeviceInfo & devReqInfo,const DmAppImageInfo & imageInfo)34 RequestSession::RequestSession(std::string &hostPkgName, std::string &targetPkgName, const DmDeviceInfo &devReqInfo,
35 const DmAppImageInfo &imageInfo)
36 {
37 DMLOG(DM_LOG_INFO, "RequestSession construction started");
38 mDevInfo_ = devReqInfo;
39 mImageInfo_ = imageInfo;
40 mHostPkgName_ = hostPkgName;
41 mTargetPkgName = targetPkgName;
42 mPinToken_ = IpcServerAdapter::GenRandInt(MIN_PIN_TOKEN, MAX_PIN_TOKEN);
43 char randStr[TOKEN_LEN] = {0};
44 bool res = EncryptUtils::MbedtlsGenRandomStr(randStr, sizeof(randStr), false);
45 if (res == false) {
46 DMLOG(DM_LOG_ERROR, "get Random string failed");
47 mToken_ = "";
48 return;
49 }
50 mToken_ = randStr;
51 DMLOG(DM_LOG_INFO, "RequestSession construction completed");
52 }
53
GetRequestCommand(std::string & extrasJson)54 std::vector<std::string> RequestSession::GetRequestCommand(std::string &extrasJson)
55 {
56 return MsgCodec::EncodeReqAppAuth(mToken_, mHostPkgName_, mTargetPkgName, mDevInfo_, mImageInfo_, extrasJson);
57 }
58
GetPinToken()59 int32_t RequestSession::GetPinToken()
60 {
61 return mPinToken_;
62 }
63
IsWaitingForScan()64 bool RequestSession::IsWaitingForScan()
65 {
66 return mStatus_ == StatusType::STATUS_WATING_SCAN_OR_INPUT;
67 }
68
GetToken()69 std::string RequestSession::GetToken()
70 {
71 return mToken_;
72 }
73
IsMyPinToken(int32_t pinToken)74 bool RequestSession::IsMyPinToken(int32_t pinToken)
75 {
76 return pinToken == mPinToken_;
77 }
78
OnReceivePinCode(int32_t pinCode)79 void RequestSession::OnReceivePinCode(int32_t pinCode)
80 {
81 if (mStatus_ != StatusType::STATUS_WATING_SCAN_OR_INPUT) {
82 DMLOG(DM_LOG_ERROR, "mStatus_ is incorrect, mStatus_: %d", mStatus_);
83 return;
84 }
85
86 DMLOG(DM_LOG_INFO, "RequestSession::OnReceivePinCode");
87 responseMsgPtr_->SavePinCode(pinCode);
88 HichainConnector::GetInstance().AddMemeber(mDevInfo_.deviceId, responseMsgPtr_);
89 mStatus_ = StatusType::STATUS_WAITING_ADD_GROUP;
90 }
91
OnUserOperate(int32_t action)92 void RequestSession::OnUserOperate(int32_t action)
93 {
94 if (action == FaAction::USER_OPERATION_TYPE_CANCEL_PINCODE_INPUT) {
95 Release();
96 return;
97 }
98 }
99
GetRequestId()100 int64_t RequestSession::GetRequestId()
101 {
102 return mRequestId_;
103 }
104
GetRequestDeviceId()105 std::string RequestSession::GetRequestDeviceId()
106 {
107 return mRemoteDeviceId_;
108 }
109
StartFaService()110 int32_t RequestSession::StartFaService()
111 {
112 DMLOG(DM_LOG_INFO, "RequestSession::StartFaService in");
113 AbilityStatus status = DmAbilityManager::GetInstance().StartAbility(AbilityRole::ABILITY_ROLE_INITIATIVE);
114 if (status != AbilityStatus::ABILITY_STATUS_SUCCESS) {
115 DMLOG(DM_LOG_ERROR, "RequestSession::StartFaService timeout");
116 return FAIL;
117 }
118 return SUCCESS;
119 }
120
IsFinished()121 bool RequestSession::IsFinished()
122 {
123 if (mStatus_ == StatusType::STATUS_INIT || mStatus_ == StatusType::STATUS_FINISH) {
124 return true;
125 }
126 return false;
127 }
128
IsMyChannelId(long long channelId)129 bool RequestSession::IsMyChannelId(long long channelId)
130 {
131 return channelId == mChannelId_;
132 }
133
OnReceiveMsg(std::string & msg)134 void RequestSession::OnReceiveMsg(std::string &msg)
135 {
136 if (mStatus_ != StatusType::STATUS_WAITING_REPLY) {
137 DMLOG(DM_LOG_ERROR, "StatusType is not waiting reply");
138 return;
139 }
140 int32_t reply = ParseRespMsg(msg);
141 DMLOG(DM_LOG_INFO, "reply is : %d", reply);
142 if (reply == SESSION_REPLY_ACCEPT) {
143 mStatus_ = StatusType::STATUS_WATING_SCAN_OR_INPUT;
144 CloseChannel();
145 } else {
146 Release();
147 }
148 NotifyHostAppAuthResult(reply);
149 }
150
GetHostPkgName()151 std::string RequestSession::GetHostPkgName()
152 {
153 return mHostPkgName_;
154 }
155
GetTargetPkgName()156 std::string RequestSession::GetTargetPkgName()
157 {
158 return mTargetPkgName;
159 }
160
GetSessionType()161 int32_t RequestSession::GetSessionType()
162 {
163 return mSessionType_;
164 }
165
Release()166 void RequestSession::Release()
167 {
168 if (mStatus_ == StatusType::STATUS_FINISH || mStatus_ == StatusType::STATUS_INIT) {
169 DMLOG(DM_LOG_INFO, "session is already closed");
170 return;
171 }
172 DMLOG(DM_LOG_INFO, "close this session");
173 mStatus_ = StatusType::STATUS_FINISH;
174 CloseChannel();
175 }
176
CloseChannel()177 void RequestSession::CloseChannel()
178 {
179 if (mIsChannelOpened_) {
180 SoftbusSession::GetInstance().CloseSession(mChannelId_);
181 DMLOG(DM_LOG_ERROR, "RequestSession:: close the channel");
182 mIsChannelOpened_ = false;
183 }
184 }
185
ParseRespMsg(std::string & msg)186 int32_t RequestSession::ParseRespMsg(std::string &msg)
187 {
188 DMLOG(DM_LOG_INFO, "ParseRespMsg started");
189 auto msgResponseAuthPtr = MsgCodec::DecodeResponseAuth(msg);
190 if (msgResponseAuthPtr == nullptr) {
191 DMLOG(DM_LOG_ERROR, "DecodeResponseAuth error");
192 return SESSION_REPLY_UNKNOWN;
193 }
194 responseMsgPtr_ = msgResponseAuthPtr;
195 int32_t reply = msgResponseAuthPtr->GetReply();
196 mRemoteDeviceId_ = msgResponseAuthPtr->GetDeviceId();
197 if (reply == SESSION_REPLY_ACCEPT) {
198 mRemoteNetId_ = msgResponseAuthPtr->GetNetId();
199 mRemoteGroupId_ = msgResponseAuthPtr->GetGroupId();
200 mRemoteGroupName_ = msgResponseAuthPtr->GetGroupName();
201 mRequestId_ = msgResponseAuthPtr->GetRequestId();
202
203 auto remoteGroupList = msgResponseAuthPtr->GetSyncGroupList();
204 SyncDmPrivateGroup(remoteGroupList);
205 DMLOG(DM_LOG_INFO, "user accepted the auth");
206 }
207 DMLOG(DM_LOG_INFO, "ParseRespMsg completed");
208 return reply;
209 }
210
SetChannelId(long long channelId)211 void RequestSession::SetChannelId(long long channelId)
212 {
213 mChannelId_ = channelId;
214 mIsChannelOpened_ = true;
215 mStatus_ = StatusType::STATUS_WAITING_REPLY;
216 }
217
SyncDmPrivateGroup(std::vector<std::string> & remoteGroupList)218 void RequestSession::SyncDmPrivateGroup(std::vector<std::string> &remoteGroupList)
219 {
220 HichainConnector::GetInstance().SyncGroups(mRemoteDeviceId_, remoteGroupList);
221 DMLOG(DM_LOG_INFO, "RequestSession::syncDmPrivateGroup started");
222 std::vector<std::string> localGroups = {};
223 std::string synGroupMsg = MsgCodec::EncodeSyncGroup(localGroups, mRemoteDeviceId_);
224 SoftbusSession::GetInstance().SendMsg(mChannelId_, synGroupMsg);
225 DMLOG(DM_LOG_INFO, "RequestSession::SyncDmPrivateGroup completed");
226 }
227
NotifyHostAppAuthResult(int32_t errorCode)228 void RequestSession::NotifyHostAppAuthResult(int32_t errorCode)
229 {
230 if (mSessionType_ != SESSION_TYPE_IS_APP_AUTH) {
231 DMLOG(DM_LOG_ERROR, "wrong session type: %d", mSessionType_);
232 return;
233 }
234
235 std::string deviceId = mDevInfo_.deviceId;
236 if (errorCode != SESSION_REPLY_ACCEPT) {
237 IpcServerListenerAdapter::GetInstance().OnAuthResult(mHostPkgName_, deviceId, mPinToken_, FAIL, errorCode);
238 DMLOG(DM_LOG_INFO, "notify host result, errorcode: %d", errorCode);
239 return;
240 }
241
242 if (StartFaService() != SUCCESS) {
243 DMLOG(DM_LOG_INFO, "RequestSession::StartFaService failed");
244 return;
245 }
246 DMLOG(DM_LOG_INFO, "RequestSession::StartFaService success");
247 IpcServerListenerAdapter::GetInstance().OnAuthResult(mHostPkgName_, deviceId, mPinToken_, SUCCESS, errorCode);
248 }
249 }
250 }
251