File: cwdaemon-mt.patch

package info (click to toggle)
cwdaemon 0.10.2-3
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, sid, trixie
  • size: 1,192 kB
  • sloc: sh: 3,872; ansic: 2,425; perl: 1,181; makefile: 66
file content (285 lines) | stat: -rw-r--r-- 7,659 bytes parent folder | download | duplicates (7)
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
diff -ruN cwdaemon-0.9.2/ChangeLog cwdaemon-0.9.2mt/ChangeLog
--- cwdaemon-0.9.2/ChangeLog	2005-02-10 17:59:41.000000000 +0100
+++ cwdaemon-0.9.2mt/ChangeLog	2006-04-02 17:54:18.738950764 +0200
@@ -1,3 +1,5 @@
+  * Reader thread for immediate stop after ESC0, OK1ZIA
+
 cwdaemon (0.9.2)
 
   * Many patches by Jason L. Wright, AI4JW, which brings us OpenBSD support
diff -ruN cwdaemon-0.9.2/configure.in cwdaemon-0.9.2mt/configure.in
--- cwdaemon-0.9.2/configure.in	2005-03-09 15:56:44.000000000 +0100
+++ cwdaemon-0.9.2mt/configure.in	2006-04-02 18:11:34.328774214 +0200
@@ -1,7 +1,7 @@
 # Process this file with autoconf to produce a configure script.
 
 AC_PREREQ(2.57)
-AC_INIT(cwdaemon, 0.9.2, pg4i@amsat.org)
+AC_INIT(cwdaemon, 0.9.2mt, pg4i@amsat.org)
 AM_INIT_AUTOMAKE
 AM_MAINTAINER_MODE
 AC_CONFIG_SRCDIR([cwdaemon.c])
@@ -46,6 +46,11 @@
 AC_CHECK_LIB([m],[floorf],[AC_DEFINE([HAVE_FLOORF],[1],[libm includes floorf])])
 AC_CHECK_LIB([m],[sin])
 AC_CHECK_LIB([ossaudio],[_oss_ioctl])
+AC_CHECK_LIB([pthread],[pthread_create])
+if test x"$ac_cv_lib_pthread_pthread_create" = x"no"; then
+	AC_MSG_ERROR(You need library pthread to compile cwdaemon)
+fi
+CFLAGS="$CFLAGS -DREENTRANT"
 
 if test "${ac_cv_c_compiler_gnu}" = "yes"; then
   CFLAGS="${CFLAGS} -Wall"
diff -ruN cwdaemon-0.9.2/cwdaemon.c cwdaemon-0.9.2mt/cwdaemon.c
--- cwdaemon-0.9.2/cwdaemon.c	2005-03-14 18:02:29.000000000 +0100
+++ cwdaemon-0.9.2mt/cwdaemon.c	2006-04-02 18:07:23.047292861 +0200
@@ -88,6 +88,7 @@
 # include <sys/param.h>
 #endif
 #include <limits.h>
+#include <pthread.h>
 
 #include "cwlib.h"
 #include "cwdaemon.h"
@@ -95,7 +96,7 @@
 /* network vars */
 int sin_len, reply_socklen;
 int socket_descriptor;
-struct sockaddr_in k_sin, reply_sin;
+struct sockaddr_in k_sin, reply_sin; /* k_sin is protected by k_sin_mutex */
 int port = 6789;		/* default UDP port we listen on */
 char reply_data[256];
 
@@ -110,7 +111,7 @@
 int soundcard_sound = 0;	/* soundcard off */
 
 /* various variables */
-int forking = 1; 		/* we fork by default */
+int forking = 1;		/* we fork by default */
 int bandswitch;
 int priority = 0;
 
@@ -163,6 +164,13 @@
 #endif
 cwdevice *cwdev;
 
+/* variables for reader thread */
+pthread_t reader_thread;
+pthread_mutex_t k_sin_mutex; /* for k_sin */
+int reader_pipe[2];
+int async_abort;
+
+
 /* catch ^C when running in foreground */
 static RETSIGTYPE
 catchint (int signal)
@@ -336,15 +344,13 @@
 	int valid_sdevice = 0, fd;
 	long lv;
 
-	recv_rc = recvfrom (socket_descriptor, message, sizeof (message) - 1,
-		0, (struct sockaddr *) &k_sin, &sin_len);
+	recv_rc = read (reader_pipe[0], message, sizeof (message) - 1);
 
 	if (recv_rc == -1 && errno != EAGAIN)
 	{
 		errmsg ("Recvfrom");
 		exit (1);
 	}
-
 	if (recv_rc > 0)
 	{
 		message[recv_rc] = '\0';
@@ -367,6 +373,7 @@
 				soundcard_sound = 0;
 				initmorse ();
 				wordmode = 0;
+                async_abort = 0;
 				cwdev->reset (cwdev);
 				debug ("Reset all values");
 				break;
@@ -565,14 +572,16 @@
 					cw_set_volume (morse_volume);
 				}
 				break;
-	    case 'h':   /* send echo to main program when CW playing is done */
-		memcpy(&reply_sin, &k_sin, sizeof(reply_sin)); /* remember sender */
-		reply_socklen = sin_len;
-		strncpy(reply_data+1, message, sizeof(reply_data) - 2);
-		reply_data[sizeof(reply_data) - 1] = '\0';
-		reply_data[0]='h';
-		if (strlen (message) + 1 <= MAXMORSE - 1) strcat (morsetext, "^");
-		break;
+			case 'h':   /* send echo to main program when CW playing is done */
+				pthread_mutex_lock(&k_sin_mutex);
+				memcpy(&reply_sin, &k_sin, sizeof(reply_sin)); /* remember sender */
+		                pthread_mutex_unlock(&k_sin_mutex);
+				reply_socklen = sin_len;
+				strncpy(reply_data+1, message, sizeof(reply_data) - 2);
+				reply_data[sizeof(reply_data) - 1] = '\0';
+				reply_data[0]='h';
+				if (strlen (message) + 1 <= MAXMORSE - 1) strcat (morsetext, "^");
+				break;
 			}
 			return 0;
 		}
@@ -606,15 +615,15 @@
 		}
 		else if (c == '~')
 			cw_set_gap (2); /* 2 dots time additional for the next char */
-	else if (c == '^'){
-	    debug("Echo '%s'", reply_data);
-	    if (strlen(reply_data)==0) return;
-	    sendto(socket_descriptor, reply_data, strlen(reply_data), 0, 
-		(struct sockaddr *)&reply_sin, reply_socklen);
-	    strcpy(reply_data,"");
-	    break;
-  
-	}
+		else if (c == '^')
+		{
+			debug("Echo '%s'", reply_data);
+			if (strlen(reply_data)==0) return;
+			sendto(socket_descriptor, reply_data, strlen(reply_data), 0, 
+			(struct sockaddr *)&reply_sin, reply_socklen);
+			strcpy(reply_data,"");
+			break;
+		}
 		else
 		{
 			validchar = 1;
@@ -636,6 +645,7 @@
 			debug ("Morse = %c", c);
 			cw_send_character (c);
 			cw_tone_queue_wait_critical (1);
+            if (async_abort) break;
 			if (cw_get_gap () == 2) cw_set_gap (0);
 		}
 		x++;
@@ -818,6 +828,44 @@
 	}
 }
 
+/* Reader thread. Reads data from socket_descriptor and send to main thread throug reader_pipe 
+ * Sets async_abort when abort command is received */
+
+void *reader(void *arg)
+{
+	int esc, recv_rc, i;
+	char buf[257];
+	struct sockaddr_in reader_sin;
+
+	esc=0;
+	while(1)
+	{
+		recv_rc = recvfrom (socket_descriptor, buf, sizeof (buf) - 1,
+					0, (struct sockaddr *) &reader_sin, &sin_len);
+    
+		printf("recvfrom: %d\n", recv_rc);
+		if (recv_rc<=0) continue;
+
+		pthread_mutex_lock(&k_sin_mutex);
+		memcpy(&k_sin, &reader_sin, sizeof(k_sin)); 
+		pthread_mutex_unlock(&k_sin_mutex);
+     
+		for (i=0;i<recv_rc;i++)
+		{
+			if (esc && buf[i]=='0')
+			{
+				printf("reader: abort\n");
+				async_abort=1;
+			}
+			esc=buf[i]==27;
+		}
+		write(reader_pipe[1], buf, recv_rc);
+	}
+	/* unreached */
+	return NULL;
+}
+    
+
 /* main program: fork, open network connection and go into an endless loop
    waiting for something to happen on the UDP port */
 int
@@ -948,8 +996,27 @@
 		errmsg ("Bind");
 		exit (1);
 	}
+    
+	if (socketpair(AF_UNIX, SOCK_DGRAM, 0, reader_pipe)) /* [0] for reading, [1] for writing */
+	{
+		errmsg ("Creating pipe");
+		exit (1);
+	}
+            
+	async_abort = 0;
+	if (pthread_create(&reader_thread, NULL, reader, NULL))
+	{
+		errmsg ("Creating thread");
+		exit (1);
+	}
+	if (pthread_mutex_init(&k_sin_mutex, NULL))
+	{
+		errmsg ("Creating mutex");
+		exit (1);
+	}
+
 
-	save_file_flags = fcntl (socket_descriptor, F_GETFL);
+	save_file_flags = fcntl (reader_pipe[0], F_GETFL);
 	if (save_file_flags == -1)
 	{
 		errmsg("Trying get flags");
@@ -957,7 +1024,7 @@
 	}
 	save_file_flags |= O_NONBLOCK;
 
-	if (fcntl (socket_descriptor, F_SETFL, save_file_flags) == -1)
+	if (fcntl (reader_pipe[0], F_SETFL, save_file_flags) == -1)
 	{
 		errmsg ("Trying non-blocking");
 		exit (1);
@@ -994,6 +1061,8 @@
 			}
 		}
 	}
+	/* unreached */
+	pthread_mutex_destroy(&k_sin_mutex);
 
 	cwdev->free (cwdev);
 	if (close (socket_descriptor) == -1)
@@ -1001,5 +1070,11 @@
 		errmsg ("Close socket");
 		exit (1);
 	}
+	if (close (reader_pipe[0]) == -1 ||
+        close (reader_pipe[1]) == -1)
+	{
+		errmsg ("Close socket");
+		exit (1);
+	}
 	exit (0);
 }
diff -ruN cwdaemon-0.9.2/cwlib.c cwdaemon-0.9.2mt/cwlib.c
--- cwdaemon-0.9.2/cwlib.c	2005-02-03 18:40:59.000000000 +0100
+++ cwdaemon-0.9.2mt/cwlib.c	2006-04-02 18:13:03.501329694 +0200
@@ -423,6 +423,7 @@
  */
 static int	cw_sk_key_down		= FALSE;/* Indicates key up or down */
 
+extern int async_abort;
 
 /**
  * cw_version()
@@ -2875,6 +2876,10 @@
 	while (queue_length > level)
 	    {
 		cw_signal_wait_internal ();
+		if (async_abort){
+		    cw_flush_tone_queue();
+		    return RC_SUCCESS;
+		}
 
 		/* Calculate the current queue length again. */
 		if (cw_tq_head >= cw_tq_tail)