Use global init/cleanup function

This commit is contained in:
Andreas Eversberg 2017-01-13 07:28:31 +01:00
parent bc028cd4d3
commit 2d8d7e62c1
9 changed files with 83 additions and 101 deletions

View File

@ -282,20 +282,11 @@ int main(int argc, char *argv[])
sid_stations(sid); sid_stations(sid);
/* init functions */ /* init functions */
if (use_mncc_sock) { rc = init_common(station_id, 10);
rc = mncc_init("/tmp/bsc_mncc"); if (rc < 0)
if (rc < 0) { goto fail;
fprintf(stderr, "Failed to setup MNCC socket. Quitting!\n");
return -1;
}
}
dsp_init(); dsp_init();
init_frame(); init_frame();
rc = call_init(station_id, call_audiodev, samplerate, latency, 10, loopback);
if (rc < 0) {
fprintf(stderr, "Failed to create call control instance. Quitting!\n");
goto fail;
}
/* check for mandatory CC */ /* check for mandatory CC */
for (i = 0; i < num_kanal; i++) { for (i = 0; i < num_kanal; i++) {
@ -368,9 +359,7 @@ int main(int argc, char *argv[])
fail: fail:
/* cleanup functions */ /* cleanup functions */
call_cleanup(); cleanup_common();
if (use_mncc_sock)
mncc_exit();
/* destroy transceiver instance */ /* destroy transceiver instance */
while (sender_head) while (sender_head)

View File

@ -166,20 +166,11 @@ int main(int argc, char *argv[])
print_image(); print_image();
/* init functions */ /* init functions */
if (use_mncc_sock) { rc = init_common(station_id, 5);
rc = mncc_init("/tmp/bsc_mncc"); if (rc < 0)
if (rc < 0) { goto fail;
fprintf(stderr, "Failed to setup MNCC socket. Quitting!\n");
return -1;
}
}
dsp_init(); dsp_init();
anetz_init(); anetz_init();
rc = call_init(station_id, call_audiodev, samplerate, latency, 5, loopback);
if (rc < 0) {
fprintf(stderr, "Failed to create call control instance. Quitting!\n");
goto fail;
}
/* create transceiver instance */ /* create transceiver instance */
for (i = 0; i < num_kanal; i++) { for (i = 0; i < num_kanal; i++) {
@ -195,9 +186,7 @@ int main(int argc, char *argv[])
fail: fail:
/* cleanup functions */ /* cleanup functions */
call_cleanup(); cleanup_common();
if (use_mncc_sock)
mncc_exit();
/* destroy transceiver instance */ /* destroy transceiver instance */
while (sender_head) while (sender_head)

View File

@ -170,20 +170,11 @@ int main(int argc, char *argv[])
print_image(); print_image();
/* init functions */ /* init functions */
if (use_mncc_sock) { rc = init_common(station_id, 5);
rc = mncc_init("/tmp/bsc_mncc"); if (rc < 0)
if (rc < 0) { goto fail;
fprintf(stderr, "Failed to setup MNCC socket. Quitting!\n");
goto fail;
}
}
dsp_init(); dsp_init();
bnetz_init(); bnetz_init();
rc = call_init(station_id, call_audiodev, samplerate, latency, 5, loopback);
if (rc < 0) {
fprintf(stderr, "Failed to create call control instance. Quitting!\n");
goto fail;
}
/* SDR always requires emphasis */ /* SDR always requires emphasis */
if (!strcmp(audiodev[0], "sdr")) { if (!strcmp(audiodev[0], "sdr")) {
@ -206,9 +197,7 @@ int main(int argc, char *argv[])
fail: fail:
/* cleanup functions */ /* cleanup functions */
call_cleanup(); cleanup_common();
if (use_mncc_sock)
mncc_exit();
/* destroy transceiver instance */ /* destroy transceiver instance */
while(sender_head) while(sender_head)

View File

@ -244,13 +244,9 @@ int main(int argc, char *argv[])
print_image(); print_image();
/* init functions */ /* init functions */
if (use_mncc_sock) { rc = init_common(station_id, 7);
rc = mncc_init("/tmp/bsc_mncc"); if (rc < 0)
if (rc < 0) { goto fail;
fprintf(stderr, "Failed to setup MNCC socket. Quitting!\n");
goto fail;
}
}
scrambler_init(); scrambler_init();
init_sysinfo(); init_sysinfo();
dsp_init(); dsp_init();
@ -261,11 +257,6 @@ int main(int argc, char *argv[])
} }
init_coding(); init_coding();
cnetz_init(); cnetz_init();
rc = call_init(station_id, call_audiodev, samplerate, latency, 7, loopback);
if (rc < 0) {
fprintf(stderr, "Failed to create call control instance. Quitting!\n");
goto fail;
}
/* check for mandatory OgK */ /* check for mandatory OgK */
for (i = 0; i < num_kanal; i++) { for (i = 0; i < num_kanal; i++) {
@ -326,9 +317,7 @@ int main(int argc, char *argv[])
fail: fail:
/* cleanup functions */ /* cleanup functions */
call_cleanup(); cleanup_common();
if (use_mncc_sock)
mncc_exit();
flush_db(); flush_db();

View File

@ -34,10 +34,6 @@
#define DISC_TIMEOUT 30 #define DISC_TIMEOUT 30
extern int use_mncc_sock;
extern int send_patterns;
extern int release_on_disconnect;
/* stream patterns/announcements */ /* stream patterns/announcements */
int16_t *test_spl = NULL; int16_t *test_spl = NULL;
int16_t *ringback_spl = NULL; int16_t *ringback_spl = NULL;
@ -175,6 +171,10 @@ typedef struct call {
int test_audio_pos; /* position for test tone toward mobile */ int test_audio_pos; /* position for test tone toward mobile */
int dial_digits; /* number of digits to be dialed */ int dial_digits; /* number of digits to be dialed */
int loopback; /* loopback test for echo */ int loopback; /* loopback test for echo */
int use_mncc_sock;
int send_patterns;
int release_on_disconnect;
} call_t; } call_t;
static call_t call; static call_t call;
@ -454,20 +454,23 @@ static void process_timeout(struct timer *timer)
} }
} }
int call_init(const char *station_id, const char *audiodev, int samplerate, int latency, int dial_digits, int loopback) int call_init(const char *station_id, const char *audiodev, int samplerate, int latency, int dial_digits, int loopback, int use_mncc_sock, int send_patterns, int release_on_disconnect)
{ {
int rc = 0; int rc = 0;
init_testton(); init_testton();
if (use_mncc_sock)
return 0;
memset(&call, 0, sizeof(call)); memset(&call, 0, sizeof(call));
strncpy(call.station_id, station_id, sizeof(call.station_id) - 1); strncpy(call.station_id, station_id, sizeof(call.station_id) - 1);
call.latspl = latency * samplerate / 1000; call.latspl = latency * samplerate / 1000;
call.dial_digits = dial_digits; call.dial_digits = dial_digits;
call.loopback = loopback; call.loopback = loopback;
call.use_mncc_sock = use_mncc_sock;
call.send_patterns = send_patterns;
call.release_on_disconnect = release_on_disconnect;
if (call.use_mncc_sock)
return 0;
if (!audiodev[0]) if (!audiodev[0])
return 0; return 0;
@ -502,7 +505,7 @@ error:
void call_cleanup(void) void call_cleanup(void)
{ {
if (use_mncc_sock) if (call.use_mncc_sock)
return; return;
/* close sound devoice */ /* close sound devoice */
@ -625,7 +628,7 @@ void print_console_text(void)
* returns 1 on exit (ctrl+c) */ * returns 1 on exit (ctrl+c) */
void process_call(int c) void process_call(int c)
{ {
if (use_mncc_sock) { if (call.use_mncc_sock) {
mncc_handle(); mncc_handle();
return; return;
} }
@ -714,7 +717,7 @@ int call_in_setup(int callref, const char *callerid, const char *dialing)
if (!strcmp(dialing, "010")) if (!strcmp(dialing, "010"))
PDEBUG(DCALL, DEBUG_INFO, " -> Call to Operator '%s'\n", dialing); PDEBUG(DCALL, DEBUG_INFO, " -> Call to Operator '%s'\n", dialing);
if (use_mncc_sock) { if (call.use_mncc_sock) {
uint8_t buf[sizeof(struct gsm_mncc)]; uint8_t buf[sizeof(struct gsm_mncc)];
struct gsm_mncc *mncc = (struct gsm_mncc *)buf; struct gsm_mncc *mncc = (struct gsm_mncc *)buf;
int rc; int rc;
@ -777,11 +780,11 @@ void call_in_alerting(int callref)
PDEBUG(DCALL, DEBUG_INFO, "Call is alerting\n"); PDEBUG(DCALL, DEBUG_INFO, "Call is alerting\n");
if (use_mncc_sock) { if (call.use_mncc_sock) {
uint8_t buf[sizeof(struct gsm_mncc)]; uint8_t buf[sizeof(struct gsm_mncc)];
struct gsm_mncc *mncc = (struct gsm_mncc *)buf; struct gsm_mncc *mncc = (struct gsm_mncc *)buf;
if (!send_patterns) { if (!call.send_patterns) {
memset(buf, 0, sizeof(buf)); memset(buf, 0, sizeof(buf));
mncc->msg_type = MNCC_ALERT_IND; mncc->msg_type = MNCC_ALERT_IND;
mncc->callref = callref; mncc->callref = callref;
@ -830,7 +833,7 @@ void call_in_answer(int callref, const char *connect_id)
PDEBUG(DCALL, DEBUG_INFO, "Call has been answered by '%s'\n", connect_id); PDEBUG(DCALL, DEBUG_INFO, "Call has been answered by '%s'\n", connect_id);
if (use_mncc_sock) { if (call.use_mncc_sock) {
_indicate_answer(callref, connect_id); _indicate_answer(callref, connect_id);
set_pattern_process(callref, PATTERN_NONE); set_pattern_process(callref, PATTERN_NONE);
set_state_process(callref, CALL_CONNECT); set_state_process(callref, CALL_CONNECT);
@ -857,7 +860,7 @@ void call_in_release(int callref, int cause)
PDEBUG(DCALL, DEBUG_INFO, "Call has been released with cause=%d\n", cause); PDEBUG(DCALL, DEBUG_INFO, "Call has been released with cause=%d\n", cause);
if (use_mncc_sock) { if (call.use_mncc_sock) {
uint8_t buf[sizeof(struct gsm_mncc)]; uint8_t buf[sizeof(struct gsm_mncc)];
struct gsm_mncc *mncc = (struct gsm_mncc *)buf; struct gsm_mncc *mncc = (struct gsm_mncc *)buf;
@ -869,7 +872,7 @@ void call_in_release(int callref, int cause)
mncc->cause.value = cause; mncc->cause.value = cause;
if (is_process(callref)) { if (is_process(callref)) {
if (!send_patterns if (!call.send_patterns
|| is_process_state(callref) == CALL_DISCONNECTED || is_process_state(callref) == CALL_DISCONNECTED
|| is_process_state(callref) == CALL_SETUP_MO) { || is_process_state(callref) == CALL_SETUP_MO) {
PDEBUG(DMNCC, DEBUG_INFO, "Releasing MNCC call towards Network\n"); PDEBUG(DMNCC, DEBUG_INFO, "Releasing MNCC call towards Network\n");
@ -901,7 +904,7 @@ void call_tx_audio(int callref, int16_t *samples, int count)
if (!callref) if (!callref)
return; return;
if (use_mncc_sock) { if (call.use_mncc_sock) {
uint8_t buf[sizeof(struct gsm_data_frame) + count * sizeof(int16_t)]; uint8_t buf[sizeof(struct gsm_data_frame) + count * sizeof(int16_t)];
struct gsm_data_frame *data = (struct gsm_data_frame *)buf; struct gsm_data_frame *data = (struct gsm_data_frame *)buf;
@ -1046,7 +1049,7 @@ void call_mncc_recv(uint8_t *buf, int length)
rc = call_out_setup(callref, caller_id, caller_type, number); rc = call_out_setup(callref, caller_id, caller_type, number);
if (rc < 0) { if (rc < 0) {
PDEBUG(DCALL, DEBUG_NOTICE, "Call rejected, cause %d\n", -rc); PDEBUG(DCALL, DEBUG_NOTICE, "Call rejected, cause %d\n", -rc);
if (send_patterns) { if (call.send_patterns) {
PDEBUG(DCALL, DEBUG_DEBUG, "Early connecting after setup\n"); PDEBUG(DCALL, DEBUG_DEBUG, "Early connecting after setup\n");
_indicate_answer(callref, number); _indicate_answer(callref, number);
disconnect_process(callref, -rc); disconnect_process(callref, -rc);
@ -1064,7 +1067,7 @@ void call_mncc_recv(uint8_t *buf, int length)
break; break;
} }
if (send_patterns) { if (call.send_patterns) {
PDEBUG(DCALL, DEBUG_DEBUG, "Early connecting after setup\n"); PDEBUG(DCALL, DEBUG_DEBUG, "Early connecting after setup\n");
_indicate_answer(callref, number); _indicate_answer(callref, number);
break; break;
@ -1077,7 +1080,7 @@ void call_mncc_recv(uint8_t *buf, int length)
case MNCC_DISC_REQ: case MNCC_DISC_REQ:
PDEBUG(DMNCC, DEBUG_INFO, "Received MNCC disconnect from Network with cause %d\n", mncc->cause.value); PDEBUG(DMNCC, DEBUG_INFO, "Received MNCC disconnect from Network with cause %d\n", mncc->cause.value);
if (is_process_state(callref) == CALL_CONNECT && release_on_disconnect) { if (is_process_state(callref) == CALL_CONNECT && call.release_on_disconnect) {
PDEBUG(DCALL, DEBUG_INFO, "Releaseing, because we don't send disconnect tones to mobile phone\n"); PDEBUG(DCALL, DEBUG_INFO, "Releaseing, because we don't send disconnect tones to mobile phone\n");
PDEBUG(DMNCC, DEBUG_INFO, "Releasing MNCC call towards Network\n"); PDEBUG(DMNCC, DEBUG_INFO, "Releasing MNCC call towards Network\n");

View File

@ -9,7 +9,7 @@ enum number_type {
TYPE_INTERNATIONAL, TYPE_INTERNATIONAL,
}; };
int call_init(const char *station_id, const char *audiodev, int samplerate, int latency, int dial_digits, int loopback); int call_init(const char *station_id, const char *audiodev, int samplerate, int latency, int dial_digits, int loopback, int use_mncc_sock, int send_patterns, int release_on_disconnect);
void call_cleanup(void); void call_cleanup(void);
void process_call(int c); void process_call(int c);
void clear_console_text(void); void clear_console_text(void);

View File

@ -39,7 +39,9 @@ void opt_switch_common(int c, char *arg0, int *skip_args);
extern int quit; extern int quit;
void sighandler(int sigset); void sighandler(int sigset);
int init_common(const char *station_id, int station_id_digits);
void main_common(int *quit, int latency, int interval, void (*myhandler)(void)); void main_common(int *quit, int latency, int interval, void (*myhandler)(void));
void cleanup_common(void);
void dump_info(void); void dump_info(void);

View File

@ -32,6 +32,7 @@
#include "sender.h" #include "sender.h"
#include "timer.h" #include "timer.h"
#include "call.h" #include "call.h"
#include "mncc_sock.h"
#ifdef HAVE_SDR #ifdef HAVE_SDR
#include "sdr.h" #include "sdr.h"
#endif #endif
@ -353,6 +354,35 @@ static int get_char()
return -1; return -1;
} }
int init_common(const char *station_id, int station_id_digits)
{
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");
return rc;
}
}
/* init call device */
rc = call_init(station_id, call_audiodev, samplerate, latency, station_id_digits, loopback, use_mncc_sock, send_patterns, release_on_disconnect);
if (rc < 0) {
fprintf(stderr, "Failed to create call control instance. Quitting!\n");
return rc;
}
#ifdef HAVE_SDR
rc = sdr_init(sdr_args, sdr_rx_gain, sdr_tx_gain, write_iq_rx_wave, write_iq_tx_wave, read_iq_rx_wave);
if (rc < 0)
return rc;
#endif
return 0;
}
/* Loop through all transceiver instances of one network. */ /* Loop through all transceiver instances of one network. */
void main_common(int *quit, int latency, int interval, void (*myhandler)(void)) void main_common(int *quit, int latency, int interval, void (*myhandler)(void))
{ {
@ -362,11 +392,6 @@ void main_common(int *quit, int latency, int interval, void (*myhandler)(void))
struct termios term, term_orig; struct termios term, term_orig;
int c; int c;
#ifdef HAVE_SDR
if (sdr_init(sdr_args, sdr_rx_gain, sdr_tx_gain))
return;
#endif
/* open audio */ /* open audio */
if (sender_open_audio()) if (sender_open_audio())
return; return;
@ -477,3 +502,10 @@ next_char:
} }
} }
void cleanup_common(void)
{
call_cleanup();
if (use_mncc_sock)
mncc_exit();
}

View File

@ -347,24 +347,15 @@ int main(int argc, char *argv[])
print_image(); print_image();
/* init functions */ /* init functions */
if (use_mncc_sock) { rc = init_common(station_id, 7);
rc = mncc_init("/tmp/bsc_mncc"); if (rc < 0)
if (rc < 0) { goto fail;
fprintf(stderr, "Failed to setup MNCC socket. Quitting!\n");
return -1;
}
}
rc = init_frame(); rc = init_frame();
if (rc < 0) { if (rc < 0) {
fprintf(stderr, "Failed to setup frames. Quitting!\n"); fprintf(stderr, "Failed to setup frames. Quitting!\n");
return -1; return -1;
} }
dsp_init(); dsp_init();
rc = call_init(station_id, call_audiodev, samplerate, latency, 7, loopback);
if (rc < 0) {
fprintf(stderr, "Failed to create call control instance. Quitting!\n");
goto fail;
}
/* SDR always requires emphasis */ /* SDR always requires emphasis */
if (!strcmp(audiodev[0], "sdr")) { if (!strcmp(audiodev[0], "sdr")) {
@ -397,9 +388,7 @@ fail:
unlink(SMS_DELIVER); unlink(SMS_DELIVER);
/* cleanup functions */ /* cleanup functions */
call_cleanup(); cleanup_common();
if (use_mncc_sock)
mncc_exit();
/* destroy transceiver instance */ /* destroy transceiver instance */
while (sender_head) while (sender_head)