summaryrefslogtreecommitdiff
path: root/src/pdu_encoder.c
blob: 3df30162b5a62a08369644c15c8bbfcb277be1b9 (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
158
159
160
161
162
163
164
165
166
167
/*
 * PDU Encoder 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 <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <getopt.h>
#include <errno.h>
#include <time.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>

#include "global.h"
#include "cmd_options.h"
#include "utils.h"
#include "sms_utils.h"
#include "sms_send.h"
#include "sms_list.h"
#include "sms_delete.h"
#include "xprintf.h"
#include "pdu_encode.h"
#include "pdu_decode.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-encoder [ OPTIONS ... ] [ <recipient-addr> ]\n");
	fprintf(out, "where  OPTIONS := { \n");
	fprintf(out, "         --alphabet { seven-bit | eight-bit } |\n");
	fprintf(out, "         -f, --file <input-file> (default is to read from stdin) |\n");
	fprintf(out, "         --smsc-addr <smsc-addr> |\n");
	fprintf(out, "         --verbose\n");
	fprintf(out, "       }\n");
	fprintf(out, "\n");
}

static char *short_options =
	__CMD_OPT_FILE
	__CMD_OPT_VERBOSE;
static struct option long_options[] = {
	{"alphabet", 1, NULL, CMD_OPT_ALPHABET},
	{"file", 1, NULL, CMD_OPT_FILE},
	{"smsc-addr", 1, NULL, CMD_OPT_SMSC_ADDR},
	{"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 alphabet = PDU_ALPHABET_DEFAULT;
	int in_fd = fileno(stdin);
	char *smsc_addr = NULL;

	xprintf_init();

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

		case CMD_OPT_ALPHABET:
			if (!strcmp(optarg, "seven-bit")) {
				alphabet = PDU_ALPHABET_DEFAULT;
			} else if (!strcmp(optarg, "eight-bit")) {
				alphabet = PDU_ALPHABET_EIGHT;
			}
			break;

		case CMD_OPT_FILE:
			in_fd = open(optarg, O_RDONLY);
			if (in_fd < 0) {
				fprintf(stderr, "failed to open %s for reading\n", optarg);
				return 1;
			}
			break;

		case CMD_OPT_SMSC_ADDR:
			smsc_addr = optarg;
			break;

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

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

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

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

	argc -= optind;
	argv += optind;

	struct pdu_info pdu;
	char pdu_str[PDU_BUFFER_SIZE];

	memset(&pdu, 0, sizeof(pdu));

	pdu.type.msg_type = PDU_MTI_SUBMIT;
	pdu.type.validity_period_format = PDU_VPF_RELATIVE;
	pdu.validity_period = PDU_VPF_RELATIVE_2DAYS;
	pdu.data_coding.general.alphabet = alphabet;

	if (smsc_addr) {
		sms_addr_fill(&pdu.smsc_addr, smsc_addr, SMS_ADDR_UNSPEC);
	}

	if (argc) {
		sms_addr_fill(&pdu.addr, *argv, SMS_ADDR_UNSPEC);
		argc++;
		argv--;
	}

	pdu_user_data_read(in_fd, &pdu);

	pdu_encode(pdu_str, sizeof(pdu_str), &pdu);

	printf("pdu: %s\n", pdu_str);

	return 0;
}