18
18
19
19
#include " Bridge.h"
20
20
21
- BridgeClass::BridgeClass (Stream &_stream) : index(0 ), stream(_stream), started(false ) {
21
+ BridgeClass::BridgeClass (Stream &_stream) :
22
+ index(0 ), stream(_stream), started(false ), max_retries(0 ) {
22
23
// Empty
23
24
}
24
25
@@ -27,35 +28,52 @@ void BridgeClass::begin() {
27
28
return ;
28
29
started = true ;
29
30
30
- // Wait for Atheros bootloader to finish startup
31
+ // Wait for U-boot to finish startup
31
32
do {
32
33
dropAll ();
33
- delay (1100 );
34
+ delay (1000 );
34
35
} while (stream.available ()>0 );
36
+
37
+ while (true ) {
38
+ // Bridge interrupt:
39
+ // - Ask the bridge to close itself
40
+ uint8_t quit_cmd[] = {' X' ,' X' ,' X' ,' X' ,' X' };
41
+ max_retries = 1 ;
42
+ transfer (quit_cmd, 5 );
43
+
44
+ // Bridge startup:
45
+ // - If the bridge is not running starts it safely
46
+ stream.print (CTRL_C);
47
+ delay (250 );
48
+ stream.print (F (" \n " ));
49
+ delay (250 );
50
+ stream.print (F (" \n " ));
51
+ delay (500 );
52
+ // Wait for OpenWRT message
53
+ // "Press enter to activate console"
54
+ stream.print (F (" run-bridge\n " ));
55
+ delay (500 );
56
+ dropAll ();
35
57
36
- // Bridge startup:
37
- // - If the bridge is not running starts it safely
38
- stream.print (CTRL_C);
39
- delay (250 );
40
- stream.print (F (" \n " ));
41
- delay (500 );
42
- stream.print (F (" \n " ));
43
- delay (750 );
44
- // Wait for OpenWRT message
45
- // "Press enter to activate console"
46
- stream.print (F (" run-bridge\n " ));
47
- delay (500 );
48
- dropAll ();
49
-
50
- // - If the bridge was already running previous commands
51
- // are ignored as "invalid packets".
52
-
53
- // Reset the brigde
54
- uint8_t cmd[] = {' X' ,' X' , ' 1' ,' 0' ,' 0' };
55
- uint8_t res[1 ];
56
- transfer (cmd, 5 , res, 1 );
57
- if (res[0 ] != 0 )
58
- while (true );
58
+ // Reset the brigde to check if it is running
59
+ uint8_t cmd[] = {' X' ,' X' , ' 1' ,' 0' ,' 0' };
60
+ uint8_t res[1 ];
61
+ max_retries = 20 ;
62
+ uint16_t l = transfer (cmd, 5 , res, 1 );
63
+ if (l == TRANSFER_TIMEOUT) {
64
+ // Bridge didn't start...
65
+ // Maybe the board is starting-up?
66
+
67
+ // Wait and retry
68
+ delay (1000 );
69
+ continue ;
70
+ }
71
+ if (res[0 ] != 0 )
72
+ while (true );
73
+
74
+ max_retries = 50 ;
75
+ return ;
76
+ }
59
77
}
60
78
61
79
void BridgeClass::put (const char *key, const char *value) {
@@ -99,7 +117,8 @@ uint16_t BridgeClass::transfer(const uint8_t *buff1, uint16_t len1,
99
117
uint8_t *rxbuff, uint16_t rxlen)
100
118
{
101
119
uint16_t len = len1 + len2 + len3;
102
- for ( ; ; delay (100 ), dropAll () /* Delay for retransmission */ ) {
120
+ uint8_t retries = 0 ;
121
+ for ( ; retries<max_retries; retries++, delay (100 ), dropAll () /* Delay for retransmission */ ) {
103
122
// Send packet
104
123
crcReset ();
105
124
stream.write ((char )0xFF ); // Start of packet (0xFF)
@@ -177,6 +196,9 @@ uint16_t BridgeClass::transfer(const uint8_t *buff1, uint16_t len1,
177
196
return rxlen;
178
197
return l;
179
198
}
199
+
200
+ // Max retries exceeded
201
+ return TRANSFER_TIMEOUT;
180
202
}
181
203
182
204
int BridgeClass::timedRead (unsigned int timeout) {
0 commit comments