Skip to content

Commit

Permalink
ESP32/init: timer:sleep(infinity) on exit when devmode is enabled
Browse files Browse the repository at this point in the history
Application is started inside of `try .. catch` block and
`timer:sleep(infinity)` is used after application exit according to
devmode settings.

Signed-off-by: Davide Bettio <[email protected]>
  • Loading branch information
bettio committed Aug 6, 2023
1 parent 6fe8c61 commit d1bab7b
Showing 1 changed file with 34 additions and 30 deletions.
64 changes: 34 additions & 30 deletions libs/esp32boot/esp32init.erl
Original file line number Diff line number Diff line change
Expand Up @@ -24,38 +24,24 @@

start() ->
console:print(<<"AtomVM init.\n">>),
boot().

io:format("Starting application...~n"),

Exit =
try boot() of
Result -> {exit, Result}
catch
Error -> {crash, Error}
end,
erlang:display(Exit),

io:format("Looping...~n"),

loop().

loop() ->
receive
Msg ->
erlang:display({received_message, Msg}),
loop()
end.

maybe_start_dev_mode(SystemStatus) ->
is_dev_mode_enabled(SystemStatus) ->
case {SystemStatus, esp:nvs_get_binary(atomvm, dev_mode)} of
{_, <<"always">>} ->
ep32devmode:start_dev_mode();
true;
{_, <<"never">>} ->
not_started;
false;
{ok, undefined} ->
not_started;
{failed_app_start, undefined} ->
esp32devmode:start_dev_mode()
false;
{app_fail, undefined} ->
true
end.

maybe_start_dev_mode(SystemStatus) ->
case is_dev_mode_enabled(SystemStatus) of
true -> esp32devmode:start_dev_mode();
false -> not_started
end.

% TODO: add support for multiple apps
Expand All @@ -67,11 +53,29 @@ boot() ->
case atomvm:add_avm_pack_file(BootPath, [{name, app}]) of
ok ->
StartModule = get_start_module(),
maybe_start_dev_mode(ok),
StartModule:start();
DevOnFail = is_dev_mode_enabled(app_fail),
StartedDevMode = maybe_start_dev_mode(ok),

io:format("Starting application...~n"),
case DevOnFail of
true ->
try StartModule:start() of
Result -> io:format("Exited: ~p.~n", [Result])
catch
Error -> io:format("Crashed: ~p.~n", [Error])
end,
case StartedDevMode of
false -> maybe_start_dev_mode(app_fail);
true -> ok
end,
timer:sleep(infinity);
false ->
StartModule:start()
end;
{error, Reason} ->
io:format("Failed app start: ~p.~n", [Reason]),
maybe_start_dev_mode(failed_app_start)
maybe_start_dev_mode(app_fail),
timer:sleep(infinity)
end.

get_boot_path() ->
Expand Down

0 comments on commit d1bab7b

Please sign in to comment.