added arduino, modified build

This commit is contained in:
2020-02-02 15:28:36 -08:00
parent 0189d519c6
commit 6480bc593f
3583 changed files with 1305025 additions and 247 deletions

View File

@@ -0,0 +1,312 @@
/*
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

View File

@@ -0,0 +1,131 @@
/*
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_
#ifndef BRIDGE_BAUDRATE
#define BRIDGE_BAUDRATE 250000
#endif
#include <Arduino.h>
#include <Stream.h>
class BridgeClass {
public:
BridgeClass(Stream &_stream);
void begin();
void end();
// 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)
{
return 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);
}
uint16_t getBridgeVersion()
{
return bridgeVersion;
}
static const uint16_t TRANSFER_TIMEOUT = 0xFFFF;
private:
uint8_t index;
int timedRead(unsigned int timeout);
void dropAll();
uint16_t bridgeVersion;
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(unsigned long baudrate = BRIDGE_BAUDRATE) {
serial.begin(baudrate);
BridgeClass::begin();
}
void end(unsigned long baudrate = BRIDGE_BAUDRATE) {
serial.begin(baudrate);
BridgeClass::end();
}
private:
HardwareSerial &serial;
};
extern SerialBridgeClass Bridge;
// Some microcrontrollers don't start the bootloader after a reset.
// This function is intended to let the microcontroller erase its
// flash after checking a specific signal coming from the external
// device without the need to press the erase button on the board.
// The purpose is to enable a software update that does not require
// a manual interaction with the board.
extern void checkForRemoteSketchUpdate(uint8_t pin = 7);
#endif /* BRIDGE_H_ */
#include <Console.h>
#include <Process.h>

View File

@@ -0,0 +1,207 @@
/*
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 <BridgeClient.h>
BridgeClient::BridgeClient(uint8_t _h, BridgeClass &_b) :
bridge(_b), handle(_h), opened(true), buffered(0) {
}
BridgeClient::BridgeClient(BridgeClass &_b) :
bridge(_b), handle(0), opened(false), buffered(0) {
}
BridgeClient::~BridgeClient() {
}
BridgeClient& BridgeClient::operator=(const BridgeClient &_x) {
opened = _x.opened;
handle = _x.handle;
return *this;
}
void BridgeClient::stop() {
if (opened) {
uint8_t cmd[] = {'j', handle};
bridge.transfer(cmd, 2);
}
opened = false;
buffered = 0;
readPos = 0;
}
void BridgeClient::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 BridgeClient::available() {
// Look if there is new data available
doBuffer();
return buffered;
}
int BridgeClient::read() {
doBuffer();
if (buffered == 0)
return -1; // no chars available
else {
buffered--;
return buffer[readPos++];
}
}
int BridgeClient::read(uint8_t *buff, size_t size) {
size_t readed = 0;
do {
if (buffered == 0) {
doBuffer();
if (buffered == 0)
return readed;
}
buff[readed++] = buffer[readPos++];
buffered--;
} while (readed < size);
return readed;
}
int BridgeClient::peek() {
doBuffer();
if (buffered == 0)
return -1; // no chars available
else
return buffer[readPos];
}
size_t BridgeClient::write(uint8_t c) {
if (!opened)
return 0;
uint8_t cmd[] = {'l', handle, c};
bridge.transfer(cmd, 3);
return 1;
}
size_t BridgeClient::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 BridgeClient::flush() {
}
uint8_t BridgeClient::connected() {
if (!opened)
return false;
// Client is "connected" if it has unread bytes
if (available())
return true;
uint8_t cmd[] = {'L', handle};
uint8_t res[1];
bridge.transfer(cmd, 2, res, 1);
return (res[0] == 1);
}
int BridgeClient::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 BridgeClient::connect(const char *host, uint16_t port) {
uint8_t tmp[] = {
'C',
static_cast<uint8_t>(port >> 8),
static_cast<uint8_t>(port)
};
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;
stop();
handle = 0;
return 0;
}
int BridgeClient::connectSSL(const char *host, uint16_t port) {
if (bridge.getBridgeVersion() < 161)
return -1;
uint8_t tmp[] = {
'Z',
static_cast<uint8_t>(port >> 8),
static_cast<uint8_t>(port)
};
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;
stop();
handle = 0;
return 0;
}

View File

@@ -0,0 +1,71 @@
/*
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_CLIENT_H_
#define _BRIDGE_CLIENT_H_
#include <Bridge.h>
#include <Client.h>
class BridgeClient : public Client {
public:
// Constructor with a user provided BridgeClass instance
BridgeClient(uint8_t _h, BridgeClass &_b = Bridge);
BridgeClient(BridgeClass &_b = Bridge);
~BridgeClient();
// 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;
}
virtual BridgeClient& operator=(const BridgeClient &_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);
int connectSSL(const char* host, uint16_t port);
private:
BridgeClass &bridge;
uint8_t handle;
boolean opened;
private:
void doBuffer();
uint8_t buffered;
uint8_t readPos;
static const int BUFFER_SIZE = 64;
uint8_t buffer[BUFFER_SIZE];
};
#endif // _BRIDGE_CLIENT_H_

View File

@@ -0,0 +1,36 @@
/*
Copyright (c) 2016 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 <BridgeSSLClient.h>
BridgeSSLClient::BridgeSSLClient(uint8_t _h, BridgeClass &_b) :
BridgeClient(_h, _b)
{
}
BridgeSSLClient::BridgeSSLClient(BridgeClass &_b):
BridgeClient(_b)
{
}
BridgeSSLClient::~BridgeSSLClient() {
}
int BridgeSSLClient::connect(const char *host, uint16_t port) {
return BridgeClient::connectSSL(host, port);
}

View File

@@ -0,0 +1,36 @@
/*
Copyright (c) 2016 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_SSL_CLIENT_H_
#define _BRIDGE_SSL_CLIENT_H_
#include <Bridge.h>
#include <Client.h>
#include <BridgeClient.h>
class BridgeSSLClient : public BridgeClient {
public:
// Constructor with a user provided BridgeClass instance
BridgeSSLClient(uint8_t _h, BridgeClass &_b = Bridge);
BridgeSSLClient(BridgeClass &_b = Bridge);
~BridgeSSLClient();
virtual int connect(const char* host, uint16_t port);
};
#endif // _BRIDGE_SSL_CLIENT_H_

View 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 <BridgeServer.h>
#include <BridgeClient.h>
BridgeServer::BridgeServer(uint16_t _p, BridgeClass &_b) :
bridge(_b), port(_p), listening(false), useLocalhost(false) {
}
void BridgeServer::begin() {
uint8_t tmp[] = {
'N',
static_cast<uint8_t>(port >> 8),
static_cast<uint8_t>(port)
};
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);
}
BridgeClient BridgeServer::accept() {
uint8_t cmd[] = {'k'};
uint8_t res[1];
unsigned int l = bridge.transfer(cmd, 1, res, 1);
if (l == 0)
return BridgeClient();
return BridgeClient(res[0]);
}
size_t BridgeServer::write(uint8_t c) {
uint8_t cmd[] = { 'b', c };
bridge.transfer(cmd, 2);
return 1;
}

View File

@@ -0,0 +1,51 @@
/*
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_SERVER_H_
#define _BRIDGE_SERVER_H_
#include <Bridge.h>
#include <Server.h>
class BridgeClient;
class BridgeServer : public Server {
public:
// Constructor with a user provided BridgeClass instance
BridgeServer(uint16_t port = 5555, BridgeClass &_b = Bridge);
void begin();
BridgeClient accept();
virtual size_t write(uint8_t c);
void listenOnLocalhost() {
useLocalhost = true;
}
void noListenOnLocalhost() {
useLocalhost = false;
}
private:
BridgeClass &bridge;
uint16_t port;
bool listening;
bool useLocalhost;
};
#endif // _BRIDGE_SERVER_H_

View File

@@ -0,0 +1,198 @@
/*
Copyright (c) 2015 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 "BridgeUdp.h"
BridgeUDP::BridgeUDP(BridgeClass &_b) :
bridge(_b), opened(false), avail(0), buffered(0), readPos(0) {
}
/* Start BridgeUDP socket, listening at local port PORT */
uint8_t BridgeUDP::begin(uint16_t port) {
if (opened)
return 0;
uint8_t cmd[] = {'e', (uint8_t)((port >> 8) & 0xFF), (uint8_t)(port & 0xFF)};
uint8_t res[2];
bridge.transfer(cmd, 3, res, 2);
if (res[1] == 1) // Error...
return 0;
handle = res[0];
opened = true;
return 1;
}
/* Release any resources being used by this BridgeUDP instance */
void BridgeUDP::stop()
{
if (!opened)
return;
uint8_t cmd[] = {'q', handle};
bridge.transfer(cmd, 2);
opened = false;
}
int BridgeUDP::beginPacket(const char *host, uint16_t port)
{
if (!opened)
return 0;
uint8_t cmd[] = {'E', handle, (uint8_t)((port >> 8) & 0xFF), (uint8_t)(port & 0xFF)};
uint8_t res[1];
bridge.transfer(cmd, 4, (const uint8_t *)host, strlen(host), res, 1);
return res[0]; // 1=Success, 0=Error
}
int BridgeUDP::beginBroadcastPacket(uint16_t port)
{
if (!opened)
return 0;
uint8_t cmd[] = {'v', handle, (uint8_t)((port >> 8) & 0xFF), (uint8_t)(port & 0xFF)};
uint8_t res[1];
bridge.transfer(cmd, 4, res, 1);
return res[0]; // 1=Success, 0=Error
}
int BridgeUDP::beginPacket(IPAddress ip, uint16_t port)
{
if (!opened)
return 0;
String address;
address.reserve(18);
address += ip[0];
address += '.';
address += ip[1];
address += '.';
address += ip[2];
address += '.';
address += ip[3];
return beginPacket(address.c_str(), port);
}
int BridgeUDP::endPacket()
{
if (!opened)
return 0;
uint8_t cmd[] = {'H', handle};
uint8_t res[1];
bridge.transfer(cmd, 2, res, 1);
return res[0]; // 1=Success, 0=Error
}
size_t BridgeUDP::write(const uint8_t *buffer, size_t size)
{
if (!opened)
return 0;
uint8_t cmd[] = {'h', handle};
uint8_t res[1];
bridge.transfer(cmd, 2, buffer, size, res, 1);
return res[0]; // 1=Success, 0=Error
}
int BridgeUDP::parsePacket()
{
if (!opened)
return 0;
buffered = 0;
readPos = 0;
uint8_t cmd[] = {'Q', handle};
uint8_t res[3];
bridge.transfer(cmd, 2, res, 3);
if (res[0] == 0) {
// There aren't any packets available
return 0;
}
avail = (res[1] << 8) + res[2];
return 1;
}
void BridgeUDP::doBuffer() {
// If there are already char in buffer exit
if (buffered > 0)
return;
if (avail == 0)
return;
// Try to buffer up to 32 characters
readPos = 0;
uint8_t cmd[] = {'u', handle, sizeof(buffer)};
buffered = bridge.transfer(cmd, 3, buffer, sizeof(buffer));
}
int BridgeUDP::read()
{
if (!opened)
return -1;
doBuffer();
if (buffered == 0) {
return -1; // no chars available
}
buffered--;
avail--;
return buffer[readPos++];
}
int BridgeUDP::read(unsigned char* buff, size_t size)
{
if (!opened)
return -1;
size_t readed = 0;
do {
if (buffered == 0) {
doBuffer();
if (buffered == 0)
return readed;
}
buff[readed++] = buffer[readPos++];
buffered--;
avail--;
} while (readed < size);
return readed;
}
int BridgeUDP::peek()
{
if (!opened)
return -1;
doBuffer();
if (buffered == 0)
return -1; // no chars available
return buffer[readPos];
}
IPAddress BridgeUDP::remoteIP()
{
if (!opened)
return -1;
uint8_t cmd[] = {'T', handle};
uint8_t res[7];
bridge.transfer(cmd, 2, res, 7);
if (res[0] == 0)
return IPAddress(0,0,0,0);
return IPAddress(res[1], res[2], res[3], res[4]);
}
uint16_t BridgeUDP::remotePort()
{
if (!opened)
return -1;
uint8_t cmd[] = {'T', handle};
uint8_t res[7];
bridge.transfer(cmd, 2, res, 7);
if (res[0] == 0)
return 0;
return (res[5] << 8) + res[6];
}

View File

@@ -0,0 +1,65 @@
/*
Copyright (c) 2015 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
*/
#pragma once
#include <Udp.h>
#include "Bridge.h"
class BridgeUDP : public UDP {
public:
BridgeUDP(BridgeClass &_b = Bridge);
virtual uint8_t begin(uint16_t);
virtual void stop();
virtual int beginPacket(IPAddress ip, uint16_t port);
virtual int beginPacket(const char *host, uint16_t port);
virtual int beginBroadcastPacket(uint16_t port);
virtual int endPacket();
virtual size_t write(uint8_t d) { return write(&d, 1); }
virtual size_t write(const uint8_t *buffer, size_t size);
using Print::write;
virtual int parsePacket();
/* return number of bytes available in the current packet,
will return zero if parsePacket hasn't been called yet */
virtual int available() { return avail; }
virtual int read();
virtual int read(unsigned char* buffer, size_t len);
virtual int read(char* buffer, size_t len) { return read((unsigned char*)buffer, len); };
virtual int peek();
virtual void flush() { avail = 0; }
virtual IPAddress remoteIP();
virtual uint16_t remotePort();
private:
BridgeClass &bridge;
uint8_t handle;
boolean opened;
private:
void doBuffer();
uint16_t avail;
uint8_t buffered;
uint8_t readPos;
static const int BUFFER_SIZE = 64;
uint8_t buffer[BUFFER_SIZE];
};

View File

@@ -0,0 +1,150 @@
/*
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);
} else {
outBuffer[outBuffered++] = c;
if (outBuffered == outBufferSize)
flush();
}
return 1;
}
size_t ConsoleClass::write(const uint8_t *buff, size_t size) {
if (autoFlush) {
uint8_t tmp[] = { 'P' };
bridge.transfer(tmp, 1, buff, size, NULL, 0);
} else {
size_t sent = size;
while (sent > 0) {
outBuffer[outBuffered++] = *buff++;
sent--;
if (outBuffered == outBufferSize)
flush();
}
}
return size;
}
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;

View File

@@ -0,0 +1,71 @@
/*
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

View File

@@ -0,0 +1,283 @@
/*
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>
namespace BridgeLib {
File::File(BridgeClass &b) : bridge(b), mode(255) {
// Empty
}
File::File(const char *_filename, uint8_t _mode, BridgeClass &b) : bridge(b), mode(_mode) {
filename = _filename;
uint8_t 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,
static_cast<uint8_t>(position >> 24),
static_cast<uint8_t>(position >> 16),
static_cast<uint8_t>(position >> 8),
static_cast<uint8_t>(position)
};
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;
pos = static_cast<uint32_t>(res[1]) << 24;
pos += static_cast<uint32_t>(res[2]) << 16;
pos += static_cast<uint32_t>(res[3]) << 8;
pos += static_cast<uint32_t>(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 BUFFER_SIZE characters
readPos = 0;
uint8_t cmd[] = {'G', handle, BUFFER_SIZE - 1};
uint16_t readed = bridge.transfer(cmd, 3, buffer, BUFFER_SIZE);
//err = buff[0]; // First byte is error code
if (readed == BridgeClass::TRANSFER_TIMEOUT || readed == 0) {
// transfer failed to retrieve any data
buffered = 0;
} else {
// transfer retrieved at least one byte of data so skip the error code character
readPos++;
buffered = readed - 1;
}
}
int File::available() {
// Look if there is new data available
doBuffer();
return buffered;
}
void File::flush() {
}
int File::read(void *buff, uint16_t nbyte) {
uint16_t n = 0;
uint8_t *p = reinterpret_cast<uint8_t *>(buff);
while (n < nbyte) {
if (buffered == 0) {
doBuffer();
if (buffered == 0)
break;
}
*p++ = buffer[readPos++];
buffered--;
n++;
}
return n;
}
uint32_t File::size() {
if (bridge.getBridgeVersion() < 101)
return 0;
uint8_t cmd[] = {'t', handle};
uint8_t buff[5];
bridge.transfer(cmd, 2, buff, 5);
//err = res[0]; // First byte is error code
uint32_t res;
res = ((uint32_t)buff[1]) << 24;
res |= ((uint32_t)buff[2]) << 16;
res |= ((uint32_t)buff[3]) << 8;
res |= ((uint32_t)buff[4]);
return res;
}
void File::close() {
if (mode == 255)
return;
uint8_t cmd[] = {'f', handle};
uint8_t ret[1];
bridge.transfer(cmd, 2, ret, 1);
mode = 255;
}
const char *File::name() {
return filename.c_str();
}
boolean File::isDirectory() {
uint8_t res[1];
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;
}

View File

@@ -0,0 +1,120 @@
/*
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
namespace BridgeLib {
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 hierarchy--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;
};
// We enclose File and FileSystem classes in namespace BridgeLib to avoid
// conflicts with legacy SD library.
// This ensure compatibility with older sketches that uses only Bridge lib
// (the user can still use File instead of BridgeFile)
using namespace BridgeLib;
// This allows sketches to use BridgeLib::File together with SD library
// (you must use BridgeFile instead of File when needed to disambiguate)
typedef BridgeLib::File BridgeFile;
typedef BridgeLib::FileSystemClass BridgeFileSystemClass;
#define BridgeFileSystem BridgeLib::FileSystem
#endif

View File

@@ -0,0 +1,204 @@
/*
Copyright (c) 2013-2014 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"
HttpClient::HttpClient() :
insecure(false) {
// Empty
}
unsigned int HttpClient::get(String &url) {
begin("curl");
if (insecure) {
addParameter("-k");
}
addHeader();
addParameter(url);
return run();
}
unsigned int HttpClient::get(const char *url) {
begin("curl");
if (insecure) {
addParameter("-k");
}
addHeader();
addParameter(url);
return run();
}
void HttpClient::getAsynchronously(String &url) {
begin("curl");
if (insecure) {
addParameter("-k");
}
addHeader();
addParameter(url);
runAsynchronously();
}
void HttpClient::getAsynchronously(const char *url) {
begin("curl");
if (insecure) {
addParameter("-k");
}
addHeader();
addParameter(url);
runAsynchronously();
}
unsigned int HttpClient::post(String &url, String &data) {
return post(url.c_str(), data.c_str());
}
unsigned int HttpClient::post(const char *url, const char *data) {
begin("curl");
if (insecure) {
addParameter("-k");
}
addParameter("--request");
addParameter("POST");
addParameter("--data");
addParameter(data);
addHeader();
addParameter(url);
return run();
}
void HttpClient::postAsynchronously(String &url, String &data) {
postAsynchronously(url.c_str(), data.c_str());
}
void HttpClient::postAsynchronously(const char *url, const char *data) {
begin("curl");
if (insecure) {
addParameter("-k");
}
addParameter("--request");
addParameter("POST");
addParameter("--data");
addParameter(data);
addHeader();
addParameter(url);
runAsynchronously();
}
unsigned int HttpClient::patch(String &url, String &data) {
return patch(url.c_str(), data.c_str());
}
unsigned int HttpClient::patch(const char *url, const char *data) {
begin("curl");
if (insecure) {
addParameter("-k");
}
addParameter("--request");
addParameter("PATCH");
addParameter("--data");
addParameter(data);
addHeader();
addParameter(url);
return run();
}
void HttpClient::patchAsynchronously(String &url, String &data) {
patchAsynchronously(url.c_str(), data.c_str());
}
void HttpClient::patchAsynchronously(const char *url, const char *data) {
begin("curl");
if (insecure) {
addParameter("-k");
}
addParameter("--request");
addParameter("PATCH");
addParameter("--data");
addParameter(data);
addHeader();
addParameter(url);
runAsynchronously();
}
unsigned int HttpClient::put(String &url, String &data) {
return put(url.c_str(), data.c_str());
}
unsigned int HttpClient::put(const char *url, const char *data) {
begin("curl");
if (insecure) {
addParameter("-k");
}
addParameter("--request");
addParameter("PUT");
addParameter("--data");
addParameter(data);
addHeader();
addParameter(url);
return run();
}
void HttpClient::putAsynchronously(String &url, String &data) {
putAsynchronously(url.c_str(), data.c_str());
}
void HttpClient::putAsynchronously(const char *url, const char *data) {
begin("curl");
if (insecure) {
addParameter("-k");
}
addParameter("--request");
addParameter("PUT");
addParameter("--data");
addParameter(data);
addHeader();
addParameter(url);
runAsynchronously();
}
boolean HttpClient::ready() {
return !running();
}
unsigned int HttpClient::getResult() {
return exitValue();
}
void HttpClient::noCheckSSL() {
insecure = true;
}
void HttpClient::checkSSL() {
insecure = false;
}
void HttpClient::setHeader(String &header) {
this->header = header;
}
void HttpClient::setHeader(const char * header) {
this->header = String(header);
}
void HttpClient::addHeader() {
if (header.length() > 0) {
addParameter("--header");
addParameter(header);
}
}

View File

@@ -0,0 +1,59 @@
/*
Copyright (c) 2013-2014 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:
HttpClient();
unsigned int get(String &url);
unsigned int get(const char * url);
void getAsynchronously(String &url);
void getAsynchronously(const char * url);
unsigned int post(String &url, String &data);
unsigned int post(const char * url, const char * data);
void postAsynchronously(String &url, String &data);
void postAsynchronously(const char * url, const char * data);
unsigned int patch(String &url, String &data);
unsigned int patch(const char * url, const char * data);
void patchAsynchronously(String &url, String &data);
void patchAsynchronously(const char * url, const char * data);
unsigned int put(String &url, String &data);
unsigned int put(const char * url, const char * data);
void putAsynchronously(String &url, String &data);
void putAsynchronously(const char * url, const char * data);
void setHeader(String &header);
void setHeader(const char * header);
boolean ready();
unsigned int getResult();
void noCheckSSL();
void checkSSL();
private:
boolean insecure;
private:
void addHeader();
String header;
};
#endif /* HTTPCLIENT_H_ */

View 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);

View 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_

View 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 &param) {
*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];
//}

View File

@@ -0,0 +1,71 @@
/*
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 &param);
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;
uint8_t 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

View File

@@ -0,0 +1,27 @@
/*
Copyright (c) 2014 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 <BridgeClient.h>
#warning "The use of YunClient is deprecated. Use BridgeClient instead!"
typedef BridgeClient YunClient;
#endif // _YUN_CLIENT_H_

View File

@@ -0,0 +1,27 @@
/*
Copyright (c) 2014 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 <BridgeServer.h>
#warning "The use of YunServer is deprecated. Use BridgeServer instead!"
typedef BridgeServer YunServer;
#endif // _YUN_SERVER_H_