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_deprecated(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 char *handle_cli_radio_set_debug(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
1657 struct chan_usbradio_pvt *o = NULL;
1661 e->command = "radio set debug {on|off}";
1663 "Usage: radio set debug {on|off}\n"
1664 " Enable/Disable radio debugging.\n";
1669 if (a->argc != e->args)
1670 return CLI_SHOWUSAGE;
1672 o = find_desc(usbradio_active);
1674 if (!strncasecmp(a->argv[e->args - 1], "on", 2))
1676 else if (!strncasecmp(a->argv[e->args - 1], "off", 3))
1679 return CLI_SHOWUSAGE;
1681 ast_cli(a->fd, "USB Radio debugging %s.\n", o->debuglevel ? "enabled" : "disabled");
1686 static struct ast_cli_entry cli_radio_set_debug_deprecated = AST_CLI_DEFINE(handle_cli_radio_set_debug_deprecated, "Enable/Disable Radio Debugging");
1687 static struct ast_cli_entry cli_usbradio[] = {
1688 AST_CLI_DEFINE(handle_cli_radio_key, "Simulate Rx Signal Present"),
1689 AST_CLI_DEFINE(handle_cli_radio_unkey, "Simulate Rx Signal Lusb"),
1690 AST_CLI_DEFINE(handle_cli_radio_tune, "Radio Tune"),
1691 AST_CLI_DEFINE(handle_cli_radio_set_debug, "Enable/Disable Radio Debugging", .deprecate_cmd = &cli_radio_set_debug_deprecated),
1695 * store the callerid components
1698 static void store_callerid(struct chan_usbradio_pvt *o, const char *s)
1700 ast_callerid_split(s, o->cid_name, sizeof(o->cid_name), o->cid_num, sizeof(o->cid_num));
1704 static void store_rxdemod(struct chan_usbradio_pvt *o, const char *s)
1706 if (!strcasecmp(s, "no")) {
1707 o->rxdemod = RX_AUDIO_NONE;
1708 } else if (!strcasecmp(s, "speaker")) {
1709 o->rxdemod = RX_AUDIO_SPEAKER;
1710 } else if (!strcasecmp(s, "flat")) {
1711 o->rxdemod = RX_AUDIO_FLAT;
1713 ast_log(LOG_WARNING, "Unrecognized rxdemod parameter: %s\n", s);
1716 ast_debug(4, "set rxdemod = %s\n", s);
1720 static void store_txmixa(struct chan_usbradio_pvt *o, const char *s)
1722 if (!strcasecmp(s, "no"))
1723 o->txmixa = TX_OUT_OFF;
1725 else if (!strcasecmp(s, "voice"))
1726 o->txmixa = TX_OUT_VOICE;
1727 else if (!strcasecmp(s, "tone"))
1728 o->txmixa = TX_OUT_LSD;
1729 else if (!strcasecmp(s, "composite"))
1730 o->txmixa = TX_OUT_COMPOSITE;
1731 else if (!strcasecmp(s, "auxvoice"))
1732 o->txmixb = TX_OUT_AUX;
1734 ast_log(LOG_WARNING, "Unrecognized txmixa parameter: %s\n", s);
1736 ast_debug(4, "set txmixa = %s\n", s);
1739 static void store_txmixb(struct chan_usbradio_pvt *o, const char *s)
1741 if (!strcasecmp(s, "no"))
1742 o->txmixb = TX_OUT_OFF;
1743 else if (!strcasecmp(s, "voice"))
1744 o->txmixb = TX_OUT_VOICE;
1745 else if (!strcasecmp(s, "tone"))
1746 o->txmixb = TX_OUT_LSD;
1747 else if (!strcasecmp(s, "composite"))
1748 o->txmixb = TX_OUT_COMPOSITE;
1749 else if (!strcasecmp(s, "auxvoice"))
1750 o->txmixb = TX_OUT_AUX;
1752 ast_log(LOG_WARNING, "Unrecognized txmixb parameter: %s\n", s);
1754 ast_debug(4, "set txmixb = %s\n", s);
1757 static void store_rxcdtype(struct chan_usbradio_pvt *o, const char *s)
1759 if (!strcasecmp(s, "no"))
1760 o->rxcdtype = CD_IGNORE;
1761 else if (!strcasecmp(s, "usb"))
1762 o->rxcdtype = CD_HID;
1763 else if (!strcasecmp(s, "dsp"))
1764 o->rxcdtype = CD_XPMR_NOISE;
1765 else if (!strcasecmp(s, "vox"))
1766 o->rxcdtype = CD_XPMR_VOX;
1767 else if (!strcasecmp(s, "usbinvert"))
1768 o->rxcdtype = CD_HID_INVERT;
1770 ast_log(LOG_WARNING, "Unrecognized rxcdtype parameter: %s\n", s);
1772 ast_debug(4, "set rxcdtype = %s\n", s);
1775 static void store_rxsdtype(struct chan_usbradio_pvt *o, const char *s)
1777 if (!strcasecmp(s, "no") || !strcasecmp(s, "SD_IGNORE"))
1778 o->rxsdtype = SD_IGNORE;
1779 else if (!strcasecmp(s, "usb") || !strcasecmp(s, "SD_HID"))
1780 o->rxsdtype = SD_HID;
1781 else if (!strcasecmp(s, "usbinvert") || !strcasecmp(s, "SD_HID_INVERT"))
1782 o->rxsdtype = SD_HID_INVERT;
1783 else if (!strcasecmp(s, "software") || !strcasecmp(s, "SD_XPMR"))
1784 o->rxsdtype = SD_XPMR;
1786 ast_log(LOG_WARNING, "Unrecognized rxsdtype parameter: %s\n", s);
1788 ast_debug(4, "set rxsdtype = %s\n", s);
1791 static void store_rxgain(struct chan_usbradio_pvt *o, const char *s)
1794 if (sscanf(s, "%f", &f) == 1)
1796 ast_debug(4, "set rxgain = %f\n", f);
1799 static void store_rxvoiceadj(struct chan_usbradio_pvt *o, const char *s)
1802 if (sscanf(s, "%f", &f) == 1)
1804 ast_debug(4, "set rxvoiceadj = %f\n", f);
1807 static void store_rxctcssadj(struct chan_usbradio_pvt *o, const char *s)
1810 if (sscanf(s, "%f", &f) == 1)
1812 ast_debug(4, "set rxctcssadj = %f\n", f);
1815 static void store_txtoctype(struct chan_usbradio_pvt *o, const char *s)
1817 if (!strcasecmp(s, "no") || !strcasecmp(s, "TOC_NONE"))
1818 o->txtoctype = TOC_NONE;
1819 else if (!strcasecmp(s, "phase") || !strcasecmp(s, "TOC_PHASE"))
1820 o->txtoctype = TOC_PHASE;
1821 else if (!strcasecmp(s, "notone") || !strcasecmp(s, "TOC_NOTONE"))
1822 o->txtoctype = TOC_NOTONE;
1824 ast_log(LOG_WARNING, "Unrecognized txtoctype parameter: %s\n", s);
1826 ast_debug(4, "set txtoctype = %s\n", s);
1829 static void store_rxctcssfreq(struct chan_usbradio_pvt *o, const char *s)
1832 if (sscanf(s, "%f", &f) == 1)
1834 ast_debug(4, "set rxctcss = %f\n", f);
1837 static void store_txctcssfreq(struct chan_usbradio_pvt *o, const char *s)
1840 if (sscanf(s, "%f", &f) == 1)
1842 ast_debug(4, "set txctcss = %f\n", f);
1845 static void tune_txoutput(struct chan_usbradio_pvt *o, int value)
1848 o->pmrChan->txPttIn = 1;
1851 /* generate 1KHz tone at 7200 peak */
1852 o->pmrChan->spsSigGen1->freq = 10000;
1853 o->pmrChan->spsSigGen1->outputGain = (float)(0.22 * M_Q8);
1854 o->pmrChan->b.startSpecialTone = 1;
1857 TxTestTone(o->pmrChan, 1);
1860 /* o->pmrChan->b.stopSpecialTone = 1; */
1863 TxTestTone(o->pmrChan, 0);
1865 o->pmrChan->txPttIn = 0;
1869 static void tune_rxinput(struct chan_usbradio_pvt *o)
1871 const int target = 23000;
1872 const int tolerance = 2000;
1873 const int settingmin = 1;
1874 const int settingstart = 2;
1875 const int maxtries = 12;
1879 int setting = 0, tries = 0, tmpdiscfactor, meas;
1882 settingmax = o->micmax;
1884 if (o->pmrChan->rxDemod)
1887 setting = settingstart;
1889 while (tries < maxtries) {
1890 setamixer(o->devicenum, MIXER_PARAM_MIC_CAPTURE_VOL, setting, 0);
1891 setamixer(o->devicenum, MIXER_PARAM_MIC_BOOST, o->rxboostset, 0);
1893 if (o->rxcdtype == CD_XPMR_VOX || o->rxdemod == RX_AUDIO_SPEAKER) {
1894 ast_debug(4, "Measure Direct Input\n");
1895 o->pmrChan->spsMeasure->source = o->pmrChan->spsRx->source;
1896 o->pmrChan->spsMeasure->discfactor = 1000;
1897 o->pmrChan->spsMeasure->enabled = 1;
1898 o->pmrChan->spsMeasure->amax = o->pmrChan->spsMeasure->amin = 0;
1900 meas = o->pmrChan->spsMeasure->apeak;
1901 o->pmrChan->spsMeasure->enabled = 0;
1903 ast_debug(4, "Measure HF Noise\n");
1904 tmpdiscfactor = o->pmrChan->spsRx->discfactor;
1905 o->pmrChan->spsRx->discfactor = (i16)1000;
1906 o->pmrChan->spsRx->discounteru = o->pmrChan->spsRx->discounterl = 0;
1907 o->pmrChan->spsRx->amax = o->pmrChan->spsRx->amin = 0;
1909 meas = o->pmrChan->rxRssi;
1910 o->pmrChan->spsRx->discfactor = tmpdiscfactor;
1911 o->pmrChan->spsRx->discounteru = o->pmrChan->spsRx->discounterl = 0;
1912 o->pmrChan->spsRx->amax = o->pmrChan->spsRx->amin = 0;
1916 ast_log(LOG_NOTICE, "tries=%d, setting=%d, meas=%i\n", tries, setting, meas);
1918 if ( meas < (target - tolerance) || meas > (target + tolerance) || tries < 3)
1919 setting = setting * target / meas;
1920 else if (tries > 4 && meas > (target - tolerance) && meas < (target + tolerance) )
1923 if (setting < settingmin)
1924 setting = settingmin;
1925 else if (setting > settingmax)
1926 setting = settingmax;
1930 ast_log(LOG_NOTICE, "DONE tries=%d, setting=%d, meas=%i\n", tries,
1931 (setting * 1000) / o->micmax, meas);
1932 if (meas < (target - tolerance) || meas > (target + tolerance))
1933 ast_log(LOG_NOTICE, "ERROR: RX INPUT ADJUST FAILED.\n");
1935 ast_log(LOG_NOTICE, "INFO: RX INPUT ADJUST SUCCESS.\n");
1936 o->rxmixerset = (setting * 1000) / o->micmax;
1941 static void tune_rxvoice(struct chan_usbradio_pvt *o)
1943 const int target = 7200; /* peak */
1944 const int tolerance = 360; /* peak to peak */
1945 const float settingmin = 0.1;
1946 const float settingmax = 4;
1947 const float settingstart = 1;
1948 const int maxtries = 12;
1952 int tries = 0, meas;
1954 ast_log(LOG_NOTICE, "INFO: RX VOICE ADJUST START.\n");
1955 ast_log(LOG_NOTICE, "target=%d tolerance=%d\n", target, tolerance);
1957 if (!o->pmrChan->spsMeasure)
1958 ast_log(LOG_ERROR, "NO MEASURE BLOCK.\n");
1960 if (!o->pmrChan->spsMeasure->source || !o->pmrChan->prxVoiceAdjust )
1961 ast_log(LOG_ERROR, "NO SOURCE OR MEASURE SETTING.\n");
1963 o->pmrChan->spsMeasure->source = o->pmrChan->spsRxOut->sink;
1964 o->pmrChan->spsMeasure->enabled = 1;
1965 o->pmrChan->spsMeasure->discfactor = 1000;
1967 setting=settingstart;
1969 ast_debug(4, "ERROR: NO MEASURE BLOCK.\n");
1971 while (tries < maxtries) {
1972 *(o->pmrChan->prxVoiceAdjust) = setting * M_Q8;
1974 o->pmrChan->spsMeasure->amax = o->pmrChan->spsMeasure->amin = 0;
1976 meas = o->pmrChan->spsMeasure->apeak;
1977 ast_log(LOG_NOTICE, "tries=%d, setting=%f, meas=%i\n", tries, setting, meas);
1979 if (meas < (target - tolerance) || meas > (target + tolerance) || tries < 3)
1980 setting = setting * target / meas;
1981 else if (tries > 4 && meas > (target - tolerance) && meas < (target + tolerance))
1983 if (setting < settingmin)
1984 setting = settingmin;
1985 else if (setting > settingmax)
1986 setting = settingmax;
1991 o->pmrChan->spsMeasure->enabled = 0;
1993 ast_log(LOG_NOTICE, "DONE tries=%d, setting=%f, meas=%f\n", tries, setting, (float)meas);
1994 if (meas < (target - tolerance) || meas > (target + tolerance))
1995 ast_log(LOG_ERROR, "RX VOICE GAIN ADJUST FAILED.\n");
1997 ast_log(LOG_NOTICE, "RX VOICE GAIN ADJUST SUCCESS.\n");
1998 o->rxvoiceadj = setting;
2002 static void tune_rxctcss(struct chan_usbradio_pvt *o)
2004 const int target = 4096;
2005 const int tolerance = 100;
2006 const float settingmin = 0.1;
2007 const float settingmax = 4;
2008 const float settingstart = 1;
2009 const int maxtries = 12;
2012 int tries = 0, meas;
2014 ast_log(LOG_NOTICE, "RX CTCSS ADJUST START.\n");
2015 ast_log(LOG_NOTICE, "target=%d tolerance=%d \n", target, tolerance);
2017 o->pmrChan->spsMeasure->source = o->pmrChan->prxCtcssMeasure;
2018 o->pmrChan->spsMeasure->discfactor = 400;
2019 o->pmrChan->spsMeasure->enabled = 1;
2021 setting = settingstart;
2023 while (tries < maxtries) {
2024 *(o->pmrChan->prxCtcssAdjust) = setting * M_Q8;
2026 o->pmrChan->spsMeasure->amax = o->pmrChan->spsMeasure->amin = 0;
2028 meas = o->pmrChan->spsMeasure->apeak;
2029 ast_debug(4, "tries=%d, setting=%f, meas=%i\n", tries, setting, meas);
2031 if (meas < (target - tolerance) || meas > (target + tolerance) || tries < 3)
2032 setting = setting * target / meas;
2033 else if (tries > 4 && meas > (target - tolerance) && meas < (target + tolerance))
2035 if (setting < settingmin)
2036 setting = settingmin;
2037 else if (setting > settingmax)
2038 setting = settingmax;
2042 o->pmrChan->spsMeasure->enabled = 0;
2043 ast_debug(4, "DONE tries=%d, setting=%f, meas=%f\n", tries, setting, (float)meas);
2044 if (meas < (target - tolerance) || meas > (target + tolerance))
2045 ast_log(LOG_ERROR, "RX CTCSS GAIN ADJUST FAILED.\n");
2047 ast_log(LOG_NOTICE, "RX CTCSS GAIN ADJUST SUCCESS.\n");
2048 o->rxctcssadj = setting;
2052 this file then is included in chan_usbradio.conf
2053 #include /etc/asterisk/usbradio_tune.conf
2055 static void tune_write(struct chan_usbradio_pvt *o)
2059 fp = fopen("/etc/asterisk/usbradio_tune.conf", "w");
2061 if (!strcmp(o->name, "dsp"))
2062 fprintf(fp, "[general]\n");
2064 fprintf(fp, "[%s]\n", o->name);
2066 fprintf(fp, "; name=%s\n", o->name);
2067 fprintf(fp, "; devicenum=%d\n", o->devicenum);
2069 fprintf(fp, "rxmixerset=%d\n", o->rxmixerset);
2070 fprintf(fp, "rxboostset=%d\n", o->rxboostset);
2071 fprintf(fp, "txmixaset=%d\n", o->txmixaset);
2072 fprintf(fp, "txmixbset=%d\n", o->txmixbset);
2074 fprintf(fp, "rxvoiceadj=%f\n", o->rxvoiceadj);
2075 fprintf(fp, "rxctcssadj=%f\n", o->rxctcssadj);
2076 fprintf(fp, "txctcssadj=%d\n", o->txctcssadj);
2078 fprintf(fp, "rxsquelchadj=%d\n", o->rxsquelchadj);
2082 static void mixer_write(struct chan_usbradio_pvt *o)
2084 setamixer(o->devicenum, MIXER_PARAM_MIC_PLAYBACK_SW, 0, 0);
2085 setamixer(o->devicenum, MIXER_PARAM_MIC_PLAYBACK_VOL, 0, 0);
2086 setamixer(o->devicenum, MIXER_PARAM_SPKR_PLAYBACK_SW, 1, 0);
2087 setamixer(o->devicenum, MIXER_PARAM_SPKR_PLAYBACK_VOL,
2088 o->txmixaset * o->spkrmax / 1000,
2089 o->txmixbset * o->spkrmax / 1000);
2090 setamixer(o->devicenum, MIXER_PARAM_MIC_CAPTURE_VOL,
2091 o->rxmixerset * o->micmax / 1000, 0);
2092 setamixer(o->devicenum, MIXER_PARAM_MIC_BOOST, o->rxboostset, 0);
2093 setamixer(o->devicenum, MIXER_PARAM_MIC_CAPTURE_SW, 1, 0);
2096 adjust dsp multiplier to add resolution to tx level adjustment
2098 static void mult_set(struct chan_usbradio_pvt *o)
2101 if (o->pmrChan->spsTxOutA) {
2102 o->pmrChan->spsTxOutA->outputGain =
2103 mult_calc((o->txmixaset * 152) / 1000);
2105 if (o->pmrChan->spsTxOutB) {
2106 o->pmrChan->spsTxOutB->outputGain =
2107 mult_calc((o->txmixbset * 152) / 1000);
2111 * input 0 - 151 outputs are pot and multiplier
2113 static int mult_calc(int value)
2115 const int multx = M_Q8;
2118 pot= ((int)(value / 4) * 4) + 2;
2119 mult = multx - ((multx * (3 - (value % 4))) / (pot + 2));
2123 #define pd(x) ast_debug(4, #x" = %d\n", x)
2124 #define pp(x) ast_debug(4, #x" = %p\n", x)
2125 #define ps(x) ast_debug(4, #x" = %s\n", x)
2126 #define pf(x) ast_debug(4, #x" = %f\n", x)
2129 static void pmrdump(struct chan_usbradio_pvt *o)
2135 ast_debug(4, "odump()\n");
2147 pd(o->rxsquelchadj);
2156 ast_debug(4, "pmrdump()\n");
2158 ast_debug(4, "prxSquelchAdjust=%d\n", *(o->pmrChan->prxSquelchAdjust));
2160 pd(p->rxCarrierPoint);
2161 pd(p->rxCarrierHyst);
2163 pd(p->rxCtcss->relax);
2165 pd(p->rxCtcssIndex);
2171 pd(p->rxDeEmpEnable);
2172 pd(p->rxCenterSlicerEnable);
2173 pd(p->rxCtcssDecodeEnable);
2174 pd(p->rxDcsDecodeEnable);
2177 pd(p->txLimiterEnable);
2178 pd(p->txPreEmpEnable);
2182 pd(p->spsTxOutA->outputGain);
2184 pd(p->spsTxOutB->outputGain);
2191 * grab fields from the config file, init the descriptor and open the device.
2193 static struct chan_usbradio_pvt *store_config(struct ast_config *cfg, char *ctg)
2195 struct ast_variable *v;
2196 struct chan_usbradio_pvt *o;
2197 struct ast_config *cfg1;
2198 struct ast_flags config_flags = { 0 };
2201 traceusb1(" store_config() ctg == NULL\n");
2202 o = &usbradio_default;
2205 if (!(o = ast_calloc(1, sizeof(*o)))){
2208 *o = usbradio_default;
2209 /* "general" is also the default thing */
2210 if (strcmp(ctg, "general") == 0) {
2211 o->name = ast_strdup("dsp");
2212 usbradio_active = o->name;
2214 o->name = ast_strdup(ctg);
2217 strcpy(o->mohinterpret, "default");
2218 o->micmax = amixer_max(o->devicenum, MIXER_PARAM_MIC_CAPTURE_VOL);
2219 o->spkrmax = amixer_max(o->devicenum, MIXER_PARAM_SPKR_PLAYBACK_VOL);
2220 /* fill other fields from configuration */
2221 for (v = ast_variable_browse(cfg, ctg); v; v = v->next) {
2223 /* handle jb conf */
2224 if (!ast_jb_read_conf(&global_jbconf, v->name, v->value))
2226 CV_START(v->name, v->value);
2228 CV_UINT("frags", o->frags);
2229 CV_UINT("queuesize", o->queuesize);
2230 CV_UINT("devicenum", o->devicenum);
2231 CV_UINT("debug", usbradio_debug);
2232 CV_BOOL("rxcpusaver", o->rxcpusaver);
2233 CV_BOOL("txcpusaver", o->txcpusaver);
2234 CV_BOOL("invertptt", o->invertptt);
2235 CV_F("rxdemod", store_rxdemod(o, v->value));
2236 CV_BOOL("txprelim", o->txprelim);;
2237 CV_F("txmixa", store_txmixa(o, v->value));
2238 CV_F("txmixb", store_txmixb(o, v->value));
2239 CV_F("carrierfrom", store_rxcdtype(o, v->value));
2240 CV_F("rxsdtype", store_rxsdtype(o, v->value));
2241 CV_F("rxctcssfreq", store_rxctcssfreq(o, v->value));
2242 CV_F("txctcssfreq", store_txctcssfreq(o, v->value));
2243 CV_F("rxgain", store_rxgain(o, v->value));
2244 CV_BOOL("rxboostset", o->rxboostset);
2245 CV_UINT("rxctcssrelax", o->rxctcssrelax);
2246 CV_F("txtoctype", store_txtoctype(o, v->value));
2247 CV_UINT("hdwtype", o->hdwtype);
2248 CV_UINT("duplex", o->radioduplex);
2253 cfg1 = ast_config_load(config1, config_flags);
2255 o->rxmixerset = 500;
2258 o->rxvoiceadj = 0.5;
2259 o->rxctcssadj = 0.5;
2260 o->txctcssadj = 200;
2261 o->rxsquelchadj = 500;
2262 ast_log(LOG_WARNING, "File %s not found, using default parameters.\n", config1);
2264 for (v = ast_variable_browse(cfg1, ctg); v; v = v->next) {
2266 CV_START(v->name, v->value);
2267 CV_UINT("rxmixerset", o->rxmixerset);
2268 CV_UINT("txmixaset", o->txmixaset);
2269 CV_UINT("txmixbset", o->txmixbset);
2270 CV_F("rxvoiceadj", store_rxvoiceadj(o, v->value));
2271 CV_F("rxctcssadj", store_rxctcssadj(o, v->value));
2272 CV_UINT("txctcssadj", o->txctcssadj);
2273 CV_UINT("rxsquelchadj", o->rxsquelchadj);
2276 ast_config_destroy(cfg1);
2281 if (o == &usbradio_default) /* we are done with the default */
2284 o->lastopen = ast_tvnow(); /* don't leave it 0 or tvdiff may wrap */
2285 o->dsp = ast_dsp_new();
2287 ast_dsp_set_features(o->dsp, DSP_FEATURE_DTMF_DETECT);
2288 ast_dsp_digitmode(o->dsp, DSP_DIGITMODE_DTMF | DSP_DIGITMODE_MUTECONF | DSP_DIGITMODE_RELAXDTMF);
2291 if (o->rxctcssfreq != 0 && o->rxdemod == RX_AUDIO_SPEAKER)
2292 ast_log(LOG_ERROR, "Incompatable Options o->rxctcssfreq=%f and o->rxdemod=speaker\n", o->rxctcssfreq);
2294 if (o->pmrChan == NULL) {
2297 memset(&tChan, 0, sizeof(tChan));
2299 tChan.rxDemod = o->rxdemod;
2300 tChan.rxCdType = o->rxcdtype;
2302 tChan.txMod = o->txprelim;
2304 tChan.txMixA = o->txmixa;
2305 tChan.txMixB = o->txmixb;
2307 tChan.rxCpuSaver = o->rxcpusaver;
2308 tChan.txCpuSaver = o->txcpusaver;
2310 tChan.rxCtcssFreq = o->rxctcssfreq;
2311 tChan.txCtcssFreq = o->txctcssfreq;
2313 o->pmrChan = createPmrChannel(&tChan, FRAME_SIZE);
2315 o->pmrChan->radioDuplex = o->radioduplex;
2317 o->pmrChan->rxCpuSaver = o->rxcpusaver;
2318 o->pmrChan->txCpuSaver = o->txcpusaver;
2320 *(o->pmrChan->prxSquelchAdjust) =
2321 ((999 - o->rxsquelchadj) * 32767) / 1000;
2323 o->pmrChan->spsRx->outputGain = o->rxvoiceadj*M_Q8;
2325 o->pmrChan->txTocType = o->txtoctype;
2327 if ((o->txmixa == TX_OUT_LSD) ||
2328 (o->txmixa == TX_OUT_COMPOSITE) ||
2329 (o->txmixb == TX_OUT_LSD) ||
2330 (o->txmixb == TX_OUT_COMPOSITE)) {
2331 *(o->pmrChan->prxCtcssAdjust) = o->rxctcssadj * M_Q8;
2332 set_txctcss_level(o);
2335 o->pmrChan->rxCtcss->relax = o->rxctcssrelax;
2339 if ((o->txmixa != TX_OUT_VOICE) && (o->txmixb != TX_OUT_VOICE) &&
2340 (o->txmixa != TX_OUT_COMPOSITE) && (o->txmixb != TX_OUT_COMPOSITE))
2341 ast_log(LOG_ERROR, "No txvoice output configured.\n");
2343 if (o->txctcssfreq &&
2344 o->txmixa != TX_OUT_LSD && o->txmixa != TX_OUT_COMPOSITE &&
2345 o->txmixb != TX_OUT_LSD && o->txmixb != TX_OUT_COMPOSITE)
2346 ast_log(LOG_ERROR, "No txtone output configured.\n");
2348 if (o->rxctcssfreq && o->pmrChan->rxCtcssIndex < 0)
2349 ast_log(LOG_ERROR, "Invalid CTCSS Frequency.\n");
2359 /* link into list of devices */
2360 if (o != &usbradio_default) {
2361 o->next = usbradio_default.next;
2362 usbradio_default.next = o;
2367 #if DEBUG_FILETEST == 1
2371 int RxTestIt(struct chan_usbradio_pvt *o)
2373 const int numSamples = SAMPLES_PER_BLOCK;
2374 const int numChannels = 16;
2385 FILE *hInput = NULL, *hOutput = NULL, *hOutputTx = NULL;
2387 i16 iBuff[numSamples * 2 * 6], oBuff[numSamples];
2389 ast_debug(4, "RxTestIt()\n");
2392 pChan->b.txCapture = 1;
2393 pChan->b.rxCapture = 1;
2397 hInput = fopen("/usr/src/xpmr/testdata/rx_in.pcm", "r");
2399 ast_debug(4, " RxTestIt() File Not Found.\n");
2402 hOutput = fopen("/usr/src/xpmr/testdata/rx_debug.pcm", "w");
2404 ast_debug(4, " RxTestIt() Working...\n");
2406 while (!feof(hInput)) {
2407 fread((void *)iBuff, 2, numSamples * 2 * 6, hInput);
2410 txHangTime -= numSamples;
2414 if (pChan->rxCtcss->decode)
2415 txHangTime = (8000 / 1000 * 2000);
2417 if (pChan->rxCtcss->decode && !txEnable) {
2419 /* pChan->inputBlanking = (8000 / 1000 * 200); */
2420 } else if (!pChan->rxCtcss->decode && txEnable) {
2424 PmrRx(pChan, iBuff, oBuff);
2426 fwrite((void *)pChan->prxDebug, 2, numSamples * numChannels, hOutput);
2428 pChan->b.txCapture = 0;
2429 pChan->b.rxCapture = 0;
2436 ast_debug(4, " RxTestIt() Complete.\n");
2442 #include "./xpmr/xpmr.c"
2445 static int load_module(void)
2447 struct ast_config *cfg = NULL;
2449 struct ast_flags config_flags = { 0 };
2451 /* Copy the default jb config over global_jbconf */
2452 memcpy(&global_jbconf, &default_jbconf, sizeof(struct ast_jb_conf));
2454 /* load config file */
2455 if (!(cfg = ast_config_load(config, config_flags))) {
2456 ast_log(LOG_NOTICE, "Unable to load config %s\n", config);
2457 return AST_MODULE_LOAD_DECLINE;
2461 store_config(cfg, ctg);
2462 } while ( (ctg = ast_category_browse(cfg, ctg)) != NULL);
2464 ast_config_destroy(cfg);
2466 if (find_desc(usbradio_active) == NULL) {
2467 ast_log(LOG_NOTICE, "Device %s not found\n", usbradio_active);
2468 /* XXX we could default to 'dsp' perhaps ? */
2469 /* XXX should cleanup allocated memory etc. */
2470 return AST_MODULE_LOAD_FAILURE;
2473 if (ast_channel_register(&usbradio_tech)) {
2474 ast_log(LOG_ERROR, "Unable to register channel type 'usb'\n");
2475 return AST_MODULE_LOAD_FAILURE;
2478 ast_cli_register_multiple(cli_usbradio, sizeof(cli_usbradio) / sizeof(struct ast_cli_entry));
2480 return AST_MODULE_LOAD_SUCCESS;
2484 static int unload_module(void)
2486 struct chan_usbradio_pvt *o;
2488 ast_log(LOG_WARNING, "unload_module() called\n");
2490 ast_channel_unregister(&usbradio_tech);
2491 ast_cli_unregister_multiple(cli_usbradio, sizeof(cli_usbradio) / sizeof(struct ast_cli_entry));
2493 for (o = usbradio_default.next; o; o = o->next) {
2495 ast_log(LOG_WARNING, "destroyPmrChannel() called\n");
2497 destroyPmrChannel(o->pmrChan);
2499 #if DEBUG_CAPTURES == 1
2500 if (frxcapraw) { fclose(frxcapraw); frxcapraw = NULL; }
2501 if (frxcaptrace) { fclose(frxcaptrace); frxcaptrace = NULL; }
2502 if (frxoutraw) { fclose(frxoutraw); frxoutraw = NULL; }
2503 if (ftxcapraw) { fclose(ftxcapraw); ftxcapraw = NULL; }
2504 if (ftxcaptrace) { fclose(ftxcaptrace); ftxcaptrace = NULL; }
2505 if (ftxoutraw) { fclose(ftxoutraw); ftxoutraw = NULL; }
2510 ast_dsp_free(o->dsp);
2512 ast_softhangup(o->owner, AST_SOFTHANGUP_APPUNLOAD);
2513 if (o->owner) /* XXX how ??? */
2515 /* XXX what about the thread ? */
2516 /* XXX what about the memory allocated ? */
2521 AST_MODULE_INFO_STANDARD(ASTERISK_GPL_KEY, "usb Console Channel Driver");