~ubuntu-branches/ubuntu/quantal/cwdaemon/quantal

« back to all changes in this revision

Viewing changes to test/cwtest2.c~

  • Committer: Package Import Robot
  • Author(s): Kamil Ignacak
  • Date: 2012-04-05 19:33:45 UTC
  • mfrom: (0.1.3)
  • Revision ID: package-import@ubuntu.com-20120405193345-jpw5uvzif9fen4m8
Tags: 0.9.5-1
* New version of upstream package;
* cwdaemon project has new home page (cwdaemon.sf.net);
* Debian patches 01-06 from 0.9.4-9 applied, debian/patch/* files removed;
* cwdaemon now has git repository, so adding watch file to debian/*;
* other necessary changes in debian/*;

  This upstream version (0.9.5) is essentially an official upstream
  release of Debian package 0.9.4-9, with Debian patches applied.
  No new features and no new fixes were added to 0.9.5, compared to
  0.9.4-9. Purpose of upstream release 0.9.5 is to establish a starting
  point for revived cwdaemon project.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
/*
2
 
 * cwtest.c - test program for cwdaemon
3
 
 * Copyright (C) 2003, 2006 Joop Stakenborg <pg4i@amsat.org>
4
 
 *
5
 
 * Some of this code is taken from netkeyer.c, which is part of the tlf source,
6
 
 * here is the copyright:
7
 
 * Tlf - contest logging program for amateur radio operators
8
 
 * Copyright (C) 2001-2002-2003 Rein Couperus <pa0rct@amsat.org>
9
 
 *
10
 
 * This program is free software; you can redistribute it and/or modify
11
 
 * it under the terms of the GNU General Public License as published by
12
 
 * the Free Software Foundation; either version 2 of the License, or
13
 
 * (at your option) any later version.
14
 
 *
15
 
 * This program is distributed in the hope that it will be useful,
16
 
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17
 
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18
 
 * GNU Library General Public License for more details.
19
 
 *
20
 
 * You should have received a copy of the GNU General Public License
21
 
 * along with this program; if not, write to the Free Software
22
 
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
23
 
 */
24
 
 
25
 
/*
26
 
 * Compile this program with "gcc -o cwtest cwtest.c"
27
 
 * Usage: cwtest or cwtest <portname>
28
 
 */
29
 
 
30
 
#include <stdlib.h>
31
 
#include <stdio.h>
32
 
#include <unistd.h>
33
 
#include <string.h>
34
 
#include <sys/socket.h>
35
 
#include <netinet/in.h>
36
 
#include <arpa/inet.h>
37
 
#include <netdb.h>
38
 
#include <errno.h>
39
 
#include <signal.h>
40
 
 
41
 
#define K_MESSAGE 1
42
 
 
43
 
#define K_RESET 0
44
 
#define K_SPEED 2
45
 
#define K_TONE 3
46
 
#define K_ABORT 4
47
 
#define K_STOP 5
48
 
#define K_WORDMODE 6
49
 
#define K_WEIGHT 7
50
 
#define K_DEVICE 8
51
 
#define K_TOD 9         // set txdelay (turn on delay)
52
 
#define K_ADDRESS 10    // set port address of device (obsolete)
53
 
#define K_SET14 11      // set pin 14 on lpt
54
 
#define K_TUNE 12       // tune
55
 
#define K_PTT 13        // PTT on/off
56
 
#define K_SWITCH 14     // set band switch output pins 2,7,8,9 on lpt
57
 
#define K_SDEVICE 15    // set sound device
58
 
#define K_VOLUME 16     // volume for soundcard
59
 
 
60
 
int netkeyer_port = 6789;
61
 
char netkeyer_hostaddress[16] = "127.0.0.1";
62
 
int socket_descriptor;
63
 
int close_rc;
64
 
ssize_t sendto_rc;
65
 
struct sockaddr_in address;
66
 
struct hostent *hostbyname;
67
 
 
68
 
 
69
 
int netkeyer_init (void)
70
 
{
71
 
        hostbyname = gethostbyname (netkeyer_hostaddress);
72
 
        if (hostbyname == NULL)
73
 
        {
74
 
                perror ("gethostbyname failed");
75
 
                return (1);
76
 
        }
77
 
        bzero (&address, sizeof(address));
78
 
        address.sin_family = AF_INET;
79
 
        memcpy (&address.sin_addr.s_addr, hostbyname->h_addr, 
80
 
                sizeof (address.sin_addr.s_addr));
81
 
        address.sin_port = htons (netkeyer_port);
82
 
        socket_descriptor = socket (AF_INET, SOCK_DGRAM, 0);
83
 
        if (socket_descriptor == -1)
84
 
        {
85
 
                perror ("socket call failed");
86
 
                return (1);
87
 
        }
88
 
        return (0);
89
 
}
90
 
 
91
 
int netkeyer_close (void)
92
 
{
93
 
 
94
 
        close_rc = close (socket_descriptor);
95
 
        if (close_rc == -1)
96
 
        {
97
 
                perror ("close call failed");
98
 
                return (-1);
99
 
        }
100
 
        return (0);
101
 
}
102
 
 
103
 
 
104
 
int netkeyer(int cw_op, char *cwmessage)
105
 
{
106
 
        char buf[80];
107
 
 
108
 
        switch (cw_op)
109
 
        {
110
 
                case K_RESET :
111
 
                        buf[0]= 27;
112
 
                        sprintf (buf+1,"0");
113
 
                        break;
114
 
                case K_MESSAGE :
115
 
                        sprintf (buf, cwmessage);
116
 
                        break;
117
 
                case K_SPEED :
118
 
                        buf[0]= 27;
119
 
                        sprintf (buf+1,"2");
120
 
                        sprintf (buf+2, cwmessage);
121
 
                        break;
122
 
                case K_TONE :
123
 
                        buf[0]= 27;
124
 
                        sprintf (buf+1,"3");
125
 
                        sprintf (buf+2, cwmessage);
126
 
                        break;
127
 
                case K_ABORT :
128
 
                        buf[0]= 27;
129
 
                        sprintf (buf+1,"4");
130
 
                        break;
131
 
                case K_STOP :
132
 
                        buf[0]= 27;
133
 
                        sprintf (buf+1,"5");
134
 
                        break;
135
 
                case K_WORDMODE :
136
 
                        buf[0]= 27;
137
 
                        sprintf (buf+1,"6");
138
 
                        break;
139
 
                case K_WEIGHT :
140
 
                        buf[0]= 27;
141
 
                        sprintf (buf+1,"7");
142
 
                        sprintf (buf+2, cwmessage);
143
 
                        break;
144
 
                case K_DEVICE :
145
 
                        buf[0]= 27;
146
 
                        sprintf (buf+1,"8");
147
 
                        sprintf (buf+2, cwmessage);
148
 
                        break;
149
 
                case K_PTT :
150
 
                        buf[0]= 27;
151
 
                        sprintf (buf+1,"a");
152
 
                        sprintf (buf+2, cwmessage);
153
 
                        break;
154
 
                case K_TUNE :
155
 
                        buf[0]= 27;
156
 
                        sprintf (buf+1,"c");
157
 
                        sprintf (buf+2, cwmessage);
158
 
                        break;
159
 
                case K_TOD :
160
 
                        buf[0]= 27;
161
 
                        sprintf (buf+1,"d");
162
 
                        sprintf (buf+2, cwmessage);
163
 
                        break;
164
 
                case K_SDEVICE :
165
 
                        buf[0]= 27;
166
 
                        sprintf (buf+1,"f");
167
 
                        sprintf (buf+2, cwmessage);
168
 
                        break;
169
 
                case K_VOLUME :
170
 
                        buf[0]= 27;
171
 
                        sprintf (buf+1,"g");
172
 
                        sprintf (buf+2, cwmessage);
173
 
                        break;
174
 
                default :
175
 
                        buf[0]='\0';
176
 
        }
177
 
 
178
 
        if (buf[0] != '\0') 
179
 
        {
180
 
                sendto_rc = sendto (socket_descriptor, buf, sizeof (buf),
181
 
                        0, (struct sockaddr *)&address, sizeof (address));
182
 
        }
183
 
 
184
 
        buf[0] = '\0';
185
 
        cw_op = K_RESET;
186
 
 
187
 
        if (sendto_rc == -1)
188
 
        {
189
 
                printf ("Keyer send failed...!\n");
190
 
                return (1);
191
 
        }
192
 
        return(0);
193
 
}
194
 
 
195
 
static void
196
 
catchint (int signal)
197
 
{
198
 
        int     result = netkeyer (K_ABORT, "");
199
 
        exit (0);
200
 
}
201
 
 
202
 
int main (int argc, char **argv)
203
 
{
204
 
        int result;
205
 
 
206
 
        result = netkeyer_init ();
207
 
        if (result == 1) exit (1);
208
 
                
209
 
/* tests start here, no error handling */
210
 
        if (argc > 1)
211
 
        {
212
 
                result = netkeyer (K_DEVICE, argv[1]);
213
 
                printf("opening port %s\n", argv[1]);
214
 
        }
215
 
 
216
 
        printf("test message abort");
217
 
        signal (SIGALRM, catchint);
218
 
        result = netkeyer (K_MESSAGE, "paris paris");
219
 
        alarm (1);
220
 
        while (1) {}
221
 
        
222
 
        printf("done");
223
 
 
224
 
/* end tests */
225
 
        result = netkeyer_close ();
226
 
        if (result == 1) exit (1);
227
 
        exit (0);
228
 
}