view uptools/atcmd/smsend_multmain.c @ 381:40b1498ec39d

fcup-smsend: UCS-2 send mode implemented
author Mychaela Falconia <falcon@freecalypso.org>
date Fri, 09 Mar 2018 02:31:15 +0000
parents 1fa4dcbb1c87
children 88131632ce48
line wrap: on
line source

/*
 * This is the main module for the fcup-smsendmult utility.
 */

#include <sys/types.h>
#include <ctype.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <strings.h>
#include <unistd.h>
#include "../../rvinterf/include/exitcodes.h"

int sms_write_mode, text_mode, utf8_input;
u_char dest_addr[12];
int dest_addr_global;
char input_line[21+5+1+320+2], *msgtext;
int lineno;
u_char msgtext_gsm7[160];
unsigned msgtext_gsmlen;

process_cmdline(argc, argv)
	char **argv;
{
	int c;
	extern int optind;

	while ((c = getopt(argc, argv, "B:np:RtuwWX:")) != EOF) {
		if (atinterf_cmdline_opt(c))
			continue;
		switch (c) {
		case 't':
			text_mode = 1;
			continue;
		case 'u':
			utf8_input = 1;
			continue;
		case 'w':
			sms_write_mode = 1;
			continue;
		case 'W':
			sms_write_mode = 2;
			continue;
		default:
			/* error msg already printed */
			exit(ERROR_USAGE);
		}
	}
	if (argc > optind + 1) {
		fprintf(stderr, "usage: %s [options] [dest-addr]\n",
			argv[0]);
		exit(ERROR_USAGE);
	}
	if (argv[optind]) {
		dest_addr_global = 1;
		if (parse_and_encode_dest_addr(argv[optind], dest_addr) < 0) {
			fprintf(stderr,
			"error: destination address argument is invalid\n");
			exit(ERROR_USAGE);
		}
	} else
		dest_addr_global = 0;
	return(0);
}

preen_input_line()
{
	char *cp;

	cp = index(input_line, '\n');
	if (!cp) {
		fprintf(stderr, "input line %d: too long or unterminated\n",
			lineno);
		exit(ERROR_USAGE);
	}
	*cp = '\0';
	if (dest_addr_global) {
		msgtext = input_line;
		return(0);
	}
	if (!input_line[0] || isspace(input_line[0])) {
inv:		fprintf(stderr, "input line %d: invalid syntax\n", lineno);
		exit(ERROR_USAGE);
	}
	for (cp = input_line; *cp && !isspace(*cp); cp++)
		;
	if (!*cp)
		goto inv;
	*cp++ = '\0';
	if (parse_and_encode_dest_addr(input_line, dest_addr) < 0)
		goto inv;
	while (isspace(*cp))
		cp++;
	msgtext = cp;
	return(1);
}

preen_message()
{
	int rc;

	if (utf8_input && utf8_to_latin1(msgtext) < 0) {
		fprintf(stderr, "input line %d: invalid UTF-8 message\n",
			lineno);
		exit(ERROR_USAGE);
	}
	if (text_mode) {
		if (strlen(msgtext) > 160) {
toolong:		fprintf(stderr,
				"input line %d: message exceeds 160 chars\n",
				lineno);
			exit(ERROR_USAGE);
		}
		return(0);
	}
	rc = latin1_to_gsm7(msgtext, msgtext_gsm7, 160, &msgtext_gsmlen);
	if (rc == -1) {
		fprintf(stderr,
			"input line %d: message not valid for GSM7 charset\n",
			lineno);
		exit(ERROR_USAGE);
	}
	if (rc == -2)
		goto toolong;
	return(0);
}

init_send_process()
{
	atinterf_init();
	/* enable verbose error messages */
	atinterf_exec_cmd_needok("AT+CMEE=2", 0, 0);
	if (text_mode)
		prep_for_text_mode();
	else
		prep_for_pdu_mode();
	if (sms_write_mode == 0)
		atinterf_exec_cmd_needok("AT+CMMS=1", 0, 0);
}

main(argc, argv)
	char **argv;
{
	int initdone = 0;

	process_cmdline(argc, argv);
	for (lineno = 1; fgets(input_line, sizeof input_line, stdin); lineno++){
		preen_input_line();
		preen_message();
		if (!initdone) {
			init_send_process();
			initdone = 1;
		}
		if (text_mode)
			send_in_text_mode(dest_addr, msgtext);
		else
			send_in_pdu_mode(dest_addr, msgtext_gsm7,
					 msgtext_gsmlen, 0, 0);
	}
	if (!initdone)
		exit(0);
	if (sms_write_mode == 0)
		atinterf_exec_cmd_needok("AT+CMMS=0", 0, 0);
	if (sms_write_mode == 1)
		sendafterwr_process();
	exit(0);
}