blob: 2b9fb9f01321397ff5e13f473ce46b76cbe72669 [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>
9#include <osmocom/abis/e1_input.h>
10
11#include "storage.h"
Harald Welteb7e40232016-07-29 14:26:50 +020012#include "recorder.h"
13
14static int g_out_fd = -1;;
15static uint64_t g_written_bytes;
16
17static 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
29static 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 Welte39cfbf42016-07-28 09:04:11 +020055
56int e1frame_store(struct e1inp_ts *ts, struct msgb *msg, enum osmo_e1cap_capture_mode mode)
57{
Harald Welteb7e40232016-07-29 14:26:50 +020058 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 Welte39cfbf42016-07-28 09:04:11 +020069
Harald Welteb7e40232016-07-29 14:26:50 +020070 h->len = htonl(msg->len);
71 gettimeofday(&h->ts, NULL);
Harald Welte39cfbf42016-07-28 09:04:11 +020072 h->line_nr = ts->line->num;
73 h->ts_nr = ts->num;
74 h->capture_mode = mode;
75 h->flags = 0;
76
Harald Welteb7e40232016-07-29 14:26:50 +020077 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 Welte39cfbf42016-07-28 09:04:11 +020085 return 0;
86}