Harald Welte | eeb78dd | 2011-08-02 13:44:54 +0200 | [diff] [blame] | 1 | /* 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 | |
| 31 | enum program_mode { |
| 32 | MODE_NONE, |
| 33 | MODE_A2F, |
| 34 | MODE_F2A, |
| 35 | }; |
| 36 | |
Harald Welte | 6e264ae | 2012-11-24 13:14:09 +0100 | [diff] [blame] | 37 | static int arfcn2freq(int arfcn) |
Harald Welte | eeb78dd | 2011-08-02 13:44:54 +0200 | [diff] [blame] | 38 | { |
Harald Welte | eeb78dd | 2011-08-02 13:44:54 +0200 | [diff] [blame] | 39 | 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 Welte | 6e264ae | 2012-11-24 13:14:09 +0100 | [diff] [blame] | 55 | arfcn & ~ARFCN_FLAG_MASK, |
| 56 | freq10u/10, freq10u%10, freq10d/10, freq10d%10); |
Harald Welte | eeb78dd | 2011-08-02 13:44:54 +0200 | [diff] [blame] | 57 | |
| 58 | return 0; |
| 59 | } |
| 60 | |
Sylvain Munaut | c44310e | 2012-12-11 23:45:03 +0100 | [diff] [blame] | 61 | static 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 Welte | eeb78dd | 2011-08-02 13:44:54 +0200 | [diff] [blame] | 82 | static void help(const char *progname) |
| 83 | { |
Harald Welte | 6e264ae | 2012-11-24 13:14:09 +0100 | [diff] [blame] | 84 | printf("Usage: %s [-h] [-p] [-a arfcn] [-f freq] [-u|-d]\n", |
Harald Welte | eeb78dd | 2011-08-02 13:44:54 +0200 | [diff] [blame] | 85 | progname); |
| 86 | } |
| 87 | |
| 88 | int main(int argc, char **argv) |
| 89 | { |
Sylvain Munaut | c44310e | 2012-12-11 23:45:03 +0100 | [diff] [blame] | 90 | int arfcn, freq, pcs = 0, uplink = -1; |
Harald Welte | eeb78dd | 2011-08-02 13:44:54 +0200 | [diff] [blame] | 91 | int opt; |
| 92 | char *param; |
| 93 | enum program_mode mode = MODE_NONE; |
| 94 | |
Harald Welte | 6e264ae | 2012-11-24 13:14:09 +0100 | [diff] [blame] | 95 | while ((opt = getopt(argc, argv, "pa:f:ud")) != -1) { |
Harald Welte | eeb78dd | 2011-08-02 13:44:54 +0200 | [diff] [blame] | 96 | switch (opt) { |
Harald Welte | 6e264ae | 2012-11-24 13:14:09 +0100 | [diff] [blame] | 97 | case 'p': |
| 98 | pcs = 1; |
| 99 | break; |
Harald Welte | eeb78dd | 2011-08-02 13:44:54 +0200 | [diff] [blame] | 100 | case 'a': |
| 101 | mode = MODE_A2F; |
| 102 | param = optarg; |
| 103 | break; |
| 104 | case 'f': |
| 105 | mode = MODE_F2A; |
| 106 | param = optarg; |
| 107 | break; |
Sylvain Munaut | c44310e | 2012-12-11 23:45:03 +0100 | [diff] [blame] | 108 | case 'u': |
| 109 | uplink = 1; |
| 110 | break; |
| 111 | case 'd': |
| 112 | uplink = 0; |
| 113 | break; |
Harald Welte | eeb78dd | 2011-08-02 13:44:54 +0200 | [diff] [blame] | 114 | 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 Welte | 6e264ae | 2012-11-24 13:14:09 +0100 | [diff] [blame] | 129 | arfcn = atoi(param); |
| 130 | if (pcs) |
| 131 | arfcn |= ARFCN_PCS; |
| 132 | arfcn2freq(arfcn); |
Harald Welte | eeb78dd | 2011-08-02 13:44:54 +0200 | [diff] [blame] | 133 | break; |
Sylvain Munaut | c44310e | 2012-12-11 23:45:03 +0100 | [diff] [blame] | 134 | case MODE_F2A: |
| 135 | freq = (int)(atof(param) * 10.0f); |
| 136 | freq2arfcn(freq, uplink); |
| 137 | break; |
Harald Welte | eeb78dd | 2011-08-02 13:44:54 +0200 | [diff] [blame] | 138 | } |
| 139 | |
| 140 | exit(0); |
| 141 | } |