1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645
|
/*****************************************************************************/
/*
* main.c -- hfkernel main file, parameter parsing and thread
* starting, basic sound functions for hfhernel Linux sound I/O.
*
* Copyright (C) 1996 Thomas Sailer (sailer@ife.ee.ethz.ch)
* Swiss Federal Institute of Technology (ETH), Electronics Lab
* modified by Gnther Montag
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
*
*
*/
/*****************************************************************************/
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif
#include <pthread.h>
#include <stdarg.h>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/mman.h>
#include <sys/stat.h>
#include <errno.h>
#include <signal.h>
#include <syslog.h>
#include <sys/types.h>
#include <endian.h>
#include <sys/soundcard.h>
#include <sys/socket.h>
#include <fcntl.h>
#include <sys/ioctl.h>
#include <sys/time.h>
#include <endian.h>
#include <assert.h>
#ifdef DISABLE_REALTIME
#undef HAVE_SCHED_H
#endif
#ifdef HAVE_SCHED_H
#include <sched.h>
#endif
#include "fskl1.h"
#include "fskutil.h"
#include "msg.h"
#include "main.h"
#include "standby.h"
#include "oss.h"
#include "mt63hflink.h"
#include "cw.h"
#ifdef HAVE_ALSA_ASOUNDLIB_H
#include "alsa.h"
#endif /* HAVE_ALSA_ASOUNDLIB_H */
#ifndef PID_FILE
# define PID_FILE "/var/run/hfkernel.pid"
#endif
/* --------------------------------------------------------------------- */
/* these variables take hfkernel's options */
static int logging = 0;
const char *name_audio = NULL;
const char *name_ptt = NULL;
const char *name_mixer = NULL;
float snd_corr = 1, tv_corr = 1, cpu_mhz = 0;
int dis_rdtsc = 0, invert_ptt = 0;
const char *name_comm = "/var/run/hfapp";
int force_halfduplex = 0;
int nommap = 0;
unsigned int standby_flags =
STANDBY_FLAG_FREQ_ESTIMATION|STANDBY_FLAG_MONITOR_200|STANDBY_FLAG_MONITOR_300;
unsigned int comm_perm = S_IRWXU|S_IRWXG|S_IRWXO;
extern int brake, elbug_rts;
pthread_t thr_l2;
int fd_audio = -1, fd_mixer = -1, fd_ptt = -1;
pthread_t thr_l1;
int samples_remain = 0;
int samples_count = 0;
short *samples_ptr = NULL;
/* --------------------------------------------------------------------- */
/*
* Logging functions
*/
void errprintf(int severity, const char *fmt, ...)
{
va_list args;
va_start(args, fmt);
if (logging) {
char tmp[512];
vsnprintf(tmp, sizeof(tmp), fmt, args);
syslog(severity, tmp);
} else {
fprintf(stderr, "hfkernel[%u]: ", getpid());
vfprintf(stderr, fmt, args);
}
va_end(args);
if (severity <= SEV_FATAL)
exit(1);
}
/* --------------------------------------------------------------------- */
void errstr(int severity, const char *st)
{
errprintf(severity, "error: %s: %s\n", st, strerror(errno));
}
/* --------------------------------------------------------------------- */
void output_ptt(int ptt)
{
int par = TIOCM_RTS;
if (elbug_rts) return;
if (fd_ptt < 0) return;
if (ioctl(fd_ptt,
((ptt && !invert_ptt) || (!ptt && invert_ptt)) ?
TIOCMBIS : TIOCMBIC, &par)) {
errstr(SEV_WARNING, "ioctl: TIOCMBI[CS]");
errstr(SEV_WARNING, "serial port for ptt can not be opened!");
errstr(SEV_FATAL,
"maybe you have to modify serial port in /etc/hf.conf !");
}
}
void kill_daemon()
{
FILE *f;
int pid;
if (!(f = fopen (PID_FILE, "r")))
{
errstr( SEV_FATAL, "Failed to read from PID file");
exit(1);
}
fscanf (f, "%d", &pid);
fclose (f);
kill( SIGKILL, pid );
unlink( PID_FILE );
exit(1);
}
int write_pid()
{
char buf[20];
int fd;
long pid;
if ((fd = open (PID_FILE, O_CREAT | O_TRUNC | O_WRONLY, 0600)) == -1)
{
errstr (SEV_FATAL, "cannot open pidfile for writing ");
exit(1);
}
else
{
pid = getpid ();
snprintf (buf, sizeof (buf), "%ld", (long) pid);
if (write (fd, buf, strlen (buf)) != strlen (buf))
{
errstr (SEV_FATAL, "cannot write to pidfile ");
exit(1);
}
close(fd);
}
return pid;
}
/* --------------------------------------------------------------------- */
/* thought i need it for mt63, but no need now, maybe later */
void l1_switch_to_nommap()
{
#ifndef NOREALTIME
static struct sched_param schp;
#endif /* NOREALTIME */
pthread_attr_t thr_attr;
if (pthread_cancel(thr_l1))
errstr(SEV_FATAL, "pthread_cancel");
if (pthread_join(thr_l1, NULL))
errstr(SEV_FATAL, "pthread_join");
if (pthread_attr_init(&thr_attr))
errstr(SEV_FATAL, "pthread_attr_init");
//printf("l1_swtommap: fd_audio is %d\n", fd_audio);
#ifndef NOREALTIME
memset(&schp, 0, sizeof(schp));
if (pthread_attr_setschedpolicy(&thr_attr, SCHED_RR))
errstr(SEV_FATAL, "pthread_attr_setschedpolicy");
schp.sched_priority = sched_get_priority_min(SCHED_RR)+2;
if (pthread_attr_setschedparam(&thr_attr, &schp))
errstr(SEV_FATAL, "pthread_attr_setschedparam");
#endif /* NOREALTIME */
if (pthread_create(&thr_l1, &thr_attr,
oss_nommap_fdx_driver, (char *)name_audio))
errstr(SEV_FATAL, "pthread_create nommap-driver");
if (pthread_attr_destroy(&thr_attr))
errstr(SEV_WARNING, "pthread_attr_destroy");
printf("switched to nommap sound driver.\n");
//bufprintf(HFAPP_MSG_DATA_STATUS, "switched to nommap sound driver");
return;
}
/* --------------------------------------------------------------------- */
/* thought i need it for mt63, but no need now, maybe later */
void l1_switch_to_mmap()
{
#ifndef NOREALTIME
static struct sched_param schp;
#endif /* NOREALTIME */
pthread_attr_t thr_attr;
//printf("switch back mmap sound driver: before cancel\n");
if (pthread_cancel(thr_l1))
errstr(SEV_FATAL, "pthread_cancel");
//printf("switch back mmap sound driver: before join\n");
if (pthread_join(thr_l1, NULL))
errstr(SEV_FATAL, "pthread_join");
//printf("switch back mmap sound driver: before init\n");
if (pthread_attr_init(&thr_attr))
errstr(SEV_FATAL, "pthread_attr_init");
//printf("l1_switch_to_mmap: fd_audio is %d\n", fd_audio);
#ifndef NOREALTIME
memset(&schp, 0, sizeof(schp));
if (pthread_attr_setschedpolicy(&thr_attr, SCHED_RR))
errstr(SEV_FATAL, "pthread_attr_setschedpolicy");
schp.sched_priority = sched_get_priority_min(SCHED_RR)+2;
if (pthread_attr_setschedparam(&thr_attr, &schp))
errstr(SEV_FATAL, "pthread_attr_setschedparam");
#endif /* NOREALTIME */
//printf("switch back mmap sound driver: before create\n");
if (pthread_create(&thr_l1, &thr_attr,
oss_fdx_driver, (char *)name_audio))
errstr(SEV_FATAL, "pthread_create mmap-driver");
if (pthread_attr_destroy(&thr_attr))
errstr(SEV_WARNING, "pthread_attr_destroy");
printf("switched back to Tom's good old mmap driver.\n");
//bufprintf(HFAPP_MSG_DATA_STATUS,
// "switched back to Tom's good old mmap driver");
return;
}
/* --------------------------------------------------------------------- */
void l1_init()
{
static struct sched_param schp;
pthread_attr_t thr_attr;
int OSS = 0, ALSA = 0, fdx = 0;
if (!name_audio) {
name_audio = "/dev/dsp";
printf("hfkernel: l1_init: will try OSS driver for %s\n",
name_audio);
OSS = 1;
} else if (strstr (name_audio, "dev/")) {
printf("hfkernel: l1_init: will try OSS driver for %s\n",
name_audio);
OSS = 1;
} else if (strstr (name_audio, "hw:")) {
#ifndef HAVE_ALSA_ASOUNDLIB_H
errprintf(SEV_FATAL, "hfkernel: l1_init: Soundcard name \n"
"contains 'hw:...', which is ALSA convention, \n"
"but program is not compiled with ALSA driver.\n");
exit (1);
#endif /* HAVE_ALSA_ASOUNDLIB_H */
printf("hfkernel: l1_init: will try ALSA driver for '...hw:...'\n");
ALSA = 1;
} else {
errprintf(SEV_FATAL, "hfkernel: l1_init: Soundcard name \n"
"does not contain 'dev/...' (OSS) and not 'hw:...'(ALSA) -> \n"
"please check soundcard option in /etc/hf.conf\n"
"because I want to know what driver to use....\n");
exit (1);
}
if (OSS) {
fdx = probe_oss(name_audio);
}
l1_fsk_modem_init();
if (name_ptt) {
if ((fd_ptt = open(name_ptt, O_RDWR, 0)) < 0)
errstr(SEV_WARNING, "open ptt device");
} else
fd_ptt = -1;
output_ptt(0);
if (OSS) {
errprintf(SEV_INFO, "preparing mixer ...\n");
oss_prepare_mixer(name_mixer);
}
/*
* start the l1 thread
*/
memset(&schp, 0, sizeof(schp));
if (pthread_attr_init(&thr_attr))
errstr(SEV_FATAL, "pthread_attr_init");
if (pthread_attr_setschedpolicy(&thr_attr, SCHED_RR))
errstr(SEV_FATAL, "pthread_attr_setschedpolicy");
schp.sched_priority = sched_get_priority_min(SCHED_RR)+2;
if (pthread_attr_setschedparam(&thr_attr, &schp))
errstr(SEV_FATAL, "pthread_attr_setschedparam");
if (OSS) {
if (fdx) {
errprintf(SEV_INFO,
"creating thread for oss full duplex driver ...\n");
if (nommap) {
if (pthread_create(&thr_l1, &thr_attr,
oss_nommap_fdx_driver, (char *)name_audio))
errstr(SEV_FATAL, "pthread_create");
if (pthread_attr_destroy(&thr_attr))
errstr(SEV_WARNING, "pthread_attr_destroy");
return;
} else {
if (pthread_create(&thr_l1, &thr_attr,
oss_fdx_driver, (char *)name_audio))
errstr(SEV_FATAL, "pthread_create");
if (pthread_attr_destroy(&thr_attr))
errstr(SEV_WARNING, "pthread_attr_destroy");
return;
}
}
}
#ifdef HAVE_ALSA_ASOUNDLIB_H
if (ALSA) {
errprintf(SEV_INFO, "creating thread for ALSA driver ...\n");
if (pthread_create(&thr_l1, &thr_attr, alsa_fdx_driver,
(char *)name_audio))
errstr(SEV_FATAL, "pthread_create");
if (pthread_attr_destroy(&thr_attr))
errstr(SEV_WARNING, "pthread_attr_destroy");
return;
}
#endif /* HAVE_ALSA_ASOUNDLIB_H */
refclock_probe(tv_corr, cpu_mhz, dis_rdtsc);
if (OSS) {
errprintf(SEV_INFO,
"creating thread for oss half duplex driver ...\n");
if (nommap) {
if (pthread_create(&thr_l1, &thr_attr, oss_nommap_hdx_driver,
(char *)name_audio))
errstr(SEV_FATAL, "pthread_create");
if (pthread_attr_destroy(&thr_attr))
errstr(SEV_WARNING, "pthread_attr_destroy");
return;
} else {
if (pthread_create(&thr_l1, &thr_attr, oss_hdx_driver,
(char *)name_audio))
errstr(SEV_FATAL, "pthread_create");
if (pthread_attr_destroy(&thr_attr))
errstr(SEV_WARNING, "pthread_attr_destroy");
return;
}
}
}
/* --------------------------------------------------------------------- */
void l1_input_samples(l1_time_t tstart, l1_time_t tinc,
short *samples, unsigned int nsamples)
{
if (modefamily == MT63) {
l1_mt63_input_samples(samples, nsamples);
return;
}
else if (modefamily == FSK) {
l1_fsk_input_samples(tstart, tinc, samples, nsamples);
return;
}
else
errprintf(SEV_WARNING, "hfkernel: no rx mode-family specified.\n");
return;
//exit (1);
}
/* --------------------------------------------------------------------- */
int l1_output_samples(l1_time_t tstart, l1_time_t tinc,
short *samples, unsigned int nsamples)
{
int retval;
if (modefamily == MT63) {
retval = l1_mt63_output_samples(tstart, tinc, samples, nsamples);
return retval;
}
else if (modefamily == FSK) {
retval = l1_fsk_output_samples(tstart, tinc, samples, nsamples);
return retval;
}
else
errprintf(SEV_WARNING, "hfkernel: no tx mode-family specified.\n");
return 0;
//exit (1);
}
/* --------------------------------------------------------------------- */
void l1_start_sample(short *data, unsigned int len)
{
if (!data || !len)
return;
samples_ptr = data;
samples_count = len;
samples_remain = len;
}
/* --------------------------------------------------------------------- */
int l1_sample_finished(void)
{
if (samples_remain == 0 && samples_count > 0) {
samples_count = 0;
return 1;
}
return 0;
}
/* --------------------------------------------------------------------- */
static void start_threads(void)
{
#ifdef HAVE_SCHED_H
struct sched_param schp;
#endif
pthread_attr_t thr_attr;
pthread_t thr_io;
signal(SIGPIPE, SIG_IGN);
if (pthread_attr_init(&thr_attr))
errstr(SEV_FATAL, "pthread_attr_init");
#ifdef HAVE_SCHED_H
memset(&schp, 0, sizeof(schp));
if (pthread_attr_setschedpolicy(&thr_attr, SCHED_RR))
errstr(SEV_WARNING, "pthread_attr_setschedpolicy");
schp.sched_priority = sched_get_priority_min(SCHED_RR)+1;
if (pthread_attr_setschedparam(&thr_attr, &schp))
errstr(SEV_WARNING, "pthread_attr_setschedparam");
#endif
/* here a primitive test for implementing new modes... */
// if (pthread_create(&thr_l2, &thr_attr, mode_cw_tx, NULL))
if (pthread_create(&thr_l2, &thr_attr, mode_standby, NULL))
errstr(SEV_FATAL, "pthread_create");
#ifdef HAVE_SCHED_H
schp.sched_priority = sched_get_priority_min(SCHED_RR);
if (pthread_attr_setschedparam(&thr_attr, &schp))
errstr(SEV_WARNING, "pthread_attr_setschedparam");
#endif
if (pthread_create(&thr_io, &thr_attr, io_process, NULL))
errstr(SEV_FATAL, "pthread_create");
if (pthread_attr_destroy(&thr_attr))
errstr(SEV_WARNING, "pthread_attr_destroy");
if (pthread_join(thr_io, NULL))
errstr(SEV_WARNING, "pthread_join");
if (munlockall())
errstr(SEV_WARNING, "mlockall");
}
/* --------------------------------------------------------------------- */
int main(int argc, char *argv[])
{
int c;
int err = 0;
float pre_cpumhz = 0;
float pre_snd_corr = 1.000;
float pre_tvcorr = 1.000;
while ((c = getopt(argc, argv, "a:M:c:klhip:m:nt:s:r:Rf23")) != -1)
switch (c) {
case 'a':
name_audio = optarg;
break;
case 'M':
name_mixer = optarg;
break;
case 'c':
if (optarg != NULL) name_comm = optarg;
break;
case 'p':
name_ptt = optarg;
break;
case 'h':
force_halfduplex = 1;
break;
case 'i':
invert_ptt = 1;
break;
case 'k':
kill_daemon();
break;
case 'l':
logging = 1;
break;
case 'm':
pre_cpumhz = strtod(optarg, NULL);
if (pre_cpumhz < 10 || pre_cpumhz > 16000) {
fprintf(stderr,
"CPU clock out of range 10MHz..16000MHz\n");
err++;
}
cpu_mhz = pre_cpumhz;
break;
case 'n':
nommap = 1;
break;
case 's':
pre_snd_corr = strtod(optarg, NULL);
if (pre_snd_corr < 0.9 || pre_snd_corr > 1.1) {
fprintf (stderr,
"Soundcard clock adjust factor outside "
"of range 0.9 .. 1.1\n");
err++;
}
snd_corr = pre_snd_corr;
// errprintf(SEV_INFO,
// "sound clock correction: %f\n", pre_snd_corr);
break;
case 't':
pre_tvcorr = strtod(optarg, NULL);
if (pre_tvcorr < 0.9 || pre_tvcorr > 1.1) {
fprintf (stderr,
"gettimeofday clock adjust factor %f"
"outside of range 0.9 .. 1.1\n", pre_tvcorr);
err++;
}
// errprintf(SEV_INFO,
// "gettimeofday correction: %f\n", tvcorr);
tv_corr = pre_tvcorr;
break;
case 'r':
comm_perm = strtoul(optarg, NULL, 0);
break;
case 'R':
dis_rdtsc = 1;
break;
case 'f':
standby_flags &= ~STANDBY_FLAG_FREQ_ESTIMATION;
errprintf(SEV_INFO,
"standby: will not use frequency estimation.\n");
break;
case '2':
standby_flags &= ~STANDBY_FLAG_MONITOR_200;
errprintf(SEV_INFO,
"standby: will not monitor 200 baud.\n");
break;
case '3':
standby_flags &= ~STANDBY_FLAG_MONITOR_300;
errprintf(SEV_INFO,
"standby: will not monitor 300 baud.\n");
break;
default:
err++;
break;
}
if (err) {
fprintf(stderr, "usage: hfkernel [-2] [-3] [-a <audio device>] [-c <comm socket>] [-f] [-h]\n"
" [-i] [-k] [-l] [-M <mixer device>] [-m <cpu clock MHz>] \n"
" [-n] [-p <ptt comm port>] [-R] [-r <socket perms] \n"
" [-s <soundcard clock correction>] [-t <gettimeofday correction>]\n\n"
" -2: standby: disable monitoring of 200baud signals\n"
" -3: standby: disable monitoring of 300baud signals\n"
" -a: audio device path (default for OSS: /dev/dsp)\n"
" (for ALSA driver use -a plughw:0,0)\n"
" -c: path of the communication socket (default: hfapp)\n"
" -f: standby: disable frequency estimation\n"
" -h: force half duplex mode (for OSS only!)\n"
" -i: invert PTT (default: PTT = positive signal)\n"
" -k: stop hfkernel (this is also used by the start script hf)\n"
" -l: logging (default: off)\n"
" -M: mixer device path (default: none)\n"
" -m: CPU clock in MHz (exactly at kHz level)\n"
" -n: no mmap() (which some cards/drivers can't) (for OSS only!)\n"
" -p: path of the serial port to output PTT (default: none)\n"
" -R: disable the use of the rdtsc instruction (Intel systems only)\n"
" -r: access permissions of the communication socket (default: 0777 = rwxrwxrwx)\n"
" -s: soundcard sampling rate correction\n"
" -t: gettimeofday correction factor\n\n");
exit(1);
}
if (logging)
openlog("hfkernel", LOG_PID, LOG_DAEMON);
errprintf(SEV_INFO, "hfkernel %s starting...\n", VERSION);
#ifndef DISABLE_REALTIME
if (mlockall(MCL_CURRENT))
errstr(SEV_WARNING, "mlockall");
#endif
l1_init();
errno = EAGAIN;
write_pid();
start_threads();
exit(0);
}
|