1
0
mirror of https://github.com/esp8266/Arduino.git synced 2025-06-20 21:01:25 +03:00

Run new astyle formatter against libs Bridge, SpacebrewYun and Temboo

This commit is contained in:
Federico Fissore
2013-10-21 10:13:31 +02:00
parent b4c68b3dff
commit 00bdd3df03
19 changed files with 841 additions and 805 deletions

View File

@ -28,17 +28,17 @@ void BridgeClass::begin() {
if (started)
return;
started = true;
// Wait for U-boot to finish startup
do {
dropAll();
delay(1000);
} while (stream.available()>0);
} while (stream.available() > 0);
while (true) {
// Bridge interrupt:
// - Ask the bridge to close itself
uint8_t quit_cmd[] = {'X','X','X','X','X'};
uint8_t quit_cmd[] = {'X', 'X', 'X', 'X', 'X'};
max_retries = 1;
transfer(quit_cmd, 5);
@ -55,9 +55,9 @@ void BridgeClass::begin() {
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 cmd[] = {'X', 'X', '1', '0', '0'};
uint8_t res[1];
max_retries = 50;
uint16_t l = transfer(cmd, 5, res, 1);
@ -70,7 +70,7 @@ void BridgeClass::begin() {
continue;
}
if (res[0] != 0)
while (true);
while (true);
max_retries = 50;
return;
@ -95,7 +95,7 @@ unsigned int BridgeClass::get(const char *key, uint8_t *value, unsigned int maxl
}
void BridgeClass::crcUpdate(uint8_t c) {
CRC = _crc_ccitt_update(CRC, c);
//CRC = CRC ^ c;
//CRC = (CRC >> 8) + (CRC << 8);
@ -115,13 +115,13 @@ bool BridgeClass::crcCheck(uint16_t _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)
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 */) {
for ( ; retries < max_retries; retries++, delay(100), dropAll() /* Delay for retransmission */) {
// Send packet
crcReset();
stream.write((char)0xFF); // Start of packet (0xFF)
@ -132,31 +132,31 @@ uint16_t BridgeClass::transfer(const uint8_t *buff1, uint16_t len1,
crcUpdate((len >> 8) & 0xFF);
stream.write((char)(len & 0xFF)); // Message length (lo)
crcUpdate(len & 0xFF);
for (uint16_t i=0; i<len1; i++) { // Payload
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
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
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(10);
if (lh < 0)
@ -171,7 +171,7 @@ uint16_t BridgeClass::transfer(const uint8_t *buff1, uint16_t len1,
l += ll;
// Recv data
for (uint16_t i=0; i<l; i++) {
for (uint16_t i = 0; i < l; i++) {
int c = timedRead(5);
if (c < 0)
continue;
@ -180,7 +180,7 @@ uint16_t BridgeClass::transfer(const uint8_t *buff1, uint16_t len1,
rxbuff[i] = c;
crcUpdate(c);
}
// Check CRC
int crc_hi = timedRead(5);
if (crc_hi < 0)
@ -188,12 +188,12 @@ uint16_t BridgeClass::transfer(const uint8_t *buff1, uint16_t len1,
int crc_lo = timedRead(5);
if (crc_lo < 0)
continue;
if (!crcCheck((crc_hi<<8)+crc_lo))
if (!crcCheck((crc_hi << 8) + crc_lo))
continue;
// Increase index
index++;
// Return bytes received
if (l > rxlen)
return rxlen;
@ -210,7 +210,7 @@ int BridgeClass::timedRead(unsigned int timeout) {
do {
c = stream.read();
if (c >= 0) return c;
} while(millis() - _startMillis < timeout);
} while (millis() - _startMillis < timeout);
return -1; // -1 indicates timeout
}
@ -222,8 +222,8 @@ void BridgeClass::dropAll() {
// Bridge instance
#ifdef __AVR_ATmega32U4__
// Leonardo variants (where HardwareSerial is Serial1)
SerialBridgeClass Bridge(Serial1);
// Leonardo variants (where HardwareSerial is Serial1)
SerialBridgeClass Bridge(Serial1);
#else
SerialBridgeClass Bridge(Serial);
SerialBridgeClass Bridge(Serial);
#endif

View File

@ -23,70 +23,80 @@
#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); }
public:
BridgeClass(Stream &_stream);
void begin();
static const int TRANSFER_TIMEOUT = 0xFFFF;
// 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);
}
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;
// 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(unsigned long baudrate = 250000) {
serial.begin(baudrate);
BridgeClass::begin();
}
private:
HardwareSerial &serial;
public:
SerialBridgeClass(HardwareSerial &_serial)
: BridgeClass(_serial), serial(_serial) {
// Empty
}
void begin(unsigned long baudrate = 250000) {
serial.begin(baudrate);
BridgeClass::begin();
}
private:
HardwareSerial &serial;
};
extern SerialBridgeClass Bridge;

View File

@ -19,7 +19,7 @@
#include <Console.h>
// Default constructor uses global Bridge instance
ConsoleClass::ConsoleClass() :
ConsoleClass::ConsoleClass() :
bridge(Bridge), inBuffered(0), inReadPos(0), inBuffer(NULL),
autoFlush(true)
{
@ -27,7 +27,7 @@ ConsoleClass::ConsoleClass() :
}
// Constructor with a user provided BridgeClass instance
ConsoleClass::ConsoleClass(BridgeClass &_b) :
ConsoleClass::ConsoleClass(BridgeClass &_b) :
bridge(_b), inBuffered(0), inReadPos(0), inBuffer(NULL),
autoFlush(true)
{
@ -53,10 +53,10 @@ size_t ConsoleClass::write(uint8_t c) {
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];
uint8_t *tmp = new uint8_t[size + 1];
tmp[0] = 'P';
memcpy(tmp+1, buff, size);
bridge.transfer(tmp, size+1);
memcpy(tmp + 1, buff, size);
bridge.transfer(tmp, size + 1);
delete[] tmp;
return size;
} else {
@ -72,25 +72,25 @@ size_t ConsoleClass::write(const uint8_t *buff, size_t size) {
void ConsoleClass::flush() {
if (autoFlush)
return;
bridge.transfer(outBuffer, outBuffered);
outBuffered = 1;
}
void ConsoleClass::noBuffer() {
if (autoFlush)
return;
return;
delete[] outBuffer;
autoFlush = true;
}
void ConsoleClass::buffer(uint8_t size) {
noBuffer();
if (size==0)
return;
outBuffer = new uint8_t[size+1];
if (size == 0)
return;
outBuffer = new uint8_t[size + 1];
outBuffer[0] = 'P'; // WRITE tag
outBufferSize = size+1;
outBufferSize = size + 1;
outBuffered = 1;
autoFlush = false;
}
@ -98,7 +98,7 @@ void ConsoleClass::buffer(uint8_t size) {
bool ConsoleClass::connected() {
uint8_t tmp = 'a';
bridge.transfer(&tmp, 1, &tmp, 1);
return tmp==1;
return tmp == 1;
}
int ConsoleClass::available() {

View File

@ -22,46 +22,48 @@
#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();
public:
// Default constructor uses global Bridge instance
ConsoleClass();
// Constructor with a user provided BridgeClass instance
ConsoleClass(BridgeClass &_b);
~ConsoleClass();
void buffer(uint8_t size);
void noBuffer();
void begin();
void end();
bool connected();
void buffer(uint8_t size);
void noBuffer();
// 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;
bool connected();
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;
// 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;

View File

@ -26,7 +26,7 @@ File::File(BridgeClass &b) : mode(255), bridge(b) {
File::File(const char *_filename, uint8_t _mode, BridgeClass &b) : mode(_mode), bridge(b) {
filename = _filename;
char modes[] = {'r','w','a'};
char modes[] = {'r', 'w', 'a'};
uint8_t cmd[] = {'F', modes[mode]};
uint8_t res[2];
dirPosition = 1;
@ -53,12 +53,12 @@ size_t File::write(uint8_t c) {
size_t File::write(const uint8_t *buf, size_t size) {
if (mode == 255)
return -1;
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 -res[0];
return size;
}
@ -91,7 +91,7 @@ boolean File::seek(uint32_t position) {
};
uint8_t res[1];
bridge.transfer(cmd, 6, res, 1);
if (res[0]==0) {
if (res[0] == 0) {
// If seek succeed then flush buffers
buffered = 0;
return true;
@ -121,10 +121,10 @@ void File::doBuffer() {
uint8_t cmd[] = {'G', handle, BUFFER_SIZE - 1};
buffered = bridge.transfer(cmd, 3, buffer, BUFFER_SIZE) - 1;
//err = buff[0]; // First byte is error code
if (buffered>0) {
if (buffered > 0) {
// Shift the reminder of buffer
for (uint8_t i=0; i<buffered; i++)
buffer[i] = buffer[i+1];
for (uint8_t i = 0; i < buffered; i++)
buffer[i] = buffer[i + 1];
}
}
@ -166,7 +166,7 @@ boolean File::isDirectory() {
}
File File::openNextFile(uint8_t mode){
File File::openNextFile(uint8_t mode) {
Process awk;
char tmp;
String command;
@ -178,26 +178,26 @@ File File::openNextFile(uint8_t mode){
command += " | awk 'NR==";
command += dirPosition;
command += "'";
awk.runShellCommand(command);
while(awk.running());
while (awk.running());
command = "";
while (awk.available()){
while (awk.available()) {
tmp = awk.read();
if (tmp!='\n') command += tmp;
if (tmp != '\n') command += tmp;
}
if (command.length() == 0)
return File();
dirPosition++;
filepath = filename + "/" + command;
return File(filepath.c_str(),mode);
}
return File(filepath.c_str(), mode);
void File::rewindDirectory(void){
}
void File::rewindDirectory(void) {
dirPosition = 1;
}

View File

@ -26,76 +26,76 @@
#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;
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);
public:
FileSystemClass() : bridge(Bridge) { }
FileSystemClass(BridgeClass &_b) : bridge(_b) { }
// Methods to determine if the requested file path exists.
boolean exists(const char *filepath);
boolean begin();
// 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);
// 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);
private:
friend class File;
BridgeClass &bridge;
// 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;

View File

@ -22,14 +22,14 @@
#include <Process.h>
class HttpClient : public Process {
public:
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();
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();
};

View File

@ -26,7 +26,7 @@ unsigned int MailboxClass::readMessage(uint8_t *buff, unsigned int 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];
uint8_t buff[maxLength + 1];
int l = bridge.transfer(tmp, 1, buff, maxLength);
buff[l] = 0;
str = (const char *)buff;

View File

@ -22,30 +22,30 @@
#include <Bridge.h>
class MailboxClass {
public:
MailboxClass(BridgeClass &b = Bridge) : bridge(b) { }
public:
MailboxClass(BridgeClass &b = Bridge) : bridge(b) { }
void begin() { }
void end() { }
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);
// 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);
// 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();
// Return the size of the next available message, 0 if there are
// no messages in queue.
unsigned int messageAvailable();
private:
BridgeClass &bridge;
private:
BridgeClass &bridge;
};
extern MailboxClass Mailbox;

View File

@ -84,8 +84,8 @@ void Process::runAsynchronously() {
delete cmdline;
cmdline = NULL;
if (res[0]==0) // res[0] contains error code
if (res[0] == 0) // res[0] contains error code
started = true;
}

View File

@ -22,48 +22,50 @@
#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();
public:
// Constructor with a user provided BridgeClass instance
Process(BridgeClass &_b = Bridge) :
bridge(_b), started(false), buffered(0), readPos(0) { }
~Process();
unsigned int runShellCommand(const String &command);
void runShellCommandAsynchronously(const String &command);
void begin(const String &command);
void addParameter(const String &param);
unsigned int run();
void runAsynchronously();
boolean running();
unsigned int exitValue();
void close();
operator bool () { return started; }
unsigned int runShellCommand(const String &command);
void runShellCommandAsynchronously(const String &command);
// 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;
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];
private:
void doBuffer();
uint8_t buffered;
uint8_t readPos;
static const int BUFFER_SIZE = 64;
uint8_t buffer[BUFFER_SIZE];
};
#endif

View File

@ -30,9 +30,9 @@ YunClient::~YunClient() {
}
YunClient& YunClient::operator=(const YunClient &_x) {
opened = _x.opened;
handle = _x.handle;
return *this;
opened = _x.opened;
handle = _x.handle;
return *this;
}
void YunClient::stop() {
@ -73,13 +73,13 @@ int YunClient::read() {
int YunClient::read(uint8_t *buff, size_t size) {
int readed = 0;
do {
if (buffered == 0) {
doBuffer();
if (buffered == 0) {
doBuffer();
if (buffered == 0)
return readed;
}
buff[readed++] = buffer[readPos++];
buffered--;
}
buff[readed++] = buffer[readPos++];
buffered--;
} while (readed < size);
return readed;
}
@ -141,7 +141,7 @@ int YunClient::connect(const char *host, uint16_t port) {
};
uint8_t res[1];
int l = bridge.transfer(tmp, 3, (const uint8_t *)host, strlen(host), res, 1);
if (l==0)
if (l == 0)
return 0;
handle = res[0];

View File

@ -23,46 +23,48 @@
#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; }
public:
// Constructor with a user provided BridgeClass instance
YunClient(int _h, BridgeClass &_b = Bridge);
YunClient(BridgeClass &_b = Bridge);
~YunClient();
YunClient& operator=(const YunClient &_x);
// 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 void stop();
virtual uint8_t connected();
virtual operator bool () {
return opened;
}
virtual int connect(IPAddress ip, uint16_t port);
virtual int connect(const char *host, uint16_t port);
YunClient& operator=(const YunClient &_x);
private:
BridgeClass &bridge;
unsigned int handle;
boolean opened;
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];
private:
void doBuffer();
uint8_t buffered;
uint8_t readPos;
static const int BUFFER_SIZE = 64;
uint8_t buffer[BUFFER_SIZE];
};
#endif // _YUN_CLIENT_H_

View File

@ -41,7 +41,7 @@ YunClient YunServer::accept() {
uint8_t cmd[] = {'k'};
uint8_t res[1];
unsigned int l = bridge.transfer(cmd, 1, res, 1);
if (l==0)
if (l == 0)
return YunClient();
return YunClient(res[0]);
}

View File

@ -25,23 +25,27 @@
class YunClient;
class YunServer : public Server {
public:
// Constructor with a user provided BridgeClass instance
YunServer(uint16_t port = 5555, BridgeClass &_b = Bridge);
public:
// Constructor with a user provided BridgeClass instance
YunServer(uint16_t port = 5555, BridgeClass &_b = Bridge);
void begin();
YunClient accept();
void begin();
YunClient accept();
virtual size_t write(uint8_t c);
virtual size_t write(uint8_t c);
void listenOnLocalhost() { useLocalhost = true; }
void noListenOnLocalhost() { useLocalhost = false; }
void listenOnLocalhost() {
useLocalhost = true;
}
void noListenOnLocalhost() {
useLocalhost = false;
}
private:
uint16_t port;
bool listening;
bool useLocalhost;
BridgeClass &bridge;
private:
uint16_t port;
bool listening;
bool useLocalhost;
BridgeClass &bridge;
};
#endif // _YUN_SERVER_H_