Neels Hofmeyr | 17518fe | 2017-06-20 04:35:06 +0200 | [diff] [blame] | 1 | /*! \file osmo-arfcn.c |
| 2 | * Utility program for ARFCN / frequency calculations. */ |
Harald Welte | eeb78dd | 2011-08-02 13:44:54 +0200 | [diff] [blame] | 3 | /* |
| 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 | |
| 32 | enum program_mode { |
| 33 | MODE_NONE, |
| 34 | MODE_A2F, |
| 35 | MODE_F2A, |
| 36 | }; |
| 37 | |
Harald Welte | 6e264ae | 2012-11-24 13:14:09 +0100 | [diff] [blame] | 38 | static int arfcn2freq(int arfcn) |
Harald Welte | eeb78dd | 2011-08-02 13:44:54 +0200 | [diff] [blame] | 39 | { |
Harald Welte | eeb78dd | 2011-08-02 13:44:54 +0200 | [diff] [blame] | 40 | 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 Welte | 6e264ae | 2012-11-24 13:14:09 +0100 | [diff] [blame] | 56 | arfcn & ~ARFCN_FLAG_MASK, |
| 57 | freq10u/10, freq10u%10, freq10d/10, freq10d%10); |
Harald Welte | eeb78dd | 2011-08-02 13:44:54 +0200 | [diff] [blame] | 58 | |
| 59 | return 0; |
| 60 | } |
| 61 | |
Sylvain Munaut | c44310e | 2012-12-11 23:45:03 +0100 | [diff] [blame] | 62 | static int freq2arfcn(int freq10, int uplink) |
| 63 | { |
| 64 | uint16_t arfcn; |
Pau Espin Pedrol | c877251 | 2018-11-16 12:59:46 +0100 | [diff] [blame] | 65 | enum gsm_band band; |
Sylvain Munaut | c44310e | 2012-12-11 23:45:03 +0100 | [diff] [blame] | 66 | |
| 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 Pedrol | c877251 | 2018-11-16 12:59:46 +0100 | [diff] [blame] | 79 | if (gsm_arfcn2band_rc(arfcn, &band) < 0) { |
| 80 | fprintf(stderr, "ARFCN contains no valid band\n"); |
| 81 | return -EINVAL; |
| 82 | } |
| 83 | |
Sylvain Munaut | c44310e | 2012-12-11 23:45:03 +0100 | [diff] [blame] | 84 | printf("%s: ARFCN %4d\n", |
Pau Espin Pedrol | c877251 | 2018-11-16 12:59:46 +0100 | [diff] [blame] | 85 | gsm_band_name(band), |
Sylvain Munaut | c44310e | 2012-12-11 23:45:03 +0100 | [diff] [blame] | 86 | arfcn & ~ARFCN_FLAG_MASK); |
Holger Hans Peter Freyther | d0ce550 | 2013-04-21 21:04:19 +0200 | [diff] [blame] | 87 | return 0; |
Sylvain Munaut | c44310e | 2012-12-11 23:45:03 +0100 | [diff] [blame] | 88 | } |
| 89 | |
Harald Welte | eeb78dd | 2011-08-02 13:44:54 +0200 | [diff] [blame] | 90 | static void help(const char *progname) |
| 91 | { |
Harald Welte | 6e264ae | 2012-11-24 13:14:09 +0100 | [diff] [blame] | 92 | printf("Usage: %s [-h] [-p] [-a arfcn] [-f freq] [-u|-d]\n", |
Harald Welte | eeb78dd | 2011-08-02 13:44:54 +0200 | [diff] [blame] | 93 | progname); |
| 94 | } |
| 95 | |
| 96 | int main(int argc, char **argv) |
| 97 | { |
Sylvain Munaut | c44310e | 2012-12-11 23:45:03 +0100 | [diff] [blame] | 98 | int arfcn, freq, pcs = 0, uplink = -1; |
Harald Welte | eeb78dd | 2011-08-02 13:44:54 +0200 | [diff] [blame] | 99 | int opt; |
| 100 | char *param; |
| 101 | enum program_mode mode = MODE_NONE; |
| 102 | |
Harald Welte | f7404bb | 2019-12-03 21:35:02 +0100 | [diff] [blame] | 103 | while ((opt = getopt(argc, argv, "pa:f:udh")) != -1) { |
Harald Welte | eeb78dd | 2011-08-02 13:44:54 +0200 | [diff] [blame] | 104 | switch (opt) { |
Harald Welte | 6e264ae | 2012-11-24 13:14:09 +0100 | [diff] [blame] | 105 | case 'p': |
| 106 | pcs = 1; |
| 107 | break; |
Harald Welte | eeb78dd | 2011-08-02 13:44:54 +0200 | [diff] [blame] | 108 | case 'a': |
| 109 | mode = MODE_A2F; |
| 110 | param = optarg; |
| 111 | break; |
| 112 | case 'f': |
| 113 | mode = MODE_F2A; |
| 114 | param = optarg; |
| 115 | break; |
Sylvain Munaut | c44310e | 2012-12-11 23:45:03 +0100 | [diff] [blame] | 116 | case 'u': |
| 117 | uplink = 1; |
| 118 | break; |
| 119 | case 'd': |
| 120 | uplink = 0; |
| 121 | break; |
Harald Welte | eeb78dd | 2011-08-02 13:44:54 +0200 | [diff] [blame] | 122 | case 'h': |
| 123 | help(argv[0]); |
| 124 | exit(0); |
| 125 | break; |
| 126 | default: |
| 127 | break; |
| 128 | } |
| 129 | } |
| 130 | |
Harald Welte | 278a6c8 | 2019-12-03 21:35:12 +0100 | [diff] [blame] | 131 | if (argc > optind) { |
| 132 | fprintf(stderr, "Unsupported positional arguments in command line\n"); |
| 133 | exit(2); |
| 134 | } |
| 135 | |
Harald Welte | eeb78dd | 2011-08-02 13:44:54 +0200 | [diff] [blame] | 136 | switch (mode) { |
| 137 | case MODE_NONE: |
| 138 | help(argv[0]); |
| 139 | exit(2); |
| 140 | break; |
| 141 | case MODE_A2F: |
Harald Welte | 6e264ae | 2012-11-24 13:14:09 +0100 | [diff] [blame] | 142 | arfcn = atoi(param); |
| 143 | if (pcs) |
| 144 | arfcn |= ARFCN_PCS; |
| 145 | arfcn2freq(arfcn); |
Harald Welte | eeb78dd | 2011-08-02 13:44:54 +0200 | [diff] [blame] | 146 | break; |
Sylvain Munaut | c44310e | 2012-12-11 23:45:03 +0100 | [diff] [blame] | 147 | case MODE_F2A: |
| 148 | freq = (int)(atof(param) * 10.0f); |
| 149 | freq2arfcn(freq, uplink); |
| 150 | break; |
Harald Welte | eeb78dd | 2011-08-02 13:44:54 +0200 | [diff] [blame] | 151 | } |
| 152 | |
| 153 | exit(0); |
| 154 | } |