Harald Welte | bdf1b35 | 2019-05-17 10:21:45 +0200 | [diff] [blame] | 1 | /* Integration of libosmocore into our bare-iron embedded target */ |
| 2 | |
| 3 | #include <string.h> |
| 4 | #include <stdio.h> |
| 5 | #include <osmocom/core/utils.h> |
| 6 | #include <osmocom/core/msgb.h> |
| 7 | |
| 8 | |
| 9 | extern void *g_tall_ctx; |
| 10 | void *g_msgb_ctx; |
| 11 | |
| 12 | /*********************************************************************** |
Harald Welte | 8a534f1 | 2019-05-17 17:20:25 +0200 | [diff] [blame] | 13 | * Timers |
| 14 | ***********************************************************************/ |
| 15 | |
| 16 | #include <sys/time.h> |
Harald Welte | b98478a | 2019-05-17 19:15:08 +0200 | [diff] [blame] | 17 | #include "driver_init.h" |
Harald Welte | 8a534f1 | 2019-05-17 17:20:25 +0200 | [diff] [blame] | 18 | |
Harald Welte | b98478a | 2019-05-17 19:15:08 +0200 | [diff] [blame] | 19 | volatile uint64_t jiffies; |
| 20 | |
| 21 | void SysTick_Handler(void) |
Harald Welte | 8a534f1 | 2019-05-17 17:20:25 +0200 | [diff] [blame] | 22 | { |
Harald Welte | b98478a | 2019-05-17 19:15:08 +0200 | [diff] [blame] | 23 | jiffies++; |
Harald Welte | 8a534f1 | 2019-05-17 17:20:25 +0200 | [diff] [blame] | 24 | } |
| 25 | |
Harald Welte | b98478a | 2019-05-17 19:15:08 +0200 | [diff] [blame] | 26 | int _gettimeofday(struct timeval *tv, void *tz) |
| 27 | { |
| 28 | tv->tv_sec = jiffies / 1000; |
| 29 | tv->tv_usec = (jiffies % 1000) * 1000; |
| 30 | return 0; |
| 31 | } |
Harald Welte | 8a534f1 | 2019-05-17 17:20:25 +0200 | [diff] [blame] | 32 | |
| 33 | /*********************************************************************** |
Harald Welte | bdf1b35 | 2019-05-17 10:21:45 +0200 | [diff] [blame] | 34 | * Logging |
| 35 | ***********************************************************************/ |
| 36 | |
| 37 | #include "logging.h" |
| 38 | #include <osmocom/core/logging.h> |
| 39 | |
| 40 | static const struct log_info_cat log_info_cat[] = { |
| 41 | [DUSB] = { |
| 42 | .name = "USB", |
| 43 | .description = "USB Transport", |
| 44 | .enabled = 1, |
| 45 | .loglevel = LOGL_NOTICE, |
| 46 | }, |
| 47 | [DCCID] = { |
| 48 | .name = "CCID", |
| 49 | .description = "CCID Core", |
| 50 | .color = "\033[1;35m", |
| 51 | .enabled = 1, |
| 52 | .loglevel = LOGL_DEBUG, |
| 53 | }, |
| 54 | }; |
| 55 | |
| 56 | static const struct log_info log_info = { |
| 57 | .cat = log_info_cat, |
| 58 | .num_cat = ARRAY_SIZE(log_info_cat), |
| 59 | }; |
| 60 | |
| 61 | /* call-back function to the logging framework. We use this instead of the libosmocore |
| 62 | * provided target, as libosmocore wants to put 4kB on the stack. */ |
| 63 | static void stderr_raw_output(struct log_target *target, int subsys, unsigned int level, |
| 64 | const char *file, int line, int cont, const char *format, va_list ap) |
| 65 | { |
| 66 | if (!cont) { |
| 67 | /* TODO: Timestamp? */ |
| 68 | if (target->print_category) |
| 69 | fprintf(stderr, "%s ", log_category_name(subsys)); |
| 70 | |
| 71 | if (target->print_level) |
| 72 | fprintf(stderr, "%s ", log_level_str(level)); |
| 73 | |
| 74 | if (target->print_category_hex) |
| 75 | fprintf(stderr, "<%4.4x> ", subsys); |
| 76 | |
| 77 | if (target->print_filename_pos == LOG_FILENAME_POS_HEADER_END) { |
| 78 | const char *bn; |
| 79 | switch (target->print_filename2) { |
| 80 | case LOG_FILENAME_NONE: |
| 81 | break; |
| 82 | case LOG_FILENAME_PATH: |
| 83 | fprintf(stderr, "%s:%d ", file, line); |
| 84 | break; |
| 85 | case LOG_FILENAME_BASENAME: |
| 86 | bn = strrchr(file, '/'); |
| 87 | if (!bn || !bn[1]) |
| 88 | bn = file; |
| 89 | else |
| 90 | bn++; |
| 91 | fprintf(stderr, "%s:%d ", bn, line); |
| 92 | break; |
| 93 | } |
| 94 | } |
| 95 | } |
| 96 | vfprintf(stderr, format, ap); |
| 97 | /* TODO: LOG_FILENAME_POS_LINE_END; we cannot modify the format string here :/ */ |
| 98 | int fmt_len = strlen(format); |
| 99 | if (fmt_len && format[fmt_len-1] == '\n') |
| 100 | fputc('\r', stderr); |
| 101 | } |
| 102 | |
| 103 | static struct log_target *log_target_create_stderr_raw(void) |
| 104 | { |
| 105 | struct log_target *target; |
| 106 | |
| 107 | target = log_target_create(); |
| 108 | if (!target) |
| 109 | return NULL; |
| 110 | |
| 111 | target->type = LOG_TGT_TYPE_STDERR; |
| 112 | target->raw_output = stderr_raw_output; |
| 113 | target->print_category = true; |
| 114 | target->print_level = true; |
| 115 | |
| 116 | return target; |
| 117 | } |
| 118 | |
| 119 | void libosmo_emb_init(void) |
| 120 | { |
| 121 | struct log_target *stderr_target; |
| 122 | |
| 123 | /* msgb */ |
| 124 | g_msgb_ctx = talloc_pool(g_tall_ctx, 20480); |
| 125 | talloc_set_memlimit(g_msgb_ctx, 20480); |
| 126 | msgb_talloc_ctx_init(g_msgb_ctx, 0); |
| 127 | |
| 128 | /* logging */ |
| 129 | log_init(&log_info, g_tall_ctx); |
| 130 | stderr_target = log_target_create_stderr_raw(); |
| 131 | log_add_target(stderr_target); |
| 132 | log_set_all_filter(stderr_target, 1); |
Harald Welte | b98478a | 2019-05-17 19:15:08 +0200 | [diff] [blame] | 133 | |
| 134 | /* timer */ |
| 135 | SysTick_Config(SystemCoreClock / 1000); |
Harald Welte | bdf1b35 | 2019-05-17 10:21:45 +0200 | [diff] [blame] | 136 | } |