Skip to content

Commit 34885b0

Browse files
committed
Refactored YunClient and YunServer classes.
Added YunClient.connect() methods.
1 parent 8c9a060 commit 34885b0

File tree

5 files changed

+240
-148
lines changed

5 files changed

+240
-148
lines changed

Diff for: hardware/arduino/avr/cores/arduino/IPAddress.h

+1
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
#ifndef IPAddress_h
2727
#define IPAddress_h
2828

29+
#include <stdint.h>
2930
#include <Printable.h>
3031

3132
// A class to make it easier to handle and pass around IP addresses

Diff for: hardware/arduino/avr/libraries/Bridge/YunClient.cpp

+167
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,167 @@
1+
/*
2+
Copyright (c) 2013 Arduino LLC. All right reserved.
3+
4+
This library is free software; you can redistribute it and/or
5+
modify it under the terms of the GNU Lesser General Public
6+
License as published by the Free Software Foundation; either
7+
version 2.1 of the License, or (at your option) any later version.
8+
9+
This library is distributed in the hope that it will be useful,
10+
but WITHOUT ANY WARRANTY; without even the implied warranty of
11+
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12+
Lesser General Public License for more details.
13+
14+
You should have received a copy of the GNU Lesser General Public
15+
License along with this library; if not, write to the Free Software
16+
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
17+
*/
18+
19+
#include <YunClient.h>
20+
21+
YunClient::YunClient(int _h, BridgeClass &_b) :
22+
bridge(_b), handle(_h), opened(true), buffered(0) {
23+
}
24+
25+
YunClient::YunClient(BridgeClass &_b) :
26+
bridge(_b), handle(0), opened(false), buffered(0) {
27+
}
28+
29+
YunClient::~YunClient() {
30+
}
31+
32+
YunClient& YunClient::operator=(const YunClient &_x) {
33+
opened = _x.opened;
34+
handle = _x.handle;
35+
return *this;
36+
}
37+
38+
void YunClient::stop() {
39+
if (opened) {
40+
uint8_t cmd[] = {'j', handle};
41+
bridge.transfer(cmd, 2);
42+
}
43+
opened = false;
44+
}
45+
46+
void YunClient::doBuffer() {
47+
// If there are already char in buffer exit
48+
if (buffered > 0)
49+
return;
50+
51+
// Try to buffer up to 32 characters
52+
readPos = 0;
53+
uint8_t cmd[] = {'K', handle, sizeof(buffer)};
54+
buffered = bridge.transfer(cmd, 3, buffer, sizeof(buffer));
55+
}
56+
57+
int YunClient::available() {
58+
// Look if there is new data available
59+
doBuffer();
60+
return buffered;
61+
}
62+
63+
int YunClient::read() {
64+
doBuffer();
65+
if (buffered == 0)
66+
return -1; // no chars available
67+
else {
68+
buffered--;
69+
return buffer[readPos++];
70+
}
71+
}
72+
73+
int YunClient::read(uint8_t *buff, size_t size) {
74+
int readed = 0;
75+
do {
76+
if (buffered == 0) {
77+
doBuffer();
78+
if (buffered == 0)
79+
return readed;
80+
}
81+
buff[readed++] = buffer[readPos++];
82+
buffered--;
83+
} while (readed < size);
84+
return readed;
85+
}
86+
87+
int YunClient::peek() {
88+
doBuffer();
89+
if (buffered == 0)
90+
return -1; // no chars available
91+
else
92+
return buffer[readPos];
93+
}
94+
95+
size_t YunClient::write(uint8_t c) {
96+
if (!opened)
97+
return 0;
98+
uint8_t cmd[] = {'l', handle, c};
99+
bridge.transfer(cmd, 3);
100+
return 1;
101+
}
102+
103+
size_t YunClient::write(const uint8_t *buf, size_t size) {
104+
if (!opened)
105+
return 0;
106+
uint8_t cmd[] = {'l', handle};
107+
bridge.transfer(cmd, 2, buf, size, NULL, 0);
108+
return size;
109+
}
110+
111+
void YunClient::flush() {
112+
}
113+
114+
uint8_t YunClient::connected() {
115+
if (!opened)
116+
return false;
117+
uint8_t cmd[] = {'L', handle};
118+
uint8_t res[1];
119+
bridge.transfer(cmd, 2, res, 1);
120+
return (res[0] == 1);
121+
}
122+
123+
int YunClient::connect(IPAddress ip, uint16_t port) {
124+
String address;
125+
address.reserve(18);
126+
address += ip[0];
127+
address += '.';
128+
address += ip[1];
129+
address += '.';
130+
address += ip[2];
131+
address += '.';
132+
address += ip[3];
133+
return connect(address.c_str(), port);
134+
}
135+
136+
int YunClient::connect(const char *host, uint16_t port) {
137+
uint8_t tmp[] = {
138+
'C',
139+
(port >> 8) & 0xFF,
140+
port & 0xFF
141+
};
142+
uint8_t res[1];
143+
int l = bridge.transfer(tmp, 3, (const uint8_t *)host, strlen(host), res, 1);
144+
if (l==0)
145+
return 0;
146+
handle = res[0];
147+
148+
// wait for connection
149+
uint8_t tmp2[] = { 'c', handle };
150+
uint8_t res2[1];
151+
while (true) {
152+
bridge.transfer(tmp2, 2, res2, 1);
153+
if (res2[0] == 0)
154+
break;
155+
delay(1);
156+
}
157+
opened = true;
158+
159+
// check for successful connection
160+
if (connected())
161+
return 1;
162+
163+
opened = false;
164+
handle = 0;
165+
return 0;
166+
}
167+

Diff for: hardware/arduino/avr/libraries/Bridge/YunClient.h

+68
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
/*
2+
Copyright (c) 2013 Arduino LLC. All right reserved.
3+
4+
This library is free software; you can redistribute it and/or
5+
modify it under the terms of the GNU Lesser General Public
6+
License as published by the Free Software Foundation; either
7+
version 2.1 of the License, or (at your option) any later version.
8+
9+
This library is distributed in the hope that it will be useful,
10+
but WITHOUT ANY WARRANTY; without even the implied warranty of
11+
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12+
Lesser General Public License for more details.
13+
14+
You should have received a copy of the GNU Lesser General Public
15+
License along with this library; if not, write to the Free Software
16+
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
17+
*/
18+
19+
#ifndef _YUN_CLIENT_H_
20+
#define _YUN_CLIENT_H_
21+
22+
#include <Bridge.h>
23+
#include <Client.h>
24+
25+
class YunClient : public Client {
26+
public:
27+
// Constructor with a user provided BridgeClass instance
28+
YunClient(int _h, BridgeClass &_b = Bridge);
29+
YunClient(BridgeClass &_b = Bridge);
30+
~YunClient();
31+
32+
// Stream methods
33+
// (read message)
34+
virtual int available();
35+
virtual int read();
36+
virtual int read(uint8_t *buf, size_t size);
37+
virtual int peek();
38+
// (write response)
39+
virtual size_t write(uint8_t);
40+
virtual size_t write(const uint8_t *buf, size_t size);
41+
virtual void flush();
42+
// TODO: add optimized function for block write
43+
44+
virtual operator bool () { return opened; }
45+
46+
YunClient& operator=(const YunClient &_x);
47+
48+
virtual void stop();
49+
virtual uint8_t connected();
50+
51+
virtual int connect(IPAddress ip, uint16_t port);
52+
virtual int connect(const char *host, uint16_t port);
53+
54+
private:
55+
BridgeClass &bridge;
56+
unsigned int handle;
57+
boolean opened;
58+
59+
private:
60+
void doBuffer();
61+
uint8_t buffered;
62+
uint8_t readPos;
63+
static const int BUFFER_SIZE = 64;
64+
uint8_t buffer[BUFFER_SIZE];
65+
66+
};
67+
68+
#endif // _YUN_CLIENT_H_

Diff for: hardware/arduino/avr/libraries/Bridge/YunServer.cpp

+1-101
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
*/
1818

1919
#include <YunServer.h>
20+
#include <YunClient.h>
2021

2122
YunServer::YunServer(uint16_t _p, BridgeClass &_b) :
2223
bridge(_b), port(_p), listening(false), useLocalhost(false) {
@@ -45,104 +46,3 @@ YunClient YunServer::accept() {
4546
return YunClient(res[0]);
4647
}
4748

48-
YunClient::YunClient(int _h, BridgeClass &_b) :
49-
bridge(_b), handle(_h), opened(true), buffered(0) {
50-
}
51-
52-
YunClient::YunClient(BridgeClass &_b) :
53-
bridge(_b), handle(0), opened(false), buffered(0) {
54-
}
55-
56-
YunClient::~YunClient() {
57-
}
58-
59-
YunClient& YunClient::operator=(const YunClient &_x) {
60-
opened = _x.opened;
61-
handle = _x.handle;
62-
return *this;
63-
}
64-
65-
void YunClient::stop() {
66-
if (opened) {
67-
uint8_t cmd[] = {'j', handle};
68-
bridge.transfer(cmd, 2);
69-
}
70-
opened = false;
71-
}
72-
73-
void YunClient::doBuffer() {
74-
// If there are already char in buffer exit
75-
if (buffered > 0)
76-
return;
77-
78-
// Try to buffer up to 32 characters
79-
readPos = 0;
80-
uint8_t cmd[] = {'K', handle, sizeof(buffer)};
81-
buffered = bridge.transfer(cmd, 3, buffer, sizeof(buffer));
82-
}
83-
84-
int YunClient::available() {
85-
// Look if there is new data available
86-
doBuffer();
87-
return buffered;
88-
}
89-
90-
int YunClient::read() {
91-
doBuffer();
92-
if (buffered == 0)
93-
return -1; // no chars available
94-
else {
95-
buffered--;
96-
return buffer[readPos++];
97-
}
98-
}
99-
100-
int YunClient::read(uint8_t *buff, size_t size) {
101-
int readed = 0;
102-
do {
103-
if (buffered == 0) {
104-
doBuffer();
105-
if (buffered == 0)
106-
return readed;
107-
}
108-
buff[readed++] = buffer[readPos++];
109-
buffered--;
110-
} while (readed < size);
111-
return readed;
112-
}
113-
114-
int YunClient::peek() {
115-
doBuffer();
116-
if (buffered == 0)
117-
return -1; // no chars available
118-
else
119-
return buffer[readPos];
120-
}
121-
122-
size_t YunClient::write(uint8_t c) {
123-
if (!opened)
124-
return 0;
125-
uint8_t cmd[] = {'l', handle, c};
126-
bridge.transfer(cmd, 3);
127-
return 1;
128-
}
129-
130-
size_t YunClient::write(const uint8_t *buf, size_t size) {
131-
if (!opened)
132-
return 0;
133-
uint8_t cmd[] = {'l', handle};
134-
bridge.transfer(cmd, 2, buf, size, NULL, 0);
135-
return size;
136-
}
137-
138-
void YunClient::flush() {
139-
}
140-
141-
uint8_t YunClient::connected() {
142-
if (!opened)
143-
return false;
144-
uint8_t cmd[] = {'L', handle};
145-
uint8_t res[1];
146-
bridge.transfer(cmd, 2, res, 1);
147-
return (res[0] == 1);
148-
}

0 commit comments

Comments
 (0)