Compare commits
43 Commits
SecondValu
...
master
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 |
@ -1,17 +1,17 @@
|
||||
CC=gcc
|
||||
|
||||
CFLAGS=-Wall
|
||||
LDFLAGS=-lwiringPi -lcurl -lconfig
|
||||
LDFLAGS=-lwiringPi -lconfig
|
||||
|
||||
INST_DIR=/opt/sbin
|
||||
|
||||
REFCNT:=$(shell git rev-list --all --count)
|
||||
VERSION:=$(shell cat VERSION)
|
||||
REFCNT := $(shell git rev-list --all --count)
|
||||
VERSION := $(shell git rev-parse --short=8 HEAD)
|
||||
|
||||
.PHONY: all
|
||||
all: counter
|
||||
|
||||
counter: counter.o LS7366R.o influx.o ringbuffer.o led.o logging.o version.o
|
||||
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
|
||||
|
@ -1 +1 @@
|
||||
0.97
|
||||
0.98
|
||||
|
@ -6,12 +6,13 @@
|
||||
#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[];
|
||||
@ -22,12 +23,11 @@ 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() {
|
||||
@ -69,57 +69,43 @@ void start() {
|
||||
}
|
||||
|
||||
int main (void) {
|
||||
fprintf(stderr, "VERSION: %s, REFCNT: %u\n", VERSION, REFCNT);
|
||||
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;
|
||||
}
|
||||
fprintf(stderr, "CONFIG: epsilon=%f\n", epsilon);
|
||||
|
||||
|
||||
double lastF = 0;
|
||||
bool settled = false;
|
||||
uint8_t ledTick = 0;
|
||||
struct timespec timestamp;
|
||||
uint32_t last_seconds = 0;
|
||||
uint32_t last_milliseconds = 0;
|
||||
uint32_t mainsCntSum = 0;
|
||||
uint32_t mainsCntCnt = 0;
|
||||
|
||||
while (1) {
|
||||
uint32_t period = ringbufferGet();
|
||||
|
||||
double fRaw = 1.0 / (((double) period) / 1000000.0);
|
||||
int valid = settled ? 1 : 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);
|
||||
|
||||
double gradient = fRaw - lastF;
|
||||
double fSmoothed = fRaw;
|
||||
if (settled && (fabs(gradient) > epsilon)) {
|
||||
logmsg(LOG_INFO, "Current f=%f, last f=%f, gradient %f too large, invalid\n", fRaw, lastF, gradient);
|
||||
skipped++;
|
||||
fSmoothed = lastF;
|
||||
valid = 0;
|
||||
} else {
|
||||
lastF = fRaw;
|
||||
}
|
||||
mainsCntSum += period;
|
||||
mainsCntCnt += 1;
|
||||
|
||||
if (settled) {
|
||||
influxAddFrequency(period, fRaw, fSmoothed, gradient, valid);
|
||||
}
|
||||
if (duration >= 1000) {
|
||||
last_seconds = current_seconds;
|
||||
last_milliseconds = current_milliseconds;
|
||||
|
||||
ledTick++;
|
||||
if (ledTick == 50) {
|
||||
ledTick = 0;
|
||||
led(E_GREEN, false);
|
||||
if (! settled) {
|
||||
logmsg(LOG_INFO, "Now it is settled\n");
|
||||
settled = true;
|
||||
}
|
||||
}
|
||||
uint32_t cnt = mainsCntSum / mainsCntCnt;
|
||||
mainsCntSum = 0;
|
||||
mainsCntCnt = 0;
|
||||
uint32_t freq = PRECISION * COUNTER_FREQUENCY / cnt;
|
||||
|
||||
sinkSenderPut(current_seconds, freq);
|
||||
}
|
||||
}
|
||||
|
||||
// will never be reached
|
||||
|
@ -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"
|
179
src/influx.c
179
src/influx.c
@ -1,179 +0,0 @@
|
||||
#include <stdint.h>
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <curl/curl.h>
|
||||
#include <time.h>
|
||||
#include <string.h>
|
||||
#include <unistd.h>
|
||||
#include <libconfig.h>
|
||||
|
||||
#include "led.h"
|
||||
#include "logging.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;
|
||||
const char DEFAULT_LOCATION[] = "Essen_DE";
|
||||
const char LOCATION_KEY[] = "location";
|
||||
const char *location;
|
||||
|
||||
const uint8_t ONE_SECOND_DIVIDER = 50;
|
||||
|
||||
|
||||
extern uint32_t skipped;
|
||||
|
||||
|
||||
// #define BUFSIZE 131070
|
||||
#define BUFSIZE 65535
|
||||
// #define BUFSIZE 1024
|
||||
// char influxBuffer[BUFSIZE];
|
||||
// char *bufferNextEntry;
|
||||
|
||||
typedef struct influxBuffer {
|
||||
uint32_t entries;
|
||||
uint32_t totalEntries;
|
||||
char *nextEntry;
|
||||
char buffer[BUFSIZE];
|
||||
} tInfluxBuffer;
|
||||
|
||||
typedef enum {
|
||||
PERIOD_20MS = 0,
|
||||
PERIOD_1S,
|
||||
PERIOD_END
|
||||
} ePeriod;
|
||||
|
||||
tInfluxBuffer *influxBuffers[PERIOD_END];
|
||||
|
||||
#define HOSTNAMESIZE 128
|
||||
char hostname[HOSTNAMESIZE];
|
||||
|
||||
static void influxClearBuffer(tInfluxBuffer *influxBuffer, bool initial) {
|
||||
memset(influxBuffer->buffer, 0, BUFSIZE);
|
||||
influxBuffer->nextEntry = influxBuffer->buffer;
|
||||
influxBuffer->entries = 0;
|
||||
if (initial) {
|
||||
influxBuffer->totalEntries = 0;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
void influxInit(config_t *pCfg) {
|
||||
if (! config_lookup_string(pCfg, INFLUXURL_KEY, &influxUrl)) {
|
||||
influxUrl = DEFAULT_INFLUXURL;
|
||||
}
|
||||
fprintf(stderr, "CONFIG: influxUrl=%s\n", influxUrl);
|
||||
if (! config_lookup_string(pCfg, INFLUXUSER_KEY, &influxUser)) {
|
||||
influxUser = NULL;
|
||||
}
|
||||
fprintf(stderr, "CONFIG: influxUser=%s\n", (influxUser == NULL ? "<null>" : influxUser));
|
||||
if (! config_lookup_string(pCfg, INFLUXPASS_KEY, &influxPass)) {
|
||||
influxPass = NULL;
|
||||
}
|
||||
fprintf(stderr, "CONFIG: influxPass=%s\n", (influxPass == NULL ? "<null>" : influxPass));
|
||||
if (! config_lookup_string(pCfg, LOCATION_KEY, &location)) {
|
||||
location = DEFAULT_LOCATION;
|
||||
}
|
||||
fprintf(stderr, "CONFIG: location=%s\n", location);
|
||||
if (! config_lookup_string(pCfg, INFLUXTAG_KEY, &influxTag)) {
|
||||
gethostname(hostname, HOSTNAMESIZE);
|
||||
influxTag = hostname;
|
||||
}
|
||||
fprintf(stderr, "CONFIG: influxTag=%s\n", influxTag);
|
||||
influxBuffers[PERIOD_20MS] = (tInfluxBuffer*) malloc(sizeof(tInfluxBuffer));
|
||||
influxClearBuffer(influxBuffers[PERIOD_20MS], true);
|
||||
influxBuffers[PERIOD_1S] = (tInfluxBuffer*) malloc(sizeof(tInfluxBuffer));
|
||||
influxClearBuffer(influxBuffers[PERIOD_1S], true);
|
||||
}
|
||||
|
||||
|
||||
|
||||
static void influxSendRequest(tInfluxBuffer *influxBuffer) {
|
||||
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->buffer);
|
||||
CURLcode res = curl_easy_perform(curl);
|
||||
if(res != CURLE_OK) {
|
||||
logmsg(LOG_ERR, "curl_easy_perform() failed: %s\n", curl_easy_strerror(res));
|
||||
led(E_RED, true);
|
||||
}
|
||||
curl_easy_cleanup(curl);
|
||||
}
|
||||
led(E_BLUE, false);
|
||||
}
|
||||
|
||||
void influxAddFrequency(uint32_t period, double fRaw, double fSmoothed, double gradient,
|
||||
int valid) {
|
||||
static uint8_t divider = 0;
|
||||
static uint32_t summedUpTime = 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,valid=%d,location=%s "
|
||||
"period=%u,freq=%f,freqSmoothed=%f,gradient=%f,freqRaw=%f "
|
||||
"%llu\n",
|
||||
influxTag, valid, location,
|
||||
period, fSmoothed, fSmoothed, gradient, fRaw,
|
||||
tt);
|
||||
|
||||
if ((influxBuffers[PERIOD_20MS]->nextEntry + c + 10) > (influxBuffers[PERIOD_20MS]->buffer + BUFSIZE)) {
|
||||
influxSendRequest(influxBuffers[PERIOD_20MS]);
|
||||
influxBuffers[PERIOD_20MS]->totalEntries += influxBuffers[PERIOD_20MS]->entries;
|
||||
logmsg(LOG_INFO, "%u 20ms-entries sent to database, in total %u, invalid: %u\n",
|
||||
influxBuffers[PERIOD_20MS]->entries, influxBuffers[PERIOD_20MS]->totalEntries, skipped);
|
||||
influxClearBuffer(influxBuffers[PERIOD_20MS], false);
|
||||
}
|
||||
|
||||
memcpy(influxBuffers[PERIOD_20MS]->nextEntry, tmpBuf, c);
|
||||
influxBuffers[PERIOD_20MS]->nextEntry += c;
|
||||
influxBuffers[PERIOD_20MS]->entries += 1;
|
||||
|
||||
summedUpTime += period;
|
||||
divider += 1;
|
||||
|
||||
if (divider == ONE_SECOND_DIVIDER) {
|
||||
double freq1S = ((double)ONE_SECOND_DIVIDER) / (((double)summedUpTime) / 1000000.0);
|
||||
logmsg(LOG_DEBUG, "%llu: %u %f\n", tt, summedUpTime, freq1S);
|
||||
|
||||
int c = sprintf(tmpBuf, "mainsfrequency1S,host=%s,location=%s "
|
||||
"freq=%f "
|
||||
"%llu\n",
|
||||
influxTag, location,
|
||||
freq1S,
|
||||
tt);
|
||||
|
||||
//if ((influxBuffers[PERIOD_1S]->nextEntry + c + 10) > (influxBuffers[PERIOD_1S]->buffer + BUFSIZE)) {
|
||||
if (influxBuffers[PERIOD_1S]->entries == 30) {
|
||||
influxSendRequest(influxBuffers[PERIOD_1S]);
|
||||
influxBuffers[PERIOD_1S]->totalEntries += influxBuffers[PERIOD_1S]->entries;
|
||||
logmsg(LOG_INFO, "%u 1s-entries sent to database, in total %u\n",
|
||||
influxBuffers[PERIOD_1S]->entries, influxBuffers[PERIOD_1S]->totalEntries);
|
||||
influxClearBuffer(influxBuffers[PERIOD_1S], false);
|
||||
}
|
||||
|
||||
memcpy(influxBuffers[PERIOD_1S]->nextEntry, tmpBuf, c);
|
||||
influxBuffers[PERIOD_1S]->nextEntry += c;
|
||||
influxBuffers[PERIOD_1S]->entries += 1;
|
||||
|
||||
divider = 0;
|
||||
summedUpTime = 0.0;
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -1,9 +0,0 @@
|
||||
#ifndef _INFLUX_H_
|
||||
#define _INFLUX_H_
|
||||
|
||||
#include <libconfig.h>
|
||||
|
||||
void influxAddFrequency(uint32_t period, double fRaw, double fSmoothed, double gradient, int valid);
|
||||
void influxInit(config_t *pCfg);
|
||||
|
||||
#endif // _INFLUX_H_
|
20
src/led.c
20
src/led.c
@ -1,15 +1,29 @@
|
||||
#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() {
|
||||
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(GREEN_OUT, OUTPUT);
|
||||
digitalWrite(GREEN_OUT, 0);
|
||||
|
||||
@ -24,9 +38,9 @@ void ledInit() {
|
||||
void led (tColor color, bool state) {
|
||||
if (color == E_RED) {
|
||||
digitalWrite(RED_OUT, state ? 1 : 0);
|
||||
} else if (color == E_BLUE) {
|
||||
} else if (color == E_BLUE && ledsActive) {
|
||||
digitalWrite(BLUE_OUT, state ? 1 : 0);
|
||||
} else if (color == E_GREEN) {
|
||||
} else if (color == E_GREEN && ledsActive) {
|
||||
digitalWrite(GREEN_OUT, state ? 1 : 0);
|
||||
}
|
||||
}
|
||||
|
@ -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_
|
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_
|
Reference in New Issue
Block a user