view uptools/atinterf/fcup-atinterf.c @ 341:692dbc4c2f07

fcup-atinterf program written, compiles
author Mychaela Falconia <falcon@freecalypso.org>
date Sun, 04 Feb 2018 07:56:21 +0000
parents
children e0260c2982b6
line wrap: on
line source

#include <sys/types.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <strings.h>
#include <unistd.h>

extern int target_fd;

int wakeup_after_sec = 7;

FILE *target_rd;
char response[515];

char command[513], message[513];
int cmd_with_msg;

read_command_input(buf)
	char *buf;
{
	char *nl;

	if (!fgets(buf, 513, stdin))
		return(0);
	nl = index(buf, '\n');
	if (!nl) {
		printf("Ecommand or message is too long\n");
		exit(1);
	}
	*nl = '\0';
	return(1);
}

send_to_target(cmd, term)
	char *cmd;
{
	char *endp;
	int len, cc;

	endp = index(cmd, '\0');
	*endp = term;
	len = endp - cmd + 1;
	cc = write(target_fd, cmd, len);
	*endp = '\0';
	if (cc != len) {
		printf("Etarget write error\n");
		exit(1);
	}
}

collect_target_response()
{
	char *nl;

	if (!fgets(response, 515, target_rd)) {
		printf("Etarget read error\n");
		exit(1);
	}
	nl = index(response, '\n');
	if (!nl) {
		printf("Etarget response is too long\n");
		exit(1);
	}
	while (nl > response && nl[-1] == '\r')
		nl--;
	*nl = '\0';
}

execute_command()
{
	int c;

	send_to_target(command, '\r');
	collect_target_response();
	if (strcmp(command, response)) {
		printf("Efailed to get echo of command\n");
		exit(1);
	}
	if (cmd_with_msg) {
		if ((c = getc(target_rd)) != '>') {
			ungetc(c, target_rd);
			collect_target_response();
			printf("F%s\n", response);
			return;
		}
		if ((c = getc(target_rd)) != ' ') {
			ungetc(c, target_rd);
			collect_target_response();
			printf("F%s\n", response);
			return;
		}
		send_to_target(message, '\033');
		collect_target_response();
		if (strcmp(message, response)) {
			printf("Efailed to get echo of message\n");
			exit(1);
		}
	}
	for (;;) {
		collect_target_response();
		if (!strcmp(response, "OK") || !strcmp(response, "ERROR") ||
		    !strcmp(response, "BUSY") ||
		    !strcmp(response, "NO CARRIER") ||
		    !strncmp(response, "+CME ERROR", 10) ||
		    !strncmp(response, "+CMS ERROR", 10)) {
			printf("F%s\n", response);
			return;
		}
		printf("I%s\n", response);
	}
}

main(argc, argv)
	char **argv;
{
	if (argc < 3 || argc > 4) {
		fprintf(stderr,
			"usage: %s ttyport baudrate [wakeup-after-sec]\n",
			argv[0]);
		exit(1);
	}
	open_serial_port(argv[1]);
	set_fixed_baudrate(argv[2]);
	set_serial_nonblock(0);
	if (argc > 3)
		wakeup_after_sec = strtoul(argv[3], 0, 0);

	target_rd = fdopen(target_fd, "r");
	if (!target_rd) {
		perror("fdopen");
		exit(1);
	}

	while (read_command_input(command)) {
		if (!strcasecmp(command, "c+m")) {
			cmd_with_msg = 1;
			if (!read_command_input(command))
				break;
			if (!read_command_input(message))
				break;
		} else
			cmd_with_msg = 0;
		execute_command();
		fflush(stdout);
	}
	exit(0);
}