180 lines
3.6 KiB
C
180 lines
3.6 KiB
C
|
#include <stdint.h>
|
||
|
#include <stdio.h>
|
||
|
#include <string.h>
|
||
|
|
||
|
#include <glib.h>
|
||
|
|
||
|
#include "tdc_proto.h"
|
||
|
#include "tdc_variable.h"
|
||
|
|
||
|
void tdcvar_get_value(struct tdc_var *var, char *buf, int size, int viewmode)
|
||
|
{
|
||
|
switch ((var->flags & TDC_TYPEMASK) | ((viewmode & 0x01) << 8)) {
|
||
|
case TDC_UNSIGNED:
|
||
|
switch (var->flags & TDC_SIZEMASK) {
|
||
|
case 1: snprintf(buf, size, "%u", var->data_uint8);
|
||
|
break;
|
||
|
|
||
|
case 2: snprintf(buf, size, "%u", var->data_uint16);
|
||
|
break;
|
||
|
|
||
|
case 4: snprintf(buf, size, "%u", var->data_uint32);
|
||
|
break;
|
||
|
|
||
|
case 8: snprintf(buf, size, "%llu", var->data_uint64);
|
||
|
break;
|
||
|
|
||
|
default:
|
||
|
snprintf(buf, size, "???");
|
||
|
break;
|
||
|
}
|
||
|
break;
|
||
|
|
||
|
case TDC_UNSIGNED | 0x100:
|
||
|
switch (var->flags & TDC_SIZEMASK) {
|
||
|
case 1: snprintf(buf, size, "0x%02x", var->data_uint8);
|
||
|
break;
|
||
|
|
||
|
case 2: snprintf(buf, size, "0x%04x", var->data_uint16);
|
||
|
break;
|
||
|
|
||
|
case 4: snprintf(buf, size, "0x%08x", var->data_uint32);
|
||
|
break;
|
||
|
|
||
|
case 8: snprintf(buf, size, "0x%16llx", var->data_uint64);
|
||
|
break;
|
||
|
|
||
|
default:
|
||
|
snprintf(buf, size, "???");
|
||
|
break;
|
||
|
}
|
||
|
break;
|
||
|
|
||
|
case TDC_SIGNED:
|
||
|
switch (var->flags & TDC_SIZEMASK) {
|
||
|
case 1: snprintf(buf, size, "%d", (int8_t)var->data_uint8);
|
||
|
break;
|
||
|
|
||
|
case 2: snprintf(buf, size, "%d", (int16_t)var->data_uint16);
|
||
|
break;
|
||
|
|
||
|
case 4: snprintf(buf, size, "%d", (int32_t)var->data_uint32);
|
||
|
break;
|
||
|
|
||
|
case 8: snprintf(buf, size, "%lld", (int64_t)var->data_uint64);
|
||
|
break;
|
||
|
|
||
|
default:
|
||
|
snprintf(buf, size, "???");
|
||
|
break;
|
||
|
}
|
||
|
break;
|
||
|
|
||
|
case TDC_SIGNED | 0x100:
|
||
|
switch (var->flags & TDC_SIZEMASK) {
|
||
|
case 1: snprintf(buf, size, "0x%02x", (int8_t)var->data_uint8);
|
||
|
break;
|
||
|
|
||
|
case 2: snprintf(buf, size, "0x%04x", (int16_t)var->data_uint16);
|
||
|
break;
|
||
|
|
||
|
case 4: snprintf(buf, size, "0x%08x", (int32_t)var->data_uint32);
|
||
|
break;
|
||
|
|
||
|
case 8: snprintf(buf, size, "0x%16llx", (int64_t)var->data_uint64);
|
||
|
break;
|
||
|
|
||
|
default:
|
||
|
snprintf(buf, size, "???");
|
||
|
break;
|
||
|
}
|
||
|
break;
|
||
|
|
||
|
case TDC_FP:
|
||
|
case TDC_FP | 0x100:
|
||
|
switch (var->flags & TDC_SIZEMASK) {
|
||
|
case sizeof(float):
|
||
|
snprintf(buf, size, "%.8f", var->data_float);
|
||
|
break;
|
||
|
|
||
|
case sizeof(double):
|
||
|
snprintf(buf, size, "%.12f", var->data_double);
|
||
|
break;
|
||
|
|
||
|
default:
|
||
|
snprintf(buf, size, "???");
|
||
|
break;
|
||
|
}
|
||
|
break;
|
||
|
|
||
|
case TDC_FIXED:
|
||
|
case TDC_FIXED | 0x100:
|
||
|
snprintf(buf, size, "???");
|
||
|
break;
|
||
|
}
|
||
|
}
|
||
|
|
||
|
void tdcvar_get_type(struct tdc_var *var, char *buf, int size)
|
||
|
{
|
||
|
int pos;
|
||
|
int width = (var->flags & TDC_SIZEMASK);
|
||
|
|
||
|
switch (var->flags & TDC_TYPEMASK) {
|
||
|
case TDC_UNSIGNED:
|
||
|
pos = snprintf(buf, size, "uint%d_t", width * 8);
|
||
|
break;
|
||
|
|
||
|
case TDC_SIGNED:
|
||
|
pos = snprintf(buf, size, "int%d_t", width * 8);
|
||
|
break;
|
||
|
|
||
|
case TDC_FP:
|
||
|
if (width == sizeof(float))
|
||
|
pos = snprintf(buf, size, "float");
|
||
|
|
||
|
else if (size == sizeof(double))
|
||
|
pos = snprintf(buf, size, "double");
|
||
|
|
||
|
else
|
||
|
pos = snprintf(buf, size, "fp%d_t", width * 8);
|
||
|
break;
|
||
|
|
||
|
case TDC_FIXED:
|
||
|
pos = snprintf(buf, size, "fix%d_t", width * 8);
|
||
|
break;
|
||
|
|
||
|
default:
|
||
|
pos = snprintf(buf, size, "???");
|
||
|
break;
|
||
|
}
|
||
|
|
||
|
if (var->flags & TDC_READONLY)
|
||
|
pos = snprintf(buf + pos, size - pos, " (ro)");
|
||
|
}
|
||
|
|
||
|
struct tdc_var * tdcvar_create(int id, uint32_t flags, char *name, int len)
|
||
|
{
|
||
|
struct tdc_var *var = g_malloc0(sizeof(struct tdc_var) + len + 1);
|
||
|
var->id = id & 0xFF;
|
||
|
var->flags = flags;
|
||
|
|
||
|
strncpy(var->name, name, len);
|
||
|
var->name[len] = '\0';
|
||
|
|
||
|
printf("create_var(%d, 0x%x, '%s', %d)\n", var->id, var->flags, var->name, len);
|
||
|
return var;
|
||
|
}
|
||
|
|
||
|
void tdcvar_update(struct tdc_var *var, uint8_t *data, int len)
|
||
|
{
|
||
|
if ((var->flags & TDC_SIZEMASK) != len)
|
||
|
return;
|
||
|
|
||
|
memcpy(&var->data_uint8, data, len);
|
||
|
}
|
||
|
|
||
|
void tdcvar_destroy(struct tdc_var *var)
|
||
|
{
|
||
|
g_free(var);
|
||
|
}
|