blob: 9e4534f43be0ef0a9052fd1d0aa75decf701badd [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 *
Harald Welteeeb78dd2011-08-02 13:44:54 +020018 */
19
20#include <stdio.h>
21#include <getopt.h>
22#include <errno.h>
23#include <unistd.h>
24#include <stdlib.h>
25
26#include <osmocom/gsm/gsm_utils.h>
27
28enum program_mode {
29 MODE_NONE,
30 MODE_A2F,
31 MODE_F2A,
32};
33
Harald Welte6e264ae2012-11-24 13:14:09 +010034static int arfcn2freq(int arfcn)
Harald Welteeeb78dd2011-08-02 13:44:54 +020035{
Harald Welteeeb78dd2011-08-02 13:44:54 +020036 uint16_t freq10u, freq10d;
37
38 if (arfcn < 0 || arfcn > 0xffff) {
39 fprintf(stderr, "Invalid ARFCN %d\n", arfcn);
40 return -EINVAL;
41 }
42
43 freq10u = gsm_arfcn2freq10(arfcn, 1);
44 freq10d = gsm_arfcn2freq10(arfcn, 0);
45 if (freq10u == 0xffff || freq10d == 0xffff) {
46 fprintf(stderr, "Error during conversion of ARFCN %d\n",
47 arfcn);
48 return -EINVAL;
49 }
50
51 printf("ARFCN %4d: Uplink %4u.%1u MHz / Downlink %4u.%1u MHz\n",
Harald Welte6e264ae2012-11-24 13:14:09 +010052 arfcn & ~ARFCN_FLAG_MASK,
53 freq10u/10, freq10u%10, freq10d/10, freq10d%10);
Harald Welteeeb78dd2011-08-02 13:44:54 +020054
55 return 0;
56}
57
Sylvain Munautc44310e2012-12-11 23:45:03 +010058static int freq2arfcn(int freq10, int uplink)
59{
60 uint16_t arfcn;
Pau Espin Pedrolc8772512018-11-16 12:59:46 +010061 enum gsm_band band;
Sylvain Munautc44310e2012-12-11 23:45:03 +010062
63 if (uplink != 0 && uplink != 1) {
64 fprintf(stderr, "Need to specify uplink or downlink\n");
65 return -EINVAL;
66 }
67
68 arfcn = gsm_freq102arfcn(freq10, uplink);
69
70 if (arfcn == 0xffff) {
71 fprintf(stderr, "Unable to find matching ARFCN\n");
72 return -EINVAL;
73 }
74
Pau Espin Pedrolc8772512018-11-16 12:59:46 +010075 if (gsm_arfcn2band_rc(arfcn, &band) < 0) {
76 fprintf(stderr, "ARFCN contains no valid band\n");
77 return -EINVAL;
78 }
79
Sylvain Munautc44310e2012-12-11 23:45:03 +010080 printf("%s: ARFCN %4d\n",
Pau Espin Pedrolc8772512018-11-16 12:59:46 +010081 gsm_band_name(band),
Sylvain Munautc44310e2012-12-11 23:45:03 +010082 arfcn & ~ARFCN_FLAG_MASK);
Holger Hans Peter Freytherd0ce5502013-04-21 21:04:19 +020083 return 0;
Sylvain Munautc44310e2012-12-11 23:45:03 +010084}
85
Harald Welteeeb78dd2011-08-02 13:44:54 +020086static void help(const char *progname)
87{
Harald Welte6e264ae2012-11-24 13:14:09 +010088 printf("Usage: %s [-h] [-p] [-a arfcn] [-f freq] [-u|-d]\n",
Harald Welteeeb78dd2011-08-02 13:44:54 +020089 progname);
90}
91
92int main(int argc, char **argv)
93{
Sylvain Munautc44310e2012-12-11 23:45:03 +010094 int arfcn, freq, pcs = 0, uplink = -1;
Harald Welteeeb78dd2011-08-02 13:44:54 +020095 int opt;
96 char *param;
97 enum program_mode mode = MODE_NONE;
98
Harald Weltef7404bb2019-12-03 21:35:02 +010099 while ((opt = getopt(argc, argv, "pa:f:udh")) != -1) {
Harald Welteeeb78dd2011-08-02 13:44:54 +0200100 switch (opt) {
Harald Welte6e264ae2012-11-24 13:14:09 +0100101 case 'p':
102 pcs = 1;
103 break;
Harald Welteeeb78dd2011-08-02 13:44:54 +0200104 case 'a':
105 mode = MODE_A2F;
106 param = optarg;
107 break;
108 case 'f':
109 mode = MODE_F2A;
110 param = optarg;
111 break;
Sylvain Munautc44310e2012-12-11 23:45:03 +0100112 case 'u':
113 uplink = 1;
114 break;
115 case 'd':
116 uplink = 0;
117 break;
Harald Welteeeb78dd2011-08-02 13:44:54 +0200118 case 'h':
119 help(argv[0]);
120 exit(0);
121 break;
122 default:
123 break;
124 }
125 }
126
Harald Welte278a6c82019-12-03 21:35:12 +0100127 if (argc > optind) {
128 fprintf(stderr, "Unsupported positional arguments in command line\n");
129 exit(2);
130 }
131
Harald Welteeeb78dd2011-08-02 13:44:54 +0200132 switch (mode) {
133 case MODE_NONE:
134 help(argv[0]);
135 exit(2);
136 break;
137 case MODE_A2F:
Harald Welte6e264ae2012-11-24 13:14:09 +0100138 arfcn = atoi(param);
139 if (pcs)
140 arfcn |= ARFCN_PCS;
141 arfcn2freq(arfcn);
Harald Welteeeb78dd2011-08-02 13:44:54 +0200142 break;
Sylvain Munautc44310e2012-12-11 23:45:03 +0100143 case MODE_F2A:
144 freq = (int)(atof(param) * 10.0f);
145 freq2arfcn(freq, uplink);
146 break;
Harald Welteeeb78dd2011-08-02 13:44:54 +0200147 }
148
149 exit(0);
150}