From a4daa772c3186cdf9ba0283d937f49f48b11646a Mon Sep 17 00:00:00 2001 From: cyberman54 Date: Sun, 27 Dec 2020 00:36:23 +0100 Subject: [PATCH 1/2] generalize hash() function --- include/hash.h | 2 +- src/hash.cpp | 4 ++++ src/macsniff.cpp | 2 +- 3 files changed, 6 insertions(+), 2 deletions(-) diff --git a/include/hash.h b/include/hash.h index b86e7b2e..2caa6133 100644 --- a/include/hash.h +++ b/include/hash.h @@ -4,6 +4,6 @@ #include #include -uint32_t IRAM_ATTR rokkit(const char *data, int len); +uint32_t IRAM_ATTR hash(const char *data, int len); #endif \ No newline at end of file diff --git a/src/hash.cpp b/src/hash.cpp index e5d268f9..137e98a5 100644 --- a/src/hash.cpp +++ b/src/hash.cpp @@ -85,3 +85,7 @@ uint32_t IRAM_ATTR rokkit(const char *data, int len) { return hash; } + +uint32_t IRAM_ATTR hash(const char *data, int len) { + return rokkit(data, len); +} diff --git a/src/macsniff.cpp b/src/macsniff.cpp index 6fefc07c..3e5e8210 100644 --- a/src/macsniff.cpp +++ b/src/macsniff.cpp @@ -141,7 +141,7 @@ uint16_t mac_analyze(MacBuffer_t MacBuffer) { snprintf(buff, sizeof(buff), "%08X", *mac + (uint32_t)salt); // convert unsigned 32-bit salted MAC // to 8 digit hex string - uint16_t hashedmac = rokkit(&buff[3], 5); // hash MAC 8 digit -> 5 digit + uint16_t hashedmac = hash(&buff[3], 5); // hash MAC 8 digit -> 5 digit auto newmac = macs.insert(hashedmac); // add hashed MAC, if new unique bool added = newmac.second ? true : false; // true if hashed MAC is unique in container From 1a2f9e04394c926b217767b9867e9193b7ebb887 Mon Sep 17 00:00:00 2001 From: cyberman54 Date: Sun, 27 Dec 2020 00:40:07 +0100 Subject: [PATCH 2/2] mqttclient.cpp: hash mqtt client name --- include/mqttclient.h | 2 +- src/mqttclient.cpp | 5 ++++- 2 files changed, 5 insertions(+), 2 deletions(-) diff --git a/include/mqttclient.h b/include/mqttclient.h index 6e351e2a..5e595cca 100644 --- a/include/mqttclient.h +++ b/include/mqttclient.h @@ -17,7 +17,7 @@ #define MQTT_KEEPALIVE 10 // keep alive interval in seconds #ifndef MQTT_CLIENTNAME -#define MQTT_CLIENTNAME clientId.c_str() +#define MQTT_CLIENTNAME clientId #endif extern TaskHandle_t mqttTask; diff --git a/src/mqttclient.cpp b/src/mqttclient.cpp index 8e1d5aec..6dd5a89a 100644 --- a/src/mqttclient.cpp +++ b/src/mqttclient.cpp @@ -41,7 +41,10 @@ esp_err_t mqtt_init(void) { int mqtt_connect(const char *my_host, const uint16_t my_port) { IPAddress mqtt_server_ip; - static String clientId = "paxcounter-" + ETH.macAddress(); + const uint16_t hashed = hash(Ð.macAddress()[0], 4); // hash MAC to 4 digits + char clientId[16]; + snprintf(clientId, 16, "paxcounter_%d", hashed); + ESP_LOGI(TAG, "MQTT name is %s", MQTT_CLIENTNAME); // resolve server host name