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 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721
|
/* HF amateur radio communicator
*
* digital HF modes (PSK31, RTTY, MT63, ...)
* primary server module
*/
/* servermain:
*
* opens and initializes sound card
*
* initializer encoder and decoder classes (modes/psk31*.C)
*
* starts new thread for encoding and decoding
*
* offers interface functions for the user interface
*
* mutex operations for accesing en-/decoder classes from en/decoding
* thread and from user interface thread (==interface functions)
*/
#include <sys/mman.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <errno.h>
#include <sys/socket.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <sys/un.h>
#include <netinet/in.h>
#include <time.h>
#include <fcntl.h>
#include <sys/soundcard.h>
#include <sys/ioctl.h>
#ifdef USE_PTHREAD
#include <pthread.h>
#else
#define pthread_mutex_init(x,y)
#define pthread_mutex_lock(x)
#define pthread_mutex_unlock(x)
#endif
#include "../modes/psk31-receiver.h"
#include "../modes/psk31-transmitter.h"
#include "../modes/psk31-fft.h"
#include "../modes/psk31-coder.h"
#include "server.h"
#define USE_REALTIME
// receive / transmit state information
int full_duplex;
static char *audio_path="/dev/audio";
static char *ptt_path=NULL;
static int audiofd=-1, pttfd=-1;
static int ptt_invert=0;
// schedule_transmit: 0=no 1=when dcd goes off, 2=immediately
static int schedule_transmit=0, transmit=0;
static int dcdlevel=0;
/* information: mutex usage:
* mutex_rx is used for accessing the psk31_receiver objects as well as
* the psk31_fft object. this lock is set by the GUI if when
* changing configuration parameters.
* mutex_tx is similar for the psk31_transmitter object.
* the buffer mutexes are used for the RX buffers, when copying
* data to the GUI and when transfering data from the
* psk31 objects in the master loop
*/
/* secondary RX decoders: [limit: N_BUFFERS-3]
* a secondary can be started by commControl on an unused channel (3..254)
* with command COMM_MODE and value set to MO_NORMAL
* it can be disabled by COMM_MODE with value MO_DISABLED
* if the channel is disable, no other command than COMM_MODE(MO_NORMAL) is
* accepted.
*/
//static psk31_receiver *psk31rx; --- now in buffer structure!
static psk31_transmitter *psk31tx;
static psk31_fft *psk31fft;
#ifdef USE_PTHREAD
static pthread_t master_thr;
static pthread_mutex_t mutex_rx = PTHREAD_MUTEX_INITIALIZER;
static pthread_mutex_t mutex_tx = PTHREAD_MUTEX_INITIALIZER;
#endif
/* static pthread_mutex_t mutex_fft= PTHREAD_MUTEX_INITIALIZER; */
/* FFT uses RX mutex! Thus psk31rx also can access the fft */
typedef struct {
int len;
int rpos;
int wpos;
char *mem;
psk31_receiver *psk31rx;
#ifdef USE_PTHREAD
pthread_mutex_t mutex;
#endif
} buffer_t;
// Buffer==Channel: 0=TXdata-Echo 1=FFT-data 2,...=RX-data
#define N_BUFFERS 6
static buffer_t buffers[N_BUFFERS];
static void init_buffer() {
int i;
for(i=0; i<N_BUFFERS; i++) {
buffers[i].len = 16384;
buffers[i].rpos = buffers[i].wpos = 0;
buffers[i].mem = (char *)malloc(16384);
buffers[i].psk31rx = NULL;
pthread_mutex_init(&buffers[i].mutex, NULL);
}
}
static int write_buffer(int bufnr, char *data, int cnt) {
int n, error;
buffer_t *b = buffers+bufnr;
if(cnt>b->len) return -1; // too much data!
pthread_mutex_lock( &b->mutex );
if(cnt> ((b->wpos-b->rpos)%b->len)) error=-1; else error=0;
if(b->wpos+cnt > b->len) n=b->len-b->wpos; else n=cnt;
bcopy( data, b->mem+b->wpos, n );
b->wpos += n;
cnt-=n;
if(cnt>0) {
bcopy( data+n, b->mem, cnt);
b->wpos = cnt;
}
pthread_mutex_unlock( &b->mutex);
return error;
}
static int read_buffer(int bufnr, char *data, int cnt) {
int n, buflen;
buffer_t *b = buffers+bufnr;
pthread_mutex_lock( &b->mutex );
buflen = (b->wpos-b->rpos)%b->len;
if(buflen<0) buflen+=b->len;
if(cnt>buflen) cnt=buflen;
if(b->rpos+cnt > b->len) n=b->len-b->rpos; else n=cnt;
bcopy( b->mem+b->rpos, data, n );
b->rpos += n;
cnt-=n;
if( cnt>0 ) {
bcopy( b->mem, data+n, cnt );
b->rpos = cnt;
}
pthread_mutex_unlock( &b->mutex);
return n;
}
#if 0
static void atexitfunc() {
if(audiofd>=0) close(audiofd);
}
#endif
char ibuf[65536];
char *ptr;
static int init_audio() {
int audio,val;
audio=open(audio_path, O_RDWR|O_NONBLOCK);
if(audio<0) return -1;
val=8000;
if( ioctl(audio, SNDCTL_DSP_SPEED, &val)<0 ) return -1;
if(val!=8000) {
fprintf(stderr,"inexact sampling rate: "
"request for %d resulted in %d",8000,val);
}
val=AFMT_S16_LE;
if( ioctl(audio, SNDCTL_DSP_SETFMT, &val)<0 ) return -1;
val=DMA_BUF_BITS;
if( ioctl(audio, SNDCTL_DSP_SETFRAGMENT, &val)<0 ) return -1;
val=1;
if( ioctl(audio, SNDCTL_DSP_NONBLOCK, &val)<0 ) return -1;
val=0;
if( ioctl(audio, SNDCTL_DSP_STEREO, &val)<0 ) return -1;
// Check if the device is operating in full duplex mode
if( ioctl(audio, SNDCTL_DSP_GETCAPS, &val)<0 )
perror("Warning: GETCAPS on audio device failed");
else
if(val&DSP_CAP_DUPLEX) full_duplex=1;
fprintf(stderr,"Using %s mode!\n", full_duplex?"full duplex":"halv duplex");
#if 0
val = 0;
if (ioctl(audio, SNDCTL_DSP_SETTRIGGER, &val) == -1)
perror("ioctl: SNDCTL_DSP_SETTRIGGER");
val = PCM_ENABLE_INPUT;
if (ioctl(audio, SNDCTL_DSP_SETTRIGGER, &val) == -1)
perror("ioctl: SNDCTL_DSP_SETTRIGGER");
#endif
return audio;
}
static int ctl_ptt(int onoff);
static int init_ptt(void) {
pttfd = open(ptt_path, O_RDWR);
if(pttfd>0) {
if(ctl_ptt(0)<0) return -1;
}
return pttfd;
}
static int ctl_ptt(int onoff) {
//fprintf(stderr,"pttctl[%d]\n",onoff);
if(pttfd<0) return 0; // PTT control disabled, nothing to do...
if(ptt_invert) onoff=!onoff;
int arg = TIOCM_RTS|TIOCM_DTR;
if(ioctl(pttfd, onoff?TIOCMBIS:TIOCMBIC, &arg)<0)
return -1;
return 0;
}
#ifdef USE_PTHREAD
void master_handler(void);
static void *master_thr_func(void *dummy) {
while(1) {
master_handler();
}
}
#endif
void master_handler(void) {
int res;
fd_set rset, wset, eset;
struct timeval tm;
int dcd;
buffers[COMM_RXCH].psk31rx->get_info(NULL,NULL,NULL,NULL,
NULL,&dcd,NULL,NULL);
if( (schedule_transmit==1&&dcd==0) || schedule_transmit==2) {
ctl_ptt(1);
psk31tx->send_char(TX_START);
transmit=1;
schedule_transmit=0;
}
FD_ZERO(&rset); FD_ZERO(&wset); FD_ZERO(&eset);
FD_SET(audiofd, &rset);
//FD_SET(audiofd, &eset);
if(transmit) FD_SET(audiofd, &wset);
tm.tv_sec=0; tm.tv_usec=50000; /* 50ms */
res=select(audiofd+1, &rset, &wset, &eset, &tm);
/* In my older version I had the problem, that there exist
* sound drivers which do not support select correctly. So this
* code tries to read/write from/to the audiodevice without
* respecting FD_ISSET. This should work with all drivers */
if( !transmit ) {
short sample;
for(;;) {
res=read(audiofd, &sample, 2);
if(res==0) {
break;
} else if(res!=2) {
//fprintf(stderr,%d %d\n",res,errno);
if(errno==EINTR) break; //continue;
if(errno==EAGAIN||errno==EBUSY) break;
perror("Audio read failed");
exit(1);
}
for(int r=COMM_RXCH; r<N_BUFFERS; r++) {
pthread_mutex_lock(&mutex_rx);
psk31_receiver *rx = buffers[r].psk31rx;
if(rx==NULL) {
pthread_mutex_unlock(&mutex_rx);
continue;
}
res = rx->process_rx_sample(sample);
pthread_mutex_unlock(&mutex_rx);
if(res!=NO_CHAR) {
rx->get_info(NULL,NULL,NULL,NULL,
NULL,&dcd,NULL,NULL);
if (dcd||(dcdlevel==-1)) {
char cd = (char)res;
write_buffer(r, &cd, 1);
}
}
}
}
}
if(transmit) {
char cd;
for(;;) {
if(full_duplex) {
// clear buffer if necessary
char buffer[128];
for(;;) {
res=read(audiofd, buffer, 128);
if(res!=128) break;
}
}
pthread_mutex_lock(&mutex_tx);
res=psk31tx->processor();
pthread_mutex_unlock(&mutex_tx);
// echo-characters, including TX_END,
// are delivered from psk31tx->processor at the
// exact end of the transmission!
if(res==TX_BUSY) {
break;
}
if(res==TX_ERROR) {
perror("tx error:");
break;
}
if(res==TX_END) {
transmit=0;
ctl_ptt(0);
break;
}
cd = (char)(res&0xFF);
write_buffer(0, &cd, 1);
}
}
}
#ifdef USE_PTHREAD
static int run_master_thread() {
pthread_attr_t attr;
fprintf(stderr,"run_master_thread()\n");
#ifdef USEREALTIME
struct sched_param schp;
#endif
if (pthread_attr_init(&attr)) {
perror("Cannot initialize pthread attributes");
exit(4);
}
#ifdef USEREALTIME
memset(&schp, 0, sizeof(schp));
if( pthread_attr_setschedpolicy(&attr, SCHED_RR) ) {
perror("Cannot set realtime scheduling attribute");
exit(5);
}
schp.sched_priority = sched_get_priority_min(SCHED_RR)+1;
if( pthread_attr_setschedparam(&attr, &schp) ) {
perror("Cannot set realtime scheduling priority");
exit(6);
}
#endif
if( pthread_create(&master_thr, &attr, master_thr_func, NULL) ) {
perror("Cannot create IO processing thread");
exit(7);
}
if (pthread_attr_destroy(&attr)) {
perror("Failed to destroy attribute");
}
return 0;
}
#endif
/* audio, ptt: full path to device
* data: directory of coder data file (psk31.cod)
* ptt may be something like "/dev/ttyS0,i" for inverted PTT
*/
int server_main(char *audio, char *ptt, char *datadir)
{
init_buffer();
if(audio) audio_path = strdup(audio);
if(ptt) ptt_path = strdup(ptt);
if(ptt_path!=NULL) {
int len = strlen(ptt_path);
if(len>2 && ptt_path[len-2]==',') {
ptt_invert = (ptt_path[len-1]!='0');
ptt_path[len-2]=0;
} else ptt_invert=0;
}
if( (audiofd=init_audio())<0 ) {
fprintf(stderr, "Cannot open audio device ");
perror(audio_path);
return -1;
}
if( ptt_path && ( (pttfd=init_ptt())<0 ) ) {
fprintf(stderr, "Cannot open PTT device ");
perror(ptt_path);
}
psk31_coder::init_tables(datadir);
psk31fft = new psk31_fft();
psk31fft->set_parameters(1024, 1024, psk31_fft::MODE_RXDATA);
buffers[COMM_RXCH].psk31rx = new psk31_receiver(psk31fft);
psk31tx = new psk31_transmitter();
psk31tx->set_audiofd(audiofd);
#ifdef USE_PTHREAD
if( run_master_thread()<0 )
return -1;
#endif
return 0;
}
int commWaitUpdate(unsigned long timeout)
{
return -1;
}
int commGetData(int channel, char *buffer, int buflen)
{
if(channel<0||channel>=N_BUFFERS) return -1;
if(channel==COMM_FFTCH) {
if(psk31fft->has_new_data()) {
int len;
psk31fft->get_parameters(&len, NULL, NULL);
if(buflen<(int)(len*sizeof(float))) {
fprintf(stderr,"no space in buffer for FFT data\n");
return 0;
}
psk31fft->get_abs_data((float *)buffer, 0, len, 1);
#if 0
for(int i=0; i<1024; i++)
fprintf(stderr,"%d ",((float *)buffer)[i]);
#endif
return len;
}
else return 0;
} else {
return read_buffer(channel, buffer, buflen);
}
}
int commPutData(char *buffer, int buflen)
{
if(buflen<=0) buflen=strlen(buffer);
pthread_mutex_lock(&mutex_tx);
for(int i=0; i<buflen; i++) {
psk31tx->send_char((unsigned char)(buffer[i]));
}
pthread_mutex_unlock(&mutex_tx);
return buflen;
}
static int txControl(int spec, int value)
{
int retval=0;
static int mq=0, ml=0, mcw=0;
pthread_mutex_lock(&mutex_tx);
switch(spec) {
case COMM_PTT:
// if(transmit == (value&PTTON)) break; // nothing to do!
switch(value) {
case PTTON:
// schedule TX start on DCD off
schedule_transmit=1;
break;
case PTTON|PTTFORCE:
// schedule TX start now
schedule_transmit=2;
break;
case PTTOFF:
// schedule TX end!
psk31tx->send_char(TX_END);
break;
case PTTOFF|PTTFORCE:
// shall we just clear the internal buffer and send
// a postamble before aborting? should be the right
// thing to do! TX_END|TX_URGENT does this job!
// However, this does not allow "instant" abort
// for emergency cases when something really goes
// wrong... (TODO: clarify this matter...)
psk31tx->send_char(TX_END|TX_URGENT);
#if 0
transmit=0;
ctl_ptt(0);
#endif
break;
}
break;
case COMM_QPSK:
mq=value;
psk31tx->send_char(TX_MODE|(value?TXM_QPSK:0)|(ml?TXM_LSB:0));
break;
case COMM_LSB:
ml=value;
psk31tx->send_char(TX_MODE|(mq?TXM_QPSK:0)|(value?TXM_LSB:0));
break;
case COMM_MODE:
//fprintf(stderr,"mode: value=%d\n",value);
if(value==MO_TUNE) {
psk31tx->send_char(TX_MODE|TXM_TUNE);
mcw = 2;
}
else if(value==MO_CWSEND) {
psk31tx->send_char(TX_MODE|TXM_CW);
mcw = 1;
}
else {
psk31tx->send_char( TX_MODE | (mq?TXM_QPSK:0)
| (ml?TXM_LSB:0) );
mcw = 0;
}
break;
case COMM_FREQ:
psk31tx->send_char( TX_FREQ|(0xFFFFF&value) );
break;
default:
retval=-1;
}
pthread_mutex_unlock(&mutex_tx);
return retval;
}
static int rxControl(int channel, int spec, int value)
{
int retval=0;
int mqpsk, mlsb, mafc, musedcd, mdcd; float mfreq;
int lastdelta, strength;
psk31_receiver *rx = buffers[channel].psk31rx;
if(rx==NULL) {
if(spec==COMM_MODE && value==MO_NORMAL) {
// start new decoder object
#if 0
fprintf(stderr,"info: starting new decoder object"
"on channel %d\n",channel);
#endif
buffers[channel].psk31rx = new psk31_receiver(NULL);
return 0;
}
fprintf(stderr,"warning: rxControl on disabled RX channel\n"
"channel=%d spec=%d value=%d\n",channel,spec,value);
return -1;
}
pthread_mutex_lock(&mutex_rx);
rx->get_info(&mqpsk, &mlsb, &mfreq, &musedcd, &mafc, &mdcd,
&lastdelta, &strength);
switch(spec) {
case COMM_SWAP:
/* exchange psk31_receiver objects of current channel and
* channel <value> */
// TODO: keep IQ FFT data reference on COMM_RXCH!!
// (currently not used, so not a big issue)
if(value<COMM_RXCH||value>=N_BUFFERS) {
retval=-1; break;
}
{
psk31_receiver *rx2 = buffers[value].psk31rx;
buffers[channel].psk31rx = rx2;
buffers[value].psk31rx = rx;
if(value==COMM_RXCH) {
rx->set_fft(psk31fft); rx2->set_fft(NULL);
}
else if(channel==COMM_RXCH) {
rx2->set_fft(psk31fft); rx->set_fft(NULL);
}
}
break;
case COMM_DCD:
rx->set_dcd(value);
break;
case COMM_DCDLEVEL:
dcdlevel = value;
rx->set_dcdlevel(value);
break;
case COMM_QPSK:
rx->set_mode(value, mlsb);
break;
case COMM_LSB:
rx->set_mode(mqpsk, value);
break;
case COMM_AFC:
rx->set_afc(value);
break;
case COMM_MODE:
if(value==MO_DISABLED) {
if(channel==COMM_RXCH) {
// do not delete master RX channel. won't work
// as base FFT depends on this master channel
retval=-1;
break;
}
#if 0
fprintf(stderr,"info: deleting decoder object"
"on channel %d\n",channel);
#endif
delete rx;
buffers[channel].psk31rx=NULL;
buffers[channel].rpos = buffers[channel].wpos = 0;
} else {
fprintf(stderr,"warning: active channel %d: "
"COMM_MODE %d\n",channel,value);
}
break;
case COMM_FREQ:
rx->set_freq(0.01*value);
break;
default:
retval=-1;
}
pthread_mutex_unlock(&mutex_rx);
return retval;
}
int fftControl(int spec, int value)
{
int len, overlap, mode;
int retval=0;
pthread_mutex_lock(&mutex_rx);
psk31fft->get_parameters(&len, &overlap, &mode);
switch(spec) {
case COMM_FFTN:
len = value;
psk31fft->set_parameters(len, overlap, mode);
break;
case COMM_FFTOVERLAP:
overlap = value;
psk31fft->set_parameters(len, overlap, mode);
break;
default:
retval=-1;
}
pthread_mutex_unlock(&mutex_rx);
return retval;
}
int commControl(int channel, int spec, int value)
{
if(channel==255) {
// transmit channel
return txControl(spec, value);
} else if(channel==0) {
// echo channel
return 0; // no control here!
} else if(channel==1) {
// fft channel
return fftControl(spec, value);
} else if(channel>=COMM_RXCH&&channel<N_BUFFERS) {
// rx data chennal
return rxControl(channel, spec, value);
} else {
return -1; // invalid channel
}
}
int commGetInfo(int channel, void *buffer, int buflen)
{
float freq;
PSK31info *info;
if(channel==COMM_TXCH) {
// transmit channel
if(buflen<(int)sizeof(PSK31info)) return -1;
info = (PSK31info *)buffer;
info->ptt = transmit;
pthread_mutex_lock(&mutex_tx);
psk31tx->get_info(&info->qpsk, &info->lsb, &info->cw, &freq);
info->freq = (int)(100*freq);
pthread_mutex_unlock(&mutex_tx);
} else if(channel==COMM_ECHOCH) {
// echo channel
return -1; // no info on echo channel
} else if(channel==COMM_FFTCH) {
// fft channel
return -1; // no info in fft channel (TODO)
} else if(channel>=COMM_RXCH&&channel<N_BUFFERS) {
// rx channel
int retval=0;
if(buflen<(int)sizeof(PSK31info)) return -1;
info = (PSK31info *)buffer;
info->ptt = transmit;
pthread_mutex_lock(&mutex_rx);
psk31_receiver *rx = buffers[channel].psk31rx;
if(rx==NULL) {
// TODO: different return value for invalid channel
// vs disabled channel
retval=-1;
} else {
rx->get_info(&info->qpsk, &info->lsb, &freq,
&info->dcdlevel, &info->afc, &info->dcd,
&info->phdelta, &info->strength );
info->freq = (int)(100*freq);
}
pthread_mutex_unlock(&mutex_rx);
return retval;
} else {
return -1; // invalid channel
}
return 0; // or what should we return here?
}
|