blob: ddbadd073173e03e8c57fa427a9b412433f15700 [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 Welte2359de62019-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 Welte4edbb352019-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 Welte4edbb352019-11-06 16:21:55 +010082 tv = h->ts;
83 gettimeofday(&tv, NULL);
Harald Welte39cfbf42016-07-28 09:04:11 +020084 h->line_nr = ts->line->num;
85 h->ts_nr = ts->num;
86 h->capture_mode = mode;
87 h->flags = 0;
88
Harald Welte2ac78492016-10-19 10:47:41 +020089 rc = storage_reopen_if_needed();
90 if (rc < 0)
91 return rc;
Harald Welteb7e40232016-07-29 14:26:50 +020092
93 rc = writev(g_out_fd, iov, ARRAY_SIZE(iov));
94 if (rc < 0)
95 return rc;
96
97 g_written_bytes += rc;
98
Harald Welte39cfbf42016-07-28 09:04:11 +020099 return 0;
100}
Harald Weltedbb0f5a2016-10-18 23:51:54 +0200101
102/* reading */
103
104
105struct osmo_e1cap_file {
106 int fd;
107};
108
109#define OSMO_E1REC_ALLOC_SIZE 1024
110
111struct osmo_e1cap_file *osmo_e1cap_open(void *ctx, const char *path)
112{
113 struct osmo_e1cap_file *f = talloc_zero(ctx, struct osmo_e1cap_file);
114 if (!f)
115 return NULL;
116
117 f->fd = open(path, O_RDONLY);
118 if (f->fd < 0) {
119 talloc_free(f);
120 return NULL;
121 }
122 return f;
123}
124
125struct osmo_e1cap_pkthdr *osmo_e1cap_read_next(struct osmo_e1cap_file *f)
126{
127 struct osmo_e1cap_pkthdr *pkt = talloc_zero_size(f, OSMO_E1REC_ALLOC_SIZE);
128 int rc;
129
130 if (!pkt)
131 return NULL;
132
133 /* read header */
134 rc = read(f->fd, (uint8_t *)pkt, sizeof(*pkt));
135 if (rc < sizeof(*pkt)) {
136 talloc_free(pkt);
137 return NULL;
138 }
139 pkt->len = ntohl(pkt->len);
Harald Weltedbb0f5a2016-10-18 23:51:54 +0200140 /* read data */
141 if (pkt->len > OSMO_E1REC_ALLOC_SIZE - sizeof(*pkt)) {
142 pkt = talloc_realloc_size(f, pkt, sizeof(*pkt) + pkt->len);
143 if (!pkt)
144 return NULL;
145 }
146 rc = read(f->fd, (uint8_t*)(pkt+1), pkt->len);
147 if (rc < pkt->len) {
148 talloc_free(pkt);
149 return NULL;
150 }
151 return pkt;
152}