blob: d41c141d57def63151541f37e23817f98ebd8bd9 [file] [log] [blame]
Holger Hans Peter Freytherdaa653f2010-10-11 07:56:06 +02001/*
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 Freytherbf128002011-11-13 22:48:37 +010022#include <osmocom/core/application.h>
23#include <osmocom/core/logging.h>
Pablo Neira Ayuso83419342011-03-22 16:36:13 +010024#include <osmocom/gsm/gsm0480.h>
Andreas Eversberg95975552013-08-08 12:38:53 +020025#include <osmocom/gsm/gsm_utils.h>
Holger Hans Peter Freytherdaa653f2010-10-11 07:56:06 +020026#include <stdio.h>
27#include <stdlib.h>
28#include <string.h>
29
30static 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
37static 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 Freytherc88a44f2010-10-11 08:21:00 +020053static 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 Freytherbf128002011-11-13 22:48:37 +010070struct log_info info = {};
71
Andreas Eversberg95975552013-08-08 12:38:53 +020072void gsm_7bit_ussd(char *text)
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));
81 gsm_7bit_decode_ussd(decoded, coded, y * 8 / 7);
82 y = strlen(decoded);
83 printf("decoded = %s\n\n", osmo_hexdump((uint8_t *)decoded, y));
84}
85
Holger Hans Peter Freytherdaa653f2010-10-11 07:56:06 +020086int main(int argc, char **argv)
87{
Holger Hans Peter Freytherf6323712010-10-11 08:49:27 +020088 struct ussd_request req;
Holger Hans Peter Freytherdaa653f2010-10-11 07:56:06 +020089 const int size = sizeof(ussd_request);
90 int i;
91
Holger Hans Peter Freytherbf128002011-11-13 22:48:37 +010092 osmo_init_logging(&info);
93
Holger Hans Peter Freytherf6323712010-10-11 08:49:27 +020094 gsm0480_decode_ussd_request((struct gsm48_hdr *) ussd_request, size, &req);
95 printf("Tested if it still works. Text was: %s\n", req.text);
96
97
Holger Hans Peter Freytherdaa653f2010-10-11 07:56:06 +020098 printf("Testing parsing a USSD request and truncated versions\n");
99
100 for (i = size; i > sizeof(struct gsm48_hdr); --i) {
101 int rc = parse_ussd(&ussd_request[0], i);
102 printf("Result for %d is %d\n", rc, i);
103 }
104
Holger Hans Peter Freytherc88a44f2010-10-11 08:21:00 +0200105 printf("Mangling the container now\n");
106 for (i = size; i > sizeof(struct gsm48_hdr) + 2; --i) {
107 int rc = parse_mangle_ussd(&ussd_request[0], i);
108 printf("Result for %d is %d\n", rc, i);
109 }
110
Andreas Eversberg95975552013-08-08 12:38:53 +0200111 printf("<CR> case test for 7 bit encode\n");
112 gsm_7bit_ussd("01234567");
113 gsm_7bit_ussd("0123456");
114 gsm_7bit_ussd("01234567\r");
115 gsm_7bit_ussd("0123456\r");
116 gsm_7bit_ussd("012345\r");
117
Holger Hans Peter Freytherdaa653f2010-10-11 07:56:06 +0200118 return 0;
119}