Compare commits

14 Commits

Author SHA1 Message Date
6431b3ba5f fix 2021-03-25 12:19:14 +01:00
7f17d9ddff commit store 2021-03-25 12:11:49 +01:00
ae347139ab fix 2021-03-25 11:24:14 +01:00
07e8b947b5 static variables 2021-03-25 11:22:57 +01:00
51122a0705 version 2021-03-25 11:04:50 +01:00
cb96432b90 set version 2021-03-25 11:03:05 +01:00
2a4c89aee6 app_version 2021-03-25 11:02:22 +01:00
1a03fdb9da version 2021-03-25 10:58:51 +01:00
44696c4d97 one more char replaced 2021-03-25 10:44:33 +01:00
7464d741d6 fix 2021-03-25 10:34:34 +01:00
073c003446 avoid some chararcters 2021-03-25 10:27:16 +01:00
14a0ff8810 one-off 2021-03-25 10:07:07 +01:00
271bcb6d91 sharedSecret 2021-03-25 10:01:16 +01:00
9d5c7cfe84 device id 2021-03-25 09:17:04 +01:00
7 changed files with 73 additions and 31 deletions

1
.gitignore vendored
View File

@ -1,3 +1,4 @@
src/build/
src/main/version.c
config.csv
config.bin

6
readme.md Normal file
View File

@ -0,0 +1,6 @@
Generate configuration binary:
~/esp/esp-idf/components/nvs_flash/nvs_partition_generator/nvs_partition_gen.py generate --version 1 config.csv config.bin 0x6000
Flash configuration binary:
esptool -p /dev/ttyUSB0 write_flash 0x9000 ../config.bin

View File

@ -5,4 +5,5 @@ idf_component_register(SRCS "app_main.c"
"timesync.c"
"sha256.c"
"sinkSender.c"
"version.c"
INCLUDE_DIRS ".")

View File

@ -18,7 +18,7 @@
#include "timesync.h"
#include "sinkSender.h"
const char VERSION[] = "deadbeef";
extern const uint32_t APP_VERSION;
// static const char *TAG = "app";

View File

@ -23,9 +23,6 @@ static const uint64_t QUEUE_MARKER = UINT64_MAX;
xQueueHandle minuteBufferQueue = NULL;
static t_minuteBuffer minuteBuffer;
static uint32_t secondOfMinute;
static bool settled = false;
static void counterSecondTask(void *arg) {
while (1) {
@ -35,9 +32,14 @@ static void counterSecondTask(void *arg) {
}
static void counterZeroCrossingAveragerTask(void *arg) {
static uint32_t secondOfMinute = 0;
static bool settled = false;
static t_minuteBuffer minuteBuffer;
uint64_t counterCnt = 0;
uint64_t counterSum = 0;
uint64_t savedCounterValue = 0;
while (1) {
uint64_t currentCounterValue;
xQueueReceive(zeroCrossingQueue, &currentCounterValue, portMAX_DELAY);
@ -45,7 +47,8 @@ static void counterZeroCrossingAveragerTask(void *arg) {
if (counterCnt > 0) {
uint32_t counterSecondAverage = ((uint32_t)(counterSum)) / ((uint32_t)(counterCnt));
bool timeInSync = timesyncReady();
ESP_LOGI(TAG, "%d %u %u %u", timeInSync, (uint32_t)counterCnt, (uint32_t)counterSum, counterSecondAverage);
ESP_LOGI(TAG, "%d %u %u %u %u", timeInSync, secondOfMinute, (uint32_t)counterCnt,
(uint32_t)counterSum, counterSecondAverage);
if (timeInSync) {
uint32_t frequency = PRECISION * COUNTER_FREQUENCY / counterSecondAverage;
@ -119,9 +122,6 @@ void counterInit() {
zeroCrossingQueue = xQueueCreate(20, sizeof(uint64_t));
minuteBufferQueue = xQueueCreate(5, sizeof(t_minuteBuffer));
settled = false;
secondOfMinute = 0;
xTaskCreate(counterSecondTask, "counter_second_task", 2048, NULL, 5, NULL);
xTaskCreate(counterZeroCrossingAveragerTask, "counter_averager_task", 2048, NULL, 5, NULL);
xTaskCreate(counterZeroCrossingAveragerTask, "counter_averager_task", 4096, NULL, 5, NULL);
}

View File

@ -20,8 +20,6 @@
static const char *TAG = "ss";
extern char VERSION[];
static const char SINK_SERVER[] = "sink.hottis.de";
static const uint16_t SINK_PORT = 20169;
@ -31,6 +29,7 @@ static char deviceId[16];
static const char DEFAULT_SHAREDSECRET[] = "1234567890123456789012345678901";
static char sharedSecret[32];
extern const uint32_t APP_VERSION;
extern xQueueHandle minuteBufferQueue;
@ -41,7 +40,7 @@ static void sinksenderSend(t_minuteBuffer *minuteBuffer) {
minuteBuffer->s.totalRunningHours = (uint32_t) uptime;
minuteBuffer->s.totalPowercycles = 0;
minuteBuffer->s.totalWatchdogResets = 0;
minuteBuffer->s.version = strtoll(VERSION, NULL, 16);
minuteBuffer->s.version = APP_VERSION;
memset(minuteBuffer->s.deviceId, 0, sizeof(minuteBuffer->s.deviceId));
strcpy(minuteBuffer->s.deviceId, deviceId);
@ -104,39 +103,64 @@ void sinksenderInit() {
ESP_LOGI(TAG, "Initializing sink sender");
ESP_LOGI(TAG, "About to load sink sender configuration");
uint8_t macAddress[6];
if (ESP_OK == esp_efuse_mac_get_default(macAddress)) {
sprintf(deviceId, "%02x%02x%02x%02x%02x%02x",
macAddress[0], macAddress[1], macAddress[2],
macAddress[3], macAddress[4], macAddress[5]);
} else {
ESP_LOGE(TAG, "can not read mac address, use default device id");
strcpy(deviceId, DEFAULT_DEVICE_ID);
}
nvs_handle_t nvsHandle;
if (ESP_OK != nvs_open("sink", NVS_READWRITE, &nvsHandle)) {
ESP_LOGE(TAG, "Unable to open nvs namespace sink, use default values");
strcpy(deviceId, DEFAULT_DEVICE_ID);
strcpy(sharedSecret, DEFAULT_SHAREDSECRET);
} else {
size_t s;
esp_err_t err;
err = nvs_get_str(nvsHandle, "deviceId", NULL, &s);
ESP_LOGI(TAG, "1. err: %d, len: %d", err, s);
err = nvs_get_str(nvsHandle, "deviceId", deviceId, &s);
ESP_LOGI(TAG, "2. err: %d, len: %d", err, s);
if (err == ESP_OK) {
ESP_LOGI(TAG, "deviceId: %s", deviceId);
} else {
strcpy(deviceId, DEFAULT_DEVICE_ID);
ESP_LOGI(TAG, "deviceId not configured, use default");
}
err = nvs_get_str(nvsHandle, "sharedSecret", NULL, &s);
ESP_LOGI(TAG, "1. err: %d, len: %d", err, s);
err = nvs_get_str(nvsHandle, "sharedSecret", sharedSecret, &s);
ESP_LOGI(TAG, "2. err: %d, len: %d", err, s);
if (err == ESP_OK) {
ESP_LOGI(TAG, "sharedSecret: %s", sharedSecret);
if (err != ESP_OK) {
ESP_LOGI(TAG, "sharedSecret not configured, create one");
memset(sharedSecret, 0, sizeof(sharedSecret));
esp_fill_random(sharedSecret, sizeof(sharedSecret)-1);
for (uint8_t i = 0; i < sizeof(sharedSecret)-1; i++) {
sharedSecret[i] = (sharedSecret[i] % 90) + 33; // 0 .. 255 -> 33 .. 122
if (sharedSecret[i] == 34) { // "
sharedSecret[i] = 123; // {
}
if (sharedSecret[i] == 39) { // '
sharedSecret[i] = 124; // |
}
if (sharedSecret[i] == 92) { // }
sharedSecret[i] = 125; //
}
if (sharedSecret[i] == 96) { // `
sharedSecret[i] = 126; // ~
}
}
ESP_LOGI(TAG, "generated sharedSecret is %s", sharedSecret);
if (ESP_OK == nvs_set_str(nvsHandle, "sharedSecret", sharedSecret)) {
ESP_LOGI(TAG, "new sharedSecret stored");
if (ESP_OK == nvs_commit(nvsHandle)) {
ESP_LOGI(TAG, "config store committed");
} else {
strcpy(sharedSecret, DEFAULT_SHAREDSECRET);
ESP_LOGI(TAG, "sharedSecret not configured, use default");
ESP_LOGE(TAG, "unable to commit config store");
}
} else {
ESP_LOGE(TAG, "unable to store sharedSecret");
}
}
}
ESP_LOGI(TAG, "finally deviceId: %s", deviceId);
ESP_LOGI(TAG, "finally sharedSecret: %s", sharedSecret);
ESP_LOGI(TAG, "finally sharedSecret: [masked]");
nvs_close(nvsHandle);
xTaskCreate(sinksenderExecTask, "sinksender_exec_task", 4096, NULL, 5, NULL);
}

10
tools/setVersion.sh Executable file
View File

@ -0,0 +1,10 @@
#!/bin/bash
VERSION=`git rev-parse --short=8 HEAD`
cat - > ./src/main/version.c <<EOF
#include <stdint.h>
const uint32_t APP_VERSION = 0x${VERSION};
EOF