mirror of
https://github.com/esp8266/Arduino.git
synced 2025-06-20 21:01:25 +03:00
Bridge library to the 1.5 format
This commit is contained in:
229
libraries/Bridge/src/Bridge.cpp
Normal file
229
libraries/Bridge/src/Bridge.cpp
Normal file
@ -0,0 +1,229 @@
|
||||
/*
|
||||
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"
|
||||
#include <util/crc16.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[1];
|
||||
max_retries = 50;
|
||||
uint16_t l = transfer(cmd, 5, res, 1);
|
||||
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);
|
||||
|
||||
max_retries = 50;
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
void BridgeClass::put(const char *key, const char *value) {
|
||||
// TODO: do it in a more efficient way
|
||||
String cmd = "D";
|
||||
cmd += key;
|
||||
cmd += "\xFE";
|
||||
cmd += value;
|
||||
transfer((uint8_t*)cmd.c_str(), cmd.length());
|
||||
}
|
||||
|
||||
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;
|
||||
}
|
||||
|
||||
void BridgeClass::crcUpdate(uint8_t c) {
|
||||
|
||||
CRC = _crc_ccitt_update(CRC, c);
|
||||
//CRC = CRC ^ c;
|
||||
//CRC = (CRC >> 8) + (CRC << 8);
|
||||
}
|
||||
|
||||
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 100ms
|
||||
if (timedRead(100) != 0xFF)
|
||||
continue;
|
||||
crcReset();
|
||||
crcUpdate(0xFF);
|
||||
|
||||
// Check packet index
|
||||
if (timedRead(5) != index)
|
||||
continue;
|
||||
crcUpdate(index);
|
||||
|
||||
// Recv len
|
||||
int lh = timedRead(5);
|
||||
if (lh < 0)
|
||||
continue;
|
||||
crcUpdate(lh);
|
||||
int ll = timedRead(5);
|
||||
if (ll < 0)
|
||||
continue;
|
||||
crcUpdate(ll);
|
||||
uint16_t l = lh;
|
||||
l <<= 8;
|
||||
l += ll;
|
||||
|
||||
// Recv data
|
||||
for (uint16_t i=0; i<l; i++) {
|
||||
int c = timedRead(5);
|
||||
if (c < 0)
|
||||
continue;
|
||||
// Cut received data if rxbuffer is too small
|
||||
if (i < rxlen)
|
||||
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();
|
||||
}
|
||||
}
|
||||
|
||||
// Bridge instance
|
||||
#ifdef __AVR_ATmega32U4__
|
||||
// Leonardo variants (where HardwareSerial is Serial1)
|
||||
SerialBridgeClass Bridge(Serial1);
|
||||
#else
|
||||
SerialBridgeClass Bridge(Serial);
|
||||
#endif
|
97
libraries/Bridge/src/Bridge.h
Normal file
97
libraries/Bridge/src/Bridge.h
Normal file
@ -0,0 +1,97 @@
|
||||
/*
|
||||
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
|
||||
*/
|
||||
|
||||
#ifndef BRIDGE_H_
|
||||
#define BRIDGE_H_
|
||||
|
||||
#include <Arduino.h>
|
||||
#include <Stream.h>
|
||||
|
||||
class BridgeClass {
|
||||
public:
|
||||
BridgeClass(Stream &_stream);
|
||||
void begin();
|
||||
|
||||
// Methods to handle key/value datastore
|
||||
void put(const char *key, const char *value);
|
||||
void put(const String &key, const String &value)
|
||||
{ put(key.c_str(), value.c_str()); }
|
||||
unsigned int get(const char *key, uint8_t *buff, unsigned int size);
|
||||
unsigned int get(const char *key, char *value, unsigned int maxlen)
|
||||
{ get(key, reinterpret_cast<uint8_t *>(value), maxlen); }
|
||||
|
||||
// Trasnfer a frame (with error correction and response)
|
||||
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);
|
||||
// multiple inline versions of the same function to allow efficient frame concatenation
|
||||
uint16_t transfer(const uint8_t *buff1, uint16_t len1)
|
||||
{ return transfer(buff1, len1, NULL, 0); }
|
||||
uint16_t transfer(const uint8_t *buff1, uint16_t len1,
|
||||
uint8_t *rxbuff, uint16_t rxlen)
|
||||
{ return transfer(buff1, len1, NULL, 0, rxbuff, rxlen); }
|
||||
uint16_t transfer(const uint8_t *buff1, uint16_t len1,
|
||||
const uint8_t *buff2, uint16_t len2,
|
||||
uint8_t *rxbuff, uint16_t rxlen)
|
||||
{ return transfer(buff1, len1, buff2, len2, NULL, 0, rxbuff, rxlen); }
|
||||
|
||||
static const int TRANSFER_TIMEOUT = 0xFFFF;
|
||||
|
||||
private:
|
||||
uint8_t index;
|
||||
int timedRead(unsigned int timeout);
|
||||
void dropAll();
|
||||
|
||||
private:
|
||||
void crcUpdate(uint8_t c);
|
||||
void crcReset();
|
||||
void crcWrite();
|
||||
bool crcCheck(uint16_t _CRC);
|
||||
uint16_t CRC;
|
||||
|
||||
private:
|
||||
static const char CTRL_C = 3;
|
||||
Stream &stream;
|
||||
bool started;
|
||||
uint8_t max_retries;
|
||||
};
|
||||
|
||||
// This subclass uses a serial port Stream
|
||||
class SerialBridgeClass : public BridgeClass {
|
||||
public:
|
||||
SerialBridgeClass(HardwareSerial &_serial)
|
||||
: BridgeClass(_serial), serial(_serial) {
|
||||
// Empty
|
||||
}
|
||||
|
||||
void begin() {
|
||||
serial.begin(250000);
|
||||
BridgeClass::begin();
|
||||
}
|
||||
|
||||
private:
|
||||
HardwareSerial &serial;
|
||||
};
|
||||
|
||||
extern SerialBridgeClass Bridge;
|
||||
|
||||
#endif /* BRIDGE_H_ */
|
||||
|
||||
#include <Console.h>
|
||||
#include <Process.h>
|
153
libraries/Bridge/src/Console.cpp
Normal file
153
libraries/Bridge/src/Console.cpp
Normal file
@ -0,0 +1,153 @@
|
||||
/*
|
||||
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 <Console.h>
|
||||
|
||||
// Default constructor uses global Bridge instance
|
||||
ConsoleClass::ConsoleClass() :
|
||||
bridge(Bridge), inBuffered(0), inReadPos(0), inBuffer(NULL),
|
||||
autoFlush(true)
|
||||
{
|
||||
// Empty
|
||||
}
|
||||
|
||||
// Constructor with a user provided BridgeClass instance
|
||||
ConsoleClass::ConsoleClass(BridgeClass &_b) :
|
||||
bridge(_b), inBuffered(0), inReadPos(0), inBuffer(NULL),
|
||||
autoFlush(true)
|
||||
{
|
||||
// Empty
|
||||
}
|
||||
|
||||
ConsoleClass::~ConsoleClass() {
|
||||
end();
|
||||
}
|
||||
|
||||
size_t ConsoleClass::write(uint8_t c) {
|
||||
if (autoFlush) {
|
||||
uint8_t tmp[] = { 'P', c };
|
||||
bridge.transfer(tmp, 2);
|
||||
return 1;
|
||||
} else {
|
||||
outBuffer[outBuffered++] = c;
|
||||
if (outBuffered == outBufferSize)
|
||||
flush();
|
||||
}
|
||||
}
|
||||
|
||||
size_t ConsoleClass::write(const uint8_t *buff, size_t size) {
|
||||
if (autoFlush) {
|
||||
// TODO: do it in a more efficient way
|
||||
uint8_t *tmp = new uint8_t[size+1];
|
||||
tmp[0] = 'P';
|
||||
memcpy(tmp+1, buff, size);
|
||||
bridge.transfer(tmp, size+1);
|
||||
delete[] tmp;
|
||||
return size;
|
||||
} else {
|
||||
while (size > 0) {
|
||||
outBuffer[outBuffered++] = *buff++;
|
||||
size--;
|
||||
if (outBuffered == outBufferSize)
|
||||
flush();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void ConsoleClass::flush() {
|
||||
if (autoFlush)
|
||||
return;
|
||||
|
||||
bridge.transfer(outBuffer, outBuffered);
|
||||
outBuffered = 1;
|
||||
}
|
||||
|
||||
void ConsoleClass::noBuffer() {
|
||||
if (autoFlush)
|
||||
return;
|
||||
delete[] outBuffer;
|
||||
autoFlush = true;
|
||||
}
|
||||
|
||||
void ConsoleClass::buffer(uint8_t size) {
|
||||
noBuffer();
|
||||
if (size==0)
|
||||
return;
|
||||
outBuffer = new uint8_t[size+1];
|
||||
outBuffer[0] = 'P'; // WRITE tag
|
||||
outBufferSize = size+1;
|
||||
outBuffered = 1;
|
||||
autoFlush = false;
|
||||
}
|
||||
|
||||
bool ConsoleClass::connected() {
|
||||
uint8_t tmp = 'a';
|
||||
bridge.transfer(&tmp, 1, &tmp, 1);
|
||||
return tmp==1;
|
||||
}
|
||||
|
||||
int ConsoleClass::available() {
|
||||
// Look if there is new data available
|
||||
doBuffer();
|
||||
return inBuffered;
|
||||
}
|
||||
|
||||
int ConsoleClass::read() {
|
||||
doBuffer();
|
||||
if (inBuffered == 0)
|
||||
return -1; // no chars available
|
||||
else {
|
||||
inBuffered--;
|
||||
return inBuffer[inReadPos++];
|
||||
}
|
||||
}
|
||||
|
||||
int ConsoleClass::peek() {
|
||||
doBuffer();
|
||||
if (inBuffered == 0)
|
||||
return -1; // no chars available
|
||||
else
|
||||
return inBuffer[inReadPos];
|
||||
}
|
||||
|
||||
void ConsoleClass::doBuffer() {
|
||||
// If there are already char in buffer exit
|
||||
if (inBuffered > 0)
|
||||
return;
|
||||
|
||||
// Try to buffer up to 32 characters
|
||||
inReadPos = 0;
|
||||
uint8_t tmp[] = { 'p', BUFFER_SIZE };
|
||||
inBuffered = bridge.transfer(tmp, 2, inBuffer, BUFFER_SIZE);
|
||||
}
|
||||
|
||||
void ConsoleClass::begin() {
|
||||
bridge.begin();
|
||||
end();
|
||||
inBuffer = new uint8_t[BUFFER_SIZE];
|
||||
}
|
||||
|
||||
void ConsoleClass::end() {
|
||||
noBuffer();
|
||||
if (inBuffer) {
|
||||
delete[] inBuffer;
|
||||
inBuffer = NULL;
|
||||
}
|
||||
}
|
||||
|
||||
ConsoleClass Console;
|
69
libraries/Bridge/src/Console.h
Normal file
69
libraries/Bridge/src/Console.h
Normal file
@ -0,0 +1,69 @@
|
||||
/*
|
||||
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
|
||||
*/
|
||||
|
||||
#ifndef CONSOLE_H_
|
||||
#define CONSOLE_H_
|
||||
|
||||
#include <Bridge.h>
|
||||
|
||||
class ConsoleClass : public Stream {
|
||||
public:
|
||||
// Default constructor uses global Bridge instance
|
||||
ConsoleClass();
|
||||
// Constructor with a user provided BridgeClass instance
|
||||
ConsoleClass(BridgeClass &_b);
|
||||
~ConsoleClass();
|
||||
|
||||
void begin();
|
||||
void end();
|
||||
|
||||
void buffer(uint8_t size);
|
||||
void noBuffer();
|
||||
|
||||
bool connected();
|
||||
|
||||
// Stream methods
|
||||
// (read from console socket)
|
||||
int available();
|
||||
int read();
|
||||
int peek();
|
||||
// (write to console socket)
|
||||
size_t write(uint8_t);
|
||||
size_t write(const uint8_t *buffer, size_t size);
|
||||
void flush();
|
||||
|
||||
operator bool () { return connected(); }
|
||||
|
||||
private:
|
||||
BridgeClass &bridge;
|
||||
|
||||
void doBuffer();
|
||||
uint8_t inBuffered;
|
||||
uint8_t inReadPos;
|
||||
static const int BUFFER_SIZE = 32;
|
||||
uint8_t *inBuffer;
|
||||
|
||||
bool autoFlush;
|
||||
uint8_t outBuffered;
|
||||
uint8_t outBufferSize;
|
||||
uint8_t *outBuffer;
|
||||
};
|
||||
|
||||
extern ConsoleClass Console;
|
||||
|
||||
#endif
|
250
libraries/Bridge/src/FileIO.cpp
Normal file
250
libraries/Bridge/src/FileIO.cpp
Normal file
@ -0,0 +1,250 @@
|
||||
/*
|
||||
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 <FileIO.h>
|
||||
|
||||
|
||||
|
||||
File::File(BridgeClass &b) : mode(255), bridge(b) {
|
||||
// Empty
|
||||
}
|
||||
|
||||
File::File(const char *_filename, uint8_t _mode, BridgeClass &b) : mode(_mode), bridge(b) {
|
||||
filename = _filename;
|
||||
char modes[] = {'r','w','a'};
|
||||
uint8_t cmd[] = {'F', modes[mode]};
|
||||
uint8_t res[2];
|
||||
dirPosition = 1;
|
||||
bridge.transfer(cmd, 2, (uint8_t*)filename.c_str(), filename.length(), res, 2);
|
||||
if (res[0] != 0) { // res[0] contains error code
|
||||
mode = 255; // In case of error keep the file closed
|
||||
return;
|
||||
}
|
||||
handle = res[1];
|
||||
buffered = 0;
|
||||
}
|
||||
|
||||
File::operator bool() {
|
||||
return (mode != 255);
|
||||
}
|
||||
|
||||
File::~File() {
|
||||
close();
|
||||
}
|
||||
|
||||
size_t File::write(uint8_t c) {
|
||||
return write(&c, 1);
|
||||
}
|
||||
|
||||
size_t File::write(const uint8_t *buf, size_t size) {
|
||||
if (mode == 255)
|
||||
return -1;
|
||||
uint8_t cmd[] = {'g', handle};
|
||||
uint8_t res[1];
|
||||
bridge.transfer(cmd, 2, buf, size, res, 1);
|
||||
if (res[0] != 0) // res[0] contains error code
|
||||
return -res[0];
|
||||
return size;
|
||||
}
|
||||
|
||||
int File::read() {
|
||||
doBuffer();
|
||||
if (buffered == 0)
|
||||
return -1; // no chars available
|
||||
else {
|
||||
buffered--;
|
||||
return buffer[readPos++];
|
||||
}
|
||||
}
|
||||
|
||||
int File::peek() {
|
||||
doBuffer();
|
||||
if (buffered == 0)
|
||||
return -1; // no chars available
|
||||
else
|
||||
return buffer[readPos];
|
||||
}
|
||||
|
||||
boolean File::seek(uint32_t position) {
|
||||
uint8_t cmd[] = {
|
||||
's',
|
||||
handle,
|
||||
(position >> 24) & 0xFF,
|
||||
(position >> 16) & 0xFF,
|
||||
(position >> 8) & 0xFF,
|
||||
position & 0xFF
|
||||
};
|
||||
uint8_t res[1];
|
||||
bridge.transfer(cmd, 6, res, 1);
|
||||
if (res[0]==0) {
|
||||
// If seek succeed then flush buffers
|
||||
buffered = 0;
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
uint32_t File::position() {
|
||||
uint8_t cmd[] = {'S', handle};
|
||||
uint8_t res[5];
|
||||
bridge.transfer(cmd, 2, res, 5);
|
||||
//err = res[0]; // res[0] contains error code
|
||||
uint32_t pos = res[1] << 24;
|
||||
pos += res[2] << 16;
|
||||
pos += res[3] << 8;
|
||||
pos += res[4];
|
||||
return pos - buffered;
|
||||
}
|
||||
|
||||
void File::doBuffer() {
|
||||
// If there are already char in buffer exit
|
||||
if (buffered > 0)
|
||||
return;
|
||||
|
||||
// Try to buffer up to 32 characters
|
||||
readPos = 0;
|
||||
uint8_t cmd[] = {'G', handle, sizeof(buffer)};
|
||||
buffered = bridge.transfer(cmd, 3, buffer, sizeof(buffer)) - 1;
|
||||
//err = buff[0]; // First byte is error code
|
||||
if (buffered>0) {
|
||||
// Shift the reminder of buffer
|
||||
for (uint8_t i=0; i<buffered; i++)
|
||||
buffer[i] = buffer[i+1];
|
||||
}
|
||||
}
|
||||
|
||||
int File::available() {
|
||||
// Look if there is new data available
|
||||
doBuffer();
|
||||
return buffered;
|
||||
}
|
||||
|
||||
void File::flush() {
|
||||
}
|
||||
|
||||
//int read(void *buf, uint16_t nbyte)
|
||||
|
||||
//uint32_t size()
|
||||
|
||||
void File::close() {
|
||||
if (mode == 255)
|
||||
return;
|
||||
uint8_t cmd[] = {'f', handle};
|
||||
bridge.transfer(cmd, 2);
|
||||
mode = 255;
|
||||
}
|
||||
|
||||
const char *File::name() {
|
||||
return filename.c_str();
|
||||
}
|
||||
|
||||
|
||||
boolean File::isDirectory() {
|
||||
uint8_t res[1];
|
||||
uint8_t lenght;
|
||||
uint8_t cmd[] = {'i'};
|
||||
if (mode != 255)
|
||||
return 0;
|
||||
|
||||
bridge.transfer(cmd, 1, (uint8_t *)filename.c_str(), filename.length(), res, 1);
|
||||
return res[0];
|
||||
}
|
||||
|
||||
|
||||
File File::openNextFile(uint8_t mode){
|
||||
Process awk;
|
||||
char tmp;
|
||||
String command;
|
||||
String filepath;
|
||||
if (dirPosition == 0xFFFF) return File();
|
||||
|
||||
command = "ls ";
|
||||
command += filename;
|
||||
command += " | awk 'NR==";
|
||||
command += dirPosition;
|
||||
command += "'";
|
||||
|
||||
awk.runShellCommand(command);
|
||||
|
||||
while(awk.running());
|
||||
|
||||
command = "";
|
||||
|
||||
while (awk.available()){
|
||||
tmp = awk.read();
|
||||
if (tmp!='\n') command += tmp;
|
||||
}
|
||||
if (command.length() == 0)
|
||||
return File();
|
||||
dirPosition++;
|
||||
filepath = filename + "/" + command;
|
||||
return File(filepath.c_str(),mode);
|
||||
|
||||
}
|
||||
|
||||
void File::rewindDirectory(void){
|
||||
dirPosition = 1;
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
boolean FileSystemClass::begin() {
|
||||
return true;
|
||||
}
|
||||
|
||||
File FileSystemClass::open(const char *filename, uint8_t mode) {
|
||||
return File(filename, mode);
|
||||
}
|
||||
|
||||
boolean FileSystemClass::exists(const char *filepath) {
|
||||
Process ls;
|
||||
ls.begin("ls");
|
||||
ls.addParameter(filepath);
|
||||
int res = ls.run();
|
||||
return (res == 0);
|
||||
}
|
||||
|
||||
boolean FileSystemClass::mkdir(const char *filepath) {
|
||||
Process mk;
|
||||
mk.begin("mkdir");
|
||||
mk.addParameter("-p");
|
||||
mk.addParameter(filepath);
|
||||
int res = mk.run();
|
||||
return (res == 0);
|
||||
}
|
||||
|
||||
boolean FileSystemClass::remove(const char *filepath) {
|
||||
Process rm;
|
||||
rm.begin("rm");
|
||||
rm.addParameter(filepath);
|
||||
int res = rm.run();
|
||||
return (res == 0);
|
||||
}
|
||||
|
||||
boolean FileSystemClass::rmdir(const char *filepath) {
|
||||
Process rm;
|
||||
rm.begin("rmdir");
|
||||
rm.addParameter(filepath);
|
||||
int res = rm.run();
|
||||
return (res == 0);
|
||||
}
|
||||
|
||||
FileSystemClass FileSystem;
|
103
libraries/Bridge/src/FileIO.h
Normal file
103
libraries/Bridge/src/FileIO.h
Normal file
@ -0,0 +1,103 @@
|
||||
/*
|
||||
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
|
||||
*/
|
||||
|
||||
#ifndef __FILEIO_H__
|
||||
#define __FILEIO_H__
|
||||
|
||||
#include <Process.h>
|
||||
|
||||
#define FILE_READ 0
|
||||
#define FILE_WRITE 1
|
||||
#define FILE_APPEND 2
|
||||
|
||||
class File : public Stream {
|
||||
|
||||
public:
|
||||
File(BridgeClass &b = Bridge);
|
||||
File(const char *_filename, uint8_t _mode, BridgeClass &b = Bridge);
|
||||
~File();
|
||||
|
||||
virtual size_t write(uint8_t);
|
||||
virtual size_t write(const uint8_t *buf, size_t size);
|
||||
virtual int read();
|
||||
virtual int peek();
|
||||
virtual int available();
|
||||
virtual void flush();
|
||||
int read(void *buf, uint16_t nbyte);
|
||||
boolean seek(uint32_t pos);
|
||||
uint32_t position();
|
||||
uint32_t size();
|
||||
void close();
|
||||
operator bool();
|
||||
const char * name();
|
||||
boolean isDirectory();
|
||||
File openNextFile(uint8_t mode = FILE_READ);
|
||||
void rewindDirectory(void);
|
||||
|
||||
//using Print::write;
|
||||
|
||||
private:
|
||||
void doBuffer();
|
||||
uint8_t buffered;
|
||||
uint8_t readPos;
|
||||
uint16_t dirPosition;
|
||||
static const int BUFFER_SIZE = 64;
|
||||
uint8_t buffer[BUFFER_SIZE];
|
||||
|
||||
|
||||
private:
|
||||
BridgeClass &bridge;
|
||||
String filename;
|
||||
uint8_t mode;
|
||||
uint8_t handle;
|
||||
|
||||
};
|
||||
|
||||
class FileSystemClass {
|
||||
public:
|
||||
FileSystemClass() : bridge(Bridge) { }
|
||||
FileSystemClass(BridgeClass &_b) : bridge(_b) { }
|
||||
|
||||
boolean begin();
|
||||
|
||||
// Open the specified file/directory with the supplied mode (e.g. read or
|
||||
// write, etc). Returns a File object for interacting with the file.
|
||||
// Note that currently only one file can be open at a time.
|
||||
File open(const char *filename, uint8_t mode = FILE_READ);
|
||||
|
||||
// Methods to determine if the requested file path exists.
|
||||
boolean exists(const char *filepath);
|
||||
|
||||
// Create the requested directory heirarchy--if intermediate directories
|
||||
// do not exist they will be created.
|
||||
boolean mkdir(const char *filepath);
|
||||
|
||||
// Delete the file.
|
||||
boolean remove(const char *filepath);
|
||||
|
||||
boolean rmdir(const char *filepath);
|
||||
|
||||
private:
|
||||
friend class File;
|
||||
|
||||
BridgeClass &bridge;
|
||||
};
|
||||
|
||||
extern FileSystemClass FileSystem;
|
||||
|
||||
#endif
|
53
libraries/Bridge/src/HttpClient.cpp
Normal file
53
libraries/Bridge/src/HttpClient.cpp
Normal file
@ -0,0 +1,53 @@
|
||||
/*
|
||||
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 "HttpClient.h"
|
||||
|
||||
unsigned int HttpClient::get(String &url) {
|
||||
begin("curl");
|
||||
addParameter(url);
|
||||
return run();
|
||||
}
|
||||
|
||||
unsigned int HttpClient::get(const char *url) {
|
||||
begin("curl");
|
||||
addParameter(url);
|
||||
return run();
|
||||
}
|
||||
|
||||
void HttpClient::getAsynchronously(String &url) {
|
||||
begin("curl");
|
||||
addParameter(url);
|
||||
runAsynchronously();
|
||||
}
|
||||
|
||||
void HttpClient::getAsynchronously(const char *url) {
|
||||
begin("curl");
|
||||
addParameter(url);
|
||||
runAsynchronously();
|
||||
}
|
||||
|
||||
boolean HttpClient::ready() {
|
||||
return running();
|
||||
}
|
||||
|
||||
unsigned int HttpClient::getResult() {
|
||||
return exitValue();
|
||||
}
|
||||
|
||||
|
36
libraries/Bridge/src/HttpClient.h
Normal file
36
libraries/Bridge/src/HttpClient.h
Normal file
@ -0,0 +1,36 @@
|
||||
/*
|
||||
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
|
||||
*/
|
||||
|
||||
#ifndef HTTPCLIENT_H_
|
||||
#define HTTPCLIENT_H_
|
||||
|
||||
#include <Process.h>
|
||||
|
||||
class HttpClient : public Process {
|
||||
public:
|
||||
|
||||
unsigned int get(String &url);
|
||||
unsigned int get(const char * url);
|
||||
void getAsynchronously(String &url);
|
||||
void getAsynchronously(const char * url);
|
||||
boolean ready();
|
||||
unsigned int getResult();
|
||||
|
||||
};
|
||||
|
||||
#endif /* HTTPCLIENT_H_ */
|
56
libraries/Bridge/src/Mailbox.cpp
Normal file
56
libraries/Bridge/src/Mailbox.cpp
Normal file
@ -0,0 +1,56 @@
|
||||
/*
|
||||
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 <Mailbox.h>
|
||||
|
||||
unsigned int MailboxClass::readMessage(uint8_t *buff, unsigned int size) {
|
||||
uint8_t tmp[] = { 'm' };
|
||||
return bridge.transfer(tmp, 1, buff, size);
|
||||
}
|
||||
|
||||
void MailboxClass::readMessage(String &str, unsigned int maxLength) {
|
||||
uint8_t tmp[] = { 'm' };
|
||||
// XXX: Is there a better way to create the string?
|
||||
uint8_t buff[maxLength+1];
|
||||
int l = bridge.transfer(tmp, 1, buff, maxLength);
|
||||
buff[l] = 0;
|
||||
str = (const char *)buff;
|
||||
}
|
||||
|
||||
void MailboxClass::writeMessage(const uint8_t *buff, unsigned int size) {
|
||||
uint8_t cmd[] = {'M'};
|
||||
bridge.transfer(cmd, 1, buff, size, NULL, 0);
|
||||
}
|
||||
|
||||
void MailboxClass::writeMessage(const String& str) {
|
||||
writeMessage((uint8_t*) str.c_str(), str.length());
|
||||
}
|
||||
|
||||
void MailboxClass::writeJSON(const String& str) {
|
||||
uint8_t cmd[] = {'J'};
|
||||
bridge.transfer(cmd, 1, (uint8_t*) str.c_str(), str.length(), NULL, 0);
|
||||
}
|
||||
|
||||
unsigned int MailboxClass::messageAvailable() {
|
||||
uint8_t tmp[] = {'n'};
|
||||
uint8_t res[2];
|
||||
bridge.transfer(tmp, 1, res, 2);
|
||||
return (res[0] << 8) + res[1];
|
||||
}
|
||||
|
||||
MailboxClass Mailbox(Bridge);
|
53
libraries/Bridge/src/Mailbox.h
Normal file
53
libraries/Bridge/src/Mailbox.h
Normal file
@ -0,0 +1,53 @@
|
||||
/*
|
||||
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
|
||||
*/
|
||||
|
||||
#ifndef _MAILBOX_CLASS_H_INCLUDED_
|
||||
#define _MAILBOX_CLASS_H_INCLUDED_
|
||||
|
||||
#include <Bridge.h>
|
||||
|
||||
class MailboxClass {
|
||||
public:
|
||||
MailboxClass(BridgeClass &b = Bridge) : bridge(b) { }
|
||||
|
||||
void begin() { }
|
||||
void end() { }
|
||||
|
||||
// Receive a message and store it inside a buffer
|
||||
unsigned int readMessage(uint8_t *buffer, unsigned int size);
|
||||
// Receive a message and store it inside a String
|
||||
void readMessage(String &str, unsigned int maxLength=128);
|
||||
|
||||
// Send a message
|
||||
void writeMessage(const uint8_t *buffer, unsigned int size);
|
||||
// Send a message
|
||||
void writeMessage(const String& str);
|
||||
// Send a JSON message
|
||||
void writeJSON(const String& str);
|
||||
|
||||
// Return the size of the next available message, 0 if there are
|
||||
// no messages in queue.
|
||||
unsigned int messageAvailable();
|
||||
|
||||
private:
|
||||
BridgeClass &bridge;
|
||||
};
|
||||
|
||||
extern MailboxClass Mailbox;
|
||||
|
||||
#endif // _MAILBOX_CLASS_H_INCLUDED_
|
142
libraries/Bridge/src/Process.cpp
Normal file
142
libraries/Bridge/src/Process.cpp
Normal file
@ -0,0 +1,142 @@
|
||||
/*
|
||||
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 <Process.h>
|
||||
|
||||
Process::~Process() {
|
||||
close();
|
||||
}
|
||||
|
||||
size_t Process::write(uint8_t c) {
|
||||
uint8_t cmd[] = {'I', handle, c};
|
||||
bridge.transfer(cmd, 3);
|
||||
return 1;
|
||||
}
|
||||
|
||||
void Process::flush() {
|
||||
}
|
||||
|
||||
int Process::available() {
|
||||
// Look if there is new data available
|
||||
doBuffer();
|
||||
return buffered;
|
||||
}
|
||||
|
||||
int Process::read() {
|
||||
doBuffer();
|
||||
if (buffered == 0)
|
||||
return -1; // no chars available
|
||||
else {
|
||||
buffered--;
|
||||
return buffer[readPos++];
|
||||
}
|
||||
}
|
||||
|
||||
int Process::peek() {
|
||||
doBuffer();
|
||||
if (buffered == 0)
|
||||
return -1; // no chars available
|
||||
else
|
||||
return buffer[readPos];
|
||||
}
|
||||
|
||||
void Process::doBuffer() {
|
||||
// If there are already char in buffer exit
|
||||
if (buffered > 0)
|
||||
return;
|
||||
|
||||
// Try to buffer up to 32 characters
|
||||
readPos = 0;
|
||||
uint8_t cmd[] = {'O', handle, sizeof(buffer)};
|
||||
buffered = bridge.transfer(cmd, 3, buffer, sizeof(buffer));
|
||||
}
|
||||
|
||||
void Process::begin(const String &command) {
|
||||
close();
|
||||
cmdline = new String(command);
|
||||
}
|
||||
|
||||
void Process::addParameter(const String ¶m) {
|
||||
*cmdline += "\xFE";
|
||||
*cmdline += param;
|
||||
}
|
||||
|
||||
void Process::runAsynchronously() {
|
||||
uint8_t cmd[] = {'R'};
|
||||
uint8_t res[2];
|
||||
bridge.transfer(cmd, 1, (uint8_t*)cmdline->c_str(), cmdline->length(), res, 2);
|
||||
handle = res[1];
|
||||
|
||||
delete cmdline;
|
||||
cmdline = NULL;
|
||||
|
||||
if (res[0]==0) // res[0] contains error code
|
||||
started = true;
|
||||
}
|
||||
|
||||
boolean Process::running() {
|
||||
uint8_t cmd[] = {'r', handle};
|
||||
uint8_t res[1];
|
||||
bridge.transfer(cmd, 2, res, 1);
|
||||
return (res[0] == 1);
|
||||
}
|
||||
|
||||
unsigned int Process::exitValue() {
|
||||
uint8_t cmd[] = {'W', handle};
|
||||
uint8_t res[2];
|
||||
bridge.transfer(cmd, 2, res, 2);
|
||||
return (res[0] << 8) + res[1];
|
||||
}
|
||||
|
||||
unsigned int Process::run() {
|
||||
runAsynchronously();
|
||||
while (running())
|
||||
delay(100);
|
||||
return exitValue();
|
||||
}
|
||||
|
||||
void Process::close() {
|
||||
if (started) {
|
||||
uint8_t cmd[] = {'w', handle};
|
||||
bridge.transfer(cmd, 2);
|
||||
}
|
||||
started = false;
|
||||
}
|
||||
|
||||
unsigned int Process::runShellCommand(const String &command) {
|
||||
runShellCommandAsynchronously(command);
|
||||
while (running())
|
||||
delay(100);
|
||||
return exitValue();
|
||||
}
|
||||
|
||||
void Process::runShellCommandAsynchronously(const String &command) {
|
||||
begin("/bin/ash");
|
||||
addParameter("-c");
|
||||
addParameter(command);
|
||||
runAsynchronously();
|
||||
}
|
||||
|
||||
// This method is currently unused
|
||||
//static unsigned int __commandOutputAvailable(uint8_t handle) {
|
||||
// uint8_t cmd[] = {'o', handle};
|
||||
// uint8_t res[1];
|
||||
// Bridge.transfer(cmd, 2, res, 1);
|
||||
// return res[0];
|
||||
//}
|
||||
|
69
libraries/Bridge/src/Process.h
Normal file
69
libraries/Bridge/src/Process.h
Normal file
@ -0,0 +1,69 @@
|
||||
/*
|
||||
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
|
||||
*/
|
||||
|
||||
#ifndef PROCESS_H_
|
||||
#define PROCESS_H_
|
||||
|
||||
#include <Bridge.h>
|
||||
|
||||
class Process : public Stream {
|
||||
public:
|
||||
// Constructor with a user provided BridgeClass instance
|
||||
Process(BridgeClass &_b = Bridge) :
|
||||
bridge(_b), started(false), buffered(0), readPos(0) { }
|
||||
~Process();
|
||||
|
||||
void begin(const String &command);
|
||||
void addParameter(const String ¶m);
|
||||
unsigned int run();
|
||||
void runAsynchronously();
|
||||
boolean running();
|
||||
unsigned int exitValue();
|
||||
void close();
|
||||
|
||||
unsigned int runShellCommand(const String &command);
|
||||
void runShellCommandAsynchronously(const String &command);
|
||||
|
||||
operator bool () { return started; }
|
||||
|
||||
// Stream methods
|
||||
// (read from process stdout)
|
||||
int available();
|
||||
int read();
|
||||
int peek();
|
||||
// (write to process stdin)
|
||||
size_t write(uint8_t);
|
||||
void flush();
|
||||
// TODO: add optimized function for block write
|
||||
|
||||
private:
|
||||
BridgeClass &bridge;
|
||||
unsigned int handle;
|
||||
String *cmdline;
|
||||
boolean started;
|
||||
|
||||
private:
|
||||
void doBuffer();
|
||||
uint8_t buffered;
|
||||
uint8_t readPos;
|
||||
static const int BUFFER_SIZE = 64;
|
||||
uint8_t buffer[BUFFER_SIZE];
|
||||
|
||||
};
|
||||
|
||||
#endif
|
167
libraries/Bridge/src/YunClient.cpp
Normal file
167
libraries/Bridge/src/YunClient.cpp
Normal file
@ -0,0 +1,167 @@
|
||||
/*
|
||||
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 <YunClient.h>
|
||||
|
||||
YunClient::YunClient(int _h, BridgeClass &_b) :
|
||||
bridge(_b), handle(_h), opened(true), buffered(0) {
|
||||
}
|
||||
|
||||
YunClient::YunClient(BridgeClass &_b) :
|
||||
bridge(_b), handle(0), opened(false), buffered(0) {
|
||||
}
|
||||
|
||||
YunClient::~YunClient() {
|
||||
}
|
||||
|
||||
YunClient& YunClient::operator=(const YunClient &_x) {
|
||||
opened = _x.opened;
|
||||
handle = _x.handle;
|
||||
return *this;
|
||||
}
|
||||
|
||||
void YunClient::stop() {
|
||||
if (opened) {
|
||||
uint8_t cmd[] = {'j', handle};
|
||||
bridge.transfer(cmd, 2);
|
||||
}
|
||||
opened = false;
|
||||
}
|
||||
|
||||
void YunClient::doBuffer() {
|
||||
// If there are already char in buffer exit
|
||||
if (buffered > 0)
|
||||
return;
|
||||
|
||||
// Try to buffer up to 32 characters
|
||||
readPos = 0;
|
||||
uint8_t cmd[] = {'K', handle, sizeof(buffer)};
|
||||
buffered = bridge.transfer(cmd, 3, buffer, sizeof(buffer));
|
||||
}
|
||||
|
||||
int YunClient::available() {
|
||||
// Look if there is new data available
|
||||
doBuffer();
|
||||
return buffered;
|
||||
}
|
||||
|
||||
int YunClient::read() {
|
||||
doBuffer();
|
||||
if (buffered == 0)
|
||||
return -1; // no chars available
|
||||
else {
|
||||
buffered--;
|
||||
return buffer[readPos++];
|
||||
}
|
||||
}
|
||||
|
||||
int YunClient::read(uint8_t *buff, size_t size) {
|
||||
int readed = 0;
|
||||
do {
|
||||
if (buffered == 0) {
|
||||
doBuffer();
|
||||
if (buffered == 0)
|
||||
return readed;
|
||||
}
|
||||
buff[readed++] = buffer[readPos++];
|
||||
buffered--;
|
||||
} while (readed < size);
|
||||
return readed;
|
||||
}
|
||||
|
||||
int YunClient::peek() {
|
||||
doBuffer();
|
||||
if (buffered == 0)
|
||||
return -1; // no chars available
|
||||
else
|
||||
return buffer[readPos];
|
||||
}
|
||||
|
||||
size_t YunClient::write(uint8_t c) {
|
||||
if (!opened)
|
||||
return 0;
|
||||
uint8_t cmd[] = {'l', handle, c};
|
||||
bridge.transfer(cmd, 3);
|
||||
return 1;
|
||||
}
|
||||
|
||||
size_t YunClient::write(const uint8_t *buf, size_t size) {
|
||||
if (!opened)
|
||||
return 0;
|
||||
uint8_t cmd[] = {'l', handle};
|
||||
bridge.transfer(cmd, 2, buf, size, NULL, 0);
|
||||
return size;
|
||||
}
|
||||
|
||||
void YunClient::flush() {
|
||||
}
|
||||
|
||||
uint8_t YunClient::connected() {
|
||||
if (!opened)
|
||||
return false;
|
||||
uint8_t cmd[] = {'L', handle};
|
||||
uint8_t res[1];
|
||||
bridge.transfer(cmd, 2, res, 1);
|
||||
return (res[0] == 1);
|
||||
}
|
||||
|
||||
int YunClient::connect(IPAddress ip, uint16_t port) {
|
||||
String address;
|
||||
address.reserve(18);
|
||||
address += ip[0];
|
||||
address += '.';
|
||||
address += ip[1];
|
||||
address += '.';
|
||||
address += ip[2];
|
||||
address += '.';
|
||||
address += ip[3];
|
||||
return connect(address.c_str(), port);
|
||||
}
|
||||
|
||||
int YunClient::connect(const char *host, uint16_t port) {
|
||||
uint8_t tmp[] = {
|
||||
'C',
|
||||
(port >> 8) & 0xFF,
|
||||
port & 0xFF
|
||||
};
|
||||
uint8_t res[1];
|
||||
int l = bridge.transfer(tmp, 3, (const uint8_t *)host, strlen(host), res, 1);
|
||||
if (l==0)
|
||||
return 0;
|
||||
handle = res[0];
|
||||
|
||||
// wait for connection
|
||||
uint8_t tmp2[] = { 'c', handle };
|
||||
uint8_t res2[1];
|
||||
while (true) {
|
||||
bridge.transfer(tmp2, 2, res2, 1);
|
||||
if (res2[0] == 0)
|
||||
break;
|
||||
delay(1);
|
||||
}
|
||||
opened = true;
|
||||
|
||||
// check for successful connection
|
||||
if (connected())
|
||||
return 1;
|
||||
|
||||
opened = false;
|
||||
handle = 0;
|
||||
return 0;
|
||||
}
|
||||
|
68
libraries/Bridge/src/YunClient.h
Normal file
68
libraries/Bridge/src/YunClient.h
Normal file
@ -0,0 +1,68 @@
|
||||
/*
|
||||
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
|
||||
*/
|
||||
|
||||
#ifndef _YUN_CLIENT_H_
|
||||
#define _YUN_CLIENT_H_
|
||||
|
||||
#include <Bridge.h>
|
||||
#include <Client.h>
|
||||
|
||||
class YunClient : public Client {
|
||||
public:
|
||||
// Constructor with a user provided BridgeClass instance
|
||||
YunClient(int _h, BridgeClass &_b = Bridge);
|
||||
YunClient(BridgeClass &_b = Bridge);
|
||||
~YunClient();
|
||||
|
||||
// Stream methods
|
||||
// (read message)
|
||||
virtual int available();
|
||||
virtual int read();
|
||||
virtual int read(uint8_t *buf, size_t size);
|
||||
virtual int peek();
|
||||
// (write response)
|
||||
virtual size_t write(uint8_t);
|
||||
virtual size_t write(const uint8_t *buf, size_t size);
|
||||
virtual void flush();
|
||||
// TODO: add optimized function for block write
|
||||
|
||||
virtual operator bool () { return opened; }
|
||||
|
||||
YunClient& operator=(const YunClient &_x);
|
||||
|
||||
virtual void stop();
|
||||
virtual uint8_t connected();
|
||||
|
||||
virtual int connect(IPAddress ip, uint16_t port);
|
||||
virtual int connect(const char *host, uint16_t port);
|
||||
|
||||
private:
|
||||
BridgeClass &bridge;
|
||||
unsigned int handle;
|
||||
boolean opened;
|
||||
|
||||
private:
|
||||
void doBuffer();
|
||||
uint8_t buffered;
|
||||
uint8_t readPos;
|
||||
static const int BUFFER_SIZE = 64;
|
||||
uint8_t buffer[BUFFER_SIZE];
|
||||
|
||||
};
|
||||
|
||||
#endif // _YUN_CLIENT_H_
|
54
libraries/Bridge/src/YunServer.cpp
Normal file
54
libraries/Bridge/src/YunServer.cpp
Normal file
@ -0,0 +1,54 @@
|
||||
/*
|
||||
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 <YunServer.h>
|
||||
#include <YunClient.h>
|
||||
|
||||
YunServer::YunServer(uint16_t _p, BridgeClass &_b) :
|
||||
bridge(_b), port(_p), listening(false), useLocalhost(false) {
|
||||
}
|
||||
|
||||
void YunServer::begin() {
|
||||
uint8_t tmp[] = {
|
||||
'N',
|
||||
(port >> 8) & 0xFF,
|
||||
port & 0xFF
|
||||
};
|
||||
uint8_t res[1];
|
||||
String address = F("127.0.0.1");
|
||||
if (!useLocalhost)
|
||||
address = F("0.0.0.0");
|
||||
bridge.transfer(tmp, 3, (const uint8_t *)address.c_str(), address.length(), res, 1);
|
||||
listening = (res[0] == 1);
|
||||
}
|
||||
|
||||
YunClient YunServer::accept() {
|
||||
uint8_t cmd[] = {'k'};
|
||||
uint8_t res[1];
|
||||
unsigned int l = bridge.transfer(cmd, 1, res, 1);
|
||||
if (l==0)
|
||||
return YunClient();
|
||||
return YunClient(res[0]);
|
||||
}
|
||||
|
||||
size_t YunServer::write(uint8_t c) {
|
||||
uint8_t cmd[] = { 'b', c };
|
||||
bridge.transfer(cmd, 2);
|
||||
return 1;
|
||||
}
|
||||
|
47
libraries/Bridge/src/YunServer.h
Normal file
47
libraries/Bridge/src/YunServer.h
Normal file
@ -0,0 +1,47 @@
|
||||
/*
|
||||
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
|
||||
*/
|
||||
|
||||
#ifndef _YUN_SERVER_H_
|
||||
#define _YUN_SERVER_H_
|
||||
|
||||
#include <Bridge.h>
|
||||
#include <Server.h>
|
||||
|
||||
class YunClient;
|
||||
|
||||
class YunServer : public Server {
|
||||
public:
|
||||
// Constructor with a user provided BridgeClass instance
|
||||
YunServer(uint16_t port = 5555, BridgeClass &_b = Bridge);
|
||||
|
||||
void begin();
|
||||
YunClient accept();
|
||||
|
||||
virtual size_t write(uint8_t c);
|
||||
|
||||
void listenOnLocalhost() { useLocalhost = true; }
|
||||
void noListenOnLocalhost() { useLocalhost = false; }
|
||||
|
||||
private:
|
||||
uint16_t port;
|
||||
bool listening;
|
||||
bool useLocalhost;
|
||||
BridgeClass &bridge;
|
||||
};
|
||||
|
||||
#endif // _YUN_SERVER_H_
|
Reference in New Issue
Block a user