]>
Commit | Line | Data |
---|---|---|
037166c1 MG |
1 | /* |
2 | Sprite_tms hack to communicate with a Rigol DS1000-series scope using Linux. | |
3 | This code is licensed under the GPL V3. | |
4 | ||
5 | Warning: This code can in theory fubar the communications with the scope to a | |
6 | point where the Linux USB-stack seems to get confused. Do a | |
7 | rmmod uhci_hcd; modprobe uhci_hcd | |
8 | (or alternately: use ohci_hcd) if that happens and you should be fine. | |
610d5b65 | 9 | */ |
037166c1 | 10 | |
037166c1 MG |
11 | #include <stdlib.h> |
12 | #include <stdio.h> | |
13 | #include <string.h> | |
14 | #include <sys/stat.h> | |
a8982973 MG |
15 | #include <sys/types.h> |
16 | #include <sys/wait.h> | |
037166c1 MG |
17 | #include <fcntl.h> |
18 | #include <unistd.h> | |
a8982973 MG |
19 | #include <signal.h> |
20 | #include <time.h> | |
037166c1 MG |
21 | |
22 | #include <readline/readline.h> | |
23 | #include <readline/history.h> | |
24 | ||
713be7a4 | 25 | #include "scope.h" |
0b7a29d9 | 26 | #include "commands.h" |
037166c1 | 27 | |
037166c1 MG |
28 | #define MIN(a,b) (((a)<(b))?(a):(b)) |
29 | ||
30 | inline char printable (char ch) | |
31 | { | |
610d5b65 MG |
32 | if (ch < ' ') return '.'; |
33 | if (ch > '~') return '.'; | |
34 | return ch; | |
037166c1 MG |
35 | } |
36 | ||
37 | //Debugging: Print a buffers contents in hex | |
38 | void printb (unsigned char *pkt, int len) | |
39 | { | |
610d5b65 MG |
40 | int i, j; |
41 | ||
42 | for (i=0;i<len;i+= 16) { | |
43 | printf ("%04x: ", i); | |
44 | for (j=0;j < MIN(len-i, 16);j++) { | |
45 | printf (" %02x", pkt[i+j]); | |
46 | if (j == 7) printf (" "); | |
47 | } | |
48 | if (j < 7) printf (" "); | |
49 | for (;j<17;j++) | |
50 | printf (" "); | |
51 | ||
52 | for (j=0;j < MIN(len-i, 16);j++) { | |
53 | printf ("%c", printable (pkt[i+j])); | |
54 | } | |
55 | printf ("\n"); | |
56 | } | |
037166c1 MG |
57 | } |
58 | ||
037166c1 MG |
59 | #define HAVE_READLINE |
60 | #ifndef HAVE_READLINE | |
61 | ||
62 | char *readline (prompt) | |
63 | { | |
610d5b65 MG |
64 | char *buf; |
65 | ||
66 | printf (prompt); | |
67 | fflush (stdout); | |
68 | buf = malloc (1024); | |
69 | ||
70 | if (fgets (buf, 1023, stdin) == NULL) { | |
71 | free (buf); | |
72 | return NULL; | |
73 | } | |
74 | l = strlen (buf); | |
75 | if (buf[l-1] == '\n') { | |
76 | buf[l] = 0; | |
77 | } | |
78 | return buf; | |
037166c1 MG |
79 | } |
80 | ||
81 | void add_history (char *buf) | |
82 | { | |
83 | } | |
84 | #endif | |
85 | ||
a8982973 MG |
86 | void child_reaper(int sig) |
87 | { | |
88 | pid_t child; | |
89 | ||
90 | do { | |
91 | child = waitpid(-1, NULL, WNOHANG); | |
92 | } while(child > 0); | |
93 | ||
94 | } | |
037166c1 MG |
95 | |
96 | int main(int argc, char **argv) | |
97 | { | |
713be7a4 | 98 | struct scope *sc; |
610d5b65 MG |
99 | char *scpi; |
100 | unsigned char *buff; | |
101 | int l; | |
a8982973 MG |
102 | struct sigaction act; |
103 | ||
610d5b65 | 104 | //Initialize scope |
713be7a4 MG |
105 | sc = initscope(); |
106 | claimscope(sc); | |
610d5b65 | 107 | buff = malloc (1024*1024); |
a8982973 MG |
108 | if (buff == NULL) { |
109 | perror("malloc"); | |
110 | exit(EXIT_FAILURE); | |
111 | } | |
112 | ||
113 | bzero(&act, sizeof(act)); | |
114 | act.sa_handler = child_reaper; | |
115 | act.sa_flags = SA_NOCLDSTOP|SA_RESTART; | |
116 | if (sigaction(SIGCHLD, &act, NULL) == -1) { | |
117 | perror("sigaction"); | |
118 | exit(EXIT_FAILURE); | |
119 | } | |
120 | ||
610d5b65 MG |
121 | while (1) { |
122 | scpi = readline ("> "); | |
123 | ||
124 | if (!scpi) break; | |
125 | if (strlen (scpi) == 0) { | |
126 | free (scpi); | |
127 | continue; | |
128 | } | |
129 | ||
130 | add_history (scpi); | |
131 | ||
132 | if (strncmp (scpi, "quit", 4) == 0) break; | |
133 | if (strncmp (scpi, "plot", 4) == 0) { | |
134 | do_plot (sc); | |
135 | continue; | |
136 | } | |
a8982973 | 137 | if (strncmp (scpi, "databuf", 7) == 0) { |
616d54b7 | 138 | do_get_buf(sc); |
610d5b65 MG |
139 | continue; |
140 | } | |
a8982973 | 141 | if (strncmp (scpi, "screen", 6) == 0) { |
616d54b7 MG |
142 | do_get_screen(sc); |
143 | continue; | |
144 | } | |
145 | if (strncmp (scpi, "display", 7) == 0) { | |
146 | do_display_screen(sc); | |
a8982973 MG |
147 | continue; |
148 | } | |
610d5b65 MG |
149 | |
150 | l = strlen (scpi); | |
151 | //printf ("got buf(%d): ", l); | |
152 | //printb (scpi, l+2); | |
153 | if (strchr (scpi, '?')) { | |
154 | //printf ("Expecting reply\n"); | |
713be7a4 | 155 | l = sendscpi(sc, scpi, buff, 1024*1024); |
610d5b65 MG |
156 | //printf ("Got replylen = %d.\n", l); |
157 | buff[l] = 0; //zero-terminate | |
158 | printb (buff, l); | |
159 | } else { | |
160 | //printf ("No reply expected\n"); | |
713be7a4 | 161 | l=sendscpi(sc,scpi,NULL,0); |
610d5b65 MG |
162 | } |
163 | free (scpi); | |
164 | } | |
713be7a4 MG |
165 | releasescope(sc); |
166 | closescope(sc); | |
610d5b65 | 167 | return 0; |
037166c1 | 168 | } |