blob: 0a926e492ad192856664c4937f9204e8e411bfbc [file] [log] [blame]
Harald Welteb7e40232016-07-29 14:26:50 +02001#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 Welte39cfbf42016-07-28 09:04:11 +02008#include <osmocom/core/msgb.h>
Harald Weltedbb0f5a2016-10-18 23:51:54 +02009#include <osmocom/core/talloc.h>
Harald Welte39cfbf42016-07-28 09:04:11 +020010#include <osmocom/abis/e1_input.h>
11
12#include "storage.h"
Harald Welteb7e40232016-07-29 14:26:50 +020013#include "recorder.h"
14
15static int g_out_fd = -1;;
16static uint64_t g_written_bytes;
17
18static const char *storage_gen_filename(void)
19{
20 static char buf[32];
21 time_t t;
22 struct tm *tmp;
23
24 t = time(NULL);
25 tmp = localtime(&t);
26 strftime(buf, sizeof(buf), "%Y%m%d%H%M%S.e1cap", tmp);
27 return buf;
28}
29
Harald Weltedf7a3062016-10-19 10:47:17 +020030void storage_close(void)
31{
32 LOGP(DMAIN, LOGL_INFO, "Closing Log file\n");
33 close(g_out_fd);
34 g_out_fd = -1;
35}
36
Harald Welteb7e40232016-07-29 14:26:50 +020037static int storage_reopen_if_needed(void)
38{
39 if (g_written_bytes / (1024*1024) >= g_recorder.max_file_size_mb) {
Harald Weltedf7a3062016-10-19 10:47:17 +020040 storage_close();
41 /* we re-open below */
Harald Welteb7e40232016-07-29 14:26:50 +020042 }
43
44 if (g_out_fd < 0) {
45 int rc;
46 const char *fname = storage_gen_filename();
Harald Weltedf7a3062016-10-19 10:47:17 +020047 LOGP(DMAIN, LOGL_INFO, "Opening Log file %s\n", fname);
Harald Welteb7e40232016-07-29 14:26:50 +020048 rc = chdir(g_recorder.storage_path);
49 if (rc < 0) {
50 LOGP(DMAIN, LOGL_ERROR, "Unable to chdir(%s): %s\n",
51 g_recorder.storage_path, strerror(errno));
52 return -1;
53 }
54 g_out_fd = open(fname, O_WRONLY|O_CREAT, 0664);
55 if (g_out_fd < 0) {
56 LOGP(DMAIN, LOGL_ERROR, "Unable to open(%s): %s\n",
57 fname, strerror(errno));
58 }
59 g_written_bytes = 0;
60 }
61
62 return g_out_fd;
63}
Harald Welte39cfbf42016-07-28 09:04:11 +020064
65int e1frame_store(struct e1inp_ts *ts, struct msgb *msg, enum osmo_e1cap_capture_mode mode)
66{
Harald Welteb7e40232016-07-29 14:26:50 +020067 struct osmo_e1cap_pkthdr _h, *h = &_h;
68 int rc;
69 struct iovec iov[2] = {
70 {
71 .iov_base = h,
72 .iov_len = sizeof(*h),
73 }, {
74 .iov_base = msg->data,
75 .iov_len = msg->len,
76 }
77 };
Harald Welte39cfbf42016-07-28 09:04:11 +020078
Harald Welteb7e40232016-07-29 14:26:50 +020079 h->len = htonl(msg->len);
80 gettimeofday(&h->ts, NULL);
Harald Welte39cfbf42016-07-28 09:04:11 +020081 h->line_nr = ts->line->num;
82 h->ts_nr = ts->num;
83 h->capture_mode = mode;
84 h->flags = 0;
85
Harald Welte2ac78492016-10-19 10:47:41 +020086 rc = storage_reopen_if_needed();
87 if (rc < 0)
88 return rc;
Harald Welteb7e40232016-07-29 14:26:50 +020089
90 rc = writev(g_out_fd, iov, ARRAY_SIZE(iov));
91 if (rc < 0)
92 return rc;
93
94 g_written_bytes += rc;
95
Harald Welte39cfbf42016-07-28 09:04:11 +020096 return 0;
97}
Harald Weltedbb0f5a2016-10-18 23:51:54 +020098
99/* reading */
100
101
102struct osmo_e1cap_file {
103 int fd;
104};
105
106#define OSMO_E1REC_ALLOC_SIZE 1024
107
108struct osmo_e1cap_file *osmo_e1cap_open(void *ctx, const char *path)
109{
110 struct osmo_e1cap_file *f = talloc_zero(ctx, struct osmo_e1cap_file);
111 if (!f)
112 return NULL;
113
114 f->fd = open(path, O_RDONLY);
115 if (f->fd < 0) {
116 talloc_free(f);
117 return NULL;
118 }
119 return f;
120}
121
122struct osmo_e1cap_pkthdr *osmo_e1cap_read_next(struct osmo_e1cap_file *f)
123{
124 struct osmo_e1cap_pkthdr *pkt = talloc_zero_size(f, OSMO_E1REC_ALLOC_SIZE);
125 int rc;
126
127 if (!pkt)
128 return NULL;
129
130 /* read header */
131 rc = read(f->fd, (uint8_t *)pkt, sizeof(*pkt));
132 if (rc < sizeof(*pkt)) {
133 talloc_free(pkt);
134 return NULL;
135 }
136 pkt->len = ntohl(pkt->len);
Harald Weltedbb0f5a2016-10-18 23:51:54 +0200137 /* read data */
138 if (pkt->len > OSMO_E1REC_ALLOC_SIZE - sizeof(*pkt)) {
139 pkt = talloc_realloc_size(f, pkt, sizeof(*pkt) + pkt->len);
140 if (!pkt)
141 return NULL;
142 }
143 rc = read(f->fd, (uint8_t*)(pkt+1), pkt->len);
144 if (rc < pkt->len) {
145 talloc_free(pkt);
146 return NULL;
147 }
148 return pkt;
149}