1 diff -ruN asterisk-1.0.9-old/channels/Makefile asterisk-1.0.9-new/channels/Makefile
2 --- asterisk-1.0.9-old/channels/Makefile 2005-08-22 20:42:22.000000000 +0200
3 +++ asterisk-1.0.9-new/channels/Makefile 2005-08-22 21:12:14.000000000 +0200
6 #CHANNEL_LIBS+=chan_vofr
9 +# Asterisk Bluetooth Support
10 +# http://www.crazygreek.co.uk/content/chan_bluetooth
12 +CHANNEL_LIBS += chan_bluetooth.so
14 ifeq (${OSARCH},OpenBSD)
15 MYSQLLIB=-L/usr/local/lib/mysql -lmysqlclient
16 CFLAGS+=-I/usr/local/include
18 chan_h323.so: chan_h323.o h323/libchanh323.a
19 $(CC) $(SOLINK) -o $@ $< h323/libchanh323.a $(CHANH323LIB) -L$(PWLIBDIR)/lib $(PTLIB) -L$(OPENH323DIR)/lib $(H323LIB) -L/usr/lib -lcrypto -lssl -lexpat
21 +chan_bluetooth.so: chan_bluetooth.o
22 + $(CC) $(SOLINK) -o $@ $< $(LDFLAGS_EXTRA) -lbluetooth
25 #chan_modem.so : chan_modem.o
26 # $(CC) -rdynamic -shared -Xlinker -x -o $@ $<
27 diff -ruN asterisk-1.0.9-old/channels/chan_bluetooth.c asterisk-1.0.9-new/channels/chan_bluetooth.c
28 --- asterisk-1.0.9-old/channels/chan_bluetooth.c 1970-01-01 01:00:00.000000000 +0100
29 +++ asterisk-1.0.9-new/channels/chan_bluetooth.c 2004-11-06 17:35:58.000000000 +0100
32 + * Asterisk -- A telephony toolkit for Linux.
34 + * Asterisk Bluetooth Channel
36 + * Author: Theo Zourzouvillys <theo@adaptive-it.co.uk>
38 + * Adaptive Linux Solutions <http://www.adaptive-it.co.uk>
40 + * Copyright (C) 2004 Adaptive Linux Solutions
42 + * This program is free software, distributed under the terms of
43 + * the GNU General Public License
45 + * ******************* NOTE NOTE NOTE NOTE NOTE *********************
47 + * This code is not at all tested, and only been developed with a
48 + * HBH-200 headset and a Nokia 6310i right now.
50 + * Expect it to crash, dial random numbers, and steal all your money.
52 + * PLEASE try any headsets and phones, and let me know the results,
53 + * working or not, along with all debug output!
55 + * ------------------------------------------------------------------
57 + * Asterisk Bluetooth Support
59 + * Well, here we go - Attempt to provide Handsfree profile support in
60 + * both AG and HF modes, AG (AudioGateway) mode support for using
61 + * headsets, and HF (Handsfree) mode for utilising mobile/cell phones
63 + * It would be nice to also provide Headset support at some time in
64 + * the future, however, a working Handsfree profile is nice for now,
65 + * and as far as I can see, almost all new HS devices also support HF
67 + * ------------------------------------------------------------------
70 + * You need to have bluez's bluetooth stack, along with user space
71 + * tools (>=v2.10), and running hcid and sdsp.
73 + * See bluetooth.conf for configuration details.
75 + * - Ensure bluetooth subsystem is up and running. 'hciconfig'
76 + * should show interface as UP.
78 + * - If you're trying to use a headset/HS, start up asterisk, and try
79 + * to pair it as you normally would.
81 + * - If you're trying to use a Phone/AG, just make sure bluetooth is
82 + * enabled on your phone, and start up asterisk.
84 + * - 'bluetooth show peers' will show all bluetooth devices states.
86 + * - Send a call out by using Dial(BLT/DevName/0123456). Call a HS
87 + * with Dial(BLT/DevName)
89 + * ------------------------------------------------------------------
92 + * - What should happen when an AG is paired with asterisk and
93 + * someone uses the AG dalling a number manually? My test phone
94 + * seems to try to open an SCO link. Perhaps an extension to
95 + * route the call to, or maybe drop the RFCOM link all together?
97 + * ------------------------------------------------------------------
100 + * PLEASE email <theo@adaptive-it.co.uk> with the results of ANY
101 + * device not listed in here (working or not), or if the device is
102 + * listed and it doesn't work! Please also email full debug output
103 + * for any device not working correctly or generating errors in log.
105 + * HandsFree Profile:
108 + * - Ericsson HBH-200
110 + * AG (AudioGateway):
113 + * ------------------------------------------------------------------
115 + * Questions, bugs, or (preferably) patches to:
117 + * <theo@adaptive-it.co.uk>
119 + * ------------------------------------------------------------------
122 +/* ---------------------------------- */
126 +#include <asterisk/lock.h>
127 +#include <asterisk/utils.h>
128 +#include <asterisk/channel.h>
129 +#include <asterisk/channel_pvt.h>
130 +#include <asterisk/config.h>
131 +#include <asterisk/logger.h>
132 +#include <asterisk/module.h>
133 +#include <asterisk/pbx.h>
134 +#include <asterisk/sched.h>
135 +#include <asterisk/options.h>
136 +#include <asterisk/cli.h>
137 +#include <asterisk/callerid.h>
138 +#include <sys/socket.h>
139 +#include <sys/signal.h>
140 +#include <sys/time.h>
144 +#include <arpa/inet.h>
146 +#include <sys/ioctl.h>
149 +#include <bluetooth/bluetooth.h>
150 +#include <bluetooth/hci.h>
151 +#include <bluetooth/hci_lib.h>
152 +#include <bluetooth/sco.h>
153 +#include <bluetooth/rfcomm.h>
154 +#include <bluetooth/sdp.h>
155 +#include <bluetooth/sdp_lib.h>
157 +/* --- Data types and definitions --- */
159 +#ifndef HANDSFREE_AUDIO_GW_SVCLASS_ID
160 +# define HANDSFREE_AUDIO_GW_SVCLASS_ID 0x111f
163 +#define BLUETOOTH_FORMAT AST_FORMAT_SLINEAR
164 +#define BLT_CHAN_NAME "BLT"
165 +#define BLT_CONFIG_FILE "bluetooth.conf"
166 +#define BLT_RDBUFF_MAX 1024
167 +#define BLT_DEFAULT_HCI_DEV 0
168 +#define BLT_SVN_REVISION "$Rev: 38 $"
170 +/* ---------------------------------- */
173 + BLT_ROLE_NONE = 0, // Unknown Device
174 + BLT_ROLE_HS = 1, // Device is a Headset
175 + BLT_ROLE_AG = 2 // Device is an Audio Gateway
178 +/* State when we're in HS mode */
181 + BLT_STATE_WANT_R = 0,
182 + BLT_STATE_WANT_N = 1,
183 + BLT_STATE_WANT_CMD = 2,
184 + BLT_STATE_WANT_N2 = 3,
189 + BLT_STATUS_CONNECTING,
190 + BLT_STATUS_NEGOTIATING,
192 + BLT_STATUS_RINGING,
193 + BLT_STATUS_IN_CALL,
196 +/* ---------------------------------- */
198 +/* Default config settings */
200 +#define BLT_DEFAULT_CHANNEL_AG 5
201 +#define BLT_DEFAULT_CHANNEL_HS 6
202 +#define BLT_DEFAULT_ROLE BLT_ROLE_HS
203 +#define BLT_OBUF_LEN (48 * 25)
207 +/* ---------------------------------- */
209 +typedef struct blt_dev blt_dev_t;
211 +// XXX:T: Tidy this lot up.
214 + blt_status_t status; /* Device Status */
216 + struct ast_channel * owner; /* Channel we belong to, possibly NULL */
217 + blt_dev_t * dev; /* The bluetooth device channel is for */
218 + struct ast_frame fr; /* Recieved frame */
221 + int sco_pipe[2]; /* SCO alert pipe */
222 + int sco; /* SCO fd */
223 + int sco_handle; /* SCO Handle */
224 + int sco_mtu; /* SCO MTU */
225 + int sco_running; /* 1 when sCO thread should be running */
226 + pthread_t sco_thread; /* SCO thread */
227 + ast_mutex_t sco_lock; /* SCO lock */
228 + int sco_pos_in; /* Reader in position */
229 + int sco_pos_out; /* Reader out position */
230 + int sco_sending; /* Sending SCO packets */
231 + char buf[1024]; /* Incoming data buffer */
232 + char sco_buf_out[BUFLEN+1]; /* 24 chunks of 48 */
233 + char sco_buf_in[BUFLEN+1]; /* 24 chunks of 48 */
235 + char dnid[1024]; /* Outgoi gncall dialed number */
236 + unsigned char * obuf[BLT_OBUF_LEN]; /* Outgoing data buffer */
237 + int obuf_len; /* Output Buffer Position */
238 + int obuf_wpos; /* Buffer Reader */
241 + int autoconnect; /* 1 for autoconnect */
242 + int outgoing_id; /* Outgoing connection scheduler id */
243 + char * name; /* Devices friendly name */
244 + blt_role_t role; /* Device role (HS or AG) */
245 + bdaddr_t bdaddr; /* remote address */
246 + int channel; /* remote channel */
247 + int rd; /* RFCOMM fd */
248 + int tmp_rd; /* RFCOMM fd */
249 + int call_cnt; /* Number of attempted calls */
250 + ast_mutex_t lock; /* RFCOMM socket lock */
251 + char rd_buff[BLT_RDBUFF_MAX]; /* RFCOMM input buffer */
252 + int rd_buff_pos; /* RFCOMM input buffer position */
253 + int ready; /* 1 When ready */
256 + char last_ok_cmd[BLT_RDBUFF_MAX]; /* Runtime[AG]: Last AT command that was OK */
257 + int cind; /* Runtime[AG]: Recieved +CIND */
258 + int call_pos, service_pos, callsetup_pos; /* Runtime[AG]: Positions in CIND/CMER */
259 + int call, service, callsetup; /* Runtime[AG]: Values */
262 + blt_state_t state; /* Runtime: Device state (AG mode only) */
263 + int ring_timer; /* Runtime:Ring Timer */
264 + char last_err_cmd[BLT_RDBUFF_MAX]; /* Runtime: Last AT command that was OK */
265 + void (*cb)(blt_dev_t * dev, char * str); /* Runtime: Callback when in HS mode */
267 + int brsf; /* Runtime: Bluetooth Retrieve Supported Features */
268 + int bvra; /* Runtime: Bluetooth Voice Recognised Activation */
269 + int gain_speaker; /* Runtime: Gain Of Speaker */
270 + int clip; /* Runtime: Supports CLID */
271 + int colp; /* Runtime: Connected Line ID */
272 + int elip; /* Runtime: (Ericsson) Supports CLID */
273 + int eolp; /* Runtime: (Ericsson) Connected Line ID */
274 + int ringing; /* Runtime: Device is ringing */
276 + blt_dev_t * next; /* Next in linked list */
280 +typedef struct blt_atcb {
285 + /* DTE callbacks: */
286 + int (*set)(blt_dev_t * dev, const char * arg, int len);
287 + int (*read)(blt_dev_t * dev);
288 + int (*execute)(blt_dev_t * dev, const char * data);
289 + int (*test)(blt_dev_t * dev);
291 + /* DCE callbacks: */
292 + int (*unsolicited)(blt_dev_t * dev, const char * value);
296 +/* ---------------------------------- */
298 +static void rd_close(blt_dev_t * dev, int reconnect, int err);
299 +static int send_atcmd(blt_dev_t * device, const char * fmt, ...);
300 +static int sco_connect(blt_dev_t * dev);
302 +/* ---------------------------------- */
304 +/* RFCOMM channel we listen on*/
305 +static int rfcomm_channel_ag = BLT_DEFAULT_CHANNEL_AG;
306 +static int rfcomm_channel_hs = BLT_DEFAULT_CHANNEL_HS;
308 +/* Address of local bluetooth interface */
309 +static int hcidev_id;
310 +static bdaddr_t local_bdaddr;
312 +/* All the current sockets */
313 +AST_MUTEX_DEFINE_STATIC(iface_lock);
314 +static blt_dev_t * iface_head;
315 +static int ifcount = 0;
317 +static int sdp_record_hs = -1;
318 +static int sdp_record_ag = -1;
320 +/* RFCOMM listen socket */
321 +static int rfcomm_sock_ag = -1;
322 +static int rfcomm_sock_hs = -1;
323 +static int sco_socket = -1;
325 +static int monitor_pid = -1;
327 +/* The socket monitoring thread */
328 +static pthread_t monitor_thread = AST_PTHREADT_NULL;
329 +AST_MUTEX_DEFINE_STATIC(monitor_lock);
331 +/* Cound how many times this module is currently in use */
332 +static int usecnt = 0;
333 +AST_MUTEX_DEFINE_STATIC(usecnt_lock);
335 +static struct sched_context * sched = NULL;
337 +/* ---------------------------------- */
340 +role2str(blt_role_t role)
347 + case BLT_ROLE_NONE:
353 +status2str(blt_status_t status)
356 + case BLT_STATUS_DOWN:
358 + case BLT_STATUS_CONNECTING:
359 + return "Connecting";
360 + case BLT_STATUS_NEGOTIATING:
361 + return "Negotiating";
362 + case BLT_STATUS_READY:
364 + case BLT_STATUS_RINGING:
366 + case BLT_STATUS_IN_CALL:
372 +int sock_err(int fd)
375 + int len = sizeof(ret);
376 + getsockopt(fd, SOL_SOCKET, SO_ERROR, &ret, &len);
380 +/* ---------------------------------- */
383 +parse_cind(const char * str, char * name, int name_len)
387 + memset(name, 0, name_len);
391 + if (++c == 1 && *(str+1) == '"') {
392 + const char * start = str + 2;
395 + while (*str && *str != '"') {
401 + strncpy(name, start, (len > name_len) ? name_len : len);
403 + } else if (*str == ')')
405 + else if (c == 0 && *str == ',')
413 +set_cind(blt_dev_t * dev, int indicator, int val)
416 + ast_log(LOG_DEBUG, "CIND %d set to %d\n", indicator, val);
418 + if (indicator == dev->callsetup_pos) {
422 + dev->callsetup = val;
426 + // Outgoign ringing
427 + if (dev->owner && dev->role == BLT_ROLE_AG)
428 + ast_queue_control(dev->owner, AST_CONTROL_RINGING);
435 + if (dev->owner && dev->role == BLT_ROLE_AG && dev->call == 0)
436 + ast_queue_control(dev->owner, AST_CONTROL_CONGESTION);
440 + } else if (indicator == dev->service_pos) {
445 + ast_log(LOG_NOTICE, "Audio Gateway %s lost signal\n", dev->name);
446 + else if (dev->service == 0 && val > 0)
447 + ast_log(LOG_NOTICE, "Audio Gateway %s got signal\n", dev->name);
449 + dev->service = val;
451 + } else if (indicator == dev->call_pos) {
459 + ast_queue_control(dev->owner, AST_CONTROL_ANSWER);
460 + } else if (val == 0)
461 + ast_queue_control(dev->owner, AST_CONTROL_HANGUP);
469 +/* ---------------------------------- */
472 +set_buffer(char * ring, char * data, int circular_len, int * pos, int data_len)
474 + int start_pos = *(pos);
479 + // Set can_do to the most we can do in this copy.
481 + copy = MIN(circular_len - start_pos, data_len);
482 + memcpy(ring + start_pos, data + done, copy);
488 + if (start_pos == circular_len)
491 + *(pos) = start_pos;
496 +get_buffer(char * dst, char * ring, int ring_size, int * head, int to_copy)
500 + // |1|2|3|4|5|6|7|8|9|
505 + // Set can_do to the most we can do in this copy.
506 + copy = MIN(ring_size - *head, to_copy);
508 + // ast_log(LOG_DEBUG, "Getting: %d bytes, From pos %d\n", copy, *head);
509 + memcpy(dst, ring + *head, copy);
515 + if (*head == ring_size )
523 +/* Handle SCO audio sync.
525 + * If we are the MASTER, then we control the timing,
526 + * in 48 byte chunks. If we're the SLAVE, we send
527 + * as and when we recieve a packet.
529 + * Because of packet/timing nessecity, we
530 + * start up a thread when we're passing audio, so
531 + * that things are timed exactly right.
533 + * sco_thread() is the function that handles it.
538 +sco_thread(void * data)
540 + blt_dev_t * dev = (blt_dev_t*)data;
542 + struct pollfd pfd[2];
550 + // Avoid deadlock in odd circumstances
552 + ast_log(LOG_DEBUG, "SCO thread started on fd %d, pid %d\n", dev->sco, getpid());
554 + // dev->status = BLT_STATUS_IN_CALL;
555 + // ast_queue_control(dev->owner, AST_CONTROL_ANSWER);
556 + // Set buffer to silence, just incase.
558 + ast_mutex_lock(&(dev->sco_lock));
560 + memset(dev->sco_buf_in, 0x7f, BUFLEN);
561 + memset(dev->sco_buf_out, 0x7f, BUFLEN);
563 + dev->sco_pos_in = 0;
564 + dev->sco_pos_out = 0;
566 + ast_mutex_unlock(&(dev->sco_lock));
570 + ast_mutex_lock(&(dev->sco_lock));
572 + if (dev->sco_running != 1) {
573 + ast_log(LOG_DEBUG, "SCO stopped.\n");
577 + pfd[0].fd = dev->sco;
578 + pfd[0].events = POLLIN;
580 + pfd[1].fd = dev->sco_pipe[1];
581 + pfd[1].events = POLLIN;
583 + ast_mutex_unlock(&(dev->sco_lock));
585 + res = poll(pfd, 2, 50);
587 + if (res == -1 && errno != EINTR) {
588 + ast_log(LOG_DEBUG, "SCO poll() error\n");
595 + ast_mutex_lock(&(dev->sco_lock));
597 + if (pfd[0].revents & POLLIN) {
599 + len = read(dev->sco, buf, 48);
602 + ast_mutex_lock(&(dev->lock));
603 + set_buffer(dev->sco_buf_in, buf, BUFLEN, &in_pos, len);
604 + get_buffer(buf, dev->sco_buf_out, BUFLEN, &out_pos, len);
605 + write(dev->sco, buf, len);
606 + if (dev->owner && dev->owner->_state == AST_STATE_UP)
607 + write(dev->sco_pipe[1], &c, 1);
608 + ast_mutex_unlock(&(dev->lock));
611 + ast_mutex_unlock(&(dev->sco_lock));
613 + } else if (pfd[0].revents) {
615 + int e = sock_err(pfd[0].fd);
616 + ast_log(LOG_ERROR, "SCO connection error: %s (errno %d)\n", strerror(e), e);
619 + } else if (pfd[1].revents & POLLIN) {
623 + len = read(pfd[1].fd, &c, 1);
624 + sending = (sending) ? 0 : 1;
626 + ast_mutex_unlock(&(dev->sco_lock));
628 + } else if (pfd[1].revents) {
630 + int e = sock_err(pfd[1].fd);
631 + ast_log(LOG_ERROR, "SCO pipe connection event %d on pipe[1]=%d: %s (errno %d)\n", pfd[1].revents, pfd[1].fd, strerror(e), e);
635 + ast_log(LOG_NOTICE, "Unhandled poll output\n");
636 + ast_mutex_unlock(&(dev->sco_lock));
641 + ast_mutex_lock(&(dev->lock));
644 + dev->sco_running = -1;
645 + ast_mutex_unlock(&(dev->sco_lock));
647 + ast_queue_control(dev->owner, AST_CONTROL_HANGUP);
648 + ast_mutex_unlock(&(dev->lock));
649 + ast_log(LOG_DEBUG, "SCO thread stopped\n");
653 +/* Start SCO thread. Must be called with dev->lock */
656 +sco_start(blt_dev_t * dev, int fd)
659 + if (dev->sco_pipe[1] <= 0) {
660 + ast_log(LOG_ERROR, "SCO pipe[1] == %d\n", dev->sco_pipe[1]);
664 + ast_mutex_lock(&(dev->sco_lock));
666 + if (dev->sco_running != -1) {
667 + ast_log(LOG_ERROR, "Tried to start SCO thread while already running\n");
668 + ast_mutex_unlock(&(dev->sco_lock));
672 + if (dev->sco == -1) {
675 + } else if (sco_connect(dev) != 0) {
676 + ast_log(LOG_ERROR, "SCO fd invalid\n");
677 + ast_mutex_unlock(&(dev->sco_lock));
682 + dev->sco_running = 1;
684 + if (ast_pthread_create(&(dev->sco_thread), NULL, sco_thread, dev) < 0) {
685 + ast_log(LOG_ERROR, "Unable to start SCO thread.\n");
686 + dev->sco_running = -1;
687 + ast_mutex_unlock(&(dev->sco_lock));
691 + ast_mutex_unlock(&(dev->sco_lock));
696 +/* Stop SCO thread. Must be called with dev->lock */
699 +sco_stop(blt_dev_t * dev)
701 + ast_mutex_lock(&(dev->sco_lock));
702 + if (dev->sco_running == 1)
703 + dev->sco_running = 0;
705 + dev->sco_running = -1;
706 + dev->sco_sending = 0;
707 + ast_mutex_unlock(&(dev->sco_lock));
711 +/* ---------------------------------- */
713 +/* Answer the call. Call with lock held on device */
716 +answer(blt_dev_t * dev)
719 + if ( (!dev->owner) || (dev->ready != 1) || (dev->status != BLT_STATUS_READY && dev->status != BLT_STATUS_RINGING)) {
720 + ast_log(LOG_ERROR, "Attempt to answer() in invalid state (owner=%p, ready=%d, status=%s)\n",
721 + dev->owner, dev->ready, status2str(dev->status));
725 + // dev->sd = sco_connect(&local_bdaddr, &(dev->bdaddr), NULL, NULL, 0);
726 + // dev->status = BLT_STATUS_IN_CALL;
727 + // dev->owner->fds[0] = dev->sd;
728 + // if we are answering (hitting button):
729 + ast_queue_control(dev->owner, AST_CONTROL_ANSWER);
730 + // if asterisk signals us to answer:
731 + // ast_setstate(ast, AST_STATE_UP);
733 + /* Start SCO link */
734 + sco_start(dev, -1);
738 +/* ---------------------------------- */
741 +blt_write(struct ast_channel * ast, struct ast_frame * frame)
743 + blt_dev_t * dev = ast->pvt->pvt;
745 + /* Write a frame of (presumably voice) data */
747 + if (frame->frametype != AST_FRAME_VOICE) {
748 + ast_log(LOG_WARNING, "Don't know what to do with frame type '%d'\n", frame->frametype);
752 + if (!(frame->subclass & BLUETOOTH_FORMAT)) {
753 + ast_log(LOG_WARNING, "Cannot handle frames in format %d\n", frame->subclass);
757 + if (ast->_state != AST_STATE_UP) {
761 + ast_mutex_lock(&(dev->sco_lock));
762 + set_buffer(dev->sco_buf_out, frame->data, BUFLEN, &(dev->sco_pos_out), MIN(frame->datalen, BUFLEN));
763 + ast_mutex_unlock(&(dev->sco_lock));
769 +static struct ast_frame *
770 +blt_read(struct ast_channel * ast)
772 + blt_dev_t * dev = ast->pvt->pvt;
776 + /* Some nice norms */
778 + dev->fr.datalen = 0;
779 + dev->fr.samples = 0;
780 + dev->fr.data = NULL;
781 + dev->fr.src = BLT_CHAN_NAME;
782 + dev->fr.offset = 0;
783 + dev->fr.mallocd = 0;
784 + dev->fr.delivery.tv_sec = 0;
785 + dev->fr.delivery.tv_usec = 0;
787 + ast_mutex_lock(&(dev->sco_lock));
788 + dev->sco_sending = 1;
789 + read(dev->sco_pipe[0], &c, 1);
790 + len = get_buffer(dev->buf, dev->sco_buf_in, BUFLEN, &(dev->sco_pos_in), 48);
791 + ast_mutex_unlock(&(dev->sco_lock));
793 + dev->fr.data = dev->buf;
794 + dev->fr.samples = len / 2;
795 + dev->fr.datalen = len;
796 + dev->fr.frametype = AST_FRAME_VOICE;
797 + dev->fr.subclass = BLUETOOTH_FORMAT;
798 + dev->fr.offset = 0;
803 +/* Escape Any '"' in str. Return malloc()ed string */
805 +escape_str(char * str)
819 + ret = malloc(len + 1);
820 + pret = memset(ret, 0, len + 1);
834 +ring_hs(blt_dev_t * dev)
836 +#if (ASTERISK_VERSION_NUM < 010100)
837 + char tmp[AST_MAX_EXTENSION];
841 + ast_mutex_lock(&(dev->lock));
843 + if (dev->owner == NULL) {
844 + ast_mutex_unlock(&(dev->lock));
849 + dev->status = BLT_STATUS_RINGING;
851 + send_atcmd(dev, "RING");
853 + dev->owner->rings++;
855 + // XXX:T: '"' needs to be escaped in ELIP.
857 +#if (ASTERISK_VERSION_NUM < 010100)
859 + if (dev->owner->callerid) {
861 + memset(tmp, 0, sizeof(tmp));
862 + strncpy(tmp, dev->owner->callerid, sizeof(tmp)-1);
864 + if (!ast_callerid_parse(tmp, &name, &num)) {
866 + if (dev->clip && num)
867 + send_atcmd(dev, "+CLIP: \"%s\",129", num);
869 + if (dev->elip && name) {
870 + char * esc = escape_str(name);
871 + send_atcmd(dev, "*ELIP: \"%s\"", esc);
880 + if (dev->clip && dev->owner->cid.cid_num)
881 + send_atcmd(dev, "+CLIP: \"%s\",129", dev->owner->cid.cid_num);
883 + if (dev->elip && dev->owner->cid.cid_name) {
884 + char * esc = escape_str(dev->owner->cid.cid_name);
885 + send_atcmd(dev, "*ELIP: \"%s\"", esc);
891 + ast_mutex_unlock(&(dev->lock));
897 + * If the HS is already connected, then just send RING, otherwise, things get a
898 + * little more sticky. We first have to find the channel for HS using SDP,
899 + * then intiate the connection. Once we've done that, we can start the call.
903 +blt_call(struct ast_channel * ast, char * dest, int timeout)
905 + blt_dev_t * dev = ast->pvt->pvt;
907 + if ((ast->_state != AST_STATE_DOWN) && (ast->_state != AST_STATE_RESERVED)) {
908 + ast_log(LOG_WARNING, "blt_call called on %s, neither down nor reserved\n", ast->name);
912 + ast_log(LOG_DEBUG, "Calling %s on %s [t: %d]\n", dest, ast->name, timeout);
914 + if (ast_mutex_lock(&iface_lock)) {
915 + ast_log(LOG_ERROR, "Failed to get iface_lock.\n");
919 +// ast_mutex_lock(&(dev->lock));
921 + if (dev->ready == 0) {
922 + ast_log(LOG_WARNING, "Tried to call a device not ready/connected.\n");
923 + ast_setstate(ast, AST_CONTROL_CONGESTION);
924 +// ast_mutex_unlock(&(dev->lock));
925 + ast_mutex_unlock(&iface_lock);
929 + if (dev->role == BLT_ROLE_HS) {
931 + send_atcmd(dev, "+CIEV: 3,1");
933 + dev->ring_timer = ast_sched_add(sched, 5000, AST_SCHED_CB(ring_hs), dev);
937 + ast_setstate(ast, AST_STATE_RINGING);
938 + ast_queue_control(ast, AST_CONTROL_RINGING);
940 + } else if (dev->role == BLT_ROLE_AG) {
942 + send_atcmd(dev, "ATD%s;", dev->dnid);
946 + ast_setstate(ast, AST_CONTROL_CONGESTION);
947 + ast_log(LOG_ERROR, "Unknown device role\n");
951 +// ast_mutex_unlock(&(dev->lock));
952 + ast_mutex_unlock(&iface_lock);
958 +blt_hangup(struct ast_channel * ast)
960 + blt_dev_t * dev = ast->pvt->pvt;
962 + ast_log(LOG_DEBUG, "blt_hangup(%s)\n", ast->name);
964 + if (!ast->pvt->pvt) {
965 + ast_log(LOG_WARNING, "Asked to hangup channel not connected\n");
969 + if (ast_mutex_lock(&iface_lock)) {
970 + ast_log(LOG_ERROR, "Failed to get iface_lock\n");
974 + ast_mutex_lock(&(dev->lock));
977 + dev->sco_sending = 0;
979 + if (dev->role == BLT_ROLE_HS) {
981 + if (dev->ringing == 0) {
982 + // Actual call in progress
983 + send_atcmd(dev, "+CIEV: 2,0");
986 + // Just ringing still
988 + if (dev->role == BLT_ROLE_HS)
989 + send_atcmd(dev, "+CIEV: 3,0");
991 + if (dev->ring_timer >= 0)
992 + ast_sched_del(sched, dev->ring_timer);
994 + dev->ring_timer = -1;
999 + } else if (dev->role == BLT_ROLE_AG) {
1002 + send_atcmd(dev, "AT+CHUP");
1006 + if (dev->status == BLT_STATUS_IN_CALL || dev->status == BLT_STATUS_RINGING)
1007 + dev->status = BLT_STATUS_READY;
1009 + ast->pvt->pvt = NULL;
1010 + dev->owner = NULL;
1011 + ast_mutex_unlock(&(dev->lock));
1012 + ast_setstate(ast, AST_STATE_DOWN);
1013 + ast_mutex_unlock(&(iface_lock));
1019 +blt_indicate(struct ast_channel * c, int condition)
1021 + ast_log(LOG_DEBUG, "blt_indicate (%d)\n", condition);
1023 + switch(condition) {
1024 + case AST_CONTROL_RINGING:
1027 + ast_log(LOG_WARNING, "Don't know how to condition %d\n", condition);
1034 +blt_answer(struct ast_channel * ast)
1036 + blt_dev_t * dev = ast->pvt->pvt;
1038 + ast_mutex_lock(&dev->lock);
1040 + // if (dev->ring_timer >= 0)
1041 + // ast_sched_del(sched, dev->ring_timer);
1042 + // dev->ring_timer = -1;
1044 + ast_log(LOG_DEBUG, "Answering interface\n");
1046 + if (ast->_state != AST_STATE_UP) {
1047 + send_atcmd(dev, "+CIEV: 2,1");
1048 + send_atcmd(dev, "+CIEV: 3,0");
1049 + sco_start(dev, -1);
1050 + ast_setstate(ast, AST_STATE_UP);
1053 + ast_mutex_unlock(&dev->lock);
1058 +static struct ast_channel *
1059 +blt_new(blt_dev_t * dev, int state, const char * context, const char * number)
1061 + struct ast_channel * ast;
1064 + if ((ast = ast_channel_alloc(1)) == NULL) {
1065 + ast_log(LOG_WARNING, "Unable to allocate channel structure\n");
1069 + snprintf(ast->name, sizeof(ast->name), "BLT/%s", dev->name);
1071 + // ast->fds[0] = socket(PF_BLUETOOTH, SOCK_SEQPACKET, BTPROTO_SCO);
1073 + ast->nativeformats = BLUETOOTH_FORMAT;
1074 + ast->pvt->rawreadformat = BLUETOOTH_FORMAT;
1075 + ast->pvt->rawwriteformat = BLUETOOTH_FORMAT;
1076 + ast->writeformat = BLUETOOTH_FORMAT;
1077 + ast->readformat = BLUETOOTH_FORMAT;
1079 + ast_setstate(ast, state);
1081 + ast->type = BLT_CHAN_NAME;
1083 + ast->pvt->pvt = dev;
1085 + ast->pvt->call = blt_call;
1086 + ast->pvt->indicate = blt_indicate;
1087 + ast->pvt->hangup = blt_hangup;
1088 + ast->pvt->read = blt_read;
1089 + ast->pvt->write = blt_write;
1090 + ast->pvt->answer = blt_answer;
1092 + strncpy(ast->context, context, sizeof(ast->context)-1);
1093 + strncpy(ast->exten, number, sizeof(ast->exten) - 1);
1095 + ast->language[0] = '\0';
1097 + ast->fds[0] = dev->sco_pipe[0];
1098 + write(dev->sco_pipe[1], &c, 1);
1102 + ast_mutex_lock(&usecnt_lock);
1104 + ast_mutex_unlock(&usecnt_lock);
1106 + ast_update_use_count();
1108 + if (state != AST_STATE_DOWN) {
1109 + if (ast_pbx_start(ast)) {
1110 + ast_log(LOG_WARNING, "Unable to start PBX on %s\n", ast->name);
1118 +static struct ast_channel *
1119 +#if (ASTERISK_VERSION_NUM < 010100)
1120 +blt_request(char * type, int format, void * local_data)
1122 +blt_request(const char * type, int format, void * local_data)
1125 + char * data = (char*)local_data;
1127 + blt_dev_t * dev = NULL;
1128 + struct ast_channel * ast = NULL;
1129 + char * number = data, * dname;
1131 + dname = strsep(&number, "/");
1133 + oldformat = format;
1135 + format &= BLUETOOTH_FORMAT;
1138 + ast_log(LOG_NOTICE, "Asked to get a channel of unsupported format '%d'\n", oldformat);
1142 + ast_log(LOG_DEBUG, "Dialing '%s' via '%s'\n", number, dname);
1144 + if (ast_mutex_lock(&iface_lock)) {
1145 + ast_log(LOG_ERROR, "Unable to lock iface_list\n");
1152 + if (strcmp(dev->name, dname) == 0) {
1153 + ast_mutex_lock(&(dev->lock));
1154 + if (!dev->ready) {
1155 + ast_log(LOG_ERROR, "Device %s is not connected\n", dev->name);
1156 + ast_mutex_unlock(&(dev->lock));
1157 + ast_mutex_unlock(&iface_lock);
1165 + ast_mutex_unlock(&iface_lock);
1168 + ast_log(LOG_WARNING, "Failed to find device named '%s'\n", dname);
1172 + if (number && dev->role != BLT_ROLE_AG) {
1173 + ast_log(LOG_WARNING, "Tried to send a call out on non AG\n");
1174 + ast_mutex_unlock(&(dev->lock));
1178 + if (dev->role == BLT_ROLE_AG)
1179 + strncpy(dev->dnid, number, sizeof(dev->dnid) - 1);
1181 + ast = blt_new(dev, AST_STATE_DOWN, "bluetooth", "s");
1183 + ast_mutex_unlock(&(dev->lock));
1188 +/* ---------------------------------- */
1191 +/* ---- AT COMMAND SOCKET STUFF ---- */
1194 +send_atcmd(blt_dev_t * dev, const char * fmt, ...)
1200 + va_start(ap, fmt);
1201 + len = vsnprintf(buf, 1023, fmt, ap);
1204 + if (option_verbose)
1205 + ast_verbose(VERBOSE_PREFIX_1 "[%s] %*s < %s\n", role2str(dev->role), 10, dev->name, buf);
1207 + write(dev->rd, "\r\n", 2);
1208 + len = write(dev->rd, buf, len);
1209 + write(dev->rd, "\r\n", 2);
1210 + return (len) ? 0 : -1;
1215 +send_atcmd_ok(blt_dev_t * dev, const char * cmd)
1218 + strncpy(dev->last_ok_cmd, cmd, BLT_RDBUFF_MAX - 1);
1219 + if (option_verbose)
1220 + ast_verbose(VERBOSE_PREFIX_1 "[%s] %*s < OK\n", role2str(dev->role), 10, dev->name);
1221 + len = write(dev->rd, "\r\nOK\r\n", 6);
1222 + return (len) ? 0 : -1;
1226 +send_atcmd_error(blt_dev_t * dev)
1230 + if (option_verbose)
1231 + ast_verbose(VERBOSE_PREFIX_1 "[%s] %*s < ERROR\n", role2str(dev->role), 10, dev->name);
1233 +// write(dev->rd, "\r\n", 2);
1234 +// len = write(dev->rd, dev->last_ok_cmd, 5);
1235 + write(dev->rd, "\r\n", 2);
1236 + len = write(dev->rd, "ERROR", 5);
1237 + write(dev->rd, "\r\n", 2);
1239 + return (len) ? 0 : -1;
1243 +/* ---------------------------------- */
1245 +/* -- Handle negotiation when we're an AG -- */
1247 +/* Bluetooth Support */
1250 +atcmd_brsf_set(blt_dev_t * dev, const char * arg, int len)
1252 + ast_log(LOG_DEBUG, "Device Supports: %s\n", arg);
1253 + dev->brsf = atoi(arg);
1254 + send_atcmd(dev, "+BRSF: %d", 23);
1258 +/* Bluetooth Voice Recognition */
1261 +atcmd_bvra_set(blt_dev_t * dev, const char * arg, int len)
1263 + ast_log(LOG_WARNING, "+BVRA Not Yet Supported\n");
1266 + // XXX:T: Fix voice recognition somehow!
1267 + int action = atoi(arg);
1268 + ast_log(LOG_DEBUG, "Voice Recognition: %s\n", (a) ? "ACTIVATED" : "DEACTIVATED");
1269 + if ((action == 0) & (dev->bvra == 1)) {
1272 + // XXX:T: Shutdown any active bvra channel
1273 + ast_log(LOG_DEBUG, "Voice Recognition: DISABLED\n");
1274 + } else if ((action == 1) && (dev->bvra == 0)) {
1277 + // XXX:T: Schedule connection to voice recognition extension/application
1278 + ast_log(LOG_DEBUG, "Voice Recognition: ENABLED\n");
1280 + ast_log(LOG_ERROR, "+BVRA out of sync (we think %d, but HS wants %d)\n", dev->bvra, action);
1290 +atcmd_cclk_read(blt_dev_t * dev)
1293 + const time_t ti = time(0);
1294 + tp = localtime_r(&ti, &t);
1295 + send_atcmd(dev, "+CCLK: \"%02d/%02d/%02d,%02d:%02d:%02d+%02d\"",
1296 + (tp->tm_year % 100), (tp->tm_mon + 1), (tp->tm_mday),
1297 + tp->tm_hour, tp->tm_min, tp->tm_sec, ((tp->tm_gmtoff / 60) / 15));
1301 +/* CHUP - Hangup Call */
1304 +atcmd_chup_execute(blt_dev_t * dev, const char * data)
1306 + if (!dev->owner) {
1307 + ast_log(LOG_ERROR, "Request to hangup call when none in progress\n");
1310 + ast_log(LOG_DEBUG, "Hangup Call\n");
1311 + ast_queue_control(dev->owner, AST_CONTROL_HANGUP);
1315 +/* CIND - Call Indicator */
1318 +atcmd_cind_read(blt_dev_t * dev)
1320 + send_atcmd(dev, "+CIND: 1,0,0");
1325 +atcmd_cind_test(blt_dev_t * dev)
1327 + send_atcmd(dev, "+CIND: (\"service\",(0,1)),(\"call\",(0,1)),(\"callsetup\",(0-4))");
1334 +atcmd_clan_read(blt_dev_t * dev)
1336 + send_atcmd(dev, "+CLAN: \"en\"");
1340 +/* Caller Id Presentation */
1343 +atcmd_clip_set(blt_dev_t * dev, const char * arg, int len)
1345 + dev->clip = atoi(arg);
1349 +/* Conneced Line Identification Presentation */
1352 +atcmd_colp_set(blt_dev_t * dev, const char * arg, int len)
1354 + dev->colp = atoi(arg);
1358 +/* CMER - Mobile Equipment Event Reporting */
1361 +atcmd_cmer_set(blt_dev_t * dev, const char * arg, int len)
1364 + dev->status = BLT_STATUS_READY;
1368 +/* PhoneBook Types:
1370 + * - FD - SIM Fixed Dialing Phone Book
1371 + * - ME - ME Phone book
1372 + * - SM - SIM Phone Book
1373 + * - DC - ME dialled-calls list
1374 + * - RC - ME recieved-calls lisr
1375 + * - MC - ME missed-calls list
1376 + * - MV - ME Voice Activated Dialing List
1377 + * - HP - Hierachial Phone Book
1378 + * - BC - Own Business Card (PIN2 required)
1382 +/* Read Phone Book Entry */
1385 +atcmd_cpbr_set(blt_dev_t * dev, const char * arg, int len)
1387 + // XXX:T: Fix the phone book!
1388 + // * Maybe add res_phonebook or something? */
1389 + send_atcmd(dev, "+CPBR: %d,\"%s\",128,\"%s\"", atoi(arg), arg, arg);
1393 +/* Select Phone Book */
1396 +atcmd_cpbs_set(blt_dev_t * dev, const char * arg, int len)
1398 + // XXX:T: I guess we'll just accept any?
1403 +atcmd_cscs_set(blt_dev_t * dev, const char * arg, int len)
1405 + // XXX:T: Language
1410 +atcmd_eips_set(blt_dev_t * dev, const char * arg, int len)
1412 + ast_log(LOG_DEBUG, "Identify Presentation Set: %s=%s\n",
1413 + (*(arg) == 49) ? "ELIP" : "EOLP",
1414 + (*(arg+2) == 49) ? "ON" : "OFF");
1417 + dev->eolp = (*(arg+2) == 49) ? 1 : 0;
1419 + dev->elip = (*(arg+2) == 49) ? 1 : 0;
1424 +/* VGS - Speaker Volume Gain */
1427 +atcmd_vgs_set(blt_dev_t * dev, const char * arg, int len)
1429 + dev->gain_speaker = atoi(arg);
1435 +atcmd_dial_execute(blt_dev_t * dev, const char * data)
1437 + char * number = NULL;
1439 + /* Make sure there is a ';' at the end of the line */
1440 + if (*(data + (strlen(data) - 1)) != ';') {
1441 + ast_log(LOG_WARNING, "Can't dial non-voice right now: %s\n", data);
1445 + number = strndup(data, strlen(data) - 1);
1446 + ast_log(LOG_NOTICE, "Dial: [%s]\n", number);
1448 + send_atcmd(dev, "+CIEV: 2,1");
1449 + send_atcmd(dev, "+CIEV: 3,0");
1451 + sco_start(dev, -1);
1453 + if (blt_new(dev, AST_STATE_UP, "bluetooth", number) == NULL) {
1465 +atcmd_answer_execute(blt_dev_t * dev, const char * data)
1468 + if (!dev->ringing || !dev->owner) {
1469 + ast_log(LOG_WARNING, "Can't answer non existant call\n");
1475 + if (dev->ring_timer >= 0)
1476 + ast_sched_del(sched, dev->ring_timer);
1478 + dev->ring_timer = -1;
1480 + send_atcmd(dev, "+CIEV: 2,1");
1481 + send_atcmd(dev, "+CIEV: 3,0");
1483 + return answer(dev);
1487 +ag_unsol_ciev(blt_dev_t * dev, const char * data)
1489 + const char * orig = data;
1493 + while (*(data) && *(data) == ' ')
1496 + if (*(data) == 0) {
1497 + ast_log(LOG_WARNING, "Invalid value[1] for '+CIEV:%s'\n", orig);
1501 + indicator = *(data++) - 48;
1503 + if (*(data++) != ',') {
1504 + ast_log(LOG_WARNING, "Invalid value[2] for '+CIEV:%s'\n", orig);
1508 + if (*(data) == 0) {
1509 + ast_log(LOG_WARNING, "Invalid value[3] for '+CIEV:%s'\n", orig);
1513 + status = *(data) - 48;
1515 + set_cind(dev, indicator, status);
1521 +ag_unsol_cind(blt_dev_t * dev, const char * data)
1524 + while (*(data) && *(data) == ' ')
1528 + if (dev->cind == 0)
1533 + while ((data = parse_cind(data, name, 1023)) != NULL) {
1534 + ast_log(LOG_DEBUG, "CIND: %d=%s\n", pos, name);
1535 + if (strcmp(name, "call") == 0)
1536 + dev->call_pos = pos;
1537 + else if (strcmp(name, "service") == 0)
1538 + dev->service_pos = pos;
1539 + else if (strcmp(name, "call_setup") == 0 || strcmp(name, "callsetup") == 0)
1540 + dev->callsetup_pos = pos;
1544 + ast_log(LOG_DEBUG, "CIND: %d=%s\n", pos, name);
1548 + int pos = 1, len = 0;
1550 + const char * start = data;
1553 + if (*data == ',') {
1554 + memset(val, 0, 128);
1555 + strncpy(val, start, len);
1556 + set_cind(dev, pos, atoi(val));
1567 + memset(val, 0, 128);
1568 + strncpy(val, start, len);
1569 + ast_log(LOG_DEBUG, "CIND IND %d set to %d [%s]\n", pos, atoi(val), val);
1580 + { "A", NULL, NULL, atcmd_answer_execute, NULL, NULL },
1581 + { "D", NULL, NULL, atcmd_dial_execute, NULL, NULL },
1582 + { "+BRSF", atcmd_brsf_set, NULL, NULL, NULL, NULL },
1583 + { "+BVRA", atcmd_bvra_set, NULL, NULL, NULL, NULL },
1584 + { "+CCLK", NULL, atcmd_cclk_read, NULL, NULL, NULL },
1585 + { "+CHUP", NULL, NULL, atcmd_chup_execute, NULL, NULL },
1586 + { "+CIEV", NULL, NULL, NULL, NULL, ag_unsol_ciev },
1587 + { "+CIND", NULL, atcmd_cind_read, NULL, atcmd_cind_test, ag_unsol_cind },
1588 + { "+CLAN", NULL, atcmd_clan_read, NULL, NULL, NULL },
1589 + { "+CLIP", atcmd_clip_set, NULL, NULL, NULL, NULL },
1590 + { "+COLP", atcmd_colp_set, NULL, NULL, NULL, NULL },
1591 + { "+CMER", atcmd_cmer_set, NULL, NULL, NULL, NULL },
1592 + { "+CPBR", atcmd_cpbr_set, NULL, NULL, NULL, NULL },
1593 + { "+CPBS", atcmd_cpbs_set, NULL, NULL, NULL, NULL },
1594 + { "+CSCS", atcmd_cscs_set, NULL, NULL, NULL, NULL },
1595 + { "*EIPS", atcmd_eips_set, NULL, NULL, NULL, NULL },
1596 + { "+VGS", atcmd_vgs_set, NULL, NULL, NULL, NULL },
1599 +#define ATCMD_LIST_LEN (sizeof(atcmd_list) / sizeof(blt_atcb_t))
1601 +/* ---------------------------------- */
1603 +/* -- Handle negotiation when we're a HS -- */
1606 +ag_unknown_response(blt_dev_t * dev, char * cmd)
1608 + ast_log(LOG_DEBUG, "Got UNKN response: %s\n", cmd);
1616 +ag_cgmi_response(blt_dev_t * dev, char * cmd)
1618 + // CGMM - Phone Model
1619 + // CGMR - Phone Revision
1622 + // VTS - send tone
1626 + // CSMS - SMS STUFFS
1630 + // CSCA - sms CENTER NUMBER
1631 + // CNMI - SMS INDICATION
1632 + // ast_log(LOG_DEBUG, "Manufacturer: %s\n", cmd);
1633 + dev->cb = ag_unknown_response;
1637 +ag_cgmi_valid_response(blt_dev_t * dev, char * cmd)
1639 + // send_atcmd(dev, "AT+WS46?");
1640 + // send_atcmd(dev, "AT+CRC=1");
1641 + // send_atcmd(dev, "AT+CNUM");
1643 + if (strcmp(cmd, "OK") == 0) {
1644 + send_atcmd(dev, "AT+CGMI");
1645 + dev->cb = ag_cgmi_response;
1647 + dev->cb = ag_unknown_response;
1652 +ag_clip_response(blt_dev_t * dev, char * cmd)
1654 + send_atcmd(dev, "AT+CGMI=?");
1655 + dev->cb = ag_cgmi_valid_response;
1659 +ag_cmer_response(blt_dev_t * dev, char * cmd)
1661 + dev->cb = ag_clip_response;
1663 + dev->status = BLT_STATUS_READY;
1664 + send_atcmd(dev, "AT+CLIP=1");
1668 +ag_cind_status_response(blt_dev_t * dev, char * cmd)
1670 + // XXX:T: Handle response.
1671 + dev->cb = ag_cmer_response;
1672 + send_atcmd(dev, "AT+CMER=3,0,0,1");
1673 + // Initiase SCO link!
1677 +ag_cind_response(blt_dev_t * dev, char * cmd)
1679 + dev->cb = ag_cind_status_response;
1681 + send_atcmd(dev, "AT+CIND?");
1685 +ag_brsf_response(blt_dev_t * dev, char * cmd)
1687 + dev->cb = ag_cind_response;
1688 + ast_log(LOG_DEBUG, "Bluetooth features: %s\n", cmd);
1690 + send_atcmd(dev, "AT+CIND=?");
1693 +/* ---------------------------------- */
1696 +sdp_register(sdp_session_t * session)
1698 + // XXX:T: Fix this horrible function so it makes some sense and is extensible!
1699 + sdp_list_t *svclass_id, *pfseq, *apseq, *root;
1700 + uuid_t root_uuid, svclass_uuid, ga_svclass_uuid, l2cap_uuid, rfcomm_uuid;
1701 + sdp_profile_desc_t profile;
1702 + sdp_list_t *aproto, *proto[2];
1703 + sdp_record_t record;
1704 + uint8_t u8 = rfcomm_channel_ag;
1705 + uint8_t u8_hs = rfcomm_channel_hs;
1706 + sdp_data_t *channel;
1709 + memset((void *)&record, 0, sizeof(sdp_record_t));
1710 + record.handle = 0xffffffff;
1711 + sdp_uuid16_create(&root_uuid, PUBLIC_BROWSE_GROUP);
1712 + root = sdp_list_append(0, &root_uuid);
1713 + sdp_set_browse_groups(&record, root);
1715 + // Register as an AG
1717 + sdp_uuid16_create(&svclass_uuid, HANDSFREE_AUDIO_GW_SVCLASS_ID);
1718 + svclass_id = sdp_list_append(0, &svclass_uuid);
1719 + sdp_uuid16_create(&ga_svclass_uuid, GENERIC_AUDIO_SVCLASS_ID);
1720 + svclass_id = sdp_list_append(svclass_id, &ga_svclass_uuid);
1721 + sdp_set_service_classes(&record, svclass_id);
1722 + sdp_uuid16_create(&profile.uuid, 0x111f);
1723 + profile.version = 0x0100;
1724 + pfseq = sdp_list_append(0, &profile);
1726 + sdp_set_profile_descs(&record, pfseq);
1728 + sdp_uuid16_create(&l2cap_uuid, L2CAP_UUID);
1729 + proto[0] = sdp_list_append(0, &l2cap_uuid);
1730 + apseq = sdp_list_append(0, proto[0]);
1732 + sdp_uuid16_create(&rfcomm_uuid, RFCOMM_UUID);
1733 + proto[1] = sdp_list_append(0, &rfcomm_uuid);
1734 + channel = sdp_data_alloc(SDP_UINT8, &u8);
1735 + proto[1] = sdp_list_append(proto[1], channel);
1736 + apseq = sdp_list_append(apseq, proto[1]);
1738 + aproto = sdp_list_append(0, apseq);
1739 + sdp_set_access_protos(&record, aproto);
1741 + sdp_set_info_attr(&record, "Voice Gateway", 0, 0);
1743 + if (sdp_record_register(session, &record, SDP_RECORD_PERSIST) < 0) {
1744 + ast_log(LOG_ERROR, "Service Record registration failed\n");
1749 + sdp_record_ag = record.handle;
1751 + ast_log(LOG_NOTICE, "HeadsetAudioGateway service registered\n");
1753 + sdp_data_free(channel);
1754 + sdp_list_free(proto[0], 0);
1755 + sdp_list_free(proto[1], 0);
1756 + sdp_list_free(apseq, 0);
1757 + sdp_list_free(aproto, 0);
1761 + memset((void *)&record, 0, sizeof(sdp_record_t));
1762 + record.handle = 0xffffffff;
1763 + sdp_uuid16_create(&root_uuid, PUBLIC_BROWSE_GROUP);
1764 + root = sdp_list_append(0, &root_uuid);
1765 + sdp_set_browse_groups(&record, root);
1767 + // Register as an HS
1769 + sdp_uuid16_create(&svclass_uuid, HANDSFREE_AUDIO_GW_SVCLASS_ID);
1770 + svclass_id = sdp_list_append(0, &svclass_uuid);
1771 + sdp_uuid16_create(&ga_svclass_uuid, GENERIC_AUDIO_SVCLASS_ID);
1772 + svclass_id = sdp_list_append(svclass_id, &ga_svclass_uuid);
1773 + sdp_set_service_classes(&record, svclass_id);
1774 + sdp_uuid16_create(&profile.uuid, 0x111e);
1775 + profile.version = 0x0100;
1776 + pfseq = sdp_list_append(0, &profile);
1777 + sdp_set_profile_descs(&record, pfseq);
1779 + sdp_uuid16_create(&l2cap_uuid, L2CAP_UUID);
1780 + proto[0] = sdp_list_append(0, &l2cap_uuid);
1781 + apseq = sdp_list_append(0, proto[0]);
1783 + sdp_uuid16_create(&rfcomm_uuid, RFCOMM_UUID);
1784 + proto[1] = sdp_list_append(0, &rfcomm_uuid);
1785 + channel = sdp_data_alloc(SDP_UINT8, &u8_hs);
1786 + proto[1] = sdp_list_append(proto[1], channel);
1787 + apseq = sdp_list_append(apseq, proto[1]);
1789 + aproto = sdp_list_append(0, apseq);
1790 + sdp_set_access_protos(&record, aproto);
1791 + sdp_set_info_attr(&record, "Voice Gateway", 0, 0);
1793 + if (sdp_record_register(session, &record, SDP_RECORD_PERSIST) < 0) {
1794 + ast_log(LOG_ERROR, "Service Record registration failed\n");
1799 + sdp_record_hs = record.handle;
1801 + ast_log(LOG_NOTICE, "HeadsetAudioGateway service registered\n");
1804 + sdp_data_free(channel);
1805 + sdp_list_free(proto[0], 0);
1806 + sdp_list_free(proto[1], 0);
1807 + sdp_list_free(apseq, 0);
1808 + sdp_list_free(aproto, 0);
1814 +rfcomm_listen(bdaddr_t * bdaddr, int channel)
1818 + struct sockaddr_rc loc_addr;
1821 + if ((sock = socket(PF_BLUETOOTH, SOCK_STREAM, BTPROTO_RFCOMM)) < 0) {
1822 + ast_log(LOG_ERROR, "Can't create socket: %s (errno: %d)\n", strerror(errno), errno);
1826 + loc_addr.rc_family = AF_BLUETOOTH;
1828 + /* Local Interface Address */
1829 + bacpy(&loc_addr.rc_bdaddr, bdaddr);
1832 + loc_addr.rc_channel = channel;
1834 + if (bind(sock, (struct sockaddr *)&loc_addr, sizeof(loc_addr)) < 0) {
1835 + ast_log(LOG_ERROR, "Can't bind socket: %s (errno: %d)\n", strerror(errno), errno);
1840 + if (setsockopt(sock, SOL_SOCKET, SO_REUSEADDR, &on, sizeof(on)) == -1) {
1841 + ast_log(LOG_ERROR, "Set socket SO_REUSEADDR option on failed: errno %d, %s", errno, strerror(errno));
1846 + if (fcntl(sock, F_SETFL, O_RDWR|O_NONBLOCK) != 0)
1847 + ast_log(LOG_ERROR, "Can't set RFCOMM socket to NBIO\n");
1849 + if (listen(sock, 10) < 0) {
1850 + ast_log(LOG_ERROR,"Can not listen on the socket. %s(%d)\n", strerror(errno), errno);
1855 + ast_log(LOG_NOTICE, "Listening for RFCOMM channel %d connections on FD %d\n", channel, sock);
1862 +sco_listen(bdaddr_t * bdaddr)
1866 + struct sockaddr_sco loc_addr;
1868 + memset(&loc_addr, 0, sizeof(loc_addr));
1870 + if ((sock = socket(PF_BLUETOOTH, SOCK_SEQPACKET, BTPROTO_SCO)) < 0) {
1871 + ast_log(LOG_ERROR, "Can't create SCO socket: %s (errno: %d)\n", strerror(errno), errno);
1875 + loc_addr.sco_family = AF_BLUETOOTH;
1876 + bacpy(&loc_addr.sco_bdaddr, BDADDR_ANY);
1878 + if (bind(sock, (struct sockaddr *)&loc_addr, sizeof(loc_addr)) < 0) {
1879 + ast_log(LOG_ERROR, "Can't bind SCO socket: %s (errno: %d)\n", strerror(errno), errno);
1884 + if (setsockopt(sock, SOL_SOCKET, SO_REUSEADDR, &on, sizeof(on)) == -1) {
1885 + ast_log(LOG_ERROR, "Set SCO socket SO_REUSEADDR option on failed: errno %d, %s", errno, strerror(errno));
1890 + if (fcntl(sock, F_SETFL, O_RDWR|O_NONBLOCK) != 0)
1891 + ast_log(LOG_ERROR, "Can't set SCO socket to NBIO\n");
1893 + if (listen(sock, 10) < 0) {
1894 + ast_log(LOG_ERROR,"Can not listen on SCO socket: %s(%d)\n", strerror(errno), errno);
1899 + ast_log(LOG_NOTICE, "Listening for SCO connections on FD %d\n", sock);
1905 +rfcomm_connect(bdaddr_t * src, bdaddr_t * dst, int channel, int nbio)
1907 + struct sockaddr_rc addr;
1910 + if ((s = socket(PF_BLUETOOTH, SOCK_STREAM, BTPROTO_RFCOMM)) < 0) {
1914 + memset(&addr, 0, sizeof(addr));
1915 + addr.rc_family = AF_BLUETOOTH;
1916 + bacpy(&addr.rc_bdaddr, src);
1917 + addr.rc_channel = 0;
1919 + if (bind(s, (struct sockaddr *)&addr, sizeof(addr)) < 0) {
1924 + memset(&addr, 0, sizeof(addr));
1925 + addr.rc_family = AF_BLUETOOTH;
1926 + bacpy(&addr.rc_bdaddr, dst);
1927 + addr.rc_channel = channel;
1930 + if (fcntl(s, F_SETFL, O_RDWR|O_NONBLOCK) != 0)
1931 + ast_log(LOG_ERROR, "Can't set RFCOMM socket to NBIO\n");
1934 + if (connect(s, (struct sockaddr *)&addr, sizeof(addr)) < 0 && (nbio != 1 || (errno != EAGAIN))) {
1942 +/* Must be called with dev->lock held */
1945 +sco_connect(blt_dev_t * dev)
1947 + struct sockaddr_sco addr;
1948 + // struct sco_conninfo conn;
1949 + // struct sco_options opts;
1951 + // bdaddr_t * src = &local_bdaddr;
1954 + bdaddr_t * dst = &(dev->bdaddr);
1956 + if (dev->sco != -1) {
1957 + ast_log(LOG_ERROR, "SCO fd already open.\n");
1961 + if ((s = socket(PF_BLUETOOTH, SOCK_SEQPACKET, BTPROTO_SCO)) < 0) {
1962 + ast_log(LOG_ERROR, "Can't create SCO socket(): %s\n", strerror(errno));
1966 + memset(&addr, 0, sizeof(addr));
1968 + addr.sco_family = AF_BLUETOOTH;
1969 + bacpy(&addr.sco_bdaddr, BDADDR_ANY);
1971 + if (bind(s, (struct sockaddr *)&addr, sizeof(addr)) < 0) {
1972 + ast_log(LOG_ERROR, "Can't bind() SCO socket: %s\n", strerror(errno));
1977 + memset(&addr, 0, sizeof(addr));
1978 + addr.sco_family = AF_BLUETOOTH;
1979 + bacpy(&addr.sco_bdaddr, dst);
1981 + if (fcntl(s, F_SETFL, O_RDWR|O_NONBLOCK) != 0)
1982 + ast_log(LOG_ERROR, "Can't set SCO socket to NBIO\n");
1984 + if ((connect(s, (struct sockaddr *)&addr, sizeof(addr)) < 0) && (errno != EAGAIN)) {
1985 + ast_log(LOG_ERROR, "Can't connect() SCO socket: %s (errno %d)\n", strerror(errno), errno);
1990 + //size = sizeof(conn);
1993 +/* XXX:T: HERE, fix getting SCO conninfo.
1995 + if (getsockopt(s, SOL_SCO, SCO_CONNINFO, &conn, &size) < 0) {
1996 + ast_log(LOG_ERROR, "Can't getsockopt SCO_CONNINFO on SCO socket: %s\n", strerror(errno));
2001 + size = sizeof(opts);
2003 + if (getsockopt(s, SOL_SCO, SCO_OPTIONS, &opts, &size) < 0) {
2004 + ast_log(LOG_ERROR, "Can't getsockopt SCO_OPTIONS on SCO socket: %s\n", strerror(errno));
2009 + dev->sco_handle = conn.hci_handle;
2010 + dev->sco_mtu = opts.mtu;
2014 + ast_log(LOG_DEBUG, "SCO: %d\n", s);
2022 +/* ---------------------------------- */
2024 +/* Non blocking (async) outgoing bluetooth connection */
2027 +try_connect(blt_dev_t * dev)
2030 + ast_mutex_lock(&(dev->lock));
2032 + if (dev->status != BLT_STATUS_CONNECTING && dev->status != BLT_STATUS_DOWN) {
2033 + ast_mutex_unlock(&(dev->lock));
2037 + if (dev->rd != -1) {
2040 + struct pollfd pfd;
2042 + if (dev->status != BLT_STATUS_CONNECTING) {
2043 + ast_mutex_unlock(&(dev->lock));
2044 + dev->outgoing_id = -1;
2048 + // ret = connect(dev->rd, (struct sockaddr *)&(dev->addr), sizeof(struct sockaddr_rc)); //
2051 + pfd.events = POLLIN | POLLOUT;
2053 + ret = poll(&pfd, 1, 0);
2058 + dev->status = BLT_STATUS_DOWN;
2059 + dev->outgoing_id = ast_sched_add(sched, 10000, AST_SCHED_CB(try_connect), dev);
2060 + ast_mutex_unlock(&(dev->lock));
2066 + int len = sizeof(ret);
2067 + getsockopt(dev->rd, SOL_SOCKET, SO_ERROR, &ret, &len);
2071 + ast_log(LOG_NOTICE, "Initialised bluetooth link to device %s\n", dev->name);
2075 + struct hci_conn_info_req * cr;
2079 + cr = malloc(sizeof(*cr) + sizeof(struct hci_conn_info));
2080 + dd = hci_open_dev(hcidev_id);
2081 + cr->type = ACL_LINK;
2082 + bacpy(&cr->bdaddr, &(dev->bdaddr));
2084 + if (ioctl(dd, HCIGETCONNINFO, (unsigned long)cr) < 0) {
2085 + ast_log(LOG_ERROR, "Failed to get connection info: %s\n", strerror(errno));
2087 + ast_log(LOG_DEBUG, "HCI Handle: %d\n", cr->conn_info->handle);
2090 + if (hci_read_remote_name(dd, &(dev->bdaddr), sizeof(name), name, 25000) == 0)
2091 + ast_log(LOG_DEBUG, "Remote Name: %s\n", name);
2096 + dev->status = BLT_STATUS_NEGOTIATING;
2098 + /* If this device is a AG, we initiate the negotiation. */
2100 + if (dev->role == BLT_ROLE_AG) {
2101 + dev->cb = ag_brsf_response;
2102 + send_atcmd(dev, "AT+BRSF=23");
2105 + dev->outgoing_id = -1;
2106 + ast_mutex_unlock(&(dev->lock));
2111 + if (ret != EHOSTDOWN)
2112 + ast_log(LOG_NOTICE, "Connect to device %s failed: %s (errno %d)\n", dev->name, strerror(ret), ret);
2116 + dev->status = BLT_STATUS_DOWN;
2117 + dev->outgoing_id = ast_sched_add(sched, (ret == EHOSTDOWN) ? 10000 : 2500, AST_SCHED_CB(try_connect), dev);
2118 + ast_mutex_unlock(&(dev->lock));
2125 + dev->outgoing_id = ast_sched_add(sched, 100, AST_SCHED_CB(try_connect), dev);
2126 + ast_mutex_unlock(&(dev->lock));
2130 + fd = rfcomm_connect(&local_bdaddr, &(dev->bdaddr), dev->channel, 1);
2133 + ast_log(LOG_WARNING, "NBIO connect() to %s returned %d: %s\n", dev->name, errno, strerror(errno));
2134 + dev->outgoing_id = ast_sched_add(sched, 5000, AST_SCHED_CB(try_connect), dev);
2135 + ast_mutex_unlock(&(dev->lock));
2140 + dev->status = BLT_STATUS_CONNECTING;
2141 + dev->outgoing_id = ast_sched_add(sched, 100, AST_SCHED_CB(try_connect), dev);
2142 + ast_mutex_unlock(&(dev->lock));
2147 +/* Called whenever a new command is recieved while we're the AG */
2151 +process_rfcomm_cmd(blt_dev_t * dev, char * cmd)
2154 + char * fullcmd = cmd;
2156 + if (option_verbose)
2157 + ast_verbose(VERBOSE_PREFIX_1 "[%s] %*s > %s\n", role2str(dev->role), 10, dev->name, cmd);
2159 + /* Read the 'AT' from the start of the string */
2160 + if (strncmp(cmd, "AT", 2)) {
2161 + ast_log(LOG_WARNING, "Unknown command without 'AT': %s\n", cmd);
2162 + send_atcmd_error(dev);
2168 + // Don't forget 'AT' on it's own is OK.
2170 + if (strlen(cmd) == 0) {
2171 + send_atcmd_ok(dev, fullcmd);
2175 + for (i = 0 ; i < ATCMD_LIST_LEN ; i++) {
2176 + if (strncmp(atcmd_list[i].str, cmd, strlen(atcmd_list[i].str)) == 0) {
2177 + char * pos = (cmd + strlen(atcmd_list[i].str));
2178 + if ((strncmp(pos, "=?", 2) == 0) && (strlen(pos) == 2)) {
2179 + /* TEST command */
2180 + if (atcmd_list[i].test) {
2181 + if (atcmd_list[i].test(dev) == 0)
2182 + send_atcmd_ok(dev, fullcmd);
2184 + send_atcmd_error(dev);
2186 + send_atcmd_ok(dev, fullcmd);
2188 + } else if ((strncmp(pos, "?", 1) == 0) && (strlen(pos) == 1)) {
2189 + /* READ command */
2190 + if (atcmd_list[i].read) {
2191 + if (atcmd_list[i].read(dev) == 0)
2192 + send_atcmd_ok(dev, fullcmd);
2194 + send_atcmd_error(dev);
2196 + ast_log(LOG_WARNING, "AT Command: '%s' missing READ function\n", fullcmd);
2197 + send_atcmd_error(dev);
2199 + } else if (strncmp(pos, "=", 1) == 0) {
2201 + if (atcmd_list[i].set) {
2202 + if (atcmd_list[i].set(dev, (pos + 1), (*(pos + 1)) ? strlen(pos + 1) : 0) == 0)
2203 + send_atcmd_ok(dev, fullcmd);
2205 + send_atcmd_error(dev);
2207 + ast_log(LOG_WARNING, "AT Command: '%s' missing SET function\n", fullcmd);
2208 + send_atcmd_error(dev);
2211 + /* EXECUTE command */
2212 + if (atcmd_list[i].execute) {
2213 + if (atcmd_list[i].execute(dev, cmd + strlen(atcmd_list[i].str)) == 0)
2214 + send_atcmd_ok(dev, fullcmd);
2216 + send_atcmd_error(dev);
2218 + ast_log(LOG_WARNING, "AT Command: '%s' missing EXECUTE function\n", fullcmd);
2219 + send_atcmd_error(dev);
2226 + ast_log(LOG_WARNING, "Unknown AT Command: '%s' (%s)\n", fullcmd, cmd);
2227 + send_atcmd_error(dev);
2232 +/* Called when a socket is incoming */
2235 +handle_incoming(int fd, blt_role_t role)
2238 + struct sockaddr_rc addr;
2239 + int len = sizeof(addr);
2241 + // Got a new incoming socket.
2242 + ast_log(LOG_DEBUG, "Incoming RFCOMM socket\n");
2244 + ast_mutex_lock(&iface_lock);
2246 + fd = accept(fd, (struct sockaddr*)&addr, &len);
2250 + if (bacmp(&(dev->bdaddr), &addr.rc_bdaddr) == 0) {
2251 + ast_log(LOG_DEBUG, "Connect from %s\n", dev->name);
2252 + ast_mutex_lock(&(dev->lock));
2253 + /* Kill any outstanding connect attempt. */
2254 + if (dev->outgoing_id > -1) {
2255 + ast_sched_del(sched, dev->outgoing_id);
2256 + dev->outgoing_id = -1;
2259 + rd_close(dev, 0, 0);
2261 + dev->status = BLT_STATUS_NEGOTIATING;
2264 + if (dev->role == BLT_ROLE_AG) {
2265 + dev->cb = ag_brsf_response;
2266 + send_atcmd(dev, "AT+BRSF=23");
2269 + ast_mutex_unlock(&(dev->lock));
2275 + if (dev == NULL) {
2276 + ast_log(LOG_WARNING, "Connect from unknown device\n");
2279 + ast_mutex_unlock(&iface_lock);
2285 +handle_incoming_sco(int master)
2289 + struct sockaddr_sco addr;
2290 + struct sco_conninfo conn;
2291 + struct sco_options opts;
2292 + int len = sizeof(addr);
2295 + ast_log(LOG_DEBUG, "Incoming SCO socket\n");
2297 + fd = accept(master, (struct sockaddr*)&addr, &len);
2299 + if (fcntl(fd, F_SETFL, O_RDWR|O_NONBLOCK) != 0) {
2300 + ast_log(LOG_ERROR, "Can't set SCO socket to NBIO\n");
2305 + len = sizeof(conn);
2307 + if (getsockopt(fd, SOL_SCO, SCO_CONNINFO, &conn, &len) < 0) {
2308 + ast_log(LOG_ERROR, "Can't getsockopt SCO_CONNINFO on SCO socket: %s\n", strerror(errno));
2313 + len = sizeof(opts);
2315 + if (getsockopt(fd, SOL_SCO, SCO_OPTIONS, &opts, &len) < 0) {
2316 + ast_log(LOG_ERROR, "Can't getsockopt SCO_OPTIONS on SCO socket: %s\n", strerror(errno));
2321 + ast_mutex_lock(&iface_lock);
2324 + if (bacmp(&(dev->bdaddr), &addr.sco_bdaddr) == 0) {
2325 + ast_log(LOG_DEBUG, "SCO Connect from %s\n", dev->name);
2326 + ast_mutex_lock(&(dev->lock));
2327 + if (dev->sco_running != -1) {
2328 + ast_log(LOG_ERROR, "Incoming SCO socket, but SCO thread already running.\n");
2330 + sco_start(dev, fd);
2332 + ast_mutex_unlock(&(dev->lock));
2338 + ast_mutex_unlock(&iface_lock);
2340 + if (dev == NULL) {
2341 + ast_log(LOG_WARNING, "SCO Connect from unknown device\n");
2344 + // XXX:T: We need to handle the fact we might have an outgoing connection attempt in progress.
2345 + ast_log(LOG_DEBUG, "SCO: %d, HCIHandle=%d, MUT=%d\n", fd, conn.hci_handle, opts.mtu);
2353 +/* Called when there is data waiting on a socket */
2356 +handle_rd_data(blt_dev_t * dev)
2361 + while ((ret = read(dev->rd, &c, 1)) == 1) {
2363 + // log_buf[i++] = c;
2365 + if (dev->role == BLT_ROLE_HS) {
2368 + ret = process_rfcomm_cmd(dev, dev->rd_buff);
2369 + dev->rd_buff_pos = 0;
2370 + memset(dev->rd_buff, 0, BLT_RDBUFF_MAX);
2374 + if (dev->rd_buff_pos >= BLT_RDBUFF_MAX)
2377 + dev->rd_buff[dev->rd_buff_pos++] = c;
2379 + } else if (dev->role == BLT_ROLE_AG) {
2381 + switch (dev->state) {
2383 + case BLT_STATE_WANT_R:
2385 + dev->state = BLT_STATE_WANT_N;
2386 + } else if (c == '+') {
2387 + dev->state = BLT_STATE_WANT_CMD;
2388 + dev->rd_buff[dev->rd_buff_pos++] = '+';
2390 + ast_log(LOG_ERROR, "Device %s: Expected '\\r', got %d. state=BLT_STATE_WANT_R\n", dev->name, c);
2395 + case BLT_STATE_WANT_N:
2397 + dev->state = BLT_STATE_WANT_CMD;
2399 + ast_log(LOG_ERROR, "Device %s: Expected '\\n', got %d. state=BLT_STATE_WANT_N\n", dev->name, c);
2404 + case BLT_STATE_WANT_CMD:
2406 + dev->state = BLT_STATE_WANT_N2;
2408 + if (dev->rd_buff_pos >= BLT_RDBUFF_MAX) {
2409 + ast_log(LOG_ERROR, "Device %s: Buffer exceeded\n", dev->name);
2412 + dev->rd_buff[dev->rd_buff_pos++] = c;
2416 + case BLT_STATE_WANT_N2:
2419 + dev->state = BLT_STATE_WANT_R;
2421 + if (dev->rd_buff[0] == '+') {
2423 + // find unsolicited
2424 + for (i = 0 ; i < ATCMD_LIST_LEN ; i++) {
2425 + if (strncmp(atcmd_list[i].str, dev->rd_buff, strlen(atcmd_list[i].str)) == 0) {
2426 + if (atcmd_list[i].unsolicited)
2427 + atcmd_list[i].unsolicited(dev, dev->rd_buff + strlen(atcmd_list[i].str) + 1);
2429 + ast_log(LOG_WARNING, "Device %s: Unhandled Unsolicited: %s\n", dev->name, dev->rd_buff);
2434 + if (option_verbose)
2435 + ast_verbose(VERBOSE_PREFIX_1 "[%s] %*s > %s\n", role2str(dev->role), 10, dev->name, dev->rd_buff);
2437 + if (i == ATCMD_LIST_LEN)
2438 + ast_log(LOG_DEBUG, "Device %s: Got unsolicited message: %s\n", dev->name, dev->rd_buff);
2443 + strcmp(dev->rd_buff, "OK") != 0 &&
2444 + strcmp(dev->rd_buff, "CONNECT") != 0 &&
2445 + strcmp(dev->rd_buff, "RING") != 0 &&
2446 + strcmp(dev->rd_buff, "NO CARRIER") != 0 &&
2447 + strcmp(dev->rd_buff, "ERROR") != 0 &&
2448 + strcmp(dev->rd_buff, "NO DIALTONE") != 0 &&
2449 + strcmp(dev->rd_buff, "BUSY") != 0 &&
2450 + strcmp(dev->rd_buff, "NO ANSWER") != 0 &&
2451 + strcmp(dev->rd_buff, "DELAYED") != 0
2453 + // It must be a multiline error
2454 + strncpy(dev->last_err_cmd, dev->rd_buff, 1023);
2455 + if (option_verbose)
2456 + ast_verbose(VERBOSE_PREFIX_1 "[%s] %*s > %s\n", role2str(dev->role), 10, dev->name, dev->rd_buff);
2457 + } else if (dev->cb) {
2458 + if (option_verbose)
2459 + ast_verbose(VERBOSE_PREFIX_1 "[%s] %*s > %s\n", role2str(dev->role), 10, dev->name, dev->rd_buff);
2460 + dev->cb(dev, dev->rd_buff);
2462 + ast_log(LOG_ERROR, "Device %s: Data on socket in HS mode, but no callback\n", dev->name);
2467 + dev->rd_buff_pos = 0;
2468 + memset(dev->rd_buff, 0, BLT_RDBUFF_MAX);
2472 + ast_log(LOG_ERROR, "Device %s: Expected '\\n' got %d. state = BLT_STATE_WANT_N2:\n", dev->name, c);
2480 + ast_log(LOG_ERROR, "Device %s: Unknown device state %d\n", dev->name, dev->state);
2492 +/* Close the devices RFCOMM socket, and SCO if it exists. Must hold dev->lock */
2495 +rd_close(blt_dev_t * dev, int reconnect, int e)
2504 + dev->status = BLT_STATUS_DOWN;
2509 + ast_setstate(dev->owner, AST_STATE_DOWN);
2510 + ast_queue_control(dev->owner, AST_CONTROL_HANGUP);
2513 + /* Schedule a reconnect */
2514 + if (reconnect && dev->autoconnect) {
2515 + dev->outgoing_id = ast_sched_add(sched, 5000, AST_SCHED_CB(try_connect), dev);
2517 + if (monitor_thread == pthread_self()) {
2518 + // Because we're not the monitor thread, we needd to inturrupt poll().
2519 + pthread_kill(monitor_thread, SIGURG);
2523 + ast_log(LOG_NOTICE, "Device %s disconnected, scheduled reconnect in 5 seconds: %s (errno %d)\n", dev->name, strerror(e), e);
2525 + ast_log(LOG_NOTICE, "Device %s disconnected: %s (errno %d)\n", dev->name, strerror(e), e);
2532 + * Remember that we can only add to the scheduler from
2533 + * the do_monitor thread, as it calculates time to next one from
2538 +do_monitor(void * data)
2540 +#define SRV_SOCK_CNT 3
2544 + struct pollfd * pfds = malloc(sizeof(struct pollfd) * (ifcount + SRV_SOCK_CNT));
2546 + /* -- We start off by trying to connect all of our devices (non blocking) -- */
2548 + monitor_pid = getpid();
2550 + if (ast_mutex_lock(&iface_lock)) {
2551 + ast_log(LOG_ERROR, "Failed to get iface_lock.\n");
2558 + if (socketpair(PF_UNIX, SOCK_STREAM, 0, dev->sco_pipe) != 0) {
2559 + ast_log(LOG_ERROR, "Failed to create socket pair: %s (errno %d)\n", strerror(errno), errno);
2560 + ast_mutex_unlock(&iface_lock);
2564 + if (dev->autoconnect && dev->status == BLT_STATUS_DOWN)
2565 + dev->outgoing_id = ast_sched_add(sched, 1500, AST_SCHED_CB(try_connect), dev);
2568 + ast_mutex_unlock(&iface_lock);
2570 + /* -- Now, Scan all sockets, and service scheduler -- */
2572 + pfds[0].fd = rfcomm_sock_ag;
2573 + pfds[0].events = POLLIN;
2575 + pfds[1].fd = rfcomm_sock_hs;
2576 + pfds[1].events = POLLIN;
2578 + pfds[2].fd = sco_socket;
2579 + pfds[2].events = POLLIN;
2582 + int cnt = SRV_SOCK_CNT;
2585 + /* -- Build pfds -- */
2587 + if (ast_mutex_lock(&iface_lock)) {
2588 + ast_log(LOG_ERROR, "Failed to get iface_lock.\n");
2593 + ast_mutex_lock(&(dev->lock));
2594 + if (dev->rd > 0 && ((dev->status != BLT_STATUS_DOWN) && (dev->status != BLT_STATUS_CONNECTING))) {
2595 + pfds[cnt].fd = dev->rd;
2596 + pfds[cnt].events = POLLIN;
2599 + ast_mutex_unlock(&(dev->lock));
2602 + ast_mutex_unlock(&iface_lock);
2604 + /* -- End Build pfds -- */
2606 + res = ast_sched_wait(sched);
2607 + res = poll(pfds, cnt, MAX(100, MIN(100, res)));
2610 + ast_sched_runq(sched);
2612 + if (pfds[0].revents) {
2613 + handle_incoming(rfcomm_sock_ag, BLT_ROLE_AG);
2617 + if (pfds[1].revents) {
2618 + handle_incoming(rfcomm_sock_hs, BLT_ROLE_HS);
2622 + if (pfds[2].revents) {
2623 + handle_incoming_sco(sco_socket);
2630 + for (i = SRV_SOCK_CNT ; i < cnt ; i++) {
2632 + /* Optimise a little bit */
2635 + else if (pfds[i].revents == 0)
2638 + /* -- Find the socket that has activity -- */
2640 + if (ast_mutex_lock(&iface_lock)) {
2641 + ast_log(LOG_ERROR, "Failed to get iface_lock.\n");
2648 + if (pfds[i].fd == dev->rd) {
2649 + ast_mutex_lock(&(dev->lock));
2650 + if (pfds[i].revents & POLLIN) {
2651 + if (handle_rd_data(dev) == -1) {
2652 + rd_close(dev, 0, 0);
2655 + rd_close(dev, 1, sock_err(dev->rd));
2657 + ast_mutex_unlock(&(dev->lock));
2664 + if (dev == NULL) {
2665 + ast_log(LOG_ERROR, "Unhandled fd from poll()\n");
2666 + close(pfds[i].fd);
2669 + ast_mutex_unlock(&iface_lock);
2671 + /* -- End find socket with activity -- */
2681 +restart_monitor(void)
2684 + if (monitor_thread == AST_PTHREADT_STOP)
2687 + if (ast_mutex_lock(&monitor_lock)) {
2688 + ast_log(LOG_WARNING, "Unable to lock monitor\n");
2692 + if (monitor_thread == pthread_self()) {
2693 + ast_mutex_unlock(&monitor_lock);
2694 + ast_log(LOG_WARNING, "Cannot kill myself\n");
2698 + if (monitor_thread != AST_PTHREADT_NULL) {
2700 + /* Just signal it to be sure it wakes up */
2701 + pthread_cancel(monitor_thread);
2702 + pthread_kill(monitor_thread, SIGURG);
2703 + ast_log(LOG_DEBUG, "Waiting for monitor thread to join...\n");
2704 + pthread_join(monitor_thread, NULL);
2705 + ast_log(LOG_DEBUG, "joined\n");
2709 + /* Start a new monitor */
2710 + if (ast_pthread_create(&monitor_thread, NULL, do_monitor, NULL) < 0) {
2711 + ast_mutex_unlock(&monitor_lock);
2712 + ast_log(LOG_ERROR, "Unable to start monitor thread.\n");
2718 + ast_mutex_unlock(&monitor_lock);
2723 +blt_parse_config(void)
2725 + struct ast_config * cfg;
2726 + struct ast_variable * v;
2729 + cfg = ast_load(BLT_CONFIG_FILE);
2732 + ast_log(LOG_NOTICE, "Unable to load Bluetooth config: %s. Bluetooth disabled\n", BLT_CONFIG_FILE);
2736 + v = ast_variable_browse(cfg, "general");
2739 + if (!strcasecmp(v->name, "rfchannel_ag")) {
2740 + rfcomm_channel_ag = atoi(v->value);
2741 + } else if (!strcasecmp(v->name, "rfchannel_hs")) {
2742 + rfcomm_channel_hs = atoi(v->value);
2743 + } else if (!strcasecmp(v->name, "interface")) {
2744 + hcidev_id = atoi(v->value);
2746 + ast_log(LOG_WARNING, "Unknown config key '%s' in section [general]\n", v->name);
2750 + cat = ast_category_browse(cfg, NULL);
2756 + if (strcasecmp(cat, "general")) {
2757 + blt_dev_t * device = malloc(sizeof(blt_dev_t));
2758 + memset(device, 0, sizeof(blt_dev_t));
2759 + device->sco_running = -1;
2762 + device->outgoing_id = -1;
2763 + device->status = BLT_STATUS_DOWN;
2764 + str2ba(cat, &(device->bdaddr));
2765 + device->name = ast_variable_retrieve(cfg, cat, "name");
2767 + str = ast_variable_retrieve(cfg, cat, "type");
2769 + if (str == NULL) {
2770 + ast_log(LOG_ERROR, "Device [%s] has no role. Specify type=<HS/AG>\n", cat);
2772 + } else if (strcasecmp(str, "HS") == 0)
2773 + device->role = BLT_ROLE_HS;
2774 + else if (strcasecmp(str, "AG") == 0) {
2775 + device->role = BLT_ROLE_AG;
2777 + ast_log(LOG_ERROR, "Device [%s] has invalid role '%s'\n", cat, str);
2781 + /* XXX:T: Find channel to use using SDP.
2782 + * However, this needs to be non blocking, and I can't see
2783 + * anything in sdp_lib.h that will allow non blocking calls.
2786 + device->channel = 1;
2788 + if ((str = ast_variable_retrieve(cfg, cat, "channel")) != NULL)
2789 + device->channel = atoi(str);
2791 + if ((str = ast_variable_retrieve(cfg, cat, "autoconnect")) != NULL)
2792 + device->autoconnect = (strcasecmp(str, "yes") == 0 || strcmp(str, "1") == 0) ? 1 : 0;
2794 + device->next = iface_head;
2795 + iface_head = device;
2799 + cat = ast_category_browse(cfg, cat);
2806 +blt_show_peers(int fd, int argc, char *argv[])
2810 + if (ast_mutex_lock(&iface_lock)) {
2811 + ast_log(LOG_ERROR, "Failed to get Iface lock\n");
2812 + ast_cli(fd, "Failed to get iface lock\n");
2813 + return RESULT_FAILURE;
2818 + ast_cli(fd, "BDAddr Name Role Status A/C SCOCon/Fd/Th Sig\n");
2819 + ast_cli(fd, "----------------- ---------- ---- ----------- --- ------------ ---\n");
2823 + ba2str(&(dev->bdaddr), b1);
2824 + ast_cli(fd, "%s %-10s %-4s %-11s %-3s %2d/%02d/%-6ld %s\n",
2825 + b1, dev->name, (dev->role == BLT_ROLE_HS) ? "HS" : "AG", status2str(dev->status),
2826 + (dev->autoconnect) ? "Yes" : "No",
2830 + (dev->role == BLT_ROLE_AG) ? (dev->service) ? "Yes" : "No" : "N/A"
2835 + ast_mutex_unlock(&iface_lock);
2836 + return RESULT_SUCCESS;
2840 +blt_show_information(int fd, int argc, char *argv[])
2843 + ba2str(&local_bdaddr, b1);
2844 + ast_cli(fd, "-------------------------------------------\n");
2845 + ast_cli(fd, " Version : %s\n", BLT_SVN_REVISION);
2846 + ast_cli(fd, " Monitor PID : %d\n", monitor_pid);
2847 + ast_cli(fd, " RFCOMM AG : Channel %d, FD %d\n", rfcomm_channel_ag, rfcomm_sock_ag);
2848 + ast_cli(fd, " RFCOMM HS : Channel %d, FD %d\n", rfcomm_channel_hs, rfcomm_sock_hs);
2849 + ast_cli(fd, " Device : hci%d, MAC Address %s\n", hcidev_id, b1);
2850 + ast_cli(fd, "-------------------------------------------\n");
2851 + return RESULT_SUCCESS;
2855 +blt_ag_sendcmd(int fd, int argc, char *argv[])
2860 + return RESULT_SHOWUSAGE;
2862 + ast_mutex_lock(&iface_lock);
2865 + if (!strcasecmp(argv[2], dev->name))
2869 + ast_mutex_unlock(&iface_lock);
2872 + ast_cli(fd, "Device '%s' does not exist\n", argv[2]);
2873 + return RESULT_FAILURE;
2876 + if (dev->role != BLT_ROLE_AG) {
2877 + ast_cli(fd, "Device '%s' is not an AudioGateway\n", argv[2]);
2878 + return RESULT_FAILURE;
2881 + if (dev->status == BLT_STATUS_DOWN || dev->status == BLT_STATUS_NEGOTIATING) {
2882 + ast_cli(fd, "Device '%s' is not connected\n", argv[2]);
2883 + return RESULT_FAILURE;
2886 + if (*(argv[3] + strlen(argv[3]) - 1) == '.')
2887 + *(argv[3] + strlen(argv[3]) - 1) = '?';
2889 + ast_cli(fd, "Sending AT command to %s: %s\n", dev->name, argv[3]);
2891 + ast_mutex_lock(&(dev->lock));
2892 + send_atcmd(dev, argv[3]);
2893 + ast_mutex_unlock(&(dev->lock));
2895 + return RESULT_SUCCESS;
2899 +complete_device(char * line, char * word, int pos, int state, int rpos, blt_role_t role)
2908 + ast_mutex_lock(&iface_lock);
2914 + if ((dev->role == role) && (!strncasecmp(word, dev->name, strlen(word)))) {
2915 + if (++which > state)
2923 + ret = strdup(dev->name);
2927 + ast_mutex_unlock(&iface_lock);
2933 +complete_device_2_ag(char * line, char * word, int pos, int state)
2935 + return complete_device(line, word, pos, state, 2, BLT_ROLE_AG);
2938 +static char show_peers_usage[] =
2939 +"Usage: bluetooth show peers\n"
2940 +" List all bluetooth peers and their status\n";
2942 +static struct ast_cli_entry
2944 + { { "bluetooth", "show", "peers", NULL }, blt_show_peers, "List Bluetooth Peers", show_peers_usage };
2947 +static char ag_sendcmd[] =
2948 +"Usage: bluetooth ag <device> sendcmd <cmd>\n"
2949 +" Sends a AT cmd over the RFCOMM link, and print result (AG only)\n";
2951 +static struct ast_cli_entry
2953 + { { "bluetooth", "sendcmd", NULL }, blt_ag_sendcmd, "Send AG an AT command", ag_sendcmd, complete_device_2_ag };
2955 +static char show_information[] =
2956 +"Usage: bluetooth show information\n"
2957 +" Lists information about the bluetooth subsystem\n";
2959 +static struct ast_cli_entry
2960 +cli_show_information =
2961 + { { "bluetooth", "show", "information", NULL }, blt_show_information, "List Bluetooth Info", show_information };
2964 +remove_sdp_records(void)
2967 + sdp_session_t * sdp;
2968 + sdp_list_t * attr;
2969 + sdp_record_t * rec;
2971 + uint32_t range = 0x0000ffff;
2973 + if (sdp_record_ag == -1 || sdp_record_hs == -1)
2976 + ast_log(LOG_DEBUG, "Removing SDP records\n");
2978 + sdp = sdp_connect(BDADDR_ANY, BDADDR_LOCAL, SDP_RETRY_IF_BUSY);
2983 + attr = sdp_list_append(0, &range);
2984 + rec = sdp_service_attr_req(sdp, sdp_record_ag, SDP_ATTR_REQ_RANGE, attr);
2985 + sdp_list_free(attr, 0);
2988 + if (sdp_record_unregister(sdp, rec) == 0)
2991 + attr = sdp_list_append(0, &range);
2992 + rec = sdp_service_attr_req(sdp, sdp_record_hs, SDP_ATTR_REQ_RANGE, attr);
2993 + sdp_list_free(attr, 0);
2996 + if (sdp_record_unregister(sdp, rec) == 0)
3002 + ast_log(LOG_NOTICE, "Removed SDP records\n");
3004 + ast_log(LOG_ERROR, "Failed to remove SDP records\n");
3009 +__unload_module(void)
3012 + ast_channel_unregister(BLT_CHAN_NAME);
3014 + if (monitor_thread != AST_PTHREADT_NULL) {
3016 + if (ast_mutex_lock(&monitor_lock)) {
3018 + if (monitor_thread && (monitor_thread != AST_PTHREADT_STOP) && (monitor_thread != AST_PTHREADT_NULL)) {
3019 + pthread_cancel(monitor_thread);
3020 + pthread_kill(monitor_thread, SIGURG);
3021 + fprintf(stderr, "Waiting for monitor thread to join...\n");
3022 + pthread_join(monitor_thread, NULL);
3023 + fprintf(stderr, "joined\n");
3025 + monitor_thread = AST_PTHREADT_STOP;
3026 + ast_mutex_unlock(&monitor_lock);
3030 + ast_log(LOG_WARNING, "Unable to lock the monitor\n");
3037 + ast_unregister_atexit(remove_sdp_records);
3038 + remove_sdp_records();
3045 + sdp_session_t * sess;
3049 + hcidev_id = BLT_DEFAULT_HCI_DEV;
3051 + if (blt_parse_config() != 0) {
3052 + ast_log(LOG_ERROR, "Bluetooth configuration error. Bluetooth Disabled\n");
3053 + return unload_module();
3056 + dd = hci_open_dev(hcidev_id);
3058 + ast_log(LOG_ERROR, "Unable to open interface hci%d: %s.\n", hcidev_id, strerror(errno));
3062 + hci_read_voice_setting(dd, &vs, 1000);
3066 + if (vs != 0x0060) {
3067 + ast_log(LOG_ERROR, "Bluetooth voice setting must be 0x0060, not 0x%04x\n", vs);
3072 + if ((sched = sched_context_create()) == NULL) {
3073 + ast_log(LOG_WARNING, "Unable to create schedule context\n");
3077 + memset(&local_bdaddr, 0, sizeof(local_bdaddr));
3079 + hci_devba(hcidev_id, &local_bdaddr);
3081 + /* --- Add SDP record --- */
3083 + sess = sdp_connect(&local_bdaddr, BDADDR_LOCAL, SDP_RETRY_IF_BUSY);
3085 + if ((rfcomm_sock_ag = rfcomm_listen(&local_bdaddr, rfcomm_channel_ag)) < 0) {
3089 + if ((rfcomm_sock_hs = rfcomm_listen(&local_bdaddr, rfcomm_channel_hs)) < 0)
3092 + if ((sco_socket = sco_listen(&local_bdaddr)) < 0)
3096 + ast_log(LOG_ERROR, "Failed to connect to SDP server: %s\n", strerror(errno));
3100 + if (sdp_register(sess) != 0) {
3101 + ast_log(LOG_ERROR, "Failed to register HeadsetAudioGateway in SDP\n");
3107 + if (restart_monitor() != 0)
3110 + if (ast_channel_register(BLT_CHAN_NAME, "Bluetooth Driver", BLUETOOTH_FORMAT, blt_request)) {
3111 + ast_log(LOG_ERROR, "Unable to register channel class BTL\n");
3112 + __unload_module();
3116 + ast_cli_register(&cli_show_information);
3117 + ast_cli_register(&cli_show_peers);
3118 + ast_cli_register(&cli_ag_sendcmd);
3120 + ast_register_atexit(remove_sdp_records);
3122 + ast_log(LOG_NOTICE, "Loaded Bluetooth support, %s\n", BLT_SVN_REVISION + 1);
3128 +unload_module(void)
3130 + ast_cli_unregister(&cli_ag_sendcmd);
3131 + ast_cli_unregister(&cli_show_peers);
3132 + ast_cli_unregister(&cli_show_information);
3133 + return __unload_module();
3140 + ast_mutex_lock(&usecnt_lock);
3142 + ast_mutex_unlock(&usecnt_lock);
3146 +char *description()
3148 + return "Bluetooth Channel Driver";
3154 + return ASTERISK_GPL_KEY;
3158 diff -ruN asterisk-1.0.9-old/configs/bluetooth.conf asterisk-1.0.9-new/configs/bluetooth.conf
3159 --- asterisk-1.0.9-old/configs/bluetooth.conf 1970-01-01 01:00:00.000000000 +0100
3160 +++ asterisk-1.0.9-new/configs/bluetooth.conf 2004-10-22 11:10:48.000000000 +0200
3163 +; Channel we listen on as a HS (Headset)
3165 +; Channel we listen on as an AG (AudioGateway)
3167 +; hci interface to use (number - e.g '0')
3170 +;; A HBH-500 Handsfree Kit
3171 +[00:0A:D9:A1:AA:D2]
3172 +; Any name to use, this is what we use to send calls to (BLT/<name>).
3174 +; IS this a HS or AG?
3178 +; RFCOMM channel to connect to. For a HandsSet:
3179 +; sdptool search --bdaddr xx:xx:xx:xx:xx:xx 0x111E
3180 +; or,for an AudioGateway (Phone):
3181 +; sdptool search --bdaddr xx:xx:xx:xx:xx:xx 0x111F
3183 +; Find the 'channel' value under RFCOMM.
3186 +; Automatically conenct?
3190 +[00:60:57:1C:00:99]