Arduino  1.8.10
BridgeClass クラス

#include <Bridge.h>

BridgeClass の継承関係図
SerialBridgeClass

公開メンバ関数

 BridgeClass (Stream &_stream)
 
void begin ()
 
void end ()
 
void put (const char *key, const char *value)
 
void put (const String &key, const String &value)
 
unsigned int get (const char *key, uint8_t *buff, unsigned int size)
 
unsigned int get (const char *key, char *value, unsigned int maxlen)
 
uint16_t 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 transfer (const uint8_t *buff1, uint16_t len1)
 
uint16_t transfer (const uint8_t *buff1, uint16_t len1, uint8_t *rxbuff, uint16_t rxlen)
 
uint16_t transfer (const uint8_t *buff1, uint16_t len1, const uint8_t *buff2, uint16_t len2, uint8_t *rxbuff, uint16_t rxlen)
 
uint16_t getBridgeVersion ()
 

静的公開変数類

static const uint16_t TRANSFER_TIMEOUT = 0xFFFF
 

詳解

Bridge.h29 行目に定義があります。

構築子と解体子

◆ BridgeClass()

BridgeClass::BridgeClass ( Stream _stream)

Bridge.cpp21 行目に定義があります。

21  :
22  index(0), stream(_stream), started(false), max_retries(0) {
23  // Empty
24 }
WiFiServerStream stream(SERVER_PORT)

関数詳解

◆ begin()

void BridgeClass::begin ( void  )

Bridge.cpp26 行目に定義があります。

26  {
27  if (started)
28  return;
29  started = true;
30 
31  // Wait for U-boot to finish startup
32  do {
33  dropAll();
34  delay(1000);
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();
57 
58  // Reset the brigde to check if it is running
59  uint8_t cmd[] = {'X', 'X', '1', '0', '0'};
60  uint8_t res[4];
61  max_retries = 50;
62  uint16_t l = transfer(cmd, 5, res, 4);
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  // Detect bridge version
75  if (l == 4) {
76  bridgeVersion = (res[1]-'0')*100 + (res[2]-'0')*10 + (res[3]-'0');
77  } else {
78  // Bridge v1.0.0 didn't send any version info
79  bridgeVersion = 100;
80  }
81 
82  max_retries = 50;
83  return;
84  }
85 }
static const uint16_t TRANSFER_TIMEOUT
Definition: Bridge.h:74
WiFiServerStream stream(SERVER_PORT)
void delay(unsigned long)
Definition: wiring.c:106
size_t print(const __FlashStringHelper *)
Definition: Print.cpp:44
uint16_t 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)
Definition: Bridge.cpp:168
int available()
Definition: WiFiStream.h:199

◆ end()

void BridgeClass::end ( void  )

Bridge.cpp87 行目に定義があります。

87  {
88 
89  while (true) {
90  // Bridge interrupt:
91  // - Ask the bridge to close itself
92  uint8_t quit_cmd[] = {'X', 'X', 'X', 'X', 'X'};
93  max_retries = 1;
94  transfer(quit_cmd, 5);
95  delay(100);
96  stream.print(CTRL_C);
97  delay(250);
98  stream.print(F("cd \n"));
99  //expect a shell
100  bool done = false;
101  delay(100);
102  while (stream.available()) {
103  char c = stream.read();
104  if (c == '#') {
105  done = true;
106  break;
107  }
108  }
109  if (done) {
110  stream.print(F("reset\n"));
111  break;
112  }
113  }
114  delay(100);
115  dropAll();
116 }
int read()
Definition: WiFiStream.h:214
WiFiServerStream stream(SERVER_PORT)
void delay(unsigned long)
Definition: wiring.c:106
size_t print(const __FlashStringHelper *)
Definition: Print.cpp:44
uint16_t 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)
Definition: Bridge.cpp:168
int available()
Definition: WiFiStream.h:199

◆ get() [1/2]

unsigned int BridgeClass::get ( const char *  key,
uint8_t *  buff,
unsigned int  size 
)

Bridge.cpp128 行目に定義があります。

128  {
129  uint8_t cmd[] = {'d'};
130  unsigned int l = transfer(cmd, 1, (uint8_t *)key, strlen(key), value, maxlen);
131  if (l < maxlen)
132  value[l] = 0; // Zero-terminate string
133  return l;
134 }
size_t strlen(const char *) __ATTR_PURE__
Calculate the length of a string.
uint16_t 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)
Definition: Bridge.cpp:168

◆ get() [2/2]

unsigned int BridgeClass::get ( const char *  key,
char *  value,
unsigned int  maxlen 
)
inline

Bridge.h42 行目に定義があります。

43  {
44  return get(key, reinterpret_cast<uint8_t *>(value), maxlen);
45  }
unsigned int get(const char *key, uint8_t *buff, unsigned int size)
Definition: Bridge.cpp:128

◆ getBridgeVersion()

uint16_t BridgeClass::getBridgeVersion ( )
inline

Bridge.h69 行目に定義があります。

70  {
71  return bridgeVersion;
72  }

◆ put() [1/2]

void BridgeClass::put ( const char *  key,
const char *  value 
)

Bridge.cpp118 行目に定義があります。

118  {
119  // TODO: do it in a more efficient way
120  String cmd = "D";
121  uint8_t res[1];
122  cmd += key;
123  cmd += "\xFE";
124  cmd += value;
125  transfer((uint8_t*)cmd.c_str(), cmd.length(), res, 1);
126 }
uint16_t 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)
Definition: Bridge.cpp:168

◆ put() [2/2]

void BridgeClass::put ( const String &  key,
const String &  value 
)
inline

Bridge.h37 行目に定義があります。

38  {
39  put(key.c_str(), value.c_str());
40  }
void put(const char *key, const char *value)
Definition: Bridge.cpp:118

◆ transfer() [1/4]

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 
)

Bridge.cpp168 行目に定義があります。

172 {
173  uint16_t len = len1 + len2 + len3;
174  uint8_t retries = 0;
175  for ( ; retries < max_retries; retries++, delay(100), dropAll() /* Delay for retransmission */) {
176  // Send packet
177  crcReset();
178  stream.write((char)0xFF); // Start of packet (0xFF)
179  crcUpdate(0xFF);
180  stream.write((char)index); // Message index
181  crcUpdate(index);
182  stream.write((char)((len >> 8) & 0xFF)); // Message length (hi)
183  crcUpdate((len >> 8) & 0xFF);
184  stream.write((char)(len & 0xFF)); // Message length (lo)
185  crcUpdate(len & 0xFF);
186  for (uint16_t i = 0; i < len1; i++) { // Payload
187  stream.write((char)buff1[i]);
188  crcUpdate(buff1[i]);
189  }
190  for (uint16_t i = 0; i < len2; i++) { // Payload
191  stream.write((char)buff2[i]);
192  crcUpdate(buff2[i]);
193  }
194  for (uint16_t i = 0; i < len3; i++) { // Payload
195  stream.write((char)buff3[i]);
196  crcUpdate(buff3[i]);
197  }
198  crcWrite(); // CRC
199 
200  // Wait for ACK in 200ms
201  if (timedRead(200) != 0xFF)
202  continue;
203  crcReset();
204  crcUpdate(0xFF);
205 
206  // Check packet index
207  if (timedRead(5) != index)
208  continue;
209  crcUpdate(index);
210 
211  // Recv len
212  int lh = timedRead(10);
213  if (lh < 0)
214  continue;
215  crcUpdate(lh);
216  int ll = timedRead(10);
217  if (ll < 0)
218  continue;
219  crcUpdate(ll);
220  uint16_t l = lh;
221  l <<= 8;
222  l += ll;
223 
224  // Recv data
225  for (uint16_t i = 0; i < l; i++) {
226  // Cut received data if rxbuffer is too small
227  if (i >= rxlen)
228  break;
229  int c = timedRead(5);
230  if (c < 0)
231  continue;
232  rxbuff[i] = c;
233  crcUpdate(c);
234  }
235 
236  // Check CRC
237  int crc_hi = timedRead(5);
238  if (crc_hi < 0)
239  continue;
240  int crc_lo = timedRead(5);
241  if (crc_lo < 0)
242  continue;
243  if (!crcCheck((crc_hi << 8) + crc_lo))
244  continue;
245 
246  // Increase index
247  index++;
248 
249  // Return bytes received
250  if (l > rxlen)
251  return rxlen;
252  return l;
253  }
254 
255  // Max retries exceeded
256  return TRANSFER_TIMEOUT;
257 }
static const uint16_t TRANSFER_TIMEOUT
Definition: Bridge.h:74
WiFiServerStream stream(SERVER_PORT)
uint8_t i
void delay(unsigned long)
Definition: wiring.c:106
size_t write(uint8_t byte)
Definition: WiFiStream.h:219

◆ transfer() [2/4]

uint16_t BridgeClass::transfer ( const uint8_t *  buff1,
uint16_t  len1 
)
inline

Bridge.h53 行目に定義があります。

54  {
55  return transfer(buff1, len1, NULL, 0);
56  }
#define NULL
Definition: def.h:44
uint16_t 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)
Definition: Bridge.cpp:168

◆ transfer() [3/4]

uint16_t BridgeClass::transfer ( const uint8_t *  buff1,
uint16_t  len1,
uint8_t *  rxbuff,
uint16_t  rxlen 
)
inline

Bridge.h57 行目に定義があります。

59  {
60  return transfer(buff1, len1, NULL, 0, rxbuff, rxlen);
61  }
#define NULL
Definition: def.h:44
uint16_t 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)
Definition: Bridge.cpp:168

◆ transfer() [4/4]

uint16_t BridgeClass::transfer ( const uint8_t *  buff1,
uint16_t  len1,
const uint8_t *  buff2,
uint16_t  len2,
uint8_t *  rxbuff,
uint16_t  rxlen 
)
inline

Bridge.h62 行目に定義があります。

65  {
66  return transfer(buff1, len1, buff2, len2, NULL, 0, rxbuff, rxlen);
67  }
#define NULL
Definition: def.h:44
uint16_t 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)
Definition: Bridge.cpp:168

メンバ詳解

◆ TRANSFER_TIMEOUT

const uint16_t BridgeClass::TRANSFER_TIMEOUT = 0xFFFF
static

Bridge.h74 行目に定義があります。


このクラス詳解は次のファイルから抽出されました: