-
Notifications
You must be signed in to change notification settings - Fork 58
Expand file tree
/
Copy pathComponentEvents.scala
More file actions
340 lines (326 loc) · 11.6 KB
/
ComponentEvents.scala
File metadata and controls
340 lines (326 loc) · 11.6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
package fpp.compiler.codegen
import fpp.compiler.analysis._
import fpp.compiler.ast._
import fpp.compiler.codegen._
/** Writes out C++ for component events */
case class ComponentEvents (
s: CppWriterState,
aNode: Ast.Annotated[AstNode[Ast.DefComponent]]
) extends ComponentCppWriterUtils(s, aNode) {
def getConstantMembers: List[CppDoc.Class.Member] = {
val throttleEnum =
if throttledEvents.isEmpty then Nil
else List(
Line.blank :: lines(
s"//! Event throttle values: sets initial value of countdown variables"
),
wrapInEnum(
throttledEvents.flatMap((_, event) =>
writeEnumConstant(
eventThrottleConstantName(event.getName),
event.throttle.get,
Some(s"Throttle reset count for ${event.getName}")
)
)
)
).flatten
if !hasEvents then Nil
else List(
linesClassMember(
List(
Line.blank :: lines(s"//! Event IDs"),
wrapInEnum(
sortedEvents.flatMap((id, event) =>
writeEnumConstant(
eventIdConstantName(event.getName),
id,
AnnotationCppWriter.asStringOpt(event.aNode),
CppWriterUtils.Hex
)
)
),
throttleEnum
).flatten
)
)
}
def getFunctionMembers: List[CppDoc.Class.Member] = {
List(
getLoggingFunctions,
getThrottleFunctions
).flatten
}
def getVariableMembers: List[CppDoc.Class.Member] = {
addAccessTagAndComment(
"PRIVATE",
"Counter values for event throttling",
throttledEvents.map((_, event) =>
linesClassMember(
Line.blank :: lines(
s"""|//! Throttle for ${event.getName}
|FwIndexType ${eventThrottleCounterName(event.getName)};
|"""
)
)
),
CppDoc.Lines.Hpp
)
}
private def getLoggingFunctions: List[CppDoc.Class.Member] = {
def writeLogBody(id: Event.Id, event: Event) =
line("// Emit the event on the log port") ::
wrapInIf(
s"this->${portVariableName(eventPort.get)}[0].isConnected()",
intersperseBlankLines(
List(
List.concat(
lines("Fw::LogBuffer _logBuff;"),
eventParamTypeMap(id) match {
case Nil => Nil
case _ => lines("Fw::SerializeStatus _status = Fw::FW_SERIALIZE_OK;")
}
),
List.concat(
lines("#if FW_AMPCS_COMPATIBLE"),
eventParamTypeMap(id) match {
case Nil => lines("Fw::SerializeStatus _status = Fw::FW_SERIALIZE_OK;\n")
case _ => Nil
},
event.aNode._2.data.severity match {
case Ast.SpecEvent.Fatal if eventParamTypeMap(id).nonEmpty => lines(
s"""|// Serialize the number of arguments
|_status = _logBuff.serialize(static_cast<U8>(${eventParamTypeMap(id).length} + 1));
|FW_ASSERT(
| _status == Fw::FW_SERIALIZE_OK,
| static_cast<FwAssertArgType>(_status)
|);
|
|// For FATAL, add stack size of 4 and a dummy entry. No support for stacks yet.
|_status = _logBuff.serialize(static_cast<U8>(4));
|FW_ASSERT(
| _status == Fw::FW_SERIALIZE_OK,
| static_cast<FwAssertArgType>(_status)
|);
|
|_status = _logBuff.serialize(static_cast<U32>(0));
|FW_ASSERT(
| _status == Fw::FW_SERIALIZE_OK,
| static_cast<FwAssertArgType>(_status)
|);
|"""
)
case _ => lines(
s"""|// Serialize the number of arguments
|_status = _logBuff.serialize(static_cast<U8>(${eventParamTypeMap(id).length}));
|FW_ASSERT(
| _status == Fw::FW_SERIALIZE_OK,
| static_cast<FwAssertArgType>(_status)
|);
|"""
)
},
lines("#endif")
),
intersperseBlankLines(
eventParamTypeMap(id).map((name, typeName, ty) => {
List.concat(
ty.getUnderlyingType match {
case t: Type.String =>
val serialSize = writeStringSize(s, t)
lines(
s"_status = $name.serialize(_logBuff, FW_MIN(FW_LOG_STRING_MAX_SIZE, $serialSize));"
)
case t => lines(
s"""|#if FW_AMPCS_COMPATIBLE
|// Serialize the argument size
|_status = _logBuff.serialize(
| static_cast<U8>(${writeSerializedSizeExpr(s, t, typeName)})
|);
|FW_ASSERT(
| _status == Fw::FW_SERIALIZE_OK,
| static_cast<FwAssertArgType>(_status)
|);
|#endif
|_status = _logBuff.serialize($name);
|"""
)
},
lines(
"""|FW_ASSERT(
| _status == Fw::FW_SERIALIZE_OK,
| static_cast<FwAssertArgType>(_status)
|);
|"""
)
)
})
),
lines(
s"""|this->${portVariableName(eventPort.get)}[0].invoke(
| _id,
| _logTime,
| Fw::LogSeverity::${writeSeverity(event)},
| _logBuff
|);
|"""
)
)
)
)
def writeTextLogBody(event: Event) = List.concat(
lines(
s"""|// Emit the event on the text log port
|#if FW_ENABLE_TEXT_LOGGING
|"""
),
wrapInIf(
s"this->${portVariableName(textEventPort.get)}[0].isConnected()",
intersperseBlankLines(
List(
lines(
s"""|#if FW_OBJECT_NAMES == 1
|const char* _formatString =
| "(%s) %s: ${writeEventFormat(event)}";
|#else
|const char* _formatString =
| "%s: ${writeEventFormat(event)}";
|#endif
|"""
),
event.aNode._2.data.params.flatMap(param =>
s.a.typeMap(param._2.data.typeName.id) match {
case Type.String(_) => Nil
case t if s.isPrimitive(t, writeFormalParamType(param._2.data)) => Nil
case _ => lines(
s"""|Fw::String ${param._2.data.name}Str;
|${param._2.data.name}.toString(${param._2.data.name}Str);
|"""
)
}
),
List.concat(
lines(
s"""|Fw::TextLogString _logString;
|_logString.format(
| _formatString,
|#if FW_OBJECT_NAMES == 1
| this->m_objName.toChar(),
|#endif
|"""
),
lines(
(s"\"${event.getName} \"" ::
event.aNode._2.data.params.map(param => {
val name = param._2.data.name
s.a.typeMap(param._2.data.typeName.id) match {
case Type.String(_) => s"$name.toChar()"
case t if s.isPrimitive(t, writeFormalParamType(param._2.data)) =>
promoteF32ToF64 (t) (name)
case _ => s"${name}Str.toChar()"
}
})).mkString(",\n")
).map(indentIn),
lines(");")
),
lines(
s"""|this->${portVariableName(textEventPort.get)}[0].invoke(
| _id,
| _logTime,
| Fw::LogSeverity::${writeSeverity(event)},
| _logString
|);
|"""
)
)
)
),
lines("#endif")
)
def writeBody(id: Event.Id, event: Event) = intersperseBlankLines(
List(
event.throttle match {
case None => Nil
case Some(_) => lines(
s"""|// Check throttle value
|if (this->${eventThrottleCounterName(event.getName)} >= ${eventThrottleConstantName(event.getName)}) {
| return;
|}
|else {
| this->${eventThrottleCounterName(event.getName)}++;
|}
|"""
)
},
lines(
s"""|// Get the time
|Fw::Time _logTime;
|if (this->${portVariableName(timeGetPort.get)}[0].isConnected()) {
| this->${portVariableName(timeGetPort.get)}[0].invoke(_logTime);
|}
|
|FwEventIdType _id = static_cast<FwEventIdType>(0);
|
|_id = this->getIdBase() + ${eventIdConstantName(event.getName)};
|"""
),
writeLogBody(id, event),
writeTextLogBody(event)
)
)
addAccessTagAndComment(
"PROTECTED",
"Event logging functions",
sortedEvents.map((id, event) =>
functionClassMember(
Some(
addSeparatedString(
s"Log event ${event.getName}",
AnnotationCppWriter.asStringOpt(event.aNode)
)
),
eventLogName(event),
formalParamsCppWriter.write(
event.aNode._2.data.params,
"Fw::StringBase",
FormalParamsCppWriter.Value
),
CppDoc.Type("void"),
writeBody(id, event),
CppDoc.Function.NonSV,
event.throttle match {
case Some(_) => CppDoc.Function.NonConst
case None => CppDoc.Function.Const
}
)
)
)
}
private def getThrottleFunctions: List[CppDoc.Class.Member] = {
addAccessTagAndComment(
"PROTECTED",
"Event throttle reset functions",
throttledEvents.map((_, event) =>
functionClassMember(
Some(s"Reset throttle value for ${event.getName}"),
eventThrottleResetName(event),
Nil,
CppDoc.Type("void"),
lines(
s"""|// Reset throttle counter
|this->${eventThrottleCounterName(event.getName)} = 0;
|"""
)
)
)
)
}
// Get the name for an event throttle constant
private def eventThrottleConstantName(name: String) =
s"${eventIdConstantName(name)}_THROTTLE"
// Get the name for an event logging function
private def eventLogName(event: Event) =
s"log_${writeSeverity(event)}_${event.getName}"
// Get the name for an event throttle reset function
private def eventThrottleResetName(event: Event) =
s"${eventLogName(event)}_ThrottleClear"
}