36#ifndef _UTILS_ESPWIFICLIENT_HXX_
37#define _UTILS_ESPWIFICLIENT_HXX_
42#include <user_interface.h>
72 const string &hostname,
int port,
unsigned send_buf_size,
73 std::function<
void()> connect_callback)
80 ,
sendBuf_((uint8_t*)malloc(send_buf_size))
87 wifi_set_opmode_current(STATION_MODE);
89 memcpy(&
stationConfig_.password, password.c_str(), password.size() + 1);
92 wifi_station_connect();
100 os_printf(
"%s: %d\n", __FUNCTION__, evt->event);
104 case EVENT_STAMODE_CONNECTED:
106 os_printf(
"connect to ssid %s, channel %d\n",
107 evt->event_info.connected.ssid,
108 evt->event_info.connected.channel);
112 case EVENT_STAMODE_DISCONNECTED:
114 os_printf(
"disconnect from ssid %s, reason %d\n",
115 evt->event_info.disconnected.ssid,
116 evt->event_info.disconnected.reason);
118 system_deep_sleep_set_option(0);
119 system_deep_sleep(60 * 1000 * 1000);
123 case EVENT_STAMODE_GOT_IP:
155 static void dns_done(
const char *name, ip_addr_t *ipaddr,
void *arg)
159 os_printf(
"DNS lookup failed\n");
160 wifi_station_disconnect();
170 os_printf(
"Connecting to %s:%d...\n",
host_.c_str(),
port_);
172 conn_.type = ESPCONN_TCP;
173 conn_.state = ESPCONN_NONE;
175 conn_.proto.tcp->local_port = espconn_port();
177 memcpy(
conn_.proto.tcp->remote_ip, &ipaddr->addr, 4);
182 espconn_connect(&
conn_);
188 os_printf(
"%s\n", __FUNCTION__);
206 os_printf(
"%s\n", __FUNCTION__);
215 wifi_station_disconnect();
243 b->data()->skipMember_ =
this;
244 b->data()->assign(pdata, len);
270 espconn_sent(&
conn_, (uint8 *)
message()->data()->data(),
357 } bufferTimer_{
this};
#define STATE(_fn)
Turns a function name into an argument to be supplied to functions expecting a state.
Timer that triggers the parent flow when expiring.
long long timeout() override
callback when the timer expires.
BufferTimer(ESPWifiClient *parent)
Constructor.
ESPWifiClient * parent_
parent who owns *this.
Uses the ESPConn API on the ESP8266 wifi-enabled MCU to connect to a wifi base station,...
uint16_t sendBlocked_
True when we are waiting for a notification from the TCP stack send done callback.
struct espconn conn_
IP (including DNS) connection handle.
void do_dns_lookup()
Initiates the DNS lookup of the target host.
Action send_done()
Callback state when we are sending directly off of the input buffer becuase the data payload is too m...
uint16_t bufEnd_
Offset in sendBuf_ of the first unused byte.
struct station_config stationConfig_
Configuration of the access pint we are connecting to.
static void static_data_received(void *arg, char *pdata, unsigned short len)
Callback for incoming data.
static void dns_done(const char *name, ip_addr_t *ipaddr, void *arg)
Callback when the DNS lookup is completed.
ESPWifiClient(const string &ssid, const string &password, CanHubFlow *hub, const string &hostname, int port, unsigned send_buf_size, std::function< void()> connect_callback)
Creates a wifi+TCP client connection via the ESPConn API.
std::unique_ptr< GCAdapterBase > gcAdapter_
Transcoder bridge from CAN to GridConnect protocol.
uint16_t timerPending_
True when there is a send timer running with the assembly buffer being not full.
unsigned bufSize_
How many bytes are there in the send buffer.
Action entry() override
Sending base state.
void tcp_disconnected()
Callback when the TCP connection is lost.
void send_buffer()
Writes all bytes that are in the send buffer to the TCP socket.
static void static_wifi_callback(System_Event_t *evt)
Callback when an event happens on the wifi port.
int port_
Port numer we are connecting to on the target host.
void timeout()
Called from the timer to signal sending off the buffer's contents.
CanHubFlow * hub_
CAN hub to send incoming packets to and to receive outgoing packets from.
void do_connect(ip_addr_t *ipaddr)
Connects to the specific IP address.
std::function< void()> connectCallback_
Application level callback function to call when the connection has been successfully established.
HubFlow gcHub_
String-typed hub for the gridconnect-rendered packets.
esp_tcp tcp_
TCP connection handle.
void tcp_connected()
Callback when the TCP connection is established.
static void static_tcp_disconnected(void *arg)
Callback when the TCP connection is lost.
uint16_t sendPending_
True when the TCP stack is busy.
string host_
Target host we are trying to connect to.
static void static_tcp_connected(void *arg)
Callback when the TCP connection is established.
static void static_data_sent(void *arg)
Callback when data that was requested to be sent has completed sending.
ip_addr_t targetIp_
IP address of the target host.
uint8_t * sendBuf_
Temporarily stores outgoing data until the TCP stack becomes free.
void data_received(char *pdata, unsigned short len)
Callback when incoming data is received.
void data_sent()
Callback from the TCP stack when the data send has been completed.
MessageType * alloc()
Synchronously allocates a message buffer from the pool of this flow.
static GCAdapterBase * CreateGridConnectAdapter(HubFlow *gc_side, CanHubFlow *can_side, bool double_bytes)
This function connects an ASCII (GridConnect-format) CAN adapter to a binary CAN adapter,...
void register_port(port_type *port)
Adds a new port.
void unregister_port(port_type *port)
Removes a previously added port.
Return type for a state flow callback.
Service * service()
Return a pointer to the service I am bound to.
Action wait()
Wait for an asynchronous call.
Action again()
Call the current state again via call_immediately.
Action wait_and_call(Callback c)
Wait for resource to become available before proceeding to next state.
void notify() override
Wakeup call arrived. Schedules *this on the executor.
Action exit()
Terminates the processing of this flow.
Action release_and_exit()
Terminates the processing of the current message.
State flow with a given typed input queue.
A timer that can schedule itself to run on an executor at specified times in the future.
@ NONE
Do not restart the timer.
void release() OVERRIDE
Unrefs the current buffer.
void send(MessageType *msg, unsigned priority=UINT_MAX) OVERRIDE
Sends a message to the state flow for processing.
bool queue_empty() OVERRIDE
#define MSEC_TO_NSEC(_msec)
Convert a millisecond value to a nanosecond value.