26
26
#define ESP8266_ALL_SOCKET_IDS -1
27
27
28
28
ESP8266::ESP8266 (PinName tx, PinName rx, bool debug, PinName rts, PinName cts)
29
- : _serial(tx, rx, ESP8266_DEFAULT_BAUD_RATE),
29
+ : _sdk_v(-1 ,-1 ,-1 ),
30
+ _at_v(-1 ,-1 ,-1 ),
31
+ _tcp_passive(false ),
32
+ _serial(tx, rx, ESP8266_DEFAULT_BAUD_RATE),
30
33
_serial_rts(rts),
31
34
_serial_cts(cts),
32
35
_parser(&_serial),
33
- _tcp_passive(false ),
34
36
_packets(0 ),
35
37
_packets_end(&_packets),
36
- _sdk_v(-1 ,-1 ,-1 ),
37
- _at_v(-1 ,-1 ,-1 ),
38
+ _heap_usage(0 ),
38
39
_connect_error(0 ),
39
40
_fail(false ),
40
41
_sock_already(false ),
41
42
_closed(false ),
42
- _connection_status(NSAPI_STATUS_DISCONNECTED),
43
- _heap_usage(0 )
43
+ _connection_status(NSAPI_STATUS_DISCONNECTED)
44
44
{
45
45
_serial.set_baud ( ESP8266_DEFAULT_BAUD_RATE );
46
46
_parser.debug_on (debug);
47
47
_parser.set_delimiter (" \r\n " );
48
- _parser.oob (" +IPD" , callback (this , &ESP8266::_packet_handler ));
48
+ _parser.oob (" +IPD" , callback (this , &ESP8266::_oob_packet_hdlr ));
49
49
// Note: espressif at command document says that this should be +CWJAP_CUR:<error code>
50
50
// but seems that at least current version is not sending it
51
51
// https://www.espressif.com/sites/default/files/documentation/4a-esp8266_at_instruction_set_en.pdf
52
52
// Also seems that ERROR is not sent, but FAIL instead
53
- _parser.oob (" +CWJAP: " , callback (this , &ESP8266::_connect_error_handler ));
54
- _parser.oob (" 0 ,CLOSED" , callback (this , &ESP8266::_oob_socket0_closed_handler ));
55
- _parser.oob (" 1 ,CLOSED" , callback (this , &ESP8266::_oob_socket1_closed_handler ));
56
- _parser.oob (" 2 ,CLOSED" , callback (this , &ESP8266::_oob_socket2_closed_handler ));
57
- _parser.oob (" 3 ,CLOSED" , callback (this , &ESP8266::_oob_socket3_closed_handler ));
58
- _parser.oob (" 4,CLOSED " , callback (this , &ESP8266::_oob_socket4_closed_handler ));
59
- _parser.oob (" WIFI " , callback (this , &ESP8266::_connection_status_handler ));
60
- _parser.oob (" UNLINK" , callback (this , &ESP8266::_oob_socket_close_error ));
61
- _parser.oob (" ALREADY CONNECTED" , callback (this , &ESP8266::_oob_cipstart_already_connected ));
53
+ _parser.oob (" 0,CLOSED " , callback (this , &ESP8266::_oob_socket0_closed ));
54
+ _parser.oob (" 1 ,CLOSED" , callback (this , &ESP8266::_oob_socket1_closed ));
55
+ _parser.oob (" 2 ,CLOSED" , callback (this , &ESP8266::_oob_socket2_closed ));
56
+ _parser.oob (" 3 ,CLOSED" , callback (this , &ESP8266::_oob_socket3_closed ));
57
+ _parser.oob (" 4 ,CLOSED" , callback (this , &ESP8266::_oob_socket4_closed ));
58
+ _parser.oob (" +CWJAP: " , callback (this , &ESP8266::_oob_connect_err ));
59
+ _parser.oob (" WIFI " , callback (this , &ESP8266::_oob_connection_status ));
60
+ _parser.oob (" UNLINK" , callback (this , &ESP8266::_oob_socket_close_err ));
61
+ _parser.oob (" ALREADY CONNECTED" , callback (this , &ESP8266::_oob_cipstart_already ));
62
62
63
63
for (int i= 0 ; i < SOCKET_COUNT; i++) {
64
- _sinfo [i].open = false ;
65
- _sinfo [i].proto = NSAPI_UDP;
64
+ _sock_i [i].open = false ;
65
+ _sock_i [i].proto = NSAPI_UDP;
66
66
}
67
67
}
68
68
@@ -377,7 +377,7 @@ int ESP8266::scan(WiFiAccessPoint *res, unsigned limit)
377
377
return NSAPI_ERROR_DEVICE_ERROR;
378
378
}
379
379
380
- while (recv_ap (&ap)) {
380
+ while (_recv_ap (&ap)) {
381
381
if (cnt < limit) {
382
382
res[cnt] = WiFiAccessPoint (ap);
383
383
}
@@ -398,7 +398,7 @@ nsapi_error_t ESP8266::open_udp(int id, const char* addr, int port, int local_po
398
398
static const char *type = " UDP" ;
399
399
bool done = false ;
400
400
401
- if (id >= SOCKET_COUNT || _sinfo [id].open ) {
401
+ if (id >= SOCKET_COUNT || _sock_i [id].open ) {
402
402
return NSAPI_ERROR_PARAMETER;
403
403
}
404
404
@@ -423,8 +423,8 @@ nsapi_error_t ESP8266::open_udp(int id, const char* addr, int port, int local_po
423
423
}
424
424
continue ;
425
425
}
426
- _sinfo [id].open = true ;
427
- _sinfo [id].proto = NSAPI_UDP;
426
+ _sock_i [id].open = true ;
427
+ _sock_i [id].proto = NSAPI_UDP;
428
428
break ;
429
429
}
430
430
}
@@ -440,7 +440,7 @@ nsapi_error_t ESP8266::open_tcp(int id, const char* addr, int port, int keepaliv
440
440
static const char *type = " TCP" ;
441
441
bool done = false ;
442
442
443
- if (id >= SOCKET_COUNT || _sinfo [id].open ) {
443
+ if (id >= SOCKET_COUNT || _sock_i [id].open ) {
444
444
return NSAPI_ERROR_PARAMETER;
445
445
}
446
446
@@ -465,8 +465,8 @@ nsapi_error_t ESP8266::open_tcp(int id, const char* addr, int port, int keepaliv
465
465
}
466
466
continue ;
467
467
}
468
- _sinfo [id].open = true ;
469
- _sinfo [id].proto = NSAPI_TCP;
468
+ _sock_i [id].open = true ;
469
+ _sock_i [id].proto = NSAPI_TCP;
470
470
break ;
471
471
}
472
472
}
@@ -509,7 +509,7 @@ nsapi_error_t ESP8266::send(int id, const void *data, uint32_t amount)
509
509
return NSAPI_ERROR_DEVICE_ERROR;
510
510
}
511
511
512
- void ESP8266::_packet_handler ()
512
+ void ESP8266::_oob_packet_hdlr ()
513
513
{
514
514
int id;
515
515
int amount;
@@ -521,8 +521,8 @@ void ESP8266::_packet_handler()
521
521
}
522
522
// In passive mode amount not used...
523
523
if (_tcp_passive
524
- && _sinfo [id].open == true
525
- && _sinfo [id].proto == NSAPI_TCP) {
524
+ && _sock_i [id].open == true
525
+ && _sock_i [id].proto == NSAPI_TCP) {
526
526
if (!_parser.recv (" %d\n " , &amount)) {
527
527
MBED_ERROR (MBED_MAKE_ERROR (MBED_MODULE_DRIVER, MBED_ERROR_CODE_ENODATA), \
528
528
" ESP8266::_packet_handler(): Data length missing" );
@@ -565,7 +565,7 @@ void ESP8266::_packet_handler()
565
565
_packets_end = &packet->next ;
566
566
}
567
567
568
- void ESP8266::process_oob (uint32_t timeout, bool all) {
568
+ void ESP8266::_process_oob (uint32_t timeout, bool all) {
569
569
set_timeout (timeout);
570
570
// Poll for inbound packets
571
571
while (_parser.process_oob () && all) {
@@ -592,7 +592,7 @@ int32_t ESP8266::_recv_tcp_passive(int id, void *data, uint32_t amount, uint32_t
592
592
}
593
593
594
594
// Socket closed, doesn't mean there couldn't be data left
595
- if (!_sinfo [id].open ) {
595
+ if (!_sock_i [id].open ) {
596
596
done = _parser.send (" AT+CIPRECVDATA=%d,%lu" , id, amount)
597
597
&& _parser.recv (" +CIPRECVDATA,%ld:" , &len)
598
598
&& _parser.read ((char *)data, len)
@@ -615,7 +615,7 @@ int32_t ESP8266::recv_tcp(int id, void *data, uint32_t amount, uint32_t timeout)
615
615
616
616
// No flow control, drain the USART receive register ASAP to avoid data overrun
617
617
if (_serial_rts == NC) {
618
- process_oob (timeout, true );
618
+ _process_oob (timeout, true );
619
619
}
620
620
621
621
// check if any packets are ready for us
@@ -649,14 +649,14 @@ int32_t ESP8266::recv_tcp(int id, void *data, uint32_t amount, uint32_t timeout)
649
649
}
650
650
}
651
651
}
652
- if (!_sinfo [id].open ) {
652
+ if (!_sock_i [id].open ) {
653
653
_smutex.unlock ();
654
654
return 0 ;
655
655
}
656
656
657
657
// Flow control, read from USART receive register only when no more data is buffered, and as little as possible
658
658
if (_serial_rts != NC) {
659
- process_oob (timeout, false );
659
+ _process_oob (timeout, false );
660
660
}
661
661
_smutex.unlock ();
662
662
@@ -670,7 +670,7 @@ int32_t ESP8266::recv_udp(int id, void *data, uint32_t amount, uint32_t timeout)
670
670
671
671
// No flow control, drain the USART receive register ASAP to avoid data overrun
672
672
if (_serial_rts == NC) {
673
- process_oob (timeout, true );
673
+ _process_oob (timeout, true );
674
674
}
675
675
676
676
set_timeout ();
@@ -699,7 +699,7 @@ int32_t ESP8266::recv_udp(int id, void *data, uint32_t amount, uint32_t timeout)
699
699
700
700
// Flow control, read from USART receive register only when no more data is buffered, and as little as possible
701
701
if (_serial_rts != NC) {
702
- process_oob (timeout, false );
702
+ _process_oob (timeout, false );
703
703
}
704
704
705
705
_smutex.unlock ();
@@ -738,15 +738,15 @@ bool ESP8266::close(int id)
738
738
if (!_parser.recv (" OK\n " )) {
739
739
if (_closed) { // UNLINK ERROR
740
740
_closed = false ;
741
- _sinfo [id].open = false ;
741
+ _sock_i [id].open = false ;
742
742
_clear_socket_packets (id);
743
743
_smutex.unlock ();
744
744
// ESP8266 has a habit that it might close a socket on its own.
745
745
// debug("ESP8266: socket %d already closed when calling close\n", id);
746
746
return true ;
747
747
}
748
748
} else {
749
- _socket_open [id].id = - 1 ;
749
+ _sock_i [id].open = false ;
750
750
_clear_socket_packets (id);
751
751
_smutex.unlock ();
752
752
return true ;
@@ -783,7 +783,7 @@ void ESP8266::attach(mbed::Callback<void(nsapi_event_t, intptr_t)> status_cb)
783
783
_connection_status_cb = status_cb;
784
784
}
785
785
786
- bool ESP8266::recv_ap (nsapi_wifi_ap_t *ap)
786
+ bool ESP8266::_recv_ap (nsapi_wifi_ap_t *ap)
787
787
{
788
788
int sec;
789
789
int dummy;
@@ -801,7 +801,7 @@ bool ESP8266::recv_ap(nsapi_wifi_ap_t *ap)
801
801
return ret;
802
802
}
803
803
804
- void ESP8266::_connect_error_handler ()
804
+ void ESP8266::_oob_connect_err ()
805
805
{
806
806
_fail = false ;
807
807
_connect_error = 0 ;
@@ -813,46 +813,46 @@ void ESP8266::_connect_error_handler()
813
813
}
814
814
815
815
816
- void ESP8266::_oob_cipstart_already_connected ()
816
+ void ESP8266::_oob_cipstart_already ()
817
817
{
818
818
_sock_already = true ;
819
819
_parser.abort ();
820
820
}
821
821
822
- void ESP8266::_oob_socket_close_error ()
822
+ void ESP8266::_oob_socket_close_err ()
823
823
{
824
824
if (_parser.recv (" ERROR\n " )) {
825
825
_closed = true ; // Not possible to pinpoint to a certain socket
826
826
_parser.abort ();
827
827
}
828
828
}
829
829
830
- void ESP8266::_oob_socket0_closed_handler ()
830
+ void ESP8266::_oob_socket0_closed ()
831
831
{
832
- _sinfo [0 ].open = false ;
832
+ _sock_i [0 ].open = false ;
833
833
}
834
834
835
- void ESP8266::_oob_socket1_closed_handler ()
835
+ void ESP8266::_oob_socket1_closed ()
836
836
{
837
- _sinfo [1 ].open = false ;
837
+ _sock_i [1 ].open = false ;
838
838
}
839
839
840
- void ESP8266::_oob_socket2_closed_handler ()
840
+ void ESP8266::_oob_socket2_closed ()
841
841
{
842
- _sinfo [2 ].open = false ;
842
+ _sock_i [2 ].open = false ;
843
843
}
844
844
845
- void ESP8266::_oob_socket3_closed_handler ()
845
+ void ESP8266::_oob_socket3_closed ()
846
846
{
847
- _sinfo [3 ].open = false ;
847
+ _sock_i [3 ].open = false ;
848
848
}
849
849
850
- void ESP8266::_oob_socket4_closed_handler ()
850
+ void ESP8266::_oob_socket4_closed ()
851
851
{
852
- _sinfo [4 ].open = false ;
852
+ _sock_i [4 ].open = false ;
853
853
}
854
854
855
- void ESP8266::_connection_status_handler ()
855
+ void ESP8266::_oob_connection_status ()
856
856
{
857
857
char status[13 ];
858
858
if (_parser.recv (" %12[^\" ]\n " , status)) {
0 commit comments