Harald Welte | df7a306 | 2016-10-19 10:47:17 +0200 | [diff] [blame] | 1 | #include <signal.h> |
Harald Welte | 4dc14a7 | 2016-10-19 10:53:47 +0200 | [diff] [blame] | 2 | #include <getopt.h> |
Harald Welte | 2c5c079 | 2019-12-04 11:35:14 +0100 | [diff] [blame] | 3 | #include <errno.h> |
Harald Welte | df7a306 | 2016-10-19 10:47:17 +0200 | [diff] [blame] | 4 | |
Harald Welte | 39cfbf4 | 2016-07-28 09:04:11 +0200 | [diff] [blame] | 5 | #include <osmocom/core/signal.h> |
| 6 | #include <osmocom/core/logging.h> |
| 7 | #include <osmocom/core/application.h> |
| 8 | #include <osmocom/vty/vty.h> |
| 9 | #include <osmocom/vty/telnet_interface.h> |
Harald Welte | b7e4023 | 2016-07-29 14:26:50 +0200 | [diff] [blame] | 10 | #include <osmocom/vty/logging.h> |
Harald Welte | 39cfbf4 | 2016-07-28 09:04:11 +0200 | [diff] [blame] | 11 | |
| 12 | #include <osmocom/abis/abis.h> |
| 13 | #include <osmocom/abis/e1_input.h> |
| 14 | |
| 15 | #include "storage.h" |
| 16 | #include "recorder.h" |
| 17 | |
Harald Welte | 06e3198 | 2019-12-03 22:06:17 +0100 | [diff] [blame^] | 18 | #include "config.h" |
| 19 | |
Harald Welte | 39cfbf4 | 2016-07-28 09:04:11 +0200 | [diff] [blame] | 20 | static enum osmo_e1cap_capture_mode ts2cap_mode(struct e1inp_ts *ts) |
| 21 | { |
| 22 | switch (ts->type) { |
| 23 | case E1INP_TS_TYPE_RAW: |
| 24 | return OSMO_E1CAP_MODE_RAW; |
Harald Welte | 1335113 | 2016-10-17 22:13:36 +0200 | [diff] [blame] | 25 | case E1INP_TS_TYPE_HDLC: |
Harald Welte | 39cfbf4 | 2016-07-28 09:04:11 +0200 | [diff] [blame] | 26 | return OSMO_E1CAP_MODE_HDLC; |
| 27 | case E1INP_TS_TYPE_TRAU: |
| 28 | return OSMO_E1CAP_MODE_TRAU; |
| 29 | default: |
| 30 | OSMO_ASSERT(0); |
| 31 | } |
| 32 | } |
| 33 | |
| 34 | /* receive a raw message frome the E1 timeslot */ |
| 35 | void e1ts_raw_recv(struct e1inp_ts *ts, struct msgb *msg) |
| 36 | { |
Harald Welte | 0e91aa1 | 2016-07-28 21:03:40 +0200 | [diff] [blame] | 37 | struct e1_recorder_line *rline = &g_recorder.line[ts->line->num]; |
Harald Welte | 39cfbf4 | 2016-07-28 09:04:11 +0200 | [diff] [blame] | 38 | enum osmo_e1cap_capture_mode cap_mode = ts2cap_mode(ts); |
Harald Welte | 2ac7849 | 2016-10-19 10:47:41 +0200 | [diff] [blame] | 39 | int rc; |
Harald Welte | 39cfbf4 | 2016-07-28 09:04:11 +0200 | [diff] [blame] | 40 | |
| 41 | /* FIXME: special processing of TFP and PGSL */ |
| 42 | |
Harald Welte | 2ac7849 | 2016-10-19 10:47:41 +0200 | [diff] [blame] | 43 | rc = e1frame_store(ts, msg, cap_mode); |
| 44 | if (rc < 0) { |
| 45 | LOGP(DMAIN, LOGL_FATAL, "Error writing E1/T1 frame to disk\n"); |
| 46 | exit(1); |
| 47 | } |
Harald Welte | 39cfbf4 | 2016-07-28 09:04:11 +0200 | [diff] [blame] | 48 | |
Harald Welte | 0e91aa1 | 2016-07-28 21:03:40 +0200 | [diff] [blame] | 49 | if (rline->mirror.enabled) { |
Harald Welte | b7e4023 | 2016-07-29 14:26:50 +0200 | [diff] [blame] | 50 | struct e1inp_line *other_line = |
| 51 | e1inp_line_find(rline->mirror.line_nr); |
| 52 | struct e1inp_ts *other_ts; |
| 53 | other_ts = &other_line->ts[ts->num-1]; |
Harald Welte | 4a92d0b | 2016-10-18 21:36:01 +0200 | [diff] [blame] | 54 | if (!other_ts) { |
| 55 | msgb_free(msg); |
Harald Welte | b7e4023 | 2016-07-29 14:26:50 +0200 | [diff] [blame] | 56 | return; |
Harald Welte | 4a92d0b | 2016-10-18 21:36:01 +0200 | [diff] [blame] | 57 | } |
Harald Welte | 39cfbf4 | 2016-07-28 09:04:11 +0200 | [diff] [blame] | 58 | /* forward data to destination line */ |
Harald Welte | b7e4023 | 2016-07-29 14:26:50 +0200 | [diff] [blame] | 59 | OSMO_ASSERT(other_ts->type == ts->type); |
| 60 | msgb_enqueue(&other_ts->raw.tx_queue, msg); |
Harald Welte | 4a92d0b | 2016-10-18 21:36:01 +0200 | [diff] [blame] | 61 | } else |
| 62 | msgb_free(msg); |
Harald Welte | 39cfbf4 | 2016-07-28 09:04:11 +0200 | [diff] [blame] | 63 | } |
| 64 | |
| 65 | static int inp_sig_cb(unsigned int subsys, unsigned int signal, |
| 66 | void *handler_data, void *signal_data) |
| 67 | { |
| 68 | OSMO_ASSERT(subsys == SS_L_INPUT); |
| 69 | |
| 70 | /* FIXME */ |
| 71 | |
| 72 | return 0; |
| 73 | } |
| 74 | |
Harald Welte | 39cfbf4 | 2016-07-28 09:04:11 +0200 | [diff] [blame] | 75 | static const struct log_info_cat recorder_categories[] = { |
| 76 | [DMAIN] = { |
| 77 | .name = "MAIN", |
Harald Welte | b7e4023 | 2016-07-29 14:26:50 +0200 | [diff] [blame] | 78 | .description = "Osmocom E1 Recorder", |
Harald Welte | 39cfbf4 | 2016-07-28 09:04:11 +0200 | [diff] [blame] | 79 | .enabled = 1, .loglevel = LOGL_DEBUG, |
| 80 | }, |
| 81 | }; |
| 82 | static struct log_info info = { |
| 83 | .cat = recorder_categories, |
| 84 | .num_cat = ARRAY_SIZE(recorder_categories), |
| 85 | }; |
| 86 | |
| 87 | struct vty_app_info vty_info = { |
| 88 | .name = "osmo-e1-recorder", |
Harald Welte | 06e3198 | 2019-12-03 22:06:17 +0100 | [diff] [blame^] | 89 | .version = PACKAGE_VERSION, |
| 90 | .copyright = "(C) 2016-2019 by Harald Welte <laforge@gnumonks.org>\n", |
Harald Welte | 39cfbf4 | 2016-07-28 09:04:11 +0200 | [diff] [blame] | 91 | }; |
| 92 | |
| 93 | static void *rec_tall_ctx; |
| 94 | struct e1_recorder g_recorder; |
Harald Welte | 4dc14a7 | 2016-10-19 10:53:47 +0200 | [diff] [blame] | 95 | static char *g_config_file = "osmo-e1-recorder.cfg"; |
Harald Welte | 39cfbf4 | 2016-07-28 09:04:11 +0200 | [diff] [blame] | 96 | |
Harald Welte | df7a306 | 2016-10-19 10:47:17 +0200 | [diff] [blame] | 97 | static void signal_handler(int signo) |
| 98 | { |
| 99 | switch (signo) { |
| 100 | case SIGHUP: |
| 101 | storage_close(); |
| 102 | break; |
| 103 | case SIGUSR1: |
| 104 | talloc_report(rec_tall_ctx, stderr); |
| 105 | break; |
| 106 | } |
| 107 | } |
| 108 | |
Harald Welte | 06e3198 | 2019-12-03 22:06:17 +0100 | [diff] [blame^] | 109 | static void print_help(void) |
| 110 | { |
| 111 | printf( " -h --help This help\n" |
| 112 | " -V --version Print version of the program\n" |
| 113 | " -c --config FILE Specify configuration file\n" |
| 114 | ); |
| 115 | } |
| 116 | |
Harald Welte | 4dc14a7 | 2016-10-19 10:53:47 +0200 | [diff] [blame] | 117 | static void handle_options(int argc, char **argv) |
| 118 | { |
| 119 | while (1) { |
| 120 | int option_index = 0, c; |
| 121 | static const struct option long_options[] = { |
| 122 | { "config-file", 1, 0, 'c' }, |
Harald Welte | 06e3198 | 2019-12-03 22:06:17 +0100 | [diff] [blame^] | 123 | { "help", 0, 0, 'h' }, |
| 124 | { "version", 0, 0, 'V' }, |
Harald Welte | 4dc14a7 | 2016-10-19 10:53:47 +0200 | [diff] [blame] | 125 | { 0, 0, 0, 0 } |
| 126 | }; |
| 127 | |
Harald Welte | 06e3198 | 2019-12-03 22:06:17 +0100 | [diff] [blame^] | 128 | c = getopt_long(argc, argv, "c:hV", |
Harald Welte | 4dc14a7 | 2016-10-19 10:53:47 +0200 | [diff] [blame] | 129 | long_options, &option_index); |
| 130 | if (c == -1) |
| 131 | break; |
| 132 | |
| 133 | switch (c) { |
| 134 | case 'c': |
| 135 | g_config_file = optarg; |
| 136 | break; |
Harald Welte | 06e3198 | 2019-12-03 22:06:17 +0100 | [diff] [blame^] | 137 | case 'h': |
| 138 | print_help(); |
| 139 | exit(0); |
| 140 | break; |
| 141 | case 'V': |
| 142 | print_version(1); |
| 143 | exit(0); |
| 144 | break; |
Harald Welte | 4dc14a7 | 2016-10-19 10:53:47 +0200 | [diff] [blame] | 145 | } |
| 146 | } |
Harald Welte | cbf23b6 | 2019-12-03 22:14:20 +0100 | [diff] [blame] | 147 | |
| 148 | if (argc > optind) { |
| 149 | fprintf(stderr, "Unsupported positional arguments on command line\n"); |
| 150 | exit(2); |
| 151 | } |
Harald Welte | 4dc14a7 | 2016-10-19 10:53:47 +0200 | [diff] [blame] | 152 | } |
| 153 | |
Harald Welte | 39cfbf4 | 2016-07-28 09:04:11 +0200 | [diff] [blame] | 154 | int main(int argc, char **argv) |
| 155 | { |
| 156 | int rc; |
| 157 | |
| 158 | rec_tall_ctx = talloc_named_const(NULL, 0, "recorder"); |
| 159 | |
| 160 | osmo_init_logging(&info); |
| 161 | vty_init(&vty_info); |
Harald Welte | b7e4023 | 2016-07-29 14:26:50 +0200 | [diff] [blame] | 162 | logging_vty_add_cmds(&info); |
Harald Welte | 39cfbf4 | 2016-07-28 09:04:11 +0200 | [diff] [blame] | 163 | osmo_signal_register_handler(SS_L_INPUT, inp_sig_cb, NULL); |
| 164 | libosmo_abis_init(rec_tall_ctx); |
| 165 | e1inp_vty_init(); |
| 166 | recorder_vty_init(); |
| 167 | |
Harald Welte | df7a306 | 2016-10-19 10:47:17 +0200 | [diff] [blame] | 168 | signal(SIGHUP, &signal_handler); |
| 169 | signal(SIGUSR1, &signal_handler); |
| 170 | |
Harald Welte | 4dc14a7 | 2016-10-19 10:53:47 +0200 | [diff] [blame] | 171 | handle_options(argc, argv); |
| 172 | |
| 173 | rc = vty_read_config_file(g_config_file, NULL); |
Harald Welte | 2c5c079 | 2019-12-04 11:35:14 +0100 | [diff] [blame] | 174 | if (rc < 0) { |
| 175 | fprintf(stderr, "Cannot parse configuration file '%s': %s\n", g_config_file, |
| 176 | strerror(errno)); |
Harald Welte | 39cfbf4 | 2016-07-28 09:04:11 +0200 | [diff] [blame] | 177 | exit(1); |
Harald Welte | 2c5c079 | 2019-12-04 11:35:14 +0100 | [diff] [blame] | 178 | } |
Harald Welte | 39cfbf4 | 2016-07-28 09:04:11 +0200 | [diff] [blame] | 179 | |
| 180 | /* start telne tafte reading config for vty_get_bind_adr() */ |
| 181 | telnet_init_dynif(rec_tall_ctx, NULL, vty_get_bind_addr(), 4444); |
| 182 | |
| 183 | while (1) { |
| 184 | osmo_select_main(0); |
| 185 | }; |
| 186 | } |