Skip to content

Commit 3cb2d44

Browse files
committed
Updated "Network Programmers Guide" documentation
Adds descriptions of new configuration options and `connect/0,1`, `disconnect/0`, and `sta_status/0` functions. Signed-off-by: Winford <winford@object.stream>
1 parent 4711b28 commit 3cb2d44

File tree

1 file changed

+84
-9
lines changed

1 file changed

+84
-9
lines changed

doc/src/network-programming-guide.md

Lines changed: 84 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -20,21 +20,61 @@ This document describes the basic design of the AtomVM network interfaces, and h
2020

2121
In STA mode, the ESP32 or the Pico W/Pico 2 W connect to an existing WiFi network.
2222

23-
In this case, the input configuration should be a properties list containing a tuple of the form `{sta, <sta-properties>}`, where `<sta-properties>` is a property list containing configuration properties for the device in station mode.
23+
In this case, the input configuration should be a properties list containing a tuple of the form
24+
`{sta, <sta-properties>}`, where `<sta-properties>` is a property list containing configuration
25+
properties for the device in station mode.
2426

2527
The `<sta-properties>` property list should contain the following entries:
2628

2729
* `{ssid, string() | binary()}` The SSID to which the device should connect.
2830
* `{psk, string() | binary()}` The password required to authenticate to the network, if required.
2931

30-
The [`network:start/1`](./apidocs/erlang/eavmlib/network.md#start1) will immediately return `{ok, Pid}`, where `Pid` is the process ID of the network server instance, if the network was properly initialized, or `{error, Reason}`, if there was an error in configuration. However, the application may want to wait for the device to connect to the target network and obtain an IP address, for example, before starting clients or services that require network access.
32+
In addition, the following optional parameters can be specified to configure the network:
3133

32-
Applications can specify callback functions, which get triggered as events emerge from the network layer, including connection to and disconnection from the target network, as well as IP address acquisition.
34+
* `{dhcp_hostname, string()|binary()}` The DHCP hostname as which the device should register
35+
(default: `<<"atomvm-<hexmac>">>`, where `<hexmac>` is the hexadecimal representation of the
36+
factory-assigned MAC address of the device).
37+
38+
The following options are only supported on ESP32 platform:
39+
40+
* `{beacon_timeout, fun(() -> term())}` A callback function which will be called when the device
41+
does not receive a beacon frame from the connected access point during the "inactive time" (6
42+
second default, currently not configurable).
43+
* `managed` or `{managed, boolean()}` Used to activate application-managed mode,
44+
[see below](#managed-mode).
45+
46+
The [`network:start/1`](./apidocs/erlang/eavmlib/network.md#start1) will immediately return
47+
`{ok, Pid}`, where `Pid` is the process ID of the network server instance, if the network was
48+
properly initialized, or `{error, Reason}`, if there was an error in configuration. However, the
49+
application may want to wait for the device to connect to the target network and obtain an IP
50+
address, for example, before starting clients or services that require network access.
51+
52+
### Managed mode
53+
54+
For fine-grained control over network connections in station mode, the `managed` boolean may be
55+
used, to start the driver and enable station mode, without starting a connection to an access
56+
point. In this mode, providing the `ssid` and `psk` tuples is optional. If a configuration is
57+
provided, this will be used by the
58+
[`sta_connect/0`](./apidocs/erlang/eavmlib/network.md#sta_connect0) function to initiate a
59+
connection to the access point. If `ssid` and `psk` are omitted from the configuration they must be
60+
supplied to [`sta_connect/1`](./apidocs/erlang/eavmlib/network.md#sta_connect1) to initiate a
61+
connection to an access point. Any new configuration values passed to `sta_connect/1` will replace
62+
any previous values, but leave the rest unchanged. Callback configuration as well as `mdns` and
63+
`sntp` configurations may also be updated in the configuration passed to `sta_connect/1`.
64+
65+
When using managed mode applications should include a `disconnected` callback to also inhibit the
66+
automatic reconnection behavior.
67+
68+
### Station mode callbacks
69+
70+
Applications can specify callback functions, which get triggered as events emerge from the network
71+
layer, including connection to and disconnection from the target network, as well as IP address
72+
acquisition.
3373

3474
Callback functions can be specified by the following configuration parameters:
3575

3676
* `{connected, fun(() -> term())}` A callback function which will be called when the device connects to the target network.
37-
* `{disconnected, fun(() -> term())}` A callback function which will be called when the device disconnects from the target network.
77+
* `{disconnected, fun(() -> term())}` A callback function which will be called when the device disconnects from the target network. If no callback function is provided the default behavior is to attempt to reconnect immediately. By providing a callback function the application can decide whether to reconnect, or connect to a new access point.
3878
* `{got_ip, fun((ip_info()) -> term())}` A callback function which will be called when the device obtains an IP address. In this case, the IPv4 IP address, net mask, and gateway are provided as a parameter to the callback function.
3979

4080
```{warning}
@@ -43,10 +83,7 @@ IPv6 addresses are not yet supported in AtomVM.
4383

4484
Callback functions are optional, but are highly recommended for building robust WiFi applications. The return value from callback functions is ignored, and AtomVM provides no guarantees about the execution context (i.e., BEAM process) in which these functions are invoked.
4585

46-
In addition, the following optional parameters can be specified to configure the AP network (ESP32 only):
47-
48-
* `{dhcp_hostname, string()|binary()}` The DHCP hostname as which the device should register (`<<"atomvm-<hexmac>">>`, where `<hexmac>` is the hexadecimal representation of the factory-assigned MAC address of the device).
49-
* `{beacon_timeout, fun(() -> term())}` A callback function which will be called when the device does not receive a beacon frame from the connected access point during the "inactive time" (6 second default, currently not configurable).
86+
### Station mode example configuration
5087

5188
The following example illustrates initialization of the WiFi network in STA mode. The example program will configure the network to connect to a specified network. Events that occur during the lifecycle of the network will trigger invocations of the specified callback functions.
5289

@@ -75,7 +112,8 @@ gotIp(IpInfo) ->
75112
io:format("Got IP: ~p~n", [IpInfo]).
76113

77114
disconnected() ->
78-
io:format("Disconnected from AP.~n").
115+
io:format("Disconnected from AP, attempting to reconnect~n"),
116+
network:sta_connect().
79117
```
80118

81119
In a typical application, the network should be configured and an IP address should be acquired first, before starting clients or services that have a dependency on the network.
@@ -102,6 +140,43 @@ end
102140

103141
To obtain the signal strength (in decibels) of the connection to the associated access point use [`network:sta_rssi/0`](./apidocs/erlang/eavmlib/network.md#sta_rssi0).
104142

143+
### STA (or AP+STA) mode functions
144+
145+
#### `sta_status`
146+
147+
The function [`network:sta_status/0`](./apidocs/erlang/eavmlib/network.md#sta_status0) may be used
148+
any time after the driver has been started to get the current connection state of the sta
149+
interface. When a connection is initiated, either at start up or when `network:sta_connect/1` is used
150+
in application `managed` mode (which will start with a `disconnected` state) the interface will be
151+
marked as `connecting` followed by `associated` after a connection is established with an access
152+
point. After receiving an IP address the connection will be fully `connected`. If a beacon timeout
153+
event is received (this indicates poor signal strength or a heavily congested network) the status
154+
will change to `degraded` for the remainder of the connection session. This does not always mean
155+
that the connection is still poor, but it can be a helpful diagnostic when experiencing network
156+
problems, and often does result in a dropped connection. When stopping the interface with
157+
`network:sta_disconnect/0` the state will change to `disconnecting` until the interface is completely
158+
stopped and set to `disconnected`.
159+
160+
#### `sta_disconnect`
161+
162+
The function [`network:sta_disconnect/0`](./apidocs/erlang/eavmlib/network.md#sta_disconnect0) will
163+
disconnect a station from the associated access point. Note that using this function without
164+
providing a custom `disconnected` event callback function will result in the driver immediately
165+
attempting to reconnect to the last associated access point.
166+
167+
This function is currently only supported on the ESP32 platform.
168+
169+
#### `sta_connect`
170+
171+
Using the function [`network:sta_connect/0`](./apidocs/erlang/eavmlib/network.md#sta_connect0) will
172+
start a connection to the last configured access point. To connect to a new access point use
173+
[`network:sta_connect/1`](./apidocs/erlang/eavmlib/network.md#sta_connect1) with either a proplist
174+
consisting of `[{ssid, NetworkName}, {psk, Password}, {dhcp_hostname, Hostname}]` (setting the
175+
hostname is optional, and `psk` is not required for unsecure networks), or a complete
176+
`network_config()` consisting of `[sta_config(), sntp_config(), mdns_config()]`.
177+
178+
This function is currently only supported on the ESP32 platform.
179+
105180
## AP mode
106181

107182
In AP mode, the ESP32 starts a WiFi network to which other devices (laptops, mobile devices, other ESP32 devices, etc) can connect. The ESP32 will create an IPv4 network, and will assign itself the address `192.168.4.1`. Devices that attach to the ESP32 in AP mode will be assigned sequential addresses in the `192.168.4.0/24` range, e.g., `192.168.4.2`, `192.168.4.3`, etc.

0 commit comments

Comments
 (0)