2 * Asterisk -- An open source telephony toolkit.
4 * Copyright (C) 1999 - 2005, Digium, Inc.
5 * Copyright (C) 2007, Jim Dixon
7 * Jim Dixon, WB6NIL <jim@lambdatel.com>
8 * Steve Henke, W9SH <w9sh@arrl.net>
9 * Based upon work by Mark Spencer <markster@digium.com> and Luigi Rizzo
11 * See http://www.asterisk.org for more information about
12 * the Asterisk project. Please do not directly contact
13 * any of the maintainers of this project for assistance;
14 * the project provides a web site, mailing lists and IRC
15 * channels for your use.
17 * This program is free software, distributed under the terms of
18 * the GNU General Public License Version 2. See the LICENSE file
19 * at the top of the source tree.
24 * \brief Channel driver for CM108 USB Cards with Radio Interface
26 * \author Jim Dixon <jim@lambdatel.com>
27 * \author Steve Henke <w9sh@arrl.net>
30 * \arg \ref Config_usbradio
32 * \ingroup channel_drivers
36 <depend>asound</depend>
38 <defaultenabled>no</defaultenabled>
43 ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
47 #include <sys/ioctl.h>
51 #include <alsa/asoundlib.h>
53 #define CHAN_USBRADIO 1
55 #define DEBUG_USBRADIO 0
56 #define DEBUG_CAPTURES 1
58 #define DEBUG_CAP_RX_OUT 0
59 #define DEBUG_CAP_TX_OUT 0
61 #define DEBUG_FILETEST 0
63 #define RX_CAP_RAW_FILE "/tmp/rx_cap_in.pcm"
64 #define RX_CAP_TRACE_FILE "/tmp/rx_trace.pcm"
65 #define RX_CAP_OUT_FILE "/tmp/rx_cap_out.pcm"
67 #define TX_CAP_RAW_FILE "/tmp/tx_cap_in.pcm"
68 #define TX_CAP_TRACE_FILE "/tmp/tx_trace.pcm"
69 #define TX_CAP_OUT_FILE "/tmp/tx_cap_out.pcm"
71 #define MIXER_PARAM_MIC_PLAYBACK_SW "Mic Playback Switch"
72 #define MIXER_PARAM_MIC_PLAYBACK_VOL "Mic Playback Volume"
73 #define MIXER_PARAM_MIC_CAPTURE_SW "Mic Capture Switch"
74 #define MIXER_PARAM_MIC_CAPTURE_VOL "Mic Capture Volume"
75 #define MIXER_PARAM_MIC_BOOST "Auto Gain Control"
76 #define MIXER_PARAM_SPKR_PLAYBACK_SW "Speaker Playback Switch"
77 #define MIXER_PARAM_SPKR_PLAYBACK_VOL "Speaker Playback Volume"
79 #include "./xpmr/xpmr.h"
82 #define traceusb1(a, ...) ast_debug(4, a __VA_ARGS__)
84 #define traceusb1(a, ...)
88 #define traceusb2(a, ...) ast_debug(4, a __VA_ARGS__)
90 #define traceusb2(a, ...)
94 #include <linux/soundcard.h>
95 #elif defined(__FreeBSD__)
96 #include <sys/soundcard.h>
98 #include <soundcard.h>
101 #include "asterisk/lock.h"
102 #include "asterisk/frame.h"
103 #include "asterisk/callerid.h"
104 #include "asterisk/channel.h"
105 #include "asterisk/module.h"
106 #include "asterisk/pbx.h"
107 #include "asterisk/config.h"
108 #include "asterisk/cli.h"
109 #include "asterisk/utils.h"
110 #include "asterisk/causes.h"
111 #include "asterisk/endian.h"
112 #include "asterisk/stringfields.h"
113 #include "asterisk/abstract_jb.h"
114 #include "asterisk/musiconhold.h"
115 #include "asterisk/dsp.h"
117 #define C108_VENDOR_ID 0x0d8c
118 #define C108_PRODUCT_ID 0x000c
119 #define C108_HID_INTERFACE 3
121 #define HID_REPORT_GET 0x01
122 #define HID_REPORT_SET 0x09
124 #define HID_RT_INPUT 0x01
125 #define HID_RT_OUTPUT 0x02
127 /*! Global jitterbuffer configuration - by default, jb is disabled */
128 static struct ast_jb_conf default_jbconf =
132 .resync_threshold = -1,
135 static struct ast_jb_conf global_jbconf;
138 * usbradio.conf parameters are
142 ; General config options, with default values shown.
143 ; You should use one section per device, with [general] being used
147 ; debug = 0x0 ; misc debug flags, default is 0
149 ; Set the device to use for I/O
151 ; Set hardware type here
152 ; hdwtype=0 ; 0=limey, 1=sph
154 ; rxboostset=0 ; no rx gain boost
155 ; rxctcssrelax=1 ; reduce talkoff from radios w/o CTCSS Tx HPF
156 ; rxctcssfreq=100.0 ; rx ctcss freq in floating point. must be in table
157 ; txctcssfreq=100.0 ; tx ctcss freq, any frequency permitted
159 ; carrierfrom=dsp ;no,usb,usbinvert,dsp,vox
160 ; ctcssfrom=dsp ;no,usb,dsp
162 ; rxdemod=flat ; input type from radio: no,speaker,flat
163 ; txprelim=yes ; output is pre-emphasised and limited
164 ; txtoctype=no ; no,phase,notone
166 ; txmixa=composite ;no,voice,tone,composite,auxvoice
167 ; txmixb=no ;no,voice,tone,composite,auxvoice
171 ;------------------------------ JITTER BUFFER CONFIGURATION --------------------------
172 ; jbenable = yes ; Enables the use of a jitterbuffer on the receiving side of an
173 ; USBRADIO channel. Defaults to "no". An enabled jitterbuffer will
174 ; be used only if the sending side can create and the receiving
175 ; side can not accept jitter. The USBRADIO channel can't accept jitter,
176 ; thus an enabled jitterbuffer on the receive USBRADIO side will always
177 ; be used if the sending side can create jitter.
179 ; jbmaxsize = 200 ; Max length of the jitterbuffer in milliseconds.
181 ; jbresyncthreshold = 1000 ; Jump in the frame timestamps over which the jitterbuffer is
182 ; resynchronized. Useful to improve the quality of the voice, with
183 ; big jumps in/broken timestamps, usualy sent from exotic devices
184 ; and programs. Defaults to 1000.
186 ; jbimpl = fixed ; Jitterbuffer implementation, used on the receiving side of an USBRADIO
187 ; channel. Two implementations are currenlty available - "fixed"
188 ; (with size always equals to jbmax-size) and "adaptive" (with
189 ; variable size, actually the new jb of IAX2). Defaults to fixed.
191 ; jblog = no ; Enables jitterbuffer frame logging. Defaults to "no".
192 ;-----------------------------------------------------------------------------------
200 * The following parameters are used in the driver:
202 * FRAME_SIZE the size of an audio frame, in samples.
203 * 160 is used almost universally, so you should not change it.
205 * FRAGS the argument for the SETFRAGMENT ioctl.
206 * Overridden by the 'frags' parameter in usbradio.conf
208 * Bits 0-7 are the base-2 log of the device's block size,
209 * bits 16-31 are the number of blocks in the driver's queue.
210 * There are a lot of differences in the way this parameter
211 * is supported by different drivers, so you may need to
212 * experiment a bit with the value.
213 * A good default for linux is 30 blocks of 64 bytes, which
214 * results in 6 frames of 320 bytes (160 samples).
215 * FreeBSD works decently with blocks of 256 or 512 bytes,
216 * leaving the number unspecified.
217 * Note that this only refers to the device buffer size,
218 * this module will then try to keep the lenght of audio
219 * buffered within small constraints.
221 * QUEUE_SIZE The max number of blocks actually allowed in the device
222 * driver's buffer, irrespective of the available number.
223 * Overridden by the 'queuesize' parameter in usbradio.conf
225 * Should be >=2, and at most as large as the hw queue above
226 * (otherwise it will never be full).
229 #define FRAME_SIZE 160
230 #define QUEUE_SIZE 20
232 #if defined(__FreeBSD__)
235 #define FRAGS ( ( (6 * 5) << 16 ) | 0xc )
239 * XXX text message sizes are probably 256 chars, but i am
240 * not sure if there is a suitable definition anywhere.
242 #define TEXT_SIZE 256
245 #define TRYOPEN 1 /* try to open on startup */
247 #define O_CLOSE 0x444 /* special 'close' mode for device */
248 /* Which device to use */
249 #if defined( __OpenBSD__ ) || defined( __NetBSD__ )
250 #define DEV_DSP "/dev/audio"
252 #define DEV_DSP "/dev/dsp"
256 #define MIN(a,b) ((a) < (b) ? (a) : (b))
259 #define MAX(a,b) ((a) > (b) ? (a) : (b))
262 static char *config = "usbradio.conf"; /* default config file */
263 static char *config1 = "usbradio_tune.conf"; /* tune config file */
265 static FILE *frxcapraw = NULL, *frxcaptrace = NULL, *frxoutraw = NULL;
266 static FILE *ftxcapraw = NULL, *ftxcaptrace = NULL, *ftxoutraw = NULL;
268 static int usbradio_debug;
269 #if 0 /* maw asdf sph */
270 static int usbradio_debug_level = 0;
273 enum {RX_AUDIO_NONE,RX_AUDIO_SPEAKER,RX_AUDIO_FLAT};
274 enum {CD_IGNORE,CD_XPMR_NOISE,CD_XPMR_VOX,CD_HID,CD_HID_INVERT};
275 enum {SD_IGNORE,SD_HID,SD_HID_INVERT,SD_XPMR}; /* no,external,externalinvert,software */
276 enum {RX_KEY_CARRIER,RX_KEY_CARRIER_CODE};
277 enum {TX_OUT_OFF,TX_OUT_VOICE,TX_OUT_LSD,TX_OUT_COMPOSITE,TX_OUT_AUX};
278 enum {TOC_NONE,TOC_PHASE,TOC_NOTONE};
280 /* DECLARE STRUCTURES */
283 * descriptor for one of our channels.
284 * There is one used for 'default' values (from the [general] entry in
285 * the configuration file), and then one instance for each device
286 * (the default is cloned from [general], others are only created
287 * if the relevant section exists).
289 struct chan_usbradio_pvt {
290 struct chan_usbradio_pvt *next;
294 int total_blocks; /* total blocks in the output device */
296 enum { M_UNSET, M_FULL, M_READ, M_WRITE } duplex;
301 unsigned int queuesize; /* max fragments in queue */
302 unsigned int frags; /* parameter for SETFRAGMENT */
304 int warned; /* various flags used for warnings */
305 #define WARN_used_blocks 1
308 int w_errors; /* overfull in the write path */
309 struct timeval lastopen;
314 /* boost support. BOOST_SCALE * 10 ^(BOOST_MAX/20) must
315 * be representable in 16 bits to avoid overflows.
317 #define BOOST_SCALE (1<<9)
318 #define BOOST_MAX 40 /* slightly less than 7 bits */
319 int boost; /* input boost, scaled by BOOST_SCALE */
328 struct ast_channel *owner;
329 char ext[AST_MAX_EXTENSION];
330 char ctx[AST_MAX_CONTEXT];
331 char language[MAX_LANGUAGE];
332 char cid_name[256]; /* XXX */
333 char cid_num[256]; /* XXX */
334 char mohinterpret[MAX_MUSICCLASS];
336 /* buffers used in usbradio_write, 2 per int by 2 channels by 6 times oversampling (48KS/s) */
337 char usbradio_write_buf[FRAME_SIZE * 2 * 2 * 6];
338 char usbradio_write_buf_1[FRAME_SIZE * 2 * 2 * 6];
340 int usbradio_write_dst;
341 /* buffers used in usbradio_read - AST_FRIENDLY_OFFSET space for headers
342 * plus enough room for a full frame
344 char usbradio_read_buf[FRAME_SIZE * (2 * 12) + AST_FRIENDLY_OFFSET];
345 char usbradio_read_buf_8k[FRAME_SIZE * 2 + AST_FRIENDLY_OFFSET];
346 int readpos; /* read position above */
347 struct ast_frame read_f; /* returned by usbradio_read */
355 char rxcarrierdetect; /*!< status from pmr channel */
356 char rxctcssdecode; /*!< status from pmr channel */
359 char rxkeyed; /*!< indicates rx signal present */
362 char txkeyed; /*! tx key request from upper layers */
378 int rxsquelchadj; /*!< this copy needs to be here for initialization */
403 int hid_gpio_ctl_loc;
407 int hid_io_ctcss_loc;
419 /* maw add additional defaults !!! */
420 static struct chan_usbradio_pvt usbradio_default = {
422 .duplex = M_UNSET, /* XXX check this */
425 .queuesize = QUEUE_SIZE,
429 .readpos = AST_FRIENDLY_OFFSET, /* start here on reads */
430 .lastopen = { 0, 0 },
431 .boost = BOOST_SCALE,
434 /* DECLARE FUNCTION PROTOTYPES */
436 static void store_txtoctype(struct chan_usbradio_pvt *o, const char *s);
437 static int hidhdwconfig(struct chan_usbradio_pvt *o);
438 static int set_txctcss_level(struct chan_usbradio_pvt *o);
439 static void pmrdump(struct chan_usbradio_pvt *o);
440 static void mult_set(struct chan_usbradio_pvt *o);
441 static int mult_calc(int value);
442 static void mixer_write(struct chan_usbradio_pvt *o);
443 static void tune_rxinput(struct chan_usbradio_pvt *o);
444 static void tune_rxvoice(struct chan_usbradio_pvt *o);
445 static void tune_rxctcss(struct chan_usbradio_pvt *o);
446 static void tune_txoutput(struct chan_usbradio_pvt *o, int value);
447 static void tune_write(struct chan_usbradio_pvt *o);
449 static char *usbradio_active; /* the active device */
451 static int setformat(struct chan_usbradio_pvt *o, int mode);
453 static struct ast_channel *usbradio_request(const char *type, int format, void *data
455 static int usbradio_digit_begin(struct ast_channel *c, char digit);
456 static int usbradio_digit_end(struct ast_channel *c, char digit, unsigned int duration);
457 static int usbradio_text(struct ast_channel *c, const char *text);
458 static int usbradio_hangup(struct ast_channel *c);
459 static int usbradio_answer(struct ast_channel *c);
460 static struct ast_frame *usbradio_read(struct ast_channel *chan);
461 static int usbradio_call(struct ast_channel *c, char *dest, int timeout);
462 static int usbradio_write(struct ast_channel *chan, struct ast_frame *f);
463 static int usbradio_indicate(struct ast_channel *chan, int cond, const void *data, size_t datalen);
464 static int usbradio_fixup(struct ast_channel *oldchan, struct ast_channel *newchan);
466 #if DEBUG_FILETEST == 1
467 static int RxTestIt(struct chan_usbradio_pvt *o);
470 static char tdesc[] = "USB (CM108) Radio Channel Driver";
472 static const struct ast_channel_tech usbradio_tech = {
474 .description = tdesc,
475 .capabilities = AST_FORMAT_SLINEAR,
476 .requester = usbradio_request,
477 .send_digit_begin = usbradio_digit_begin,
478 .send_digit_end = usbradio_digit_end,
479 .send_text = usbradio_text,
480 .hangup = usbradio_hangup,
481 .answer = usbradio_answer,
482 .read = usbradio_read,
483 .call = usbradio_call,
484 .write = usbradio_write,
485 .indicate = usbradio_indicate,
486 .fixup = usbradio_fixup,
489 /* Call with: devnum: alsa major device number, param: ascii Formal
490 Parameter Name, val1, first or only value, val2 second value, or 0
491 if only 1 value. Values: 0-99 (percent) or 0-1 for baboon.
493 Note: must add -lasound to end of linkage */
495 static int amixer_max(int devnum,char *param)
500 snd_ctl_elem_id_t *id;
501 snd_hctl_elem_t *elem;
502 snd_ctl_elem_info_t *info;
504 snprintf(str, sizeof(str), "hw:%d", devnum);
505 if (snd_hctl_open(&hctl, str, 0))
508 id = alloca(snd_ctl_elem_id_sizeof());
509 memset(id, 0, snd_ctl_elem_id_sizeof());
510 snd_ctl_elem_id_set_interface(id, SND_CTL_ELEM_IFACE_MIXER);
511 snd_ctl_elem_id_set_name(id, param);
512 elem = snd_hctl_find_elem(hctl, id);
514 snd_hctl_close(hctl);
517 info = alloca(snd_ctl_elem_info_sizeof());
518 memset(info, 0, snd_ctl_elem_info_sizeof());
519 snd_hctl_elem_info(elem,info);
520 type = snd_ctl_elem_info_get_type(info);
523 case SND_CTL_ELEM_TYPE_INTEGER:
524 rv = snd_ctl_elem_info_get_max(info);
526 case SND_CTL_ELEM_TYPE_BOOLEAN:
530 snd_hctl_close(hctl);
534 /*! \brief Call with: devnum: alsa major device number, param: ascii Formal
535 Parameter Name, val1, first or only value, val2 second value, or 0
536 if only 1 value. Values: 0-99 (percent) or 0-1 for baboon.
538 Note: must add -lasound to end of linkage */
540 static int setamixer(int devnum, char *param, int v1, int v2)
545 snd_ctl_elem_id_t *id;
546 snd_ctl_elem_value_t *control;
547 snd_hctl_elem_t *elem;
548 snd_ctl_elem_info_t *info;
550 snprintf(str, sizeof(str), "hw:%d", devnum);
551 if (snd_hctl_open(&hctl, str, 0))
554 id = alloca(snd_ctl_elem_id_sizeof());
555 memset(id, 0, snd_ctl_elem_id_sizeof());
556 snd_ctl_elem_id_set_interface(id, SND_CTL_ELEM_IFACE_MIXER);
557 snd_ctl_elem_id_set_name(id, param);
558 elem = snd_hctl_find_elem(hctl, id);
560 snd_hctl_close(hctl);
563 info = alloca(snd_ctl_elem_info_sizeof());
564 memset(info, 0, snd_ctl_elem_info_sizeof());
565 snd_hctl_elem_info(elem,info);
566 type = snd_ctl_elem_info_get_type(info);
567 control = alloca(snd_ctl_elem_value_sizeof());
568 memset(control, 0, snd_ctl_elem_value_sizeof());
569 snd_ctl_elem_value_set_id(control, id);
571 case SND_CTL_ELEM_TYPE_INTEGER:
572 snd_ctl_elem_value_set_integer(control, 0, v1);
573 if (v2 > 0) snd_ctl_elem_value_set_integer(control, 1, v2);
575 case SND_CTL_ELEM_TYPE_BOOLEAN:
576 snd_ctl_elem_value_set_integer(control, 0, (v1 != 0));
579 if (snd_hctl_elem_write(elem, control)) {
580 snd_hctl_close(hctl);
583 snd_hctl_close(hctl);
587 static void hid_set_outputs(struct usb_dev_handle *handle,
588 unsigned char *outputs)
590 usb_control_msg(handle,
591 USB_ENDPOINT_OUT + USB_TYPE_CLASS + USB_RECIP_INTERFACE,
593 0 + (HID_RT_OUTPUT << 8),
595 (char *)outputs, 4, 5000);
598 static void hid_get_inputs(struct usb_dev_handle *handle,
599 unsigned char *inputs)
601 usb_control_msg(handle,
602 USB_ENDPOINT_IN + USB_TYPE_CLASS + USB_RECIP_INTERFACE,
604 0 + (HID_RT_INPUT << 8),
606 (char *)inputs, 4, 5000);
609 static struct usb_device *hid_device_init(void)
611 struct usb_bus *usb_bus;
612 struct usb_device *dev;
617 for (usb_bus = usb_busses; usb_bus; usb_bus = usb_bus->next) {
618 for (dev = usb_bus->devices; dev; dev = dev->next) {
619 if ((dev->descriptor.idVendor == C108_VENDOR_ID) && (dev->descriptor.idProduct == C108_PRODUCT_ID))
626 static int hidhdwconfig(struct chan_usbradio_pvt *o)
628 if (o->hdwtype == 1) { /*sphusb */
629 o->hid_gpio_ctl = 0x08; /* set GPIO4 to output mode */
630 o->hid_gpio_ctl_loc = 2; /* For CTL of GPIO */
631 o->hid_io_cor = 4; /* GPIO3 is COR */
632 o->hid_io_cor_loc = 1; /* GPIO3 is COR */
633 o->hid_io_ctcss = 2; /* GPIO 2 is External CTCSS */
634 o->hid_io_ctcss_loc = 1; /* is GPIO 2 */
635 o->hid_io_ptt = 8; /* GPIO 4 is PTT */
636 o->hid_gpio_loc = 1; /* For ALL GPIO */
637 } else if (o->hdwtype == 0) { /* dudeusb */
638 o->hid_gpio_ctl = 0x0c;/* set GPIO 3 & 4 to output mode */
639 o->hid_gpio_ctl_loc = 2; /* For CTL of GPIO */
640 o->hid_io_cor = 2; /* VOLD DN is COR */
641 o->hid_io_cor_loc = 0; /* VOL DN COR */
642 o->hid_io_ctcss = 2; /* GPIO 2 is External CTCSS */
643 o->hid_io_ctcss_loc = 1; /* is GPIO 2 */
644 o->hid_io_ptt = 4; /* GPIO 3 is PTT */
645 o->hid_gpio_loc = 1; /* For ALL GPIO */
646 } else if (o->hdwtype == 3) { /* custom version */
647 o->hid_gpio_ctl = 0x0c; /* set GPIO 3 & 4 to output mode */
648 o->hid_gpio_ctl_loc = 2; /* For CTL of GPIO */
649 o->hid_io_cor = 2; /* VOLD DN is COR */
650 o->hid_io_cor_loc = 0; /* VOL DN COR */
651 o->hid_io_ctcss = 2; /* GPIO 2 is External CTCSS */
652 o->hid_io_ctcss_loc = 1; /* is GPIO 2 */
653 o->hid_io_ptt = 4; /* GPIO 3 is PTT */
654 o->hid_gpio_loc = 1; /* For ALL GPIO */
661 static void *hidthread(void *arg)
663 unsigned char buf[4], keyed;
665 struct usb_device *usb_dev;
666 struct usb_dev_handle *usb_handle;
667 struct chan_usbradio_pvt *o = arg;
669 usb_dev = hid_device_init();
670 if (usb_dev == NULL) {
671 ast_log(LOG_ERROR, "USB HID device not found\n");
674 usb_handle = usb_open(usb_dev);
675 if (usb_handle == NULL) {
676 ast_log(LOG_ERROR, "Not able to open USB device\n");
679 if (usb_claim_interface(usb_handle, C108_HID_INTERFACE) < 0) {
680 if (usb_detach_kernel_driver_np(usb_handle, C108_HID_INTERFACE) < 0) {
681 ast_log(LOG_ERROR, "Not able to detach the USB device\n");
684 if (usb_claim_interface(usb_handle, C108_HID_INTERFACE) < 0) {
685 ast_log(LOG_ERROR, "Not able to claim the USB device\n");
689 memset(buf, 0, sizeof(buf));
690 buf[2] = o->hid_gpio_ctl;
692 hid_set_outputs(usb_handle, buf);
693 traceusb1("hidthread: Starting normally!!\n");
695 while (!o->stophid) {
696 buf[o->hid_gpio_ctl_loc] = o->hid_gpio_ctl;
697 hid_get_inputs(usb_handle, buf);
698 keyed = !(buf[o->hid_io_cor_loc] & o->hid_io_cor);
699 if (keyed != o->rxhidsq) {
701 ast_log(LOG_NOTICE, "chan_usbradio() hidthread: update rxhidsq = %d\n", keyed);
705 /* if change in tx stuff */
707 if (o->txkeyed || o->txchankey || o->txtestkey || o->pmrChan->txPttOut)
710 if (o->lasttx != txtmp) {
713 ast_log(LOG_NOTICE, "hidthread: tx set to %d\n", txtmp);
714 buf[o->hid_gpio_loc] = 0;
716 buf[o->hid_gpio_loc] = o->hid_io_ptt;
717 buf[o->hid_gpio_ctl_loc] = o->hid_gpio_ctl;
718 hid_set_outputs(usb_handle, buf);
721 time(&o->lasthidtime);
724 buf[o->hid_gpio_loc] = 0;
726 buf[o->hid_gpio_loc] = o->hid_io_ptt;
727 buf[o->hid_gpio_ctl_loc] = o->hid_gpio_ctl;
728 hid_set_outputs(usb_handle, buf);
733 * returns a pointer to the descriptor with the given name
735 static struct chan_usbradio_pvt *find_desc(char *dev)
737 struct chan_usbradio_pvt *o = NULL;
740 ast_log(LOG_WARNING, "null dev\n");
742 for (o = usbradio_default.next; o && o->name && dev && strcmp(o->name, dev) != 0; o = o->next);
745 ast_log(LOG_WARNING, "could not find <%s>\n", dev ? dev : "--no-device--");
751 * split a string in extension-context, returns pointers to malloc'ed
753 * If we do not have 'overridecontext' then the last @ is considered as
754 * a context separator, and the context is overridden.
755 * This is usually not very necessary as you can play with the dialplan,
756 * and it is nice not to need it because you have '@' in SIP addresses.
757 * Return value is the buffer address.
760 static char *ast_ext_ctx(const char *src, char **ext, char **ctx)
762 struct chan_usbradio_pvt *o = find_desc(usbradio_active);
764 if (ext == NULL || ctx == NULL)
765 return NULL; /* error */
769 if (src && *src != '\0')
770 *ext = ast_strdup(src);
775 if (!o->overridecontext) {
776 /* parse from the right */
777 *ctx = strrchr(*ext, '@');
787 * Returns the number of blocks used in the audio output channel
789 static int used_blocks(struct chan_usbradio_pvt *o)
791 struct audio_buf_info info;
793 if (ioctl(o->sounddev, SNDCTL_DSP_GETOSPACE, &info)) {
794 if (!(o->warned & WARN_used_blocks)) {
795 ast_log(LOG_WARNING, "Error reading output space\n");
796 o->warned |= WARN_used_blocks;
801 if (o->total_blocks == 0) {
802 ast_debug(4, "fragtotal %d size %d avail %d\n", info.fragstotal, info.fragsize, info.fragments);
803 o->total_blocks = info.fragments;
806 return o->total_blocks - info.fragments;
809 /*! \brief Write an exactly FRAME_SIZE sized frame */
810 static int soundcard_writeframe(struct chan_usbradio_pvt *o, short *data)
815 setformat(o, O_RDWR);
817 return 0; /* not fatal */
819 * Nothing complex to manage the audio device queue.
820 * If the buffer is full just drop the extra, otherwise write.
821 * XXX in some cases it might be useful to write anyways after
822 * a number of failures, to restart the output chain.
824 res = used_blocks(o);
825 if (res > o->queuesize) { /* no room to write a block */
826 if (o->w_errors++ == 0 && (usbradio_debug & 0x4))
827 ast_log(LOG_WARNING, "write: used %d blocks (%d)\n", res, o->w_errors);
832 return write(o->sounddev, ((void *) data), FRAME_SIZE * 2 * 12);
836 * reset and close the device if opened,
837 * then open and initialize it in the desired mode,
838 * trigger reads and writes so we can start using it.
840 static int setformat(struct chan_usbradio_pvt *o, int mode)
842 int fmt, desired, res, fd;
845 if (o->sounddev >= 0) {
846 ioctl(o->sounddev, SNDCTL_DSP_RESET, 0);
851 if (mode == O_CLOSE) /* we are done */
853 if (ast_tvdiff_ms(ast_tvnow(), o->lastopen) < 1000)
854 return -1; /* don't open too often */
855 o->lastopen = ast_tvnow();
856 strcpy(device, "/dev/dsp");
858 snprintf(device + strlen("/dev/dsp"), sizeof(device) - strlen("/dev/dsp"), "%d", o->devicenum);
859 fd = o->sounddev = open(device, mode | O_NONBLOCK);
861 ast_log(LOG_WARNING, "Unable to re-open DSP device %d: %s\n", o->devicenum, strerror(errno));
865 o->owner->fds[0] = fd;
867 #if __BYTE_ORDER == __LITTLE_ENDIAN
872 res = ioctl(fd, SNDCTL_DSP_SETFMT, &fmt);
874 ast_log(LOG_WARNING, "Unable to set format to 16-bit signed\n");
879 res = ioctl(fd, SNDCTL_DSP_SETDUPLEX, 0);
880 /* Check to see if duplex set (FreeBSD Bug) */
881 res = ioctl(fd, SNDCTL_DSP_GETCAPS, &fmt);
882 if (res == 0 && (fmt & DSP_CAP_DUPLEX)) {
883 ast_verb(2, "Console is full duplex\n");
896 res = ioctl(fd, SNDCTL_DSP_STEREO, &fmt);
898 ast_log(LOG_WARNING, "Failed to set audio device to mono\n");
901 fmt = desired = 48000; /* 8000 Hz desired */
902 res = ioctl(fd, SNDCTL_DSP_SPEED, &fmt);
905 ast_log(LOG_WARNING, "Failed to set audio device to mono\n");
908 if (fmt != desired) {
909 if (!(o->warned & WARN_speed)) {
911 "Requested %d Hz, got %d Hz -- sound may be choppy\n",
913 o->warned |= WARN_speed;
917 * on Freebsd, SETFRAGMENT does not work very well on some cards.
918 * Default to use 256 bytes, let the user override
922 res = ioctl(fd, SNDCTL_DSP_SETFRAGMENT, &fmt);
924 if (!(o->warned & WARN_frag)) {
926 "Unable to set fragment size -- sound may be choppy\n");
927 o->warned |= WARN_frag;
931 /* on some cards, we need SNDCTL_DSP_SETTRIGGER to start outputting */
932 res = PCM_ENABLE_INPUT | PCM_ENABLE_OUTPUT;
933 res = ioctl(fd, SNDCTL_DSP_SETTRIGGER, &res);
934 /* it may fail if we are in half duplex, never mind */
939 * some of the standard methods supported by channels.
941 static int usbradio_digit_begin(struct ast_channel *c, char digit)
946 static int usbradio_digit_end(struct ast_channel *c, char digit, unsigned int duration)
948 /* no better use for received digits than print them */
949 ast_verb(0, " << Console Received digit %c of duration %u ms >> \n",
954 static int usbradio_text(struct ast_channel *c, const char *text)
956 /* print received messages */
957 ast_verb(0, " << Console Received text %s >> \n", text);
962 * handler for incoming calls. Either autoanswer, or start ringing
964 static int usbradio_call(struct ast_channel *c, char *dest, int timeout)
966 struct chan_usbradio_pvt *o = c->tech_pvt;
968 time(&o->lasthidtime);
969 ast_pthread_create_background(&o->hidthread, NULL, hidthread, o);
970 ast_setstate(c, AST_STATE_UP);
975 * remote side answered the phone
977 static int usbradio_answer(struct ast_channel *c)
979 ast_setstate(c, AST_STATE_UP);
984 static int usbradio_hangup(struct ast_channel *c)
986 struct chan_usbradio_pvt *o = c->tech_pvt;
990 ast_module_unref(ast_module_info->self);
992 if (o->autoanswer || o->autohangup) {
993 /* Assume auto-hangup too */
995 setformat(o, O_CLOSE);
999 pthread_join(o->hidthread, NULL);
1004 /* used for data coming from the network */
1005 static int usbradio_write(struct ast_channel *c, struct ast_frame *f)
1008 struct chan_usbradio_pvt *o = c->tech_pvt;
1010 traceusb2("usbradio_write() o->nosound=%d\n", o->nosound); /*sph maw asdf */
1013 * we could receive a block which is not a multiple of our
1014 * FRAME_SIZE, so buffer it locally and write to the device
1015 * in FRAME_SIZE chunks.
1016 * Keep the residue stored for future use.
1019 if (o->txkeyed || o->txtestkey)
1020 o->pmrChan->txPttIn = 1;
1022 o->pmrChan->txPttIn = 0;
1024 #if DEBUG_CAPTURES == 1 /* to write input data to a file datalen=320 */
1025 if (ftxcapraw && o->b.txcapraw) {
1026 i16 i, tbuff[f->datalen];
1027 for (i = 0; i < f->datalen; i += 2) {
1028 tbuff[i] = ((i16 *)(f->data))[i / 2];
1029 tbuff[i + 1] = o->txkeyed * M_Q13;
1031 fwrite(tbuff, 2, f->datalen, ftxcapraw);
1032 /*fwrite(f->data,1,f->datalen,ftxcapraw); */
1036 PmrTx(o->pmrChan,(i16*)f->data,(i16*)o->usbradio_write_buf_1);
1038 #if 0 /* to write 48KS/s stereo data to a file */
1039 if (!ftxoutraw) ftxoutraw = fopen(TX_CAP_OUT_FILE,"w");
1040 if (ftxoutraw) fwrite(o->usbradio_write_buf_1,1,f->datalen * 2 * 6,ftxoutraw);
1043 #if DEBUG_CAPTURES == 1
1044 if (o->b.txcap2 && ftxcaptrace)
1045 fwrite((o->pmrChan->ptxDebug), 1, FRAME_SIZE * 2 * 16, ftxcaptrace);
1048 src = 0; /* read position into f->data */
1049 datalen = f->datalen * 12;
1050 while (src < datalen) {
1051 /* Compute spare room in the buffer */
1052 int l = sizeof(o->usbradio_write_buf) - o->usbradio_write_dst;
1054 if (datalen - src >= l) { /* enough to fill a frame */
1055 memcpy(o->usbradio_write_buf + o->usbradio_write_dst, o->usbradio_write_buf_1 + src, l);
1056 soundcard_writeframe(o, (short *) o->usbradio_write_buf);
1058 o->usbradio_write_dst = 0;
1059 } else { /* copy residue */
1061 memcpy(o->usbradio_write_buf + o->usbradio_write_dst, o->usbradio_write_buf_1 + src, l);
1062 src += l; /* but really, we are done */
1063 o->usbradio_write_dst += l;
1069 static struct ast_frame *usbradio_read(struct ast_channel *c)
1072 struct chan_usbradio_pvt *o = c->tech_pvt;
1073 struct ast_frame *f = &o->read_f, *f1;
1074 struct ast_frame wf = { AST_FRAME_CONTROL };
1077 traceusb2("usbradio_read()\n"); /* sph maw asdf */
1079 if (o->lasthidtime) {
1081 if ((now - o->lasthidtime) > 3) {
1082 ast_log(LOG_ERROR, "HID process has died or something!!\n");
1086 if (o->lastrx && (!o->rxkeyed)) {
1088 wf.subclass = AST_CONTROL_RADIO_UNKEY;
1089 ast_queue_frame(o->owner, &wf);
1090 } else if ((!o->lastrx) && (o->rxkeyed)) {
1092 wf.subclass = AST_CONTROL_RADIO_KEY;
1093 ast_queue_frame(o->owner, &wf);
1095 /* XXX can be simplified returning &ast_null_frame */
1096 /* prepare a NULL frame in case we don't have enough data to return */
1097 memset(f, 0, sizeof(struct ast_frame));
1098 f->frametype = AST_FRAME_NULL;
1099 f->src = usbradio_tech.type;
1101 res = read(o->sounddev, o->usbradio_read_buf + o->readpos,
1102 sizeof(o->usbradio_read_buf) - o->readpos);
1103 if (res < 0) /* audio data not ready, return a NULL frame */
1107 if (o->readpos < sizeof(o->usbradio_read_buf)) /* not enough samples */
1113 #if DEBUG_CAPTURES == 1
1114 if (o->b.rxcapraw && frxcapraw)
1115 fwrite((o->usbradio_read_buf + AST_FRIENDLY_OFFSET), 1, FRAME_SIZE * 2 * 2 * 6, frxcapraw);
1120 (i16 *)(o->usbradio_read_buf + AST_FRIENDLY_OFFSET),
1121 (i16 *)(o->usbradio_read_buf_8k + AST_FRIENDLY_OFFSET));
1124 static FILE *hInput;
1125 i16 iBuff[FRAME_SIZE * 2 * 6];
1127 o->pmrChan->b.rxCapture = 1;
1130 hInput = fopen("/usr/src/xpmr/testdata/rx_in.pcm", "r");
1132 ast_log(LOG_ERROR, " Input Data File Not Found.\n");
1137 if (0 == fread((void *)iBuff, 2, FRAME_SIZE * 2 * 6, hInput))
1142 (i16 *)(o->usbradio_read_buf_8k + AST_FRIENDLY_OFFSET));
1147 if (!frxoutraw) frxoutraw = fopen(RX_CAP_OUT_FILE, "w");
1148 if (frxoutraw) fwrite((o->usbradio_read_buf_8k + AST_FRIENDLY_OFFSET), 1, FRAME_SIZE * 2, frxoutraw);
1151 #if DEBUG_CAPTURES == 1
1152 if (frxcaptrace && o->b.rxcap2) fwrite((o->pmrChan->prxDebug), 1, FRAME_SIZE * 2 * 16, frxcaptrace);
1155 if (o->rxcdtype == CD_HID && (o->pmrChan->rxExtCarrierDetect != o->rxhidsq))
1156 o->pmrChan->rxExtCarrierDetect = o->rxhidsq;
1157 if (o->rxcdtype == CD_HID_INVERT && (o->pmrChan->rxExtCarrierDetect == o->rxhidsq))
1158 o->pmrChan->rxExtCarrierDetect = !o->rxhidsq;
1160 if ( (o->rxcdtype == CD_HID && o->rxhidsq) ||
1161 (o->rxcdtype == CD_HID_INVERT && !o->rxhidsq) ||
1162 (o->rxcdtype == CD_XPMR_NOISE && o->pmrChan->rxCarrierDetect) ||
1163 (o->rxcdtype == CD_XPMR_VOX && o->pmrChan->rxCarrierDetect) )
1168 if (res != o->rxcarrierdetect) {
1169 o->rxcarrierdetect = res;
1171 ast_debug(4, "rxcarrierdetect = %d\n", res);
1174 if (o->pmrChan->rxCtcss->decode != o->rxctcssdecode) {
1176 ast_debug(4, "rxctcssdecode = %d\n", o->pmrChan->rxCtcss->decode);
1177 o->rxctcssdecode = o->pmrChan->rxCtcss->decode;
1180 if ( ( o->rxctcssfreq && (o->rxctcssdecode == o->pmrChan->rxCtcssIndex)) ||
1181 ( !o->rxctcssfreq && o->rxcarrierdetect) )
1187 o->readpos = AST_FRIENDLY_OFFSET; /* reset read pointer for next frame */
1188 if (c->_state != AST_STATE_UP) /* drop data if frame is not up */
1190 /* ok we can build and deliver the frame to the caller */
1191 f->frametype = AST_FRAME_VOICE;
1192 f->subclass = AST_FORMAT_SLINEAR;
1193 f->samples = FRAME_SIZE;
1194 f->datalen = FRAME_SIZE * 2;
1195 f->data = o->usbradio_read_buf_8k + AST_FRIENDLY_OFFSET;
1196 if (o->boost != BOOST_SCALE) { /* scale and clip values */
1198 int16_t *p = (int16_t *) f->data;
1199 for (i = 0; i < f->samples; i++) {
1200 x = (p[i] * o->boost) / BOOST_SCALE;
1203 else if (x < -32768)
1209 f->offset = AST_FRIENDLY_OFFSET;
1211 f1 = ast_dsp_process(c, o->dsp, f);
1212 if ((f1->frametype == AST_FRAME_DTMF_END) || (f1->frametype == AST_FRAME_DTMF_BEGIN)) {
1213 if ((f1->subclass == 'm') || (f1->subclass == 'u'))
1214 f1->frametype = AST_FRAME_DTMF_BEGIN;
1215 if (f1->frametype == AST_FRAME_DTMF_END)
1216 ast_log(LOG_NOTICE,"Got DTMF char %c\n",f1->subclass);
1223 static int usbradio_fixup(struct ast_channel *oldchan, struct ast_channel *newchan)
1225 struct chan_usbradio_pvt *o = newchan->tech_pvt;
1226 ast_log(LOG_WARNING,"usbradio_fixup()\n");
1231 static int usbradio_indicate(struct ast_channel *c, int cond, const void *data, size_t datalen)
1233 struct chan_usbradio_pvt *o = c->tech_pvt;
1237 case AST_CONTROL_BUSY:
1238 case AST_CONTROL_CONGESTION:
1239 case AST_CONTROL_RINGING:
1243 case AST_CONTROL_PROGRESS:
1244 case AST_CONTROL_PROCEEDING:
1245 case AST_CONTROL_VIDUPDATE:
1247 case AST_CONTROL_HOLD:
1248 ast_verb(0, " << Console Has Been Placed on Hold >> \n");
1249 ast_moh_start(c, data, o->mohinterpret);
1251 case AST_CONTROL_UNHOLD:
1252 ast_verb(0, " << Console Has Been Retrieved from Hold >> \n");
1255 case AST_CONTROL_RADIO_KEY:
1258 ast_verb(0, " << Radio Transmit On. >> \n");
1260 case AST_CONTROL_RADIO_UNKEY:
1263 ast_verb(0, " << Radio Transmit Off. >> \n");
1266 ast_log(LOG_WARNING, "Don't know how to display condition %d on %s\n", cond, c->name);
1274 * allocate a new channel.
1276 static struct ast_channel *usbradio_new(struct chan_usbradio_pvt *o, char *ext, char *ctx, int state)
1278 struct ast_channel *c;
1279 char device[15] = "dsp";
1282 snprintf(device + 3, sizeof(device) - 3, "%d", o->devicenum);
1283 c = ast_channel_alloc(1, state, o->cid_num, o->cid_name, "", ext, ctx, 0, "usbRadio/%s", device);
1286 c->tech = &usbradio_tech;
1287 if (o->sounddev < 0)
1288 setformat(o, O_RDWR);
1289 c->fds[0] = o->sounddev; /* -1 if device closed, override later */
1290 c->nativeformats = AST_FORMAT_SLINEAR;
1291 c->readformat = AST_FORMAT_SLINEAR;
1292 c->writeformat = AST_FORMAT_SLINEAR;
1295 if (!ast_strlen_zero(o->language))
1296 ast_string_field_set(c, language, o->language);
1297 /* Don't use ast_set_callerid() here because it will
1298 * generate a needless NewCallerID event */
1299 c->cid.cid_num = ast_strdup(o->cid_num);
1300 c->cid.cid_ani = ast_strdup(o->cid_num);
1301 c->cid.cid_name = ast_strdup(o->cid_name);
1302 if (!ast_strlen_zero(ext))
1303 c->cid.cid_dnid = ast_strdup(ext);
1306 ast_module_ref(ast_module_info->self);
1307 ast_jb_configure(c, &global_jbconf);
1308 if (state != AST_STATE_DOWN) {
1309 if (ast_pbx_start(c)) {
1310 ast_log(LOG_WARNING, "Unable to start PBX on %s\n", c->name);
1312 o->owner = c = NULL;
1313 /* XXX what about the channel itself ? */
1314 /* XXX what about usecnt ? */
1321 static struct ast_channel *usbradio_request(const char *type, int format, void *data, int *cause)
1323 struct ast_channel *c;
1324 struct chan_usbradio_pvt *o = find_desc(data);
1326 ast_debug(4, "usbradio_request ty <%s> data 0x%p <%s>\n", type, data, (char *) data);
1328 ast_log(LOG_NOTICE, "Device %s not found\n", (char *) data);
1329 /* XXX we could default to 'dsp' perhaps ? */
1332 if ((format & AST_FORMAT_SLINEAR) == 0) {
1333 ast_log(LOG_NOTICE, "Format 0x%x unsupported\n", format);
1337 ast_log(LOG_NOTICE, "Already have a call (chan %p) on the usb channel\n", o->owner);
1338 *cause = AST_CAUSE_BUSY;
1341 c = usbradio_new(o, NULL, NULL, AST_STATE_DOWN);
1343 ast_log(LOG_WARNING, "Unable to create new usb channel\n");
1349 static char *handle_cli_radio_key(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
1351 struct chan_usbradio_pvt *o = NULL;
1355 e->command = "radio key";
1357 "Usage: radio key\n"
1358 " Simulates COR active.\n";
1365 return CLI_SHOWUSAGE;
1367 o = find_desc(usbradio_active);
1373 static char *handle_cli_radio_unkey(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
1375 struct chan_usbradio_pvt *o = NULL;
1379 e->command = "radio unkey";
1381 "Usage: radio unkey\n"
1382 " Simulates COR un-active.\n";
1389 return CLI_SHOWUSAGE;
1391 o = find_desc(usbradio_active);
1397 static char *handle_cli_radio_tune(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
1399 struct chan_usbradio_pvt *o = NULL;
1404 e->command = "radio tune [rxnoise|rxvoice|rxtone|rxsquelch|rxcap|rxtracecap|"
1405 "txvoice|txtone|txcap|txtracecap|auxvoice|nocap|dump|save]";
1406 /* radio tune 6 3000 measured tx value */
1408 "Usage: radio tune <function>\n"
1412 " rxsquelch [newsetting]\n"
1415 " txvoice [newsetting]\n"
1416 " txtone [newsetting]\n"
1419 " auxvoice [newsetting]\n"
1422 " save (settings to tuning file)\n"
1424 " All [newsetting]s are values 0-999\n";
1430 if ((a->argc < 2) || (a->argc > 4))
1431 return CLI_SHOWUSAGE;
1433 if (a->argc == 2) { /* just show stuff */
1434 ast_cli(a->fd, "Output A is currently set to %s.\n",
1435 o->txmixa == TX_OUT_COMPOSITE ? "composite" :
1436 o->txmixa == TX_OUT_VOICE ? "voice" :
1437 o->txmixa == TX_OUT_LSD ? "tone" :
1438 o->txmixa == TX_OUT_AUX ? "auxvoice" :
1441 ast_cli(a->fd, "Output B is currently set to %s.\n",
1442 o->txmixb == TX_OUT_COMPOSITE ? "composite" :
1443 o->txmixb == TX_OUT_VOICE ? "voice" :
1444 o->txmixb == TX_OUT_LSD ? "tone" :
1445 o->txmixb == TX_OUT_AUX ? "auxvoice" :
1448 ast_cli(a->fd, "Tx Voice Level currently set to %d\n", o->txmixaset);
1449 ast_cli(a->fd, "Tx Tone Level currently set to %d\n", o->txctcssadj);
1450 ast_cli(a->fd, "Rx Squelch currently set to %d\n", o->rxsquelchadj);
1451 return CLI_SHOWUSAGE;
1454 o = find_desc(usbradio_active);
1456 if (!strcasecmp(a->argv[2], "rxnoise"))
1458 else if (!strcasecmp(a->argv[2], "rxvoice"))
1460 else if (!strcasecmp(a->argv[2], "rxtone"))
1462 else if (!strcasecmp(a->argv[2], "rxsquelch")) {
1464 ast_cli(a->fd, "Current Signal Strength is %d\n", ((32767 - o->pmrChan->rxRssi) * 1000 / 32767));
1465 ast_cli(a->fd, "Current Squelch setting is %d\n", o->rxsquelchadj);
1467 ast_cli(a->fd,"Current Raw RSSI is %d\n",o->pmrChan->rxRssi);
1468 ast_cli(a->fd,"Current (real) Squelch setting is %d\n",*(o->pmrChan->prxSquelchAdjust));
1471 i = atoi(a->argv[3]);
1472 if ((i < 0) || (i > 999))
1473 return CLI_SHOWUSAGE;
1474 ast_cli(a->fd, "Changed Squelch setting to %d\n", i);
1475 o->rxsquelchadj = i;
1476 *(o->pmrChan->prxSquelchAdjust) = ((999 - i) * 32767) / 1000;
1478 } else if (!strcasecmp(a->argv[2], "txvoice")) {
1481 if ((o->txmixa != TX_OUT_VOICE) && (o->txmixb != TX_OUT_VOICE) &&
1482 (o->txmixa != TX_OUT_COMPOSITE) && (o->txmixb != TX_OUT_COMPOSITE)) {
1483 ast_log(LOG_ERROR, "No txvoice output configured.\n");
1484 } else if (a->argc == 3) {
1485 if ((o->txmixa == TX_OUT_VOICE) || (o->txmixa == TX_OUT_COMPOSITE))
1486 ast_cli(a->fd, "Current txvoice setting on Channel A is %d\n", o->txmixaset);
1488 ast_cli(a->fd, "Current txvoice setting on Channel B is %d\n", o->txmixbset);
1490 i = atoi(a->argv[3]);
1491 if ((i < 0) || (i > 999))
1492 return CLI_SHOWUSAGE;
1494 if ((o->txmixa == TX_OUT_VOICE) || (o->txmixa == TX_OUT_COMPOSITE)) {
1496 ast_cli(a->fd, "Changed txvoice setting on Channel A to %d\n", o->txmixaset);
1499 ast_cli(a->fd, "Changed txvoice setting on Channel B to %d\n", o->txmixbset);
1503 ast_cli(a->fd, "Changed Tx Voice Output setting to %d\n", i);
1506 } else if (!strcasecmp(a->argv[2], "auxvoice")) {
1508 if ( (o->txmixa != TX_OUT_AUX) && (o->txmixb != TX_OUT_AUX))
1509 ast_log(LOG_WARNING, "No auxvoice output configured.\n");
1510 else if (a->argc == 3) {
1511 if (o->txmixa == TX_OUT_AUX)
1512 ast_cli(a->fd, "Current auxvoice setting on Channel A is %d\n", o->txmixaset);
1514 ast_cli(a->fd, "Current auxvoice setting on Channel B is %d\n", o->txmixbset);
1516 i = atoi(a->argv[3]);
1517 if ((i < 0) || (i > 999))
1518 return CLI_SHOWUSAGE;
1519 if (o->txmixa == TX_OUT_AUX) {
1521 ast_cli(a->fd, "Changed auxvoice setting on Channel A to %d\n", o->txmixaset);
1524 ast_cli(a->fd, "Changed auxvoice setting on Channel B to %d\n", o->txmixbset);
1529 /* tune_auxoutput(o,i); */
1530 } else if (!strcasecmp(a->argv[2], "txtone")) {
1532 ast_cli(a->fd, "Current Tx CTCSS modulation setting = %d\n", o->txctcssadj);
1534 i = atoi(a->argv[3]);
1535 if ((i < 0) || (i > 999))
1536 return CLI_SHOWUSAGE;
1538 set_txctcss_level(o);
1539 ast_cli(a->fd, "Changed Tx CTCSS modulation setting to %i\n", i);
1544 } else if (!strcasecmp(a->argv[2],"dump"))
1546 else if (!strcasecmp(a->argv[2],"nocap")) {
1547 ast_cli(a->fd, "File capture (trace) was rx=%d tx=%d and now off.\n", o->b.rxcap2, o->b.txcap2);
1548 ast_cli(a->fd, "File capture (raw) was rx=%d tx=%d and now off.\n", o->b.rxcapraw, o->b.txcapraw);
1549 o->b.rxcapraw = o->b.txcapraw = o->b.rxcap2 = o->b.txcap2 = o->pmrChan->b.rxCapture = o->pmrChan->b.txCapture = 0;
1555 fclose(frxcaptrace);
1567 fclose(ftxcaptrace);
1574 } else if (!strcasecmp(a->argv[2], "rxtracecap")) {
1576 frxcaptrace = fopen(RX_CAP_TRACE_FILE, "w");
1577 ast_cli(a->fd, "Trace rx on.\n");
1578 o->b.rxcap2 = o->pmrChan->b.rxCapture = 1;
1579 } else if (!strcasecmp(a->argv[2], "txtracecap")) {
1581 ftxcaptrace = fopen(TX_CAP_TRACE_FILE, "w");
1582 ast_cli(a->fd, "Trace tx on.\n");
1583 o->b.txcap2 = o->pmrChan->b.txCapture = 1;
1584 } else if (!strcasecmp(a->argv[2], "rxcap")) {
1586 frxcapraw = fopen(RX_CAP_RAW_FILE, "w");
1587 ast_cli(a->fd, "cap rx raw on.\n");
1589 } else if (!strcasecmp(a->argv[2], "txcap")) {
1591 ftxcapraw = fopen(TX_CAP_RAW_FILE, "w");
1592 ast_cli(a->fd, "cap tx raw on.\n");
1594 } else if (!strcasecmp(a->argv[2], "save")) {
1596 ast_cli(a->fd, "Saved radio tuning settings to usbradio_tune.conf\n");
1598 return CLI_SHOWUSAGE;
1603 set transmit ctcss modulation level
1604 adjust mixer output or internal gain depending on output type
1605 setting range is 0.0 to 0.9
1607 static int set_txctcss_level(struct chan_usbradio_pvt *o)
1609 if (o->txmixa == TX_OUT_LSD) {
1610 o->txmixaset = (151 * o->txctcssadj) / 1000;
1613 } else if (o->txmixb == TX_OUT_LSD) {
1614 o->txmixbset = (151 * o->txctcssadj) / 1000;
1618 *o->pmrChan->ptxCtcssAdjust = (o->txctcssadj * M_Q8) / 1000;
1623 CLI debugging on and off
1625 static char *handle_cli_radio_set_debug(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
1627 struct chan_usbradio_pvt *o = NULL;
1631 e->command = "radio set debug [off]";
1633 "Usage: radio set debug [off]\n"
1634 " Enable/Disable radio debugging.\n";
1638 if (a->argc < 3 || a->argc > 4)
1639 return CLI_SHOWUSAGE;
1640 if (a->argc == 4 && strncasecmp(a->argv[3], "off", 3))
1641 return CLI_SHOWUSAGE;
1643 o = find_desc(usbradio_active);
1650 ast_cli(a->fd, "USB Radio debugging %s.\n", o->debuglevel ? "enabled" : "disabled");
1655 static struct ast_cli_entry cli_usbradio[] = {
1656 AST_CLI_DEFINE(handle_cli_radio_key, "Simulate Rx Signal Present"),
1657 AST_CLI_DEFINE(handle_cli_radio_unkey, "Simulate Rx Signal Lusb"),
1658 AST_CLI_DEFINE(handle_cli_radio_tune, "Radio Tune"),
1659 AST_CLI_DEFINE(handle_cli_radio_set_debug, "Enable/Disable Radio Debugging"),
1663 * store the callerid components
1666 static void store_callerid(struct chan_usbradio_pvt *o, const char *s)
1668 ast_callerid_split(s, o->cid_name, sizeof(o->cid_name), o->cid_num, sizeof(o->cid_num));
1672 static void store_rxdemod(struct chan_usbradio_pvt *o, const char *s)
1674 if (!strcasecmp(s, "no")) {
1675 o->rxdemod = RX_AUDIO_NONE;
1676 } else if (!strcasecmp(s, "speaker")) {
1677 o->rxdemod = RX_AUDIO_SPEAKER;
1678 } else if (!strcasecmp(s, "flat")) {
1679 o->rxdemod = RX_AUDIO_FLAT;
1681 ast_log(LOG_WARNING, "Unrecognized rxdemod parameter: %s\n", s);
1684 ast_debug(4, "set rxdemod = %s\n", s);
1688 static void store_txmixa(struct chan_usbradio_pvt *o, const char *s)
1690 if (!strcasecmp(s, "no"))
1691 o->txmixa = TX_OUT_OFF;
1693 else if (!strcasecmp(s, "voice"))
1694 o->txmixa = TX_OUT_VOICE;
1695 else if (!strcasecmp(s, "tone"))
1696 o->txmixa = TX_OUT_LSD;
1697 else if (!strcasecmp(s, "composite"))
1698 o->txmixa = TX_OUT_COMPOSITE;
1699 else if (!strcasecmp(s, "auxvoice"))
1700 o->txmixb = TX_OUT_AUX;
1702 ast_log(LOG_WARNING, "Unrecognized txmixa parameter: %s\n", s);
1704 ast_debug(4, "set txmixa = %s\n", s);
1707 static void store_txmixb(struct chan_usbradio_pvt *o, const char *s)
1709 if (!strcasecmp(s, "no"))
1710 o->txmixb = TX_OUT_OFF;
1711 else if (!strcasecmp(s, "voice"))
1712 o->txmixb = TX_OUT_VOICE;
1713 else if (!strcasecmp(s, "tone"))
1714 o->txmixb = TX_OUT_LSD;
1715 else if (!strcasecmp(s, "composite"))
1716 o->txmixb = TX_OUT_COMPOSITE;
1717 else if (!strcasecmp(s, "auxvoice"))
1718 o->txmixb = TX_OUT_AUX;
1720 ast_log(LOG_WARNING, "Unrecognized txmixb parameter: %s\n", s);
1722 ast_debug(4, "set txmixb = %s\n", s);
1725 static void store_rxcdtype(struct chan_usbradio_pvt *o, const char *s)
1727 if (!strcasecmp(s, "no"))
1728 o->rxcdtype = CD_IGNORE;
1729 else if (!strcasecmp(s, "usb"))
1730 o->rxcdtype = CD_HID;
1731 else if (!strcasecmp(s, "dsp"))
1732 o->rxcdtype = CD_XPMR_NOISE;
1733 else if (!strcasecmp(s, "vox"))
1734 o->rxcdtype = CD_XPMR_VOX;
1735 else if (!strcasecmp(s, "usbinvert"))
1736 o->rxcdtype = CD_HID_INVERT;
1738 ast_log(LOG_WARNING, "Unrecognized rxcdtype parameter: %s\n", s);
1740 ast_debug(4, "set rxcdtype = %s\n", s);
1743 static void store_rxsdtype(struct chan_usbradio_pvt *o, const char *s)
1745 if (!strcasecmp(s, "no") || !strcasecmp(s, "SD_IGNORE"))
1746 o->rxsdtype = SD_IGNORE;
1747 else if (!strcasecmp(s, "usb") || !strcasecmp(s, "SD_HID"))
1748 o->rxsdtype = SD_HID;
1749 else if (!strcasecmp(s, "usbinvert") || !strcasecmp(s, "SD_HID_INVERT"))
1750 o->rxsdtype = SD_HID_INVERT;
1751 else if (!strcasecmp(s, "software") || !strcasecmp(s, "SD_XPMR"))
1752 o->rxsdtype = SD_XPMR;
1754 ast_log(LOG_WARNING, "Unrecognized rxsdtype parameter: %s\n", s);
1756 ast_debug(4, "set rxsdtype = %s\n", s);
1759 static void store_rxgain(struct chan_usbradio_pvt *o, const char *s)
1762 if (sscanf(s, "%f", &f) == 1)
1764 ast_debug(4, "set rxgain = %f\n", f);
1767 static void store_rxvoiceadj(struct chan_usbradio_pvt *o, const char *s)
1770 if (sscanf(s, "%f", &f) == 1)
1772 ast_debug(4, "set rxvoiceadj = %f\n", f);
1775 static void store_rxctcssadj(struct chan_usbradio_pvt *o, const char *s)
1778 if (sscanf(s, "%f", &f) == 1)
1780 ast_debug(4, "set rxctcssadj = %f\n", f);
1783 static void store_txtoctype(struct chan_usbradio_pvt *o, const char *s)
1785 if (!strcasecmp(s, "no") || !strcasecmp(s, "TOC_NONE"))
1786 o->txtoctype = TOC_NONE;
1787 else if (!strcasecmp(s, "phase") || !strcasecmp(s, "TOC_PHASE"))
1788 o->txtoctype = TOC_PHASE;
1789 else if (!strcasecmp(s, "notone") || !strcasecmp(s, "TOC_NOTONE"))
1790 o->txtoctype = TOC_NOTONE;
1792 ast_log(LOG_WARNING, "Unrecognized txtoctype parameter: %s\n", s);
1794 ast_debug(4, "set txtoctype = %s\n", s);
1797 static void store_rxctcssfreq(struct chan_usbradio_pvt *o, const char *s)
1800 if (sscanf(s, "%f", &f) == 1)
1802 ast_debug(4, "set rxctcss = %f\n", f);
1805 static void store_txctcssfreq(struct chan_usbradio_pvt *o, const char *s)
1808 if (sscanf(s, "%f", &f) == 1)
1810 ast_debug(4, "set txctcss = %f\n", f);
1813 static void tune_txoutput(struct chan_usbradio_pvt *o, int value)
1816 o->pmrChan->txPttIn = 1;
1819 /* generate 1KHz tone at 7200 peak */
1820 o->pmrChan->spsSigGen1->freq = 10000;
1821 o->pmrChan->spsSigGen1->outputGain = (float)(0.22 * M_Q8);
1822 o->pmrChan->b.startSpecialTone = 1;
1825 TxTestTone(o->pmrChan, 1);
1828 /* o->pmrChan->b.stopSpecialTone = 1; */
1831 TxTestTone(o->pmrChan, 0);
1833 o->pmrChan->txPttIn = 0;
1837 static void tune_rxinput(struct chan_usbradio_pvt *o)
1839 const int target = 23000;
1840 const int tolerance = 2000;
1841 const int settingmin = 1;
1842 const int settingstart = 2;
1843 const int maxtries = 12;
1847 int setting = 0, tries = 0, tmpdiscfactor, meas;
1850 settingmax = o->micmax;
1852 if (o->pmrChan->rxDemod)
1855 setting = settingstart;
1857 while (tries < maxtries) {
1858 setamixer(o->devicenum, MIXER_PARAM_MIC_CAPTURE_VOL, setting, 0);
1859 setamixer(o->devicenum, MIXER_PARAM_MIC_BOOST, o->rxboostset, 0);
1861 if (o->rxcdtype == CD_XPMR_VOX || o->rxdemod == RX_AUDIO_SPEAKER) {
1862 ast_debug(4, "Measure Direct Input\n");
1863 o->pmrChan->spsMeasure->source = o->pmrChan->spsRx->source;
1864 o->pmrChan->spsMeasure->discfactor = 1000;
1865 o->pmrChan->spsMeasure->enabled = 1;
1866 o->pmrChan->spsMeasure->amax = o->pmrChan->spsMeasure->amin = 0;
1868 meas = o->pmrChan->spsMeasure->apeak;
1869 o->pmrChan->spsMeasure->enabled = 0;
1871 ast_debug(4, "Measure HF Noise\n");
1872 tmpdiscfactor = o->pmrChan->spsRx->discfactor;
1873 o->pmrChan->spsRx->discfactor = (i16)1000;
1874 o->pmrChan->spsRx->discounteru = o->pmrChan->spsRx->discounterl = 0;
1875 o->pmrChan->spsRx->amax = o->pmrChan->spsRx->amin = 0;
1877 meas = o->pmrChan->rxRssi;
1878 o->pmrChan->spsRx->discfactor = tmpdiscfactor;
1879 o->pmrChan->spsRx->discounteru = o->pmrChan->spsRx->discounterl = 0;
1880 o->pmrChan->spsRx->amax = o->pmrChan->spsRx->amin = 0;
1884 ast_log(LOG_NOTICE, "tries=%d, setting=%d, meas=%i\n", tries, setting, meas);
1886 if ( meas < (target - tolerance) || meas > (target + tolerance) || tries < 3)
1887 setting = setting * target / meas;
1888 else if (tries > 4 && meas > (target - tolerance) && meas < (target + tolerance) )
1891 if (setting < settingmin)
1892 setting = settingmin;
1893 else if (setting > settingmax)
1894 setting = settingmax;
1898 ast_log(LOG_NOTICE, "DONE tries=%d, setting=%d, meas=%i\n", tries,
1899 (setting * 1000) / o->micmax, meas);
1900 if (meas < (target - tolerance) || meas > (target + tolerance))
1901 ast_log(LOG_NOTICE, "ERROR: RX INPUT ADJUST FAILED.\n");
1903 ast_log(LOG_NOTICE, "INFO: RX INPUT ADJUST SUCCESS.\n");
1904 o->rxmixerset = (setting * 1000) / o->micmax;
1909 static void tune_rxvoice(struct chan_usbradio_pvt *o)
1911 const int target = 7200; /* peak */
1912 const int tolerance = 360; /* peak to peak */
1913 const float settingmin = 0.1;
1914 const float settingmax = 4;
1915 const float settingstart = 1;
1916 const int maxtries = 12;
1920 int tries = 0, meas;
1922 ast_log(LOG_NOTICE, "INFO: RX VOICE ADJUST START.\n");
1923 ast_log(LOG_NOTICE, "target=%d tolerance=%d\n", target, tolerance);
1925 if (!o->pmrChan->spsMeasure)
1926 ast_log(LOG_ERROR, "NO MEASURE BLOCK.\n");
1928 if (!o->pmrChan->spsMeasure->source || !o->pmrChan->prxVoiceAdjust )
1929 ast_log(LOG_ERROR, "NO SOURCE OR MEASURE SETTING.\n");
1931 o->pmrChan->spsMeasure->source = o->pmrChan->spsRxOut->sink;
1932 o->pmrChan->spsMeasure->enabled = 1;
1933 o->pmrChan->spsMeasure->discfactor = 1000;
1935 setting=settingstart;
1937 ast_debug(4, "ERROR: NO MEASURE BLOCK.\n");
1939 while (tries < maxtries) {
1940 *(o->pmrChan->prxVoiceAdjust) = setting * M_Q8;
1942 o->pmrChan->spsMeasure->amax = o->pmrChan->spsMeasure->amin = 0;
1944 meas = o->pmrChan->spsMeasure->apeak;
1945 ast_log(LOG_NOTICE, "tries=%d, setting=%f, meas=%i\n", tries, setting, meas);
1947 if (meas < (target - tolerance) || meas > (target + tolerance) || tries < 3)
1948 setting = setting * target / meas;
1949 else if (tries > 4 && meas > (target - tolerance) && meas < (target + tolerance))
1951 if (setting < settingmin)
1952 setting = settingmin;
1953 else if (setting > settingmax)
1954 setting = settingmax;
1959 o->pmrChan->spsMeasure->enabled = 0;
1961 ast_log(LOG_NOTICE, "DONE tries=%d, setting=%f, meas=%f\n", tries, setting, (float)meas);
1962 if (meas < (target - tolerance) || meas > (target + tolerance))
1963 ast_log(LOG_ERROR, "RX VOICE GAIN ADJUST FAILED.\n");
1965 ast_log(LOG_NOTICE, "RX VOICE GAIN ADJUST SUCCESS.\n");
1966 o->rxvoiceadj = setting;
1970 static void tune_rxctcss(struct chan_usbradio_pvt *o)
1972 const int target = 4096;
1973 const int tolerance = 100;
1974 const float settingmin = 0.1;
1975 const float settingmax = 4;
1976 const float settingstart = 1;
1977 const int maxtries = 12;
1980 int tries = 0, meas;
1982 ast_log(LOG_NOTICE, "RX CTCSS ADJUST START.\n");
1983 ast_log(LOG_NOTICE, "target=%d tolerance=%d \n", target, tolerance);
1985 o->pmrChan->spsMeasure->source = o->pmrChan->prxCtcssMeasure;
1986 o->pmrChan->spsMeasure->discfactor = 400;
1987 o->pmrChan->spsMeasure->enabled = 1;
1989 setting = settingstart;
1991 while (tries < maxtries) {
1992 *(o->pmrChan->prxCtcssAdjust) = setting * M_Q8;
1994 o->pmrChan->spsMeasure->amax = o->pmrChan->spsMeasure->amin = 0;
1996 meas = o->pmrChan->spsMeasure->apeak;
1997 ast_debug(4, "tries=%d, setting=%f, meas=%i\n", tries, setting, meas);
1999 if (meas < (target - tolerance) || meas > (target + tolerance) || tries < 3)
2000 setting = setting * target / meas;
2001 else if (tries > 4 && meas > (target - tolerance) && meas < (target + tolerance))
2003 if (setting < settingmin)
2004 setting = settingmin;
2005 else if (setting > settingmax)
2006 setting = settingmax;
2010 o->pmrChan->spsMeasure->enabled = 0;
2011 ast_debug(4, "DONE tries=%d, setting=%f, meas=%f\n", tries, setting, (float)meas);
2012 if (meas < (target - tolerance) || meas > (target + tolerance))
2013 ast_log(LOG_ERROR, "RX CTCSS GAIN ADJUST FAILED.\n");
2015 ast_log(LOG_NOTICE, "RX CTCSS GAIN ADJUST SUCCESS.\n");
2016 o->rxctcssadj = setting;
2020 this file then is included in chan_usbradio.conf
2021 #include /etc/asterisk/usbradio_tune.conf
2023 static void tune_write(struct chan_usbradio_pvt *o)
2027 fp = fopen("/etc/asterisk/usbradio_tune.conf", "w");
2029 if (!strcmp(o->name, "dsp"))
2030 fprintf(fp, "[general]\n");
2032 fprintf(fp, "[%s]\n", o->name);
2034 fprintf(fp, "; name=%s\n", o->name);
2035 fprintf(fp, "; devicenum=%d\n", o->devicenum);
2037 fprintf(fp, "rxmixerset=%d\n", o->rxmixerset);
2038 fprintf(fp, "rxboostset=%d\n", o->rxboostset);
2039 fprintf(fp, "txmixaset=%d\n", o->txmixaset);
2040 fprintf(fp, "txmixbset=%d\n", o->txmixbset);
2042 fprintf(fp, "rxvoiceadj=%f\n", o->rxvoiceadj);
2043 fprintf(fp, "rxctcssadj=%f\n", o->rxctcssadj);
2044 fprintf(fp, "txctcssadj=%d\n", o->txctcssadj);
2046 fprintf(fp, "rxsquelchadj=%d\n", o->rxsquelchadj);
2050 static void mixer_write(struct chan_usbradio_pvt *o)
2052 setamixer(o->devicenum, MIXER_PARAM_MIC_PLAYBACK_SW, 0, 0);
2053 setamixer(o->devicenum, MIXER_PARAM_MIC_PLAYBACK_VOL, 0, 0);
2054 setamixer(o->devicenum, MIXER_PARAM_SPKR_PLAYBACK_SW, 1, 0);
2055 setamixer(o->devicenum, MIXER_PARAM_SPKR_PLAYBACK_VOL,
2056 o->txmixaset * o->spkrmax / 1000,
2057 o->txmixbset * o->spkrmax / 1000);
2058 setamixer(o->devicenum, MIXER_PARAM_MIC_CAPTURE_VOL,
2059 o->rxmixerset * o->micmax / 1000, 0);
2060 setamixer(o->devicenum, MIXER_PARAM_MIC_BOOST, o->rxboostset, 0);
2061 setamixer(o->devicenum, MIXER_PARAM_MIC_CAPTURE_SW, 1, 0);
2064 adjust dsp multiplier to add resolution to tx level adjustment
2066 static void mult_set(struct chan_usbradio_pvt *o)
2069 if (o->pmrChan->spsTxOutA) {
2070 o->pmrChan->spsTxOutA->outputGain =
2071 mult_calc((o->txmixaset * 152) / 1000);
2073 if (o->pmrChan->spsTxOutB) {
2074 o->pmrChan->spsTxOutB->outputGain =
2075 mult_calc((o->txmixbset * 152) / 1000);
2079 * input 0 - 151 outputs are pot and multiplier
2081 static int mult_calc(int value)
2083 const int multx = M_Q8;
2086 pot= ((int)(value / 4) * 4) + 2;
2087 mult = multx - ((multx * (3 - (value % 4))) / (pot + 2));
2091 #define pd(x) ast_debug(4, #x" = %d\n", x)
2092 #define pp(x) ast_debug(4, #x" = %p\n", x)
2093 #define ps(x) ast_debug(4, #x" = %s\n", x)
2094 #define pf(x) ast_debug(4, #x" = %f\n", x)
2097 static void pmrdump(struct chan_usbradio_pvt *o)
2103 ast_debug(4, "odump()\n");
2115 pd(o->rxsquelchadj);
2124 ast_debug(4, "pmrdump()\n");
2126 ast_debug(4, "prxSquelchAdjust=%d\n", *(o->pmrChan->prxSquelchAdjust));
2128 pd(p->rxCarrierPoint);
2129 pd(p->rxCarrierHyst);
2131 pd(p->rxCtcss->relax);
2133 pd(p->rxCtcssIndex);
2139 pd(p->rxDeEmpEnable);
2140 pd(p->rxCenterSlicerEnable);
2141 pd(p->rxCtcssDecodeEnable);
2142 pd(p->rxDcsDecodeEnable);
2145 pd(p->txLimiterEnable);
2146 pd(p->txPreEmpEnable);
2150 pd(p->spsTxOutA->outputGain);
2152 pd(p->spsTxOutB->outputGain);
2159 * grab fields from the config file, init the descriptor and open the device.
2161 static struct chan_usbradio_pvt *store_config(struct ast_config *cfg, char *ctg)
2163 struct ast_variable *v;
2164 struct chan_usbradio_pvt *o;
2165 struct ast_config *cfg1;
2166 struct ast_flags config_flags = { 0 };
2169 traceusb1(" store_config() ctg == NULL\n");
2170 o = &usbradio_default;
2173 if (!(o = ast_calloc(1, sizeof(*o)))){
2176 *o = usbradio_default;
2177 /* "general" is also the default thing */
2178 if (strcmp(ctg, "general") == 0) {
2179 o->name = ast_strdup("dsp");
2180 usbradio_active = o->name;
2182 o->name = ast_strdup(ctg);
2185 strcpy(o->mohinterpret, "default");
2186 o->micmax = amixer_max(o->devicenum, MIXER_PARAM_MIC_CAPTURE_VOL);
2187 o->spkrmax = amixer_max(o->devicenum, MIXER_PARAM_SPKR_PLAYBACK_VOL);
2188 /* fill other fields from configuration */
2189 for (v = ast_variable_browse(cfg, ctg); v; v = v->next) {
2191 /* handle jb conf */
2192 if (!ast_jb_read_conf(&global_jbconf, v->name, v->value))
2194 CV_START(v->name, v->value);
2196 CV_UINT("frags", o->frags);
2197 CV_UINT("queuesize", o->queuesize);
2198 CV_UINT("devicenum", o->devicenum);
2199 CV_UINT("debug", usbradio_debug);
2200 CV_BOOL("rxcpusaver", o->rxcpusaver);
2201 CV_BOOL("txcpusaver", o->txcpusaver);
2202 CV_BOOL("invertptt", o->invertptt);
2203 CV_F("rxdemod", store_rxdemod(o, v->value));
2204 CV_BOOL("txprelim", o->txprelim);;
2205 CV_F("txmixa", store_txmixa(o, v->value));
2206 CV_F("txmixb", store_txmixb(o, v->value));
2207 CV_F("carrierfrom", store_rxcdtype(o, v->value));
2208 CV_F("rxsdtype", store_rxsdtype(o, v->value));
2209 CV_F("rxctcssfreq", store_rxctcssfreq(o, v->value));
2210 CV_F("txctcssfreq", store_txctcssfreq(o, v->value));
2211 CV_F("rxgain", store_rxgain(o, v->value));
2212 CV_BOOL("rxboostset", o->rxboostset);
2213 CV_UINT("rxctcssrelax", o->rxctcssrelax);
2214 CV_F("txtoctype", store_txtoctype(o, v->value));
2215 CV_UINT("hdwtype", o->hdwtype);
2216 CV_UINT("duplex", o->radioduplex);
2221 cfg1 = ast_config_load(config1, config_flags);
2223 o->rxmixerset = 500;
2226 o->rxvoiceadj = 0.5;
2227 o->rxctcssadj = 0.5;
2228 o->txctcssadj = 200;
2229 o->rxsquelchadj = 500;
2230 ast_log(LOG_WARNING, "File %s not found, using default parameters.\n", config1);
2232 for (v = ast_variable_browse(cfg1, ctg); v; v = v->next) {
2234 CV_START(v->name, v->value);
2235 CV_UINT("rxmixerset", o->rxmixerset);
2236 CV_UINT("txmixaset", o->txmixaset);
2237 CV_UINT("txmixbset", o->txmixbset);
2238 CV_F("rxvoiceadj", store_rxvoiceadj(o, v->value));
2239 CV_F("rxctcssadj", store_rxctcssadj(o, v->value));
2240 CV_UINT("txctcssadj", o->txctcssadj);
2241 CV_UINT("rxsquelchadj", o->rxsquelchadj);
2244 ast_config_destroy(cfg1);
2249 if (o == &usbradio_default) /* we are done with the default */
2252 o->lastopen = ast_tvnow(); /* don't leave it 0 or tvdiff may wrap */
2253 o->dsp = ast_dsp_new();
2255 ast_dsp_set_features(o->dsp, DSP_FEATURE_DTMF_DETECT);
2256 ast_dsp_digitmode(o->dsp, DSP_DIGITMODE_DTMF | DSP_DIGITMODE_MUTECONF | DSP_DIGITMODE_RELAXDTMF);
2259 if (o->rxctcssfreq != 0 && o->rxdemod == RX_AUDIO_SPEAKER)
2260 ast_log(LOG_ERROR, "Incompatable Options o->rxctcssfreq=%f and o->rxdemod=speaker\n", o->rxctcssfreq);
2262 if (o->pmrChan == NULL) {
2265 memset(&tChan, 0, sizeof(tChan));
2267 tChan.rxDemod = o->rxdemod;
2268 tChan.rxCdType = o->rxcdtype;
2270 tChan.txMod = o->txprelim;
2272 tChan.txMixA = o->txmixa;
2273 tChan.txMixB = o->txmixb;
2275 tChan.rxCpuSaver = o->rxcpusaver;
2276 tChan.txCpuSaver = o->txcpusaver;
2278 tChan.rxCtcssFreq = o->rxctcssfreq;
2279 tChan.txCtcssFreq = o->txctcssfreq;
2281 o->pmrChan = createPmrChannel(&tChan, FRAME_SIZE);
2283 o->pmrChan->radioDuplex = o->radioduplex;
2285 o->pmrChan->rxCpuSaver = o->rxcpusaver;
2286 o->pmrChan->txCpuSaver = o->txcpusaver;
2288 *(o->pmrChan->prxSquelchAdjust) =
2289 ((999 - o->rxsquelchadj) * 32767) / 1000;
2291 o->pmrChan->spsRx->outputGain = o->rxvoiceadj*M_Q8;
2293 o->pmrChan->txTocType = o->txtoctype;
2295 if ((o->txmixa == TX_OUT_LSD) ||
2296 (o->txmixa == TX_OUT_COMPOSITE) ||
2297 (o->txmixb == TX_OUT_LSD) ||
2298 (o->txmixb == TX_OUT_COMPOSITE)) {
2299 *(o->pmrChan->prxCtcssAdjust) = o->rxctcssadj * M_Q8;
2300 set_txctcss_level(o);
2303 o->pmrChan->rxCtcss->relax = o->rxctcssrelax;
2307 if ((o->txmixa != TX_OUT_VOICE) && (o->txmixb != TX_OUT_VOICE) &&
2308 (o->txmixa != TX_OUT_COMPOSITE) && (o->txmixb != TX_OUT_COMPOSITE))
2309 ast_log(LOG_ERROR, "No txvoice output configured.\n");
2311 if (o->txctcssfreq &&
2312 o->txmixa != TX_OUT_LSD && o->txmixa != TX_OUT_COMPOSITE &&
2313 o->txmixb != TX_OUT_LSD && o->txmixb != TX_OUT_COMPOSITE)
2314 ast_log(LOG_ERROR, "No txtone output configured.\n");
2316 if (o->rxctcssfreq && o->pmrChan->rxCtcssIndex < 0)
2317 ast_log(LOG_ERROR, "Invalid CTCSS Frequency.\n");
2327 /* link into list of devices */
2328 if (o != &usbradio_default) {
2329 o->next = usbradio_default.next;
2330 usbradio_default.next = o;
2335 #if DEBUG_FILETEST == 1
2339 int RxTestIt(struct chan_usbradio_pvt *o)
2341 const int numSamples = SAMPLES_PER_BLOCK;
2342 const int numChannels = 16;
2353 FILE *hInput = NULL, *hOutput = NULL, *hOutputTx = NULL;
2355 i16 iBuff[numSamples * 2 * 6], oBuff[numSamples];
2357 ast_debug(4, "RxTestIt()\n");
2360 pChan->b.txCapture = 1;
2361 pChan->b.rxCapture = 1;
2365 hInput = fopen("/usr/src/xpmr/testdata/rx_in.pcm", "r");
2367 ast_debug(4, " RxTestIt() File Not Found.\n");
2370 hOutput = fopen("/usr/src/xpmr/testdata/rx_debug.pcm", "w");
2372 ast_debug(4, " RxTestIt() Working...\n");
2374 while (!feof(hInput)) {
2375 fread((void *)iBuff, 2, numSamples * 2 * 6, hInput);
2378 txHangTime -= numSamples;
2382 if (pChan->rxCtcss->decode)
2383 txHangTime = (8000 / 1000 * 2000);
2385 if (pChan->rxCtcss->decode && !txEnable) {
2387 /* pChan->inputBlanking = (8000 / 1000 * 200); */
2388 } else if (!pChan->rxCtcss->decode && txEnable) {
2392 PmrRx(pChan, iBuff, oBuff);
2394 fwrite((void *)pChan->prxDebug, 2, numSamples * numChannels, hOutput);
2396 pChan->b.txCapture = 0;
2397 pChan->b.rxCapture = 0;
2404 ast_debug(4, " RxTestIt() Complete.\n");
2410 #include "./xpmr/xpmr.c"
2413 static int load_module(void)
2415 struct ast_config *cfg = NULL;
2417 struct ast_flags config_flags = { 0 };
2419 /* Copy the default jb config over global_jbconf */
2420 memcpy(&global_jbconf, &default_jbconf, sizeof(struct ast_jb_conf));
2422 /* load config file */
2423 if (!(cfg = ast_config_load(config, config_flags))) {
2424 ast_log(LOG_NOTICE, "Unable to load config %s\n", config);
2425 return AST_MODULE_LOAD_DECLINE;
2429 store_config(cfg, ctg);
2430 } while ( (ctg = ast_category_browse(cfg, ctg)) != NULL);
2432 ast_config_destroy(cfg);
2434 if (find_desc(usbradio_active) == NULL) {
2435 ast_log(LOG_NOTICE, "Device %s not found\n", usbradio_active);
2436 /* XXX we could default to 'dsp' perhaps ? */
2437 /* XXX should cleanup allocated memory etc. */
2438 return AST_MODULE_LOAD_FAILURE;
2441 if (ast_channel_register(&usbradio_tech)) {
2442 ast_log(LOG_ERROR, "Unable to register channel type 'usb'\n");
2443 return AST_MODULE_LOAD_FAILURE;
2446 ast_cli_register_multiple(cli_usbradio, sizeof(cli_usbradio) / sizeof(struct ast_cli_entry));
2448 return AST_MODULE_LOAD_SUCCESS;
2452 static int unload_module(void)
2454 struct chan_usbradio_pvt *o;
2456 ast_log(LOG_WARNING, "unload_module() called\n");
2458 ast_channel_unregister(&usbradio_tech);
2459 ast_cli_unregister_multiple(cli_usbradio, sizeof(cli_usbradio) / sizeof(struct ast_cli_entry));
2461 for (o = usbradio_default.next; o; o = o->next) {
2463 ast_log(LOG_WARNING, "destroyPmrChannel() called\n");
2465 destroyPmrChannel(o->pmrChan);
2467 #if DEBUG_CAPTURES == 1
2468 if (frxcapraw) { fclose(frxcapraw); frxcapraw = NULL; }
2469 if (frxcaptrace) { fclose(frxcaptrace); frxcaptrace = NULL; }
2470 if (frxoutraw) { fclose(frxoutraw); frxoutraw = NULL; }
2471 if (ftxcapraw) { fclose(ftxcapraw); ftxcapraw = NULL; }
2472 if (ftxcaptrace) { fclose(ftxcaptrace); ftxcaptrace = NULL; }
2473 if (ftxoutraw) { fclose(ftxoutraw); ftxoutraw = NULL; }
2478 ast_dsp_free(o->dsp);
2480 ast_softhangup(o->owner, AST_SOFTHANGUP_APPUNLOAD);
2481 if (o->owner) /* XXX how ??? */
2483 /* XXX what about the thread ? */
2484 /* XXX what about the memory allocated ? */
2489 AST_MODULE_INFO_STANDARD(ASTERISK_GPL_KEY, "usb Console Channel Driver");