FreeCalypso > hg > freecalypso-tools
annotate loadtools/tpinterfb.c @ 682:a7496a1e0df7
doc/Flash-programming: typo fix
author | Mychaela Falconia <falcon@freecalypso.org> |
---|---|
date | Tue, 10 Mar 2020 04:10:05 +0000 |
parents | 141372e0d28f |
children | 8d7dcfd9df53 |
rev | line source |
---|---|
649
141372e0d28f
fc-loadtool module refactoring: tpinterfb.c split out
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
1 /* |
141372e0d28f
fc-loadtool module refactoring: tpinterfb.c split out
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
2 * This module contains functions for binary (as opposed to our usual ASCII) |
141372e0d28f
fc-loadtool module refactoring: tpinterfb.c split out
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
3 * interfacing to loadagent. |
141372e0d28f
fc-loadtool module refactoring: tpinterfb.c split out
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
4 */ |
141372e0d28f
fc-loadtool module refactoring: tpinterfb.c split out
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
5 |
141372e0d28f
fc-loadtool module refactoring: tpinterfb.c split out
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
6 #include <sys/types.h> |
141372e0d28f
fc-loadtool module refactoring: tpinterfb.c split out
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
7 #include <sys/time.h> |
141372e0d28f
fc-loadtool module refactoring: tpinterfb.c split out
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
8 #include <sys/errno.h> |
141372e0d28f
fc-loadtool module refactoring: tpinterfb.c split out
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
9 #include <stdio.h> |
141372e0d28f
fc-loadtool module refactoring: tpinterfb.c split out
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
10 #include <string.h> |
141372e0d28f
fc-loadtool module refactoring: tpinterfb.c split out
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
11 #include <strings.h> |
141372e0d28f
fc-loadtool module refactoring: tpinterfb.c split out
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
12 #include <stdlib.h> |
141372e0d28f
fc-loadtool module refactoring: tpinterfb.c split out
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
13 |
141372e0d28f
fc-loadtool module refactoring: tpinterfb.c split out
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
14 extern int errno; |
141372e0d28f
fc-loadtool module refactoring: tpinterfb.c split out
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
15 |
141372e0d28f
fc-loadtool module refactoring: tpinterfb.c split out
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
16 extern int target_fd; |
141372e0d28f
fc-loadtool module refactoring: tpinterfb.c split out
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
17 |
141372e0d28f
fc-loadtool module refactoring: tpinterfb.c split out
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
18 collect_binblock_from_target(buf, expect_len, timeout) |
141372e0d28f
fc-loadtool module refactoring: tpinterfb.c split out
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
19 u_char *buf; |
141372e0d28f
fc-loadtool module refactoring: tpinterfb.c split out
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
20 unsigned expect_len; |
141372e0d28f
fc-loadtool module refactoring: tpinterfb.c split out
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
21 { |
141372e0d28f
fc-loadtool module refactoring: tpinterfb.c split out
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
22 fd_set fds; |
141372e0d28f
fc-loadtool module refactoring: tpinterfb.c split out
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
23 struct timeval tv; |
141372e0d28f
fc-loadtool module refactoring: tpinterfb.c split out
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
24 unsigned rcvd; |
141372e0d28f
fc-loadtool module refactoring: tpinterfb.c split out
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
25 int cc; |
141372e0d28f
fc-loadtool module refactoring: tpinterfb.c split out
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
26 |
141372e0d28f
fc-loadtool module refactoring: tpinterfb.c split out
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
27 for (rcvd = 0; rcvd < expect_len; ) { |
141372e0d28f
fc-loadtool module refactoring: tpinterfb.c split out
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
28 FD_ZERO(&fds); |
141372e0d28f
fc-loadtool module refactoring: tpinterfb.c split out
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
29 FD_SET(target_fd, &fds); |
141372e0d28f
fc-loadtool module refactoring: tpinterfb.c split out
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
30 tv.tv_sec = timeout; |
141372e0d28f
fc-loadtool module refactoring: tpinterfb.c split out
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
31 tv.tv_usec = 0; |
141372e0d28f
fc-loadtool module refactoring: tpinterfb.c split out
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
32 cc = select(target_fd+1, &fds, NULL, NULL, &tv); |
141372e0d28f
fc-loadtool module refactoring: tpinterfb.c split out
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
33 if (cc < 0) { |
141372e0d28f
fc-loadtool module refactoring: tpinterfb.c split out
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
34 if (errno == EINTR) |
141372e0d28f
fc-loadtool module refactoring: tpinterfb.c split out
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
35 continue; |
141372e0d28f
fc-loadtool module refactoring: tpinterfb.c split out
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
36 perror("select"); |
141372e0d28f
fc-loadtool module refactoring: tpinterfb.c split out
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
37 return(-1); |
141372e0d28f
fc-loadtool module refactoring: tpinterfb.c split out
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
38 } |
141372e0d28f
fc-loadtool module refactoring: tpinterfb.c split out
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
39 if (cc < 1) { |
141372e0d28f
fc-loadtool module refactoring: tpinterfb.c split out
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
40 fprintf(stderr, |
141372e0d28f
fc-loadtool module refactoring: tpinterfb.c split out
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
41 "error: timeout waiting for binary block\n"); |
141372e0d28f
fc-loadtool module refactoring: tpinterfb.c split out
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
42 return(-1); |
141372e0d28f
fc-loadtool module refactoring: tpinterfb.c split out
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
43 } |
141372e0d28f
fc-loadtool module refactoring: tpinterfb.c split out
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
44 cc = read(target_fd, buf + rcvd, expect_len - rcvd); |
141372e0d28f
fc-loadtool module refactoring: tpinterfb.c split out
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
45 if (cc <= 0) { |
141372e0d28f
fc-loadtool module refactoring: tpinterfb.c split out
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
46 perror("read after successful select"); |
141372e0d28f
fc-loadtool module refactoring: tpinterfb.c split out
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
47 return(-1); |
141372e0d28f
fc-loadtool module refactoring: tpinterfb.c split out
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
48 } |
141372e0d28f
fc-loadtool module refactoring: tpinterfb.c split out
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
49 rcvd += cc; |
141372e0d28f
fc-loadtool module refactoring: tpinterfb.c split out
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
50 } |
141372e0d28f
fc-loadtool module refactoring: tpinterfb.c split out
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
51 return(0); |
141372e0d28f
fc-loadtool module refactoring: tpinterfb.c split out
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
52 } |