2016-02-21 17:13:30 +00:00
/* NMT 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 <stdlib.h>
# include <string.h>
2016-07-04 17:52:00 +00:00
# include <fcntl.h>
# include <unistd.h>
2018-05-19 08:56:43 +00:00
# include <errno.h>
2016-07-04 17:52:00 +00:00
# include <sys/types.h>
# include <sys/stat.h>
2017-11-18 07:06:06 +00:00
# include "../libsample/sample.h"
2017-11-18 07:33:07 +00:00
# include "../libmobile/main_mobile.h"
2017-11-18 07:58:57 +00:00
# include "../libdebug/debug.h"
2018-05-19 08:56:43 +00:00
# include "../liboptions/options.h"
2016-02-21 17:13:30 +00:00
# include "nmt.h"
# include "frame.h"
# include "dsp.h"
# include "tones.h"
# include "announcement.h"
2017-06-09 18:18:24 +00:00
# include "countries.h"
2016-02-21 17:13:30 +00:00
2016-07-10 09:15:29 +00:00
# define SMS_DELIVER " / tmp / nmt_sms_deliver"
# define SMS_SUBMIT " / tmp / nmt_sms_submit"
static int sms_deliver_fd = - 1 ;
2016-07-04 17:52:00 +00:00
2016-02-21 17:13:30 +00:00
/* settings */
2017-07-22 08:38:18 +00:00
int nmt_system = 450 ;
2016-04-25 18:20:54 +00:00
int num_chan_type = 0 ;
enum nmt_chan_type chan_type [ MAX_SENDER ] = { CHAN_TYPE_CC_TC } ;
2017-07-22 08:38:18 +00:00
int ms_power = 1 ; /* 0..3 */
2017-06-09 18:18:24 +00:00
char country [ 16 ] = " " ;
2019-03-09 08:51:09 +00:00
uint8_t traffic_area ;
int traffic_area_given = 0 ;
2016-02-21 17:13:30 +00:00
char area_no = 0 ;
2016-06-20 17:37:56 +00:00
int compandor = 1 ;
2017-01-08 15:47:26 +00:00
int num_supervisory = 0 ;
int supervisory [ MAX_SENDER ] = { 1 } ;
2016-07-04 17:52:00 +00:00
const char * smsc_number = " 767 " ;
2016-07-20 09:58:12 +00:00
int send_callerid = 0 ;
2016-02-21 17:13:30 +00:00
void print_help ( const char * arg0 )
{
2017-08-30 15:42:49 +00:00
main_mobile_print_help ( arg0 , " [-N 900] -Y <traffic area> | list [-I 1] [-0 1] " ) ;
2016-02-21 17:13:30 +00:00
/* - - */
2017-07-22 08:38:18 +00:00
printf ( " -N --nmt-system 450/900 \n " ) ;
2021-01-01 21:11:48 +00:00
printf ( " Give NMT type as first parameter. (default = '%d') \n " , nmt_system ) ;
2017-07-22 08:38:18 +00:00
printf ( " Note: This option must be given at first! \n " ) ;
2016-11-27 05:47:06 +00:00
printf ( " -T --channel-type <channel type> | list \n " ) ;
2017-07-22 08:38:18 +00:00
printf ( " Give channel type, use 'list' to get a list. (default = '%s') \n " , chan_type_short_name ( nmt_system , chan_type [ 0 ] ) ) ;
2016-02-21 17:13:30 +00:00
printf ( " -P --ms-power <power level> \n " ) ;
2017-07-22 08:38:18 +00:00
printf ( " Give power level of the mobile station 0..3. (default = '%d') \n " , ms_power ) ;
2017-10-02 10:32:02 +00:00
if ( nmt_system = = 450 ) {
2017-07-22 08:38:18 +00:00
printf ( " NMT-450: 3 = 15 W / 7 W (handheld), 2 = 1.5 W, 1/0 = 150 mW \n " ) ;
2017-10-02 10:32:02 +00:00
} else {
2017-07-22 08:38:18 +00:00
printf ( " NMT-900: 3/2 = 6 W, 1 = 1.5 W, 0 = 150 mW \n " ) ;
2017-10-02 10:32:02 +00:00
}
2018-01-02 10:51:32 +00:00
printf ( " Note: Power level < 2 causes mobile to search longer for a cell. \n " ) ;
2016-11-27 05:47:06 +00:00
printf ( " -Y --traffic-area <traffic area> | list \n " ) ;
2016-02-21 17:13:30 +00:00
printf ( " NOTE: MUST MATCH WITH YOUR ROAMING SETTINGS IN THE PHONE! \n " ) ;
printf ( " Your phone will not connect, if country code is different! \n " ) ;
2019-07-15 19:14:52 +00:00
printf ( " Give short country code and traffic area separated by comma. \n " ) ;
2017-07-02 08:45:47 +00:00
printf ( " (Example: Give 'SE,1' for Sweden, traffic area 1) \n " ) ;
printf ( " Add '!' to force traffic area that is not supported by country. \n " ) ;
printf ( " (Example: Give 'B,12!' for Belgium, traffic area 12) \n " ) ;
printf ( " Use 'list' to get a list of available country code names \n " ) ;
2016-11-27 05:47:06 +00:00
printf ( " -A --area-number <area no> | 0 \n " ) ;
2016-02-21 17:13:30 +00:00
printf ( " Give area number 1..4 or 0 for no area number. (default = '%d') \n " , area_no ) ;
2016-06-20 17:37:56 +00:00
printf ( " -C --compandor 1 | 0 \n " ) ;
printf ( " Make use of the compandor to reduce noise during call. (default = '%d') \n " , compandor ) ;
2016-02-21 17:13:30 +00:00
printf ( " -0 --supervisory 1..4 | 0 \n " ) ;
printf ( " Use supervisory signal 1..4 to detect loss of signal from mobile \n " ) ;
2017-01-08 15:47:26 +00:00
printf ( " station, use 0 to disable. (default = '%d') \n " , supervisory [ 0 ] ) ;
2016-07-04 17:52:00 +00:00
printf ( " -S --smsc-number <digits> \n " ) ;
printf ( " If this number is dialed, the mobile is connected to the SMSC (Short \n " ) ;
printf ( " Message Service Center). (default = '%s') \n " , smsc_number ) ;
2016-07-20 09:58:12 +00:00
printf ( " -I --caller-id 1 | 0 \n " ) ;
printf ( " If set, the caller ID is sent while ringing the phone. (default = '%d') \n " , send_callerid ) ;
2016-02-21 17:13:30 +00:00
printf ( " \n station-id: Give 7 digits of station-id, you don't need to enter it \n " ) ;
printf ( " for every start of this program. \n " ) ;
2017-08-30 15:42:49 +00:00
main_mobile_print_hotkeys ( ) ;
2016-02-21 17:13:30 +00:00
}
2018-05-19 08:56:43 +00:00
static void add_options ( void )
2016-02-21 17:13:30 +00:00
{
2018-05-19 08:56:43 +00:00
main_mobile_add_options ( ) ;
option_add ( ' N ' , " nmt-system " , 1 ) ;
option_add ( ' T ' , " channel-type " , 1 ) ;
option_add ( ' P ' , " ms-power " , 1 ) ;
option_add ( ' Y ' , " traffic-area " , 1 ) ;
option_add ( ' A ' , " area-number " , 1 ) ;
option_add ( ' C ' , " compandor " , 1 ) ;
option_add ( ' 0 ' , " supervisory " , 1 ) ;
option_add ( ' S ' , " smsc-number " , 1 ) ;
option_add ( ' I ' , " caller-id " , 1 ) ;
}
static int handle_options ( int short_option , int argi , char * * argv )
{
int rc ;
2017-06-09 18:18:24 +00:00
char * p ;
2017-01-08 15:47:26 +00:00
int super ;
2016-02-21 17:13:30 +00:00
2018-05-19 08:56:43 +00:00
switch ( short_option ) {
case ' N ' :
nmt_system = atoi ( argv [ argi ] ) ;
if ( nmt_system ! = 450 & & nmt_system ! = 900 ) {
fprintf ( stderr , " Error, NMT system type '%s' unknown. Please use '-N 450' for NMT-450 or '-N 900' for NMT-900. \n " , argv [ argi ] ) ;
return - EINVAL ;
}
if ( nmt_system = = 900 )
ms_power = 0 ;
if ( ! option_is_first ( ) ) {
fprintf ( stderr , " Please specify the NMT system (-N) as first command line option! \n " ) ;
return - EINVAL ;
}
break ;
case ' T ' :
if ( ! strcmp ( argv [ argi ] , " list " ) ) {
nmt_channel_list ( nmt_system ) ;
return 0 ;
}
rc = nmt_channel_by_short_name ( nmt_system , argv [ argi ] ) ;
if ( rc < 0 ) {
fprintf ( stderr , " Error, channel type '%s' unknown. Please use '-t list' to get a list. I suggest to use the default. \n " , argv [ argi ] ) ;
return - EINVAL ;
}
OPT_ARRAY ( num_chan_type , chan_type , rc )
break ;
case ' P ' :
ms_power = atoi ( argv [ argi ] ) ;
if ( ms_power > 3 )
ms_power = 3 ;
if ( ms_power < 0 )
ms_power = 0 ;
break ;
case ' Y ' :
if ( ! strcmp ( argv [ argi ] , " list " ) ) {
nmt_country_list ( nmt_system ) ;
return 0 ;
}
/* digits */
strncpy ( country , argv [ argi ] , sizeof ( country ) - 1 ) ;
country [ sizeof ( country ) - 1 ] = ' \0 ' ;
p = strchr ( country , ' , ' ) ;
if ( ! p ) {
fprintf ( stderr , " Illegal traffic area '%s', see '-h' for help \n " , argv [ argi ] ) ;
return - EINVAL ;
}
* p + + = ' \0 ' ;
rc = nmt_country_by_short_name ( nmt_system , country ) ;
if ( rc < 0 ) {
2016-02-21 17:13:30 +00:00
error_ta :
2018-05-19 08:56:43 +00:00
fprintf ( stderr , " Invalid traffic area '%s', use '-Y list' for a list of valid areas \n " , argv [ argi ] ) ;
return - EINVAL ;
}
2019-03-09 08:51:09 +00:00
traffic_area = rc < < 4 ;
2018-05-19 08:56:43 +00:00
if ( p [ strlen ( p ) - 1 ] ! = ' ! ' ) {
rc = nmt_ta_by_short_name ( nmt_system , country , atoi ( p ) ) ;
if ( rc < 0 )
goto error_ta ;
}
2019-03-09 08:51:09 +00:00
if ( atoi ( p ) < 0 | | atoi ( p ) > 15 )
goto error_ta ;
traffic_area | = ( nmt_system = = 450 ) ? nmt_flip_ten ( atoi ( p ) ) : atoi ( p ) ;
traffic_area_given = 1 ;
2018-05-19 08:56:43 +00:00
break ;
case ' A ' :
area_no = argv [ argi ] [ 0 ] - ' 0 ' ;
if ( area_no > 4 ) {
fprintf ( stderr , " Area number '%s' out of range, please use 1..4 or 0 for no area \n " , argv [ argi ] ) ;
return - EINVAL ;
2016-02-21 17:13:30 +00:00
}
2018-05-19 08:56:43 +00:00
break ;
case ' C ' :
compandor = atoi ( argv [ argi ] ) ;
break ;
case ' 0 ' :
super = atoi ( argv [ argi ] ) ;
if ( super < 0 | | super > 4 ) {
fprintf ( stderr , " Given supervisory signal is wrong, use '-h' for help! \n " ) ;
return - EINVAL ;
}
OPT_ARRAY ( num_supervisory , supervisory , super )
break ;
case ' S ' :
2021-01-25 14:16:29 +00:00
smsc_number = options_strdup ( argv [ argi ] ) ;
2018-05-19 08:56:43 +00:00
break ;
case ' I ' :
send_callerid = atoi ( argv [ argi ] ) ;
break ;
default :
return main_mobile_handle_options ( short_option , argi , argv ) ;
2016-02-21 17:13:30 +00:00
}
2018-05-19 08:56:43 +00:00
return 1 ;
2016-02-21 17:13:30 +00:00
}
2016-07-04 17:52:00 +00:00
static void myhandler ( void )
{
static char buffer [ 256 ] ;
static int pos = 0 , rc , i ;
int space = sizeof ( buffer ) - pos ;
2016-07-10 09:15:29 +00:00
rc = read ( sms_deliver_fd , buffer + pos , space ) ;
2016-07-04 17:52:00 +00:00
if ( rc > 0 ) {
pos + = rc ;
if ( pos = = space ) {
fprintf ( stderr , " SMS buffer overflow! \n " ) ;
pos = 0 ;
}
/* check for end of line */
for ( i = 0 ; i < pos ; i + + ) {
if ( buffer [ i ] = = ' \r ' | | buffer [ i ] = = ' \n ' )
break ;
}
/* send sms */
if ( i < pos ) {
buffer [ i ] = ' \0 ' ;
pos = 0 ;
deliver_sms ( buffer ) ;
}
}
}
2016-07-10 09:15:29 +00:00
int submit_sms ( const char * sms )
{
FILE * fp ;
fp = fopen ( SMS_SUBMIT , " a " ) ;
if ( ! fp ) {
fprintf ( stderr , " Failed to open SMS submit file '%s'! \n " , SMS_SUBMIT ) ;
return - 1 ;
}
fprintf ( fp , " %s \n " , sms ) ;
fclose ( fp ) ;
return 0 ;
}
2016-02-21 17:13:30 +00:00
int main ( int argc , char * argv [ ] )
{
2018-05-19 08:56:43 +00:00
int rc , argi ;
2016-02-21 17:13:30 +00:00
const char * station_id = " " ;
2016-03-26 17:42:38 +00:00
int mandatory = 0 ;
2016-04-25 18:20:54 +00:00
int i ;
2016-02-21 17:13:30 +00:00
/* init common tones */
init_nmt_tones ( ) ;
init_announcement ( ) ;
2017-08-30 15:42:49 +00:00
main_mobile_init ( ) ;
2018-05-19 08:56:43 +00:00
/* handle options / config file */
add_options ( ) ;
2021-03-11 06:38:00 +00:00
rc = options_config_file ( argc , argv , " ~/.osmocom/analog/nmt.conf " , handle_options ) ;
2018-05-19 08:56:43 +00:00
if ( rc < 0 )
return 0 ;
argi = options_command_line ( argc , argv , handle_options ) ;
if ( argi < = 0 )
return argi ;
2016-02-21 17:13:30 +00:00
2018-05-19 08:56:43 +00:00
if ( argi < argc ) {
station_id = argv [ argi ] ;
2016-05-15 18:33:20 +00:00
if ( strlen ( station_id ) ! = 7 ) {
printf ( " Given station ID '%s' does not have 7 digits \n " , station_id ) ;
return 0 ;
}
}
2016-02-21 17:13:30 +00:00
2016-04-25 18:20:54 +00:00
if ( ! num_kanal ) {
2016-03-26 17:42:38 +00:00
printf ( " No channel ( \" Kanal \" ) is specified, I suggest channel 1 (-k 1). \n \n " ) ;
mandatory = 1 ;
2016-02-21 17:13:30 +00:00
}
2017-05-14 10:27:56 +00:00
if ( use_sdr ) {
2021-09-18 09:43:01 +00:00
/* set device */
2017-05-14 10:27:56 +00:00
for ( i = 0 ; i < num_kanal ; i + + )
2021-09-18 09:43:01 +00:00
dsp_device [ i ] = " sdr " ;
num_device = num_kanal ;
2017-10-28 17:01:27 +00:00
/* set channel types for more than 1 channel */
if ( num_kanal > 1 & & num_chan_type = = 0 ) {
2018-01-02 10:51:32 +00:00
if ( loopback )
chan_type [ 0 ] = CHAN_TYPE_TEST ;
else
chan_type [ 0 ] = CHAN_TYPE_CC ;
for ( i = 1 ; i < num_kanal ; i + + ) {
if ( loopback )
chan_type [ i ] = CHAN_TYPE_TEST ;
else
chan_type [ i ] = CHAN_TYPE_TC ;
}
2017-05-14 10:27:56 +00:00
num_chan_type = num_kanal ;
}
2017-06-10 15:11:47 +00:00
if ( num_supervisory = = 0 ) {
/* set supervisory signal */
for ( i = 0 ; i < num_kanal ; i + + )
supervisory [ i ] = ( i % 4 ) + 1 ;
num_supervisory = num_kanal ;
}
2017-05-14 10:27:56 +00:00
}
2021-09-18 09:43:01 +00:00
if ( num_kanal = = 1 & & num_device = = 0 )
num_device = 1 ; /* use default */
if ( num_kanal ! = num_device ) {
2016-11-28 08:16:48 +00:00
fprintf ( stderr , " You need to specify as many sound devices as you have channels. \n " ) ;
2018-05-19 08:56:43 +00:00
return - EINVAL ;
2016-04-25 18:20:54 +00:00
}
2018-01-02 10:51:32 +00:00
if ( num_kanal = = 1 & & num_chan_type = = 0 ) {
2017-05-14 10:27:56 +00:00
num_chan_type = 1 ; /* use default */
2018-01-02 10:51:32 +00:00
if ( loopback )
chan_type [ 0 ] = CHAN_TYPE_TEST ;
}
2016-04-25 18:20:54 +00:00
if ( num_kanal ! = num_chan_type ) {
2016-11-28 08:16:48 +00:00
fprintf ( stderr , " You need to specify as many channel types as you have channels. \n " ) ;
2018-05-19 08:56:43 +00:00
return - EINVAL ;
2016-04-25 18:20:54 +00:00
}
2017-01-08 15:47:26 +00:00
if ( num_kanal = = 1 & & num_supervisory = = 0 )
num_supervisory = 1 ; /* use default */
if ( num_kanal ! = num_supervisory ) {
fprintf ( stderr , " You need to specify as many supervisory signals as you have channels. \n " ) ;
fprintf ( stderr , " They shall be different at channels that are close to each other. \n " ) ;
2018-05-19 08:56:43 +00:00
return - EINVAL ;
2017-01-08 15:47:26 +00:00
}
if ( num_kanal ) {
uint8_t super [ 5 ] = { 0 , 0 , 0 , 0 , 0 } ;
for ( i = 0 ; i < num_kanal ; i + + ) {
if ( supervisory [ i ] = = 0 ) {
2019-07-20 16:11:17 +00:00
fprintf ( stderr , " No supervisory signal given for channel %s. This is ok, but signal loss dannot be detected. \n " , kanal [ i ] ) ;
2017-01-08 15:47:26 +00:00
continue ;
}
if ( super [ supervisory [ i ] ] ) {
2019-07-20 16:11:17 +00:00
fprintf ( stderr , " Supervisory signal %d is selected for both cannels #%s and #%d. I advice to use different signal, to avoid co-channel interferences. \n " , supervisory [ i ] , kanal [ i ] , super [ supervisory [ i ] ] ) ;
2017-01-08 15:47:26 +00:00
continue ;
}
2019-07-20 16:11:17 +00:00
super [ supervisory [ i ] ] = atoi ( kanal [ i ] ) ;
2017-01-08 15:47:26 +00:00
}
}
2016-02-21 17:13:30 +00:00
2019-03-09 08:51:09 +00:00
if ( ! traffic_area_given ) {
2017-05-14 10:27:56 +00:00
printf ( " No traffic area is specified, I suggest to use Sweden (-Y SE,1) and set the phone's roaming to 'SE' also. \n \n " ) ;
2016-03-26 17:42:38 +00:00
mandatory = 1 ;
}
if ( mandatory ) {
2018-05-19 08:56:43 +00:00
print_help ( argv [ 0 ] ) ;
2016-02-21 17:13:30 +00:00
return 0 ;
}
2016-07-04 17:52:00 +00:00
/* create pipe for SMS delivery */
2016-07-10 09:15:29 +00:00
unlink ( SMS_DELIVER ) ;
rc = mkfifo ( SMS_DELIVER , 0666 ) ;
2016-07-04 17:52:00 +00:00
if ( rc < 0 ) {
2016-07-10 09:15:29 +00:00
fprintf ( stderr , " Failed to create SMS deliver FIFO '%s'! \n " , SMS_DELIVER ) ;
2016-07-04 17:52:00 +00:00
goto fail ;
} else {
2016-07-10 09:15:29 +00:00
sms_deliver_fd = open ( SMS_DELIVER , O_RDONLY | O_NONBLOCK ) ;
if ( sms_deliver_fd < 0 ) {
fprintf ( stderr , " Failed to open SMS deliver FIFO! '%s' \n " , SMS_DELIVER ) ;
2016-07-04 17:52:00 +00:00
goto fail ;
}
}
2018-11-10 14:16:20 +00:00
/* inits */
fm_init ( fast_math ) ;
2016-07-20 05:51:28 +00:00
rc = init_frame ( ) ;
if ( rc < 0 ) {
fprintf ( stderr , " Failed to setup frames. Quitting! \n " ) ;
return - 1 ;
}
2016-02-21 17:13:30 +00:00
dsp_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-02-21 17:13:30 +00:00
/* create transceiver instance */
2016-04-25 18:20:54 +00:00
for ( i = 0 ; i < num_kanal ; i + + ) {
2021-09-18 09:43:01 +00:00
rc = nmt_create ( nmt_system , country , kanal [ i ] , chan_type [ i ] , dsp_device [ i ] , use_sdr , dsp_samplerate , rx_gain , tx_gain , do_pre_emphasis , do_de_emphasis , write_rx_wave , write_tx_wave , read_rx_wave , read_tx_wave , ms_power , traffic_area , area_no , compandor , supervisory [ i ] , smsc_number , send_callerid , loopback ) ;
2016-04-25 18:20:54 +00:00
if ( rc < 0 ) {
fprintf ( stderr , " Failed to create transceiver instance. Quitting! \n " ) ;
goto fail ;
}
2019-07-20 16:11:17 +00:00
printf ( " base station on channel %s ready, please tune transmitter to %.4f MHz and receiver to %.4f MHz. (%.3f MHz offset) \n " , kanal [ i ] , nmt_channel2freq ( nmt_system , country , atoi ( kanal [ i ] ) , 0 , NULL , NULL , NULL ) / 1e6 , nmt_channel2freq ( nmt_system , country , atoi ( kanal [ i ] ) , 1 , NULL , NULL , NULL ) / 1e6 , nmt_channel2freq ( nmt_system , country , atoi ( kanal [ i ] ) , 2 , NULL , NULL , NULL ) / 1e6 ) ;
2016-02-21 17:13:30 +00:00
}
2017-07-22 08:38:18 +00:00
nmt_check_channels ( nmt_system ) ;
2016-07-31 13:24:19 +00:00
2021-09-18 09:43:01 +00:00
main_mobile ( " nmt " , & quit , myhandler , station_id , 7 ) ;
2016-02-21 17:13:30 +00:00
fail :
2016-07-04 17:52:00 +00:00
/* fifo */
2016-07-10 09:15:29 +00:00
if ( sms_deliver_fd > 0 )
close ( sms_deliver_fd ) ;
unlink ( SMS_DELIVER ) ;
2016-07-04 17:52:00 +00:00
2016-02-21 17:13:30 +00:00
/* destroy transceiver instance */
while ( sender_head )
nmt_destroy ( sender_head ) ;
2018-11-10 14:16:20 +00:00
/* exits */
fm_exit ( ) ;
2021-01-25 14:16:29 +00:00
options_free ( ) ;
2016-02-21 17:13:30 +00:00
return 0 ;
}