view uptools/atcmd/smsend_multmain.c @ 659:761e8b0c65b0

loadagent: first step in implementation of binary flash programming
author Mychaela Falconia <falcon@freecalypso.org>
date Tue, 03 Mar 2020 17:55:20 +0000
parents dc2fd8e6f42c
children 2969032bdfac
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, ucs2_mode;
u_char dest_addr[12];
int dest_addr_global;
char input_line[21+5+1+320+2], *msgtext;
int lineno;
int initdone;

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

	while ((c = getopt(argc, argv, "B:np:RtuUwWX:")) != EOF) {
		if (atinterf_cmdline_opt(c))
			continue;
		switch (c) {
		case 't':
			text_mode = 1;
			continue;
		case 'u':
			utf8_input = 1;
			continue;
		case 'U':
			ucs2_mode = 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 (ucs2_mode && text_mode) {
		fprintf(stderr, "%s error: UCS-2 not supported in text mode\n",
			argv[0]);
		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);
}

init_send_process()
{
	if (initdone)
		return(0);
	atinterf_init();
	/* enable verbose error messages */
	atinterf_exec_cmd_needok("AT+CMEE=2", (char *) 0, (void *) 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", (char *) 0, (void *) 0);
	initdone = 1;
	return(1);
}

process_msg_gsm7()
{
	u_char msgtext_gsm7[160];
	unsigned msgtext_gsmlen;
	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);
		}
		init_send_process();
		send_in_text_mode(dest_addr, msgtext);
		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;
	init_send_process();
	send_in_pdu_mode(dest_addr, msgtext_gsm7, msgtext_gsmlen, 0, 0);
	return(0);
}

process_msg_ucs2()
{
	u_short msgtext_uni[70];
	unsigned msgtext_unilen;
	int rc;

	rc = utf8_to_ucs2(msgtext, msgtext_uni, 70, &msgtext_unilen);
	if (rc == -1) {
		fprintf(stderr, "input line %d: invalid UTF-8 message\n",
			lineno);
		exit(ERROR_USAGE);
	}
	if (rc == -2) {
		fprintf(stderr,
			"input line %d: message exceeds 70 UCS-2 chars\n",
			lineno);
		exit(ERROR_USAGE);
	}
	init_send_process();
	send_pdu_ucs2(dest_addr, msgtext_uni, msgtext_unilen, 0, 0);
	return(0);
}

main(argc, argv)
	char **argv;
{
	process_cmdline(argc, argv);
	for (lineno = 1; fgets(input_line, sizeof input_line, stdin); lineno++){
		preen_input_line();
		if (ucs2_mode)
			process_msg_ucs2();
		else
			process_msg_gsm7();
	}
	if (!initdone)
		exit(0);
	if (sms_write_mode == 0)
		atinterf_exec_cmd_needok("AT+CMMS=0", (char *) 0, (void *) 0);
	if (sms_write_mode == 1)
		sendafterwr_process();
	exit(0);
}