FreeCalypso > hg > freecalypso-tools
annotate loadtools/buzplay.c @ 783:c136a1a2474b
simagent: initial implementation of APDU exchange
author | Mychaela Falconia <falcon@freecalypso.org> |
---|---|
date | Sat, 13 Mar 2021 22:06:08 +0000 |
parents | 684eddecbc62 |
children |
rev | line source |
---|---|
86
684eddecbc62
fc-buzplay play command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
1 /* |
684eddecbc62
fc-buzplay play command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
2 * The actual functionality of fc-buzplay is implemented here. |
684eddecbc62
fc-buzplay play command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
3 */ |
684eddecbc62
fc-buzplay play command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
4 |
684eddecbc62
fc-buzplay play command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
5 #include <sys/types.h> |
684eddecbc62
fc-buzplay play command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
6 #include <ctype.h> |
684eddecbc62
fc-buzplay play command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
7 #include <stdio.h> |
684eddecbc62
fc-buzplay play command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
8 #include <string.h> |
684eddecbc62
fc-buzplay play command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
9 #include <strings.h> |
684eddecbc62
fc-buzplay play command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
10 #include <stdlib.h> |
684eddecbc62
fc-buzplay play command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
11 |
684eddecbc62
fc-buzplay play command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
12 cmd_play(argc, argv) |
684eddecbc62
fc-buzplay play command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
13 char **argv; |
684eddecbc62
fc-buzplay play command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
14 { |
684eddecbc62
fc-buzplay play command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
15 FILE *f; |
684eddecbc62
fc-buzplay play command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
16 char linebuf[256], *cp, *num1, *num2; |
684eddecbc62
fc-buzplay play command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
17 int lineno; |
684eddecbc62
fc-buzplay play command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
18 char *targv[4]; |
684eddecbc62
fc-buzplay play command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
19 u_long n1, n2, total_ms; |
684eddecbc62
fc-buzplay play command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
20 int rc, timeout; |
684eddecbc62
fc-buzplay play command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
21 |
684eddecbc62
fc-buzplay play command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
22 f = fopen(argv[1], "r"); |
684eddecbc62
fc-buzplay play command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
23 if (!f) { |
684eddecbc62
fc-buzplay play command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
24 perror(argv[1]); |
684eddecbc62
fc-buzplay play command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
25 return(-1); |
684eddecbc62
fc-buzplay play command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
26 } |
684eddecbc62
fc-buzplay play command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
27 printf("Uploading the melody to the target\n"); |
684eddecbc62
fc-buzplay play command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
28 targv[0] = "I"; |
684eddecbc62
fc-buzplay play command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
29 targv[1] = 0; |
684eddecbc62
fc-buzplay play command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
30 tpinterf_make_cmd(targv); |
684eddecbc62
fc-buzplay play command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
31 if (tpinterf_send_cmd() < 0) { |
684eddecbc62
fc-buzplay play command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
32 fclose(f); |
684eddecbc62
fc-buzplay play command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
33 return(-1); |
684eddecbc62
fc-buzplay play command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
34 } |
684eddecbc62
fc-buzplay play command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
35 rc = tpinterf_pass_output(1); |
684eddecbc62
fc-buzplay play command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
36 if (rc) { |
684eddecbc62
fc-buzplay play command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
37 fclose(f); |
684eddecbc62
fc-buzplay play command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
38 return(rc); |
684eddecbc62
fc-buzplay play command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
39 } |
684eddecbc62
fc-buzplay play command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
40 targv[0] = "E"; |
684eddecbc62
fc-buzplay play command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
41 targv[3] = 0; |
684eddecbc62
fc-buzplay play command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
42 total_ms = 0; |
684eddecbc62
fc-buzplay play command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
43 for (lineno = 1; fgets(linebuf, sizeof linebuf, f); lineno++) { |
684eddecbc62
fc-buzplay play command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
44 cp = index(linebuf, '\n'); |
684eddecbc62
fc-buzplay play command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
45 if (!cp) { |
684eddecbc62
fc-buzplay play command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
46 fprintf(stderr, "%s line %d: missing newline\n", |
684eddecbc62
fc-buzplay play command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
47 argv[1], lineno); |
684eddecbc62
fc-buzplay play command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
48 fclose(f); |
684eddecbc62
fc-buzplay play command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
49 return(-1); |
684eddecbc62
fc-buzplay play command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
50 } |
684eddecbc62
fc-buzplay play command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
51 for (cp = linebuf; isspace(*cp); cp++) |
684eddecbc62
fc-buzplay play command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
52 ; |
684eddecbc62
fc-buzplay play command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
53 if (*cp == '\0' || *cp == '#') |
684eddecbc62
fc-buzplay play command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
54 continue; |
684eddecbc62
fc-buzplay play command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
55 if (!isdigit(*cp)) { |
684eddecbc62
fc-buzplay play command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
56 inv: fprintf(stderr, "%s line %d: unexpected content\n", |
684eddecbc62
fc-buzplay play command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
57 argv[1], lineno); |
684eddecbc62
fc-buzplay play command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
58 fclose(f); |
684eddecbc62
fc-buzplay play command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
59 return(-1); |
684eddecbc62
fc-buzplay play command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
60 } |
684eddecbc62
fc-buzplay play command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
61 for (num1 = cp; *cp && !isspace(*cp); cp++) |
684eddecbc62
fc-buzplay play command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
62 if (!isdigit(*cp)) |
684eddecbc62
fc-buzplay play command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
63 goto inv; |
684eddecbc62
fc-buzplay play command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
64 if (isspace(*cp)) |
684eddecbc62
fc-buzplay play command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
65 *cp++ = '\0'; |
684eddecbc62
fc-buzplay play command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
66 while (isspace(*cp)) |
684eddecbc62
fc-buzplay play command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
67 cp++; |
684eddecbc62
fc-buzplay play command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
68 if (!isdigit(*cp)) |
684eddecbc62
fc-buzplay play command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
69 goto inv; |
684eddecbc62
fc-buzplay play command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
70 for (num2 = cp; *cp && !isspace(*cp); cp++) |
684eddecbc62
fc-buzplay play command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
71 if (!isdigit(*cp)) |
684eddecbc62
fc-buzplay play command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
72 goto inv; |
684eddecbc62
fc-buzplay play command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
73 if (isspace(*cp)) |
684eddecbc62
fc-buzplay play command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
74 *cp++ = '\0'; |
684eddecbc62
fc-buzplay play command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
75 while (isspace(*cp)) |
684eddecbc62
fc-buzplay play command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
76 cp++; |
684eddecbc62
fc-buzplay play command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
77 if (*cp != '\0' && *cp != '#') |
684eddecbc62
fc-buzplay play command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
78 goto inv; |
684eddecbc62
fc-buzplay play command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
79 n1 = strtoul(num1, 0, 10); |
684eddecbc62
fc-buzplay play command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
80 n2 = strtoul(num2, 0, 10); |
684eddecbc62
fc-buzplay play command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
81 if (n1 > 255) { |
684eddecbc62
fc-buzplay play command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
82 fprintf(stderr, |
684eddecbc62
fc-buzplay play command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
83 "%s line %d: the tone number is out of range\n", |
684eddecbc62
fc-buzplay play command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
84 argv[1], lineno); |
684eddecbc62
fc-buzplay play command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
85 fclose(f); |
684eddecbc62
fc-buzplay play command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
86 return(-1); |
684eddecbc62
fc-buzplay play command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
87 } |
684eddecbc62
fc-buzplay play command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
88 if (n2 < 1 || n2 > 0xFFFF) { |
684eddecbc62
fc-buzplay play command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
89 fprintf(stderr, |
684eddecbc62
fc-buzplay play command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
90 "%s line %d: the duration number is out of range\n", |
684eddecbc62
fc-buzplay play command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
91 argv[1], lineno); |
684eddecbc62
fc-buzplay play command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
92 fclose(f); |
684eddecbc62
fc-buzplay play command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
93 return(-1); |
684eddecbc62
fc-buzplay play command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
94 } |
684eddecbc62
fc-buzplay play command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
95 /* send it to the target */ |
684eddecbc62
fc-buzplay play command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
96 targv[1] = num1; |
684eddecbc62
fc-buzplay play command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
97 targv[2] = num2; |
684eddecbc62
fc-buzplay play command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
98 tpinterf_make_cmd(targv); |
684eddecbc62
fc-buzplay play command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
99 if (tpinterf_send_cmd() < 0) { |
684eddecbc62
fc-buzplay play command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
100 fclose(f); |
684eddecbc62
fc-buzplay play command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
101 return(-1); |
684eddecbc62
fc-buzplay play command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
102 } |
684eddecbc62
fc-buzplay play command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
103 rc = tpinterf_pass_output(1); |
684eddecbc62
fc-buzplay play command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
104 if (rc) { |
684eddecbc62
fc-buzplay play command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
105 fclose(f); |
684eddecbc62
fc-buzplay play command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
106 return(rc); |
684eddecbc62
fc-buzplay play command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
107 } |
684eddecbc62
fc-buzplay play command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
108 /* account for the duration */ |
684eddecbc62
fc-buzplay play command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
109 total_ms += n2 * 5; |
684eddecbc62
fc-buzplay play command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
110 } |
684eddecbc62
fc-buzplay play command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
111 fclose(f); |
684eddecbc62
fc-buzplay play command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
112 if (!total_ms) { |
684eddecbc62
fc-buzplay play command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
113 fprintf(stderr, "%s is empty!\n", argv[1]); |
684eddecbc62
fc-buzplay play command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
114 return(-1); |
684eddecbc62
fc-buzplay play command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
115 } |
684eddecbc62
fc-buzplay play command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
116 printf("Requesting play of the uploaded melody on the target\n"); |
684eddecbc62
fc-buzplay play command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
117 targv[0] = "P"; |
684eddecbc62
fc-buzplay play command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
118 targv[1] = 0; |
684eddecbc62
fc-buzplay play command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
119 tpinterf_make_cmd(targv); |
684eddecbc62
fc-buzplay play command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
120 if (tpinterf_send_cmd() < 0) |
684eddecbc62
fc-buzplay play command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
121 return(-1); |
684eddecbc62
fc-buzplay play command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
122 timeout = total_ms / 1000 + 2; |
684eddecbc62
fc-buzplay play command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
123 return tpinterf_pass_output(timeout); |
684eddecbc62
fc-buzplay play command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
124 } |