blob: 5209e2072061125d77bb66337ea262ac9d55f10a [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",
44 .enabled = 1,
45 .loglevel = LOGL_NOTICE,
46 },
47 [DCCID] = {
48 .name = "CCID",
Harald Welte06348362019-05-19 00:45:17 +020049 .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 Weltebdf1b352019-05-17 10:21:45 +020080 .enabled = 1,
81 .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 */
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();
Eric Wildff5c3902019-10-17 20:21:44 +0200160 //log_add_target(stderr_target);
161 //log_set_all_filter(stderr_target, 1);
Harald Welteb98478a2019-05-17 19:15:08 +0200162
163 /* timer */
164 SysTick_Config(SystemCoreClock / 1000);
Harald Weltebdf1b352019-05-17 10:21:45 +0200165}