view uptools/atcmd/smsend_multmain.c @ 407:19e5a3e2f9c0

fcup-settime: moved time() retrieval a little closer to the output A fundamental problem with all simple time transfer tools is that there is always some delay between the time retrieval on the source system and that transmitted time being set on the destination, and the resulting time on the destination system is off by that delay amount. This delay cannot be fully eliminated when working in a simple environment like ours, but we should make our best effort to minimize it. In the present case, moving the atinterf_init() call before the time() retrieval should make a teensy-tiny improvement.
author Mychaela Falconia <falcon@freecalypso.org>
date Sat, 11 Aug 2018 21:52:17 +0000
parents f8c693d16978
children dc2fd8e6f42c
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", 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);
	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", 0, 0);
	if (sms_write_mode == 1)
		sendafterwr_process();
	exit(0);
}