blob: 61108f8d73b39cf394fda8c09acf64b3bf641fd8 [file] [log] [blame]
Neels Hofmeyr17518fe2017-06-20 04:35:06 +02001/*! \file osmo-arfcn.c
2 * Utility program for ARFCN / frequency calculations. */
Harald Welteeeb78dd2011-08-02 13:44:54 +02003/*
4 * (C) 2011 by Harald Welte <laforge@gnumonks.org>
5 *
6 * All Rights Reserved
7 *
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
12 *
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License along
19 * with this program; if not, write to the Free Software Foundation, Inc.,
20 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
21 *
22 */
23
24#include <stdio.h>
25#include <getopt.h>
26#include <errno.h>
27#include <unistd.h>
28#include <stdlib.h>
29
30#include <osmocom/gsm/gsm_utils.h>
31
32enum program_mode {
33 MODE_NONE,
34 MODE_A2F,
35 MODE_F2A,
36};
37
Harald Welte6e264ae2012-11-24 13:14:09 +010038static int arfcn2freq(int arfcn)
Harald Welteeeb78dd2011-08-02 13:44:54 +020039{
Harald Welteeeb78dd2011-08-02 13:44:54 +020040 uint16_t freq10u, freq10d;
41
42 if (arfcn < 0 || arfcn > 0xffff) {
43 fprintf(stderr, "Invalid ARFCN %d\n", arfcn);
44 return -EINVAL;
45 }
46
47 freq10u = gsm_arfcn2freq10(arfcn, 1);
48 freq10d = gsm_arfcn2freq10(arfcn, 0);
49 if (freq10u == 0xffff || freq10d == 0xffff) {
50 fprintf(stderr, "Error during conversion of ARFCN %d\n",
51 arfcn);
52 return -EINVAL;
53 }
54
55 printf("ARFCN %4d: Uplink %4u.%1u MHz / Downlink %4u.%1u MHz\n",
Harald Welte6e264ae2012-11-24 13:14:09 +010056 arfcn & ~ARFCN_FLAG_MASK,
57 freq10u/10, freq10u%10, freq10d/10, freq10d%10);
Harald Welteeeb78dd2011-08-02 13:44:54 +020058
59 return 0;
60}
61
Sylvain Munautc44310e2012-12-11 23:45:03 +010062static int freq2arfcn(int freq10, int uplink)
63{
64 uint16_t arfcn;
65
66 if (uplink != 0 && uplink != 1) {
67 fprintf(stderr, "Need to specify uplink or downlink\n");
68 return -EINVAL;
69 }
70
71 arfcn = gsm_freq102arfcn(freq10, uplink);
72
73 if (arfcn == 0xffff) {
74 fprintf(stderr, "Unable to find matching ARFCN\n");
75 return -EINVAL;
76 }
77
78 printf("%s: ARFCN %4d\n",
79 gsm_band_name(gsm_arfcn2band(arfcn)),
80 arfcn & ~ARFCN_FLAG_MASK);
Holger Hans Peter Freytherd0ce5502013-04-21 21:04:19 +020081 return 0;
Sylvain Munautc44310e2012-12-11 23:45:03 +010082}
83
Harald Welteeeb78dd2011-08-02 13:44:54 +020084static void help(const char *progname)
85{
Harald Welte6e264ae2012-11-24 13:14:09 +010086 printf("Usage: %s [-h] [-p] [-a arfcn] [-f freq] [-u|-d]\n",
Harald Welteeeb78dd2011-08-02 13:44:54 +020087 progname);
88}
89
90int main(int argc, char **argv)
91{
Sylvain Munautc44310e2012-12-11 23:45:03 +010092 int arfcn, freq, pcs = 0, uplink = -1;
Harald Welteeeb78dd2011-08-02 13:44:54 +020093 int opt;
94 char *param;
95 enum program_mode mode = MODE_NONE;
96
Harald Welte6e264ae2012-11-24 13:14:09 +010097 while ((opt = getopt(argc, argv, "pa:f:ud")) != -1) {
Harald Welteeeb78dd2011-08-02 13:44:54 +020098 switch (opt) {
Harald Welte6e264ae2012-11-24 13:14:09 +010099 case 'p':
100 pcs = 1;
101 break;
Harald Welteeeb78dd2011-08-02 13:44:54 +0200102 case 'a':
103 mode = MODE_A2F;
104 param = optarg;
105 break;
106 case 'f':
107 mode = MODE_F2A;
108 param = optarg;
109 break;
Sylvain Munautc44310e2012-12-11 23:45:03 +0100110 case 'u':
111 uplink = 1;
112 break;
113 case 'd':
114 uplink = 0;
115 break;
Harald Welteeeb78dd2011-08-02 13:44:54 +0200116 case 'h':
117 help(argv[0]);
118 exit(0);
119 break;
120 default:
121 break;
122 }
123 }
124
125 switch (mode) {
126 case MODE_NONE:
127 help(argv[0]);
128 exit(2);
129 break;
130 case MODE_A2F:
Harald Welte6e264ae2012-11-24 13:14:09 +0100131 arfcn = atoi(param);
132 if (pcs)
133 arfcn |= ARFCN_PCS;
134 arfcn2freq(arfcn);
Harald Welteeeb78dd2011-08-02 13:44:54 +0200135 break;
Sylvain Munautc44310e2012-12-11 23:45:03 +0100136 case MODE_F2A:
137 freq = (int)(atof(param) * 10.0f);
138 freq2arfcn(freq, uplink);
139 break;
Harald Welteeeb78dd2011-08-02 13:44:54 +0200140 }
141
142 exit(0);
143}