-
Notifications
You must be signed in to change notification settings - Fork 8
/
CoojaTraceReference.html
565 lines (424 loc) · 14.5 KB
/
CoojaTraceReference.html
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
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
<html>
<style>
h1, h2, h3 { margin-bottom: 0}
h2, h3 { margin-top:20px }
pre { background-color: #EEEEEE }
tt { color: #1111CC; background-color: #EEEEFF}
tt.es {color: #AA2200; background-color: #FFEEEE }
</style>
<h1>CoojaTrace Reference</h1>
<p>
CoojaTrace allows you to use observables (Signals and EventStreams) in your simulation for assertions and logging.
</p>
<h2>Simulation and Motes</h2>
<p>The Cooja simulation object:</p>
<pre>sim</pre>
<p>All motes in a map with mote ID as key:</p>
<pre>sim.motes</pre>
<p>A specific mote:</p>
<pre>sim.motes(5) // get mote with ID 5</pre>
<p>All motes in a list:</p>
<pre>sim.motes.values</pre>
<p>Run code for all current motes and all motes <strong>added during simulation</strong>:</p>
<pre>
for(mote <- sim.allMotes) {
// code (for example log/assert calls)
}
</pre>
<p>Current Simulation time (in microseconds, not a signal):</p>
<pre>sim.time</pre>
<h2>Signals</h2>
<p>Transform a signal by applying a function to every value:</p>
<pre>
// multiply every value by 42
signal.map(_ * 42)
// turn every value c into a tuple by
// calling c.name and c.address
signal.map(c => (c.name, c.address) )
// alternative syntax
for(c <- signal) yield (c * 42)
</pre>
<p>Turn an event stream into a signal:</p>
<pre>stream.hold(initalValue)</pre>
<p>Signal reflecting only distinct changes of original signal:</p>
<pre>signal.distinct</pre>
<p>Create a constant signal:</p>
<pre>Val(value)</pre>
<p>Execute a function/block of code for each change:</p>
<pre>
signal.foreach(function) // function receives new value as argument
for(change <- signal) {
// code (can access new value in variable change)
}
// run code if any signal changes
for(c1 <- signal1; c2 <-signal2; ...) {
// code (can access new values in variables c1, c2, ...)
}
</pre>
<p>Current value of a signal:</p>
<pre>
signal.now
</pre>
<h3>"Magic" signals:</h3>
<p>
To simplify your test code, you can treat a signal like its value and CoojaTrace will automatically map your expression to a mapped signal.<br />
<em>Note:</em> There is a slight performance decrease, as your entire expression is re-evaluated at every change. So use the explicit .map(...) syntax for signals that change very often (i.e. stackpointers).
</p>
<pre>
// works like stringSignal.map(_.length)
stringSignal.length
// works like listSignal.map(_.head)
listSignal.head
// works like
// for(a <- signalA; b <- signalB; c <- signalC) yield ((a+b)<c)
( (signalA + signalB) < signalC )
// works like Val(mote.toString)
mote.toString
</pre>
<h3>Checking equality:</h3>
<p>
Unlike all other comparison operators, the equals operator (==) is defined for all Scala objects, including signals.
This means that comparing signals using == does <em>not</em> compare the signal <em>values</em>, but the signals themselves. To compare the values, use the === operator instead. To check inequality, use the =!= operator.
</p>
<pre>
sig1 = Var(1)
sig2 = Var(1)
// does NOT compare signal values
sig1 == sig2 // returns false
sig1 != sig2 // returns true
// does compare signal values
sig1 === sig2 // returns Signal[Boolean] (which is true so far)
sig1 =!= sig2 // returns Signal[Boolean] (which is false so far)
</pre>
<h2>EventStreams</h2>
<p>Transform a stream by applying a function to every event:</p>
<pre>
// multiply every value by 42
stream.map(_ * 42)
// alternative syntax
for(event <- stream) yield (event * 42)
// turn every event c into a tuple by
// calling c.name and c.address
stream.map(c => (c.name, c.address) )
</pre>
<p>Turn a signal into an event stream of its changes:</p>
<pre>signal.change</pre>
<p>Filter an event stream with a predicate function:<br />
(removes all elements where predicate evaluates to false)
<pre>
// Remove all elements where name == "timer"
signal.filter(_.name != "timer")
// Only keep events between 10 and 100
signal.filter(e => (e > 10) && (e < 100))
</pre>
<p>Execute a function/block of code for each event:</p>
<pre>
stream.foreach(function) // function receives new event as argument
for(event <- stream) {
// code (can access new event in variable event)
}
</pre>
<p>Extract multiple values from one event:<br />
Can be given any number of functions, which are called with every event of the event stream. Their return values are fired as a List by the returned stream.<br />
Order of given functions and function result values in fired lists is the same.<br />
Useful for logging several attributes of complex events.
</p>
<pre>
// extract three attributes of each RadioTransmission event
// so that they can be logged as individual columns
sim.radioMedium.transmissions.extract(
_.sourceMote,
_.destinationMotes.mkString(","),
_.packetString
) // returns EventStream[List[Any]]
</pre>
<p>Create new stream which fires events of original stream until predicate function return false:</p>
<pre>
stream.takeWhile(predicateFunction)
</pre>
<h2>Simulation observables</h2>
<p>Current simulation time in milliseconds: <tt>Signal[Long]</tt></p>
<pre>sim.milliSeconds</pre>
<p>Simulation log messages: <tt class="es">EventStream[LogMessage]</tt></p>
<pre>sim.log</pre>
<p>Log message members:</p>
<pre>
logMessage.mote // Mote
logMessage.message // String
</pre>
<p>Newly added motes: <tt class="es">EventStream[Mote]</tt></p>
<pre>sim.newMotes</pre>
<p>Mote2Mote relations: <tt>SeqSignal[MoteRelation]</tt></p>
<pre>sim.moteRelations</pre>
<p>Simulation radio medium:</p>
<pre>sim.radioMedium</pre>
<p>Active radio connections: <tt>SeqSignal[RadioConnection]</tt></p>
<pre>sim.radioMedium.connections</pre>
<p>Radio transmissions: <tt class="es">EventStream[RadioTransmission]</tt></p>
<pre>sim.radioMedium.transmissions</pre>
<p>RadioTransmission members:</p>
<pre>
transmission.startTime // Long
transmission.endTime // Long
transmission.source // Radio
transmission.destinations // Set[Radio]
transmission.interfered // Set[Radio]
transmission.packet // RadioPacket
transmission.sourceMote // Mote
transmission.destinationMotes // Set[Mote]
transmission.interferedMote // Set[Mote]
transmission.interferedNonDestinations // Set[Mote]
transmission.packetData // Array[Byte]
transmission.packetString // String
</pre>
<h2>Mote observables</h2>
<p>LED status: <tt>Signal[LEDStatus]</tt></p>
<pre>mote.led.status</pre>
<p>Radio events: <tt class="es">EventStream[RadioEvent]</tt></p>
<pre>mote.radio.events</pre>
<p>Radio interference status: <tt>Signal[Boolean]</tt></p>
<pre>mote.radio.interfered</pre>
<p>Radio receiver status: <tt>Signal[Boolean]</tt></p>
<pre>mote.radio.receiverOn</pre>
<p>Radio reception status: <tt>Signal[Boolean]</tt></p>
<pre>mote.radio.receiving</pre>
<p>Radio transmission status: <tt>Signal[Boolean]</tt></p>
<pre>mote.radio.transmitting</pre>
<p>Radio channel: <tt>Signal[Int]</tt></p>
<pre>mote.radio.channel</pre>
<p>Radio output power: <tt>Signal[Double]</tt></p>
<pre>mote.radio.currentOutputPower</pre>
<p>Radio output power indicator: <tt>Signal[Int]</tt></p>
<pre>mote.radio.currentOutputPowerIndicator</pre>
<p>Radio signal strength: <tt>Signal[Double]</tt></p>
<pre>mote.radio.currentSignalStrength</pre>
<p>Radio position: <tt>Signal[Position]</tt></p>
<pre>mote.radio.position</pre>
<p>Radio transmitted packets: <tt class="es">EventStream[RadioPacket]</tt></p>
<pre>mote.radio.packetsTransmitted</pre>
<p>Radio received packets: <tt class="es">EventStream[RadioPacket]</tt></p>
<pre>mote.radio.packetsReceived</pre>
<p>Radio transmissions: <tt class="es">EventStream[RadioTransmission]</tt></p>
<pre>mote.radio.transmissions</pre>
<p>Radio receptions: <tt class="es">EventStream[RadioTransmission]</tt></p>
<pre>mote.radio.receptions</pre>
<p>Log messages: <tt class="es">EventStream[String]</tt></p>
<pre>mote.log.messages</pre>
<p>Beeper status: <tt>Signal[Boolean]</tt></p>
<pre>mote.beeper.beeping</pre>
<p>Button status: <tt>Signal[Boolean]</tt></p>
<pre>mote.button.pressed</pre>
<p>IP address: <tt>Signal[String]</tt></p>
<pre>mote.ipAddress.ipAddress</pre>
<p>Rime address: <tt>Signal[String]</tt></p>
<pre>mote.rimeAddress.address</pre>
<p>Mote ID (dynamic): <tt>Signal[Int]</tt></p>
<pre>mote.moteID.id</pre>
<p>Mote position: <tt>Signal[Position]</tt></p>
<pre>mote.position.position</pre>
<p>Mote attributes: <tt>Signal[Map[String, String]]</tt></p>
<pre>mote.moteAttributes.attributes</pre>
<p>Contiki process (name from .map file): <tt>Signal[Process]</tt></p>
<pre>mote.currentProcess</pre>
<p>Contiki process (name from memory): <tt>Signal[Process]</tt></p>
<pre>mote.currentProcessDynamic</pre>
<p>Stack Pointer: <tt>Signal[Int]</tt></p>
<pre>mote.cpu.stackPointer</pre>
<p>Register: <tt>Signal[Byte]</tt></p>
<pre>mote.cpu.register(registerName)</pre>
<p>CPU mode (active, lp0, lp1, lp2, lp3, lp4): <tt>Signal[String]</tt></p>
<pre>mote.cpuMode</pre>
<p>Watchpoint (will not stop simulation, just fires name): <tt>EventStream[String]</tt></p>
<pre>mote.watchpoint(sourceFileName, sourceLineNumber, watchPointName)</pre>
<p>Memory variable (byte/char): <tt>Signal[Byte]</tt></p>
<pre>
mote.memory.variable(name, CByte)
mote.memory.variable(address, CByte)
</pre>
<p>Memory variable (int): <tt>Signal[Int]</tt></p>
<pre>
mote.memory.variable(name, CInt)
mote.memory.variable(address, CInt)
</pre>
<p>Memory variable (byte array): <tt>Signal[Array[Byte]]</tt></p>
<pre>
mote.memory.variable(name, CArray(length))
mote.memory.variable(address, CArray(length))
</pre>
<p>Memory variable (pointer): <tt>Signal[Int]</tt></p>
<pre>
mote.memory.variable(name, CPointer)
mote.memory.variable(address, CPointer)
</pre>
<p>Memory pointer:</p>
<pre>
// turn pointer variable into usable pointer
// targetType: CInt, CByte, CPointer, CArray(...) of variable
// this pointer points at
ptr(pointerVar, targetType)
// return pointer pointing at variable
&(variable)
</pre>
<p>Pointer arithmetics:</p>
<pre>
// pointer arithmetic (respects target type byte size)
pointer + offset
pointer - offset
</pre>
<p>Dereference pointer:</p>
<pre>
// dereference pointer using creation target type
*(pointer)
// dereference pointer and set target type (CInt, ...) explicitly
*(pointer, targetType)
</pre>
<h2>Assertions</h2>
<p>Create an assertion (stops simulation when predicate is false):</p>
<pre>
assert(predicateStream, assertionName)
assert(predicateSignal, assertionName)
// stop when stream fires value below 10
assert(stream.map(_ >= 10), "Stream value too low")
// stop when signal value is empty string
assert(signal != "", "signal string is empty")
</pre>
<h2>Logging</h2>
<p>
Log one or more signals to a log destination:<br />
If <em>any</em> of the signals changes, all signal values will be logged.
</p>
<pre>
log(destination, signal)
log(destination, signal1, signal2, ...)
</pre>
<p>
Log one event stream and optionally sample multiple signals when event is fired:<br />
Signal changes will <em>not</em> be logged, they are <em>only</em> sampled when event stream fires.
</p>
<pre>
log(destination, stream)
log(destination, stream, signal1, signal2, ...)
</pre>
<p>Create a log window (destination):</p>
<pre>
// with time column and one column "Value"
LogWindow(windowTitle)
// with time column and column list
LogWindow(windowTitle, List(columnName1, columnName2, ...))
</pre>
<p>Create a log file (destination):</p>
<pre>
// with time column and one column "Value"
LogFile(fileName)
// with time column and column list
LogFile(fileName, List(columnName1, columnName2, ...))
// without header
LogFile(..., header=false)
// with different column seperator (default: tab)
LogFile(..., sep=newSeperatorString)
</pre>
<p>Create a log window (destination):</p>
<pre>
// with time column and one column "Value"
LogTable(SQLiteDB(DBFileName), tableName)
// with time column and column list
LogTable(SQLiteDB(DBFileName), tableName,
List(columnName1, columnName2, ...))
</pre>
<p>Log different streams/signals to one destination (column counts must match!)</p>
<pre>
val dest = Log...(...)
log(dest, signal1, signal2)
log(dest, signal3, signal4)
log(dest, stream, signal5)
</pre>
<p>Change log destination time column:</p>
<pre>
// change time column name
Log...(..., timeColumn="newTimeColumnName")
// do not log time
Log...(..., timeColumn=null)
</pre>
<h2>Operators</h2>
<p>Count events:</p>
<pre>
count(stream) // EventStream[Int]
// same as count(signal.change)
count(signal) // EventStream[Int]
</pre>
<p>Maximum:</p>
<pre>
max(stream) // EventStream[T]
max(signal) // Signal[T]
</pre>
<p>Minimum:</p>
<pre>
min(stream) // EventStream[T]
min(signal) // Signal[T]
</pre>
<p>Average:</p>
<pre>
avg(stream) // EventStream[T]
avg(signal) // Signal[T]
</pre>
<p>Standard deviation:</p>
<pre>
stdDev(stream) // EventStream[T]
stdDev(signal) // Signal[T]
</pre>
<p>Deltas stream:</p>
<pre>
delta(stream) // EventStream[T]
delta(signal) // EventStream[T] !
</pre>
<p>
Zip all given signals into a signal of a list of their values.<br />
Order of values in list signal matches argument ordering.<br />
A change in <em>any</em> of the source signals will change the list signal.
</p>
<pre>
zip(signalA, signalB, signalC) // Signal[List[T]]
</pre>
<p>
Sums all time durations (in microseconds), during which the given boolean signal is <em>true</em>.
</p>
<pre>
timeSum(booleanSignal)) // Signal[Long]
</pre>
<p>Create a tuple with position:</p>
<pre>
withPosition(stream) // EventStream[ (Int, T) ]
</pre>
<p>Create a tuple with time in microseconds:</p>
<pre>
withTime(stream) // EventStream[ (Long, T) ]
</pre>
<p>Create a tuple with time in microseconds:</p>
<pre>
withTime(stream) // EventStream[ (Long, T) ]
</pre>
<p>
Apply a sliding position window, returns a stream of windows.<br />
Each window is returned as a list of all corresponding values.<br />
<em>range:</em> size of one window (number of values contained in one window)<br />
<em>slide:</em> "space" between two windows (number of values between two window starts) <br />
<em>offset:</em> number of values to wait before starting first window
</p>
<pre>
posWindow(stream, range, slide, offset) // EventStream[List[T]]
</pre>
<p>
Apply a sliding position window, returns a stream of windows.<br />
Each window is returned as a list of all corresponding values.<br />
<em>range:</em> size of one window (microseconds between first and last value in one window)<br />
<em>slide:</em> "space" between two windows (microseconds between two window starts)<br />
<em>offset:</em> time in microseconds to wait before starting first window
</p>
<pre>
timeWindow(stream, range, slide, offset) // EventStream[List[T]]
// variant: start time of next window is
// not influenced by end time of last window:
absoluteTimeWindow(stream, range, slide, offset)
</pre>
</pre>
</html>