Skip to content

Commit 7f57305

Browse files
committed
app_fsk: Fix code issues and style.
* Use Asterisk coding style. * Fix memory leaks.
1 parent 0078759 commit 7f57305

2 files changed

Lines changed: 96 additions & 96 deletions

File tree

apps/app_fsk.c

Lines changed: 96 additions & 91 deletions
Original file line numberDiff line numberDiff line change
@@ -1,36 +1,34 @@
11
/*
2-
* FSK util for Asterisk
3-
*
4-
* Copyright (C) 2013-2021 Alessandro Carminati <alessandro.carminati@gmail.com>
5-
*
6-
* This program is free software: you can redistribute it and/or modify
7-
* it under the terms of the GNU General Public License as published by
8-
* the Free Software Foundation, either version 3 of the License, or
9-
* (at your option) any later version.
10-
*
11-
* This program is distributed in the hope that it will be useful,
12-
* but WITHOUT ANY WARRANTY; without even the implied warranty of
13-
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14-
* GNU General Public License for more details.
15-
*
16-
* You should have received a copy of the GNU General Public License
17-
* along with this program. If not, see <http://www.gnu.org/licenses/>.
18-
*/
2+
* Asterisk -- An open source telephony toolkit.
3+
*
4+
* Copyright (C) 2013-2021 Alessandro Carminati <alessandro.carminati@gmail.com>
5+
* Copyright (C) 2026 Naveen Albert <asterisk@phreaknet.org>
6+
*
7+
* See http://www.asterisk.org for more information about
8+
* the Asterisk project. Please do not directly contact
9+
* any of the maintainers of this project for assistance;
10+
* the project provides a web site, mailing lists and IRC
11+
* channels for your use.
12+
*
13+
* This program is free software, distributed under the terms of
14+
* the GNU General Public License Version 2. See the LICENSE file
15+
* at the top of the source tree.
16+
*/
1917

2018
/*! \file
21-
*
22-
* \brief App to Send/Receive fsk audio stream (Bell103)
23-
*
24-
* \author Alessandro Carminati <alessandro.carminati@gmail.com>
25-
*
26-
* \ingroup applications
27-
*/
19+
*
20+
* \brief FSK applications
21+
*
22+
* \author Alessandro Carminati <alessandro.carminati@gmail.com>
23+
* \author Naveen Albert <asterisk@phreaknet.org>
24+
*
25+
* \ingroup applications
26+
*/
2827

2928
/*** MODULEINFO
30-
<defaultenabled>no</defaultenabled>
3129
<depend>spandsp</depend>
3230
<support_level>extended</support_level>
33-
***/
31+
***/
3432

3533
/* Needed for spandsp headers */
3634
#define ASTMM_LIBC ASTMM_IGNORE
@@ -74,16 +72,19 @@
7472
<para>Name of modem protocol to use. Default is Bell 103.</para>
7573
<enumlist>
7674
<enum name="103">
77-
<para>Bell 103 modem (300 baud), originating side</para>
75+
<para>Bell 103 modem (300 baud), originating</para>
7876
</enum>
7977
<enum name="202">
8078
<para>Bell 202 modem (1200 baud)</para>
8179
</enum>
8280
</enumlist>
8381
</parameter>
82+
<parameter name="data" required="yes">
83+
<para>The data to send</para>
84+
</parameter>
8485
</syntax>
8586
<description>
86-
<para>SendFSK() is an utility to send digital messages over an audio channel</para>
87+
<para>Sends digital messages over an audio channel</para>
8788
</description>
8889
<see-also>
8990
<ref type="application">ReceiveFSK</ref>
@@ -101,7 +102,7 @@
101102
<para>Name of modem protocol to use. Default is Bell 103.</para>
102103
<enumlist>
103104
<enum name="103">
104-
<para>Bell 103 modem (300 baud), terminating side</para>
105+
<para>Bell 103 modem (300 baud), terminating</para>
105106
</enum>
106107
<enum name="202">
107108
<para>Bell 202 modem (1200 baud)</para>
@@ -120,7 +121,7 @@
120121
</parameter>
121122
</syntax>
122123
<description>
123-
<para>ReceiveFSK() is an utility to receive digital messages from an audio channel</para>
124+
<para>Receives digital messages from an audio channel</para>
124125
<para>This application will answer the channel if it has not yet been answered.</para>
125126
</description>
126127
<see-also>
@@ -130,65 +131,58 @@
130131
***/
131132

132133
enum read_option_flags {
133-
OPT_HANGOUT = (1 << 0),
134-
OPT_SILENCE = (1 << 1),
134+
OPT_HANGOUT = (1 << 0),
135+
OPT_SILENCE = (1 << 1),
135136
};
136137

137138
AST_APP_OPTIONS(read_app_options, {
138139
AST_APP_OPTION('h', OPT_HANGOUT),
139140
AST_APP_OPTION('s', OPT_SILENCE),
140141
});
141142

142-
struct receive_buffer_s {
143+
struct receive_buffer {
143144
int ptr;
144-
int quitoncarrierlost;
145-
int FSK_eof;
145+
unsigned int quitoncarrierlost:1;
146+
unsigned int fsk_eof:1;
146147
char *buffer;
147148
};
148149

149-
struct transmit_buffer_s {
150+
struct transmit_buffer {
150151
int ptr;
151152
int bytes2send;
152153
int current_bit_no;
153154
char *buffer;
154155
};
155156

156-
typedef struct transmit_buffer_s transmit_buffer_t;
157-
typedef struct receive_buffer_s receive_buffer_t;
158-
159-
static const char app_fskTX[] = "SendFSK";
160-
static const char app_fskRX[] = "ReceiveFSK";
161-
162-
static void rx_status(void *user_data, int status){
163-
receive_buffer_t *data;
157+
static const char app_fsk_tx[] = "SendFSK";
158+
static const char app_fsk_rx[] = "ReceiveFSK";
164159

165-
data = (receive_buffer_t *) user_data;
160+
static void rx_status(void *user_data, int status)
161+
{
162+
struct receive_buffer *data = user_data;
166163
ast_debug(1, "FSK rx status is %s (%d)\n", signal_status_to_str(status), status);
167-
if ((status == -1) && (data->quitoncarrierlost)) {
168-
data->FSK_eof = 1;
164+
if (status == -1 && data->quitoncarrierlost) {
165+
data->fsk_eof = 1;
169166
}
170167
}
171168

172169
static void get_bit(void *user_data, int bit)
173170
{
174-
receive_buffer_t *data;
175-
176-
data=(receive_buffer_t *) user_data;
171+
struct receive_buffer *data = user_data;
177172
if (bit < 0) {
178173
rx_status(user_data, bit);
179174
return;
180175
}
181-
182-
ast_debug(1, "Got '%c' on the stream\n", (char) bit & 0xff);
183-
*(data->buffer + data->ptr++)=(char) bit & 0xff;
176+
ast_debug(2, "Got '%c' on the stream\n", (char) bit & 0xff);
177+
*(data->buffer + data->ptr++) = (char) bit & 0xff;
184178
}
185179

186-
static int put_bit(transmit_buffer_t *user_data)
180+
static int put_bit(struct transmit_buffer *user_data)
187181
{
188182
int8_t data;
189183

190184
if (user_data->ptr <= user_data->bytes2send) {
191-
if ( (user_data->current_bit_no != 0) && (user_data->current_bit_no != 9) ) {
185+
if ((user_data->current_bit_no != 0) && (user_data->current_bit_no != 9)) {
192186
data = *((int8_t *)user_data->buffer + user_data->ptr) & (1 << (user_data->current_bit_no - 1));
193187
} else if (user_data->current_bit_no != 9) {
194188
data = 0;
@@ -198,8 +192,6 @@ static int put_bit(transmit_buffer_t *user_data)
198192
++user_data->current_bit_no;
199193
if (user_data->current_bit_no == 10) {
200194
user_data->ptr++;
201-
}
202-
if (user_data->current_bit_no == 10) {
203195
user_data->current_bit_no = 0;
204196
}
205197
return data != 0 ? 1 : 0;
@@ -208,13 +200,13 @@ static int put_bit(transmit_buffer_t *user_data)
208200
}
209201
}
210202

211-
static int fskTX_exec(struct ast_channel *chan, const char *data) { /* SendFSK */
212-
typedef struct ast_frame ast_frame_t;
203+
static int fsk_tx_exec(struct ast_channel *chan, const char *data)
204+
{
213205
char *argcopy = NULL;
214206
fsk_tx_state_t *caller_tx;
215-
transmit_buffer_t *out;
207+
struct transmit_buffer *out;
216208
int16_t caller_amp[BLOCK_LEN];
217-
ast_frame_t *fr;
209+
struct ast_frame *fr;
218210
struct ast_frame f = {
219211
.frametype = AST_FRAME_VOICE,
220212
.src = "SendFSK",
@@ -243,7 +235,7 @@ static int fskTX_exec(struct ast_channel *chan, const char *data) { /* SendFSK *
243235
ast_log(LOG_WARNING, "SendFSK requires an argument\n");
244236
return -1;
245237
}
246-
if (chan == NULL) {
238+
if (!chan) {
247239
ast_log(LOG_ERROR, "SendFSK channel is NULL. Giving up.\n");
248240
return -1;
249241
}
@@ -265,50 +257,56 @@ static int fskTX_exec(struct ast_channel *chan, const char *data) { /* SendFSK *
265257

266258
ast_debug(1, "Modem channel is '%s'\n", preset_fsk_specs[modem].name);
267259

268-
out = (transmit_buffer_t *) ast_malloc(sizeof(*out));
269-
out->buffer = (char *) arglist.data;
260+
out = ast_malloc(sizeof(*out));
261+
if (!out) {
262+
return -1;
263+
}
264+
out->buffer = arglist.data;
270265
out->bytes2send = strlen(arglist.data);
271266
out->current_bit_no = 0;
272267
out->ptr = 0;
268+
273269
memset(caller_amp, 0, sizeof(*caller_amp));
270+
274271
caller_tx = fsk_tx_init(NULL, &preset_fsk_specs[modem], (get_bit_func_t) &put_bit, out);
275272
while (out->ptr < out->bytes2send) {
276273
res = ast_waitfor(chan, 1000);
277274
fr = ast_read(chan);
278275
if (!fr) {
279-
ast_debug(1, "Null == hangup() detected\n");
276+
ast_debug(2, "User hung up\n");
280277
res = -1;
281278
break;
282279
}
283280
if (fr->frametype == AST_FRAME_DTMF) {
284281
ast_debug(1, "User pressed a key\n");
285282
}
283+
ast_frfree(fr);
286284
samples = fsk_tx(caller_tx, caller_amp, BLOCK_LEN);
287285
if ((res = ast_write(chan, &f)) < 0) {
288286
ast_debug(1, "Failed to write %d samples\n", samples);
289287
res = -1;
290-
ast_frfree(fr);
291288
break;
292289
}
293290
}
294291
memset(caller_amp, 0, sizeof(caller_amp));
295292
res = ast_waitfor(chan, -1);
296293
fr = ast_read(chan);
297-
if (fr != NULL) {
298-
if (ast_write(chan, &f) < 0) {
299-
res = -1;
300-
ast_frfree(fr);
301-
}
302-
} else {
303-
ast_log(LOG_WARNING, "ast_read returned NULL value.\n");
294+
if (!fr) {
295+
ast_debug(2, "User hung up\n");
296+
return -1;
297+
}
298+
ast_frfree(fr);
299+
if (ast_write(chan, &f) < 0) {
300+
res = -1;
304301
}
305302
ast_debug(1, "SendFSK Completed.\n");
306303
return 0;
307304
}
308305

309-
static int fskRX_exec(struct ast_channel *chan, const char *data) { /* ReceiveFSK */
306+
static int fsk_rx_exec(struct ast_channel *chan, const char *data)
307+
{
310308
fsk_rx_state_t *caller_rx;
311-
receive_buffer_t *in;
309+
struct receive_buffer *in;
312310
char *argcopy = NULL;
313311
struct ast_frame *f;
314312
struct ast_flags flags = {0};
@@ -330,7 +328,7 @@ static int fskRX_exec(struct ast_channel *chan, const char *data) { /* ReceiveFS
330328
ast_log(LOG_WARNING, "ReceiveFSK requires at least a variable as argument\n");
331329
return -1;
332330
}
333-
if (chan == NULL) {
331+
if (!chan) {
334332
ast_log(LOG_ERROR, "ReceiveFSK channel is NULL. Giving up.\n");
335333
return -1;
336334
}
@@ -362,10 +360,12 @@ static int fskRX_exec(struct ast_channel *chan, const char *data) { /* ReceiveFS
362360
}
363361

364362
memset(output_frame, 0, sizeof(output_frame));
365-
in = (receive_buffer_t *) ast_malloc(sizeof(*in));
363+
in = ast_malloc(sizeof(*in));
364+
if (!in) {
365+
return -1;
366+
}
366367

367368
if (!ast_strlen_zero(arglist.options)) {
368-
ast_debug(1, "This instance has flags\n");
369369
ast_app_parse_options(read_app_options, &flags, NULL, arglist.options);
370370
if (ast_test_flag(&flags, OPT_HANGOUT )) {
371371
in->quitoncarrierlost = 0;
@@ -375,13 +375,16 @@ static int fskRX_exec(struct ast_channel *chan, const char *data) { /* ReceiveFS
375375
}
376376
}
377377

378-
in->FSK_eof = 0;
378+
in->fsk_eof = 0;
379379
in->quitoncarrierlost = 1;
380380

381-
in->buffer = (char *) ast_malloc(65536);
382-
memset(in->buffer, 0, 65536); /* Reserve 64KB space for receive buffer and set to 0 its pointer. */
381+
in->buffer = ast_calloc(1, 65536); /* Reserve 64KB space for receive buffer. */
382+
if (!in->buffer) {
383+
ast_free(in);
384+
return -1;
385+
}
386+
383387
in->ptr = 0;
384-
ast_debug(1, "output buffer allocated\n");
385388

386389
if (silence_flag) {
387390
silgen = ast_channel_start_silence_generator(chan);
@@ -397,8 +400,8 @@ static int fskRX_exec(struct ast_channel *chan, const char *data) { /* ReceiveFS
397400
if (f->frametype == AST_FRAME_VOICE){
398401
fsk_rx(caller_rx, f->data.ptr, f->samples);
399402
}
400-
if (in->FSK_eof != 0) {
401-
ast_log(LOG_NOTICE, "FSK_eof\n");
403+
if (in->fsk_eof != 0) {
404+
ast_debug(1, "fsk_eof\n");
402405
break;
403406
}
404407
f->subclass.format = ast_format_slin;
@@ -428,22 +431,24 @@ static int fskRX_exec(struct ast_channel *chan, const char *data) { /* ReceiveFS
428431
return 0;
429432
}
430433

431-
static int unload_module(void) {
434+
static int unload_module(void)
435+
{
432436
int res;
433437

434-
res = ast_unregister_application(app_fskTX);
435-
res |= ast_unregister_application(app_fskRX);
438+
res = ast_unregister_application(app_fsk_tx);
439+
res |= ast_unregister_application(app_fsk_rx);
436440

437441
return res;
438442
}
439443

440-
static int load_module(void) {
444+
static int load_module(void)
445+
{
441446
int res;
442447

443-
res = ast_register_application_xml(app_fskTX, fskTX_exec);
444-
res |= ast_register_application_xml(app_fskRX, fskRX_exec);
448+
res = ast_register_application_xml(app_fsk_tx, fsk_tx_exec);
449+
res |= ast_register_application_xml(app_fsk_rx, fsk_rx_exec);
445450

446451
return res;
447452
}
448453

449-
AST_MODULE_INFO_STANDARD_EXTENDED(ASTERISK_GPL_KEY, "FSK utility application");
454+
AST_MODULE_INFO_STANDARD_EXTENDED(ASTERISK_GPL_KEY, "FSK send/receive applications");

phreaknet.sh

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2850,11 +2850,6 @@ phreak_patches() {
28502850
if [ "$HAVE_COMPATIBLE_SPANDSP" = "1" ]; then
28512851
download_github_module "dgorski/app_tdd" "main" "app_tdd.c" "apps/app_tdd.c"
28522852
fi
2853-
if [ "$OS_DIST_INFO" = "FreeBSD" ]; then
2854-
sed -i '' '/defaultenabled/d' apps/app_fsk.c
2855-
else
2856-
sed -i 's/<defaultenabled>no<\/defaultenabled>//g' apps/app_fsk.c # temporary bug fix
2857-
fi
28582853

28592854
## Add patches to existing modules
28602855
phreak_tree_patch "apps/app_stack.c" "returnif.patch" # Add ReturnIf application

0 commit comments

Comments
 (0)