summaryrefslogtreecommitdiff
path: root/src/pdu_decoder.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/pdu_decoder.c')
-rw-r--r--src/pdu_decoder.c157
1 files changed, 157 insertions, 0 deletions
diff --git a/src/pdu_decoder.c b/src/pdu_decoder.c
new file mode 100644
index 0000000..f6a72e4
--- /dev/null
+++ b/src/pdu_decoder.c
@@ -0,0 +1,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;
+}
+