Harald Welte | b7e4023 | 2016-07-29 14:26:50 +0200 | [diff] [blame] | 1 | #include <time.h> |
| 2 | #include <fcntl.h> |
| 3 | #include <string.h> |
| 4 | #include <errno.h> |
| 5 | #include <unistd.h> |
| 6 | #include <sys/stat.h> |
| 7 | |
Harald Welte | 39cfbf4 | 2016-07-28 09:04:11 +0200 | [diff] [blame] | 8 | #include <osmocom/core/msgb.h> |
| 9 | #include <osmocom/abis/e1_input.h> |
| 10 | |
| 11 | #include "storage.h" |
Harald Welte | b7e4023 | 2016-07-29 14:26:50 +0200 | [diff] [blame] | 12 | #include "recorder.h" |
| 13 | |
| 14 | static int g_out_fd = -1;; |
| 15 | static uint64_t g_written_bytes; |
| 16 | |
| 17 | static const char *storage_gen_filename(void) |
| 18 | { |
| 19 | static char buf[32]; |
| 20 | time_t t; |
| 21 | struct tm *tmp; |
| 22 | |
| 23 | t = time(NULL); |
| 24 | tmp = localtime(&t); |
| 25 | strftime(buf, sizeof(buf), "%Y%m%d%H%M%S.e1cap", tmp); |
| 26 | return buf; |
| 27 | } |
| 28 | |
| 29 | static int storage_reopen_if_needed(void) |
| 30 | { |
| 31 | if (g_written_bytes / (1024*1024) >= g_recorder.max_file_size_mb) { |
| 32 | close(g_out_fd); |
| 33 | g_out_fd = -1; |
| 34 | } |
| 35 | |
| 36 | if (g_out_fd < 0) { |
| 37 | int rc; |
| 38 | const char *fname = storage_gen_filename(); |
| 39 | rc = chdir(g_recorder.storage_path); |
| 40 | if (rc < 0) { |
| 41 | LOGP(DMAIN, LOGL_ERROR, "Unable to chdir(%s): %s\n", |
| 42 | g_recorder.storage_path, strerror(errno)); |
| 43 | return -1; |
| 44 | } |
| 45 | g_out_fd = open(fname, O_WRONLY|O_CREAT, 0664); |
| 46 | if (g_out_fd < 0) { |
| 47 | LOGP(DMAIN, LOGL_ERROR, "Unable to open(%s): %s\n", |
| 48 | fname, strerror(errno)); |
| 49 | } |
| 50 | g_written_bytes = 0; |
| 51 | } |
| 52 | |
| 53 | return g_out_fd; |
| 54 | } |
Harald Welte | 39cfbf4 | 2016-07-28 09:04:11 +0200 | [diff] [blame] | 55 | |
| 56 | int e1frame_store(struct e1inp_ts *ts, struct msgb *msg, enum osmo_e1cap_capture_mode mode) |
| 57 | { |
Harald Welte | b7e4023 | 2016-07-29 14:26:50 +0200 | [diff] [blame] | 58 | struct osmo_e1cap_pkthdr _h, *h = &_h; |
| 59 | int rc; |
| 60 | struct iovec iov[2] = { |
| 61 | { |
| 62 | .iov_base = h, |
| 63 | .iov_len = sizeof(*h), |
| 64 | }, { |
| 65 | .iov_base = msg->data, |
| 66 | .iov_len = msg->len, |
| 67 | } |
| 68 | }; |
Harald Welte | 39cfbf4 | 2016-07-28 09:04:11 +0200 | [diff] [blame] | 69 | |
Harald Welte | b7e4023 | 2016-07-29 14:26:50 +0200 | [diff] [blame] | 70 | h->len = htonl(msg->len); |
| 71 | gettimeofday(&h->ts, NULL); |
Harald Welte | 39cfbf4 | 2016-07-28 09:04:11 +0200 | [diff] [blame] | 72 | h->line_nr = ts->line->num; |
| 73 | h->ts_nr = ts->num; |
| 74 | h->capture_mode = mode; |
| 75 | h->flags = 0; |
| 76 | |
Harald Welte | b7e4023 | 2016-07-29 14:26:50 +0200 | [diff] [blame] | 77 | storage_reopen_if_needed(); |
| 78 | |
| 79 | rc = writev(g_out_fd, iov, ARRAY_SIZE(iov)); |
| 80 | if (rc < 0) |
| 81 | return rc; |
| 82 | |
| 83 | g_written_bytes += rc; |
| 84 | |
Harald Welte | 39cfbf4 | 2016-07-28 09:04:11 +0200 | [diff] [blame] | 85 | return 0; |
| 86 | } |