-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathClient_ESP8266.h
115 lines (107 loc) · 3.29 KB
/
Client_ESP8266.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
/*
Copyright (C) 2015 Embedded and Real-Time Systems Laboratory
Graduate School of Information Science, Nagoya Univ., JAPAN
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 _CLIENT_ESP8366_H_
#define _CLIENT_ESP8366_H_
#include "Print.h"
#include "Stream.h"
#include "IPAddress.h"
#include "ESP8266.h"
#define ESP8266CLIENT_BUFFER_LEN 512
class ESP8266Client: public Client, public ESP8266 {
public:
/*
* @retval 0 : faild
* @retval 1 : success
*/
int connect(IPAddress ip, uint16_t port) {
String ip_str;
bufferlen= 0;
ip_str.concat(ip[0]);
ip_str.concat(".");
ip_str.concat(ip[1]);
ip_str.concat(".");
ip_str.concat(ip[2]);
ip_str.concat(".");
ip_str.concat(ip[3]);
_connected = (createTCP(ip_str, port))? 1 : 0;
return (int)_connected;
};
int connect(const char *host, uint16_t port) {
String ip_str(host);
bufferlen= 0;
_connected = (createTCP(ip_str, port))? 1 : 0;
return (int)_connected;
};
size_t write(uint8_t data) {
if(send(&data, 1)){
return 1;
}
else {
return 0;
}
};
size_t write(const uint8_t *buf, size_t size){
if(send(buf, size)){
return size;
}
else {
return 0;
}
};
int available(){
if (bufferlen > 0) {
return 1;
}
if (dataAvailable()) {
bufferlen = recv(buffer, ESP8266CLIENT_BUFFER_LEN);
pbuffer = buffer;
//In case of that data is available but it is not valid data(not +IPD,...),
//connection is closed by server.(send "CLOSED")
if(bufferlen == 0){
_connected = 0;
releaseTCP();
return 0;
}
return 1;
}
return 0;
};
int read(){
if (bufferlen == 0) {
bufferlen = recv(buffer, ESP8266CLIENT_BUFFER_LEN);
pbuffer = buffer;
}
if (bufferlen > 0){
bufferlen--;
return *pbuffer++;
}
else {
return (int)-1;
}
};
int read(uint8_t *buf, size_t size){return recv(buf, size);};
int peek(){return 1;};
void flush(){};
void stop(){releaseTCP();};
uint8_t connected(){return _connected;};
operator bool(){return (bool)(_connected == 1);};
private:
uint8_t _connected;
uint8_t buffer[ESP8266CLIENT_BUFFER_LEN];
uint8_t bufferlen;
uint8_t *pbuffer;
};
#endif /* _CLIENT_ESP8366_H_ */