blob: 2be3958e5b47d3a65bb7c37d63fa8a546596af33 [file] [log] [blame]
Harald Welteeeb78dd2011-08-02 13:44:54 +02001/* Utility program for ARFCN / frequency calculations */
2/*
3 * (C) 2011 by Harald Welte <laforge@gnumonks.org>
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 <stdio.h>
24#include <getopt.h>
25#include <errno.h>
26#include <unistd.h>
27#include <stdlib.h>
28
29#include <osmocom/gsm/gsm_utils.h>
30
31enum program_mode {
32 MODE_NONE,
33 MODE_A2F,
34 MODE_F2A,
35};
36
Harald Welte6e264ae2012-11-24 13:14:09 +010037static int arfcn2freq(int arfcn)
Harald Welteeeb78dd2011-08-02 13:44:54 +020038{
Harald Welteeeb78dd2011-08-02 13:44:54 +020039 uint16_t freq10u, freq10d;
40
41 if (arfcn < 0 || arfcn > 0xffff) {
42 fprintf(stderr, "Invalid ARFCN %d\n", arfcn);
43 return -EINVAL;
44 }
45
46 freq10u = gsm_arfcn2freq10(arfcn, 1);
47 freq10d = gsm_arfcn2freq10(arfcn, 0);
48 if (freq10u == 0xffff || freq10d == 0xffff) {
49 fprintf(stderr, "Error during conversion of ARFCN %d\n",
50 arfcn);
51 return -EINVAL;
52 }
53
54 printf("ARFCN %4d: Uplink %4u.%1u MHz / Downlink %4u.%1u MHz\n",
Harald Welte6e264ae2012-11-24 13:14:09 +010055 arfcn & ~ARFCN_FLAG_MASK,
56 freq10u/10, freq10u%10, freq10d/10, freq10d%10);
Harald Welteeeb78dd2011-08-02 13:44:54 +020057
58 return 0;
59}
60
Sylvain Munautc44310e2012-12-11 23:45:03 +010061static int freq2arfcn(int freq10, int uplink)
62{
63 uint16_t arfcn;
64
65 if (uplink != 0 && uplink != 1) {
66 fprintf(stderr, "Need to specify uplink or downlink\n");
67 return -EINVAL;
68 }
69
70 arfcn = gsm_freq102arfcn(freq10, uplink);
71
72 if (arfcn == 0xffff) {
73 fprintf(stderr, "Unable to find matching ARFCN\n");
74 return -EINVAL;
75 }
76
77 printf("%s: ARFCN %4d\n",
78 gsm_band_name(gsm_arfcn2band(arfcn)),
79 arfcn & ~ARFCN_FLAG_MASK);
80}
81
Harald Welteeeb78dd2011-08-02 13:44:54 +020082static void help(const char *progname)
83{
Harald Welte6e264ae2012-11-24 13:14:09 +010084 printf("Usage: %s [-h] [-p] [-a arfcn] [-f freq] [-u|-d]\n",
Harald Welteeeb78dd2011-08-02 13:44:54 +020085 progname);
86}
87
88int main(int argc, char **argv)
89{
Sylvain Munautc44310e2012-12-11 23:45:03 +010090 int arfcn, freq, pcs = 0, uplink = -1;
Harald Welteeeb78dd2011-08-02 13:44:54 +020091 int opt;
92 char *param;
93 enum program_mode mode = MODE_NONE;
94
Harald Welte6e264ae2012-11-24 13:14:09 +010095 while ((opt = getopt(argc, argv, "pa:f:ud")) != -1) {
Harald Welteeeb78dd2011-08-02 13:44:54 +020096 switch (opt) {
Harald Welte6e264ae2012-11-24 13:14:09 +010097 case 'p':
98 pcs = 1;
99 break;
Harald Welteeeb78dd2011-08-02 13:44:54 +0200100 case 'a':
101 mode = MODE_A2F;
102 param = optarg;
103 break;
104 case 'f':
105 mode = MODE_F2A;
106 param = optarg;
107 break;
Sylvain Munautc44310e2012-12-11 23:45:03 +0100108 case 'u':
109 uplink = 1;
110 break;
111 case 'd':
112 uplink = 0;
113 break;
Harald Welteeeb78dd2011-08-02 13:44:54 +0200114 case 'h':
115 help(argv[0]);
116 exit(0);
117 break;
118 default:
119 break;
120 }
121 }
122
123 switch (mode) {
124 case MODE_NONE:
125 help(argv[0]);
126 exit(2);
127 break;
128 case MODE_A2F:
Harald Welte6e264ae2012-11-24 13:14:09 +0100129 arfcn = atoi(param);
130 if (pcs)
131 arfcn |= ARFCN_PCS;
132 arfcn2freq(arfcn);
Harald Welteeeb78dd2011-08-02 13:44:54 +0200133 break;
Sylvain Munautc44310e2012-12-11 23:45:03 +0100134 case MODE_F2A:
135 freq = (int)(atof(param) * 10.0f);
136 freq2arfcn(freq, uplink);
137 break;
Harald Welteeeb78dd2011-08-02 13:44:54 +0200138 }
139
140 exit(0);
141}