-
Notifications
You must be signed in to change notification settings - Fork 37
Expand file tree
/
Copy pathMMTCPclient.c
More file actions
423 lines (410 loc) · 15.4 KB
/
MMTCPclient.c
File metadata and controls
423 lines (410 loc) · 15.4 KB
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
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
/***********************************************************************************************************************
PicoMite MMBasic
MMTCPclient.c
<COPYRIGHT HOLDERS> Geoff Graham, Peter Mather
Copyright (c) 2021, <COPYRIGHT HOLDERS> All rights reserved.
Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:
1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.
2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer
in the documentation and/or other materials provided with the distribution.
3. The name MMBasic be used when referring to the interpreter in any documentation and promotional material and the original copyright message be displayed
on the console at startup (additional copyright messages may be added).
4. All advertising materials mentioning features or use of this software must display the following acknowledgement: This product includes software developed
by the <copyright holder>.
5. Neither the name of the <copyright holder> nor the names of its contributors may be used to endorse or promote products derived from this software
without specific prior written permission.
THIS SOFTWARE IS PROVIDED BY <COPYRIGHT HOLDERS> AS IS AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL <COPYRIGHT HOLDERS> BE LIABLE FOR ANY DIRECT,
INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
************************************************************************************************************************/
#include "MMBasic_Includes.h"
#include "Hardware_Includes.h"
#define DEBUG_printf
TCP_CLIENT_T *TCP_CLIENT = NULL;
int streampointer = 0;
// Perform initialisation
TCP_CLIENT_T *tcp_client_init(void)
{
TCP_CLIENT_T *state = calloc(1, sizeof(TCP_CLIENT_T));
if (!state)
{
// DEBUG_printf("failed to allocate state\n");
return NULL;
}
// ip4addr_aton(TEST_TCP_SERVER_IP, &state->remote_addr);
return state;
}
// Call back with a DNS result
void tcp_dns_found(const char *hostname, const ip_addr_t *ipaddr, void *arg)
{
TCP_CLIENT_T *state = (TCP_CLIENT_T *)arg;
if (ipaddr)
{
state->remote_addr = *ipaddr;
char buff[STRINGSIZE] = {0};
sprintf(buff, "tcp address %s\r\n", ip4addr_ntoa(ipaddr));
if (!optionsuppressstatus)
MMPrintString(buff);
state->complete = 1;
// ntp_request(state);
}
else
{
free(state);
error("tcp dns request failed");
}
}
static err_t tcp_client_close(void *arg)
{
TCP_CLIENT_T *state = (TCP_CLIENT_T *)arg;
err_t err = ERR_OK;
if (state->tcp_pcb != NULL)
{
tcp_arg((struct tcp_pcb *)state->tcp_pcb, NULL);
tcp_poll((struct tcp_pcb *)state->tcp_pcb, NULL, 0);
tcp_sent((struct tcp_pcb *)state->tcp_pcb, NULL);
tcp_recv((struct tcp_pcb *)state->tcp_pcb, NULL);
tcp_err((struct tcp_pcb *)state->tcp_pcb, NULL);
err = tcp_close((struct tcp_pcb *)state->tcp_pcb);
if (err != ERR_OK)
{
// DEBUG_printf("close failed %d, calling abort\n", err);
tcp_abort((struct tcp_pcb *)state->tcp_pcb);
err = ERR_ABRT;
}
state->tcp_pcb = NULL;
}
return err;
}
static err_t tcp_client_sent(void *arg, struct tcp_pcb *tpcb, u16_t len)
{
// TCP_CLIENT_T *state = (TCP_CLIENT_T*)arg;
// DEBUG_printf("tcp_client_sent %u\r\n", len);
return ERR_OK;
}
static void tcp_client_err(void *arg, err_t err)
{
if (err != ERR_ABRT)
{
error("TCP client");
}
}
err_t tcp_client_recv(void *arg, struct tcp_pcb *tpcb, struct pbuf *p, err_t err)
{
TCP_CLIENT_T *state = (TCP_CLIENT_T *)arg;
if (!p)
{
return ERR_OK;
}
// this method is callback from lwIP, so cyw43_arch_lwip_begin is not required, however you
// can use this method to cause an assertion in debug mode, if this method is called when
// cyw43_arch_lwip_begin IS needed
// cyw43_arch_lwip_check();
if (p->tot_len > 0)
{
routinechecks(); // don't know why I'm doing this but it solves a race condition for the RP2350
// Receive the buffer
const uint16_t buffer_left = state->BUF_SIZE - state->buffer_len;
state->buffer_len += pbuf_copy_partial(p, (void *)state->buffer + state->buffer_len,
p->tot_len > buffer_left ? buffer_left : p->tot_len, 0);
tcp_recved(tpcb, p->tot_len);
cyw43_arch_lwip_begin();
uint64_t *x = (uint64_t *)state->buffer;
x--;
*x = state->buffer_len;
cyw43_arch_lwip_end();
}
pbuf_free(p);
return ERR_OK;
}
err_t tcp_client_recv_stream(void *arg, struct tcp_pcb *tpcb, struct pbuf *p, err_t err)
{
TCP_CLIENT_T *state = (TCP_CLIENT_T *)arg;
if (!p)
{
return ERR_OK;
}
// this method is callback from lwIP, so cyw43_arch_lwip_begin is not required, however you
// can use this method to cause an assertion in debug mode, if this method is called when
// cyw43_arch_lwip_begin IS needed
// cyw43_arch_lwip_check();
if (p->tot_len > 0)
{
for (int j = 0; j < p->tot_len; j++)
{
state->buffer[*state->buffer_write] = ((char *)p->payload)[j];
*state->buffer_write = (*state->buffer_write + 1) % state->BUF_SIZE; // advance the head of the queue
if (*state->buffer_write == *state->buffer_read)
{ // if the buffer has overflowed
*state->buffer_read = (*state->buffer_read + 1) % state->BUF_SIZE; // throw away the oldest char
}
}
// cyw43_arch_lwip_end();
tcp_recved(tpcb, p->tot_len);
}
pbuf_free(p);
return ERR_OK;
}
static err_t tcp_client_connected(void *arg, struct tcp_pcb *tpcb, err_t err)
{
TCP_CLIENT_T *state = (TCP_CLIENT_T *)arg;
if (err != ERR_OK)
{
error("connect failed %", err);
}
if (!optionsuppressstatus)
MMPrintString("Connected\r\n");
state->connected = true;
return ERR_OK;
}
static bool tcp_client_open(void *arg)
{
TCP_CLIENT_T *state = (TCP_CLIENT_T *)arg;
char buff[STRINGSIZE] = {0};
sprintf("Connecting to %s port %u\r\n", ip4addr_ntoa(&state->remote_addr), state->TCP_PORT);
if (!optionsuppressstatus)
MMPrintString(buff);
state->tcp_pcb = tcp_new_ip_type(IP_GET_TYPE(&state->remote_addr));
if (!state->tcp_pcb)
{
error("failed to create pcb");
return false;
}
tcp_arg((struct tcp_pcb *)state->tcp_pcb, state);
// tcp_poll(state->tcp_pcb, tcp_client_poll, POLL_TIME_S * 2);
tcp_sent((struct tcp_pcb *)state->tcp_pcb, tcp_client_sent);
if (state->buffer_write == NULL)
tcp_recv((struct tcp_pcb *)state->tcp_pcb, tcp_client_recv);
else
tcp_recv((struct tcp_pcb *)state->tcp_pcb, tcp_client_recv_stream);
tcp_err((struct tcp_pcb *)state->tcp_pcb, tcp_client_err);
state->buffer_len = 0;
// cyw43_arch_lwip_begin/end should be used around calls into lwIP to ensure correct locking.
// You can omit them if you are in a callback from lwIP. Note that when using pico_cyw_arch_poll
// these calls are a no-op and can be omitted, but it is a good practice to use them in
// case you switch the cyw43_arch type later.
// cyw43_arch_lwip_begin();
err_t err = tcp_connect((struct tcp_pcb *)state->tcp_pcb, &state->remote_addr, state->TCP_PORT, tcp_client_connected);
// cyw43_arch_lwip_end();
return err == ERR_OK;
}
void close_tcpclient(void)
{
TCP_CLIENT_T *state = TCP_CLIENT;
if (!state)
return;
tcp_client_close(state);
free(state);
TCP_CLIENT = NULL;
}
int cmd_tcpclient(void)
{
unsigned char *tp;
tp = checkstring(cmdline, (unsigned char *)"OPEN TCP CLIENT");
if (tp)
{
int timeout = 5000;
getcsargs(&tp, 5);
if (argc < 3)
SyntaxError();
;
ip4_addr_t remote_addr;
char *IP = GetTempStrMemory();
TCP_CLIENT_T *state = tcp_client_init();
IP = (char *)getCstring(argv[0]);
int port = getint(argv[2], 1, 65535);
if (argc == 5)
timeout = getint(argv[4], 1, 100000);
TCP_CLIENT = state;
state->TCP_PORT = port;
state->buffer_write = NULL;
if (!isalpha((uint8_t)*IP) && strchr(IP, '.') && strchr(IP, '.') < IP + 4)
{
if (!ip4addr_aton(IP, &remote_addr))
error("Invalid address format");
state->remote_addr = remote_addr;
}
else
{
int err = dns_gethostbyname(IP, &remote_addr, tcp_dns_found, state);
if (err == ERR_OK)
state->remote_addr = remote_addr;
else if (err == ERR_INPROGRESS)
{
Timer4 = timeout;
while (!state->complete && Timer4 && !(err == ERR_OK))
if (startupcomplete)
cyw43_arch_poll();
if (!Timer4)
error("Failed to convert web address");
state->complete = 0;
}
else
error("Failed to find TCP address");
}
if (!tcp_client_open(state))
{
error("Failed to open client");
}
Timer4 = timeout;
while (!state->connected && Timer4)
if (startupcomplete)
cyw43_arch_poll();
if (!Timer4)
error("No response from client");
return 1;
}
tp = checkstring(cmdline, (unsigned char *)"OPEN TCP STREAM");
if (tp)
{
int timeout = 5000;
getcsargs(&tp, 5);
if (argc < 3)
SyntaxError();
;
ip4_addr_t remote_addr;
char *IP = GetTempStrMemory();
TCP_CLIENT_T *state = tcp_client_init();
IP = (char *)getCstring(argv[0]);
int port = getint(argv[2], 1, 65535);
if (argc == 5)
timeout = getint(argv[4], 1, 100000);
TCP_CLIENT = state;
state->TCP_PORT = port;
state->buffer_write = &streampointer;
if (!isalpha((uint8_t)*IP) && strchr(IP, '.') && strchr(IP, '.') < IP + 4)
{
if (!ip4addr_aton(IP, &remote_addr))
error("Invalid address format");
state->remote_addr = remote_addr;
}
else
{
int err = dns_gethostbyname(IP, &remote_addr, tcp_dns_found, state);
if (err == ERR_OK)
state->remote_addr = remote_addr;
else if (err == ERR_INPROGRESS)
{
Timer4 = timeout;
while (!state->complete && Timer4 && !(err == ERR_OK))
ProcessWeb(0);
if (!Timer4)
error("Failed to convert web address");
state->complete = 0;
}
else
error("Failed to find TCP address");
}
if (!tcp_client_open(state))
{
error("Failed to open client");
}
Timer4 = timeout;
while (!state->connected && Timer4)
{
{
if (startupcomplete)
ProcessWeb(0);
}
}
if (!Timer4)
error("No response from client");
return 1;
}
tp = checkstring(cmdline, (unsigned char *)"TCP CLIENT REQUEST");
if (tp)
{
int64_t *dest = NULL;
uint8_t *q = NULL;
int size = 0, timeout = 5000;
TCP_CLIENT_T *state = TCP_CLIENT;
getcsargs(&tp, 5);
if (!state)
error("No connection");
if (!state->connected)
error("No connection");
if (argc < 3)
SyntaxError();
;
char *request = (char *)getstring(argv[0]);
size = parseintegerarray(argv[2], &dest, 2, 1, NULL, true, NULL) * 8;
dest[0] = 0;
q = (uint8_t *)&dest[1];
if (argc == 5)
timeout = getint(argv[4], 1, 100000);
state->BUF_SIZE = size;
state->buffer = q;
state->buffer_len = 0;
err_t err = tcp_write((struct tcp_pcb *)state->tcp_pcb, &request[1], (uint32_t)request[0], 0);
if (err)
error("write failed %", err);
Timer4 = timeout;
while (!state->buffer_len && Timer4)
ProcessWeb(0);
if (!Timer4)
error("No response from server");
else
Timer4 = 200;
while (Timer4)
ProcessWeb(0);
return 1;
}
tp = checkstring(cmdline, (unsigned char *)"TCP CLIENT STREAM");
if (tp)
{
void *ptr1 = NULL;
int64_t *dest = NULL;
uint8_t *q = NULL;
int size = 0;
TCP_CLIENT_T *state = TCP_CLIENT;
getcsargs(&tp, 7);
if (!state)
error("No connection");
if (!state->connected)
error("No connection");
if (argc != 7)
SyntaxError();
;
char *request = (char *)getstring(argv[0]);
size = parseintegerarray(argv[2], &dest, 2, 1, NULL, true, NULL) * 8;
dest[0] = 0;
q = (uint8_t *)&dest[1];
ptr1 = findvar(argv[4], V_FIND | V_NOFIND_ERR);
if (g_vartbl[g_VarIndex].type & T_INT)
{
if (g_vartbl[g_VarIndex].dims[0] != 0)
error("Argument 3 must be an integer");
state->buffer_read = (int *)ptr1;
}
else
error("Argument 3 must be an integer");
ptr1 = findvar(argv[6], V_FIND | V_NOFIND_ERR);
if (g_vartbl[g_VarIndex].type & T_INT)
{
if (g_vartbl[g_VarIndex].dims[0] != 0)
error("Argument 4 must be an integer");
state->buffer_write = (int *)ptr1;
}
else
error("Argument 4 must be an integer");
state->BUF_SIZE = size;
state->buffer = q;
state->buffer_len = 0;
err_t err = tcp_write((struct tcp_pcb *)state->tcp_pcb, &request[1], (uint32_t)request[0], 0);
if (err)
error("write failed %", err);
return 1;
}
tp = checkstring(cmdline, (unsigned char *)"CLOSE TCP CLIENT");
if (tp)
{
TCP_CLIENT_T *state = TCP_CLIENT;
if (!state)
error("No connection");
close_tcpclient();
return 1;
}
return 0;
}