From bdf1b35fbbefcfe4545d1dd4ee6e30d05920b102 Mon Sep 17 00:00:00 2001 From: Harald Welte Date: Fri, 17 May 2019 10:21:45 +0200 Subject: integrate libosmocore logging into firmware We do so using our own 'raw' log target to avoid the 4k-on-stack buffer of libosmocore _output() function. Change-Id: I7a10b5b2b50bcee0154a1fa3fc43756aec836226 --- sysmoOCTSIM/gcc/Makefile | 3 ++ sysmoOCTSIM/libosmo_emb.c | 112 ++++++++++++++++++++++++++++++++++++++++++++++ sysmoOCTSIM/logging.h | 8 ++++ sysmoOCTSIM/main.c | 9 ++-- 4 files changed, 129 insertions(+), 3 deletions(-) create mode 100644 sysmoOCTSIM/libosmo_emb.c create mode 100644 sysmoOCTSIM/logging.h diff --git a/sysmoOCTSIM/gcc/Makefile b/sysmoOCTSIM/gcc/Makefile index 3001015..640fdff 100644 --- a/sysmoOCTSIM/gcc/Makefile +++ b/sysmoOCTSIM/gcc/Makefile @@ -83,6 +83,7 @@ hal/src/hal_init.o \ gcc/gcc/startup_same54.o \ hal/src/hal_usb_device.o \ main.o \ +libosmo_emb.o \ manual_test.o \ talloc.o \ usb_descriptors.o \ @@ -136,6 +137,7 @@ OBJS_AS_ARGS += \ "gcc/gcc/startup_same54.o" \ "hal/src/hal_usb_device.o" \ "main.o" \ +"libosmo_emb.o" \ "manual_test.o" \ "talloc.o" \ "usb_descriptors.o" \ @@ -195,6 +197,7 @@ DEPS_AS_ARGS += \ "hal/src/hal_usart_async_rings.d" \ "hpl/osc32kctrl/hpl_osc32kctrl.d" \ "main.d" \ +"libosmo_emb.d" \ "manual_test.d" \ "talloc.d" \ "usb_descriptors.d" \ diff --git a/sysmoOCTSIM/libosmo_emb.c b/sysmoOCTSIM/libosmo_emb.c new file mode 100644 index 0000000..b5be6f1 --- /dev/null +++ b/sysmoOCTSIM/libosmo_emb.c @@ -0,0 +1,112 @@ +/* Integration of libosmocore into our bare-iron embedded target */ + +#include +#include +#include +#include + + +extern void *g_tall_ctx; +void *g_msgb_ctx; + +/*********************************************************************** + * Logging + ***********************************************************************/ + +#include "logging.h" +#include + +static const struct log_info_cat log_info_cat[] = { + [DUSB] = { + .name = "USB", + .description = "USB Transport", + .enabled = 1, + .loglevel = LOGL_NOTICE, + }, + [DCCID] = { + .name = "CCID", + .description = "CCID Core", + .color = "\033[1;35m", + .enabled = 1, + .loglevel = LOGL_DEBUG, + }, +}; + +static const struct log_info log_info = { + .cat = log_info_cat, + .num_cat = ARRAY_SIZE(log_info_cat), +}; + +/* call-back function to the logging framework. We use this instead of the libosmocore + * provided target, as libosmocore wants to put 4kB on the stack. */ +static void stderr_raw_output(struct log_target *target, int subsys, unsigned int level, + const char *file, int line, int cont, const char *format, va_list ap) +{ + if (!cont) { + /* TODO: Timestamp? */ + if (target->print_category) + fprintf(stderr, "%s ", log_category_name(subsys)); + + if (target->print_level) + fprintf(stderr, "%s ", log_level_str(level)); + + if (target->print_category_hex) + fprintf(stderr, "<%4.4x> ", subsys); + + if (target->print_filename_pos == LOG_FILENAME_POS_HEADER_END) { + const char *bn; + switch (target->print_filename2) { + case LOG_FILENAME_NONE: + break; + case LOG_FILENAME_PATH: + fprintf(stderr, "%s:%d ", file, line); + break; + case LOG_FILENAME_BASENAME: + bn = strrchr(file, '/'); + if (!bn || !bn[1]) + bn = file; + else + bn++; + fprintf(stderr, "%s:%d ", bn, line); + break; + } + } + } + vfprintf(stderr, format, ap); + /* TODO: LOG_FILENAME_POS_LINE_END; we cannot modify the format string here :/ */ + int fmt_len = strlen(format); + if (fmt_len && format[fmt_len-1] == '\n') + fputc('\r', stderr); +} + +static struct log_target *log_target_create_stderr_raw(void) +{ + struct log_target *target; + + target = log_target_create(); + if (!target) + return NULL; + + target->type = LOG_TGT_TYPE_STDERR; + target->raw_output = stderr_raw_output; + target->print_category = true; + target->print_level = true; + + return target; +} + +void libosmo_emb_init(void) +{ + struct log_target *stderr_target; + + /* msgb */ + g_msgb_ctx = talloc_pool(g_tall_ctx, 20480); + talloc_set_memlimit(g_msgb_ctx, 20480); + msgb_talloc_ctx_init(g_msgb_ctx, 0); + + /* logging */ + log_init(&log_info, g_tall_ctx); + stderr_target = log_target_create_stderr_raw(); + log_add_target(stderr_target); + log_set_all_filter(stderr_target, 1); +} diff --git a/sysmoOCTSIM/logging.h b/sysmoOCTSIM/logging.h new file mode 100644 index 0000000..900f88e --- /dev/null +++ b/sysmoOCTSIM/logging.h @@ -0,0 +1,8 @@ +#pragma once + +#include + +enum { + DUSB, + DCCID +}; diff --git a/sysmoOCTSIM/main.c b/sysmoOCTSIM/main.c index 80b52dc..01ea0a8 100644 --- a/sysmoOCTSIM/main.c +++ b/sysmoOCTSIM/main.c @@ -946,11 +946,12 @@ DEFUN(sim_iccid, cmd_sim_iccid, "sim-iccid", "Read ICCID from SIM card") } extern void testmode_init(void); +extern void libosmo_emb_init(void); #include "talloc.h" +#include "logging.h" #include void *g_tall_ctx; -void *g_msgb_ctx; DEFUN(_talloc_report, cmd_talloc_report, "talloc-report", "Generate a talloc report") { @@ -1039,8 +1040,10 @@ int main(void) talloc_enable_null_tracking(); g_tall_ctx = talloc_named_const(NULL, 0, "global"); printf("g_tall_ctx=%p\r\n", g_tall_ctx); - g_msgb_ctx = talloc_pool(g_tall_ctx, 20480); - talloc_set_memlimit(g_msgb_ctx, 20480); + + libosmo_emb_init(); + + LOGP(DUSB, LOGL_ERROR, "foobar usb\n"); command_print_prompt(); while (true) { // main loop -- cgit v1.2.3