2016-03-01 17:40:38 +00:00
/* B-Netz main
*
* ( 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>
2017-10-09 18:49:14 +00:00
# include <math.h>
2017-01-27 15:57:34 +00:00
# include "../common/sample.h"
2016-03-01 17:40:38 +00:00
# include "../common/debug.h"
# include "../common/call.h"
# include "../common/mncc_sock.h"
2017-08-20 06:27:02 +00:00
# include "../common/main_mobile.h"
2017-11-16 17:37:25 +00:00
# include "../anetz/freiton.h"
# include "../anetz/besetztton.h"
2016-03-01 17:40:38 +00:00
# include "bnetz.h"
# include "dsp.h"
2016-03-02 18:47:40 +00:00
# include "stations.h"
2016-03-01 17:40:38 +00:00
# include "image.h"
2016-03-11 05:59:05 +00:00
# include "ansage.h"
2016-03-01 17:40:38 +00:00
int gfs = 2 ;
2017-08-12 14:38:40 +00:00
int metering = 20 ;
2017-01-14 07:03:42 +00:00
const char * paging = " tone " ;
2017-10-09 18:49:14 +00:00
double squelch_db = - INFINITY ;
2016-03-01 17:40:38 +00:00
void print_help ( const char * arg0 )
{
2017-08-30 15:42:49 +00:00
main_mobile_print_help ( arg0 , " [-G <gfs>] " ) ;
2016-03-01 17:40:38 +00:00
/* - - */
2016-11-27 05:47:06 +00:00
printf ( " -G --gfs <gruppenfreisignal> | <lat>,<lon> \n " ) ;
2016-03-01 17:40:38 +00:00
printf ( " Gruppenfreisignal \" 1..9 | 19 | 10..18 (default = '%d') \n " , gfs ) ;
2016-03-02 18:47:40 +00:00
printf ( " Alternative give your coordinates of your location, to find closest \n " ) ;
printf ( " base station. (e.g. '--gfs 54.487291,9.069993') Or use '--gfs list' to \n " ) ;
printf ( " get a list of all base station locations. \n " ) ;
2016-11-27 05:47:06 +00:00
printf ( " -G --gfs 19 \n " ) ;
2016-07-19 18:34:35 +00:00
printf ( " Set to 19 in order to make the phone transmit at 100 mW instead of \n " ) ;
printf ( " full 15 Watts. If supported, the phone uses the channel with low power \n " ) ;
printf ( " (Kanal kleiner Leistung). \n " ) ;
2017-08-12 14:38:40 +00:00
printf ( " -M --gebuehrenimpuls <secods> | -<seconds> | 0 \n " ) ;
printf ( " Send metering pulses every given number of seconds or 0 to turn off. \n " ) ;
2017-10-16 17:48:39 +00:00
printf ( " Pulses will be sent on outgoing calls only and only if mobile station \n " ) ;
printf ( " requests it. Use negative value to force metering pulses for all calls. \n " ) ;
printf ( " (default = %d) \n " , metering ) ;
2017-01-07 09:33:13 +00:00
printf ( " -P --paging tone | notone | positive | negative | <file>=<on>:<off> \n " ) ;
2016-03-01 17:40:38 +00:00
printf ( " Send a tone, give a signal or write to a file when switching to \n " ) ;
printf ( " channel 19. (paging the phone). \n " ) ;
2016-04-25 18:20:54 +00:00
printf ( " 'tone', 'positive', 'negative' is sent on second audio channel. \n " ) ;
2016-10-03 12:46:25 +00:00
printf ( " 'tone' sends a tone whenever channel 19 is switched. \n " ) ;
printf ( " 'notone' sends a tone whenever channel 19 is NOT switched. \n " ) ;
2016-03-01 17:40:38 +00:00
printf ( " 'positive' sends a positive signal for channel 19, else negative. \n " ) ;
printf ( " 'negative' sends a negative signal for channel 19, else positive. \n " ) ;
printf ( " Example: /sys/class/gpio/gpio17/value=1:0 writes a '1' to \n " ) ;
printf ( " /sys/class/gpio/gpio17/value to switching to channel 19 and a '0' to \n " ) ;
2017-01-14 07:03:42 +00:00
printf ( " switch back. (default = %s) \n " , paging ) ;
2017-10-09 18:49:14 +00:00
printf ( " -S --squelch <dB> \n " ) ;
printf ( " Use given RF level to detect loss of signal. When the signal gets lost \n " ) ;
printf ( " and stays below this level, the connection is released. \n " ) ;
printf ( " Use 'auto' to do automatic noise floor calibration to detect loss. \n " ) ;
printf ( " Only works with SDR! (disabled by default) \n " ) ;
2016-03-01 17:40:38 +00:00
printf ( " \n station-id: Give 5 digit station-id, you don't need to enter it for every \n " ) ;
printf ( " start of this program. \n " ) ;
2017-08-30 15:42:49 +00:00
main_mobile_print_hotkeys ( ) ;
2016-03-01 17:40:38 +00:00
}
static int handle_options ( int argc , char * * argv )
{
int skip_args = 0 ;
2016-03-02 18:47:40 +00:00
char * p ;
2016-03-01 17:40:38 +00:00
static struct option long_options_special [ ] = {
2016-11-27 05:47:06 +00:00
{ " gfs " , 1 , 0 , ' G ' } ,
2017-08-12 14:38:40 +00:00
{ " gebuehrenimpuls " , 1 , 0 , ' M ' } ,
2017-01-07 09:33:13 +00:00
{ " paging " , 1 , 0 , ' P ' } ,
2017-10-09 18:49:14 +00:00
{ " squelch " , 1 , 0 , ' S ' } ,
2016-03-01 17:40:38 +00:00
{ 0 , 0 , 0 , 0 } ,
} ;
2017-10-09 18:49:14 +00:00
main_mobile_set_options ( " G:M:P:S: " , long_options_special ) ;
2016-03-01 17:40:38 +00:00
while ( 1 ) {
int option_index = 0 , c ;
c = getopt_long ( argc , argv , optstring , long_options , & option_index ) ;
if ( c = = - 1 )
break ;
switch ( c ) {
2016-11-27 05:47:06 +00:00
case ' G ' :
2016-03-02 18:47:40 +00:00
if ( ! strcasecmp ( optarg , " list " ) ) {
station_list ( ) ;
exit ( 0 ) ;
}
if ( ( p = strchr ( optarg , ' , ' ) ) ) {
gfs = get_station_by_coordinates ( atof ( optarg ) , atof ( p + 1 ) ) ;
if ( gfs = = 0 )
exit ( 0 ) ;
} else
gfs = atoi ( optarg ) ;
2016-03-01 17:40:38 +00:00
skip_args + = 2 ;
break ;
2017-08-12 14:38:40 +00:00
case ' M ' :
metering = atoi ( optarg ) ;
skip_args + = 2 ;
break ;
2016-03-01 17:40:38 +00:00
case ' P ' :
2017-01-14 07:03:42 +00:00
paging = strdup ( optarg ) ;
2016-03-01 17:40:38 +00:00
skip_args + = 2 ;
break ;
2017-10-09 18:49:14 +00:00
case ' S ' :
if ( ! strcasecmp ( optarg , " auto " ) )
squelch_db = 0.0 ;
else
squelch_db = atof ( optarg ) ;
2016-03-05 07:14:36 +00:00
skip_args + = 2 ;
break ;
2016-03-01 17:40:38 +00:00
default :
2017-08-30 15:42:49 +00:00
main_mobile_opt_switch ( c , argv [ 0 ] , & skip_args ) ;
2016-03-01 17:40:38 +00:00
}
}
return skip_args ;
}
int main ( int argc , char * argv [ ] )
{
int rc ;
int skip_args ;
const char * station_id = " " ;
2016-04-25 18:20:54 +00:00
int i ;
2016-03-01 17:40:38 +00:00
2016-03-11 05:59:05 +00:00
/* init common tones */
init_freiton ( ) ;
init_besetzton ( ) ;
init_ansage ( ) ;
2017-08-30 15:42:49 +00:00
main_mobile_init ( ) ;
2016-03-01 17:40:38 +00:00
skip_args = handle_options ( argc , argv ) ;
argc - = skip_args ;
argv + = skip_args ;
if ( argc > 1 ) {
station_id = argv [ 1 ] ;
if ( strlen ( station_id ) ! = 5 ) {
printf ( " Given station ID '%s' does not have 5 digits \n " , station_id ) ;
return 0 ;
}
}
2016-04-25 18:20:54 +00:00
if ( ! num_kanal ) {
2017-06-10 15:49:59 +00:00
printf ( " No channel ( \" Kanal \" ) is specified, I suggest channel 1 (sound card) or 17 (SDR). \n \n " ) ;
2016-05-08 05:23:05 +00:00
print_help ( argv [ - skip_args ] ) ;
2016-03-01 17:40:38 +00:00
return 0 ;
}
2017-05-14 10:27:56 +00:00
if ( use_sdr ) {
/* set audiodev */
for ( i = 0 ; i < num_kanal ; i + + )
audiodev [ i ] = " sdr " ;
num_audiodev = num_kanal ;
}
2017-01-04 13:14:02 +00:00
if ( num_kanal = = 1 & & num_audiodev = = 0 )
2017-05-14 10:27:56 +00:00
num_audiodev = 1 ; /* use default */
2017-01-04 13:14:02 +00:00
if ( num_kanal ! = num_audiodev ) {
2016-11-28 08:16:48 +00:00
fprintf ( stderr , " You need to specify as many sound devices as you have channels. \n " ) ;
2016-04-25 18:20:54 +00:00
exit ( 0 ) ;
}
2016-03-01 17:40:38 +00:00
if ( ! loopback )
print_image ( ) ;
/* init functions */
dsp_init ( ) ;
bnetz_init ( ) ;
2017-01-07 15:53:28 +00:00
/* SDR always requires emphasis */
2017-05-14 10:27:56 +00:00
if ( use_sdr ) {
2017-01-07 15:53:28 +00:00
do_pre_emphasis = 1 ;
do_de_emphasis = 1 ;
}
2016-03-01 17:40:38 +00:00
/* create transceiver instance */
2016-04-25 18:20:54 +00:00
for ( i = 0 ; i < num_kanal ; i + + ) {
2017-10-09 18:49:14 +00:00
rc = bnetz_create ( kanal [ i ] , audiodev [ i ] , use_sdr , samplerate , rx_gain , gfs , do_pre_emphasis , do_de_emphasis , write_rx_wave , write_tx_wave , read_rx_wave , read_tx_wave , loopback , squelch_db , paging , metering ) ;
2016-04-25 18:20:54 +00:00
if ( rc < 0 ) {
fprintf ( stderr , " Failed to create \" Sender \" instance. Quitting! \n " ) ;
goto fail ;
}
2017-10-12 17:43:15 +00:00
printf ( " Base station for channel %d ready, please tune transmitter to %.3f MHz and receiver " " to %.3f MHz. (%.3f MHz offset) \n " , kanal [ i ] , bnetz_kanal2freq ( kanal [ i ] , 0 ) / 1e6 , bnetz_kanal2freq ( kanal [ i ] , 1 ) / 1e6 , bnetz_kanal2freq ( kanal [ i ] , 2 ) / 1e6 ) ;
2017-01-07 09:33:13 +00:00
printf ( " To call phone, switch transmitter (using paging signal) to %.3f MHz. \n " , bnetz_kanal2freq ( 19 , 0 ) / 1e6 ) ;
2016-03-01 17:40:38 +00:00
}
2017-08-20 06:27:02 +00:00
main_mobile ( & quit , latency , interval , NULL , station_id , 5 ) ;
2016-03-01 17:40:38 +00:00
fail :
/* destroy transceiver instance */
while ( sender_head )
bnetz_destroy ( sender_head ) ;
return 0 ;
}