view serial/xmit.c @ 44:0bc89d61fc59

top Makefile: add serial
author Mychaela Falconia <falcon@freecalypso.org>
date Sat, 20 Mar 2021 21:53:31 +0000
parents 5ee00413b8af
children
line wrap: on
line source

/*
 * This module implements the function for sending byte strings
 * to the SIM and collecting the UART echo.
 */

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

extern int target_fd;
extern int inverse_coding;

send_bytes_to_sim(data, nbytes)
	u_char *data;
	unsigned nbytes;
{
	u_char buf1[255], buf2[255];
	fd_set fds;
	struct timeval tv;
	unsigned rcvd;
	int cc;

	bcopy(data, buf1, nbytes);
	if (inverse_coding)
		invert_bytes(buf1, nbytes);
	write(target_fd, buf1, nbytes);
	for (rcvd = 0; rcvd < nbytes; ) {
		FD_ZERO(&fds);
		FD_SET(target_fd, &fds);
		tv.tv_sec = 1;
		tv.tv_usec = 0;
		cc = select(target_fd+1, &fds, NULL, NULL, &tv);
		if (cc < 0) {
			if (errno == EINTR)
				continue;
			perror("select");
			return(-1);
		}
		if (cc < 1) {
			fprintf(stderr,
			"error: timeout waiting for echo of Tx bytes\n");
			return(-1);
		}
		cc = read(target_fd, buf2 + rcvd, nbytes - rcvd);
		if (cc <= 0) {
			perror("read after successful select");
			return(-1);
		}
		rcvd += cc;
	}
	if (bcmp(buf1, buf2, nbytes)) {
		fprintf(stderr, "error: UART echo mismatch\n");
		return(-1);
	} else
		return(0);
}