Harald Welte | c8a0b93 | 2012-08-24 21:27:26 +0200 | [diff] [blame] | 1 | /* |
| 2 | * (C) 2012 by Harald Welte <laforge@gnumonks.org> |
| 3 | * All Rights Reserved |
| 4 | * |
| 5 | * This program is free software; you can redistribute it and/or modify |
| 6 | * it under the terms of the GNU General Public License as published by |
| 7 | * the Free Software Foundation; either version 2 of the License, or |
| 8 | * (at your option) any later version. |
| 9 | * |
| 10 | * This program is distributed in the hope that it will be useful, |
| 11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 13 | * GNU General Public License for more details. |
| 14 | * |
| 15 | * You should have received a copy of the GNU General Public License along |
| 16 | * with this program; if not, write to the Free Software Foundation, Inc., |
| 17 | * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. |
| 18 | * |
| 19 | */ |
| 20 | |
| 21 | #include <string.h> |
| 22 | #include <stdio.h> |
Holger Hans Peter Freyther | cd252e3 | 2013-07-03 09:56:53 +0200 | [diff] [blame] | 23 | #include <stdlib.h> |
Harald Welte | c8a0b93 | 2012-08-24 21:27:26 +0200 | [diff] [blame] | 24 | |
| 25 | #include <osmocom/gsm/protocol/gsm_04_08.h> |
| 26 | #include <osmocom/gsm/gsm48_ie.h> |
Holger Hans Peter Freyther | cd252e3 | 2013-07-03 09:56:53 +0200 | [diff] [blame] | 27 | #include <osmocom/gsm/gsm48.h> |
Harald Welte | c8a0b93 | 2012-08-24 21:27:26 +0200 | [diff] [blame] | 28 | #include <osmocom/gsm/mncc.h> |
Holger Hans Peter Freyther | cd252e3 | 2013-07-03 09:56:53 +0200 | [diff] [blame] | 29 | #include <osmocom/core/backtrace.h> |
Harald Welte | c8a0b93 | 2012-08-24 21:27:26 +0200 | [diff] [blame] | 30 | #include <osmocom/core/utils.h> |
| 31 | #include <osmocom/core/msgb.h> |
| 32 | |
| 33 | |
| 34 | static const uint8_t csd_9600_v110_lv[] = { 0x07, 0xa1, 0xb8, 0x89, 0x21, 0x15, 0x63, 0x80 }; |
| 35 | |
| 36 | static const struct gsm_mncc_bearer_cap bcap_csd_9600_v110 = { |
| 37 | .transfer = GSM48_BCAP_ITCAP_UNR_DIG_INF, |
| 38 | .mode = GSM48_BCAP_TMOD_CIRCUIT, |
| 39 | .coding = GSM48_BCAP_CODING_GSM_STD, |
| 40 | .radio = GSM48_BCAP_RRQ_FR_ONLY, |
| 41 | .speech_ver[0]= -1, |
| 42 | .data = { |
| 43 | .rate_adaption = GSM48_BCAP_RA_V110_X30, |
| 44 | .sig_access = GSM48_BCAP_SA_I440_I450, |
| 45 | .async = 1, |
| 46 | .nr_stop_bits = 1, |
| 47 | .nr_data_bits = 8, |
| 48 | .user_rate = GSM48_BCAP_UR_9600, |
| 49 | .parity = GSM48_BCAP_PAR_NONE, |
| 50 | .interm_rate = GSM48_BCAP_IR_16k, |
| 51 | .transp = GSM48_BCAP_TR_TRANSP, |
| 52 | .modem_type = GSM48_BCAP_MT_NONE, |
| 53 | }, |
| 54 | }; |
| 55 | |
| 56 | static const uint8_t speech_all_lv[] = { 0x06, 0x60, 0x04, 0x02, 0x00, 0x05, 0x81 }; |
| 57 | |
| 58 | static const struct gsm_mncc_bearer_cap bcap_speech_all = { |
| 59 | .transfer = GSM48_BCAP_ITCAP_SPEECH, |
| 60 | .mode = GSM48_BCAP_TMOD_CIRCUIT, |
| 61 | .coding = GSM48_BCAP_CODING_GSM_STD, |
| 62 | .radio = GSM48_BCAP_RRQ_DUAL_FR, |
| 63 | .speech_ver = { |
| 64 | 4, 2, 0, 5, 1, -1, |
| 65 | }, |
| 66 | }; |
| 67 | |
| 68 | |
| 69 | struct bcap_test { |
| 70 | const uint8_t *lv; |
| 71 | const struct gsm_mncc_bearer_cap *bc; |
| 72 | const char *name; |
| 73 | }; |
| 74 | |
| 75 | static const struct bcap_test bcap_tests[] = { |
| 76 | { csd_9600_v110_lv, &bcap_csd_9600_v110, "CSD 9600/V.110/transparent" }, |
| 77 | { speech_all_lv, &bcap_speech_all, "Speech, all codecs" }, |
| 78 | }; |
| 79 | |
| 80 | static int test_bearer_cap() |
| 81 | { |
| 82 | struct gsm_mncc_bearer_cap bc; |
| 83 | int i, rc; |
| 84 | |
| 85 | for (i = 0; i < ARRAY_SIZE(bcap_tests); i++) { |
| 86 | struct msgb *msg = msgb_alloc(100, "test"); |
| 87 | int lv_len; |
| 88 | |
| 89 | memset(&bc, 0, sizeof(bc)); |
| 90 | |
| 91 | /* test decoding */ |
| 92 | rc = gsm48_decode_bearer_cap(&bc, bcap_tests[i].lv); |
| 93 | if (rc < 0) { |
| 94 | fprintf(stderr, "Error decoding %s\n", |
| 95 | bcap_tests[i].name); |
| 96 | return rc; |
| 97 | } |
| 98 | if (memcmp(&bc, bcap_tests[i].bc, sizeof(bc))) { |
| 99 | fprintf(stderr, "Incorrect decoded result of %s:\n", |
| 100 | bcap_tests[i].name); |
| 101 | fprintf(stderr, " should: %s\n", |
| 102 | osmo_hexdump((uint8_t *) bcap_tests[i].bc, sizeof(bc))); |
| 103 | fprintf(stderr, " is: %s\n", |
| 104 | osmo_hexdump((uint8_t *) &bc, sizeof(bc))); |
| 105 | return -1; |
| 106 | } |
| 107 | |
| 108 | /* also test re-encode? */ |
| 109 | rc = gsm48_encode_bearer_cap(msg, 1, &bc); |
| 110 | if (rc < 0) { |
| 111 | fprintf(stderr, "Error encoding %s\n", |
| 112 | bcap_tests[i].name); |
| 113 | return rc; |
| 114 | } |
| 115 | lv_len = bcap_tests[i].lv[0]+1; |
| 116 | if (memcmp(msg->data, bcap_tests[i].lv, lv_len)) { |
| 117 | fprintf(stderr, "Incorrect encoded result of %s:\n", |
| 118 | bcap_tests[i].name); |
| 119 | fprintf(stderr, " should: %s\n", |
| 120 | osmo_hexdump(bcap_tests[i].lv, lv_len)); |
| 121 | fprintf(stderr, " is: %s\n", |
| 122 | osmo_hexdump(msg->data, msg->len)); |
| 123 | return -1; |
| 124 | } |
| 125 | |
| 126 | printf("Test `%s' passed\n", bcap_tests[i].name); |
| 127 | msgb_free(msg); |
| 128 | } |
| 129 | |
| 130 | return 0; |
| 131 | } |
| 132 | |
Max | 99377c2 | 2017-08-30 19:17:50 +0200 | [diff] [blame] | 133 | static inline void dump_ra(const struct gprs_ra_id *raid) |
| 134 | { |
| 135 | printf("RA: MNC=%u, MCC=%u, LAC=%u, RAC=%u\n", raid->mnc, raid->mcc, raid->lac, raid->rac); |
| 136 | } |
| 137 | |
| 138 | static inline void check_ra(const struct gprs_ra_id *raid) |
| 139 | { |
| 140 | uint8_t buf[6]; |
| 141 | int res; |
| 142 | struct gprs_ra_id raid0 = { |
| 143 | .mnc = 0, |
| 144 | .mcc = 0, |
| 145 | .lac = 0, |
| 146 | .rac = 0, |
| 147 | }; |
| 148 | |
| 149 | res = gsm48_construct_ra(buf, raid); |
| 150 | printf("Constructed RA: %d - %s\n", res, res != sizeof(buf) ? "FAIL" : "OK"); |
| 151 | |
| 152 | gsm48_parse_ra(&raid0, buf); |
| 153 | dump_ra(raid); |
| 154 | dump_ra(&raid0); |
| 155 | printf("RA test..."); |
| 156 | if (raid->mnc != raid0.mnc || raid->mcc != raid0.mcc || raid->lac != raid0.lac || raid->rac != raid0.rac) |
| 157 | printf("FAIL\n"); |
| 158 | else |
| 159 | printf("passed\n"); |
| 160 | } |
| 161 | |
| 162 | static void test_ra_cap(void) |
| 163 | { |
| 164 | struct gprs_ra_id raid1 = { |
| 165 | .mnc = 121, |
| 166 | .mcc = 77, |
| 167 | .lac = 666, |
| 168 | .rac = 5, |
| 169 | }; |
| 170 | struct gprs_ra_id raid2 = { |
| 171 | .mnc = 98, |
| 172 | .mcc = 84, |
| 173 | .lac = 11, |
| 174 | .rac = 89, |
| 175 | }; |
| 176 | |
| 177 | check_ra(&raid1); |
| 178 | check_ra(&raid2); |
| 179 | } |
| 180 | |
Holger Hans Peter Freyther | cd252e3 | 2013-07-03 09:56:53 +0200 | [diff] [blame] | 181 | static void test_mid_from_tmsi(void) |
| 182 | { |
| 183 | static const uint8_t res[] = { 0x17, 0x05, 0xf4, 0xaa, 0xbb, 0xcc, 0xdd }; |
| 184 | |
| 185 | |
| 186 | uint32_t tmsi = 0xAABBCCDD; |
| 187 | uint8_t buf[3 + sizeof(uint32_t)]; |
| 188 | |
| 189 | printf("Simple TMSI encoding test...."); |
| 190 | |
| 191 | memset(&buf, 0xFE, sizeof(buf)); |
| 192 | gsm48_generate_mid_from_tmsi(buf, tmsi); |
| 193 | |
| 194 | OSMO_ASSERT(memcmp(buf, res, sizeof(res)) == 0); |
| 195 | printf("passed\n"); |
| 196 | } |
| 197 | |
Harald Welte | c8a0b93 | 2012-08-24 21:27:26 +0200 | [diff] [blame] | 198 | int main(int argc, char **argv) |
| 199 | { |
Neels Hofmeyr | 8a3409c | 2016-09-16 02:37:24 +0200 | [diff] [blame] | 200 | msgb_talloc_ctx_init(NULL, 0); |
Harald Welte | c8a0b93 | 2012-08-24 21:27:26 +0200 | [diff] [blame] | 201 | test_bearer_cap(); |
Holger Hans Peter Freyther | cd252e3 | 2013-07-03 09:56:53 +0200 | [diff] [blame] | 202 | test_mid_from_tmsi(); |
Max | 99377c2 | 2017-08-30 19:17:50 +0200 | [diff] [blame] | 203 | test_ra_cap(); |
Holger Hans Peter Freyther | cd252e3 | 2013-07-03 09:56:53 +0200 | [diff] [blame] | 204 | |
| 205 | return EXIT_SUCCESS; |
Harald Welte | c8a0b93 | 2012-08-24 21:27:26 +0200 | [diff] [blame] | 206 | } |