Added gpsd support.
[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 #ifdef _USE_GPSD
96                 {"gpsd-host", 1, 0, 'g'},
97                 {"gpsd-port", 1, 0, 'p'}
98 #else
99                 {"gps", 1, 0, 'g'},
100                 {"baud", 1, 0, 'b'}
101 #endif
102         };
103
104         *options = opts;
105         return ARRAY_SIZE(opts);
106 }
107
108 static int l23_cfg_print_help()
109 {
110         printf("\nApplication specific\n");
111         printf("  -l --logfile LOGFILE  Logfile for the cell log.\n");
112         printf("  -r --rach    RACH             Nr. of RACH bursts to send.\n");
113         printf("  -n --no-rach                  Send no rach bursts.\n");
114 #ifdef _USE_GPSD
115         printf("  -g --gpsd-host HOST   127.0.0.1. gpsd host.\n");
116         printf("  -p --port PORT                2947. gpsd port\n");
117 #else
118         printf("  -g --gps DEVICE               /dev/ttyACM0. GPS device.\n");
119         printf("  -b --baud BAUDRAT             The baud rate of the GPS device\n");
120 #endif
121         return 0;
122 }
123
124 static int l23_cfg_handle(int c, const char *optarg)
125 {
126         switch (c) {
127         case 'l':
128                 logname = talloc_strdup(l23_ctx, optarg);
129                 break;
130         case 'r':
131                 RACH_MAX = atoi(optarg);
132                 break;
133         case 'n':
134                 RACH_MAX = 0;
135                 break;
136 #ifdef _USE_GPSD
137         case 'g':
138                 snprintf(gps.gpsd_host, ARRAY_SIZE(gps.gpsd_host), "%s", optarg);
139                 /* force string terminator */
140                 gps.gpsd_host[ARRAY_SIZE(gps.gpsd_host) - 1] = '\0';
141                 LOGP(DGPS, LOGL_INFO, "Using gpsd host %s\n", gps.gpsd_host);
142                 break;
143         case 'p':
144                 snprintf(gps.gpsd_port, ARRAY_SIZE(gps.gpsd_port), "%s", optarg);
145                 /* force string terminator */
146                 gps.gpsd_port[ARRAY_SIZE(gps.gpsd_port) - 1] = '\0';
147                 LOGP(DGPS, LOGL_INFO, "Using gpsd port %s\n", gps.gpsd_port);
148                 break;
149 #else
150         case 'g':
151                 snprintf(gps.device, ARRAY_SIZE(gps.device), "%s", optarg);
152                 /* force string terminator */
153                 gps.device[ARRAY_SIZE(gps.device) - 1] = '\0';
154                 LOGP(DGPS, LOGL_INFO, "Using GPS device %s\n", gps.device);
155                 break;
156         case 'b':
157                 gps.baud = atoi(optarg);
158                 LOGP(DGPS, LOGL_INFO, "Setting GPS baudrate to %u\n", gps.baud);
159                 break;
160 #endif
161         }
162         return 0;
163 }
164
165 static struct l23_app_info info = {
166         .copyright      = "Copyright (C) 2010 Andreas Eversberg\n",
167 #ifdef _USE_GPSD
168         .getopt_string  = "l:r:ng:p:",
169 #else
170         .getopt_string  = "l:r:ng:b:",
171 #endif
172         .cfg_supported  = l23_cfg_supported,
173         .cfg_getopt_opt = l23_getopt_options,
174         .cfg_handle_opt = l23_cfg_handle,
175         .cfg_print_help = l23_cfg_print_help,
176 };
177
178 struct l23_app_info *l23_app_info()
179 {
180         return &info;
181 }