Add option for multiple data tags
This commit is contained in:
parent
d4915014d9
commit
c359f71e05
10 changed files with 135 additions and 22 deletions
32
include/data_tag.h
Normal file
32
include/data_tag.h
Normal file
|
@ -0,0 +1,32 @@
|
|||
/** @file
|
||||
Custom data tags for data struct.
|
||||
|
||||
Copyright (C) 2021 Christian Zuckschwerdt
|
||||
|
||||
This program is free software; you can redistribute it and/or modify
|
||||
it under the terms of the GNU General Public License as published by
|
||||
the Free Software Foundation; either version 2 of the License, or
|
||||
(at your option) any later version.
|
||||
*/
|
||||
|
||||
#ifndef INCLUDE_TAGS_H_
|
||||
#define INCLUDE_TAGS_H_
|
||||
|
||||
struct data;
|
||||
|
||||
typedef struct data_tag {
|
||||
char *key;
|
||||
char *val;
|
||||
int prepend;
|
||||
} data_tag_t;
|
||||
|
||||
/// Create a data tag. Might fail and return NULL.
|
||||
data_tag_t *data_tag_create(char *params);
|
||||
|
||||
/// Free a data tag.
|
||||
void data_tag_free(data_tag_t *tag);
|
||||
|
||||
/// Apply a data tag.
|
||||
struct data *data_tag_apply(data_tag_t *tag, struct data *data, char const *filename);
|
||||
|
||||
#endif /* INCLUDE_TAGS_H_ */
|
|
@ -93,6 +93,8 @@ void add_dumper(struct r_cfg *cfg, char const *spec, int overwrite);
|
|||
|
||||
void add_infile(struct r_cfg *cfg, char *in_file);
|
||||
|
||||
void add_data_tag(struct r_cfg *cfg, char *param);
|
||||
|
||||
/* runtime */
|
||||
|
||||
struct mg_mgr *get_mgr(struct r_cfg *cfg);
|
||||
|
|
|
@ -90,8 +90,7 @@ typedef struct r_cfg {
|
|||
int no_default_devices;
|
||||
struct r_device *devices;
|
||||
uint16_t num_r_devices;
|
||||
char *output_key;
|
||||
char *output_tag;
|
||||
list_t data_tags;
|
||||
list_t output_handler;
|
||||
struct dm_state *demod;
|
||||
char const *sr_filename;
|
||||
|
|
|
@ -13,6 +13,7 @@ add_library(r_433 STATIC
|
|||
compat_time.c
|
||||
confparse.c
|
||||
data.c
|
||||
data_tag.c
|
||||
decoder_util.c
|
||||
fileformat.c
|
||||
http_server.c
|
||||
|
|
|
@ -11,6 +11,7 @@ rtl_433_SOURCES = abuf.c \
|
|||
compat_time.c \
|
||||
confparse.c \
|
||||
data.c \
|
||||
data_tag.c \
|
||||
decoder_util.c \
|
||||
fileformat.c \
|
||||
http_server.c \
|
||||
|
|
61
src/data_tag.c
Normal file
61
src/data_tag.c
Normal file
|
@ -0,0 +1,61 @@
|
|||
/** @file
|
||||
Custom data tags for data struct.
|
||||
|
||||
Copyright (C) 2021 Christian Zuckschwerdt
|
||||
|
||||
This program is free software; you can redistribute it and/or modify
|
||||
it under the terms of the GNU General Public License as published by
|
||||
the Free Software Foundation; either version 2 of the License, or
|
||||
(at your option) any later version.
|
||||
*/
|
||||
|
||||
#include <stdlib.h>
|
||||
#include <stdio.h>
|
||||
#include <string.h>
|
||||
|
||||
#include "data_tag.h"
|
||||
#include "data.h"
|
||||
#include "optparse.h"
|
||||
#include "fileformat.h"
|
||||
#include "fatal.h"
|
||||
|
||||
data_tag_t *data_tag_create(char *param)
|
||||
{
|
||||
data_tag_t *tag;
|
||||
tag = calloc(1, sizeof(data_tag_t));
|
||||
if (!tag) {
|
||||
WARN_CALLOC("data_tag_create()");
|
||||
return NULL;
|
||||
}
|
||||
|
||||
tag->prepend = 1; // always prepend tag
|
||||
tag->val = param;
|
||||
tag->key = asepc(&tag->val, '=');
|
||||
if (!tag->val) {
|
||||
tag->val = tag->key;
|
||||
tag->key = "tag";
|
||||
}
|
||||
|
||||
return tag; // NOTE: returns NULL on alloc failure.
|
||||
}
|
||||
|
||||
void data_tag_free(data_tag_t *tag)
|
||||
{
|
||||
free(tag);
|
||||
}
|
||||
|
||||
data_t *data_tag_apply(data_tag_t *tag, data_t *data, char const *filename)
|
||||
{
|
||||
char const *val = tag->val;
|
||||
if (filename && !strcmp("PATH", tag->val)) {
|
||||
val = filename;
|
||||
}
|
||||
else if (filename && !strcmp("FILE", tag->val)) {
|
||||
val = file_basename(filename);
|
||||
}
|
||||
data = data_prepend(data,
|
||||
tag->key, "", DATA_STRING, val,
|
||||
NULL);
|
||||
|
||||
return data;
|
||||
}
|
30
src/r_api.c
30
src/r_api.c
|
@ -25,6 +25,7 @@
|
|||
#include "pulse_detect_fsk.h"
|
||||
#include "sdr.h"
|
||||
#include "data.h"
|
||||
#include "data_tag.h"
|
||||
#include "list.h"
|
||||
#include "optparse.h"
|
||||
#include "output_mqtt.h"
|
||||
|
@ -191,6 +192,8 @@ void r_free_cfg(r_cfg_t *cfg)
|
|||
|
||||
list_free_elems(&cfg->output_handler, (list_elem_free_fn)data_output_free);
|
||||
|
||||
list_free_elems(&cfg->data_tags, (list_elem_free_fn)data_tag_free);
|
||||
|
||||
list_free_elems(&cfg->in_files, NULL);
|
||||
|
||||
mg_mgr_free(cfg->mgr);
|
||||
|
@ -335,8 +338,10 @@ char const **well_known_output_fields(r_cfg_t *cfg)
|
|||
|
||||
if (cfg->verbose_bits)
|
||||
*p++ = "bits";
|
||||
if (cfg->output_key)
|
||||
*p++ = cfg->output_key;
|
||||
for (void **iter = cfg->data_tags.elems; iter && *iter; ++iter) {
|
||||
data_tag_t *tag = *iter;
|
||||
*p++ = tag->key;
|
||||
}
|
||||
if (cfg->report_protocol)
|
||||
*p++ = "protocol";
|
||||
if (cfg->report_description)
|
||||
|
@ -751,18 +756,10 @@ void data_acquired_handler(r_device *r_dev, data_t *data)
|
|||
NULL);
|
||||
}
|
||||
|
||||
// prepend "tag" if available
|
||||
if (cfg->output_tag) {
|
||||
char const *output_tag = cfg->output_tag;
|
||||
if (cfg->in_filename && !strcmp("PATH", cfg->output_tag)) {
|
||||
output_tag = cfg->in_filename;
|
||||
}
|
||||
else if (cfg->in_filename && !strcmp("FILE", cfg->output_tag)) {
|
||||
output_tag = file_basename(cfg->in_filename);
|
||||
}
|
||||
data = data_prepend(data,
|
||||
cfg->output_key, "", DATA_STRING, output_tag,
|
||||
NULL);
|
||||
// apply all tags
|
||||
for (void **iter = cfg->data_tags.elems; iter && *iter; ++iter) {
|
||||
data_tag_t *tag = *iter;
|
||||
data = data_tag_apply(tag, data, cfg->in_filename);
|
||||
}
|
||||
|
||||
for (size_t i = 0; i < cfg->output_handler.len; ++i) { // list might contain NULLs
|
||||
|
@ -1024,3 +1021,8 @@ void add_infile(r_cfg_t *cfg, char *in_file)
|
|||
{
|
||||
list_push(&cfg->in_files, in_file);
|
||||
}
|
||||
|
||||
void add_data_tag(struct r_cfg *cfg, char *param)
|
||||
{
|
||||
list_push(&cfg->data_tags, data_tag_create(param));
|
||||
}
|
||||
|
|
|
@ -226,6 +226,16 @@ static void help_output(void)
|
|||
exit(0);
|
||||
}
|
||||
|
||||
_Noreturn
|
||||
static void help_tags(void)
|
||||
{
|
||||
term_help_printf(
|
||||
"\t\t= Data tags option =\n"
|
||||
" [-K FILE | PATH | <tag> | <key>=<value>] Add an expanded token or fixed tag to every output line.\n"
|
||||
"\tIf <tag> or <value> is \"FILE\" or \"PATH\" an expanded token will be added.\n");
|
||||
exit(0);
|
||||
}
|
||||
|
||||
_Noreturn
|
||||
static void help_meta(void)
|
||||
{
|
||||
|
@ -1048,12 +1058,9 @@ static void parse_conf_option(r_cfg_t *cfg, int opt, char *arg)
|
|||
}
|
||||
break;
|
||||
case 'K':
|
||||
cfg->output_tag = arg;
|
||||
cfg->output_key = asepc(&cfg->output_tag, '=');
|
||||
if (!cfg->output_tag) {
|
||||
cfg->output_tag = cfg->output_key;
|
||||
cfg->output_key = "tag";
|
||||
}
|
||||
if (!arg)
|
||||
help_tags();
|
||||
add_data_tag(cfg, arg);
|
||||
break;
|
||||
case 'C':
|
||||
if (!arg)
|
||||
|
|
|
@ -101,6 +101,7 @@ COPY ..\..\libusb\MS64\dll\libusb*.dll $(TargetDir)</Command>
|
|||
<ClInclude Include="..\include\compat_time.h" />
|
||||
<ClInclude Include="..\include\confparse.h" />
|
||||
<ClInclude Include="..\include\data.h" />
|
||||
<ClInclude Include="..\include\data_tag.h" />
|
||||
<ClInclude Include="..\include\decoder.h" />
|
||||
<ClInclude Include="..\include\decoder_util.h" />
|
||||
<ClInclude Include="..\include\fatal.h" />
|
||||
|
@ -139,6 +140,7 @@ COPY ..\..\libusb\MS64\dll\libusb*.dll $(TargetDir)</Command>
|
|||
<ClCompile Include="..\src\compat_time.c" />
|
||||
<ClCompile Include="..\src\confparse.c" />
|
||||
<ClCompile Include="..\src\data.c" />
|
||||
<ClCompile Include="..\src\data_tag.c" />
|
||||
<ClCompile Include="..\src\decoder_util.c" />
|
||||
<ClCompile Include="..\src\fileformat.c" />
|
||||
<ClCompile Include="..\src\http_server.c" />
|
||||
|
|
|
@ -41,6 +41,9 @@
|
|||
<ClInclude Include="..\include\data.h">
|
||||
<Filter>Header Files</Filter>
|
||||
</ClInclude>
|
||||
<ClInclude Include="..\include\data_tag.h">
|
||||
<Filter>Header Files</Filter>
|
||||
</ClInclude>
|
||||
<ClInclude Include="..\include\decoder.h">
|
||||
<Filter>Header Files</Filter>
|
||||
</ClInclude>
|
||||
|
@ -151,6 +154,9 @@
|
|||
<ClCompile Include="..\src\data.c">
|
||||
<Filter>Source Files</Filter>
|
||||
</ClCompile>
|
||||
<ClCompile Include="..\src\data_tag.c">
|
||||
<Filter>Source Files</Filter>
|
||||
</ClCompile>
|
||||
<ClCompile Include="..\src\decoder_util.c">
|
||||
<Filter>Source Files</Filter>
|
||||
</ClCompile>
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue