1 #include <sancus/reactive.h>
 2 
 3 #include <stdio.h>
 4 
 5 #include "wan_commands.h"
 6 
 7 #define SMNAME   ESME_LS
 8 
 9 #define LOG_ENTRY_SIZE 4
10 #define LOG_SIZE       3
11 
12 SM_OUTPUT(SMNAME, central_system_out);
13 SM_OUTPUT(SMNAME, esme_out);
14 
15 static int     SM_DATA(SMNAME) seq;
16 static uint8_t SM_DATA(SMNAME) supply_state;
17 
18 static uint8_t SM_DATA(SMNAME) load_switch_log[LOG_ENTRY_SIZE * LOG_SIZE];
19 static uint8_t SM_DATA(SMNAME) load_switch_log_index;
20 
21 #define CHANNEL_CS   1
22 #define CHANNEL_ESME 2
23 
24 void SM_FUNC(SMNAME) write_load_switch_log(const uint8_t *data, size_t len)
25 {
26     if (load_switch_log_index == LOG_SIZE) {
27       load_switch_log_index = 0; }
28 
29     load_switch_log[(load_switch_log_index * LOG_ENTRY_SIZE) + 0] = data[0];
30     load_switch_log[(load_switch_log_index * LOG_ENTRY_SIZE) + 1] = data[1];
31     load_switch_log[(load_switch_log_index * LOG_ENTRY_SIZE) + 2] = data[2];
32 
33     load_switch_log_index++;
34 }
35 
36 void SM_FUNC(SMNAME) produce_output(uint8_t channel, uint8_t info)
37 {
38     uint8_t data[4 + (LOG_ENTRY_SIZE * LOG_SIZE)] __attribute__((aligned(2)))
39                    = { (seq    >> (8*0)) & 0xff,
40                        (seq    >> (8*1)) & 0xff,
41                        info };
42     seq += 1;
43 
44     if (info == 0xff) {
45       for(int i = 0; i < (LOG_ENTRY_SIZE * LOG_SIZE); i++) {
46         data[3 + i] = load_switch_log[i];
47       }
48     }
49 
50     if (channel == CHANNEL_CS) {
51       central_system_out(data, sizeof(data));
52     }
53 
54     if (channel == CHANNEL_ESME) {
55       esme_out(data, sizeof(data));
56     }
57 }
58 
59 void SM_FUNC(SMNAME) get_input(uint8_t channel, const uint8_t *data,
60     size_t len)
61 {
62     if (len >= 3) {
63         if (channel == CHANNEL_CS) {
64           if (data[2] & ENABLE_SUPPLY) {
65             write_load_switch_log(data, len);
66             supply_state = SUPPLY_STATE_ENABLED;
67             produce_output(CHANNEL_CS, ENABLE_SUPPLY);
68           }
69           if (data[2] & DISABLE_SUPPLY) {
70             write_load_switch_log(data, len);
71             supply_state = SUPPLY_STATE_DISABLED;
72             produce_output(CHANNEL_CS, DISABLE_SUPPLY);
73           }
74           if (data[2] & GET_SUPPLY_STATE) {
75             produce_output(CHANNEL_CS, supply_state);
76           }
77           if (data[2] & GET_LOAD_SWITCH_LOG) {
78             produce_output(CHANNEL_CS, 0xff);
79           }
80         }
81         if (channel == CHANNEL_ESME) {
82           if (data[2] & GET_SUPPLY_STATE) {
83             produce_output(CHANNEL_ESME, supply_state);
84           }
85         }
86     }
87 }
88 
89 SM_INPUT(SMNAME, central_system_in, data, len)
90 {
91     get_input(CHANNEL_CS, data, len);
92 }
93 
94 SM_INPUT(SMNAME, esme_in, data, len)
95 {
96     get_input(CHANNEL_ESME, data, len);
97 }
98