|
|
|
@ -1,4 +1,5 @@
|
|
|
|
|
#include <stdint.h>
|
|
|
|
|
#include <string.h>
|
|
|
|
|
|
|
|
|
|
#include <tim.h>
|
|
|
|
|
|
|
|
|
@ -8,6 +9,7 @@
|
|
|
|
|
#include <PontCoopScheduler.h>
|
|
|
|
|
#include <wizHelper.h>
|
|
|
|
|
#include <config.h>
|
|
|
|
|
#include <socket.h>
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
const uint32_t COUNTER_FREQUENCY = 1.0e6;
|
|
|
|
@ -21,15 +23,16 @@ volatile static uint32_t mainsCntCnt = 0;
|
|
|
|
|
|
|
|
|
|
static t_seconds *seconds;
|
|
|
|
|
|
|
|
|
|
typedef struct {
|
|
|
|
|
typedef struct __attribute__((__packed__)) {
|
|
|
|
|
uint64_t timestamp;
|
|
|
|
|
uint32_t frequency;
|
|
|
|
|
} t_event;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
#define SECONDS_PER_MINUTE 60
|
|
|
|
|
typedef struct {
|
|
|
|
|
char deviceId[sizeof(((t_configBlock*)0)->location)];
|
|
|
|
|
typedef struct __attribute__((__packed__)) {
|
|
|
|
|
char deviceId[sizeof(((t_configBlock*)0)->deviceId)];
|
|
|
|
|
char location[sizeof(((t_configBlock*)0)->location)];
|
|
|
|
|
uint8_t done;
|
|
|
|
|
t_event events[SECONDS_PER_MINUTE];
|
|
|
|
|
} t_minuteStruct;
|
|
|
|
@ -43,6 +46,35 @@ typedef union {
|
|
|
|
|
t_minuteBuffer minuteBuffers[NUM_OF_MINUTE_BUFFERS];
|
|
|
|
|
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) {
|
|
|
|
|
for (uint8_t minuteBufferIdx = 0; minuteBufferIdx < NUM_OF_MINUTE_BUFFERS; minuteBufferIdx++) {
|
|
|
|
@ -50,10 +82,19 @@ void counterMinuteTick(void *handle) {
|
|
|
|
|
|
|
|
|
|
if (minuteBuffer->s.done == 1) {
|
|
|
|
|
coloredMsg(LOG_BLUE, "cmt, buffer %d is done, handle it", minuteBufferIdx);
|
|
|
|
|
for (uint8_t i = 0; i < SECONDS_PER_MINUTE; i++) {
|
|
|
|
|
coloredMsg(LOG_BLUE, "cmt: freq: %lu, time: %lu", minuteBuffer->s.events[i].frequency, minuteBuffer->s.events[i].timestamp);
|
|
|
|
|
memset(minuteBuffer->s.deviceId, 0, sizeof(minuteBuffer->s.deviceId));
|
|
|
|
|
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) {
|
|
|
|
|
activeMinuteBuffer = 0;
|
|
|
|
|
}
|
|
|
|
|
schAdd(counterMinuteTick, NULL, 1000, 0);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
@ -123,8 +165,9 @@ void counterInit() {
|
|
|
|
|
minuteBuffers[0].s.done = 0;
|
|
|
|
|
minuteBuffers[1].s.done = 0;
|
|
|
|
|
|
|
|
|
|
config = getConfig();
|
|
|
|
|
|
|
|
|
|
schAdd(counterSecondTick, NULL, 0, 1000);
|
|
|
|
|
schAdd(counterMinuteTick, NULL, 5, 60 * 1000);
|
|
|
|
|
|
|
|
|
|
HAL_TIM_IC_Start_IT(&mainsCnt, TIM_CHANNEL_1);
|
|
|
|
|
}
|
|
|
|
|