/
network_esp8266.c
1248 lines (1103 loc) · 42.9 KB
/
network_esp8266.c
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
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* This file is part of Espruino, a JavaScript interpreter for Microcontrollers
*
* Copyright (C) 2015 Gordon Williams <gw@pur3.co.uk>
*
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
*
* ----------------------------------------------------------------------------
* This file is designed to be parsed during the build process
*
* Contains ESP8266 board network specific functions.
* ----------------------------------------------------------------------------
*/
// ESP8266 specific includes
#include <c_types.h>
#include <user_interface.h>
#include <mem.h>
#include "osapi_release.h"
#include <espconn.h>
#include <espmissingincludes.h>
#define _GCC_WRAP_STDINT_H
typedef long long int64_t;
#include "network_esp8266.h"
#include "socketerrors.h"
#include "esp8266_board_utils.h"
#include "pktbuf.h"
//#define espconn_abort espconn_disconnect
// Set NET_DBG to 0 to disable debug printf's, to 1 for important printf's
#ifdef RELEASE
#define DBG(format, ...) do { } while(0)
#else
// Normal debug
#if NET_DBG > 0
#define DBG(format, ...) os_printf(format, ## __VA_ARGS__)
// #include "jsinteractive.h"
// #define DBG(format, ...) jsiConsolePrintf(format, ## __VA_ARGS__)
static char DBG_LIB[] = "net_esp8266"; // library name
#else
#define DBG(format, ...) do { } while(0)
#endif
#endif
static struct socketData *getSocketData(int s);
/**
* The next socketId to be used.
*/
static int g_nextSocketId = 0;
static int getServerSocketByLocalPort(unsigned short port);
static void setSocketInError(struct socketData *pSocketData, int code);
static void dumpEspConn(struct espconn *pEspConn);
static struct socketData *allocateNewSocket();
static int connectSocket(struct socketData *pSocketData);
static void doClose(struct socketData *pSocketData);
static void releaseSocket(struct socketData *pSocketData);
static void resetSocket(struct socketData *pSocketData);
static void esp8266_dumpSocketData(struct socketData *pSocketData);
static void esp8266_callback_connectCB_inbound(void *arg);
static void esp8266_callback_connectCB_outbound(void *arg);
static void esp8266_callback_disconnectCB(void *arg);
static void esp8266_callback_sentCB(void *arg);
static void esp8266_callback_writeFinishedCB(void *arg);
static void esp8266_callback_recvCB(void *arg, char *pData, unsigned short len);
static void esp8266_callback_reconnectCB(void *arg, sint8 err);
/** Socket data structure
*
* We maintain an array of socketData structures. The number of such structures is defined in the
* MAX_SOCKETS define. The private variable that contains the array is called "socketArray".
* Each one of these array instances represents a possible socket structure that we can use.
*
* Each socket maintains state and its creation purpose.
*
* The trickiest part is closing. If the socket lib closes a socket it forgets about the socket
* as soon as we return. We then have to issue a disconnect to espconn and await the disconnect
* call-back in the SOCKET_STATE_DISCONNECTING. Once that's done, we can deallocate everything.
* If we receive a disconnect from the remote end, we free the espconn struct and we transition
* to SOCKET_STATE_CLOSED until we can respond to a send/recv call from the
* socket library with -1 and it then calls close.
* In summary, the rules are:
* - SOCKET_STATE_DISCONNECTING: the socket lib has closed, we close when we get disconCB
* - SOCKET_STATE_CLOSED: we have closed espconn, awaiting socket lib to issue close
* - SOCKET_STATE_ABORTING: we await reconCB/disconCB, we also await socket lib to issue close
*/
/**
* The potential states for a socket.
* See the socket state diagram.
*/
enum SOCKET_STATE {
SOCKET_STATE_UNUSED, //!< Unused socket "slot"
SOCKET_STATE_UNACCEPTED, //!< New inbound connection that Espruino hasn't accepted yet
SOCKET_STATE_HOST_RESOLVING, //!< Resolving a hostname, happens before CONNECTING
SOCKET_STATE_CONNECTING, //!< In the process of connecting
SOCKET_STATE_IDLE, //!< Connected but nothing in tx buffers
SOCKET_STATE_TRANSMITTING, //!< Connected and espconn_send has been called, awaiting CB
SOCKET_STATE_DISCONNECTING, //!< Did disconnect, awaiting discon callback from espconn
SOCKET_STATE_ABORTING, //!< Did abort, awaiting discon callback from espconn
SOCKET_STATE_TO_ABORT, //!< Need to abort asap (couldn't do it in callback)
SOCKET_STATE_CLOSED, //!< Closed, espconn struct freed, awaiting close from socket lib
};
/**
* How was the socket created.
*/
enum SOCKET_CREATION_TYPE {
SOCKET_CREATED_NONE, //!< The socket has not yet been created.
SOCKET_CREATED_SERVER, //!< Listening socket ("server socket")
SOCKET_CREATED_OUTBOUND, //!< Outbound connection
SOCKET_CREATED_INBOUND //!< Inbound connection
};
/**
* The core socket structure.
* The structure is initialized by resetSocket.
*/
struct socketData {
int socketId; //!< The id of THIS socket.
enum SOCKET_STATE state; //!< What is the socket state?
enum SOCKET_CREATION_TYPE creationType; //!< How was the socket created?
struct espconn *pEspconn; //!< The ESPConn structure.
uint8 *currentTx; //!< Data currently being transmitted.
PktBuf *rxBufQ; //!< Queue of received buffers
short errorCode; //!< Error code, 0=no error
uint32_t host;
unsigned short port;
uint32_t multicastGrpIp;
uint32_t multicastIp;
};
/**
* An array of socket data structures.
*/
static struct socketData socketArray[MAX_SOCKETS];
/**
* Flag the sockets as initially NOT initialized.
*/
static bool g_socketsInitialized = false;
/**
* Dump all the socket structures.
* This is used exclusively for debugging. It walks through each of the
* socket structures and dumps their state to the debug log.
*/
void esp8266_dumpAllSocketData() {
#ifndef RELEASE
for (int i=0; i<MAX_SOCKETS; i++) {
esp8266_dumpSocketData(&socketArray[i]);
}
#endif
}
#ifndef RELEASE
/**
* Write the details of a socket to the debug log.
* The data associated with the socket is dumped to the debug log.
*/
void esp8266_dumpSocket(
int socketId //!< The ID of the socket data structure to be logged.
) {
struct socketData *pSocketData = getSocketData(socketId);
esp8266_dumpSocketData(pSocketData);
}
/**
* Write the details of a socketData to the debug log.
* The data associated with the socketData is dumped to the debug log.
*/
static void esp8266_dumpSocketData(
struct socketData *pSocketData //!< The socket data structure to be logged
) {
DBG("=== socket %d", pSocketData->socketId);
char *creationTypeMsg;
switch(pSocketData->creationType) {
case SOCKET_CREATED_NONE:
creationTypeMsg = "none";
break;
case SOCKET_CREATED_INBOUND:
creationTypeMsg = "inbound";
break;
case SOCKET_CREATED_OUTBOUND:
creationTypeMsg = "outbound";
break;
case SOCKET_CREATED_SERVER:
creationTypeMsg = "server";
break;
}
DBG(" type=%s, txBuf=%p", creationTypeMsg, pSocketData->currentTx);
char *stateMsg;
switch(pSocketData->state) {
case SOCKET_STATE_CLOSED:
stateMsg = "closing";
break;
case SOCKET_STATE_CONNECTING:
stateMsg = "connecting";
break;
case SOCKET_STATE_DISCONNECTING:
stateMsg = "disconnecting";
break;
case SOCKET_STATE_ABORTING:
stateMsg = "aborting";
break;
case SOCKET_STATE_IDLE:
stateMsg = "idle";
break;
case SOCKET_STATE_TRANSMITTING:
stateMsg = "transmitting";
break;
case SOCKET_STATE_HOST_RESOLVING:
stateMsg = "resolving";
break;
case SOCKET_STATE_UNACCEPTED:
stateMsg = "unaccepted";
break;
case SOCKET_STATE_UNUSED:
stateMsg = "unused";
break;
default:
stateMsg = "Unexpected state!!";
break;
}
DBG(", state=%s, espconn=%p, err=%d", stateMsg, pSocketData->pEspconn, pSocketData->errorCode);
DBG(", rx:");
for (PktBuf *b=pSocketData->rxBufQ; b; b=b->next) {
DBG(" %d@%p", b->filled, b);
}
DBG("\n");
}
#endif
/**
* Dump a struct espconn (for debugging purposes).
*/
#if 0
static void dumpEspConn(
struct espconn *pEspConn //!<
) {
char ipString[20];
LOG("Dump of espconn: 0x%x\n", (int)pEspConn);
if (pEspConn == NULL) {
return;
}
switch(pEspConn->type) {
case ESPCONN_TCP:
LOG(" - type = TCP\n");
LOG(" - local address = %d.%d.%d.%d [%d]\n",
pEspConn->proto.tcp->local_ip[0],
pEspConn->proto.tcp->local_ip[1],
pEspConn->proto.tcp->local_ip[2],
pEspConn->proto.tcp->local_ip[3],
pEspConn->proto.tcp->local_port);
LOG(" - remote address = %d.%d.%d.%d [%d]\n",
pEspConn->proto.tcp->remote_ip[0],
pEspConn->proto.tcp->remote_ip[1],
pEspConn->proto.tcp->remote_ip[2],
pEspConn->proto.tcp->remote_ip[3],
pEspConn->proto.tcp->remote_port);
break;
case ESPCONN_UDP:
LOG(" - type = UDP\n");
LOG(" - local_port = %d\n", pEspConn->proto.udp->local_port);
LOG(" - local_ip = %d.%d.%d.%d\n",
pEspConn->proto.udp->local_ip[0],
pEspConn->proto.udp->local_ip[1],
pEspConn->proto.udp->local_ip[2],
pEspConn->proto.udp->local_ip[3]);
LOG(" - remote_port = %d\n", pEspConn->proto.udp->remote_port);
LOG(" - remote_ip = %d.%d.%d.%d\n",
pEspConn->proto.udp->remote_ip[0],
pEspConn->proto.udp->remote_ip[1],
pEspConn->proto.udp->remote_ip[2],
pEspConn->proto.udp->remote_ip[3]);
break;
default:
LOG(" - type = Unknown!! 0x%x\n", pEspConn->type);
}
switch(pEspConn->state) {
case ESPCONN_NONE:
LOG(" - state=NONE");
break;
case ESPCONN_WAIT:
LOG(" - state=WAIT");
break;
case ESPCONN_LISTEN:
LOG(" - state=LISTEN");
break;
case ESPCONN_CONNECT:
LOG(" - state=CONNECT");
break;
case ESPCONN_WRITE:
LOG(" - state=WRITE");
break;
case ESPCONN_READ:
LOG(" - state=READ");
break;
case ESPCONN_CLOSE:
LOG(" - state=CLOSE");
break;
default:
LOG(" - state=unknown!!");
break;
}
LOG(", link_cnt=%d", pEspConn->link_cnt);
LOG(", reverse=0x%x\n", (unsigned int)pEspConn->reverse);
}
#endif
/**
* Get the next new global socket id.
* \return A new socketId that is assured to be unique.
*/
static int getNextGlobalSocketId() {
return ++g_nextSocketId;
}
/**
* Allocate a new socket
* Look for the first free socket in the array of sockets and return the first one
* that is available. The socketId property is set to a unique and new socketId value
* that will not previously have been seen.
* \return The socketData structure for the returned socket.
*/
static struct socketData *allocateNewSocket() {
// Walk through each of the sockets in the array of possible sockets and stop
// at the first one that is flagged as not in use. For that socket, set its
// socketId to the next global socketId value.
for (int i=0; i<MAX_SOCKETS; i++) {
if (socketArray[i].state == SOCKET_STATE_UNUSED) {
socketArray[i].socketId = getNextGlobalSocketId();
return &socketArray[i];
}
}
esp8266_dumpAllSocketData();
return(NULL);
}
/**
* Retrieve the socketData for the given socket index.
* \return The socket data for the given socket or NULL if there is no matching socket.
*/
static struct socketData *getSocketData(int socketId) {
struct socketData *pSocketData = socketArray;
for (int socketArrayIndex=0; socketArrayIndex<MAX_SOCKETS; socketArrayIndex++) {
if (pSocketData->socketId == socketId) {
return pSocketData;
}
pSocketData++;
}
DBG("%s: socket %d not found\n", DBG_LIB, socketId);
return NULL;
}
/**
* Find the server socket that is bound to the given local port.
* \return The socket id of the socket listening on the given port or -1 if there is no
* server socket that matches.
*/
static int getServerSocketByLocalPort(
unsigned short port //!< The port number on which a server socket is listening.
) {
// Loop through each of the sockets in the socket array looking for a socket
// that is inuse, a server and has a local_port of the passed in port number.
int socketArrayIndex;
struct socketData *pSocketData = socketArray;
for (socketArrayIndex=0; socketArrayIndex<MAX_SOCKETS; socketArrayIndex++) {
if (pSocketData->state != SOCKET_STATE_UNUSED &&
pSocketData->creationType == SOCKET_CREATED_SERVER &&
pSocketData->pEspconn->proto.tcp->local_port == port)
{
return pSocketData->socketId;
}
pSocketData++;
} // End of for each socket
return -1;
}
/**
* Release the socket and return it to the free pool.
* The connection (espconn) must be closed and deallocated before calling releaseSocket.
*/
static void releaseSocket(
struct socketData *pSocketData //!< The socket to release
) {
assert(pSocketData != NULL);
//DBG("%s: freeing socket %d\n", DBG_LIB, pSocketData->socketId);
assert(pSocketData->state != SOCKET_STATE_UNUSED);
assert(pSocketData->pEspconn == NULL);
// free any unconsumed receive buffers
while (pSocketData->rxBufQ != NULL)
pSocketData->rxBufQ = PktBuf_ShiftFree(pSocketData->rxBufQ);
if (pSocketData->currentTx != NULL) {
//DBG("%s: freeing tx buf %p\n", DBG_LIB, pSocketData->currentTx);
os_free(pSocketData->currentTx);
pSocketData->currentTx = NULL;
}
os_memset(pSocketData, 0, sizeof(struct socketData));
}
/**
* Release the espconn structure
*/
static void releaseEspconn(
struct socketData *pSocketData
) {
if (pSocketData->pEspconn == NULL) return;
// if the socket is an inbound connection then espconn will free the struct, else we do it now
if (pSocketData->creationType != SOCKET_CREATED_INBOUND) {
//DBG("%s: freeing espconn %p/%p for socket %d\n", DBG_LIB,
// pSocketData->pEspconn, pSocketData->pEspconn->proto.tcp, pSocketData->socketId);
os_free(pSocketData->pEspconn->proto.tcp);
pSocketData->pEspconn->proto.tcp = NULL;
os_free(pSocketData->pEspconn);
}
pSocketData->pEspconn = NULL;
}
/**
* Initialize the entire socket array
*/
void netInit_esp8266_board() {
if (g_socketsInitialized) return;
g_socketsInitialized = true;
os_memset(socketArray, 0, sizeof(socketArray));
}
/**
* Perform an actual closure of the socket by calling the ESP8266 disconnect API.
*/
static void doClose(
struct socketData *pSocketData //!< The socket to be closed.
) {
if (pSocketData == NULL) return; // just in case
// if we're already closing, then don't do anything
if (pSocketData->state == SOCKET_STATE_CLOSED ||
pSocketData->state == SOCKET_STATE_DISCONNECTING)
{
return;
}
// if we're in the name resolution phase, we don't have much to do
// if we're in aborting ditto
if (pSocketData->state == SOCKET_STATE_HOST_RESOLVING ||
pSocketData->state == SOCKET_STATE_ABORTING)
{
pSocketData->state = SOCKET_STATE_DISCONNECTING;
return;
}
// if we need to abort, then do that
if (pSocketData->state == SOCKET_STATE_TO_ABORT) {
espconn_abort(pSocketData->pEspconn);
pSocketData->state = SOCKET_STATE_DISCONNECTING;
return;
}
// Tell espconn to disconnect/delete the connection
if (pSocketData->creationType == SOCKET_CREATED_SERVER) {
//dumpEspConn(pSocketData->pEspconn);
int rc = espconn_delete(pSocketData->pEspconn);
if (rc != 0) {
setSocketInError(pSocketData, rc);
}
// we do not get a disconnected callback so we go straight to SOCKET_STATE_UNUSED
pSocketData->state = SOCKET_STATE_UNUSED;
pSocketData->creationType = SOCKET_CREATED_NONE;
} else {
int rc = espconn_disconnect(pSocketData->pEspconn);
if (rc == 0) {
pSocketData->state = SOCKET_STATE_DISCONNECTING;
} else {
setSocketInError(pSocketData, rc);
pSocketData->state = SOCKET_STATE_UNUSED; // don't expect a callback
pSocketData->creationType = SOCKET_CREATED_NONE;
}
}
}
/**
* Set the given socket as being in error supplying the espconn code.
* This translates the espconn code to an Espruino socket error code.
*/
static void setSocketInError(
struct socketData *pSocketData, //!< The socket that is being flagged as in error.
int code //!< The espconn error code
) {
assert(pSocketData != NULL);
assert(pSocketData->state != SOCKET_STATE_UNUSED);
if (pSocketData->errorCode != 0) return; // don't overwrite previous error
int err = code;
switch (code) {
case ESPCONN_MEM: err = SOCKET_ERR_MEM; break;
case ESPCONN_ABRT: err = SOCKET_ERR_RESET; break;
case ESPCONN_CLSD: err = SOCKET_ERR_CLOSED; break;
case ESPCONN_IF: err = SOCKET_ERR_UNKNOWN; break;
case ESPCONN_ISCONN: err = SOCKET_ERR_BUSY; break;
case ESPCONN_HANDSHAKE: err = SOCKET_ERR_SSL_HAND; break;
case ESPCONN_SSL_INVALID_DATA: err = SOCKET_ERR_SSL_INVALID; break;
}
pSocketData->errorCode = err;
DBG("%s: error %d->%d on socket %d: %s\n", DBG_LIB,
code, err, pSocketData->socketId, socketErrorString(err));
}
/**
* Callback function registered to the ESP8266 environment that is
* invoked when a new inbound connection has been formed.
*/
static void esp8266_callback_connectCB_inbound(
void *arg //!<
) {
struct espconn *pEspconn = (struct espconn *)arg;
assert(pEspconn != NULL);
struct socketData *pClientSocketData = allocateNewSocket();
if (pClientSocketData == NULL) {
DBG("%s: out of sockets, dropping inbound connection\n", DBG_LIB);
espconn_disconnect(pEspconn);
return;
}
DBG("%s: accepted socket %d inbound to port %d from %d.%d.%d.%d:%d\n", DBG_LIB,
pClientSocketData->socketId, pEspconn->proto.tcp->local_port,
IP2STR(pEspconn->proto.tcp->remote_ip), pEspconn->proto.tcp->remote_port);
//dumpEspConn(pEspconn);
// register callbacks on the new connection
if (pEspconn->type == ESPCONN_TCP) {
espconn_regist_disconcb(pEspconn, esp8266_callback_disconnectCB);
espconn_regist_reconcb(pEspconn, esp8266_callback_reconnectCB);
}
espconn_regist_sentcb(pEspconn, esp8266_callback_sentCB);
espconn_regist_recvcb(pEspconn, esp8266_callback_recvCB);
pClientSocketData->pEspconn = pEspconn;
pClientSocketData->pEspconn->reverse = pClientSocketData;
pClientSocketData->creationType = SOCKET_CREATED_INBOUND;
pClientSocketData->state = SOCKET_STATE_UNACCEPTED;
}
/**
* Callback function registered to the ESP8266 environment that is
* invoked when a new outbound connection has been formed.
*/
static void esp8266_callback_connectCB_outbound(
void *arg //!< A pointer to a `struct espconn`.
) {
struct espconn *pEspconn = (struct espconn *)arg;
assert(pEspconn != NULL);
struct socketData *pSocketData = (struct socketData *)pEspconn->reverse;
if (pSocketData == NULL) return; // stray callback (possibly after a disconnect)
DBG("%s: socket %d connected\n", DBG_LIB, pSocketData->socketId);
// if we're connecting, then move on, else ignore (could be that we're disconnecting)
if (pSocketData->state == SOCKET_STATE_CONNECTING) {
pSocketData->state = SOCKET_STATE_IDLE;
}
}
/**
* Callback function registered to the ESP8266 environment that is
* Invoked when a connection has been disconnected. This does get invoked if we
* initiated the disconnect (new since SDK 1.5?).
*/
static void esp8266_callback_disconnectCB(
void *arg //!< A pointer to a `struct espconn`.
) {
struct espconn *pEspconn = (struct espconn *)arg;
struct socketData *pSocketData = (struct socketData *)pEspconn->reverse;
if (pSocketData == NULL) return;
//if (pEspconn != pSocketData->pEspconn) DBG("%s: pEspconn changed in disconnectCB ***\n", DBG_LIB);
assert(pSocketData->state != SOCKET_STATE_UNUSED);
DBG("%s: socket %d disconnected\n", DBG_LIB, pSocketData->socketId);
// we can deallocate the espconn structure
releaseEspconn(pSocketData);
// if we were in SOCKET_STATE_DISCONNECTING the socket lib is already done with this socket,
// so we can free the whole thing. Otherwise, we transition to SOCKET_STATE_CLOSED because
// we will need to tell the socket lib about the disconnect.
if (pSocketData->state == SOCKET_STATE_DISCONNECTING) {
releaseSocket(pSocketData);
} else {
// we can deallocate the tx buffer
if (pSocketData->currentTx != NULL) {
//DBG("%s: freeing tx buf %p\n", DBG_LIB, pSocketData->currentTx);
os_free(pSocketData->currentTx);
pSocketData->currentTx = NULL;
}
pSocketData->state = SOCKET_STATE_CLOSED;
}
}
/**
* Error handler callback.
* Although this is called `reconnect` by Espressif, this is really a connection reset callback.
*/
static void esp8266_callback_reconnectCB(
void *arg, //!< A pointer to a `struct espconn`.
sint8 err //!< The error code.
) {
struct espconn *pEspconn = (struct espconn *)arg;
struct socketData *pSocketData = (struct socketData *)pEspconn->reverse;
if (pSocketData == NULL) return; // we already closed this.
//if (pEspconn != pSocketData->pEspconn) DBG("%s: pEspconn changed in reconnectCB ***\n", DBG_LIB);
DBG("%s: socket %d connection reset: Err %d - %s\n", DBG_LIB,
pSocketData->socketId, err, esp8266_errorToString(err));
// Do the same as for a disconnect
esp8266_callback_disconnectCB(arg);
// Set the socket state as in error (unless it got freed by esp8266_callback_disconnectCB)
if (pSocketData->state != SOCKET_STATE_UNUSED)
setSocketInError(pSocketData, err);
//DBG("%s: ret from reconnectCB\n", DBG_LIB);
}
/**
* Callback function registered to the ESP8266 environment that is
* invoked when a send operation has been completed. This signals that we can reuse the tx buffer
* and that we can send the next chunk of data.
*/
static void esp8266_callback_sentCB(
void *arg //!< A pointer to a `struct espconn`.
) {
struct espconn *pEspconn = (struct espconn *)arg;
struct socketData *pSocketData = (struct socketData *)pEspconn->reverse;
if (pSocketData == NULL) return; // we already closed this.
//if (pEspconn != pSocketData->pEspconn) DBG("%s: pEspconn changed in sentCB ***\n", DBG_LIB);
assert(pSocketData->state != SOCKET_STATE_UNUSED);
//DBG("%s: socket %d send completed\n", DBG_LIB, pSocketData->socketId);
// We have transmitted the data ... which means that the data that was in the transmission
// buffer can be released.
if (pSocketData->currentTx != NULL) {
os_free(pSocketData->currentTx);
pSocketData->currentTx = NULL;
}
if (pSocketData->state == SOCKET_STATE_TRANSMITTING) {
pSocketData->state = SOCKET_STATE_IDLE;
}
}
/**
* ESP8266 callback function that is invoked when new data has arrived over
* the TCP/IP connection.
*/
static void esp8266_callback_recvCB(
void *arg, //!< A pointer to a `struct espconn`.
char *pData, //!< A pointer to data received over the socket.
unsigned short len //!< The length of the data.
) {
struct espconn *pEspconn = (struct espconn *)arg;
struct socketData *pSocketData = (struct socketData *)pEspconn->reverse;
if (pSocketData == NULL) return; // we closed this socket
//if (pEspconn != pSocketData->pEspconn) DBG("%s: pEspconn changed in recvCB ***\n", DBG_LIB);
assert(pSocketData->state != SOCKET_STATE_UNUSED);
//DBG("%s: socket %d recv %d\n", DBG_LIB, pSocketData->socketId, len);
//DBG("%s: recv data: %p\n", DBG_LIB, pData);
// if this is a dead connection then just ignore the callback
if (pSocketData->state == SOCKET_STATE_ABORTING ||
pSocketData->state == SOCKET_STATE_TO_ABORT ||
pSocketData->state == SOCKET_STATE_CLOSED ||
pSocketData->state == SOCKET_STATE_DISCONNECTING) {
return;
}
bool isUDP = pEspconn->type == ESPCONN_UDP;
if (isUDP && len > 536/2 /*chunkSize*/) {
jsWarn("Recv %d too long (UDP)!\n", len);
return;
}
// Allocate a buffer and add to the receive queue
PktBuf *buf = PktBuf_New(len);
if (!buf) {
// handle out of memory condition
DBG("%s: Out of memory allocating %d for recv\n", DBG_LIB, len);
// at this point we're gonna deallocate all receive buffers as a panic measure
while (pSocketData->rxBufQ != NULL)
pSocketData->rxBufQ = PktBuf_ShiftFree(pSocketData->rxBufQ);
// save the error
setSocketInError(pSocketData, ESPCONN_MEM);
// now reset the connection
//espconn_abort(pEspconn); // can't do this: espconn crashes!
pSocketData->state = SOCKET_STATE_TO_ABORT; // some function called from socket lib will abort
//DBG("%s: ret from recvCB\n", DBG_LIB);
return;
}
if (isUDP) {
// UDP remote host/port
remot_info *premot = NULL;
if (espconn_get_connection_info(pEspconn, &premot, 0) == ESPCONN_OK) {
pSocketData->host = *(uint32_t *)&premot->remote_ip;
pSocketData->port = *(unsigned short *)&premot->remote_port;
}
}
DBG("%s: Recv %d to %x:%d\n", DBG_LIB, len, pSocketData->host, pSocketData->port);
// if this is the second buffer then stop the flood!
if (pSocketData->rxBufQ != NULL) espconn_recv_hold(pEspconn);
// got buffer, fill it
os_memcpy(buf->data, pData, len);
buf->filled = len;
pSocketData->rxBufQ = PktBuf_Push(pSocketData->rxBufQ, buf);
}
// -------------------------------------------------
/**
* Define the implementation functions for the logical network functions.
*/
void netSetCallbacks_esp8266_board(
JsNetwork *net //!< The Network we are going to use.
) {
net->idle = net_ESP8266_BOARD_idle;
net->checkError = net_ESP8266_BOARD_checkError;
net->createsocket = net_ESP8266_BOARD_createSocket;
net->closesocket = net_ESP8266_BOARD_closeSocket;
net->accept = net_ESP8266_BOARD_accept;
net->gethostbyname = net_ESP8266_BOARD_gethostbyname;
net->recv = net_ESP8266_BOARD_recv;
net->send = net_ESP8266_BOARD_send;
// The TCP MSS is 536, we use half that 'cause otherwise we easily run out of JSvars memory
net->chunkSize = 536/2;
}
/**
* Determine if there is a new client connection on the server socket.
* This function is called to poll to see if the serverSckt has a new
* accepted connection (socket) and, if it does, return it else return -1 to indicate
* that there was no new accepted socket.
*/
int net_ESP8266_BOARD_accept(
JsNetwork *net, //!< The Network we are going to use to create the socket.
int serverSckt //!< The socket that we are checking to see if there is a new client connection.
) {
struct socketData *pSocketData = getSocketData(serverSckt);
assert(pSocketData->state != SOCKET_STATE_UNUSED);
assert(pSocketData->creationType == SOCKET_CREATED_SERVER);
// iterate through all sockets and see whether there is one in the UNACCEPTED state that is for
// the server socket's local port.
uint16_t serverPort = pSocketData->pEspconn->proto.tcp->local_port;
for (uint8_t i=0; i<MAX_SOCKETS; i++) {
if (socketArray[i].state == SOCKET_STATE_UNACCEPTED &&
socketArray[i].pEspconn != NULL &&
socketArray[i].pEspconn->proto.tcp->local_port == serverPort)
{
DBG("%s: Accepted socket %d\n", DBG_LIB, socketArray[i].socketId);
socketArray[i].state = SOCKET_STATE_IDLE;
return socketArray[i].socketId;
}
}
return -1;
}
/**
* Receive data from the network device.
* Returns the number of bytes received which may be 0 and <0 if there was an error.
*/
int net_ESP8266_BOARD_recv(
JsNetwork *net, //!< The Network we are going to use to create the socket.
SocketType socketType, //!< The socket type bitmask
int sckt, //!< The socket from which we are to receive data.
void *buf, //!< The storage buffer into which we will receive data.
size_t len //!< The length of the buffer.
) {
//DBG("%s:recv\n", DBG_LIB);
struct socketData *pSocketData = getSocketData(sckt);
assert(pSocketData);
assert(pSocketData->state != SOCKET_STATE_UNUSED);
// handle socket that needs aborting
if (pSocketData->state == SOCKET_STATE_TO_ABORT) {
espconn_abort(pSocketData->pEspconn);
return pSocketData->errorCode;
}
// If there is no data in the receive buffer, then all we need do is return
// 0 bytes as the length of data moved or -1 if the socket is actually closed.
if (pSocketData->rxBufQ == NULL) {
switch (pSocketData->state) {
case SOCKET_STATE_CLOSED:
return pSocketData->errorCode != 0 ? pSocketData->errorCode : SOCKET_ERR_CLOSED;
case SOCKET_STATE_DISCONNECTING:
case SOCKET_STATE_ABORTING:
return pSocketData->errorCode;
case SOCKET_STATE_HOST_RESOLVING:
case SOCKET_STATE_CONNECTING:
return SOCKET_ERR_NO_CONN;
default:
return 0; // we just have no data
}
}
PktBuf *rxBuf = pSocketData->rxBufQ;
size_t delta = 0;
if (socketType & ST_UDP) {
JsNetUDPPacketHeader *header = (JsNetUDPPacketHeader*)buf;
delta = sizeof(JsNetUDPPacketHeader);
buf += delta;
len -= delta;
// UDP remote host/port
*(uint32_t*)&header->host = pSocketData->host;
header->port = pSocketData->port;
header->length = rxBuf->filled;
}
// If the receive buffer is able to completely fit in the buffer
// passed into us then we can copy all the data and the receive buffer will be clear.
if (rxBuf->filled <= len) {
os_memcpy(buf, rxBuf->data, rxBuf->filled);
uint16_t retLen = rxBuf->filled;
pSocketData->rxBufQ = PktBuf_ShiftFree(rxBuf);
// if we now have exactly one buffer enqueued we need to re-enable the flood
if (pSocketData->rxBufQ != NULL && pSocketData->rxBufQ->next == NULL)
espconn_recv_unhold(pSocketData->pEspconn);
//DBG("%s: socket %d JS recv %d\n", DBG_LIB, sckt, retLen);
return retLen + delta;
}
// If we are here, then we have more data in the receive buffer than is available
// to be returned in this request for data. So we have to copy the amount of data
// that is allowed to be returned and then strip that from the beginning of the
// receive buffer.
// First we copy the data we are going to return.
os_memcpy(buf, rxBuf->data, len);
// Next we shift up the remaining data
uint16_t newLen = rxBuf->filled - len;
os_memmove(rxBuf->data, rxBuf->data + len, newLen);
rxBuf->filled = newLen;
//DBG("%s: socket %d JS recv %d\n", DBG_LIB, sckt, len);
return len + delta;
}
/**
* Send data to the partner.
* The return is the number of bytes actually transmitted which may also be
* 0 to indicate no bytes sent or -1 to indicate an error. For the ESP8266 implementation we
* will return 0 if the socket is not connected or we are in the `SOCKET_STATE_TRANSMITTING`
* state.
*/
int net_ESP8266_BOARD_send(
JsNetwork *net, //!< The Network we are going to use to create the socket.
SocketType socketType, //!< The socket type bitmask
int sckt, //!< The socket over which we will send data.
const void *buf, //!< The buffer containing the data to be sent.
size_t len //!< The length of data in the buffer to send.
) {
//DBG("%s:send\n", DBG_LIB);
struct socketData *pSocketData = getSocketData(sckt);
assert(pSocketData->state != SOCKET_STATE_UNUSED);
DBG("%s:send state:%d err:%d\n", DBG_LIB, pSocketData->state, pSocketData->errorCode);
// If the socket is in error or it is closing return -1
switch (pSocketData->state) {
case SOCKET_STATE_CLOSED:
case SOCKET_STATE_DISCONNECTING:
return pSocketData->errorCode != 0 ? pSocketData->errorCode : SOCKET_ERR_CLOSED;
case SOCKET_STATE_ABORTING:
return pSocketData->errorCode;
case SOCKET_STATE_TO_ABORT:
espconn_abort(pSocketData->pEspconn);
return pSocketData->errorCode;
default:
break;
}
// Unless we are in the idle state, we can't send more shtuff
if (pSocketData->state != SOCKET_STATE_IDLE) {
return 0;
}
// Log the content of the data we are sending.
//esp8266_board_writeString(buf, len);
//os_printf("\n");
size_t delta = 0;
if (socketType & ST_UDP) {
JsNetUDPPacketHeader *header = (JsNetUDPPacketHeader*)buf;
// UDP remote IP/port need to be set everytime we call espconn_send
*(uint32_t *)&pSocketData->pEspconn->proto.tcp->remote_ip = *(uint32_t *)&header->host;
pSocketData->pEspconn->proto.tcp->remote_port = header->port;
delta = sizeof(JsNetUDPPacketHeader);
buf += delta;
len = header->length;
DBG("%s: Sendto %d to %x:%d\n", DBG_LIB, len, *(uint32_t *)&header->host, header->port);
}
// Copy the data to be sent into a transmit buffer we hand off to espconn
assert(pSocketData->currentTx == NULL);
pSocketData->currentTx = (uint8_t *)os_malloc(len);
if (pSocketData->currentTx == NULL) {
DBG("%s: Out of memory sending %d on socket %d\n", DBG_LIB, len, sckt);
setSocketInError(pSocketData, ESPCONN_MEM);
espconn_abort(pSocketData->pEspconn);
pSocketData->state = SOCKET_STATE_ABORTING;
return pSocketData->errorCode;
}
memcpy(pSocketData->currentTx, buf, len);
// Set transmitting now as the sentCB is called synchronously from inside
// espconn_send for UDP
pSocketData->state = SOCKET_STATE_TRANSMITTING;
// Send the data over the ESP8266 SDK.
int rc = espconn_send(pSocketData->pEspconn, pSocketData->currentTx, len);
if (rc < 0) {
setSocketInError(pSocketData, rc);
os_free(pSocketData->currentTx);
pSocketData->currentTx = NULL;
espconn_abort(pSocketData->pEspconn);
pSocketData->state = SOCKET_STATE_ABORTING;
return rc;
}
//DBG("%s: socket %d JS send %d\n", DBG_LIB, sckt, len);
return len + delta;
}
/**
* Perform idle processing.
* There is the possibility that we may wish to perform logic when we are idle. For the
* ESP8266 there is no specific idle network processing needed.
*/
void net_ESP8266_BOARD_idle(
JsNetwork *net //!< The Network we are part of.
) {
// Don't echo here because it is called continuously
//os_printf("> net_ESP8266_BOARD_idle\n");
}
/**
* Check for errors.
* Returns true if there are NO errors.
*/
bool net_ESP8266_BOARD_checkError(
JsNetwork *net //!< The Network we are checking.
) {
//os_printf("> net_ESP8266_BOARD_checkError\n");
return true;
}
/* Static variable hack to support async DNS resolutions. This is not great, but it works.
* There is only one call to net_ESP8266_BOARD_gethostbyname and it is immediately followed
* by a call to net_ESP8266_BOARD_createSocket, so we save the hostname from the first call
* in a global variable and then use it in the second to actually kick off the name resolution.
*/