blob: a12c490f7bc753fdeb197f87d9889ab4b760d8bb [file] [log] [blame]
Harald Weltebdf1b352019-05-17 10:21:45 +02001/* 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
9extern void *g_tall_ctx;
10void *g_msgb_ctx;
11
12/***********************************************************************
Harald Welte8a534f12019-05-17 17:20:25 +020013 * Timers
14 ***********************************************************************/
15
16#include <sys/time.h>
Harald Welteb98478a2019-05-17 19:15:08 +020017#include "driver_init.h"
Harald Welte8a534f12019-05-17 17:20:25 +020018
Harald Welteb98478a2019-05-17 19:15:08 +020019volatile uint64_t jiffies;
20
21void SysTick_Handler(void)
Harald Welte8a534f12019-05-17 17:20:25 +020022{
Harald Welteb98478a2019-05-17 19:15:08 +020023 jiffies++;
Harald Welte8a534f12019-05-17 17:20:25 +020024}
25
Harald Welteb98478a2019-05-17 19:15:08 +020026int _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 Welte8a534f12019-05-17 17:20:25 +020032
33/***********************************************************************
Harald Weltebdf1b352019-05-17 10:21:45 +020034 * Logging
35 ***********************************************************************/
36
37#include "logging.h"
38#include <osmocom/core/logging.h>
39
40static const struct log_info_cat log_info_cat[] = {
41 [DUSB] = {
42 .name = "USB",
43 .description = "USB Transport",
Eric Wild27f60452019-11-27 18:04:39 +010044 .enabled = 0,
Harald Weltebdf1b352019-05-17 10:21:45 +020045 .loglevel = LOGL_NOTICE,
46 },
47 [DCCID] = {
48 .name = "CCID",
Harald Welte06348362019-05-19 00:45:17 +020049 .description = "USB-CCID Protocol",
Eric Wild27f60452019-11-27 18:04:39 +010050 .enabled = 0,
Harald Welte06348362019-05-19 00:45:17 +020051 .loglevel = LOGL_DEBUG,
52 },
53 [DISO7816] = {
54 .name = "ISO7816",
55 .description = "ISO7816-3 State machines",
Eric Wild27f60452019-11-27 18:04:39 +010056 .enabled = 0,
Harald Welte06348362019-05-19 00:45:17 +020057 .loglevel = LOGL_DEBUG,
58 },
59 [DATR] = {
60 .name = "ATR",
61 .description = "ATR (Answer To Reset) FSM",
Eric Wild27f60452019-11-27 18:04:39 +010062 .enabled = 0,
Harald Welte06348362019-05-19 00:45:17 +020063 .loglevel = LOGL_DEBUG,
64 },
65 [DTPDU] = {
66 .name = "TPDU",
67 .description = "TPDU FSM",
Eric Wild27f60452019-11-27 18:04:39 +010068 .enabled = 0,
Harald Welte06348362019-05-19 00:45:17 +020069 .loglevel = LOGL_DEBUG,
70 },
71 [DPPS] = {
72 .name = "PPS",
73 .description = "PPS (Protocol and Parameter Selection) FSM",
Eric Wild27f60452019-11-27 18:04:39 +010074 .enabled = 0,
Harald Welte06348362019-05-19 00:45:17 +020075 .loglevel = LOGL_DEBUG,
76 },
77 [DCARD] = {
78 .name = "CARD",
79 .description = "Card FSM",
Eric Wild27f60452019-11-27 18:04:39 +010080 .enabled = 0,
Harald Weltebdf1b352019-05-17 10:21:45 +020081 .loglevel = LOGL_DEBUG,
82 },
83};
84
85static 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. */
92static 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
132static 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
148void libosmo_emb_init(void)
149{
150 struct log_target *stderr_target;
151
152 /* msgb */
Eric Wild27f60452019-11-27 18:04:39 +0100153#if 0
Harald Weltebdf1b352019-05-17 10:21:45 +0200154 g_msgb_ctx = talloc_pool(g_tall_ctx, 20480);
155 talloc_set_memlimit(g_msgb_ctx, 20480);
156 msgb_talloc_ctx_init(g_msgb_ctx, 0);
Eric Wild27f60452019-11-27 18:04:39 +0100157#endif
Harald Weltebdf1b352019-05-17 10:21:45 +0200158 /* logging */
159 log_init(&log_info, g_tall_ctx);
Eric Wild27f60452019-11-27 18:04:39 +0100160#if 0
Harald Weltebdf1b352019-05-17 10:21:45 +0200161 stderr_target = log_target_create_stderr_raw();
162 log_add_target(stderr_target);
163 log_set_all_filter(stderr_target, 1);
Eric Wild27f60452019-11-27 18:04:39 +0100164#endif
Harald Welteb98478a2019-05-17 19:15:08 +0200165 /* timer */
166 SysTick_Config(SystemCoreClock / 1000);
Harald Weltebdf1b352019-05-17 10:21:45 +0200167}