1 #include <sancus/reactive.h> 2 3 #include <stdio.h> 4 5 6 #include "sensor.h" 7 #include "helpers.c" 8 9 static __attribute__((noinline)) void decode_print(uint8_t b) 10 { 11 if (b & SPOT_OCCUPIED) { 12 printf(" OCCU"); 13 } 14 if (b & SPOT_FREE) { 15 printf(" FREE"); 16 } 17 if (b & CAR_ENTER) { 18 printf(" ENTR"); 19 } 20 if (b & CAR_EXIT) { 21 printf(" EXIT"); 22 } 23 } 24 25 26 #define SMNAME aggregator 27 #define SNONCE 0xacdc 28 29 SM_OUTPUT(SMNAME, output); 30 31 static uint8_t SM_DATA(SMNAME) spot1_status; 32 static uint8_t SM_DATA(SMNAME) spot2_status; 33 static int SM_DATA(SMNAME) seq; 34 35 void SM_FUNC(SMNAME) produce_output(void) 36 { 37 uint8_t free_spots = 0; 38 39 if (spot1_status & SPOT_FREE) { free_spots += 1; } 40 if (spot2_status & SPOT_FREE) { free_spots += 1; } 41 42 uint8_t data[] __attribute__((aligned(2))) 43 = { (SNONCE >> (8*0)) & 0xff, 44 (SNONCE >> (8*1)) & 0xff, 45 (seq >> (8*0)) & 0xff, 46 (seq >> (8*1)) & 0xff, 47 free_spots, 0 }; 48 seq += 1; 49 50 printf1("* SM aggregator: sending ++ "); 51 for (unsigned i = 0; i < sizeof(data); i++) { 52 print_byte(data[i]); 53 } 54 printf1("+\n"); 55 56 output(data, sizeof(data)); 57 } 58 59 void SM_FUNC(SMNAME) get_input(uint8_t input, const uint8_t *data, size_t len) 60 { 61 printf1("* SM aggregator: got input "); 62 print_byte(input); 63 printf1(" -- "); 64 65 for (unsigned i = 0; i < len; i++) { 66 print_byte(data[i]); 67 } 68 if (len >= 5) { 69 decode_print(data[4]); 70 if (input == 1) { spot1_status = data[4]; } 71 if (input == 2) { spot2_status = data[4]; } 72 } 73 74 printf1("-\n"); 75 produce_output(); 76 } 77 78 SM_INPUT(SMNAME, input1, data, len) 79 { 80 get_input(1, data, len); 81 } 82 83 SM_INPUT(SMNAME, input2, data, len) 84 { 85 get_input(2, data, len); 86 } 87