6#include <ArduinoJson.h>
8#include <net/ethernet.h>
9#include <net/settings.h>
10#include <utils/logging.h>
12#include <protocol/jsonl_server.h>
13#include <web/server.h>
17net::EthernetClass net::Ethernet;
18net::MDNSClass net::MDNS;
20FLASHMEM
void net::StartupConfig::reset_defaults() {
21#ifdef ANABRID_SKIP_ETHERNET
22 enable_ethernet =
false;
23 LOG_ALWAYS(
"Skipping Ethernet due to build flag ANABRID_ETHERNET");
25 enable_ethernet =
true;
30#ifdef ANABRID_SKIP_DHCP
32 LOG_ALWAYS(
"Skipping DHCP due to build flag ANABRID_SKIP_DHCP");
42 sprintf(mac_str,
"lucidac-%02X-%02X-%02X", mac[3], mac[4], mac[5]);
45 static_ipaddr = IPAddress(192, 168, 1, 100);
46 static_netmask = IPAddress(255, 255, 255, 0);
47 static_gw = IPAddress(192, 168, 1, 1);
48 static_dns = IPAddress(8, 8, 8, 8);
51 enable_webserver =
false;
52 enable_websockets =
false;
58 connection_timeout_ms = 72 * 1000 * 1000;
62FLASHMEM
void net::StartupConfig::fromJson(JsonObjectConst
src, nvmconfig::Context c) {
63 JSON_GET(
src, enable_ethernet);
64 JSON_GET(
src, enable_dhcp);
65 JSON_GET(
src, enable_jsonl);
66 JSON_GET(
src, enable_webserver);
67 JSON_GET(
src, enable_websockets);
68 JSON_GET(
src, enable_mdns);
69 JSON_GET(
src, jsonl_port);
70 JSON_GET(
src, webserver_port);
72 JSON_GET_AS(
src, hostname,
const char *);
73 JSON_GET_AS(
src, static_ipaddr, IPAddress);
74 JSON_GET_AS(
src, static_netmask, IPAddress);
75 JSON_GET_AS(
src, static_gw, IPAddress);
76 JSON_GET_AS(
src, static_dns, IPAddress);
80 if (hostname.length() > 250)
81 hostname = hostname.substr(0, 250);
84FLASHMEM
void net::StartupConfig::toJson(JsonObject target, nvmconfig::Context c)
const {
85 JSON_SET(target, enable_ethernet);
86 JSON_SET(target, enable_dhcp);
87 JSON_SET(target, enable_jsonl);
88 JSON_SET(target, enable_webserver);
89 JSON_SET(target, enable_websockets);
90 JSON_SET(target, enable_mdns);
91 JSON_SET(target, jsonl_port);
92 JSON_SET(target, webserver_port);
93 JSON_SET(target, mac);
94 JSON_SET(target, hostname);
95 JSON_SET(target, static_ipaddr);
96 JSON_SET(target, static_netmask);
97 JSON_SET(target, static_gw);
98 JSON_SET(target, static_dns);
101FLASHMEM
int net::StartupConfig::begin_ip() {
102 if (!enable_ethernet) {
103 LOG_ALWAYS(
"Ethernet disabled by user setting");
114 net::Ethernet.setMACAddress(mac.mac);
116 LOG2(
"MAC: ", toString(mac).c_str())
122 LOG2(
"DHCP with Hostname: ", hostname.c_str());
123 net::Ethernet.setHostname(hostname.c_str());
124 if (!net::Ethernet.begin()) {
125 LOG_ERROR(
"Error starting ethernet DHCP client.");
128 LOG(ANABRID_DEBUG_INIT,
"Waiting for IP address on ethernet...");
129 if (!net::Ethernet.waitForLocalIP(10 * 1000 )) {
130 LOG_ERROR(
"Error getting IP address.");
134 if (!valid(static_ipaddr) || !valid(static_netmask) || !valid(static_gw)) {
135 LOG_ERROR(
"Illegal ipaddr/netmask/gw. Recovering with defaults.");
136 auto defaults = StartupConfig();
137 static_ipaddr = defaults.static_ipaddr;
138 static_netmask = defaults.static_netmask;
139 static_gw = defaults.static_gw;
141 if (!net::Ethernet.begin(static_ipaddr, static_netmask, static_gw)) {
142 LOG_ERROR(
"Error starting ethernet with static IP address.");
145 if (!valid(static_dns)) {
146 LOG_ERROR(
"Illegal dns server. Recovering with defaults.")
147 auto defaults = StartupConfig();
148 static_dns = defaults.static_dns;
150 net::Ethernet.setDNSServerIP(static_dns);
153 LOG4("JSONL Listening on ",
net::Ethernet.localIP(), ":", jsonl_port);
157FLASHMEM
void net::StartupConfig::begin_mdns() {
158 MDNS.begin(hostname.c_str());
160 MDNS.addService(
"_lucijsonl",
"_tcp", jsonl_port);
161 if (enable_webserver)
162 MDNS.addService(
"_http",
"_tcp", webserver_port);
170FLASHMEM
void net::status(JsonObject &msg_out) {
171 msg_out[
"interfaceStatus"] = net::Ethernet.interfaceStatus();
174 msg_out[
"otp_mac"] = std::string(MacAddress::otp());
176 auto ip = msg_out.createNestedObject(
"ip");
177 ip[
"local"] = net::Ethernet.localIP();
178 ip[
"broadcast"] = net::Ethernet.broadcastIP();
179 ip[
"gateway"] = net::Ethernet.gatewayIP();
181 auto dhcp = msg_out.createNestedObject(
"dhcp");
182 dhcp[
"active"] = net::Ethernet.isDHCPActive();
183 dhcp[
"enabled"] = net::Ethernet.isDHCPEnabled();
185 auto link = msg_out.createNestedObject(
"link");
186 link[
"state"] = net::Ethernet.linkState();
187 link[
"speed"] = net::Ethernet.linkSpeed();
188 link[
"isCrossover"] = net::Ethernet.linkIsCrossover();
189 link[
"isFullDuplex"] = net::Ethernet.linkIsFullDuplex();
if(((src) >=(0x60000000) &&(src)<(0x60000000)+(0x800000)))