aboutsummaryrefslogtreecommitdiffstats
path: root/src/send.c
blob: 4e6a205f14766692d749c2b6304b0de84dd50333 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
/**
 * Copyright © 2015  Mattias Andrée (maandree@member.fsf.org)
 * 
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 * 
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */
#include <stdio.h>
#include <math.h> /* -lm */
#include <stdint.h>
#include <alsa/asoundlib.h> /* dep: alsa-lib (pkg-config alsa) */


#define UTYPE   uint32_t
#define SMIN    INT32_MIN
#define SMAX    INT32_MAX
#define FORMAT  SND_PCM_FORMAT_U32

#define SAMPLE_RATE  52000   /* Hz */
#define DURATION     100     /* ms */ /* 100 ms → 10 Bd → 5 B/s */
#define LATENCY      100000  /* µs */

#define CHAR_ESCAPE  0x10  /* Data link escape */
#define CHAR_CANCEL  0x18  /* Cancel */
#define CHAR_END     0x04  /* End of transmission */


#define N(buf)  (sizeof(buf) / sizeof(*buf))


static const char* argv0;
static snd_pcm_t* sound_handle = NULL;


void get_freq(int nibble, int* low, int* high)
{
  static const int LOW[] = { 697, 770, 852, 941 };
  static const int HIGH[] = { 1209, 1336, 1477, 1663 };
  
  *low  =  LOW[(nibble >> 0) & 0x03];
  *high = HIGH[(nibble >> 2) & 0x03];
}

int send_tones(int low, int high)
{
#define GENERATE_TONE(tone)  \
  sin(2 * M_PI * ((double)i / (SAMPLE_RATE / (tone))))
  
  static UTYPE buffer[SAMPLE_RATE / 1000 * DURATION];
  snd_pcm_sframes_t frames;
  int r;
  size_t i;
  
  for (i = 0; i < N(buffer); i++)
    buffer[i] = (GENERATE_TONE(low * 1) + GENERATE_TONE(high * 1) + 
		 GENERATE_TONE(low * 4) + GENERATE_TONE(high * 4)) *
                (SMAX / 4) - SMIN;
  
  r = frames = snd_pcm_writei(sound_handle, buffer, N(buffer));
  if (frames < 0)
    r = frames = snd_pcm_recover(sound_handle, r, 0 /* do not print error reason? */);
  if (r < 0)
    {
      fprintf(stderr, "%s: snd_pcm_writei: %s\n", argv0, snd_strerror(r));
      return -1;
	}
  if ((r > 0) && ((size_t)r < N(buffer)))
    printf("%s: short write: expected %zu, wrote %zu\n", argv0, N(buffer), (size_t)frames);
  
  return 0;
}

int send_byte(int c)
{
  int low1, high1, low2, high2;
  get_freq((c >> 0) & 0x0F, &low1, &high1);
  get_freq((c >> 4) & 0x0F, &low2, &high2);
  if (send_tones(low1, high1))
    return -1;
  if (send_tones(low2, high2))
    return -1;
  return 0;
}

int send_byte_with_ecc(int c)
{
  static int cs[4];
  static int ptr = 0;
  int data[8];
  int i;
  
  if (c < 0)
    while (ptr < 4)
      cs[ptr++] = -c;
  else
    cs[ptr++] = c;
  
  if (ptr < 4)
    return 0;
  
  ptr = 0;
  
  data[0] = cs[0] ^ cs[1] ^ cs[3];
  data[1] = cs[0] ^ cs[2] ^ cs[3];
  data[2] = cs[0];
  data[3] = cs[1] ^ cs[2] ^ cs[3];
  data[4] = cs[1];
  data[5] = cs[2];
  data[6] = cs[3];
  data[7] = cs[0] ^ cs[1] ^ cs[2] ^ cs[3];
  
  for (i = 0; i < 8; i++)
    if (send_byte(data[i]))
      return -1;
  
  return 0;
}


int main(int argc, char* argv[])
{
  int r;
  char buf[1024];
  int c;
  ssize_t n, i;
  
  (void) argc;
  argv0 = argv[0];
  
  r = snd_pcm_open(&sound_handle, "default", SND_PCM_STREAM_PLAYBACK, 0);
  if (r < 0)
    return
      fprintf(stderr, "%s: snd_pcm_open: %s\n", *argv, snd_strerror(r)),
      1;
  
  r = snd_pcm_set_params(sound_handle,
			 FORMAT,
			 SND_PCM_ACCESS_RW_INTERLEAVED,
			 1,            /* channels */
			 SAMPLE_RATE,
			 1,            /* allow resampling? */
			 LATENCY);
  if (r < 0)
    {
      fprintf(stderr, "%s: snd_pcm_set_params: %s\n", *argv, snd_strerror(r));
      goto snd_fail;
    }
  
  for (;;)
    {
      n = read(STDIN_FILENO, buf, sizeof(buf));
      if (n < 0)
	goto fail;
      if (n == 0)
	break;
      for (i = 0; i < n; i++)
	{
	  c = buf[i];
	  if ((c == CHAR_ESCAPE) || (c == CHAR_CANCEL) || (c == CHAR_END))
	    if (send_byte_with_ecc(CHAR_ESCAPE))
	      goto snd_fail;
	  if (send_byte_with_ecc(c))
	    goto snd_fail;
	}
    }
  if (send_byte_with_ecc(CHAR_END))
    goto snd_fail;
  if (send_byte_with_ecc(-CHAR_END))
    goto snd_fail;
  
  snd_pcm_close(sound_handle);
  return 0;
 fail:
  perror(argv0);
  send_byte_with_ecc(CHAR_CANCEL);
  send_byte_with_ecc(-CHAR_CANCEL);
  return 1;
 snd_fail:
  snd_pcm_close(sound_handle);
  send_byte_with_ecc(CHAR_CANCEL);
  send_byte_with_ecc(-CHAR_CANCEL);
  return 1;
}