Browse Source

Introduce optional build of gdml

Use level from audio frames as guard in reflector
master
Jeroen Vreeken 10 months ago
parent
commit
8c907cbb14
11 changed files with 199 additions and 89 deletions
  1. +10
    -1
      Makefile.am
  2. +12
    -4
      configure.ac
  3. +1
    -1
      dml/dml_host.h
  4. +105
    -15
      dml_config.c
  5. +3
    -0
      dml_config.h
  6. +4
    -1
      dml_crypto.c
  7. +3
    -17
      dml_fprs_db.c
  8. +24
    -2
      dml_host.c
  9. +1
    -5
      dml_list.c
  10. +32
    -25
      dml_reflector.c
  11. +4
    -18
      dml_trx.c

+ 10
- 1
Makefile.am View File

@ -23,7 +23,7 @@ libdml_la_SOURCES = \
libdml_la_CFLAGS=-fPIC
libdml_la_LDFLAGS= -shared -fPIC -version-info 0:0:0 @LIB_LDFLAGS@ @GLIB_LIBS@
bin_PROGRAMS = dmld dml_list dml_streamer dml_stream_client dml_stream_client_codec2 dml_fprs_db
bin_PROGRAMS = dmld dml_list dml_streamer dml_stream_client dml_stream_client_codec2 dml_fprs_db gdml
DML_SRCS = \
dml_stream_client_simple.c
@ -96,3 +96,12 @@ endif
dml_fprs_db_SOURCES = $(DML_SRCS) $(FPRS_DB_SRCS) dml_fprs_db.c
dml_fprs_db_LDADD = libdml.la
dml_fprs_db_LDFLAGS = -leth_ar -lm
if ENABLE_GDML
bin_PROGRAMS += gdml
gdml_CFLAGS = @GTK_CFLAGS@
gdml_LDADD = libdml.la
gdml_LDFLAGS = @GTK_LIBS@
endif

+ 12
- 4
configure.ac View File

@ -94,25 +94,32 @@ AC_SEARCH_LIBS([magic_open], [magic], [libmagic_found_lib=yes], [])
AC_CHECK_HEADERS([magic.h],
[libmagic_found_headers=yes; break;])
AS_IF([test "x$libmagic_found_headers" = "xyes" && test "x$libmagic_found_lib" = "xyes"],
[enable_dml_httpd="yes"], [enable_dml_httpd="no"]
)
# glib
PKG_CHECK_MODULES([GLIB], [glib-2.0])
# gtk+
PKG_CHECK_MODULES([GTK], [gtk+-3.0], [GTK_FOUND=yes], [GTK_FOUND=no])
# build selection logic
AS_IF([test "x$libmagic_found_headers" = "xyes" && test "x$libmagic_found_lib" = "xyes"],
[enable_dml_httpd="yes"], [enable_dml_httpd="no"]
)
AS_IF([test "x$EMSCRIPTEN_FOUND" = "xno" ],
[enable_dml_trx="yes"; enable_dml_reflector="yes" ],
[enable_dml_trx="no"; enable_dml_reflector="no" ]
)
AS_IF([test "x$GTK_FOUND" = "xyes" ], [enable_gdml="yes"], [enable_gdml="no"])
AM_CONDITIONAL(ENABLE_DML_HTTPD, test "x$enable_dml_httpd" = "xyes")
AM_CONDITIONAL(ENABLE_DML_REFLECTOR, test "x$enable_dml_reflector" = "xyes")
AM_CONDITIONAL(ENABLE_DML_TRX, test "x$enable_dml_trx" = "xyes")
AM_CONDITIONAL(ENABLE_GDML, test "x$enable_gdml" = "xyes")
AC_SUBST([LIB_LDFLAGS])
@ -124,3 +131,4 @@ echo " dml_httpd: " $enable_dml_httpd
echo " dml_reflector: " $enable_dml_reflector
echo " dml_trx: " $enable_dml_trx
echo " wasm: " $EMSCRIPTEN_FOUND
echo " gdml: " $enable_gdml

+ 1
- 1
dml/dml_host.h View File

@ -23,7 +23,7 @@
struct dml_host;
struct dml_host *dml_host_create(char *server);
struct dml_host *dml_host_create(char *config_file);
struct dml_connection *dml_host_connection_get(struct dml_host *host);


+ 105
- 15
dml_config.c View File

@ -1,5 +1,5 @@
/*
Copyright Jeroen Vreeken (jeroen@vreeken.net), 2015
Copyright Jeroen Vreeken (jeroen@vreeken.net), 2015, 2020
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
@ -15,10 +15,14 @@
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#define _GNU_SOURCE
#include "dml_config.h"
#include <stdio.h>
#include <string.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <glib.h>
struct dml_config {
struct dml_config *next;
@ -29,11 +33,83 @@ struct dml_config {
static struct dml_config *config_list = NULL;
static char *dml_config_file = "dml.conf";
static char *dml_config_default_path = NULL;
static void dml_config_default_init(void)
{
const char *home = g_get_home_dir();
asprintf(&dml_config_default_path, "%s/.dml/", home);
printf("dml_config_default_path: %s\n", dml_config_default_path);
mkdir(dml_config_default_path, 0700);
}
char *dml_config_path(void)
{
if (!dml_config_default_path)
{
dml_config_default_init();
}
return dml_config_default_path;
}
static char *dml_config_default(void)
{
if (!dml_config_default_path)
{
dml_config_default_init();
}
char *conf;
asprintf(&conf, "%s/dml.conf", dml_config_default_path);
printf("file: %s\n", conf);
return conf;
}
int dml_config_add(char *key, char *value)
{
struct dml_config *conf;
conf = calloc(1, sizeof(struct dml_config));
if (!conf)
goto err_alloc;
conf->key = strdup(key);
if (!conf->key)
goto err_key;
conf->value = strdup(value);
if (!conf->value)
goto err_value;
struct dml_config **entry;
for (entry = &config_list; *entry; entry = &(*entry)->next);
*entry = conf;
return 0;
err_value:
free(conf->key);
err_key:
free(conf);
err_alloc:
return -1;
}
int dml_config_load(char *file)
{
FILE *fd;
char *rf;
if (!file)
file = dml_config_default();
dml_config_file = strdup(file);
fd = fopen(file, "r");
if (!fd)
goto err_fopen;
@ -48,23 +124,12 @@ int dml_config_load(char *file)
key = strtok(buffer, " \t=");
value = strtok(NULL, "\n\r");
if (key && value) {
struct dml_config *conf;
while (value[0] == ' ' ||
value[0] == '\t' ||
value[0] == '=')
value++;
conf = calloc(1, sizeof(struct dml_config));
if (!conf)
goto err_calloc;
conf->key = strdup(key);
conf->value = strdup(value);
struct dml_config **entry;
for (entry = &config_list; *entry; entry = &(*entry)->next);
*entry = conf;
dml_config_add(key, value);
}
} while (rf);
@ -72,12 +137,34 @@ int dml_config_load(char *file)
return 0;
err_calloc:
err_fopen:
return -1;
}
int dml_config_save(char *file)
{
FILE *fd;
if (!file)
file = dml_config_file;
fd = fopen(file, "w");
if (!fd)
goto err_fopen;
struct dml_config *entry;
for (entry = config_list; entry; entry = entry->next) {
fprintf(fd, "%s=%s\n", entry->key, entry->value);
}
fclose(fd);
return 0;
err_fopen:
return -1;
}
char *dml_config_value(char *key, char *prev_value, char *def)
{
struct dml_config *entry;
@ -93,5 +180,8 @@ char *dml_config_value(char *key, char *prev_value, char *def)
return entry->value;
}
if (def)
dml_config_add(key, def);
return def;
}

+ 3
- 0
dml_config.h View File

@ -20,7 +20,10 @@
#include <stdlib.h>
char *dml_config_path(void);
int dml_config_load(char *file);
int dml_config_save(char *file);
char *dml_config_value(char *key, char *prev_value, char *def);


+ 4
- 1
dml_crypto.c View File

@ -132,8 +132,11 @@ int dml_crypto_cert_add_verify(void *certdata, size_t size, uint8_t id[DML_ID_SI
X509_STORE_CTX_free(ctx);
// fprintf(stderr, "verify cert rc: %d: %d\n", rc, err);
if (rc != 1)
if (rc != 1) {
int x509_err = X509_STORE_CTX_get_error(ctx);
fprintf(stderr, "verify error: %d: %s\n", x509_err, X509_verify_cert_error_string(x509_err));
goto err_verify;
}
struct dml_stream *ds = dml_stream_by_id(id);
if (!ds)


+ 3
- 17
dml_fprs_db.c View File

@ -238,8 +238,6 @@ int main(int argc, char **argv)
char *file = "dml_fprs_db.conf";
char *certificate;
char *key;
char *server;
char *ca;
uint8_t id[DML_ID_SIZE];
char *name;
char *description;
@ -249,19 +247,17 @@ int main(int argc, char **argv)
if (argc > 1)
file = argv[1];
if (dml_config_load(file)) {
printf("Failed to load config file %s\n", file);
host = dml_host_create(file);
if (!host) {
printf("Could not create host\n");
return -1;
}
name = dml_config_value("name", NULL, "test_db");
description = dml_config_value("description", NULL, "Test database");
server = dml_config_value("server", NULL, "localhost");
certificate = dml_config_value("certificate", NULL, "");
key = dml_config_value("key", NULL, "");
ca = dml_config_value("ca", NULL, ".");
aprsis_port = atoi(dml_config_value("aprsis_port", NULL, "14580"));
aprsis_host = dml_config_value("aprsis_host", NULL, NULL);
aprsis_call = dml_config_value("aprsis_call", NULL, NULL);
@ -272,11 +268,6 @@ int main(int argc, char **argv)
true, message_cb);
}
if (dml_crypto_init(NULL, ca)) {
fprintf(stderr, "Failed to init crypto\n");
return -1;
}
if (dml_crypto_load_cert(certificate)) {
printf("Could not load certificate\n");
return -1;
@ -319,11 +310,6 @@ int main(int argc, char **argv)
dml_stream_description_set(stream_fprs_db, description);
dml_stream_bps_set(stream_fprs_db, bps);
host = dml_host_create(server);
if (!host) {
printf("Could not create host\n");
return -1;
}
dml_host_mime_filter_set(host, 1, (char*[]){ DML_MIME_FPRS });
dml_host_stream_added_cb_set(host, stream_added_cb, NULL);
dml_host_stream_removed_cb_set(host, stream_removed_cb, NULL);


+ 24
- 2
dml_host.c View File

@ -23,6 +23,8 @@
#include <dml/dml_crypto.h>
#include <dml/dml_packet.h>
#include <dml_config.h>
#include <string.h>
#include <stdio.h>
@ -525,12 +527,28 @@ int dml_host_connection_closed_cb_set(struct dml_host *host,
return 0;
}
struct dml_host *dml_host_create(char *server)
struct dml_host *dml_host_create(char *config_file)
{
struct dml_host *host = calloc(1, sizeof(struct dml_host));
if (!host)
goto err_alloc;
if (dml_config_load(config_file)) {
if (config_file) {
printf("Failed to load config file %s\n", config_file);
goto err_config;
}
}
char *server = dml_config_value("server", NULL, "localhost");
char *ca = dml_config_value("ca", NULL, dml_config_path());
if (dml_crypto_init(NULL, ca)) {
fprintf(stderr, "Failed to init crypto\n");
goto err_crypto;
}
host->client = dml_client_create(server, 0, client_connect, host);
if (dml_client_connect(host->client)) {
@ -538,7 +556,11 @@ struct dml_host *dml_host_create(char *server)
g_timeout_add_seconds(2, client_reconnect, host);
}
err_alloc:
return host;
err_crypto:
err_config:
free(host);
err_alloc:
return NULL;
}

+ 1
- 5
dml_list.c View File

@ -69,11 +69,7 @@ int main(int argc, char **argv)
{
struct dml_host *host;
char *server = "localhost";
if (argc > 1)
server = argv[1];
host = dml_host_create(server);
host = dml_host_create(NULL);
if (!host) {
printf("Could not create host\n");
return -1;


+ 32
- 25
dml_reflector.c View File

@ -37,10 +37,11 @@
#include <time.h>
#define DML_REFLECTOR_PARROT_WAIT (500)
#define DML_REFLECTOR_PARROT_WAIT_MS (500)
#define DML_REFLECTOR_PARROT_MAX (60*60*50)
#define DML_REFLECTOR_DATA_KEEPALIVE 10
#define DML_REFLECTOR_GUARD_TIME_MS (200)
uint8_t ref_id[DML_ID_SIZE];
char *name;
@ -232,11 +233,22 @@ void parrot_queue_add(void *data, size_t size, int duration)
*listp = entry;
g_source_remove_by_user_data(&parrot_queue);
g_timeout_add(DML_REFLECTOR_PARROT_WAIT, parrot_dequeue, &parrot_queue);
g_timeout_add(DML_REFLECTOR_PARROT_WAIT_MS, parrot_dequeue, &parrot_queue);
}
static bool tx_state = false;
static uint8_t tx_level = 0;
static char tx_call[ETH_AR_MAC_SIZE] = {0};
static gboolean guard_cb(void *arg)
{
printf("No incomming activity, releasing guard");
tx_level = 0;
return G_SOURCE_REMOVE;
}
static void stream_data_cb(struct dml_host *host, struct dml_stream *ds, uint64_t timestamp, void *data, size_t data_size, void *arg)
{
@ -248,20 +260,29 @@ static void stream_data_cb(struct dml_host *host, struct dml_stream *ds, uint64_
uint8_t *datab = data;
int mode = datab[6];
bool state = datab[7] & 0x1;
uint8_t level = datab[7];
duration = trx_dv_duration(data_size - 8, mode);
printf("mode %d state %d duration: %d\n", mode, state, duration);
printf("mode %d level %d duration: %d: ", mode, level, duration);
if (state != tx_state) {
if (level > tx_level) {
char call[ETH_AR_CALL_SIZE];
int ssid;
bool multicast;
eth_ar_mac2call(call, &ssid, &multicast, data);
tx_state = state;
printf("State changed to %s by %s-%d\n", state ? "ON":"OFF", multicast ? "MULTICAST" : call, ssid);
tx_level = level;
memcpy(tx_call, data, ETH_AR_MAC_SIZE);
printf("State changed to %s (level=%d) by %s-%d\n", level ? "ON":"OFF", level, multicast ? "MULTICAST" : call, ssid);
} else {
if (memcmp(data, tx_call, ETH_AR_MAC_SIZE)) {
printf("Dropped due to tx guard\n");
return;
}
printf("Accepted\n");
g_source_remove_by_user_data(&tx_level);
g_timeout_add(DML_REFLECTOR_GUARD_TIME_MS, guard_cb, &tx_level);
}
if (!parrot)
@ -324,15 +345,14 @@ int main(int argc, char **argv)
char *file = "dml_reflector.conf";
char *certificate;
char *key;
char *server;
char *ca;
uint8_t *header;
if (argc > 1)
file = argv[1];
if (dml_config_load(file)) {
printf("Failed to load config file %s\n", file);
host = dml_host_create(file);
if (!host) {
printf("Could not create host\n");
return -1;
}
name = dml_config_value("name", NULL, "test_reflector");
@ -341,17 +361,9 @@ int main(int argc, char **argv)
parrot = atoi(dml_config_value("parrot", NULL, "0"));
server = dml_config_value("server", NULL, "localhost");
certificate = dml_config_value("certificate", NULL, "");
key = dml_config_value("key", NULL, "");
ca = dml_config_value("ca", NULL, ".");
if (dml_crypto_init(NULL, ca)) {
fprintf(stderr, "Failed to init crypto\n");
return -1;
}
if (dml_crypto_load_cert(certificate)) {
printf("Could not load certificate\n");
return -1;
@ -375,11 +387,6 @@ int main(int argc, char **argv)
dml_stream_description_set(stream_dv, description);
dml_stream_bps_set(stream_dv, bps);
host = dml_host_create(server);
if (!host) {
printf("Could not create host\n");
return -1;
}
dml_host_mime_filter_set(host, 1, (char*[]){ DML_MIME_DV_C2 });
dml_host_stream_data_cb_set(host, stream_data_cb, NULL);
dml_host_stream_req_reverse_connect_cb_set(host, stream_req_reverse_connect_cb, NULL);


+ 4
- 18
dml_trx.c View File

@ -661,7 +661,7 @@ static int dv_in_cb(void *arg, uint8_t from[6], uint8_t to[6], uint8_t *dv, size
uint8_t data[8 + size];
if (!rx_state) {
printf("rx_state to on\n");
printf("rx_state to on, level: %d\n", level);
}
rx_state = level;
@ -963,8 +963,6 @@ int main(int argc, char **argv)
char *file = "dml_trx.conf";
char *certificate;
char *key;
char *server;
char *ca;
char *dv_dev;
char *name;
char *description;
@ -979,15 +977,15 @@ int main(int argc, char **argv)
if (argc > 1)
file = argv[1];
if (dml_config_load(file)) {
printf("Failed to load config file %s\n", file);
host = dml_host_create(file);
if (!host) {
printf("Could not create host\n");
return -1;
}
name = dml_config_value("name", NULL, "test_trx");
alias = dml_config_value("alias", NULL, "0000");
description = dml_config_value("description", NULL, "Test transceiver");
server = dml_config_value("server", NULL, "localhost");
certificate = dml_config_value("certificate", NULL, "");
key = dml_config_value("key", NULL, "");
@ -1011,13 +1009,6 @@ int main(int argc, char **argv)
return -1;
}
ca = dml_config_value("ca", NULL, ".");
if (dml_crypto_init(NULL, ca)) {
fprintf(stderr, "Failed to init crypto\n");
return -1;
}
if (dml_crypto_load_cert(certificate)) {
printf("Could not load certificate\n");
return -1;
@ -1054,11 +1045,6 @@ int main(int argc, char **argv)
dml_stream_description_set(stream_fprs, description);
dml_stream_bps_set(stream_fprs, bps);
host = dml_host_create(server);
if (!host) {
printf("Could not create host\n");
return -1;
}
dml_host_connection_closed_cb_set(host, connection_closed_cb, NULL);
dml_host_mime_filter_set(host, 2, (char*[]){ DML_MIME_DV_C2 , DML_MIME_FPRS });
dml_host_stream_removed_cb_set(host, stream_removed_cb, NULL);


Loading…
Cancel
Save