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; |
| 65 | |
| 66 | if (uplink != 0 && uplink != 1) { |
| 67 | fprintf(stderr, "Need to specify uplink or downlink\n"); |
| 68 | return -EINVAL; |
| 69 | } |
| 70 | |
| 71 | arfcn = gsm_freq102arfcn(freq10, uplink); |
| 72 | |
| 73 | if (arfcn == 0xffff) { |
| 74 | fprintf(stderr, "Unable to find matching ARFCN\n"); |
| 75 | return -EINVAL; |
| 76 | } |
| 77 | |
| 78 | printf("%s: ARFCN %4d\n", |
| 79 | gsm_band_name(gsm_arfcn2band(arfcn)), |
| 80 | arfcn & ~ARFCN_FLAG_MASK); |
Holger Hans Peter Freyther | d0ce550 | 2013-04-21 21:04:19 +0200 | [diff] [blame] | 81 | return 0; |
Sylvain Munaut | c44310e | 2012-12-11 23:45:03 +0100 | [diff] [blame] | 82 | } |
| 83 | |
Harald Welte | eeb78dd | 2011-08-02 13:44:54 +0200 | [diff] [blame] | 84 | static void help(const char *progname) |
| 85 | { |
Harald Welte | 6e264ae | 2012-11-24 13:14:09 +0100 | [diff] [blame] | 86 | printf("Usage: %s [-h] [-p] [-a arfcn] [-f freq] [-u|-d]\n", |
Harald Welte | eeb78dd | 2011-08-02 13:44:54 +0200 | [diff] [blame] | 87 | progname); |
| 88 | } |
| 89 | |
| 90 | int main(int argc, char **argv) |
| 91 | { |
Sylvain Munaut | c44310e | 2012-12-11 23:45:03 +0100 | [diff] [blame] | 92 | int arfcn, freq, pcs = 0, uplink = -1; |
Harald Welte | eeb78dd | 2011-08-02 13:44:54 +0200 | [diff] [blame] | 93 | int opt; |
| 94 | char *param; |
| 95 | enum program_mode mode = MODE_NONE; |
| 96 | |
Harald Welte | 6e264ae | 2012-11-24 13:14:09 +0100 | [diff] [blame] | 97 | while ((opt = getopt(argc, argv, "pa:f:ud")) != -1) { |
Harald Welte | eeb78dd | 2011-08-02 13:44:54 +0200 | [diff] [blame] | 98 | switch (opt) { |
Harald Welte | 6e264ae | 2012-11-24 13:14:09 +0100 | [diff] [blame] | 99 | case 'p': |
| 100 | pcs = 1; |
| 101 | break; |
Harald Welte | eeb78dd | 2011-08-02 13:44:54 +0200 | [diff] [blame] | 102 | case 'a': |
| 103 | mode = MODE_A2F; |
| 104 | param = optarg; |
| 105 | break; |
| 106 | case 'f': |
| 107 | mode = MODE_F2A; |
| 108 | param = optarg; |
| 109 | break; |
Sylvain Munaut | c44310e | 2012-12-11 23:45:03 +0100 | [diff] [blame] | 110 | case 'u': |
| 111 | uplink = 1; |
| 112 | break; |
| 113 | case 'd': |
| 114 | uplink = 0; |
| 115 | break; |
Harald Welte | eeb78dd | 2011-08-02 13:44:54 +0200 | [diff] [blame] | 116 | case 'h': |
| 117 | help(argv[0]); |
| 118 | exit(0); |
| 119 | break; |
| 120 | default: |
| 121 | break; |
| 122 | } |
| 123 | } |
| 124 | |
| 125 | switch (mode) { |
| 126 | case MODE_NONE: |
| 127 | help(argv[0]); |
| 128 | exit(2); |
| 129 | break; |
| 130 | case MODE_A2F: |
Harald Welte | 6e264ae | 2012-11-24 13:14:09 +0100 | [diff] [blame] | 131 | arfcn = atoi(param); |
| 132 | if (pcs) |
| 133 | arfcn |= ARFCN_PCS; |
| 134 | arfcn2freq(arfcn); |
Harald Welte | eeb78dd | 2011-08-02 13:44:54 +0200 | [diff] [blame] | 135 | break; |
Sylvain Munaut | c44310e | 2012-12-11 23:45:03 +0100 | [diff] [blame] | 136 | case MODE_F2A: |
| 137 | freq = (int)(atof(param) * 10.0f); |
| 138 | freq2arfcn(freq, uplink); |
| 139 | break; |
Harald Welte | eeb78dd | 2011-08-02 13:44:54 +0200 | [diff] [blame] | 140 | } |
| 141 | |
| 142 | exit(0); |
| 143 | } |