• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 "msg_response_auth.h"
17 #include "device_manager_log.h"
18 #include "device_manager_errno.h"
19 #include "constants.h"
20 #include "parameter.h"
21 #include "hichain_connector.h"
22 #include "anonymous_string.h"
23 
24 namespace OHOS {
25 namespace DistributedHardware {
MsgResponseAuth(int32_t reply,std::string & reqDeviceId)26 MsgResponseAuth::MsgResponseAuth(int32_t reply, std::string &reqDeviceId)
27 {
28     mHead_ = std::make_shared<MsgHead>(MSG_TYPE_RESP_AUTH);
29     mReply_ = reply;
30     char localDeviceId[DEVICE_UUID_LENGTH] = {0};
31     GetDevUdid(localDeviceId, DEVICE_UUID_LENGTH);
32     mDeviceId_ = localDeviceId;
33     std::vector<GroupInfo> groupList;
34     HichainConnector::GetInstance().GetRelatedGroups(reqDeviceId, groupList);
35     HichainConnector::GetInstance().GetSyncGroupList(groupList, mSyncGroupList_);
36 }
37 
MsgResponseAuth(int32_t reply,int64_t requestId,std::string & groupId,std::string & groupName,std::string & reqDeviceId)38 MsgResponseAuth::MsgResponseAuth(int32_t reply, int64_t requestId, std::string &groupId, std::string &groupName,
39     std::string &reqDeviceId)
40 {
41     mHead_ = std::make_shared<MsgHead>(MSG_TYPE_RESP_AUTH);
42     mReply_ = reply;
43     mNetId_ = "";
44     mGroupId_ = groupId;
45     mGroupName_ = groupName;
46     mRequestId_ = requestId;
47     char localDeviceId[DEVICE_UUID_LENGTH] = {0};
48     GetDevUdid(localDeviceId, DEVICE_UUID_LENGTH);
49     mDeviceId_ = localDeviceId;
50     std::vector<GroupInfo> groupList;
51     HichainConnector::GetInstance().GetRelatedGroups(reqDeviceId, groupList);
52     HichainConnector::GetInstance().GetSyncGroupList(groupList, mSyncGroupList_);
53 }
54 
Encode(nlohmann::json & jsonObj)55 void MsgResponseAuth::Encode(nlohmann::json &jsonObj)
56 {
57     DMLOG(DM_LOG_INFO, "MsgResponseAuth encode started");
58     mHead_->Encode(jsonObj);
59     jsonObj[TAG_REPLY] = mReply_;
60     jsonObj[TAG_DEVICE_ID] = mDeviceId_;
61     jsonObj[TAG_GROUPIDS] = mSyncGroupList_;
62     if (mReply_ == SESSION_REPLY_ACCEPT) {
63         jsonObj[TAG_NET_ID] = mNetId_;
64         jsonObj[TAG_REQUEST_ID] = mRequestId_;
65         jsonObj[TAG_GROUP_ID] = mGroupId_;
66         jsonObj[TAG_GROUP_NAME] = mGroupName_;
67         DMLOG(DM_LOG_INFO, "MsgResponseAuth encode completed");
68     }
69     DMLOG(DM_LOG_INFO, "encode mReply_ is : %d", mReply_);
70     DMLOG(DM_LOG_INFO, "encode deviceId is : %s", GetAnonyString(mDeviceId_).c_str());
71     DMLOG(DM_LOG_INFO, "encode netId is : %s", GetAnonyString(mNetId_).c_str());
72     DMLOG(DM_LOG_INFO, "encode mGroupId_ is %s", GetAnonyString(mGroupId_).c_str());
73     DMLOG(DM_LOG_INFO, "encode mGroupName_ is %s", mGroupName_.c_str());
74     DMLOG(DM_LOG_INFO, "encode mRequestId_ is %d", mRequestId_);
75 }
76 
Decode(nlohmann::json & jsonObj)77 int32_t MsgResponseAuth::Decode(nlohmann::json &jsonObj)
78 {
79     DMLOG(DM_LOG_INFO, "MsgResponseAuth decode started");
80     if (!jsonObj.contains(TAG_REPLY) || !jsonObj.contains(TAG_DEVICE_ID) || !jsonObj.contains(TAG_GROUPIDS)) {
81         DMLOG(DM_LOG_ERROR, "MsgResponseAuth::decode, err json string, first time");
82         return MSG_DECODE_PARA_FAILED;
83     }
84 
85     MsgHead msgHead;
86     mHead_ = msgHead.Decode(jsonObj);
87     mReply_ = jsonObj[TAG_REPLY];
88     mDeviceId_ = jsonObj[TAG_DEVICE_ID];
89     std::vector<std::string> groupList = jsonObj[TAG_GROUPIDS];
90     for (auto str : groupList) {
91         mSyncGroupList_.push_back(str);
92     }
93 
94     if (mReply_ == SESSION_REPLY_ACCEPT) {
95         if (!jsonObj.contains(TAG_NET_ID) || !jsonObj.contains(TAG_GROUP_ID) || !jsonObj.contains(TAG_GROUP_NAME) ||
96             !jsonObj.contains(TAG_REQUEST_ID)) {
97             DMLOG(DM_LOG_ERROR, "MsgResponseAuth::decode, err json string, second time");
98             return MSG_DECODE_PARA_FAILED;
99         }
100         mNetId_ = jsonObj[TAG_NET_ID];
101         mGroupId_ = jsonObj[TAG_GROUP_ID];
102         mGroupName_ = jsonObj[TAG_GROUP_NAME];
103         mRequestId_ = jsonObj[TAG_REQUEST_ID];
104     }
105     DMLOG(DM_LOG_INFO, "decode mReply_ is : %d", mReply_);
106     DMLOG(DM_LOG_INFO, "decode mGroupId_ is %s", GetAnonyString(mGroupId_).c_str());
107     DMLOG(DM_LOG_INFO, "decode mGroupName_ is %s", GetAnonyString(mGroupName_).c_str());
108     DMLOG(DM_LOG_INFO, "decode deviceId is : %s", GetAnonyString(mDeviceId_).c_str());
109     DMLOG(DM_LOG_INFO, "decode netId is : %s", GetAnonyString(mNetId_).c_str());
110     DMLOG(DM_LOG_INFO, "decode mRequestId_ is %d", mRequestId_);
111     DMLOG(DM_LOG_INFO, "MsgResponseAuth decode completed");
112     return DEVICEMANAGER_OK;
113 }
114 
GetReply()115 int32_t MsgResponseAuth::GetReply()
116 {
117     return mReply_;
118 }
119 
GetNetId()120 std::string MsgResponseAuth::GetNetId()
121 {
122     return mNetId_;
123 }
124 
GetGroupId()125 std::string MsgResponseAuth::GetGroupId()
126 {
127     return mGroupId_;
128 }
129 
GetDeviceId()130 std::string MsgResponseAuth::GetDeviceId()
131 {
132     return mDeviceId_;
133 }
134 
GetGroupName()135 std::string MsgResponseAuth::GetGroupName()
136 {
137     return mGroupName_;
138 }
139 
GetRequestId()140 int64_t MsgResponseAuth::GetRequestId()
141 {
142     return mRequestId_;
143 }
144 
GetSyncGroupList()145 std::vector<std::string> MsgResponseAuth::GetSyncGroupList()
146 {
147     return mSyncGroupList_;
148 }
149 
GetPinCode()150 int32_t MsgResponseAuth::GetPinCode()
151 {
152     return mPinCode_;
153 }
154 
SavePinCode(int32_t pinCode)155 void MsgResponseAuth::SavePinCode(int32_t pinCode)
156 {
157     mPinCode_ = pinCode;
158 }
159 }
160 }
161