nal: Fix loops & socket handle management #7

Merged
harry merged 6 commits from fix-nal into master 2021-04-12 09:28:56 +08:00

6 Commits (master)

Author SHA1 Message Date
Harry Ho 232a08f110 nal: Fix comments & styling 2021-03-17 10:21:18 +08:00
Harry Ho 2eadb652ff nal: Fix infinite loop when TX buffer is full
* For example, if the PHY linkup is down, instead of looping until resumption of the linkup, a write operation now closes the socket for re-connection in the future
2021-03-16 10:25:23 +08:00
Harry Ho 6de19f43cc nal: Prevent pushing duplicate handles for the same TcpSocket 2021-03-12 12:36:30 +08:00
Harry Ho 66c3aa534f nal: socket (TcpSocket) → handle; internal_socket → socket 2021-03-12 11:26:07 +08:00
Harry Ho 99899e6657 nal: Fix read/write not pushing erroneous socket back to the stack
* Based on quartiq's minimq as of 933687c2e4
* In minimq applications, a socket is expected to be returned when `nal::TcpStack::open()` is called
  * `MqttClient::read()`/`write()` takes away the TCP socket handle (wrapped as an `Option`) from its `RefCell`, and then calls `nal::TcpStack::read()`/`write()`; if NAL returns `nb::Error`, then the MQTT client will propagate and return the error, leaving `None` behind
  * Afterwards, when `MqttClient::socket_is_connected()` gets called (e.g. while polling the interface), it will detect that the socket handle is `None`, and attempt to call `nal::TcpStack::open()`
  * Since `open()` pops a socket from the array (`unused_handles`), when implementing this NAL the socket should have been pushed back to the stack, i.e. by `close()`; this prevents any future calls of `open()` from returning `NetworkError::NoSocket` due to emptiness of the array of socket handles
2021-03-11 17:32:44 +08:00
Harry Ho d9e50bbcb6 nal: Prevent looping until the stack successfully connects to remote
* `NetworkStack::connect()`:
  * Add timeout for connection attempt
  * Now returns the socket at TCP ESTABLISHED or CLOSED states, or after connection timeout
* Split `NetworkStack::update()` into `update()` (for controlling the clock) and `poll()` (for polling the smoltcp EthernetInterface)
* Also remove option `auto_time_update`; the main application is responsible for what values `embedded_time::clock::Clock::try_now()` should return
2021-03-05 14:52:57 +08:00