blob: d18f8cdbb9c207024596d4f009a45293e8ab107b [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 Welteb7e40232016-07-29 14:26:50 +020086 storage_reopen_if_needed();
87
88 rc = writev(g_out_fd, iov, ARRAY_SIZE(iov));
89 if (rc < 0)
90 return rc;
91
92 g_written_bytes += rc;
93
Harald Welte39cfbf42016-07-28 09:04:11 +020094 return 0;
95}
Harald Weltedbb0f5a2016-10-18 23:51:54 +020096
97/* reading */
98
99
100struct osmo_e1cap_file {
101 int fd;
102};
103
104#define OSMO_E1REC_ALLOC_SIZE 1024
105
106struct osmo_e1cap_file *osmo_e1cap_open(void *ctx, const char *path)
107{
108 struct osmo_e1cap_file *f = talloc_zero(ctx, struct osmo_e1cap_file);
109 if (!f)
110 return NULL;
111
112 f->fd = open(path, O_RDONLY);
113 if (f->fd < 0) {
114 talloc_free(f);
115 return NULL;
116 }
117 return f;
118}
119
120struct osmo_e1cap_pkthdr *osmo_e1cap_read_next(struct osmo_e1cap_file *f)
121{
122 struct osmo_e1cap_pkthdr *pkt = talloc_zero_size(f, OSMO_E1REC_ALLOC_SIZE);
123 int rc;
124
125 if (!pkt)
126 return NULL;
127
128 /* read header */
129 rc = read(f->fd, (uint8_t *)pkt, sizeof(*pkt));
130 if (rc < sizeof(*pkt)) {
131 talloc_free(pkt);
132 return NULL;
133 }
134 pkt->len = ntohl(pkt->len);
Harald Weltedbb0f5a2016-10-18 23:51:54 +0200135 /* read data */
136 if (pkt->len > OSMO_E1REC_ALLOC_SIZE - sizeof(*pkt)) {
137 pkt = talloc_realloc_size(f, pkt, sizeof(*pkt) + pkt->len);
138 if (!pkt)
139 return NULL;
140 }
141 rc = read(f->fd, (uint8_t*)(pkt+1), pkt->len);
142 if (rc < pkt->len) {
143 talloc_free(pkt);
144 return NULL;
145 }
146 return pkt;
147}