This commit is contained in:
Wolfgang Hottgenroth 2021-02-08 13:13:05 +01:00
parent 388d7fa7ab
commit f2a3d2edc4
Signed by: wn
GPG Key ID: 6C1E5E531E0D5D7F
5 changed files with 56 additions and 10 deletions

View File

@ -5,7 +5,7 @@
#include <spi.h> #include <spi.h>
#include <assert.h> #include <assert.h>
#define CONFIG_MAGIC 0xdead0005 #define CONFIG_MAGIC 0xdead0006
typedef struct __attribute__((__packed__)) s_configBlock { typedef struct __attribute__((__packed__)) s_configBlock {
uint32_t configMagic; uint32_t configMagic;
@ -15,7 +15,8 @@ typedef struct __attribute__((__packed__)) s_configBlock {
char deviceId[16]; char deviceId[16];
char sharedSecret[32]; char sharedSecret[32];
char location[48]; char location[48];
uint8_t filler[22]; char sinkServer[48];
uint8_t filler[6];
} t_configBlock; } t_configBlock;

View File

@ -15,6 +15,7 @@ t_configBlock defaultConfigBlock = {
.deviceId = "MainsCnt01", .deviceId = "MainsCnt01",
.sharedSecret = "sharedSecretGanzGeheim", .sharedSecret = "sharedSecretGanzGeheim",
.location = "Essen, DE", .location = "Essen, DE",
.sinkServer = "laborpc",
.filler = { 0 } .filler = { 0 }
}; };

View File

@ -1,4 +1,5 @@
#include <stdint.h> #include <stdint.h>
#include <string.h>
#include <tim.h> #include <tim.h>
@ -8,6 +9,7 @@
#include <PontCoopScheduler.h> #include <PontCoopScheduler.h>
#include <wizHelper.h> #include <wizHelper.h>
#include <config.h> #include <config.h>
#include <socket.h>
const uint32_t COUNTER_FREQUENCY = 1.0e6; const uint32_t COUNTER_FREQUENCY = 1.0e6;
@ -21,15 +23,16 @@ volatile static uint32_t mainsCntCnt = 0;
static t_seconds *seconds; static t_seconds *seconds;
typedef struct { typedef struct __attribute__((__packed__)) {
uint64_t timestamp; uint64_t timestamp;
uint32_t frequency; uint32_t frequency;
} t_event; } t_event;
#define SECONDS_PER_MINUTE 60 #define SECONDS_PER_MINUTE 60
typedef struct { typedef struct __attribute__((__packed__)) {
char deviceId[sizeof(((t_configBlock*)0)->location)]; char deviceId[sizeof(((t_configBlock*)0)->deviceId)];
char location[sizeof(((t_configBlock*)0)->location)];
uint8_t done; uint8_t done;
t_event events[SECONDS_PER_MINUTE]; t_event events[SECONDS_PER_MINUTE];
} t_minuteStruct; } t_minuteStruct;
@ -43,6 +46,35 @@ typedef union {
t_minuteBuffer minuteBuffers[NUM_OF_MINUTE_BUFFERS]; t_minuteBuffer minuteBuffers[NUM_OF_MINUTE_BUFFERS];
uint8_t activeMinuteBuffer = 0; uint8_t activeMinuteBuffer = 0;
static t_configBlock *config;
extern uint8_t SINK_SOCK;
const uint16_t SINK_PORT = 20169;
int8_t counterSendMinuteBuffer(t_minuteBuffer *minuteBuffer) {
uint8_t sinkAddr[4];
if (! wizDnsQuery(config->sinkServer, sinkAddr) {
coloredMsg(LOG_BLUE, "csmb, failed to resolve sink server name");
return -1;
} else {
coloredMsg(LOG_BLUE, "csmb, sink server at %d.%d.%d.%d", sinkAddr[0], sinkAddr[1], sinkAddr[2], sinkAddr[3]);
}
socket(SINK_SOCK, Sn_MR_UDP, SINK_PORT, 0);
uint8_t sockState = getSn_SR(SINK_SOCK);
if (sockState == SOCK_UDP) {
sendto(SINK_SOCK, minuteBuffer->b, sizeof(minuteBuffer->b), sinkAddr, SINK_PORT);
coloredMsg(LOG_BLUE, "csmb, sent");
} else {
coloredMsg(LOG_BLUE, "csmb, socket in unexpected state: %d", sockState);
return -2;
}
close(SINK_SOCK);
return 1;
}
void counterMinuteTick(void *handle) { void counterMinuteTick(void *handle) {
for (uint8_t minuteBufferIdx = 0; minuteBufferIdx < NUM_OF_MINUTE_BUFFERS; minuteBufferIdx++) { for (uint8_t minuteBufferIdx = 0; minuteBufferIdx < NUM_OF_MINUTE_BUFFERS; minuteBufferIdx++) {
@ -50,10 +82,19 @@ void counterMinuteTick(void *handle) {
if (minuteBuffer->s.done == 1) { if (minuteBuffer->s.done == 1) {
coloredMsg(LOG_BLUE, "cmt, buffer %d is done, handle it", minuteBufferIdx); coloredMsg(LOG_BLUE, "cmt, buffer %d is done, handle it", minuteBufferIdx);
for (uint8_t i = 0; i < SECONDS_PER_MINUTE; i++) { memset(minuteBuffer->s.deviceId, 0, sizeof(minuteBuffer->s.deviceId));
coloredMsg(LOG_BLUE, "cmt: freq: %lu, time: %lu", minuteBuffer->s.events[i].frequency, minuteBuffer->s.events[i].timestamp); strcpy(minuteBuffer->s.deviceId, config->deviceId);
memset(minuteBuffer->s.location, 0, sizeof(minuteBuffer->s.location));
strcpy(minuteBuffer->s.location, config->location);
int8_t res = counterSendMinuteBuffer(minuteBuffer);
if (res == 1) {
coloredMsg(LOG_BLUE, "cmt, successfully sent");
minuteBuffer->s.done = 0;
} else {
coloredMsg(LOG_BLUE, "cmt, not successfully sent, try again");
schAdd(counterMinuteTick, NULL, 1000, 0);
} }
minuteBuffer->s.done = 0;
} }
} }
} }
@ -93,6 +134,7 @@ void counterSecondTick(void *handle) {
if (activeMinuteBuffer >= NUM_OF_MINUTE_BUFFERS) { if (activeMinuteBuffer >= NUM_OF_MINUTE_BUFFERS) {
activeMinuteBuffer = 0; activeMinuteBuffer = 0;
} }
schAdd(counterMinuteTick, NULL, 1000, 0);
} }
} }
} }
@ -123,8 +165,9 @@ void counterInit() {
minuteBuffers[0].s.done = 0; minuteBuffers[0].s.done = 0;
minuteBuffers[1].s.done = 0; minuteBuffers[1].s.done = 0;
config = getConfig();
schAdd(counterSecondTick, NULL, 0, 1000); schAdd(counterSecondTick, NULL, 0, 1000);
schAdd(counterMinuteTick, NULL, 5, 60 * 1000);
HAL_TIM_IC_Start_IT(&mainsCnt, TIM_CHANNEL_1); HAL_TIM_IC_Start_IT(&mainsCnt, TIM_CHANNEL_1);
} }

View File

@ -8,3 +8,4 @@ const uint8_t DHCP_SOCK = 0;
const uint8_t SNTP_SOCK = 1; const uint8_t SNTP_SOCK = 1;
const uint8_t CMD_SOCK = 2; const uint8_t CMD_SOCK = 2;
const uint8_t DNS_SOCK = 3; const uint8_t DNS_SOCK = 3;
const uint8_t SINK_SOCK = 4;

View File

@ -178,7 +178,7 @@ static void wizSNTPHandler(void *handle) {
} }
} }
uint8_t tryAgainIn = (success) ? 3600 : 1; uint16_t tryAgainIn = (success) ? 3600 : 1;
schAdd(wizSNTPHandler, NULL, tryAgainIn * 1000, 0); schAdd(wizSNTPHandler, NULL, tryAgainIn * 1000, 0);
coloredMsg(LOG_BLUE, "wizsh, next sntp request in %d seconds", tryAgainIn); coloredMsg(LOG_BLUE, "wizsh, next sntp request in %d seconds", tryAgainIn);
} }