From 3d4da669553fa0d60a63a7b2891a290c3f935548 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Martin=20J=C3=A4ger?= Date: Fri, 24 Apr 2020 17:54:59 +0200 Subject: [PATCH] Generate DeviceID from hwinfo --- boards/Kconfig | 5 +++ boards/arm/mppt_1210_hus/Kconfig.defconfig | 3 ++ boards/arm/mppt_2420_hpx/Kconfig.defconfig | 3 ++ boards/arm/mppt_2420_lc/Kconfig.defconfig | 3 ++ boards/arm/pwm_2420_lus/Kconfig.defconfig | 3 ++ src/data_nodes.cpp | 42 +++++++++++++++++++--- src/data_nodes.h | 29 +++++++++++++-- src/main.cpp | 2 +- zephyr/prj.conf | 2 ++ 9 files changed, 84 insertions(+), 8 deletions(-) diff --git a/boards/Kconfig b/boards/Kconfig index 8b47c5c..5e0edee 100644 --- a/boards/Kconfig +++ b/boards/Kconfig @@ -24,6 +24,11 @@ config CONTROL_FREQUENCY Frequencies higher than 10 Hz caused issues with MPPT control during lab test with PV simulator. Might be different with actual solar panel. +config LIBRE_SOLAR_TYPE_ID + int + help + Numeric Libre Solar hardware type ID + # # Visible (user-configurable) Kconfig symbols diff --git a/boards/arm/mppt_1210_hus/Kconfig.defconfig b/boards/arm/mppt_1210_hus/Kconfig.defconfig index 209d0cb..0491004 100755 --- a/boards/arm/mppt_1210_hus/Kconfig.defconfig +++ b/boards/arm/mppt_1210_hus/Kconfig.defconfig @@ -6,6 +6,9 @@ if BOARD_MPPT_1210_HUS config BOARD default "mppt_1210_hus" +config LIBRE_SOLAR_TYPE_ID + default 4 + if UART_CONSOLE config UART_2 diff --git a/boards/arm/mppt_2420_hpx/Kconfig.defconfig b/boards/arm/mppt_2420_hpx/Kconfig.defconfig index b4edb40..9a9bd42 100644 --- a/boards/arm/mppt_2420_hpx/Kconfig.defconfig +++ b/boards/arm/mppt_2420_hpx/Kconfig.defconfig @@ -6,6 +6,9 @@ if BOARD_MPPT_2420_HPX config BOARD default "mppt_2420_hpx" +config LIBRE_SOLAR_TYPE_ID + default 7 + if UART_CONSOLE config UART_1 diff --git a/boards/arm/mppt_2420_lc/Kconfig.defconfig b/boards/arm/mppt_2420_lc/Kconfig.defconfig index 423cd15..d354d49 100755 --- a/boards/arm/mppt_2420_lc/Kconfig.defconfig +++ b/boards/arm/mppt_2420_lc/Kconfig.defconfig @@ -6,6 +6,9 @@ if BOARD_MPPT_2420_LC config BOARD default "mppt_2420_lc" +config LIBRE_SOLAR_TYPE_ID + default 1 + if UART_CONSOLE config UART_2 diff --git a/boards/arm/pwm_2420_lus/Kconfig.defconfig b/boards/arm/pwm_2420_lus/Kconfig.defconfig index f6f9eee..0ad0411 100644 --- a/boards/arm/pwm_2420_lus/Kconfig.defconfig +++ b/boards/arm/pwm_2420_lus/Kconfig.defconfig @@ -6,6 +6,9 @@ if BOARD_PWM_2420_LUS config BOARD default "pwm_2420_lus" +config LIBRE_SOLAR_TYPE_ID + default 5 + if UART_CONSOLE config UART_2 diff --git a/src/data_nodes.cpp b/src/data_nodes.cpp index 360450e..357bdb9 100644 --- a/src/data_nodes.cpp +++ b/src/data_nodes.cpp @@ -8,6 +8,8 @@ #ifndef UNIT_TEST #include +#include +#include #endif #include @@ -32,7 +34,7 @@ const char* const device_type = DT_CHARGE_CONTROLLER_PCB_TYPE; const char* const hardware_version = DT_CHARGE_CONTROLLER_PCB_VERSION_STR; const char* const firmware_version = "0.1"; const char* const firmware_commit = COMMIT_HASH; -uint32_t device_id = CONFIG_DEVICE_ID; +char device_id[9]; static char auth_password[11]; @@ -66,7 +68,7 @@ static DataNode data_nodes[] = { TS_NODE_PATH(ID_INFO, "info", 0, NULL), - TS_NODE_UINT32(0x19, "DeviceID", &(device_id), + TS_NODE_STRING(0x19, "DeviceID", device_id, sizeof(device_id), ID_INFO, TS_ANY_R | TS_MKR_W, PUB_NVM), TS_NODE_STRING(0x1A, "Manufacturer", manufacturer, 0, @@ -90,7 +92,7 @@ static DataNode data_nodes[] = { // CONFIGURATION ////////////////////////////////////////////////////////// // using IDs >= 0x30 except for high priority data objects - TS_NODE_PATH(ID_CONF, "conf", 0, &data_objects_update_conf), + TS_NODE_PATH(ID_CONF, "conf", 0, &data_nodes_update_conf), // battery settings @@ -441,7 +443,7 @@ static DataNode data_nodes[] = { ThingSet ts(data_nodes, sizeof(data_nodes)/sizeof(DataNode)); -void data_objects_update_conf() +void data_nodes_update_conf() { bool changed; if (battery_conf_check(&bat_conf_user)) { @@ -464,8 +466,18 @@ void data_objects_update_conf() eeprom_store_data(); } -void data_objects_read_eeprom() +void data_nodes_init() { +#ifndef UNIT_TEST + uint8_t buf[12]; + hwinfo_get_device_id(buf, sizeof(buf)); + + uint64_t id64 = crc32_ieee(buf, sizeof(buf)); + id64 += ((uint64_t)CONFIG_LIBRE_SOLAR_TYPE_ID) << 32; + + uint64_to_base32(id64, device_id, sizeof(device_id), alphabet_crockford); +#endif + eeprom_restore_data(); if (battery_conf_check(&bat_conf_user)) { battery_conf_overwrite(&bat_conf_user, &bat_conf, &charger); @@ -498,4 +510,24 @@ void thingset_auth() } } +void uint64_to_base32(uint64_t in, char *out, size_t size, const char *alphabet) +{ + // 13 is the maximum number of characters needed to encode 64-bit variable to base32 + int len = (size > 13) ? 13 : size; + + // find out actual length of output string + for (int i = 0; i < len; i++) { + if ((in >> (i * 5)) == 0) { + len = i; + break; + } + } + + for (int i = 0; i < len; i++) { + out[len-i-1] = alphabet[(in >> (i * 5)) % 32]; + } + out[len] = '\0'; +} + + #endif /* CUSTOM_DATA_NODES_FILE */ diff --git a/src/data_nodes.h b/src/data_nodes.h index 8300038..7a0f840 100644 --- a/src/data_nodes.h +++ b/src/data_nodes.h @@ -12,6 +12,9 @@ * @brief Handling of ThingSet data nodes */ +#include +#include + /* * Categories / first layer node IDs */ @@ -46,9 +49,31 @@ extern bool pub_serial_enable; extern bool pub_can_enable; -void data_objects_update_conf(); -void data_objects_read_eeprom(); +/** + * Callback function to be called when conf values were changed + */ +void data_nodes_update_conf(); +/** + * Initializes and reads data nodes from EEPROM + */ +void data_nodes_init(); + +/** + * Callback to provide authentication mechanism via ThingSet + */ void thingset_auth(); +/** + * Alphabet used for base32 encoding + * + * https://en.wikipedia.org/wiki/Base32#Crockford's_Base32 + */ +const char alphabet_crockford[] = "0123456789abcdefghjkmnpqrstvwxyz"; + +/** + * Convert numeric device ID to base32 string + */ +void uint64_to_base32(uint64_t in, char *out, size_t size, const char *alphabet); + #endif /* DATA_OBJECTS_H */ diff --git a/src/main.cpp b/src/main.cpp index bd92b26..978b7be 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -42,7 +42,7 @@ void main(void) battery_conf_overwrite(&bat_conf, &bat_conf_user); // initialize conf_user with same values // read custom configuration from EEPROM - data_objects_read_eeprom(); + data_nodes_init(); // Data Acquisition (DAQ) setup daq_setup(); diff --git a/zephyr/prj.conf b/zephyr/prj.conf index 70e72db..a253218 100644 --- a/zephyr/prj.conf +++ b/zephyr/prj.conf @@ -23,6 +23,8 @@ CONFIG_CONSOLE_GETCHAR=y CONFIG_CPLUSPLUS=y +CONFIG_HWINFO=y + CONFIG_BUILD_OUTPUT_BIN=y # ThingSet protocol interface at UART serial