FreeCalypso > hg > freecalypso-tools
annotate miscutil/fc-tch2fr.c @ 689:730a9f919dcd
fc-loadtool flash erase-program-boot fix to not stop scripts
| author | Mychaela Falconia <falcon@freecalypso.org> | 
|---|---|
| date | Wed, 11 Mar 2020 00:48:15 +0000 | 
| parents | d57f68d0568d | 
| children | 
| rev | line source | 
|---|---|
| 
6
 
d57f68d0568d
fc-tch2fr utility written, added under miscutil
 
Mychaela Falconia <falcon@freecalypso.org> 
parents:  
diff
changeset
 | 
1 /* | 
| 
 
d57f68d0568d
fc-tch2fr utility written, added under miscutil
 
Mychaela Falconia <falcon@freecalypso.org> 
parents:  
diff
changeset
 | 
2 * Our experimental Calypso firmware enables us to capture the output of | 
| 
 
d57f68d0568d
fc-tch2fr utility written, added under miscutil
 
Mychaela Falconia <falcon@freecalypso.org> 
parents:  
diff
changeset
 | 
3 * the GSM 05.03 channel decoder in the DSP, i.e., the bits leaving the | 
| 
 
d57f68d0568d
fc-tch2fr utility written, added under miscutil
 
Mychaela Falconia <falcon@freecalypso.org> 
parents:  
diff
changeset
 | 
4 * channel decoder and going into the speech decoder. Our fc-shell utility | 
| 
 
d57f68d0568d
fc-tch2fr utility written, added under miscutil
 
Mychaela Falconia <falcon@freecalypso.org> 
parents:  
diff
changeset
 | 
5 * allows saving this capture to a file; the captured booty includes not only | 
| 
 
d57f68d0568d
fc-tch2fr utility written, added under miscutil
 
Mychaela Falconia <falcon@freecalypso.org> 
parents:  
diff
changeset
 | 
6 * the expected 260 bits per frame, but also some DSP status words which are | 
| 
 
d57f68d0568d
fc-tch2fr utility written, added under miscutil
 
Mychaela Falconia <falcon@freecalypso.org> 
parents:  
diff
changeset
 | 
7 * not fully understood, but which are believed to contain indications as to | 
| 
 
d57f68d0568d
fc-tch2fr utility written, added under miscutil
 
Mychaela Falconia <falcon@freecalypso.org> 
parents:  
diff
changeset
 | 
8 * whether the decoded speech frame is good or bad. | 
| 
 
d57f68d0568d
fc-tch2fr utility written, added under miscutil
 
Mychaela Falconia <falcon@freecalypso.org> 
parents:  
diff
changeset
 | 
9 * | 
| 
 
d57f68d0568d
fc-tch2fr utility written, added under miscutil
 
Mychaela Falconia <falcon@freecalypso.org> 
parents:  
diff
changeset
 | 
10 * My first naive thought was to save the captured speech frames in libgsm | 
| 
 
d57f68d0568d
fc-tch2fr utility written, added under miscutil
 
Mychaela Falconia <falcon@freecalypso.org> 
parents:  
diff
changeset
 | 
11 * format so I could then play them with the 'play' command (SoX package) | 
| 
 
d57f68d0568d
fc-tch2fr utility written, added under miscutil
 
Mychaela Falconia <falcon@freecalypso.org> 
parents:  
diff
changeset
 | 
12 * under Linux, but the problem with this naive approach is that the bad frames | 
| 
 
d57f68d0568d
fc-tch2fr utility written, added under miscutil
 
Mychaela Falconia <falcon@freecalypso.org> 
parents:  
diff
changeset
 | 
13 * indication is lost, and some of the saved "speech" frames will contain | 
| 
 
d57f68d0568d
fc-tch2fr utility written, added under miscutil
 
Mychaela Falconia <falcon@freecalypso.org> 
parents:  
diff
changeset
 | 
14 * utter garbage, resulting in very unkind-on-ears noises if that file is | 
| 
 
d57f68d0568d
fc-tch2fr utility written, added under miscutil
 
Mychaela Falconia <falcon@freecalypso.org> 
parents:  
diff
changeset
 | 
15 * then played. I don't know what the proper solution should be; I don't know | 
| 
 
d57f68d0568d
fc-tch2fr utility written, added under miscutil
 
Mychaela Falconia <falcon@freecalypso.org> 
parents:  
diff
changeset
 | 
16 * what the commercial cellphone implementations of the GSM 06.10 speech decoder | 
| 
 
d57f68d0568d
fc-tch2fr utility written, added under miscutil
 
Mychaela Falconia <falcon@freecalypso.org> 
parents:  
diff
changeset
 | 
17 * (buried in black box DSPs) do when they get bad frames from the channel | 
| 
 
d57f68d0568d
fc-tch2fr utility written, added under miscutil
 
Mychaela Falconia <falcon@freecalypso.org> 
parents:  
diff
changeset
 | 
18 * decoder. | 
| 
 
d57f68d0568d
fc-tch2fr utility written, added under miscutil
 
Mychaela Falconia <falcon@freecalypso.org> 
parents:  
diff
changeset
 | 
19 * | 
| 
 
d57f68d0568d
fc-tch2fr utility written, added under miscutil
 
Mychaela Falconia <falcon@freecalypso.org> 
parents:  
diff
changeset
 | 
20 * The present utility reproduces the naive behaviour of my previous | 
| 
 
d57f68d0568d
fc-tch2fr utility written, added under miscutil
 
Mychaela Falconia <falcon@freecalypso.org> 
parents:  
diff
changeset
 | 
21 * implementation of fc-shell's tch record command: it takes hex files written | 
| 
 
d57f68d0568d
fc-tch2fr utility written, added under miscutil
 
Mychaela Falconia <falcon@freecalypso.org> 
parents:  
diff
changeset
 | 
22 * by the current implementation of tch record in fc-shell, DISREGARDS the | 
| 
 
d57f68d0568d
fc-tch2fr utility written, added under miscutil
 
Mychaela Falconia <falcon@freecalypso.org> 
parents:  
diff
changeset
 | 
23 * DSP status words, and blindly converts each 260-bit frame (good or bad) | 
| 
 
d57f68d0568d
fc-tch2fr utility written, added under miscutil
 
Mychaela Falconia <falcon@freecalypso.org> 
parents:  
diff
changeset
 | 
24 * into libgsm format. | 
| 
 
d57f68d0568d
fc-tch2fr utility written, added under miscutil
 
Mychaela Falconia <falcon@freecalypso.org> 
parents:  
diff
changeset
 | 
25 */ | 
| 
 
d57f68d0568d
fc-tch2fr utility written, added under miscutil
 
Mychaela Falconia <falcon@freecalypso.org> 
parents:  
diff
changeset
 | 
26 | 
| 
 
d57f68d0568d
fc-tch2fr utility written, added under miscutil
 
Mychaela Falconia <falcon@freecalypso.org> 
parents:  
diff
changeset
 | 
27 #include <sys/types.h> | 
| 
 
d57f68d0568d
fc-tch2fr utility written, added under miscutil
 
Mychaela Falconia <falcon@freecalypso.org> 
parents:  
diff
changeset
 | 
28 #include <ctype.h> | 
| 
 
d57f68d0568d
fc-tch2fr utility written, added under miscutil
 
Mychaela Falconia <falcon@freecalypso.org> 
parents:  
diff
changeset
 | 
29 #include <stdio.h> | 
| 
 
d57f68d0568d
fc-tch2fr utility written, added under miscutil
 
Mychaela Falconia <falcon@freecalypso.org> 
parents:  
diff
changeset
 | 
30 #include <stdlib.h> | 
| 
 
d57f68d0568d
fc-tch2fr utility written, added under miscutil
 
Mychaela Falconia <falcon@freecalypso.org> 
parents:  
diff
changeset
 | 
31 | 
| 
 
d57f68d0568d
fc-tch2fr utility written, added under miscutil
 
Mychaela Falconia <falcon@freecalypso.org> 
parents:  
diff
changeset
 | 
32 static | 
| 
 
d57f68d0568d
fc-tch2fr utility written, added under miscutil
 
Mychaela Falconia <falcon@freecalypso.org> 
parents:  
diff
changeset
 | 
33 decode_hex_digit(ch) | 
| 
 
d57f68d0568d
fc-tch2fr utility written, added under miscutil
 
Mychaela Falconia <falcon@freecalypso.org> 
parents:  
diff
changeset
 | 
34 { | 
| 
 
d57f68d0568d
fc-tch2fr utility written, added under miscutil
 
Mychaela Falconia <falcon@freecalypso.org> 
parents:  
diff
changeset
 | 
35 if (isdigit(ch)) | 
| 
 
d57f68d0568d
fc-tch2fr utility written, added under miscutil
 
Mychaela Falconia <falcon@freecalypso.org> 
parents:  
diff
changeset
 | 
36 return(ch - '0'); | 
| 
 
d57f68d0568d
fc-tch2fr utility written, added under miscutil
 
Mychaela Falconia <falcon@freecalypso.org> 
parents:  
diff
changeset
 | 
37 else if (isupper(ch)) | 
| 
 
d57f68d0568d
fc-tch2fr utility written, added under miscutil
 
Mychaela Falconia <falcon@freecalypso.org> 
parents:  
diff
changeset
 | 
38 return(ch - 'A' + 10); | 
| 
 
d57f68d0568d
fc-tch2fr utility written, added under miscutil
 
Mychaela Falconia <falcon@freecalypso.org> 
parents:  
diff
changeset
 | 
39 else | 
| 
 
d57f68d0568d
fc-tch2fr utility written, added under miscutil
 
Mychaela Falconia <falcon@freecalypso.org> 
parents:  
diff
changeset
 | 
40 return(ch - 'a' + 10); | 
| 
 
d57f68d0568d
fc-tch2fr utility written, added under miscutil
 
Mychaela Falconia <falcon@freecalypso.org> 
parents:  
diff
changeset
 | 
41 } | 
| 
 
d57f68d0568d
fc-tch2fr utility written, added under miscutil
 
Mychaela Falconia <falcon@freecalypso.org> 
parents:  
diff
changeset
 | 
42 | 
| 
 
d57f68d0568d
fc-tch2fr utility written, added under miscutil
 
Mychaela Falconia <falcon@freecalypso.org> 
parents:  
diff
changeset
 | 
43 main(argc, argv) | 
| 
 
d57f68d0568d
fc-tch2fr utility written, added under miscutil
 
Mychaela Falconia <falcon@freecalypso.org> 
parents:  
diff
changeset
 | 
44 char **argv; | 
| 
 
d57f68d0568d
fc-tch2fr utility written, added under miscutil
 
Mychaela Falconia <falcon@freecalypso.org> 
parents:  
diff
changeset
 | 
45 { | 
| 
 
d57f68d0568d
fc-tch2fr utility written, added under miscutil
 
Mychaela Falconia <falcon@freecalypso.org> 
parents:  
diff
changeset
 | 
46 FILE *inf, *outf; | 
| 
 
d57f68d0568d
fc-tch2fr utility written, added under miscutil
 
Mychaela Falconia <falcon@freecalypso.org> 
parents:  
diff
changeset
 | 
47 char linebuf[128]; | 
| 
 
d57f68d0568d
fc-tch2fr utility written, added under miscutil
 
Mychaela Falconia <falcon@freecalypso.org> 
parents:  
diff
changeset
 | 
48 int lineno; | 
| 
 
d57f68d0568d
fc-tch2fr utility written, added under miscutil
 
Mychaela Falconia <falcon@freecalypso.org> 
parents:  
diff
changeset
 | 
49 char *cp; | 
| 
 
d57f68d0568d
fc-tch2fr utility written, added under miscutil
 
Mychaela Falconia <falcon@freecalypso.org> 
parents:  
diff
changeset
 | 
50 int i, j; | 
| 
 
d57f68d0568d
fc-tch2fr utility written, added under miscutil
 
Mychaela Falconia <falcon@freecalypso.org> 
parents:  
diff
changeset
 | 
51 u_char tidsp_bytes[33], libgsm_bytes[33]; | 
| 
 
d57f68d0568d
fc-tch2fr utility written, added under miscutil
 
Mychaela Falconia <falcon@freecalypso.org> 
parents:  
diff
changeset
 | 
52 | 
| 
 
d57f68d0568d
fc-tch2fr utility written, added under miscutil
 
Mychaela Falconia <falcon@freecalypso.org> 
parents:  
diff
changeset
 | 
53 if (argc != 3) { | 
| 
 
d57f68d0568d
fc-tch2fr utility written, added under miscutil
 
Mychaela Falconia <falcon@freecalypso.org> 
parents:  
diff
changeset
 | 
54 fprintf(stderr, "usage: %s infile outfile\n", argv[0]); | 
| 
 
d57f68d0568d
fc-tch2fr utility written, added under miscutil
 
Mychaela Falconia <falcon@freecalypso.org> 
parents:  
diff
changeset
 | 
55 exit(1); | 
| 
 
d57f68d0568d
fc-tch2fr utility written, added under miscutil
 
Mychaela Falconia <falcon@freecalypso.org> 
parents:  
diff
changeset
 | 
56 } | 
| 
 
d57f68d0568d
fc-tch2fr utility written, added under miscutil
 
Mychaela Falconia <falcon@freecalypso.org> 
parents:  
diff
changeset
 | 
57 inf = fopen(argv[1], "r"); | 
| 
 
d57f68d0568d
fc-tch2fr utility written, added under miscutil
 
Mychaela Falconia <falcon@freecalypso.org> 
parents:  
diff
changeset
 | 
58 if (!inf) { | 
| 
 
d57f68d0568d
fc-tch2fr utility written, added under miscutil
 
Mychaela Falconia <falcon@freecalypso.org> 
parents:  
diff
changeset
 | 
59 perror(argv[1]); | 
| 
 
d57f68d0568d
fc-tch2fr utility written, added under miscutil
 
Mychaela Falconia <falcon@freecalypso.org> 
parents:  
diff
changeset
 | 
60 exit(1); | 
| 
 
d57f68d0568d
fc-tch2fr utility written, added under miscutil
 
Mychaela Falconia <falcon@freecalypso.org> 
parents:  
diff
changeset
 | 
61 } | 
| 
 
d57f68d0568d
fc-tch2fr utility written, added under miscutil
 
Mychaela Falconia <falcon@freecalypso.org> 
parents:  
diff
changeset
 | 
62 outf = fopen(argv[2], "w"); | 
| 
 
d57f68d0568d
fc-tch2fr utility written, added under miscutil
 
Mychaela Falconia <falcon@freecalypso.org> 
parents:  
diff
changeset
 | 
63 if (!outf) { | 
| 
 
d57f68d0568d
fc-tch2fr utility written, added under miscutil
 
Mychaela Falconia <falcon@freecalypso.org> 
parents:  
diff
changeset
 | 
64 perror(argv[2]); | 
| 
 
d57f68d0568d
fc-tch2fr utility written, added under miscutil
 
Mychaela Falconia <falcon@freecalypso.org> 
parents:  
diff
changeset
 | 
65 exit(1); | 
| 
 
d57f68d0568d
fc-tch2fr utility written, added under miscutil
 
Mychaela Falconia <falcon@freecalypso.org> 
parents:  
diff
changeset
 | 
66 } | 
| 
 
d57f68d0568d
fc-tch2fr utility written, added under miscutil
 
Mychaela Falconia <falcon@freecalypso.org> 
parents:  
diff
changeset
 | 
67 for (lineno = 1; fgets(linebuf, sizeof linebuf, inf); lineno++) { | 
| 
 
d57f68d0568d
fc-tch2fr utility written, added under miscutil
 
Mychaela Falconia <falcon@freecalypso.org> 
parents:  
diff
changeset
 | 
68 /* skip DSP status words */ | 
| 
 
d57f68d0568d
fc-tch2fr utility written, added under miscutil
 
Mychaela Falconia <falcon@freecalypso.org> 
parents:  
diff
changeset
 | 
69 cp = linebuf; | 
| 
 
d57f68d0568d
fc-tch2fr utility written, added under miscutil
 
Mychaela Falconia <falcon@freecalypso.org> 
parents:  
diff
changeset
 | 
70 for (i = 0; i < 3; i++) { | 
| 
 
d57f68d0568d
fc-tch2fr utility written, added under miscutil
 
Mychaela Falconia <falcon@freecalypso.org> 
parents:  
diff
changeset
 | 
71 for (j = 0; j < 4; j++) { | 
| 
 
d57f68d0568d
fc-tch2fr utility written, added under miscutil
 
Mychaela Falconia <falcon@freecalypso.org> 
parents:  
diff
changeset
 | 
72 if (!isxdigit(*cp++)) { | 
| 
 
d57f68d0568d
fc-tch2fr utility written, added under miscutil
 
Mychaela Falconia <falcon@freecalypso.org> 
parents:  
diff
changeset
 | 
73 invalid: fprintf(stderr, | 
| 
 
d57f68d0568d
fc-tch2fr utility written, added under miscutil
 
Mychaela Falconia <falcon@freecalypso.org> 
parents:  
diff
changeset
 | 
74 "error: %s is not in the expected format\n", | 
| 
 
d57f68d0568d
fc-tch2fr utility written, added under miscutil
 
Mychaela Falconia <falcon@freecalypso.org> 
parents:  
diff
changeset
 | 
75 argv[1]); | 
| 
 
d57f68d0568d
fc-tch2fr utility written, added under miscutil
 
Mychaela Falconia <falcon@freecalypso.org> 
parents:  
diff
changeset
 | 
76 exit(1); | 
| 
 
d57f68d0568d
fc-tch2fr utility written, added under miscutil
 
Mychaela Falconia <falcon@freecalypso.org> 
parents:  
diff
changeset
 | 
77 } | 
| 
 
d57f68d0568d
fc-tch2fr utility written, added under miscutil
 
Mychaela Falconia <falcon@freecalypso.org> 
parents:  
diff
changeset
 | 
78 } | 
| 
 
d57f68d0568d
fc-tch2fr utility written, added under miscutil
 
Mychaela Falconia <falcon@freecalypso.org> 
parents:  
diff
changeset
 | 
79 if (*cp++ != ' ') | 
| 
 
d57f68d0568d
fc-tch2fr utility written, added under miscutil
 
Mychaela Falconia <falcon@freecalypso.org> 
parents:  
diff
changeset
 | 
80 goto invalid; | 
| 
 
d57f68d0568d
fc-tch2fr utility written, added under miscutil
 
Mychaela Falconia <falcon@freecalypso.org> 
parents:  
diff
changeset
 | 
81 } | 
| 
 
d57f68d0568d
fc-tch2fr utility written, added under miscutil
 
Mychaela Falconia <falcon@freecalypso.org> 
parents:  
diff
changeset
 | 
82 /* read the frame bits */ | 
| 
 
d57f68d0568d
fc-tch2fr utility written, added under miscutil
 
Mychaela Falconia <falcon@freecalypso.org> 
parents:  
diff
changeset
 | 
83 for (i = 0; i < 33; i++) { | 
| 
 
d57f68d0568d
fc-tch2fr utility written, added under miscutil
 
Mychaela Falconia <falcon@freecalypso.org> 
parents:  
diff
changeset
 | 
84 if (!isxdigit(cp[0]) || !isxdigit(cp[1])) | 
| 
 
d57f68d0568d
fc-tch2fr utility written, added under miscutil
 
Mychaela Falconia <falcon@freecalypso.org> 
parents:  
diff
changeset
 | 
85 goto invalid; | 
| 
 
d57f68d0568d
fc-tch2fr utility written, added under miscutil
 
Mychaela Falconia <falcon@freecalypso.org> 
parents:  
diff
changeset
 | 
86 tidsp_bytes[i] = (decode_hex_digit(cp[0]) << 4) | | 
| 
 
d57f68d0568d
fc-tch2fr utility written, added under miscutil
 
Mychaela Falconia <falcon@freecalypso.org> 
parents:  
diff
changeset
 | 
87 decode_hex_digit(cp[1]); | 
| 
 
d57f68d0568d
fc-tch2fr utility written, added under miscutil
 
Mychaela Falconia <falcon@freecalypso.org> 
parents:  
diff
changeset
 | 
88 cp += 2; | 
| 
 
d57f68d0568d
fc-tch2fr utility written, added under miscutil
 
Mychaela Falconia <falcon@freecalypso.org> 
parents:  
diff
changeset
 | 
89 } | 
| 
 
d57f68d0568d
fc-tch2fr utility written, added under miscutil
 
Mychaela Falconia <falcon@freecalypso.org> 
parents:  
diff
changeset
 | 
90 gsm0610_tidsp_to_libgsm(tidsp_bytes, libgsm_bytes); | 
| 
 
d57f68d0568d
fc-tch2fr utility written, added under miscutil
 
Mychaela Falconia <falcon@freecalypso.org> 
parents:  
diff
changeset
 | 
91 fwrite(libgsm_bytes, 1, 33, outf); | 
| 
 
d57f68d0568d
fc-tch2fr utility written, added under miscutil
 
Mychaela Falconia <falcon@freecalypso.org> 
parents:  
diff
changeset
 | 
92 } | 
| 
 
d57f68d0568d
fc-tch2fr utility written, added under miscutil
 
Mychaela Falconia <falcon@freecalypso.org> 
parents:  
diff
changeset
 | 
93 exit(0); | 
| 
 
d57f68d0568d
fc-tch2fr utility written, added under miscutil
 
Mychaela Falconia <falcon@freecalypso.org> 
parents:  
diff
changeset
 | 
94 } | 
