1 /*
2 * Copyright (c) 2024 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 "soft_bus_message.h"
17
18 #define LOG_TAG "USER_AUTH_SA"
19 namespace OHOS {
20 namespace UserIam {
21 namespace UserAuth {
SoftBusMessage(int32_t messageSeq,const std::string & connectionName,const std::string & srcEndPoint,const std::string & destEndPoint,const std::shared_ptr<Attributes> & attributes)22 SoftBusMessage::SoftBusMessage(int32_t messageSeq, const std::string &connectionName,
23 const std::string &srcEndPoint, const std::string &destEndPoint,
24 const std::shared_ptr<Attributes> &attributes)
25 : messageSeq_(messageSeq), connectionName_(connectionName), srcEndPoint_(srcEndPoint),
26 destEndPoint_(destEndPoint), attributes_(attributes)
27 {
28 IAM_LOGD("start");
29 }
30
GetMessageSeq()31 uint32_t SoftBusMessage::GetMessageSeq()
32 {
33 return messageSeq_;
34 }
35
GetMessageVersion()36 uint32_t SoftBusMessage::GetMessageVersion()
37 {
38 return messageVersion_;
39 }
40
GetAckFlag()41 uint32_t SoftBusMessage::GetAckFlag()
42 {
43 return isAck_;
44 }
45
GetAttributes()46 std::shared_ptr<Attributes> SoftBusMessage::GetAttributes()
47 {
48 return attributes_;
49 }
50
GetSrcEndPoint()51 std::string SoftBusMessage::GetSrcEndPoint()
52 {
53 return srcEndPoint_;
54 }
55
GetDestEndPoint()56 std::string SoftBusMessage::GetDestEndPoint()
57 {
58 return destEndPoint_;
59 }
60
GetConnectionName()61 std::string SoftBusMessage::GetConnectionName()
62 {
63 return connectionName_;
64 }
65
CreateMessage(bool isAck)66 std::shared_ptr<Attributes> SoftBusMessage::CreateMessage(bool isAck)
67 {
68 IAM_LOGD("start.");
69 IF_FALSE_LOGE_AND_RETURN_VAL(attributes_ != nullptr, nullptr);
70
71 auto attributes = Common::MakeShared<Attributes>(attributes_->Serialize());
72 if (attributes == nullptr) {
73 IAM_LOGE("attributes create fail");
74 return nullptr;
75 }
76
77 bool ret = attributes->SetInt32Value(Attributes::ATTR_MSG_SEQ_NUM, messageSeq_);
78 IF_FALSE_LOGE_AND_RETURN_VAL(ret, nullptr);
79
80 if (isAck == true) {
81 ret = attributes->SetBoolValue(Attributes::ATTR_MSG_ACK, true);
82 IF_FALSE_LOGE_AND_RETURN_VAL(ret, nullptr);
83 } else {
84 ret = attributes->SetBoolValue(Attributes::ATTR_MSG_ACK, false);
85 IF_FALSE_LOGE_AND_RETURN_VAL(ret, nullptr);
86 }
87
88 ret = attributes->SetStringValue(Attributes::ATTR_MSG_SRC_END_POINT, srcEndPoint_);
89 IF_FALSE_LOGE_AND_RETURN_VAL(ret, nullptr);
90
91 ret = attributes->SetStringValue(Attributes::ATTR_MSG_DEST_END_POINT, destEndPoint_);
92 IF_FALSE_LOGE_AND_RETURN_VAL(ret, nullptr);
93
94 ret = attributes->SetStringValue(Attributes::ATTR_CONNECTION_NAME, connectionName_);
95 IF_FALSE_LOGE_AND_RETURN_VAL(ret, nullptr);
96
97 ret = attributes->SetUint32Value(Attributes::ATTR_MSG_VERSION, DEFAULT_MESSAGE_VERSION);
98 IF_FALSE_LOGE_AND_RETURN_VAL(ret, nullptr);
99
100 int32_t msgType = -1;
101 attributes->GetInt32Value(Attributes::ATTR_MSG_TYPE, msgType); // ATTR_MSG_TYPE may be empty
102
103 std::string udid;
104 bool getLocalUdidRet = DeviceManagerUtil::GetInstance().GetLocalDeviceUdid(udid);
105 IF_FALSE_LOGE_AND_RETURN_VAL(getLocalUdidRet, nullptr);
106
107 ret = attributes->SetStringValue(Attributes::ATTR_MSG_SRC_UDID, udid);
108 IF_FALSE_LOGE_AND_RETURN_VAL(ret, nullptr);
109
110 IAM_LOGI("CreateMessage success: messageVersion:%{public}u, messageSeq:%{public}u, connectionName:%{public}s, "
111 "msgType:%{public}d, isAck:%{public}d, srcEndPoint:%{public}s, destEndPoint:%{public}s",
112 DEFAULT_MESSAGE_VERSION, messageSeq_, connectionName_.c_str(), msgType, isAck, srcEndPoint_.c_str(),
113 destEndPoint_.c_str());
114
115 return attributes;
116 }
117
ParseMessage(void * message,uint32_t messageLen)118 std::shared_ptr<Attributes> SoftBusMessage::ParseMessage(void *message, uint32_t messageLen)
119 {
120 IAM_LOGD("start.");
121 if (message == nullptr || messageLen == 0) {
122 IAM_LOGE("ParseMessage fail");
123 return nullptr;
124 }
125
126 std::vector<uint8_t> data(static_cast<char *>(message), static_cast<char *>(message) + messageLen);
127 auto attributes = Common::MakeShared<Attributes>(data);
128 if (attributes == nullptr) {
129 IAM_LOGE("attributes create fail");
130 return nullptr;
131 }
132
133 bool ret = attributes->GetUint32Value(Attributes::ATTR_MSG_SEQ_NUM, messageSeq_);
134 IF_FALSE_LOGE_AND_RETURN_VAL(ret, nullptr);
135
136 ret = attributes->GetBoolValue(Attributes::ATTR_MSG_ACK, isAck_);
137 IF_FALSE_LOGE_AND_RETURN_VAL(ret, nullptr);
138
139 ret = attributes->GetStringValue(Attributes::ATTR_MSG_SRC_END_POINT, srcEndPoint_);
140 IF_FALSE_LOGE_AND_RETURN_VAL(ret, nullptr);
141
142 ret = attributes->GetStringValue(Attributes::ATTR_MSG_DEST_END_POINT, destEndPoint_);
143 IF_FALSE_LOGE_AND_RETURN_VAL(ret, nullptr);
144
145 ret = attributes->GetStringValue(Attributes::ATTR_CONNECTION_NAME, connectionName_);
146 IF_FALSE_LOGE_AND_RETURN_VAL(ret, nullptr);
147
148 ret = attributes->GetUint32Value(Attributes::ATTR_MSG_VERSION, messageVersion_);
149 IF_FALSE_LOGE_AND_RETURN_VAL(ret, nullptr);
150
151 int32_t msgType = -1;
152 attributes->GetInt32Value(Attributes::ATTR_MSG_TYPE, msgType); // ATTR_MSG_TYPE may be empty
153 attributes_ = attributes;
154
155 IAM_LOGI("ParseMessage success: messageVersion:%{public}u messageSeq:%{public}u, connectionName:%{public}s, "
156 "msgType:%{public}d, isAck:%{public}d, srcEndPoint:%{public}s, destEndPoint:%{public}s",
157 messageVersion_, messageSeq_, connectionName_.c_str(), msgType, isAck_, srcEndPoint_.c_str(),
158 destEndPoint_.c_str());
159 return attributes;
160 }
161 } // namespace UserAuth
162 } // namespace UserIam
163 } // namespace OHOS