-
-
Notifications
You must be signed in to change notification settings - Fork 46
/
ESP8266Client.cpp
139 lines (105 loc) · 2.7 KB
/
ESP8266Client.cpp
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
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
#include "ESP8266Client.h"
bool ESP8266Client::begin()
{
if (_esp8266->setMultipleConnections(true) != ESP8266_COMMAND_OK)
return false;
return true;
}
int ESP8266Client::connect(const char* host, uint16_t port)
{
ESP8266CommandStatus status = _esp8266->connect(_id, ESP8266_PROTOCOL_TCP, host, port);
if (status == ESP8266_COMMAND_OK || status == ESP8266_COMMAND_ALREADY_CONNECTED) {
_connected = true;
return 1;
}
_connected = false;
return 0;
}
int ESP8266Client::connect(IPAddress ip, uint16_t port)
{
ESP8266CommandStatus status = _esp8266->connect(_id, ESP8266_PROTOCOL_TCP, ip, port);
if (status == ESP8266_COMMAND_OK || status == ESP8266_COMMAND_ALREADY_CONNECTED) {
_connected = true;
return 1;
}
_connected = false;
return 0;
}
int ESP8266Client::available()
{
return _esp8266->available();
}
int ESP8266Client::read()
{
return _esp8266->read();
}
int ESP8266Client::read(uint8_t* buffer, size_t size)
{
return _esp8266->read(buffer, size);
}
size_t ESP8266Client::write(uint8_t b)
{
ESP8266CommandStatus status = _esp8266->send(_id, &b, 1);
if (status == ESP8266_COMMAND_NO_LINK)
_connected = false;
if (status != ESP8266_COMMAND_OK)
return 0;
return 1;
}
size_t ESP8266Client::write(const char* data)
{
ESP8266CommandStatus status = _esp8266->send(_id, data);
if (status == ESP8266_COMMAND_NO_LINK)
_connected = false;
if (status != ESP8266_COMMAND_OK)
return 0;
return strlen(data);
}
size_t ESP8266Client::write(const uint8_t* buffer, size_t size)
{
ESP8266CommandStatus status = _esp8266->send(_id, buffer, size);
if (status == ESP8266_COMMAND_NO_LINK)
_connected = false;
if (status != ESP8266_COMMAND_OK)
return 0;
return size;
}
int ESP8266Client::peek()
{
return _esp8266->peek();
}
void ESP8266Client::flush()
{
return _esp8266->flush();
}
void ESP8266Client::stop()
{
_esp8266->close(_id);
_connected = false;
}
uint8_t ESP8266Client::connected()
{
ESP8266Connection connections[ESP8266_MAX_CONNECTIONS - 1];
ESP8266ConnectionStatus status;
unsigned int count;
// check _connected flag
if (_connected)
return 1;
// assume connected if data is available
if (available() > 0)
return 1;
// get connection status
if (_esp8266->getConnectionStatus(status, connections, count) != ESP8266_COMMAND_OK)
return 0;
for (unsigned int i = 0; i < count; i++) {
if (connections[i].id == _id) {
_connected = true;
return 1;
}
}
return 0;
}
ESP8266Client::operator bool()
{
return connected();
}