Open Model Railroad Network (OpenMRN)
Loading...
Searching...
No Matches
ESPWifiClient Class Reference

Uses the ESPConn API on the ESP8266 wifi-enabled MCU to connect to a wifi base station, perform a DNS lookup to a given target, and connect to a given port via TCP. More...

#include <ESPWifiClient.hxx>

Inheritance diagram for ESPWifiClient:
Singleton< ESPWifiClient > StateFlow< MessageType, QueueType >

Classes

class  BufferTimer
 Timer that triggers the parent flow when expiring. More...
 

Public Member Functions

 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.
 
void do_dns_lookup ()
 Initiates the DNS lookup of the target host.
 
void do_connect (ip_addr_t *ipaddr)
 Connects to the specific IP address.
 
void tcp_connected ()
 Callback when the TCP connection is established.
 
void tcp_disconnected ()
 Callback when the TCP connection is lost.
 
void data_received (char *pdata, unsigned short len)
 Callback when incoming data is received.
 

Static Public Member Functions

static void static_wifi_callback (System_Event_t *evt)
 Callback when an event happens on the wifi port.
 
static void dns_done (const char *name, ip_addr_t *ipaddr, void *arg)
 Callback when the DNS lookup is completed.
 
static void static_tcp_connected (void *arg)
 Callback when the TCP connection is established.
 
static void static_tcp_disconnected (void *arg)
 Callback when the TCP connection is lost.
 
static void static_data_received (void *arg, char *pdata, unsigned short len)
 Callback for incoming data.
 
static void static_data_sent (void *arg)
 Callback when data that was requested to be sent has completed sending.
 
- Static Public Member Functions inherited from Singleton< ESPWifiClient >
static ESPWifiClientinstance ()
 
static bool exists ()
 

Private Member Functions

Action entry () override
 Sending base state.
 
void timeout ()
 Called from the timer to signal sending off the buffer's contents.
 
Action send_done ()
 Callback state when we are sending directly off of the input buffer becuase the data payload is too much to fit into the send assembly buffer.
 
void send_buffer ()
 Writes all bytes that are in the send buffer to the TCP socket.
 
void data_sent ()
 Callback from the TCP stack when the data send has been completed.
 
- Private Member Functions inherited from StateFlow< MessageType, QueueType >
 StateFlow (Service *service)
 Constructor.
 
- Private Member Functions inherited from TypedStateFlow< MessageType, UntypedStateFlow< QueueType > >
 TypedStateFlow (Service *service)
 Constructor.
 
virtual ~TypedStateFlow ()
 Destructor.
 
void send (MessageType *msg, unsigned priority=UINT_MAX) OVERRIDE
 Sends a message to the state flow for processing.
 
void release () OVERRIDE
 Unrefs the current buffer.
 
void return_buffer ()
 For state flows that are operated using invoke_subflow_and_wait this is a way to hand back the buffer to the caller.
 
MessageType * message ()
 
MessageType * transfer_message ()
 Releases ownership of the current message.
 
- Private Member Functions inherited from UntypedStateFlow< QueueType >
 UntypedStateFlow (Service *service)
 Constructor.
 
void send (BufferBase *msg, unsigned priority=UINT_MAX)
 Sends a message to the state flow for processing.
 
QMemberqueue_next (unsigned *priority) OVERRIDE
 Takes the front entry in the queue.
 
bool queue_empty () OVERRIDE
 
Action call_immediately (Callback c)
 Imediately call the next state upon return.
 
- Private Member Functions inherited from StateFlowWithQueue
void notify () override
 Wakeup call arrived. Schedules *this on the executor.
 
bool is_waiting ()
 
 StateFlowWithQueue (Service *service)
 Constructor.
 
Action exit ()
 Terminates the processing of this flow.
 
Action release_and_exit ()
 Terminates the processing of the current message.
 
BufferBasemessage ()
 
BufferBasetransfer_message ()
 Releases ownership of the current message.
 
void reset_message (BufferBase *message, unsigned priority)
 Sets the current message being processed.
 
unsigned priority ()
 
void set_priority (unsigned priority)
 Overrides the current priority.
 
void start_flow_at_init (Callback c)
 Call this from the constructor of the child class to do some work before the main queue processing loop begins.
 
- Private Member Functions inherited from StateFlowBase
void run () override
 Callback from the executor.
 
Serviceservice ()
 Return a pointer to the service I am bound to.
 
 StateFlowBase (Service *service)
 Constructor.
 
 ~StateFlowBase ()
 Destructor.
 
void reset_flow (Callback c)
 Resets the flow to the specified state.
 
bool is_state (Callback c)
 
bool is_terminated ()
 
void start_flow (Callback c)
 Resets the flow to the specified state and starts it.
 
Action again ()
 Call the current state again via call_immediately.
 
Action exit ()
 Terminate current StateFlow activity.
 
Action delete_this ()
 Terminates the flow and deletes *this.
 
Action set_terminated ()
 Sets the flow to terminated state.
 
Action call_immediately (Callback c)
 Imediately call the next state upon return.
 
Action wait ()
 Wait for an asynchronous call.
 
Action wait_and_call (Callback c)
 Wait for resource to become available before proceeding to next state.
 
template<class T >
Action allocate_and_call (FlowInterface< Buffer< T > > *target_flow, Callback c, Pool *pool=nullptr)
 Allocates a buffer from a pool and proceed to the next state when allocation is successful.
 
Action allocate_and_call (Callback c, QAsync *queue)
 Allocates an entry from an asynchronous queue, and transitions to a state once the allocation is complete.
 
template<class T >
Buffer< T > * full_allocation_result (FlowInterface< Buffer< T > > *target_flow)
 Takes the result of the asynchronous allocation without resetting the object.
 
template<class T >
T * full_allocation_result (TypedQAsync< T > *queue)
 Takes the result of the asynchronous allocation without resetting the object.
 
template<class T >
void cast_allocation_result (T **member)
 Takes the result of the asynchronous allocation without resetting the object.
 
template<class T >
Buffer< T > * get_allocation_result (FlowInterface< Buffer< T > > *target_flow)
 Takes the result of the asynchronous allocation.
 
Action yield_and_call (Callback c)
 Place the current flow to the back of the executor, and transition to a new state after we get the CPU again.
 
Action yield ()
 Place the current flow to the back of the executor, and re-try the current state after we get the CPU again.
 
Action sleep_and_call (::Timer *timer, long long timeout_nsec, Callback c)
 Suspends execution of this control flow for a specified time.
 
template<class T , typename... Args>
Action invoke_subflow_and_wait (FlowInterface< Buffer< T > > *target_flow, Callback c, Args &&... args)
 Calls a helper flow to perform some actions.
 
Action read_repeated (StateFlowSelectHelper *helper, int fd, void *buf, size_t size, Callback c, unsigned priority=Selectable::MAX_PRIO)
 Blocks until size bytes are read and then invokes the next state.
 
Action read_single (StateFlowSelectHelper *helper, int fd, void *buf, size_t size, Callback c, unsigned priority=Selectable::MAX_PRIO)
 Attempts to read at most size_t bytes, and blocks the caller until at least one byte is read.
 
Action read_nonblocking (StateFlowSelectHelper *helper, int fd, void *buf, size_t size, Callback c, unsigned priority=Selectable::MAX_PRIO)
 Attempts to read at most size bytes, and then invokes the next state, even if only zero bytes are available right now.
 
Action read_repeated_with_timeout (StateFlowTimedSelectHelper *helper, long long timeout_nsec, int fd, void *buf, size_t size, Callback c, unsigned priority=Selectable::MAX_PRIO)
 Blocks until size bytes are read, or a timeout expires.
 
Action internal_try_read ()
 Implementation state that gets repeatedly called upon every wakeup and tries to make progress on reading.
 
Action write_repeated (StateFlowSelectHelper *helper, int fd, const void *buf, size_t size, Callback c, unsigned priority=Selectable::MAX_PRIO)
 Writes some data into a file descriptor, repeating the operation as necessary until all bytes are written.
 
Action internal_try_write ()
 Implementation state that gets repeatedly called upon every wakeup and tries to make progress on writing.
 
- Private Member Functions inherited from Executable
void test_deletion ()
 
- Private Member Functions inherited from QMember
void init ()
 Initiailize a QMember, in place of a public placement construction.
 
 QMember ()
 Constructor.
 
 ~QMember ()
 Destructor.
 
- Private Member Functions inherited from LinkedObject< StateFlowWithQueue >
StateFlowWithQueuelink_next ()
 
StateFlowWithQueuelink_this ()
 
 LinkedObject ()
 Constructor. Puts *this on the linked list.
 
 ~LinkedObject ()
 Constructor. Removes *this from the linked list.
 
- Private Member Functions inherited from FlowInterface< MessageType >
virtual Poolpool ()
 
virtual MessageType * type_helper ()
 This function is never user in the code, but GDB can use it to infer the correct message types.
 
MessageType * alloc ()
 Synchronously allocates a message buffer from the pool of this flow.
 
void alloc_async (Executable *target)
 Asynchronously allocates a message buffer from the pool of this flow.
 

Private Attributes

ESPWifiClient::BufferTimer this
 Instance of the timer we own.
 
struct station_config stationConfig_
 Configuration of the access pint we are connecting to.
 
struct espconn conn_
 IP (including DNS) connection handle.
 
esp_tcp tcp_
 TCP connection handle.
 
ip_addr_t targetIp_
 IP address of the target host.
 
string host_
 Target host we are trying to connect to.
 
int port_
 Port numer we are connecting to on the target host.
 
uint16_t sendPending_: 1
 True when the TCP stack is busy.
 
uint16_t sendBlocked_: 1
 True when we are waiting for a notification from the TCP stack send done callback.
 
uint16_t timerPending_: 1
 True when there is a send timer running with the assembly buffer being not full.
 
uint16_t bufEnd_: 16
 Offset in sendBuf_ of the first unused byte.
 
uint8_t * sendBuf_
 Temporarily stores outgoing data until the TCP stack becomes free.
 
unsigned bufSize_
 How many bytes are there in the send buffer.
 
CanHubFlowhub_
 CAN hub to send incoming packets to and to receive outgoing packets from.
 
HubFlow gcHub_
 String-typed hub for the gridconnect-rendered packets.
 
std::unique_ptr< GCAdapterBasegcAdapter_
 Transcoder bridge from CAN to GridConnect protocol.
 
std::function< void()> connectCallback_
 Application level callback function to call when the connection has been successfully established.
 
- Private Attributes inherited from QMember
QMembernext
 pointer to the next member in the queue
 
- Private Attributes inherited from LinkedObject< StateFlowWithQueue >
StateFlowWithQueuelink_
 Linked list pointer.
 

Additional Inherited Members

- Private Types inherited from TypedStateFlow< MessageType, UntypedStateFlow< QueueType > >
typedef Base::Action Action
 Allows using Action without having StateFlowBase:: prefix in front of it.
 
- Private Types inherited from UntypedStateFlow< QueueType >
typedef Action(StateFlowBase::* Callback) ()
 State Flow callback prototype.
 
- Private Types inherited from StateFlowBase
typedef Action(StateFlowBase::* Callback) ()
 State Flow callback prototype.
 
- Private Types inherited from FlowInterface< MessageType >
typedef MessageType message_type
 Stores the message template type for external reference.
 
- Static Private Member Functions inherited from StateFlowBase
template<class T , typename... Args>
static void invoke_subflow_and_ignore_result (FlowInterface< Buffer< T > > *target_flow, Args &&... args)
 Calls a helper flow to perform some actions.
 
- Static Private Member Functions inherited from LinkedObject< StateFlowWithQueue >
static StateFlowWithQueuelink_head ()
 
static Atomichead_mu ()
 Locks the list for modification (at any entry!).
 
- Static Private Member Functions inherited from FlowInterface< MessageType >
static MessageType * cast_alloc (QMember *entry)
 Down casts and initializes an asynchronous allocation result to the appropriate flow's buffer type.
 
- Static Private Attributes inherited from LinkedObject< StateFlowWithQueue >
static StateFlowWithQueuehead_
 Beginning of the list.
 

Detailed Description

Uses the ESPConn API on the ESP8266 wifi-enabled MCU to connect to a wifi base station, perform a DNS lookup to a given target, and connect to a given port via TCP.

Acts as a HubPort, meaning the data coming from the Hub (usually gridconnect packets) are sent to the TCP connection.

Definition at line 55 of file ESPWifiClient.hxx.

Constructor & Destructor Documentation

◆ ESPWifiClient()

ESPWifiClient::ESPWifiClient ( const string &  ssid,
const string &  password,
CanHubFlow hub,
const string &  hostname,
int  port,
unsigned  send_buf_size,
std::function< void()>  connect_callback 
)
inline

Creates a wifi+TCP client connection via the ESPConn API.

Parameters
ssidwifi access point name
passwordpassphrase for the wifi access point, or empty string for open (unencrypted) connection.
hubCAN hub to connect to the server
hostnamehostname of the gridconnect TCP hub server. IP address in dot format is not supported.
portport number of the TCP hub server.
send_buf_sizein bytes, how much to buffer befone handing over to the TCP stack.
connect_callbackwill be called after the wifi and the TCP connection is established. Usually used for starting the OpenLCB stack.

Definition at line 71 of file ESPWifiClient.hxx.

Member Function Documentation

◆ data_received()

void ESPWifiClient::data_received ( char *  pdata,
unsigned short  len 
)
inline

Callback when incoming data is received.

Parameters
pdataincoming data
lennumber of bytes received.

Definition at line 240 of file ESPWifiClient.hxx.

◆ data_sent()

void ESPWifiClient::data_sent ( )
inlineprivate

Callback from the TCP stack when the data send has been completed.

Definition at line 326 of file ESPWifiClient.hxx.

◆ dns_done()

static void ESPWifiClient::dns_done ( const char *  name,
ip_addr_t *  ipaddr,
void *  arg 
)
inlinestatic

Callback when the DNS lookup is completed.

Parameters
namewhat we were looking up (ignored)
ipaddrip address of the host
argignored.

Definition at line 155 of file ESPWifiClient.hxx.

◆ do_connect()

void ESPWifiClient::do_connect ( ip_addr_t *  ipaddr)
inline

Connects to the specific IP address.

Parameters
ipaddris the address of the host we wanted to connect to.

Definition at line 168 of file ESPWifiClient.hxx.

◆ do_dns_lookup()

void ESPWifiClient::do_dns_lookup ( )
inline

Initiates the DNS lookup of the target host.

Definition at line 144 of file ESPWifiClient.hxx.

◆ entry()

Action ESPWifiClient::entry ( )
inlineoverrideprivatevirtual

Sending base state.

Returns
next action.

Implements TypedStateFlow< MessageType, UntypedStateFlow< QueueType > >.

Definition at line 250 of file ESPWifiClient.hxx.

◆ send_buffer()

void ESPWifiClient::send_buffer ( )
inlineprivate

Writes all bytes that are in the send buffer to the TCP socket.

Definition at line 319 of file ESPWifiClient.hxx.

◆ send_done()

Action ESPWifiClient::send_done ( )
inlineprivate

Callback state when we are sending directly off of the input buffer becuase the data payload is too much to fit into the send assembly buffer.

Called when the send is completed and the input buffer can be releases.

Returns
next action

Definition at line 313 of file ESPWifiClient.hxx.

◆ static_data_received()

static void ESPWifiClient::static_data_received ( void *  arg,
char *  pdata,
unsigned short  len 
)
inlinestatic

Callback for incoming data.

Parameters
argignored
pdatapointer to data received.
lennumber of bytes received.

Definition at line 223 of file ESPWifiClient.hxx.

◆ static_data_sent()

static void ESPWifiClient::static_data_sent ( void *  arg)
inlinestatic

Callback when data that was requested to be sent has completed sending.

Parameters
argignored.

Definition at line 230 of file ESPWifiClient.hxx.

◆ static_tcp_connected()

static void ESPWifiClient::static_tcp_connected ( void *  arg)
inlinestatic

Callback when the TCP connection is established.

Parameters
argignored.

Definition at line 186 of file ESPWifiClient.hxx.

◆ static_tcp_disconnected()

static void ESPWifiClient::static_tcp_disconnected ( void *  arg)
inlinestatic

Callback when the TCP connection is lost.

Parameters
argignored.

Definition at line 204 of file ESPWifiClient.hxx.

◆ static_wifi_callback()

static void ESPWifiClient::static_wifi_callback ( System_Event_t *  evt)
inlinestatic

Callback when an event happens on the wifi port.

Parameters
evtdescribes event that happened.

Definition at line 98 of file ESPWifiClient.hxx.

◆ tcp_connected()

void ESPWifiClient::tcp_connected ( )
inline

Callback when the TCP connection is established.

Definition at line 193 of file ESPWifiClient.hxx.

◆ tcp_disconnected()

void ESPWifiClient::tcp_disconnected ( )
inline

Callback when the TCP connection is lost.

Definition at line 211 of file ESPWifiClient.hxx.

◆ timeout()

void ESPWifiClient::timeout ( )
inlineprivate

Called from the timer to signal sending off the buffer's contents.

Definition at line 301 of file ESPWifiClient.hxx.

Member Data Documentation

◆ bufEnd_

uint16_t ESPWifiClient::bufEnd_
private

Offset in sendBuf_ of the first unused byte.

Definition at line 382 of file ESPWifiClient.hxx.

◆ bufSize_

unsigned ESPWifiClient::bufSize_
private

How many bytes are there in the send buffer.

Definition at line 386 of file ESPWifiClient.hxx.

◆ conn_

struct espconn ESPWifiClient::conn_
private

IP (including DNS) connection handle.

Definition at line 362 of file ESPWifiClient.hxx.

◆ connectCallback_

std::function<void()> ESPWifiClient::connectCallback_
private

Application level callback function to call when the connection has been successfully established.

Definition at line 396 of file ESPWifiClient.hxx.

◆ gcAdapter_

std::unique_ptr<GCAdapterBase> ESPWifiClient::gcAdapter_
private

Transcoder bridge from CAN to GridConnect protocol.

Definition at line 393 of file ESPWifiClient.hxx.

◆ gcHub_

HubFlow ESPWifiClient::gcHub_
private

String-typed hub for the gridconnect-rendered packets.

Definition at line 391 of file ESPWifiClient.hxx.

◆ host_

string ESPWifiClient::host_
private

Target host we are trying to connect to.

Definition at line 369 of file ESPWifiClient.hxx.

◆ hub_

CanHubFlow* ESPWifiClient::hub_
private

CAN hub to send incoming packets to and to receive outgoing packets from.

Definition at line 389 of file ESPWifiClient.hxx.

◆ port_

int ESPWifiClient::port_
private

Port numer we are connecting to on the target host.

Definition at line 371 of file ESPWifiClient.hxx.

◆ sendBlocked_

uint16_t ESPWifiClient::sendBlocked_
private

True when we are waiting for a notification from the TCP stack send done callback.

Definition at line 376 of file ESPWifiClient.hxx.

◆ sendBuf_

uint8_t* ESPWifiClient::sendBuf_
private

Temporarily stores outgoing data until the TCP stack becomes free.

Definition at line 384 of file ESPWifiClient.hxx.

◆ sendPending_

uint16_t ESPWifiClient::sendPending_
private

True when the TCP stack is busy.

Definition at line 373 of file ESPWifiClient.hxx.

◆ stationConfig_

struct station_config ESPWifiClient::stationConfig_
private

Configuration of the access pint we are connecting to.

Definition at line 360 of file ESPWifiClient.hxx.

◆ targetIp_

ip_addr_t ESPWifiClient::targetIp_
private

IP address of the target host.

Definition at line 367 of file ESPWifiClient.hxx.

◆ tcp_

esp_tcp ESPWifiClient::tcp_
private

TCP connection handle.

Definition at line 364 of file ESPWifiClient.hxx.

◆ timerPending_

uint16_t ESPWifiClient::timerPending_
private

True when there is a send timer running with the assembly buffer being not full.

Definition at line 379 of file ESPWifiClient.hxx.


The documentation for this class was generated from the following file: