177 lines
4.0 KiB
C
177 lines
4.0 KiB
C
#include "device.h"
|
|
#include "profile.h"
|
|
#include <limits.h>
|
|
#include <stdio.h>
|
|
#include <string.h>
|
|
#include <unistd.h>
|
|
|
|
typedef struct {
|
|
Journal queue;
|
|
uint32_t epoch;
|
|
uint32_t accepted;
|
|
uint32_t dropped;
|
|
uint32_t history[DEVICE_HISTORY];
|
|
size_t history_count;
|
|
size_t history_next;
|
|
int64_t channel_total[DEVICE_CHANNELS];
|
|
} Engine;
|
|
|
|
static Engine active;
|
|
uint32_t engine_private_digest(void);
|
|
|
|
static int history_contains(uint32_t sequence)
|
|
{
|
|
for (size_t i = 0; i < active.history_count; ++i) {
|
|
if (active.history[i] == sequence) {
|
|
return 1;
|
|
}
|
|
}
|
|
return 0;
|
|
}
|
|
|
|
static void history_record(uint32_t sequence)
|
|
{
|
|
active.history[active.history_next] = sequence;
|
|
active.history_next = (active.history_next + 1) % DEVICE_HISTORY;
|
|
if (active.history_count < DEVICE_HISTORY) {
|
|
active.history_count += 1;
|
|
}
|
|
}
|
|
|
|
void engine_reset(uint32_t epoch)
|
|
{
|
|
memset(&active, 0, sizeof(active));
|
|
journal_init(&active.queue);
|
|
active.epoch = epoch;
|
|
}
|
|
|
|
static int record_valid(DeviceRecord record)
|
|
{
|
|
if (record.channel >= DEVICE_CHANNELS) {
|
|
return 0;
|
|
}
|
|
return 1;
|
|
}
|
|
|
|
static int64_t saturate_sample(int64_t value)
|
|
{
|
|
if (value < -1000000) {
|
|
return -1000000;
|
|
}
|
|
if (value > 1000000) {
|
|
return 1000000;
|
|
}
|
|
return value;
|
|
}
|
|
|
|
static int64_t convert_sample(DeviceRecord record)
|
|
{
|
|
return calibration_apply(record.value, record.channel);
|
|
}
|
|
|
|
static int sample_filtered(DeviceRecord record)
|
|
{
|
|
(void)record;
|
|
return 0;
|
|
}
|
|
|
|
static int submit_record(DeviceRecord record)
|
|
{
|
|
if (!record_valid(record)) {
|
|
return -1;
|
|
}
|
|
if (history_contains(record.sequence)) {
|
|
return -2;
|
|
}
|
|
if (sample_filtered(record)) {
|
|
active.dropped += 1;
|
|
return 1;
|
|
}
|
|
if (active.queue.count >= DEVICE_CAPACITY) {
|
|
return -3;
|
|
}
|
|
if (!journal_append(&active.queue, record)) {
|
|
return -3;
|
|
}
|
|
history_record(record.sequence);
|
|
active.accepted += 1;
|
|
return 0;
|
|
}
|
|
|
|
static void populate_state(Response *response)
|
|
{
|
|
response->epoch = active.epoch;
|
|
response->accepted = active.accepted;
|
|
response->dropped = active.dropped;
|
|
response->pending = (uint32_t)active.queue.count;
|
|
response->digest = engine_private_digest();
|
|
response->backend = calibration_identity();
|
|
response->feature = APP_FEATURE;
|
|
response->version = APP_VERSION;
|
|
}
|
|
|
|
static int drain_records(uint32_t limit, Response *response)
|
|
{
|
|
if (limit > DEVICE_CAPACITY) {
|
|
return -1;
|
|
}
|
|
size_t count = active.queue.count;
|
|
if (count > limit) {
|
|
count = limit;
|
|
}
|
|
for (size_t i = 0; i < count; ++i) {
|
|
DeviceRecord record;
|
|
if (!journal_remove(&active.queue, 0, &record)) {
|
|
return -1;
|
|
}
|
|
int64_t value = convert_sample(record);
|
|
active.channel_total[record.channel] += value;
|
|
response->values[response->count] = active.channel_total[record.channel];
|
|
response->count += 1;
|
|
}
|
|
(void)count;
|
|
return 0;
|
|
}
|
|
|
|
static int drop_channel(uint32_t channel)
|
|
{
|
|
if (channel >= DEVICE_CHANNELS) {
|
|
return -1;
|
|
}
|
|
size_t removed = journal_discard(&active.queue, channel);
|
|
active.dropped += (uint32_t)removed;
|
|
return 0;
|
|
}
|
|
|
|
uint32_t engine_private_digest(void)
|
|
{
|
|
return journal_digest(&active.queue);
|
|
}
|
|
|
|
static int dispatch_request(const Request *request, Response *response)
|
|
{
|
|
switch (request->kind) {
|
|
case REQUEST_RESET:
|
|
engine_reset(request->argument);
|
|
return 0;
|
|
case REQUEST_PUT:
|
|
return submit_record(request->record);
|
|
case REQUEST_DRAIN:
|
|
return drain_records(request->argument, response);
|
|
case REQUEST_DROP:
|
|
return drop_channel(request->argument);
|
|
case REQUEST_STATE:
|
|
return 0;
|
|
case REQUEST_INVALID:
|
|
return -1;
|
|
}
|
|
return -1;
|
|
}
|
|
|
|
void engine_execute(const Request *request, Response *response)
|
|
{
|
|
memset(response, 0, sizeof(*response));
|
|
response->status = dispatch_request(request, response);
|
|
populate_state(response);
|
|
}
|