blob: 5f138f8cd1e5ea943cd811667d1bfe355fea0ead [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;
Pau Espin Pedrolc8772512018-11-16 12:59:46 +010065 enum gsm_band band;
Sylvain Munautc44310e2012-12-11 23:45:03 +010066
67 if (uplink != 0 && uplink != 1) {
68 fprintf(stderr, "Need to specify uplink or downlink\n");
69 return -EINVAL;
70 }
71
72 arfcn = gsm_freq102arfcn(freq10, uplink);
73
74 if (arfcn == 0xffff) {
75 fprintf(stderr, "Unable to find matching ARFCN\n");
76 return -EINVAL;
77 }
78
Pau Espin Pedrolc8772512018-11-16 12:59:46 +010079 if (gsm_arfcn2band_rc(arfcn, &band) < 0) {
80 fprintf(stderr, "ARFCN contains no valid band\n");
81 return -EINVAL;
82 }
83
Sylvain Munautc44310e2012-12-11 23:45:03 +010084 printf("%s: ARFCN %4d\n",
Pau Espin Pedrolc8772512018-11-16 12:59:46 +010085 gsm_band_name(band),
Sylvain Munautc44310e2012-12-11 23:45:03 +010086 arfcn & ~ARFCN_FLAG_MASK);
Holger Hans Peter Freytherd0ce5502013-04-21 21:04:19 +020087 return 0;
Sylvain Munautc44310e2012-12-11 23:45:03 +010088}
89
Harald Welteeeb78dd2011-08-02 13:44:54 +020090static void help(const char *progname)
91{
Harald Welte6e264ae2012-11-24 13:14:09 +010092 printf("Usage: %s [-h] [-p] [-a arfcn] [-f freq] [-u|-d]\n",
Harald Welteeeb78dd2011-08-02 13:44:54 +020093 progname);
94}
95
96int main(int argc, char **argv)
97{
Sylvain Munautc44310e2012-12-11 23:45:03 +010098 int arfcn, freq, pcs = 0, uplink = -1;
Harald Welteeeb78dd2011-08-02 13:44:54 +020099 int opt;
100 char *param;
101 enum program_mode mode = MODE_NONE;
102
Harald Weltef7404bb2019-12-03 21:35:02 +0100103 while ((opt = getopt(argc, argv, "pa:f:udh")) != -1) {
Harald Welteeeb78dd2011-08-02 13:44:54 +0200104 switch (opt) {
Harald Welte6e264ae2012-11-24 13:14:09 +0100105 case 'p':
106 pcs = 1;
107 break;
Harald Welteeeb78dd2011-08-02 13:44:54 +0200108 case 'a':
109 mode = MODE_A2F;
110 param = optarg;
111 break;
112 case 'f':
113 mode = MODE_F2A;
114 param = optarg;
115 break;
Sylvain Munautc44310e2012-12-11 23:45:03 +0100116 case 'u':
117 uplink = 1;
118 break;
119 case 'd':
120 uplink = 0;
121 break;
Harald Welteeeb78dd2011-08-02 13:44:54 +0200122 case 'h':
123 help(argv[0]);
124 exit(0);
125 break;
126 default:
127 break;
128 }
129 }
130
Harald Welte278a6c82019-12-03 21:35:12 +0100131 if (argc > optind) {
132 fprintf(stderr, "Unsupported positional arguments in command line\n");
133 exit(2);
134 }
135
Harald Welteeeb78dd2011-08-02 13:44:54 +0200136 switch (mode) {
137 case MODE_NONE:
138 help(argv[0]);
139 exit(2);
140 break;
141 case MODE_A2F:
Harald Welte6e264ae2012-11-24 13:14:09 +0100142 arfcn = atoi(param);
143 if (pcs)
144 arfcn |= ARFCN_PCS;
145 arfcn2freq(arfcn);
Harald Welteeeb78dd2011-08-02 13:44:54 +0200146 break;
Sylvain Munautc44310e2012-12-11 23:45:03 +0100147 case MODE_F2A:
148 freq = (int)(atof(param) * 10.0f);
149 freq2arfcn(freq, uplink);
150 break;
Harald Welteeeb78dd2011-08-02 13:44:54 +0200151 }
152
153 exit(0);
154}