From fdd51a05dac24f85dc886c26be849de08ce3d7d6 Mon Sep 17 00:00:00 2001 From: Paul Guyot Date: Sat, 11 Jan 2025 00:02:06 +0100 Subject: [PATCH] Add epmd_disterl esp32 example Signed-off-by: Paul Guyot --- examples/erlang/esp32/CMakeLists.txt | 1 + examples/erlang/esp32/epmd_disterl.erl | 56 ++++++++++++++++++++++++++ 2 files changed, 57 insertions(+) create mode 100644 examples/erlang/esp32/epmd_disterl.erl diff --git a/examples/erlang/esp32/CMakeLists.txt b/examples/erlang/esp32/CMakeLists.txt index fc28c3f5a..f28afeb8f 100644 --- a/examples/erlang/esp32/CMakeLists.txt +++ b/examples/erlang/esp32/CMakeLists.txt @@ -37,3 +37,4 @@ pack_runnable(sx127x sx127x eavmlib estdlib) pack_runnable(reformat_nvs reformat_nvs eavmlib) pack_runnable(uartecho uartecho eavmlib estdlib) pack_runnable(ledc_example ledc_example eavmlib estdlib) +pack_runnable(epmd_disterl epmd_disterl eavmlib estdlib) diff --git a/examples/erlang/esp32/epmd_disterl.erl b/examples/erlang/esp32/epmd_disterl.erl new file mode 100644 index 000000000..71a49367e --- /dev/null +++ b/examples/erlang/esp32/epmd_disterl.erl @@ -0,0 +1,56 @@ +% +% This file is part of AtomVM. +% +% Copyright 2025 Paul Guyot +% +% Licensed under the Apache License, Version 2.0 (the "License"); +% you may not use this file except in compliance with the License. +% You may obtain a copy of the License at +% +% http://www.apache.org/licenses/LICENSE-2.0 +% +% Unless required by applicable law or agreed to in writing, software +% distributed under the License is distributed on an "AS IS" BASIS, +% WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +% See the License for the specific language governing permissions and +% limitations under the License. +% +% SPDX-License-Identifier: Apache-2.0 OR LGPL-2.1-or-later +% + +-module(epmd_disterl). + +-export([start/0]). + +start() -> + Creds = [ + {ssid, "myssid"}, + {psk, "mypsk"} + ], + case network:wait_for_sta(Creds, 30000) of + {ok, {Address, _Netmask, _Gateway}} -> + distribution_start(Address); + Error -> + io:format("An error occurred starting network: ~p~n", [Error]) + end. + +distribution_start(Address) -> + {ok, _EPMDPid} = epmd:start_link([]), + {ok, _KernelPid} = kernel:start(normal, []), + {X, Y, Z, T} = Address, + Node = list_to_atom(lists:flatten(io_lib:format("atomvm@~B.~B.~B.~B", [X, Y, Z, T]))), + {ok, _NetKernelPid} = net_kernel:start(Node, #{name_domain => longnames}), + io:format("Distribution was started\n"), + io:format("Node is ~p\n", [node()]), + net_kernel:set_cookie(<<"AtomVM">>), + io:format("Cookie is ~s\n", [net_kernel:get_cookie()]), + register(disterl, self()), + io:format( + "This AtomVM node is waiting for 'quit' message, and this process is registered as 'disterl'\n" + ), + io:format("On an OTP node with long names distribution, run:\n"), + io:format("erlang:set_cookie('~s', 'AtomVM').\n", [Node]), + io:format("{disterl, '~s'} ! quit.\n", [Node]), + receive + quit -> ok + end.