Compare commits
63 Commits
small_down
...
new_sntp_i
Author | SHA1 | Date | |
---|---|---|---|
c46e5dbb3b | |||
e8f9455c17
|
|||
9f907cb84e
|
|||
4b39b99848
|
|||
5d25490af7
|
|||
6ad1eda428
|
|||
70de623e5f
|
|||
7a60bb9c97
|
|||
3cefd16838
|
|||
3c6ddcb99b
|
|||
13a67dc660
|
|||
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
|
|||
a10b9c1dfd | |||
482d60e903
|
|||
9db7360623
|
|||
b3832a3d1b
|
|||
4ca37156f3
|
|||
a2a484fcc5
|
|||
734343c477
|
|||
f58538ab79
|
|||
b39c396871 | |||
7dd27d374f | |||
3e2da4e385
|
|||
3334b5d9da
|
|||
d29529bf8a
|
|||
5b7248a8a5
|
|||
5677f35260
|
|||
fd914a94ea
|
|||
37767dc81d
|
|||
af21143561
|
|||
5eaa9aceed
|
|||
d749d2e7fc
|
|||
12626941ce
|
|||
b3b1908480
|
|||
dfce60e01f
|
|||
bfbcc309df
|
|||
e9b60d4058
|
|||
73ac869f26
|
|||
a711fcbe2a
|
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";
|
||||
|
@ -1,4 +1,4 @@
|
||||
// Processed by ../tools/insertMyCode.sh
|
||||
// Processed by ../tools/insertMyCode.sh
|
||||
/* USER CODE BEGIN Header */
|
||||
/**
|
||||
******************************************************************************
|
||||
@ -27,8 +27,8 @@
|
||||
#include "gpio.h"
|
||||
|
||||
/* Private includes ----------------------------------------------------------*/
|
||||
/* USER CODE BEGIN Includes */
|
||||
#include "main2.h"
|
||||
/* USER CODE BEGIN Includes */
|
||||
#include "main2.h"
|
||||
|
||||
|
||||
/* USER CODE END Includes */
|
||||
@ -70,8 +70,8 @@ void SystemClock_Config(void);
|
||||
*/
|
||||
int main(void)
|
||||
{
|
||||
/* USER CODE BEGIN 1 */
|
||||
my_setup_1();
|
||||
/* USER CODE BEGIN 1 */
|
||||
my_setup_1();
|
||||
|
||||
|
||||
/* USER CODE END 1 */
|
||||
@ -98,9 +98,9 @@ int main(void)
|
||||
MX_SPI2_Init();
|
||||
MX_TIM1_Init();
|
||||
MX_USART1_UART_Init();
|
||||
MX_IWDG_Init();
|
||||
/* USER CODE BEGIN 2 */
|
||||
my_setup_2();
|
||||
// MX_IWDG_Init();
|
||||
/* USER CODE BEGIN 2 */
|
||||
my_setup_2();
|
||||
|
||||
|
||||
/* USER CODE END 2 */
|
||||
@ -111,8 +111,8 @@ int main(void)
|
||||
{
|
||||
/* USER CODE END WHILE */
|
||||
|
||||
/* USER CODE BEGIN 3 */
|
||||
my_loop();
|
||||
/* USER CODE BEGIN 3 */
|
||||
my_loop();
|
||||
|
||||
}
|
||||
/* USER CODE END 3 */
|
||||
@ -167,13 +167,13 @@ void SystemClock_Config(void)
|
||||
*/
|
||||
void Error_Handler(void)
|
||||
{
|
||||
/* USER CODE BEGIN Error_Handler_Debug */
|
||||
my_errorHandler();
|
||||
/* USER CODE BEGIN Error_Handler_Debug */
|
||||
my_errorHandler();
|
||||
|
||||
/* User can add his own implementation to report the HAL error return state */
|
||||
|
||||
/* USER CODE END Error_Handler_Debug */
|
||||
while(1) { };
|
||||
/* USER CODE END Error_Handler_Debug */
|
||||
while(1) { };
|
||||
|
||||
}
|
||||
|
||||
|
150
cube/Makefile
150
cube/Makefile
@ -1,8 +1,11 @@
|
||||
# Processed by ../tools/insertMyCode.sh
|
||||
# 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)
|
||||
#
|
||||
@ -11,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
|
||||
######################################
|
||||
@ -36,36 +54,61 @@ BUILD_DIR = build
|
||||
# source
|
||||
######################################
|
||||
# C sources
|
||||
C_SOURCES = \
|
||||
User/Src/cmdHandler.c User/Src/config.c User/Src/configCmds.c User/Src/counter.c User/Src/eeprom.c User/Src/logger.c User/Src/main2.c User/Src/ports.c User/Src/regularCmds.c User/Src/ringbuffer.c User/Src/sha256.c User/Src/show.c User/Src/utils.c User/Src/wizHelper.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 \
|
||||
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
|
||||
|
||||
|
||||
@ -109,8 +152,8 @@ MCU = $(CPU) -mthumb $(FPU) $(FLOAT-ABI)
|
||||
AS_DEFS =
|
||||
|
||||
# C defines
|
||||
C_DEFS = \
|
||||
-DUSE_HAL_DRIVER \
|
||||
C_DEFS = \
|
||||
-DUSE_HAL_DRIVER \
|
||||
-DSTM32F103xB
|
||||
|
||||
|
||||
@ -118,25 +161,30 @@ C_DEFS = \
|
||||
AS_INCLUDES =
|
||||
|
||||
# C includes
|
||||
C_INCLUDES = \
|
||||
-Ihottislib \
|
||||
-IUser/Inc \
|
||||
-IioLibrary_Driver/Internet/SNTP \
|
||||
-IioLibrary_Driver/Internet/DNS \
|
||||
-IioLibrary_Driver/Internet/DHCP \
|
||||
-IioLibrary_Driver/Ethernet \
|
||||
-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
|
||||
|
||||
|
||||
ifeq ($(NETWORK), LAN)
|
||||
C_INCLUDES += \
|
||||
-IioLibrary_Driver/Internet/SNTP \
|
||||
-IioLibrary_Driver/Internet/DNS \
|
||||
-IioLibrary_Driver/Internet/DHCP \
|
||||
-IioLibrary_Driver/Ethernet
|
||||
endif
|
||||
|
||||
|
||||
# compile gcc flags
|
||||
ASFLAGS = $(MCU) $(AS_DEFS) $(AS_INCLUDES) $(OPT) -Wall -Werror -fdata-sections -ffunction-sections
|
||||
|
||||
CFLAGS = $(MCU) $(C_DEFS) $(C_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
|
||||
@ -168,14 +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)))
|
||||
OBJECTS += $(addprefix $(BUILD_DIR)/,w5500.a)
|
||||
|
||||
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)))
|
||||
|
||||
$(BUILD_DIR)/w5500.a:
|
||||
(cd ioLibrary_Driver && $(MAKE) && cp w5500.a ../$(BUILD_DIR) && cd ..)
|
||||
|
||||
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 $@
|
||||
|
||||
@ -206,4 +260,4 @@ clean:
|
||||
#######################################
|
||||
-include $(wildcard $(BUILD_DIR)/*.d)
|
||||
|
||||
# *** EOF ***
|
||||
# *** EOF ***
|
||||
|
@ -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 ***
|
@ -1,9 +0,0 @@
|
||||
#ifndef _CMDHANDLER_H_
|
||||
#define _CMDHANDLER_H_
|
||||
|
||||
#include <stdint.h>
|
||||
|
||||
void cmdHandlerInit();
|
||||
|
||||
|
||||
#endif /* _CMDHANDLER_H_ */
|
@ -1,23 +0,0 @@
|
||||
#ifndef _CMDHELPER_H_
|
||||
#define _CMDHELPER_H_
|
||||
|
||||
#include <stdint.h>
|
||||
#include <stdbool.h>
|
||||
|
||||
|
||||
typedef bool (*cmdFunc_t)(uint8_t argc, char **args);
|
||||
|
||||
typedef struct {
|
||||
char name[24];
|
||||
char help[512];
|
||||
cmdFunc_t cmdFunc;
|
||||
} cmd_t;
|
||||
|
||||
void sendString(const char *buf);
|
||||
bool sendFormatString(const char *format, ...);
|
||||
|
||||
const cmd_t *getRegularCommands();
|
||||
const cmd_t *getAdminCommands();
|
||||
const cmd_t *getConfigCommands();
|
||||
|
||||
#endif /* _CMDHELPER_H_ */
|
@ -4,17 +4,25 @@
|
||||
#include <stdint.h>
|
||||
#include <sha256.h>
|
||||
|
||||
#define CONFIG_MAGIC 0xdead0007
|
||||
#define CONFIG_MAGIC 0xdead0008
|
||||
|
||||
typedef struct __attribute__((__packed__)) s_configBlock {
|
||||
uint32_t configMagic;
|
||||
char deviceName[16];
|
||||
uint8_t macAddress[6];
|
||||
char ntpServer[48];
|
||||
char deviceId[16];
|
||||
char sharedSecret[SHA256_BLOCK_SIZE];
|
||||
char sinkServer[48];
|
||||
uint8_t filler[22];
|
||||
union {
|
||||
struct {
|
||||
uint8_t macAddress[6];
|
||||
} lan;
|
||||
struct {
|
||||
char ssid[48];
|
||||
char key[65]; // the actual key may have up to 64 chars and has to end with a \0
|
||||
} wifi;
|
||||
} networkspecific;
|
||||
uint8_t filler[11];
|
||||
} t_configBlock;
|
||||
|
||||
|
||||
|
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_
|
21
cube/User/Inc/networkAbstractionLayer.h
Normal file
21
cube/User/Inc/networkAbstractionLayer.h
Normal file
@ -0,0 +1,21 @@
|
||||
#ifndef _NETWORK_ABSTRACTION_LAYER_H_
|
||||
#define _NETWORK_ABSTRACTION_LAYER_H_
|
||||
|
||||
#include <stdint.h>
|
||||
#include <stdbool.h>
|
||||
#include <sinkStruct.h>
|
||||
|
||||
|
||||
typedef struct {
|
||||
uint64_t seconds;
|
||||
uint32_t missedUpdates;
|
||||
bool valid;
|
||||
} t_seconds;
|
||||
|
||||
void networkInit();
|
||||
t_seconds* networkGetSeconds();
|
||||
int8_t networkSendMinuteBuffer(t_minuteBuffer *minuteBuffer);
|
||||
|
||||
|
||||
|
||||
#endif /* _NETWORK_ABSTRACTION_LAYER_H_ */
|
13
cube/User/Inc/networkAbstractionLayer_impl.h
Normal file
13
cube/User/Inc/networkAbstractionLayer_impl.h
Normal file
@ -0,0 +1,13 @@
|
||||
#ifndef _NETWORK_ABSTRACTION_LAYER_IMPL_H_
|
||||
#define _NETWORK_ABSTRACTION_LAYER_IMPL_H_
|
||||
|
||||
#include <stdint.h>
|
||||
|
||||
uint64_t networkSntpQuery();
|
||||
|
||||
|
||||
int8_t networkUdpSend(char *hostname, uint16_t port, uint8_t *buf, uint16_t bufLen);
|
||||
void networkImplInit();
|
||||
|
||||
|
||||
#endif /* _NETWORK_ABSTRACTION_LAYER_IMPL_H_ */
|
@ -9,6 +9,10 @@
|
||||
typedef struct __attribute__((__packed__)) {
|
||||
char deviceId[sizeof(((t_configBlock*)0)->deviceId)];
|
||||
uint8_t hash[SHA256_BLOCK_SIZE];
|
||||
uint32_t totalRunningHours;
|
||||
uint32_t totalPowercycles;
|
||||
uint32_t totalWatchdogResets;
|
||||
uint32_t version;
|
||||
uint64_t timestamp;
|
||||
uint32_t frequency[SECONDS_PER_MINUTE];
|
||||
} t_minuteStruct;
|
||||
|
@ -5,18 +5,12 @@
|
||||
#include <stdint.h>
|
||||
|
||||
|
||||
typedef struct {
|
||||
uint64_t seconds;
|
||||
uint32_t missedUpdates;
|
||||
bool valid;
|
||||
} t_seconds;
|
||||
|
||||
int wizInit();
|
||||
bool isNetworkAvailable();
|
||||
uint8_t* wizGetIPAddress();
|
||||
bool wizDnsQuery(char *name, uint8_t *ip);
|
||||
|
||||
t_seconds* wizGetSeconds();
|
||||
uint64_t wizSntpQuery();
|
||||
|
||||
|
||||
#endif // _WIZHELPER_H_
|
||||
|
@ -1,326 +0,0 @@
|
||||
#include <cmdHandler.h>
|
||||
#include <cmdHelper.h>
|
||||
|
||||
#include <stdint.h>
|
||||
#include <stdbool.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#include <stdio.h>
|
||||
#include <stdarg.h>
|
||||
|
||||
#include <socket.h>
|
||||
|
||||
#include <logger.h>
|
||||
#include <PontCoopScheduler.h>
|
||||
#include <wizHelper.h>
|
||||
#include <config.h>
|
||||
|
||||
|
||||
extern const uint8_t CMD_SOCK;
|
||||
|
||||
const uint16_t cmdPort = 23;
|
||||
|
||||
typedef enum {
|
||||
CH_INIT,
|
||||
CH_LISTEN,
|
||||
CH_WAITING,
|
||||
CH_BANNER,
|
||||
CH_PROMPT,
|
||||
CH_RECEIVE,
|
||||
CH_DISCONNECT,
|
||||
CH_DISCONNECT_WAIT,
|
||||
CH_ERROR
|
||||
} chState_t;
|
||||
|
||||
void sendString(const char *buf) {
|
||||
send(CMD_SOCK, (uint8_t*)buf, strlen(buf));
|
||||
}
|
||||
|
||||
bool sendFormatString(const char *format, ...) {
|
||||
bool retCode = true;
|
||||
va_list vl;
|
||||
va_start(vl, format);
|
||||
char buf[4096];
|
||||
int vcnt = vsnprintf(buf, sizeof(buf), format, vl);
|
||||
retCode = (vcnt < sizeof(buf));
|
||||
va_end(vl);
|
||||
sendString(buf);
|
||||
return retCode;
|
||||
}
|
||||
|
||||
// returns 0 to continue waiting for input
|
||||
// returns -1 to close the connection
|
||||
// returns 1 to toggle to admin mode
|
||||
// returns 2 to toggle to config mode
|
||||
// returns 3 to toggle back to default mode
|
||||
static int8_t cmdExecuteCommand(uint8_t *cmdLine, bool resetSpecialModes) {
|
||||
const static char HELP_MSG[] = \
|
||||
"Usage\n\r" \
|
||||
"\n\r" \
|
||||
"help ................................. Show this help page\n\r" \
|
||||
"quit ................................. Terminate the console session\n\r" \
|
||||
"config ............................... Enter configuration mode\n\r" \
|
||||
;
|
||||
const static char CONFIG_INTRO_MSG[] = \
|
||||
"In configuration mode each command changing the configuration\n\r" \
|
||||
"will save changes directly to the EEPROM.\n\r" \
|
||||
"However, the system will only consider these changes after a\n\r" \
|
||||
"restart since only in this sitution the EEPROM is read.\n\r" \
|
||||
"\n\r" \
|
||||
;
|
||||
const static char GOODBYE_MSG[] = "Good bye\n\r";
|
||||
const static char OK_MSG[] = "OK\n\r";
|
||||
const static char FAILED_MSG[] = "Failed\n\r";
|
||||
const static char UNKNOWN_COMMAND[] = "Unknown command\n\r";
|
||||
uint8_t *messageToSend = NULL;
|
||||
|
||||
static bool configMode = false;
|
||||
|
||||
if (resetSpecialModes) {
|
||||
configMode = false;
|
||||
}
|
||||
|
||||
cmd_t const * commands = getRegularCommands();
|
||||
if (configMode) {
|
||||
commands = getConfigCommands();
|
||||
}
|
||||
|
||||
coloredMsg(LOG_YELLOW, "ch cec cmdLine is %s", cmdLine);;
|
||||
|
||||
#define MAX_NUM_OF_ARGS 8
|
||||
char *args[MAX_NUM_OF_TASKS];
|
||||
uint8_t argc = 0;
|
||||
char *inCmdLine = (char*)cmdLine;
|
||||
do {
|
||||
args[argc++] = strtok(inCmdLine, " ");
|
||||
inCmdLine = NULL;
|
||||
} while (args[argc - 1] != NULL);
|
||||
if (argc > 0) {
|
||||
argc--;
|
||||
}
|
||||
char *cmd = args[0];
|
||||
|
||||
int8_t retCode = 0;
|
||||
coloredMsg(LOG_YELLOW, "ch cec cmd is %s, number of arguments %d", cmd, argc);
|
||||
|
||||
if (0 == strcmp(cmd, "quit")) {
|
||||
messageToSend = (uint8_t*)GOODBYE_MSG;
|
||||
retCode = -1;
|
||||
} else if (0 == strcmp(cmd, "help")) {
|
||||
if (configMode) {
|
||||
sendString(CONFIG_INTRO_MSG);
|
||||
}
|
||||
sendString(HELP_MSG);
|
||||
uint8_t cmdIdx = 0;
|
||||
while (true) {
|
||||
cmd_t command = commands[cmdIdx];
|
||||
if (0 == strcmp("END_OF_CMDS", command.name)) {
|
||||
break;
|
||||
}
|
||||
sendString(command.help);
|
||||
cmdIdx++;
|
||||
}
|
||||
messageToSend = NULL;
|
||||
} else if (0 == strcmp(cmd, "config")) {
|
||||
coloredMsg(LOG_YELLOW, "ch cec enable config mode");
|
||||
configMode = true;
|
||||
retCode = 2;
|
||||
} else {
|
||||
uint8_t cmdIdx = 0;
|
||||
while (true) {
|
||||
cmd_t command = commands[cmdIdx];
|
||||
if (0 == strcmp("END_OF_CMDS", command.name)) {
|
||||
messageToSend = (uint8_t*) UNKNOWN_COMMAND;
|
||||
break;
|
||||
}
|
||||
if (0 == strcmp(cmd, command.name)) {
|
||||
messageToSend = command.cmdFunc(argc, args) ? (uint8_t*)OK_MSG : (uint8_t*)FAILED_MSG;
|
||||
break;
|
||||
}
|
||||
cmdIdx++;
|
||||
}
|
||||
}
|
||||
|
||||
if (messageToSend) {
|
||||
coloredMsg(LOG_YELLOW, "ch cec command finally returns %s", messageToSend);
|
||||
send(CMD_SOCK, messageToSend, strlen((char*)messageToSend));
|
||||
}
|
||||
|
||||
return retCode;
|
||||
}
|
||||
|
||||
static void cmdHandlerEngine(void *handle) {
|
||||
static uint8_t receiveBuffer[256];
|
||||
|
||||
static chState_t state = CH_INIT;
|
||||
static bool resetSpecialModes = false;
|
||||
|
||||
static char banner[] = \
|
||||
"MBGW3\n\r" \
|
||||
"Type help for usage help\n\r" \
|
||||
"or quit to close the connection.\n\r";
|
||||
|
||||
static char *prompt;
|
||||
static char defaultPrompt[] = " > ";
|
||||
static char configPrompt[] = " (config) $ ";
|
||||
|
||||
|
||||
int8_t res = 0;
|
||||
uint8_t sockState;
|
||||
int32_t resultSend;
|
||||
int16_t receivedOctets;
|
||||
int32_t resultRecv;
|
||||
uint8_t resultDisconnect;
|
||||
|
||||
|
||||
if (isNetworkAvailable()) {
|
||||
switch (state) {
|
||||
case CH_INIT:
|
||||
coloredMsg(LOG_YELLOW, "ch che initializing socket");
|
||||
|
||||
res = socket(CMD_SOCK, Sn_MR_TCP, cmdPort, SF_IO_NONBLOCK);
|
||||
coloredMsg(LOG_YELLOW, "ch che socket returns %d", res);
|
||||
|
||||
if (res == CMD_SOCK) {
|
||||
coloredMsg(LOG_YELLOW, "ch che socket is initialized");
|
||||
state = CH_LISTEN;
|
||||
} else {
|
||||
state = CH_ERROR;
|
||||
}
|
||||
break;
|
||||
|
||||
case CH_LISTEN:
|
||||
coloredMsg(LOG_YELLOW, "ch che listening");
|
||||
|
||||
res = listen(CMD_SOCK);
|
||||
coloredMsg(LOG_YELLOW, "ch che listen returns %d", res);
|
||||
|
||||
if (res == SOCK_OK) {
|
||||
coloredMsg(LOG_YELLOW, "ch che ok, waiting for established");
|
||||
state = CH_WAITING;
|
||||
} else {
|
||||
state = CH_ERROR;
|
||||
}
|
||||
break;
|
||||
|
||||
case CH_WAITING:
|
||||
sockState = getSn_SR(CMD_SOCK);
|
||||
if (sockState != SOCK_LISTEN) {
|
||||
coloredMsg(LOG_YELLOW, "ch che socket state is 0x%02x", sockState);
|
||||
state = CH_DISCONNECT;
|
||||
}
|
||||
|
||||
if (sockState == SOCK_ESTABLISHED) {
|
||||
coloredMsg(LOG_YELLOW, "ch che connection is established");
|
||||
state = CH_BANNER;
|
||||
}
|
||||
break;
|
||||
|
||||
case CH_BANNER:
|
||||
coloredMsg(LOG_YELLOW, "ch che send banner");
|
||||
sockState = getSn_SR(CMD_SOCK);
|
||||
if (sockState != SOCK_ESTABLISHED) {
|
||||
coloredMsg(LOG_YELLOW, "ch che sockState is 0x%02x when trying to send banner", sockState);
|
||||
state = CH_DISCONNECT;
|
||||
} else {
|
||||
resultSend = send(CMD_SOCK, (uint8_t*)banner, strlen(banner));
|
||||
coloredMsg(LOG_YELLOW, "ch che sent banner, send returns 0x%02x", resultSend);
|
||||
prompt = defaultPrompt;
|
||||
resetSpecialModes = true;
|
||||
state = CH_PROMPT;
|
||||
}
|
||||
break;
|
||||
|
||||
case CH_PROMPT:
|
||||
coloredMsg(LOG_YELLOW, "ch che send prompt");
|
||||
sockState = getSn_SR(CMD_SOCK);
|
||||
if (sockState != SOCK_ESTABLISHED) {
|
||||
coloredMsg(LOG_YELLOW, "ch che sockState is 0x%02x when trying to send promt", sockState);
|
||||
state = CH_DISCONNECT;
|
||||
} else {
|
||||
sendFormatString("%s %s", getConfig()->deviceName, prompt);
|
||||
coloredMsg(LOG_YELLOW, "ch che sent prompt %s %s", getConfig()->deviceName, prompt);
|
||||
state = CH_RECEIVE;
|
||||
}
|
||||
break;
|
||||
|
||||
case CH_RECEIVE:
|
||||
sockState = getSn_SR(CMD_SOCK);
|
||||
if (sockState != SOCK_ESTABLISHED) {
|
||||
coloredMsg(LOG_YELLOW, "ch che sockState is 0x%02x when trying to receive something", sockState);
|
||||
state = CH_DISCONNECT;
|
||||
} else {
|
||||
receivedOctets = getSn_RX_RSR(CMD_SOCK);
|
||||
|
||||
if (receivedOctets > 0) {
|
||||
memset(receiveBuffer, 0, sizeof(receiveBuffer));
|
||||
resultRecv = recv(CMD_SOCK, receiveBuffer, sizeof(receiveBuffer));
|
||||
coloredMsg(LOG_YELLOW, "ch che recv returns 0x%02x", resultRecv);
|
||||
if (resultRecv > 0) {
|
||||
if ((receiveBuffer[strlen((char*)receiveBuffer) - 1] == 0x0a) ||
|
||||
(receiveBuffer[strlen((char*)receiveBuffer) - 1] == 0x0d)) {
|
||||
receiveBuffer[strlen((char*)receiveBuffer) - 1] = 0;
|
||||
}
|
||||
if ((receiveBuffer[strlen((char*)receiveBuffer) - 1] == 0x0a) ||
|
||||
(receiveBuffer[strlen((char*)receiveBuffer) - 1] == 0x0d)) {
|
||||
receiveBuffer[strlen((char*)receiveBuffer) - 1] = 0;
|
||||
}
|
||||
coloredMsg(LOG_YELLOW, "ch che received: %s", receiveBuffer);
|
||||
int8_t resCEC = cmdExecuteCommand(receiveBuffer, resetSpecialModes);
|
||||
resetSpecialModes = false;
|
||||
switch (resCEC) {
|
||||
case 0:
|
||||
state = CH_PROMPT;
|
||||
break;
|
||||
case -1:
|
||||
state = CH_DISCONNECT;
|
||||
break;
|
||||
case 2:
|
||||
prompt = configPrompt;
|
||||
state = CH_PROMPT;
|
||||
break;
|
||||
case 3:
|
||||
prompt = defaultPrompt;
|
||||
state = CH_PROMPT;
|
||||
break;
|
||||
default:
|
||||
state = CH_DISCONNECT;
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
break;
|
||||
|
||||
case CH_DISCONNECT:
|
||||
coloredMsg(LOG_YELLOW, "ch che close our end");
|
||||
resultDisconnect = disconnect(CMD_SOCK);
|
||||
coloredMsg(LOG_YELLOW, "ch che disconnect returns 0x%02x", resultDisconnect);
|
||||
state = CH_DISCONNECT_WAIT;
|
||||
break;
|
||||
|
||||
case CH_DISCONNECT_WAIT:
|
||||
//coloredMsg(LOG_YELLOW, "ch che waiting after disconnect");
|
||||
sockState = getSn_SR(CMD_SOCK);
|
||||
//coloredMsg(LOG_YELLOW, "ch che sockState is 0x%02x", sockState);
|
||||
if (sockState == SOCK_CLOSED) {
|
||||
coloredMsg(LOG_YELLOW, "ch che socket is closed now");
|
||||
state = CH_INIT;
|
||||
}
|
||||
break;
|
||||
|
||||
|
||||
case CH_ERROR:
|
||||
coloredMsg(LOG_YELLOW, "ch che error state, will stop here");
|
||||
schDel(cmdHandlerEngine, NULL);
|
||||
state = CH_INIT;
|
||||
schAdd(cmdHandlerEngine, NULL, 5000, 100);
|
||||
coloredMsg(LOG_YELLOW, "ch che restart command handler engine in 5s");
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void cmdHandlerInit() {
|
||||
schAdd(cmdHandlerEngine, NULL, 0, 100);
|
||||
}
|
@ -10,12 +10,11 @@
|
||||
t_configBlock defaultConfigBlock = {
|
||||
.configMagic = CONFIG_MAGIC,
|
||||
.deviceName = "MainsCnt",
|
||||
.macAddress = { 0x00, 0xA0, 0x57, 0x05, 0x3E, 0x0F },
|
||||
.networkspecific.lan.macAddress = { 0x00, 0xA0, 0x57, 0x05, 0x3E, 0x0F },
|
||||
.ntpServer = "0.de.pool.ntp.org",
|
||||
.deviceId = "MainsCnt01",
|
||||
.sharedSecret = "sharedSecretGanzGeheim",
|
||||
.sinkServer = "laborpc",
|
||||
.filler = { 0 }
|
||||
.sinkServer = "laborpc"
|
||||
};
|
||||
|
||||
|
||||
@ -44,12 +43,12 @@ void configInit() {
|
||||
}
|
||||
coloredMsg(LOG_BLUE, "cfg ci configMagic: %lx", mainConfigBlock.configMagic);
|
||||
coloredMsg(LOG_BLUE, "cfg ci deviceName: %s", mainConfigBlock.deviceName);
|
||||
coloredMsg(LOG_BLUE, "cfg ci MAC address: %02x:%02x:%02x:%02x:%02x:%02x", mainConfigBlock.macAddress[0],
|
||||
mainConfigBlock.macAddress[1],
|
||||
mainConfigBlock.macAddress[2],
|
||||
mainConfigBlock.macAddress[3],
|
||||
mainConfigBlock.macAddress[4],
|
||||
mainConfigBlock.macAddress[5]);
|
||||
// coloredMsg(LOG_BLUE, "cfg ci MAC address: %02x:%02x:%02x:%02x:%02x:%02x", mainConfigBlock.macAddress[0],
|
||||
// mainConfigBlock.macAddress[1],
|
||||
// mainConfigBlock.macAddress[2],
|
||||
// mainConfigBlock.macAddress[3],
|
||||
// mainConfigBlock.macAddress[4],
|
||||
// mainConfigBlock.macAddress[5]);
|
||||
coloredMsg(LOG_BLUE, "cfg ci ntp server: %s", mainConfigBlock.ntpServer);
|
||||
coloredMsg(LOG_BLUE, "cfg ci deviceId: %s", mainConfigBlock.deviceId);
|
||||
coloredMsg(LOG_BLUE, "cfg ci sharedSecret: %s", mainConfigBlock.sharedSecret);
|
||||
|
@ -1,212 +0,0 @@
|
||||
#include <cmdHelper.h>
|
||||
#include <logger.h>
|
||||
|
||||
#include <eeprom.h>
|
||||
#include <config.h>
|
||||
|
||||
#include <string.h>
|
||||
#include <stdlib.h>
|
||||
|
||||
|
||||
|
||||
static bool showConfigCmd(uint8_t argc, char **args) {
|
||||
bool retCode = true;
|
||||
|
||||
t_configBlock configBlock;
|
||||
eepromReadConfigBlock(&configBlock);
|
||||
sendString("This is the saved configuration.\n\r");
|
||||
sendString("It is not necessarily the active configuration.\n\r");
|
||||
sendFormatString("configMagic: %lx\n\r", configBlock.configMagic);
|
||||
sendFormatString("deviceName: %s\n\r", configBlock.deviceName);
|
||||
sendFormatString("MAC address: %02x:%02x:%02x:%02x:%02x:%02x\n\r", configBlock.macAddress[0],
|
||||
configBlock.macAddress[1],
|
||||
configBlock.macAddress[2],
|
||||
configBlock.macAddress[3],
|
||||
configBlock.macAddress[4],
|
||||
configBlock.macAddress[5]);
|
||||
sendFormatString("NTP Server: %s\n\r", configBlock.ntpServer);
|
||||
sendFormatString("deviceId: %s\n\r", configBlock.deviceId);
|
||||
sendFormatString("sharedSecret: (will not be displayed)\n\r");
|
||||
sendFormatString("Sink Server: %s\n\r", configBlock.sinkServer);
|
||||
|
||||
return retCode;
|
||||
}
|
||||
|
||||
static bool setStringParameterCmd(uint8_t argc, char **args, size_t offset, size_t length) {
|
||||
bool retCode = true;
|
||||
|
||||
t_configBlock configBlock;
|
||||
char *parameterName = args[1];
|
||||
char *newParameterValue = args[2];
|
||||
if (strlen(newParameterValue) >= length) {
|
||||
sendString("given new value for is too long\n\r");
|
||||
retCode = false;
|
||||
} else {
|
||||
sendFormatString("set %s to %s\n\r", parameterName, newParameterValue);
|
||||
|
||||
eepromReadConfigBlock(&configBlock);
|
||||
strcpy((((char*)&configBlock) + offset), newParameterValue);
|
||||
eepromWriteConfigBlock(&configBlock);
|
||||
}
|
||||
|
||||
return retCode;
|
||||
}
|
||||
|
||||
/*
|
||||
static bool setInt32ParameterCmd(uint8_t argc, char **args, size_t offset, int32_t minV, int32_t maxV) {
|
||||
bool retCode = true;
|
||||
|
||||
t_configBlock configBlock;
|
||||
char *parameterName = args[1];
|
||||
char *newParameterValue = args[2];
|
||||
long int value = strtol(newParameterValue, NULL, 10);
|
||||
if (value < minV) {
|
||||
sendString("value is too small\n\r");
|
||||
retCode = false;
|
||||
} else if (value > maxV) {
|
||||
sendString("value is too large\n\r");
|
||||
} else {
|
||||
int32_t v = (int32_t) value;
|
||||
sendFormatString("set %s to %ld\n\r", parameterName, v);
|
||||
|
||||
eepromReadConfigBlock(&configBlock);
|
||||
*((int32_t*)(((uint8_t*)&configBlock) + offset)) = v;
|
||||
eepromWriteConfigBlock(&configBlock);
|
||||
}
|
||||
|
||||
return retCode;
|
||||
}
|
||||
*/
|
||||
|
||||
static bool setDeviceNameCmd(uint8_t argc, char **args) {
|
||||
return setStringParameterCmd(argc, args,
|
||||
offsetof(t_configBlock, deviceName),
|
||||
sizeof(((t_configBlock*)0)->deviceName));
|
||||
}
|
||||
|
||||
static bool setNtpServerCmd(uint8_t argc, char **args) {
|
||||
return setStringParameterCmd(argc, args,
|
||||
offsetof(t_configBlock, ntpServer),
|
||||
sizeof(((t_configBlock*)0)->ntpServer));
|
||||
}
|
||||
|
||||
static bool setDeviceIdCmd(uint8_t argc, char **args) {
|
||||
return setStringParameterCmd(argc, args,
|
||||
offsetof(t_configBlock, deviceId),
|
||||
sizeof(((t_configBlock*)0)->deviceId));
|
||||
}
|
||||
|
||||
static bool setSharedSecretCmd(uint8_t argc, char **args) {
|
||||
return setStringParameterCmd(argc, args,
|
||||
offsetof(t_configBlock, sharedSecret),
|
||||
sizeof(((t_configBlock*)0)->sharedSecret));
|
||||
}
|
||||
|
||||
static bool setSinkServerCmd(uint8_t argc, char **args) {
|
||||
return setStringParameterCmd(argc, args,
|
||||
offsetof(t_configBlock, sinkServer),
|
||||
sizeof(((t_configBlock*)0)->sinkServer));
|
||||
}
|
||||
|
||||
|
||||
const static cmd_t SET_COMMANDS[] = {
|
||||
{ .name = "devicename", .cmdFunc = setDeviceNameCmd,
|
||||
.help = \
|
||||
"devicename ........................... Name of this device\n\r"
|
||||
},
|
||||
{ .name = "ntpserver", .cmdFunc = setNtpServerCmd,
|
||||
.help = \
|
||||
"ntpserver ............................ Name of the NTP server\n\r"
|
||||
},
|
||||
{ .name = "deviceid", .cmdFunc = setDeviceIdCmd,
|
||||
.help = \
|
||||
"deviceid ............................. ID of this device\n\r"
|
||||
},
|
||||
{ .name = "sharedSecret", .cmdFunc = setSharedSecretCmd,
|
||||
.help = \
|
||||
"sharedsecret ......................... Shared secret\n\r"
|
||||
},
|
||||
{ .name = "sinkserver", .cmdFunc = setSinkServerCmd,
|
||||
.help = \
|
||||
"sinkserver ........................... Name of the sink server\n\r"
|
||||
},
|
||||
{ .name = "END_OF_CMDS", .help = "",.cmdFunc = NULL }
|
||||
};
|
||||
|
||||
const static char UNKNOWN_PARAMETER[] = "unknown parameter\n\r";
|
||||
const static char OK_MSG[] = "OK\n\r";
|
||||
const static char FAILED_MSG[] = "Failed\n\r";
|
||||
|
||||
static bool setCmd(uint8_t argc, char **args) {
|
||||
bool retCode = false;
|
||||
uint8_t *messageToSend = NULL;
|
||||
|
||||
char *cmd = args[1];
|
||||
if (argc >= 2) {
|
||||
if (0 == strcmp("help", cmd)) {
|
||||
sendString("You can set the following parameters:\n\r");
|
||||
uint8_t cmdIdx = 0;
|
||||
while (true) {
|
||||
cmd_t command = SET_COMMANDS[cmdIdx];
|
||||
if (0 == strcmp("END_OF_CMDS", command.name)) {
|
||||
break;
|
||||
}
|
||||
sendString(command.help);
|
||||
cmdIdx++;
|
||||
}
|
||||
retCode = true;
|
||||
} else {
|
||||
uint8_t cmdIdx = 0;
|
||||
while (true) {
|
||||
cmd_t command = SET_COMMANDS[cmdIdx];
|
||||
if (0 == strcmp("END_OF_CMDS", command.name)) {
|
||||
messageToSend = (uint8_t*) UNKNOWN_PARAMETER;
|
||||
break;
|
||||
}
|
||||
if (0 == strcmp(cmd, command.name)) {
|
||||
retCode = command.cmdFunc(argc, args);
|
||||
messageToSend = retCode ? (uint8_t*)OK_MSG : (uint8_t*)FAILED_MSG;
|
||||
sendString("Remember you need to restart to active this change.\n\r");
|
||||
break;
|
||||
}
|
||||
cmdIdx++;
|
||||
}
|
||||
sendString((const char*)messageToSend);
|
||||
}
|
||||
}
|
||||
|
||||
return retCode;
|
||||
}
|
||||
|
||||
static bool restartCmd(uint8_t argc, char **args) {
|
||||
HAL_NVIC_SystemReset();
|
||||
// you won't come here ...
|
||||
return true;
|
||||
}
|
||||
|
||||
|
||||
|
||||
const cmd_t CONFIG_COMMANDS[] = {
|
||||
{ .name = "show", .cmdFunc = showConfigCmd,
|
||||
.help = \
|
||||
"show ................................. Show the configuration\n\r"
|
||||
},
|
||||
{ .name = "set", .cmdFunc = setCmd,
|
||||
.help = \
|
||||
"set .................................. Set configuration parameters\n\r" \
|
||||
" Argument help gives a list of \n\r" \
|
||||
" parameters\n\r"
|
||||
},
|
||||
{ .name = "restart", .cmdFunc = restartCmd,
|
||||
.help = \
|
||||
"restart .............................. Restart the system,\n\r" \
|
||||
" Required to reload config\n\r"
|
||||
},
|
||||
{ .name = "END_OF_CMDS", .help = "",.cmdFunc = NULL }
|
||||
};
|
||||
|
||||
|
||||
|
||||
const cmd_t *getConfigCommands() {
|
||||
return CONFIG_COMMANDS;
|
||||
}
|
@ -1,5 +1,6 @@
|
||||
#include <stdint.h>
|
||||
#include <string.h>
|
||||
#include <stdlib.h>
|
||||
|
||||
#include <tim.h>
|
||||
|
||||
@ -7,11 +8,11 @@
|
||||
#include <show.h>
|
||||
#include <logger.h>
|
||||
#include <PontCoopScheduler.h>
|
||||
#include <wizHelper.h>
|
||||
#include <config.h>
|
||||
#include <socket.h>
|
||||
#include <sinkStruct.h>
|
||||
#include <sha256.h>
|
||||
#include <eeprom.h>
|
||||
#include <networkAbstractionLayer.h>
|
||||
|
||||
|
||||
|
||||
@ -25,6 +26,7 @@ volatile static uint32_t mainsCntSum = 0;
|
||||
volatile static uint32_t mainsCntCnt = 0;
|
||||
|
||||
static t_seconds *seconds;
|
||||
static t_deviceStats *deviceStats;
|
||||
|
||||
|
||||
|
||||
@ -35,33 +37,6 @@ uint8_t activeMinuteBuffer;
|
||||
|
||||
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++) {
|
||||
@ -69,6 +44,13 @@ void counterMinuteTick(void *handle) {
|
||||
|
||||
if (minuteBufferReady[minuteBufferIdx] == 1) {
|
||||
coloredMsg(LOG_BLUE, "cmt, buffer %d is done, handle it", minuteBufferIdx);
|
||||
|
||||
minuteBuffer->s.totalRunningHours = deviceStats->totalRunningHours;
|
||||
minuteBuffer->s.totalPowercycles = deviceStats->totalPowercycles;
|
||||
minuteBuffer->s.totalWatchdogResets = deviceStats->totalWatchdogResets;
|
||||
minuteBuffer->s.version = strtol(VERSION, NULL, 16);
|
||||
|
||||
|
||||
memset(minuteBuffer->s.deviceId, 0, sizeof(minuteBuffer->s.deviceId));
|
||||
strcpy(minuteBuffer->s.deviceId, config->deviceId);
|
||||
|
||||
@ -114,7 +96,7 @@ void counterMinuteTick(void *handle) {
|
||||
minuteBuffer->s.hash[31]
|
||||
);
|
||||
|
||||
int8_t res = counterSendMinuteBuffer(minuteBuffer);
|
||||
int8_t res = networkSendMinuteBuffer(minuteBuffer);
|
||||
if (res == 1) {
|
||||
coloredMsg(LOG_BLUE, "cmt, successfully sent");
|
||||
minuteBufferReady[minuteBufferIdx] = false;
|
||||
@ -190,7 +172,9 @@ void mainsCntsInputCaptureCallback(TIM_HandleTypeDef *htim) {
|
||||
|
||||
|
||||
void counterInit() {
|
||||
seconds = wizGetSeconds();
|
||||
deviceStats = getGlobalDeviceStats();
|
||||
|
||||
seconds = networkGetSeconds();
|
||||
for (uint8_t i = 0; i < NUM_OF_MINUTE_BUFFERS; i++) {
|
||||
minuteBufferReady[i] = false;
|
||||
}
|
||||
|
@ -13,11 +13,16 @@
|
||||
#include <show.h>
|
||||
#include <logger.h>
|
||||
#include <eeprom.h>
|
||||
#include <wizHelper.h>
|
||||
#include <cmdHandler.h>
|
||||
#include <networkAbstractionLayer.h>
|
||||
#include <config.h>
|
||||
#include <counter.h>
|
||||
|
||||
#if NETWORK_STACK == 2
|
||||
#include <modemCom.h>
|
||||
#endif
|
||||
|
||||
|
||||
|
||||
|
||||
void my_setup_1() {
|
||||
schInit();
|
||||
@ -33,18 +38,13 @@ void my_setup_2() {
|
||||
show(LED_RED, OFF);
|
||||
show(LED_GREEN, BLINK);
|
||||
logMsg("Application starting");
|
||||
logMsg("Version: " VERSION);
|
||||
|
||||
eepromInit();
|
||||
|
||||
configInit();
|
||||
|
||||
wizInit();
|
||||
|
||||
if (HAL_GPIO_ReadPin(ADMIN_MODE_GPIO_Port, ADMIN_MODE_Pin)) {
|
||||
show(LED_BLUE, BLINK);
|
||||
coloredMsg(LED_RED, "STARTING ADMIN MODE");
|
||||
cmdHandlerInit();
|
||||
}
|
||||
networkInit();
|
||||
|
||||
counterInit();
|
||||
|
||||
@ -71,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) {
|
||||
|
||||
}
|
70
cube/User/Src/networkAbstractionLayer.c
Normal file
70
cube/User/Src/networkAbstractionLayer.c
Normal file
@ -0,0 +1,70 @@
|
||||
#include <networkAbstractionLayer.h>
|
||||
#include <PontCoopScheduler.h>
|
||||
#include <iwdg.h>
|
||||
#include <logger.h>
|
||||
#include <sinkStruct.h>
|
||||
#include <config.h>
|
||||
|
||||
#include <networkAbstractionLayer_impl.h>
|
||||
|
||||
|
||||
const uint16_t SINK_PORT = 20169;
|
||||
|
||||
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) {
|
||||
coloredMsg(LOG_YELLOW, "nsh, initially querying time");
|
||||
uint64_t tmpSeconds = networkSntpQuery();
|
||||
if (tmpSeconds != 0) {
|
||||
coloredMsg(LOG_YELLOW, "nsh, success, time is %lu", tmpSeconds);
|
||||
seconds.seconds = tmpSeconds;
|
||||
seconds.missedUpdates = 0;
|
||||
seconds.valid = true;
|
||||
} else {
|
||||
coloredMsg(LOG_YELLOW, "nsh, failed");
|
||||
seconds.missedUpdates += 1;
|
||||
}
|
||||
} 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;
|
||||
if (seconds.seconds != tmpSeconds) {
|
||||
coloredMsg(LOG_YELLOW, "nsh, local time updated");
|
||||
seconds.seconds = tmpSeconds;
|
||||
} else {
|
||||
coloredMsg(LOG_YELLOW, "nsh, local time still in sync");
|
||||
}
|
||||
} else {
|
||||
coloredMsg(LOG_YELLOW, "nsh, failed, trying again ...");
|
||||
seconds.missedUpdates += 1;
|
||||
tryAgain = true;
|
||||
}
|
||||
}
|
||||
|
||||
HAL_IWDG_Refresh(&hiwdg);
|
||||
}
|
||||
|
||||
t_seconds* networkGetSeconds() {
|
||||
return &seconds;
|
||||
}
|
||||
|
||||
int8_t networkSendMinuteBuffer(t_minuteBuffer *minuteBuffer) {
|
||||
return networkUdpSend(config->sinkServer, SINK_PORT, minuteBuffer->b, sizeof(minuteBuffer->b));
|
||||
}
|
||||
|
||||
void networkInit() {
|
||||
networkImplInit();
|
||||
|
||||
config = getConfig();
|
||||
schAdd(networkSecondsHandler, NULL, 0, 1000);
|
||||
}
|
165
cube/User/Src/networkAbstractionLayer_lan.c
Normal file
165
cube/User/Src/networkAbstractionLayer_lan.c
Normal file
@ -0,0 +1,165 @@
|
||||
#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 <wizchip_conf.h>
|
||||
#include <config.h>
|
||||
|
||||
static t_configBlock *config;
|
||||
|
||||
|
||||
const uint16_t MAX_RECV_RETRY_COUNT = 100;
|
||||
const uint64_t UNIX_NTP_EPOCH_DIFF = 2208988800;
|
||||
|
||||
const uint8_t SEND_LI_VN_MODE = 0xe3; // LI: unknown (3), VN: 4, Mode: Client (3)
|
||||
typedef struct {
|
||||
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;
|
||||
uint64_t org;
|
||||
uint64_t rec;
|
||||
uint64_t xmt;
|
||||
} ntpMsg_t;
|
||||
|
||||
extern uint8_t SNTP_SOCK;
|
||||
|
||||
const uint16_t NTP_PORT = 123;
|
||||
|
||||
enum {
|
||||
SNTP_STATE_IDLE,
|
||||
SNTP_STATE_START,
|
||||
SNTP_STATE_SEND,
|
||||
SNTP_STATE_RECV,
|
||||
SNTP_STATE_ERROR,
|
||||
SNTP_STATE_DONE
|
||||
} sntpState = SNTP_STATE_IDLE;
|
||||
uint64_t seconds;
|
||||
|
||||
|
||||
/*
|
||||
static void networkSntpEngine(void *handle) {
|
||||
static uint16_t retryCount = 0;
|
||||
|
||||
coloredMsg(LOG_BLUE, "nes, %u", sntpState);
|
||||
|
||||
switch (sntpState) {
|
||||
case SNTP_STATE_START:
|
||||
coloredMsg(LOG_BLUE, "nes, about to send");
|
||||
uint8_t ntpAddr[4];
|
||||
if (! wizDnsQuery(config->ntpServer, ntpAddr)) {
|
||||
coloredMsg(LOG_BLUE, "nes, failed to resolve ntp server");
|
||||
sntpState = SNTP_STATE_ERROR;
|
||||
} else {
|
||||
socket(SNTP_SOCK, Sn_MR_UDP, NTP_PORT, 0);
|
||||
retryCount = 0;
|
||||
uint8_t sockState = getSn_SR(SNTP_SOCK);
|
||||
if (sockState == SOCK_UDP) {
|
||||
ntpMsg_t ntpMsg;
|
||||
memset(&ntpMsg, 0, sizeof(ntpMsg));
|
||||
ntpMsg.li_vn_mode = SEND_LI_VN_MODE;
|
||||
sendto(SNTP_SOCK, (uint8_t*)&ntpMsg, sizeof(ntpMsg), ntpAddr, NTP_PORT);
|
||||
coloredMsg(LOG_BLUE, "nes, sent");
|
||||
sntpState = SNTP_STATE_RECV;
|
||||
schAdd(networkSntpEngine, NULL, 1000, 0);
|
||||
} else {
|
||||
coloredMsg(LOG_BLUE, "nes, socket in unexpected state: %d", sockState);
|
||||
sntpState = SNTP_STATE_ERROR;
|
||||
}
|
||||
}
|
||||
break;
|
||||
case SNTP_STATE_RECV:
|
||||
coloredMsg(LOG_BLUE, "nes, check receive");
|
||||
uint16_t recvLen = getSn_RX_RSR(SNTP_SOCK);
|
||||
if (recvLen == 0) {
|
||||
retryCount += 1;
|
||||
if (retryCount > MAX_RECV_RETRY_COUNT) {
|
||||
coloredMsg(LOG_BLUE, "nes max retry count reached, failed");
|
||||
sntpState = SNTP_STATE_ERROR;
|
||||
} else {
|
||||
coloredMsg(LOG_BLUE, "nes, nothing received yet, try again");
|
||||
schAdd(networkSntpEngine, NULL, 100, 0);
|
||||
}
|
||||
} else if (recvLen == sizeof(ntpMsg_t)) {
|
||||
ntpMsg_t ntpMsg;
|
||||
memset(&ntpMsg, 0, sizeof(ntpMsg_t));
|
||||
uint8_t srcAddr[4];
|
||||
uint16_t srcPort;
|
||||
recvfrom(SNTP_SOCK, (uint8_t*)&ntpMsg, sizeof(ntpMsg_t), srcAddr, &srcPort);
|
||||
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", retryCount);
|
||||
seconds = ntpMsg.rec - UNIX_NTP_EPOCH_DIFF;
|
||||
coloredMsg(LOG_BLUE, "nes, seconds: %lu", seconds);
|
||||
sntpState = SNTP_STATE_DONE;
|
||||
} else {
|
||||
coloredMsg(LOG_BLUE, "nes, invalid number of octets received: %d", recvLen);
|
||||
sntpState = SNTP_STATE_ERROR;
|
||||
}
|
||||
break;
|
||||
default:
|
||||
coloredMsg(LOG_BLUE, "nes, unexpected state");
|
||||
}
|
||||
}
|
||||
*/
|
||||
|
||||
|
||||
uint64_t networkSntpQuery() {
|
||||
/*
|
||||
uint64_t res = 0;
|
||||
if (sntpState == SNTP_STATE_IDLE) {
|
||||
sntpState = SNTP_STATE_START;
|
||||
schAdd(networkSntpEngine, NULL, 1, 0);
|
||||
} else if (sntpState == SNTP_STATE_ERROR) {
|
||||
sntpState = SNTP_STATE_IDLE;
|
||||
} else if (sntpState == SNTP_STATE_DONE) {
|
||||
sntpState = SNTP_STATE_IDLE;
|
||||
res = seconds;
|
||||
}
|
||||
return res;
|
||||
*/
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
||||
extern uint8_t SINK_SOCK;
|
||||
|
||||
int8_t networkUdpSend(char *hostname, uint16_t port, uint8_t *buf, uint16_t bufLen) {
|
||||
uint8_t sinkAddr[4];
|
||||
if (! wizDnsQuery(hostname, sinkAddr)) {
|
||||
coloredMsg(LOG_BLUE, "nus, failed to resolve sink server name");
|
||||
return -1;
|
||||
} else {
|
||||
coloredMsg(LOG_BLUE, "nus, sink server at %d.%d.%d.%d", sinkAddr[0], sinkAddr[1], sinkAddr[2], sinkAddr[3]);
|
||||
}
|
||||
|
||||
socket(SINK_SOCK, Sn_MR_UDP, port, 0);
|
||||
uint8_t sockState = getSn_SR(SINK_SOCK);
|
||||
if (sockState == SOCK_UDP) {
|
||||
sendto(SINK_SOCK, buf, bufLen, sinkAddr, port);
|
||||
coloredMsg(LOG_BLUE, "nus, sent");
|
||||
} else {
|
||||
coloredMsg(LOG_BLUE, "nus, socket in unexpected state: %d", sockState);
|
||||
return -2;
|
||||
}
|
||||
|
||||
close(SINK_SOCK);
|
||||
|
||||
return 1;
|
||||
}
|
||||
|
||||
void networkImplInit() {
|
||||
config = getConfig();
|
||||
wizInit();
|
||||
}
|
22
cube/User/Src/networkAbstractionLayer_wifi.c
Normal file
22
cube/User/Src/networkAbstractionLayer_wifi.c
Normal file
@ -0,0 +1,22 @@
|
||||
#include <networkAbstractionLayer_impl.h>
|
||||
#include <logger.h>
|
||||
#include <utils.h>
|
||||
|
||||
#include <stdint.h>
|
||||
#include <main.h>
|
||||
|
||||
|
||||
uint64_t networkSntpQuery() {
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
||||
int8_t networkUdpSend(char *hostname, uint16_t port, uint8_t *buf, uint16_t bufLen) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
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);
|
||||
}
|
@ -6,6 +6,5 @@
|
||||
|
||||
const uint8_t DHCP_SOCK = 0;
|
||||
const uint8_t SNTP_SOCK = 1;
|
||||
const uint8_t CMD_SOCK = 2;
|
||||
const uint8_t DNS_SOCK = 3;
|
||||
const uint8_t SINK_SOCK = 4;
|
||||
const uint8_t DNS_SOCK = 2;
|
||||
const uint8_t SINK_SOCK = 3;
|
||||
|
@ -1,52 +0,0 @@
|
||||
#include <cmdHelper.h>
|
||||
#include <logger.h>
|
||||
#include <PontCoopScheduler.h>
|
||||
|
||||
#include <eeprom.h>
|
||||
|
||||
|
||||
static bool globalStatsCmd(uint8_t argc, char **args) {
|
||||
uint32_t uptime = HAL_GetTick() / 1000;
|
||||
sendFormatString(\
|
||||
"Current uptime: %ld\n\r" \
|
||||
"\n\r",
|
||||
uptime
|
||||
);
|
||||
|
||||
t_deviceStats *deviceStats = getGlobalDeviceStats();
|
||||
sendFormatString(\
|
||||
"Global Device statistics\n\r" \
|
||||
" Total running hours: %ld\n\r" \
|
||||
" Total power cycles: %ld\n\r" \
|
||||
" Total watchdog resets: %ld\n\r" \
|
||||
"\n\r",
|
||||
deviceStats->totalRunningHours, deviceStats->totalPowercycles,
|
||||
deviceStats->totalWatchdogResets
|
||||
);
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
static bool resetStatsCmd(uint8_t argc, char **args) {
|
||||
t_deviceStats *deviceStats = getGlobalDeviceStats();
|
||||
deviceStats->totalWatchdogResets = 0;
|
||||
schAdd(eepromHourlyUpdateDeviceStats, NULL, 1, 0);
|
||||
return true;
|
||||
}
|
||||
|
||||
|
||||
const cmd_t COMMANDS[] = {
|
||||
{ .name = "stats", .cmdFunc = globalStatsCmd,
|
||||
.help = \
|
||||
"stats .......................... Show the device statistics\n\r"
|
||||
},
|
||||
{ .name = "resetStats", .cmdFunc = resetStatsCmd,
|
||||
.help = \
|
||||
"resetStats ..................... Reset the device statistics\n\r"
|
||||
},
|
||||
{ .name = "END_OF_CMDS", .help = "",.cmdFunc = NULL }
|
||||
};
|
||||
|
||||
const cmd_t *getRegularCommands() {
|
||||
return COMMANDS;
|
||||
}
|
@ -13,7 +13,6 @@
|
||||
#include <dns.h>
|
||||
#include <sntp.h>
|
||||
#include <config.h>
|
||||
#include <iwdg.h>
|
||||
|
||||
|
||||
static t_configBlock *config;
|
||||
@ -27,14 +26,9 @@ static uint8_t dhcpBuffer[DHCP_BUFFER_SIZE];
|
||||
static uint8_t dnsBuffer[DNS_BUFFER_SIZE];
|
||||
|
||||
|
||||
static uint8_t sntpBuffer[MAX_SNTP_BUF_SIZE];
|
||||
static t_seconds seconds = { .seconds = 0, .missedUpdates = 0, .valid = false };
|
||||
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;
|
||||
@ -135,70 +129,12 @@ bool wizDnsQuery(char *name, uint8_t *ip) {
|
||||
}
|
||||
|
||||
|
||||
static void wizSNTPHandler(void *handle) {
|
||||
bool success = false;
|
||||
|
||||
if (networkAvailable) {
|
||||
coloredMsg(LOG_BLUE, "wizsh, about to call SNTP");
|
||||
|
||||
uint8_t ntpServer[4];
|
||||
if (wizDnsQuery(config->ntpServer, ntpServer)) {
|
||||
SNTP_init(SNTP_SOCK, ntpServer, 0, sntpBuffer);
|
||||
bool updated = false;
|
||||
for (uint8_t i = 0; i < 16; i++) {
|
||||
datetime curTime;
|
||||
int8_t res = SNTP_run(&curTime);
|
||||
uint64_t receivedSeconds = curTime.seconds - UNIX_NTP_EPOCH_DIFF;
|
||||
if (res == 1) {
|
||||
if (seconds.seconds != receivedSeconds) {
|
||||
coloredMsg(LOG_BLUE, "wizsh, time deviation: %lu %lu",
|
||||
seconds.seconds, receivedSeconds);
|
||||
} else {
|
||||
coloredMsg(LOG_BLUE, "wizsh, time still matching");
|
||||
}
|
||||
|
||||
seconds.seconds = receivedSeconds;
|
||||
seconds.valid = true;
|
||||
seconds.missedUpdates = 0;
|
||||
updated = true;
|
||||
success = true;
|
||||
coloredMsg(LOG_BLUE, "wizsh, curTime: %lu", seconds.seconds);
|
||||
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
if (! updated) {
|
||||
coloredMsg(LOG_BLUE, "wizsh, time update failed");
|
||||
seconds.missedUpdates += 1;
|
||||
}
|
||||
} else {
|
||||
seconds.missedUpdates += 1;
|
||||
coloredMsg(LOG_BLUE, "wizsh, error when querying ntp server name");
|
||||
}
|
||||
}
|
||||
|
||||
uint16_t tryAgainIn = (success) ? 3600 : 1;
|
||||
schAdd(wizSNTPHandler, NULL, tryAgainIn * 1000, 0);
|
||||
coloredMsg(LOG_BLUE, "wizsh, next sntp request in %d seconds", tryAgainIn);
|
||||
}
|
||||
|
||||
static void wizSecondsHandler(void *handle) {
|
||||
seconds.seconds += 1;
|
||||
HAL_IWDG_Refresh(&hiwdg);
|
||||
}
|
||||
|
||||
t_seconds* wizGetSeconds() {
|
||||
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();
|
||||
|
||||
static uint8_t lastStablePhyLink = 255;
|
||||
static bool dhcpInitialized = false;
|
||||
static bool sntpInitialized = false;
|
||||
|
||||
uint8_t phyLink = 0;
|
||||
int8_t res = ctlwizchip(CW_GET_PHYLINK, (void*) &phyLink);
|
||||
@ -218,12 +154,6 @@ static void wizPhyLinkHandler(void *handle) {
|
||||
coloredMsg(LOG_BLUE, "wizplh, DHCP handler scheduled");
|
||||
|
||||
dhcpInitialized = true;
|
||||
|
||||
|
||||
schAdd(wizSNTPHandler, NULL, 15, 0);
|
||||
coloredMsg(LOG_BLUE, "wizplh, SNTP handler scheduled");
|
||||
|
||||
sntpInitialized = true;
|
||||
} else {
|
||||
networkAvailable = false;
|
||||
show(LED_GREEN, BLINK);
|
||||
@ -239,14 +169,6 @@ static void wizPhyLinkHandler(void *handle) {
|
||||
|
||||
dhcpInitialized = false;
|
||||
}
|
||||
|
||||
if (sntpInitialized) {
|
||||
schDel(wizSNTPHandler, NULL);
|
||||
coloredMsg(LOG_BLUE, "wizplh, SNTP handler unscheduled");
|
||||
seconds.valid = false;
|
||||
|
||||
sntpInitialized = false;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -255,7 +177,7 @@ static void wizPhyLinkHandler(void *handle) {
|
||||
int wizInit() {
|
||||
config = getConfig();
|
||||
netInfo.dhcp = NETINFO_DHCP;
|
||||
memcpy(netInfo.mac, config->macAddress, 6);
|
||||
memcpy(netInfo.mac, config->networkspecific.lan.macAddress, 6);
|
||||
|
||||
coloredMsg(LOG_BLUE, "wizI, resetting Ethernet module");
|
||||
wizReset(true);
|
||||
@ -304,7 +226,5 @@ int wizInit() {
|
||||
schAdd(wizPhyLinkHandler, NULL, 0, 1000);
|
||||
coloredMsg(LOG_BLUE, "wizI, PhyLink handler scheduled");
|
||||
|
||||
schAdd(wizSecondsHandler, NULL, 0, 1000);
|
||||
coloredMsg(LOG_BLUE, "wizI, Seconds handler scheduled");
|
||||
return 0;
|
||||
}
|
||||
|
@ -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
|
||||
|
@ -270,7 +270,10 @@ 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", buf->s.deviceId);
|
||||
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;
|
||||
|
||||
|
@ -6,8 +6,8 @@ MAIN_C_BAK=${MAIN_C}-bak
|
||||
IT_C=./Core/Src/stm32f1xx_it.c
|
||||
IT_C_BAK=${IT_C}-bak
|
||||
|
||||
MAKEFILE=./Makefile
|
||||
MAKEFILE_BAK=${MAKEFILE}-bak
|
||||
# MAKEFILE=./Makefile
|
||||
# MAKEFILE_BAK=${MAKEFILE}-bak
|
||||
|
||||
PROCESSED="Processed by $0"
|
||||
|
||||
@ -31,7 +31,7 @@ checkFile () {
|
||||
|
||||
|
||||
checkFile $MAIN_C $MAIN_C_BAK
|
||||
checkFile $MAKEFILE $MAKEFILE_BAK
|
||||
# checkFile $MAKEFILE $MAKEFILE_BAK
|
||||
checkFile $IT_C $IT_C_BAK
|
||||
|
||||
cp $MAIN_C $MAIN_C_BAK
|
||||
@ -62,22 +62,22 @@ for I in hottislib/*.c; do
|
||||
SRC_EXT+="$I "
|
||||
done
|
||||
|
||||
cp $MAKEFILE $MAKEFILE_BAK
|
||||
echo "# $PROCESSED" > $MAKEFILE
|
||||
cat $MAKEFILE_BAK | \
|
||||
sed -e 's/\(-specs=nano.specs\)/\1 -u _printf_float/' \
|
||||
-e 's/\(-Wall\)/\1 -Werror/' \
|
||||
-e 's%\(# list of ASM program objects\)%OBJECTS += $(addprefix $(BUILD_DIR)/,w5500.a)\n\1%' \
|
||||
-e 's,\($(BUILD_DIR)/%.o: %.c Makefile | $(BUILD_DIR)\),$(BUILD_DIR)/w5500.a:\n\t(cd ioLibrary_Driver \&\& $(MAKE) \&\& cp w5500.a ../$(BUILD_DIR) \&\& cd ..)\n\n\1,' \
|
||||
-e 's,\(C_SOURCES = \\\),\1\n'"$SRC_EXT"' \\,' \
|
||||
-e 's,\(C_INCLUDES = \\\),\1\n-IioLibrary_Driver/Ethernet \\,' \
|
||||
-e 's,\(C_INCLUDES = \\\),\1\n-IioLibrary_Driver/Internet/DHCP \\,' \
|
||||
-e 's,\(C_INCLUDES = \\\),\1\n-IioLibrary_Driver/Internet/DNS \\,' \
|
||||
-e 's,\(C_INCLUDES = \\\),\1\n-IioLibrary_Driver/Internet/SNTP \\,' \
|
||||
-e 's,\(C_INCLUDES = \\\),\1\n-IUser/Inc \\,' \
|
||||
-e 's,\(C_INCLUDES = \\\),\1\n-Ihottislib \\,' \
|
||||
>> $MAKEFILE
|
||||
|
||||
# cp $MAKEFILE $MAKEFILE_BAK
|
||||
# echo "# $PROCESSED" > $MAKEFILE
|
||||
# cat $MAKEFILE_BAK | \
|
||||
# sed -e 's/\(-specs=nano.specs\)/\1 -u _printf_float/' \
|
||||
# -e 's/\(-Wall\)/\1 -Werror/' \
|
||||
# -e 's%\(# list of ASM program objects\)%OBJECTS += $(addprefix $(BUILD_DIR)/,w5500.a)\n\1%' \
|
||||
# -e 's,\($(BUILD_DIR)/%.o: %.c Makefile | $(BUILD_DIR)\),$(BUILD_DIR)/w5500.a:\n\t(cd ioLibrary_Driver \&\& $(MAKE) \&\& cp w5500.a ../$(BUILD_DIR) \&\& cd ..)\n\n\1,' \
|
||||
# -e 's,\(C_SOURCES = \\\),\1\n'"$SRC_EXT"' \\,' \
|
||||
# -e 's,\(C_INCLUDES = \\\),\1\n-IioLibrary_Driver/Ethernet \\,' \
|
||||
# -e 's,\(C_INCLUDES = \\\),\1\n-IioLibrary_Driver/Internet/DHCP \\,' \
|
||||
# -e 's,\(C_INCLUDES = \\\),\1\n-IioLibrary_Driver/Internet/DNS \\,' \
|
||||
# -e 's,\(C_INCLUDES = \\\),\1\n-IioLibrary_Driver/Internet/SNTP \\,' \
|
||||
# -e 's,\(C_INCLUDES = \\\),\1\n-IUser/Inc \\,' \
|
||||
# -e 's,\(C_INCLUDES = \\\),\1\n-Ihottislib \\,' \
|
||||
# >> $MAKEFILE
|
||||
cp Makefile.sav Makefile
|
||||
|
||||
|
||||
|
||||
|
@ -1,3 +1,3 @@
|
||||
#!/bin/bash
|
||||
|
||||
cp Makefile Makefile.sav
|
||||
rm -rf build/ Core/ Drivers/ Makefile Makefile-bak startup_stm32f103xb.s STM32F103C8Tx_FLASH.ld
|
||||
|
Reference in New Issue
Block a user