blob: a0845359b9be50a3e878ffcba2a91076f72c0aeb [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>
Harald Welte6f45dcd2019-11-06 06:55:47 +01007#include <sys/uio.h>
Harald Welteb7e40232016-07-29 14:26:50 +02008
Harald Welte39cfbf42016-07-28 09:04:11 +02009#include <osmocom/core/msgb.h>
Harald Weltedbb0f5a2016-10-18 23:51:54 +020010#include <osmocom/core/talloc.h>
Harald Welte39cfbf42016-07-28 09:04:11 +020011#include <osmocom/abis/e1_input.h>
12
13#include "storage.h"
Harald Welteb7e40232016-07-29 14:26:50 +020014#include "recorder.h"
15
16static int g_out_fd = -1;;
17static uint64_t g_written_bytes;
18
19static const char *storage_gen_filename(void)
20{
21 static char buf[32];
22 time_t t;
23 struct tm *tmp;
24
25 t = time(NULL);
26 tmp = localtime(&t);
27 strftime(buf, sizeof(buf), "%Y%m%d%H%M%S.e1cap", tmp);
28 return buf;
29}
30
Harald Weltedf7a3062016-10-19 10:47:17 +020031void storage_close(void)
32{
33 LOGP(DMAIN, LOGL_INFO, "Closing Log file\n");
34 close(g_out_fd);
35 g_out_fd = -1;
36}
37
Harald Welteb7e40232016-07-29 14:26:50 +020038static int storage_reopen_if_needed(void)
39{
40 if (g_written_bytes / (1024*1024) >= g_recorder.max_file_size_mb) {
Harald Weltedf7a3062016-10-19 10:47:17 +020041 storage_close();
42 /* we re-open below */
Harald Welteb7e40232016-07-29 14:26:50 +020043 }
44
45 if (g_out_fd < 0) {
46 int rc;
47 const char *fname = storage_gen_filename();
Harald Weltedf7a3062016-10-19 10:47:17 +020048 LOGP(DMAIN, LOGL_INFO, "Opening Log file %s\n", fname);
Harald Welteb7e40232016-07-29 14:26:50 +020049 rc = chdir(g_recorder.storage_path);
50 if (rc < 0) {
51 LOGP(DMAIN, LOGL_ERROR, "Unable to chdir(%s): %s\n",
52 g_recorder.storage_path, strerror(errno));
53 return -1;
54 }
55 g_out_fd = open(fname, O_WRONLY|O_CREAT, 0664);
56 if (g_out_fd < 0) {
57 LOGP(DMAIN, LOGL_ERROR, "Unable to open(%s): %s\n",
58 fname, strerror(errno));
59 }
60 g_written_bytes = 0;
61 }
62
63 return g_out_fd;
64}
Harald Welte39cfbf42016-07-28 09:04:11 +020065
66int e1frame_store(struct e1inp_ts *ts, struct msgb *msg, enum osmo_e1cap_capture_mode mode)
67{
Harald Welteb7e40232016-07-29 14:26:50 +020068 struct osmo_e1cap_pkthdr _h, *h = &_h;
Harald Welte6df07292019-11-06 16:21:55 +010069 struct timeval tv;
Harald Welteb7e40232016-07-29 14:26:50 +020070 int rc;
71 struct iovec iov[2] = {
72 {
73 .iov_base = h,
74 .iov_len = sizeof(*h),
75 }, {
76 .iov_base = msg->data,
77 .iov_len = msg->len,
78 }
79 };
Harald Welte39cfbf42016-07-28 09:04:11 +020080
Harald Welteb7e40232016-07-29 14:26:50 +020081 h->len = htonl(msg->len);
Harald Welte6df07292019-11-06 16:21:55 +010082 gettimeofday(&tv, NULL);
83 h->ts.tv_sec = tv.tv_sec;
84 h->ts.tv_usec = tv.tv_usec;
Harald Welte39cfbf42016-07-28 09:04:11 +020085 h->line_nr = ts->line->num;
86 h->ts_nr = ts->num;
87 h->capture_mode = mode;
88 h->flags = 0;
89
Harald Welte2ac78492016-10-19 10:47:41 +020090 rc = storage_reopen_if_needed();
91 if (rc < 0)
92 return rc;
Harald Welteb7e40232016-07-29 14:26:50 +020093
94 rc = writev(g_out_fd, iov, ARRAY_SIZE(iov));
95 if (rc < 0)
96 return rc;
97
98 g_written_bytes += rc;
99
Harald Welte39cfbf42016-07-28 09:04:11 +0200100 return 0;
101}
Harald Weltedbb0f5a2016-10-18 23:51:54 +0200102
103/* reading */
104
105
106struct osmo_e1cap_file {
107 int fd;
108};
109
110#define OSMO_E1REC_ALLOC_SIZE 1024
111
112struct osmo_e1cap_file *osmo_e1cap_open(void *ctx, const char *path)
113{
114 struct osmo_e1cap_file *f = talloc_zero(ctx, struct osmo_e1cap_file);
115 if (!f)
116 return NULL;
117
118 f->fd = open(path, O_RDONLY);
119 if (f->fd < 0) {
120 talloc_free(f);
121 return NULL;
122 }
123 return f;
124}
125
126struct osmo_e1cap_pkthdr *osmo_e1cap_read_next(struct osmo_e1cap_file *f)
127{
128 struct osmo_e1cap_pkthdr *pkt = talloc_zero_size(f, OSMO_E1REC_ALLOC_SIZE);
129 int rc;
130
131 if (!pkt)
132 return NULL;
133
134 /* read header */
135 rc = read(f->fd, (uint8_t *)pkt, sizeof(*pkt));
136 if (rc < sizeof(*pkt)) {
137 talloc_free(pkt);
138 return NULL;
139 }
140 pkt->len = ntohl(pkt->len);
Harald Weltedbb0f5a2016-10-18 23:51:54 +0200141 /* read data */
142 if (pkt->len > OSMO_E1REC_ALLOC_SIZE - sizeof(*pkt)) {
143 pkt = talloc_realloc_size(f, pkt, sizeof(*pkt) + pkt->len);
144 if (!pkt)
145 return NULL;
146 }
147 rc = read(f->fd, (uint8_t*)(pkt+1), pkt->len);
148 if (rc < pkt->len) {
149 talloc_free(pkt);
150 return NULL;
151 }
152 return pkt;
153}