1 #include <sancus/reactive.h>
 2 
 3 #include <stdio.h>
 4 
 5 
 6 #include "sensor.h"
 7 
 8 SM_OUTPUT(SMNAME, output);
 9 
10 static int     SM_DATA(SMNAME) initialised;
11 static int     SM_DATA(SMNAME) nonce;
12 static int     SM_DATA(SMNAME) seq;
13 static uint8_t SM_DATA(SMNAME) action;
14 
15 #define __stringify(arg) #arg
16 #define STRINGIFY(ARG)   __stringify(ARG)
17 
18 
19 void SM_FUNC(SMNAME) init(void)
20 {
21   if (! initialised) {
22     nonce       = SNONCE;
23     seq         = 0;
24     action      = 0;
25     initialised = 1;
26   }
27 }
28 
29 SM_ENTRY(SMNAME) void produce_output(void)
30 {
31     puts("* SM " STRINGIFY(SMNAME) ": produce_output");
32 
33     if (! initialised) { init(); }
34 
35     if (seq == 0) { action = SPOT_FREE; }
36     if (seq == 2) { action = SPOT_OCCUPIED | CAR_ENTER; }
37     if (seq == 3) { action = SPOT_OCCUPIED; }
38     if (seq == 5) { action = SPOT_FREE | CAR_EXIT; }
39     if (seq == 6) { action = SPOT_FREE; }
40 
41     uint8_t data[] __attribute__((aligned(2)))
42                    = { (nonce >> (8*0)) & 0xff,
43                        (nonce >> (8*1)) & 0xff,
44                        (seq   >> (8*0)) & 0xff,
45                        (seq   >> (8*1)) & 0xff,
46                        action, 0 };
47     seq += 1;
48 
49     output(data, sizeof(data));
50 }
51