Welcome to mirror list, hosted at ThFree Co, Russian Federation.

github.com/Duet3D/RepRapFirmware.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorDavid Crocker <dcrocker@eschertech.com>2020-01-26 23:56:03 +0300
committerDavid Crocker <dcrocker@eschertech.com>2020-01-26 23:56:03 +0300
commit79df4b88ed68875cd58ef4e1ec5ea22f9ab9d55e (patch)
tree701938eab5583381e5896116dec79e21cc278a82
parent6e8f8223fd862eae27545ead596b52c5a6a46ca2 (diff)
Refactored MAC address code
M540 on Duet WiFi now returns correct MAC address MAC address added to object model WiFi firmware version now returned correctly in object model
-rw-r--r--src/GCodes/GCodeBuffer/BinaryParser.cpp5
-rw-r--r--src/GCodes/GCodeBuffer/BinaryParser.h3
-rw-r--r--src/GCodes/GCodeBuffer/GCodeBuffer.cpp2
-rw-r--r--src/GCodes/GCodeBuffer/GCodeBuffer.h2
-rw-r--r--src/GCodes/GCodeBuffer/StringParser.cpp13
-rw-r--r--src/GCodes/GCodeBuffer/StringParser.h4
-rw-r--r--src/GCodes/GCodes2.cpp8
-rw-r--r--src/GCodes/GCodes4.cpp2
-rw-r--r--src/Networking/ESP8266WiFi/WiFiInterface.cpp25
-rw-r--r--src/Networking/ESP8266WiFi/WiFiInterface.h6
-rw-r--r--src/Networking/LwipEthernet/LwipEthernetInterface.cpp11
-rw-r--r--src/Networking/LwipEthernet/LwipEthernetInterface.h6
-rw-r--r--src/Networking/Network.cpp25
-rw-r--r--src/Networking/Network.h4
-rw-r--r--src/Networking/NetworkDefs.h15
-rw-r--r--src/Networking/NetworkInterface.h4
-rw-r--r--src/Networking/W5500Ethernet/W5500Interface.cpp14
-rw-r--r--src/Networking/W5500Ethernet/W5500Interface.h6
-rw-r--r--src/ObjectModel/ObjectModel.cpp17
-rw-r--r--src/ObjectModel/ObjectModel.h3
-rw-r--r--src/Pccb/Network.cpp23
-rw-r--r--src/Pccb/Network.h18
-rw-r--r--src/Platform.cpp14
-rw-r--r--src/Platform.h4
-rw-r--r--src/Version.h2
25 files changed, 158 insertions, 78 deletions
diff --git a/src/GCodes/GCodeBuffer/BinaryParser.cpp b/src/GCodes/GCodeBuffer/BinaryParser.cpp
index 3993791d..e4bb1470 100644
--- a/src/GCodes/GCodeBuffer/BinaryParser.cpp
+++ b/src/GCodes/GCodeBuffer/BinaryParser.cpp
@@ -9,6 +9,7 @@
#include "GCodeBuffer.h"
#include "Platform.h"
#include "RepRap.h"
+#include <Networking/NetworkDefs.h>
BinaryParser::BinaryParser(GCodeBuffer& gcodeBuffer) noexcept : gb(gcodeBuffer)
{
@@ -247,7 +248,7 @@ void BinaryParser::GetIPAddress(IPAddress& returnedIp)
returnedIp.SetV4(ip);
}
-void BinaryParser::GetMacAddress(uint8_t mac[6])
+void BinaryParser::GetMacAddress(MacAddress& mac)
{
if (seenParameter == nullptr)
{
@@ -269,7 +270,7 @@ void BinaryParser::GetMacAddress(uint8_t mac[6])
{
throw ConstructParseException("invalid MAC address");
}
- mac[n] = (uint8_t)v;
+ mac.bytes[n] = (uint8_t)v;
++n;
p = pp;
if (*p != ':')
diff --git a/src/GCodes/GCodeBuffer/BinaryParser.h b/src/GCodes/GCodeBuffer/BinaryParser.h
index 29bed30f..9fdbcc33 100644
--- a/src/GCodes/GCodeBuffer/BinaryParser.h
+++ b/src/GCodes/GCodeBuffer/BinaryParser.h
@@ -15,6 +15,7 @@
class GCodeBuffer;
class IPAddress;
+class MacAddress;
class BinaryParser
{
@@ -34,7 +35,7 @@ public:
uint32_t GetUIValue() THROWS_GCODE_EXCEPTION; // Get an unsigned integer value
DriverId GetDriverId() THROWS_GCODE_EXCEPTION; // Get a driver ID
void GetIPAddress(IPAddress& returnedIp) THROWS_GCODE_EXCEPTION; // Get an IP address quad after a key letter
- void GetMacAddress(uint8_t mac[6]) THROWS_GCODE_EXCEPTION; // Get a MAC address sextet after a key letter
+ void GetMacAddress(MacAddress& mac) THROWS_GCODE_EXCEPTION; // Get a MAC address sextet after a key letter
void GetUnprecedentedString(const StringRef& str, bool allowEmpty) THROWS_GCODE_EXCEPTION; // Get a string with no preceding key letter
void GetQuotedString(const StringRef& str) THROWS_GCODE_EXCEPTION; // Get and copy a quoted string
void GetPossiblyQuotedString(const StringRef& str) THROWS_GCODE_EXCEPTION; // Get and copy a string which may or may not be quoted
diff --git a/src/GCodes/GCodeBuffer/GCodeBuffer.cpp b/src/GCodes/GCodeBuffer/GCodeBuffer.cpp
index 059aed5e..47c01cf5 100644
--- a/src/GCodes/GCodeBuffer/GCodeBuffer.cpp
+++ b/src/GCodes/GCodeBuffer/GCodeBuffer.cpp
@@ -249,7 +249,7 @@ void GCodeBuffer::GetIPAddress(IPAddress& returnedIp)
}
// Get a MAC address sextet after a key letter
-void GCodeBuffer::GetMacAddress(uint8_t mac[6])
+void GCodeBuffer::GetMacAddress(MacAddress& mac)
{
if (isBinaryBuffer)
{
diff --git a/src/GCodes/GCodeBuffer/GCodeBuffer.h b/src/GCodes/GCodeBuffer/GCodeBuffer.h
index 7dc8f846..9170d05e 100644
--- a/src/GCodes/GCodeBuffer/GCodeBuffer.h
+++ b/src/GCodes/GCodeBuffer/GCodeBuffer.h
@@ -73,7 +73,7 @@ public:
int32_t GetIValue() THROWS_GCODE_EXCEPTION __attribute__((hot)); // Get an integer after a key letter
uint32_t GetUIValue() THROWS_GCODE_EXCEPTION; // Get an unsigned integer value
void GetIPAddress(IPAddress& returnedIp) THROWS_GCODE_EXCEPTION; // Get an IP address quad after a key letter
- void GetMacAddress(uint8_t mac[6]) THROWS_GCODE_EXCEPTION; // Get a MAC address sextet after a key letter
+ void GetMacAddress(MacAddress& mac) THROWS_GCODE_EXCEPTION; // Get a MAC address sextet after a key letter
PwmFrequency GetPwmFrequency() THROWS_GCODE_EXCEPTION; // Get a PWM frequency
float GetPwmValue() THROWS_GCODE_EXCEPTION; // Get a PWM value
DriverId GetDriverId() THROWS_GCODE_EXCEPTION; // Get a driver ID
diff --git a/src/GCodes/GCodeBuffer/StringParser.cpp b/src/GCodes/GCodeBuffer/StringParser.cpp
index c4e6b619..c21042f4 100644
--- a/src/GCodes/GCodeBuffer/StringParser.cpp
+++ b/src/GCodes/GCodeBuffer/StringParser.cpp
@@ -15,6 +15,7 @@
#include "RepRap.h"
#include <General/IP4String.h>
#include <General/StringBuffer.h>
+#include <Networking/NetworkDefs.h>
// Replace the default definition of THROW_INTERNAL_ERROR by one that gives line information
#undef THROW_INTERNAL_ERROR
@@ -1282,8 +1283,8 @@ void StringParser::GetIPAddress(IPAddress& returnedIp)
returnedIp.SetV4(ip);
}
-// Get a MAX address sextet after a key letter
-void StringParser::GetMacAddress(uint8_t mac[6])
+// Get a MAC address sextet after a key letter
+void StringParser::GetMacAddress(MacAddress& mac)
{
if (readPointer <= 0)
{
@@ -1301,7 +1302,7 @@ void StringParser::GetMacAddress(uint8_t mac[6])
readPointer = -1;
throw ConstructParseException("invalid MAC address");
}
- mac[n] = (uint8_t)v;
+ mac.bytes[n] = (uint8_t)v;
++n;
p = pp;
if (*p != ':')
@@ -1683,6 +1684,12 @@ void StringParser::AppendAsString(ExpressionValue val, const StringRef& str)
#endif
break;
+ case TYPE_OF(MacAddress):
+ str.catf("%02x:%02x:%02x:%02x:%02x:%02x",
+ (unsigned int)(val.uVal & 0xFF), (unsigned int)((val.uVal >> 8) & 0xFF), (unsigned int)((val.uVal >> 16) & 0xFF), (unsigned int)((val.uVal >> 24) & 0xFF),
+ (unsigned int)(val.param & 0xFF), (unsigned int)((val.param >> 8) & 0xFF));
+ break;
+
default:
throw ConstructParseException("string value expected");
}
diff --git a/src/GCodes/GCodeBuffer/StringParser.h b/src/GCodes/GCodeBuffer/StringParser.h
index b610225b..3b0ad789 100644
--- a/src/GCodes/GCodeBuffer/StringParser.h
+++ b/src/GCodes/GCodeBuffer/StringParser.h
@@ -14,9 +14,11 @@
#include <MessageType.h>
#include <ObjectModel/ObjectModel.h>
#include <GCodes/GCodeException.h>
+#include <Networking/NetworkDefs.h>
class GCodeBuffer;
class IPAddress;
+class MacAddress;
class StringBuffer;
class StringParser
@@ -46,7 +48,7 @@ public:
uint32_t GetUIValue() THROWS_GCODE_EXCEPTION; // Get an unsigned integer value
DriverId GetDriverId() THROWS_GCODE_EXCEPTION; // Get a driver ID
void GetIPAddress(IPAddress& returnedIp) THROWS_GCODE_EXCEPTION; // Get an IP address quad after a key letter
- void GetMacAddress(uint8_t mac[6]) THROWS_GCODE_EXCEPTION; // Get a MAC address sextet after a key letter
+ void GetMacAddress(MacAddress& mac) THROWS_GCODE_EXCEPTION; // Get a MAC address sextet after a key letter
void GetUnprecedentedString(const StringRef& str, bool allowEmpty) THROWS_GCODE_EXCEPTION; // Get a string with no preceding key letter
void GetQuotedString(const StringRef& str, bool allowEmpty = false) THROWS_GCODE_EXCEPTION; // Get and copy a quoted string
void GetPossiblyQuotedString(const StringRef& str, bool allowEmpty = false) THROWS_GCODE_EXCEPTION; // Get and copy a string which may or may not be quoted
diff --git a/src/GCodes/GCodes2.cpp b/src/GCodes/GCodes2.cpp
index a3a06791..4068cdf9 100644
--- a/src/GCodes/GCodes2.cpp
+++ b/src/GCodes/GCodes2.cpp
@@ -2918,14 +2918,14 @@ bool GCodes::HandleMcode(GCodeBuffer& gb, const StringRef& reply)
const unsigned int interface = (gb.Seen('I') ? gb.GetUIValue() : 0);
if (gb.Seen('P'))
{
- uint8_t mac[6];
+ MacAddress mac;
gb.GetMacAddress(mac);
- reprap.GetNetwork().SetMacAddress(interface, mac);
+ result = reprap.GetNetwork().SetMacAddress(interface, mac, reply);
}
else
{
- const uint8_t * const mac = reprap.GetNetwork().GetMacAddress(interface);
- reply.printf("MAC: %02x:%02x:%02x:%02x:%02x:%02x", mac[0], mac[1], mac[2], mac[3], mac[4], mac[5]);
+ const MacAddress& mac = reprap.GetNetwork().GetMacAddress(interface);
+ reply.printf("MAC: %02x:%02x:%02x:%02x:%02x:%02x", mac.bytes[0], mac.bytes[1], mac.bytes[2], mac.bytes[3], mac.bytes[4], mac.bytes[5]);
}
}
break;
diff --git a/src/GCodes/GCodes4.cpp b/src/GCodes/GCodes4.cpp
index 23a31b52..27f2b3d6 100644
--- a/src/GCodes/GCodes4.cpp
+++ b/src/GCodes/GCodes4.cpp
@@ -208,7 +208,7 @@ void GCodes::RunStateMachine(GCodeBuffer& gb, const StringRef& reply)
if (LockMovementAndWaitForStandstill(gb)) // movement should already be locked, but we need to wait for the previous homing move to complete
{
// Test whether the previous homing move homed any axes
- if (!toBeHomed.Intersects(axesHomed))
+ if (toBeHomed.Disjoint(axesHomed))
{
reply.copy("Homing failed");
error = true;
diff --git a/src/Networking/ESP8266WiFi/WiFiInterface.cpp b/src/Networking/ESP8266WiFi/WiFiInterface.cpp
index da341686..7b7b6534 100644
--- a/src/Networking/ESP8266WiFi/WiFiInterface.cpp
+++ b/src/Networking/ESP8266WiFi/WiFiInterface.cpp
@@ -175,14 +175,15 @@ WiFiInterface::WiFiInterface(Platform& p) noexcept : platform(p), uploader(nullp
constexpr ObjectModelTableEntry WiFiInterface::objectModelTable[] =
{
// These entries must be in alphabetical order
- { "actualIP", OBJECT_MODEL_FUNC(self->ipAddress), ObjectModelEntryFlags::none },
- { "firmwareVersion", OBJECT_MODEL_FUNC_NOSELF(nullptr), ObjectModelEntryFlags::none },
- { "gateway", OBJECT_MODEL_FUNC(self->gateway), ObjectModelEntryFlags::none },
- { "subnet", OBJECT_MODEL_FUNC(self->netmask), ObjectModelEntryFlags::none },
- { "type", OBJECT_MODEL_FUNC_NOSELF("wifi"), ObjectModelEntryFlags::none },
+ { "actualIP", OBJECT_MODEL_FUNC(self->ipAddress), ObjectModelEntryFlags::none },
+ { "firmwareVersion", OBJECT_MODEL_FUNC(self->wiFiServerVersion), ObjectModelEntryFlags::none },
+ { "gateway", OBJECT_MODEL_FUNC(self->gateway), ObjectModelEntryFlags::none },
+ { "mac", OBJECT_MODEL_FUNC(self->macAddress), ObjectModelEntryFlags::none },
+ { "subnet", OBJECT_MODEL_FUNC(self->netmask), ObjectModelEntryFlags::none },
+ { "type", OBJECT_MODEL_FUNC_NOSELF("wifi"), ObjectModelEntryFlags::none },
};
-constexpr uint8_t WiFiInterface::objectModelTableDescriptor[] = { 1, 5 };
+constexpr uint8_t WiFiInterface::objectModelTableDescriptor[] = { 1, 6 };
DEFINE_GET_OBJECT_MODEL_TABLE(WiFiInterface)
@@ -518,12 +519,13 @@ void WiFiInterface::Spin() noexcept
platform.Message(NetworkInfoMessage, "WiFi module started\n");
SetupSpi(); // set up the SPI subsystem
- // Read the status to get the WiFi server version
+ // Read the status to get the WiFi server version and MAC address
Receiver<NetworkStatusResponse> status;
const int32_t rc = SendCommand(NetworkCommand::networkGetStatus, 0, 0, nullptr, 0, status);
if (rc > 0)
{
SafeStrncpy(wiFiServerVersion, status.Value().versionText, ARRAY_SIZE(wiFiServerVersion));
+ macAddress.SetFromBytes(status.Value().macAddress);
// Set the hostname before anything else is done
if (SendCommand(NetworkCommand::networkSetHostName, 0, 0, reprap.GetNetwork().GetHostname(), HostNameLength, nullptr, 0) != ResponseEmpty)
@@ -1106,13 +1108,10 @@ void WiFiInterface::UpdateHostname(const char *hostname) noexcept
}
}
-void WiFiInterface::SetMacAddress(const uint8_t mac[]) noexcept
+GCodeResult WiFiInterface::SetMacAddress(const MacAddress& mac, const StringRef& reply) noexcept
{
- for (size_t i = 0; i < 6; i++)
- {
- macAddress[i] = mac[i];
- }
- // TODO actually update the mac address on the wifi module. For now we don't support this.
+ reply.copy("Not supported on this interface");
+ return GCodeResult::warningNotSupported;
}
void WiFiInterface::InitSockets() noexcept
diff --git a/src/Networking/ESP8266WiFi/WiFiInterface.h b/src/Networking/ESP8266WiFi/WiFiInterface.h
index 647fef05..0f88f81f 100644
--- a/src/Networking/ESP8266WiFi/WiFiInterface.h
+++ b/src/Networking/ESP8266WiFi/WiFiInterface.h
@@ -59,8 +59,8 @@ public:
void UpdateHostname(const char *hostname) noexcept override;
IPAddress GetIPAddress() const noexcept override { return ipAddress; }
void SetIPAddress(IPAddress p_ip, IPAddress p_netmask, IPAddress p_gateway) noexcept override;
- void SetMacAddress(const uint8_t mac[]) noexcept override;
- const uint8_t *GetMacAddress() const noexcept override { return macAddress; }
+ GCodeResult SetMacAddress(const MacAddress& mac, const StringRef& reply) noexcept override;
+ const MacAddress& GetMacAddress() const noexcept override { return macAddress; }
void OpenDataPort(Port port) noexcept override;
void TerminateDataPort() noexcept override;
@@ -145,7 +145,7 @@ private:
IPAddress ipAddress;
IPAddress netmask;
IPAddress gateway;
- uint8_t macAddress[6];
+ MacAddress macAddress;
char requestedSsid[SsidLength + 1];
char actualSsid[SsidLength + 1];
diff --git a/src/Networking/LwipEthernet/LwipEthernetInterface.cpp b/src/Networking/LwipEthernet/LwipEthernetInterface.cpp
index 4fa2dd36..ba6f0f85 100644
--- a/src/Networking/LwipEthernet/LwipEthernetInterface.cpp
+++ b/src/Networking/LwipEthernet/LwipEthernetInterface.cpp
@@ -112,8 +112,8 @@ constexpr ObjectModelTableEntry LwipEthernetInterface::objectModelTable[] =
{
// These entries must be in alphabetical order
{ "actualIP", OBJECT_MODEL_FUNC(self->ipAddress), ObjectModelEntryFlags::none },
- { "firmwareVersion", OBJECT_MODEL_FUNC_NOSELF(nullptr), ObjectModelEntryFlags::none },
{ "gateway", OBJECT_MODEL_FUNC(self->gateway), ObjectModelEntryFlags::none },
+ { "mac", OBJECT_MODEL_FUNC(self->macAddress), ObjectModelEntryFlags::none },
{ "subnet", OBJECT_MODEL_FUNC(self->netmask), ObjectModelEntryFlags::none },
{ "type", OBJECT_MODEL_FUNC_NOSELF("ethernet"), ObjectModelEntryFlags::none },
};
@@ -137,7 +137,7 @@ void LwipEthernetInterface::Init() noexcept
listeningPcbs[i] = nullptr;
}
- memcpy(macAddress, platform.GetDefaultMacAddress(), sizeof(macAddress));
+ macAddress = platform.GetDefaultMacAddress();
}
GCodeResult LwipEthernetInterface::EnableProtocol(NetworkProtocol protocol, int port, int secure, const StringRef& reply) noexcept
@@ -338,7 +338,7 @@ void LwipEthernetInterface::Start() noexcept
const char *hostname = reprap.GetNetwork().GetHostname();
// Allow the MAC address to be set only before LwIP is started...
- ethernet_configure_interface(platform.GetDefaultMacAddress(), hostname);
+ ethernet_configure_interface(platform.GetDefaultMacAddress().bytes, hostname);
init_ethernet(DefaultIpAddress, DefaultNetMask, DefaultGateway);
// Initialise mDNS subsystem
@@ -589,9 +589,10 @@ void LwipEthernetInterface::UpdateHostname(const char *hostname) noexcept
}
}
-void LwipEthernetInterface::SetMacAddress(const uint8_t mac[]) noexcept
+GCodeResult LwipEthernetInterface::SetMacAddress(const MacAddress& mac, const StringRef& reply) noexcept
{
- memcpy(macAddress, mac, sizeof(macAddress));
+ macAddress = mac;
+ return GCodeResult::ok;
}
void LwipEthernetInterface::OpenDataPort(Port port) noexcept
diff --git a/src/Networking/LwipEthernet/LwipEthernetInterface.h b/src/Networking/LwipEthernet/LwipEthernetInterface.h
index d4269cd3..04c57947 100644
--- a/src/Networking/LwipEthernet/LwipEthernetInterface.h
+++ b/src/Networking/LwipEthernet/LwipEthernetInterface.h
@@ -48,8 +48,8 @@ public:
void UpdateHostname(const char *hostname) noexcept override;
IPAddress GetIPAddress() const noexcept override;
void SetIPAddress(IPAddress p_ipAddress, IPAddress p_netmask, IPAddress p_gateway) noexcept override;
- void SetMacAddress(const uint8_t mac[]) noexcept override;
- const uint8_t *GetMacAddress() const noexcept override { return macAddress; }
+ GCodeResult SetMacAddress(const MacAddress& mac, const StringRef& reply) noexcept override;
+ const MacAddress& GetMacAddress() const noexcept override { return macAddress; }
// LwIP interfaces
bool ConnectionEstablished(tcp_pcb *pcb) noexcept;
@@ -105,7 +105,7 @@ private:
IPAddress ipAddress;
IPAddress netmask;
IPAddress gateway;
- uint8_t macAddress[6];
+ MacAddress macAddress;
};
#endif
diff --git a/src/Networking/Network.cpp b/src/Networking/Network.cpp
index 1b72efed..547bc7b6 100644
--- a/src/Networking/Network.cpp
+++ b/src/Networking/Network.cpp
@@ -46,6 +46,23 @@ constexpr size_t NetworkStackWords = 550;
static Task<NetworkStackWords> networkTask;
+// MacAddress members
+uint32_t MacAddress::LowWord() const noexcept
+{
+ return (((((bytes[3] << 8) | bytes[2]) << 8) | bytes[1]) << 8) | bytes[0];
+}
+
+uint16_t MacAddress::HighWord() const noexcept
+{
+ return (bytes[5] << 8) | bytes[4];
+}
+
+void MacAddress::SetFromBytes(const uint8_t mb[6]) noexcept
+{
+ memcpy(bytes, mb, sizeof(bytes));
+}
+
+// Network members
Network::Network(Platform& p) noexcept : platform(p), responders(nullptr), nextResponderToPoll(nullptr)
{
#if defined(DUET3_V03)
@@ -496,15 +513,17 @@ void Network::SetHostname(const char *name) noexcept
}
// Net the MAC address. Pass -1 as the interface number to set the default MAC address for interfaces that don't have one.
-void Network::SetMacAddress(unsigned int interface, const uint8_t mac[]) noexcept
+GCodeResult Network::SetMacAddress(unsigned int interface, const MacAddress& mac, const StringRef& reply) noexcept
{
if (interface < NumNetworkInterfaces)
{
- interfaces[interface]->SetMacAddress(mac);
+ return interfaces[interface]->SetMacAddress(mac, reply);
}
+ reply.copy("unknown interface ");
+ return GCodeResult::error;
}
-const uint8_t *Network::GetMacAddress(unsigned int interface) const noexcept
+const MacAddress& Network::GetMacAddress(unsigned int interface) const noexcept
{
if (interface >= NumNetworkInterfaces)
{
diff --git a/src/Networking/Network.h b/src/Networking/Network.h
index d37ca894..f0e0d5ae 100644
--- a/src/Networking/Network.h
+++ b/src/Networking/Network.h
@@ -81,8 +81,8 @@ public:
IPAddress GetIPAddress(unsigned int interface) const noexcept;
const char *GetHostname() const noexcept { return hostname; }
void SetHostname(const char *name) noexcept;
- void SetMacAddress(unsigned int interface, const uint8_t mac[]) noexcept;
- const uint8_t *GetMacAddress(unsigned int interface) const noexcept;
+ GCodeResult SetMacAddress(unsigned int interface, const MacAddress& mac, const StringRef& reply) noexcept;
+ const MacAddress& GetMacAddress(unsigned int interface) const noexcept;
bool FindResponder(Socket *skt, NetworkProtocol protocol) noexcept;
diff --git a/src/Networking/NetworkDefs.h b/src/Networking/NetworkDefs.h
index 3a578c81..1624cda4 100644
--- a/src/Networking/NetworkDefs.h
+++ b/src/Networking/NetworkDefs.h
@@ -19,7 +19,20 @@ typedef uint8_t SocketNumber;
typedef uint16_t Port;
typedef uint8_t NetworkProtocol;
-constexpr uint8_t DefaultMacAddress[6] = { 0xBE, 0xEF, 0xDE, 0xAD, 0xFE, 0xED };
+struct MacAddress
+{
+public:
+ uint8_t bytes[6];
+
+ uint32_t LowWord() const noexcept;
+ uint16_t HighWord() const noexcept;
+
+ void SetFromBytes(const uint8_t mb[6]) noexcept;
+ void SetDefault() noexcept { SetFromBytes(defaultBytes); }
+
+private:
+ static constexpr uint8_t defaultBytes[6] = { 0xBE, 0xEF, 0xDE, 0xAD, 0xFE, 0xED };
+};
constexpr IPAddress DefaultIpAddress; // will be initialised to 0 by constructor
constexpr IPAddress DefaultNetMask(0xFFFFFF00); // equivalent to 255.255.255.0. Use constexpr constructor to avoid it being allocated in RAM.
diff --git a/src/Networking/NetworkInterface.h b/src/Networking/NetworkInterface.h
index 3168c296..bfd88c86 100644
--- a/src/Networking/NetworkInterface.h
+++ b/src/Networking/NetworkInterface.h
@@ -33,8 +33,8 @@ public:
virtual IPAddress GetIPAddress() const noexcept = 0;
virtual void SetIPAddress(IPAddress p_ipAddress, IPAddress p_netmask, IPAddress p_gateway) noexcept = 0;
- virtual void SetMacAddress(const uint8_t mac[]) noexcept = 0;
- virtual const uint8_t *GetMacAddress() const noexcept = 0;
+ virtual GCodeResult SetMacAddress(const MacAddress& mac, const StringRef& reply) noexcept = 0;
+ virtual const MacAddress& GetMacAddress() const noexcept = 0;
virtual void UpdateHostname(const char *hostname) noexcept = 0;
diff --git a/src/Networking/W5500Ethernet/W5500Interface.cpp b/src/Networking/W5500Ethernet/W5500Interface.cpp
index b4d38aba..2299d72b 100644
--- a/src/Networking/W5500Ethernet/W5500Interface.cpp
+++ b/src/Networking/W5500Ethernet/W5500Interface.cpp
@@ -50,8 +50,8 @@ constexpr ObjectModelTableEntry W5500Interface::objectModelTable[] =
{
// These entries must be in alphabetical order
{ "actualIP", OBJECT_MODEL_FUNC(self->ipAddress), ObjectModelEntryFlags::none },
- { "firmwareVersion", OBJECT_MODEL_FUNC_NOSELF(nullptr), ObjectModelEntryFlags::none },
{ "gateway", OBJECT_MODEL_FUNC(self->gateway), ObjectModelEntryFlags::none },
+ { "mac", OBJECT_MODEL_FUNC(self->macAddress), ObjectModelEntryFlags::none },
{ "subnet", OBJECT_MODEL_FUNC(self->netmask), ObjectModelEntryFlags::none },
{ "type", OBJECT_MODEL_FUNC_NOSELF("ethernet"), ObjectModelEntryFlags::none },
};
@@ -71,7 +71,7 @@ void W5500Interface::Init() noexcept
lastTickMillis = millis();
SetIPAddress(DefaultIpAddress, DefaultNetMask, DefaultGateway);
- memcpy(macAddress, platform.GetDefaultMacAddress(), sizeof(macAddress));
+ macAddress = platform.GetDefaultMacAddress();
}
GCodeResult W5500Interface::EnableProtocol(NetworkProtocol protocol, int port, int secure, const StringRef& reply) noexcept
@@ -196,12 +196,10 @@ GCodeResult W5500Interface::GetNetworkState(const StringRef& reply) noexcept
}
// Update the MAC address
-void W5500Interface::SetMacAddress(const uint8_t mac[]) noexcept
+GCodeResult W5500Interface::SetMacAddress(const MacAddress& mac, const StringRef& reply) noexcept
{
- for (size_t i = 0; i < 6; i++)
- {
- macAddress[i] = mac[i];
- }
+ macAddress = mac;
+ return GCodeResult::ok;
}
// Start up the network
@@ -224,7 +222,7 @@ void W5500Interface::Start() noexcept
setPHYCFGR(PHYCFGR_OPMD | PHYCFGR_OPMDC_ALLA); // set auto negotiation and reset the PHY
wizchip_init(bufSizes, bufSizes);
- setSHAR(macAddress);
+ setSHAR(macAddress.bytes);
setSIPR(ipAddress);
setGAR(gateway);
setSUBR(netmask);
diff --git a/src/Networking/W5500Ethernet/W5500Interface.h b/src/Networking/W5500Ethernet/W5500Interface.h
index 3fcdc789..4f83bbfb 100644
--- a/src/Networking/W5500Ethernet/W5500Interface.h
+++ b/src/Networking/W5500Ethernet/W5500Interface.h
@@ -53,8 +53,8 @@ public:
void UpdateHostname(const char *name) noexcept override;
IPAddress GetIPAddress() const noexcept override { return ipAddress; }
void SetIPAddress(IPAddress p_ipAddress, IPAddress p_netmask, IPAddress p_gateway) noexcept override;
- void SetMacAddress(const uint8_t mac[]) noexcept override;
- const uint8_t *GetMacAddress() const noexcept override { return macAddress; }
+ GCodeResult SetMacAddress(const MacAddress& mac, const StringRef& reply) noexcept override;
+ const MacAddress& GetMacAddress() const noexcept override { return macAddress; }
void OpenDataPort(Port port) noexcept override;
void TerminateDataPort() noexcept override;
@@ -102,7 +102,7 @@ private:
IPAddress ipAddress;
IPAddress netmask;
IPAddress gateway;
- uint8_t macAddress[6];
+ MacAddress macAddress;
};
#endif
diff --git a/src/ObjectModel/ObjectModel.cpp b/src/ObjectModel/ObjectModel.cpp
index 7628ed0a..8e3d4f72 100644
--- a/src/ObjectModel/ObjectModel.cpp
+++ b/src/ObjectModel/ObjectModel.cpp
@@ -14,6 +14,10 @@
#include <cstring>
#include <General/SafeStrtod.h>
+ExpressionValue::ExpressionValue(const MacAddress& mac) noexcept : type(TYPE_OF(MacAddress)), param(mac.HighWord()), uVal(mac.LowWord())
+{
+}
+
void ObjectExplorationContext::AddIndex(int32_t index)
{
if (numIndicesCounted == MaxIndices)
@@ -425,6 +429,12 @@ void ObjectModel::ReportItemAsJson(OutputBuffer *buf, ObjectExplorationContext&
#endif
break;
+ case TYPE_OF(MacAddress):
+ buf->catf("%02x:%02x:%02x:%02x:%02x:%02x",
+ (unsigned int)(val.uVal & 0xFF), (unsigned int)((val.uVal >> 8) & 0xFF), (unsigned int)((val.uVal >> 16) & 0xFF), (unsigned int)((val.uVal >> 24) & 0xFF),
+ (unsigned int)(val.param & 0xFF), (unsigned int)((val.param >> 8) & 0xFF));
+ break;
+
case NoType:
buf->cat("null");
break;
@@ -656,6 +666,13 @@ ExpressionValue ObjectModel::GetObjectValue(const StringParser& sp, ObjectExplor
}
return ExpressionValue((int32_t)val.uVal);
+ case TYPE_OF(MacAddress):
+ if (*idString == 0)
+ {
+ return (context.WantArrayLength()) ? ExpressionValue((int32_t)17) : val;
+ }
+ break;
+
case TYPE_OF(const char*):
if (*idString == 0 && context.WantArrayLength())
{
diff --git a/src/ObjectModel/ObjectModel.h b/src/ObjectModel/ObjectModel.h
index 9a18ffe6..54d007b6 100644
--- a/src/ObjectModel/ObjectModel.h
+++ b/src/ObjectModel/ObjectModel.h
@@ -16,6 +16,7 @@
#include <General/IPAddress.h>
#include <General/Bitmap.h>
#include <RTOSIface/RTOSIface.h>
+#include <Networking/NetworkDefs.h>
typedef uint8_t TypeCode;
constexpr TypeCode NoType = 0; // code for an invalid or unknown type
@@ -54,6 +55,7 @@ template<> constexpr TypeCode TypeOf<IPAddress> () noexcept { return 12; }
template<> constexpr TypeCode TypeOf<const ObjectModelArrayDescriptor*> () noexcept { return 13; }
template<> constexpr TypeCode TypeOf<DateTime> () noexcept { return 14; }
template<> constexpr TypeCode TypeOf<DriverId> () noexcept { return 15; }
+template<> constexpr TypeCode TypeOf<MacAddress> () noexcept { return 16; }
#define TYPE_OF(_t) (TypeOf<_t>())
@@ -97,6 +99,7 @@ struct ExpressionValue
explicit ExpressionValue(Bitmap<uint16_t> bm) noexcept : type(TYPE_OF(Bitmap<uint16_t>)), param(0), uVal(bm.GetRaw()) { }
explicit ExpressionValue(Bitmap<uint32_t> bm) noexcept : type(TYPE_OF(Bitmap<uint32_t>)), param(0), uVal(bm.GetRaw()) { }
explicit ExpressionValue(Bitmap<uint64_t> bm) noexcept : type(TYPE_OF(Bitmap<uint64_t>)), param(bm.GetRaw() >> 32), uVal((uint32_t)bm.GetRaw()) { }
+ explicit ExpressionValue(const MacAddress& mac) noexcept;
void Set(bool b) noexcept { type = TYPE_OF(bool); bVal = b; }
void Set(char c) noexcept { type = TYPE_OF(char); cVal = c; }
diff --git a/src/Pccb/Network.cpp b/src/Pccb/Network.cpp
index b9626666..72cdb7e1 100644
--- a/src/Pccb/Network.cpp
+++ b/src/Pccb/Network.cpp
@@ -3,6 +3,29 @@
const char * const notSupportedText = "Networking is not supported on this hardware";
+// MacAddress members
+uint32_t MacAddress::LowWord() const noexcept
+{
+ return (((((bytes[3] << 8) | bytes[2]) << 8) | bytes[1]) << 8) | bytes[0];
+}
+
+uint16_t MacAddress::HighWord() const noexcept
+{
+ return (bytes[5] << 8) | bytes[4];
+}
+
+void MacAddress::SetFromBytes(const uint8_t mb[6]) noexcept
+{
+ memcpy(bytes, mb, sizeof(bytes));
+}
+
+// Network members
+GCodeResult Network::SetMacAddress(unsigned int interface, const MacAddress& mac, const StringRef& reply) noexcept
+{
+ reply.copy(notSupportedText);
+ return GCodeResult::error;
+}
+
GCodeResult Network::EnableProtocol(unsigned int interface, int protocol, int port, bool secure, const StringRef& reply) noexcept
{
reply.copy(notSupportedText);
diff --git a/src/Pccb/Network.h b/src/Pccb/Network.h
index abffc851..4901699f 100644
--- a/src/Pccb/Network.h
+++ b/src/Pccb/Network.h
@@ -5,20 +5,13 @@
#include "MessageType.h"
#include "GCodes/GCodeResult.h"
#include "General/IPAddress.h"
-
-const IPAddress DefaultIpAddress;
-const IPAddress DefaultNetMask;
-const IPAddress DefaultGateway;
-const uint8_t macAddress[6] = { 0, 0, 0, 0, 0, 0 };
-
-const uint8_t DefaultMacAddress[6] = { 0, 0, 0, 0, 0, 0 };
-const size_t SsidBufferLength = 32; // maximum characters in an SSID
+#include <Networking/NetworkDefs.h>
// The main network class that drives the network.
class Network
{
public:
- Network(Platform& p) noexcept { }
+ Network(Platform& p) noexcept { macAddress.SetDefault(); }
void Init() const noexcept { }
void Activate() const noexcept { }
void Exit() const noexcept { }
@@ -32,8 +25,8 @@ public:
GCodeResult GetNetworkState(unsigned int interface, const StringRef& reply) noexcept;
void SetEthernetIPAddress(IPAddress p_ipAddress, IPAddress p_netmask, IPAddress p_gateway) noexcept { }
- void SetMacAddress(unsigned int interface, const uint8_t mac[]) noexcept { }
- const uint8_t *GetMacAddress(unsigned int interface) const noexcept { return macAddress; }
+ GCodeResult SetMacAddress(unsigned int interface, const MacAddress& mac, const StringRef& reply) noexcept;
+ const MacAddress& GetMacAddress(unsigned int interface) const noexcept { return macAddress; }
void SetHostname(const char *name) const noexcept { }
bool IsWiFiInterface(unsigned int interface) const noexcept { return false; }
@@ -43,6 +36,9 @@ public:
void HandleHttpGCodeReply(OutputBuffer *buf) noexcept;
void HandleTelnetGCodeReply(OutputBuffer *buf) noexcept;
uint32_t GetHttpReplySeq() noexcept { return 0; }
+
+private:
+ MacAddress macAddress;
};
#endif
diff --git a/src/Platform.cpp b/src/Platform.cpp
index eee1e2c6..634be5dd 100644
--- a/src/Platform.cpp
+++ b/src/Platform.cpp
@@ -368,17 +368,17 @@ void Platform::Init() noexcept
// On the Duet Ethernet and SAM E70, use the unique chip ID as most of the MAC address.
// The unique ID is 128 bits long whereas the whole MAC address is only 48 bits,
// so we can't guarantee that each Duet will get a unique MAC address this way.
- memset(defaultMacAddress, 0, sizeof(defaultMacAddress));
- defaultMacAddress[0] = 0xBE; // use a fixed first byte with the locally-administered bit set
+ memset(defaultMacAddress.bytes, 0, sizeof(defaultMacAddress.bytes));
+ defaultMacAddress.bytes[0] = 0xBE; // use a fixed first byte with the locally-administered bit set
const uint8_t * const idBytes = reinterpret_cast<const uint8_t *>(uniqueId);
for (size_t i = 0; i < 15; ++i)
{
- defaultMacAddress[(i % 5) + 1] ^= idBytes[i];
+ defaultMacAddress.bytes[(i % 5) + 1] ^= idBytes[i];
}
}
else
{
- ARRAY_INIT(defaultMacAddress, DefaultMacAddress);
+ defaultMacAddress.SetDefault();
}
#endif
@@ -1062,7 +1062,7 @@ void Platform::Spin() noexcept
else if (openLoadATimer.IsRunning())
{
notOpenLoadADrivers |= mask;
- if (!openLoadADrivers.Intersects(~notOpenLoadADrivers))
+ if (openLoadADrivers.Disjoint(~notOpenLoadADrivers))
{
openLoadATimer.Stop();
}
@@ -1081,7 +1081,7 @@ void Platform::Spin() noexcept
else if (openLoadBTimer.IsRunning())
{
notOpenLoadBDrivers |= mask;
- if (!openLoadBDrivers.Intersects(~notOpenLoadBDrivers))
+ if (openLoadBDrivers.Disjoint(~notOpenLoadBDrivers))
{
openLoadBTimer.Stop();
}
@@ -1090,7 +1090,7 @@ void Platform::Spin() noexcept
# if HAS_STALL_DETECT
if ((stat & TMC_RR_SG) != 0)
{
- if (!stalledDrivers.Intersects(mask))
+ if (stalledDrivers.Disjoint(mask))
{
// This stall is new so check whether we need to perform some action in response to the stall
if (rehomeOnStallDrivers.Intersects(mask))
diff --git a/src/Platform.h b/src/Platform.h
index 220ddd28..7e9682c6 100644
--- a/src/Platform.h
+++ b/src/Platform.h
@@ -308,7 +308,7 @@ public:
const char *GetBoardShortName() const;
#endif
- const uint8_t *GetDefaultMacAddress() const noexcept { return defaultMacAddress; }
+ const MacAddress& GetDefaultMacAddress() const noexcept { return defaultMacAddress; }
// Timing
void Tick() noexcept __attribute__((hot)); // Process a systick interrupt
@@ -591,7 +591,7 @@ private:
IPAddress ipAddress;
IPAddress netMask;
IPAddress gateWay;
- uint8_t defaultMacAddress[6];
+ MacAddress defaultMacAddress;
// Board and processor
#if SAM4E || SAM4S || SAME70
diff --git a/src/Version.h b/src/Version.h
index 902b897b..a729174c 100644
--- a/src/Version.h
+++ b/src/Version.h
@@ -20,7 +20,7 @@
#endif
#ifndef DATE
-# define DATE "2020-01-25b1"
+# define DATE "2020-01-26b1"
#endif
#define AUTHORS "reprappro, dc42, chrishamm, t3p3, dnewman, printm3d"