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