Holger Hans Peter Freyther | daa653f | 2010-10-11 07:56:06 +0200 | [diff] [blame] | 1 | /* |
| 2 | * (C) 2010 by Holger Hans Peter Freyther |
| 3 | * (C) 2010 by On-Waves |
| 4 | * All Rights Reserved |
| 5 | * |
| 6 | * This program is free software; you can redistribute it and/or modify |
| 7 | * it under the terms of the GNU General Public License as published by |
| 8 | * the Free Software Foundation; either version 2 of the License, or |
| 9 | * (at your option) any later version. |
| 10 | * |
| 11 | * This program is distributed in the hope that it will be useful, |
| 12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 14 | * GNU General Public License for more details. |
| 15 | * |
| 16 | * You should have received a copy of the GNU General Public License along |
| 17 | * with this program; if not, write to the Free Software Foundation, Inc., |
| 18 | * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. |
| 19 | * |
| 20 | */ |
| 21 | |
Holger Hans Peter Freyther | bf12800 | 2011-11-13 22:48:37 +0100 | [diff] [blame] | 22 | #include <osmocom/core/application.h> |
| 23 | #include <osmocom/core/logging.h> |
Pablo Neira Ayuso | 8341934 | 2011-03-22 16:36:13 +0100 | [diff] [blame] | 24 | #include <osmocom/gsm/gsm0480.h> |
Andreas Eversberg | 9597555 | 2013-08-08 12:38:53 +0200 | [diff] [blame] | 25 | #include <osmocom/gsm/gsm_utils.h> |
Holger Hans Peter Freyther | daa653f | 2010-10-11 07:56:06 +0200 | [diff] [blame] | 26 | #include <stdio.h> |
| 27 | #include <stdlib.h> |
| 28 | #include <string.h> |
| 29 | |
| 30 | static const uint8_t ussd_request[] = { |
| 31 | 0x0b, 0x7b, 0x1c, 0x15, 0xa1, 0x13, 0x02, 0x01, |
| 32 | 0x03, 0x02, 0x01, 0x3b, 0x30, 0x0b, 0x04, 0x01, |
| 33 | 0x0f, 0x04, 0x06, 0x2a, 0xd5, 0x4c, 0x16, 0x1b, |
| 34 | 0x01, 0x7f, 0x01, 0x00 |
| 35 | }; |
| 36 | |
| 37 | static int parse_ussd(const uint8_t *_data, int len) |
| 38 | { |
| 39 | uint8_t *data; |
| 40 | int rc; |
| 41 | struct ussd_request req; |
| 42 | struct gsm48_hdr *hdr; |
| 43 | |
| 44 | data = malloc(len); |
| 45 | memcpy(data, _data, len); |
| 46 | hdr = (struct gsm48_hdr *) &data[0]; |
| 47 | rc = gsm0480_decode_ussd_request(hdr, len, &req); |
| 48 | free(data); |
| 49 | |
| 50 | return rc; |
| 51 | } |
| 52 | |
Holger Hans Peter Freyther | c88a44f | 2010-10-11 08:21:00 +0200 | [diff] [blame] | 53 | static int parse_mangle_ussd(const uint8_t *_data, int len) |
| 54 | { |
| 55 | uint8_t *data; |
| 56 | int rc; |
| 57 | struct ussd_request req; |
| 58 | struct gsm48_hdr *hdr; |
| 59 | |
| 60 | data = malloc(len); |
| 61 | memcpy(data, _data, len); |
| 62 | hdr = (struct gsm48_hdr *) &data[0]; |
| 63 | hdr->data[1] = len - sizeof(*hdr) - 2; |
| 64 | rc = gsm0480_decode_ussd_request(hdr, len, &req); |
| 65 | free(data); |
| 66 | |
| 67 | return rc; |
| 68 | } |
| 69 | |
Holger Hans Peter Freyther | bf12800 | 2011-11-13 22:48:37 +0100 | [diff] [blame] | 70 | struct log_info info = {}; |
| 71 | |
Jacob Erlbeck | 8a1666b | 2013-08-08 12:38:54 +0200 | [diff] [blame^] | 72 | static void test_7bit_ussd(const char *text, const char *encoded_hex, const char *appended_after_decode) |
Andreas Eversberg | 9597555 | 2013-08-08 12:38:53 +0200 | [diff] [blame] | 73 | { |
| 74 | uint8_t coded[256]; |
| 75 | char decoded[256]; |
| 76 | int y; |
| 77 | |
| 78 | printf("original = %s\n", osmo_hexdump((uint8_t *)text, strlen(text))); |
| 79 | gsm_7bit_encode_ussd(coded, text, &y); |
| 80 | printf("encoded = %s\n", osmo_hexdump(coded, y)); |
Jacob Erlbeck | 8a1666b | 2013-08-08 12:38:54 +0200 | [diff] [blame^] | 81 | |
| 82 | OSMO_ASSERT(strcmp(encoded_hex, osmo_hexdump_nospc(coded, y)) == 0); |
| 83 | |
Andreas Eversberg | 9597555 | 2013-08-08 12:38:53 +0200 | [diff] [blame] | 84 | gsm_7bit_decode_ussd(decoded, coded, y * 8 / 7); |
| 85 | y = strlen(decoded); |
| 86 | printf("decoded = %s\n\n", osmo_hexdump((uint8_t *)decoded, y)); |
Jacob Erlbeck | 8a1666b | 2013-08-08 12:38:54 +0200 | [diff] [blame^] | 87 | |
| 88 | OSMO_ASSERT(strncmp(text, decoded, strlen(text)) == 0); |
| 89 | OSMO_ASSERT(strcmp(appended_after_decode, decoded + strlen(text)) == 0); |
Andreas Eversberg | 9597555 | 2013-08-08 12:38:53 +0200 | [diff] [blame] | 90 | } |
| 91 | |
Holger Hans Peter Freyther | daa653f | 2010-10-11 07:56:06 +0200 | [diff] [blame] | 92 | int main(int argc, char **argv) |
| 93 | { |
Holger Hans Peter Freyther | f632371 | 2010-10-11 08:49:27 +0200 | [diff] [blame] | 94 | struct ussd_request req; |
Holger Hans Peter Freyther | daa653f | 2010-10-11 07:56:06 +0200 | [diff] [blame] | 95 | const int size = sizeof(ussd_request); |
| 96 | int i; |
| 97 | |
Holger Hans Peter Freyther | bf12800 | 2011-11-13 22:48:37 +0100 | [diff] [blame] | 98 | osmo_init_logging(&info); |
| 99 | |
Holger Hans Peter Freyther | f632371 | 2010-10-11 08:49:27 +0200 | [diff] [blame] | 100 | gsm0480_decode_ussd_request((struct gsm48_hdr *) ussd_request, size, &req); |
| 101 | printf("Tested if it still works. Text was: %s\n", req.text); |
| 102 | |
| 103 | |
Holger Hans Peter Freyther | daa653f | 2010-10-11 07:56:06 +0200 | [diff] [blame] | 104 | printf("Testing parsing a USSD request and truncated versions\n"); |
| 105 | |
| 106 | for (i = size; i > sizeof(struct gsm48_hdr); --i) { |
| 107 | int rc = parse_ussd(&ussd_request[0], i); |
| 108 | printf("Result for %d is %d\n", rc, i); |
| 109 | } |
| 110 | |
Holger Hans Peter Freyther | c88a44f | 2010-10-11 08:21:00 +0200 | [diff] [blame] | 111 | printf("Mangling the container now\n"); |
| 112 | for (i = size; i > sizeof(struct gsm48_hdr) + 2; --i) { |
| 113 | int rc = parse_mangle_ussd(&ussd_request[0], i); |
| 114 | printf("Result for %d is %d\n", rc, i); |
| 115 | } |
| 116 | |
Andreas Eversberg | 9597555 | 2013-08-08 12:38:53 +0200 | [diff] [blame] | 117 | printf("<CR> case test for 7 bit encode\n"); |
Jacob Erlbeck | 8a1666b | 2013-08-08 12:38:54 +0200 | [diff] [blame^] | 118 | test_7bit_ussd("01234567", "b0986c46abd96e", ""); |
| 119 | test_7bit_ussd("0123456", "b0986c46abd91a", ""); |
| 120 | test_7bit_ussd("01234567\r", "b0986c46abd96e0d", ""); |
| 121 | /* The appended \r is compliant to GSM 03.38 section 6.1.2.3.1: */ |
| 122 | test_7bit_ussd("0123456\r", "b0986c46abd91a0d", "\r"); |
| 123 | test_7bit_ussd("012345\r", "b0986c46ab351a", ""); |
Andreas Eversberg | 9597555 | 2013-08-08 12:38:53 +0200 | [diff] [blame] | 124 | |
Holger Hans Peter Freyther | daa653f | 2010-10-11 07:56:06 +0200 | [diff] [blame] | 125 | return 0; |
| 126 | } |