-
Notifications
You must be signed in to change notification settings - Fork 1
/
M590Client.cpp
214 lines (165 loc) · 5.12 KB
/
M590Client.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
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
/*--------------------------------------------------------------------
This file is part of the Arduino M590 library.
The Arduino M590 library is free software: you can redistribute it
and/or modify it under the terms of the GNU General Public License as
published by the Free Software Foundation, either version 3 of the
License, or (at your option) any later version.
The Arduino M590 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 General Public License for more details.
You should have received a copy of the GNU General Public License
along with The Arduino M590 library. If not, see
<http://www.gnu.org/licenses/>.
--------------------------------------------------------------------*/
#include <inttypes.h>
#include "M590Client.h"
#include "debug.h"
int16_t M590Client::_state[MAX_LINK] = {NA_STATE, NA_STATE};
M590Client::M590Client(M590Drv *dev) : _link(255), _modem(dev) {
}
M590Client::M590Client(M590Drv *dev, uint8_t link) : _link(link), _modem(dev) {
}
////////////////////////////////////////////////////////////////////////////////
// Overrided Print methods
////////////////////////////////////////////////////////////////////////////////
// the standard print method will call write for each character in the buffer
// this is very slow on ESP
size_t M590Client::print(const __FlashStringHelper *ifsh) {
printFSH(ifsh, false);
}
// if we do override this, the standard println will call the print
// method twice
size_t M590Client::println(const __FlashStringHelper *ifsh) {
printFSH(ifsh, true);
}
////////////////////////////////////////////////////////////////////////////////
// Implementation of Client virtual methods
////////////////////////////////////////////////////////////////////////////////
int M590Client::connect(const char *host, uint16_t port) {
IPAddress ip;
if (_modem->urlResolve(host, ip)) {
return connect(ip, port);
}
return 0;
}
int M590Client::connect_P(const char *host, uint16_t port) {
char buf[strlen_P((char*)host) + 1];
strncpy_P(buf, (char*)host, sizeof(buf));
return connect(buf, port);
}
int M590Client::connect(IPAddress ip, uint16_t port) {
LOGINFO1(F("Connecting to"), ip);
_link = get_first_link();
if (_link != NO_LINK_AVAIL) {
if (!_modem->tcpConnect(ip, port, _link)) return 0;
_state[_link] = _link;
}
else {
Serial.println(F("No socket available"));
return 0;
}
return 1;
}
size_t M590Client::write(uint8_t b) {
return write(&b, 1);
}
size_t M590Client::write(const uint8_t *buf, size_t size) {
if (_link >= MAX_LINK || size==0) {
setWriteError();
return 0;
}
if (!_modem->tcpWrite(buf, size, _link)) {
setWriteError();
LOGERROR1(F("Failed to write to socket"), _link);
delay(1000);
stop();
return 0;
}
return size;
}
int M590Client::available() {
if (_link != 255) {
int bytes = _modem->avlData(_link);
return bytes;
}
}
int M590Client::read() {
uint8_t b;
if (!available()) return -1;
bool conn_close = false;
_modem->readData(&b, false, _link, &conn_close);
if (conn_close) {
_state[_link] = NA_STATE;
_link = 255;
}
return b;
}
int M590Client::read(uint8_t *buf, size_t size) {
if (!available()) return -1;
return _modem->readDataBuf(buf, size, _link);
}
int M590Client::peek() {
uint8_t b;
if (!available()) return -1;
bool conn_close = false;
_modem->readData(&b, true, _link, &conn_close);
if (conn_close) {
_state[_link] = NA_STATE;
_link = 255;
}
return b;
}
void M590Client::flush() {
while (available())
read();
}
void M590Client::stop() {
if (_link == 255) return;
LOGINFO1(F("Disconnecting link"), _link);
_modem->tcpClose(_link);
_state[_link] = NA_STATE;
_link = 255;
}
uint8_t M590Client::connected() {
return (status() == ESTABLISHED);
}
M590Client::operator bool() {
return _link != 255;
}
////////////////////////////////////////////////////////////////////////////////
// Additional WiFi standard methods
////////////////////////////////////////////////////////////////////////////////
uint8_t M590Client::status() {
if (_link == 255) return CLOSED;
if (_modem->avlData(_link)) return ESTABLISHED;
if (_modem->linkStatus(_link)) return ESTABLISHED;
_state[_link] = NA_STATE;
_link = 255;
return CLOSED;
}
////////////////////////////////////////////////////////////////////////////////
// Private Methods
////////////////////////////////////////////////////////////////////////////////
uint8_t M590Client::get_first_link() {
for (int i = 0; i < MAX_LINK; i++) {
if (_state[i] == NA_STATE) return i;
}
return LINK_NOT_AVAIL;
}
size_t M590Client::printFSH(const __FlashStringHelper *ifsh, bool appendCrLf) {
size_t size = strlen_P((char*)ifsh);
if (_link >= MAX_LINK || size==0) {
setWriteError();
return 0;
}
if (!_modem->tcpWrite(ifsh, size, _link, appendCrLf)) {
setWriteError();
LOGERROR1(F("Failed to write to socket"), _link);
delay(1000);
stop();
return 0;
}
return size;
}
/* vim: set ft=cpp ai ts=2 sts=2 et sw=2 sta nowrap nu : */