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", |
Harald Welte | 0634836 | 2019-05-19 00:45:17 +0200 | [diff] [blame] | 49 | .description = "USB-CCID Protocol", |
| 50 | .enabled = 1, |
| 51 | .loglevel = LOGL_DEBUG, |
| 52 | }, |
| 53 | [DISO7816] = { |
| 54 | .name = "ISO7816", |
| 55 | .description = "ISO7816-3 State machines", |
| 56 | .enabled = 1, |
| 57 | .loglevel = LOGL_DEBUG, |
| 58 | }, |
| 59 | [DATR] = { |
| 60 | .name = "ATR", |
| 61 | .description = "ATR (Answer To Reset) FSM", |
| 62 | .enabled = 1, |
| 63 | .loglevel = LOGL_DEBUG, |
| 64 | }, |
| 65 | [DTPDU] = { |
| 66 | .name = "TPDU", |
| 67 | .description = "TPDU FSM", |
| 68 | .enabled = 1, |
| 69 | .loglevel = LOGL_DEBUG, |
| 70 | }, |
| 71 | [DPPS] = { |
| 72 | .name = "PPS", |
| 73 | .description = "PPS (Protocol and Parameter Selection) FSM", |
| 74 | .enabled = 1, |
| 75 | .loglevel = LOGL_DEBUG, |
| 76 | }, |
| 77 | [DCARD] = { |
| 78 | .name = "CARD", |
| 79 | .description = "Card FSM", |
Harald Welte | bdf1b35 | 2019-05-17 10:21:45 +0200 | [diff] [blame] | 80 | .enabled = 1, |
| 81 | .loglevel = LOGL_DEBUG, |
| 82 | }, |
| 83 | }; |
| 84 | |
| 85 | static const struct log_info log_info = { |
| 86 | .cat = log_info_cat, |
| 87 | .num_cat = ARRAY_SIZE(log_info_cat), |
| 88 | }; |
| 89 | |
| 90 | /* call-back function to the logging framework. We use this instead of the libosmocore |
| 91 | * provided target, as libosmocore wants to put 4kB on the stack. */ |
| 92 | static void stderr_raw_output(struct log_target *target, int subsys, unsigned int level, |
| 93 | const char *file, int line, int cont, const char *format, va_list ap) |
| 94 | { |
| 95 | if (!cont) { |
| 96 | /* TODO: Timestamp? */ |
| 97 | if (target->print_category) |
| 98 | fprintf(stderr, "%s ", log_category_name(subsys)); |
| 99 | |
| 100 | if (target->print_level) |
| 101 | fprintf(stderr, "%s ", log_level_str(level)); |
| 102 | |
| 103 | if (target->print_category_hex) |
| 104 | fprintf(stderr, "<%4.4x> ", subsys); |
| 105 | |
| 106 | if (target->print_filename_pos == LOG_FILENAME_POS_HEADER_END) { |
| 107 | const char *bn; |
| 108 | switch (target->print_filename2) { |
| 109 | case LOG_FILENAME_NONE: |
| 110 | break; |
| 111 | case LOG_FILENAME_PATH: |
| 112 | fprintf(stderr, "%s:%d ", file, line); |
| 113 | break; |
| 114 | case LOG_FILENAME_BASENAME: |
| 115 | bn = strrchr(file, '/'); |
| 116 | if (!bn || !bn[1]) |
| 117 | bn = file; |
| 118 | else |
| 119 | bn++; |
| 120 | fprintf(stderr, "%s:%d ", bn, line); |
| 121 | break; |
| 122 | } |
| 123 | } |
| 124 | } |
| 125 | vfprintf(stderr, format, ap); |
| 126 | /* TODO: LOG_FILENAME_POS_LINE_END; we cannot modify the format string here :/ */ |
| 127 | int fmt_len = strlen(format); |
| 128 | if (fmt_len && format[fmt_len-1] == '\n') |
| 129 | fputc('\r', stderr); |
| 130 | } |
| 131 | |
| 132 | static struct log_target *log_target_create_stderr_raw(void) |
| 133 | { |
| 134 | struct log_target *target; |
| 135 | |
| 136 | target = log_target_create(); |
| 137 | if (!target) |
| 138 | return NULL; |
| 139 | |
| 140 | target->type = LOG_TGT_TYPE_STDERR; |
| 141 | target->raw_output = stderr_raw_output; |
| 142 | target->print_category = true; |
| 143 | target->print_level = true; |
| 144 | |
| 145 | return target; |
| 146 | } |
| 147 | |
| 148 | void libosmo_emb_init(void) |
| 149 | { |
| 150 | struct log_target *stderr_target; |
| 151 | |
| 152 | /* msgb */ |
| 153 | g_msgb_ctx = talloc_pool(g_tall_ctx, 20480); |
| 154 | talloc_set_memlimit(g_msgb_ctx, 20480); |
| 155 | msgb_talloc_ctx_init(g_msgb_ctx, 0); |
| 156 | |
| 157 | /* logging */ |
| 158 | log_init(&log_info, g_tall_ctx); |
| 159 | stderr_target = log_target_create_stderr_raw(); |
| 160 | log_add_target(stderr_target); |
| 161 | log_set_all_filter(stderr_target, 1); |
Harald Welte | b98478a | 2019-05-17 19:15:08 +0200 | [diff] [blame] | 162 | |
| 163 | /* timer */ |
| 164 | SysTick_Config(SystemCoreClock / 1000); |
Harald Welte | bdf1b35 | 2019-05-17 10:21:45 +0200 | [diff] [blame] | 165 | } |