comparison serial/xmit.c @ 41:5ee00413b8af

serial: beginning of fcsim-serial-be
author Mychaela Falconia <falcon@freecalypso.org>
date Sat, 20 Mar 2021 20:53:51 +0000
parents
children
comparison
equal deleted inserted replaced
40:8f505d413815 41:5ee00413b8af
1 /*
2 * This module implements the function for sending byte strings
3 * to the SIM and collecting the UART echo.
4 */
5
6 #include <sys/types.h>
7 #include <sys/time.h>
8 #include <sys/errno.h>
9 #include <stdio.h>
10 #include <stdlib.h>
11 #include <string.h>
12 #include <strings.h>
13 #include <unistd.h>
14
15 extern int target_fd;
16 extern int inverse_coding;
17
18 send_bytes_to_sim(data, nbytes)
19 u_char *data;
20 unsigned nbytes;
21 {
22 u_char buf1[255], buf2[255];
23 fd_set fds;
24 struct timeval tv;
25 unsigned rcvd;
26 int cc;
27
28 bcopy(data, buf1, nbytes);
29 if (inverse_coding)
30 invert_bytes(buf1, nbytes);
31 write(target_fd, buf1, nbytes);
32 for (rcvd = 0; rcvd < nbytes; ) {
33 FD_ZERO(&fds);
34 FD_SET(target_fd, &fds);
35 tv.tv_sec = 1;
36 tv.tv_usec = 0;
37 cc = select(target_fd+1, &fds, NULL, NULL, &tv);
38 if (cc < 0) {
39 if (errno == EINTR)
40 continue;
41 perror("select");
42 return(-1);
43 }
44 if (cc < 1) {
45 fprintf(stderr,
46 "error: timeout waiting for echo of Tx bytes\n");
47 return(-1);
48 }
49 cc = read(target_fd, buf2 + rcvd, nbytes - rcvd);
50 if (cc <= 0) {
51 perror("read after successful select");
52 return(-1);
53 }
54 rcvd += cc;
55 }
56 if (bcmp(buf1, buf2, nbytes)) {
57 fprintf(stderr, "error: UART echo mismatch\n");
58 return(-1);
59 } else
60 return(0);
61 }