From 074ad55604f5d5678a29cdf69cd4232156ad585b Mon Sep 17 00:00:00 2001 From: fvanroie <15969459+fvanroie@users.noreply.github.com> Date: Sun, 25 Apr 2021 02:13:41 +0200 Subject: [PATCH] Add get_hardware_id --- src/dev/device.h | 7 +++++-- src/dev/esp32/esp32.cpp | 24 ++++++++++++++++++++++++ src/dev/esp32/esp32.h | 12 ++++-------- src/dev/esp8266/esp8266.cpp | 25 +++++++++++++++++++++++++ src/dev/esp8266/esp8266.h | 12 +++--------- src/dev/posix/hasp_posix.cpp | 8 ++++++++ src/dev/posix/hasp_posix.h | 1 + src/dev/stm32f4/stm32f4.cpp | 9 +++++++++ src/dev/stm32f4/stm32f4.h | 2 ++ src/dev/win32/hasp_win32.cpp | 6 ++++++ src/dev/win32/hasp_win32.h | 1 + 11 files changed, 88 insertions(+), 19 deletions(-) diff --git a/src/dev/device.h b/src/dev/device.h index 2cca34c7..94a8f5ac 100644 --- a/src/dev/device.h +++ b/src/dev/device.h @@ -41,8 +41,11 @@ class BaseDevice { { return ""; } - const char* get_model(); - + virtual const char* get_model(); + virtual const char* get_hardware_id() + { + return ""; + } virtual void init() {} virtual void show_info() diff --git a/src/dev/esp32/esp32.cpp b/src/dev/esp32/esp32.cpp index 66fe647f..e1ce1fda 100644 --- a/src/dev/esp32/esp32.cpp +++ b/src/dev/esp32/esp32.cpp @@ -5,6 +5,7 @@ #include "Arduino.h" #include +#include #include "esp_system.h" #include "hasp_conf.h" @@ -21,6 +22,24 @@ namespace dev { +Esp32Device::Esp32Device() +{ + _hostname = MQTT_NODENAME; + _backlight_invert = (TFT_BACKLIGHT_ON == LOW); + _backlight_power = 1; + _backlight_level = 255; + _backlight_pin = TFT_BCKL; + + /* fill unique identifier with wifi mac */ + byte mac[6]; + WiFi.macAddress(mac); + _hardware_id.reserve(13); + for(int i = 0; i < 6; ++i) { + if(mac[i] < 0x10) _hardware_id += "0"; + _hardware_id += String(mac[i], HEX).c_str(); + } +} + void Esp32Device::reboot() { ESP.restart(); @@ -76,6 +95,11 @@ const char* Esp32Device::get_chip_model() // model += chip_info.revision; } +const char* Esp32Device::get_hardware_id() +{ + return _hardware_id.c_str(); +} + void Esp32Device::set_backlight_pin(uint8_t pin) { _backlight_pin = pin; diff --git a/src/dev/esp32/esp32.h b/src/dev/esp32/esp32.h index 4eaff04f..968e22fd 100644 --- a/src/dev/esp32/esp32.h +++ b/src/dev/esp32/esp32.h @@ -14,14 +14,8 @@ namespace dev { class Esp32Device : public BaseDevice { public: - Esp32Device() - { - _hostname = MQTT_NODENAME; - _backlight_invert = (TFT_BACKLIGHT_ON == LOW); - _backlight_power = 1; - _backlight_level = 255; - _backlight_pin = TFT_BCKL; - } + Esp32Device(); + void reboot() override; void show_info() override; @@ -29,6 +23,7 @@ class Esp32Device : public BaseDevice { void set_hostname(const char*); const char* get_core_version(); const char* get_chip_model(); + const char* get_hardware_id(); void set_backlight_pin(uint8_t pin) override; void set_backlight_level(uint8_t val) override; @@ -45,6 +40,7 @@ class Esp32Device : public BaseDevice { private: std::string _hostname; + std::string _hardware_id; uint8_t _backlight_pin; uint8_t _backlight_level; diff --git a/src/dev/esp8266/esp8266.cpp b/src/dev/esp8266/esp8266.cpp index e685c5c4..0fc52514 100644 --- a/src/dev/esp8266/esp8266.cpp +++ b/src/dev/esp8266/esp8266.cpp @@ -5,6 +5,7 @@ #include "Arduino.h" #include +#include #include "esp8266.h" @@ -15,6 +16,25 @@ namespace dev { +Esp8266Device::Esp8266Device() +{ + _hostname = MQTT_NODENAME; + _backlight_invert = (TFT_BACKLIGHT_ON == LOW); + _backlight_power = 1; + _backlight_level = 255; + _core_version = ESP.getCoreVersion().c_str(); + _backlight_pin = TFT_BCKL; + + /* fill unique identifier with wifi mac */ + byte mac[6]; + WiFi.macAddress(mac); + _hardware_id.reserve(13); + for(int i = 0; i < 6; ++i) { + if(mac[i] < 0x10) _hardware_id += "0"; + _hardware_id += String(mac[i], HEX).c_str(); + } +} + void Esp8266Device::reboot() { ESP.restart(); @@ -46,6 +66,11 @@ const char* Esp8266Device::get_chip_model() return "ESP8266"; } +const char* Esp8266Device::get_hardware_id() +{ + return _hardware_id.c_str(); +} + void Esp8266Device::set_backlight_pin(uint8_t pin) { _backlight_pin = pin; diff --git a/src/dev/esp8266/esp8266.h b/src/dev/esp8266/esp8266.h index 2880ed7e..ee829fd0 100644 --- a/src/dev/esp8266/esp8266.h +++ b/src/dev/esp8266/esp8266.h @@ -14,15 +14,7 @@ namespace dev { class Esp8266Device : public BaseDevice { public: - Esp8266Device() - { - _hostname = MQTT_NODENAME; - _backlight_invert = (TFT_BACKLIGHT_ON == LOW); - _backlight_power = 1; - _backlight_level = 255; - _core_version = ESP.getCoreVersion().c_str(); - _backlight_pin = TFT_BCKL; - } + Esp8266Device(); void reboot() override; void show_info() override; @@ -31,6 +23,7 @@ class Esp8266Device : public BaseDevice { void set_hostname(const char*); const char* get_core_version(); const char* get_chip_model(); + const char* get_hardware_id(); void set_backlight_pin(uint8_t pin) override; void set_backlight_level(uint8_t val) override; @@ -47,6 +40,7 @@ class Esp8266Device : public BaseDevice { private: std::string _hostname; + std::string _hardware_id; std::string _core_version; uint8_t _backlight_pin; diff --git a/src/dev/posix/hasp_posix.cpp b/src/dev/posix/hasp_posix.cpp index 08e2b553..84024437 100644 --- a/src/dev/posix/hasp_posix.cpp +++ b/src/dev/posix/hasp_posix.cpp @@ -66,21 +66,29 @@ const char* PosixDevice::get_hostname() { return _hostname.c_str(); } + void PosixDevice::set_hostname(const char* hostname) { _hostname = hostname; monitor_title(hostname); // SDL_SetWindowTitle(monitor.window, hostname); } + const char* PosixDevice::get_core_version() { return _core_version.c_str(); } + const char* PosixDevice::get_chip_model() { return _chip_model.c_str(); } +const char* PosixDevice::get_hardware_id() +{ + return "223344556677"; +} + void PosixDevice::set_backlight_pin(uint8_t pin) { // PosixDevice::backlight_pin = pin; diff --git a/src/dev/posix/hasp_posix.h b/src/dev/posix/hasp_posix.h index 3a47edc8..589bd16e 100644 --- a/src/dev/posix/hasp_posix.h +++ b/src/dev/posix/hasp_posix.h @@ -38,6 +38,7 @@ class PosixDevice : public BaseDevice { void set_hostname(const char*); const char* get_core_version(); const char* get_chip_model(); + const char* get_hardware_id(); void set_backlight_pin(uint8_t pin); void set_backlight_level(uint8_t val); diff --git a/src/dev/stm32f4/stm32f4.cpp b/src/dev/stm32f4/stm32f4.cpp index ae73b238..8a1a84ac 100644 --- a/src/dev/stm32f4/stm32f4.cpp +++ b/src/dev/stm32f4/stm32f4.cpp @@ -30,6 +30,7 @@ const char* Stm32f4Device::get_hostname() { return _hostname.c_str(); } + void Stm32f4Device::set_hostname(const char* hostname) { _hostname = hostname; @@ -40,6 +41,14 @@ const char* Stm32f4Device::get_core_version() // return ESP.getCoreVersion().c_str(); } +const char* Stm32f4Device::get_hardware_id() +{ + // https://stm32duinoforum.com/forum/viewtopic_f_29_t_2909_start_10.html + // Serial.println("UID [HEX] : "+String(*(uint32_t*)(UID_BASE), HEX)+" "+String(*(uint32_t*)(UID_BASE+0x04), + // HEX)+" "+String(*(uint32_t*)(UID_BASE+0x08), HEX)); + return _hardware_id.c_str(); +} + void Stm32f4Device::set_backlight_pin(uint8_t pin) { _backlight_pin = pin; diff --git a/src/dev/stm32f4/stm32f4.h b/src/dev/stm32f4/stm32f4.h index 35c1bd88..0dc65f45 100644 --- a/src/dev/stm32f4/stm32f4.h +++ b/src/dev/stm32f4/stm32f4.h @@ -30,6 +30,7 @@ class Stm32f4Device : public BaseDevice { void set_hostname(const char*); const char* get_core_version(); const char* get_chip_model(); + const char* get_chip_hardware_id(); void set_backlight_pin(uint8_t pin) override; void set_backlight_level(uint8_t val) override; @@ -46,6 +47,7 @@ class Stm32f4Device : public BaseDevice { private: std::string _hostname; + std::string _hardware_id; uint8_t _backlight_pin; uint8_t _backlight_level; diff --git a/src/dev/win32/hasp_win32.cpp b/src/dev/win32/hasp_win32.cpp index b6917d08..a69fd4a3 100644 --- a/src/dev/win32/hasp_win32.cpp +++ b/src/dev/win32/hasp_win32.cpp @@ -55,11 +55,17 @@ const char* Win32Device::get_core_version() { return _core_version.c_str(); } + const char* Win32Device::get_chip_model() { return "SDL2"; } +const char* Win32Device::get_hardware_id() +{ + return "112233445566"; +} + void Win32Device::set_backlight_pin(uint8_t pin) { // Win32Device::_backlight_pin = pin; diff --git a/src/dev/win32/hasp_win32.h b/src/dev/win32/hasp_win32.h index 67a727b1..a222b39e 100644 --- a/src/dev/win32/hasp_win32.h +++ b/src/dev/win32/hasp_win32.h @@ -59,6 +59,7 @@ class Win32Device : public BaseDevice { void set_hostname(const char*); const char* get_core_version(); const char* get_chip_model(); + const char* get_hardware_id(); void set_backlight_pin(uint8_t pin); void set_backlight_level(uint8_t val);