|
Open Model Railroad Network (OpenMRN)
|
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>
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 ESPWifiClient * | instance () |
| 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. | |
| QMember * | queue_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. | |
| BufferBase * | message () |
| BufferBase * | transfer_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. | |
| Service * | service () |
| 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 > | |
| StateFlowWithQueue * | link_next () |
| StateFlowWithQueue * | link_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 Pool * | pool () |
| 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. | |
| CanHubFlow * | hub_ |
| 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< GCAdapterBase > | gcAdapter_ |
| 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 | |
| QMember * | next |
| pointer to the next member in the queue | |
Private Attributes inherited from LinkedObject< StateFlowWithQueue > | |
| StateFlowWithQueue * | link_ |
| 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 StateFlowWithQueue * | link_head () |
| static Atomic * | head_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 StateFlowWithQueue * | head_ |
| Beginning of the list. | |
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.
|
inline |
Creates a wifi+TCP client connection via the ESPConn API.
| ssid | wifi access point name |
| password | passphrase for the wifi access point, or empty string for open (unencrypted) connection. |
| hub | CAN hub to connect to the server |
| hostname | hostname of the gridconnect TCP hub server. IP address in dot format is not supported. |
| port | port number of the TCP hub server. |
| send_buf_size | in bytes, how much to buffer befone handing over to the TCP stack. |
| connect_callback | will 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.
|
inline |
Callback when incoming data is received.
| pdata | incoming data |
| len | number of bytes received. |
Definition at line 240 of file ESPWifiClient.hxx.
|
inlineprivate |
Callback from the TCP stack when the data send has been completed.
Definition at line 326 of file ESPWifiClient.hxx.
|
inlinestatic |
Callback when the DNS lookup is completed.
| name | what we were looking up (ignored) |
| ipaddr | ip address of the host |
| arg | ignored. |
Definition at line 155 of file ESPWifiClient.hxx.
|
inline |
Connects to the specific IP address.
| ipaddr | is the address of the host we wanted to connect to. |
Definition at line 168 of file ESPWifiClient.hxx.
|
inline |
Initiates the DNS lookup of the target host.
Definition at line 144 of file ESPWifiClient.hxx.
|
inlineoverrideprivatevirtual |
Sending base state.
Implements TypedStateFlow< MessageType, UntypedStateFlow< QueueType > >.
Definition at line 250 of file ESPWifiClient.hxx.
|
inlineprivate |
Writes all bytes that are in the send buffer to the TCP socket.
Definition at line 319 of file ESPWifiClient.hxx.
|
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.
Definition at line 313 of file ESPWifiClient.hxx.
|
inlinestatic |
Callback for incoming data.
| arg | ignored |
| pdata | pointer to data received. |
| len | number of bytes received. |
Definition at line 223 of file ESPWifiClient.hxx.
|
inlinestatic |
Callback when data that was requested to be sent has completed sending.
| arg | ignored. |
Definition at line 230 of file ESPWifiClient.hxx.
|
inlinestatic |
Callback when the TCP connection is established.
| arg | ignored. |
Definition at line 186 of file ESPWifiClient.hxx.
|
inlinestatic |
Callback when the TCP connection is lost.
| arg | ignored. |
Definition at line 204 of file ESPWifiClient.hxx.
|
inlinestatic |
Callback when an event happens on the wifi port.
| evt | describes event that happened. |
Definition at line 98 of file ESPWifiClient.hxx.
|
inline |
Callback when the TCP connection is established.
Definition at line 193 of file ESPWifiClient.hxx.
|
inline |
Callback when the TCP connection is lost.
Definition at line 211 of file ESPWifiClient.hxx.
|
inlineprivate |
Called from the timer to signal sending off the buffer's contents.
Definition at line 301 of file ESPWifiClient.hxx.
|
private |
Offset in sendBuf_ of the first unused byte.
Definition at line 382 of file ESPWifiClient.hxx.
|
private |
How many bytes are there in the send buffer.
Definition at line 386 of file ESPWifiClient.hxx.
|
private |
IP (including DNS) connection handle.
Definition at line 362 of file ESPWifiClient.hxx.
|
private |
Application level callback function to call when the connection has been successfully established.
Definition at line 396 of file ESPWifiClient.hxx.
|
private |
Transcoder bridge from CAN to GridConnect protocol.
Definition at line 393 of file ESPWifiClient.hxx.
|
private |
String-typed hub for the gridconnect-rendered packets.
Definition at line 391 of file ESPWifiClient.hxx.
|
private |
Target host we are trying to connect to.
Definition at line 369 of file ESPWifiClient.hxx.
|
private |
CAN hub to send incoming packets to and to receive outgoing packets from.
Definition at line 389 of file ESPWifiClient.hxx.
|
private |
Port numer we are connecting to on the target host.
Definition at line 371 of file ESPWifiClient.hxx.
|
private |
True when we are waiting for a notification from the TCP stack send done callback.
Definition at line 376 of file ESPWifiClient.hxx.
|
private |
Temporarily stores outgoing data until the TCP stack becomes free.
Definition at line 384 of file ESPWifiClient.hxx.
|
private |
True when the TCP stack is busy.
Definition at line 373 of file ESPWifiClient.hxx.
|
private |
Configuration of the access pint we are connecting to.
Definition at line 360 of file ESPWifiClient.hxx.
|
private |
IP address of the target host.
Definition at line 367 of file ESPWifiClient.hxx.
|
private |
TCP connection handle.
Definition at line 364 of file ESPWifiClient.hxx.
|
private |
True when there is a send timer running with the assembly buffer being not full.
Definition at line 379 of file ESPWifiClient.hxx.