From ca6de814cf17a8d6e491947a416453c30b19e624 Mon Sep 17 00:00:00 2001 From: Marvin Roger Date: Sat, 19 Nov 2016 10:19:37 +0100 Subject: [PATCH] :bug: Make hardware device IDs actually unique --- src/Homie/Boot/BootConfig.cpp | 6 ++---- src/Homie/Boot/BootNormal.cpp | 2 +- src/Homie/Utils/DeviceId.cpp | 31 +++++++++++++++---------------- src/Homie/Utils/DeviceId.hpp | 6 ++++-- 4 files changed, 22 insertions(+), 23 deletions(-) diff --git a/src/Homie/Boot/BootConfig.cpp b/src/Homie/Boot/BootConfig.cpp index 6de9dcbe..39a33e91 100644 --- a/src/Homie/Boot/BootConfig.cpp +++ b/src/Homie/Boot/BootConfig.cpp @@ -25,9 +25,7 @@ void BootConfig::setup() { digitalWrite(Interface::get().led.pin, Interface::get().led.on); } - const char* deviceId = DeviceId::get(); - - Interface::get().getLogger() << F("Device ID is ") << deviceId << endl; + Interface::get().getLogger() << F("Device ID is ") << DeviceId::get() << endl; WiFi.mode(WIFI_AP_STA); @@ -37,7 +35,7 @@ void BootConfig::setup() { strcat(apName, DeviceId::get()); WiFi.softAPConfig(ACCESS_POINT_IP, ACCESS_POINT_IP, IPAddress(255, 255, 255, 0)); - WiFi.softAP(apName, deviceId); + WiFi.softAP(apName, DeviceId::get()); Interface::get().getLogger() << F("AP started as ") << apName << endl; _dns.setTTL(30); diff --git a/src/Homie/Boot/BootNormal.cpp b/src/Homie/Boot/BootNormal.cpp index 0332b133..71ad9699 100644 --- a/src/Homie/Boot/BootNormal.cpp +++ b/src/Homie/Boot/BootNormal.cpp @@ -154,7 +154,7 @@ void BootNormal::_onMqttConnected() { Interface::get().getMqttClient().publish(_prefixMqttTopic(PSTR("/$homie")), 1, true, HOMIE_VERSION); Interface::get().getMqttClient().publish(_prefixMqttTopic(PSTR("/$implementation")), 1, true, "esp8266"); - Interface::get().getMqttClient().publish(_prefixMqttTopic(PSTR("/$mac")), 1, true, DeviceId::get()); + Interface::get().getMqttClient().publish(_prefixMqttTopic(PSTR("/$mac")), 1, true, WiFi.macAddress().c_str()); for (HomieNode* iNode : HomieNode::nodes) { std::unique_ptr subtopic = std::unique_ptr(new char[1 + strlen(iNode->getId()) + 12 + 1]); // /id/$properties diff --git a/src/Homie/Utils/DeviceId.cpp b/src/Homie/Utils/DeviceId.cpp index 21f2984c..de0fd584 100644 --- a/src/Homie/Utils/DeviceId.cpp +++ b/src/Homie/Utils/DeviceId.cpp @@ -1,16 +1,15 @@ -#include "DeviceId.hpp" - -using namespace HomieInternals; - -char DeviceId::_deviceId[] = ""; // need to define the static variable - -void DeviceId::generate() { - char flashChipId[6 + 1]; - sprintf(flashChipId, "%06x", ESP.getFlashChipId()); - - sprintf(DeviceId::_deviceId, "%06x%s", ESP.getChipId(), flashChipId + strlen(flashChipId) - 2); -} - -const char* DeviceId::get() { - return DeviceId::_deviceId; -} +#include "DeviceId.hpp" + +using namespace HomieInternals; + +char DeviceId::_deviceId[] = ""; // need to define the static variable + +void DeviceId::generate() { + uint8_t mac[6]; + WiFi.macAddress(mac); + sprintf(DeviceId::_deviceId, "%02x%02x%02x%02x%02x%02x", mac[0], mac[1], mac[2], mac[3], mac[4], mac[5]); +} + +const char* DeviceId::get() { + return DeviceId::_deviceId; +} diff --git a/src/Homie/Utils/DeviceId.hpp b/src/Homie/Utils/DeviceId.hpp index 7d6fc7fc..ae11b5cc 100644 --- a/src/Homie/Utils/DeviceId.hpp +++ b/src/Homie/Utils/DeviceId.hpp @@ -1,6 +1,8 @@ #pragma once -#include "Arduino.h" +#include "Arduino.h" + +#include namespace HomieInternals { class DeviceId { @@ -9,6 +11,6 @@ class DeviceId { static const char* get(); private: - static char _deviceId[8 + 1]; + static char _deviceId[12 + 1]; }; } // namespace HomieInternals