blob: 5103c97bd280bc476c37644b3b5278a0c340177a [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);
Holger Hans Peter Freytherd0ce5502013-04-21 21:04:19 +020080 return 0;
Sylvain Munautc44310e2012-12-11 23:45:03 +010081}
82
Harald Welteeeb78dd2011-08-02 13:44:54 +020083static void help(const char *progname)
84{
Harald Welte6e264ae2012-11-24 13:14:09 +010085 printf("Usage: %s [-h] [-p] [-a arfcn] [-f freq] [-u|-d]\n",
Harald Welteeeb78dd2011-08-02 13:44:54 +020086 progname);
87}
88
89int main(int argc, char **argv)
90{
Sylvain Munautc44310e2012-12-11 23:45:03 +010091 int arfcn, freq, pcs = 0, uplink = -1;
Harald Welteeeb78dd2011-08-02 13:44:54 +020092 int opt;
93 char *param;
94 enum program_mode mode = MODE_NONE;
95
Harald Welte6e264ae2012-11-24 13:14:09 +010096 while ((opt = getopt(argc, argv, "pa:f:ud")) != -1) {
Harald Welteeeb78dd2011-08-02 13:44:54 +020097 switch (opt) {
Harald Welte6e264ae2012-11-24 13:14:09 +010098 case 'p':
99 pcs = 1;
100 break;
Harald Welteeeb78dd2011-08-02 13:44:54 +0200101 case 'a':
102 mode = MODE_A2F;
103 param = optarg;
104 break;
105 case 'f':
106 mode = MODE_F2A;
107 param = optarg;
108 break;
Sylvain Munautc44310e2012-12-11 23:45:03 +0100109 case 'u':
110 uplink = 1;
111 break;
112 case 'd':
113 uplink = 0;
114 break;
Harald Welteeeb78dd2011-08-02 13:44:54 +0200115 case 'h':
116 help(argv[0]);
117 exit(0);
118 break;
119 default:
120 break;
121 }
122 }
123
124 switch (mode) {
125 case MODE_NONE:
126 help(argv[0]);
127 exit(2);
128 break;
129 case MODE_A2F:
Harald Welte6e264ae2012-11-24 13:14:09 +0100130 arfcn = atoi(param);
131 if (pcs)
132 arfcn |= ARFCN_PCS;
133 arfcn2freq(arfcn);
Harald Welteeeb78dd2011-08-02 13:44:54 +0200134 break;
Sylvain Munautc44310e2012-12-11 23:45:03 +0100135 case MODE_F2A:
136 freq = (int)(atof(param) * 10.0f);
137 freq2arfcn(freq, uplink);
138 break;
Harald Welteeeb78dd2011-08-02 13:44:54 +0200139 }
140
141 exit(0);
142}