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 "event_bean.h"
17 #include "log.h"
18 #include "monitor_error.h"
19
20 namespace {
21 constexpr OHOS::HiviewDFX::HiLogLabel LABEL = {LOG_CORE, LOG_DOMAIN_FOUNDATION, "EventBean"};
22 }
23
24 namespace OHOS {
25 namespace Media {
26 namespace MediaMonitor {
27
28 constexpr int MAX_MAP_SIZE = 1000;
29
EventBean()30 EventBean::EventBean() {}
31
EventBean(const ModuleId & mId,const EventId & eId,const EventType & type)32 EventBean::EventBean(const ModuleId &mId, const EventId &eId,
33 const EventType &type)
34 : moduleId_(mId), eventId_(eId), eventType_(type) {}
35
ReadFromParcel(MessageParcel & parcel)36 void EventBean::ReadFromParcel(MessageParcel &parcel)
37 {
38 moduleId_ = static_cast<ModuleId>(parcel.ReadInt32());
39 eventId_ = static_cast<EventId>(parcel.ReadInt32());
40 eventType_ = static_cast<EventType>(parcel.ReadInt32());
41
42 int32_t intMapSize = parcel.ReadInt32();
43 FALSE_RETURN_MSG(intMapSize < MAX_MAP_SIZE,
44 "The size of intMapSize exceeds the maximum value");
45 for (int32_t index = 0; index < intMapSize; index++) {
46 std::string key = parcel.ReadString();
47 int32_t value = parcel.ReadInt32();
48 Add(key, value);
49 }
50
51 int32_t stringMapSize = parcel.ReadInt32();
52 FALSE_RETURN_MSG(stringMapSize < MAX_MAP_SIZE,
53 "The size of stringMapSize exceeds the maximum value");
54 for (int32_t index = 0; index < stringMapSize; index++) {
55 std::string key = parcel.ReadString();
56 std::string value = parcel.ReadString();
57 Add(key, value);
58 }
59
60 int32_t uint64MapSize = parcel.ReadInt32();
61 FALSE_RETURN_MSG(uint64MapSize < MAX_MAP_SIZE,
62 "The size of uint64MapSize exceeds the maximum value");
63 for (int32_t index = 0; index < uint64MapSize; index++) {
64 std::string key = parcel.ReadString();
65 uint64_t value = parcel.ReadUint64();
66 Add(key, value);
67 }
68
69 int32_t floatMapSize = parcel.ReadInt32();
70 FALSE_RETURN_MSG(floatMapSize < MAX_MAP_SIZE,
71 "The size of floatMapSize exceeds the maximum value");
72 for (int32_t index = 0; index < floatMapSize; index++) {
73 std::string key = parcel.ReadString();
74 float value = parcel.ReadFloat();
75 Add(key, value);
76 }
77 }
78
Marshalling(Parcel & parcel) const79 bool EventBean::Marshalling(Parcel &parcel) const
80 {
81 FALSE_RETURN_V_MSG_E(parcel.WriteInt32(moduleId_), false, "write moduleId failed");
82 FALSE_RETURN_V_MSG_E(parcel.WriteInt32(eventId_), false, "write eventId failed");
83 FALSE_RETURN_V_MSG_E(parcel.WriteInt32(eventType_), false, "write eventId failed");
84
85 FALSE_RETURN_V_MSG_E(intMap_.size() < MAX_MAP_SIZE, false,
86 "The size of intMap_ exceeds the maximum value");
87 FALSE_RETURN_V_MSG_E(parcel.WriteInt32(intMap_.size()), false, "write intMap.size() failed");
88 for (auto &it : intMap_) {
89 FALSE_RETURN_V_MSG_E(parcel.WriteString(it.first), false, "intMap failed to WriteString for key");
90 FALSE_RETURN_V_MSG_E(parcel.WriteInt32(it.second), false, "intMap failed to WriteInt32 for value");
91 }
92
93 FALSE_RETURN_V_MSG_E(stringMap_.size() < MAX_MAP_SIZE, false,
94 "The size of stringMap_ exceeds the maximum value");
95 FALSE_RETURN_V_MSG_E(parcel.WriteInt32(stringMap_.size()), false, "write stringMap.size() failed");
96 for (auto &it : stringMap_) {
97 FALSE_RETURN_V_MSG_E(parcel.WriteString(it.first), false, "stringMap failed to WriteString for key");
98 FALSE_RETURN_V_MSG_E(parcel.WriteString(it.second), false, "stringMap failed to WriteString for value");
99 }
100
101 FALSE_RETURN_V_MSG_E(uint64Map_.size() < MAX_MAP_SIZE, false,
102 "The size of uint64Map_ exceeds the maximum value");
103 FALSE_RETURN_V_MSG_E(parcel.WriteInt32(uint64Map_.size()), false, "write uint64Map.size() failed");
104 for (auto &it : uint64Map_) {
105 FALSE_RETURN_V_MSG_E(parcel.WriteString(it.first), false, "uint64Map failed to WriteString for key");
106 FALSE_RETURN_V_MSG_E(parcel.WriteUint64(it.second), false, "uint64Map failed to WriteInt32 for value");
107 }
108
109 FALSE_RETURN_V_MSG_E(floatMap_.size() < MAX_MAP_SIZE, false,
110 "The size of floatMap_ exceeds the maximum value");
111 FALSE_RETURN_V_MSG_E(parcel.WriteInt32(floatMap_.size()), false, "write floatMap.size() failed");
112 for (auto &it : floatMap_) {
113 FALSE_RETURN_V_MSG_E(parcel.WriteString(it.first), false, "floatMap failed to WriteString for key");
114 FALSE_RETURN_V_MSG_E(parcel.WriteFloat(it.second), false, "floatMap failed to WriteInt32 for value");
115 }
116 return true;
117 }
118
Unmarshalling(Parcel & data)119 EventBean *EventBean::Unmarshalling(Parcel &data)
120 {
121 EventBean *eventBean = new (std::nothrow) EventBean();
122 FALSE_RETURN_V_MSG_E(eventBean != nullptr, nullptr, "Create failed.");
123 MessageParcel *parcelIn = static_cast<MessageParcel*>(&data);
124 eventBean->ReadFromParcel(*parcelIn);
125 return eventBean;
126 }
127
Add(const std::string & key,int32_t value)128 void EventBean::Add(const std::string &key, int32_t value)
129 {
130 intMap_.emplace(key, value);
131 }
132
Add(const std::string & key,std::string value)133 void EventBean::Add(const std::string &key, std::string value)
134 {
135 stringMap_.emplace(key, value);
136 }
137
Add(const std::string & key,uint64_t value)138 void EventBean::Add(const std::string &key, uint64_t value)
139 {
140 uint64Map_.emplace(key, value);
141 }
142
Add(const std::string & key,float value)143 void EventBean::Add(const std::string &key, float value)
144 {
145 floatMap_.emplace(key, value);
146 }
147
GetIntMap()148 std::map<std::string, int32_t> EventBean::GetIntMap()
149 {
150 return intMap_;
151 }
152
GetStringMap()153 std::map<std::string, std::string> EventBean::GetStringMap()
154 {
155 return stringMap_;
156 }
157
GetUint64Map()158 std::map<std::string, uint64_t> EventBean::GetUint64Map()
159 {
160 return uint64Map_;
161 }
162
GetFloatMap()163 std::map<std::string, float> EventBean::GetFloatMap()
164 {
165 return floatMap_;
166 }
167
GetModuleId()168 ModuleId EventBean::GetModuleId()
169 {
170 return moduleId_;
171 }
GetEventId()172 EventId EventBean::GetEventId()
173 {
174 return eventId_;
175 }
176
GetEventType()177 EventType EventBean::GetEventType()
178 {
179 return eventType_;
180 }
181
SetModuleId(const ModuleId & mId)182 void EventBean::SetModuleId(const ModuleId &mId)
183 {
184 moduleId_ = mId;
185 }
186
SetEventId(const EventId & eId)187 void EventBean::SetEventId(const EventId &eId)
188 {
189 eventId_ = eId;
190 }
191
SetEventType(const EventType & type)192 void EventBean::SetEventType(const EventType &type)
193 {
194 eventType_ = type;
195 }
196
GetIntValue(const std::string & key)197 int32_t EventBean::GetIntValue(const std::string &key)
198 {
199 if (intMap_.find(key) != intMap_.end()) {
200 return intMap_.find(key)->second;
201 } else {
202 return -1;
203 }
204 }
205
GetStringValue(const std::string & key)206 std::string EventBean::GetStringValue(const std::string &key)
207 {
208 if (stringMap_.find(key) != stringMap_.end()) {
209 return stringMap_.find(key)->second;
210 } else {
211 return "UNKNOWN";
212 }
213 }
214
GetUint64Value(const std::string & key)215 uint64_t EventBean::GetUint64Value(const std::string &key)
216 {
217 if (uint64Map_.find(key) != uint64Map_.end()) {
218 return uint64Map_.find(key)->second;
219 } else {
220 return 0;
221 }
222 }
223
GetFloatValue(const std::string & key)224 float EventBean::GetFloatValue(const std::string &key)
225 {
226 if (floatMap_.find(key) != floatMap_.end()) {
227 return floatMap_.find(key)->second;
228 } else {
229 return 0;
230 }
231 }
232
UpdateIntMap(const std::string & key,int32_t value)233 void EventBean::UpdateIntMap(const std::string &key, int32_t value)
234 {
235 if (intMap_.find(key) != intMap_.end()) {
236 intMap_[key] = value;
237 }
238 }
239
UpdateStringMap(const std::string & key,std::string value)240 void EventBean::UpdateStringMap(const std::string &key, std::string value)
241 {
242 if (stringMap_.find(key) != stringMap_.end()) {
243 stringMap_[key] = value;
244 }
245 }
246
UpdateUint64Map(const std::string & key,uint64_t value)247 void EventBean::UpdateUint64Map(const std::string &key, uint64_t value)
248 {
249 if (uint64Map_.find(key) != uint64Map_.end()) {
250 uint64Map_[key] = value;
251 }
252 }
253
UpdateFloatMap(const std::string & key,float value)254 void EventBean::UpdateFloatMap(const std::string &key, float value)
255 {
256 if (floatMap_.find(key) != floatMap_.end()) {
257 floatMap_[key] = value;
258 }
259 }
260 } // namespace MediaMonitor
261 } // namespace Media
262 } // namespace OHOS