|
ra8-firmware 0.1.0
Bare-metal firmware for the Renesas RA8 family (RA8D2 / RA8P1)
|
The Wi-Fi facade state machine, dispatched over a backend vtable. More...
#include "ra8_wifi.h"#include <stdint.h>#include "ra8_attributes.h"#include "ra8_check.h"#include "ra8_err.h"#include "ra8_wifi_backend.h"Go to the source code of this file.
Macros | |
| #define | RA8_WIFI_TAG "WIFI" |
| Component tag for this facade's log lines. | |
Functions | |
| static ra8_err_t | internal_check_backend_lifecycle (const ra8_wifi_backend_t *b) |
| Validate the backend rows that change the radio's state. | |
| static ra8_err_t | internal_check_backend_session (const ra8_wifi_backend_t *b) |
| Validate the backend rows that join and leave a network. | |
| static ra8_err_t | internal_check_backend_query (const ra8_wifi_backend_t *b) |
| Validate the backend rows that report state or pace a wait. | |
| static ra8_err_t | internal_check_backend (const ra8_wifi_backend_t *b) |
| Validate every row of a candidate backend table. | |
| ra8_err_t | ra8_wifi_init (ra8_wifi_t *wifi, const ra8_wifi_cfg_t *cfg) |
| Bring the radio and its link up and make a handle usable. | |
| ra8_err_t | ra8_wifi_deinit (ra8_wifi_t *wifi) |
| Release a handle and power its radio down. | |
| static ra8_err_t | internal_ensure_radio_up (ra8_wifi_t *wifi) |
| Raise the radio if it is not already up. | |
| static ra8_err_t | internal_await_association (ra8_wifi_t *wifi) |
| Pump the backend until the station associates or the budget runs out. | |
| ra8_err_t | ra8_wifi_connect (ra8_wifi_t *wifi, const char *ssid, const char *psk) |
| Join a network by SSID and passphrase, blocking until associated. | |
| ra8_err_t | ra8_wifi_disconnect (ra8_wifi_t *wifi) |
| Disassociate the station and power the radio down. | |
| ra8_err_t | ra8_wifi_wait_ip (ra8_wifi_t *wifi, ra8_wifi_lease_t *out) |
| Obtain an IP address for the associated station, blocking until bound. | |
| ra8_err_t | ra8_wifi_get_ip (const ra8_wifi_t *wifi, ra8_wifi_lease_t *out) |
| Read the cached DHCP lease without touching the wire. | |
| ra8_err_t | ra8_wifi_status (const ra8_wifi_t *wifi, ra8_wifi_status_t *out) |
| Read a handle's cached status without touching the wire. | |
| ra8_err_t | ra8_wifi_poll (ra8_wifi_t *wifi, ra8_wifi_link_t *out) |
| Service the link once and refresh the cached association state. | |
| ra8_err_t | ra8_wifi_get_mac (ra8_wifi_t *wifi, ra8_wifi_mac_t *out) |
| Read the station's own MAC address. | |
| ra8_err_t | ra8_wifi_get_ap (ra8_wifi_t *wifi, ra8_wifi_ap_t *out) |
| Read what the backend knows about the associated AP. | |
The Wi-Fi facade state machine, dispatched over a backend vtable.
Implements ra8_wifi.h. Every entry point is a short step in the lifecycle down->associating->associated->ip_bound, expressed purely in terms of the ra8_wifi_backend_t function pointers and the caller-supplied ra8_wifi_ip_bind_fn. This translation unit names no radio and includes no ra8_c6link header, which is exactly what makes it host-testable against a mock backend.
Definition in file ra8_wifi.c.
| #define RA8_WIFI_TAG "WIFI" |
Component tag for this facade's log lines.
Definition at line 33 of file ra8_wifi.c.
Referenced by internal_await_association(), internal_check_backend_lifecycle(), internal_check_backend_query(), internal_check_backend_session(), internal_ensure_radio_up(), ra8_wifi_connect(), ra8_wifi_deinit(), ra8_wifi_disconnect(), ra8_wifi_get_ap(), ra8_wifi_get_ip(), ra8_wifi_get_mac(), ra8_wifi_init(), ra8_wifi_poll(), ra8_wifi_status(), and ra8_wifi_wait_ip().
|
static |
Pump the backend until the station associates or the budget runs out.
A join request only asks; association completes asynchronously, so the transport has to be serviced until the link reports up. The poll count is a compile-time bound (k_ra8_wifi_join_polls), so this terminates whether or not the AP ever answers – NASA Power of 10 Rule 2 – and the backend idles k_ra8_wifi_poll_gap_ms between attempts, which is what makes that count a budget in seconds rather than in however fast the radio answers.
A failed attempt ends the attempt, not the wait. Servicing a link whose radio is mid-association routinely fails: the co-processor is busy on the air and has nothing to hand back, which k_ra8_wifi_backend_c6link reports as k_ra8_err_hw_timeout. Treating the first such reading as fatal is what made association a coin toss on silicon, so the loop records the error and carries on, and reports it only if no attempt in the entire budget succeeded – the one reading that does mean the radio is not there.
| [in,out] | wifi | Open handle with a join already requested. |
| k_ra8_ok | The link came up; the handle is in the associated state. |
| k_ra8_err_timeout | The budget was exhausted with the link still down. |
| k_ra8_err_null_ptr | wifi was null. |
wifi is open and its radio is up. Definition at line 282 of file ra8_wifi.c.
References internal_await_association(), k_ra8_err_timeout, k_ra8_ok, k_ra8_wifi_join_polls, k_ra8_wifi_link_down, k_ra8_wifi_link_up, k_ra8_wifi_poll_gap_ms, k_ra8_wifi_state_associated, RA8_CHECK_NULL_PTR, and RA8_WIFI_TAG.
Referenced by internal_await_association(), and ra8_wifi_connect().
|
static |
Validate every row of a candidate backend table.
The single entry point ra8_wifi_init uses. It runs the three role-sized validators in turn and stops at the first gap, so the error names the row that is missing rather than reporting only that "the table is wrong".
| [in] | b | Candidate backend table; may be null. |
| k_ra8_ok | Every function pointer is set. |
| k_ra8_err_null_ptr | b or one of its rows was null. |
b is the table an application selected by address. Definition at line 153 of file ra8_wifi.c.
References internal_check_backend_lifecycle(), internal_check_backend_query(), internal_check_backend_session(), k_ra8_ok, and RA8_INTERNAL.
Referenced by ra8_wifi_init().
|
static |
Validate the backend rows that change the radio's state.
The lifecycle half of ra8_wifi_backend_t – opening and closing the transport and raising or lowering the radio. The table is validated in three role-sized pieces rather than one function carrying ten expansions of RA8_CHECK_NULL_PTR, which is what pushed it past the complexity budget.
| [in] | b | Candidate backend table; never null (the caller checked). |
| k_ra8_ok | Every lifecycle row is set. |
| k_ra8_err_null_ptr | One of the lifecycle rows was null. |
b is non-null. Definition at line 58 of file ra8_wifi.c.
References k_ra8_ok, RA8_CHECK_NULL_PTR, RA8_INTERNAL, and RA8_WIFI_TAG.
Referenced by internal_check_backend().
|
static |
Validate the backend rows that report state or pace a wait.
The observation half of ra8_wifi_backend_t – pumping the transport, reading back the station address and the associated AP, and idling between attempts. None of these change the radio, which is why they are validated apart from the lifecycle rows.
| [in] | b | Candidate backend table; never null (the caller checked). |
| k_ra8_ok | Every query row is set. |
| k_ra8_err_null_ptr | One of the query rows was null. |
b is non-null. Definition at line 121 of file ra8_wifi.c.
References k_ra8_ok, RA8_CHECK_NULL_PTR, RA8_INTERNAL, and RA8_WIFI_TAG.
Referenced by internal_check_backend().
|
static |
Validate the backend rows that join and leave a network.
The session half of ra8_wifi_backend_t. Separate from the lifecycle rows because a radio can be up with no network attached, so these are the rows a reconnect exercises without touching the transport.
| [in] | b | Candidate backend table; never null (the caller checked). |
| k_ra8_ok | Both session rows are set. |
| k_ra8_err_null_ptr | One of the session rows was null. |
b is non-null. Definition at line 90 of file ra8_wifi.c.
References k_ra8_ok, RA8_CHECK_NULL_PTR, RA8_INTERNAL, and RA8_WIFI_TAG.
Referenced by internal_check_backend().
|
static |
Raise the radio if it is not already up.
Idempotent by design: ra8_wifi_connect may be called repeatedly (a retry after a failed join, a move to another SSID) and must not cycle a radio that is already running, because a backend is entitled to treat a second radio_up as an error.
| [in,out] | wifi | Open handle whose radio should be running on return. |
| k_ra8_ok | The radio is up, whether or not this call raised it. |
| k_ra8_err_null_ptr | wifi was null. |
wifi is open (ra8_wifi_init succeeded). Definition at line 232 of file ra8_wifi.c.
References ra8_wifi::backend, ra8_wifi::backend_ctx, k_ra8_ok, RA8_CHECK_NULL_PTR, RA8_INTERNAL, RA8_WIFI_TAG, ra8_wifi::radio_on, and ra8_wifi_backend::radio_up.
Referenced by ra8_wifi_connect().
|
nodiscard |
Join a network by SSID and passphrase, blocking until associated.
Starts the radio if it is not already on, reads and caches the station MAC, asks the backend to associate, and services the link until the station joins or the attempt budget (k_ra8_wifi_join_polls) is spent, idling k_ra8_wifi_poll_gap_ms between attempts. On success the handle is at k_ra8_wifi_state_associated and ra8_wifi_wait_ip is the next step.
| [in,out] | wifi | Open handle; must be non-null. |
| [in] | ssid | Target SSID, NUL-terminated; must be non-null and 1.. k_ra8_wifi_ssid_max octets. |
| [in] | psk | Passphrase, NUL-terminated, or null for an open network. |
| k_ra8_ok | The station is associated. |
| k_ra8_err_null_ptr | wifi or ssid was null. |
| k_ra8_err_not_initialized | wifi is not open. |
| k_ra8_err_invalid_size | ssid was empty or a credential was too long. |
| k_ra8_err_timeout | The join did not complete within the budget. |
| k_ra8_err_protocol_error | The radio refused a step of the join. |
| k_ra8_err_spi_error | The backend's transport refused every transfer of the whole budget. |
| k_ra8_err_hw_timeout | The radio answered no attempt of the whole budget. |
wifi has been initialised. ssid names a network that is in range. Definition at line 311 of file ra8_wifi.c.
References ra8_wifi::backend, ra8_wifi::backend_ctx, ra8_wifi_backend::get_mac, internal_await_association(), internal_ensure_radio_up(), ra8_wifi_backend::join, k_ra8_err_not_initialized, k_ra8_ok, k_ra8_wifi_state_associating, ra8_wifi::mac, ra8_wifi::mac_valid, ra8_wifi::open, RA8_CHECK_NULL_PTR, RA8_WIFI_TAG, and ra8_wifi::state.
Referenced by wifi_hal_join_run().
|
nodiscard |
Release a handle and power its radio down.
Asks the backend to close its link and drop the transport binding, then marks the handle closed. Safe to call from any state; a handle that never associated closes just as cleanly as one that held a lease.
| [in,out] | wifi | Open handle; must be non-null. |
| k_ra8_ok | The handle is closed. |
| k_ra8_err_null_ptr | wifi was null. |
| k_ra8_err_not_initialized | wifi was not open. |
wifi. Definition at line 195 of file ra8_wifi.c.
References ra8_wifi::backend, ra8_wifi::backend_ctx, ra8_wifi_backend::close, k_ra8_err_not_initialized, k_ra8_wifi_state_down, ra8_wifi::open, RA8_CHECK_NULL_PTR, RA8_WIFI_TAG, ra8_wifi::radio_on, and ra8_wifi::state.
|
nodiscard |
Disassociate the station and power the radio down.
Asks the backend to leave the network and stop the radio, clears the cached lease, and returns the handle to k_ra8_wifi_state_down. The handle stays open and can associate again with a fresh ra8_wifi_connect.
| [in,out] | wifi | Open handle; must be non-null. |
| k_ra8_ok | The station has left and the radio is stopped. |
| k_ra8_err_null_ptr | wifi was null. |
| k_ra8_err_not_initialized | wifi is not open. |
| k_ra8_err_protocol_error | A teardown step was refused; the rest still ran. |
| k_ra8_err_spi_error | The backend's transport refused a transfer. |
wifi has been initialised. Definition at line 338 of file ra8_wifi.c.
References ra8_wifi::backend, ra8_wifi::backend_ctx, k_ra8_err_not_initialized, k_ra8_ok, k_ra8_wifi_state_down, ra8_wifi::lease, ra8_wifi_backend::leave, ra8_wifi::open, RA8_CHECK_NULL_PTR, RA8_WIFI_TAG, ra8_wifi_backend::radio_down, ra8_wifi::radio_on, and ra8_wifi::state.
|
nodiscard |
Read what the backend knows about the associated AP.
Asks the backend for the co-processor's own association record – BSSID, SSID, channel, signal level and auth mode – and caches the signal level for ra8_wifi_status. A useful post-connect sanity check on what the radio actually settled on.
| [in,out] | wifi | Open handle; must be non-null. |
| [out] | out | Record to fill; must be non-null. |
| k_ra8_ok | out describes the current association. |
| k_ra8_err_null_ptr | wifi or out was null. |
| k_ra8_err_not_initialized | wifi is not open. |
| k_ra8_err_invalid_state | The station is not associated. |
| k_ra8_err_protocol_error | The backend reported no AP record. |
| k_ra8_err_spi_error | The backend's transport refused a transfer. |
wifi is associated; asking otherwise is refused. wifi has been initialised. out is fully written and rssi is cached for status. out is cleared rather than left half-written.Definition at line 462 of file ra8_wifi.c.
References ra8_wifi::backend, ra8_wifi::backend_ctx, ra8_wifi_backend::get_ap, k_ra8_err_invalid_state, k_ra8_err_not_initialized, k_ra8_ok, k_ra8_wifi_state_associated, ra8_wifi::open, RA8_CHECK_NULL_PTR, RA8_WIFI_TAG, ra8_wifi::rssi, ra8_wifi_ap::rssi, and ra8_wifi::state.
Referenced by wifi_hal_join_run().
|
nodiscard |
Read the cached DHCP lease without touching the wire.
Returns the lease ra8_wifi_wait_ip last obtained, straight from the handle. It performs no I/O, so it is the cheap way for an application to read back its address after binding; out->bound distinguishes a real lease from none.
| [in] | wifi | Open handle; must be non-null. |
| [out] | out | Lease to fill; must be non-null. bound is false when no lease has been obtained. |
| k_ra8_ok | out holds the cached lease. |
| k_ra8_err_null_ptr | wifi or out was null. |
| k_ra8_err_not_initialized | wifi is not open. |
wifi has been initialised. out is fully written, including on the unbound path.Definition at line 383 of file ra8_wifi.c.
References k_ra8_err_not_initialized, k_ra8_ok, ra8_wifi::lease, ra8_wifi::open, RA8_CHECK_NULL_PTR, and RA8_WIFI_TAG.
|
nodiscard |
Read the station's own MAC address.
Asks the backend for the station address, caches it on the handle and copies it out. The address is a property of the radio's efuses, so it is stable across resets and available once the radio has been started.
That stability is also the fallback: when the backend cannot be asked – an associated station shares its link with the traffic the AP has begun forwarding, and a query can lose that race – the cached address ra8_wifi_connect already read is returned instead. Only a handle that has never held a valid address reports the failure, because only then is there no answer to give.
| [in,out] | wifi | Open handle; must be non-null. |
| [out] | out | Address to fill; must be non-null. |
| k_ra8_ok | out holds the station address, freshly read or cached. |
| k_ra8_err_null_ptr | wifi or out was null. |
| k_ra8_err_not_initialized | wifi is not open. |
| k_ra8_err_protocol_error | The backend reported no valid address and no address has ever been cached. |
| k_ra8_err_spi_error | The backend's transport refused a transfer and no address has ever been cached. |
wifi has been initialised. out holds k_ra8_wifi_mac_bytes octets. out is cleared rather than left half-written.Definition at line 434 of file ra8_wifi.c.
References ra8_wifi::backend, ra8_wifi::backend_ctx, ra8_wifi_backend::get_mac, k_ra8_err_not_initialized, k_ra8_ok, ra8_wifi::mac, ra8_wifi::mac_valid, ra8_wifi::open, RA8_CHECK_NULL_PTR, and RA8_WIFI_TAG.
Referenced by wifi_hal_join_run().
|
nodiscard |
Bring the radio and its link up and make a handle usable.
Validates the configuration, copies it into the handle, and asks the backend to open: bind its transport, open its link, and prove the radio answers. No network is joined here – that is ra8_wifi_connect.
| [out] | wifi | Handle to initialise; must be non-null. |
| [in] | cfg | Configuration; must be non-null with a complete backend table and an ip_bind hook. |
| k_ra8_ok | The handle is open and the radio is answering. |
| k_ra8_err_null_ptr | wifi, cfg, cfg->backend, a backend row, or cfg->ip_bind was null. |
| k_ra8_err_invalid_state | wifi is already open. |
| k_ra8_err_timeout | The backend could not prove the radio is answering. |
| k_ra8_err_hw_timeout | The radio never armed its handshake. |
| k_ra8_err_spi_error | The backend's transport refused a transfer. |
wifi is zero-initialised, or has been deinitialised. wifi is left closed rather than half-open.Definition at line 166 of file ra8_wifi.c.
References ra8_wifi::backend, ra8_wifi_cfg::backend, ra8_wifi::backend_ctx, ra8_wifi_cfg::backend_ctx, internal_check_backend(), ra8_wifi::ip_bind, ra8_wifi_cfg::ip_bind, ra8_wifi::ip_ctx, ra8_wifi_cfg::ip_ctx, k_ra8_err_invalid_state, k_ra8_ok, k_ra8_wifi_state_down, ra8_wifi::open, ra8_wifi_backend::open, RA8_CHECK_NULL_PTR, RA8_WIFI_TAG, and ra8_wifi::state.
Referenced by wifi_hal_join_run().
|
nodiscard |
Service the link once and refresh the cached association state.
Drives the backend for one service cycle – draining events, noticing a disconnection – and folds the result into the handle's lifecycle. Use it to watch a link that is associated but has not yet obtained an IP; it must not be called once ra8_wifi_wait_ip has bound an address, because from that point the IP stack owns the wire.
| [in,out] | wifi | Open handle; must be non-null. |
| [out] | out | Association state after the cycle; must be non-null. |
| k_ra8_ok | The link was serviced; out holds the reading. |
| k_ra8_err_null_ptr | wifi or out was null. |
| k_ra8_err_not_initialized | wifi is not open. |
| k_ra8_err_invalid_state | wifi already holds an IP; the IP stack owns the wire and must not be pre-empted. |
| k_ra8_err_hw_timeout | The radio never armed its handshake. |
| k_ra8_err_spi_error | The backend's transport refused a transfer. |
wifi has been initialised and has not bound an IP. Definition at line 411 of file ra8_wifi.c.
References ra8_wifi::backend, ra8_wifi::backend_ctx, k_ra8_err_invalid_state, k_ra8_err_not_initialized, k_ra8_ok, k_ra8_wifi_link_down, k_ra8_wifi_link_up, k_ra8_wifi_state_associated, k_ra8_wifi_state_down, k_ra8_wifi_state_ip_bound, ra8_wifi::open, RA8_CHECK_NULL_PTR, RA8_WIFI_TAG, ra8_wifi_backend::service, and ra8_wifi::state.
Referenced by wifi_hal_settle().
|
nodiscard |
Read a handle's cached status without touching the wire.
Fills out from state the handle already holds – lifecycle position, the associated and IP-bound flags derived from it, the last signal reading and the current lease. It performs no I/O and is safe to poll for a display; call ra8_wifi_poll first when a fresher association reading is needed.
| [in] | wifi | Open handle; must be non-null. |
| [out] | out | Status to fill; must be non-null. |
| k_ra8_ok | out describes the handle. |
| k_ra8_err_null_ptr | wifi or out was null. |
| k_ra8_err_not_initialized | wifi is not open. |
wifi has been initialised. out is fully written.Definition at line 394 of file ra8_wifi.c.
References ra8_wifi_status::associated, ra8_wifi_status::ip, ra8_wifi_status::ip_bound, k_ra8_err_not_initialized, k_ra8_ok, k_ra8_wifi_state_associated, k_ra8_wifi_state_ip_bound, ra8_wifi::lease, ra8_wifi::open, RA8_CHECK_NULL_PTR, RA8_WIFI_TAG, ra8_wifi::rssi, ra8_wifi_status::rssi, ra8_wifi::state, and ra8_wifi_status::state.
Referenced by wifi_hal_settle().
|
nodiscard |
Obtain an IP address for the associated station, blocking until bound.
Runs the configured ra8_wifi_ip_bind_fn with the station MAC, caches the lease it returns, and advances the handle to k_ra8_wifi_state_ip_bound. The provider owns the mechanism (DHCP today) and the wait; from here the caller uses the IP stack's socket API and must not call ra8_wifi_poll.
| [in,out] | wifi | Open, associated handle; must be non-null. |
| [out] | out | Lease to fill; must be non-null. |
| k_ra8_ok | out holds a usable lease. |
| k_ra8_err_null_ptr | wifi or out was null. |
| k_ra8_err_not_initialized | wifi is not open. |
| k_ra8_err_invalid_state | The station is not associated. |
| k_ra8_err_timeout | The provider did not obtain a lease. |
wifi. out is cleared rather than left half-written.Definition at line 353 of file ra8_wifi.c.
References ra8_wifi_lease::bound, ra8_wifi_lease::ip, ra8_wifi::ip_bind, ra8_wifi::ip_ctx, k_ra8_err_invalid_state, k_ra8_err_not_initialized, k_ra8_err_timeout, k_ra8_ok, k_ra8_wifi_state_associated, k_ra8_wifi_state_ip_bound, ra8_wifi::lease, ra8_wifi::mac, ra8_wifi::open, RA8_CHECK_NULL_PTR, RA8_WIFI_TAG, and ra8_wifi::state.
Referenced by wifi_hal_join_run().