Skip to content

Adding HttpClient #69

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 8 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 32 additions & 1 deletion hardware/arduino/cores/arduino/Print.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,10 @@ size_t Print::print(const __FlashStringHelper *ifsh)
while (1) {
unsigned char c = pgm_read_byte(p++);
if (c == 0) break;
if (attachedPrint)
{
attachedPrint->print(c);
}
n += write(c);
}
return n;
Expand All @@ -55,18 +59,30 @@ size_t Print::print(const String &s)
{
size_t n = 0;
for (uint16_t i = 0; i < s.length(); i++) {
if (attachedPrint)
{
attachedPrint->print(s[i]);
}
n += write(s[i]);
}
return n;
}

size_t Print::print(const char str[])
{
if (attachedPrint)
{
attachedPrint->print(str);
}
return write(str);
}

size_t Print::print(char c)
{
if (attachedPrint)
{
attachedPrint->print(c);
}
return write(c);
}

Expand All @@ -88,6 +104,10 @@ size_t Print::print(unsigned int n, int base)
size_t Print::print(long n, int base)
{
if (base == 0) {
if (attachedPrint)
{
attachedPrint->print(n);
}
return write(n);
} else if (base == 10) {
if (n < 0) {
Expand All @@ -103,7 +123,14 @@ size_t Print::print(long n, int base)

size_t Print::print(unsigned long n, int base)
{
if (base == 0) return write(n);
if (base == 0)
{
if (attachedPrint)
{
attachedPrint->print(n);
}
return write(n);
}
else return printNumber(n, base);
}

Expand Down Expand Up @@ -219,6 +246,10 @@ size_t Print::printNumber(unsigned long n, uint8_t base) {
*--str = c < 10 ? c + '0' : c + 'A' - 10;
} while(n);

if (attachedPrint)
{
attachedPrint->print(str);
}
return write(str);
}

Expand Down
6 changes: 5 additions & 1 deletion hardware/arduino/cores/arduino/Print.h
Original file line number Diff line number Diff line change
Expand Up @@ -38,9 +38,11 @@ class Print
size_t printNumber(unsigned long, uint8_t);
size_t printFloat(double, uint8_t);
protected:
Print* attachedPrint; // if non-NULL, points to a Print instance where we should output
// any sent/received data
void setWriteError(int err = 1) { write_error = err; }
public:
Print() : write_error(0) {}
Print() : write_error(0), attachedPrint(NULL) {}

int getWriteError() { return write_error; }
void clearWriteError() { setWriteError(0); }
Expand All @@ -49,6 +51,8 @@ class Print
size_t write(const char *str) { return write((const uint8_t *)str, strlen(str)); }
virtual size_t write(const uint8_t *buffer, size_t size);

void attach(Print& aPrint) { attachedPrint = &aPrint; };

size_t print(const __FlashStringHelper *);
size_t print(const String &);
size_t print(const char[]);
Expand Down
24 changes: 24 additions & 0 deletions hardware/arduino/cores/arduino/Stream.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -244,3 +244,27 @@ size_t Stream::readBytesUntil(char terminator, char *buffer, size_t length)
return index; // return number of characters, not including null terminator
}

String Stream::readString()
{
String ret;
int c = timedRead();
while (c >= 0)
{
ret += (char)c;
c = timedRead();
}
return ret;
}

String Stream::readStringUntil(char terminator)
{
String ret;
int c = timedRead();
while (c >= 0 && c != terminator)
{
ret += (char)c;
c = timedRead();
}
return ret;
}

2 changes: 2 additions & 0 deletions hardware/arduino/cores/arduino/Stream.h
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,8 @@ class Stream : public Print
// returns the number of characters placed in the buffer (0 means no valid data found)

// Arduino String functions to be added here
String readString();
String readStringUntil(char terminator);

protected:
long parseInt(char skipChar); // as above but the given skipChar is ignored
Expand Down
17 changes: 16 additions & 1 deletion libraries/Ethernet/EthernetClient.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,10 @@ int EthernetClient::read() {
if ( recv(_sock, &b, 1) > 0 )
{
// recv worked
if (attachedPrint)
{
attachedPrint->print((char)b);
}
return b;
}
else
Expand All @@ -107,7 +111,18 @@ int EthernetClient::read() {
}

int EthernetClient::read(uint8_t *buf, size_t size) {
return recv(_sock, buf, size);
int ret = recv(_sock, buf, size);
if (ret > 0)
{
if (attachedPrint)
{
for (int i=0; i < ret; i++)
{
attachedPrint->print((char)buf[i]);
}
}
}
return ret;
}

int EthernetClient::peek() {
Expand Down
Loading