|
| 1 | +% |
| 2 | +% This file is part of AtomVM. |
| 3 | +% |
| 4 | +% Copyright 2025 Paul Guyot <pguyot@kallisys.net> |
| 5 | +% |
| 6 | +% Licensed under the Apache License, Version 2.0 (the "License"); |
| 7 | +% you may not use this file except in compliance with the License. |
| 8 | +% You may obtain a copy of the License at |
| 9 | +% |
| 10 | +% http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | +% |
| 12 | +% Unless required by applicable law or agreed to in writing, software |
| 13 | +% distributed under the License is distributed on an "AS IS" BASIS, |
| 14 | +% WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 15 | +% See the License for the specific language governing permissions and |
| 16 | +% limitations under the License. |
| 17 | +% |
| 18 | +% SPDX-License-Identifier: Apache-2.0 OR LGPL-2.1-or-later |
| 19 | +% |
| 20 | + |
| 21 | +-module(picow_wifi_epmd_disterl). |
| 22 | + |
| 23 | +-export([start/0]). |
| 24 | + |
| 25 | +start() -> |
| 26 | + Parent = self(), |
| 27 | + Config = [ |
| 28 | + {sta, [ |
| 29 | + {ssid, <<"myssid">>}, |
| 30 | + {psk, <<"mypsk">>}, |
| 31 | + {got_ip, fun(Arg) -> got_ip(Parent, Arg) end} |
| 32 | + ]}, |
| 33 | + {sntp, [ |
| 34 | + {host, "pool.ntp.org"} |
| 35 | + ]} |
| 36 | + ], |
| 37 | + case network:start(Config) of |
| 38 | + {ok, _Pid} -> |
| 39 | + receive |
| 40 | + {got_ip, IPv4} -> |
| 41 | + distribution_start(IPv4) |
| 42 | + end; |
| 43 | + Error -> |
| 44 | + erlang:display(Error) |
| 45 | + end. |
| 46 | + |
| 47 | +got_ip(Parent, {IPv4, _Netmask, _Gateway}) -> |
| 48 | + Parent ! {got_ip, IPv4}. |
| 49 | + |
| 50 | +distribution_start(Address) -> |
| 51 | + {ok, _EPMDPid} = epmd:start_link([]), |
| 52 | + {ok, _KernelPid} = kernel:start(normal, []), |
| 53 | + {X, Y, Z, T} = Address, |
| 54 | + Node = list_to_atom(lists:flatten(io_lib:format("atomvm@~B.~B.~B.~B", [X, Y, Z, T]))), |
| 55 | + {ok, _NetKernelPid} = net_kernel:start(Node, #{name_domain => longnames}), |
| 56 | + io:format("Distribution was started\n"), |
| 57 | + io:format("Node is ~p\n", [node()]), |
| 58 | + net_kernel:set_cookie(<<"AtomVM">>), |
| 59 | + io:format("Cookie is ~s\n", [net_kernel:get_cookie()]), |
| 60 | + register(disterl, self()), |
| 61 | + io:format( |
| 62 | + "This AtomVM node is waiting for 'quit' message, and this process is registered as 'disterl'\n" |
| 63 | + ), |
| 64 | + io:format("On an OTP node with long names distribution, run:\n"), |
| 65 | + io:format("erlang:set_cookie('~s', 'AtomVM').\n", [Node]), |
| 66 | + io:format("{disterl, '~s'} ! quit.\n", [Node]), |
| 67 | + receive |
| 68 | + quit -> ok |
| 69 | + end. |
0 commit comments