REDAC HybridController
Firmware for LUCIDAC/REDAC Teensy
Loading...
Searching...
No Matches
ethernet.cpp
Go to the documentation of this file.
1// Copyright (c) 2024 anabrid GmbH
2// Contact: https://www.anabrid.com/licensing/
3//
4// SPDX-License-Identifier: MIT OR GPL-2.0-or-later
5
6#include <ArduinoJson.h>
7
8#include <net/ethernet.h>
9#include <net/settings.h>
10#include <utils/logging.h>
11
12#include <protocol/jsonl_server.h>
13#include <web/server.h>
14
15using namespace utils;
16
17net::EthernetClass net::Ethernet;
18net::MDNSClass net::MDNS;
19
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");
24#else
25 enable_ethernet = true;
26#endif
27
28// This build flag basically solves a "deadlock" if a no DHCP server is
29// available but the teensy is configured for dhcp.
30#ifdef ANABRID_SKIP_DHCP
31 enable_dhcp = false;
32 LOG_ALWAYS("Skipping DHCP due to build flag ANABRID_SKIP_DHCP");
33#else
34 enable_dhcp = true;
35#endif
36
37 mac.reset();
38
39 // Note that the default hostname by QNEthernet is just "teensy-lwip".
40 // Instead, here we choose something unique to the device.
41 char mac_str[50];
42 sprintf(mac_str, "lucidac-%02X-%02X-%02X", mac[3], mac[4], mac[5]);
43 hostname = mac_str;
44
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);
49
50 enable_jsonl = true;
51 enable_webserver = false;
52 enable_websockets = false;
53 enable_mdns = true;
54
55 jsonl_port = 5732;
56 webserver_port = 80;
57
58 connection_timeout_ms = 72 * 1000 * 1000;
59 max_connections = 20;
60}
61
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);
71 JSON_GET(src, mac);
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);
77
78 // Maximum hostname length is an industry standard (POSIX is 255 chars limits.h)
79 // and also and dictated by EEPROM size.
80 if (hostname.length() > 250)
81 hostname = hostname.substr(0, 250);
82}
83
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);
99}
100
101FLASHMEM int net::StartupConfig::begin_ip() {
102 if (!enable_ethernet) {
103 LOG_ALWAYS("Ethernet disabled by user setting");
104 }
105
106 // if(!net::Ethernet.linkState()) {
107 // TODO: This is only a workaround in the moment and requires later
108 // setup with the onLinkState callback
109 // LOG_ALWAYS("Ethernet: No link detected. Skipping Ethernet setup.");
110 // return;
111 //}
112
113 if (valid(mac))
114 net::Ethernet.setMACAddress(mac.mac); // else keep system default
115
116 LOG2("MAC: ", toString(mac).c_str())
117
118 // TODO: Should indicate the state of the IP aquisition on the
119 // LEDs of the LUCIDAC front.
120
121 if (enable_dhcp) {
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.");
126 return 1;
127 }
128 LOG(ANABRID_DEBUG_INIT, "Waiting for IP address on ethernet...");
129 if (!net::Ethernet.waitForLocalIP(10 * 1000 /* ms*/)) {
130 LOG_ERROR("Error getting IP address.");
131 return 2;
132 }
133 } else {
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;
140 }
141 if (!net::Ethernet.begin(static_ipaddr, static_netmask, static_gw)) {
142 LOG_ERROR("Error starting ethernet with static IP address.");
143 return 3;
144 }
145 if (!valid(static_dns)) {
146 LOG_ERROR("Illegal dns server. Recovering with defaults.")
147 auto defaults = StartupConfig();
148 static_dns = defaults.static_dns;
149 }
150 net::Ethernet.setDNSServerIP(static_dns);
151 }
152
153 LOG4("JSONL Listening on ", net::Ethernet.localIP(), ":", jsonl_port);
154 return 0;
155}
156
157FLASHMEM void net::StartupConfig::begin_mdns() {
158 MDNS.begin(hostname.c_str());
159 if (enable_jsonl)
160 MDNS.addService("_lucijsonl", "_tcp", jsonl_port);
161 if (enable_webserver)
162 MDNS.addService("_http", "_tcp", webserver_port);
163}
164
165/*void net::StartupConfig::begin() {
166 net::register_settings();
167 begin_ip();
168}*/
169
170FLASHMEM void net::status(JsonObject &msg_out) {
171 msg_out["interfaceStatus"] = net::Ethernet.interfaceStatus();
172
173 // TODO: Move to net_get
174 msg_out["otp_mac"] = std::string(MacAddress::otp()); // one time programmable system/permanent mac
175
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();
180
181 auto dhcp = msg_out.createNestedObject("dhcp");
182 dhcp["active"] = net::Ethernet.isDHCPActive(); // probably move to net_get
183 dhcp["enabled"] = net::Ethernet.isDHCPEnabled();
184
185 auto link = msg_out.createNestedObject("link");
186 link["state"] = net::Ethernet.linkState();
187 link["speed"] = net::Ethernet.linkSpeed(); // in Mpbs
188 link["isCrossover"] = net::Ethernet.linkIsCrossover();
189 link["isFullDuplex"] = net::Ethernet.linkIsFullDuplex();
190
191 // msg_out["tcp_port"] = net::ethernet::Ethernet.server_port;
194 // auto settings = msg_out.createNestedObject("settings");
195 // eth.write_to_json(settings);
196}
uint32_t src
Definition flasher.cpp:63
if(((src) >=(0x60000000) &&(src)<(0x60000000)+(0x800000)))
Definition flasher.cpp:177