-
Notifications
You must be signed in to change notification settings - Fork 314
/
Copy pathttcalc.c
530 lines (435 loc) · 12.4 KB
/
ttcalc.c
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
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
//
// This file is part of Dire Wolf, an amateur radio packet TNC.
//
// Copyright (C) 2013, 2015 John Langner, WB2OSZ
//
// 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 2 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/>.
//
/*------------------------------------------------------------------
*
* Module: ttcalc.c
*
* Purpose: Simple Touch Tone to Speech calculator.
*
* Description: Demonstration of how Dire Wolf can be used
* as a DTMF / Speech interface for ham radio applications.
*
* Usage: Start up direwolf with configuration:
* - DTMF decoder enabled.
* - Text-to-speech enabled.
* - Listening to standard port 8000 for a client application.
*
* Run this in a different window.
*
* User sends formulas such as:
*
* 2 * 3 * 4 #
*
* with the touch tone pad.
* The result is sent back with speech, e.g. "Twenty Four."
*
*---------------------------------------------------------------*/
#include "direwolf.h" // Sets _WIN32_WINNT for XP API level needed by ws2tcpip.h
#if __WIN32__
#include <winsock2.h>
#include <ws2tcpip.h> // _WIN32_WINNT must be set to 0x0501 before including this
#else
#include <stdlib.h>
#include <netdb.h>
#include <sys/types.h>
#include <sys/ioctl.h>
#include <sys/socket.h>
#include <arpa/inet.h>
#include <netinet/in.h>
#include <netinet/tcp.h>
#include <errno.h>
#endif
#include <unistd.h>
#include <stdio.h>
#include <assert.h>
#include <ctype.h>
#include <string.h>
#include "direwolf.h"
#include "ax25_pad.h"
#include "textcolor.h"
struct agwpe_s {
short portx; /* 0 for first, 1 for second, etc. */
short port_hi_reserved;
short kind_lo; /* message type */
short kind_hi;
char call_from[10];
char call_to[10];
int data_len; /* Number of data bytes following. */
int user_reserved;
};
static int calculator (char *str);
static int connect_to_server (char *hostname, char *port);
static char * ia_to_text (int Family, void * pAddr, char * pStringBuf, size_t StringBufSize);
/*------------------------------------------------------------------
*
* Name: main
*
*---------------------------------------------------------------*/
int main (int argc, char *argv[])
{
int server_sock = -1;
struct agwpe_s mon_cmd;
char data[1024];
char hostname[30] = "localhost";
char port[10] = "8000";
int err;
#if __WIN32__
#else
setlinebuf (stdout);
#endif
assert (calculator("12a34#") == 46);
assert (calculator("2*3A4#") == 10);
assert (calculator("5*100A3#") == 503);
assert (calculator("6a4*5#") == 50);
/*
* Try to attach to Dire Wolf.
*/
server_sock = connect_to_server (hostname, port);
if (server_sock == -1) {
exit (1);
}
/*
* Send command to toggle reception of frames in raw format.
*
* Note: Monitor format is only for UI frames.
*/
memset (&mon_cmd, 0, sizeof(mon_cmd));
mon_cmd.kind_lo = 'k';
err = SOCK_SEND (server_sock, (char*)(&mon_cmd), sizeof(mon_cmd));
(void)err;
/*
* Print all of the monitored packets.
*/
while (1) {
int n;
n = SOCK_RECV (server_sock, (char*)(&mon_cmd), sizeof(mon_cmd));
if (n != sizeof(mon_cmd)) {
printf ("Read error, received %d command bytes.\n", n);
exit (1);
}
assert (mon_cmd.data_len >= 0 && mon_cmd.data_len < (int)(sizeof(data)));
if (mon_cmd.data_len > 0) {
n = SOCK_RECV (server_sock, data, mon_cmd.data_len);
if (n != mon_cmd.data_len) {
printf ("Read error, client received %d data bytes when %d expected. Terminating.\n", n, mon_cmd.data_len);
exit (1);
}
}
/*
* Print it.
*/
if (mon_cmd.kind_lo == 'K') {
packet_t pp;
char *pinfo;
int info_len;
char result[400];
char *p;
alevel_t alevel;
int chan;
chan = mon_cmd.portx;
memset (&alevel, 0, sizeof(alevel));
pp = ax25_from_frame ((unsigned char *)(data+1), mon_cmd.data_len-1, alevel);
ax25_format_addrs (pp, result);
info_len = ax25_get_info (pp, (unsigned char **)(&pinfo));
pinfo[info_len] = '\0';
strcat (result, pinfo);
for (p=result; *p!='\0'; p++) {
if (! isprint(*p)) *p = ' ';
}
printf ("[%d] %s\n", chan, result);
/*
* Look for Special touch tone packet with "t" in first position of the Information part.
*/
if (*pinfo == 't') {
int n;
char reply_text[200];
packet_t reply_pp;
struct {
struct agwpe_s hdr;
char extra;
unsigned char frame[AX25_MAX_PACKET_LEN];
} xmit_raw;
/*
* Send touch tone sequence to calculator and get the answer.
*
* Put your own application here instead. Here are some ideas:
*
* http://www.tapr.org/pipermail/aprssig/2015-January/044069.html
*/
n = calculator (pinfo+1);
printf ("\nCalculator returns %d\n\n", n);
/*
* Convert to AX.25 frame.
* Notice that the special destination will cause it to be spoken.
*/
snprintf (reply_text, sizeof(reply_text), "N0CALL>SPEECH:%d", n);
reply_pp = ax25_from_text(reply_text, 1);
/*
* Send it to the TNC.
* In this example we are transmitting speech on the same channel
* where the tones were heard. We could also send AX.25 frames to
* other radio channels.
*/
memset (&xmit_raw, 0, sizeof(xmit_raw));
xmit_raw.hdr.portx = chan;
xmit_raw.hdr.kind_lo = 'K';
xmit_raw.hdr.data_len = 1 + ax25_pack (reply_pp, xmit_raw.frame);
err = SOCK_SEND (server_sock, (char*)(&xmit_raw), sizeof(xmit_raw.hdr)+xmit_raw.hdr.data_len);
ax25_delete (reply_pp);
}
ax25_delete (pp);
}
}
} /* main */
/*------------------------------------------------------------------
*
* Name: calculator
*
* Purpose: Simple calculator to demonstrate Touch Tone to Speech
* application tool kit.
*
* Inputs: str - Sequence of touch tone characters: 0-9 A-D * #
* It should be terminated with #.
*
* Returns: Numeric result of calculation.
*
* Description: This is a simple calculator that recognizes
* numbers,
* * for multiply
* A for add
* # for equals result
*
* Adding functions to B, C, and D is left as an
* exercise for the reader.
*
* Examples: 2 * 3 A 4 # Ten
* 5 * 1 0 0 A 3 # Five Hundred Three
*
*---------------------------------------------------------------*/
#define DO_LAST_OP \
switch (lastop) { \
case NONE: result = num; num = 0; break; \
case ADD: result += num; num = 0; break; \
case SUB: result -= num; num = 0; break; \
case MUL: result *= num; num = 0; break; \
case DIV: result /= num; num = 0; break; \
}
static int calculator (char *str)
{
int result;
int num;
enum { NONE, ADD, SUB, MUL, DIV } lastop;
char *p;
result = 0;
num = 0;
lastop = NONE;
for (p = str; *p != '\0'; p++) {
if (isdigit(*p)) {
num = num * 10 + *p - '0';
}
else if (*p == '*') {
DO_LAST_OP;
lastop = MUL;
}
else if (*p == 'A' || *p == 'a') {
DO_LAST_OP;
lastop = ADD;
}
else if (*p == '#') {
DO_LAST_OP;
return (result);
}
}
return (result); // not expected.
}
/*------------------------------------------------------------------
*
* Name: connect_to_server
*
* Purpose: Connect to Dire Wolf TNC server.
*
* Inputs: hostname
* port
*
* Returns: File descriptor or -1 for error.
*
*---------------------------------------------------------------*/
static int connect_to_server (char *hostname, char *port)
{
#if __WIN32__
#else
//int e;
#endif
#define MAX_HOSTS 30
struct addrinfo hints;
struct addrinfo *ai_head = NULL;
struct addrinfo *ai;
struct addrinfo *hosts[MAX_HOSTS];
int num_hosts, n;
int err;
char ipaddr_str[46]; /* text form of IP address */
#if __WIN32__
WSADATA wsadata;
#endif
/*
* File descriptor for socket to server.
* Set to -1 if not connected.
* (Don't use SOCKET type because it is unsigned.)
*/
int server_sock = -1;
#if __WIN32__
err = WSAStartup (MAKEWORD(2,2), &wsadata);
if (err != 0) {
printf("WSAStartup failed: %d\n", err);
exit (1);
}
if (LOBYTE(wsadata.wVersion) != 2 || HIBYTE(wsadata.wVersion) != 2) {
printf("Could not find a usable version of Winsock.dll\n");
WSACleanup();
exit (1);
}
#endif
memset (&hints, 0, sizeof(hints));
hints.ai_family = AF_UNSPEC; /* Allow either IPv4 or IPv6. */
// hints.ai_family = AF_INET; /* IPv4 only. */
hints.ai_socktype = SOCK_STREAM;
hints.ai_protocol = IPPROTO_TCP;
/*
* Connect to specified hostname & port.
*/
ai_head = NULL;
err = getaddrinfo(hostname, port, &hints, &ai_head);
if (err != 0) {
#if __WIN32__
printf ("Can't get address for server %s, err=%d\n", hostname, WSAGetLastError());
#else
printf ("Can't get address for server %s, %s\n", hostname, gai_strerror(err));
#endif
freeaddrinfo(ai_head);
exit (1);
}
num_hosts = 0;
for (ai = ai_head; ai != NULL; ai = ai->ai_next) {
hosts[num_hosts] = ai;
if (num_hosts < MAX_HOSTS) num_hosts++;
}
// Try each address until we find one that is successful.
for (n=0; n<num_hosts; n++) {
#if __WIN32__
SOCKET is;
#else
int is;
#endif
ai = hosts[n];
ia_to_text (ai->ai_family, ai->ai_addr, ipaddr_str, sizeof(ipaddr_str));
is = socket(ai->ai_family, ai->ai_socktype, ai->ai_protocol);
#if __WIN32__
if (is == INVALID_SOCKET) {
printf ("Socket creation failed, err=%d", WSAGetLastError());
WSACleanup();
is = -1;
continue;
}
#else
if (err != 0) {
printf ("Socket creation failed, err=%s", gai_strerror(err));
(void) close (is);
is = -1;
continue;
}
#endif
err = connect(is, ai->ai_addr, (int)ai->ai_addrlen);
#if __WIN32__
if (err == SOCKET_ERROR) {
closesocket (is);
is = -1;
continue;
}
#else
if (err != 0) {
(void) close (is);
is = -1;
continue;
}
int flag = 1;
err = setsockopt (is, IPPROTO_TCP, TCP_NODELAY, (void*)(long)(&flag), (socklen_t)sizeof(flag));
if (err < 0) {
printf("setsockopt TCP_NODELAY failed.\n");
}
#endif
/*
* Success.
*/
printf("Client app now connected to %s (%s), port %s\n", hostname, ipaddr_str, port);
server_sock = is;
break;
}
freeaddrinfo(ai_head);
if (server_sock == -1) {
printf("Unnable to connect to %s (%s), port %s\n", hostname, ipaddr_str, port);
}
return (server_sock);
} /* end connect_to_server */
/*------------------------------------------------------------------
*
* Name: ia_to_text
*
* Purpose: Convert Internet address to text.
* Can't use InetNtop because it is supported only
* on Windows Vista and later.
*
*---------------------------------------------------------------*/
static char * ia_to_text (int Family, void * pAddr, char * pStringBuf, size_t StringBufSize)
{
struct sockaddr_in *sa4;
struct sockaddr_in6 *sa6;
switch (Family) {
case AF_INET:
sa4 = (struct sockaddr_in *)pAddr;
#if __WIN32__
snprintf (pStringBuf, StringBufSize, "%d.%d.%d.%d", sa4->sin_addr.S_un.S_un_b.s_b1,
sa4->sin_addr.S_un.S_un_b.s_b2,
sa4->sin_addr.S_un.S_un_b.s_b3,
sa4->sin_addr.S_un.S_un_b.s_b4);
#else
inet_ntop (AF_INET, &(sa4->sin_addr), pStringBuf, StringBufSize);
#endif
break;
case AF_INET6:
sa6 = (struct sockaddr_in6 *)pAddr;
#if __WIN32__
snprintf (pStringBuf, StringBufSize, "%x:%x:%x:%x:%x:%x:%x:%x",
ntohs(((unsigned short *)(&(sa6->sin6_addr)))[0]),
ntohs(((unsigned short *)(&(sa6->sin6_addr)))[1]),
ntohs(((unsigned short *)(&(sa6->sin6_addr)))[2]),
ntohs(((unsigned short *)(&(sa6->sin6_addr)))[3]),
ntohs(((unsigned short *)(&(sa6->sin6_addr)))[4]),
ntohs(((unsigned short *)(&(sa6->sin6_addr)))[5]),
ntohs(((unsigned short *)(&(sa6->sin6_addr)))[6]),
ntohs(((unsigned short *)(&(sa6->sin6_addr)))[7]));
#else
inet_ntop (AF_INET6, &(sa6->sin6_addr), pStringBuf, StringBufSize);
#endif
break;
default:
snprintf (pStringBuf, StringBufSize, "Invalid address family!");
}
return pStringBuf;
} /* end ia_to_text */
/* end ttcalc.c */