summaryrefslogtreecommitdiff
path: root/src/pdu_decoder.c
blob: f6a72e40ce165f161bf95361c0fc0896711a2947 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
/*
 * PDU Decoder tool
 *
 * Copyright (C) 2010 by James Maki
 *
 * Author: James Maki <jmaki@multitech.com>
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
 * General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
 *
 */

#define __MAIN_FILE_C	1

#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <stdint.h>
#include <ctype.h>
#include <time.h>
#include <getopt.h>

#include "global.h"
#include "cmd_options.h"
#include "sms_utils.h"
#include "pdu_decode.h"
#include "xprintf.h"
#include "utils.h"

static void print_version(const char *name)
{
	printf("%s (" PACKAGE ") " VERSION " (" __DATE__ " " __TIME__ ")\n", name);
	printf("Copyright (C) 2010 by Multi-Tech Systems\n");
	printf(
"This program is free software; you may redistribute it under the terms of\n"
"the GNU General Public License version 2 or (at your option) any later version.\n"
"This program has absolutely no warranty.\n");
}

static void usage(FILE *out)
{
	fprintf(out, "Usage: pdu-decoder [ OPTIONS ... ] PDU [ PDU ... ]\n");
	fprintf(out, "where  OPTIONS := { \n");
	fprintf(out, "         --verbose\n");
	fprintf(out, "       }\n");
	fprintf(out, "\n");
}

static char *short_options = "+"
	__CMD_OPT_VERBOSE;
static struct option long_options[] = {
	{"verbose", 0, NULL, CMD_OPT_VERBOSE},
	{"version", 0, NULL, CMD_OPT_VERSION},
	{"help", 0, NULL, CMD_OPT_HELP},
	{0, 0, 0, 0},
};

int main(int argc, char *argv[])
{
	int i;
	int option_index;
	int err;
	struct pdu_info pdu;
	char buf[64];
	int nr_success = 0;
	int nr_fail = 0;
	int nr_short = 0;
	int nr_long = 0;

	xprintf_init();

	while ((i = getopt_long(argc, argv, short_options, long_options, &option_index)) >= 0) {
		switch (i) {
		case 0:
			break;

		case CMD_OPT_VERBOSE:
			Global.core.verbose = true;
			break;

		case CMD_OPT_VERSION:
			print_version("pdu-decoder");
			exit(0);
			break;

		case CMD_OPT_HELP:
			usage(stdout);
			exit(0);
			break;

		default:
			usage(stderr);
			exit(1);
		}
	}

	if (optind >= argc) {
		usage(stderr);
		exit(1);
	}
	argc -= optind;
	argv += optind;

	for ( ; *argv; argv++) {
		err = pdu_decode(*argv, &pdu);
		if (err < 0) {
			printf("pdu decode failed: %d\n", err);
			nr_fail++;
		} else {
			log_debug("pdu-len: %d", strlen(*argv));
			log_debug("decoded-len: %d", err);

			if (err < strlen(*argv)) {
				nr_short++;
			} else if (err > strlen(*argv)) {
				nr_long++;
			}

			printf("message-length: %d\n", pdu.msg_len);
			printf("smsc-addr-length: 0x%02X\n", pdu.smsc_addr.len);
			printf("smsc-addr-type: 0x%02X\n", pdu.smsc_addr.type);
			printf("smsc-addr: %s\n", pdu.smsc_addr.addr);
			printf("type: 0x%02X\n", pdu.type.type);
			printf("message-reference: 0x%02X\n", pdu.msg_reference);
			printf("protocol-id: 0x%02X\n", pdu.protocol_id);
			printf("addr-length: 0x%02X\n", pdu.addr.len);
			printf("addr-type: 0x%02X\n", pdu.addr.type);
			printf("addr: %s\n", pdu.addr.addr);
			pdu_format_vp(&pdu, buf, sizeof(buf));
			printf("validity-period: %s\n", buf);
			pdu_format_timestamp(&pdu, buf, sizeof(buf), "%Y-%m-%d %T %z");
			printf("timestamp: %s\n", buf);
			printf("user-data-length: 0x%02X\n", pdu.user_data_len);
			printf("user-data: \"%.*J\"\n", pdu.user_data_len, pdu.user_data);
			printf("\n");

			nr_success++;
		}
	}

	printf("%d successful, %d failed\n", nr_success, nr_fail);
	log_debug("%d short, %d long", nr_short, nr_long);

	return 0;
}