1
+ #include " WiFiClient.h"
2
+
3
+ /* -------------------------------------------------------------------------- */
4
+ WiFiClient::WiFiClient () : _sock(-1 ) { }
5
+ /* -------------------------------------------------------------------------- */
6
+
7
+ /* -------------------------------------------------------------------------- */
8
+ void WiFiClient::getSocket () {
9
+ /* -------------------------------------------------------------------------- */
10
+ if (_sock == -1 ) {
11
+ string res = " " ;
12
+ modem.begin ();
13
+
14
+ if (modem.write (string (PROMPT (_BEGINCLIENT)),res, " %s" , CMD (_BEGINCLIENT))) {
15
+ _sock = atoi (res.c_str ());
16
+ Serial.print (" GET SOCKET: " );
17
+ Serial.println (_sock);
18
+ }
19
+ }
20
+ }
21
+
22
+
23
+
24
+ /* -------------------------------------------------------------------------- */
25
+ int WiFiClient::connect (IPAddress ip, uint16_t port){
26
+ /* -------------------------------------------------------------------------- */
27
+ getSocket ();
28
+ if (_sock >= 0 ) {
29
+ string res = " " ;
30
+ modem.begin ();
31
+ if (modem.write (string (PROMPT (_CLIENTCONNECTIP)),res, " %s%d,%s,%d\r\n " , CMD_WRITE (_CLIENTCONNECTIP), _sock, ip.toString (),port)) {
32
+ return 1 ;
33
+ }
34
+ }
35
+ return 0 ;
36
+ }
37
+
38
+ /* -------------------------------------------------------------------------- */
39
+ int WiFiClient::connect (const char *host, uint16_t port){
40
+ /* -------------------------------------------------------------------------- */
41
+ getSocket ();
42
+ if (_sock >= 0 ) {
43
+ string res = " " ;
44
+ modem.begin ();
45
+ if (modem.write (string (PROMPT (_CLIENTCONNECTNAME)),res, " %s%d,%s,%d\r\n " , CMD_WRITE (_CLIENTCONNECTNAME), _sock, host,port)) {
46
+ return 1 ;
47
+ }
48
+ }
49
+ return 0 ;
50
+ }
51
+
52
+ /* -------------------------------------------------------------------------- */
53
+ size_t WiFiClient::write (uint8_t b){
54
+ /* -------------------------------------------------------------------------- */
55
+ return write (&b, 1 );
56
+ }
57
+
58
+ /* -------------------------------------------------------------------------- */
59
+ size_t WiFiClient::write (const uint8_t *buf, size_t size){
60
+ /* -------------------------------------------------------------------------- */
61
+ if (_sock >= 0 ) {
62
+ string res = " " ;
63
+ modem.begin ();
64
+ modem.write_nowait (string (PROMPT (_CLIENTSEND)),res, " %s%d,%d\r\n " , CMD_WRITE (_CLIENTSEND), _sock, size);
65
+ if (modem.passthrough (buf,size)) {
66
+ return size;
67
+ }
68
+
69
+ }
70
+ return 0 ;
71
+
72
+ }
73
+
74
+
75
+
76
+ /* -------------------------------------------------------------------------- */
77
+ int WiFiClient::available (){
78
+ /* -------------------------------------------------------------------------- */
79
+ int rv = 0 ;
80
+ if (_sock >= 0 ) {
81
+ string res = " " ;
82
+ modem.begin ();
83
+ if (modem.write (string (PROMPT (_AVAILABLE)),res, " %s%d\r\n " , CMD_WRITE (_AVAILABLE), _sock)) {
84
+ rv = atoi (res.c_str ());
85
+ }
86
+ }
87
+ return rv;
88
+ }
89
+
90
+ /* -------------------------------------------------------------------------- */
91
+ int WiFiClient::read () {
92
+ /* -------------------------------------------------------------------------- */
93
+ uint8_t b;
94
+ if (read (&b, 1 ) == 1 ) {
95
+ return b;
96
+ }
97
+ return -1 ;
98
+ }
99
+
100
+ /* -------------------------------------------------------------------------- */
101
+ int WiFiClient::read (uint8_t *buf, size_t size) {
102
+ /* -------------------------------------------------------------------------- */
103
+ int rv = -1 ;
104
+ if (_sock >= 0 ) {
105
+ string res = " " ;
106
+ modem.begin ();
107
+ vector<string> tokens;
108
+ if (modem.write (string (PROMPT (_CLIENTRECEIVE)),res, " %s%d,%d\r\n " , CMD_WRITE (_CLIENTRECEIVE), _sock, size)) {
109
+ split (tokens, res, string (" |" ));
110
+ if (tokens.size () >= 2 ) {
111
+ rv = atoi (tokens[0 ].c_str ());
112
+ memcpy (buf,tokens[1 ].data (), (rv < size) ? rv : size);
113
+ }
114
+ }
115
+ }
116
+ return rv;
117
+ }
118
+
119
+ /* -------------------------------------------------------------------------- */
120
+ int WiFiClient::peek () {
121
+ /* -------------------------------------------------------------------------- */
122
+ int rv = -1 ;
123
+ if (_sock >= 0 ) {
124
+ string res = " " ;
125
+ modem.begin ();
126
+ if (modem.write (string (PROMPT (_PEEK)),res, " %s%d\r\n " , CMD_WRITE (_PEEK), _sock)) {
127
+ rv = atoi (res.c_str ());
128
+ }
129
+ }
130
+ return rv;
131
+ }
132
+
133
+
134
+ /* -------------------------------------------------------------------------- */
135
+ void WiFiClient::flush () {
136
+ /* -------------------------------------------------------------------------- */
137
+ if (_sock >= 0 ) {
138
+ string res = " " ;
139
+ modem.begin ();
140
+ modem.write (string (PROMPT (_CLIENTFLUSH)),res, " %s%d\r\n " , CMD_WRITE (_CLIENTFLUSH), _sock);
141
+ }
142
+ }
143
+
144
+ /* -------------------------------------------------------------------------- */
145
+ void WiFiClient::stop () {
146
+ /* -------------------------------------------------------------------------- */
147
+ if (_sock >= 0 ) {
148
+ string res = " " ;
149
+ modem.begin ();
150
+ modem.write (string (PROMPT (_CLIENTCLOSE)),res, " %s%d\r\n " , CMD_WRITE (_CLIENTCLOSE), _sock);
151
+ _sock = -1 ;
152
+ }
153
+ }
154
+
155
+ /* -------------------------------------------------------------------------- */
156
+ uint8_t WiFiClient::connected () {
157
+ /* -------------------------------------------------------------------------- */
158
+ uint8_t rv = 0 ;
159
+ if (_sock >= 0 ) {
160
+ string res = " " ;
161
+ modem.begin ();
162
+ if (modem.write (string (PROMPT (_CLIENTCONNECTED)),res, " %s%d\r\n " , CMD_WRITE (_CLIENTCONNECTED), _sock)) {
163
+ rv = atoi (res.c_str ());
164
+ }
165
+ }
166
+ return rv;
167
+ }
168
+
169
+
170
+
171
+ /* -------------------------------------------------------------------------- */
172
+ IPAddress WiFiClient::remoteIP () {
173
+ /* -------------------------------------------------------------------------- */
174
+ IPAddress ip;
175
+ if (_sock >= 0 ) {
176
+ string res = " " ;
177
+ modem.begin ();
178
+ if (modem.write (string (PROMPT (_REMOTEIP)),res, " %s%d\r\n " , CMD_WRITE (_REMOTEIP), _sock)) {
179
+ ip.fromString (res.c_str ());
180
+ return ip;
181
+ }
182
+ }
183
+ return IPAddress (0 ,0 ,0 ,0 );
184
+ }
185
+
186
+ /* -------------------------------------------------------------------------- */
187
+ uint16_t WiFiClient::remotePort (){
188
+ /* -------------------------------------------------------------------------- */
189
+ uint16_t rv = 0 ;
190
+ if (_sock >= 0 ) {
191
+ string res = " " ;
192
+ modem.begin ();
193
+ if (modem.write (string (PROMPT (_REMOTEPORT)),res, " %s%d\r\n " , CMD_WRITE (_REMOTEPORT), _sock)) {
194
+ rv = atoi (res.c_str ());
195
+ return rv;
196
+ }
197
+ }
198
+ return rv;
199
+ }
0 commit comments