63 Commits

Author SHA1 Message Date
9e3fa03053 red in any case 2021-03-06 10:01:50 +01:00
6010facb07 close socket 2021-03-06 09:59:43 +01:00
ea459e7dbd fix 2021-03-05 11:02:01 +01:00
b57eadb5e7 fix 2021-03-05 11:00:57 +01:00
62d847b9da fix 2021-03-05 10:59:42 +01:00
19ea1ec097 config 2021-03-05 10:58:45 +01:00
badc6aa1f5 led support configurable 2021-03-05 10:57:28 +01:00
c4736640cd no output of sharedSecret 2021-03-04 18:07:36 +01:00
72f2c72280 static settled 2021-03-04 18:06:23 +01:00
f52efb4f27 output 2021-03-04 18:05:28 +01:00
5254b318f8 output 2021-03-04 18:02:27 +01:00
a13feaac02 less output 2021-03-04 18:01:25 +01:00
d73b97ecb2 output 2021-03-04 18:00:21 +01:00
d97e941b48 settled 2021-03-04 17:57:28 +01:00
ce4562762a less debug 2021-03-04 17:46:25 +01:00
10b4916a4c long version 2021-03-04 17:44:32 +01:00
f542bfc71f led 2021-03-04 17:41:30 +01:00
7e80a6d160 some fixes 2021-03-04 17:36:18 +01:00
7d60c537f9 uptime again 2021-03-04 17:28:30 +01:00
47fb07d055 uptime 2021-03-04 17:27:46 +01:00
5d58a9e526 sysinfo 2021-03-04 17:27:07 +01:00
ba351b773a uptime 2021-03-04 17:26:14 +01:00
681e852cec fix 2021-03-04 17:14:50 +01:00
b984284a20 fix 2021-03-04 17:12:47 +01:00
6f425d0353 send 2021-03-04 17:10:45 +01:00
5e5b6e62ca dns 2021-03-04 16:57:11 +01:00
1e9b811592 fix 2021-03-04 16:37:24 +01:00
6fc7dd2e77 fix 2021-03-04 16:36:36 +01:00
c80cb92c53 prepare buffer 2021-03-04 16:35:01 +01:00
a97fc6f725 fix 2021-03-04 16:26:58 +01:00
d4a6b9ac53 sha 2021-03-04 16:25:43 +01:00
c84dd1acba config 2021-03-04 16:25:22 +01:00
e92bf0817e sinkSender 2021-03-04 15:54:30 +01:00
9eee986123 freq 2021-03-04 15:44:27 +01:00
446a279277 one seoncs 2021-03-04 15:34:20 +01:00
4be07210e0 millis 2021-03-04 15:32:26 +01:00
1796830059 millis 2021-03-04 15:30:09 +01:00
0ad3cba113 millis 2021-03-04 15:26:39 +01:00
49b0974022 time 2021-03-04 15:02:44 +01:00
bb3b626db1 fix 2021-03-04 14:49:41 +01:00
e357072888 start change to sinkServer support 2021-03-04 13:55:27 +01:00
bfbf686298 50 periods 2020-03-09 14:32:40 +01:00
b7df92d079 increase version 2020-03-09 12:42:04 +01:00
59e3350aaf adjust 1s store period 2020-03-09 12:40:30 +01:00
6c21983917 fix calculation 2020-03-09 12:37:45 +01:00
80103312ce some debug 2020-03-09 12:33:45 +01:00
9213cfac11 some debug 2020-03-09 12:31:47 +01:00
07c80eb21c forgotten inculde 2020-03-09 12:24:09 +01:00
eb83c754bf output 1s 2020-03-09 12:23:16 +01:00
4f81343a89 fix mem stuff 2020-03-09 12:22:13 +01:00
e8102e74a9 1s entries 2020-03-09 12:05:06 +01:00
784346b833 adjust service file 2019-11-14 15:05:17 +01:00
e3ee3a08e8 add restart target 2019-11-14 14:42:11 +01:00
5ef114b634 code beautifying, extended filtering discarded, better adjust epsilon 2019-11-14 14:41:20 +01:00
2dd586d7e4 fix service file 2019-11-13 20:22:05 +00:00
5d1f7dde89 version stuff 2019-11-13 21:14:31 +01:00
8d56c5aff4 enhance makefile 2019-11-12 16:12:33 +01:00
5230139544 store only when settled 2019-11-12 16:02:53 +01:00
08c76f13de save raw freq, add location 2019-11-12 14:43:38 +01:00
6a13da024d add gradient into data 2019-11-08 22:54:56 +01:00
8c0c864905 mark invalid and replaced values in database 2019-11-05 15:07:19 +01:00
61b1498d7b logging 2019-11-05 14:36:38 +01:00
e21e114313 output to stderr 2019-11-04 17:30:45 +01:00
17 changed files with 522 additions and 177 deletions

View File

@ -1,17 +1,51 @@
CC=gcc
CFLAGS=-Wall
LDFLAGS=-lwiringPi -lcurl -lconfig
LDFLAGS=-lwiringPi -lconfig
counter: counter.o LS7366R.o influx.o ringbuffer.o led.o
$(CC) -o $@ $(LDFLAGS) $^
INST_DIR=/opt/sbin
.c.o:
$(CC) $(CFLAGS) -c $<
REFCNT := $(shell git rev-list --all --count)
VERSION := $(shell git rev-parse --short=8 HEAD)
.PHONY: all
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
clean:
-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
View File

@ -0,0 +1 @@
0.98

View File

@ -5,24 +5,29 @@
#include <stdio.h>
#include <unistd.h>
#include <libconfig.h>
#include <math.h>
#include <time.h>
#include "LS7366R.h"
#include "influx.h"
#include "ringbuffer.h"
#include "led.h"
#include "logging.h"
#include "sinkSender.h"
extern char VERSION[];
extern uint32_t REFCNT;
const int CTRL_OUT = 16;
const int INTR_IN = 19;
const int SPI_CHAN = 0;
const int SPI_SPEED = 1000000;
const uint32_t PRECISION = 1000;
const uint32_t COUNTER_FREQUENCY = 1e6;
config_t cfg;
const char EPSILON_KEY[] = "epsilon";
const double DEFAULT_EPSILON = 0.01;
uint32_t skipped = 0;
void isr() {
@ -51,9 +56,9 @@ void init() {
void readConfig() {
config_init(&cfg);
if (! config_read_file(&cfg, "/opt/etc/counter.cfg")) {
fprintf(stderr, "failed to read config file: %s:%d - %s\n",
config_error_file(&cfg), config_error_line(&cfg),
config_error_text(&cfg));
logmsg(LOG_ERR, "failed to read config file: %s:%d - %s\n",
config_error_file(&cfg), config_error_line(&cfg),
config_error_text(&cfg));
config_destroy(&cfg);
exit(-1);
}
@ -64,47 +69,43 @@ void start() {
}
int main (void) {
logmsg(LOG_INFO, "VERSION: %s, REFCNT: %u\n", VERSION, REFCNT);
readConfig();
init();
ledInit();
ledInit(&cfg);
ls7366rInit(SPI_CHAN);
influxInit(&cfg);
sinkSenderInit(&cfg);
start();
double epsilon;
if (! config_lookup_float(&cfg, EPSILON_KEY, &epsilon)) {
epsilon = DEFAULT_EPSILON;
}
struct timespec timestamp;
uint32_t last_seconds = 0;
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) {
uint32_t diff = ringbufferGet();
uint32_t period = ringbufferGet();
double f = 1.0 / (((double) diff) / 1000000.0);
clock_gettime(CLOCK_REALTIME, &timestamp);
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)) {
printf("Current f=%f, last f=%f, gradient too large, skipped\n", f, lastF);
skipped++;
f = lastF;
mainsCntSum += period;
mainsCntCnt += 1;
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

View File

@ -1,3 +1,8 @@
influxUser = "secundus"
influxPass = "geheim"
influxUrl = "http://172.16.3.15:8086/write?db=smarthome2&precision=ms"
led = "off"
sinkServer = "sink.hottis.de"
sinkPort = 20169
deviceId = "mainscnt03"
// sharedSecret has to have exactly 31 octets
sharedSecret = "1234567890123456789012345678901"

View File

@ -6,9 +6,7 @@ After=network-online.target
[Service]
Type=simple
GuessMainPID=yes
ExecStart=/opt/bin/counter
ExecStop=kill -SIGINT $mainpid
ExecStart=/opt/sbin/counter
Restart=on-failure
WorkingDirectory=/tmp

View File

@ -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++;
}

View File

@ -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_

View File

@ -1,32 +1,46 @@
#include <stdbool.h>
#include <wiringPi.h>
#include <libconfig.h>
#include <stdbool.h>
#include <strings.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 RED_OUT = 21;
const int BLUE_OUT = 26;
void ledInit() {
pinMode(GREEN_OUT, OUTPUT);
digitalWrite(GREEN_OUT, 0);
void ledInit(config_t *pCfg) {
if (! config_lookup_string(pCfg, LED_KEY, &ledsActiveStr)) {
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);
digitalWrite(RED_OUT, 0);
pinMode(GREEN_OUT, OUTPUT);
digitalWrite(GREEN_OUT, 0);
pinMode(BLUE_OUT, OUTPUT);
digitalWrite(BLUE_OUT, 0);
pinMode(RED_OUT, OUTPUT);
digitalWrite(RED_OUT, 0);
pinMode(BLUE_OUT, OUTPUT);
digitalWrite(BLUE_OUT, 0);
}
void led (tColor color, bool state) {
if (color == E_RED) {
digitalWrite(RED_OUT, state ? 1 : 0);
} else if (color == E_BLUE) {
digitalWrite(BLUE_OUT, state ? 1 : 0);
} else if (color == E_GREEN) {
digitalWrite(GREEN_OUT, state ? 1 : 0);
}
if (color == E_RED) {
digitalWrite(RED_OUT, state ? 1 : 0);
} else if (color == E_BLUE && ledsActive) {
digitalWrite(BLUE_OUT, state ? 1 : 0);
} else if (color == E_GREEN && ledsActive) {
digitalWrite(GREEN_OUT, state ? 1 : 0);
}
}

View File

@ -2,10 +2,11 @@
#define _LED_H_
#include <stdbool.h>
#include <libconfig.h>
typedef enum { E_RED, E_BLUE, E_GREEN } tColor;
void ledInit();
void ledInit(config_t *pCfg);
void led(tColor color, bool state);
#endif // _LED_H_

13
src/logging.c Normal file
View 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
View 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
View 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
View 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
View 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
View 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
View 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
View File

@ -0,0 +1,6 @@
#include <stdint.h>
const uint32_t REFCNT = D_REFCNT;
const char VERSION[] = D_VERSION;