File size: 7,761 Bytes
a9536d3
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
/*
  Copyright (c) 2013 Arduino LLC. All right reserved.

  This library is free software; you can redistribute it and/or
  modify it under the terms of the GNU Lesser General Public
  License as published by the Free Software Foundation; either
  version 2.1 of the License, or (at your option) any later version.

  This library is distributed in the hope that it will be useful,
  but WITHOUT ANY WARRANTY; without even the implied warranty of
  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  Lesser General Public License for more details.

  You should have received a copy of the GNU Lesser General Public
  License along with this library; if not, write to the Free Software
  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*/

#include "Bridge.h"

BridgeClass::BridgeClass(Stream &_stream) :
  index(0), stream(_stream), started(false), max_retries(0) {
  // Empty
}

void BridgeClass::begin() {
  if (started)
    return;
  started = true;

  // Wait for U-boot to finish startup
  do {
    dropAll();
    delay(1000);
  } while (stream.available() > 0);

  while (true) {
    // Bridge interrupt:
    // - Ask the bridge to close itself
    uint8_t quit_cmd[] = {'X', 'X', 'X', 'X', 'X'};
    max_retries = 1;
    transfer(quit_cmd, 5);

    // Bridge startup:
    // - If the bridge is not running starts it safely
    stream.print(CTRL_C);
    delay(250);
    stream.print(F("\n"));
    delay(250);
    stream.print(F("\n"));
    delay(500);
    // Wait for OpenWRT message
    // "Press enter to activate console"
    stream.print(F("run-bridge\n"));
    delay(500);
    dropAll();

    // Reset the brigde to check if it is running
    uint8_t cmd[] = {'X', 'X', '1', '0', '0'};
    uint8_t res[4];
    max_retries = 50;
    uint16_t l = transfer(cmd, 5, res, 4);
    if (l == TRANSFER_TIMEOUT) {
      // Bridge didn't start...
      // Maybe the board is starting-up?

      // Wait and retry
      delay(1000);
      continue;
    }
    if (res[0] != 0)
      while (true);

    // Detect bridge version
    if (l == 4) {
      bridgeVersion = (res[1]-'0')*100 + (res[2]-'0')*10 + (res[3]-'0');
    } else {
      // Bridge v1.0.0 didn't send any version info
      bridgeVersion = 100;
    }

    max_retries = 50;
    return;
  }
}

void BridgeClass::end() {

  while (true) {
    // Bridge interrupt:
    // - Ask the bridge to close itself
    uint8_t quit_cmd[] = {'X', 'X', 'X', 'X', 'X'};
    max_retries = 1;
    transfer(quit_cmd, 5);
    delay(100);
    stream.print(CTRL_C);
    delay(250);
    stream.print(F("cd \n"));
    //expect a shell
    bool done = false;
    delay(100);
    while (stream.available()) {
      char c = stream.read();
      if (c == '#') {
        done = true;
        break;
      }
    }
    if (done) {
      stream.print(F("reset\n"));
      break;
    }
  }
  delay(100);
  dropAll();
}

void BridgeClass::put(const char *key, const char *value) {
  // TODO: do it in a more efficient way
  String cmd = "D";
  uint8_t res[1];
  cmd += key;
  cmd += "\xFE";
  cmd += value;
  transfer((uint8_t*)cmd.c_str(), cmd.length(), res, 1);
}

unsigned int BridgeClass::get(const char *key, uint8_t *value, unsigned int maxlen) {
  uint8_t cmd[] = {'d'};
  unsigned int l = transfer(cmd, 1, (uint8_t *)key, strlen(key), value, maxlen);
  if (l < maxlen)
    value[l] = 0; // Zero-terminate string
  return l;
}

#if defined(ARDUINO_ARCH_AVR)
// AVR use an optimized implementation of CRC
#include <util/crc16.h>
#else
// Generic implementation for non-AVR architectures
uint16_t _crc_ccitt_update(uint16_t crc, uint8_t data)
{
  data ^= crc & 0xff;
  data ^= data << 4;
  return ((((uint16_t)data << 8) | ((crc >> 8) & 0xff)) ^
          (uint8_t)(data >> 4) ^
          ((uint16_t)data << 3));
}
#endif

void BridgeClass::crcUpdate(uint8_t c) {
  CRC = _crc_ccitt_update(CRC, c);
}

void BridgeClass::crcReset() {
  CRC = 0xFFFF;
}

void BridgeClass::crcWrite() {
  stream.write((char)(CRC >> 8));
  stream.write((char)(CRC & 0xFF));
}

bool BridgeClass::crcCheck(uint16_t _CRC) {
  return CRC == _CRC;
}

uint16_t BridgeClass::transfer(const uint8_t *buff1, uint16_t len1,
                               const uint8_t *buff2, uint16_t len2,
                               const uint8_t *buff3, uint16_t len3,
                               uint8_t *rxbuff, uint16_t rxlen)
{
  uint16_t len = len1 + len2 + len3;
  uint8_t retries = 0;
  for ( ; retries < max_retries; retries++, delay(100), dropAll() /* Delay for retransmission */) {
    // Send packet
    crcReset();
    stream.write((char)0xFF);                // Start of packet (0xFF)
    crcUpdate(0xFF);
    stream.write((char)index);               // Message index
    crcUpdate(index);
    stream.write((char)((len >> 8) & 0xFF)); // Message length (hi)
    crcUpdate((len >> 8) & 0xFF);
    stream.write((char)(len & 0xFF));        // Message length (lo)
    crcUpdate(len & 0xFF);
    for (uint16_t i = 0; i < len1; i++) { // Payload
      stream.write((char)buff1[i]);
      crcUpdate(buff1[i]);
    }
    for (uint16_t i = 0; i < len2; i++) { // Payload
      stream.write((char)buff2[i]);
      crcUpdate(buff2[i]);
    }
    for (uint16_t i = 0; i < len3; i++) { // Payload
      stream.write((char)buff3[i]);
      crcUpdate(buff3[i]);
    }
    crcWrite();                     // CRC

    // Wait for ACK in 200ms
    if (timedRead(200) != 0xFF)
      continue;
    crcReset();
    crcUpdate(0xFF);

    // Check packet index
    if (timedRead(5) != index)
      continue;
    crcUpdate(index);

    // Recv len
    int lh = timedRead(10);
    if (lh < 0)
      continue;
    crcUpdate(lh);
    int ll = timedRead(10);
    if (ll < 0)
      continue;
    crcUpdate(ll);
    uint16_t l = lh;
    l <<= 8;
    l += ll;

    // Recv data
    for (uint16_t i = 0; i < l; i++) {
      // Cut received data if rxbuffer is too small
      if (i >= rxlen)
          break;
      int c = timedRead(5);
      if (c < 0)
        continue;
      rxbuff[i] = c;
      crcUpdate(c);
    }

    // Check CRC
    int crc_hi = timedRead(5);
    if (crc_hi < 0)
      continue;
    int crc_lo = timedRead(5);
    if (crc_lo < 0)
      continue;
    if (!crcCheck((crc_hi << 8) + crc_lo))
      continue;

    // Increase index
    index++;

    // Return bytes received
    if (l > rxlen)
      return rxlen;
    return l;
  }

  // Max retries exceeded
  return TRANSFER_TIMEOUT;
}

int BridgeClass::timedRead(unsigned int timeout) {
  int c;
  unsigned long _startMillis = millis();
  do {
    c = stream.read();
    if (c >= 0) return c;
  } while (millis() - _startMillis < timeout);
  return -1;     // -1 indicates timeout
}

void BridgeClass::dropAll() {
  while (stream.available() > 0) {
    stream.read();
  }
}

#if defined(ARDUINO_ARCH_SAM)
#include <Reset.h>
#endif

#if defined(ARDUINO_ARCH_SAM)
void checkForRemoteSketchUpdate(uint8_t pin) {
  // The host force pin LOW to signal that a new sketch is coming
  pinMode(pin, INPUT_PULLUP);
  delay(50);
  if (digitalRead(pin) == LOW) {
    initiateReset(1);
    while (true)
      ; // Wait for reset to SAM-BA
  }

  // Restore in standard state
  pinMode(pin, INPUT);
}
#else
void checkForRemoteSketchUpdate(uint8_t /* pin */) {
  // Empty, bootloader is enough.
}
#endif

// Bridge instance
#if defined(SERIAL_PORT_LINUXBRIDGE)
SerialBridgeClass Bridge(SERIAL_PORT_LINUXBRIDGE);
#elif defined(SERIAL_PORT_HARDWARE)
SerialBridgeClass Bridge(SERIAL_PORT_HARDWARE);
#elif defined(SERIAL_PORT_HARDWARE_OPEN)
SerialBridgeClass Bridge(SERIAL_PORT_HARDWARE_OPEN);
#elif defined(__AVR_ATmega32U4__) // Legacy fallback
// Leonardo variants (where HardwareSerial is Serial1)
SerialBridgeClass Bridge(Serial1);
#else
SerialBridgeClass Bridge(Serial);
#endif