| 1 |
/* |
| 2 |
* cwdaemon - morse sounding daemon for the parallel or serial port |
| 3 |
* Copyright (C) 2002 - 2005 Joop Stakenborg <[email protected]> |
| 4 |
* and many authors, see the AUTHORS file. |
| 5 |
* Copyright (C) 2012 - 2015 Kamil Ignacak <[email protected]> |
| 6 |
* |
| 7 |
* This program is free software; you can redistribute it and/or modify |
| 8 |
* it under the terms of the GNU General Public License as published by |
| 9 |
* the Free Software Foundation; either version 2 of the License, or |
| 10 |
* (at your option) any later version. |
| 11 |
* |
| 12 |
* This program is distributed in the hope that it will be useful, |
| 13 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 14 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 15 |
* GNU Library General Public License for more details. |
| 16 |
|
| 17 |
* You should have received a copy of the GNU General Public License |
| 18 |
* along with this program; if not, write to the Free Software |
| 19 |
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA |
| 20 |
* 02110-1301, USA. |
| 21 |
*/ |
| 22 |
|
| 23 |
#define _POSIX_C_SOURCE 200809L /* nanosleep(), strdup() */ |
| 24 |
#define _GNU_SOURCE /* getopt_long() */ |
| 25 |
|
| 26 |
#include "config.h" |
| 27 |
|
| 28 |
# if HAVE_STDIO_H |
| 29 |
# include <stdio.h> |
| 30 |
#endif |
| 31 |
#if HAVE_SYS_TYPES_H |
| 32 |
# include <sys/types.h> |
| 33 |
#endif |
| 34 |
#if HAVE_SYS_STAT_H |
| 35 |
# include <sys/stat.h> |
| 36 |
#endif |
| 37 |
#if STDC_HEADERS |
| 38 |
# include <stdlib.h> |
| 39 |
# include <stddef.h> |
| 40 |
#else |
| 41 |
# if HAVE_STDLIB_H |
| 42 |
# include <stdlib.h> |
| 43 |
# endif |
| 44 |
#endif |
| 45 |
#if HAVE_STRING_H |
| 46 |
# if !STDC_HEADERS && HAVE_MEMORY_H |
| 47 |
# include <memory.h> |
| 48 |
# endif |
| 49 |
# include <string.h> |
| 50 |
#endif |
| 51 |
#if HAVE_UNISTD_H |
| 52 |
# include <unistd.h> |
| 53 |
#endif |
| 54 |
|
| 55 |
#if DHAVE_ARPA_INET |
| 56 |
# include <arpa/inet.h> |
| 57 |
#endif |
| 58 |
#if HAVE_ARPA_INET_H |
| 59 |
# include <arpa/inet.h> |
| 60 |
#endif |
| 61 |
|
| 62 |
#if HAVE_FCNTL_H |
| 63 |
# include <fcntl.h> |
| 64 |
#endif |
| 65 |
#if HAVE_NETDB |
| 66 |
# include <netdb.h> |
| 67 |
#endif |
| 68 |
#if HAVE_NETINET_IN_H |
| 69 |
# include <netinet/in.h> |
| 70 |
#endif |
| 71 |
#if HAVE_SYS_SOCKET_H |
| 72 |
# include <sys/socket.h> |
| 73 |
#endif |
| 74 |
#if HAVE_SYSLOG_H |
| 75 |
# include <syslog.h> |
| 76 |
#endif |
| 77 |
#if HAVE_ERRNO_H |
| 78 |
# include <errno.h> |
| 79 |
#endif |
| 80 |
|
| 81 |
#if TIME_WITH_SYS_TIME |
| 82 |
# include <sys/time.h> |
| 83 |
# include <time.h> |
| 84 |
#else |
| 85 |
# if HAVE_SYS_TIME_H |
| 86 |
# include <sys/time.h> |
| 87 |
# else |
| 88 |
# include <time.h> |
| 89 |
# endif |
| 90 |
#endif |
| 91 |
|
| 92 |
#if HAVE_SIGNAL_H |
| 93 |
# include <signal.h> |
| 94 |
#endif |
| 95 |
#if HAVE_STDARG_H |
| 96 |
# include <stdarg.h> |
| 97 |
#endif |
| 98 |
#if HAVE_SYS_RESOURCE_H |
| 99 |
# include <sys/resource.h> |
| 100 |
#endif |
| 101 |
#if (defined(__unix__) || defined(unix)) && !defined(USG) |
| 102 |
# include <sys/param.h> |
| 103 |
#endif |
| 104 |
#include <limits.h> |
| 105 |
#include <assert.h> |
| 106 |
|
| 107 |
#if defined(HAVE_GETOPT_H) |
| 108 |
#include <getopt.h> |
| 109 |
#endif |
| 110 |
|
| 111 |
#include <stdint.h> /* uint32_t */ |
| 112 |
|
| 113 |
#include <libcw.h> |
| 114 |
#include <libcw_debug.h> |
| 115 |
#include "cwdaemon.h" |
| 116 |
|
| 117 |
|
| 118 |
/** |
| 119 |
\file cwdaemon.c |
| 120 |
|
| 121 |
cwdaemon exchanges data with client through messages. Most of |
| 122 |
messages are sent by client application to cwdaemon - those |
| 123 |
are called in this file "requests". On several occasions cwdaemon |
| 124 |
sends some data back to the client. Such messages are called |
| 125 |
"replies". |
| 126 |
|
| 127 |
message: |
| 128 |
- request |
| 129 |
- reply |
| 130 |
|
| 131 |
Size of a message is not constant. |
| 132 |
Maximal size of a message is CWDAEMON_MESSAGE_SIZE_MAX. |
| 133 |
|
| 134 |
|
| 135 |
|
| 136 |
cwdaemon can be configured either through command line arguments on |
| 137 |
start of the daemon, or through requests (escaped requests) sent |
| 138 |
over network. |
| 139 |
|
| 140 |
<verbatim> |
| 141 |
Feature command line argument escaped request |
| 142 |
--------------------------------------------------------------- |
| 143 |
help -h, --help N/A |
| 144 |
version -V, --version N/A |
| 145 |
keying device -d, --cwdevice 8 |
| 146 |
don't fork daemon -n, --nofork N/A |
| 147 |
network port -p, --port 9 (obsolete) |
| 148 |
process priority -P, --priority N/A |
| 149 |
Morse speed (wpm) -s, --wpm 2 |
| 150 |
PTT delay -t, --pttdelay d |
| 151 |
PTT keying on/off N/A a |
| 152 |
sound system -x, --system f |
| 153 |
sound volume -v, --volume g |
| 154 |
Morse weighting -w, --weighting 7 |
| 155 |
sound tone -T, --tone 3 |
| 156 |
debug verbosity -i N/A |
| 157 |
debug verbosity -y, --verbosity N/A |
| 158 |
libcw debug flags -I, --libcwflags N/A |
| 159 |
debug output -f, --debugfile N/A |
| 160 |
|
| 161 |
reset parameters N/A 0 |
| 162 |
abort message N/A 4 |
| 163 |
exit daemon N/A 5 |
| 164 |
set word mode N/A 6 |
| 165 |
set SSB way N/A b |
| 166 |
tune N/A c |
| 167 |
band switch N/A e |
| 168 |
|
| 169 |
</verbatim> |
| 170 |
|
| 171 |
|
| 172 |
*/ |
| 173 |
|
| 174 |
|
| 175 |
|
| 176 |
|
| 177 |
/* cwdaemon constants. */ |
| 178 |
#define CWDAEMON_MORSE_SPEED_DEFAULT 24 /* [wpm] */ |
| 179 |
#define CWDAEMON_MORSE_TONE_DEFAULT 800 /* [Hz] */ |
| 180 |
#define CWDAEMON_MORSE_VOLUME_DEFAULT 70 /* [%] */ |
| 181 |
|
| 182 |
/* TODO: why the limitation to 50 ms? Is it enough? */ |
| 183 |
#define CWDAEMON_PTT_DELAY_DEFAULT 0 /* [ms] */ |
| 184 |
#define CWDAEMON_PTT_DELAY_MIN 0 /* [ms] */ |
| 185 |
#define CWDAEMON_PTT_DELAY_MAX 50 /* [ms] */ |
| 186 |
|
| 187 |
/* Notice that the range accepted by cwdaemon is different than that |
| 188 |
accepted by libcw. */ |
| 189 |
#define CWDAEMON_MORSE_WEIGHTING_DEFAULT 0 |
| 190 |
#define CWDAEMON_MORSE_WEIGHTING_MIN -50 |
| 191 |
#define CWDAEMON_MORSE_WEIGHTING_MAX 50 |
| 192 |
|
| 193 |
#define CWDAEMON_NETWORK_PORT_DEFAULT 6789 |
| 194 |
#define CWDAEMON_AUDIO_SYSTEM_DEFAULT CW_AUDIO_CONSOLE /* Console buzzer, from libcw.h. */ |
| 195 |
#define CWDAEMON_VERBOSITY_DEFAULT CWDAEMON_VERBOSITY_W /* Threshold of verbosity of debug strings. */ |
| 196 |
|
| 197 |
#define CWDAEMON_USECS_PER_MSEC 1000 /* Just to avoid magic numbers. */ |
| 198 |
#define CWDAEMON_USECS_PER_SEC 1000000 /* Just to avoid magic numbers. */ |
| 199 |
#define CWDAEMON_MESSAGE_SIZE_MAX 256 /* Maximal size of single message. */ |
| 200 |
#define CWDAEMON_REQUEST_QUEUE_SIZE_MAX 4000 /* Maximal size of common buffer/fifo where requests may be pushed to. */ |
| 201 |
|
| 202 |
#define CWDAEMON_TUNE_SECONDS_MAX 10 /* Maximal time of tuning. TODO: why the limitation to 10 s? Is it enough? */ |
| 203 |
|
| 204 |
|
| 205 |
/* For developer's debugging purposes. */ |
| 206 |
//extern cw_debug_t cw_debug_object_dev; |
| 207 |
|
| 208 |
|
| 209 |
/* Default values of parameters, may be modified only through |
| 210 |
command line arguments passed to cwdaemon. |
| 211 |
|
| 212 |
After setting these variables with values passed in command line, |
| 213 |
these become the default state of cwdaemon. Values of default_* |
| 214 |
will be used when resetting libcw and cwdaemon to initial state. */ |
| 215 |
static int default_morse_speed = CWDAEMON_MORSE_SPEED_DEFAULT; |
| 216 |
static int default_morse_tone = CWDAEMON_MORSE_TONE_DEFAULT; |
| 217 |
static int default_morse_volume = CWDAEMON_MORSE_VOLUME_DEFAULT; |
| 218 |
static int default_ptt_delay = CWDAEMON_PTT_DELAY_DEFAULT; |
| 219 |
static int default_audio_system = CWDAEMON_AUDIO_SYSTEM_DEFAULT; |
| 220 |
static int default_weighting = CWDAEMON_MORSE_WEIGHTING_DEFAULT; |
| 221 |
static int default_verbosity = CWDAEMON_VERBOSITY_DEFAULT; |
| 222 |
|
| 223 |
|
| 224 |
/* Actual values of parameters, used to control ongoing operation of |
| 225 |
cwdaemon+libcw. These values can be modified through requests |
| 226 |
received from socket in cwdaemon_receive(). */ |
| 227 |
static int current_morse_speed = CWDAEMON_MORSE_SPEED_DEFAULT; |
| 228 |
static int current_morse_tone = CWDAEMON_MORSE_TONE_DEFAULT; |
| 229 |
static int current_morse_volume = CWDAEMON_MORSE_VOLUME_DEFAULT; |
| 230 |
static int current_ptt_delay = CWDAEMON_PTT_DELAY_DEFAULT; |
| 231 |
static int current_audio_system = CWDAEMON_AUDIO_SYSTEM_DEFAULT; |
| 232 |
static int current_weighting = CWDAEMON_MORSE_WEIGHTING_DEFAULT; |
| 233 |
static int current_verbosity = CWDAEMON_VERBOSITY_DEFAULT; |
| 234 |
|
| 235 |
/* Level of libcw's tone queue that triggers 'callback for low level |
| 236 |
in tone queue'. The callback function is |
| 237 |
cwdaemon_tone_queue_low_callback(), it is registered with |
| 238 |
cw_register_tone_queue_low_callback(). |
| 239 |
|
| 240 |
I REALLY don't think that you would want to set it to any value |
| 241 |
other than '1'. */ |
| 242 |
static const int tq_low_watermark = 1; |
| 243 |
|
| 244 |
/* Quick and dirty solution to following problem: when cwdaemon for |
| 245 |
some reason fails to open audio output, and attempts to play |
| 246 |
characters received from client, it crashes. It doesn't know that |
| 247 |
it attempts to play to closed audio output. |
| 248 |
|
| 249 |
This is a flag telling cwdaemon if audio output is available or not. |
| 250 |
|
| 251 |
TODO: the variable is almost unused. Start using it. |
| 252 |
|
| 253 |
TODO: decide on terminology: "audio system" or "sound system". */ |
| 254 |
static bool has_audio_output = false; |
| 255 |
|
| 256 |
|
| 257 |
|
| 258 |
|
| 259 |
/* Network variables. */ |
| 260 |
|
| 261 |
/* cwdaemon usually receives requests from client, but on occasions |
| 262 |
it needs to send a reply back. This is why in addition to |
| 263 |
request_* we also have reply_* */ |
| 264 |
|
| 265 |
static int socket_descriptor = 0; |
| 266 |
|
| 267 |
/* Default UDP port we listen on. Can be changed only through command |
| 268 |
line switch. |
| 269 |
|
| 270 |
There is a code path suggesting that it was possible to change the |
| 271 |
port using network request, but now this code path is marked as |
| 272 |
"obsolete". |
| 273 |
*/ |
| 274 |
static int port = CWDAEMON_NETWORK_PORT_DEFAULT; |
| 275 |
|
| 276 |
static struct sockaddr_in request_addr; |
| 277 |
static socklen_t request_addrlen; |
| 278 |
|
| 279 |
static struct sockaddr_in reply_addr; |
| 280 |
static socklen_t reply_addrlen; |
| 281 |
|
| 282 |
static char reply_buffer[CWDAEMON_MESSAGE_SIZE_MAX]; |
| 283 |
|
| 284 |
|
| 285 |
|
| 286 |
/* Debug variables. */ |
| 287 |
|
| 288 |
/* cwdaemon may print debug strings to a disc file instead of |
| 289 |
stdout. */ |
| 290 |
static FILE *cwdaemon_debug_f = NULL; |
| 291 |
static char *cwdaemon_debug_f_path = NULL; |
| 292 |
|
| 293 |
/* This table is addressed with values defined in "enum |
| 294 |
cwdaemon_verbosity" (src/cwdaemon.h). */ |
| 295 |
static const char *cwdaemon_verbosity_labels[] = { |
| 296 |
"NN", /* None - obviously this label will never be used. */ |
| 297 |
"EE", /* Error. */ |
| 298 |
"WW", /* Warning. */ |
| 299 |
"II", /* Information. */ |
| 300 |
"DD" }; /* Debug. */ |
| 301 |
|
| 302 |
/* An integer that is a result of ORing libcw's debug flags. See |
| 303 |
libcw.h (or is it libcw_debug.h?) for numeric values of the |
| 304 |
flags. */ |
| 305 |
static long int libcw_debug_flags = 0; |
| 306 |
|
| 307 |
|
| 308 |
|
| 309 |
/* Various variables. */ |
| 310 |
static int wordmode = 0; /* Start in character mode. */ |
| 311 |
static int forking = 1; /* We fork by default. */ |
| 312 |
static int process_priority = 0; /* Scheduling priority of cwdaemon process. */ |
| 313 |
static int async_abort = 0; /* Unused variable. It is used in patches/cwdaemon-mt.patch though. */ |
| 314 |
static int inactivity_seconds = 9999; /* Inactive since nnn seconds. */ |
| 315 |
|
| 316 |
|
| 317 |
/* Incoming requests without Escape code are stored in this pseudo-FIFO |
| 318 |
before they are played. */ |
| 319 |
static char request_queue[CWDAEMON_REQUEST_QUEUE_SIZE_MAX]; |
| 320 |
|
| 321 |
|
| 322 |
|
| 323 |
/* Flag for PTT state/behaviour. */ |
| 324 |
static unsigned char ptt_flag = 0; |
| 325 |
|
| 326 |
/* Automatically turn PTT on and off. |
| 327 |
Turn PTT on when starting to play Morse characters, and turn PTT off when |
| 328 |
there are no more characters to play. |
| 329 |
"Automatically" means that cwdaemon toggles PTT without any additional |
| 330 |
actions taken by client. Client doesn't have to tell cwdaemon when to |
| 331 |
turn PTT on/off - this is done by cwdaemon itself, automatically. |
| 332 |
|
| 333 |
If ptt delay is non-zero, cwdaemon performs delay between turning PTT on |
| 334 |
and starting to play Morse characters. |
| 335 |
TODO: is there a delay before turning PTT off? */ |
| 336 |
#define PTT_ACTIVE_AUTO 0x01 |
| 337 |
|
| 338 |
/* PTT is turned on and off manually. |
| 339 |
It is the client who decides when to turn the PTT on and off. |
| 340 |
The client has to send 'a' escape code, followed by '1' or '0' to |
| 341 |
'manually' turn PTT on or off. |
| 342 |
'MANUAL' - the opposite of 'AUTO' where it is cwdaemon that decides |
| 343 |
when to turn PTT on and off. |
| 344 |
Perhaps "PTT_ON_REQUEST" would be a better name of the constant. */ |
| 345 |
#define PTT_ACTIVE_MANUAL 0x02 |
| 346 |
|
| 347 |
/* Don't turn PTT off until cwdaemon sends back an echo to client. |
| 348 |
client may request echoing back to it a reply when cwdaemon finishes |
| 349 |
playing given request. PTT shouldn't be turned off when sending the |
| 350 |
reply (TODO: why it shouldn't?). |
| 351 |
|
| 352 |
This flag is set whenever client sends request for sending back a |
| 353 |
reply (i.e. either <ESC>h request, or caret request). |
| 354 |
|
| 355 |
This flag is re-set whenever such reply is sent (to be more |
| 356 |
precise: after playing a requested text, but just before sending to |
| 357 |
the client the requested reply). */ |
| 358 |
#define PTT_ACTIVE_ECHO 0x04 |
| 359 |
|
| 360 |
|
| 361 |
|
| 362 |
void cwdaemon_set_ptt_on(cwdevice *device, const char *info); |
| 363 |
void cwdaemon_set_ptt_off(cwdevice *device, const char *info); |
| 364 |
void cwdaemon_switch_band(cwdevice *device, unsigned int band); |
| 365 |
|
| 366 |
void cwdaemon_play_request(char *request); |
| 367 |
|
| 368 |
void cwdaemon_tune(uint32_t seconds); |
| 369 |
void cwdaemon_keyingevent(void *arg, int keystate); |
| 370 |
void cwdaemon_prepare_reply(char *reply, const char *request, size_t n); |
| 371 |
void cwdaemon_tone_queue_low_callback(void *arg); |
| 372 |
|
| 373 |
bool cwdaemon_initialize_socket(void); |
| 374 |
void cwdaemon_close_socket(void); |
| 375 |
ssize_t cwdaemon_sendto(const char *reply); |
| 376 |
int cwdaemon_recvfrom(char *request, int n); |
| 377 |
int cwdaemon_receive(void); |
| 378 |
void cwdaemon_handle_escaped_request(char *request); |
| 379 |
|
| 380 |
void cwdaemon_reset_almost_all(void); |
| 381 |
|
| 382 |
/* Functions managing cwdevices. */ |
| 383 |
bool cwdaemon_cwdevices_init(void); |
| 384 |
void cwdaemon_cwdevices_free(void); |
| 385 |
void cwdaemon_cwdevice_init(void); |
| 386 |
bool cwdaemon_cwdevice_set(cwdevice **device, const char *desc); |
| 387 |
void cwdaemon_cwdevice_free(void); |
| 388 |
|
| 389 |
/* Functions managing libcw output. */ |
| 390 |
bool cwdaemon_open_libcw_output(int audio_system); |
| 391 |
void cwdaemon_close_libcw_output(void); |
| 392 |
void cwdaemon_reset_libcw_output(void); |
| 393 |
|
| 394 |
/* Utility functions. */ |
| 395 |
bool cwdaemon_get_long(const char *buf, long *lvp); |
| 396 |
void cwdaemon_udelay(unsigned long us); |
| 397 |
|
| 398 |
|
| 399 |
|
| 400 |
static void cwdaemon_args_parse(int argc, char *argv[]); |
| 401 |
static void cwdaemon_args_process_short(int c, const char *optarg); |
| 402 |
static void cwdaemon_args_process_long(int argc, char *argv[]); |
| 403 |
static void cwdaemon_args_help(void); |
| 404 |
|
| 405 |
/* These two are called only in code handling command line options. No |
| 406 |
request can prompt cwdaemon to inform about version or to fork. |
| 407 |
|
| 408 |
While it would make some sense to make request to get version of |
| 409 |
cwdaemon while it is already running, it is impossible to request |
| 410 |
non-forking of daemon that - after parsing command line options - |
| 411 |
has already forked. */ |
| 412 |
static void cwdaemon_params_version(void); |
| 413 |
static void cwdaemon_params_nofork(void); |
| 414 |
|
| 415 |
static bool cwdaemon_params_cwdevice(const char *optarg); |
| 416 |
static bool cwdaemon_params_port(const char *optarg); |
| 417 |
static bool cwdaemon_params_priority(int *priority, const char *optarg); |
| 418 |
static bool cwdaemon_params_wpm(int *wpm, const char *optarg); |
| 419 |
static bool cwdaemon_params_tune(uint32_t *seconds, const char *optarg); |
| 420 |
static int cwdaemon_params_pttdelay(int *delay, const char *optarg); |
| 421 |
static bool cwdaemon_params_volume(int *volume, const char *optarg); |
| 422 |
static bool cwdaemon_params_weighting(int *weighting, const char *optarg); |
| 423 |
static bool cwdaemon_params_tone(int *tone, const char *optarg); |
| 424 |
static void cwdaemon_params_inc_verbosity(int *verbosity); |
| 425 |
static bool cwdaemon_params_set_verbosity(int *verbosity, const char *optarg); |
| 426 |
static bool cwdaemon_params_libcwflags(const char *optarg); |
| 427 |
static bool cwdaemon_params_debugfile(const char *optarg); |
| 428 |
static bool cwdaemon_params_system(int *system, const char *optarg); |
| 429 |
static bool cwdaemon_params_ptt_on_off(const char *optarg); |
| 430 |
|
| 431 |
|
| 432 |
|
| 433 |
static void cwdaemon_debug_open(void); |
| 434 |
static void cwdaemon_debug_close(void); |
| 435 |
|
| 436 |
/* Auto, manual, echo. */ |
| 437 |
static char cwdaemon_debug_ptt_flag[3 + 1]; |
| 438 |
static const char *cwdaemon_debug_ptt_flags(void); |
| 439 |
|
| 440 |
RETSIGTYPE cwdaemon_catch_sigint(int signal); |
| 441 |
|
| 442 |
|
| 443 |
cwdevice cwdevice_ttys = { |
| 444 |
.init = ttys_init, |
| 445 |
.free = ttys_free, |
| 446 |
.reset = ttys_reset, |
| 447 |
.cw = ttys_cw, |
| 448 |
.ptt = ttys_ptt, |
| 449 |
.ssbway = NULL, |
| 450 |
.switchband = NULL, |
| 451 |
.footswitch = NULL, |
| 452 |
.fd = 0, |
| 453 |
.desc = NULL |
| 454 |
}; |
| 455 |
|
| 456 |
cwdevice cwdevice_null = { |
| 457 |
.init = null_init, |
| 458 |
.free = null_free, |
| 459 |
.reset = null_reset, |
| 460 |
.cw = null_cw, |
| 461 |
.ptt = null_ptt, |
| 462 |
.ssbway = NULL, |
| 463 |
.switchband = NULL, |
| 464 |
.footswitch = NULL, |
| 465 |
.fd = 0, |
| 466 |
.desc = NULL |
| 467 |
}; |
| 468 |
|
| 469 |
#if defined (HAVE_LINUX_PPDEV_H) || defined (HAVE_DEV_PPBUS_PPI_H) |
| 470 |
cwdevice cwdevice_lp = { |
| 471 |
.init = lp_init, |
| 472 |
.free = lp_free, |
| 473 |
.reset = lp_reset, |
| 474 |
.cw = lp_cw, |
| 475 |
.ptt = lp_ptt, |
| 476 |
.ssbway = lp_ssbway, |
| 477 |
.switchband = lp_switchband, |
| 478 |
.footswitch = lp_footswitch, |
| 479 |
.fd = 0, |
| 480 |
.desc = NULL |
| 481 |
}; |
| 482 |
#endif |
| 483 |
|
| 484 |
|
| 485 |
|
| 486 |
/* Selected keying device: |
| 487 |
serial port (cwdevice_ttys) || parallel port (cwdevice_lp) || null (cwdevice_null). |
| 488 |
It should be configured with cwdaemon_cwdevice_set(). */ |
| 489 |
/* FIXME: if no device is specified in command line, and no physical |
| 490 |
device is available, the global_cwdevice is NULL, which causes the |
| 491 |
program to break. */ |
| 492 |
static cwdevice *global_cwdevice = NULL; |
| 493 |
|
| 494 |
|
| 495 |
|
| 496 |
|
| 497 |
|
| 498 |
/* catch ^C when running in foreground */ |
| 499 |
RETSIGTYPE cwdaemon_catch_sigint(__attribute__((unused)) int signal) |
| 500 |
{ |
| 501 |
printf("%s: Exiting\n", PACKAGE); |
| 502 |
exit(EXIT_SUCCESS); |
| 503 |
} |
| 504 |
|
| 505 |
|
| 506 |
|
| 507 |
|
| 508 |
|
| 509 |
/** |
| 510 |
\brief Print error string to the console or syslog |
| 511 |
|
| 512 |
Function checks if cwdaemon has forked, and prints given error string |
| 513 |
to stdout (if cwdaemon hasn't forked) or to syslog (if cwdaemon has |
| 514 |
forked). |
| 515 |
|
| 516 |
Function accepts printf-line formatting string as first argument, and |
| 517 |
a set of optional arguments to be inserted into the formatting string. |
| 518 |
|
| 519 |
\param format - first part of an error string, a formatting string |
| 520 |
*/ |
| 521 |
void cwdaemon_errmsg(const char *format, ...) |
| 522 |
{ |
| 523 |
va_list ap; |
| 524 |
char s[1025]; |
| 525 |
|
| 526 |
va_start(ap, format); |
| 527 |
vsnprintf(s, 1024, format, ap); |
| 528 |
va_end(ap); |
| 529 |
|
| 530 |
if (forking) { |
| 531 |
syslog(LOG_ERR, "%s\n", s); |
| 532 |
} else { |
| 533 |
printf("%s: %s failed: \"%s\"\n", PACKAGE, s, strerror(errno)); |
| 534 |
fflush(stdout); |
| 535 |
} |
| 536 |
|
| 537 |
return; |
| 538 |
} |
| 539 |
|
| 540 |
|
| 541 |
|
| 542 |
|
| 543 |
|
| 544 |
/** |
| 545 |
\brief Print debug string to debug file |
| 546 |
|
| 547 |
Function decides if given \p verbosity level is sufficient to print |
| 548 |
given \p format debug string, and prints it to predefined file. If |
| 549 |
current global verbosity level is "None", no information will be |
| 550 |
printed. |
| 551 |
|
| 552 |
Currently \p verbosity can have one of values defined in enum |
| 553 |
cwdaemon_verbosity. |
| 554 |
|
| 555 |
Function accepts printf-line formatting string as last named |
| 556 |
argument (\p format), and a set of optional arguments to be |
| 557 |
inserted into the formatting string. |
| 558 |
|
| 559 |
\param verbosity - verbosity level of given debug string |
| 560 |
\param format - formatting string of a debug string being printed |
| 561 |
*/ |
| 562 |
void cwdaemon_debug(int verbosity, __attribute__((unused)) const char *func, __attribute__((unused)) int line, const char *format, ...) |
| 563 |
{ |
| 564 |
if (current_verbosity > CWDAEMON_VERBOSITY_N |
| 565 |
&& verbosity <= current_verbosity |
| 566 |
&& cwdaemon_debug_f) { |
| 567 |
|
| 568 |
va_list ap; |
| 569 |
char s[1024 + 1]; |
| 570 |
va_start(ap, format); |
| 571 |
vsnprintf(s, 1024, format, ap); |
| 572 |
va_end(ap); |
| 573 |
|
| 574 |
fprintf(cwdaemon_debug_f, "%s:%s: %s\n", PACKAGE, cwdaemon_verbosity_labels[verbosity], s); |
| 575 |
// fprintf(cwdaemon_debug_f, "cwdaemon: %s(): %d\n", func, line); |
| 576 |
fflush(cwdaemon_debug_f); |
| 577 |
} |
| 578 |
|
| 579 |
return; |
| 580 |
} |
| 581 |
|
| 582 |
|
| 583 |
|
| 584 |
|
| 585 |
void cwdaemon_debug_close(void) |
| 586 |
{ |
| 587 |
if (cwdaemon_debug_f |
| 588 |
&& cwdaemon_debug_f != stdout |
| 589 |
&& cwdaemon_debug_f != stderr) { |
| 590 |
|
| 591 |
fclose(cwdaemon_debug_f); |
| 592 |
cwdaemon_debug_f = NULL; |
| 593 |
} |
| 594 |
|
| 595 |
return; |
| 596 |
} |
| 597 |
|
| 598 |
|
| 599 |
|
| 600 |
|
| 601 |
|
| 602 |
const char *cwdaemon_debug_ptt_flags(void) |
| 603 |
{ |
| 604 |
|
| 605 |
if (ptt_flag & PTT_ACTIVE_AUTO) { |
| 606 |
cwdaemon_debug_ptt_flag[0] = 'A'; |
| 607 |
} else { |
| 608 |
cwdaemon_debug_ptt_flag[0] = 'a'; |
| 609 |
} |
| 610 |
|
| 611 |
if (ptt_flag & PTT_ACTIVE_MANUAL) { |
| 612 |
cwdaemon_debug_ptt_flag[1] = 'M'; |
| 613 |
} else { |
| 614 |
cwdaemon_debug_ptt_flag[1] = 'm'; |
| 615 |
} |
| 616 |
|
| 617 |
if (ptt_flag & PTT_ACTIVE_ECHO) { |
| 618 |
cwdaemon_debug_ptt_flag[2] = 'E'; |
| 619 |
} else { |
| 620 |
cwdaemon_debug_ptt_flag[2] = 'e'; |
| 621 |
} |
| 622 |
|
| 623 |
cwdaemon_debug_ptt_flag[3] = '\0'; |
| 624 |
|
| 625 |
return cwdaemon_debug_ptt_flag; |
| 626 |
} |
| 627 |
|
| 628 |
|
| 629 |
|
| 630 |
|
| 631 |
|
| 632 |
/** |
| 633 |
\brief Sleep for specified amount of microseconds |
| 634 |
|
| 635 |
Function can detect an interrupt from a signal, and continue sleeping, |
| 636 |
but only once. |
| 637 |
|
| 638 |
\param us - microseconds to sleep |
| 639 |
*/ |
| 640 |
void cwdaemon_udelay(unsigned long us) |
| 641 |
{ |
| 642 |
struct timespec sleeptime, time_remainder; |
| 643 |
|
| 644 |
sleeptime.tv_sec = 0; |
| 645 |
sleeptime.tv_nsec = us * 1000; |
| 646 |
|
| 647 |
if (nanosleep(&sleeptime, &time_remainder) == -1) { |
| 648 |
if (errno == EINTR) { |
| 649 |
nanosleep(&time_remainder, NULL); |
| 650 |
} else { |
| 651 |
cwdaemon_errmsg("Nanosleep"); |
| 652 |
} |
| 653 |
} |
| 654 |
|
| 655 |
return; |
| 656 |
} |
| 657 |
|
| 658 |
|
| 659 |
|
| 660 |
|
| 661 |
|
| 662 |
/** |
| 663 |
\brief Band switch function using LPT port |
| 664 |
|
| 665 |
Band switch function using LPT (parallel) port of PC computer. |
| 666 |
|
| 667 |
In general, data is transmitted through LPT using pins 9 (MSB) - 2 (LSB). |
| 668 |
It seems that "TR Log" software has established a standard for controlling |
| 669 |
band switches using LPT port (or "TR Log" has just followed already |
| 670 |
established standard. Original information in cwdaemon's documentation |
| 671 |
mentions: "Pinout is compatible with the standard (CT, TRlog)."). |
| 672 |
|
| 673 |
cwdaemon follows the standard. The standard utilizes only a subset of |
| 674 |
the data pins: pins 9, 8, 7, and 2. |
| 675 |
|
| 676 |
From TR Log manual, version 6.79, Appendix A, Table A.3 |
| 677 |
(the manual is available at www.trlog.com): |
| 678 |
|
| 679 |
Hex value is transmitted through pins 9 (MSB), 8, 7, and 2 (LSB) |
| 680 |
|
| 681 |
Band Value |
| 682 |
160 1 |
| 683 |
80 2 |
| 684 |
40 3 |
| 685 |
30 4 |
| 686 |
20 5 |
| 687 |
17 6 |
| 688 |
15 7 |
| 689 |
12 8 |
| 690 |
10 9 |
| 691 |
6 A |
| 692 |
2 B |
| 693 |
222 C |
| 694 |
432 D |
| 695 |
902 E |
| 696 |
1GHz F |
| 697 |
Other or None 0 |
| 698 |
|
| 699 |
|
| 700 |
|
| 701 |
The function works only for a subset of devices that are able to |
| 702 |
perform band switching. Currently the only such device is parallel port. |
| 703 |
|
| 704 |
\brief device - device to use to switch band |
| 705 |
\brief band - band number to switch to (a hex number) |
| 706 |
*/ |
| 707 |
#if defined (HAVE_LINUX_PPDEV_H) || defined (HAVE_DEV_PPBUS_PPI_H) |
| 708 |
void cwdaemon_switch_band(cwdevice *device, unsigned int band) |
| 709 |
{ |
| 710 |
unsigned int bit_pattern = (band & 0x01) | ((band & 0x0e) << 4); |
| 711 |
if (device->switchband) { |
| 712 |
device->switchband(device, bit_pattern); |
| 713 |
cwdaemon_debug(CWDAEMON_VERBOSITY_I, __func__, __LINE__, "set band switch to %x", band); |
| 714 |
} else { |
| 715 |
cwdaemon_debug(CWDAEMON_VERBOSITY_E, __func__, __LINE__, "band switch output not implemented"); |
| 716 |
} |
| 717 |
|
| 718 |
return; |
| 719 |
} |
| 720 |
#endif |
| 721 |
|
| 722 |
|
| 723 |
|
| 724 |
|
| 725 |
|
| 726 |
/** |
| 727 |
\brief Switch PTT on |
| 728 |
|
| 729 |
\param device - current keying device |
| 730 |
\param info - debug information displayed when performing the switching |
| 731 |
*/ |
| 732 |
void cwdaemon_set_ptt_on(cwdevice *device, const char *info) |
| 733 |
{ |
| 734 |
/* For backward compatibility it is assumed that ptt_delay=0 |
| 735 |
means "cwdaemon shouldn't turn PTT on, at all". */ |
| 736 |
|
| 737 |
if (current_ptt_delay && !(ptt_flag & PTT_ACTIVE_AUTO)) { |
| 738 |
device->ptt(device, ON); |
| 739 |
cwdaemon_debug(CWDAEMON_VERBOSITY_I, __func__, __LINE__, info); |
| 740 |
|
| 741 |
|
| 742 |
#if 0 |
| 743 |
int rv = cw_queue_tone(current_ptt_delay * CWDAEMON_USECS_PER_MSEC, 0); /* try to 'enqueue' delay */ |
| 744 |
if (rv == CW_FAILURE) { /* Old libcw may reject freq=0. */ |
| 745 |
cwdaemon_debug(CWDAEMON_VERBOSITY_E, __func__, __LINE__, |
| 746 |
"cw_queue_tone() failed: rv=%d errno=\"%s\", using udelay() instead", |
| 747 |
rv, strerror(errno)); |
| 748 |
/* TODO: wouldn't it be simpler to not to call |
| 749 |
cw_queue_tone() and use only cwdaemon_udelay()? */ |
| 750 |
cwdaemon_udelay(current_ptt_delay * CWDAEMON_USECS_PER_MSEC); |
| 751 |
} |
| 752 |
#else |
| 753 |
cwdaemon_udelay(current_ptt_delay * CWDAEMON_USECS_PER_MSEC); |
| 754 |
#endif |
| 755 |
|
| 756 |
ptt_flag |= PTT_ACTIVE_AUTO; |
| 757 |
cwdaemon_debug(CWDAEMON_VERBOSITY_D, __func__, __LINE__, "PTT flag +PTT_ACTIVE_AUTO (0x%02x/%s)", ptt_flag, cwdaemon_debug_ptt_flags()); |
| 758 |
} |
| 759 |
|
| 760 |
return; |
| 761 |
} |
| 762 |
|
| 763 |
|
| 764 |
|
| 765 |
|
| 766 |
|
| 767 |
/** |
| 768 |
\brief Switch PTT off |
| 769 |
|
| 770 |
\param device - current keying device |
| 771 |
\param info - debug information displayed when performing the switching |
| 772 |
*/ |
| 773 |
void cwdaemon_set_ptt_off(cwdevice *device, const char *info) |
| 774 |
{ |
| 775 |
device->ptt(device, OFF); |
| 776 |
ptt_flag = 0; |
| 777 |
cwdaemon_debug(CWDAEMON_VERBOSITY_D, __func__, __LINE__, "PTT flag = 0 (0x%02x/%s)", ptt_flag, cwdaemon_debug_ptt_flags()); |
| 778 |
|
| 779 |
cwdaemon_debug(CWDAEMON_VERBOSITY_I, __func__, __LINE__, info); |
| 780 |
|
| 781 |
return; |
| 782 |
} |
| 783 |
|
| 784 |
|
| 785 |
|
| 786 |
|
| 787 |
|
| 788 |
/** |
| 789 |
\brief Tune for a number of seconds |
| 790 |
|
| 791 |
Play a continuous sound for a given number of seconds. |
| 792 |
|
| 793 |
Parameter type is uint32_t, which gives us maximum of 4294967295 |
| 794 |
seconds, i.e. ~136 years. Should be enough. |
| 795 |
|
| 796 |
TODO: change the argument type to size_t. |
| 797 |
|
| 798 |
\param seconds - time of tuning |
| 799 |
*/ |
| 800 |
void cwdaemon_tune(uint32_t seconds) |
| 801 |
{ |
| 802 |
if (seconds > 0) { |
| 803 |
cw_flush_tone_queue(); |
| 804 |
cwdaemon_set_ptt_on(global_cwdevice, "PTT (TUNE) on"); |
| 805 |
|
| 806 |
/* make it similar to normal CW, allowing interrupt */ |
| 807 |
for (uint32_t i = 0; i < seconds; i++) { |
| 808 |
cw_queue_tone(CWDAEMON_USECS_PER_SEC, current_morse_tone); |
| 809 |
} |
| 810 |
|
| 811 |
cw_send_character('e'); /* append minimal tone to return to normal flow */ |
| 812 |
} |
| 813 |
|
| 814 |
return; |
| 815 |
} |
| 816 |
|
| 817 |
|
| 818 |
|
| 819 |
|
| 820 |
|
| 821 |
/** |
| 822 |
\brief Reset some initial parameters of cwdaemon and libcw |
| 823 |
|
| 824 |
TODO: split this function into: |
| 825 |
cwdaemon_reset_basic_params() |
| 826 |
cwdaemon_reset_libcw_output() |
| 827 |
and call these two functions separately instead of this one. |
| 828 |
This function that combines these two doesn't make much sense. |
| 829 |
*/ |
| 830 |
void cwdaemon_reset_almost_all(void) |
| 831 |
{ |
| 832 |
current_morse_speed = default_morse_speed; |
| 833 |
current_morse_tone = default_morse_tone; |
| 834 |
current_morse_volume = default_morse_volume; |
| 835 |
current_audio_system = default_audio_system; |
| 836 |
current_ptt_delay = default_ptt_delay; |
| 837 |
current_weighting = default_weighting; |
| 838 |
|
| 839 |
/* Right now there is no way to alter current_verbosity after |
| 840 |
start of daemon, but it's easy to imagine a new network |
| 841 |
request to modify verbosity. Maybe not very useful (to |
| 842 |
change verbosity of debug strings displayed on remote |
| 843 |
machine), but during development phase it may be useful. |
| 844 |
|
| 845 |
Anyway... Right now there is no such request, but for |
| 846 |
consistency I'm resetting the current_verbosity as well. */ |
| 847 |
current_verbosity = default_verbosity; |
| 848 |
|
| 849 |
cwdaemon_reset_libcw_output(); |
| 850 |
|
| 851 |
return; |
| 852 |
} |
| 853 |
|
| 854 |
|
| 855 |
|
| 856 |
|
| 857 |
|
| 858 |
/** |
| 859 |
\brief Open audio sink using libcw |
| 860 |
|
| 861 |
\param audio_system - audio system to be used by libcw |
| 862 |
|
| 863 |
\return -1 on failure |
| 864 |
\return 0 otherwise |
| 865 |
*/ |
| 866 |
bool cwdaemon_open_libcw_output(int audio_system) |
| 867 |
{ |
| 868 |
int rv = cw_generator_new(audio_system, NULL); |
| 869 |
if (audio_system == CW_AUDIO_OSS && rv == CW_FAILURE) { |
| 870 |
/* When reopening libcw output, previous audio system may |
| 871 |
block audio device for a short period of time after the |
| 872 |
output has been closed. In such a situation OSS may fail |
| 873 |
to open audio device. Let's give it some time. */ |
| 874 |
for (int i = 0; i < 5; i++) { |
| 875 |
cwdaemon_debug(CWDAEMON_VERBOSITY_I, __func__, __LINE__, "delaying switching to OSS, please wait few seconds."); |
| 876 |
sleep(4); |
| 877 |
rv = cw_generator_new(audio_system, NULL); |
| 878 |
if (rv == CW_SUCCESS) { |
| 879 |
break; |
| 880 |
} |
| 881 |
} |
| 882 |
} |
| 883 |
if (rv != CW_FAILURE) { |
| 884 |
rv = cw_generator_start(); |
| 885 |
cwdaemon_debug(CWDAEMON_VERBOSITY_I, __func__, __LINE__, "starting generator with sound system \"%s\": %s", cw_get_audio_system_label(audio_system), rv ? "success" : "failure"); |
| 886 |
|
| 887 |
} else { |
| 888 |
/* FIXME: |
| 889 |
When cwdaemon failed to create a generator, and user |
| 890 |
kills non-forked cwdaemon through Ctrl+C, there is |
| 891 |
a memory protection error. |
| 892 |
|
| 893 |
It seems that this error has been fixed with |
| 894 |
changes in libcw, committed on 31.12.2012. |
| 895 |
To be observed. */ |
| 896 |
cwdaemon_debug(CWDAEMON_VERBOSITY_E, __func__, __LINE__, "failed to create generator with sound system \"%s\"", cw_get_audio_system_label(audio_system)); |
| 897 |
} |
| 898 |
|
| 899 |
return rv == CW_FAILURE ? false : true; |
| 900 |
} |
| 901 |
|
| 902 |
|
| 903 |
|
| 904 |
|
| 905 |
|
| 906 |
/** |
| 907 |
\brief Close libcw audio output |
| 908 |
*/ |
| 909 |
void cwdaemon_close_libcw_output(void) |
| 910 |
{ |
| 911 |
cw_generator_stop(); |
| 912 |
cw_generator_delete(); |
| 913 |
|
| 914 |
return; |
| 915 |
} |
| 916 |
|
| 917 |
|
| 918 |
|
| 919 |
|
| 920 |
|
| 921 |
/** |
| 922 |
\brief Reset parameters of libcw to default values |
| 923 |
|
| 924 |
Function uses values of cwdaemon's global 'default_' variables, and some |
| 925 |
other values to reset state of libcw. |
| 926 |
*/ |
| 927 |
void cwdaemon_reset_libcw_output(void) |
| 928 |
{ |
| 929 |
/* This function is called when cwdaemon receives '0' escape code. |
| 930 |
README describes this code as "Reset to default values". |
| 931 |
Therefore we use default_* below. |
| 932 |
|
| 933 |
However, the function is called after "current_" values |
| 934 |
have been reset to "default_" values. So maybe we could use |
| 935 |
"current_" values and somehow encapsulate the calls to |
| 936 |
cw_set_*() functions? The calls are also made elsewhere. |
| 937 |
*/ |
| 938 |
|
| 939 |
/* Delete old generator (if it exists). */ |
| 940 |
cwdaemon_close_libcw_output(); |
| 941 |
|
| 942 |
cwdaemon_debug(CWDAEMON_VERBOSITY_I, __func__, __LINE__, "setting sound system \"%s\"", cw_get_audio_system_label(default_audio_system)); |
| 943 |
|
| 944 |
if (cwdaemon_open_libcw_output(default_audio_system)) { |
| 945 |
has_audio_output = true; |
| 946 |
} else { |
| 947 |
has_audio_output = false; |
| 948 |
return; |
| 949 |
} |
| 950 |
|
| 951 |
/* Remember that tone queue is bound to a generator. When |
| 952 |
cwdaemon switches on request to other sound system, it will |
| 953 |
have to re-register the callback. */ |
| 954 |
cw_register_tone_queue_low_callback(cwdaemon_tone_queue_low_callback, NULL, tq_low_watermark); |
| 955 |
|
| 956 |
cw_set_frequency(default_morse_tone); |
| 957 |
cw_set_send_speed(default_morse_speed); |
| 958 |
cw_set_volume(default_morse_volume); |
| 959 |
cw_set_gap(0); |
| 960 |
cw_set_weighting(default_weighting * 0.6 + CWDAEMON_MORSE_WEIGHTING_MAX); |
| 961 |
|
| 962 |
return; |
| 963 |
} |
| 964 |
|
| 965 |
|
| 966 |
|
| 967 |
|
| 968 |
|
| 969 |
/** |
| 970 |
\brief Properly parse a 'long' integer |
| 971 |
|
| 972 |
Parse a string with digits, convert it to a long integer |
| 973 |
|
| 974 |
\param buf - input buffer with a string |
| 975 |
\param lvp - pointer to output long int variable |
| 976 |
|
| 977 |
\return false on failure |
| 978 |
\return true on success |
| 979 |
*/ |
| 980 |
bool cwdaemon_get_long(const char *buf, long *lvp) |
| 981 |
{ |
| 982 |
errno = 0; |
| 983 |
|
| 984 |
char *ep; |
| 985 |
long lv = strtol(buf, &ep, 10); |
| 986 |
if (buf[0] == '\0' || *ep != '\0') { |
| 987 |
return false; |
| 988 |
} |
| 989 |
|
| 990 |
if (errno == ERANGE && (lv == LONG_MAX || lv == LONG_MIN)) { |
| 991 |
return false; |
| 992 |
} |
| 993 |
*lvp = lv; |
| 994 |
|
| 995 |
return true; |
| 996 |
} |
| 997 |
|
| 998 |
|
| 999 |
|
| 1000 |
|
| 1001 |
|
| 1002 |
/** |
| 1003 |
\brief Prepare reply for the caller |
| 1004 |
|
| 1005 |
Fill \p reply buffer with data from given \p request, prepare some |
| 1006 |
other variables for sending reply to the client. |
| 1007 |
|
| 1008 |
Text of the reply is usually defined by caller, i.e. it is sent by client |
| 1009 |
to cwdaemon and marked by the client as text to be used in reply. |
| 1010 |
|
| 1011 |
The reply should be sent back to client as soon as cwdaemon |
| 1012 |
finishes processing/playing received request. |
| 1013 |
|
| 1014 |
There are two different procedures for recognizing what should be sent |
| 1015 |
back as reply and when: |
| 1016 |
\li received request ending with '^' character: the text of the request |
| 1017 |
should be played, but it also should be used as a reply. |
| 1018 |
This function does not specify when the reply should be sent back. |
| 1019 |
All it does is it prepares the text of reply. |
| 1020 |
|
| 1021 |
'^' can be used for char-by-char communication: client software |
| 1022 |
message with single character followed by '^'. cwdaemon plays the |
| 1023 |
character, and informs client software about playing the sound. Then |
| 1024 |
client software can send request with next character followed by '^'. |
| 1025 |
\li received request starting with "<ESC>h" escape code: the text of |
| 1026 |
request should be sent back to the client after playing text of *next* |
| 1027 |
request. So there are two requests sent by client to cwdaemon: |
| 1028 |
first defines reply, and the second defines text to be played. |
| 1029 |
First should be echoed back (but not played), second should be played. |
| 1030 |
|
| 1031 |
\param reply - buffer for reply (allocated by caller) |
| 1032 |
\param request - buffer with request |
| 1033 |
\param n - size of data in request |
| 1034 |
*/ |
| 1035 |
void cwdaemon_prepare_reply(char *reply, const char *request, size_t n) |
| 1036 |
{ |
| 1037 |
/* Since we need to prepare a reply, we need to mark our |
| 1038 |
intent to send echo. The echo (reply) will be sent to client |
| 1039 |
when libcw's tone queue becomes empty. |
| 1040 |
|
| 1041 |
It is important to set this flag at the beginning of the function. */ |
| 1042 |
ptt_flag |= PTT_ACTIVE_ECHO; |
| 1043 |
cwdaemon_debug(CWDAEMON_VERBOSITY_D, __func__, __LINE__, "PTT flag +PTT_ACTIVE_ECHO (0x%02x/%s)", ptt_flag, cwdaemon_debug_ptt_flags()); |
| 1044 |
|
| 1045 |
/* We are sending reply to the same host that sent a request. */ |
| 1046 |
memcpy(&reply_addr, &request_addr, sizeof(reply_addr)); |
| 1047 |
reply_addrlen = request_addrlen; |
| 1048 |
|
| 1049 |
strncpy(reply, request, n); |
| 1050 |
reply[n] = '\0'; /* FIXME: where is boundary checking? */ |
| 1051 |
|
| 1052 |
cwdaemon_debug(CWDAEMON_VERBOSITY_I, __func__, __LINE__, "text of request: \"%s\", text of reply: \"%s\"", request, reply); |
| 1053 |
cwdaemon_debug(CWDAEMON_VERBOSITY_I, __func__, __LINE__, "now waiting for end of transmission before echoing back to client"); |
| 1054 |
|
| 1055 |
return; |
| 1056 |
} |
| 1057 |
|
| 1058 |
|
| 1059 |
|
| 1060 |
|
| 1061 |
|
| 1062 |
/** |
| 1063 |
\brief Wrapper around sendto() |
| 1064 |
|
| 1065 |
Wrapper around sendto(), sending a \p reply to client. |
| 1066 |
Client is specified by reply_* network variables. |
| 1067 |
|
| 1068 |
\param reply - reply to be sent |
| 1069 |
|
| 1070 |
\return -1 on failure |
| 1071 |
\return number of characters sent on success |
| 1072 |
*/ |
| 1073 |
ssize_t cwdaemon_sendto(const char *reply) |
| 1074 |
{ |
| 1075 |
size_t len = strlen(reply); |
| 1076 |
/* TODO: Do we *really* need to end replies with CRLF? */ |
| 1077 |
assert(reply[len - 2] == '\r' && reply[len - 1] == '\n'); |
| 1078 |
|
| 1079 |
ssize_t rv = sendto(socket_descriptor, reply, len, 0, |
| 1080 |
(struct sockaddr *) &reply_addr, reply_addrlen); |
| 1081 |
|
| 1082 |
if (rv == -1) { |
| 1083 |
cwdaemon_debug(CWDAEMON_VERBOSITY_E, __func__, __LINE__, "sendto: \"%s\"", strerror(errno)); |
| 1084 |
return -1; |
| 1085 |
} else { |
| 1086 |
return rv; |
| 1087 |
} |
| 1088 |
} |
| 1089 |
|
| 1090 |
|
| 1091 |
|
| 1092 |
|
| 1093 |
|
| 1094 |
/** |
| 1095 |
\brief Receive request sent through socket |
| 1096 |
|
| 1097 |
Received request is returned through \p request. |
| 1098 |
Possible trailing '\r' and '\n' characters are stripped. |
| 1099 |
Request is ended with '\0'. |
| 1100 |
|
| 1101 |
\param request - buffer for received request |
| 1102 |
\param n - size of the buffer |
| 1103 |
|
| 1104 |
\return -2 if peer has performed an orderly shutdown |
| 1105 |
\return -1 if an error occurred during call to recvfrom |
| 1106 |
\return 0 if no request has been received |
| 1107 |
\return length of received request otherwise |
| 1108 |
*/ |
| 1109 |
int cwdaemon_recvfrom(char *request, int n) |
| 1110 |
{ |
| 1111 |
ssize_t recv_rc = recvfrom(socket_descriptor, |
| 1112 |
request, |
| 1113 |
n, |
| 1114 |
0, /* flags */ |
| 1115 |
(struct sockaddr *) &request_addr, |
| 1116 |
/* TODO: request_addrlen may be modified. Check it. */ |
| 1117 |
&request_addrlen); |
| 1118 |
|
| 1119 |
if (recv_rc == -1) { /* No requests available? */ |
| 1120 |
|
| 1121 |
if (errno == EAGAIN || errno == EWOULDBLOCK) { /* "a portable application should check for both possibilities" */ |
| 1122 |
/* Yup, no requests available from non-blocking socket. Good luck next time. */ |
| 1123 |
/* TODO: how much CPU time does it cost to loop waiting for a request? |
| 1124 |
Would it be reasonable to configure the socket as blocking? |
| 1125 |
How large is receive timeout? */ |
| 1126 |
|
| 1127 |
return 0; |
| 1128 |
} else { |
| 1129 |
/* Some other error. May be a serious error. */ |
| 1130 |
cwdaemon_errmsg("Recvfrom"); |
| 1131 |
return -1; |
| 1132 |
} |
| 1133 |
} else if (recv_rc == 0) { |
| 1134 |
/* "peer has performed an orderly shutdown" */ |
| 1135 |
return -2; |
| 1136 |
} else { |
| 1137 |
; /* pass */ |
| 1138 |
} |
| 1139 |
|
| 1140 |
/* Remove CRLF if present. TCP buffer may end with '\n', so make |
| 1141 |
sure that every request is consistently ended with NUL only. |
| 1142 |
Do it early, do it now. */ |
| 1143 |
int z; |
| 1144 |
while (recv_rc > 0 |
| 1145 |
&& ( (z = request[recv_rc - 1]) == '\n' || z == '\r') ) { |
| 1146 |
|
| 1147 |
request[--recv_rc] = '\0'; |
| 1148 |
} |
| 1149 |
|
| 1150 |
return recv_rc; |
| 1151 |
} |
| 1152 |
|
| 1153 |
|
| 1154 |
|
| 1155 |
|
| 1156 |
|
| 1157 |
/** |
| 1158 |
\brief Receive message from socket, act upon it |
| 1159 |
|
| 1160 |
Watch the socket and if there is an escape character check what it is, |
| 1161 |
otherwise play morse. |
| 1162 |
|
| 1163 |
FIXME: duplicate return value (zero and zero). |
| 1164 |
|
| 1165 |
\return 0 when an escape code has been received |
| 1166 |
\return 0 when no request or an empty request has been received |
| 1167 |
\return 1 when a text request has been played |
| 1168 |
*/ |
| 1169 |
int cwdaemon_receive(void) |
| 1170 |
{ |
| 1171 |
/* The request may be a printable string, so +1 for ending NUL |
| 1172 |
added somewhere below is necessary. */ |
| 1173 |
char request_buffer[CWDAEMON_MESSAGE_SIZE_MAX + 1]; |
| 1174 |
|
| 1175 |
ssize_t recv_rc = cwdaemon_recvfrom(request_buffer, CWDAEMON_MESSAGE_SIZE_MAX); |
| 1176 |
|
| 1177 |
if (recv_rc == -2) { |
| 1178 |
/* Sender has closed connection. */ |
| 1179 |
return 0; |
| 1180 |
} else if (recv_rc == -1) { |
| 1181 |
/* TODO: should we really exit? |
| 1182 |
Shouldn't we recover from the error? */ |
| 1183 |
exit(EXIT_FAILURE); |
| 1184 |
} else if (recv_rc == 0) { |
| 1185 |
cwdaemon_debug(CWDAEMON_VERBOSITY_D, __func__, __LINE__, "...recv_from (no data)"); |
| 1186 |
return 0; |
| 1187 |
} else { |
| 1188 |
; /* pass */ |
| 1189 |
} |
| 1190 |
|
| 1191 |
request_buffer[recv_rc] = '\0'; |
| 1192 |
|
| 1193 |
cwdaemon_debug(CWDAEMON_VERBOSITY_I, __func__, __LINE__, "-------------------"); |
| 1194 |
/* TODO: replace the magic number 27 with constant. */ |
| 1195 |
if (request_buffer[0] != 27) { |
| 1196 |
/* No ESCAPE. All received data should be treated |
| 1197 |
as text to be sent using Morse code. |
| 1198 |
|
| 1199 |
Note that this does not exclude possibility of |
| 1200 |
caret request (e.g. "some text^"), which does |
| 1201 |
require sending a reply to client. Such request is |
| 1202 |
correctly handled by cwdaemon_play_request(). */ |
| 1203 |
cwdaemon_debug(CWDAEMON_VERBOSITY_I, __func__, __LINE__, "request: \"%s\"", request_buffer); |
| 1204 |
if ((strlen(request_buffer) + strlen(request_queue)) <= CWDAEMON_REQUEST_QUEUE_SIZE_MAX - 1) { |
| 1205 |
strcat(request_queue, request_buffer); |
| 1206 |
cwdaemon_play_request(request_queue); |
| 1207 |
} else { |
| 1208 |
; /* TODO: how to handle this case? */ |
| 1209 |
} |
| 1210 |
return 1; |
| 1211 |
} else { |
| 1212 |
/* Don't print literal escape value, use <ESC> |
| 1213 |
symbol. First reason is that the literal value |
| 1214 |
doesn't look good in console (some non-printable |
| 1215 |
glyph), second reason is that printing <ESC>c to |
| 1216 |
terminal makes funny things with the lines already |
| 1217 |
printed to the terminal (tested in xfce4-terminal |
| 1218 |
and xterm). */ |
| 1219 |
cwdaemon_debug(CWDAEMON_VERBOSITY_I, __func__, __LINE__, "escaped request: \"<ESC>%s\"", request_buffer + 1); |
| 1220 |
cwdaemon_handle_escaped_request(request_buffer); |
| 1221 |
return 0; |
| 1222 |
} |
| 1223 |
} |
| 1224 |
|
| 1225 |
|
| 1226 |
|
| 1227 |
|
| 1228 |
/** |
| 1229 |
The function may call exit() if a request from client asks the |
| 1230 |
daemon to exit. |
| 1231 |
*/ |
| 1232 |
void cwdaemon_handle_escaped_request(char *request) |
| 1233 |
{ |
| 1234 |
long lv; |
| 1235 |
|
| 1236 |
/* Take action depending on Escape code. */ |
| 1237 |
switch ((int) request[1]) { |
| 1238 |
case '0': |
| 1239 |
/* Reset all values. */ |
| 1240 |
cwdaemon_debug(CWDAEMON_VERBOSITY_I, __func__, __LINE__, |
| 1241 |
"requested resetting of parameters"); |
| 1242 |
request_queue[0] = '\0'; |
| 1243 |
cwdaemon_reset_almost_all(); |
| 1244 |
wordmode = 0; |
| 1245 |
async_abort = 0; |
| 1246 |
global_cwdevice->reset(global_cwdevice); |
| 1247 |
|
| 1248 |
ptt_flag = 0; |
| 1249 |
cwdaemon_debug(CWDAEMON_VERBOSITY_D, __func__, __LINE__, "PTT flag = 0 (0x%02x/%s)", ptt_flag, cwdaemon_debug_ptt_flags()); |
| 1250 |
cwdaemon_debug(CWDAEMON_VERBOSITY_I, __func__, __LINE__, "resetting completed"); |
| 1251 |
|
| 1252 |
break; |
| 1253 |
case '2': |
| 1254 |
/* Set speed of Morse code, in words per minute. */ |
| 1255 |
if (cwdaemon_params_wpm(¤t_morse_speed, request + 2)) { |
| 1256 |
cw_set_send_speed(current_morse_speed); |
| 1257 |
} |
| 1258 |
break; |
| 1259 |
case '3': |
| 1260 |
/* Set tone (frequency) of morse code, in Hz. |
| 1261 |
The code assumes that minimal valid frequency is zero. */ |
| 1262 |
assert (CW_FREQUENCY_MIN == 0); |
| 1263 |
if (cwdaemon_params_tone(¤t_morse_tone, request + 2)) { |
| 1264 |
if (current_morse_tone > 0) { |
| 1265 |
|
| 1266 |
cw_set_frequency(current_morse_tone); |
| 1267 |
cwdaemon_debug(CWDAEMON_VERBOSITY_I, __func__, __LINE__, "tone: %d Hz", current_morse_tone); |
| 1268 |
|
| 1269 |
/* TODO: Should we really be adjusting |
| 1270 |
volume when the command is for |
| 1271 |
frequency? It would be more |
| 1272 |
"elegant" not to do so. */ |
| 1273 |
cw_set_volume(current_morse_volume); |
| 1274 |
|
| 1275 |
} else { /* current_morse_tone==0, sidetone off */ |
| 1276 |
cw_set_volume(0); |
| 1277 |
cwdaemon_debug(CWDAEMON_VERBOSITY_I, __func__, __LINE__, "volume off"); |
| 1278 |
} |
| 1279 |
} |
| 1280 |
break; |
| 1281 |
case '4': |
| 1282 |
/* Abort currently sent message. */ |
| 1283 |
if (wordmode) { |
| 1284 |
cwdaemon_debug(CWDAEMON_VERBOSITY_I, __func__, __LINE__, "requested aborting of message - ignoring (word mode is active)"); |
| 1285 |
} else { |
| 1286 |
cwdaemon_debug(CWDAEMON_VERBOSITY_I, __func__, __LINE__, "requested aborting of message - executing (character mode is active)"); |
| 1287 |
if (ptt_flag & PTT_ACTIVE_ECHO) { |
| 1288 |
cwdaemon_debug(CWDAEMON_VERBOSITY_I, __func__, __LINE__, "echo \"break\""); |
| 1289 |
cwdaemon_sendto("break\r\n"); |
| 1290 |
} |
| 1291 |
request_queue[0] = '\0'; |
| 1292 |
cw_flush_tone_queue(); |
| 1293 |
cw_wait_for_tone_queue(); |
| 1294 |
if (ptt_flag) { |
| 1295 |
cwdaemon_set_ptt_off(global_cwdevice, "PTT off"); |
| 1296 |
} |
| 1297 |
ptt_flag &= 0; |
| 1298 |
cwdaemon_debug(CWDAEMON_VERBOSITY_D, __func__, __LINE__, "PTT flag = 0 (0x%02x/%s)", ptt_flag, cwdaemon_debug_ptt_flags()); |
| 1299 |
} |
| 1300 |
break; |
| 1301 |
case '5': |
| 1302 |
/* Exit cwdaemon. */ |
| 1303 |
errno = 0; |
| 1304 |
#if 0 |
| 1305 |
char address[INET_ADDRSTRLEN]; |
| 1306 |
inet_ntop(request_addr.sin_family, (struct in_addr*) &(request_addr.sin_addr.s_addr), |
| 1307 |
address, INET_ADDRSTRLEN); |
| 1308 |
cwdaemon_debug(CWDAEMON_VERBOSITY_I, __func__, __LINE__, |
| 1309 |
"requested exit of daemon (client address: %s)", address); |
| 1310 |
#else |
| 1311 |
cwdaemon_debug(CWDAEMON_VERBOSITY_I, __func__, __LINE__, |
| 1312 |
"requested exit of daemon"); |
| 1313 |
#endif |
| 1314 |
exit(EXIT_SUCCESS); |
| 1315 |
|
| 1316 |
case '6': |
| 1317 |
/* Set uninterruptable (word mode). */ |
| 1318 |
request[0] = '\0'; |
| 1319 |
request_queue[0] = '\0'; |
| 1320 |
wordmode = 1; |
| 1321 |
cwdaemon_debug(CWDAEMON_VERBOSITY_I, __func__, __LINE__, "wordmode set"); |
| 1322 |
break; |
| 1323 |
case '7': |
| 1324 |
/* Set weighting of morse code dits and dashes. |
| 1325 |
Remember that cwdaemon uses values in range |
| 1326 |
-50/+50, but libcw accepts values in range |
| 1327 |
20/80. This is why you have the calculation |
| 1328 |
when calling cw_set_weighting(). */ |
| 1329 |
if (cwdaemon_params_weighting(¤t_weighting, request + 2)) { |
| 1330 |
cw_set_weighting(current_weighting * 0.6 + CWDAEMON_MORSE_WEIGHTING_MAX); |
| 1331 |
} |
| 1332 |
break; |
| 1333 |
case '8': { |
| 1334 |
/* Set type of keying device. */ |
| 1335 |
cwdaemon_params_cwdevice(request + 2); |
| 1336 |
|
| 1337 |
break; |
| 1338 |
} |
| 1339 |
case '9': |
| 1340 |
/* Change network port number. |
| 1341 |
TODO: why this is obsolete? */ |
| 1342 |
cwdaemon_debug(CWDAEMON_VERBOSITY_W, __func__, __LINE__, |
| 1343 |
"obsolete request \"9\" (change network port), ignoring"); |
| 1344 |
break; |
| 1345 |
case 'a': |
| 1346 |
/* PTT keying on or off */ |
| 1347 |
cwdaemon_params_ptt_on_off(request + 2); |
| 1348 |
|
| 1349 |
break; |
| 1350 |
case 'b': |
| 1351 |
/* SSB way. */ |
| 1352 |
#if defined(HAVE_LINUX_PPDEV_H) || defined(HAVE_DEV_PPBUS_PPI_H) |
| 1353 |
if (!cwdaemon_get_long(request + 2, &lv)) { |
| 1354 |
break; |
| 1355 |
} |
| 1356 |
|
| 1357 |
if (lv) { |
| 1358 |
if (global_cwdevice->ssbway) { |
| 1359 |
global_cwdevice->ssbway(global_cwdevice, SOUNDCARD); |
| 1360 |
cwdaemon_debug(CWDAEMON_VERBOSITY_I, __func__, __LINE__, "\"SSB way\" set to SOUNDCARD", PACKAGE); |
| 1361 |
} else { |
| 1362 |
cwdaemon_debug(CWDAEMON_VERBOSITY_W, __func__, __LINE__, "\"SSB way\" to SOUNDCARD unimplemented"); |
| 1363 |
} |
| 1364 |
} else { |
| 1365 |
if (global_cwdevice->ssbway) { |
| 1366 |
global_cwdevice->ssbway(global_cwdevice, MICROPHONE); |
| 1367 |
cwdaemon_debug(CWDAEMON_VERBOSITY_I, __func__, __LINE__, "\"SSB way\" set to MICROPHONE"); |
| 1368 |
} else { |
| 1369 |
cwdaemon_debug(CWDAEMON_VERBOSITY_W, __func__, __LINE__, "\"SSB way\" to MICROPHONE unimplemented"); |
| 1370 |
} |
| 1371 |
} |
| 1372 |
#else |
| 1373 |
cwdaemon_debug(CWDAEMON_VERBOSITY_W, __func__, __LINE__, "\"SSB way\" through parallel port unavailable (parallel port not configured)."); |
| 1374 |
#endif |
| 1375 |
break; |
| 1376 |
case 'c': |
| 1377 |
{ |
| 1378 |
/* FIXME: change this uint32_t to size_t. */ |
| 1379 |
uint32_t seconds = 0; |
| 1380 |
/* Tune for a number of seconds. */ |
| 1381 |
if (cwdaemon_params_tune(&seconds, request + 2)) { |
| 1382 |
cwdaemon_tune(seconds); |
| 1383 |
} |
| 1384 |
break; |
| 1385 |
} |
| 1386 |
case 'd': |
| 1387 |
{ |
| 1388 |
/* Set PTT delay (TOD, Turn On Delay). |
| 1389 |
The value is milliseconds. */ |
| 1390 |
|
| 1391 |
int rv = cwdaemon_params_pttdelay(¤t_ptt_delay, request + 2); |
| 1392 |
|
| 1393 |
if (rv == 0) { |
| 1394 |
/* Value totally invalid. */ |
| 1395 |
cwdaemon_debug(CWDAEMON_VERBOSITY_E, __func__, __LINE__, |
| 1396 |
"invalid requested PTT delay [ms]: \"%s\" (should be integer between %d and %d inclusive)", |
| 1397 |
request + 2, |
| 1398 |
CWDAEMON_PTT_DELAY_MIN, CWDAEMON_PTT_DELAY_MAX); |
| 1399 |
} else if (rv == 1) { |
| 1400 |
/* Value totally valid. Information |
| 1401 |
debug string has been already |
| 1402 |
printed in |
| 1403 |
cwdaemon_params_pttdelay(). */ |
| 1404 |
} else { /* rv == 2 */ |
| 1405 |
/* Value invalid (out-of-range), but |
| 1406 |
acceptable when sent over network |
| 1407 |
request and then clipped to be |
| 1408 |
in-range. Value has been clipped in |
| 1409 |
cwdaemon_params_pttdelay(), but a |
| 1410 |
warning debug string must be |
| 1411 |
printed here. */ |
| 1412 |
cwdaemon_debug(CWDAEMON_VERBOSITY_W, __func__, __LINE__, |
| 1413 |
"requested PTT delay [ms] out of range: \"%s\", clipping to \"%d\" (should be between %d and %d inclusive)", |
| 1414 |
request + 2, |
| 1415 |
CWDAEMON_PTT_DELAY_MAX, |
| 1416 |
CWDAEMON_PTT_DELAY_MIN, CWDAEMON_PTT_DELAY_MAX); |
| 1417 |
} |
| 1418 |
|
| 1419 |
if (rv && current_ptt_delay == 0) { |
| 1420 |
cwdaemon_set_ptt_off(global_cwdevice, "ensure PTT off"); |
| 1421 |
} |
| 1422 |
} |
| 1423 |
|
| 1424 |
break; |
| 1425 |
case 'e': |
| 1426 |
/* Set band switch output on parport bits 9 (MSB), 8, 7, 2 (LSB). */ |
| 1427 |
#if defined(HAVE_LINUX_PPDEV_H) || defined(HAVE_DEV_PPBUS_PPI_H) |
| 1428 |
if (!cwdaemon_get_long(request + 2, &lv)) { |
| 1429 |
break; |
| 1430 |
} |
| 1431 |
|
| 1432 |
/* We use four bits to select band, this gives 16 |
| 1433 |
bands: 0 - 15. */ |
| 1434 |
if (lv >= 0 && lv <= 15) { |
| 1435 |
cwdaemon_switch_band(global_cwdevice, lv); |
| 1436 |
} |
| 1437 |
#else |
| 1438 |
cwdaemon_debug(CWDAEMON_VERBOSITY_W, __func__, __LINE__, "band switching through parallel port is unavailable (parallel port not configured)"); |
| 1439 |
#endif |
| 1440 |
break; |
| 1441 |
case 'f': { |
| 1442 |
/* Change sound system used by libcw. */ |
| 1443 |
/* FIXME: if "request+2" describes unavailable sound system, |
| 1444 |
cwdaemon fails to open the new sound system. Since |
| 1445 |
the old one is closed with cwdaemon_close_libcw_output(), |
| 1446 |
cwdaemon has no working sound system, and is unable to |
| 1447 |
play sound. |
| 1448 |
|
| 1449 |
This can be fixed either by querying libcw if "request+2" |
| 1450 |
sound system is available, or by first trying to |
| 1451 |
open new sound system and then - on success - |
| 1452 |
closing the old one. In either case cwdaemon would |
| 1453 |
require some method to inform client about success |
| 1454 |
or failure to open new sound system. */ |
| 1455 |
if (cwdaemon_params_system(¤t_audio_system, request + 2)) { |
| 1456 |
/* Handle valid request for changing sound system. */ |
| 1457 |
cwdaemon_close_libcw_output(); |
| 1458 |
|
| 1459 |
if (cwdaemon_open_libcw_output(current_audio_system)) { |
| 1460 |
has_audio_output = true; |
| 1461 |
} else { |
| 1462 |
/* Fall back to NULL audio system. */ |
| 1463 |
cwdaemon_close_libcw_output(); |
| 1464 |
if (cwdaemon_open_libcw_output(CW_AUDIO_NULL)) { |
| 1465 |
|
| 1466 |
cwdaemon_debug(CWDAEMON_VERBOSITY_W, __func__, __LINE__, |
| 1467 |
"fall back to \"Null\" sound system"); |
| 1468 |
current_audio_system = CW_AUDIO_NULL; |
| 1469 |
has_audio_output = true; |
| 1470 |
} else { |
| 1471 |
cwdaemon_debug(CWDAEMON_VERBOSITY_E, __func__, __LINE__, |
| 1472 |
"failed to fall back to \"Null\" sound system"); |
| 1473 |
has_audio_output = false; |
| 1474 |
} |
| 1475 |
} |
| 1476 |
|
| 1477 |
if (has_audio_output) { |
| 1478 |
|
| 1479 |
/* Tone queue is bound to a |
| 1480 |
generator. Creating new generator |
| 1481 |
requires re-registering the |
| 1482 |
callback. */ |
| 1483 |
cw_register_tone_queue_low_callback(cwdaemon_tone_queue_low_callback, NULL, tq_low_watermark); |
| 1484 |
|
| 1485 |
/* This call recalibrates length of |
| 1486 |
dot and dash. */ |
| 1487 |
cw_set_frequency(current_morse_tone); |
| 1488 |
|
| 1489 |
cw_set_send_speed(current_morse_speed); |
| 1490 |
cw_set_volume(current_morse_volume); |
| 1491 |
|
| 1492 |
/* Regardless if we are using |
| 1493 |
"default" or "current" parameters, |
| 1494 |
the gap is always zero. */ |
| 1495 |
cw_set_gap(0); |
| 1496 |
|
| 1497 |
cw_set_weighting(current_weighting * 0.6 + CWDAEMON_MORSE_WEIGHTING_MAX); |
| 1498 |
} |
| 1499 |
} |
| 1500 |
break; |
| 1501 |
} |
| 1502 |
case 'g': |
| 1503 |
/* Set volume of sound, in percents. */ |
| 1504 |
if (cwdaemon_params_volume(¤t_morse_volume, request + 2)) { |
| 1505 |
cw_set_volume(current_morse_volume); |
| 1506 |
} |
| 1507 |
break; |
| 1508 |
case 'h': |
| 1509 |
/* Data after '<ESC>h' is a text to be used as reply. |
| 1510 |
It shouldn't be echoed back to client immediately. |
| 1511 |
|
| 1512 |
Instead, cwdaemon should wait for another request |
| 1513 |
(I assume that it will be a regular text to be |
| 1514 |
played), play it, and then send prepared reply back |
| 1515 |
to the client. So this is a reply with delay. */ |
| 1516 |
|
| 1517 |
/* 'request + 1' skips the leading <ESC>, but |
| 1518 |
preserves 'h'. The 'h' is a part of reply text. If |
| 1519 |
the client didn't specify reply text, the 'h' will |
| 1520 |
be the only content of server's reply. */ |
| 1521 |
|
| 1522 |
cwdaemon_prepare_reply(reply_buffer, request + 1, strlen(request + 1)); |
| 1523 |
cwdaemon_debug(CWDAEMON_VERBOSITY_I, __func__, __LINE__, "reply is ready, waiting for message from client (reply: \"%s\")", reply_buffer); |
| 1524 |
/* cwdaemon will wait for queue-empty callback before |
| 1525 |
sending the reply. */ |
| 1526 |
break; |
| 1527 |
} /* switch ((int) request[1]) */ |
| 1528 |
|
| 1529 |
return; |
| 1530 |
} |
| 1531 |
|
| 1532 |
|
| 1533 |
|
| 1534 |
|
| 1535 |
|
| 1536 |
/** |
| 1537 |
\brief Process received request, play relevant characters |
| 1538 |
|
| 1539 |
Check every character in given request, act upon markers |
| 1540 |
for speed increase or decrease, and play other characters. |
| 1541 |
|
| 1542 |
Function modifies contents of buffer \p request. |
| 1543 |
|
| 1544 |
\param request - request to be processed |
| 1545 |
*/ |
| 1546 |
void cwdaemon_play_request(char *request) |
| 1547 |
{ |
| 1548 |
//cw_block_callback(true); |
| 1549 |
|
| 1550 |
char *x = request; |
| 1551 |
|
| 1552 |
while (*x) { |
| 1553 |
switch ((int) *x) { |
| 1554 |
case '+': |
| 1555 |
case '-': |
| 1556 |
/* Speed increase & decrease */ |
| 1557 |
/* Repeated '+' and '-' characters are allowed, |
| 1558 |
in such cases increase and decrease of speed is |
| 1559 |
multiple of 2 wpm. */ |
| 1560 |
do { |
| 1561 |
current_morse_speed += (*x == '+') ? 2 : -2; |
| 1562 |
x++; |
| 1563 |
} while (*x == '+' || *x == '-'); |
| 1564 |
|
| 1565 |
if (current_morse_speed < CW_SPEED_MIN) { |
| 1566 |
current_morse_speed = CW_SPEED_MIN; |
| 1567 |
} else if (current_morse_speed > CW_SPEED_MAX) { |
| 1568 |
current_morse_speed = CW_SPEED_MAX; |
| 1569 |
} else { |
| 1570 |
; |
| 1571 |
} |
| 1572 |
cw_set_send_speed(current_morse_speed); |
| 1573 |
break; |
| 1574 |
case '~': |
| 1575 |
/* 2 dots time additional for the next char. The gap |
| 1576 |
is always reset after playing the char. */ |
| 1577 |
cw_set_gap(2); |
| 1578 |
x++; |
| 1579 |
break; |
| 1580 |
case '^': |
| 1581 |
/* Send echo to main program when CW playing is done. */ |
| 1582 |
*x = '\0'; /* Remove '^' and possible trailing garbage. */ |
| 1583 |
/* '^' can be found at the end of request, and |
| 1584 |
it means "echo text of current request back |
| 1585 |
to client once you finish playing it". */ |
| 1586 |
cwdaemon_prepare_reply(reply_buffer, request, strlen(request)); |
| 1587 |
|
| 1588 |
/* cwdaemon will wait for queue-empty callback |
| 1589 |
before sending the reply. */ |
| 1590 |
break; |
| 1591 |
case '*': |
| 1592 |
/* TODO: what's this? */ |
| 1593 |
*x = '+'; |
| 1594 |
default: |
| 1595 |
cwdaemon_set_ptt_on(global_cwdevice, "PTT (auto) on"); |
| 1596 |
/* PTT is now in AUTO. It will be turned off on low |
| 1597 |
tone queue, in cwdaemon_tone_queue_low_callback(). */ |
| 1598 |
|
| 1599 |
cwdaemon_debug(CWDAEMON_VERBOSITY_I, __func__, __LINE__, "Morse character \"%c\" to be queued in libcw", *x); |
| 1600 |
cw_send_character(*x); |
| 1601 |
cwdaemon_debug(CWDAEMON_VERBOSITY_D, __func__, __LINE__, "Morse character \"%c\" has been queued in libcw", *x); |
| 1602 |
|
| 1603 |
x++; |
| 1604 |
if (cw_get_gap() == 2) { |
| 1605 |
if (*x == '^') { |
| 1606 |
/* '^' is supposed to be the |
| 1607 |
last character in the |
| 1608 |
message, meaning that all |
| 1609 |
that was before it should |
| 1610 |
be used as reply text. So |
| 1611 |
x++ will jump to ending |
| 1612 |
NUL. */ |
| 1613 |
x++; |
| 1614 |
} else { |
| 1615 |
cw_set_gap(0); |
| 1616 |
} |
| 1617 |
} |
| 1618 |
break; |
| 1619 |
} |
| 1620 |
} |
| 1621 |
|
| 1622 |
/* All characters processed, mark buffer as empty. */ |
| 1623 |
*request = '\0'; |
| 1624 |
|
| 1625 |
//cw_block_callback(false); |
| 1626 |
|
| 1627 |
return; |
| 1628 |
} |
| 1629 |
|
| 1630 |
|
| 1631 |
|
| 1632 |
|
| 1633 |
|
| 1634 |
/** |
| 1635 |
\brief Callback function for key state change |
| 1636 |
|
| 1637 |
Function passed to libcw, will be called every time a state of libcw's |
| 1638 |
internal ("software") key changes, i.e. every time it starts or ends |
| 1639 |
producing dit or dash. |
| 1640 |
When the software key is closed (dit or dash), \p keystate is 1. |
| 1641 |
Otherwise \p keystate is 0. Following the changes of \p keystate the |
| 1642 |
function changes state of bit on output of its keying device. |
| 1643 |
|
| 1644 |
So it goes like this: |
| 1645 |
input text in cwdaemon is split into characters -> |
| 1646 |
characters in cwdaemon are sent to libcw -> |
| 1647 |
characters in libcw are converted to dits/dashes -> |
| 1648 |
dits/dashes are played by libcw, and at the same time libcw changes |
| 1649 |
state of its software key -> |
| 1650 |
libcw calls cwdaemon_keyingevent() on changes of software key -> |
| 1651 |
cwdaemon_keyingevent() changes state of a bit on cwdaemon's keying device |
| 1652 |
|
| 1653 |
\param arg - unused argument |
| 1654 |
\param keystate - state of a key, as seen by libcw |
| 1655 |
*/ |
| 1656 |
void cwdaemon_keyingevent(__attribute__((unused)) void *arg, int keystate) |
| 1657 |
{ |
| 1658 |
if (keystate == 1) { |
| 1659 |
global_cwdevice->cw(global_cwdevice, ON); |
| 1660 |
} else { |
| 1661 |
global_cwdevice->cw(global_cwdevice, OFF); |
| 1662 |
} |
| 1663 |
|
| 1664 |
inactivity_seconds = 0; |
| 1665 |
|
| 1666 |
cwdaemon_debug(CWDAEMON_VERBOSITY_I, __func__, __LINE__, "keying event \"%d\"", keystate); |
| 1667 |
|
| 1668 |
return; |
| 1669 |
} |
| 1670 |
|
| 1671 |
|
| 1672 |
|
| 1673 |
|
| 1674 |
|
| 1675 |
/** |
| 1676 |
\brief Callback routine called when tone queue is empty |
| 1677 |
|
| 1678 |
Callback routine registered with cw_register_tone_queue_low_callback(), |
| 1679 |
will be called by libcw every time number of tones drops in queue below |
| 1680 |
specific level. |
| 1681 |
|
| 1682 |
\param arg - unused argument |
| 1683 |
*/ |
| 1684 |
void cwdaemon_tone_queue_low_callback(__attribute__((unused)) void *arg) |
| 1685 |
{ |
| 1686 |
int len = cw_get_tone_queue_length(); |
| 1687 |
cwdaemon_debug(CWDAEMON_VERBOSITY_I, __func__, __LINE__, "low TQ callback: start, TQ len = %d, PTT flag = 0x%02x/%s", |
| 1688 |
len, ptt_flag, cwdaemon_debug_ptt_flags()); |
| 1689 |
|
| 1690 |
if (len > tq_low_watermark) { |
| 1691 |
cwdaemon_debug(CWDAEMON_VERBOSITY_I, __func__, __LINE__, "low TQ callback: TQ len larger than watermark, TQ len = %d", len); |
| 1692 |
} |
| 1693 |
|
| 1694 |
if (ptt_flag == PTT_ACTIVE_AUTO |
| 1695 |
/* PTT is (most probably?) on, in purely automatic mode. |
| 1696 |
This means that as soon as there are no new chars to |
| 1697 |
play, we should turn PTT off. */ |
| 1698 |
|
| 1699 |
&& request_queue[0] == '\0' |
| 1700 |
/* No new text has been queued in the meantime. */ |
| 1701 |
|
| 1702 |
&& cw_get_tone_queue_length() <= tq_low_watermark) { |
| 1703 |
/* TODO: check if this third condition is really necessary. */ |
| 1704 |
/* Originally it was 'cw_get_tone_queue_length() <= 1', |
| 1705 |
I'm guessing that '1' here was the same '1' as the |
| 1706 |
third argument to cw_register_tone_queue_low_callback(). |
| 1707 |
Feel free to correct me ;) */ |
| 1708 |
|
| 1709 |
|
| 1710 |
cwdaemon_debug(CWDAEMON_VERBOSITY_I, __func__, __LINE__, "low TQ callback: branch 1, PTT flag = 0x%02x/%s", ptt_flag, cwdaemon_debug_ptt_flags()); |
| 1711 |
|
| 1712 |
cwdaemon_set_ptt_off(global_cwdevice, "PTT (auto) off"); |
| 1713 |
|
| 1714 |
} else if (ptt_flag & PTT_ACTIVE_ECHO) { |
| 1715 |
/* PTT_ACTIVE_ECHO: client has used special request to |
| 1716 |
indicate that it is waiting for reply (echo) from |
| 1717 |
the server (i.e. cwdaemon) after the server plays |
| 1718 |
all characters. */ |
| 1719 |
|
| 1720 |
cwdaemon_debug(CWDAEMON_VERBOSITY_I, __func__, __LINE__, "low TQ callback: branch 2, PTT flag = 0x%02x/%s", ptt_flag, cwdaemon_debug_ptt_flags()); |
| 1721 |
|
| 1722 |
/* Since echo is being sent, we can turn the flag off. |
| 1723 |
For some reason cwdaemon works better when we turn the |
| 1724 |
flag off before sending the reply, rather than turning |
| 1725 |
if after sending the reply. */ |
| 1726 |
ptt_flag &= ~PTT_ACTIVE_ECHO; |
| 1727 |
cwdaemon_debug(CWDAEMON_VERBOSITY_I, __func__, __LINE__, "low TQ callback: PTT flag -PTT_ACTIVE_ECHO, PTT flag = 0x%02x/%s", ptt_flag, cwdaemon_debug_ptt_flags()); |
| 1728 |
|
| 1729 |
|
| 1730 |
cwdaemon_debug(CWDAEMON_VERBOSITY_I, __func__, __LINE__, "low TQ callback: echoing \"%s\" back to client <----------", reply_buffer); |
| 1731 |
strcat(reply_buffer, "\r\n"); /* Ensure exactly one CRLF */ |
| 1732 |
cwdaemon_sendto(reply_buffer); |
| 1733 |
/* If this line is uncommented, the callback erases a valid |
| 1734 |
reply that should be sent back to client. Commenting the |
| 1735 |
line fixes the problem, and doesn't seem to introduce |
| 1736 |
any new ones. |
| 1737 |
TODO: investigate the original problem of erasing a valid |
| 1738 |
reply. */ |
| 1739 |
/* reply_buffer[0] = '\0'; */ |
| 1740 |
|
| 1741 |
|
| 1742 |
/* wait a bit more since we expect to get more text to send |
| 1743 |
|
| 1744 |
TODO: the comment above is a bit unclear. Perhaps |
| 1745 |
it means that we have dealt with escape request |
| 1746 |
requesting an echo, and now there may be a second |
| 1747 |
request (following the escape request) that still |
| 1748 |
needs to be played ("more text to send"). |
| 1749 |
|
| 1750 |
If so, we need to call the callback again, so that |
| 1751 |
it can check if text buffer is non-empty and if |
| 1752 |
tone queue is non-empty. We call the callback again |
| 1753 |
indirectly, by simulating almost empty tone queue |
| 1754 |
(i.e. by adding only two tones to tone queue). |
| 1755 |
|
| 1756 |
I wonder why we don't call the callback directly, |
| 1757 |
maybe it has something to do with avoiding |
| 1758 |
recursion? */ |
| 1759 |
if (ptt_flag == PTT_ACTIVE_AUTO) { |
| 1760 |
cwdaemon_debug(CWDAEMON_VERBOSITY_I, __func__, __LINE__, "low TQ callback: queueing two empty tones"); |
| 1761 |
cw_queue_tone(1, 0); /* ensure Q-empty condition again */ |
| 1762 |
cw_queue_tone(1, 0); /* when trailing gap also 'sent' */ |
| 1763 |
} |
| 1764 |
} else { |
| 1765 |
/* TODO: how to correctly handle this case? |
| 1766 |
Should we do something? */ |
| 1767 |
cwdaemon_debug(CWDAEMON_VERBOSITY_I, __func__, __LINE__, "low TQ callback: branch 3, PTT flag = 0x%02x/%s", ptt_flag, cwdaemon_debug_ptt_flags()); |
| 1768 |
} |
| 1769 |
|
| 1770 |
cwdaemon_debug(CWDAEMON_VERBOSITY_I, __func__, __LINE__, "low TQ callback: end, TQ len = %d, PTT flag = 0x%02x/%s", |
| 1771 |
cw_get_tone_queue_length(), ptt_flag, cwdaemon_debug_ptt_flags()); |
| 1772 |
|
| 1773 |
return; |
| 1774 |
|
| 1775 |
} |
| 1776 |
|
| 1777 |
|
| 1778 |
|
| 1779 |
static const char *cwdaemon_args_short = "d:hniy:I:f:p:P:s:t:T:v:Vw:x:"; |
| 1780 |
|
| 1781 |
static struct option cwdaemon_args_long[] = { |
| 1782 |
{ "cwdevice", required_argument, 0, 0}, /* Keying device. */ |
| 1783 |
{ "nofork", no_argument, 0, 0}, /* Don't fork. */ |
| 1784 |
{ "port", required_argument, 0, 0}, /* Network port number. */ |
| 1785 |
#if defined(HAVE_SETPRIORITY) && defined(PRIO_PROCESS) |
| 1786 |
{ "priority", required_argument, 0, 0}, /* Process priority. */ |
| 1787 |
#endif |
| 1788 |
{ "wpm", required_argument, 0, 0}, /* Sending speed. */ |
| 1789 |
{ "pttdelay", required_argument, 0, 0}, /* PTT delay. */ |
| 1790 |
{ "volume", required_argument, 0, 0}, /* Sound volume. */ |
| 1791 |
{ "version", no_argument, 0, 0}, /* Program's version. */ |
| 1792 |
{ "weighting", required_argument, 0, 0}, /* CW weight. */ |
| 1793 |
{ "tone", required_argument, 0, 0}, /* CW tone. */ |
| 1794 |
{ "verbosity", required_argument, 0, 0}, /* Verbosity of cwdaemon's debug strings. */ |
| 1795 |
{ "libcwflags", required_argument, 0, 0}, /* libcw's debug flags. */ |
| 1796 |
{ "debugfile", required_argument, 0, 0}, /* Path to output debug file. */ |
| 1797 |
{ "system", required_argument, 0, 0}, /* Audio system. */ |
| 1798 |
{ "help", no_argument, 0, 0}, /* Print help text and exit. */ |
| 1799 |
|
| 1800 |
{ 0, 0, 0, 0} }; |
| 1801 |
|
| 1802 |
|
| 1803 |
|
| 1804 |
|
| 1805 |
void cwdaemon_args_process_long(int argc, char *argv[]) |
| 1806 |
{ |
| 1807 |
int option_index = 0; |
| 1808 |
|
| 1809 |
int c = 0; |
| 1810 |
|
| 1811 |
while ((c = getopt_long(argc, argv, cwdaemon_args_short, cwdaemon_args_long, &option_index)) != -1) { |
| 1812 |
if (c == 0) { |
| 1813 |
|
| 1814 |
cwdaemon_debug(CWDAEMON_VERBOSITY_D, __func__, __LINE__, |
| 1815 |
"long option \"%s\"%s%s\n", |
| 1816 |
cwdaemon_args_long[option_index].name, |
| 1817 |
optarg ? "=" : "", |
| 1818 |
optarg ? optarg : ""); |
| 1819 |
|
| 1820 |
const char *optname = cwdaemon_args_long[option_index].name; |
| 1821 |
|
| 1822 |
if (!strcmp(optname, "cwdevice")) { |
| 1823 |
if (!cwdaemon_params_cwdevice(optarg)) { |
| 1824 |
exit(EXIT_FAILURE); |
| 1825 |
} |
| 1826 |
|
| 1827 |
} else if (!strcmp(optname, "nofork")) { |
| 1828 |
cwdaemon_params_nofork(); |
| 1829 |
|
| 1830 |
} else if (!strcmp(optname, "port")) { |
| 1831 |
if (!cwdaemon_params_port(optarg)) { |
| 1832 |
exit(EXIT_FAILURE); |
| 1833 |
} |
| 1834 |
|
| 1835 |
} else if (!strcmp(optname, "priority")) { |
| 1836 |
if (!cwdaemon_params_priority(&process_priority, optarg)) { |
| 1837 |
exit(EXIT_FAILURE); |
| 1838 |
} |
| 1839 |
|
| 1840 |
} else if (!strcmp(optname, "wpm")) { |
| 1841 |
if (!cwdaemon_params_wpm(&default_morse_speed, optarg)) { |
| 1842 |
exit(EXIT_FAILURE); |
| 1843 |
} |
| 1844 |
|
| 1845 |
} else if (!strcmp(optname, "pttdelay")) { |
| 1846 |
if (cwdaemon_params_pttdelay(&default_ptt_delay, optarg) != 1) { |
| 1847 |
/* When processing command |
| 1848 |
line arguments we are very |
| 1849 |
strict, and accept only |
| 1850 |
fully valid optarg. */ |
| 1851 |
cwdaemon_debug(CWDAEMON_VERBOSITY_E, __func__, __LINE__, |
| 1852 |
"invalid requested PTT delay [ms]: \"%s\" (should be integer between %d and %d inclusive)", |
| 1853 |
optarg, |
| 1854 |
CWDAEMON_PTT_DELAY_MIN, CWDAEMON_PTT_DELAY_MAX); |
| 1855 |
exit(EXIT_FAILURE); |
| 1856 |
} |
| 1857 |
|
| 1858 |
} else if (!strcmp(optname, "volume")) { |
| 1859 |
if (!cwdaemon_params_volume(&default_morse_volume, optarg)) { |
| 1860 |
exit(EXIT_FAILURE); |
| 1861 |
} |
| 1862 |
|
| 1863 |
} else if (!strcmp(optname, "version")) { |
| 1864 |
cwdaemon_params_version(); |
| 1865 |
exit(EXIT_SUCCESS); |
| 1866 |
|
| 1867 |
} else if (!strcmp(optname, "weighting")) { |
| 1868 |
if (!cwdaemon_params_weighting(&default_weighting, optarg)) { |
| 1869 |
exit(EXIT_FAILURE); |
| 1870 |
} |
| 1871 |
|
| 1872 |
} else if (!strcmp(optname, "tone")) { |
| 1873 |
if (!cwdaemon_params_tone(&default_morse_tone, optarg)) { |
| 1874 |
exit(EXIT_FAILURE); |
| 1875 |
} |
| 1876 |
|
| 1877 |
} else if (!strcmp(optname, "verbosity")) { |
| 1878 |
if (!cwdaemon_params_set_verbosity(&default_verbosity, optarg)) { |
| 1879 |
exit(EXIT_FAILURE); |
| 1880 |
} |
| 1881 |
|
| 1882 |
} else if (!strcmp(optname, "libcwflags")) { |
| 1883 |
if (!cwdaemon_params_libcwflags(optarg)) { |
| 1884 |
exit(EXIT_FAILURE); |
| 1885 |
} |
| 1886 |
|
| 1887 |
} else if (!strcmp(optname, "debugfile")) { |
| 1888 |
if (!cwdaemon_params_debugfile(optarg)) { |
| 1889 |
exit(EXIT_FAILURE); |
| 1890 |
} |
| 1891 |
|
| 1892 |
} else if (!strcmp(optname, "system")) { |
| 1893 |
if (!cwdaemon_params_system(&default_audio_system, optarg)) { |
| 1894 |
exit(EXIT_FAILURE); |
| 1895 |
} |
| 1896 |
|
| 1897 |
} else { |
| 1898 |
cwdaemon_args_help(); |
| 1899 |
exit(EXIT_SUCCESS); |
| 1900 |
} |
| 1901 |
} else { |
| 1902 |
cwdaemon_args_process_short(c, optarg); |
| 1903 |
} |
| 1904 |
} |
| 1905 |
|
| 1906 |
return; |
| 1907 |
} |
| 1908 |
|
| 1909 |
|
| 1910 |
void cwdaemon_args_process_short(int c, const char *optarg) |
| 1911 |
{ |
| 1912 |
switch (c) { |
| 1913 |
case ':': |
| 1914 |
case '?': |
| 1915 |
case 'h': |
| 1916 |
cwdaemon_args_help(); |
| 1917 |
exit(EXIT_SUCCESS); |
| 1918 |
case 'd': |
| 1919 |
if (!cwdaemon_params_cwdevice(optarg)) { |
| 1920 |
exit(EXIT_FAILURE); |
| 1921 |
} |
| 1922 |
break; |
| 1923 |
case 'n': |
| 1924 |
cwdaemon_params_nofork(); |
| 1925 |
break; |
| 1926 |
case 'p': |
| 1927 |
if (!cwdaemon_params_port(optarg)) { |
| 1928 |
exit(EXIT_FAILURE); |
| 1929 |
} |
| 1930 |
break; |
| 1931 |
#if defined(HAVE_SETPRIORITY) && defined(PRIO_PROCESS) |
| 1932 |
case 'P': |
| 1933 |
if (!cwdaemon_params_priority(&process_priority, optarg)) { |
| 1934 |
exit(EXIT_FAILURE); |
| 1935 |
} |
| 1936 |
break; |
| 1937 |
#endif |
| 1938 |
case 's': |
| 1939 |
if (!cwdaemon_params_wpm(&default_morse_speed, optarg)) { |
| 1940 |
exit(EXIT_FAILURE); |
| 1941 |
} |
| 1942 |
break; |
| 1943 |
case 't': |
| 1944 |
if (cwdaemon_params_pttdelay(&default_ptt_delay, optarg) != 1) { |
| 1945 |
/* When processing command line arguments we |
| 1946 |
are very strict, and accept only fully |
| 1947 |
valid optarg. */ |
| 1948 |
cwdaemon_debug(CWDAEMON_VERBOSITY_E, __func__, __LINE__, |
| 1949 |
"invalid requested PTT delay [ms]: \"%s\" (should be integer between %d and %d inclusive)", |
| 1950 |
optarg, |
| 1951 |
CWDAEMON_PTT_DELAY_MIN, CWDAEMON_PTT_DELAY_MAX); |
| 1952 |
exit(EXIT_FAILURE); |
| 1953 |
} |
| 1954 |
break; |
| 1955 |
case 'v': |
| 1956 |
if (!cwdaemon_params_volume(&default_morse_volume, optarg)) { |
| 1957 |
exit(EXIT_FAILURE); |
| 1958 |
} |
| 1959 |
break; |
| 1960 |
case 'V': |
| 1961 |
cwdaemon_params_version(); |
| 1962 |
exit(EXIT_SUCCESS); |
| 1963 |
case 'w': |
| 1964 |
if (!cwdaemon_params_weighting(&default_weighting, optarg)) { |
| 1965 |
exit(EXIT_FAILURE); |
| 1966 |
} |
| 1967 |
break; |
| 1968 |
case 'T': |
| 1969 |
if (!cwdaemon_params_tone(&default_morse_tone, optarg)) { |
| 1970 |
exit(EXIT_FAILURE); |
| 1971 |
} |
| 1972 |
break; |
| 1973 |
case 'i': |
| 1974 |
cwdaemon_params_inc_verbosity(&default_verbosity); |
| 1975 |
break; |
| 1976 |
case 'y': |
| 1977 |
if (!cwdaemon_params_set_verbosity(&default_verbosity, optarg)) { |
| 1978 |
exit(EXIT_FAILURE); |
| 1979 |
} |
| 1980 |
break; |
| 1981 |
case 'I': |
| 1982 |
if (!cwdaemon_params_libcwflags(optarg)) { |
| 1983 |
exit(EXIT_FAILURE); |
| 1984 |
} |
| 1985 |
break; |
| 1986 |
case 'f': |
| 1987 |
if (!cwdaemon_params_debugfile(optarg)) { |
| 1988 |
exit(EXIT_FAILURE); |
| 1989 |
} |
| 1990 |
break; |
| 1991 |
case 'x': |
| 1992 |
if (!cwdaemon_params_system(&default_audio_system, optarg)) { |
| 1993 |
exit(EXIT_FAILURE); |
| 1994 |
} |
| 1995 |
break; |
| 1996 |
} |
| 1997 |
|
| 1998 |
return; |
| 1999 |
} |
| 2000 |
|
| 2001 |
|
| 2002 |
bool cwdaemon_params_cwdevice(const char *optarg) |
| 2003 |
{ |
| 2004 |
cwdaemon_debug(CWDAEMON_VERBOSITY_I, __func__, __LINE__, |
| 2005 |
"requested cwdevice \"%s\"", optarg); |
| 2006 |
|
| 2007 |
if (!cwdaemon_cwdevice_set(&global_cwdevice, optarg)) { |
| 2008 |
return false; |
| 2009 |
} else { |
| 2010 |
return true; |
| 2011 |
} |
| 2012 |
} |
| 2013 |
|
| 2014 |
|
| 2015 |
void cwdaemon_params_nofork(void) |
| 2016 |
{ |
| 2017 |
if (forking) { |
| 2018 |
printf("%s: Not forking...\n", PACKAGE); |
| 2019 |
forking = 0; |
| 2020 |
} |
| 2021 |
return; |
| 2022 |
} |
| 2023 |
|
| 2024 |
|
| 2025 |
bool cwdaemon_params_port(const char *optarg) |
| 2026 |
{ |
| 2027 |
long lv = 0; |
| 2028 |
if (!cwdaemon_get_long(optarg, &lv) || lv < 1024 || lv > 65536) { |
| 2029 |
cwdaemon_debug(CWDAEMON_VERBOSITY_E, __func__, __LINE__, |
| 2030 |
"invalid requested port number: \"%s\"", optarg); |
| 2031 |
return false; |
| 2032 |
} else { |
| 2033 |
port = lv; |
| 2034 |
cwdaemon_debug(CWDAEMON_VERBOSITY_I, __func__, __LINE__, |
| 2035 |
"requested port number: \"%ld\"", port); |
| 2036 |
return true; |
| 2037 |
} |
| 2038 |
} |
| 2039 |
|
| 2040 |
|
| 2041 |
bool cwdaemon_params_priority(int *priority, const char *optarg) |
| 2042 |
{ |
| 2043 |
long lv = 0; |
| 2044 |
if (!cwdaemon_get_long(optarg, &lv) || lv < -20 || lv > 20) { |
| 2045 |
cwdaemon_debug(CWDAEMON_VERBOSITY_E, __func__, __LINE__, |
| 2046 |
"invalid requested process priority: \"%s\" (should be integer between -20 and 20 inclusive)", |
| 2047 |
optarg); |
| 2048 |
return false; |
| 2049 |
} else { |
| 2050 |
*priority = (int) lv; |
| 2051 |
cwdaemon_debug(CWDAEMON_VERBOSITY_I, __func__, __LINE__, |
| 2052 |
"requested process priority: \"%ld\"", *priority); |
| 2053 |
return true; |
| 2054 |
} |
| 2055 |
} |
| 2056 |
|
| 2057 |
|
| 2058 |
bool cwdaemon_params_wpm(int *wpm, const char *optarg) |
| 2059 |
{ |
| 2060 |
long lv = 0; |
| 2061 |
if (!cwdaemon_get_long(optarg, &lv) || lv < CW_SPEED_MIN || lv > CW_SPEED_MAX) { |
| 2062 |
cwdaemon_debug(CWDAEMON_VERBOSITY_E, __func__, __LINE__ , |
| 2063 |
"invalid requested morse speed [wpm]: \"%s\" (should be integer between %d and %d inclusive)", |
| 2064 |
optarg, CW_SPEED_MIN, CW_SPEED_MAX); |
| 2065 |
return false; |
| 2066 |
} else { |
| 2067 |
*wpm = (int) lv; |
| 2068 |
cwdaemon_debug(CWDAEMON_VERBOSITY_I, __func__, __LINE__, |
| 2069 |
"requested morse speed [wpm]: \"%d\"", *wpm); |
| 2070 |
return true; |
| 2071 |
} |
| 2072 |
} |
| 2073 |
|
| 2074 |
|
| 2075 |
bool cwdaemon_params_tune(uint32_t *seconds, const char *optarg) |
| 2076 |
{ |
| 2077 |
long lv = 0; |
| 2078 |
|
| 2079 |
/* TODO: replace cwdaemon_get_long() with cwdaemon_get_uint32() */ |
| 2080 |
if (!cwdaemon_get_long(optarg, &lv) || lv < 0 || lv > CWDAEMON_TUNE_SECONDS_MAX) { |
| 2081 |
cwdaemon_debug(CWDAEMON_VERBOSITY_E, __func__, __LINE__ , |
| 2082 |
"invalid requested tuning time [s]: \"%s\" (should be integer between %d and %d inclusive)", |
| 2083 |
optarg, 0, CWDAEMON_TUNE_SECONDS_MAX); |
| 2084 |
return false; |
| 2085 |
} else { |
| 2086 |
cwdaemon_debug(CWDAEMON_VERBOSITY_I, __func__, __LINE__, |
| 2087 |
"requested tuning time [s]: \"%ld\"", lv); |
| 2088 |
|
| 2089 |
*seconds = (uint32_t) lv; |
| 2090 |
|
| 2091 |
return true; |
| 2092 |
} |
| 2093 |
} |
| 2094 |
|
| 2095 |
|
| 2096 |
/** |
| 2097 |
\brief Handle parameter specifying PTT Turn On Delay |
| 2098 |
|
| 2099 |
This function, and handling its return values by callers, isn't as |
| 2100 |
straightforward as it could be. This is because: |
| 2101 |
\li of some backwards-compatibility reasons, |
| 2102 |
\li because expected behaviour when handling command line argument, |
| 2103 |
and when handling network request, are a bit different, |
| 2104 |
\li because negative value of \p optarg is handled differently than |
| 2105 |
too large value of \p optarg. |
| 2106 |
|
| 2107 |
The function clearly rejects negative value passed in \p |
| 2108 |
optarg. Return value is then 0 (a "false" value in boolean |
| 2109 |
expressions). |
| 2110 |
|
| 2111 |
It is more tolerant when it comes to non-negative values, returning |
| 2112 |
1 or 2 (a "true" value in boolean expressions). |
| 2113 |
|
| 2114 |
When the non-negative value is out of range (larger than limit |
| 2115 |
accepted by cwdaemon), the value is clipped to the limit, and put |
| 2116 |
into \p delay. Return value is then 2. Caller of the function may |
| 2117 |
then decide to accept or reject the value. |
| 2118 |
|
| 2119 |
When the non-negative value is in range, the value is put into \p |
| 2120 |
delay, and return value is 1. Caller of the function must accept |
| 2121 |
the value. |
| 2122 |
|
| 2123 |
Value passed in \p optarg is copied to \p delay only when function |
| 2124 |
returns 1 or 2. |
| 2125 |
|
| 2126 |
\return 1 if value of \optarg is acceptable when it was provided as request and as command line argument (i.e. non-negative value in range); |
| 2127 |
\return 2 if value of \optarg is acceptable when it was provided as request, but not acceptable when it was provided as command line argument (i.e. non-negative value out of range); |
| 2128 |
\return 0 if value of \optarg is not acceptable, regardless how it was provided (i.e. a negative value or invalid value); |
| 2129 |
*/ |
| 2130 |
int cwdaemon_params_pttdelay(int *delay, const char *optarg) |
| 2131 |
{ |
| 2132 |
long lv = 0; |
| 2133 |
if (!cwdaemon_get_long(optarg, &lv)) { |
| 2134 |
cwdaemon_debug(CWDAEMON_VERBOSITY_E, __func__, __LINE__, |
| 2135 |
"invalid requested PTT delay [ms]: \"%s\" (should be integer between %d and %d inclusive)", |
| 2136 |
optarg, |
| 2137 |
CWDAEMON_PTT_DELAY_MIN, CWDAEMON_PTT_DELAY_MAX); |
| 2138 |
|
| 2139 |
/* 0 means "Value not acceptable in any context." */ |
| 2140 |
return 0; |
| 2141 |
} |
| 2142 |
|
| 2143 |
if (lv > CWDAEMON_PTT_DELAY_MAX) { |
| 2144 |
|
| 2145 |
/* In theory we should reject invalid value, but for |
| 2146 |
some reason in some contexts we aren't very strict |
| 2147 |
about it. So be it. Just don't allow the value to |
| 2148 |
be larger than *_MAX limit. */ |
| 2149 |
*delay = CWDAEMON_PTT_DELAY_MAX; |
| 2150 |
|
| 2151 |
/* 2 means "Value in general invalid (non-negative, |
| 2152 |
but out of range), but in some contexts we may be |
| 2153 |
tolerant and accept it after it has been decreased |
| 2154 |
to an in-range value (*_MAX). */ |
| 2155 |
return 2; |
| 2156 |
|
| 2157 |
|
| 2158 |
} else if (lv < CWDAEMON_PTT_DELAY_MIN) { |
| 2159 |
|
| 2160 |
/* In first branch of the if() we have accepted too |
| 2161 |
large value from misinformed client, but we can't |
| 2162 |
accept values that are clearly invalid (negative). */ |
| 2163 |
|
| 2164 |
/* 0 means "Value is not acceptable in any context". */ |
| 2165 |
return 0; |
| 2166 |
|
| 2167 |
} else { /* Non-negative, in range. */ |
| 2168 |
*delay = (int) lv; |
| 2169 |
|
| 2170 |
cwdaemon_debug(CWDAEMON_VERBOSITY_I, __func__, __LINE__, |
| 2171 |
"requested PTT delay [ms]: \"%ld\"", *delay); |
| 2172 |
|
| 2173 |
/* 1 means "Value valid in all contexts." */ |
| 2174 |
return 1; |
| 2175 |
} |
| 2176 |
} |
| 2177 |
|
| 2178 |
|
| 2179 |
bool cwdaemon_params_volume(int *volume, const char *optarg) |
| 2180 |
{ |
| 2181 |
long lv = 0; |
| 2182 |
if (!cwdaemon_get_long(optarg, &lv) || lv < CW_VOLUME_MIN || lv > CW_VOLUME_MAX) { |
| 2183 |
cwdaemon_debug(CWDAEMON_VERBOSITY_E, __func__, __LINE__, |
| 2184 |
"invalid requested volume [%%]: \"%s\" (should be integer between %d and %d inclusive)", |
| 2185 |
optarg, CW_VOLUME_MIN, CW_VOLUME_MAX); |
| 2186 |
return false; |
| 2187 |
} else { |
| 2188 |
*volume = (int) lv; |
| 2189 |
cwdaemon_debug(CWDAEMON_VERBOSITY_I, __func__, __LINE__, |
| 2190 |
"requested volume [%%]: \"%d\"", *volume); |
| 2191 |
return true; |
| 2192 |
} |
| 2193 |
} |
| 2194 |
|
| 2195 |
|
| 2196 |
void cwdaemon_params_version(void) |
| 2197 |
{ |
| 2198 |
printf("%s version %s\n", PACKAGE, VERSION); |
| 2199 |
return; |
| 2200 |
} |
| 2201 |
|
| 2202 |
|
| 2203 |
bool cwdaemon_params_weighting(int *weighting, const char *optarg) |
| 2204 |
{ |
| 2205 |
long lv = 0; |
| 2206 |
if (!cwdaemon_get_long(optarg, &lv) || lv < CWDAEMON_MORSE_WEIGHTING_MIN || lv > CWDAEMON_MORSE_WEIGHTING_MAX) { |
| 2207 |
cwdaemon_debug(CWDAEMON_VERBOSITY_E, __func__, __LINE__, |
| 2208 |
"invalid requested weighting: \"%s\" (should be integer between %d and %d inclusive)", |
| 2209 |
optarg, CWDAEMON_MORSE_WEIGHTING_MIN, CWDAEMON_MORSE_WEIGHTING_MAX); |
| 2210 |
return false; |
| 2211 |
} else { |
| 2212 |
*weighting = (int) lv; |
| 2213 |
cwdaemon_debug(CWDAEMON_VERBOSITY_I, __func__, __LINE__, |
| 2214 |
"requested weighting: \"%ld\"", *weighting); |
| 2215 |
return true; |
| 2216 |
} |
| 2217 |
} |
| 2218 |
|
| 2219 |
|
| 2220 |
bool cwdaemon_params_tone(int *tone, const char *optarg) |
| 2221 |
{ |
| 2222 |
long lv = 0; |
| 2223 |
if (!cwdaemon_get_long(optarg, &lv) || lv < CW_FREQUENCY_MIN || lv > CW_FREQUENCY_MAX) { |
| 2224 |
cwdaemon_debug(CWDAEMON_VERBOSITY_E, __func__, __LINE__, |
| 2225 |
"invalid requested tone [Hz]: \"%s\" (should be integer between %d and %d inclusive)", |
| 2226 |
optarg, CW_FREQUENCY_MIN, CW_FREQUENCY_MAX); |
| 2227 |
return false; |
| 2228 |
} else { |
| 2229 |
*tone = (int) lv; |
| 2230 |
cwdaemon_debug(CWDAEMON_VERBOSITY_I, __func__, __LINE__, |
| 2231 |
"requested tone [Hz]: \"%ld\"", *tone); |
| 2232 |
return true; |
| 2233 |
} |
| 2234 |
} |
| 2235 |
|
| 2236 |
|
| 2237 |
void cwdaemon_params_inc_verbosity(int *verbosity) |
| 2238 |
{ |
| 2239 |
if (*verbosity < CWDAEMON_VERBOSITY_D) { |
| 2240 |
(*verbosity)++; |
| 2241 |
|
| 2242 |
cwdaemon_debug(CWDAEMON_VERBOSITY_I, __func__, __LINE__, |
| 2243 |
"requested verbosity level threshold: \"%s\"", cwdaemon_verbosity_labels[*verbosity]); |
| 2244 |
} |
| 2245 |
|
| 2246 |
return; |
| 2247 |
} |
| 2248 |
|
| 2249 |
|
| 2250 |
bool cwdaemon_params_set_verbosity(int *verbosity, const char *optarg) |
| 2251 |
{ |
| 2252 |
if (!strcmp(optarg, "n") || !strcmp(optarg, "N")) { |
| 2253 |
*verbosity = CWDAEMON_VERBOSITY_N; |
| 2254 |
} else if (!strcmp(optarg, "e") || !strcmp(optarg, "E")) { |
| 2255 |
*verbosity = CWDAEMON_VERBOSITY_E; |
| 2256 |
} else if (!strcmp(optarg, "w") || !strcmp(optarg, "W")) { |
| 2257 |
*verbosity = CWDAEMON_VERBOSITY_W; |
| 2258 |
} else if (!strcmp(optarg, "i") || !strcmp(optarg, "I")) { |
| 2259 |
*verbosity = CWDAEMON_VERBOSITY_I; |
| 2260 |
} else if (!strcmp(optarg, "d") || !strcmp(optarg, "D")) { |
| 2261 |
*verbosity = CWDAEMON_VERBOSITY_D; |
| 2262 |
} else { |
| 2263 |
cwdaemon_debug(CWDAEMON_VERBOSITY_E, __func__, __LINE__, |
| 2264 |
"invalid requested verbosity level: \"%s\"", optarg); |
| 2265 |
return false; |
| 2266 |
} |
| 2267 |
|
| 2268 |
cwdaemon_debug(CWDAEMON_VERBOSITY_I, __func__, __LINE__, |
| 2269 |
"requested verbosity level threshold: \"%s\"", cwdaemon_verbosity_labels[*verbosity]); |
| 2270 |
return true; |
| 2271 |
} |
| 2272 |
|
| 2273 |
|
| 2274 |
bool cwdaemon_params_libcwflags(const char *optarg) |
| 2275 |
{ |
| 2276 |
long lv = 0; |
| 2277 |
if (!cwdaemon_get_long(optarg, &lv)) { |
| 2278 |
cwdaemon_debug(CWDAEMON_VERBOSITY_E, __func__, __LINE__, |
| 2279 |
"invalid requested debug flags: \"%s\" (should be numeric value)", optarg); |
| 2280 |
libcw_debug_flags = 0; |
| 2281 |
|
| 2282 |
return false; |
| 2283 |
} else { |
| 2284 |
libcw_debug_flags = lv; |
| 2285 |
cwdaemon_debug(CWDAEMON_VERBOSITY_I, __func__, __LINE__, |
| 2286 |
"requested libcw debug flags: \"%ld\"", libcw_debug_flags); |
| 2287 |
return true; |
| 2288 |
} |
| 2289 |
} |
| 2290 |
|
| 2291 |
|
| 2292 |
bool cwdaemon_params_debugfile(const char *optarg) |
| 2293 |
{ |
| 2294 |
if (!strcmp(optarg, "syslog")) { |
| 2295 |
cwdaemon_debug(CWDAEMON_VERBOSITY_E, __func__, __LINE__, |
| 2296 |
"debug output file \"%s\" not implemented yet, try it in later versions of %s", |
| 2297 |
optarg, PACKAGE); |
| 2298 |
return false; |
| 2299 |
} |
| 2300 |
|
| 2301 |
cwdaemon_debug_f_path = strdup(optarg); |
| 2302 |
if (!cwdaemon_debug_f_path) { |
| 2303 |
cwdaemon_debug(CWDAEMON_VERBOSITY_E, __func__, __LINE__, |
| 2304 |
"failed to copy path to debug file \"%s\"", optarg); |
| 2305 |
return false; |
| 2306 |
} else { |
| 2307 |
cwdaemon_debug(CWDAEMON_VERBOSITY_I, __func__, __LINE__, |
| 2308 |
"requested debug file path: \"%s\"", cwdaemon_debug_f_path); |
| 2309 |
return true; |
| 2310 |
} |
| 2311 |
} |
| 2312 |
|
| 2313 |
|
| 2314 |
bool cwdaemon_params_system(int *system, const char *optarg) |
| 2315 |
{ |
| 2316 |
if (!strncmp(optarg, "n", 1)) { |
| 2317 |
*system = CW_AUDIO_NULL; |
| 2318 |
} else if (!strncmp(optarg, "c", 1)) { |
| 2319 |
*system = CW_AUDIO_CONSOLE; |
| 2320 |
} else if (!strncmp(optarg, "s", 1)) { |
| 2321 |
*system = CW_AUDIO_SOUNDCARD; |
| 2322 |
} else if (!strncmp(optarg, "a", 1)) { |
| 2323 |
*system = CW_AUDIO_ALSA; |
| 2324 |
} else if (!strncmp(optarg, "p", 1)) { |
| 2325 |
*system = CW_AUDIO_PA; |
| 2326 |
} else if (!strncmp(optarg, "o", 1)) { |
| 2327 |
*system = CW_AUDIO_OSS; |
| 2328 |
} else { |
| 2329 |
/* TODO: print only those audio systems that are |
| 2330 |
supported on given machine. */ |
| 2331 |
cwdaemon_debug(CWDAEMON_VERBOSITY_E, __func__, __LINE__, |
| 2332 |
"invalid requested sound system: \"%s\" (use c(onsole), o(ss), a(lsa), p(ulseaudio), n(ull - no audio), or s(oundcard - autoselect from OSS/ALSA/PulseAudio))", |
| 2333 |
optarg); |
| 2334 |
return false; |
| 2335 |
} |
| 2336 |
|
| 2337 |
cwdaemon_debug(CWDAEMON_VERBOSITY_I, __func__, __LINE__, |
| 2338 |
"requested sound system: \"%s\" (\"%s\")", optarg, cw_get_audio_system_label(*system)); |
| 2339 |
return true; |
| 2340 |
} |
| 2341 |
|
| 2342 |
|
| 2343 |
bool cwdaemon_params_ptt_on_off(const char *optarg) |
| 2344 |
{ |
| 2345 |
long lv = 0; |
| 2346 |
|
| 2347 |
/* PTT keying on or off */ |
| 2348 |
if (!cwdaemon_get_long(optarg, &lv)) { |
| 2349 |
cwdaemon_debug(CWDAEMON_VERBOSITY_E, __func__, __LINE__, |
| 2350 |
"invalid requested PTT state: \"%s\" (should be numeric value \"0\" or \"1\")", optarg); |
| 2351 |
return false; |
| 2352 |
} else { |
| 2353 |
cwdaemon_debug(CWDAEMON_VERBOSITY_I, __func__, __LINE__, |
| 2354 |
"requested PTT state: \"%s\"", optarg); |
| 2355 |
} |
| 2356 |
|
| 2357 |
if (lv) { |
| 2358 |
//global_cwdevice->ptt(global_cwdevice, ON); |
| 2359 |
if (current_ptt_delay) { |
| 2360 |
cwdaemon_set_ptt_on(global_cwdevice, "PTT (manual, delay) on"); |
| 2361 |
} else { |
| 2362 |
cwdaemon_debug(CWDAEMON_VERBOSITY_I, __func__, __LINE__, "PTT (manual, immediate) on"); |
| 2363 |
} |
| 2364 |
|
| 2365 |
ptt_flag |= PTT_ACTIVE_MANUAL; |
| 2366 |
cwdaemon_debug(CWDAEMON_VERBOSITY_D, __func__, __LINE__, "PTT flag +PTT_ACTIVE_MANUAL (0x%02x/%s)", ptt_flag, cwdaemon_debug_ptt_flags()); |
| 2367 |
|
| 2368 |
} else if (ptt_flag & PTT_ACTIVE_MANUAL) { /* only if manually activated */ |
| 2369 |
|
| 2370 |
ptt_flag &= ~PTT_ACTIVE_MANUAL; |
| 2371 |
cwdaemon_debug(CWDAEMON_VERBOSITY_D, __func__, __LINE__, "PTT flag -PTT_ACTIVE_MANUAL (0x%02x/%s)", ptt_flag, cwdaemon_debug_ptt_flags()); |
| 2372 |
|
| 2373 |
if (!(ptt_flag & !PTT_ACTIVE_AUTO)) { /* no PTT modifiers */ |
| 2374 |
|
| 2375 |
if (request_queue[0] == '\0'/* no new text in the meantime */ |
| 2376 |
&& cw_get_tone_queue_length() <= 1) { |
| 2377 |
|
| 2378 |
cwdaemon_set_ptt_off(global_cwdevice, "PTT (manual, immediate) off"); |
| 2379 |
} else { |
| 2380 |
/* still sending, cannot yet switch PTT off */ |
| 2381 |
ptt_flag |= PTT_ACTIVE_AUTO; /* ensure auto-PTT active */ |
| 2382 |
cwdaemon_debug(CWDAEMON_VERBOSITY_D, __func__, __LINE__, "PTT flag +PTT_ACTIVE_AUTO (0x%02x/%s)", ptt_flag, cwdaemon_debug_ptt_flags()); |
| 2383 |
|
| 2384 |
cwdaemon_debug(CWDAEMON_VERBOSITY_I, __func__, __LINE__, "reverting from PTT (manual) to PTT (auto) now"); |
| 2385 |
} |
| 2386 |
} |
| 2387 |
} |
| 2388 |
|
| 2389 |
return true; |
| 2390 |
} |
| 2391 |
|
| 2392 |
|
| 2393 |
|
| 2394 |
|
| 2395 |
|
| 2396 |
/** |
| 2397 |
\brief Get and parse command line options |
| 2398 |
|
| 2399 |
Scan program's arguments, check for command line options, parse them. |
| 2400 |
|
| 2401 |
\param argc - main()'s argc argument |
| 2402 |
\param argv - main()'s argv argument |
| 2403 |
*/ |
| 2404 |
void cwdaemon_args_parse(int argc, char *argv[]) |
| 2405 |
{ |
| 2406 |
#if defined(HAVE_GETOPT_H) |
| 2407 |
cwdaemon_args_process_long(argc, argv); |
| 2408 |
#else |
| 2409 |
int p; |
| 2410 |
|
| 2411 |
while ((p = getopt(argc, argv, cwdaemon_args_short)) != -1) { |
| 2412 |
long lv; |
| 2413 |
|
| 2414 |
cwdaemon_args_process_short(p, optarg); |
| 2415 |
} |
| 2416 |
#endif |
| 2417 |
return; |
| 2418 |
} |
| 2419 |
|
| 2420 |
|
| 2421 |
|
| 2422 |
|
| 2423 |
|
| 2424 |
/** |
| 2425 |
\brief Configure file descriptor for debug output |
| 2426 |
*/ |
| 2427 |
void cwdaemon_debug_open(void) |
| 2428 |
{ |
| 2429 |
/* First handle a clash of command line arguments. */ |
| 2430 |
if (forking) { |
| 2431 |
if (cwdaemon_debug_f_path |
| 2432 |
&& (!strcmp(cwdaemon_debug_f_path, "stdout") |
| 2433 |
|| !strcmp(cwdaemon_debug_f_path, "stderr"))) { |
| 2434 |
|
| 2435 |
/* If the path is not specified (path is |
| 2436 |
NULL), it may imply that the debug output |
| 2437 |
should be stdout. But since we explicitly |
| 2438 |
asked for forking, the implied debug output |
| 2439 |
to stdout will be ignored. This is a valid |
| 2440 |
situation: explicit request for forking |
| 2441 |
overrides implicit expectation of debug |
| 2442 |
file being stdout. |
| 2443 |
|
| 2444 |
On the other hand, if we explicitly asked |
| 2445 |
for debug output to be stdout or stderr, |
| 2446 |
and at the same time explicitly asked for |
| 2447 |
forking, that is completely different |
| 2448 |
matter. This is an error (clash of command |
| 2449 |
line arguments). */ |
| 2450 |
|
| 2451 |
fprintf(stdout, "%s:EE: specified debug output to \"%s\" when forking\n", |
| 2452 |
PACKAGE, cwdaemon_debug_f_path); |
| 2453 |
|
| 2454 |
exit(EXIT_FAILURE); |
| 2455 |
} |
| 2456 |
} |
| 2457 |
|
| 2458 |
|
| 2459 |
/* Handle valid situations (after eliminating possible invalid |
| 2460 |
situations above). */ |
| 2461 |
|
| 2462 |
if (!cwdaemon_debug_f_path) { |
| 2463 |
|
| 2464 |
if (!forking) { |
| 2465 |
/* stdout is (for historical reasons) a |
| 2466 |
default file for printing debug strings |
| 2467 |
when not forking, so if a debug file hasn't |
| 2468 |
been defined in command line, the |
| 2469 |
cwdaemon_debug_f_path will be NULL. Treat |
| 2470 |
this situation accordingly.*/ |
| 2471 |
|
| 2472 |
fprintf(stderr, "debug output: stdout\n"); |
| 2473 |
/* This has been already done in main(), but |
| 2474 |
do it again to be super-explicit. */ |
| 2475 |
cwdaemon_debug_f = stdout; |
| 2476 |
} else { |
| 2477 |
/* Remember that in main() the file is set to |
| 2478 |
stdout. Obviously we aren't going to use |
| 2479 |
the stdout when daemon has forked. */ |
| 2480 |
cwdaemon_debug_f = NULL; |
| 2481 |
} |
| 2482 |
|
| 2483 |
} else if (!strcmp(cwdaemon_debug_f_path, "stdout")) { |
| 2484 |
|
| 2485 |
/* We shouldn't get here when forking (invalid |
| 2486 |
situation handled by first "if" in the |
| 2487 |
function). */ |
| 2488 |
assert(!forking); |
| 2489 |
|
| 2490 |
fprintf(stderr, "debug output: stdout\n"); |
| 2491 |
cwdaemon_debug_f = stdout; |
| 2492 |
|
| 2493 |
} else if (!strcmp(cwdaemon_debug_f_path, "stderr")) { |
| 2494 |
|
| 2495 |
/* We shouldn't get here when forking (invalid |
| 2496 |
situation handled by first "if" in the |
| 2497 |
function). */ |
| 2498 |
assert(!forking); |
| 2499 |
|
| 2500 |
fprintf(stderr, "debug output: stderr\n"); |
| 2501 |
cwdaemon_debug_f = stderr; |
| 2502 |
|
| 2503 |
} else { /* cwdaemon_debug_f_path is a path to disc file. */ |
| 2504 |
|
| 2505 |
/* We can get here when both forking and not forking. */ |
| 2506 |
|
| 2507 |
fprintf(stderr, "debug output: %s\n", cwdaemon_debug_f_path); |
| 2508 |
|
| 2509 |
cwdaemon_debug_f = fopen(cwdaemon_debug_f_path, "w+"); |
| 2510 |
if (!cwdaemon_debug_f) { |
| 2511 |
fprintf(stderr, "%s: failed to open output debug file \"%s\"\n", PACKAGE, cwdaemon_debug_f_path); |
| 2512 |
exit(EXIT_FAILURE); |
| 2513 |
} |
| 2514 |
} |
| 2515 |
|
| 2516 |
return; |
| 2517 |
} |
| 2518 |
|
| 2519 |
|
| 2520 |
|
| 2521 |
|
| 2522 |
|
| 2523 |
void cwdaemon_args_help(void) |
| 2524 |
{ |
| 2525 |
printf("Usage: %s [option]...\n", PACKAGE); |
| 2526 |
printf("Long options may not be supported on your system.\n\n"); |
| 2527 |
|
| 2528 |
printf("-h, --help\n"); |
| 2529 |
printf(" Print this help and exit.\n"); |
| 2530 |
printf("-V, --version\n"); |
| 2531 |
printf(" Print version information and exit.\n"); |
| 2532 |
|
| 2533 |
printf("-d, --cwdevice <device>\n"); |
| 2534 |
printf(" Use a different device.\n"); |
| 2535 |
#if defined (HAVE_LINUX_PPDEV_H) |
| 2536 |
printf(" (e.g. ttyS0,1,2, parport0,1, etc. default: parport0)\n"); |
| 2537 |
#elif defined (HAVE_DEV_PPBUS_PPI_H) |
| 2538 |
printf(" (e.g. ttyd0,1,2, ppi0,1, etc. default: ppi0)\n"); |
| 2539 |
#else |
| 2540 |
#ifdef BSD |
| 2541 |
printf(" (e.g. ttyd0,1,2, etc. default: ttyd0)\n"); |
| 2542 |
#else |
| 2543 |
printf(" (e.g. ttyS0,1,2, etc. default: ttyS0)\n"); |
| 2544 |
#endif |
| 2545 |
#endif |
| 2546 |
printf(" Use \"null\" for dummy device (no rig keying, no ssb keying, etc.).\n"); |
| 2547 |
|
| 2548 |
printf("-n, --nofork\n"); |
| 2549 |
printf(" Do not fork. Print debug information to stdout.\n"); |
| 2550 |
printf("-p, --port <port>\n"); |
| 2551 |
printf(" Use a different UDP port number (> 1023, default: %d).\n", CWDAEMON_NETWORK_PORT_DEFAULT); |
| 2552 |
#if defined(HAVE_SETPRIORITY) && defined(PRIO_PROCESS) |
| 2553 |
printf("-P, --priority <priority>\n"); |
| 2554 |
printf(" Set program's priority (-20 - 20, default: 0).\n"); |
| 2555 |
#endif |
| 2556 |
printf("-s, --wpm <speed>\n"); |
| 2557 |
printf(" Set Morse speed [wpm] (%d - %d, default: %d).\n", |
| 2558 |
CW_SPEED_MIN, CW_SPEED_MAX, CWDAEMON_MORSE_SPEED_DEFAULT); |
| 2559 |
printf("-t, --pttdelay <time>\n"); |
| 2560 |
printf(" Set PTT delay [ms] (%d - %d, default: %d).\n", |
| 2561 |
CWDAEMON_PTT_DELAY_MIN, CWDAEMON_PTT_DELAY_MAX, CWDAEMON_PTT_DELAY_DEFAULT); |
| 2562 |
printf("-x, --system <sound system>\n"); |
| 2563 |
printf(" Use a specific sound system:\n"); |
| 2564 |
printf(" c = console buzzer (default)\n"); |
| 2565 |
printf(" o = OSS\n"); |
| 2566 |
printf(" a = ALSA\n"); |
| 2567 |
printf(" p = PulseAudio\n"); |
| 2568 |
printf(" n = null (no audio)\n"); |
| 2569 |
printf(" s = soundcard (autoselect from OSS/ALSA/PulseAudio)\n"); |
| 2570 |
printf("-v, --volume <volume>\n"); |
| 2571 |
printf(" Set volume for soundcard output [%%] (%d - %d, default: %d).\n", |
| 2572 |
CW_VOLUME_MIN, CW_VOLUME_MAX, CWDAEMON_MORSE_VOLUME_DEFAULT); |
| 2573 |
printf("-w, --weighting <weight>\n"); |
| 2574 |
printf(" Set weighting (%d - %d, default: %d).\n", |
| 2575 |
CWDAEMON_MORSE_WEIGHTING_MIN, CWDAEMON_MORSE_WEIGHTING_MAX, CWDAEMON_MORSE_WEIGHTING_DEFAULT); |
| 2576 |
printf("-T, --tone <tone>\n"); |
| 2577 |
printf(" Set initial tone [Hz] (%d - %d, default: %d).\n", |
| 2578 |
CW_FREQUENCY_MIN, CW_FREQUENCY_MAX, CWDAEMON_MORSE_TONE_DEFAULT); |
| 2579 |
printf("-i\n"); |
| 2580 |
printf(" Increase verbosity of debug strings printed by cwademon.\n"); |
| 2581 |
printf(" Repeat for even more verbosity (e.g. -iii).\n"); |
| 2582 |
printf(" Alternatively you can use -y/--verbosity option.\n"); |
| 2583 |
printf("-y, --verbosity <threshold>\n"); |
| 2584 |
printf(" Set verbosity level threshold for debug strings printed by cwdaemon.\n"); |
| 2585 |
printf(" Recognized values:\n"); |
| 2586 |
printf(" n = none\n"); |
| 2587 |
printf(" e = errors\n"); |
| 2588 |
printf(" w = warnings (default)\n"); |
| 2589 |
printf(" i = information\n"); |
| 2590 |
printf(" d = details\n"); |
| 2591 |
printf(" Alternatively you can use -i option.\n"); |
| 2592 |
printf("-I, --libcwflags <flags>\n"); |
| 2593 |
printf(" Numeric value of debug flags to be passed to libcw.\n"); |
| 2594 |
printf("-f, --debugfile <output>\n"); |
| 2595 |
printf(" Print debug information to <output> instead of stdout.\n"); |
| 2596 |
printf(" Value of <output> can be explicitly stated as \"stdout\"\n"); |
| 2597 |
printf(" (when not forking).\n"); |
| 2598 |
printf(" Value of <output> can be also \"stderr\" (when not forking).\n"); |
| 2599 |
printf(" Special value of <output> being \"syslog\" is reserved for\n"); |
| 2600 |
printf(" future use. For now it will be rejected as invalid.\n"); |
| 2601 |
printf(" Passing path to disc file as value of <output> works in both\n"); |
| 2602 |
printf(" situations: when forking and when not forking.\n"); |
| 2603 |
printf("\n"); |
| 2604 |
|
| 2605 |
return; |
| 2606 |
} |
| 2607 |
|
| 2608 |
|
| 2609 |
|
| 2610 |
|
| 2611 |
|
| 2612 |
/* main program: fork, open network connection and go into an endless loop |
| 2613 |
waiting for something to happen on the UDP port */ |
| 2614 |
int main(int argc, char *argv[]) |
| 2615 |
{ |
| 2616 |
//cw_debug_set_flags(&cw_debug_object_dev, CW_DEBUG_GENERATOR | CW_DEBUG_SOUND_SYSTEM); |
| 2617 |
//(&cw_debug_object_dev)->level = CW_DEBUG_DEBUG; |
| 2618 |
|
| 2619 |
/* Until a call to cwdaemon_debug_open() is made and debug |
| 2620 |
output is configured according to command line switches, |
| 2621 |
use the default "stdout" file. */ |
| 2622 |
cwdaemon_debug_f = stdout; |
| 2623 |
|
| 2624 |
atexit(cwdaemon_cwdevices_free); |
| 2625 |
if (!cwdaemon_cwdevices_init()) { |
| 2626 |
exit(EXIT_FAILURE); |
| 2627 |
} |
| 2628 |
|
| 2629 |
atexit(cwdaemon_cwdevice_free); |
| 2630 |
/* Sets global_cwdevice to null device. This may be overridden |
| 2631 |
with command line argument. */ |
| 2632 |
cwdaemon_cwdevice_init(); |
| 2633 |
|
| 2634 |
cwdaemon_args_parse(argc, argv); |
| 2635 |
|
| 2636 |
atexit(cwdaemon_debug_close); |
| 2637 |
/* Call cwdaemon_debug_open() after parsing command line |
| 2638 |
arguments. Errors discovered during parsing of command line |
| 2639 |
args will then still be printed to stderr. Options for |
| 2640 |
debug output can be also passed as command line args, so |
| 2641 |
they weren't available until now. |
| 2642 |
|
| 2643 |
TODO: perhaps opening debug output should be moved to a |
| 2644 |
later stage, so that as many debug strings as possible are |
| 2645 |
being printed to stdout before main daemon loop? */ |
| 2646 |
cwdaemon_debug_open(); |
| 2647 |
|
| 2648 |
if (forking) { |
| 2649 |
|
| 2650 |
pid_t pid = fork(); |
| 2651 |
if (pid < 0) { |
| 2652 |
printf("%s: Fork failed: \"%s\"\n", PACKAGE, |
| 2653 |
strerror(errno)); |
| 2654 |
exit(EXIT_FAILURE); |
| 2655 |
} |
| 2656 |
|
| 2657 |
if (pid > 0) { |
| 2658 |
/* We are the parent process. The process is |
| 2659 |
no longer needed at this point. */ |
| 2660 |
exit(EXIT_SUCCESS); |
| 2661 |
} |
| 2662 |
|
| 2663 |
// cwdaemon_debug(CWDAEMON_VERBOSITY_I, __func__, __LINE__, "Child process reporting."); |
| 2664 |
openlog("netkeyer", LOG_PID, LOG_DAEMON); |
| 2665 |
pid_t sid = setsid(); |
| 2666 |
if (sid < 0) { |
| 2667 |
syslog(LOG_ERR, "%s\n", "setsid"); |
| 2668 |
exit(EXIT_FAILURE); |
| 2669 |
} |
| 2670 |
|
| 2671 |
if ((chdir("/")) < 0) { |
| 2672 |
syslog(LOG_ERR, "%s\n", "chdir"); |
| 2673 |
exit(EXIT_FAILURE); |
| 2674 |
} |
| 2675 |
umask(0); |
| 2676 |
|
| 2677 |
int fd; |
| 2678 |
/* Replace stdin/stdout/stderr with /dev/null. */ |
| 2679 |
if ((fd = open("/dev/null", O_RDWR, 0)) == -1) { |
| 2680 |
syslog(LOG_ERR, "%s\n", "open /dev/null"); |
| 2681 |
exit(EXIT_FAILURE); |
| 2682 |
} |
| 2683 |
if (dup2(fd, STDIN_FILENO) == -1) { |
| 2684 |
syslog(LOG_ERR, "%s\n", "dup2 stdin"); |
| 2685 |
exit(EXIT_FAILURE); |
| 2686 |
} |
| 2687 |
if (dup2(fd, STDOUT_FILENO) == -1) { |
| 2688 |
syslog(LOG_ERR, "%s\n", "dup2 stdout"); |
| 2689 |
exit(EXIT_FAILURE); |
| 2690 |
} |
| 2691 |
if (dup2(fd, STDERR_FILENO) == -1) { |
| 2692 |
syslog(LOG_ERR, "%s\n", "dup2 stderr"); |
| 2693 |
exit(EXIT_FAILURE); |
| 2694 |
} |
| 2695 |
if (fd != STDIN_FILENO && fd != STDOUT_FILENO && fd != STDERR_FILENO) { |
| 2696 |
(void) close(fd); |
| 2697 |
} |
| 2698 |
} else { |
| 2699 |
fprintf(stdout, "Press ^C to quit\n"); |
| 2700 |
signal(SIGINT, cwdaemon_catch_sigint); |
| 2701 |
} |
| 2702 |
|
| 2703 |
atexit(cwdaemon_close_socket); |
| 2704 |
if (!cwdaemon_initialize_socket()) { |
| 2705 |
exit(EXIT_FAILURE); |
| 2706 |
} |
| 2707 |
|
| 2708 |
#if defined(HAVE_SETPRIORITY) && defined(PRIO_PROCESS) |
| 2709 |
if (process_priority != 0) { |
| 2710 |
if (setpriority(PRIO_PROCESS, getpid(), process_priority) < 0) { |
| 2711 |
cwdaemon_errmsg("Setting process priority: \"%s\"", strerror(errno)); |
| 2712 |
exit(EXIT_FAILURE); |
| 2713 |
} |
| 2714 |
} |
| 2715 |
#endif |
| 2716 |
|
| 2717 |
/* Initialize libcw (and other things) here, this late, to be |
| 2718 |
sure that libcw has been initialized and is used only by |
| 2719 |
child process, not by parent process. */ |
| 2720 |
atexit(cwdaemon_close_libcw_output); |
| 2721 |
cwdaemon_reset_almost_all(); |
| 2722 |
if (!has_audio_output) { |
| 2723 |
/* Failed to open libcw output. */ |
| 2724 |
exit(EXIT_FAILURE); |
| 2725 |
} |
| 2726 |
|
| 2727 |
if (libcw_debug_flags) { /* debugging libcw as well */ |
| 2728 |
cw_set_debug_flags(libcw_debug_flags); |
| 2729 |
} |
| 2730 |
|
| 2731 |
|
| 2732 |
cw_register_keying_callback(cwdaemon_keyingevent, NULL); |
| 2733 |
|
| 2734 |
|
| 2735 |
/* The main loop of cwdaemon. */ |
| 2736 |
request_queue[0] = '\0'; |
| 2737 |
do { |
| 2738 |
fd_set readfd; |
| 2739 |
struct timeval udptime; |
| 2740 |
|
| 2741 |
FD_ZERO(&readfd); |
| 2742 |
FD_SET(socket_descriptor, &readfd); |
| 2743 |
|
| 2744 |
if (inactivity_seconds < 30) { |
| 2745 |
udptime.tv_sec = 1; |
| 2746 |
inactivity_seconds++; |
| 2747 |
} else { |
| 2748 |
udptime.tv_sec = 86400; |
| 2749 |
} |
| 2750 |
|
| 2751 |
udptime.tv_usec = 0; |
| 2752 |
/* udptime.tv_usec = 999000; */ /* 1s is more than enough */ |
| 2753 |
int fd_count = select(socket_descriptor + 1, &readfd, NULL, NULL, &udptime); |
| 2754 |
/* int fd_count = select(socket_descriptor + 1, &readfd, NULL, NULL, NULL); */ |
| 2755 |
if (fd_count == -1 && errno != EINTR) { |
| 2756 |
cwdaemon_errmsg("Select"); |
| 2757 |
} else { |
| 2758 |
cwdaemon_receive(); |
| 2759 |
} |
| 2760 |
|
| 2761 |
#if defined (HAVE_LINUX_PPDEV_H) || defined (HAVE_DEV_PPBUS_PPI_H) |
| 2762 |
if (global_cwdevice->footswitch) { |
| 2763 |
int state = global_cwdevice->footswitch(global_cwdevice); |
| 2764 |
global_cwdevice->ptt(global_cwdevice, !state); |
| 2765 |
} |
| 2766 |
#endif |
| 2767 |
|
| 2768 |
} while (1); |
| 2769 |
|
| 2770 |
exit(EXIT_SUCCESS); |
| 2771 |
} |
| 2772 |
|
| 2773 |
|
| 2774 |
|
| 2775 |
|
| 2776 |
|
| 2777 |
/** |
| 2778 |
\brief Set up initial values of cw keying device names |
| 2779 |
|
| 2780 |
Allocate strings with device names, assign them to appropriate |
| 2781 |
cwdevice_xyz.desc values. |
| 2782 |
|
| 2783 |
The strings can be later deallocated with |
| 2784 |
cwdaemon_cwdevices_free(). |
| 2785 |
|
| 2786 |
\return true |
| 2787 |
*/ |
| 2788 |
bool cwdaemon_cwdevices_init(void) |
| 2789 |
{ |
| 2790 |
/* Default device description of parallel/lpt port. */ |
| 2791 |
#if defined HAVE_LINUX_PPDEV_H /* Linux, obviously. */ |
| 2792 |
cwdevice_lp.desc = strdup("parport0"); |
| 2793 |
#elif defined HAVE_DEV_PPBUS_PPI_H /* FreeBSD (ppbus/ppi). */ |
| 2794 |
cwdevice_lp.desc = strdup("ppi0"); |
| 2795 |
#else /* FIXME: where is OpenBSD? */ |
| 2796 |
|
| 2797 |
#endif |
| 2798 |
|
| 2799 |
|
| 2800 |
/* Default device description of serial/com port. */ |
| 2801 |
#if defined(__linux__) |
| 2802 |
cwdevice_ttys.desc = strdup("ttyS0"); |
| 2803 |
#elif defined(__FreeBSD__) |
| 2804 |
cwdevice_ttys.desc = strdup("ttyd0"); |
| 2805 |
#elif defined(__OpenBSD__) |
| 2806 |
cwdevice_ttys.desc = strdup("tty00"); |
| 2807 |
#else |
| 2808 |
cwdevice_ttys.desc = NULL; |
| 2809 |
#endif |
| 2810 |
|
| 2811 |
|
| 2812 |
/* Default device description of null port. */ |
| 2813 |
cwdevice_null.desc = strdup("null"); |
| 2814 |
|
| 2815 |
|
| 2816 |
/* TODO: add checks of return values. */ |
| 2817 |
|
| 2818 |
return true; |
| 2819 |
} |
| 2820 |
|
| 2821 |
|
| 2822 |
|
| 2823 |
|
| 2824 |
|
| 2825 |
/** |
| 2826 |
\brief Deallocate strings with cw keying device names |
| 2827 |
|
| 2828 |
Function frees memory previously allocated with |
| 2829 |
cwdaemon_cwdevices_init(). |
| 2830 |
*/ |
| 2831 |
void cwdaemon_cwdevices_free(void) |
| 2832 |
{ |
| 2833 |
if (cwdevice_ttys.desc) { |
| 2834 |
free(cwdevice_ttys.desc); |
| 2835 |
cwdevice_ttys.desc = NULL; |
| 2836 |
} |
| 2837 |
|
| 2838 |
if (cwdevice_null.desc) { |
| 2839 |
free(cwdevice_null.desc); |
| 2840 |
cwdevice_null.desc = NULL; |
| 2841 |
} |
| 2842 |
|
| 2843 |
#if defined (HAVE_LINUX_PPDEV_H) || defined (HAVE_DEV_PPBUS_PPI_H) |
| 2844 |
if (cwdevice_lp.desc) { |
| 2845 |
free(cwdevice_lp.desc); |
| 2846 |
cwdevice_lp.desc = NULL; |
| 2847 |
} |
| 2848 |
#endif |
| 2849 |
|
| 2850 |
return; |
| 2851 |
} |
| 2852 |
|
| 2853 |
|
| 2854 |
|
| 2855 |
|
| 2856 |
|
| 2857 |
/** |
| 2858 |
\brief Assign correct device type to given device variable |
| 2859 |
|
| 2860 |
A device setup function. The function takes device name (description) |
| 2861 |
\p desc, guesses device type (parport/tty/null), and assigns the guessed |
| 2862 |
device to given \p device. |
| 2863 |
|
| 2864 |
Function assigns to \p device a pointer to global variable, there is |
| 2865 |
no need to free memory associated with the variable itself. |
| 2866 |
The function copies \p desc to 'global device'->desc , you will have to |
| 2867 |
deallocate the copied desc with cwdaemon_cwdevices_free(). |
| 2868 |
|
| 2869 |
The function can be called with \p device to which some other device |
| 2870 |
has been already assigned. |
| 2871 |
|
| 2872 |
\param device - pointer to device variable - guessed device will be |
| 2873 |
|
| 2874 |
\return false if \p desc describes invalid device, or if memory allocation error happens |
| 2875 |
\return true otherwise |
| 2876 |
*/ |
| 2877 |
bool cwdaemon_cwdevice_set(cwdevice **device, const char *desc) |
| 2878 |
{ |
| 2879 |
int fd; |
| 2880 |
|
| 2881 |
if (!desc || !strlen(desc)) { |
| 2882 |
cwdaemon_debug(CWDAEMON_VERBOSITY_E, __func__, __LINE__, |
| 2883 |
"invalid device description \"%s\"", desc); |
| 2884 |
return false; |
| 2885 |
} |
| 2886 |
|
| 2887 |
if ((fd = dev_get_tty(desc)) != -1) { |
| 2888 |
*device = &cwdevice_ttys; |
| 2889 |
} |
| 2890 |
#if defined (HAVE_LINUX_PPDEV_H) || defined (HAVE_DEV_PPBUS_PPI_H) |
| 2891 |
else if ((fd = dev_get_parport(desc)) != -1) { |
| 2892 |
if (geteuid()) { |
| 2893 |
cwdaemon_debug(CWDAEMON_VERBOSITY_E, __func__, __LINE__, |
| 2894 |
"you must run this program as root to use parallel port"); |
| 2895 |
return false; |
| 2896 |
} |
| 2897 |
*device = &cwdevice_lp; |
| 2898 |
} |
| 2899 |
#endif |
| 2900 |
else if ((fd = dev_get_null(desc)) != -1) { |
| 2901 |
*device = &cwdevice_null; |
| 2902 |
} else { |
| 2903 |
cwdaemon_debug(CWDAEMON_VERBOSITY_E, __func__, __LINE__, |
| 2904 |
"no valid device found, setting cwdevice to null device"); |
| 2905 |
/* It's better to have null device than NULL |
| 2906 |
pointer. */ |
| 2907 |
*device = &cwdevice_null; |
| 2908 |
} |
| 2909 |
|
| 2910 |
if (!*device) { |
| 2911 |
cwdaemon_debug(CWDAEMON_VERBOSITY_E, __func__, __LINE__, |
| 2912 |
"invalid keyer device: \"%s\"", desc); |
| 2913 |
return false; |
| 2914 |
} |
| 2915 |
|
| 2916 |
/* Replace default description of device with actual |
| 2917 |
description provided by caller. |
| 2918 |
|
| 2919 |
Notice that this also works for fallback null device: when |
| 2920 |
no valid device has been found, cwdaemon falls back to null |
| 2921 |
device. The name of the *intended* device (e.g. misspelled |
| 2922 |
port name) is being assigned to fallback null device. |
| 2923 |
TODO: is it a valid behaviour? Shouldn't we call the |
| 2924 |
fallback device "null"? */ |
| 2925 |
if ((*device)->desc) { |
| 2926 |
free((*device)->desc); |
| 2927 |
} |
| 2928 |
(*device)->desc = strdup(desc); |
| 2929 |
if (!(*device)->desc) { |
| 2930 |
cwdaemon_debug(CWDAEMON_VERBOSITY_E, __func__, __LINE__, |
| 2931 |
"memory allocation error"); |
| 2932 |
return false; |
| 2933 |
} |
| 2934 |
|
| 2935 |
cwdaemon_debug(CWDAEMON_VERBOSITY_I, __func__, __LINE__, |
| 2936 |
"keying device used: \"%s\"", (*device)->desc); |
| 2937 |
(*device)->init(*device, fd); |
| 2938 |
|
| 2939 |
return true; |
| 2940 |
|
| 2941 |
} |
| 2942 |
|
| 2943 |
|
| 2944 |
|
| 2945 |
|
| 2946 |
/** |
| 2947 |
\brief Assign initial value to global_cwdevice |
| 2948 |
|
| 2949 |
Assign some initial value (initial device) to global_cwdevice, |
| 2950 |
before the device will be configured through command line options. |
| 2951 |
|
| 2952 |
If cwdaemon will be started without any device specified in command |
| 2953 |
line, we could end up with global_cwdevice being NULL pointer. |
| 2954 |
|
| 2955 |
*/ |
| 2956 |
void cwdaemon_cwdevice_init(void) |
| 2957 |
{ |
| 2958 |
global_cwdevice = &cwdevice_null; |
| 2959 |
|
| 2960 |
/* cwdevice_null->desc (and now global_cwdevice->desc) has |
| 2961 |
been set in cwdaemon_cwdevices_init(). */ |
| 2962 |
|
| 2963 |
return; |
| 2964 |
} |
| 2965 |
|
| 2966 |
|
| 2967 |
|
| 2968 |
/** |
| 2969 |
\brief Clean up global cwdevice |
| 2970 |
|
| 2971 |
Global cwdevice has been initialized with |
| 2972 |
cwdaemon_cwdevice_set(). Clean it up before exiting. |
| 2973 |
|
| 2974 |
This function should be registered with atexit(). It is able to |
| 2975 |
handle situation where global cwdevice has not been initialized |
| 2976 |
yet. |
| 2977 |
|
| 2978 |
*/ |
| 2979 |
void cwdaemon_cwdevice_free(void) |
| 2980 |
{ |
| 2981 |
if (global_cwdevice |
| 2982 |
&& global_cwdevice->free) { |
| 2983 |
|
| 2984 |
global_cwdevice->free(global_cwdevice); |
| 2985 |
} |
| 2986 |
|
| 2987 |
return; |
| 2988 |
} |
| 2989 |
|
| 2990 |
|
| 2991 |
|
| 2992 |
/** |
| 2993 |
\brief Initialize network variables |
| 2994 |
|
| 2995 |
Initialize network socket and other network variables. |
| 2996 |
|
| 2997 |
\return false on failure |
| 2998 |
\return true on success |
| 2999 |
*/ |
| 3000 |
bool cwdaemon_initialize_socket(void) |
| 3001 |
{ |
| 3002 |
memset(&request_addr, '\0', sizeof (request_addr)); |
| 3003 |
request_addr.sin_family = AF_INET; |
| 3004 |
request_addr.sin_addr.s_addr = htonl(INADDR_ANY); |
| 3005 |
request_addr.sin_port = htons(port); |
| 3006 |
request_addrlen = sizeof (request_addr); |
| 3007 |
|
| 3008 |
socket_descriptor = socket(AF_INET, SOCK_DGRAM, 0); |
| 3009 |
if (socket_descriptor == -1) { |
| 3010 |
cwdaemon_errmsg("Socket open"); |
| 3011 |
return false; |
| 3012 |
} |
| 3013 |
|
| 3014 |
if (bind(socket_descriptor, |
| 3015 |
(struct sockaddr *) &request_addr, |
| 3016 |
request_addrlen) == -1) { |
| 3017 |
|
| 3018 |
cwdaemon_errmsg("Bind"); |
| 3019 |
return false; |
| 3020 |
} |
| 3021 |
|
| 3022 |
int save_flags = fcntl(socket_descriptor, F_GETFL); |
| 3023 |
if (save_flags == -1) { |
| 3024 |
cwdaemon_errmsg("Trying get flags"); |
| 3025 |
return false; |
| 3026 |
} |
| 3027 |
save_flags |= O_NONBLOCK; |
| 3028 |
|
| 3029 |
if (fcntl(socket_descriptor, F_SETFL, save_flags) == -1) { |
| 3030 |
cwdaemon_errmsg("Trying non-blocking"); |
| 3031 |
return false; |
| 3032 |
} |
| 3033 |
|
| 3034 |
return true; |
| 3035 |
} |
| 3036 |
|
| 3037 |
|
| 3038 |
|
| 3039 |
|
| 3040 |
void cwdaemon_close_socket(void) |
| 3041 |
{ |
| 3042 |
if (socket_descriptor) { |
| 3043 |
if (close(socket_descriptor) == -1) { |
| 3044 |
cwdaemon_errmsg("Close socket"); |
| 3045 |
exit(EXIT_FAILURE); |
| 3046 |
} |
| 3047 |
} |
| 3048 |
|
| 3049 |
return; |
| 3050 |
} |
| 3051 |
|
| 3052 |
|
| 3053 |
/* *** unused code *** */ |
| 3054 |
|
| 3055 |
|
| 3056 |
|
| 3057 |
|
| 3058 |
|
| 3059 |
#if 0 |
| 3060 |
|
| 3061 |
/* some simple timing utilities, see |
| 3062 |
* http://www.erlenstar.demon.co.uk/unix/faq_8.html */ |
| 3063 |
static void |
| 3064 |
timer_add (struct timeval *tv, long secs, long usecs) |
| 3065 |
{ |
| 3066 |
tv->tv_sec += secs; |
| 3067 |
if ((tv->tv_usec += usecs) >= CWDAEMON_USECS_PER_SEC) |
| 3068 |
{ |
| 3069 |
tv->tv_sec += tv->tv_usec / CWDAEMON_USECS_PER_SEC; |
| 3070 |
tv->tv_usec %= CWDAEMON_USECS_PER_SEC; |
| 3071 |
} |
| 3072 |
} |
| 3073 |
|
| 3074 |
/* Set *RES = *A - *B, returning the sign of the result */ |
| 3075 |
static int |
| 3076 |
timer_sub (struct timeval *res, const struct timeval *a, |
| 3077 |
const struct timeval *b) |
| 3078 |
{ |
| 3079 |
long sec = a->tv_sec - b->tv_sec; |
| 3080 |
long usec = a->tv_usec - b->tv_usec; |
| 3081 |
|
| 3082 |
if (usec < 0) |
| 3083 |
usec += 1000000, --sec; |
| 3084 |
|
| 3085 |
res->tv_sec = sec; |
| 3086 |
res->tv_usec = usec; |
| 3087 |
|
| 3088 |
return (sec < 0) ? (-1) : ((sec == 0 && usec == 0) ? 0 : 1); |
| 3089 |
} |
| 3090 |
|
| 3091 |
#endif |