Compare commits
63 Commits
Author | SHA1 | Date | |
---|---|---|---|
9e3fa03053 | |||
6010facb07 | |||
ea459e7dbd
|
|||
b57eadb5e7
|
|||
62d847b9da
|
|||
19ea1ec097
|
|||
badc6aa1f5
|
|||
c4736640cd | |||
72f2c72280 | |||
f52efb4f27 | |||
5254b318f8 | |||
a13feaac02 | |||
d73b97ecb2 | |||
d97e941b48
|
|||
ce4562762a | |||
10b4916a4c | |||
f542bfc71f
|
|||
7e80a6d160 | |||
7d60c537f9
|
|||
47fb07d055
|
|||
5d58a9e526
|
|||
ba351b773a
|
|||
681e852cec
|
|||
b984284a20
|
|||
6f425d0353
|
|||
5e5b6e62ca
|
|||
1e9b811592
|
|||
6fc7dd2e77
|
|||
c80cb92c53
|
|||
a97fc6f725
|
|||
d4a6b9ac53
|
|||
c84dd1acba
|
|||
e92bf0817e
|
|||
9eee986123
|
|||
446a279277
|
|||
4be07210e0
|
|||
1796830059
|
|||
0ad3cba113
|
|||
49b0974022
|
|||
bb3b626db1
|
|||
e357072888
|
|||
bfbf686298 | |||
b7df92d079 | |||
59e3350aaf | |||
6c21983917 | |||
80103312ce | |||
9213cfac11 | |||
07c80eb21c | |||
eb83c754bf | |||
4f81343a89 | |||
e8102e74a9 | |||
784346b833 | |||
e3ee3a08e8 | |||
5ef114b634 | |||
2dd586d7e4 | |||
5d1f7dde89 | |||
8d56c5aff4 | |||
5230139544 | |||
08c76f13de | |||
6a13da024d
|
|||
8c0c864905 | |||
61b1498d7b | |||
e21e114313 |
44
src/Makefile
44
src/Makefile
@ -1,17 +1,51 @@
|
|||||||
CC=gcc
|
CC=gcc
|
||||||
|
|
||||||
CFLAGS=-Wall
|
CFLAGS=-Wall
|
||||||
LDFLAGS=-lwiringPi -lcurl -lconfig
|
LDFLAGS=-lwiringPi -lconfig
|
||||||
|
|
||||||
counter: counter.o LS7366R.o influx.o ringbuffer.o led.o
|
INST_DIR=/opt/sbin
|
||||||
$(CC) -o $@ $(LDFLAGS) $^
|
|
||||||
|
|
||||||
.c.o:
|
REFCNT := $(shell git rev-list --all --count)
|
||||||
$(CC) $(CFLAGS) -c $<
|
VERSION := $(shell git rev-parse --short=8 HEAD)
|
||||||
|
|
||||||
.PHONY: all
|
.PHONY: all
|
||||||
all: counter
|
all: counter
|
||||||
|
|
||||||
|
counter: counter.o LS7366R.o ringbuffer.o led.o logging.o sinkSender.o sha256.o version.o
|
||||||
|
$(CC) -o $@ $(LDFLAGS) $^
|
||||||
|
|
||||||
|
version.o: version.c VERSION
|
||||||
|
$(CC) -DD_REFCNT=$(REFCNT) -DD_VERSION=\"$(VERSION)\" -c $<
|
||||||
|
|
||||||
|
.c.o:
|
||||||
|
$(CC) $(CFLAGS) -c $<
|
||||||
|
|
||||||
.PHONY: clean
|
.PHONY: clean
|
||||||
clean:
|
clean:
|
||||||
-rm -f *.o counter
|
-rm -f *.o counter
|
||||||
|
|
||||||
|
.PHONY: update
|
||||||
|
update:
|
||||||
|
sudo systemctl stop counter
|
||||||
|
sudo cp counter $(INST_DIR)
|
||||||
|
sudo systemctl start counter
|
||||||
|
|
||||||
|
.PHONY: install
|
||||||
|
install:
|
||||||
|
sudo mkdir -p $(INST_DIR)
|
||||||
|
sudo cp counter counter.service $(INST_DIR)
|
||||||
|
sudo systemctl enable $(INST_DIR)/counter.service
|
||||||
|
sudo systemctl start counter
|
||||||
|
|
||||||
|
.PHONY: stop
|
||||||
|
stop:
|
||||||
|
sudo systemctl stop counter
|
||||||
|
|
||||||
|
.PHONY: start
|
||||||
|
start:
|
||||||
|
sudo systemctl start counter
|
||||||
|
|
||||||
|
.PHONY: restart
|
||||||
|
restart:
|
||||||
|
sudo systemctl restart counter
|
||||||
|
|
||||||
|
1
src/VERSION
Normal file
1
src/VERSION
Normal file
@ -0,0 +1 @@
|
|||||||
|
0.98
|
@ -5,24 +5,29 @@
|
|||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
#include <unistd.h>
|
#include <unistd.h>
|
||||||
#include <libconfig.h>
|
#include <libconfig.h>
|
||||||
|
#include <math.h>
|
||||||
|
#include <time.h>
|
||||||
|
|
||||||
#include "LS7366R.h"
|
#include "LS7366R.h"
|
||||||
#include "influx.h"
|
|
||||||
#include "ringbuffer.h"
|
#include "ringbuffer.h"
|
||||||
#include "led.h"
|
#include "led.h"
|
||||||
|
#include "logging.h"
|
||||||
|
#include "sinkSender.h"
|
||||||
|
|
||||||
|
|
||||||
|
extern char VERSION[];
|
||||||
|
extern uint32_t REFCNT;
|
||||||
|
|
||||||
const int CTRL_OUT = 16;
|
const int CTRL_OUT = 16;
|
||||||
const int INTR_IN = 19;
|
const int INTR_IN = 19;
|
||||||
const int SPI_CHAN = 0;
|
const int SPI_CHAN = 0;
|
||||||
const int SPI_SPEED = 1000000;
|
const int SPI_SPEED = 1000000;
|
||||||
|
|
||||||
|
const uint32_t PRECISION = 1000;
|
||||||
|
const uint32_t COUNTER_FREQUENCY = 1e6;
|
||||||
|
|
||||||
config_t cfg;
|
config_t cfg;
|
||||||
|
|
||||||
const char EPSILON_KEY[] = "epsilon";
|
|
||||||
const double DEFAULT_EPSILON = 0.01;
|
|
||||||
|
|
||||||
uint32_t skipped = 0;
|
|
||||||
|
|
||||||
|
|
||||||
void isr() {
|
void isr() {
|
||||||
@ -51,9 +56,9 @@ void init() {
|
|||||||
void readConfig() {
|
void readConfig() {
|
||||||
config_init(&cfg);
|
config_init(&cfg);
|
||||||
if (! config_read_file(&cfg, "/opt/etc/counter.cfg")) {
|
if (! config_read_file(&cfg, "/opt/etc/counter.cfg")) {
|
||||||
fprintf(stderr, "failed to read config file: %s:%d - %s\n",
|
logmsg(LOG_ERR, "failed to read config file: %s:%d - %s\n",
|
||||||
config_error_file(&cfg), config_error_line(&cfg),
|
config_error_file(&cfg), config_error_line(&cfg),
|
||||||
config_error_text(&cfg));
|
config_error_text(&cfg));
|
||||||
config_destroy(&cfg);
|
config_destroy(&cfg);
|
||||||
exit(-1);
|
exit(-1);
|
||||||
}
|
}
|
||||||
@ -64,47 +69,43 @@ void start() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
int main (void) {
|
int main (void) {
|
||||||
|
logmsg(LOG_INFO, "VERSION: %s, REFCNT: %u\n", VERSION, REFCNT);
|
||||||
|
|
||||||
readConfig();
|
readConfig();
|
||||||
init();
|
init();
|
||||||
ledInit();
|
ledInit(&cfg);
|
||||||
ls7366rInit(SPI_CHAN);
|
ls7366rInit(SPI_CHAN);
|
||||||
influxInit(&cfg);
|
sinkSenderInit(&cfg);
|
||||||
start();
|
start();
|
||||||
|
|
||||||
double epsilon;
|
struct timespec timestamp;
|
||||||
if (! config_lookup_float(&cfg, EPSILON_KEY, &epsilon)) {
|
uint32_t last_seconds = 0;
|
||||||
epsilon = DEFAULT_EPSILON;
|
uint32_t last_milliseconds = 0;
|
||||||
}
|
uint32_t mainsCntSum = 0;
|
||||||
|
uint32_t mainsCntCnt = 0;
|
||||||
|
|
||||||
|
|
||||||
double lastF = 0;
|
|
||||||
bool settled = false;
|
|
||||||
uint8_t ledTick = 0;
|
|
||||||
while (1) {
|
while (1) {
|
||||||
uint32_t diff = ringbufferGet();
|
uint32_t period = ringbufferGet();
|
||||||
|
|
||||||
double f = 1.0 / (((double) diff) / 1000000.0);
|
clock_gettime(CLOCK_REALTIME, ×tamp);
|
||||||
|
uint32_t current_seconds = timestamp.tv_sec;
|
||||||
|
uint32_t current_milliseconds = timestamp.tv_nsec / 1e6;
|
||||||
|
uint32_t duration = ((current_seconds - last_seconds) * 1000) + (current_milliseconds - last_milliseconds);
|
||||||
|
|
||||||
if (settled && (abs(f - lastF) > epsilon)) {
|
mainsCntSum += period;
|
||||||
printf("Current f=%f, last f=%f, gradient too large, skipped\n", f, lastF);
|
mainsCntCnt += 1;
|
||||||
skipped++;
|
|
||||||
f = lastF;
|
if (duration >= 1000) {
|
||||||
|
last_seconds = current_seconds;
|
||||||
|
last_milliseconds = current_milliseconds;
|
||||||
|
|
||||||
|
uint32_t cnt = mainsCntSum / mainsCntCnt;
|
||||||
|
mainsCntSum = 0;
|
||||||
|
mainsCntCnt = 0;
|
||||||
|
uint32_t freq = PRECISION * COUNTER_FREQUENCY / cnt;
|
||||||
|
|
||||||
|
sinkSenderPut(current_seconds, freq);
|
||||||
}
|
}
|
||||||
lastF = f;
|
|
||||||
// printf("%f\n", f);
|
|
||||||
influxAddFrequency(f);
|
|
||||||
|
|
||||||
ledTick++;
|
|
||||||
if (ledTick == 50) {
|
|
||||||
ledTick = 0;
|
|
||||||
led(E_GREEN, false);
|
|
||||||
if (! settled) {
|
|
||||||
printf("Now it is settled\n");
|
|
||||||
settled = true;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// will never be reached
|
// will never be reached
|
||||||
|
@ -1,3 +1,8 @@
|
|||||||
influxUser = "secundus"
|
led = "off"
|
||||||
influxPass = "geheim"
|
|
||||||
influxUrl = "http://172.16.3.15:8086/write?db=smarthome2&precision=ms"
|
sinkServer = "sink.hottis.de"
|
||||||
|
sinkPort = 20169
|
||||||
|
|
||||||
|
deviceId = "mainscnt03"
|
||||||
|
// sharedSecret has to have exactly 31 octets
|
||||||
|
sharedSecret = "1234567890123456789012345678901"
|
@ -6,9 +6,7 @@ After=network-online.target
|
|||||||
|
|
||||||
[Service]
|
[Service]
|
||||||
Type=simple
|
Type=simple
|
||||||
GuessMainPID=yes
|
ExecStart=/opt/sbin/counter
|
||||||
ExecStart=/opt/bin/counter
|
|
||||||
ExecStop=kill -SIGINT $mainpid
|
|
||||||
Restart=on-failure
|
Restart=on-failure
|
||||||
WorkingDirectory=/tmp
|
WorkingDirectory=/tmp
|
||||||
|
|
||||||
|
104
src/influx.c
104
src/influx.c
@ -1,104 +0,0 @@
|
|||||||
#include <stdint.h>
|
|
||||||
#include <stdio.h>
|
|
||||||
#include <curl/curl.h>
|
|
||||||
#include <time.h>
|
|
||||||
#include <string.h>
|
|
||||||
#include <unistd.h>
|
|
||||||
#include <libconfig.h>
|
|
||||||
|
|
||||||
#include "led.h"
|
|
||||||
|
|
||||||
|
|
||||||
const char INFLUXURL_KEY[] = "influxUrl";
|
|
||||||
const char DEFAULT_INFLUXURL[] = "http://172.16.3.15:8086/write?db=smarthome2&precision=ms";
|
|
||||||
const char *influxUrl;
|
|
||||||
const char INFLUXUSER_KEY[] = "influxUser";
|
|
||||||
const char *influxUser;
|
|
||||||
const char INFLUXPASS_KEY[] = "influxPass";
|
|
||||||
const char *influxPass;
|
|
||||||
const char INFLUXTAG_KEY[] = "influxTag";
|
|
||||||
const char *influxTag;
|
|
||||||
|
|
||||||
extern uint32_t skipped;
|
|
||||||
|
|
||||||
|
|
||||||
// #define BUFSIZE 131070
|
|
||||||
#define BUFSIZE 65535
|
|
||||||
// #define BUFSIZE 1024
|
|
||||||
char influxBuffer[BUFSIZE];
|
|
||||||
char *bufferNextEntry;
|
|
||||||
|
|
||||||
#define HOSTNAMESIZE 128
|
|
||||||
char hostname[HOSTNAMESIZE];
|
|
||||||
|
|
||||||
static void influxClearBuffer() {
|
|
||||||
memset(influxBuffer, 0, BUFSIZE);
|
|
||||||
bufferNextEntry = influxBuffer;
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
void influxInit(config_t *pCfg) {
|
|
||||||
if (! config_lookup_string(pCfg, INFLUXURL_KEY, &influxUrl)) {
|
|
||||||
influxUrl = DEFAULT_INFLUXURL;
|
|
||||||
}
|
|
||||||
if (! config_lookup_string(pCfg, INFLUXUSER_KEY, &influxUser)) {
|
|
||||||
influxUser = NULL;
|
|
||||||
}
|
|
||||||
if (! config_lookup_string(pCfg, INFLUXPASS_KEY, &influxPass)) {
|
|
||||||
influxPass = NULL;
|
|
||||||
}
|
|
||||||
if (! config_lookup_string(pCfg, INFLUXTAG_KEY, &influxTag)) {
|
|
||||||
gethostname(hostname, HOSTNAMESIZE);
|
|
||||||
influxTag = hostname;
|
|
||||||
}
|
|
||||||
influxClearBuffer();
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
static void influxSendRequest() {
|
|
||||||
led(E_RED, false);
|
|
||||||
led(E_BLUE, true);
|
|
||||||
CURL *curl = curl_easy_init();
|
|
||||||
if(curl) {
|
|
||||||
curl_easy_setopt(curl, CURLOPT_URL, influxUrl);
|
|
||||||
if (influxUser && influxPass) {
|
|
||||||
curl_easy_setopt(curl, CURLOPT_HTTPAUTH, CURLAUTH_DIGEST);
|
|
||||||
curl_easy_setopt(curl, CURLOPT_USERNAME, influxUser);
|
|
||||||
curl_easy_setopt(curl, CURLOPT_PASSWORD, influxPass);
|
|
||||||
}
|
|
||||||
curl_easy_setopt(curl, CURLOPT_POSTFIELDS, influxBuffer);
|
|
||||||
CURLcode res = curl_easy_perform(curl);
|
|
||||||
if(res != CURLE_OK) {
|
|
||||||
fprintf(stderr, "curl_easy_perform() failed: %s\n", curl_easy_strerror(res));
|
|
||||||
led(E_RED, true);
|
|
||||||
}
|
|
||||||
curl_easy_cleanup(curl);
|
|
||||||
}
|
|
||||||
led(E_BLUE, false);
|
|
||||||
}
|
|
||||||
|
|
||||||
void influxAddFrequency(double f) {
|
|
||||||
static uint32_t entries = 0;
|
|
||||||
static uint32_t totalEntries = 0;
|
|
||||||
char tmpBuf[256];
|
|
||||||
struct timespec t;
|
|
||||||
|
|
||||||
clock_gettime(CLOCK_REALTIME, &t);
|
|
||||||
uint64_t tt = (((uint64_t)t.tv_sec) * 1000) + (((uint64_t)t.tv_nsec) / 1000000);
|
|
||||||
int c = sprintf(tmpBuf, "mainsfrequency,host=%s freq=%f %llu\n", influxTag, f, tt);
|
|
||||||
|
|
||||||
if ((bufferNextEntry + c + 10) > (influxBuffer + BUFSIZE)) {
|
|
||||||
influxSendRequest();
|
|
||||||
influxClearBuffer();
|
|
||||||
totalEntries += entries;
|
|
||||||
fprintf(stderr, "%u entries sent to database, in total %u, skipped: %u\n", entries, totalEntries, skipped);
|
|
||||||
entries = 0;
|
|
||||||
}
|
|
||||||
|
|
||||||
memcpy(bufferNextEntry, tmpBuf, c);
|
|
||||||
bufferNextEntry += c;
|
|
||||||
entries++;
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
@ -1,9 +0,0 @@
|
|||||||
#ifndef _INFLUX_H_
|
|
||||||
#define _INFLUX_H_
|
|
||||||
|
|
||||||
#include <libconfig.h>
|
|
||||||
|
|
||||||
void influxAddFrequency(double f);
|
|
||||||
void influxInit(config_t *pCfg);
|
|
||||||
|
|
||||||
#endif // _INFLUX_H_
|
|
42
src/led.c
42
src/led.c
@ -1,32 +1,46 @@
|
|||||||
#include <stdbool.h>
|
#include <stdbool.h>
|
||||||
#include <wiringPi.h>
|
#include <wiringPi.h>
|
||||||
|
#include <libconfig.h>
|
||||||
|
#include <stdbool.h>
|
||||||
|
#include <strings.h>
|
||||||
|
|
||||||
#include "led.h"
|
#include "led.h"
|
||||||
|
#include "logging.h"
|
||||||
|
|
||||||
|
|
||||||
|
const char LED_KEY[] = "led";
|
||||||
|
const char DEFAULT_LED[] = "off";
|
||||||
|
const char *ledsActiveStr;
|
||||||
|
bool ledsActive;
|
||||||
|
|
||||||
const int GREEN_OUT = 20;
|
const int GREEN_OUT = 20;
|
||||||
const int RED_OUT = 21;
|
const int RED_OUT = 21;
|
||||||
const int BLUE_OUT = 26;
|
const int BLUE_OUT = 26;
|
||||||
|
|
||||||
void ledInit() {
|
void ledInit(config_t *pCfg) {
|
||||||
pinMode(GREEN_OUT, OUTPUT);
|
if (! config_lookup_string(pCfg, LED_KEY, &ledsActiveStr)) {
|
||||||
digitalWrite(GREEN_OUT, 0);
|
ledsActiveStr = DEFAULT_LED;
|
||||||
|
}
|
||||||
|
ledsActive = (0 == strcasecmp(ledsActiveStr, "on")) || (0 == strcasecmp(ledsActiveStr, "true"));
|
||||||
|
logmsg(LOG_INFO, "CONFIG: ledsActive=%d, ledsActiveStr=%s\n", ledsActive, ledsActiveStr);
|
||||||
|
|
||||||
pinMode(RED_OUT, OUTPUT);
|
pinMode(GREEN_OUT, OUTPUT);
|
||||||
digitalWrite(RED_OUT, 0);
|
digitalWrite(GREEN_OUT, 0);
|
||||||
|
|
||||||
pinMode(BLUE_OUT, OUTPUT);
|
pinMode(RED_OUT, OUTPUT);
|
||||||
digitalWrite(BLUE_OUT, 0);
|
digitalWrite(RED_OUT, 0);
|
||||||
|
|
||||||
|
pinMode(BLUE_OUT, OUTPUT);
|
||||||
|
digitalWrite(BLUE_OUT, 0);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
void led (tColor color, bool state) {
|
void led (tColor color, bool state) {
|
||||||
if (color == E_RED) {
|
if (color == E_RED) {
|
||||||
digitalWrite(RED_OUT, state ? 1 : 0);
|
digitalWrite(RED_OUT, state ? 1 : 0);
|
||||||
} else if (color == E_BLUE) {
|
} else if (color == E_BLUE && ledsActive) {
|
||||||
digitalWrite(BLUE_OUT, state ? 1 : 0);
|
digitalWrite(BLUE_OUT, state ? 1 : 0);
|
||||||
} else if (color == E_GREEN) {
|
} else if (color == E_GREEN && ledsActive) {
|
||||||
digitalWrite(GREEN_OUT, state ? 1 : 0);
|
digitalWrite(GREEN_OUT, state ? 1 : 0);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -2,10 +2,11 @@
|
|||||||
#define _LED_H_
|
#define _LED_H_
|
||||||
|
|
||||||
#include <stdbool.h>
|
#include <stdbool.h>
|
||||||
|
#include <libconfig.h>
|
||||||
|
|
||||||
typedef enum { E_RED, E_BLUE, E_GREEN } tColor;
|
typedef enum { E_RED, E_BLUE, E_GREEN } tColor;
|
||||||
|
|
||||||
void ledInit();
|
void ledInit(config_t *pCfg);
|
||||||
void led(tColor color, bool state);
|
void led(tColor color, bool state);
|
||||||
|
|
||||||
#endif // _LED_H_
|
#endif // _LED_H_
|
13
src/logging.c
Normal file
13
src/logging.c
Normal file
@ -0,0 +1,13 @@
|
|||||||
|
#include <syslog.h>
|
||||||
|
#include <stdarg.h>
|
||||||
|
|
||||||
|
void logmsg(int prio, const char* format, ...) {
|
||||||
|
va_list vl;
|
||||||
|
|
||||||
|
openlog("counter", 0, LOG_LOCAL0);
|
||||||
|
va_start(vl, format);
|
||||||
|
vsyslog(prio, format, vl);
|
||||||
|
va_end(vl);
|
||||||
|
closelog();
|
||||||
|
}
|
||||||
|
|
8
src/logging.h
Normal file
8
src/logging.h
Normal file
@ -0,0 +1,8 @@
|
|||||||
|
#ifndef _LOGGING_H_
|
||||||
|
#define _LOGGING_H_
|
||||||
|
|
||||||
|
#include <syslog.h>
|
||||||
|
|
||||||
|
void logmsg(int prio, const char* format, ...);
|
||||||
|
|
||||||
|
#endif // _LOGGING_H_
|
158
src/sha256.c
Normal file
158
src/sha256.c
Normal file
@ -0,0 +1,158 @@
|
|||||||
|
/*********************************************************************
|
||||||
|
* Filename: sha256.c
|
||||||
|
* Author: Brad Conte (brad AT bradconte.com)
|
||||||
|
* Copyright:
|
||||||
|
* Disclaimer: This code is presented "as is" without any guarantees.
|
||||||
|
* Details: Implementation of the SHA-256 hashing algorithm.
|
||||||
|
SHA-256 is one of the three algorithms in the SHA2
|
||||||
|
specification. The others, SHA-384 and SHA-512, are not
|
||||||
|
offered in this implementation.
|
||||||
|
Algorithm specification can be found here:
|
||||||
|
* http://csrc.nist.gov/publications/fips/fips180-2/fips180-2withchangenotice.pdf
|
||||||
|
This implementation uses little endian byte order.
|
||||||
|
*********************************************************************/
|
||||||
|
|
||||||
|
/*************************** HEADER FILES ***************************/
|
||||||
|
#include <stdlib.h>
|
||||||
|
#include <memory.h>
|
||||||
|
#include "sha256.h"
|
||||||
|
|
||||||
|
/****************************** MACROS ******************************/
|
||||||
|
#define ROTLEFT(a,b) (((a) << (b)) | ((a) >> (32-(b))))
|
||||||
|
#define ROTRIGHT(a,b) (((a) >> (b)) | ((a) << (32-(b))))
|
||||||
|
|
||||||
|
#define CH(x,y,z) (((x) & (y)) ^ (~(x) & (z)))
|
||||||
|
#define MAJ(x,y,z) (((x) & (y)) ^ ((x) & (z)) ^ ((y) & (z)))
|
||||||
|
#define EP0(x) (ROTRIGHT(x,2) ^ ROTRIGHT(x,13) ^ ROTRIGHT(x,22))
|
||||||
|
#define EP1(x) (ROTRIGHT(x,6) ^ ROTRIGHT(x,11) ^ ROTRIGHT(x,25))
|
||||||
|
#define SIG0(x) (ROTRIGHT(x,7) ^ ROTRIGHT(x,18) ^ ((x) >> 3))
|
||||||
|
#define SIG1(x) (ROTRIGHT(x,17) ^ ROTRIGHT(x,19) ^ ((x) >> 10))
|
||||||
|
|
||||||
|
/**************************** VARIABLES *****************************/
|
||||||
|
static const WORD k[64] = {
|
||||||
|
0x428a2f98,0x71374491,0xb5c0fbcf,0xe9b5dba5,0x3956c25b,0x59f111f1,0x923f82a4,0xab1c5ed5,
|
||||||
|
0xd807aa98,0x12835b01,0x243185be,0x550c7dc3,0x72be5d74,0x80deb1fe,0x9bdc06a7,0xc19bf174,
|
||||||
|
0xe49b69c1,0xefbe4786,0x0fc19dc6,0x240ca1cc,0x2de92c6f,0x4a7484aa,0x5cb0a9dc,0x76f988da,
|
||||||
|
0x983e5152,0xa831c66d,0xb00327c8,0xbf597fc7,0xc6e00bf3,0xd5a79147,0x06ca6351,0x14292967,
|
||||||
|
0x27b70a85,0x2e1b2138,0x4d2c6dfc,0x53380d13,0x650a7354,0x766a0abb,0x81c2c92e,0x92722c85,
|
||||||
|
0xa2bfe8a1,0xa81a664b,0xc24b8b70,0xc76c51a3,0xd192e819,0xd6990624,0xf40e3585,0x106aa070,
|
||||||
|
0x19a4c116,0x1e376c08,0x2748774c,0x34b0bcb5,0x391c0cb3,0x4ed8aa4a,0x5b9cca4f,0x682e6ff3,
|
||||||
|
0x748f82ee,0x78a5636f,0x84c87814,0x8cc70208,0x90befffa,0xa4506ceb,0xbef9a3f7,0xc67178f2
|
||||||
|
};
|
||||||
|
|
||||||
|
/*********************** FUNCTION DEFINITIONS ***********************/
|
||||||
|
void sha256_transform(SHA256_CTX *ctx, const BYTE data[])
|
||||||
|
{
|
||||||
|
WORD a, b, c, d, e, f, g, h, i, j, t1, t2, m[64];
|
||||||
|
|
||||||
|
for (i = 0, j = 0; i < 16; ++i, j += 4)
|
||||||
|
m[i] = (data[j] << 24) | (data[j + 1] << 16) | (data[j + 2] << 8) | (data[j + 3]);
|
||||||
|
for ( ; i < 64; ++i)
|
||||||
|
m[i] = SIG1(m[i - 2]) + m[i - 7] + SIG0(m[i - 15]) + m[i - 16];
|
||||||
|
|
||||||
|
a = ctx->state[0];
|
||||||
|
b = ctx->state[1];
|
||||||
|
c = ctx->state[2];
|
||||||
|
d = ctx->state[3];
|
||||||
|
e = ctx->state[4];
|
||||||
|
f = ctx->state[5];
|
||||||
|
g = ctx->state[6];
|
||||||
|
h = ctx->state[7];
|
||||||
|
|
||||||
|
for (i = 0; i < 64; ++i) {
|
||||||
|
t1 = h + EP1(e) + CH(e,f,g) + k[i] + m[i];
|
||||||
|
t2 = EP0(a) + MAJ(a,b,c);
|
||||||
|
h = g;
|
||||||
|
g = f;
|
||||||
|
f = e;
|
||||||
|
e = d + t1;
|
||||||
|
d = c;
|
||||||
|
c = b;
|
||||||
|
b = a;
|
||||||
|
a = t1 + t2;
|
||||||
|
}
|
||||||
|
|
||||||
|
ctx->state[0] += a;
|
||||||
|
ctx->state[1] += b;
|
||||||
|
ctx->state[2] += c;
|
||||||
|
ctx->state[3] += d;
|
||||||
|
ctx->state[4] += e;
|
||||||
|
ctx->state[5] += f;
|
||||||
|
ctx->state[6] += g;
|
||||||
|
ctx->state[7] += h;
|
||||||
|
}
|
||||||
|
|
||||||
|
void sha256_init(SHA256_CTX *ctx)
|
||||||
|
{
|
||||||
|
ctx->datalen = 0;
|
||||||
|
ctx->bitlen = 0;
|
||||||
|
ctx->state[0] = 0x6a09e667;
|
||||||
|
ctx->state[1] = 0xbb67ae85;
|
||||||
|
ctx->state[2] = 0x3c6ef372;
|
||||||
|
ctx->state[3] = 0xa54ff53a;
|
||||||
|
ctx->state[4] = 0x510e527f;
|
||||||
|
ctx->state[5] = 0x9b05688c;
|
||||||
|
ctx->state[6] = 0x1f83d9ab;
|
||||||
|
ctx->state[7] = 0x5be0cd19;
|
||||||
|
}
|
||||||
|
|
||||||
|
void sha256_update(SHA256_CTX *ctx, const BYTE data[], size_t len)
|
||||||
|
{
|
||||||
|
WORD i;
|
||||||
|
|
||||||
|
for (i = 0; i < len; ++i) {
|
||||||
|
ctx->data[ctx->datalen] = data[i];
|
||||||
|
ctx->datalen++;
|
||||||
|
if (ctx->datalen == 64) {
|
||||||
|
sha256_transform(ctx, ctx->data);
|
||||||
|
ctx->bitlen += 512;
|
||||||
|
ctx->datalen = 0;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void sha256_final(SHA256_CTX *ctx, BYTE hash[])
|
||||||
|
{
|
||||||
|
WORD i;
|
||||||
|
|
||||||
|
i = ctx->datalen;
|
||||||
|
|
||||||
|
// Pad whatever data is left in the buffer.
|
||||||
|
if (ctx->datalen < 56) {
|
||||||
|
ctx->data[i++] = 0x80;
|
||||||
|
while (i < 56)
|
||||||
|
ctx->data[i++] = 0x00;
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
ctx->data[i++] = 0x80;
|
||||||
|
while (i < 64)
|
||||||
|
ctx->data[i++] = 0x00;
|
||||||
|
sha256_transform(ctx, ctx->data);
|
||||||
|
memset(ctx->data, 0, 56);
|
||||||
|
}
|
||||||
|
|
||||||
|
// Append to the padding the total message's length in bits and transform.
|
||||||
|
ctx->bitlen += ctx->datalen * 8;
|
||||||
|
ctx->data[63] = ctx->bitlen;
|
||||||
|
ctx->data[62] = ctx->bitlen >> 8;
|
||||||
|
ctx->data[61] = ctx->bitlen >> 16;
|
||||||
|
ctx->data[60] = ctx->bitlen >> 24;
|
||||||
|
ctx->data[59] = ctx->bitlen >> 32;
|
||||||
|
ctx->data[58] = ctx->bitlen >> 40;
|
||||||
|
ctx->data[57] = ctx->bitlen >> 48;
|
||||||
|
ctx->data[56] = ctx->bitlen >> 56;
|
||||||
|
sha256_transform(ctx, ctx->data);
|
||||||
|
|
||||||
|
// Since this implementation uses little endian byte ordering and SHA uses big endian,
|
||||||
|
// reverse all the bytes when copying the final state to the output hash.
|
||||||
|
for (i = 0; i < 4; ++i) {
|
||||||
|
hash[i] = (ctx->state[0] >> (24 - i * 8)) & 0x000000ff;
|
||||||
|
hash[i + 4] = (ctx->state[1] >> (24 - i * 8)) & 0x000000ff;
|
||||||
|
hash[i + 8] = (ctx->state[2] >> (24 - i * 8)) & 0x000000ff;
|
||||||
|
hash[i + 12] = (ctx->state[3] >> (24 - i * 8)) & 0x000000ff;
|
||||||
|
hash[i + 16] = (ctx->state[4] >> (24 - i * 8)) & 0x000000ff;
|
||||||
|
hash[i + 20] = (ctx->state[5] >> (24 - i * 8)) & 0x000000ff;
|
||||||
|
hash[i + 24] = (ctx->state[6] >> (24 - i * 8)) & 0x000000ff;
|
||||||
|
hash[i + 28] = (ctx->state[7] >> (24 - i * 8)) & 0x000000ff;
|
||||||
|
}
|
||||||
|
}
|
35
src/sha256.h
Normal file
35
src/sha256.h
Normal file
@ -0,0 +1,35 @@
|
|||||||
|
/*********************************************************************
|
||||||
|
* Filename: sha256.h
|
||||||
|
* Author: Brad Conte (brad AT bradconte.com)
|
||||||
|
* Copyright:
|
||||||
|
* Disclaimer: This code is presented "as is" without any guarantees.
|
||||||
|
* Details: Defines the API for the corresponding SHA1 implementation.
|
||||||
|
*********************************************************************/
|
||||||
|
|
||||||
|
#ifndef SHA256_H
|
||||||
|
#define SHA256_H
|
||||||
|
|
||||||
|
/*************************** HEADER FILES ***************************/
|
||||||
|
#include <stddef.h>
|
||||||
|
#include <stdint.h>
|
||||||
|
|
||||||
|
/****************************** MACROS ******************************/
|
||||||
|
#define SHA256_BLOCK_SIZE 32 // SHA256 outputs a 32 byte digest
|
||||||
|
|
||||||
|
/**************************** DATA TYPES ****************************/
|
||||||
|
typedef uint8_t BYTE; // 8-bit byte
|
||||||
|
typedef uint32_t WORD; // 32-bit word, change to "long" for 16-bit machines
|
||||||
|
|
||||||
|
typedef struct {
|
||||||
|
BYTE data[64];
|
||||||
|
WORD datalen;
|
||||||
|
unsigned long long bitlen;
|
||||||
|
WORD state[8];
|
||||||
|
} SHA256_CTX;
|
||||||
|
|
||||||
|
/*********************** FUNCTION DECLARATIONS **********************/
|
||||||
|
void sha256_init(SHA256_CTX *ctx);
|
||||||
|
void sha256_update(SHA256_CTX *ctx, const BYTE data[], size_t len);
|
||||||
|
void sha256_final(SHA256_CTX *ctx, BYTE hash[]);
|
||||||
|
|
||||||
|
#endif // SHA256_H
|
148
src/sinkSender.c
Normal file
148
src/sinkSender.c
Normal file
@ -0,0 +1,148 @@
|
|||||||
|
#include <stdint.h>
|
||||||
|
#include <string.h>
|
||||||
|
#include <stdlib.h>
|
||||||
|
#include <libconfig.h>
|
||||||
|
#include <netdb.h>
|
||||||
|
#include <sys/types.h>
|
||||||
|
#include <sys/socket.h>
|
||||||
|
#include <errno.h>
|
||||||
|
#include <sys/sysinfo.h>
|
||||||
|
#include <unistd.h>
|
||||||
|
#include <stdbool.h>
|
||||||
|
|
||||||
|
#include "sinkSender.h"
|
||||||
|
#include "logging.h"
|
||||||
|
#include "led.h"
|
||||||
|
#include "sinkStruct.h"
|
||||||
|
#include "sha256.h"
|
||||||
|
|
||||||
|
|
||||||
|
const char SINKSERVER_KEY[] = "sinkServer";
|
||||||
|
const char DEFAULT_SINKSERVER[] = "sink.hottis.de";
|
||||||
|
const char *sinkServer;
|
||||||
|
const char SINKPORT_KEY[] = "sinkPort";
|
||||||
|
const int DEFAULT_SINKPORT = 20169;
|
||||||
|
int sinkPort;
|
||||||
|
const char DEVICE_ID_KEY[] = "deviceId";
|
||||||
|
const char DEFAULT_DEVICE_ID[] = "mainscnt00";
|
||||||
|
const char *deviceId;
|
||||||
|
const char SHARED_SECRET_KEY[] = "sharedSecret";
|
||||||
|
const char DEFAULT_SHARED_SECRET[] = "1234567890123456789012345678901";
|
||||||
|
const char *sharedSecret;
|
||||||
|
|
||||||
|
static t_minuteBuffer minuteBuffer;
|
||||||
|
static uint32_t secondOfMinute;
|
||||||
|
|
||||||
|
extern char VERSION[];
|
||||||
|
|
||||||
|
|
||||||
|
void sinkSenderInit(config_t *pCfg) {
|
||||||
|
if (! config_lookup_string(pCfg, SINKSERVER_KEY, &sinkServer)) {
|
||||||
|
sinkServer = DEFAULT_SINKSERVER;
|
||||||
|
}
|
||||||
|
logmsg(LOG_INFO, "CONFIG: sinkServer=%s\n", sinkServer);
|
||||||
|
if (! config_lookup_int(pCfg, SINKPORT_KEY, &sinkPort)) {
|
||||||
|
sinkPort = DEFAULT_SINKPORT;
|
||||||
|
}
|
||||||
|
logmsg(LOG_INFO, "CONFIG: sinkPort=%u\n", sinkPort);
|
||||||
|
if (! config_lookup_string(pCfg, DEVICE_ID_KEY, &deviceId)) {
|
||||||
|
deviceId = DEFAULT_DEVICE_ID;
|
||||||
|
}
|
||||||
|
logmsg(LOG_INFO, "CONFIG: deviceId=%s\n", deviceId);
|
||||||
|
if (! config_lookup_string(pCfg, SHARED_SECRET_KEY, &sharedSecret)) {
|
||||||
|
sharedSecret = DEFAULT_SHARED_SECRET;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
secondOfMinute = 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
static void sinkSenderSendMinute() {
|
||||||
|
led(E_BLUE, true);
|
||||||
|
led(E_RED, false);
|
||||||
|
|
||||||
|
struct sysinfo info;
|
||||||
|
sysinfo(&info);
|
||||||
|
|
||||||
|
minuteBuffer.s.totalRunningHours = info.uptime / 3600;
|
||||||
|
minuteBuffer.s.totalPowercycles = 0;
|
||||||
|
minuteBuffer.s.totalWatchdogResets = 0;
|
||||||
|
minuteBuffer.s.version = strtoll(VERSION, NULL, 16);
|
||||||
|
|
||||||
|
memset(minuteBuffer.s.deviceId, 0, sizeof(minuteBuffer.s.deviceId));
|
||||||
|
strcpy(minuteBuffer.s.deviceId, deviceId);
|
||||||
|
|
||||||
|
memcpy(minuteBuffer.s.hash, sharedSecret, SHA256_BLOCK_SIZE);
|
||||||
|
SHA256_CTX ctx;
|
||||||
|
sha256_init(&ctx);
|
||||||
|
sha256_update(&ctx, minuteBuffer.b, sizeof(minuteBuffer.b));
|
||||||
|
sha256_final(&ctx, minuteBuffer.s.hash);
|
||||||
|
|
||||||
|
struct hostent *hptr = gethostbyname(sinkServer);
|
||||||
|
if (hptr) {
|
||||||
|
if (hptr->h_addrtype == AF_INET) {
|
||||||
|
char *sinkAddr = hptr->h_addr_list[0];
|
||||||
|
logmsg(LOG_DEBUG, "sink addr: %d.%d.%d.%d",
|
||||||
|
sinkAddr[0], sinkAddr[1], sinkAddr[2], sinkAddr[3]);
|
||||||
|
|
||||||
|
int sockfd = socket(AF_INET, SOCK_DGRAM, 0);
|
||||||
|
if (sockfd != -1) {
|
||||||
|
struct sockaddr_in servaddr;
|
||||||
|
memset(&servaddr, 0, sizeof(servaddr));
|
||||||
|
servaddr.sin_family = AF_INET;
|
||||||
|
servaddr.sin_port = htons(sinkPort);
|
||||||
|
memcpy(&servaddr.sin_addr.s_addr, sinkAddr, 4);
|
||||||
|
|
||||||
|
ssize_t res = sendto(sockfd, minuteBuffer.b, sizeof(minuteBuffer.b),
|
||||||
|
0, (struct sockaddr*)&servaddr,
|
||||||
|
sizeof(servaddr));
|
||||||
|
logmsg(LOG_DEBUG, "%d octets sent", res);
|
||||||
|
|
||||||
|
int rc = close(sockfd);
|
||||||
|
if (rc == -1) {
|
||||||
|
logmsg(LOG_ERR, "close on socket returns %s", strerror(errno));
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
led(E_RED, true);
|
||||||
|
logmsg(LOG_ERR, "unable to get socket: %s", strerror(errno));
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
led(E_RED, true);
|
||||||
|
logmsg(LOG_ERR, "unknown address type: %d", hptr->h_addrtype);
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
led(E_RED, true);
|
||||||
|
logmsg(LOG_ERR, "sinkserver %s couldn't be resolved: %s", sinkServer, hstrerror(h_errno));
|
||||||
|
}
|
||||||
|
|
||||||
|
led(E_BLUE, false);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
void sinkSenderPut(uint32_t seconds, uint32_t frequency) {
|
||||||
|
static bool settled = false;
|
||||||
|
|
||||||
|
led(E_GREEN, false);
|
||||||
|
logmsg(LOG_DEBUG, "s: %lu, f: %lu", seconds, frequency);
|
||||||
|
|
||||||
|
if (secondOfMinute == 0) {
|
||||||
|
minuteBuffer.s.timestamp = seconds;
|
||||||
|
}
|
||||||
|
minuteBuffer.s.frequency[secondOfMinute] = frequency;
|
||||||
|
secondOfMinute += 1;
|
||||||
|
|
||||||
|
if (secondOfMinute == SECONDS_PER_MINUTE) {
|
||||||
|
logmsg(LOG_DEBUG, "minute is full");
|
||||||
|
secondOfMinute = 0;
|
||||||
|
|
||||||
|
if (settled) {
|
||||||
|
sinkSenderSendMinute();
|
||||||
|
} else {
|
||||||
|
logmsg(LOG_INFO, "now it is settled");
|
||||||
|
settled = true;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
led(E_GREEN, true);
|
||||||
|
}
|
10
src/sinkSender.h
Normal file
10
src/sinkSender.h
Normal file
@ -0,0 +1,10 @@
|
|||||||
|
#ifndef _SINKSENDER_H_
|
||||||
|
#define _SINKSENDER_H_
|
||||||
|
|
||||||
|
#include <libconfig.h>
|
||||||
|
|
||||||
|
|
||||||
|
void sinkSenderInit(config_t *pCfg);
|
||||||
|
void sinkSenderPut(uint32_t seconds, uint32_t frequency);
|
||||||
|
|
||||||
|
#endif // _SINKSENDER_H_
|
26
src/sinkStruct.h
Normal file
26
src/sinkStruct.h
Normal file
@ -0,0 +1,26 @@
|
|||||||
|
#ifndef _SINKSTRUCT_H_
|
||||||
|
#define _SINKSTRUCT_H_
|
||||||
|
|
||||||
|
#include <stdint.h>
|
||||||
|
#include "sha256.h"
|
||||||
|
|
||||||
|
#define DEVICE_ID_SIZE 16
|
||||||
|
#define SECONDS_PER_MINUTE 60
|
||||||
|
|
||||||
|
typedef struct __attribute__((__packed__)) {
|
||||||
|
char deviceId[DEVICE_ID_SIZE];
|
||||||
|
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;
|
||||||
|
|
||||||
|
typedef union {
|
||||||
|
t_minuteStruct s;
|
||||||
|
uint8_t b[sizeof(t_minuteStruct)];
|
||||||
|
} t_minuteBuffer;
|
||||||
|
|
||||||
|
#endif // _SINKSTRUCT_H_
|
6
src/version.c
Normal file
6
src/version.c
Normal file
@ -0,0 +1,6 @@
|
|||||||
|
#include <stdint.h>
|
||||||
|
|
||||||
|
const uint32_t REFCNT = D_REFCNT;
|
||||||
|
const char VERSION[] = D_VERSION;
|
||||||
|
|
||||||
|
|
Reference in New Issue
Block a user