2016-03-01 17:40:38 +00:00
|
|
|
/* Common part for main.c of each base station type
|
|
|
|
*
|
|
|
|
* (C) 2016 by Andreas Eversberg <jolly@eversberg.eu>
|
|
|
|
* All Rights Reserved
|
|
|
|
*
|
|
|
|
* 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 3 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, see <http://www.gnu.org/licenses/>.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdint.h>
|
|
|
|
#include <getopt.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <signal.h>
|
2016-11-13 05:37:56 +00:00
|
|
|
#include <sched.h>
|
2016-06-17 05:28:45 +00:00
|
|
|
#include <unistd.h>
|
2016-05-06 05:00:27 +00:00
|
|
|
#include <math.h>
|
2016-06-17 05:28:45 +00:00
|
|
|
#include <termios.h>
|
2017-02-18 12:51:26 +00:00
|
|
|
#include <errno.h>
|
2017-01-27 15:57:34 +00:00
|
|
|
#include "sample.h"
|
2016-03-01 17:40:38 +00:00
|
|
|
#include "main.h"
|
|
|
|
#include "debug.h"
|
2016-06-17 05:28:45 +00:00
|
|
|
#include "sender.h"
|
|
|
|
#include "timer.h"
|
|
|
|
#include "call.h"
|
2017-01-13 06:28:31 +00:00
|
|
|
#include "mncc_sock.h"
|
2017-01-04 13:21:49 +00:00
|
|
|
#ifdef HAVE_SDR
|
|
|
|
#include "sdr.h"
|
|
|
|
#endif
|
2016-03-01 17:40:38 +00:00
|
|
|
|
|
|
|
/* common settings */
|
2016-04-25 18:20:54 +00:00
|
|
|
int num_kanal = 0;
|
|
|
|
int kanal[MAX_SENDER];
|
2017-01-04 13:14:02 +00:00
|
|
|
int num_audiodev = 0;
|
|
|
|
const char *audiodev[MAX_SENDER] = { "hw:0,0" };
|
|
|
|
const char *call_audiodev = "";
|
2016-03-01 17:40:38 +00:00
|
|
|
int samplerate = 48000;
|
2017-01-13 12:38:57 +00:00
|
|
|
int call_samplerate = 48000;
|
2016-06-18 17:33:08 +00:00
|
|
|
int interval = 1;
|
2016-03-01 17:40:38 +00:00
|
|
|
int latency = 50;
|
2017-01-02 09:16:49 +00:00
|
|
|
int uses_emphasis = 1;
|
2016-05-06 05:00:27 +00:00
|
|
|
int do_pre_emphasis = 0;
|
|
|
|
int do_de_emphasis = 0;
|
|
|
|
double rx_gain = 1.0;
|
2016-03-01 17:40:38 +00:00
|
|
|
int use_mncc_sock = 0;
|
|
|
|
int send_patterns = 1;
|
2016-07-20 10:50:22 +00:00
|
|
|
int release_on_disconnect = 1;
|
2016-03-01 17:40:38 +00:00
|
|
|
int loopback = 0;
|
|
|
|
int rt_prio = 0;
|
2016-11-27 05:47:06 +00:00
|
|
|
const char *write_rx_wave = NULL;
|
|
|
|
const char *write_tx_wave = NULL;
|
|
|
|
const char *read_rx_wave = NULL;
|
2017-01-04 13:21:49 +00:00
|
|
|
static const char *sdr_args = "";
|
2017-02-18 12:51:26 +00:00
|
|
|
static int sdr_uhd = 0;
|
|
|
|
static int sdr_soapy = 0;
|
2017-01-04 13:21:49 +00:00
|
|
|
double sdr_rx_gain = 0, sdr_tx_gain = 0;
|
2017-01-13 06:31:15 +00:00
|
|
|
const char *write_iq_rx_wave = NULL;
|
|
|
|
const char *write_iq_tx_wave = NULL;
|
|
|
|
const char *read_iq_rx_wave = NULL;
|
2016-03-11 05:59:05 +00:00
|
|
|
void print_help_common(const char *arg0, const char *ext_usage)
|
2016-03-01 17:40:38 +00:00
|
|
|
{
|
2016-03-11 05:59:05 +00:00
|
|
|
printf("Usage: %s -k <kanal/channel> %s[options] [station-id]\n", arg0, ext_usage);
|
2017-01-29 07:07:07 +00:00
|
|
|
printf("\nGlobal options:\n");
|
2016-03-01 17:40:38 +00:00
|
|
|
/* - - */
|
|
|
|
printf(" -h --help\n");
|
|
|
|
printf(" This help\n");
|
2016-11-27 05:47:06 +00:00
|
|
|
printf(" -v --verbose <level> | <level>,<category>[,<category>[,...]] | list\n");
|
2016-06-12 08:50:55 +00:00
|
|
|
printf(" Use 'list' to get a list of all levels and categories\n");
|
2016-11-27 05:47:06 +00:00
|
|
|
printf(" Verbose level: digit of debug level (default = '%d')\n", debuglevel);
|
|
|
|
printf(" Verbose level+category: level digit followed by one or more categories\n");
|
2016-06-12 08:50:55 +00:00
|
|
|
printf(" -> If no category is specified, all categories are selected\n");
|
2016-03-01 17:40:38 +00:00
|
|
|
printf(" -k --kanal <channel>\n");
|
2016-11-27 05:47:06 +00:00
|
|
|
printf(" -k --channel <channel>\n");
|
|
|
|
printf(" Channel (German = Kanal) number of \"Sender\" (German = Transceiver)\n");
|
2017-01-04 13:21:49 +00:00
|
|
|
printf(" -a --audio-device hw:<card>,<device> | sdr\n");
|
|
|
|
printf(" Sound card and device number (default = '%s')\n", audiodev[0]);
|
|
|
|
printf(" SDR device, if supported\n");
|
2016-03-01 17:40:38 +00:00
|
|
|
printf(" -s --samplerate <rate>\n");
|
|
|
|
printf(" Sample rate of sound device (default = '%d')\n", samplerate);
|
2016-06-18 17:33:08 +00:00
|
|
|
printf(" -i --interval 1..25\n");
|
|
|
|
printf(" Interval of processing loop in ms (default = '%d' ms)\n", interval);
|
|
|
|
printf(" Use 25 to drastically reduce CPU usage. In case of buffer underrun,\n");
|
|
|
|
printf(" increase latency accordingly.\n");
|
2016-11-27 05:47:06 +00:00
|
|
|
printf(" -b --buffer <ms>\n");
|
|
|
|
printf(" How many milliseconds are processed in advance (default = '%d')\n", latency);
|
2017-01-02 09:16:49 +00:00
|
|
|
if (uses_emphasis) {
|
2016-11-27 05:47:06 +00:00
|
|
|
printf(" -p --pre-emphasis\n");
|
2016-05-06 05:00:27 +00:00
|
|
|
printf(" Enable pre-emphasis, if you directly connect to the oscillator of the\n");
|
|
|
|
printf(" transmitter. (No pre-emphasis done by the transmitter.)\n");
|
2016-11-27 05:47:06 +00:00
|
|
|
printf(" -d --de-emphasis\n");
|
2016-05-06 05:00:27 +00:00
|
|
|
printf(" Enable de-emphasis, if you directly connect to the discriminator of\n");
|
|
|
|
printf(" the receiver. (No de-emphasis done by the receiver.)\n");
|
2017-01-02 09:16:49 +00:00
|
|
|
}
|
2016-11-27 05:47:06 +00:00
|
|
|
printf(" -g --rx-gain <dB>\n");
|
2016-05-06 05:00:27 +00:00
|
|
|
printf(" Raise receiver RX level by given gain in dB. This is useful if input\n");
|
|
|
|
printf(" level of the sound device is too low, even after setting maximum level\n");
|
2017-01-29 07:16:10 +00:00
|
|
|
printf(" with the mixer settings. (Works with sound card only.)\n");
|
2016-03-01 17:40:38 +00:00
|
|
|
printf(" -m --mncc-sock\n");
|
|
|
|
printf(" Disable built-in call contol and offer socket (to LCR)\n");
|
|
|
|
printf(" -c --call-device hw:<card>,<device>\n");
|
2017-01-04 13:14:02 +00:00
|
|
|
printf(" Sound card and device number for headset (default = '%s')\n", call_audiodev);
|
2017-01-13 12:38:57 +00:00
|
|
|
printf(" --call-samplerate <rate>\n");
|
|
|
|
printf(" Sample rate of sound device for headset (default = '%d')\n", call_samplerate);
|
2016-11-27 05:47:06 +00:00
|
|
|
printf(" -t --tones 0 | 1\n");
|
2016-07-20 10:50:22 +00:00
|
|
|
printf(" Connect call on setup/release to provide classic tones towards fixed\n");
|
|
|
|
printf(" network (default = '%d')\n", send_patterns);
|
2016-11-27 05:47:06 +00:00
|
|
|
printf(" -l --loopback <type>\n");
|
2016-03-01 17:40:38 +00:00
|
|
|
printf(" Loopback test: 1 = internal | 2 = external | 3 = echo\n");
|
|
|
|
printf(" -r --realtime <prio>\n");
|
|
|
|
printf(" Set prio: 0 to diable, 99 for maximum (default = %d)\n", rt_prio);
|
2016-11-27 05:47:06 +00:00
|
|
|
printf(" --write-rx-wave <file>\n");
|
2017-01-13 06:31:15 +00:00
|
|
|
printf(" Write received audio to given wave file.\n");
|
2016-11-27 05:47:06 +00:00
|
|
|
printf(" --write-tx-wave <file>\n");
|
2017-01-13 06:31:15 +00:00
|
|
|
printf(" Write transmitted audio to given wave file.\n");
|
2016-11-27 05:47:06 +00:00
|
|
|
printf(" --read-rx-wave <file>\n");
|
2017-01-13 06:31:15 +00:00
|
|
|
printf(" Replace received audio by given wave file.\n");
|
2017-01-04 13:21:49 +00:00
|
|
|
#ifdef HAVE_SDR
|
2017-01-13 06:31:15 +00:00
|
|
|
printf("\nSDR options:\n");
|
2017-02-18 12:51:26 +00:00
|
|
|
#ifdef HAVE_UHD
|
|
|
|
printf(" --sdr-uhd\n");
|
|
|
|
printf(" Force UHD driver\n");
|
|
|
|
#endif
|
|
|
|
#ifdef HAVE_SOAPY
|
|
|
|
printf(" --sdr-soapy\n");
|
|
|
|
printf(" Force SoapySDR driver\n");
|
|
|
|
#endif
|
2017-01-04 13:21:49 +00:00
|
|
|
printf(" --sdr-args <args>\n");
|
2017-02-18 12:51:26 +00:00
|
|
|
printf(" Optional SDR device arguments, seperated by comma\n");
|
|
|
|
printf(" e.g. --sdr-args <key>=<value>[,<key>=<value>[,...]]\n");
|
2017-01-04 13:21:49 +00:00
|
|
|
printf(" --sdr-rx-gain <gain>\n");
|
|
|
|
printf(" SDR device's RX gain in dB (default = %.1f)\n", sdr_rx_gain);
|
|
|
|
printf(" --sdr-tx-gain <gain>\n");
|
|
|
|
printf(" SDR device's TX gain in dB (default = %.1f)\n", sdr_tx_gain);
|
2017-01-13 06:31:15 +00:00
|
|
|
printf(" --write-iq-rx-wave <file>\n");
|
|
|
|
printf(" Write received IQ data to given wave file.\n");
|
|
|
|
printf(" --write-iq-tx-wave <file>\n");
|
|
|
|
printf(" Write transmitted IQ data to given wave file.\n");
|
|
|
|
printf(" --read-iq-rx-wave <file>\n");
|
|
|
|
printf(" Replace received IQ data by given wave file.\n");
|
2017-01-04 13:21:49 +00:00
|
|
|
#endif
|
2017-01-29 07:07:07 +00:00
|
|
|
printf("\nNetwork specific options:\n");
|
2016-03-01 17:40:38 +00:00
|
|
|
}
|
|
|
|
|
2016-12-09 15:42:38 +00:00
|
|
|
void print_hotkeys_common(void)
|
|
|
|
{
|
|
|
|
printf("\n");
|
|
|
|
printf("Press digits '0'..'9' and then 'd' key to dial towards mobile station\n");
|
|
|
|
printf("Press 'h' key to hangup.\n");
|
|
|
|
printf("Press 'w' key to toggle display of wave form of RX signal.\n");
|
|
|
|
}
|
|
|
|
|
2016-11-27 05:47:06 +00:00
|
|
|
#define OPT_CHANNEL 1000
|
|
|
|
#define OPT_WRITE_RX_WAVE 1001
|
|
|
|
#define OPT_WRITE_TX_WAVE 1002
|
|
|
|
#define OPT_READ_RX_WAVE 1003
|
2017-01-13 12:38:57 +00:00
|
|
|
#define OPT_CALL_SAMPLERATE 1004
|
2017-01-13 06:31:15 +00:00
|
|
|
|
2017-02-18 12:51:26 +00:00
|
|
|
#define OPT_SDR_UHD 1100
|
|
|
|
#define OPT_SDR_SOAPY 1101
|
|
|
|
#define OPT_SDR_ARGS 1102
|
|
|
|
#define OPT_SDR_RX_GAIN 1103
|
|
|
|
#define OPT_SDR_TX_GAIN 1104
|
|
|
|
#define OPT_WRITE_IQ_RX_WAVE 1105
|
|
|
|
#define OPT_WRITE_IQ_TX_WAVE 1106
|
|
|
|
#define OPT_READ_IQ_RX_WAVE 1107
|
2016-11-27 05:47:06 +00:00
|
|
|
|
2016-03-01 17:40:38 +00:00
|
|
|
static struct option long_options_common[] = {
|
|
|
|
{"help", 0, 0, 'h'},
|
2016-11-27 05:47:06 +00:00
|
|
|
{"debug", 1, 0, 'v'},
|
2016-03-01 17:40:38 +00:00
|
|
|
{"kanal", 1, 0, 'k'},
|
2016-11-27 05:47:06 +00:00
|
|
|
{"channel", 1, 0, OPT_CHANNEL},
|
|
|
|
{"call-device", 1, 0, 'a'},
|
2016-03-01 17:40:38 +00:00
|
|
|
{"samplerate", 1, 0, 's'},
|
2016-06-18 17:33:08 +00:00
|
|
|
{"interval", 1, 0, 'i'},
|
2016-11-27 05:47:06 +00:00
|
|
|
{"buffer", 1, 0, 'b'},
|
|
|
|
{"pre-emphasis", 0, 0, 'p'},
|
|
|
|
{"de-emphasis", 0, 0, 'd'},
|
2017-02-18 12:51:26 +00:00
|
|
|
{"rx-gain", 1, 0, 'g'},
|
2016-03-01 17:40:38 +00:00
|
|
|
{"mncc-sock", 0, 0, 'm'},
|
2016-11-27 05:47:06 +00:00
|
|
|
{"call-device", 1, 0, 'c'},
|
2017-01-13 12:38:57 +00:00
|
|
|
{"call-samplerate", 1, 0, OPT_CALL_SAMPLERATE},
|
2016-11-27 05:47:06 +00:00
|
|
|
{"tones", 0, 0, 't'},
|
|
|
|
{"loopback", 1, 0, 'l'},
|
2016-03-01 17:40:38 +00:00
|
|
|
{"realtime", 1, 0, 'r'},
|
2016-11-27 05:47:06 +00:00
|
|
|
{"write-rx-wave", 1, 0, OPT_WRITE_RX_WAVE},
|
|
|
|
{"write-tx-wave", 1, 0, OPT_WRITE_TX_WAVE},
|
|
|
|
{"read-rx-wave", 1, 0, OPT_READ_RX_WAVE},
|
2017-02-18 12:51:26 +00:00
|
|
|
{"sdr-uhd", 0, 0, OPT_SDR_UHD},
|
|
|
|
{"sdr-soapy", 0, 0, OPT_SDR_SOAPY},
|
2017-01-04 13:21:49 +00:00
|
|
|
{"sdr-args", 1, 0, OPT_SDR_ARGS},
|
|
|
|
{"sdr-rx-gain", 1, 0, OPT_SDR_RX_GAIN},
|
|
|
|
{"sdr-tx-gain", 1, 0, OPT_SDR_TX_GAIN},
|
2017-01-13 06:31:15 +00:00
|
|
|
{"write-iq-rx-wave", 1, 0, OPT_WRITE_IQ_RX_WAVE},
|
|
|
|
{"write-iq-tx-wave", 1, 0, OPT_WRITE_IQ_TX_WAVE},
|
|
|
|
{"read-iq-rx-wave", 1, 0, OPT_READ_IQ_RX_WAVE},
|
2016-03-01 17:40:38 +00:00
|
|
|
{0, 0, 0, 0}
|
|
|
|
};
|
|
|
|
|
2017-01-03 11:31:59 +00:00
|
|
|
const char *optstring_common = "hv:k:a:s:i:b:pdg:mc:t:l:r:";
|
2016-03-01 17:40:38 +00:00
|
|
|
|
|
|
|
struct option *long_options;
|
|
|
|
char *optstring;
|
|
|
|
|
2016-11-27 05:47:06 +00:00
|
|
|
static void check_duplicate_option(int num, struct option *option)
|
|
|
|
{
|
|
|
|
int i;
|
|
|
|
|
|
|
|
for (i = 0; i < num; i++) {
|
|
|
|
if (long_options[i].val == option->val) {
|
|
|
|
fprintf(stderr, "Duplicate option %d. Please fix!\n", option->val);
|
|
|
|
abort();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-03-01 17:40:38 +00:00
|
|
|
void set_options_common(const char *optstring_special, struct option *long_options_special)
|
|
|
|
{
|
|
|
|
int i;
|
|
|
|
|
|
|
|
long_options = calloc(sizeof(*long_options), 100);
|
2016-11-27 05:47:06 +00:00
|
|
|
for (i = 0; long_options_common[i].name; i++) {
|
|
|
|
check_duplicate_option(i, &long_options_common[i]);
|
2016-03-01 17:40:38 +00:00
|
|
|
memcpy(&long_options[i], &long_options_common[i], sizeof(*long_options));
|
2016-11-27 05:47:06 +00:00
|
|
|
}
|
|
|
|
for (; long_options_special->name; i++) {
|
|
|
|
check_duplicate_option(i, long_options_special);
|
2016-03-01 17:40:38 +00:00
|
|
|
memcpy(&long_options[i], long_options_special++, sizeof(*long_options));
|
2016-11-27 05:47:06 +00:00
|
|
|
}
|
2016-03-01 17:40:38 +00:00
|
|
|
|
|
|
|
optstring = calloc(strlen(optstring_common) + strlen(optstring_special) + 1, 1);
|
|
|
|
strcpy(optstring, optstring_common);
|
|
|
|
strcat(optstring, optstring_special);
|
|
|
|
}
|
|
|
|
|
|
|
|
void opt_switch_common(int c, char *arg0, int *skip_args)
|
|
|
|
{
|
2016-05-06 05:00:27 +00:00
|
|
|
double gain_db;
|
|
|
|
|
2016-03-01 17:40:38 +00:00
|
|
|
switch (c) {
|
|
|
|
case 'h':
|
|
|
|
print_help(arg0);
|
|
|
|
exit(0);
|
2016-11-27 05:47:06 +00:00
|
|
|
case 'v':
|
2016-06-12 08:50:55 +00:00
|
|
|
if (!strcasecmp(optarg, "list")) {
|
|
|
|
debug_list_cat();
|
|
|
|
exit(0);
|
|
|
|
}
|
|
|
|
if (parse_debug_opt(optarg)) {
|
|
|
|
fprintf(stderr, "Failed to parse debug option, please use -h for help.\n");
|
|
|
|
exit(0);
|
|
|
|
}
|
2016-03-01 17:40:38 +00:00
|
|
|
*skip_args += 2;
|
|
|
|
break;
|
|
|
|
case 'k':
|
2016-11-27 05:47:06 +00:00
|
|
|
case OPT_CHANNEL:
|
2016-04-25 18:20:54 +00:00
|
|
|
OPT_ARRAY(num_kanal, kanal, atoi(optarg))
|
2016-03-01 17:40:38 +00:00
|
|
|
*skip_args += 2;
|
|
|
|
break;
|
2016-11-27 05:47:06 +00:00
|
|
|
case 'a':
|
2017-01-04 13:14:02 +00:00
|
|
|
OPT_ARRAY(num_audiodev, audiodev, strdup(optarg))
|
2016-03-01 17:40:38 +00:00
|
|
|
*skip_args += 2;
|
|
|
|
break;
|
|
|
|
case 's':
|
|
|
|
samplerate = atoi(optarg);
|
|
|
|
*skip_args += 2;
|
|
|
|
break;
|
2016-06-18 17:33:08 +00:00
|
|
|
case 'i':
|
|
|
|
interval = atoi(optarg);
|
|
|
|
*skip_args += 2;
|
|
|
|
if (interval < 1)
|
|
|
|
interval = 1;
|
|
|
|
if (interval > 25)
|
|
|
|
interval = 25;
|
|
|
|
break;
|
2016-11-27 05:47:06 +00:00
|
|
|
case 'b':
|
2016-03-01 17:40:38 +00:00
|
|
|
latency = atoi(optarg);
|
|
|
|
*skip_args += 2;
|
|
|
|
break;
|
2016-11-27 05:47:06 +00:00
|
|
|
case 'p':
|
2017-01-02 09:16:49 +00:00
|
|
|
if (!uses_emphasis) {
|
|
|
|
no_emph:
|
2017-01-08 11:10:29 +00:00
|
|
|
fprintf(stderr, "This network does not use emphasis, please do not enable pre- or de-emphasis! Disable emphasis on transceiver, if possible.\n");
|
2017-01-02 09:16:49 +00:00
|
|
|
exit(0);
|
|
|
|
}
|
2016-05-06 05:00:27 +00:00
|
|
|
do_pre_emphasis = 1;
|
|
|
|
*skip_args += 1;
|
|
|
|
break;
|
2016-11-27 05:47:06 +00:00
|
|
|
case 'd':
|
2017-01-02 09:16:49 +00:00
|
|
|
if (!uses_emphasis)
|
|
|
|
goto no_emph;
|
2016-05-06 05:00:27 +00:00
|
|
|
do_de_emphasis = 1;
|
|
|
|
*skip_args += 1;
|
|
|
|
break;
|
2016-11-27 05:47:06 +00:00
|
|
|
case 'g':
|
2016-05-06 05:00:27 +00:00
|
|
|
gain_db = atof(optarg);
|
|
|
|
if (gain_db < 0.0) {
|
2017-01-08 11:10:29 +00:00
|
|
|
fprintf(stderr, "Given gain is below 0. To reduce RX signal, use sound card's mixer (or resistor net)!\n");
|
2016-05-06 05:00:27 +00:00
|
|
|
exit(0);
|
|
|
|
}
|
|
|
|
rx_gain = pow(10, gain_db / 20.0);
|
|
|
|
*skip_args += 2;
|
|
|
|
break;
|
2016-03-01 17:40:38 +00:00
|
|
|
case 'm':
|
|
|
|
use_mncc_sock = 1;
|
|
|
|
*skip_args += 1;
|
|
|
|
break;
|
2016-11-27 05:47:06 +00:00
|
|
|
case 'c':
|
2017-01-04 13:14:02 +00:00
|
|
|
call_audiodev = strdup(optarg);
|
2016-11-27 05:47:06 +00:00
|
|
|
*skip_args += 2;
|
|
|
|
break;
|
2017-01-13 12:38:57 +00:00
|
|
|
case OPT_CALL_SAMPLERATE:
|
|
|
|
call_samplerate = atoi(optarg);
|
|
|
|
*skip_args += 2;
|
|
|
|
break;
|
2016-11-27 05:47:06 +00:00
|
|
|
case 't':
|
2016-03-01 17:40:38 +00:00
|
|
|
send_patterns = atoi(optarg);
|
|
|
|
*skip_args += 2;
|
|
|
|
break;
|
2016-11-27 05:47:06 +00:00
|
|
|
case 'l':
|
2016-03-01 17:40:38 +00:00
|
|
|
loopback = atoi(optarg);
|
|
|
|
*skip_args += 2;
|
|
|
|
break;
|
|
|
|
case 'r':
|
|
|
|
rt_prio = atoi(optarg);
|
|
|
|
*skip_args += 2;
|
|
|
|
break;
|
2016-11-27 05:47:06 +00:00
|
|
|
case OPT_WRITE_RX_WAVE:
|
|
|
|
write_rx_wave = strdup(optarg);
|
|
|
|
*skip_args += 2;
|
|
|
|
break;
|
|
|
|
case OPT_WRITE_TX_WAVE:
|
|
|
|
write_tx_wave = strdup(optarg);
|
2016-03-25 12:58:16 +00:00
|
|
|
*skip_args += 2;
|
|
|
|
break;
|
2016-11-27 05:47:06 +00:00
|
|
|
case OPT_READ_RX_WAVE:
|
|
|
|
read_rx_wave = strdup(optarg);
|
2016-03-25 12:58:16 +00:00
|
|
|
*skip_args += 2;
|
|
|
|
break;
|
2017-02-18 12:51:26 +00:00
|
|
|
case OPT_SDR_UHD:
|
|
|
|
sdr_uhd = 1;
|
|
|
|
*skip_args += 1;
|
|
|
|
break;
|
|
|
|
case OPT_SDR_SOAPY:
|
|
|
|
sdr_soapy = 1;
|
|
|
|
*skip_args += 1;
|
|
|
|
break;
|
2017-01-04 13:21:49 +00:00
|
|
|
case OPT_SDR_ARGS:
|
|
|
|
sdr_args = strdup(optarg);
|
|
|
|
*skip_args += 2;
|
|
|
|
break;
|
|
|
|
case OPT_SDR_RX_GAIN:
|
|
|
|
sdr_rx_gain = atof(optarg);
|
|
|
|
*skip_args += 2;
|
|
|
|
break;
|
|
|
|
case OPT_SDR_TX_GAIN:
|
|
|
|
sdr_tx_gain = atof(optarg);
|
|
|
|
*skip_args += 2;
|
|
|
|
break;
|
2017-01-13 06:31:15 +00:00
|
|
|
case OPT_WRITE_IQ_RX_WAVE:
|
|
|
|
write_iq_rx_wave = strdup(optarg);
|
|
|
|
*skip_args += 2;
|
|
|
|
break;
|
|
|
|
case OPT_WRITE_IQ_TX_WAVE:
|
|
|
|
write_iq_tx_wave = strdup(optarg);
|
|
|
|
*skip_args += 2;
|
|
|
|
break;
|
|
|
|
case OPT_READ_IQ_RX_WAVE:
|
|
|
|
read_iq_rx_wave = strdup(optarg);
|
|
|
|
*skip_args += 2;
|
|
|
|
break;
|
2016-03-01 17:40:38 +00:00
|
|
|
default:
|
|
|
|
exit (0);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* global variable to quit main loop */
|
|
|
|
int quit = 0;
|
|
|
|
|
|
|
|
void sighandler(int sigset)
|
|
|
|
{
|
|
|
|
if (sigset == SIGHUP)
|
|
|
|
return;
|
|
|
|
if (sigset == SIGPIPE)
|
|
|
|
return;
|
|
|
|
|
2016-11-28 08:16:48 +00:00
|
|
|
clear_console_text();
|
|
|
|
printf("Signal received: %d\n", sigset);
|
2016-03-01 17:40:38 +00:00
|
|
|
|
|
|
|
quit = 1;
|
|
|
|
}
|
|
|
|
|
2016-06-17 05:28:45 +00:00
|
|
|
static int get_char()
|
|
|
|
{
|
|
|
|
struct timeval tv = {0, 0};
|
|
|
|
fd_set fds;
|
|
|
|
char c = 0;
|
|
|
|
int __attribute__((__unused__)) rc;
|
|
|
|
|
|
|
|
FD_ZERO(&fds);
|
|
|
|
FD_SET(0, &fds);
|
|
|
|
select(0+1, &fds, NULL, NULL, &tv);
|
|
|
|
if (FD_ISSET(0, &fds)) {
|
|
|
|
rc = read(0, &c, 1);
|
|
|
|
return c;
|
|
|
|
} else
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2017-03-04 06:12:12 +00:00
|
|
|
/* Loop through all transceiver instances of one network. */
|
|
|
|
void main_common(int *quit, int latency, int interval, void (*myhandler)(void), const char *station_id, int station_id_digits)
|
2017-01-13 06:28:31 +00:00
|
|
|
{
|
2017-03-04 06:12:12 +00:00
|
|
|
int latspl;
|
|
|
|
sender_t *sender;
|
|
|
|
double last_time_call = 0, begin_time, now, sleep;
|
|
|
|
struct termios term, term_orig;
|
|
|
|
int c;
|
2017-01-13 06:28:31 +00:00
|
|
|
int rc;
|
|
|
|
|
|
|
|
/* init mncc */
|
|
|
|
if (use_mncc_sock) {
|
|
|
|
rc = mncc_init("/tmp/bsc_mncc");
|
|
|
|
if (rc < 0) {
|
|
|
|
fprintf(stderr, "Failed to setup MNCC socket. Quitting!\n");
|
2017-03-04 06:12:12 +00:00
|
|
|
return;
|
2017-01-13 06:28:31 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* init call device */
|
2017-01-13 12:38:57 +00:00
|
|
|
rc = call_init(station_id, call_audiodev, call_samplerate, latency, station_id_digits, loopback, use_mncc_sock, send_patterns, release_on_disconnect);
|
2017-01-13 06:28:31 +00:00
|
|
|
if (rc < 0) {
|
|
|
|
fprintf(stderr, "Failed to create call control instance. Quitting!\n");
|
2017-03-04 06:12:12 +00:00
|
|
|
return;
|
2017-01-13 06:28:31 +00:00
|
|
|
}
|
|
|
|
|
2017-02-18 12:51:26 +00:00
|
|
|
#ifdef HAVE_UHD
|
|
|
|
#ifdef HAVE_SOAPY
|
|
|
|
if ((sdr_uhd == 1 && sdr_soapy == 1) || (sdr_uhd == 0 && sdr_soapy == 0)) {
|
|
|
|
fprintf(stderr, "UHD and SoapySDR drivers are compiled in. You must choose which one you want: --sdr-uhd or --sdr-soapy\n");
|
2017-03-04 06:12:12 +00:00
|
|
|
return;
|
2017-02-18 12:51:26 +00:00
|
|
|
}
|
|
|
|
#else
|
|
|
|
sdr_uhd = 1;
|
|
|
|
sdr_soapy = 0;
|
|
|
|
#endif
|
|
|
|
#else
|
|
|
|
#ifdef HAVE_SOAPY
|
|
|
|
sdr_uhd = 0;
|
|
|
|
sdr_soapy = 1;
|
|
|
|
#endif
|
|
|
|
#endif
|
|
|
|
|
2017-01-13 06:28:31 +00:00
|
|
|
#ifdef HAVE_SDR
|
2017-02-18 12:51:26 +00:00
|
|
|
rc = sdr_init(sdr_uhd, sdr_soapy, sdr_args, sdr_rx_gain, sdr_tx_gain, write_iq_rx_wave, write_iq_tx_wave, read_iq_rx_wave);
|
2017-01-13 06:28:31 +00:00
|
|
|
if (rc < 0)
|
2017-03-04 06:12:12 +00:00
|
|
|
return;
|
2017-01-13 06:28:31 +00:00
|
|
|
#endif
|
|
|
|
|
2017-01-04 13:14:02 +00:00
|
|
|
/* open audio */
|
|
|
|
if (sender_open_audio())
|
|
|
|
return;
|
2017-01-29 15:54:28 +00:00
|
|
|
if (call_open_audio())
|
|
|
|
return;
|
|
|
|
|
2016-11-13 05:37:56 +00:00
|
|
|
/* real time priority */
|
|
|
|
if (rt_prio > 0) {
|
|
|
|
struct sched_param schedp;
|
|
|
|
int rc;
|
|
|
|
|
|
|
|
memset(&schedp, 0, sizeof(schedp));
|
|
|
|
schedp.sched_priority = rt_prio;
|
|
|
|
rc = sched_setscheduler(0, SCHED_RR, &schedp);
|
|
|
|
if (rc)
|
|
|
|
fprintf(stderr, "Error setting SCHED_RR with prio %d\n", rt_prio);
|
|
|
|
}
|
|
|
|
|
2016-06-17 05:28:45 +00:00
|
|
|
/* prepare terminal */
|
|
|
|
tcgetattr(0, &term_orig);
|
|
|
|
term = term_orig;
|
|
|
|
term.c_lflag &= ~(ISIG|ICANON|ECHO);
|
|
|
|
term.c_cc[VMIN]=1;
|
|
|
|
term.c_cc[VTIME]=2;
|
|
|
|
tcsetattr(0, TCSANOW, &term);
|
|
|
|
|
2017-01-04 13:14:02 +00:00
|
|
|
/* catch signals */
|
|
|
|
signal(SIGINT, sighandler);
|
|
|
|
signal(SIGHUP, sighandler);
|
|
|
|
signal(SIGTERM, sighandler);
|
|
|
|
signal(SIGPIPE, sighandler);
|
|
|
|
|
2017-02-25 06:09:53 +00:00
|
|
|
/* start streaming */
|
|
|
|
if (sender_start_audio())
|
|
|
|
*quit = 1;
|
|
|
|
if (call_start_audio())
|
|
|
|
*quit = 1;
|
|
|
|
|
2016-06-17 05:28:45 +00:00
|
|
|
while(!(*quit)) {
|
2017-01-14 07:05:30 +00:00
|
|
|
begin_time = get_time();
|
|
|
|
|
2016-06-17 05:28:45 +00:00
|
|
|
/* process sound of all transceivers */
|
|
|
|
for (sender = sender_head; sender; sender = sender->next) {
|
|
|
|
/* do not process audio for an audio slave, since it is done by audio master */
|
|
|
|
if (sender->master) /* if master is set, we are an audio slave */
|
|
|
|
continue;
|
|
|
|
latspl = sender->samplerate * latency / 1000;
|
|
|
|
process_sender_audio(sender, quit, latspl);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* process timers */
|
|
|
|
process_timer();
|
|
|
|
|
|
|
|
/* process audio for mncc call instances */
|
|
|
|
now = get_time();
|
2017-01-14 07:05:30 +00:00
|
|
|
if (now - last_time_call >= 0.1)
|
|
|
|
last_time_call = now;
|
|
|
|
if (now - last_time_call >= 0.020) {
|
|
|
|
last_time_call += 0.020;
|
2016-06-17 05:28:45 +00:00
|
|
|
/* call clock every 20ms */
|
|
|
|
call_mncc_clock();
|
|
|
|
}
|
|
|
|
|
2016-06-19 15:43:48 +00:00
|
|
|
next_char:
|
2016-06-17 05:28:45 +00:00
|
|
|
c = get_char();
|
|
|
|
switch (c) {
|
|
|
|
case 3:
|
|
|
|
/* quit */
|
2016-11-28 08:16:48 +00:00
|
|
|
clear_console_text();
|
|
|
|
printf("CTRL+c received, quitting!\n");
|
2016-06-17 05:28:45 +00:00
|
|
|
*quit = 1;
|
2016-06-19 15:43:48 +00:00
|
|
|
goto next_char;
|
2016-06-17 05:28:45 +00:00
|
|
|
case 'w':
|
|
|
|
/* toggle display */
|
2017-02-05 07:54:56 +00:00
|
|
|
#ifdef HAVE_SDR
|
2017-01-07 18:53:43 +00:00
|
|
|
display_iq_on(0);
|
2017-02-05 07:54:56 +00:00
|
|
|
display_spectrum_on(0);
|
|
|
|
#endif
|
2016-06-17 05:28:45 +00:00
|
|
|
display_wave_on(-1);
|
2016-06-19 15:43:48 +00:00
|
|
|
goto next_char;
|
2017-02-05 07:54:56 +00:00
|
|
|
#ifdef HAVE_SDR
|
2017-01-07 18:53:43 +00:00
|
|
|
case 'q':
|
|
|
|
/* toggle display */
|
|
|
|
display_wave_on(0);
|
2017-02-05 07:54:56 +00:00
|
|
|
display_spectrum_on(0);
|
2017-01-07 18:53:43 +00:00
|
|
|
display_iq_on(-1);
|
|
|
|
goto next_char;
|
2017-02-05 07:54:56 +00:00
|
|
|
case 's':
|
|
|
|
/* toggle spectrum */
|
|
|
|
display_wave_on(0);
|
|
|
|
display_iq_on(0);
|
|
|
|
display_spectrum_on(-1);
|
|
|
|
goto next_char;
|
|
|
|
#endif
|
2016-08-02 07:03:46 +00:00
|
|
|
case 'i':
|
|
|
|
/* dump info */
|
|
|
|
dump_info();
|
|
|
|
goto next_char;
|
2016-06-17 05:28:45 +00:00
|
|
|
}
|
|
|
|
|
2016-06-19 15:43:48 +00:00
|
|
|
/* process audio of built-in call control */
|
|
|
|
process_call(c);
|
|
|
|
|
2016-07-09 09:15:48 +00:00
|
|
|
if (myhandler)
|
|
|
|
myhandler();
|
|
|
|
|
2017-01-14 07:05:30 +00:00
|
|
|
now = get_time();
|
|
|
|
|
|
|
|
/* sleep interval */
|
|
|
|
sleep = ((double)interval / 1000.0) - (now - begin_time);
|
|
|
|
if (sleep > 0)
|
|
|
|
usleep(sleep * 1000000.0);
|
|
|
|
|
|
|
|
// now = get_time();
|
|
|
|
// printf("duration =%.6f\n", now - begin_time);
|
2016-06-17 05:28:45 +00:00
|
|
|
}
|
|
|
|
|
2017-01-04 13:14:02 +00:00
|
|
|
/* reset signals */
|
|
|
|
signal(SIGINT, SIG_DFL);
|
|
|
|
signal(SIGHUP, SIG_DFL);
|
|
|
|
signal(SIGTERM, SIG_DFL);
|
|
|
|
signal(SIGPIPE, SIG_DFL);
|
|
|
|
|
2016-11-28 08:16:48 +00:00
|
|
|
/* get rid of last entry */
|
2016-07-24 07:12:54 +00:00
|
|
|
clear_console_text();
|
|
|
|
|
2016-06-17 05:28:45 +00:00
|
|
|
/* reset terminal */
|
|
|
|
tcsetattr(0, TCSANOW, &term_orig);
|
2016-11-13 05:37:56 +00:00
|
|
|
|
|
|
|
/* reset real time prio */
|
|
|
|
if (rt_prio > 0) {
|
|
|
|
struct sched_param schedp;
|
|
|
|
|
|
|
|
memset(&schedp, 0, sizeof(schedp));
|
|
|
|
schedp.sched_priority = 0;
|
|
|
|
sched_setscheduler(0, SCHED_OTHER, &schedp);
|
|
|
|
}
|
2016-06-17 05:28:45 +00:00
|
|
|
|
2017-03-04 06:12:12 +00:00
|
|
|
/* cleanup call control */
|
2017-01-13 06:28:31 +00:00
|
|
|
call_cleanup();
|
2017-03-04 06:12:12 +00:00
|
|
|
|
|
|
|
/* close mncc socket */
|
2017-01-13 06:28:31 +00:00
|
|
|
if (use_mncc_sock)
|
|
|
|
mncc_exit();
|
|
|
|
}
|
|
|
|
|