Compare commits
62 Commits
network_ab
...
FirstRelea
Author | SHA1 | Date | |
---|---|---|---|
6611220851 | |||
fe1bc101a3 | |||
daf281eb89
|
|||
fa3df2bb32
|
|||
f310f604a6
|
|||
e9afbbb379
|
|||
e478f00ceb
|
|||
6555208d8c
|
|||
577f1fbd42
|
|||
7dfac6e471
|
|||
4abe77f3c1
|
|||
acd5b2123a
|
|||
3b3d5ee6a6
|
|||
6e73dccd66
|
|||
991f2724a6
|
|||
fc101e30f4
|
|||
e6e1bac2c3
|
|||
3e5310557c
|
|||
cee2116f3a
|
|||
681c5d8695
|
|||
192fa69f68
|
|||
2dc99d2b01
|
|||
ef71142ce3
|
|||
d08fab14fa
|
|||
f04376ddd9
|
|||
45d5f8af9b
|
|||
0cb99a4854
|
|||
444e118e54
|
|||
145109a280 | |||
9fcc43b012
|
|||
b81c9f445f
|
|||
8e84fbe287
|
|||
7addea50a3
|
|||
7154b0abc3
|
|||
897e7039f2
|
|||
1b1180a72f
|
|||
3f024510ae | |||
24a37e9e5f | |||
b6127854e0
|
|||
b272c6a2d0 | |||
685082ae03 | |||
081256eb51 | |||
5e03f5e1a3
|
|||
f220e15afe
|
|||
f16b38ffd5
|
|||
1dd4d35530 | |||
e499c6607b
|
|||
12d8f9a9fe
|
|||
84f58a343e
|
|||
1f124976e7 | |||
9c4a5d7a01 | |||
9ec3d165a4 | |||
f141e8bf0d
|
|||
62a5a1bc70
|
|||
7af317f768
|
|||
d1e54da7aa
|
|||
baaf7bf665
|
|||
901b2fbea4
|
|||
5d8567a206
|
|||
a09024c6a2
|
|||
b6439e7df3
|
|||
f58538ab79
|
1
configmaker/.gitignore
vendored
Normal file
1
configmaker/.gitignore
vendored
Normal file
@ -0,0 +1 @@
|
||||
build/
|
40
configmaker/Makefile
Normal file
40
configmaker/Makefile
Normal file
@ -0,0 +1,40 @@
|
||||
BUILD_DIR = build
|
||||
|
||||
C_SOURCES = \
|
||||
configmaker.c
|
||||
|
||||
|
||||
C_INCLUDES = \
|
||||
-I. \
|
||||
-I../cube/User/Inc
|
||||
|
||||
VERSION := $(shell git rev-parse --short=8 HEAD)
|
||||
|
||||
CC = gcc
|
||||
CFLAGS = $(C_INCLUDES) -Wall -Werror -std=c99 -DVERSION="\"$(VERSION)\""
|
||||
LDFLAGS = -lconfig
|
||||
TARGET = configmaker
|
||||
|
||||
UNAME_S := $(shell uname -s)
|
||||
ifeq ($(UNAME_S),OpenBSD)
|
||||
CFLAGS += -I/usr/local/include -DOpenBSD=1
|
||||
LDFLAGS += -L/usr/local/lib
|
||||
endif
|
||||
|
||||
all: $(BUILD_DIR)/$(TARGET)
|
||||
|
||||
OBJECTS = $(addprefix $(BUILD_DIR)/,$(notdir $(C_SOURCES:.c=.o)))
|
||||
vpath %.c $(sort $(dir $(C_SOURCES)))
|
||||
|
||||
$(BUILD_DIR)/%.o: %.c Makefile | $(BUILD_DIR)
|
||||
$(CC) -c $(CFLAGS) $< -o $@
|
||||
|
||||
$(BUILD_DIR)/$(TARGET): $(OBJECTS) Makefile
|
||||
$(CC) $(OBJECTS) $(LDFLAGS) -o $@
|
||||
|
||||
$(BUILD_DIR):
|
||||
mkdir $@
|
||||
|
||||
.phony: clean
|
||||
clean:
|
||||
-rm -rf $(BUILD_DIR)
|
391
configmaker/configmaker.c
Normal file
391
configmaker/configmaker.c
Normal file
@ -0,0 +1,391 @@
|
||||
#define _DEFAULT_SOURCE
|
||||
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <stdbool.h>
|
||||
#include <string.h>
|
||||
#include <getopt.h>
|
||||
|
||||
#include <libconfig.h>
|
||||
|
||||
#include <sinkStruct.h>
|
||||
#include <sha256.h>
|
||||
|
||||
|
||||
const char DEFAULT_CONFIG_FILENAME[] = "./configmaker.cfg";
|
||||
|
||||
typedef struct {
|
||||
uint32_t eepromMagic;
|
||||
uint32_t configMagic;
|
||||
uint8_t macAddress[6];
|
||||
char deviceName[16];
|
||||
char deviceId[16];
|
||||
uint8_t sharedSecret[32];
|
||||
char ntpServer[48];
|
||||
char sinkServer[48];
|
||||
} t_configHandle;
|
||||
|
||||
t_configHandle configHandle;
|
||||
|
||||
|
||||
int initConfig(const char *configFilename, t_configHandle *configHandle) {
|
||||
config_init(&(configHandle->cfg));
|
||||
if (! config_read_file(&(configHandle->cfg), configFilename)) {
|
||||
logmsg(LOG_ERR, "failed to read config file: %s:%d - %s\n",
|
||||
config_error_file(&(configHandle->cfg)), config_error_line(&(configHandle->cfg)),
|
||||
config_error_text(&(configHandle->cfg)));
|
||||
config_destroy(&(configHandle->cfg));
|
||||
return -1;
|
||||
}
|
||||
|
||||
config_setting_t *devicesConfig = config_lookup(&(configHandle->cfg), "devices");
|
||||
if (devicesConfig == NULL) {
|
||||
logmsg(LOG_ERR, "receiver: no devices configuration found");
|
||||
return -2;
|
||||
}
|
||||
configHandle->numOfDevices = config_setting_length(devicesConfig);
|
||||
configHandle->devices = (t_device*) malloc(configHandle->numOfDevices * sizeof(t_device));
|
||||
for (uint16_t i = 0; i < configHandle->numOfDevices; i++) {
|
||||
config_setting_t *deviceConfig = config_setting_get_elem(devicesConfig, i);
|
||||
if (! config_setting_lookup_string(deviceConfig, "deviceId", &(configHandle->devices[i].deviceId))) {
|
||||
logmsg(LOG_ERR, "no deviceId for device %d", i);
|
||||
return -3;
|
||||
}
|
||||
if (! config_setting_lookup_string(deviceConfig, "location", &(configHandle->devices[i].location))) {
|
||||
logmsg(LOG_ERR, "no location for device %d", i);
|
||||
return -4;
|
||||
}
|
||||
if (! config_setting_lookup_string(deviceConfig, "sharedSecret", &(configHandle->devices[i].sharedSecret))) {
|
||||
logmsg(LOG_ERR, "no sharedSecret for device %d", i);
|
||||
return -5;
|
||||
}
|
||||
if (strlen(configHandle->devices[i].sharedSecret) >= SHA256_BLOCK_SIZE) {
|
||||
logmsg(LOG_ERR, "Configured sharedsecret for device %d is too long", i);
|
||||
return -6;
|
||||
}
|
||||
logmsg(LOG_INFO, "Device loaded: %d %s %s %s", i,
|
||||
configHandle->devices[i].deviceId,
|
||||
configHandle->devices[i].location,
|
||||
configHandle->devices[i].sharedSecret);
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
void deinitConfig(t_configHandle *configHandle) {
|
||||
config_destroy(&(configHandle->cfg));
|
||||
if (configHandle->devices) {
|
||||
free(configHandle->devices);
|
||||
configHandle->devices = NULL;
|
||||
}
|
||||
}
|
||||
|
||||
t_device *findDevice(t_configHandle *configHandle, char *deviceId) {
|
||||
for (uint16_t i = 0; i < configHandle->numOfDevices; i++) {
|
||||
if (! strcmp(configHandle->devices[i].deviceId, deviceId)) {
|
||||
return &(configHandle->devices[i]);
|
||||
}
|
||||
}
|
||||
return NULL;
|
||||
}
|
||||
|
||||
int initReceiver(t_configHandle *configHandle, t_receiverHandle *handle) {
|
||||
handle->configHandle = configHandle;
|
||||
|
||||
struct sockaddr_in servaddr;
|
||||
|
||||
handle->receiveSockFd = socket(AF_INET, SOCK_DGRAM, 0);
|
||||
if (handle->receiveSockFd == -1) {
|
||||
logmsg(LOG_ERR, "failed to create receive socket: %d", errno);
|
||||
return -1;
|
||||
}
|
||||
|
||||
int receivePort = 20169;
|
||||
config_lookup_int(&(configHandle->cfg), "receivePort", &receivePort);
|
||||
if (receivePort < 1 || receivePort > 65535) {
|
||||
logmsg(LOG_ERR, "illegal receive port configured");
|
||||
return -2;
|
||||
}
|
||||
|
||||
memset(&servaddr, 0, sizeof(servaddr));
|
||||
servaddr.sin_family = AF_INET;
|
||||
servaddr.sin_addr.s_addr = htonl(INADDR_ANY);
|
||||
servaddr.sin_port = htons(receivePort);
|
||||
|
||||
if (-1 == bind(handle->receiveSockFd, (const struct sockaddr *) &servaddr, sizeof(servaddr))) {
|
||||
logmsg(LOG_ERR, "unable to bind receive: %d", errno);
|
||||
return -3;
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
void deinitReceiver(t_receiverHandle *handle) {
|
||||
close(handle->receiveSockFd);
|
||||
}
|
||||
|
||||
int receiveAndVerifyMinuteBuffer(t_receiverHandle *handle, t_minuteBuffer *buf) {
|
||||
struct sockaddr_in cliaddr;
|
||||
socklen_t cliaddrlen = sizeof(cliaddr);
|
||||
|
||||
int n = recvfrom(handle->receiveSockFd, buf->b, sizeof(buf->b), MSG_TRUNC,
|
||||
(struct sockaddr *) &cliaddr, &cliaddrlen);
|
||||
logmsg(LOG_INFO, "received %d octets from %d.%d.%d.%d",
|
||||
n,
|
||||
(cliaddr.sin_addr.s_addr & 0x0ff),
|
||||
((cliaddr.sin_addr.s_addr >> 8) & 0x0ff),
|
||||
((cliaddr.sin_addr.s_addr >> 16) & 0x0ff),
|
||||
((cliaddr.sin_addr.s_addr >> 24) & 0x0ff));
|
||||
|
||||
if (n != sizeof(buf->b)) {
|
||||
logmsg(LOG_INFO, "Illegal packet size: %d", n);
|
||||
return -1;
|
||||
}
|
||||
|
||||
t_device *device = findDevice(handle->configHandle, buf->s.deviceId);
|
||||
const char *sharedSecret = device->sharedSecret;
|
||||
|
||||
uint8_t receivedHash[SHA256_BLOCK_SIZE];
|
||||
memcpy(receivedHash, buf->s.hash, SHA256_BLOCK_SIZE);
|
||||
memcpy(buf->s.hash, sharedSecret, SHA256_BLOCK_SIZE);
|
||||
|
||||
SHA256_CTX ctx;
|
||||
uint8_t calculatedHash[SHA256_BLOCK_SIZE];
|
||||
sha256_init(&ctx);
|
||||
sha256_update(&ctx, buf->b, sizeof(buf->b));
|
||||
sha256_final(&ctx, calculatedHash);
|
||||
|
||||
if (memcmp(receivedHash, calculatedHash, SHA256_BLOCK_SIZE) != 0) {
|
||||
logmsg(LOG_INFO, "Invalid hash in msg for device %s", buf->s.deviceId);
|
||||
return -5;
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
||||
int initForwarder(t_configHandle *configHandle, t_forwarderHandle *handle) {
|
||||
handle->configHandle = configHandle;
|
||||
|
||||
handle->influxUser = NULL;
|
||||
handle->influxPass = NULL;
|
||||
handle->influxServer = NULL;
|
||||
handle->influxDatabase = NULL;
|
||||
handle->influxMeasurement = NULL;
|
||||
|
||||
config_lookup_string(&(configHandle->cfg), "influxUser", &(handle->influxUser));
|
||||
config_lookup_string(&(configHandle->cfg), "influxPass", &(handle->influxPass));
|
||||
config_lookup_string(&(configHandle->cfg), "influxServer", &(handle->influxServer));
|
||||
config_lookup_string(&(configHandle->cfg), "influxDatabase", &(handle->influxDatabase));
|
||||
config_lookup_string(&(configHandle->cfg), "influxMeasurement", &(handle->influxMeasurement));
|
||||
|
||||
int influxPort = 8086;
|
||||
config_lookup_int(&(configHandle->cfg), "influxPort", &influxPort);
|
||||
if (influxPort < 1 || influxPort > 65535) {
|
||||
logmsg(LOG_ERR, "illegal influx port configured");
|
||||
return -2;
|
||||
}
|
||||
handle->influxPort = influxPort;
|
||||
|
||||
if (! handle->influxServer) {
|
||||
logmsg(LOG_ERR, "no influxServer configured");
|
||||
return -1;
|
||||
}
|
||||
if (! handle->influxDatabase) {
|
||||
logmsg(LOG_ERR, "no influxDatabase configured");
|
||||
return -2;
|
||||
}
|
||||
if (! handle->influxMeasurement) {
|
||||
logmsg(LOG_ERR, "no influxMeasurement configured");
|
||||
return -3;
|
||||
}
|
||||
|
||||
int res = snprintf(handle->influxUrl, sizeof(handle->influxUrl),
|
||||
"http://%s:%d/write?db=%s&precision=s",
|
||||
handle->influxServer, handle->influxPort, handle->influxDatabase);
|
||||
if (res > sizeof(handle->influxUrl)) {
|
||||
logmsg(LOG_ERR, "influxUrl has not enough space");
|
||||
return -4;
|
||||
}
|
||||
logmsg(LOG_INFO, "influxUrl is %s", handle->influxUrl);
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
void deinitForwarder(t_forwarderHandle *handle) {
|
||||
|
||||
}
|
||||
|
||||
int httpPostRequest(char *url, const char *user, const char *pass, char *payload) {
|
||||
CURL *curl = curl_easy_init();
|
||||
if (! curl) {
|
||||
logmsg(LOG_ERR, "error instantiating curl");
|
||||
return -1;
|
||||
}
|
||||
|
||||
curl_easy_setopt(curl, CURLOPT_URL, url);
|
||||
if (user && pass) {
|
||||
curl_easy_setopt(curl, CURLOPT_HTTPAUTH, CURLAUTH_DIGEST);
|
||||
curl_easy_setopt(curl, CURLOPT_USERNAME, user);
|
||||
curl_easy_setopt(curl, CURLOPT_PASSWORD, pass);
|
||||
}
|
||||
curl_easy_setopt(curl, CURLOPT_POSTFIELDS, payload);
|
||||
|
||||
CURLcode res = curl_easy_perform(curl);
|
||||
if (res != CURLE_OK) {
|
||||
logmsg(LOG_ERR, "post request failed: %s", curl_easy_strerror(res));
|
||||
return -2;
|
||||
}
|
||||
|
||||
curl_easy_cleanup(curl);
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
int forwardMinuteBuffer(t_forwarderHandle *handle, t_minuteBuffer *buf) {
|
||||
logmsg(LOG_INFO, "DeviceId: %s", buf->s.deviceId);
|
||||
t_device *device = findDevice(handle->configHandle, buf->s.deviceId);
|
||||
const char *location = device->location;
|
||||
|
||||
for (uint8_t j = 0; j < SECONDS_PER_MINUTE; j++) {
|
||||
logmsg(LOG_DEBUG, "Time: %lu, Frequency: %u", buf->s.events[j].timestamp, buf->s.events[j].frequency);
|
||||
|
||||
int frequency_before_point = buf->s.events[j].frequency / 1000;
|
||||
int frequency_behind_point = buf->s.events[j].frequency - (frequency_before_point * 1000);
|
||||
|
||||
char payload[256];
|
||||
int res = snprintf(payload, sizeof(payload),
|
||||
"%s,valid=1,location=%s,host=%s freq=%d.%03d"
|
||||
#ifdef OpenBSD
|
||||
" %llu"
|
||||
#else
|
||||
" %lu"
|
||||
#endif
|
||||
"",
|
||||
handle->influxMeasurement, location, buf->s.deviceId,
|
||||
frequency_before_point, frequency_behind_point,
|
||||
buf->s.events[j].timestamp);
|
||||
if (res > sizeof(payload)) {
|
||||
logmsg(LOG_ERR, "payload buffer to small");
|
||||
return -1;
|
||||
}
|
||||
logmsg(LOG_DEBUG, "Payload: %s", payload);
|
||||
res = httpPostRequest(handle->influxUrl, handle->influxUser, handle->influxPass, payload);
|
||||
if (res == 0) {
|
||||
logmsg(LOG_DEBUG, "Successfully sent to InfluxDB");
|
||||
}
|
||||
}
|
||||
|
||||
logmsg(LOG_INFO, "Successfully sent whole minute to InfluxDB");
|
||||
return 0;
|
||||
}
|
||||
|
||||
void usage() {
|
||||
printf("sinkserver for mainsfrequency counter\n");
|
||||
printf("https://home.hottis.de/gitlab/wolutator/mains-frequency-counter-stm32\n");
|
||||
printf("Version: " VERSION "\n");
|
||||
printf("\nUsage\n");
|
||||
printf(" -f FILENAME R..... Config file to be used\n");
|
||||
printf(" -v ............... Verbose, writes all logging on stdout too\n");
|
||||
printf(" -s FACILITY ...... Sets syslog facility, only LOCAL[0..7]\n");
|
||||
printf(" USER and DAEMON are supported\n");
|
||||
printf(" -n USER .......... If started as root drop privileges and become\n");
|
||||
printf(" USER\n");
|
||||
printf(" -b ............... fork into background\n");
|
||||
printf(" -h ............... This help\n");
|
||||
}
|
||||
|
||||
int main(int argc, char **argv) {
|
||||
t_configHandle configHandle;
|
||||
t_forwarderHandle forwarderHandle;
|
||||
t_receiverHandle receiverHandle;
|
||||
|
||||
|
||||
const char *configFilename = DEFAULT_CONFIG_FILENAME;
|
||||
const char *dropPrivilegesToUser = NULL;
|
||||
bool doFork = false;
|
||||
|
||||
int c;
|
||||
while ((c = getopt(argc, argv, "f:vs:hn:b")) != -1) {
|
||||
switch (c) {
|
||||
case 'f':
|
||||
configFilename = strdup(optarg);
|
||||
break;
|
||||
case 'v':
|
||||
verbose = true;
|
||||
break;
|
||||
case 's':
|
||||
setfacility(optarg);
|
||||
break;
|
||||
case 'n':
|
||||
dropPrivilegesToUser = strdup(optarg);
|
||||
break;
|
||||
case 'b':
|
||||
doFork = true;
|
||||
break;
|
||||
case 'h':
|
||||
usage();
|
||||
exit(0);
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
if ((getuid() == 0) && (dropPrivilegesToUser != NULL)) {
|
||||
logmsg(LOG_INFO, "dropping root privileges, become %s", dropPrivilegesToUser);
|
||||
struct passwd *userEntry = getpwnam(dropPrivilegesToUser);
|
||||
if (userEntry == NULL) {
|
||||
logmsg(LOG_ERR, "can not find entry for user %s", dropPrivilegesToUser);
|
||||
exit(1);
|
||||
}
|
||||
|
||||
if (setuid(userEntry->pw_uid) != 0) {
|
||||
logmsg(LOG_ERR, "unable to drop root privileges to %d", userEntry->pw_uid);
|
||||
exit(2);
|
||||
}
|
||||
}
|
||||
|
||||
if (0 != initConfig(configFilename, &configHandle)) {
|
||||
logmsg(LOG_ERR, "error when reading configuration");
|
||||
exit(3);
|
||||
}
|
||||
|
||||
if (doFork) {
|
||||
int pid = fork();
|
||||
if (pid == -1) {
|
||||
logmsg(LOG_ERR, "error when forking into background: %d", errno);
|
||||
exit(4);
|
||||
}
|
||||
if (pid != 0) {
|
||||
logmsg(LOG_INFO, "successfully forking into background, child's pid is %d", pid);
|
||||
exit(0);
|
||||
}
|
||||
}
|
||||
|
||||
if (0 != initReceiver(&configHandle, &receiverHandle)) {
|
||||
logmsg(LOG_ERR, "error when initializing receiver");
|
||||
exit(5);
|
||||
}
|
||||
|
||||
if (0 != initForwarder(&configHandle, &forwarderHandle)) {
|
||||
logmsg(LOG_ERR, "error when initializing forwarder");
|
||||
exit(6);
|
||||
}
|
||||
|
||||
|
||||
while (1) {
|
||||
t_minuteBuffer buf;
|
||||
|
||||
if (receiveAndVerifyMinuteBuffer(&receiverHandle, &buf) < 0) {
|
||||
logmsg(LOG_ERR, "error in receiveAndVerify");
|
||||
continue;
|
||||
}
|
||||
|
||||
if (forwardMinuteBuffer(&forwarderHandle, &buf) < 0) {
|
||||
logmsg(LOG_ERR, "error in send");
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
deinitForwarder(&forwarderHandle);
|
||||
deinitReceiver(&receiverHandle);
|
||||
deinitConfig(&configHandle);
|
||||
}
|
21
configmaker/configmaker.cfg
Normal file
21
configmaker/configmaker.cfg
Normal file
@ -0,0 +1,21 @@
|
||||
eepromMagic = 0xaffe000b;
|
||||
|
||||
configMagic = 0xdead0007;
|
||||
|
||||
macAddress = 0x...........;
|
||||
|
||||
# max 16 octets
|
||||
deviceName = "MainsCnt01";
|
||||
|
||||
# max 16 octets
|
||||
deviceId = "MainsCnt01";
|
||||
|
||||
# exactly 31 octets
|
||||
sharedSecret = "5DVYZoB3TwGoFoLAPF8S8EkgURLPqjY";
|
||||
|
||||
# max 48 octets
|
||||
ntpServer = "pool.ntp.org";
|
||||
|
||||
# max 48 octets
|
||||
sinkServer = "sink.hottis.de";
|
||||
|
@ -26,6 +26,9 @@ ifndef NETWORK
|
||||
$(error NETWORK is not set)
|
||||
endif
|
||||
|
||||
VERSION := $(shell git rev-parse --short=8 HEAD)
|
||||
|
||||
|
||||
######################################
|
||||
# target
|
||||
######################################
|
||||
@ -94,11 +97,14 @@ C_SOURCES += \
|
||||
User/Src/ports.c \
|
||||
User/Src/wizHelper.c \
|
||||
User/Src/networkAbstractionLayer_lan.c
|
||||
NETWORK_STACK=1
|
||||
endif
|
||||
|
||||
ifeq ($(NETWORK), WiFi)
|
||||
C_SOURCES += \
|
||||
User/Src/networkAbstractionLayer_wifi.c
|
||||
User/Src/networkAbstractionLayer_wifi.c \
|
||||
User/Src/modemCom.c
|
||||
NETWORK_STACK=2
|
||||
endif
|
||||
|
||||
# ASM sources
|
||||
@ -178,7 +184,7 @@ endif
|
||||
# compile gcc flags
|
||||
ASFLAGS = $(MCU) $(AS_DEFS) $(AS_INCLUDES) $(OPT) -Wall -Werror -fdata-sections -ffunction-sections
|
||||
|
||||
CFLAGS = $(MCU) $(C_DEFS) $(C_INCLUDES) $(OPT) -DNETWORK=$(NETWORK) -Wall -Werror -fdata-sections -ffunction-sections
|
||||
CFLAGS = $(MCU) $(C_DEFS) $(C_INCLUDES) $(OPT) -DNETWORK_STACK=$(NETWORK_STACK) -DVERSION="\"$(VERSION)\"" -Wall -Werror -fdata-sections -ffunction-sections
|
||||
|
||||
ifeq ($(DEBUG), 1)
|
||||
CFLAGS += -g -gdwarf-2
|
||||
|
@ -1,7 +1,11 @@
|
||||
# Processed by ../tools/insertMyCode.sh
|
||||
##########################################################################################################################
|
||||
# File automatically-generated by tool: [projectgenerator] version: [3.10.0-B14] date: [Sat Feb 13 18:28:59 CET 2021]
|
||||
##########################################################################################################################
|
||||
|
||||
# FILE NOT LONGER UNDER CONTROL OF THE GENERATOR BUT MANUALLY MAINTAINED, 2020-02-16 #
|
||||
|
||||
|
||||
# ------------------------------------------------
|
||||
# Generic Makefile (based on gcc)
|
||||
#
|
||||
@ -10,6 +14,21 @@
|
||||
# 2015-07-22 - first version
|
||||
# ------------------------------------------------
|
||||
|
||||
|
||||
# Network implementations, to be set on commandline
|
||||
# LAN
|
||||
# WiFi
|
||||
# GSM
|
||||
|
||||
# NETWORK = LAN
|
||||
|
||||
ifndef NETWORK
|
||||
$(error NETWORK is not set)
|
||||
endif
|
||||
|
||||
VERSION := $(shell git rev-parse --short=8 HEAD)
|
||||
|
||||
|
||||
######################################
|
||||
# target
|
||||
######################################
|
||||
@ -35,35 +54,61 @@ BUILD_DIR = build
|
||||
# source
|
||||
######################################
|
||||
# C sources
|
||||
C_SOURCES = \
|
||||
Core/Src/main.c \
|
||||
Core/Src/gpio.c \
|
||||
Core/Src/iwdg.c \
|
||||
Core/Src/spi.c \
|
||||
Core/Src/tim.c \
|
||||
Core/Src/usart.c \
|
||||
Core/Src/stm32f1xx_it.c \
|
||||
Core/Src/stm32f1xx_hal_msp.c \
|
||||
Drivers/STM32F1xx_HAL_Driver/Src/stm32f1xx_hal_gpio_ex.c \
|
||||
Drivers/STM32F1xx_HAL_Driver/Src/stm32f1xx_hal_iwdg.c \
|
||||
Drivers/STM32F1xx_HAL_Driver/Src/stm32f1xx_hal.c \
|
||||
Drivers/STM32F1xx_HAL_Driver/Src/stm32f1xx_hal_rcc.c \
|
||||
Drivers/STM32F1xx_HAL_Driver/Src/stm32f1xx_hal_rcc_ex.c \
|
||||
Drivers/STM32F1xx_HAL_Driver/Src/stm32f1xx_hal_gpio.c \
|
||||
Drivers/STM32F1xx_HAL_Driver/Src/stm32f1xx_hal_dma.c \
|
||||
Drivers/STM32F1xx_HAL_Driver/Src/stm32f1xx_hal_cortex.c \
|
||||
Drivers/STM32F1xx_HAL_Driver/Src/stm32f1xx_hal_pwr.c \
|
||||
Drivers/STM32F1xx_HAL_Driver/Src/stm32f1xx_hal_flash.c \
|
||||
Drivers/STM32F1xx_HAL_Driver/Src/stm32f1xx_hal_flash_ex.c \
|
||||
Drivers/STM32F1xx_HAL_Driver/Src/stm32f1xx_hal_exti.c \
|
||||
Drivers/STM32F1xx_HAL_Driver/Src/stm32f1xx_hal_spi.c \
|
||||
Drivers/STM32F1xx_HAL_Driver/Src/stm32f1xx_hal_tim.c \
|
||||
Drivers/STM32F1xx_HAL_Driver/Src/stm32f1xx_hal_tim_ex.c \
|
||||
Drivers/STM32F1xx_HAL_Driver/Src/stm32f1xx_hal_uart.c \
|
||||
C_SOURCES = \
|
||||
User/Src/config.c \
|
||||
User/Src/counter.c \
|
||||
User/Src/eeprom.c \
|
||||
User/Src/logger.c \
|
||||
User/Src/main2.c \
|
||||
User/Src/ringbuffer.c \
|
||||
User/Src/sha256.c \
|
||||
User/Src/show.c \
|
||||
User/Src/utils.c \
|
||||
User/Src/networkAbstractionLayer.c \
|
||||
hottislib/PontCoopScheduler.c \
|
||||
Core/Src/main.c \
|
||||
Core/Src/gpio.c \
|
||||
Core/Src/iwdg.c \
|
||||
Core/Src/spi.c \
|
||||
Core/Src/tim.c \
|
||||
Core/Src/usart.c \
|
||||
Core/Src/stm32f1xx_it.c \
|
||||
Core/Src/stm32f1xx_hal_msp.c \
|
||||
Drivers/STM32F1xx_HAL_Driver/Src/stm32f1xx_hal_gpio_ex.c \
|
||||
Drivers/STM32F1xx_HAL_Driver/Src/stm32f1xx_hal_iwdg.c \
|
||||
Drivers/STM32F1xx_HAL_Driver/Src/stm32f1xx_hal.c \
|
||||
Drivers/STM32F1xx_HAL_Driver/Src/stm32f1xx_hal_rcc.c \
|
||||
Drivers/STM32F1xx_HAL_Driver/Src/stm32f1xx_hal_rcc_ex.c \
|
||||
Drivers/STM32F1xx_HAL_Driver/Src/stm32f1xx_hal_gpio.c \
|
||||
Drivers/STM32F1xx_HAL_Driver/Src/stm32f1xx_hal_dma.c \
|
||||
Drivers/STM32F1xx_HAL_Driver/Src/stm32f1xx_hal_cortex.c \
|
||||
Drivers/STM32F1xx_HAL_Driver/Src/stm32f1xx_hal_pwr.c \
|
||||
Drivers/STM32F1xx_HAL_Driver/Src/stm32f1xx_hal_flash.c \
|
||||
Drivers/STM32F1xx_HAL_Driver/Src/stm32f1xx_hal_flash_ex.c \
|
||||
Drivers/STM32F1xx_HAL_Driver/Src/stm32f1xx_hal_exti.c \
|
||||
Drivers/STM32F1xx_HAL_Driver/Src/stm32f1xx_hal_spi.c \
|
||||
Drivers/STM32F1xx_HAL_Driver/Src/stm32f1xx_hal_tim.c \
|
||||
Drivers/STM32F1xx_HAL_Driver/Src/stm32f1xx_hal_tim_ex.c \
|
||||
Drivers/STM32F1xx_HAL_Driver/Src/stm32f1xx_hal_uart.c \
|
||||
Core/Src/system_stm32f1xx.c
|
||||
|
||||
ifeq ($(NETWORK), LAN)
|
||||
C_SOURCES += \
|
||||
User/Src/ports.c \
|
||||
User/Src/wizHelper.c \
|
||||
User/Src/networkAbstractionLayer_lan.c
|
||||
NETWORK_STACK=1
|
||||
endif
|
||||
|
||||
ifeq ($(NETWORK), WiFi)
|
||||
C_SOURCES += \
|
||||
User/Src/networkAbstractionLayer_wifi.c \
|
||||
User/Src/modemCom.c
|
||||
NETWORK_STACK=2
|
||||
endif
|
||||
|
||||
# ASM sources
|
||||
ASM_SOURCES = \
|
||||
ASM_SOURCES = \
|
||||
startup_stm32f103xb.s
|
||||
|
||||
|
||||
@ -107,8 +152,8 @@ MCU = $(CPU) -mthumb $(FPU) $(FLOAT-ABI)
|
||||
AS_DEFS =
|
||||
|
||||
# C defines
|
||||
C_DEFS = \
|
||||
-DUSE_HAL_DRIVER \
|
||||
C_DEFS = \
|
||||
-DUSE_HAL_DRIVER \
|
||||
-DSTM32F103xB
|
||||
|
||||
|
||||
@ -116,19 +161,30 @@ C_DEFS = \
|
||||
AS_INCLUDES =
|
||||
|
||||
# C includes
|
||||
C_INCLUDES = \
|
||||
-ICore/Inc \
|
||||
-IDrivers/STM32F1xx_HAL_Driver/Inc \
|
||||
-IDrivers/STM32F1xx_HAL_Driver/Inc/Legacy \
|
||||
-IDrivers/CMSIS/Device/ST/STM32F1xx/Include \
|
||||
-IDrivers/CMSIS/Include \
|
||||
C_INCLUDES = \
|
||||
-Ihottislib \
|
||||
-IUser/Inc \
|
||||
-ICore/Inc \
|
||||
-IDrivers/STM32F1xx_HAL_Driver/Inc \
|
||||
-IDrivers/STM32F1xx_HAL_Driver/Inc/Legacy \
|
||||
-IDrivers/CMSIS/Device/ST/STM32F1xx/Include \
|
||||
-IDrivers/CMSIS/Include \
|
||||
-IDrivers/CMSIS/Include
|
||||
|
||||
|
||||
# compile gcc flags
|
||||
ASFLAGS = $(MCU) $(AS_DEFS) $(AS_INCLUDES) $(OPT) -Wall -fdata-sections -ffunction-sections
|
||||
ifeq ($(NETWORK), LAN)
|
||||
C_INCLUDES += \
|
||||
-IioLibrary_Driver/Internet/SNTP \
|
||||
-IioLibrary_Driver/Internet/DNS \
|
||||
-IioLibrary_Driver/Internet/DHCP \
|
||||
-IioLibrary_Driver/Ethernet
|
||||
endif
|
||||
|
||||
CFLAGS = $(MCU) $(C_DEFS) $(C_INCLUDES) $(OPT) -Wall -fdata-sections -ffunction-sections
|
||||
|
||||
# compile gcc flags
|
||||
ASFLAGS = $(MCU) $(AS_DEFS) $(AS_INCLUDES) $(OPT) -Wall -Werror -fdata-sections -ffunction-sections
|
||||
|
||||
CFLAGS = $(MCU) $(C_DEFS) $(C_INCLUDES) $(OPT) -DNETWORK_STACK=$(NETWORK_STACK) -DVERSION="\"$(VERSION)\"" -Wall -Werror -fdata-sections -ffunction-sections
|
||||
|
||||
ifeq ($(DEBUG), 1)
|
||||
CFLAGS += -g -gdwarf-2
|
||||
@ -148,7 +204,7 @@ LDSCRIPT = STM32F103C8Tx_FLASH.ld
|
||||
# libraries
|
||||
LIBS = -lc -lm -lnosys
|
||||
LIBDIR =
|
||||
LDFLAGS = $(MCU) -specs=nano.specs -T$(LDSCRIPT) $(LIBDIR) $(LIBS) -Wl,-Map=$(BUILD_DIR)/$(TARGET).map,--cref -Wl,--gc-sections
|
||||
LDFLAGS = $(MCU) -specs=nano.specs -u _printf_float -T$(LDSCRIPT) $(LIBDIR) $(LIBS) -Wl,-Map=$(BUILD_DIR)/$(TARGET).map,--cref -Wl,--gc-sections
|
||||
|
||||
# default action: build all
|
||||
all: $(BUILD_DIR)/$(TARGET).elf $(BUILD_DIR)/$(TARGET).hex $(BUILD_DIR)/$(TARGET).bin
|
||||
@ -160,10 +216,20 @@ all: $(BUILD_DIR)/$(TARGET).elf $(BUILD_DIR)/$(TARGET).hex $(BUILD_DIR)/$(TARGET
|
||||
# list of objects
|
||||
OBJECTS = $(addprefix $(BUILD_DIR)/,$(notdir $(C_SOURCES:.c=.o)))
|
||||
vpath %.c $(sort $(dir $(C_SOURCES)))
|
||||
|
||||
ifeq ($(NETWORK), LAN)
|
||||
OBJECTS += $(addprefix $(BUILD_DIR)/,w5500.a)
|
||||
endif
|
||||
|
||||
# list of ASM program objects
|
||||
OBJECTS += $(addprefix $(BUILD_DIR)/,$(notdir $(ASM_SOURCES:.s=.o)))
|
||||
vpath %.s $(sort $(dir $(ASM_SOURCES)))
|
||||
|
||||
ifeq ($(NETWORK), LAN)
|
||||
$(BUILD_DIR)/w5500.a:
|
||||
(cd ioLibrary_Driver && $(MAKE) && cp w5500.a ../$(BUILD_DIR) && cd ..)
|
||||
endif
|
||||
|
||||
$(BUILD_DIR)/%.o: %.c Makefile | $(BUILD_DIR)
|
||||
$(CC) -c $(CFLAGS) -Wa,-a,-ad,-alms=$(BUILD_DIR)/$(notdir $(<:.c=.lst)) $< -o $@
|
||||
|
||||
@ -194,4 +260,4 @@ clean:
|
||||
#######################################
|
||||
-include $(wildcard $(BUILD_DIR)/*.d)
|
||||
|
||||
# *** EOF ***
|
||||
# *** EOF ***
|
16
cube/User/Inc/modemCom.h
Normal file
16
cube/User/Inc/modemCom.h
Normal file
@ -0,0 +1,16 @@
|
||||
#ifndef _MODEM_COM_H_
|
||||
#define _MODEM_COM_H_
|
||||
|
||||
#include <usart.h>
|
||||
|
||||
|
||||
|
||||
#if NETWORK_STACK == 2 // WiFi
|
||||
void modemTxCpltCallback(UART_HandleTypeDef *huart);
|
||||
void modemRxCpltCallback(UART_HandleTypeDef *huart);
|
||||
#endif
|
||||
|
||||
|
||||
|
||||
|
||||
#endif // _MODEM_COM_H_
|
@ -17,4 +17,5 @@ t_seconds* networkGetSeconds();
|
||||
int8_t networkSendMinuteBuffer(t_minuteBuffer *minuteBuffer);
|
||||
|
||||
|
||||
|
||||
#endif /* _NETWORK_ABSTRACTION_LAYER_H_ */
|
||||
|
@ -2,9 +2,11 @@
|
||||
#define _NETWORK_ABSTRACTION_LAYER_IMPL_H_
|
||||
|
||||
#include <stdint.h>
|
||||
#include <stdbool.h>
|
||||
|
||||
uint64_t networkSntpQuery();
|
||||
int8_t networkUdpSend(char *hostname, uint16_t port, uint8_t *buf, uint16_t bufLen);
|
||||
void networkImplInit();
|
||||
bool isNetworkAvailable();
|
||||
|
||||
#endif /* _NETWORK_ABSTRACTION_LAYER_IMPL_H_ */
|
||||
|
@ -12,7 +12,7 @@ typedef enum {
|
||||
LED_BLUE
|
||||
} signalPin_t;
|
||||
|
||||
typedef enum { ON, OFF, TOGGLE, BLINK } signalAction_t;
|
||||
typedef enum { ON, OFF, TOGGLE, BLINK, BLINK_FAST } signalAction_t;
|
||||
|
||||
void showInit();
|
||||
void show(signalPin_t signalPin, signalAction_t action);
|
||||
|
@ -12,6 +12,7 @@ typedef struct __attribute__((__packed__)) {
|
||||
uint32_t totalRunningHours;
|
||||
uint32_t totalPowercycles;
|
||||
uint32_t totalWatchdogResets;
|
||||
uint32_t version;
|
||||
uint64_t timestamp;
|
||||
uint32_t frequency[SECONDS_PER_MINUTE];
|
||||
} t_minuteStruct;
|
||||
|
@ -7,10 +7,9 @@
|
||||
|
||||
|
||||
int wizInit();
|
||||
bool isNetworkAvailable();
|
||||
bool wizIsNetworkAvailable();
|
||||
uint8_t* wizGetIPAddress();
|
||||
bool wizDnsQuery(char *name, uint8_t *ip);
|
||||
uint64_t wizSntpQuery();
|
||||
|
||||
|
||||
#endif // _WIZHELPER_H_
|
||||
|
@ -14,8 +14,7 @@ t_configBlock defaultConfigBlock = {
|
||||
.ntpServer = "0.de.pool.ntp.org",
|
||||
.deviceId = "MainsCnt01",
|
||||
.sharedSecret = "sharedSecretGanzGeheim",
|
||||
.sinkServer = "laborpc",
|
||||
.filler = { 0 }
|
||||
.sinkServer = "laborpc"
|
||||
};
|
||||
|
||||
|
||||
|
@ -1,5 +1,6 @@
|
||||
#include <stdint.h>
|
||||
#include <string.h>
|
||||
#include <stdlib.h>
|
||||
|
||||
#include <tim.h>
|
||||
|
||||
@ -47,6 +48,8 @@ void counterMinuteTick(void *handle) {
|
||||
minuteBuffer->s.totalRunningHours = deviceStats->totalRunningHours;
|
||||
minuteBuffer->s.totalPowercycles = deviceStats->totalPowercycles;
|
||||
minuteBuffer->s.totalWatchdogResets = deviceStats->totalWatchdogResets;
|
||||
minuteBuffer->s.version = strtoll(VERSION, NULL, 16);
|
||||
|
||||
|
||||
memset(minuteBuffer->s.deviceId, 0, sizeof(minuteBuffer->s.deviceId));
|
||||
strcpy(minuteBuffer->s.deviceId, config->deviceId);
|
||||
|
@ -17,6 +17,9 @@
|
||||
#include <config.h>
|
||||
#include <counter.h>
|
||||
|
||||
#if NETWORK_STACK == 2
|
||||
#include <modemCom.h>
|
||||
#endif
|
||||
|
||||
|
||||
|
||||
@ -35,6 +38,7 @@ void my_setup_2() {
|
||||
show(LED_RED, OFF);
|
||||
show(LED_GREEN, BLINK);
|
||||
logMsg("Application starting");
|
||||
logMsg("Version: " VERSION);
|
||||
|
||||
eepromInit();
|
||||
|
||||
@ -67,8 +71,24 @@ void HAL_UART_TxCpltCallback(UART_HandleTypeDef *huart) {
|
||||
debugTxCpltCallback(huart);
|
||||
}
|
||||
#endif //LOGGER_OUTPUT_BY_INTERRUPT
|
||||
#if NETWORK_STACK == 2
|
||||
if (huart == &modemUart) {
|
||||
modemTxCpltCallback(huart);
|
||||
}
|
||||
#endif
|
||||
}
|
||||
|
||||
|
||||
void HAL_UART_RxCpltCallback(UART_HandleTypeDef *huart) {
|
||||
#if NETWORK_STACK == 2
|
||||
if (huart == &modemUart) {
|
||||
modemRxCpltCallback(huart);
|
||||
}
|
||||
#endif
|
||||
}
|
||||
|
||||
|
||||
|
||||
void HAL_SPI_TxCpltCallback(SPI_HandleTypeDef *hspi) {
|
||||
if (hspi == &eepromSpi) {
|
||||
eepromSpiTxCpltCallback(hspi);
|
||||
|
12
cube/User/Src/modemCom.c
Normal file
12
cube/User/Src/modemCom.c
Normal file
@ -0,0 +1,12 @@
|
||||
#include <modemCom.h>
|
||||
#include <usart.h>
|
||||
|
||||
|
||||
|
||||
void modemTxCpltCallback(UART_HandleTypeDef *huart) {
|
||||
|
||||
}
|
||||
|
||||
void modemRxCpltCallback(UART_HandleTypeDef *huart) {
|
||||
|
||||
}
|
@ -3,6 +3,7 @@
|
||||
#include <iwdg.h>
|
||||
#include <logger.h>
|
||||
#include <sinkStruct.h>
|
||||
#include <show.h>
|
||||
#include <config.h>
|
||||
|
||||
#include <networkAbstractionLayer_impl.h>
|
||||
@ -14,12 +15,13 @@ static t_seconds seconds = { .seconds = 0, .missedUpdates = 0, .valid = false };
|
||||
static t_configBlock *config;
|
||||
|
||||
|
||||
|
||||
static void networkSecondsHandler(void *handle) {
|
||||
static bool tryAgain = false;
|
||||
|
||||
seconds.seconds += 1;
|
||||
|
||||
if (! seconds.valid) {
|
||||
if (! seconds.valid && isNetworkAvailable()) {
|
||||
coloredMsg(LOG_YELLOW, "nsh, initially querying time");
|
||||
uint64_t tmpSeconds = networkSntpQuery();
|
||||
if (tmpSeconds != 0) {
|
||||
@ -27,17 +29,19 @@ static void networkSecondsHandler(void *handle) {
|
||||
seconds.seconds = tmpSeconds;
|
||||
seconds.missedUpdates = 0;
|
||||
seconds.valid = true;
|
||||
show(LED_GREEN, ON);
|
||||
} else {
|
||||
coloredMsg(LOG_YELLOW, "nsh, failed");
|
||||
seconds.missedUpdates += 1;
|
||||
}
|
||||
} else if (tryAgain || ((seconds.seconds % 3600) == 0)) {
|
||||
} else if (tryAgain || ((seconds.seconds % 300) == 0)) {
|
||||
coloredMsg(LOG_YELLOW, "nsh, periodically querying time");
|
||||
uint64_t tmpSeconds = networkSntpQuery();
|
||||
if (tmpSeconds != 0) {
|
||||
coloredMsg(LOG_YELLOW, "nsh, success, network time is %lu", tmpSeconds);
|
||||
seconds.missedUpdates = 0;
|
||||
tryAgain = false;
|
||||
show(LED_GREEN, ON);
|
||||
if (seconds.seconds != tmpSeconds) {
|
||||
coloredMsg(LOG_YELLOW, "nsh, local time updated");
|
||||
seconds.seconds = tmpSeconds;
|
||||
|
@ -1,16 +1,198 @@
|
||||
#include <stdint.h>
|
||||
#include <string.h>
|
||||
|
||||
#include <networkAbstractionLayer_impl.h>
|
||||
#include <logger.h>
|
||||
#include <PontCoopScheduler.h>
|
||||
|
||||
#include <wizHelper.h>
|
||||
#include <wizchip_conf.h>
|
||||
#include <socket.h>
|
||||
#include <config.h>
|
||||
#include <sntp.h>
|
||||
|
||||
|
||||
static t_configBlock *config;
|
||||
|
||||
|
||||
|
||||
uint64_t networkSntpQuery() {
|
||||
return wizSntpQuery();
|
||||
static const uint64_t UNIX_NTP_EPOCH_DIFF = 2208988800;
|
||||
static const uint16_t NTP_PORT = 123;
|
||||
static const uint16_t MAX_SNTP_RETRIES = 100;
|
||||
extern const uint8_t SNTP_SOCK;
|
||||
|
||||
|
||||
|
||||
const uint8_t SEND_LI_VN_MODE = 0xe3; // LI: unknown (3), VN: 4, Mode: Client (3)
|
||||
typedef struct s_ntpStruct {
|
||||
uint8_t li_vn_mode;
|
||||
uint8_t stratum;
|
||||
uint8_t poll;
|
||||
uint8_t precision;
|
||||
uint32_t rootdelay;
|
||||
uint32_t rootdisp;
|
||||
uint32_t refid;
|
||||
//uint64_t reftime;
|
||||
uint32_t reftime_h;
|
||||
uint32_t reftime_l;
|
||||
//uint64_t org;
|
||||
uint32_t org_h;
|
||||
uint32_t org_l;
|
||||
//uint64_t rec;
|
||||
uint32_t rec_h;
|
||||
uint32_t rec_l;
|
||||
//uint64_t xmt;
|
||||
uint32_t xmt_h;
|
||||
uint32_t xmt_l;
|
||||
} t_ntpStruct;
|
||||
|
||||
typedef union __attribute__((__packed__)) {
|
||||
t_ntpStruct s;
|
||||
uint8_t b[sizeof(t_ntpStruct)];
|
||||
} ntpMsg_t;
|
||||
|
||||
typedef struct {
|
||||
ntpMsg_t ntpMsg;
|
||||
uint8_t ntpAddr[4];
|
||||
uint16_t retryCount;
|
||||
uint64_t seconds;
|
||||
enum {
|
||||
SNTP_STATE_IDLE,
|
||||
SNTP_STATE_START,
|
||||
SNTP_STATE_SEND,
|
||||
SNTP_STATE_RECV,
|
||||
SNTP_STATE_DONE,
|
||||
SNTP_STATE_ERROR
|
||||
} sntpState;
|
||||
} sntpEngineHandle_t;
|
||||
|
||||
sntpEngineHandle_t sntpEngineHandle = {
|
||||
.seconds = 0,
|
||||
.retryCount = 0,
|
||||
.sntpState = SNTP_STATE_IDLE
|
||||
};
|
||||
|
||||
|
||||
void networkSntpEngine(void *handle) {
|
||||
sntpEngineHandle_t *localHandle = (sntpEngineHandle_t*) handle;
|
||||
if (isNetworkAvailable()) {
|
||||
switch (localHandle->sntpState) {
|
||||
case SNTP_STATE_START:
|
||||
coloredMsg(LOG_BLUE, "nes, resolve ntp server");
|
||||
if (! wizDnsQuery(config->ntpServer, localHandle->ntpAddr)) {
|
||||
coloredMsg(LOG_BLUE, "nes, failed to resolve ntp server");
|
||||
localHandle->sntpState = SNTP_STATE_ERROR;
|
||||
} else {
|
||||
localHandle->sntpState = SNTP_STATE_SEND;
|
||||
schAdd(networkSntpEngine, (void*) localHandle, 10, 0);
|
||||
}
|
||||
break;
|
||||
case SNTP_STATE_SEND:
|
||||
coloredMsg(LOG_BLUE, "nes, about to call SNTP");
|
||||
localHandle->retryCount = 0;
|
||||
socket(SNTP_SOCK, Sn_MR_UDP, NTP_PORT, 0);
|
||||
uint8_t sockState = getSn_SR(SNTP_SOCK);
|
||||
if (sockState == SOCK_UDP) {
|
||||
memset(&(localHandle->ntpMsg), 0, sizeof(localHandle->ntpMsg));
|
||||
localHandle->ntpMsg.s.li_vn_mode = SEND_LI_VN_MODE;
|
||||
localHandle->ntpMsg.s.xmt_l = 1;
|
||||
sendto(SNTP_SOCK, localHandle->ntpMsg.b,
|
||||
sizeof(localHandle->ntpMsg.b), localHandle->ntpAddr, NTP_PORT);
|
||||
coloredMsg(LOG_BLUE, "nes, sent");
|
||||
localHandle->sntpState = SNTP_STATE_RECV;
|
||||
schAdd(networkSntpEngine, (void*) localHandle, 100, 0);
|
||||
} else {
|
||||
coloredMsg(LOG_BLUE, "nes, socket in unexpected state %d", sockState);
|
||||
localHandle->sntpState = SNTP_STATE_ERROR;
|
||||
}
|
||||
break;
|
||||
case SNTP_STATE_RECV:
|
||||
coloredMsg(LOG_BLUE, "nes, check receive");
|
||||
localHandle->retryCount += 1;
|
||||
uint16_t recvLen = getSn_RX_RSR(SNTP_SOCK);
|
||||
if (recvLen == 0) {
|
||||
if (localHandle->retryCount > MAX_SNTP_RETRIES) {
|
||||
coloredMsg(LOG_BLUE, "nes, max retry count reached, failed");
|
||||
localHandle->sntpState = SNTP_STATE_ERROR;
|
||||
} else {
|
||||
coloredMsg(LOG_BLUE, "nes, nothing received yet, try again");
|
||||
schAdd(networkSntpEngine, (void*) localHandle, 100, 0);
|
||||
}
|
||||
} else if (recvLen >= sizeof(localHandle->ntpMsg)) {
|
||||
memset(&(localHandle->ntpMsg), 0, sizeof(localHandle->ntpMsg));
|
||||
uint8_t srcAddr[4];
|
||||
uint16_t srcPort;
|
||||
|
||||
recvfrom(SNTP_SOCK, localHandle->ntpMsg.b,
|
||||
sizeof(localHandle->ntpMsg.b), srcAddr, &srcPort);
|
||||
|
||||
|
||||
uint8_t *buf = localHandle->ntpMsg.b;
|
||||
uint8_t x = 0;
|
||||
coloredMsg(LOG_BLUE, "nes, %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x",
|
||||
buf[x+0], buf[x+1], buf[x+2], buf[x+3], buf[x+4], buf[x+5], buf[x+6], buf[x+7],
|
||||
buf[x+8], buf[x+9], buf[x+10], buf[x+11], buf[x+12], buf[x+13], buf[x+14], buf[x+15]);
|
||||
x += 16;
|
||||
coloredMsg(LOG_BLUE, "nes, %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x",
|
||||
buf[x+0], buf[x+1], buf[x+2], buf[x+3], buf[x+4], buf[x+5], buf[x+6], buf[x+7],
|
||||
buf[x+8], buf[x+9], buf[x+10], buf[x+11], buf[x+12], buf[x+13], buf[x+14], buf[x+15]);
|
||||
x += 16;
|
||||
coloredMsg(LOG_BLUE, "nes, %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x",
|
||||
buf[x+0], buf[x+1], buf[x+2], buf[x+3], buf[x+4], buf[x+5], buf[x+6], buf[x+7],
|
||||
buf[x+8], buf[x+9], buf[x+10], buf[x+11], buf[x+12], buf[x+13], buf[x+14], buf[x+15]);
|
||||
x += 16;
|
||||
coloredMsg(LOG_BLUE, "nes, %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x",
|
||||
buf[x+0], buf[x+1], buf[x+2], buf[x+3], buf[x+4], buf[x+5], buf[x+6], buf[x+7],
|
||||
buf[x+8], buf[x+9], buf[x+10], buf[x+11], buf[x+12], buf[x+13], buf[x+14], buf[x+15]);
|
||||
|
||||
close(SNTP_SOCK);
|
||||
coloredMsg(LOG_BLUE, "nes, msg received from %d.%d.%d.%d:%d",
|
||||
srcAddr[0], srcAddr[1], srcAddr[2], srcAddr[3],
|
||||
srcPort);
|
||||
|
||||
coloredMsg(LOG_BLUE, "nes, received in the %d. cycles", localHandle->retryCount);
|
||||
uint32_t xmt_h =
|
||||
(localHandle->ntpMsg.s.xmt_h & 0x0000000ff) << 24 |
|
||||
(localHandle->ntpMsg.s.xmt_h & 0x00000ff00) << 8 |
|
||||
(localHandle->ntpMsg.s.xmt_h & 0x000ff0000) >> 8 |
|
||||
(localHandle->ntpMsg.s.xmt_h & 0x0ff000000) >> 24;
|
||||
localHandle->seconds = ((uint64_t)xmt_h) - UNIX_NTP_EPOCH_DIFF;
|
||||
coloredMsg(LOG_BLUE, "nes, xmt: %08x", xmt_h);
|
||||
coloredMsg(LOG_BLUE, "nes, seconds: %lu", localHandle->seconds);
|
||||
localHandle->sntpState = SNTP_STATE_DONE;
|
||||
} else {
|
||||
coloredMsg(LOG_BLUE, "nes, invalid number of octets received: %d", recvLen);
|
||||
localHandle->sntpState = SNTP_STATE_ERROR;
|
||||
}
|
||||
break;
|
||||
default:
|
||||
coloredMsg(LOG_BLUE, "nes, unexpected state");
|
||||
}
|
||||
} else {
|
||||
coloredMsg(LOG_BLUE, "nes, no network yet, try again");
|
||||
schAdd(networkSntpEngine, (void*) localHandle, 100, 0);
|
||||
}
|
||||
}
|
||||
|
||||
uint64_t networkSntpQuery() {
|
||||
uint64_t res = 0;
|
||||
|
||||
if (sntpEngineHandle.sntpState == SNTP_STATE_IDLE) {
|
||||
coloredMsg(LOG_BLUE, "nsq, start sntp request");
|
||||
sntpEngineHandle.sntpState = SNTP_STATE_START;
|
||||
schAdd(networkSntpEngine, (void*) &sntpEngineHandle, 1, 0);
|
||||
} else if (sntpEngineHandle.sntpState == SNTP_STATE_DONE) {
|
||||
coloredMsg(LOG_BLUE, "nsq, start sntp done");
|
||||
res = sntpEngineHandle.seconds;
|
||||
coloredMsg(LOG_BLUE, "nsq, time is %lu", res);
|
||||
sntpEngineHandle.sntpState = SNTP_STATE_IDLE;
|
||||
} else if (sntpEngineHandle.sntpState == SNTP_STATE_ERROR) {
|
||||
coloredMsg(LOG_BLUE, "nsq, start sntp failed");
|
||||
sntpEngineHandle.sntpState = SNTP_STATE_IDLE;
|
||||
}
|
||||
|
||||
return res;
|
||||
}
|
||||
|
||||
extern uint8_t SINK_SOCK;
|
||||
|
||||
@ -38,6 +220,11 @@ int8_t networkUdpSend(char *hostname, uint16_t port, uint8_t *buf, uint16_t bufL
|
||||
return 1;
|
||||
}
|
||||
|
||||
bool isNetworkAvailable() {
|
||||
return wizIsNetworkAvailable();
|
||||
}
|
||||
|
||||
void networkImplInit() {
|
||||
config = getConfig();
|
||||
wizInit();
|
||||
}
|
||||
|
@ -1,8 +1,9 @@
|
||||
#include <networkAbstractionLayer_impl.h>
|
||||
#include <logger.h>
|
||||
#include <utils.h>
|
||||
|
||||
#include <stdint.h>
|
||||
|
||||
#include <main.h>
|
||||
|
||||
|
||||
uint64_t networkSntpQuery() {
|
||||
@ -15,5 +16,7 @@ int8_t networkUdpSend(char *hostname, uint16_t port, uint8_t *buf, uint16_t bufL
|
||||
}
|
||||
|
||||
void networkImplInit() {
|
||||
|
||||
HAL_GPIO_WritePin(MODEM_RES_GPIO_Port, MODEM_RES_Pin, GPIO_PIN_RESET);
|
||||
activeDelay(255);
|
||||
HAL_GPIO_WritePin(MODEM_RES_GPIO_Port, MODEM_RES_Pin, GPIO_PIN_SET);
|
||||
}
|
||||
|
@ -1,6 +1,7 @@
|
||||
#include <main.h>
|
||||
#include <show.h>
|
||||
#include <PontCoopScheduler.h>
|
||||
#include <show.h>
|
||||
|
||||
#include <stdint.h>
|
||||
#include <stdlib.h>
|
||||
@ -24,9 +25,20 @@ showElement_t showElement[] = {
|
||||
};
|
||||
|
||||
static void showHandler(void *handle) {
|
||||
const uint8_t MAX_CNT = 3;
|
||||
static uint8_t cnt = 0;
|
||||
cnt += 1;
|
||||
if (cnt > MAX_CNT) {
|
||||
cnt = 0;
|
||||
}
|
||||
|
||||
uint8_t idx = 0;
|
||||
while (showElement[idx].port) {
|
||||
if (showElement[idx].currentState == BLINK) {
|
||||
if (cnt == MAX_CNT) {
|
||||
HAL_GPIO_TogglePin(showElement[idx].port, showElement[idx].pin);
|
||||
}
|
||||
} else if (showElement[idx].currentState == BLINK_FAST) {
|
||||
HAL_GPIO_TogglePin(showElement[idx].port, showElement[idx].pin);
|
||||
}
|
||||
idx++;
|
||||
@ -43,5 +55,5 @@ void show(signalPin_t signalPin, signalAction_t action) {
|
||||
}
|
||||
|
||||
void showInit() {
|
||||
schAdd(showHandler, NULL, 0, 250);
|
||||
schAdd(showHandler, NULL, 0, 100);
|
||||
}
|
@ -26,18 +26,14 @@ static uint8_t dhcpBuffer[DHCP_BUFFER_SIZE];
|
||||
static uint8_t dnsBuffer[DNS_BUFFER_SIZE];
|
||||
|
||||
|
||||
static uint8_t sntpBuffer[MAX_SNTP_BUF_SIZE];
|
||||
const uint64_t UNIX_NTP_EPOCH_DIFF = 2208988800;
|
||||
|
||||
|
||||
extern const uint8_t DHCP_SOCK;
|
||||
extern const uint8_t DNS_SOCK;
|
||||
extern const uint8_t SNTP_SOCK;
|
||||
|
||||
|
||||
static bool networkAvailable = false;
|
||||
|
||||
bool isNetworkAvailable() {
|
||||
bool wizIsNetworkAvailable() {
|
||||
return networkAvailable;
|
||||
}
|
||||
|
||||
@ -90,7 +86,7 @@ static void wizDHCPAssign() {
|
||||
coloredMsg(LOG_BLUE, "wizda, set netinfo again");
|
||||
|
||||
networkAvailable = true;
|
||||
show(LED_GREEN, ON);
|
||||
show(LED_GREEN, BLINK_FAST);
|
||||
coloredMsg(LOG_BLUE, "wizda, network is available");
|
||||
}
|
||||
|
||||
@ -133,36 +129,6 @@ bool wizDnsQuery(char *name, uint8_t *ip) {
|
||||
}
|
||||
|
||||
|
||||
uint64_t wizSntpQuery() {
|
||||
uint64_t seconds = 0;
|
||||
|
||||
if (networkAvailable) {
|
||||
coloredMsg(LOG_BLUE, "wizsq, about to call SNTP");
|
||||
|
||||
uint8_t ntpServer[4];
|
||||
if (wizDnsQuery(config->ntpServer, ntpServer)) {
|
||||
SNTP_init(SNTP_SOCK, ntpServer, 0, sntpBuffer);
|
||||
for (uint8_t i = 0; i < 16; i++) {
|
||||
datetime curTime;
|
||||
int8_t res = SNTP_run(&curTime);
|
||||
if (res == 1) {
|
||||
seconds = curTime.seconds - UNIX_NTP_EPOCH_DIFF;
|
||||
coloredMsg(LOG_BLUE, "wizsq, curTime: %lu", seconds);
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
if (seconds == 0) {
|
||||
coloredMsg(LOG_BLUE, "wizsq, time update failed");
|
||||
}
|
||||
} else {
|
||||
coloredMsg(LOG_BLUE, "wizsq, error when querying ntp server name");
|
||||
}
|
||||
}
|
||||
|
||||
return seconds;
|
||||
}
|
||||
|
||||
static void wizPhyLinkHandler(void *handle) {
|
||||
// this handler is anyhow called with a 1s period, so we reuse it for the DNS timer
|
||||
DNS_time_handler();
|
||||
|
@ -1,6 +1,6 @@
|
||||
#MicroXplorer Configuration settings - do not modify
|
||||
File.Version=6
|
||||
GPIO.groupedBy=
|
||||
GPIO.groupedBy=Group By Peripherals
|
||||
IWDG.IPParameters=Prescaler
|
||||
IWDG.Prescaler=IWDG_PRESCALER_256
|
||||
KeepUserPlacement=false
|
||||
@ -171,7 +171,7 @@ ProjectManager.StackSize=0x400
|
||||
ProjectManager.TargetToolchain=Makefile
|
||||
ProjectManager.ToolChainLocation=
|
||||
ProjectManager.UnderRoot=false
|
||||
ProjectManager.functionlistsort=1-MX_GPIO_Init-GPIO-false-HAL-true,2-SystemClock_Config-RCC-false-HAL-false,3-MX_SPI1_Init-SPI1-false-HAL-true,4-MX_SPI2_Init-SPI2-false-HAL-true,5-MX_TIM1_Init-TIM1-false-HAL-true,6-MX_USART1_UART_Init-USART1-false-HAL-true,7-MX_IWDG_Init-IWDG-false-HAL-true
|
||||
ProjectManager.functionlistsort=1-MX_GPIO_Init-GPIO-false-HAL-true,2-SystemClock_Config-RCC-false-HAL-false,3-MX_SPI1_Init-SPI1-false-HAL-true,4-MX_SPI2_Init-SPI2-false-HAL-true,5-MX_TIM1_Init-TIM1-false-HAL-true,6-MX_USART1_UART_Init-USART1-false-HAL-true,7-MX_IWDG_Init-IWDG-false-HAL-true,8-MX_USART2_UART_Init-USART2-false-HAL-true
|
||||
RCC.ADCFreqValue=36000000
|
||||
RCC.AHBFreq_Value=72000000
|
||||
RCC.APB1CLKDivider=RCC_HCLK_DIV2
|
||||
|
BIN
docs/ntp.pcap
Normal file
BIN
docs/ntp.pcap
Normal file
Binary file not shown.
BIN
docs/ntp2.pcap
Normal file
BIN
docs/ntp2.pcap
Normal file
Binary file not shown.
@ -270,8 +270,9 @@ int httpPostRequest(char *url, const char *user, const char *pass, char *payload
|
||||
}
|
||||
|
||||
int forwardMinuteBuffer(t_forwarderHandle *handle, t_minuteBuffer *buf) {
|
||||
logmsg(LOG_INFO, "DeviceId: %s, RunningHours: %u, Powercycles: %u, WatchdogResets: %u",
|
||||
buf->s.deviceId, buf->s.totalRunningHours, buf->s.totalPowercycles, buf->s.totalWatchdogResets);
|
||||
logmsg(LOG_INFO, "D: %s, R: %u, P: %u, W: %u, V: %08x",
|
||||
buf->s.deviceId, buf->s.totalRunningHours, buf->s.totalPowercycles, buf->s.totalWatchdogResets,
|
||||
buf->s.version);
|
||||
|
||||
t_device *device = findDevice(handle->configHandle, buf->s.deviceId);
|
||||
const char *location = device->location;
|
||||
|
Reference in New Issue
Block a user