1 diff -Nru asterisk-1.2.14.org/channels/chan_bluetooth.c asterisk-1.2.14/channels/chan_bluetooth.c
2 --- asterisk-1.2.14.org/channels/chan_bluetooth.c 1970-01-01 01:00:00.000000000 +0100
3 +++ asterisk-1.2.14/channels/chan_bluetooth.c 2006-12-27 09:04:03.000000000 +0100
6 + * Asterisk -- A telephony toolkit for Linux.
8 + * Asterisk Bluetooth Channel
10 + * Author: Theo Zourzouvillys <theo@adaptive-it.co.uk>
12 + * Adaptive Linux Solutions <http://www.adaptive-it.co.uk>
14 + * Copyright (C) 2004 Adaptive Linux Solutions
16 + * This program is free software, distributed under the terms of
17 + * the GNU General Public License
19 + * ******************* NOTE NOTE NOTE NOTE NOTE *********************
21 + * This code is not at all tested, and only been developed with a
22 + * HBH-200 headset and a Nokia 6310i right now.
24 + * Expect it to crash, dial random numbers, and steal all your money.
26 + * PLEASE try any headsets and phones, and let me know the results,
27 + * working or not, along with all debug output!
29 + * ------------------------------------------------------------------
31 + * Asterisk Bluetooth Support
33 + * Well, here we go - Attempt to provide Handsfree profile support in
34 + * both AG and HF modes, AG (AudioGateway) mode support for using
35 + * headsets, and HF (Handsfree) mode for utilising mobile/cell phones
37 + * It would be nice to also provide Headset support at some time in
38 + * the future, however, a working Handsfree profile is nice for now,
39 + * and as far as I can see, almost all new HS devices also support HF
41 + * ------------------------------------------------------------------
44 + * You need to have bluez's bluetooth stack, along with user space
45 + * tools (>=v2.10), and running hcid and sdsp.
47 + * See bluetooth.conf for configuration details.
49 + * - Ensure bluetooth subsystem is up and running. 'hciconfig'
50 + * should show interface as UP.
52 + * - If you're trying to use a headset/HS, start up asterisk, and try
53 + * to pair it as you normally would.
55 + * - If you're trying to use a Phone/AG, just make sure bluetooth is
56 + * enabled on your phone, and start up asterisk.
58 + * - 'bluetooth show peers' will show all bluetooth devices states.
60 + * - Send a call out by using Dial(BLT/DevName/0123456). Call a HS
61 + * with Dial(BLT/DevName)
63 + * ------------------------------------------------------------------
66 + * - What should happen when an AG is paired with asterisk and
67 + * someone uses the AG dalling a number manually? My test phone
68 + * seems to try to open an SCO link. Perhaps an extension to
69 + * route the call to, or maybe drop the RFCOM link all together?
71 + * ------------------------------------------------------------------
74 + * PLEASE email <theo@adaptive-it.co.uk> with the results of ANY
75 + * device not listed in here (working or not), or if the device is
76 + * listed and it doesn't work! Please also email full debug output
77 + * for any device not working correctly or generating errors in log.
79 + * HandsFree Profile:
82 + * - Ericsson HBH-200
84 + * AG (AudioGateway):
87 + * ------------------------------------------------------------------
89 + * Questions, bugs, or (preferably) patches to:
91 + * <theo@adaptive-it.co.uk>
93 + * ------------------------------------------------------------------
96 +/* ---------------------------------- */
100 +#include <asterisk/lock.h>
101 +#include <asterisk/utils.h>
102 +#include <asterisk/channel.h>
103 +#include <asterisk/config.h>
104 +#include <asterisk/logger.h>
105 +#include <asterisk/module.h>
106 +#include <asterisk/pbx.h>
107 +#include <asterisk/sched.h>
108 +#include <asterisk/options.h>
109 +#include <asterisk/cli.h>
110 +#include <asterisk/callerid.h>
111 +#include <asterisk/version.h>
112 +#include <sys/socket.h>
113 +#include <sys/signal.h>
114 +#include <sys/time.h>
118 +#include <arpa/inet.h>
120 +#include <sys/ioctl.h>
124 +#include <bluetooth/bluetooth.h>
125 +#include <bluetooth/hci.h>
126 +#include <bluetooth/hci_lib.h>
127 +#include <bluetooth/sco.h>
128 +#include <bluetooth/rfcomm.h>
129 +#include <bluetooth/sdp.h>
130 +#include <bluetooth/sdp_lib.h>
132 +/* --- Data types and definitions --- */
134 +#ifndef HANDSFREE_AUDIO_GW_SVCLASS_ID
135 +# define HANDSFREE_AUDIO_GW_SVCLASS_ID 0x111f
137 +#define BLUETOOTH_FORMAT AST_FORMAT_SLINEAR
138 +#define BLT_CHAN_NAME "BLT"
139 +#define BLT_CONFIG_FILE "bluetooth.conf"
140 +#define BLT_RDBUFF_MAX 1024
141 +#define BLT_DEFAULT_HCI_DEV 0
142 +#define BLT_SVN_REVISION "$Rev$"
144 +/* ---------------------------------- */
147 + BLT_ROLE_NONE = 0, // Unknown Device
148 + BLT_ROLE_HS = 1, // Device is a Headset
149 + BLT_ROLE_AG = 2, // Device is an Audio Gateway
150 + BLT_ROLE_GUI = 3 // Device is used as an GUI
153 +/* State when we're in HS mode */
156 + BLT_STATE_WANT_R = 0,
157 + BLT_STATE_WANT_N = 1,
158 + BLT_STATE_WANT_CMD = 2,
159 + BLT_STATE_WANT_N2 = 3,
164 + BLT_STATUS_CONNECTING,
165 + BLT_STATUS_NEGOTIATING,
167 + BLT_STATUS_RINGING,
168 + BLT_STATUS_IN_CALL,
171 +/* ---------------------------------- */
173 +/* Default config settings */
175 +#define BLT_DEFAULT_CHANNEL_AG 5
176 +#define BLT_DEFAULT_CHANNEL_HS 6
177 +#define BLT_DEFAULT_CHANNEL_GUI 1
178 +#define BLT_DEFAULT_ROLE BLT_ROLE_HS
179 +#define BLT_OBUF_LEN (48 * 25)
181 +#define BUFLEN (4800)
183 +/* ---------------------------------- */
185 +typedef struct blt_dev blt_dev_t;
187 +void ag_cgmi_response(blt_dev_t * dev, char * cmd);
188 +void ag_unknown_response(blt_dev_t * dev, char * cmd);
189 +void ag_cgmi_valid_response(blt_dev_t * dev, char * cmd);
190 +void ag_clip_response(blt_dev_t * dev, char * cmd);
191 +void ag_cmer_response(blt_dev_t * dev, char * cmd);
192 +void ag_cind_status_response(blt_dev_t * dev, char * cmd);
193 +void ag_cind_response(blt_dev_t * dev, char * cmd);
194 +void ag_brsf_response(blt_dev_t * dev, char * cmd);
195 +void remove_sdp_records(void);
197 +void gui_easm_response(blt_dev_t * dev, char * cmd);
199 +int sock_err(int fd);
200 +int parse_clip(const char * str, char *number, int number_len, char * name, int name_len, int *type);
201 +int set_buffer(char * ring, char * data, int circular_len, int * pos, int data_len);
202 +int get_buffer(char * dst, char * ring, int ring_size, int * head, int to_copy);
203 +void gui_eaid_response(blt_dev_t * dev, char * cmd);
208 + unsigned char buf[BUFLEN];
210 +// XXX:T: Tidy this lot up.
213 + blt_status_t status; /* Device Status */
215 + struct ast_channel * owner; /* Channel we belong to, possibly NULL */
216 + blt_dev_t * dev; /* The bluetooth device channel is for */
217 + struct ast_frame fr; /* Recieved frame */
220 + int sco_pipe[2]; /* SCO alert pipe */
221 + int sco; /* SCO fd */
222 + int sco_handle; /* SCO Handle */
223 + int sco_mtu; /* SCO MTU */
224 + int sco_running; /* 1 when sCO thread should be running */
225 + pthread_t sco_thread; /* SCO thread */
226 + ast_mutex_t sco_lock; /* SCO lock */
227 + int sco_pos_in; /* Reader in position (drain)*/
228 + int sco_pos_inrcv; /* Reader in position (fill) */
229 + int wakeread; /* blt_read() needs to be woken */
230 + int sco_pos_out; /* Reader out position */
231 + int sco_sending; /* Sending SCO packets */
232 + char buf[1200]; /* Incoming data buffer */
234 + char sco_buf_out[BUFLEN]; /* 24 chunks of 48 */
235 + char sco_buf_in[BUFLEN]; /* 24 chunks of 48 */
237 + char dnid[1024]; /* Outgoi gncall dialed number */
238 + unsigned char * obuf[BLT_OBUF_LEN]; /* Outgoing data buffer */
239 + int obuf_len; /* Output Buffer Position */
240 + int obuf_wpos; /* Buffer Reader */
243 + int autoconnect; /* 1 for autoconnect */
244 + int outgoing_id; /* Outgoing connection scheduler id */
245 + char * name; /* Devices friendly name */
246 + blt_role_t role; /* Device role (HS or AG) */
247 + bdaddr_t bdaddr; /* remote address */
248 + int channel; /* remote channel */
249 + int rd; /* RFCOMM fd */
250 + int tmp_rd; /* RFCOMM fd */
251 + int call_cnt; /* Number of attempted calls */
252 + ast_mutex_t lock; /* RFCOMM socket lock */
253 + char rd_buff[BLT_RDBUFF_MAX]; /* RFCOMM input buffer */
254 + int rd_buff_pos; /* RFCOMM input buffer position */
255 + int ready; /* 1 When ready */
259 + char last_ok_cmd[BLT_RDBUFF_MAX]; /* Runtime[AG]: Last AT command that was OK */
260 + int cind; /* Runtime[AG]: Recieved +CIND */
261 + int call_pos, service_pos, callsetup_pos; /* Runtime[AG]: Positions in CIND/CMER */
262 + int call, service, callsetup; /* Runtime[AG]: Values */
263 + char cid_num[AST_MAX_EXTENSION];
264 + char cid_name[AST_MAX_EXTENSION];
267 + blt_state_t state; /* Runtime: Device state (AG mode only) */
268 + int ring_timer; /* Runtime:Ring Timer */
269 + char last_err_cmd[BLT_RDBUFF_MAX]; /* Runtime: Last AT command that was OK */
270 + void (*cb)(blt_dev_t * dev, char * str); /* Runtime: Callback when in HS mode */
272 + int brsf; /* Runtime: Bluetooth Retrieve Supported Features */
273 + int bvra; /* Runtime: Bluetooth Voice Recognised Activation */
274 + int gain_speaker; /* Runtime: Gain Of Speaker */
275 + int clip; /* Runtime: Supports CLID */
276 + int colp; /* Runtime: Connected Line ID */
277 + int elip; /* Runtime: (Ericsson) Supports CLID */
278 + int eolp; /* Runtime: (Ericsson) Connected Line ID */
279 + int ringing; /* Runtime: Device is ringing */
281 + blt_dev_t * next; /* Next in linked list */
285 +typedef struct blt_atcb {
290 + /* DTE callbacks: */
291 + int (*set)(blt_dev_t * dev, const char * arg, int len);
292 + int (*read)(blt_dev_t * dev);
293 + int (*execute)(blt_dev_t * dev, const char * data);
294 + int (*test)(blt_dev_t * dev);
296 + /* DCE callbacks: */
297 + int (*unsolicited)(blt_dev_t * dev, const char * value);
301 +/* ---------------------------------- */
303 +static void rd_close(blt_dev_t * dev, int reconnect, int err);
304 +static int send_atcmd(blt_dev_t * device, const char * fmt, ...);
305 +static int sco_connect(blt_dev_t * dev);
306 +static int sco_start(blt_dev_t * dev, int fd);
308 +/* ---------------------------------- */
310 +/* RFCOMM channel we listen on*/
311 +static int rfcomm_channel_ag = BLT_DEFAULT_CHANNEL_AG;
312 +static int rfcomm_channel_hs = BLT_DEFAULT_CHANNEL_HS;
313 +static int rfcomm_channel_gui = BLT_DEFAULT_CHANNEL_GUI;
315 +static char* gui_default_sip_number = "";
316 +static char* gui_default_sip_address = "";
318 +/* Address of local bluetooth interface */
319 +static int hcidev_id;
320 +static bdaddr_t local_bdaddr;
322 +/* All the current sockets */
323 +AST_MUTEX_DEFINE_STATIC(iface_lock);
324 +static blt_dev_t * iface_head;
325 +static int ifcount = 0;
327 +static int sdp_record_hs = -1;
328 +static int sdp_record_ag = -1;
329 +static int sdp_record_gui = -1;
331 +/* RFCOMM listen socket */
332 +static int rfcomm_sock_ag = -1;
333 +static int rfcomm_sock_hs = -1;
334 +static int rfcomm_sock_gui = -1;
336 +static int sco_socket = -1;
338 +static int monitor_pid = -1;
340 +/* The socket monitoring thread */
341 +static pthread_t monitor_thread = AST_PTHREADT_NULL;
342 +AST_MUTEX_DEFINE_STATIC(monitor_lock);
344 +/* Count how many times this module is currently in use */
345 +static int usecnt = 0;
346 +AST_MUTEX_DEFINE_STATIC(usecnt_lock);
348 +static struct sched_context * sched = NULL;
350 +/* ---------------------------------- */
352 +#if ASTERISK_VERSION_NUM <= 010107
353 +#include <asterisk/channel_pvt.h>
354 +#define tech_pvt pvt->pvt
355 +#else /* CVS. FIXME: Version number */
356 +static struct ast_channel *blt_request(const char *type, int format, void *data, int *cause);
357 +static int blt_hangup(struct ast_channel *c);
358 +static int blt_answer(struct ast_channel *c);
359 +static struct ast_frame *blt_read(struct ast_channel *chan);
360 +static int blt_call(struct ast_channel *c, char *dest, int timeout);
361 +static int blt_write(struct ast_channel *chan, struct ast_frame *f);
362 +static int blt_indicate(struct ast_channel *chan, int cond);
364 +static const struct ast_channel_tech blt_tech = {
365 + .type = BLT_CHAN_NAME,
366 + .description = "Bluetooth Channel Driver",
367 + .capabilities = BLUETOOTH_FORMAT,
368 + .requester = blt_request,
369 + .hangup = blt_hangup,
370 + .answer = blt_answer,
373 + .write = blt_write,
374 + .indicate = blt_indicate,
377 +/* ---------------------------------- */
380 +role2str(blt_role_t role)
389 + case BLT_ROLE_NONE:
396 +status2str(blt_status_t status)
399 + case BLT_STATUS_DOWN:
401 + case BLT_STATUS_CONNECTING:
402 + return "Connecting";
403 + case BLT_STATUS_NEGOTIATING:
404 + return "Negotiating";
405 + case BLT_STATUS_READY:
407 + case BLT_STATUS_RINGING:
409 + case BLT_STATUS_IN_CALL:
415 +int sock_err(int fd)
418 + int len = sizeof(ret);
419 + getsockopt(fd, SOL_SOCKET, SO_ERROR, &ret, &len);
423 +/* ---------------------------------- */
424 +int parse_clip(const char * str, char *number, int number_len, char * name, int name_len, int *type)
426 + const char *c = str;
431 + memset(number, 0, number_len);
432 + memset(name, 0, name_len);
437 + while(*c && *c != '"')
441 + while(*c && *c != '"')
443 + length = c - start < number_len ? c - start : number_len;
444 + strncpy(number, start, length);
445 + number[length] = '\0';
447 + while(*c && *c != ',')
451 + while(*c && *c != ',')
453 + length = c - start < number_len ? c - start : number_len;
454 + strncpy(typestr, start, length);
455 + typestr[length] = '\0';
456 + *type = atoi(typestr);
458 + while(*c && *c != ',')
461 + while(*c && *c != ',')
464 + while(*c && *c != '"')
468 + while(*c && *c != '"')
470 + length = c - start < number_len ? c - start : number_len;
471 + strncpy(name, start, length);
472 + name[length] = '\0';
479 +parse_cind(const char * str, char * name, int name_len)
483 + memset(name, 0, name_len);
487 + if (++c == 1 && *(str+1) == '"') {
488 + const char * start = str + 2;
491 + while (*str && *str != '"') {
497 + strncpy(name, start, (len > name_len) ? name_len : len);
499 + } else if (*str == ')')
501 + else if (c == 0 && *str == ',')
509 +set_cind(blt_dev_t * dev, int indicator, int val)
512 + ast_log(LOG_DEBUG, "CIND %d set to %d\n", indicator, val);
514 + if (indicator == dev->callsetup_pos) {
518 + dev->callsetup = val;
522 + // Outgoing ringing
523 + if ((dev->owner && dev->role == BLT_ROLE_AG) ||
524 + (dev->owner && dev->role == BLT_ROLE_GUI))
525 + ast_queue_control(dev->owner, AST_CONTROL_RINGING);
532 + if ((dev->owner && dev->role == BLT_ROLE_AG && dev->call == 0) ||
533 + (dev->owner && dev->role == BLT_ROLE_AG && dev->call == 0))
534 + ast_queue_control(dev->owner, AST_CONTROL_CONGESTION);
538 + } else if (indicator == dev->service_pos) {
543 + ast_log(LOG_NOTICE, "Audio Gateway %s lost signal\n", dev->name);
544 + else if (dev->service == 0 && val > 0)
545 + ast_log(LOG_NOTICE, "Audio Gateway %s got signal\n", dev->name);
547 + dev->service = val;
549 + } else if (indicator == dev->call_pos) {
557 + sco_start(dev, -1);
558 + ast_queue_control(dev->owner, AST_CONTROL_ANSWER);
559 + } else if (val == 0)
560 + ast_queue_control(dev->owner, AST_CONTROL_HANGUP);
568 +/* ---------------------------------- */
571 +set_buffer(char * ring, char * data, int circular_len, int * pos, int data_len)
573 + int start_pos = *(pos);
578 + // Set can_do to the most we can do in this copy.
580 + copy = MIN(circular_len - start_pos, data_len);
581 + memcpy(ring + start_pos, data + done, copy);
586 + if (start_pos == circular_len) {
590 + *(pos) = start_pos;
595 +get_buffer(char * dst, char * ring, int ring_size, int * head, int to_copy)
599 + // |1|2|3|4|5|6|7|8|9|
604 + // Set can_do to the most we can do in this copy.
605 + copy = MIN(ring_size - *head, to_copy);
607 + // ast_log(LOG_DEBUG, "Getting: %d bytes, From pos %d\n", copy, *head);
608 +#if __BYTE_ORDER == __LITTLE_ENDIAN
609 + memcpy(dst, ring + *head, copy);
611 + // memcpy(dst, ring + *head, copy);
612 + ast_swapcopy_samples(dst, ring+*head, copy/2);
614 + memset(ring+*head, 0, copy);
619 + if (*head == ring_size ) {
628 +/* Handle SCO audio sync.
630 + * If we are the MASTER, then we control the timing,
631 + * in 48 byte chunks. If we're the SLAVE, we send
632 + * as and when we recieve a packet.
634 + * Because of packet/timing nessecity, we
635 + * start up a thread when we're passing audio, so
636 + * that things are timed exactly right.
638 + * sco_thread() is the function that handles it.
643 +sco_thread(void * data)
645 + blt_dev_t * dev = (blt_dev_t*)data;
647 + struct pollfd pfd[2];
655 + // Avoid deadlock in odd circumstances
657 + ast_log(LOG_WARNING, "SCO thread started on fd %d, pid %d\n", dev->sco, getpid());
659 + if (fcntl(dev->sco_pipe[1], F_SETFL, O_RDWR|O_NONBLOCK)) {
660 + ast_log(LOG_WARNING, "fcntl failed on sco_pipe\n");
663 + // dev->status = BLT_STATUS_IN_CALL;
664 + // ast_queue_control(dev->owner, AST_CONTROL_ANSWER);
665 + // Set buffer to silence, just incase.
667 + ast_mutex_lock(&(dev->sco_lock));
669 + memset(dev->sco_buf_in, 0, BUFLEN);
670 + memset(dev->sco_buf_out, 0, BUFLEN);
672 + dev->sco_pos_in = 0;
673 + dev->sco_pos_out = 0;
674 + dev->sco_pos_inrcv = 0;
677 + ast_mutex_unlock(&(dev->sco_lock));
681 + ast_mutex_lock(&(dev->sco_lock));
683 + if (dev->sco_running != 1) {
684 + ast_log(LOG_DEBUG, "SCO stopped.\n");
688 + pfd[0].fd = dev->sco;
689 + pfd[0].events = POLLIN;
691 + pfd[1].fd = dev->sco_pipe[1];
692 + pfd[1].events = POLLIN;
694 + ast_mutex_unlock(&(dev->sco_lock));
696 + res = poll(pfd, 2, 50);
698 + if (res == -1 && errno != EINTR) {
699 + ast_log(LOG_DEBUG, "SCO poll() error\n");
707 + if (pfd[0].revents & POLLIN) {
709 + len = read(dev->sco, buf, 48);
712 + ast_mutex_lock(&(dev->lock));
714 + if (dev->owner && dev->owner->_state == AST_STATE_UP) {
715 + ast_mutex_lock(&(dev->sco_lock));
716 + set_buffer(dev->sco_buf_in, buf, BUFLEN, &in_pos, len);
717 + dev->sco_pos_inrcv = in_pos;
719 + get_buffer(buf, dev->sco_buf_out, BUFLEN, &out_pos, len);
720 + if (write(dev->sco, buf, len) != len)
721 + ast_log(LOG_WARNING, "Wrote <48 to sco\n");
723 + if (dev->wakeread) {
724 + /* blt_read has caught up. Kick it */
726 + if(write(dev->sco_pipe[1], &c, 1) != 1)
727 + ast_log(LOG_WARNING, "write to kick sco_pipe failed\n");
729 + ast_mutex_unlock(&(dev->sco_lock));
731 + ast_mutex_unlock(&(dev->lock));
734 + } else if (pfd[0].revents) {
736 + int e = sock_err(pfd[0].fd);
737 + ast_log(LOG_ERROR, "SCO connection error: %s (errno %d)\n", strerror(e), e);
740 + } else if (pfd[1].revents & POLLIN) {
744 + len = read(pfd[1].fd, &c, 1);
745 + sending = (sending) ? 0 : 1;
747 + ast_mutex_unlock(&(dev->sco_lock));
749 + } else if (pfd[1].revents) {
751 + int e = sock_err(pfd[1].fd);
752 + 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);
756 + ast_log(LOG_NOTICE, "Unhandled poll output\n");
757 + ast_mutex_unlock(&(dev->sco_lock));
762 + ast_mutex_lock(&(dev->lock));
765 + dev->sco_running = -1;
767 + memset(dev->sco_buf_in, 0, BUFLEN);
768 + memset(dev->sco_buf_out, 0, BUFLEN);
770 + dev->sco_pos_in = 0;
771 + dev->sco_pos_out = 0;
772 + dev->sco_pos_inrcv = 0;
774 + ast_mutex_unlock(&(dev->sco_lock));
776 + ast_queue_control(dev->owner, AST_CONTROL_HANGUP);
777 + ast_mutex_unlock(&(dev->lock));
778 + ast_log(LOG_DEBUG, "SCO thread stopped\n");
782 +/* Start SCO thread. Must be called with dev->lock */
785 +sco_start(blt_dev_t * dev, int fd)
788 + if (dev->sco_pipe[1] <= 0) {
789 + ast_log(LOG_ERROR, "SCO pipe[1] == %d\n", dev->sco_pipe[1]);
793 + ast_mutex_lock(&(dev->sco_lock));
795 + if (dev->sco_running != -1) {
796 + ast_log(LOG_ERROR, "Tried to start SCO thread while already running\n");
797 + ast_mutex_unlock(&(dev->sco_lock));
801 + if (dev->sco == -1) {
804 + } else if (sco_connect(dev) != 0) {
805 + ast_log(LOG_ERROR, "SCO fd invalid\n");
806 + ast_mutex_unlock(&(dev->sco_lock));
811 + dev->sco_running = 1;
813 + if (ast_pthread_create(&(dev->sco_thread), NULL, sco_thread, dev) < 0) {
814 + ast_log(LOG_ERROR, "Unable to start SCO thread.\n");
815 + dev->sco_running = -1;
816 + ast_mutex_unlock(&(dev->sco_lock));
820 + ast_mutex_unlock(&(dev->sco_lock));
825 +/* Stop SCO thread. Must be called with dev->lock */
828 +sco_stop(blt_dev_t * dev)
830 + ast_mutex_lock(&(dev->sco_lock));
831 + if (dev->sco_running == 1)
832 + dev->sco_running = 0;
834 + dev->sco_running = -1;
835 + dev->sco_sending = 0;
836 + ast_mutex_unlock(&(dev->sco_lock));
840 +/* ---------------------------------- */
842 +/* Answer the call. Call with lock held on device */
845 +answer(blt_dev_t * dev)
848 + if ( (!dev->owner) || (dev->ready != 1) || (dev->status != BLT_STATUS_READY && dev->status != BLT_STATUS_RINGING)) {
849 + ast_log(LOG_ERROR, "Attempt to answer() in invalid state (owner=%p, ready=%d, status=%s)\n",
850 + dev->owner, dev->ready, status2str(dev->status));
854 + // dev->sd = sco_connect(&local_bdaddr, &(dev->bdaddr), NULL, NULL, 0);
855 + // dev->status = BLT_STATUS_IN_CALL;
856 + // dev->owner->fds[0] = dev->sd;
857 + // if we are answering (hitting button):
858 + ast_queue_control(dev->owner, AST_CONTROL_ANSWER);
859 + // if asterisk signals us to answer:
860 + // ast_setstate(ast, AST_STATE_UP);
862 + /* Start SCO link */
863 + sco_start(dev, -1);
867 +/* ---------------------------------- */
870 +blt_write(struct ast_channel * ast, struct ast_frame * frame)
872 + blt_dev_t * dev = ast->tech_pvt;
874 + /* Write a frame of (presumably voice) data */
876 + if (frame->frametype != AST_FRAME_VOICE) {
877 + ast_log(LOG_WARNING, "Don't know what to do with frame type '%d'\n", frame->frametype);
881 + if (!(frame->subclass & BLUETOOTH_FORMAT)) {
882 + static int fish = 5;
884 + ast_log(LOG_WARNING, "Cannot handle frames in format %d\n", frame->subclass);
890 + if (ast->_state != AST_STATE_UP) {
894 + ast_mutex_lock(&(dev->sco_lock));
895 + set_buffer(dev->sco_buf_out, frame->data, BUFLEN, &(dev->sco_pos_out), MIN(frame->datalen, BUFLEN));
896 + ast_mutex_unlock(&(dev->sco_lock));
902 +static struct ast_frame *
903 +blt_read(struct ast_channel * ast)
905 + blt_dev_t * dev = ast->tech_pvt;
908 + static int fish = 0;
909 + /* Some nice norms */
911 + dev->fr.datalen = 0;
912 + dev->fr.samples = 0;
913 + dev->fr.data = NULL;
914 + dev->fr.src = BLT_CHAN_NAME;
915 + dev->fr.offset = 0;
916 + dev->fr.mallocd = AST_MALLOCD_DATA;
917 + dev->fr.delivery.tv_sec = 0;
918 + dev->fr.delivery.tv_usec = 0;
919 + read(dev->sco_pipe[0], &c, 1);
920 + ast_mutex_lock(&(dev->sco_lock));
921 + dev->sco_sending = 1;
923 + if (dev->sco_pos_inrcv < dev->sco_pos_in) {
924 + /* Buffer wrapped. Read only till the end */
925 + len = BUFLEN - dev->sco_pos_in + dev->sco_pos_inrcv;
927 + len = dev->sco_pos_inrcv - dev->sco_pos_in;
929 + dev->fr.data = malloc(AST_FRIENDLY_OFFSET+len) + AST_FRIENDLY_OFFSET;
931 + get_buffer(dev->fr.data, dev->sco_buf_in, BUFLEN, &(dev->sco_pos_in), len);
933 + ast_mutex_unlock(&(dev->sco_lock));
935 + unsigned char *x = dev->fr.data;
936 + ast_log(LOG_WARNING, "blt_read %d: %02x %02x %02x %02x %02x %02x\n",
937 + dev->fr.datalen, x[0], x[1], x[2], x[3], x[4], x[5]);
941 + dev->fr.samples = len / 2;
942 + dev->fr.datalen = len;
943 + dev->fr.frametype = AST_FRAME_VOICE;
944 + dev->fr.subclass = BLUETOOTH_FORMAT;
945 + dev->fr.offset = AST_FRIENDLY_OFFSET;
949 +/* Escape Any '"' in str. Return malloc()ed string */
951 +escape_str(char * str)
965 + ret = malloc(len + 1);
966 + pret = memset(ret, 0, len + 1);
980 +ring_hs(blt_dev_t * dev)
982 +#if (ASTERISK_VERSION_NUM < 010100)
983 + char tmp[AST_MAX_EXTENSION];
987 + ast_mutex_lock(&(dev->lock));
989 + if (dev->owner == NULL) {
990 + ast_mutex_unlock(&(dev->lock));
995 + dev->status = BLT_STATUS_RINGING;
997 + send_atcmd(dev, "RING");
999 + dev->owner->rings++;
1001 + // XXX:T: '"' needs to be escaped in ELIP.
1003 +#if (ASTERISK_VERSION_NUM < 010100)
1005 + if (dev->owner->callerid) {
1007 + memset(tmp, 0, sizeof(tmp));
1008 + strncpy(tmp, dev->owner->callerid, sizeof(tmp)-1);
1010 + if (!ast_callerid_parse(tmp, &name, &num)) {
1012 + if (dev->clip && num)
1013 + send_atcmd(dev, "+CLIP: \"%s\",129", num);
1015 + if (dev->elip && name) {
1016 + char * esc = escape_str(name);
1017 + send_atcmd(dev, "*ELIP: \"%s\"", esc);
1026 + if (dev->clip && dev->owner->cid.cid_num)
1027 + send_atcmd(dev, "+CLIP: \"%s\",129", dev->owner->cid.cid_num);
1029 + if (dev->elip && dev->owner->cid.cid_name) {
1030 + char * esc = escape_str(dev->owner->cid.cid_name);
1031 + send_atcmd(dev, "*ELIP: \"%s\"", esc);
1037 + ast_mutex_unlock(&(dev->lock));
1043 + * If the HS is already connected, then just send RING, otherwise, things get a
1044 + * little more sticky. We first have to find the channel for HS using SDP,
1045 + * then initiate the connection. Once we've done that, we can start the call.
1049 +blt_call(struct ast_channel * ast, char * dest, int timeout)
1051 + blt_dev_t * dev = ast->tech_pvt;
1053 + if ((ast->_state != AST_STATE_DOWN) && (ast->_state != AST_STATE_RESERVED)) {
1054 + ast_log(LOG_WARNING, "blt_call called on %s, neither down nor reserved\n", ast->name);
1058 + ast_log(LOG_DEBUG, "Calling %s on %s [t: %d]\n", dest, ast->name, timeout);
1060 + if (ast_mutex_lock(&iface_lock)) {
1061 + ast_log(LOG_ERROR, "Failed to get iface_lock.\n");
1065 +// ast_mutex_lock(&(dev->lock));
1067 + if (dev->ready == 0) {
1068 + ast_log(LOG_WARNING, "Tried to call a device not ready/connected.\n");
1069 + ast_setstate(ast, AST_CONTROL_CONGESTION);
1070 +// ast_mutex_unlock(&(dev->lock));
1071 + ast_mutex_unlock(&iface_lock);
1075 + if (dev->role == BLT_ROLE_HS) {
1077 + send_atcmd(dev, "+CIEV: 3,1");
1079 + dev->ring_timer = ast_sched_add(sched, 5000, AST_SCHED_CB(ring_hs), dev);
1083 + ast_setstate(ast, AST_STATE_RINGING);
1084 + ast_queue_control(ast, AST_CONTROL_RINGING);
1086 + } else if (dev->role == BLT_ROLE_AG) {
1088 + send_atcmd(dev, "ATD%s;", dev->dnid);
1089 +// it does not seem like we should start the audio until the call is connected
1090 +// sco_start(dev, -1);
1091 + } else if (dev->role == BLT_ROLE_GUI) {
1093 + send_atcmd(dev, "ATD%s;", dev->dnid);
1097 + ast_setstate(ast, AST_CONTROL_CONGESTION);
1098 + ast_log(LOG_ERROR, "Unknown device role\n");
1102 +// ast_mutex_unlock(&(dev->lock));
1103 + ast_mutex_unlock(&iface_lock);
1109 +blt_hangup(struct ast_channel * ast)
1111 + blt_dev_t * dev = ast->tech_pvt;
1113 + ast_log(LOG_DEBUG, "blt_hangup(%s)\n", ast->name);
1115 + if (!ast->tech_pvt) {
1116 + ast_log(LOG_WARNING, "Asked to hangup channel not connected\n");
1120 + if (ast_mutex_lock(&iface_lock)) {
1121 + ast_log(LOG_ERROR, "Failed to get iface_lock\n");
1125 + ast_mutex_lock(&(dev->lock));
1128 + dev->sco_sending = 0;
1130 + if (dev->role == BLT_ROLE_HS) {
1132 + if (dev->ringing == 0) {
1133 + // Actual call in progress
1134 + send_atcmd(dev, "+CIEV: 2,0");
1137 + // Just ringing still
1139 + if (dev->role == BLT_ROLE_HS)
1140 + send_atcmd(dev, "+CIEV: 3,0");
1142 + if (dev->ring_timer >= 0)
1143 + ast_sched_del(sched, dev->ring_timer);
1145 + dev->ring_timer = -1;
1150 + } else if (dev->role == BLT_ROLE_AG) {
1153 + send_atcmd(dev, "ATH");
1154 + send_atcmd(dev, "AT+CHUP");
1158 + if (dev->status == BLT_STATUS_IN_CALL || dev->status == BLT_STATUS_RINGING)
1159 + dev->status = BLT_STATUS_READY;
1161 + ast->tech_pvt = NULL;
1162 + dev->owner = NULL;
1163 + ast_mutex_unlock(&(dev->lock));
1164 + ast_setstate(ast, AST_STATE_DOWN);
1165 + ast_mutex_unlock(&(iface_lock));
1171 +blt_indicate(struct ast_channel * c, int condition)
1173 + ast_log(LOG_DEBUG, "blt_indicate (%d)\n", condition);
1175 + switch(condition) {
1176 + case AST_CONTROL_RINGING:
1179 + ast_log(LOG_WARNING, "Don't know how to condition %d\n", condition);
1186 +blt_answer(struct ast_channel * ast)
1188 + blt_dev_t * dev = ast->tech_pvt;
1190 + ast_mutex_lock(&dev->lock);
1192 + // if (dev->ring_timer >= 0)
1193 + // ast_sched_del(sched, dev->ring_timer);
1194 + // dev->ring_timer = -1;
1196 + ast_log(LOG_DEBUG, "Answering interface\n");
1198 + if (ast->_state != AST_STATE_UP) {
1199 + send_atcmd(dev, "+CIEV: 2,1");
1200 + send_atcmd(dev, "+CIEV: 3,0");
1201 + sco_start(dev, -1);
1202 + ast_setstate(ast, AST_STATE_UP);
1205 + ast_mutex_unlock(&dev->lock);
1210 +static struct ast_channel *
1211 +blt_new(blt_dev_t * dev, int state, const char * context, const char * number)
1213 + struct ast_channel * ast;
1216 + if ((ast = ast_channel_alloc(1)) == NULL) {
1217 + ast_log(LOG_WARNING, "Unable to allocate channel structure\n");
1221 + snprintf(ast->name, sizeof(ast->name), "BLT/%s", dev->name);
1223 + // ast->fds[0] = socket(PF_BLUETOOTH, SOCK_SEQPACKET, BTPROTO_SCO);
1225 + ast->nativeformats = BLUETOOTH_FORMAT;
1226 + //ast->rawreadformat = BLUETOOTH_FORMAT;
1227 + //ast->rawwriteformat = BLUETOOTH_FORMAT;
1228 + ast->writeformat = BLUETOOTH_FORMAT;
1229 + ast->readformat = BLUETOOTH_FORMAT;
1231 + ast_setstate(ast, state);
1233 + ast->type = BLT_CHAN_NAME;
1235 + ast->tech_pvt = dev;
1236 +#if ASTERISK_VERSION_NUM > 010107
1237 + ast->tech = &blt_tech;
1239 + ast->pvt->call = blt_call;
1240 + ast->pvt->indicate = blt_indicate;
1241 + ast->pvt->hangup = blt_hangup;
1242 + ast->pvt->read = blt_read;
1243 + ast->pvt->write = blt_write;
1244 + ast->pvt->answer = blt_answer;
1246 + strncpy(ast->context, context, sizeof(ast->context)-1);
1247 + strncpy(ast->exten, number, sizeof(ast->exten) - 1);
1248 + if(0 == strcmp(number, "s"))
1250 + //ast_set_callerid(ast, dev->cid_num, dev->cid_name, dev->cid_num);
1253 + ast->language[0] = '\0';
1255 + ast->fds[0] = dev->sco_pipe[0];
1256 + write(dev->sco_pipe[1], &c, 1);
1260 + ast_mutex_lock(&usecnt_lock);
1262 + ast_mutex_unlock(&usecnt_lock);
1264 + ast_update_use_count();
1266 + if (state != AST_STATE_DOWN) {
1267 + if (ast_pbx_start(ast)) {
1268 + ast_log(LOG_WARNING, "Unable to start PBX on %s\n", ast->name);
1276 +static struct ast_channel *
1277 +#if (ASTERISK_VERSION_NUM < 010100)
1278 +blt_request(char * type, int format, void * local_data)
1279 +#elif (ASTERISK_VERSION_NUM <= 010107)
1280 +blt_request(const char * type, int format, void * local_data)
1282 +blt_request(const char * type, int format, void * local_data, int *cause)
1285 + char * data = (char*)local_data;
1287 + blt_dev_t * dev = NULL;
1288 + struct ast_channel * ast = NULL;
1289 + char * number = data, * dname;
1291 + dname = strsep(&number, "/");
1293 + oldformat = format;
1295 + format &= BLUETOOTH_FORMAT;
1298 + ast_log(LOG_NOTICE, "Asked to get a channel of unsupported format '%d'\n", oldformat);
1302 + ast_log(LOG_DEBUG, "Dialing '%s' via '%s'\n", number, dname);
1304 + if (ast_mutex_lock(&iface_lock)) {
1305 + ast_log(LOG_ERROR, "Unable to lock iface_list\n");
1312 + if (strcmp(dev->name, dname) == 0) {
1313 + ast_mutex_lock(&(dev->lock));
1314 + if (!dev->ready) {
1315 + ast_log(LOG_ERROR, "Device %s is not connected\n", dev->name);
1316 + ast_mutex_unlock(&(dev->lock));
1317 + ast_mutex_unlock(&iface_lock);
1325 + ast_mutex_unlock(&iface_lock);
1328 + ast_log(LOG_WARNING, "Failed to find device named '%s'\n", dname);
1332 + if (number && dev->role != BLT_ROLE_AG) {
1333 + ast_log(LOG_WARNING, "Tried to send a call out on non AG\n");
1334 + ast_mutex_unlock(&(dev->lock));
1338 + if (dev->role == BLT_ROLE_AG)
1339 + strncpy(dev->dnid, number, sizeof(dev->dnid) - 1);
1341 + ast = blt_new(dev, AST_STATE_DOWN, dev->context, "s");
1343 + ast_mutex_unlock(&(dev->lock));
1348 +/* ---------------------------------- */
1351 +/* ---- AT COMMAND SOCKET STUFF ---- */
1354 +send_atcmd(blt_dev_t * dev, const char * fmt, ...)
1360 + va_start(ap, fmt);
1361 + len = vsnprintf(buf, 1023, fmt, ap);
1364 + if (option_verbose)
1365 + ast_verbose(VERBOSE_PREFIX_1 "[%s] %*s < %s\n", role2str(dev->role), 10, dev->name, buf);
1367 + write(dev->rd, "\r\n", 2);
1368 + len = write(dev->rd, buf, len);
1369 + write(dev->rd, "\r\n", 2);
1370 + return (len) ? 0 : -1;
1375 +send_atcmd_ok(blt_dev_t * dev, const char * cmd)
1378 + strncpy(dev->last_ok_cmd, cmd, BLT_RDBUFF_MAX - 1);
1379 + if (option_verbose)
1380 + ast_verbose(VERBOSE_PREFIX_1 "[%s] %*s < OK\n", role2str(dev->role), 10, dev->name);
1381 + len = write(dev->rd, "\r\nOK\r\n", 6);
1382 + return (len) ? 0 : -1;
1386 +send_atcmd_error(blt_dev_t * dev)
1390 + if (option_verbose)
1391 + ast_verbose(VERBOSE_PREFIX_1 "[%s] %*s < ERROR\n", role2str(dev->role), 10, dev->name);
1393 +// write(dev->rd, "\r\n", 2);
1394 +// len = write(dev->rd, dev->last_ok_cmd, 5);
1395 + write(dev->rd, "\r\n", 2);
1396 + len = write(dev->rd, "ERROR", 5);
1397 + write(dev->rd, "\r\n", 2);
1399 + return (len) ? 0 : -1;
1403 +/* ---------------------------------- */
1405 +/* -- Handle negotiation when we're an AG -- */
1407 +/* Bluetooth Support */
1410 +atcmd_brsf_set(blt_dev_t * dev, const char * arg, int len)
1412 + ast_log(LOG_DEBUG, "Device Supports: %s\n", arg);
1413 + dev->brsf = atoi(arg);
1414 + send_atcmd(dev, "+BRSF: %d", 23);
1418 +/* Bluetooth Voice Recognition */
1421 +atcmd_bvra_set(blt_dev_t * dev, const char * arg, int len)
1423 + ast_log(LOG_WARNING, "+BVRA Not Yet Supported\n");
1426 + // XXX:T: Fix voice recognition somehow!
1427 + int action = atoi(arg);
1428 + ast_log(LOG_DEBUG, "Voice Recognition: %s\n", (a) ? "ACTIVATED" : "DEACTIVATED");
1429 + if ((action == 0) & (dev->bvra == 1)) {
1432 + // XXX:T: Shutdown any active bvra channel
1433 + ast_log(LOG_DEBUG, "Voice Recognition: DISABLED\n");
1434 + } else if ((action == 1) && (dev->bvra == 0)) {
1437 + // XXX:T: Schedule connection to voice recognition extension/application
1438 + ast_log(LOG_DEBUG, "Voice Recognition: ENABLED\n");
1440 + ast_log(LOG_ERROR, "+BVRA out of sync (we think %d, but HS wants %d)\n", dev->bvra, action);
1450 +atcmd_cclk_read(blt_dev_t * dev)
1453 + const time_t ti = time(0);
1454 + tp = localtime_r(&ti, &t);
1455 + send_atcmd(dev, "+CCLK: \"%02d/%02d/%02d,%02d:%02d:%02d+%02d\"",
1456 + (tp->tm_year % 100), (tp->tm_mon + 1), (tp->tm_mday),
1457 + tp->tm_hour, tp->tm_min, tp->tm_sec, ((tp->tm_gmtoff / 60) / 15));
1461 +/* CHUP - Hangup Call */
1464 +atcmd_chup_execute(blt_dev_t * dev, const char * data)
1466 + if (!dev->owner) {
1467 + ast_log(LOG_ERROR, "Request to hangup call when none in progress\n");
1470 + ast_log(LOG_DEBUG, "Hangup Call\n");
1471 + ast_queue_control(dev->owner, AST_CONTROL_HANGUP);
1475 +/* CIND - Call Indicator */
1478 +atcmd_cind_read(blt_dev_t * dev)
1480 + send_atcmd(dev, "+CIND: 1,0,0");
1485 +atcmd_cind_test(blt_dev_t * dev)
1487 + send_atcmd(dev, "+CIND: (\"service\",(0,1)),(\"call\",(0,1)),(\"callsetup\",(0-4))");
1494 +atcmd_clan_read(blt_dev_t * dev)
1496 + send_atcmd(dev, "+CLAN: \"en\"");
1500 +/* Caller Id Presentation */
1503 +atcmd_clip_set(blt_dev_t * dev, const char * arg, int len)
1505 + dev->clip = atoi(arg);
1509 +/* Connected Line Identification Presentation */
1512 +atcmd_colp_set(blt_dev_t * dev, const char * arg, int len)
1514 + dev->colp = atoi(arg);
1518 +/* CMER - Mobile Equipment Event Reporting */
1521 +atcmd_cmer_set(blt_dev_t * dev, const char * arg, int len)
1524 + dev->status = BLT_STATUS_READY;
1528 +/* PhoneBook Types:
1530 + * - FD - SIM Fixed Dialing Phone Book
1531 + * - ME - ME Phone book
1532 + * - SM - SIM Phone Book
1533 + * - DC - ME dialled-calls list
1534 + * - RC - ME recieved-calls lisr
1535 + * - MC - ME missed-calls list
1536 + * - MV - ME Voice Activated Dialing List
1537 + * - HP - Hierachial Phone Book
1538 + * - BC - Own Business Card (PIN2 required)
1542 +/* Read Phone Book Entry */
1545 +atcmd_cpbr_set(blt_dev_t * dev, const char * arg, int len)
1547 + // XXX:T: Fix the phone book!
1548 + // * Maybe add res_phonebook or something? */
1549 + send_atcmd(dev, "+CPBR: %d,\"%s\",128,\"%s\"", atoi(arg), arg, arg);
1553 +/* Select Phone Book */
1556 +atcmd_cpbs_set(blt_dev_t * dev, const char * arg, int len)
1558 + // XXX:T: I guess we'll just accept any?
1563 +atcmd_cscs_set(blt_dev_t * dev, const char * arg, int len)
1565 + // XXX:T: Language
1570 +atcmd_eips_set(blt_dev_t * dev, const char * arg, int len)
1572 + ast_log(LOG_DEBUG, "Identify Presentation Set: %s=%s\n",
1573 + (*(arg) == 49) ? "ELIP" : "EOLP",
1574 + (*(arg+2) == 49) ? "ON" : "OFF");
1577 + dev->eolp = (*(arg+2) == 49) ? 1 : 0;
1579 + dev->elip = (*(arg+2) == 49) ? 1 : 0;
1584 +/* VGS - Speaker Volume Gain */
1587 +atcmd_vgs_set(blt_dev_t * dev, const char * arg, int len)
1589 + dev->gain_speaker = atoi(arg);
1594 +gui_eaid_response(blt_dev_t * dev, char * cmd)
1596 + ast_log(LOG_NOTICE, "Submenu displayed.\n");
1600 +atcmd_eami_execute(blt_dev_t * dev, const char * data)
1602 + char * number = NULL;
1604 + number = strndup(data, strlen(data));
1605 + int menuitem = atoi(number);
1607 + ast_log(LOG_NOTICE, "Menu Item '%d'.\n", menuitem);
1609 + dev->cb = gui_eaid_response;
1611 + if (menuitem == 1) {
1612 + char command[1024] = "";
1613 + const char* c1 = "AT*EAID=8,1,\"Make a SIP call\",\"Number\",\"";
1614 + const char* c2 = "\"";
1616 + (void)strncat(command, c1, sizeof(command) - strlen(command) - 1);
1617 + (void)strncat(command, gui_default_sip_number, sizeof(command) - strlen(command) - 1);
1618 + (void)strncat(command, c2, sizeof(command) - strlen(command) - 1);
1620 + //strcat(command, "AT*EAID=8,1,\"Make a SIP call\",\"Number\",\"");
1621 + //strcat(command, gui_default_sip_number);
1622 + //strcat(command, "\"");
1623 + send_atcmd(dev, command);
1624 + } else if (menuitem == 2) {
1625 + char command[1024] = "";
1626 + const char* c1 = "AT*EAID=11,1,\"Make a SIP call\",\"SIP Address\",100,\"";
1627 + const char* c2 = "\"";
1629 + (void)strncat(command, c1, sizeof(command) - strlen(command) - 1);
1630 + (void)strncat(command, gui_default_sip_address, sizeof(command) - strlen(command) - 1);
1631 + (void)strncat(command, c2, sizeof(command) - strlen(command) - 1);
1633 + //strcat(command, "AT*EAID=11,1,\"Make a SIP call\",\"SIP Address\",100,\"");
1634 + //strcat(command, gui_default_sip_address);
1635 + //strcat(command, "\"");
1636 + send_atcmd(dev, command);
1637 + } else if (menuitem == 0) {
1638 + dev->cb = gui_easm_response;
1639 +// send_atcmd(dev,"AT*EASM=\"SIP Menu\",1,1,3,\"Call Number\",\"Call Address\",\"More Options\",1");
1640 + send_atcmd(dev,"AT*EASM=\"SIP Menu\",1,1,2,\"Call Number\",\"Call Address\",1");
1642 + ast_log(LOG_ERROR, "Menu item not implementented.\n");
1648 +atcmd_eaii_execute(blt_dev_t * dev, const char * data)
1650 + int pos = 1, len = 0;
1653 + const char * start = data;
1654 + struct sockaddr_in addr;
1657 + if (*data == ',') {
1658 + memset(type, 0, 128);
1659 + strncpy(type, start, len);
1661 + ast_log(LOG_NOTICE, "Number(8)/Address(11): '%s'.\n", type);
1673 + memset(val, 0, 128);
1674 + strncpy(val, start, len);
1678 + address = strtok(val, del);
1679 + int type_int = atoi(type);
1681 + if (strcmp(address, " 0") == 0) {
1682 + ast_log(LOG_NOTICE, "Spurious EAII:\n");
1683 + ast_log(LOG_NOTICE, data);
1687 + if (type_int == 8) {
1688 + (void)strncat(address, "@sipgate.de", sizeof(address) - strlen(address) - 1);
1691 + ast_log(LOG_NOTICE, "SIP number/address: '%i','%s'.\n", type_int, address);
1693 + if (type_int == 8 || type_int == 11) {
1695 + char messagebox[1024] = "";
1696 + const char* mb1 = "AT*EAID=1,1,\"Setting up SIP call to ";
1697 + const char* mb2 = "\",30";
1699 + (void)strncat(messagebox, mb1, sizeof(messagebox) - strlen(messagebox) - 1);
1700 + (void)strncat(messagebox, address, sizeof(messagebox) - strlen(messagebox) - 1);
1701 + (void)strncat(messagebox, mb2, sizeof(messagebox) - strlen(messagebox) - 1);
1703 + //strcat(messagebox, "AT*EAID=1,1,\"Setting up SIP call to ");
1704 + //strcat(messagebox, address);
1705 + //strcat(messagebox, "\",30");
1706 + send_atcmd(dev, messagebox);
1708 + send_atcmd(dev, "AT*ESKS=2");
1709 + send_atcmd(dev, "AT*EKSP");
1710 + send_atcmd(dev, "AT*ESKS=0");
1712 + //Create manager connection to create call
1713 + int s = socket(AF_INET,SOCK_STREAM,0);
1715 + ast_log(LOG_ERROR, "Manager connection failed.");
1717 + dev->cb = ag_cgmi_response;
1718 + send_atcmd(dev, "AT*EAID=1,1,\"Call failed\"");
1721 + addr.sin_family = AF_INET;
1722 + addr.sin_port = htons(5038);
1723 + addr.sin_addr.s_addr = inet_addr("127.0.0.1");
1724 + memset(&(addr.sin_zero), '\0', 8);
1726 + if (connect(s, (struct sockaddr *)&addr, sizeof(addr)) < 0) {
1727 + ast_log(LOG_ERROR, "Manager connection failed. (2)");
1728 + dev->cb = ag_cgmi_response;
1729 + send_atcmd(dev, "AT*EAID=1,1,\"Call failed\"");
1732 + char* command = "Action: login\r\nUsername: markus\r\nSecret: supAEr\r\n\r\n";
1733 + if (write(s,command,strlen(command)) < 0) {
1734 + ast_log(LOG_ERROR, "Manager connection failed. (3)");
1735 + dev->cb = ag_cgmi_response;
1736 + send_atcmd(dev, "AT*EAID=1,1,\"Call failed\"");
1740 + char command3[1024] = "";
1741 + const char* action = "Action: Originate\r\nChannel: SIP/";
1742 + const char* action2 = "\r\nExten: 1235\r\nPriority: 1\r\nContext: sipgate.de\r\n\r\nAction: logoff\r\n\r\n";
1744 + (void)strncat(command3, action, sizeof(command3) - strlen(command3) - 1);
1745 + (void)strncat(command3, address, sizeof(command3) - strlen(command3) - 1);
1746 + (void)strncat(command3, action2, sizeof(command3) - strlen(command3) - 1);
1748 + //strcat(command3, "Action: Originate\r\nChannel: SIP/");
1749 + //strcat(command3, address);
1750 + //strcat(command3, "\r\nExten: 1235\r\nPriority: 1\r\nContext: sipgate.de\r\n\r\n");
1751 + ast_log(LOG_NOTICE, command3);
1753 + if (write(s,command3,strlen(command3)) < 0) {
1754 + ast_log(LOG_ERROR, "Manager connection failed. (5)");
1758 + //dev->cb = ag_cgmi_response;
1764 +atcmd_dial_execute(blt_dev_t * dev, const char * data)
1766 + char * number = NULL;
1768 + /* Make sure there is a ';' at the end of the line */
1769 + if (*(data + (strlen(data) - 1)) != ';') {
1770 + ast_log(LOG_WARNING, "Can't dial non-voice right now: %s\n", data);
1774 + number = strndup(data, strlen(data) - 1);
1775 + ast_log(LOG_NOTICE, "Dial: [%s]\n", number);
1777 + send_atcmd(dev, "+CIEV: 2,1");
1778 + send_atcmd(dev, "+CIEV: 3,0");
1780 + sco_start(dev, -1);
1782 + if (blt_new(dev, AST_STATE_UP, dev->context, number) == NULL) {
1791 +static int atcmd_bldn_execute(blt_dev_t * dev, const char *data)
1793 + return atcmd_dial_execute(dev, "bldn;");
1799 +atcmd_answer_execute(blt_dev_t * dev, const char * data)
1802 + if (!dev->ringing || !dev->owner) {
1803 + ast_log(LOG_WARNING, "Can't answer non existant call\n");
1809 + if (dev->ring_timer >= 0)
1810 + ast_sched_del(sched, dev->ring_timer);
1812 + dev->ring_timer = -1;
1814 + send_atcmd(dev, "+CIEV: 2,1");
1815 + send_atcmd(dev, "+CIEV: 3,0");
1817 + return answer(dev);
1821 +ag_unsol_ciev(blt_dev_t * dev, const char * data)
1823 + const char * orig = data;
1827 + while (*(data) && *(data) == ' ')
1830 + if (*(data) == 0) {
1831 + ast_log(LOG_WARNING, "Invalid value[1] for '+CIEV:%s'\n", orig);
1835 + indicator = *(data++) - 48;
1837 + if (*(data++) != ',') {
1838 + ast_log(LOG_WARNING, "Invalid value[2] for '+CIEV:%s'\n", orig);
1842 + if (*(data) == 0) {
1843 + ast_log(LOG_WARNING, "Invalid value[3] for '+CIEV:%s'\n", orig);
1847 + status = *(data) - 48;
1849 + set_cind(dev, indicator, status);
1855 +ag_unsol_cind(blt_dev_t * dev, const char * data)
1858 + while (*(data) && *(data) == ' ')
1862 + if (dev->cind == 0)
1867 + while ((data = parse_cind(data, name, 1023)) != NULL) {
1868 + ast_log(LOG_DEBUG, "CIND: %d=%s\n", pos, name);
1869 + if (strcmp(name, "call") == 0)
1870 + dev->call_pos = pos;
1871 + else if (strcmp(name, "service") == 0)
1872 + dev->service_pos = pos;
1873 + else if (strcmp(name, "call_setup") == 0 || strcmp(name, "callsetup") == 0)
1874 + dev->callsetup_pos = pos;
1878 + ast_log(LOG_DEBUG, "CIND: %d=%s\n", pos, name);
1882 + int pos = 1, len = 0;
1884 + const char * start = data;
1887 + if (*data == ',') {
1888 + memset(val, 0, 128);
1889 + strncpy(val, start, len);
1890 + set_cind(dev, pos, atoi(val));
1901 + memset(val, 0, 128);
1902 + strncpy(val, start, len);
1903 + ast_log(LOG_DEBUG, "CIND IND %d set to %d [%s]\n", pos, atoi(val), val);
1912 + * handle an incoming call
1915 +ag_unsol_clip(blt_dev_t * dev, const char * data)
1917 + const char * orig = data;
1922 + while (*(data) && *(data) == ' ')
1925 + if (*(data) == 0) {
1926 + ast_log(LOG_WARNING, "Invalid value[1] for '+CLIP:%s'\n", orig);
1930 + parse_clip(data, number, sizeof(number)-1, name, sizeof(name)-1, &type);
1931 + ast_log(LOG_NOTICE, "Parsed '+CLIP: %s' number='%s' type='%d' name='%s'\n", data, number, type, name);
1933 + blt_new(dev, AST_STATE_RING, dev->context, "s");
1943 + { "A", NULL, NULL, atcmd_answer_execute, NULL, NULL },
1944 + { "D", NULL, NULL, atcmd_dial_execute, NULL, NULL },
1945 + { "+BRSF", atcmd_brsf_set, NULL, NULL, NULL, NULL },
1946 + { "+BVRA", atcmd_bvra_set, NULL, NULL, NULL, NULL },
1947 + { "+CCLK", NULL, atcmd_cclk_read, NULL, NULL, NULL },
1948 + { "+CHUP", NULL, NULL, atcmd_chup_execute, NULL, NULL },
1949 + { "+CIEV", NULL, NULL, NULL, NULL, ag_unsol_ciev },
1950 + { "+CIND", NULL, atcmd_cind_read, NULL, atcmd_cind_test, ag_unsol_cind },
1951 + { "*EAMI", NULL, NULL, atcmd_eami_execute, NULL, NULL},
1952 + { "*EAII", NULL, NULL, atcmd_eaii_execute, NULL, NULL},
1954 + { "+CLAN", NULL, atcmd_clan_read, NULL, NULL, NULL },
1955 + { "+CLIP", atcmd_clip_set, NULL, NULL, NULL, ag_unsol_clip },
1956 + { "+COLP", atcmd_colp_set, NULL, NULL, NULL, NULL },
1957 + { "+CMER", atcmd_cmer_set, NULL, NULL, NULL, NULL },
1958 + { "+CPBR", atcmd_cpbr_set, NULL, NULL, NULL, NULL },
1959 + { "+CPBS", atcmd_cpbs_set, NULL, NULL, NULL, NULL },
1960 + { "+CSCS", atcmd_cscs_set, NULL, NULL, NULL, NULL },
1961 + { "*EIPS", atcmd_eips_set, NULL, NULL, NULL, NULL },
1962 + { "+VGS", atcmd_vgs_set, NULL, NULL, NULL, NULL },
1963 + { "+BLDN", NULL, NULL, atcmd_bldn_execute, NULL, NULL },
1966 +#define ATCMD_LIST_LEN (sizeof(atcmd_list) / sizeof(blt_atcb_t))
1968 +/* ---------------------------------- */
1970 +/* -- Handle negotiation when we're a HS -- */
1973 +ag_unknown_response(blt_dev_t * dev, char * cmd)
1975 + ast_log(LOG_DEBUG, "Got UNKN response: %s\n", cmd);
1983 +gui_easm_response(blt_dev_t * dev, char * cmd)
1985 + ast_log(LOG_NOTICE, "Menu displayed.\n");
1989 +ag_cgmi_response(blt_dev_t * dev, char * cmd)
1991 + // CGMM - Phone Model
1992 + // CGMR - Phone Revision
1995 + // VTS - send tone
1999 + // CSMS - SMS STUFFS
2003 + // CSCA - sms CENTER NUMBER
2004 + // CNMI - SMS INDICATION
2005 + // ast_log(LOG_DEBUG, "Manufacturer: %s\n", cmd);
2007 + if (dev->role == BLT_ROLE_GUI) {
2008 + ast_log(LOG_NOTICE, "Displaying Menu.\n");
2009 + dev->cb = gui_easm_response;
2010 +// send_atcmd(dev,"AT*EASM=\"SIP Menu\",1,1,3,\"Call Number\",\"Call Address\",\"More Options\",1");
2011 + send_atcmd(dev,"AT*EASM=\"SIP Menu\",1,1,2,\"Call Number\",\"Call Address\",1");
2013 + dev->cb = ag_unknown_response;
2018 +ag_cgmi_valid_response(blt_dev_t * dev, char * cmd)
2020 + // send_atcmd(dev, "AT+WS46?");
2021 + // send_atcmd(dev, "AT+CRC=1");
2022 + // send_atcmd(dev, "AT+CNUM");
2024 + if (strcmp(cmd, "OK") == 0) {
2025 + send_atcmd(dev, "AT+CGMI");
2026 + dev->cb = ag_cgmi_response;
2028 + dev->cb = ag_unknown_response;
2033 +ag_clip_response(blt_dev_t * dev, char * cmd)
2035 + send_atcmd(dev, "AT+CGMI=?");
2036 + dev->cb = ag_cgmi_valid_response;
2040 +ag_cmer_response(blt_dev_t * dev, char * cmd)
2042 + dev->cb = ag_clip_response;
2044 + dev->status = BLT_STATUS_READY;
2045 + send_atcmd(dev, "AT+CLIP=1");
2049 +ag_cind_status_response(blt_dev_t * dev, char * cmd)
2051 + // XXX:T: Handle response.
2052 + dev->cb = ag_cmer_response;
2053 + send_atcmd(dev, "AT+CMER=3,0,0,1");
2054 + // Initiliase SCO link!
2058 +ag_cind_response(blt_dev_t * dev, char * cmd)
2060 + dev->cb = ag_cind_status_response;
2062 + send_atcmd(dev, "AT+CIND?");
2066 +ag_brsf_response(blt_dev_t * dev, char * cmd)
2068 + dev->cb = ag_cind_response;
2069 + ast_log(LOG_DEBUG, "Bluetooth features: %s\n", cmd);
2071 + send_atcmd(dev, "AT+CIND=?");
2074 +/* ---------------------------------- */
2077 +sdp_register(sdp_session_t * session)
2079 + // XXX:T: Fix this horrible function so it makes some sense and is extensible!
2080 + sdp_list_t *svclass_id, *pfseq, *apseq, *root;
2081 + uuid_t root_uuid, svclass_uuid, ga_svclass_uuid, l2cap_uuid, rfcomm_uuid;
2082 + sdp_profile_desc_t profile;
2083 + sdp_list_t *aproto, *proto[2];
2084 + sdp_record_t record;
2085 + uint8_t u8 = rfcomm_channel_ag;
2086 + uint8_t u8_hs = rfcomm_channel_hs;
2087 + sdp_data_t *channel;
2090 + memset((void *)&record, 0, sizeof(sdp_record_t));
2091 + record.handle = 0xffffffff;
2092 + sdp_uuid16_create(&root_uuid, PUBLIC_BROWSE_GROUP);
2093 + root = sdp_list_append(0, &root_uuid);
2094 + sdp_set_browse_groups(&record, root);
2096 + // Register as an AG
2098 + sdp_uuid16_create(&svclass_uuid, HANDSFREE_AUDIO_GW_SVCLASS_ID);
2099 + svclass_id = sdp_list_append(0, &svclass_uuid);
2100 + sdp_uuid16_create(&ga_svclass_uuid, GENERIC_AUDIO_SVCLASS_ID);
2101 + svclass_id = sdp_list_append(svclass_id, &ga_svclass_uuid);
2102 + sdp_set_service_classes(&record, svclass_id);
2103 + sdp_uuid16_create(&profile.uuid, 0x111f);
2104 + profile.version = 0x0100;
2105 + pfseq = sdp_list_append(0, &profile);
2107 + sdp_set_profile_descs(&record, pfseq);
2109 + sdp_uuid16_create(&l2cap_uuid, L2CAP_UUID);
2110 + proto[0] = sdp_list_append(0, &l2cap_uuid);
2111 + apseq = sdp_list_append(0, proto[0]);
2113 + sdp_uuid16_create(&rfcomm_uuid, RFCOMM_UUID);
2114 + proto[1] = sdp_list_append(0, &rfcomm_uuid);
2115 + channel = sdp_data_alloc(SDP_UINT8, &u8);
2116 + proto[1] = sdp_list_append(proto[1], channel);
2117 + apseq = sdp_list_append(apseq, proto[1]);
2119 + aproto = sdp_list_append(0, apseq);
2120 + sdp_set_access_protos(&record, aproto);
2122 + sdp_set_info_attr(&record, "Voice Gateway", 0, 0);
2124 + if (sdp_record_register(session, &record, SDP_RECORD_PERSIST) < 0) {
2125 + ast_log(LOG_ERROR, "Service Record registration failed\n");
2130 + sdp_record_ag = record.handle;
2131 + sdp_record_gui = record.handle;
2133 + ast_log(LOG_NOTICE, "HeadsetAudioGateway service registered\n");
2135 + sdp_data_free(channel);
2136 + sdp_list_free(proto[0], 0);
2137 + sdp_list_free(proto[1], 0);
2138 + sdp_list_free(apseq, 0);
2139 + sdp_list_free(aproto, 0);
2143 + memset((void *)&record, 0, sizeof(sdp_record_t));
2144 + record.handle = 0xffffffff;
2145 + sdp_uuid16_create(&root_uuid, PUBLIC_BROWSE_GROUP);
2146 + root = sdp_list_append(0, &root_uuid);
2147 + sdp_set_browse_groups(&record, root);
2149 + // Register as an HS
2151 + sdp_uuid16_create(&svclass_uuid, HANDSFREE_AUDIO_GW_SVCLASS_ID);
2152 + svclass_id = sdp_list_append(0, &svclass_uuid);
2153 + sdp_uuid16_create(&ga_svclass_uuid, GENERIC_AUDIO_SVCLASS_ID);
2154 + svclass_id = sdp_list_append(svclass_id, &ga_svclass_uuid);
2155 + sdp_set_service_classes(&record, svclass_id);
2156 + sdp_uuid16_create(&profile.uuid, 0x111e);
2157 + profile.version = 0x0100;
2158 + pfseq = sdp_list_append(0, &profile);
2159 + sdp_set_profile_descs(&record, pfseq);
2161 + sdp_uuid16_create(&l2cap_uuid, L2CAP_UUID);
2162 + proto[0] = sdp_list_append(0, &l2cap_uuid);
2163 + apseq = sdp_list_append(0, proto[0]);
2165 + sdp_uuid16_create(&rfcomm_uuid, RFCOMM_UUID);
2166 + proto[1] = sdp_list_append(0, &rfcomm_uuid);
2167 + channel = sdp_data_alloc(SDP_UINT8, &u8_hs);
2168 + proto[1] = sdp_list_append(proto[1], channel);
2169 + apseq = sdp_list_append(apseq, proto[1]);
2171 + aproto = sdp_list_append(0, apseq);
2172 + sdp_set_access_protos(&record, aproto);
2173 + sdp_set_info_attr(&record, "Voice Gateway", 0, 0);
2175 + if (sdp_record_register(session, &record, SDP_RECORD_PERSIST) < 0) {
2176 + ast_log(LOG_ERROR, "Service Record registration failed\n");
2181 + sdp_record_hs = record.handle;
2183 + ast_log(LOG_NOTICE, "HeadsetAudioGateway service registered\n");
2186 + sdp_data_free(channel);
2187 + sdp_list_free(proto[0], 0);
2188 + sdp_list_free(proto[1], 0);
2189 + sdp_list_free(apseq, 0);
2190 + sdp_list_free(aproto, 0);
2196 +rfcomm_listen(bdaddr_t * bdaddr, int channel)
2200 + struct sockaddr_rc loc_addr;
2203 + if ((sock = socket(PF_BLUETOOTH, SOCK_STREAM, BTPROTO_RFCOMM)) < 0) {
2204 + ast_log(LOG_ERROR, "Can't create socket: %s (errno: %d)\n", strerror(errno), errno);
2208 + loc_addr.rc_family = AF_BLUETOOTH;
2210 + /* Local Interface Address */
2211 + bacpy(&loc_addr.rc_bdaddr, bdaddr);
2214 + loc_addr.rc_channel = channel;
2216 + if (bind(sock, (struct sockaddr *)&loc_addr, sizeof(loc_addr)) < 0) {
2217 + ast_log(LOG_ERROR, "Can't bind socket: %s (errno: %d)\n", strerror(errno), errno);
2222 + if (setsockopt(sock, SOL_SOCKET, SO_REUSEADDR, &on, sizeof(on)) == -1) {
2223 + ast_log(LOG_ERROR, "Set socket SO_REUSEADDR option on failed: errno %d, %s", errno, strerror(errno));
2228 + if (fcntl(sock, F_SETFL, O_RDWR|O_NONBLOCK) != 0)
2229 + ast_log(LOG_ERROR, "Can't set RFCOMM socket to NBIO\n");
2231 + if (listen(sock, 10) < 0) {
2232 + ast_log(LOG_ERROR,"Can not listen on the socket. %s(%d)\n", strerror(errno), errno);
2237 + ast_log(LOG_NOTICE, "Listening for RFCOMM channel %d connections on FD %d\n", channel, sock);
2244 +sco_listen(bdaddr_t * bdaddr)
2248 + struct sockaddr_sco loc_addr;
2250 + memset(&loc_addr, 0, sizeof(loc_addr));
2252 + if ((sock = socket(PF_BLUETOOTH, SOCK_SEQPACKET, BTPROTO_SCO)) < 0) {
2253 + ast_log(LOG_ERROR, "Can't create SCO socket: %s (errno: %d)\n", strerror(errno), errno);
2257 + loc_addr.sco_family = AF_BLUETOOTH;
2258 + bacpy(&loc_addr.sco_bdaddr, BDADDR_ANY);
2260 + if (bind(sock, (struct sockaddr *)&loc_addr, sizeof(loc_addr)) < 0) {
2261 + ast_log(LOG_ERROR, "Can't bind SCO socket: %s (errno: %d)\n", strerror(errno), errno);
2266 + if (setsockopt(sock, SOL_SOCKET, SO_REUSEADDR, &on, sizeof(on)) == -1) {
2267 + ast_log(LOG_ERROR, "Set SCO socket SO_REUSEADDR option on failed: errno %d, %s", errno, strerror(errno));
2272 + if (fcntl(sock, F_SETFL, O_RDWR|O_NONBLOCK) != 0)
2273 + ast_log(LOG_ERROR, "Can't set SCO socket to NBIO\n");
2275 + if (listen(sock, 10) < 0) {
2276 + ast_log(LOG_ERROR,"Can not listen on SCO socket: %s(%d)\n", strerror(errno), errno);
2281 + ast_log(LOG_NOTICE, "Listening for SCO connections on FD %d\n", sock);
2287 +rfcomm_connect(bdaddr_t * src, bdaddr_t * dst, int channel, int nbio)
2289 + struct sockaddr_rc addr;
2292 + if ((s = socket(PF_BLUETOOTH, SOCK_STREAM, BTPROTO_RFCOMM)) < 0) {
2296 + memset(&addr, 0, sizeof(addr));
2297 + addr.rc_family = AF_BLUETOOTH;
2298 + bacpy(&addr.rc_bdaddr, src);
2299 + addr.rc_channel = 0;
2301 + if (bind(s, (struct sockaddr *)&addr, sizeof(addr)) < 0) {
2306 + memset(&addr, 0, sizeof(addr));
2307 + addr.rc_family = AF_BLUETOOTH;
2308 + bacpy(&addr.rc_bdaddr, dst);
2309 + addr.rc_channel = channel;
2312 + if (fcntl(s, F_SETFL, O_RDWR|O_NONBLOCK) != 0)
2313 + ast_log(LOG_ERROR, "Can't set RFCOMM socket to NBIO\n");
2316 + if (connect(s, (struct sockaddr *)&addr, sizeof(addr)) < 0 && (nbio != 1 || (errno != EAGAIN))) {
2324 +/* Must be called with dev->lock held */
2327 +sco_connect(blt_dev_t * dev)
2329 + struct sockaddr_sco addr;
2330 + // struct sco_conninfo conn;
2331 + // struct sco_options opts;
2333 + // bdaddr_t * src = &local_bdaddr;
2336 + bdaddr_t * dst = &(dev->bdaddr);
2338 + if (dev->sco != -1) {
2339 + ast_log(LOG_ERROR, "SCO fd already open.\n");
2343 + if ((s = socket(PF_BLUETOOTH, SOCK_SEQPACKET, BTPROTO_SCO)) < 0) {
2344 + ast_log(LOG_ERROR, "Can't create SCO socket(): %s\n", strerror(errno));
2348 + memset(&addr, 0, sizeof(addr));
2350 + addr.sco_family = AF_BLUETOOTH;
2351 + bacpy(&addr.sco_bdaddr, BDADDR_ANY);
2353 + if (bind(s, (struct sockaddr *)&addr, sizeof(addr)) < 0) {
2354 + ast_log(LOG_ERROR, "Can't bind() SCO socket: %s\n", strerror(errno));
2359 + memset(&addr, 0, sizeof(addr));
2360 + addr.sco_family = AF_BLUETOOTH;
2361 + bacpy(&addr.sco_bdaddr, dst);
2363 + if (fcntl(s, F_SETFL, O_RDWR|O_NONBLOCK) != 0)
2364 + ast_log(LOG_ERROR, "Can't set SCO socket to NBIO\n");
2366 + if ((connect(s, (struct sockaddr *)&addr, sizeof(addr)) < 0) && (errno != EAGAIN)) {
2367 + ast_log(LOG_ERROR, "Can't connect() SCO socket: %s (errno %d)\n", strerror(errno), errno);
2372 + //size = sizeof(conn);
2375 +/* XXX:T: HERE, fix getting SCO conninfo.
2377 + if (getsockopt(s, SOL_SCO, SCO_CONNINFO, &conn, &size) < 0) {
2378 + ast_log(LOG_ERROR, "Can't getsockopt SCO_CONNINFO on SCO socket: %s\n", strerror(errno));
2383 + size = sizeof(opts);
2385 + if (getsockopt(s, SOL_SCO, SCO_OPTIONS, &opts, &size) < 0) {
2386 + ast_log(LOG_ERROR, "Can't getsockopt SCO_OPTIONS on SCO socket: %s\n", strerror(errno));
2391 + dev->sco_handle = conn.hci_handle;
2392 + dev->sco_mtu = opts.mtu;
2396 + ast_log(LOG_DEBUG, "SCO: %d\n", s);
2404 +/* ---------------------------------- */
2406 +/* Non blocking (async) outgoing bluetooth connection */
2409 +try_connect(blt_dev_t * dev)
2412 + ast_mutex_lock(&(dev->lock));
2414 + if (dev->status != BLT_STATUS_CONNECTING && dev->status != BLT_STATUS_DOWN) {
2415 + ast_mutex_unlock(&(dev->lock));
2419 + if (dev->rd != -1) {
2422 + struct pollfd pfd;
2424 + if (dev->status != BLT_STATUS_CONNECTING) {
2425 + ast_mutex_unlock(&(dev->lock));
2426 + dev->outgoing_id = -1;
2430 + // ret = connect(dev->rd, (struct sockaddr *)&(dev->addr), sizeof(struct sockaddr_rc)); //
2433 + pfd.events = POLLIN | POLLOUT;
2435 + ret = poll(&pfd, 1, 0);
2440 + dev->status = BLT_STATUS_DOWN;
2441 + dev->outgoing_id = ast_sched_add(sched, 10000, AST_SCHED_CB(try_connect), dev);
2442 + ast_mutex_unlock(&(dev->lock));
2448 + int len = sizeof(ret);
2449 + getsockopt(dev->rd, SOL_SOCKET, SO_ERROR, &ret, &len);
2453 + ast_log(LOG_NOTICE, "Initialised bluetooth link to device %s\n", dev->name);
2457 + struct hci_conn_info_req * cr;
2461 + cr = malloc(sizeof(*cr) + sizeof(struct hci_conn_info));
2462 + dd = hci_open_dev(hcidev_id);
2463 + cr->type = ACL_LINK;
2464 + bacpy(&cr->bdaddr, &(dev->bdaddr));
2466 + if (ioctl(dd, HCIGETCONNINFO, (unsigned long)cr) < 0) {
2467 + ast_log(LOG_ERROR, "Failed to get connection info: %s\n", strerror(errno));
2469 + ast_log(LOG_DEBUG, "HCI Handle: %d\n", cr->conn_info->handle);
2472 + if (hci_read_remote_name(dd, &(dev->bdaddr), sizeof(name), name, 25000) == 0)
2473 + ast_log(LOG_DEBUG, "Remote Name: %s\n", name);
2478 + dev->status = BLT_STATUS_NEGOTIATING;
2480 + /* If this device is an AG/GUI, we initiate the negotiation. */
2482 + if (dev->role == BLT_ROLE_AG ||
2483 + dev->role == BLT_ROLE_GUI) {
2484 + dev->cb = ag_brsf_response;
2485 + send_atcmd(dev, "AT+BRSF=23");
2488 + dev->outgoing_id = -1;
2489 + ast_mutex_unlock(&(dev->lock));
2494 + if (ret != EHOSTDOWN)
2495 + ast_log(LOG_NOTICE, "Connect to device %s failed: %s (errno %d)\n", dev->name, strerror(ret), ret);
2499 + dev->status = BLT_STATUS_DOWN;
2500 + dev->outgoing_id = ast_sched_add(sched, (ret == EHOSTDOWN) ? 10000 : 2500, AST_SCHED_CB(try_connect), dev);
2501 + ast_mutex_unlock(&(dev->lock));
2508 + dev->outgoing_id = ast_sched_add(sched, 100, AST_SCHED_CB(try_connect), dev);
2509 + ast_mutex_unlock(&(dev->lock));
2513 + ast_log(LOG_NOTICE, "RFCOMM connect start.\n");
2514 + fd = rfcomm_connect(&local_bdaddr, &(dev->bdaddr), dev->channel, 1);
2515 + ast_log(LOG_NOTICE, "RFCOMM connect done.\n");
2518 + ast_log(LOG_WARNING, "NBIO connect() to %s returned %d: %s\n", dev->name, errno, strerror(errno));
2519 + dev->outgoing_id = ast_sched_add(sched, 5000, AST_SCHED_CB(try_connect), dev);
2520 + ast_mutex_unlock(&(dev->lock));
2525 + dev->status = BLT_STATUS_CONNECTING;
2526 + dev->outgoing_id = ast_sched_add(sched, 100, AST_SCHED_CB(try_connect), dev);
2527 + ast_mutex_unlock(&(dev->lock));
2532 +/* Called whenever a new command is received while we're the AG */
2536 +process_rfcomm_cmd(blt_dev_t * dev, char * cmd)
2539 + char * fullcmd = cmd;
2541 + if (option_verbose)
2542 + ast_verbose(VERBOSE_PREFIX_1 "[%s] %*s > %s\n", role2str(dev->role), 10, dev->name, cmd);
2544 + /* Read the 'AT' from the start of the string */
2545 + if (strncmp(cmd, "AT", 2)) {
2546 + ast_log(LOG_WARNING, "Unknown command without 'AT': %s\n", cmd);
2547 + send_atcmd_error(dev);
2553 + // Don't forget 'AT' on its own is OK.
2555 + if (strlen(cmd) == 0) {
2556 + send_atcmd_ok(dev, fullcmd);
2560 + for (i = 0 ; i < ATCMD_LIST_LEN ; i++) {
2561 + if (strncmp(atcmd_list[i].str, cmd, strlen(atcmd_list[i].str)) == 0) {
2562 + char * pos = (cmd + strlen(atcmd_list[i].str));
2563 + if ((strncmp(pos, "=?", 2) == 0) && (strlen(pos) == 2)) {
2564 + /* TEST command */
2565 + if (atcmd_list[i].test) {
2566 + if (atcmd_list[i].test(dev) == 0)
2567 + send_atcmd_ok(dev, fullcmd);
2569 + send_atcmd_error(dev);
2571 + send_atcmd_ok(dev, fullcmd);
2573 + } else if ((strncmp(pos, "?", 1) == 0) && (strlen(pos) == 1)) {
2574 + /* READ command */
2575 + if (atcmd_list[i].read) {
2576 + if (atcmd_list[i].read(dev) == 0)
2577 + send_atcmd_ok(dev, fullcmd);
2579 + send_atcmd_error(dev);
2581 + ast_log(LOG_WARNING, "AT Command: '%s' missing READ function\n", fullcmd);
2582 + send_atcmd_error(dev);
2584 + } else if (strncmp(pos, "=", 1) == 0) {
2586 + if (atcmd_list[i].set) {
2587 + if (atcmd_list[i].set(dev, (pos + 1), (*(pos + 1)) ? strlen(pos + 1) : 0) == 0)
2588 + send_atcmd_ok(dev, fullcmd);
2590 + send_atcmd_error(dev);
2592 + ast_log(LOG_WARNING, "AT Command: '%s' missing SET function\n", fullcmd);
2593 + send_atcmd_error(dev);
2596 + /* EXECUTE command */
2597 + if (atcmd_list[i].execute) {
2598 + if (atcmd_list[i].execute(dev, cmd + strlen(atcmd_list[i].str)) == 0)
2599 + send_atcmd_ok(dev, fullcmd);
2601 + send_atcmd_error(dev);
2603 + ast_log(LOG_WARNING, "AT Command: '%s' missing EXECUTE function\n", fullcmd);
2604 + send_atcmd_error(dev);
2611 + ast_log(LOG_NOTICE, "Unknown AT Command: '%s' (%s)\n", fullcmd, cmd);
2612 + send_atcmd_error(dev);
2617 +/* Called when a socket is incoming */
2620 +handle_incoming(int fd, blt_role_t role)
2623 + struct sockaddr_rc addr;
2624 + int len = sizeof(addr);
2626 + // Got a new incoming socket.
2627 + ast_log(LOG_DEBUG, "Incoming RFCOMM socket\n");
2629 + ast_mutex_lock(&iface_lock);
2631 + fd = accept(fd, (struct sockaddr*)&addr, &len);
2635 + if (bacmp(&(dev->bdaddr), &addr.rc_bdaddr) == 0) {
2636 + ast_log(LOG_DEBUG, "Connect from %s\n", dev->name);
2637 + ast_mutex_lock(&(dev->lock));
2638 + /* Kill any outstanding connect attempt. */
2639 + if (dev->outgoing_id > -1) {
2640 + ast_sched_del(sched, dev->outgoing_id);
2641 + dev->outgoing_id = -1;
2644 + rd_close(dev, 0, 0);
2646 + dev->status = BLT_STATUS_NEGOTIATING;
2649 + if (dev->role == BLT_ROLE_AG ||
2650 + dev->role == BLT_ROLE_GUI) {
2651 + dev->cb = ag_brsf_response;
2652 + send_atcmd(dev, "AT+BRSF=23");
2654 + ast_mutex_unlock(&(dev->lock));
2660 + if (dev == NULL) {
2661 + ast_log(LOG_WARNING, "Connect from unknown device\n");
2664 + ast_mutex_unlock(&iface_lock);
2670 +handle_incoming_sco(int master)
2674 + struct sockaddr_sco addr;
2675 + struct sco_conninfo conn;
2676 + struct sco_options opts;
2677 + int len = sizeof(addr);
2680 + ast_log(LOG_DEBUG, "Incoming SCO socket\n");
2682 + fd = accept(master, (struct sockaddr*)&addr, &len);
2684 + if (fcntl(fd, F_SETFL, O_RDWR|O_NONBLOCK) != 0) {
2685 + ast_log(LOG_ERROR, "Can't set SCO socket to NBIO\n");
2690 + len = sizeof(conn);
2692 + if (getsockopt(fd, SOL_SCO, SCO_CONNINFO, &conn, &len) < 0) {
2693 + ast_log(LOG_ERROR, "Can't getsockopt SCO_CONNINFO on SCO socket: %s\n", strerror(errno));
2698 + len = sizeof(opts);
2700 + if (getsockopt(fd, SOL_SCO, SCO_OPTIONS, &opts, &len) < 0) {
2701 + ast_log(LOG_ERROR, "Can't getsockopt SCO_OPTIONS on SCO socket: %s\n", strerror(errno));
2706 + ast_mutex_lock(&iface_lock);
2709 + if (bacmp(&(dev->bdaddr), &addr.sco_bdaddr) == 0) {
2710 + ast_log(LOG_DEBUG, "SCO Connect from %s\n", dev->name);
2711 + ast_mutex_lock(&(dev->lock));
2712 + if (dev->sco_running != -1) {
2713 + ast_log(LOG_ERROR, "Incoming SCO socket, but SCO thread already running.\n");
2715 + sco_start(dev, fd);
2717 + ast_mutex_unlock(&(dev->lock));
2723 + ast_mutex_unlock(&iface_lock);
2725 + if (dev == NULL) {
2726 + ast_log(LOG_WARNING, "SCO Connect from unknown device\n");
2729 + // XXX:T: We need to handle the fact we might have an outgoing connection attempt in progress.
2730 + ast_log(LOG_DEBUG, "SCO: %d, HCIHandle=%d, MUT=%d\n", fd, conn.hci_handle, opts.mtu);
2738 +/* Called when there is data waiting on a socket */
2741 +handle_rd_data(blt_dev_t * dev)
2746 + while ((ret = read(dev->rd, &c, 1)) == 1) {
2748 + // log_buf[i++] = c;
2750 + if (dev->role == BLT_ROLE_HS) {
2753 + ret = process_rfcomm_cmd(dev, dev->rd_buff);
2754 + dev->rd_buff_pos = 0;
2755 + memset(dev->rd_buff, 0, BLT_RDBUFF_MAX);
2759 + if (dev->rd_buff_pos >= BLT_RDBUFF_MAX)
2762 + dev->rd_buff[dev->rd_buff_pos++] = c;
2764 + } else if (dev->role == BLT_ROLE_AG ||
2765 + dev->role == BLT_ROLE_GUI) {
2767 + //ast_log(LOG_ERROR, "%s: %c\n", dev->name, c);
2769 + switch (dev->state) {
2770 + case BLT_STATE_WANT_R:
2771 + if (c == '\r' || c == 10) {
2772 + dev->state = BLT_STATE_WANT_N;
2773 + } else if (c == '+') {
2774 + dev->state = BLT_STATE_WANT_CMD;
2775 + dev->rd_buff[dev->rd_buff_pos++] = '+';
2777 + ast_log(LOG_ERROR, "Device %s: Expected '\\r', got %d. state=BLT_STATE_WANT_R\n", dev->name, c);
2782 + case BLT_STATE_WANT_N:
2783 + if (c == '\n' || c == 13)
2784 + dev->state = BLT_STATE_WANT_CMD;
2786 + ast_log(LOG_ERROR, "Device %s: Expected '\\n', got %d. state=BLT_STATE_WANT_N\n", dev->name, c);
2791 + case BLT_STATE_WANT_CMD:
2792 + if (c == '\r' || c == 10)
2793 + dev->state = BLT_STATE_WANT_N2;
2795 + if (dev->rd_buff_pos >= BLT_RDBUFF_MAX) {
2796 + ast_log(LOG_ERROR, "Device %s: Buffer exceeded\n", dev->name);
2799 + dev->rd_buff[dev->rd_buff_pos++] = c;
2803 + case BLT_STATE_WANT_N2:
2804 + if (c == '\n' || c == 13) {
2806 + dev->state = BLT_STATE_WANT_R;
2808 + if (dev->rd_buff[0] == '+') {
2810 + // find unsolicited
2811 + for (i = 0 ; i < ATCMD_LIST_LEN ; i++) {
2812 + if (strncmp(atcmd_list[i].str, dev->rd_buff, strlen(atcmd_list[i].str)) == 0) {
2813 + if (atcmd_list[i].unsolicited)
2814 + atcmd_list[i].unsolicited(dev, dev->rd_buff + strlen(atcmd_list[i].str) + 1);
2816 + ast_log(LOG_WARNING, "Device %s: Unhandled Unsolicited: %s\n", dev->name, dev->rd_buff);
2821 + if (option_verbose)
2822 + ast_verbose(VERBOSE_PREFIX_1 "[%s] %*s > %s\n", role2str(dev->role), 10, dev->name, dev->rd_buff);
2824 + if (i == ATCMD_LIST_LEN)
2825 + ast_log(LOG_NOTICE, "Device %s: Got unsolicited message: %s\n", dev->name, dev->rd_buff);
2827 + } else if (dev->rd_buff[0] == '*') {
2828 + if (option_verbose)
2829 + ast_verbose(VERBOSE_PREFIX_1 "[%s]* %*s > %s\n", role2str(dev->role), 9, dev->name, dev->rd_buff);
2833 + for (i = 0 ; i < ATCMD_LIST_LEN ; i++) {
2834 + if (strncmp(atcmd_list[i].str, dev->rd_buff, strlen(atcmd_list[i].str)) == 0) {
2835 + if (atcmd_list[i].execute)
2836 + atcmd_list[i].execute(dev, dev->rd_buff + strlen(atcmd_list[i].str) + 1);
2838 + ast_log(LOG_ERROR, "Device %s: Unhandled Execute: %s\n", dev->name, dev->rd_buff);
2847 + strcmp(dev->rd_buff, "OK") != 0 &&
2848 + strcmp(dev->rd_buff, "CONNECT") != 0 &&
2849 + strcmp(dev->rd_buff, "RING") != 0 &&
2850 + strcmp(dev->rd_buff, "NO CARRIER") != 0 &&
2851 + strcmp(dev->rd_buff, "ERROR") != 0 &&
2852 + strcmp(dev->rd_buff, "NO DIALTONE") != 0 &&
2853 + strcmp(dev->rd_buff, "BUSY") != 0 &&
2854 + strcmp(dev->rd_buff, "NO ANSWER") != 0 &&
2855 + strcmp(dev->rd_buff, "DELAYED") != 0
2857 + // It must be a multiline error
2858 + strncpy(dev->last_err_cmd, dev->rd_buff, 1023);
2859 + if (option_verbose)
2860 + ast_verbose(VERBOSE_PREFIX_1 "[%s] %*s > %s\n", role2str(dev->role), 10, dev->name, dev->rd_buff);
2861 + } else if (dev->cb) {
2862 + if (option_verbose)
2863 + ast_verbose(VERBOSE_PREFIX_1 "[%s] %*s > %s\n", role2str(dev->role), 10, dev->name, dev->rd_buff);
2864 + dev->cb(dev, dev->rd_buff);
2866 + ast_log(LOG_ERROR, "Device %s: Data on socket in HS mode, but no callback\n", dev->name);
2871 + dev->rd_buff_pos = 0;
2872 + memset(dev->rd_buff, 0, BLT_RDBUFF_MAX);
2875 + ast_log(LOG_ERROR, "Device %s: Expected '\\n' got %d. state = BLT_STATE_WANT_N2:\n", dev->name, c);
2883 + ast_log(LOG_ERROR, "Device %s: Unknown device state %d\n", dev->name, dev->state);
2895 +/* Close the devices RFCOMM socket, and SCO if it exists. Must hold dev->lock */
2898 +rd_close(blt_dev_t * dev, int reconnect, int e)
2907 + dev->status = BLT_STATUS_DOWN;
2912 + ast_setstate(dev->owner, AST_STATE_DOWN);
2913 + ast_queue_control(dev->owner, AST_CONTROL_HANGUP);
2916 + /* Schedule a reconnect */
2917 + if (reconnect && dev->autoconnect) {
2918 + dev->outgoing_id = ast_sched_add(sched, 5000, AST_SCHED_CB(try_connect), dev);
2920 + if (monitor_thread == pthread_self()) {
2921 + // Because we're not the monitor thread, we needd to inturrupt poll().
2922 + pthread_kill(monitor_thread, SIGURG);
2926 + ast_log(LOG_NOTICE, "Device %s disconnected, scheduled reconnect in 5 seconds: %s (errno %d)\n", dev->name, strerror(e), e);
2928 + ast_log(LOG_NOTICE, "Device %s disconnected: %s (errno %d)\n", dev->name, strerror(e), e);
2935 + * Remember that we can only add to the scheduler from
2936 + * the do_monitor thread, as it calculates time to next one from
2941 +do_monitor(void * data)
2943 +#define SRV_SOCK_CNT 4
2947 + struct pollfd * pfds = malloc(sizeof(struct pollfd) * (ifcount + SRV_SOCK_CNT));
2949 + /* -- We start off by trying to connect all of our devices (non blocking) -- */
2951 + monitor_pid = getpid();
2953 + if (ast_mutex_lock(&iface_lock)) {
2954 + ast_log(LOG_ERROR, "Failed to get iface_lock.\n");
2961 + if (socketpair(PF_UNIX, SOCK_STREAM, 0, dev->sco_pipe) != 0) {
2962 + ast_log(LOG_ERROR, "Failed to create socket pair: %s (errno %d)\n", strerror(errno), errno);
2963 + ast_mutex_unlock(&iface_lock);
2967 + if (dev->autoconnect && dev->status == BLT_STATUS_DOWN)
2968 + dev->outgoing_id = ast_sched_add(sched, 1500, AST_SCHED_CB(try_connect), dev);
2971 + ast_mutex_unlock(&iface_lock);
2973 + /* -- Now, Scan all sockets, and service scheduler -- */
2975 + pfds[0].fd = rfcomm_sock_ag;
2976 + pfds[0].events = POLLIN;
2978 + pfds[1].fd = rfcomm_sock_hs;
2979 + pfds[1].events = POLLIN;
2981 + pfds[2].fd = rfcomm_sock_gui;
2982 + pfds[2].events = POLLIN;
2984 + pfds[3].fd = sco_socket;
2985 + pfds[3].events = POLLIN;
2988 + int cnt = SRV_SOCK_CNT;
2991 + /* -- Build pfds -- */
2993 + if (ast_mutex_lock(&iface_lock)) {
2994 + ast_log(LOG_ERROR, "Failed to get iface_lock.\n");
2999 + ast_mutex_lock(&(dev->lock));
3000 + if (dev->rd > 0 && ((dev->status != BLT_STATUS_DOWN) && (dev->status != BLT_STATUS_CONNECTING))) {
3001 + pfds[cnt].fd = dev->rd;
3002 + pfds[cnt].events = POLLIN;
3005 + ast_mutex_unlock(&(dev->lock));
3008 + ast_mutex_unlock(&iface_lock);
3010 + /* -- End Build pfds -- */
3012 + res = ast_sched_wait(sched);
3013 + res = poll(pfds, cnt, MAX(100, MIN(100, res)));
3016 + ast_sched_runq(sched);
3018 + if (pfds[0].revents) {
3019 + handle_incoming(rfcomm_sock_ag, BLT_ROLE_AG);
3023 + if (pfds[1].revents) {
3024 + handle_incoming(rfcomm_sock_hs, BLT_ROLE_HS);
3028 + if (pfds[2].revents) {
3029 + handle_incoming(rfcomm_sock_gui, BLT_ROLE_GUI);
3033 + if (pfds[3].revents) {
3034 + handle_incoming_sco(sco_socket);
3041 + for (i = SRV_SOCK_CNT ; i < cnt ; i++) {
3043 + /* Optimise a little bit */
3046 + else if (pfds[i].revents == 0)
3049 + /* -- Find the socket that has activity -- */
3051 + if (ast_mutex_lock(&iface_lock)) {
3052 + ast_log(LOG_ERROR, "Failed to get iface_lock.\n");
3059 + if (pfds[i].fd == dev->rd) {
3060 + ast_mutex_lock(&(dev->lock));
3061 + if (pfds[i].revents & POLLIN) {
3062 + if (handle_rd_data(dev) == -1) {
3063 + rd_close(dev, 0, 0);
3066 + rd_close(dev, 1, sock_err(dev->rd));
3068 + ast_mutex_unlock(&(dev->lock));
3075 + if (dev == NULL) {
3076 + ast_log(LOG_ERROR, "Unhandled fd from poll()\n");
3077 + close(pfds[i].fd);
3080 + ast_mutex_unlock(&iface_lock);
3082 + /* -- End find socket with activity -- */
3092 +restart_monitor(void)
3095 + if (monitor_thread == AST_PTHREADT_STOP)
3098 + if (ast_mutex_lock(&monitor_lock)) {
3099 + ast_log(LOG_WARNING, "Unable to lock monitor\n");
3103 + if (monitor_thread == pthread_self()) {
3104 + ast_mutex_unlock(&monitor_lock);
3105 + ast_log(LOG_WARNING, "Cannot kill myself\n");
3109 + if (monitor_thread != AST_PTHREADT_NULL) {
3111 + /* Just signal it to be sure it wakes up */
3112 + pthread_cancel(monitor_thread);
3113 + pthread_kill(monitor_thread, SIGURG);
3114 + ast_log(LOG_DEBUG, "Waiting for monitor thread to join...\n");
3115 + pthread_join(monitor_thread, NULL);
3116 + ast_log(LOG_DEBUG, "joined\n");
3120 + /* Start a new monitor */
3121 + if (ast_pthread_create(&monitor_thread, NULL, do_monitor, NULL) < 0) {
3122 + ast_mutex_unlock(&monitor_lock);
3123 + ast_log(LOG_ERROR, "Unable to start monitor thread.\n");
3129 + ast_mutex_unlock(&monitor_lock);
3134 +blt_parse_config(void)
3136 + struct ast_config * cfg;
3137 + struct ast_variable * v;
3140 + cfg = ast_config_load(BLT_CONFIG_FILE);
3143 + ast_log(LOG_NOTICE, "Unable to load Bluetooth config: %s. Bluetooth disabled\n", BLT_CONFIG_FILE);
3147 + v = ast_variable_browse(cfg, "general");
3150 + if (!strcasecmp(v->name, "rfchannel_ag")) {
3151 + rfcomm_channel_ag = atoi(v->value);
3152 + } else if (!strcasecmp(v->name, "rfchannel_hs")) {
3153 + rfcomm_channel_hs = atoi(v->value);
3154 + } else if (!strcasecmp(v->name, "rfchannel_gui")) {
3155 + rfcomm_channel_gui = atoi(v->value);
3156 + } else if (!strcasecmp(v->name, "interface")) {
3157 + hcidev_id = atoi(v->value);
3158 + } else if (!strcasecmp(v->name, "gui_default_sip_number")) {
3159 + gui_default_sip_number = v->value;
3160 + } else if (!strcasecmp(v->name, "gui_default_sip_address")) {
3161 + gui_default_sip_address = v->value;
3163 + ast_log(LOG_WARNING, "Unknown config key '%s' in section [general]\n", v->name);
3167 + cat = ast_category_browse(cfg, NULL);
3173 + if (strcasecmp(cat, "general")) {
3174 + blt_dev_t * device = malloc(sizeof(blt_dev_t));
3175 + memset(device, 0, sizeof(blt_dev_t));
3176 + device->sco_running = -1;
3179 + device->outgoing_id = -1;
3180 + device->status = BLT_STATUS_DOWN;
3181 + str2ba(cat, &(device->bdaddr));
3182 + device->name = ast_variable_retrieve(cfg, cat, "name");
3184 + str = ast_variable_retrieve(cfg, cat, "type");
3186 + if (str == NULL) {
3187 + ast_log(LOG_ERROR, "Device [%s] has no role. Specify type=<HS/AG>\n", cat);
3189 + } else if (strcasecmp(str, "HS") == 0) {
3190 + device->role = BLT_ROLE_HS;
3191 + } else if (strcasecmp(str, "AG") == 0) {
3192 + device->role = BLT_ROLE_AG;
3193 + } else if (strcasecmp(str, "GUI") == 0) {
3194 + device->role = BLT_ROLE_GUI;
3196 + ast_log(LOG_ERROR, "Device [%s] has invalid role '%s'\n", cat, str);
3200 + /* XXX:T: Find channel to use using SDP.
3201 + * However, this needs to be non blocking, and I can't see
3202 + * anything in sdp_lib.h that will allow non blocking calls.
3205 + device->channel = 1;
3207 + if ((str = ast_variable_retrieve(cfg, cat, "channel")) != NULL)
3208 + device->channel = atoi(str);
3210 + if ((str = ast_variable_retrieve(cfg, cat, "autoconnect")) != NULL)
3211 + device->autoconnect = (strcasecmp(str, "yes") == 0 || strcmp(str, "1") == 0) ? 1 : 0;
3213 + if ((str = ast_variable_retrieve(cfg, cat, "context")) != NULL)
3214 + device->context = str;
3216 + device->context = "bluetooth";
3218 + device->next = iface_head;
3219 + iface_head = device;
3223 + cat = ast_category_browse(cfg, cat);
3230 +blt_show_peers(int fd, int argc, char *argv[])
3234 + if (ast_mutex_lock(&iface_lock)) {
3235 + ast_log(LOG_ERROR, "Failed to get Iface lock\n");
3236 + ast_cli(fd, "Failed to get iface lock\n");
3237 + return RESULT_FAILURE;
3242 + ast_cli(fd, "BDAddr Name Role Status A/C SCOCon/Fd/Th Sig\n");
3243 + ast_cli(fd, "----------------- ---------- ---- ----------- --- ------------ ---\n");
3247 + ba2str(&(dev->bdaddr), b1);
3248 + ast_cli(fd, "%s %-10s %-4s %-11s %-3s %2d/%02d/%-6ld %s\n",
3250 +// (dev->role == BLT_ROLE_HS) ? "HS" : "AG",
3251 + (dev->role == BLT_ROLE_HS) ? "HS" : (dev->role == BLT_ROLE_AG) ? "AG" : "GUI",
3252 + status2str(dev->status),
3253 + (dev->autoconnect) ? "Yes" : "No",
3257 + (dev->role == BLT_ROLE_AG) ? (dev->service) ? "Yes" : "No" : "N/A"
3262 + ast_mutex_unlock(&iface_lock);
3263 + return RESULT_SUCCESS;
3267 +blt_show_information(int fd, int argc, char *argv[])
3270 + ba2str(&local_bdaddr, b1);
3271 + ast_cli(fd, "-------------------------------------------\n");
3272 + ast_cli(fd, " Version : %s\n", BLT_SVN_REVISION);
3273 + ast_cli(fd, " Monitor PID : %d\n", monitor_pid);
3274 + ast_cli(fd, " RFCOMM AG : Channel %d, FD %d\n", rfcomm_channel_ag, rfcomm_sock_ag);
3275 + ast_cli(fd, " RFCOMM HS : Channel %d, FD %d\n", rfcomm_channel_hs, rfcomm_sock_hs);
3276 + ast_cli(fd, " RFCOMM GUI : Channel %d, FD %d\n", rfcomm_channel_gui, rfcomm_sock_gui);
3277 + ast_cli(fd, " Device : hci%d, MAC Address %s\n", hcidev_id, b1);
3278 + ast_cli(fd, "-------------------------------------------\n");
3279 + return RESULT_SUCCESS;
3283 +blt_ag_sendcmd(int fd, int argc, char *argv[])
3288 + return RESULT_SHOWUSAGE;
3290 + ast_mutex_lock(&iface_lock);
3293 + if (!strcasecmp(argv[2], dev->name))
3297 + ast_mutex_unlock(&iface_lock);
3300 + ast_cli(fd, "Device '%s' does not exist\n", argv[2]);
3301 + return RESULT_FAILURE;
3304 + if ((dev->role != BLT_ROLE_AG) && (dev->role != BLT_ROLE_GUI)) {
3305 + ast_cli(fd, "Device '%s' is not an AG or GUI\n", argv[2]);
3306 + return RESULT_FAILURE;
3309 + if (dev->status == BLT_STATUS_DOWN || dev->status == BLT_STATUS_NEGOTIATING) {
3310 + ast_cli(fd, "Device '%s' is not connected\n", argv[2]);
3311 + return RESULT_FAILURE;
3314 + if (*(argv[3] + strlen(argv[3]) - 1) == '.')
3315 + *(argv[3] + strlen(argv[3]) - 1) = '?';
3317 + ast_cli(fd, "Sending AT command to %s: %s\n", dev->name, argv[3]);
3319 + ast_mutex_lock(&(dev->lock));
3320 + send_atcmd(dev, argv[3]);
3321 + ast_mutex_unlock(&(dev->lock));
3323 + return RESULT_SUCCESS;
3327 +complete_device(char * line, char * word, int pos, int state, int rpos, blt_role_t role)
3336 + ast_mutex_lock(&iface_lock);
3342 + if ((dev->role == role) && (!strncasecmp(word, dev->name, strlen(word)))) {
3343 + if (++which > state)
3351 + ret = strdup(dev->name);
3355 + ast_mutex_unlock(&iface_lock);
3361 +complete_device_2_ag_gui(char * line, char * word, int pos, int state)
3363 + return complete_device(line, word, pos, state, 2, BLT_ROLE_AG);
3366 +static char show_peers_usage[] =
3367 +"Usage: bluetooth show peers\n"
3368 +" List all bluetooth peers and their status\n";
3370 +static struct ast_cli_entry
3372 + { { "bluetooth", "show", "peers", NULL }, blt_show_peers, "List Bluetooth Peers", show_peers_usage };
3375 +static char ag_sendcmd[] =
3376 +"Usage: bluetooth <device> sendcmd <cmd>\n"
3377 +" Sends a AT cmd over the RFCOMM link, and print result (AG only)\n";
3379 +static struct ast_cli_entry
3381 + { { "bluetooth", "sendcmd", NULL }, blt_ag_sendcmd, "Send AG/GUI an AT command", ag_sendcmd, complete_device_2_ag_gui };
3383 +static char show_information[] =
3384 +"Usage: bluetooth show information\n"
3385 +" Lists information about the bluetooth subsystem\n";
3387 +static struct ast_cli_entry
3388 +cli_show_information =
3389 + { { "bluetooth", "show", "information", NULL }, blt_show_information, "List Bluetooth Info", show_information };
3392 +remove_sdp_records(void)
3395 + sdp_session_t * sdp;
3396 + sdp_list_t * attr;
3397 + sdp_record_t * rec;
3399 + uint32_t range = 0x0000ffff;
3401 + if (sdp_record_ag == -1 || sdp_record_gui == -1 || sdp_record_hs == -1)
3404 + ast_log(LOG_DEBUG, "Removing SDP records\n");
3406 + sdp = sdp_connect(BDADDR_ANY, BDADDR_LOCAL, SDP_RETRY_IF_BUSY);
3411 + attr = sdp_list_append(0, &range);
3412 + rec = sdp_service_attr_req(sdp, sdp_record_ag, SDP_ATTR_REQ_RANGE, attr);
3413 + sdp_list_free(attr, 0);
3416 + if (sdp_record_unregister(sdp, rec) == 0)
3419 + rec = sdp_service_attr_req(sdp, sdp_record_gui, SDP_ATTR_REQ_RANGE, attr);
3420 + sdp_list_free(attr, 0);
3423 + if (sdp_record_unregister(sdp, rec) == 0)
3426 + attr = sdp_list_append(0, &range);
3427 + rec = sdp_service_attr_req(sdp, sdp_record_hs, SDP_ATTR_REQ_RANGE, attr);
3428 + sdp_list_free(attr, 0);
3431 + if (sdp_record_unregister(sdp, rec) == 0)
3437 + ast_log(LOG_NOTICE, "Removed SDP records\n");
3439 + ast_log(LOG_ERROR, "Failed to remove SDP records\n");
3444 +__unload_module(void)
3447 +#if ASTERISK_VERSION_NUM <= 010107
3448 + ast_channel_unregister(BLT_CHAN_NAME);
3450 + ast_channel_unregister(&blt_tech);
3453 + if (monitor_thread != AST_PTHREADT_NULL) {
3455 + if (ast_mutex_lock(&monitor_lock)) {
3457 + if (monitor_thread && (monitor_thread != AST_PTHREADT_STOP) && (monitor_thread != AST_PTHREADT_NULL)) {
3458 + pthread_cancel(monitor_thread);
3459 + pthread_kill(monitor_thread, SIGURG);
3460 + fprintf(stderr, "Waiting for monitor thread to join...\n");
3461 + pthread_join(monitor_thread, NULL);
3462 + fprintf(stderr, "joined\n");
3464 + monitor_thread = AST_PTHREADT_STOP;
3465 + ast_mutex_unlock(&monitor_lock);
3469 + ast_log(LOG_WARNING, "Unable to lock the monitor\n");
3476 + ast_unregister_atexit(remove_sdp_records);
3477 + remove_sdp_records();
3484 + sdp_session_t * sess;
3488 + hcidev_id = BLT_DEFAULT_HCI_DEV;
3490 + if (blt_parse_config() != 0) {
3491 + ast_log(LOG_ERROR, "Bluetooth configuration error. Bluetooth Disabled\n");
3492 + return unload_module();
3495 + dd = hci_open_dev(hcidev_id);
3497 + ast_log(LOG_ERROR, "Unable to open interface hci%d: %s.\n", hcidev_id, strerror(errno));
3501 + hci_read_voice_setting(dd, &vs, 1000);
3505 + if (vs != 0x0060) {
3506 + ast_log(LOG_ERROR, "Bluetooth voice setting must be 0x0060, not 0x%04x\n", vs);
3511 + if ((sched = sched_context_create()) == NULL) {
3512 + ast_log(LOG_WARNING, "Unable to create schedule context\n");
3516 + memset(&local_bdaddr, 0, sizeof(local_bdaddr));
3518 + hci_devba(hcidev_id, &local_bdaddr);
3520 + /* --- Add SDP record --- */
3522 + sess = sdp_connect(&local_bdaddr, BDADDR_LOCAL, SDP_RETRY_IF_BUSY);
3524 + if ((rfcomm_sock_ag = rfcomm_listen(&local_bdaddr, rfcomm_channel_ag)) < 0) {
3528 + if ((rfcomm_sock_hs = rfcomm_listen(&local_bdaddr, rfcomm_channel_hs)) < 0)
3531 + if ((rfcomm_sock_gui = rfcomm_listen(&local_bdaddr, rfcomm_channel_gui)) < 0)
3534 + if ((sco_socket = sco_listen(&local_bdaddr)) < 0)
3538 + ast_log(LOG_ERROR, "Failed to connect to SDP server: %s\n", strerror(errno));
3542 + if (sdp_register(sess) != 0) {
3543 + ast_log(LOG_ERROR, "Failed to register HeadsetAudioGateway in SDP\n");
3549 + if (restart_monitor() != 0)
3552 +#if ASTERISK_VERSION_NUM <= 010107
3553 + if (ast_channel_register(BLT_CHAN_NAME, "Bluetooth Driver", BLUETOOTH_FORMAT, blt_request)) {
3555 + if (ast_channel_register(&blt_tech)) {
3557 + ast_log(LOG_ERROR, "Unable to register channel class BTL\n");
3558 + __unload_module();
3562 + ast_cli_register(&cli_show_information);
3563 + ast_cli_register(&cli_show_peers);
3564 + ast_cli_register(&cli_ag_sendcmd);
3566 + ast_register_atexit(remove_sdp_records);
3568 + ast_log(LOG_NOTICE, "Loaded Bluetooth support, %s\n", BLT_SVN_REVISION + 1);
3574 +unload_module(void)
3576 + ast_cli_unregister(&cli_ag_sendcmd);
3577 + ast_cli_unregister(&cli_show_peers);
3578 + ast_cli_unregister(&cli_show_information);
3579 + return __unload_module();
3586 + ast_mutex_lock(&usecnt_lock);
3588 + ast_mutex_unlock(&usecnt_lock);
3592 +char *description()
3594 + return "Bluetooth Channel Driver";
3600 + return ASTERISK_GPL_KEY;
3604 diff -Nru asterisk-1.2.14.org/channels/Makefile asterisk-1.2.14/channels/Makefile
3605 --- asterisk-1.2.14.org/channels/Makefile 2006-08-17 23:57:19.000000000 +0200
3606 +++ asterisk-1.2.14/channels/Makefile 2006-12-27 09:03:53.000000000 +0100
3607 @@ -249,6 +249,13 @@
3608 #chan_modem.so : chan_modem.o
3609 # $(CC) -rdynamic -shared -Xlinker -x -o $@ $<
3612 +# Asterisk Bluetooth Support
3613 +# http://www.crazygreek.co.uk/content/chan_bluetooth
3615 +chan_bluetooth.so: chan_bluetooth.o
3616 + $(CC) $(SOLINK) -o $@ $< $(EXTRA_LDFLAGS) -lbluetooth
3619 for x in $(CHANNEL_LIBS); do $(INSTALL) -m 755 $$x $(DESTDIR)$(MODULES_DIR) ; done
3620 if ! [ -f chan_iax.so ]; then rm -f $(DESTDIR)$(MODULES_DIR)/chan_iax.so ; fi
3621 diff -Nru asterisk-1.2.14.org/configs/bluetooth.conf asterisk-1.2.14/configs/bluetooth.conf
3622 --- asterisk-1.2.14.org/configs/bluetooth.conf 1970-01-01 01:00:00.000000000 +0100
3623 +++ asterisk-1.2.14/configs/bluetooth.conf 2006-12-27 09:03:53.000000000 +0100
3626 +; Channel we listen on as a HS (Headset)
3628 +; Channel we listen on as an AG (AudioGateway)
3630 +; Channel we listen on as GUI
3632 +; hci interface to use (number - e.g '0')
3635 +; RFCOMM channel to connect to. For a HandsSet:
3636 +; sdptool search --bdaddr xx:xx:xx:xx:xx:xx 0x111E
3637 +; or,for an AudioGateway (Phone):
3638 +; sdptool search --bdaddr xx:xx:xx:xx:xx:xx 0x111F
3640 +; Find the 'channel' value under RFCOMM.
3643 +; Automatically connect?
3646 +;example for a SonyEricsson mobile as a GUI device
3647 +[00:0F:DE:6E:77:6B]
3654 +;[00:0E:6D:1A:3D:86]
3660 +[00:0E:A1:01:49:AE]
3666 +;[00:0A:D9:EB:FD:D8]