blob: b88db5afb8129269d26598876c2c5b19ea675f0f [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
61static void help(const char *progname)
62{
Harald Welte6e264ae2012-11-24 13:14:09 +010063 printf("Usage: %s [-h] [-p] [-a arfcn] [-f freq] [-u|-d]\n",
Harald Welteeeb78dd2011-08-02 13:44:54 +020064 progname);
65}
66
67int main(int argc, char **argv)
68{
Harald Welte6e264ae2012-11-24 13:14:09 +010069 int arfcn, pcs = 0;
Harald Welteeeb78dd2011-08-02 13:44:54 +020070 int opt;
71 char *param;
72 enum program_mode mode = MODE_NONE;
73
Harald Welte6e264ae2012-11-24 13:14:09 +010074 while ((opt = getopt(argc, argv, "pa:f:ud")) != -1) {
Harald Welteeeb78dd2011-08-02 13:44:54 +020075 switch (opt) {
Harald Welte6e264ae2012-11-24 13:14:09 +010076 case 'p':
77 pcs = 1;
78 break;
Harald Welteeeb78dd2011-08-02 13:44:54 +020079 case 'a':
80 mode = MODE_A2F;
81 param = optarg;
82 break;
83 case 'f':
84 mode = MODE_F2A;
85 param = optarg;
86 break;
87 case 'h':
88 help(argv[0]);
89 exit(0);
90 break;
91 default:
92 break;
93 }
94 }
95
96 switch (mode) {
97 case MODE_NONE:
98 help(argv[0]);
99 exit(2);
100 break;
101 case MODE_A2F:
Harald Welte6e264ae2012-11-24 13:14:09 +0100102 arfcn = atoi(param);
103 if (pcs)
104 arfcn |= ARFCN_PCS;
105 arfcn2freq(arfcn);
Harald Welteeeb78dd2011-08-02 13:44:54 +0200106 break;
107 }
108
109 exit(0);
110}