telosb reflexive jamming, beta version, confirmed works in some testing, improvements...
[goodfet] / firmware / goodfet.c
1 /*! \file goodfet.c
2   \author Travis Goodspeed
3   \brief Main module.
4   
5   This is the main module of the GoodFET, which calls the initialization
6   routines and delegates commands to the various applications.
7 */
8
9
10 #include "platform.h"
11 #include "command.h"
12 #include "apps.h"
13 #include "glitch.h"
14
15 #define RESET 0x80      // not a real app -- causes firmware to reset
16 #define DEBUGAPP 0xFF
17
18 //! General init function, calls platform-specific one.
19 void init(){
20 #ifdef MSP430
21   #define INITCHIP msp430_init();
22 #endif
23
24 #ifdef ARDUINO
25   #define INITCHIP arduino_init();
26 #endif
27
28 #ifdef INITCHIP
29 INITCHIP
30 #else
31 #warning "No init() routine for this platform!"
32 #endif
33
34 #ifdef INITPLATFORM
35   INITPLATFORM
36 #endif
37 }
38
39
40
41 //! Handle a command.
42 void handle(uint8_t const app,
43             uint8_t const verb,
44             uint32_t const len){
45   int i;
46   
47   //debugstr("GoodFET");
48   PLEDOUT&=~PLEDPIN;
49   
50   // find the app and call the handle fn
51   for(i = 0; i < num_apps; i++){
52     if(apps[i]->app == app){
53       // call the app's handle fn
54       (*(apps[i]->handle))(app, verb, len);
55
56       // exit early
57       return;
58     }
59   }
60
61   // if we get here, then the desired app is not copiled in 
62   // this firmware
63   debugstr("App missing.");
64   debughex(app);
65   txdata(app, NOK, 0);
66 }
67
68
69 //! Main loop.
70 int main(void)
71 {
72         volatile unsigned int i;
73         unsigned char app, verb;
74         unsigned long len;
75         // MSP reboot count for reset input & reboot function located at 0xFFFE
76         volatile unsigned int reset_count = 0;
77         void (*reboot_function)(void) = (void *) 0xFFFE;
78
79         init();
80         
81         txstring(MONITOR,OK,"http://goodfet.sf.net/");
82         #ifdef ECHOTEST
83         while(1) serial0_tx(serial0_rx());
84         #endif
85         
86         //Command loop.  There's no end!
87         while(1)
88         {
89                 //Magic 3
90                 app = serial_rx();
91
92                 // If the app is the reset byte (0x80) increment and loop
93                 if (app == RESET)
94                 {
95                         reset_count++;
96
97                         if (reset_count > 4) 
98                         {
99                                 // We could trigger the WDT with either:
100                                 // WDTCTL = 0;
101                                 // or
102                                 // WDTCTL = WDTPW + WDTCNTCL + WDTSSEL + 0x00;
103                                 // but instead we'll jump to our reboot function pointer
104                           #ifdef MSP430
105                                 (*reboot_function)();
106                           #else
107                                 debugstr("Rebooting not supported on this platform.");
108                           #endif
109                         }
110
111                         continue;
112                 } 
113                 else 
114                 {
115                         reset_count = 0;
116                 }
117
118                 verb = serial_rx();
119                 len = rxword();
120
121                 //Read data, looking for buffer overflow.y
122                 if(len <= CMDDATALEN)
123                 {
124                         for(i = 0; i < len; i++)
125                         {
126                                 cmddata[i] = serial_rx();
127                         }
128
129                         handle(app,verb,len);
130                 }
131                 else
132                 {
133                         //Listen to the blaberring.
134                         for(i = 0; i < len; i++)
135                                 serial_rx();
136
137                         //Reply with an error.
138                         debugstr("Buffer length exceeded.");
139                         txdata(MONITOR,NOK,0);
140                 }
141         }
142 }
143