Files
templates/c/telemetry-logger/telemetry_gas.c
T
Andrey e93bd3b6aa style: унифицируй форматирование C и C++
Закрепи табуляцию и размещение скобок в настройках проекта. Примени единый стиль к собственным исходникам.
2026-12-02 11:33:41 +03:00

162 lines
4.5 KiB
C

#include "telemetry_gas.h"
bool slog_gas_init(slog_gas_t *gas, slog_t *log, uint16_t base, uint16_t channel_base,
uint32_t schema_id, uint64_t unsigned_channels)
{
if (!gas || !log || !log->capacity || !log->archive.rows ||
(unsigned_channels >> log->channels))
{
return false;
}
uint32_t width = GL_HEADER_WORDS + log->channels;
uint32_t data_base = (uint32_t)base + GL_DATA_OFFSET;
uint32_t channel_end = (uint32_t)channel_base + log->channels;
if (data_base > 65536UL || log->capacity > (65536UL - data_base) / width ||
channel_end > 65536UL || (channel_end > base && channel_base < data_base + (uint32_t)log->capacity * width))
{
return false;
}
*gas = (slog_gas_t){.log = log, .schema_id = schema_id, .base = base, .channel_base = channel_base, .unsigned_channels = unsigned_channels};
return true;
}
static uint16_t channel_word(const slog_gas_t *gas, const slog_sample_t *row, unsigned channel)
{
bool is_unsigned = ((gas->unsigned_channels >> channel) & 1U) != 0;
if (!((row->valid >> channel) & 1U))
{
return is_unsigned ? UINT16_MAX : UINT16_C(32768);
}
int32_t value = row->values[channel];
int32_t low = is_unsigned ? 0 : -32767;
int32_t high = is_unsigned ? 65534 : 32767;
if (value < low || value > high)
{
return is_unsigned ? UINT16_MAX : UINT16_C(32768);
}
return (uint16_t)value;
}
static uint16_t archive_word(const slog_gas_t *gas, const slog_sample_t *row, unsigned column)
{
uint32_t time_ms = (uint32_t)(row->time_us / 1000U);
switch (column)
{
case 0:
return (uint16_t)time_ms;
case 1:
return (uint16_t)(time_ms >> 16);
case 2:
return (uint16_t)row->sequence;
case 3:
return (uint16_t)(row->sequence >> 16);
case 4:
return row->event;
default:
return channel_word(gas, row, column - GL_HEADER_WORDS);
}
}
gl_result slog_gas_read(const slog_gas_t *gas, uint16_t address, uint16_t *out, uint16_t count)
{
if (!gas || !gas->log || !out || !count || count > 125U)
{
return GL_ARGUMENT;
}
const slog_t *log = gas->log;
uint32_t end = (uint32_t)address + count;
uint32_t data_base = (uint32_t)gas->base + GL_DATA_OFFSET;
uint16_t width = GL_HEADER_WORDS + log->channels;
if (address >= gas->base && end <= (uint32_t)gas->base + 18U)
{
uint16_t status[18] = {
GL_MAGIC, GL_VERSION, (gas->recording ? 1 : 0) | (log->archive.count ? 2 : 0),
log->channels, width, (uint16_t)log->capacity, (uint16_t)log->active.count,
(uint16_t)log->archive.count, (uint16_t)log->generation,
(uint16_t)(log->generation >> 16), (uint16_t)gas->schema_id,
(uint16_t)(gas->schema_id >> 16), (uint16_t)log->next_sequence,
(uint16_t)(log->next_sequence >> 16), (uint16_t)log->missed_triggers,
(uint16_t)(log->missed_triggers >> 16), 0, 0};
for (uint16_t i = 0; i < count; ++i)
{
out[i] = status[address - gas->base + i];
}
return GL_OK;
}
slog_sample_t row = {0};
if (address >= data_base && end <= data_base + (uint32_t)log->archive.count * width)
{
uint32_t offset = address - data_base;
size_t index = offset / width;
uint16_t column = (uint16_t)(offset % width);
slog_archive_read(log, log->generation, index, &row);
for (uint16_t i = 0; i < count; ++i)
{
if (column == width)
{
slog_archive_read(log, log->generation, ++index, &row);
column = 0;
}
out[i] = archive_word(gas, &row, column++);
}
return GL_OK;
}
if (address >= gas->channel_base && end <= (uint32_t)gas->channel_base + log->channels)
{
slog_latest(log, &row);
for (uint16_t i = 0; i < count; ++i)
{
out[i] = channel_word(gas, &row, address - gas->channel_base + i);
}
return GL_OK;
}
return GL_ADDRESS;
}
gl_result slog_gas_write(slog_gas_t *gas, uint16_t address, uint16_t value)
{
if (!gas || !gas->log)
{
return GL_ARGUMENT;
}
uint32_t base = gas->base;
if (address == base + GL_RELEASE_LO)
{
gas->release_generation = (gas->release_generation & UINT32_C(0xffff0000)) | value;
}
else if (address == base + GL_RELEASE_HI)
{
gas->release_generation = (gas->release_generation & UINT32_C(0xffff)) |
((uint32_t)value << 16);
}
else if (address != base + GL_COMMAND)
{
return GL_ADDRESS;
}
else if (value == GL_START)
{
gas->recording = true;
}
else if (value == GL_SNAPSHOT)
{
if (!slog_snapshot(gas->log))
{
return gas->log->archive.count || gas->log->remaining ? GL_BUSY : GL_EMPTY;
}
}
else if (value == GL_RELEASE)
{
if (!slog_archive_release(gas->log, gas->release_generation))
{
return GL_STALE;
}
gas->release_generation = 0;
}
else
{
return GL_ARGUMENT;
}
return GL_OK;
}