0d612265c87450101936ed0279014c96f49e42d4
[osmocom-bb.git] / src / host / layer23 / src / misc / app_cell_log.c
1 /* "Application" code of the layer2/3 stack */
2
3 /* (C) 2010 by Andreas Eversberg <jolly@eversberg.eu>
4  *
5  * All Rights Reserved
6  *
7  * This program is free software; you can redistribute it and/or modify
8  * it under the terms of the GNU General Public License as published by
9  * the Free Software Foundation; either version 2 of the License, or
10  * (at your option) any later version.
11  *
12  * This program is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15  * GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License along
18  * with this program; if not, write to the Free Software Foundation, Inc.,
19  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
20  *
21  */
22
23 #include <signal.h>
24 #include <stdlib.h>
25 #include <time.h>
26 #include <getopt.h>
27
28 #include <osmocom/bb/common/osmocom_data.h>
29 #include <osmocom/bb/common/l1ctl.h>
30 #include <osmocom/bb/common/l23_app.h>
31 #include <osmocom/bb/common/logging.h>
32 #include <osmocom/bb/common/gps.h>
33 #include <osmocom/bb/misc/cell_log.h>
34
35 #include <osmocore/talloc.h>
36 #include <osmocore/utils.h>
37
38 extern struct log_target *stderr_target;
39 extern void *l23_ctx;
40
41 char *logname = "/var/log/osmocom.log";
42 int RACH_MAX = 2;
43
44 int _scan_work(struct osmocom_ms *ms)
45 {
46         return 0;
47 }
48
49 int _scan_exit(struct osmocom_ms *ms)
50 {
51         /* in case there is a lockup during exit */
52         signal(SIGINT, SIG_DFL);
53         signal(SIGHUP, SIG_DFL);
54         signal(SIGTERM, SIG_DFL);
55         signal(SIGPIPE, SIG_DFL);
56
57         scan_exit();
58
59         return 0;
60 }
61
62 int l23_app_init(struct osmocom_ms *ms)
63 {
64         int rc;
65
66         srand(time(NULL));
67
68 //      log_parse_category_mask(stderr_target, "DL1C:DRSL:DRR:DGPS:DSUM");
69         log_parse_category_mask(stderr_target, "DSUM");
70         log_set_log_level(stderr_target, LOGL_INFO);
71
72         l23_app_work = _scan_work;
73         l23_app_exit = _scan_exit;
74
75         rc = scan_init(ms);
76         if (rc)
77                 return rc;
78
79         l1ctl_tx_reset_req(ms, L1CTL_RES_T_FULL);
80         printf("Mobile initialized, please start phone now!\n");
81         return 0;
82 }
83
84 static int l23_cfg_supported()
85 {
86         return L23_OPT_TAP | L23_OPT_DBG;
87 }
88
89 static int l23_getopt_options(struct option **options)
90 {
91         static struct option opts [] = {
92                 {"logfile", 1, 0, 'l'},
93                 {"rach", 1, 0, 'r'},
94                 {"no-rach", 1, 0, 'n'},
95                 {"gps", 1, 0, 'g'},
96                 {"baud", 1, 0, 'b'}
97         };
98
99         *options = opts;
100         return ARRAY_SIZE(opts);
101 }
102
103 static int l23_cfg_print_help()
104 {
105         printf("\nApplication specific\n");
106         printf("  -l --logfile LOGFILE          Logfile for the cell log.\n");
107         printf("  -r --rach    RACH             Nr. of RACH bursts to send.\n");
108         printf("  -n --no-rach                  Send no rach bursts.\n");
109         printf("  -g --gps DEVICE               /dev/ttyACM0. GPS device.\n");
110         printf("  -b --baud BAUDRAT             The baud rate of the GPS device\n");
111         return 0;
112 }
113
114 static int l23_cfg_handle(int c, const char *optarg)
115 {
116         switch (c) {
117         case 'l':
118                 logname = talloc_strdup(l23_ctx, optarg);
119                 break;
120         case 'r':
121                 RACH_MAX = atoi(optarg);
122                 break;
123         case 'n':
124                 RACH_MAX = 0;
125                 break;
126         case 'g':
127                 snprintf(gps.device, ARRAY_SIZE(gps.device), "%s", optarg);
128                 /* force string terminator */
129                 gps.device[ARRAY_SIZE(gps.device) - 1] = '\0';
130                 LOGP(DGPS, LOGL_INFO, "Using GPS device %s\n", gps.device);
131                 break;
132         case 'b':
133                 gps.baud = atoi(optarg);
134                 LOGP(DGPS, LOGL_INFO, "Setting GPS baudrate to %u\n", gps.baud);
135                 break;
136         }
137
138         return 0;
139 }
140
141 static struct l23_app_info info = {
142         .copyright      = "Copyright (C) 2010 Andreas Eversberg\n",
143         .getopt_string  = "l:r:ng:b:",
144         .cfg_supported  = l23_cfg_supported,
145         .cfg_getopt_opt = l23_getopt_options,
146         .cfg_handle_opt = l23_cfg_handle,
147         .cfg_print_help = l23_cfg_print_help,
148 };
149
150 struct l23_app_info *l23_app_info()
151 {
152         return &info;
153 }