18 #ifndef ESP8266_INTERFACE_H 19 #define ESP8266_INTERFACE_H 21 #if DEVICE_SERIAL && DEVICE_INTERRUPTIN && defined(MBED_CONF_EVENTS_PRESENT) && defined(MBED_CONF_NSAPI_PRESENT) && defined(MBED_CONF_RTOS_API_PRESENT) 22 #include "drivers/DigitalOut.h" 23 #include "drivers/Timer.h" 24 #include "ESP8266/ESP8266.h" 25 #include "events/EventQueue.h" 26 #include "events/mbed_shared_queues.h" 29 #include "netsocket/nsapi_types.h" 31 #include "netsocket/WiFiAccessPoint.h" 32 #include "netsocket/WiFiInterface.h" 33 #include "platform/Callback.h" 34 #include "platform/mbed_chrono.h" 35 #if MBED_CONF_RTOS_PRESENT 36 #include "rtos/ConditionVariable.h" 38 #include "rtos/Mutex.h" 40 #define ESP8266_SOCKET_COUNT 5 42 #define ESP8266_INTERFACE_CONNECT_INTERVAL 5s 43 #define ESP8266_INTERFACE_CONNECT_TIMEOUT (2 * ESP8266_CONNECT_TIMEOUT + ESP8266_INTERFACE_CONNECT_INTERVAL) 45 #ifdef TARGET_FF_ARDUINO 46 #ifndef MBED_CONF_ESP8266_TX 47 #define MBED_CONF_ESP8266_TX D1 50 #ifndef MBED_CONF_ESP8266_RX 51 #define MBED_CONF_ESP8266_RX D0 55 #ifndef MBED_CONF_ESP8266_COUNTRY_CODE 56 #define MBED_CONF_ESP8266_COUNTRY_CODE "CN" 59 #ifndef MBED_CONF_ESP8266_CHANNEL_START 60 #define MBED_CONF_ESP8266_CHANNEL_START 1 63 #ifndef MBED_CONF_ESP8266_CHANNELS 64 #define MBED_CONF_ESP8266_CHANNELS 13 72 #if defined MBED_CONF_ESP8266_TX && defined MBED_CONF_ESP8266_RX 85 ESP8266Interface(PinName tx, PinName rx,
bool debug =
false, PinName rts = NC, PinName cts = NC, PinName rst = NC, PinName pwr = NC);
90 virtual ~ESP8266Interface();
115 uint8_t channel = 0);
165 virtual const
char *get_gateway();
175 virtual const
char *get_netmask();
191 virtual
char *get_interface_name(
char *interface_name);
197 virtual int8_t get_rssi();
229 mbed::chrono::milliseconds_u32 t_max = mbed::chrono::milliseconds_u32(0),
230 mbed::chrono::milliseconds_u32 t_min = mbed::chrono::milliseconds_u32(0));
246 #if MBED_CONF_ESP8266_BUILT_IN_DNS 260 #if MBED_CONF_ESP8266_BUILT_IN_DNS 269 int optname,
const void *optval,
unsigned optlen);
274 void *optval,
unsigned *optlen);
302 virtual int socket_open(
void **handle, nsapi_protocol_t proto);
351 virtual int socket_send(
void *handle,
const void *data,
unsigned size);
361 virtual int socket_recv(
void *handle,
void *data,
unsigned size);
421 nsapi_error_t set_country_code(
bool track_ap,
const char *country_code,
int len,
int channel_start,
int channels);
426 void refresh_conn_state_cb();
430 typedef enum esp_connection_software_status {
431 IFACE_STATUS_DISCONNECTED = 0,
432 IFACE_STATUS_CONNECTING = 1,
433 IFACE_STATUS_CONNECTED = 2,
434 IFACE_STATUS_DISCONNECTING = 3
435 } esp_connection_software_status_t;
440 ResetPin(PinName rst_pin);
451 PowerPin(PinName pwr_pin);
466 static const int ESP8266_SSID_MAX_LENGTH = 32;
467 char ap_ssid[ESP8266_SSID_MAX_LENGTH + 1];
468 static const int ESP8266_PASSPHRASE_MAX_LENGTH = 63;
469 static const int ESP8266_PASSPHRASE_MIN_LENGTH = 8;
470 char ap_pass[ESP8266_PASSPHRASE_MAX_LENGTH + 1];
474 struct _channel_info {
476 char country_code[4];
480 struct _channel_info _ch_info;
483 #if MBED_CONF_RTOS_PRESENT 489 mbed::Timer _conn_timer;
496 struct _sock_info _sock_i[ESP8266_SOCKET_COUNT];
502 bool _get_firmware_ok();
511 } _cbs[ESP8266_SOCKET_COUNT];
513 void event_deferred();
516 nsapi_connection_status_t _conn_stat;
523 int _connect_event_id;
524 int _disconnect_event_id;
525 void proc_oob_evnt();
526 void _connect_async();
527 void _disconnect_async();
529 esp_connection_software_status_t _software_conn_stat;
virtual nsapi_size_or_error_t socket_send(nsapi_socket_t handle, const void *data, nsapi_size_t size)=0
Send data over a TCP socket.
Common interface between Wi-Fi devices.
virtual void attach(mbed::Callback< void(nsapi_event_t, intptr_t)> status_cb)
Register callback for status reporting.
virtual nsapi_error_t socket_connect(nsapi_socket_t handle, const SocketAddress &address)=0
Connects TCP socket to a remote host.
virtual nsapi_error_t socket_listen(nsapi_socket_t handle, int backlog)=0
Listen for connections on a TCP socket.
Network Interface base class.
void * nsapi_socket_t
Opaque handle for network sockets.
virtual nsapi_error_t set_channel(uint8_t channel)=0
Set the Wi-Fi network channel.
virtual nsapi_error_t gethostbyname_async_cancel(int id)
Cancel asynchronous hostname translation.
virtual nsapi_error_t get_ip_address(SocketAddress *address)
Get the local IP address.
signed int nsapi_error_t
Type used to represent error codes.
virtual nsapi_error_t set_credentials(const char *ssid, const char *pass, nsapi_security_t security=NSAPI_SECURITY_NONE)=0
Set the Wi-Fi network credentials.
virtual nsapi_size_or_error_t socket_recv(nsapi_socket_t handle, void *data, nsapi_size_t size)=0
Receive data over a TCP socket.
virtual const char * get_mac_address()
Get the local MAC address.
virtual nsapi_error_t socket_close(nsapi_socket_t handle)=0
Close the socket.
virtual nsapi_error_t socket_bind(nsapi_socket_t handle, const SocketAddress &address)=0
Bind a specific address to a socket.
virtual nsapi_size_or_error_t socket_recvfrom(nsapi_socket_t handle, SocketAddress *address, void *buffer, nsapi_size_t size)=0
Receive a packet over a UDP socket.
virtual nsapi_size_or_error_t socket_sendto(nsapi_socket_t handle, const SocketAddress &address, const void *data, nsapi_size_t size)=0
Send a packet over a UDP socket.
virtual nsapi_error_t socket_open(nsapi_socket_t *handle, nsapi_protocol_t proto)=0
Opens a socket.
nsapi_error_t disconnect() override=0
Stop the interface.
virtual nsapi_connection_status_t get_connection_status() const
Get the connection status.
virtual nsapi_error_t setsockopt(nsapi_socket_t handle, int level, int optname, const void *optval, unsigned optlen)
Set stack-specific socket options.
The Mutex class is used to synchronize the execution of threads.
virtual nsapi_error_t set_dhcp(bool dhcp)
Enable or disable DHCP on connecting the network.
virtual nsapi_error_t add_dns_server(const SocketAddress &address, const char *interface_name=NULL)
Add a domain name server to list of servers to query.
virtual void socket_attach(nsapi_socket_t handle, void(*callback)(void *), void *data)=0
Register a callback on state change of the socket.
virtual nsapi_error_t gethostbyname(const char *host, SocketAddress *address, nsapi_version_t version=NSAPI_UNSPEC, const char *interface_name=NULL)
Translates a hostname to an IP address with specific version.
virtual nsapi_error_t get_gateway(SocketAddress *address)
Get the local gateway.
virtual nsapi_size_or_error_t scan(WiFiAccessPoint *res, nsapi_size_t count)=0
Scan for available networks.
The ConditionVariable class is a synchronization primitive that allows threads to wait until a partic...
virtual nsapi_error_t add_dns_server(const SocketAddress &address, const char *interface_name)
Add a domain name server to list of servers to query.
A digital output, used for setting the state of a pin.
virtual nsapi_error_t gethostbyname(const char *host, SocketAddress *address, nsapi_version_t version=NSAPI_UNSPEC, const char *interface_name=NULL)
Translate a hostname to an IP address with specific version using network interface name...
virtual nsapi_value_or_error_t gethostbyname_async(const char *host, hostbyname_cb_t callback, nsapi_version_t version=NSAPI_UNSPEC, const char *interface_name=NULL)
Translate a hostname to an IP address (asynchronous) using network interface name.
virtual nsapi_error_t set_network(const SocketAddress &ip_address, const SocketAddress &netmask, const SocketAddress &gateway)
Configure this network interface to use a static IP address.
virtual nsapi_error_t set_blocking(bool blocking)
Set asynchronous operation of connect() and disconnect() calls.
enum nsapi_security nsapi_security_t
Enum of encryption types.
virtual nsapi_error_t getsockopt(nsapi_socket_t handle, int level, int optname, void *optval, unsigned *optlen)
Get stack-specific socket options.
virtual nsapi_error_t socket_accept(nsapi_socket_t server, nsapi_socket_t *handle, SocketAddress *address=0)=0
Accepts a connection on a TCP socket.
Callback class based on template specialization.
nsapi_error_t connect() override=0
Attempt to connect to a Wi-Fi network.