FreeCalypso > hg > freecalypso-sw
annotate loadtools/tpinterf2.c @ 337:a26470040d89
fc-tmsh: old buglet in the handling of ETM traces
author | Michael Spacefalcon <msokolov@ivan.Harhan.ORG> |
---|---|
date | Tue, 22 Apr 2014 07:42:40 +0000 |
parents | 05af070c4b60 |
children |
rev | line source |
---|---|
35
05af070c4b60
loadtool: preparations for dump2bin and dump2srec
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff
changeset
|
1 /* |
05af070c4b60
loadtool: preparations for dump2bin and dump2srec
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff
changeset
|
2 * This module provides a more advanced target interface function |
05af070c4b60
loadtool: preparations for dump2bin and dump2srec
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff
changeset
|
3 * than tpinterf.c - programmatic capture of target responses, |
05af070c4b60
loadtool: preparations for dump2bin and dump2srec
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff
changeset
|
4 * for dumps etc. It will be linked by fc-loadtool, but not fc-chainload. |
05af070c4b60
loadtool: preparations for dump2bin and dump2srec
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff
changeset
|
5 */ |
05af070c4b60
loadtool: preparations for dump2bin and dump2srec
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff
changeset
|
6 |
05af070c4b60
loadtool: preparations for dump2bin and dump2srec
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff
changeset
|
7 #include <sys/types.h> |
05af070c4b60
loadtool: preparations for dump2bin and dump2srec
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff
changeset
|
8 #include <sys/time.h> |
05af070c4b60
loadtool: preparations for dump2bin and dump2srec
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff
changeset
|
9 #include <sys/errno.h> |
05af070c4b60
loadtool: preparations for dump2bin and dump2srec
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff
changeset
|
10 #include <stdio.h> |
05af070c4b60
loadtool: preparations for dump2bin and dump2srec
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff
changeset
|
11 #include <string.h> |
05af070c4b60
loadtool: preparations for dump2bin and dump2srec
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff
changeset
|
12 #include <strings.h> |
05af070c4b60
loadtool: preparations for dump2bin and dump2srec
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff
changeset
|
13 #include <stdlib.h> |
05af070c4b60
loadtool: preparations for dump2bin and dump2srec
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff
changeset
|
14 |
05af070c4b60
loadtool: preparations for dump2bin and dump2srec
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff
changeset
|
15 extern int errno; |
05af070c4b60
loadtool: preparations for dump2bin and dump2srec
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff
changeset
|
16 |
05af070c4b60
loadtool: preparations for dump2bin and dump2srec
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff
changeset
|
17 extern int target_fd; |
05af070c4b60
loadtool: preparations for dump2bin and dump2srec
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff
changeset
|
18 |
05af070c4b60
loadtool: preparations for dump2bin and dump2srec
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff
changeset
|
19 /* |
05af070c4b60
loadtool: preparations for dump2bin and dump2srec
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff
changeset
|
20 * This functions reads the serial output from the target until a |
05af070c4b60
loadtool: preparations for dump2bin and dump2srec
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff
changeset
|
21 * '=' prompt is received. All intermediate output is parsed into |
05af070c4b60
loadtool: preparations for dump2bin and dump2srec
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff
changeset
|
22 * lines and passed to a callback function. |
05af070c4b60
loadtool: preparations for dump2bin and dump2srec
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff
changeset
|
23 * |
05af070c4b60
loadtool: preparations for dump2bin and dump2srec
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff
changeset
|
24 * The callback function is called with a pointer to each received |
05af070c4b60
loadtool: preparations for dump2bin and dump2srec
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff
changeset
|
25 * line, stored in a buffer ending in NUL, with CRLF stripped. |
05af070c4b60
loadtool: preparations for dump2bin and dump2srec
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff
changeset
|
26 * The callback function is expected to return an int. If the |
05af070c4b60
loadtool: preparations for dump2bin and dump2srec
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff
changeset
|
27 * callback return value is negative, this function returns immediately |
05af070c4b60
loadtool: preparations for dump2bin and dump2srec
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff
changeset
|
28 * with that negative value. If the callback return value is positive |
05af070c4b60
loadtool: preparations for dump2bin and dump2srec
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff
changeset
|
29 * or zero, it is added to an accumulator. |
05af070c4b60
loadtool: preparations for dump2bin and dump2srec
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff
changeset
|
30 * |
05af070c4b60
loadtool: preparations for dump2bin and dump2srec
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff
changeset
|
31 * Termination: this function returns when it has received a '=' at |
05af070c4b60
loadtool: preparations for dump2bin and dump2srec
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff
changeset
|
32 * the beginning of a line (return value is the callback return |
05af070c4b60
loadtool: preparations for dump2bin and dump2srec
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff
changeset
|
33 * accumulator, or 0 if no lines came), if the callback returns a |
05af070c4b60
loadtool: preparations for dump2bin and dump2srec
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff
changeset
|
34 * negative value (that value is returned), or if an error is detected |
05af070c4b60
loadtool: preparations for dump2bin and dump2srec
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff
changeset
|
35 * within this function (return value -1, and an error message |
05af070c4b60
loadtool: preparations for dump2bin and dump2srec
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff
changeset
|
36 * printed on stderr). |
05af070c4b60
loadtool: preparations for dump2bin and dump2srec
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff
changeset
|
37 */ |
05af070c4b60
loadtool: preparations for dump2bin and dump2srec
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff
changeset
|
38 tpinterf_capture_output(timeout, callback) |
05af070c4b60
loadtool: preparations for dump2bin and dump2srec
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff
changeset
|
39 int timeout; /* seconds */ |
05af070c4b60
loadtool: preparations for dump2bin and dump2srec
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff
changeset
|
40 int (*callback)(); |
05af070c4b60
loadtool: preparations for dump2bin and dump2srec
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff
changeset
|
41 { |
05af070c4b60
loadtool: preparations for dump2bin and dump2srec
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff
changeset
|
42 char buf[512], *cp; |
05af070c4b60
loadtool: preparations for dump2bin and dump2srec
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff
changeset
|
43 char line[1024], *dp = line; |
05af070c4b60
loadtool: preparations for dump2bin and dump2srec
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff
changeset
|
44 fd_set fds; |
05af070c4b60
loadtool: preparations for dump2bin and dump2srec
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff
changeset
|
45 struct timeval tv; |
05af070c4b60
loadtool: preparations for dump2bin and dump2srec
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff
changeset
|
46 int cc, linelen = 0; |
05af070c4b60
loadtool: preparations for dump2bin and dump2srec
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff
changeset
|
47 int totout = 0, cbret; |
05af070c4b60
loadtool: preparations for dump2bin and dump2srec
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff
changeset
|
48 |
05af070c4b60
loadtool: preparations for dump2bin and dump2srec
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff
changeset
|
49 for (;;) { |
05af070c4b60
loadtool: preparations for dump2bin and dump2srec
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff
changeset
|
50 FD_ZERO(&fds); |
05af070c4b60
loadtool: preparations for dump2bin and dump2srec
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff
changeset
|
51 FD_SET(target_fd, &fds); |
05af070c4b60
loadtool: preparations for dump2bin and dump2srec
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff
changeset
|
52 tv.tv_sec = timeout; |
05af070c4b60
loadtool: preparations for dump2bin and dump2srec
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff
changeset
|
53 tv.tv_usec = 0; |
05af070c4b60
loadtool: preparations for dump2bin and dump2srec
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff
changeset
|
54 cc = select(target_fd+1, &fds, NULL, NULL, &tv); |
05af070c4b60
loadtool: preparations for dump2bin and dump2srec
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff
changeset
|
55 if (cc < 0) { |
05af070c4b60
loadtool: preparations for dump2bin and dump2srec
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff
changeset
|
56 if (errno == EINTR) |
05af070c4b60
loadtool: preparations for dump2bin and dump2srec
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff
changeset
|
57 continue; |
05af070c4b60
loadtool: preparations for dump2bin and dump2srec
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff
changeset
|
58 perror("select"); |
05af070c4b60
loadtool: preparations for dump2bin and dump2srec
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff
changeset
|
59 return(-1); |
05af070c4b60
loadtool: preparations for dump2bin and dump2srec
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff
changeset
|
60 } |
05af070c4b60
loadtool: preparations for dump2bin and dump2srec
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff
changeset
|
61 if (cc < 1) { |
05af070c4b60
loadtool: preparations for dump2bin and dump2srec
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff
changeset
|
62 fprintf(stderr, |
05af070c4b60
loadtool: preparations for dump2bin and dump2srec
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff
changeset
|
63 "error: timeout waiting for \'=\' prompt from target\n"); |
05af070c4b60
loadtool: preparations for dump2bin and dump2srec
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff
changeset
|
64 return(-1); |
05af070c4b60
loadtool: preparations for dump2bin and dump2srec
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff
changeset
|
65 } |
05af070c4b60
loadtool: preparations for dump2bin and dump2srec
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff
changeset
|
66 cc = read(target_fd, buf, sizeof buf); |
05af070c4b60
loadtool: preparations for dump2bin and dump2srec
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff
changeset
|
67 if (cc <= 0) { |
05af070c4b60
loadtool: preparations for dump2bin and dump2srec
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff
changeset
|
68 perror("read after successful select"); |
05af070c4b60
loadtool: preparations for dump2bin and dump2srec
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff
changeset
|
69 return(-1); |
05af070c4b60
loadtool: preparations for dump2bin and dump2srec
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff
changeset
|
70 } |
05af070c4b60
loadtool: preparations for dump2bin and dump2srec
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff
changeset
|
71 for (cp = buf; cc; cp++) { |
05af070c4b60
loadtool: preparations for dump2bin and dump2srec
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff
changeset
|
72 cc--; |
05af070c4b60
loadtool: preparations for dump2bin and dump2srec
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff
changeset
|
73 if (*cp == '=' && !linelen && !cc) |
05af070c4b60
loadtool: preparations for dump2bin and dump2srec
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff
changeset
|
74 return(totout); |
05af070c4b60
loadtool: preparations for dump2bin and dump2srec
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff
changeset
|
75 if (*cp == '\r') |
05af070c4b60
loadtool: preparations for dump2bin and dump2srec
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff
changeset
|
76 continue; |
05af070c4b60
loadtool: preparations for dump2bin and dump2srec
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff
changeset
|
77 if (*cp == '\n') { |
05af070c4b60
loadtool: preparations for dump2bin and dump2srec
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff
changeset
|
78 *dp = '\0'; |
05af070c4b60
loadtool: preparations for dump2bin and dump2srec
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff
changeset
|
79 cbret = callback(line); |
05af070c4b60
loadtool: preparations for dump2bin and dump2srec
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff
changeset
|
80 if (cbret < 0) |
05af070c4b60
loadtool: preparations for dump2bin and dump2srec
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff
changeset
|
81 return(cbret); |
05af070c4b60
loadtool: preparations for dump2bin and dump2srec
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff
changeset
|
82 totout += cbret; |
05af070c4b60
loadtool: preparations for dump2bin and dump2srec
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff
changeset
|
83 dp = line; |
05af070c4b60
loadtool: preparations for dump2bin and dump2srec
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff
changeset
|
84 linelen = 0; |
05af070c4b60
loadtool: preparations for dump2bin and dump2srec
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff
changeset
|
85 continue; |
05af070c4b60
loadtool: preparations for dump2bin and dump2srec
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff
changeset
|
86 } |
05af070c4b60
loadtool: preparations for dump2bin and dump2srec
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff
changeset
|
87 *dp++ = *cp; |
05af070c4b60
loadtool: preparations for dump2bin and dump2srec
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff
changeset
|
88 linelen++; |
05af070c4b60
loadtool: preparations for dump2bin and dump2srec
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff
changeset
|
89 if (linelen >= sizeof line) { |
05af070c4b60
loadtool: preparations for dump2bin and dump2srec
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff
changeset
|
90 fprintf(stderr, |
05af070c4b60
loadtool: preparations for dump2bin and dump2srec
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff
changeset
|
91 "error: target response line length exceeds buffer\n"); |
05af070c4b60
loadtool: preparations for dump2bin and dump2srec
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff
changeset
|
92 return(-1); |
05af070c4b60
loadtool: preparations for dump2bin and dump2srec
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff
changeset
|
93 } |
05af070c4b60
loadtool: preparations for dump2bin and dump2srec
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff
changeset
|
94 } |
05af070c4b60
loadtool: preparations for dump2bin and dump2srec
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff
changeset
|
95 } |
05af070c4b60
loadtool: preparations for dump2bin and dump2srec
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff
changeset
|
96 } |
05af070c4b60
loadtool: preparations for dump2bin and dump2srec
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff
changeset
|
97 |
05af070c4b60
loadtool: preparations for dump2bin and dump2srec
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff
changeset
|
98 /* single line response capture mechanism */ |
05af070c4b60
loadtool: preparations for dump2bin and dump2srec
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff
changeset
|
99 /* same line buffer size as in tpinterf_capture_output() */ |
05af070c4b60
loadtool: preparations for dump2bin and dump2srec
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff
changeset
|
100 char target_response_line[1024]; |
05af070c4b60
loadtool: preparations for dump2bin and dump2srec
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff
changeset
|
101 |
05af070c4b60
loadtool: preparations for dump2bin and dump2srec
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff
changeset
|
102 static |
05af070c4b60
loadtool: preparations for dump2bin and dump2srec
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff
changeset
|
103 oneline_catcher(linein) |
05af070c4b60
loadtool: preparations for dump2bin and dump2srec
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff
changeset
|
104 char *linein; |
05af070c4b60
loadtool: preparations for dump2bin and dump2srec
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff
changeset
|
105 { |
05af070c4b60
loadtool: preparations for dump2bin and dump2srec
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff
changeset
|
106 strcpy(target_response_line, linein); |
05af070c4b60
loadtool: preparations for dump2bin and dump2srec
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff
changeset
|
107 return(1); |
05af070c4b60
loadtool: preparations for dump2bin and dump2srec
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff
changeset
|
108 } |
05af070c4b60
loadtool: preparations for dump2bin and dump2srec
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff
changeset
|
109 |
05af070c4b60
loadtool: preparations for dump2bin and dump2srec
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff
changeset
|
110 tpinterf_capture_output_oneline(timeout) |
05af070c4b60
loadtool: preparations for dump2bin and dump2srec
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff
changeset
|
111 { |
05af070c4b60
loadtool: preparations for dump2bin and dump2srec
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff
changeset
|
112 return tpinterf_capture_output(timeout, oneline_catcher); |
05af070c4b60
loadtool: preparations for dump2bin and dump2srec
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff
changeset
|
113 } |