Skip to content

Commit 42de0ad

Browse files
committed
Merge pull request atomvm#499 from fadushin/network_api_fix
Fixed examples and docs to reflect API changes This PR fixes examples and documentation to reflect changes to the return value from the `network:start` function. The release notes have been augmented to note that the change to the network interface is API-incompatible with previous releases of AtomVM. These changes are made under both the "Apache 2.0" and the "GNU Lesser General Public License 2.1 or later" license terms (dual license). SPDX-License-Identifier: Apache-2.0 OR LGPL-2.1-or-later
2 parents 2e4df8a + db0e6be commit 42de0ad

File tree

5 files changed

+15
-12
lines changed

5 files changed

+15
-12
lines changed

CHANGELOG.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,9 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
4848
devices to be attached to the same SPI Bus.
4949
- Changed the return value from `erlang:system_info(esp32_chip_info)` from a tuple to a map, with
5050
additional information.
51+
- Changed the return type of the `network:start` function to return the tuple `{ok, Pid}` on a
52+
successful call, instead of the bare atom `ok`. Applications that use `network:start` and
53+
check the return value will need to be modified.
5154

5255
## [0.5.1] - Unreleased
5356
### Added

doc/src/network-programming-guide.md

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ The `<sta-properties>` property list should contain the following entries:
2929

3030
> Note that the station mode SSID and password _may_ be stored in non-volatile storage, in which case these parameters may be skipped. See the "NVS Credentials" section below, for more information about using non-volatile storage to store credentials that persist across device reboots.
3131
32-
The `network:start/1` will immediately return `ok`, 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+
The `network:start/1` 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.
3333

3434
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.
3535

@@ -60,7 +60,7 @@ The following example illustrates initialization of the WiFi network in STA mode
6060
{dhcp_hostname, <<"myesp32">>}
6161
]}
6262
],
63-
ok = network:start(Config),
63+
{ok, Pid} = network:start(Config),
6464
...
6565

6666
The following callback functions will be called when the corresponding events occur during the lifetime of the network connection.
@@ -113,7 +113,7 @@ If the password is omitted, then an _open network_ will be created, and a warnin
113113

114114
> Note that the station mode SSID and password _may_ be stored in non-volatile storage, in which case these parameters may be skipped. See the "NVS Credentials" section below, for more information about using non-volatile storage to store credentials that persist across device reboots.
115115
116-
The `network:start/1` will immediately return `ok`, 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 to be ready to accept connections from other devices, or to be notified when other devices connect to this AP.
116+
The `network:start/1` will immediately return `{ok, Pid}`, where `Pid` is the process id of the network server, 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 to be ready to accept connections from other devices, or to be notified when other devices connect to this AP.
117117

118118
Applications can specify callback functions, which get triggered as events emerge from the network layer, including when a station connects or disconnects from the AP, as well as when a station is assigned an IP address.
119119

@@ -146,7 +146,7 @@ The following example illustrates initialization of the WiFi network in AP mode.
146146
{sta_disconnected, fun sta_disconnected/1},
147147
]}
148148
],
149-
ok = network:start(Config),
149+
{ok, Pid} = network:start(Config),
150150
...
151151

152152
The following callback functions will be called when the corresponding events occur during the lifetime of the network connection.

examples/erlang/esp32/ap_sta_network.erl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ start() ->
4747
{sntp, [{endpoint, "pool.ntp.org"}]}
4848
],
4949
case network:start(Config) of
50-
ok ->
50+
{ok, _Pid} ->
5151
sleep_forever();
5252
Error ->
5353
erlang:display(Error)

examples/erlang/esp32/morse_server.erl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ start() ->
3434
]}
3535
],
3636
case network:start(Config) of
37-
ok ->
37+
{ok, _Pid} ->
3838
wait_for_message();
3939
Error ->
4040
erlang:display(Error)

libs/eavmlib/src/network.erl

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -154,7 +154,7 @@ wait_for_sta(StaConfig, Timeout) ->
154154
],
155155
Config = [{sta, NewStaConfig}],
156156
case start(Config) of
157-
ok ->
157+
{ok, _Pid} ->
158158
wait_for_ip(Timeout);
159159
Error ->
160160
Error
@@ -203,7 +203,7 @@ wait_for_ap(ApConfig, Timeout) ->
203203
],
204204
Config = [{ap, NewApConfig}],
205205
case start(Config) of
206-
ok ->
206+
{ok, _Pid} ->
207207
wait_for_ap_started(Timeout);
208208
Error ->
209209
Error
@@ -225,10 +225,10 @@ wait_for_ap(ApConfig, Timeout) ->
225225
-spec start(Config :: network_config()) -> ok | {error, Reason :: term()}.
226226
start(Config) ->
227227
case gen_server:start({local, ?MODULE}, ?MODULE, Config, []) of
228-
{ok, Pid} ->
228+
{ok, Pid} = R ->
229229
case gen_server:call(Pid, start) of
230230
ok ->
231-
{ok, Pid};
231+
R;
232232
Error ->
233233
Error
234234
end;
@@ -239,10 +239,10 @@ start(Config) ->
239239
-spec start_link(Config :: network_config()) -> ok | {error, Reason :: term()}.
240240
start_link(Config) ->
241241
case gen_server:start_link({local, ?MODULE}, ?MODULE, Config, []) of
242-
{ok, Pid} ->
242+
{ok, Pid} = R ->
243243
case gen_server:call(Pid, start) of
244244
ok ->
245-
{ok, Pid};
245+
R;
246246
Error ->
247247
Error
248248
end;

0 commit comments

Comments
 (0)