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
5 chan_h323.so: chan_h323.o h323/libchanh323.a
6 $(CC) $(SOLINK) -o $@ $< h323/libchanh323.a $(CHANH323LIB) -L$(PWLIBDIR)/lib $(PTLIB) -L$(OPENH323DIR)/lib $(H323LIB) -L/usr/lib -lcrypto -lssl -lexpat
9 +# Asterisk Bluetooth Support
10 +# http://www.crazygreek.co.uk/content/chan_bluetooth
12 +chan_bluetooth.so: chan_bluetooth.o
13 + $(CC) $(SOLINK) -o $@ $< $(EXTRA_LDFLAGS) -lbluetooth
16 #chan_modem.so : chan_modem.o
17 # $(CC) -rdynamic -shared -Xlinker -x -o $@ $<
18 diff -ruN asterisk-1.0.9-old/channels/chan_bluetooth.c asterisk-1.0.9-new/channels/chan_bluetooth.c
19 --- asterisk-1.0.9-old/channels/chan_bluetooth.c 1970-01-01 01:00:00.000000000 +0100
20 +++ asterisk-1.0.9-new/channels/chan_bluetooth.c 2005-09-06 22:51:30.000000000 +0200
23 + * Asterisk -- A telephony toolkit for Linux.
25 + * Asterisk Bluetooth Channel
27 + * Author: Theo Zourzouvillys <theo@adaptive-it.co.uk>
29 + * Adaptive Linux Solutions <http://www.adaptive-it.co.uk>
31 + * Copyright (C) 2004 Adaptive Linux Solutions
33 + * This program is free software, distributed under the terms of
34 + * the GNU General Public License
36 + * ******************* NOTE NOTE NOTE NOTE NOTE *********************
38 + * This code is not at all tested, and only been developed with a
39 + * HBH-200 headset and a Nokia 6310i right now.
41 + * Expect it to crash, dial random numbers, and steal all your money.
43 + * PLEASE try any headsets and phones, and let me know the results,
44 + * working or not, along with all debug output!
46 + * ------------------------------------------------------------------
48 + * Asterisk Bluetooth Support
50 + * Well, here we go - Attempt to provide Handsfree profile support in
51 + * both AG and HF modes, AG (AudioGateway) mode support for using
52 + * headsets, and HF (Handsfree) mode for utilising mobile/cell phones
54 + * It would be nice to also provide Headset support at some time in
55 + * the future, however, a working Handsfree profile is nice for now,
56 + * and as far as I can see, almost all new HS devices also support HF
58 + * ------------------------------------------------------------------
61 + * You need to have bluez's bluetooth stack, along with user space
62 + * tools (>=v2.10), and running hcid and sdsp.
64 + * See bluetooth.conf for configuration details.
66 + * - Ensure bluetooth subsystem is up and running. 'hciconfig'
67 + * should show interface as UP.
69 + * - If you're trying to use a headset/HS, start up asterisk, and try
70 + * to pair it as you normally would.
72 + * - If you're trying to use a Phone/AG, just make sure bluetooth is
73 + * enabled on your phone, and start up asterisk.
75 + * - 'bluetooth show peers' will show all bluetooth devices states.
77 + * - Send a call out by using Dial(BLT/DevName/0123456). Call a HS
78 + * with Dial(BLT/DevName)
80 + * ------------------------------------------------------------------
83 + * - What should happen when an AG is paired with asterisk and
84 + * someone uses the AG dalling a number manually? My test phone
85 + * seems to try to open an SCO link. Perhaps an extension to
86 + * route the call to, or maybe drop the RFCOM link all together?
88 + * ------------------------------------------------------------------
91 + * PLEASE email <theo@adaptive-it.co.uk> with the results of ANY
92 + * device not listed in here (working or not), or if the device is
93 + * listed and it doesn't work! Please also email full debug output
94 + * for any device not working correctly or generating errors in log.
96 + * HandsFree Profile:
99 + * - Ericsson HBH-200
101 + * AG (AudioGateway):
104 + * ------------------------------------------------------------------
106 + * Questions, bugs, or (preferably) patches to:
108 + * <theo@adaptive-it.co.uk>
110 + * ------------------------------------------------------------------
113 +/* ---------------------------------- */
117 +#include <asterisk/lock.h>
118 +#include <asterisk/utils.h>
119 +#include <asterisk/channel.h>
120 +#include <asterisk/config.h>
121 +#include <asterisk/logger.h>
122 +#include <asterisk/module.h>
123 +#include <asterisk/pbx.h>
124 +#include <asterisk/sched.h>
125 +#include <asterisk/options.h>
126 +#include <asterisk/cli.h>
127 +#include <asterisk/callerid.h>
128 +#include <sys/socket.h>
129 +#include <sys/signal.h>
130 +#include <sys/time.h>
134 +#include <arpa/inet.h>
136 +#include <sys/ioctl.h>
140 +#include <bluetooth/bluetooth.h>
141 +#include <bluetooth/hci.h>
142 +#include <bluetooth/hci_lib.h>
143 +#include <bluetooth/sco.h>
144 +#include <bluetooth/rfcomm.h>
145 +#include <bluetooth/sdp.h>
146 +#include <bluetooth/sdp_lib.h>
148 +/* --- Data types and definitions --- */
150 +#ifndef HANDSFREE_AUDIO_GW_SVCLASS_ID
151 +# define HANDSFREE_AUDIO_GW_SVCLASS_ID 0x111f
153 +#define BLUETOOTH_FORMAT AST_FORMAT_SLINEAR
154 +#define BLT_CHAN_NAME "BLT"
155 +#define BLT_CONFIG_FILE "bluetooth.conf"
156 +#define BLT_RDBUFF_MAX 1024
157 +#define BLT_DEFAULT_HCI_DEV 0
158 +#define BLT_SVN_REVISION "$Rev$"
160 +/* ---------------------------------- */
163 + BLT_ROLE_NONE = 0, // Unknown Device
164 + BLT_ROLE_HS = 1, // Device is a Headset
165 + BLT_ROLE_AG = 2, // Device is an Audio Gateway
166 + BLT_ROLE_GUI = 3 // Device is used as an GUI
169 +/* State when we're in HS mode */
172 + BLT_STATE_WANT_R = 0,
173 + BLT_STATE_WANT_N = 1,
174 + BLT_STATE_WANT_CMD = 2,
175 + BLT_STATE_WANT_N2 = 3,
180 + BLT_STATUS_CONNECTING,
181 + BLT_STATUS_NEGOTIATING,
183 + BLT_STATUS_RINGING,
184 + BLT_STATUS_IN_CALL,
187 +/* ---------------------------------- */
189 +/* Default config settings */
191 +#define BLT_DEFAULT_CHANNEL_AG 5
192 +#define BLT_DEFAULT_CHANNEL_HS 6
193 +#define BLT_DEFAULT_CHANNEL_GUI 1
194 +#define BLT_DEFAULT_ROLE BLT_ROLE_HS
195 +#define BLT_OBUF_LEN (48 * 25)
197 +#define BUFLEN (4800)
199 +/* ---------------------------------- */
201 +typedef struct blt_dev blt_dev_t;
203 +void ag_cgmi_response(blt_dev_t * dev, char * cmd);
204 +void ag_unknown_response(blt_dev_t * dev, char * cmd);
205 +void ag_cgmi_valid_response(blt_dev_t * dev, char * cmd);
206 +void ag_clip_response(blt_dev_t * dev, char * cmd);
207 +void ag_cmer_response(blt_dev_t * dev, char * cmd);
208 +void ag_cind_status_response(blt_dev_t * dev, char * cmd);
209 +void ag_cind_response(blt_dev_t * dev, char * cmd);
210 +void ag_brsf_response(blt_dev_t * dev, char * cmd);
211 +void remove_sdp_records(void);
213 +void gui_easm_response(blt_dev_t * dev, char * cmd);
215 +int sock_err(int fd);
216 +int parse_clip(const char * str, char *number, int number_len, char * name, int name_len, int *type);
217 +int set_buffer(char * ring, char * data, int circular_len, int * pos, int data_len);
218 +int get_buffer(char * dst, char * ring, int ring_size, int * head, int to_copy);
219 +void gui_eaid_response(blt_dev_t * dev, char * cmd);
224 + unsigned char buf[BUFLEN];
226 +// XXX:T: Tidy this lot up.
229 + blt_status_t status; /* Device Status */
231 + struct ast_channel * owner; /* Channel we belong to, possibly NULL */
232 + blt_dev_t * dev; /* The bluetooth device channel is for */
233 + struct ast_frame fr; /* Recieved frame */
236 + int sco_pipe[2]; /* SCO alert pipe */
237 + int sco; /* SCO fd */
238 + int sco_handle; /* SCO Handle */
239 + int sco_mtu; /* SCO MTU */
240 + int sco_running; /* 1 when sCO thread should be running */
241 + pthread_t sco_thread; /* SCO thread */
242 + ast_mutex_t sco_lock; /* SCO lock */
243 + int sco_pos_in; /* Reader in position (drain)*/
244 + int sco_pos_inrcv; /* Reader in position (fill) */
245 + int wakeread; /* blt_read() needs to be woken */
246 + int sco_pos_out; /* Reader out position */
247 + int sco_sending; /* Sending SCO packets */
248 + char buf[1200]; /* Incoming data buffer */
250 + char sco_buf_out[BUFLEN]; /* 24 chunks of 48 */
251 + char sco_buf_in[BUFLEN]; /* 24 chunks of 48 */
253 + char dnid[1024]; /* Outgoi gncall dialed number */
254 + unsigned char * obuf[BLT_OBUF_LEN]; /* Outgoing data buffer */
255 + int obuf_len; /* Output Buffer Position */
256 + int obuf_wpos; /* Buffer Reader */
259 + int autoconnect; /* 1 for autoconnect */
260 + int outgoing_id; /* Outgoing connection scheduler id */
261 + char * name; /* Devices friendly name */
262 + blt_role_t role; /* Device role (HS or AG) */
263 + bdaddr_t bdaddr; /* remote address */
264 + int channel; /* remote channel */
265 + int rd; /* RFCOMM fd */
266 + int tmp_rd; /* RFCOMM fd */
267 + int call_cnt; /* Number of attempted calls */
268 + ast_mutex_t lock; /* RFCOMM socket lock */
269 + char rd_buff[BLT_RDBUFF_MAX]; /* RFCOMM input buffer */
270 + int rd_buff_pos; /* RFCOMM input buffer position */
271 + int ready; /* 1 When ready */
275 + char last_ok_cmd[BLT_RDBUFF_MAX]; /* Runtime[AG]: Last AT command that was OK */
276 + int cind; /* Runtime[AG]: Recieved +CIND */
277 + int call_pos, service_pos, callsetup_pos; /* Runtime[AG]: Positions in CIND/CMER */
278 + int call, service, callsetup; /* Runtime[AG]: Values */
279 + char cid_num[AST_MAX_EXTENSION];
280 + char cid_name[AST_MAX_EXTENSION];
283 + blt_state_t state; /* Runtime: Device state (AG mode only) */
284 + int ring_timer; /* Runtime:Ring Timer */
285 + char last_err_cmd[BLT_RDBUFF_MAX]; /* Runtime: Last AT command that was OK */
286 + void (*cb)(blt_dev_t * dev, char * str); /* Runtime: Callback when in HS mode */
288 + int brsf; /* Runtime: Bluetooth Retrieve Supported Features */
289 + int bvra; /* Runtime: Bluetooth Voice Recognised Activation */
290 + int gain_speaker; /* Runtime: Gain Of Speaker */
291 + int clip; /* Runtime: Supports CLID */
292 + int colp; /* Runtime: Connected Line ID */
293 + int elip; /* Runtime: (Ericsson) Supports CLID */
294 + int eolp; /* Runtime: (Ericsson) Connected Line ID */
295 + int ringing; /* Runtime: Device is ringing */
297 + blt_dev_t * next; /* Next in linked list */
301 +typedef struct blt_atcb {
306 + /* DTE callbacks: */
307 + int (*set)(blt_dev_t * dev, const char * arg, int len);
308 + int (*read)(blt_dev_t * dev);
309 + int (*execute)(blt_dev_t * dev, const char * data);
310 + int (*test)(blt_dev_t * dev);
312 + /* DCE callbacks: */
313 + int (*unsolicited)(blt_dev_t * dev, const char * value);
317 +/* ---------------------------------- */
319 +static void rd_close(blt_dev_t * dev, int reconnect, int err);
320 +static int send_atcmd(blt_dev_t * device, const char * fmt, ...);
321 +static int sco_connect(blt_dev_t * dev);
322 +static int sco_start(blt_dev_t * dev, int fd);
324 +/* ---------------------------------- */
326 +/* RFCOMM channel we listen on*/
327 +static int rfcomm_channel_ag = BLT_DEFAULT_CHANNEL_AG;
328 +static int rfcomm_channel_hs = BLT_DEFAULT_CHANNEL_HS;
329 +static int rfcomm_channel_gui = BLT_DEFAULT_CHANNEL_GUI;
331 +static char* gui_default_sip_number = "";
332 +static char* gui_default_sip_address = "";
334 +/* Address of local bluetooth interface */
335 +static int hcidev_id;
336 +static bdaddr_t local_bdaddr;
338 +/* All the current sockets */
339 +AST_MUTEX_DEFINE_STATIC(iface_lock);
340 +static blt_dev_t * iface_head;
341 +static int ifcount = 0;
343 +static int sdp_record_hs = -1;
344 +static int sdp_record_ag = -1;
345 +static int sdp_record_gui = -1;
347 +/* RFCOMM listen socket */
348 +static int rfcomm_sock_ag = -1;
349 +static int rfcomm_sock_hs = -1;
350 +static int rfcomm_sock_gui = -1;
352 +static int sco_socket = -1;
354 +static int monitor_pid = -1;
356 +/* The socket monitoring thread */
357 +static pthread_t monitor_thread = AST_PTHREADT_NULL;
358 +AST_MUTEX_DEFINE_STATIC(monitor_lock);
360 +/* Count how many times this module is currently in use */
361 +static int usecnt = 0;
362 +AST_MUTEX_DEFINE_STATIC(usecnt_lock);
364 +static struct sched_context * sched = NULL;
366 +/* ---------------------------------- */
368 +#if ASTERISK_VERSION_NUM <= 010107
369 +#include <asterisk/channel_pvt.h>
370 +#define tech_pvt pvt->pvt
371 +#else /* CVS. FIXME: Version number */
372 +static struct ast_channel *blt_request(const char *type, int format, void *data, int *cause);
373 +static int blt_hangup(struct ast_channel *c);
374 +static int blt_answer(struct ast_channel *c);
375 +static struct ast_frame *blt_read(struct ast_channel *chan);
376 +static int blt_call(struct ast_channel *c, char *dest, int timeout);
377 +static int blt_write(struct ast_channel *chan, struct ast_frame *f);
378 +static int blt_indicate(struct ast_channel *chan, int cond);
380 +static const struct ast_channel_tech blt_tech = {
381 + .type = BLT_CHAN_NAME,
382 + .description = "Bluetooth Channel Driver",
383 + .capabilities = BLUETOOTH_FORMAT,
384 + .requester = blt_request,
385 + .hangup = blt_hangup,
386 + .answer = blt_answer,
389 + .write = blt_write,
390 + .indicate = blt_indicate,
393 +/* ---------------------------------- */
396 +role2str(blt_role_t role)
405 + case BLT_ROLE_NONE:
412 +status2str(blt_status_t status)
415 + case BLT_STATUS_DOWN:
417 + case BLT_STATUS_CONNECTING:
418 + return "Connecting";
419 + case BLT_STATUS_NEGOTIATING:
420 + return "Negotiating";
421 + case BLT_STATUS_READY:
423 + case BLT_STATUS_RINGING:
425 + case BLT_STATUS_IN_CALL:
431 +int sock_err(int fd)
434 + int len = sizeof(ret);
435 + getsockopt(fd, SOL_SOCKET, SO_ERROR, &ret, &len);
439 +/* ---------------------------------- */
440 +int parse_clip(const char * str, char *number, int number_len, char * name, int name_len, int *type)
442 + const char *c = str;
447 + memset(number, 0, number_len);
448 + memset(name, 0, name_len);
453 + while(*c && *c != '"')
457 + while(*c && *c != '"')
459 + length = c - start < number_len ? c - start : number_len;
460 + strncpy(number, start, length);
461 + number[length] = '\0';
463 + while(*c && *c != ',')
467 + while(*c && *c != ',')
469 + length = c - start < number_len ? c - start : number_len;
470 + strncpy(typestr, start, length);
471 + typestr[length] = '\0';
472 + *type = atoi(typestr);
474 + while(*c && *c != ',')
477 + while(*c && *c != ',')
480 + while(*c && *c != '"')
484 + while(*c && *c != '"')
486 + length = c - start < number_len ? c - start : number_len;
487 + strncpy(name, start, length);
488 + name[length] = '\0';
495 +parse_cind(const char * str, char * name, int name_len)
499 + memset(name, 0, name_len);
503 + if (++c == 1 && *(str+1) == '"') {
504 + const char * start = str + 2;
507 + while (*str && *str != '"') {
513 + strncpy(name, start, (len > name_len) ? name_len : len);
515 + } else if (*str == ')')
517 + else if (c == 0 && *str == ',')
525 +set_cind(blt_dev_t * dev, int indicator, int val)
528 + ast_log(LOG_DEBUG, "CIND %d set to %d\n", indicator, val);
530 + if (indicator == dev->callsetup_pos) {
534 + dev->callsetup = val;
538 + // Outgoing ringing
539 + if ((dev->owner && dev->role == BLT_ROLE_AG) ||
540 + (dev->owner && dev->role == BLT_ROLE_GUI))
541 + ast_queue_control(dev->owner, AST_CONTROL_RINGING);
548 + if ((dev->owner && dev->role == BLT_ROLE_AG && dev->call == 0) ||
549 + (dev->owner && dev->role == BLT_ROLE_AG && dev->call == 0))
550 + ast_queue_control(dev->owner, AST_CONTROL_CONGESTION);
554 + } else if (indicator == dev->service_pos) {
559 + ast_log(LOG_NOTICE, "Audio Gateway %s lost signal\n", dev->name);
560 + else if (dev->service == 0 && val > 0)
561 + ast_log(LOG_NOTICE, "Audio Gateway %s got signal\n", dev->name);
563 + dev->service = val;
565 + } else if (indicator == dev->call_pos) {
573 + sco_start(dev, -1);
574 + ast_queue_control(dev->owner, AST_CONTROL_ANSWER);
575 + } else if (val == 0)
576 + ast_queue_control(dev->owner, AST_CONTROL_HANGUP);
584 +/* ---------------------------------- */
587 +set_buffer(char * ring, char * data, int circular_len, int * pos, int data_len)
589 + int start_pos = *(pos);
594 + // Set can_do to the most we can do in this copy.
596 + copy = MIN(circular_len - start_pos, data_len);
597 + memcpy(ring + start_pos, data + done, copy);
602 + if (start_pos == circular_len) {
606 + *(pos) = start_pos;
611 +get_buffer(char * dst, char * ring, int ring_size, int * head, int to_copy)
615 + // |1|2|3|4|5|6|7|8|9|
620 + // Set can_do to the most we can do in this copy.
621 + copy = MIN(ring_size - *head, to_copy);
623 + // ast_log(LOG_DEBUG, "Getting: %d bytes, From pos %d\n", copy, *head);
624 +#if __BYTE_ORDER == __LITTLE_ENDIAN
625 + memcpy(dst, ring + *head, copy);
627 + // memcpy(dst, ring + *head, copy);
628 + ast_swapcopy_samples(dst, ring+*head, copy/2);
630 + memset(ring+*head, 0, copy);
635 + if (*head == ring_size ) {
644 +/* Handle SCO audio sync.
646 + * If we are the MASTER, then we control the timing,
647 + * in 48 byte chunks. If we're the SLAVE, we send
648 + * as and when we recieve a packet.
650 + * Because of packet/timing nessecity, we
651 + * start up a thread when we're passing audio, so
652 + * that things are timed exactly right.
654 + * sco_thread() is the function that handles it.
659 +sco_thread(void * data)
661 + blt_dev_t * dev = (blt_dev_t*)data;
663 + struct pollfd pfd[2];
671 + // Avoid deadlock in odd circumstances
673 + ast_log(LOG_WARNING, "SCO thread started on fd %d, pid %d\n", dev->sco, getpid());
675 + if (fcntl(dev->sco_pipe[1], F_SETFL, O_RDWR|O_NONBLOCK)) {
676 + ast_log(LOG_WARNING, "fcntl failed on sco_pipe\n");
679 + // dev->status = BLT_STATUS_IN_CALL;
680 + // ast_queue_control(dev->owner, AST_CONTROL_ANSWER);
681 + // Set buffer to silence, just incase.
683 + ast_mutex_lock(&(dev->sco_lock));
685 + memset(dev->sco_buf_in, 0, BUFLEN);
686 + memset(dev->sco_buf_out, 0, BUFLEN);
688 + dev->sco_pos_in = 0;
689 + dev->sco_pos_out = 0;
690 + dev->sco_pos_inrcv = 0;
693 + ast_mutex_unlock(&(dev->sco_lock));
697 + ast_mutex_lock(&(dev->sco_lock));
699 + if (dev->sco_running != 1) {
700 + ast_log(LOG_DEBUG, "SCO stopped.\n");
704 + pfd[0].fd = dev->sco;
705 + pfd[0].events = POLLIN;
707 + pfd[1].fd = dev->sco_pipe[1];
708 + pfd[1].events = POLLIN;
710 + ast_mutex_unlock(&(dev->sco_lock));
712 + res = poll(pfd, 2, 50);
714 + if (res == -1 && errno != EINTR) {
715 + ast_log(LOG_DEBUG, "SCO poll() error\n");
723 + if (pfd[0].revents & POLLIN) {
725 + len = read(dev->sco, buf, 48);
728 + ast_mutex_lock(&(dev->lock));
730 + if (dev->owner && dev->owner->_state == AST_STATE_UP) {
731 + ast_mutex_lock(&(dev->sco_lock));
732 + set_buffer(dev->sco_buf_in, buf, BUFLEN, &in_pos, len);
733 + dev->sco_pos_inrcv = in_pos;
735 + get_buffer(buf, dev->sco_buf_out, BUFLEN, &out_pos, len);
736 + if (write(dev->sco, buf, len) != len)
737 + ast_log(LOG_WARNING, "Wrote <48 to sco\n");
739 + if (dev->wakeread) {
740 + /* blt_read has caught up. Kick it */
742 + if(write(dev->sco_pipe[1], &c, 1) != 1)
743 + ast_log(LOG_WARNING, "write to kick sco_pipe failed\n");
745 + ast_mutex_unlock(&(dev->sco_lock));
747 + ast_mutex_unlock(&(dev->lock));
750 + } else if (pfd[0].revents) {
752 + int e = sock_err(pfd[0].fd);
753 + ast_log(LOG_ERROR, "SCO connection error: %s (errno %d)\n", strerror(e), e);
756 + } else if (pfd[1].revents & POLLIN) {
760 + len = read(pfd[1].fd, &c, 1);
761 + sending = (sending) ? 0 : 1;
763 + ast_mutex_unlock(&(dev->sco_lock));
765 + } else if (pfd[1].revents) {
767 + int e = sock_err(pfd[1].fd);
768 + 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);
772 + ast_log(LOG_NOTICE, "Unhandled poll output\n");
773 + ast_mutex_unlock(&(dev->sco_lock));
778 + ast_mutex_lock(&(dev->lock));
781 + dev->sco_running = -1;
783 + memset(dev->sco_buf_in, 0, BUFLEN);
784 + memset(dev->sco_buf_out, 0, BUFLEN);
786 + dev->sco_pos_in = 0;
787 + dev->sco_pos_out = 0;
788 + dev->sco_pos_inrcv = 0;
790 + ast_mutex_unlock(&(dev->sco_lock));
792 + ast_queue_control(dev->owner, AST_CONTROL_HANGUP);
793 + ast_mutex_unlock(&(dev->lock));
794 + ast_log(LOG_DEBUG, "SCO thread stopped\n");
798 +/* Start SCO thread. Must be called with dev->lock */
801 +sco_start(blt_dev_t * dev, int fd)
804 + if (dev->sco_pipe[1] <= 0) {
805 + ast_log(LOG_ERROR, "SCO pipe[1] == %d\n", dev->sco_pipe[1]);
809 + ast_mutex_lock(&(dev->sco_lock));
811 + if (dev->sco_running != -1) {
812 + ast_log(LOG_ERROR, "Tried to start SCO thread while already running\n");
813 + ast_mutex_unlock(&(dev->sco_lock));
817 + if (dev->sco == -1) {
820 + } else if (sco_connect(dev) != 0) {
821 + ast_log(LOG_ERROR, "SCO fd invalid\n");
822 + ast_mutex_unlock(&(dev->sco_lock));
827 + dev->sco_running = 1;
829 + if (ast_pthread_create(&(dev->sco_thread), NULL, sco_thread, dev) < 0) {
830 + ast_log(LOG_ERROR, "Unable to start SCO thread.\n");
831 + dev->sco_running = -1;
832 + ast_mutex_unlock(&(dev->sco_lock));
836 + ast_mutex_unlock(&(dev->sco_lock));
841 +/* Stop SCO thread. Must be called with dev->lock */
844 +sco_stop(blt_dev_t * dev)
846 + ast_mutex_lock(&(dev->sco_lock));
847 + if (dev->sco_running == 1)
848 + dev->sco_running = 0;
850 + dev->sco_running = -1;
851 + dev->sco_sending = 0;
852 + ast_mutex_unlock(&(dev->sco_lock));
856 +/* ---------------------------------- */
858 +/* Answer the call. Call with lock held on device */
861 +answer(blt_dev_t * dev)
864 + if ( (!dev->owner) || (dev->ready != 1) || (dev->status != BLT_STATUS_READY && dev->status != BLT_STATUS_RINGING)) {
865 + ast_log(LOG_ERROR, "Attempt to answer() in invalid state (owner=%p, ready=%d, status=%s)\n",
866 + dev->owner, dev->ready, status2str(dev->status));
870 + // dev->sd = sco_connect(&local_bdaddr, &(dev->bdaddr), NULL, NULL, 0);
871 + // dev->status = BLT_STATUS_IN_CALL;
872 + // dev->owner->fds[0] = dev->sd;
873 + // if we are answering (hitting button):
874 + ast_queue_control(dev->owner, AST_CONTROL_ANSWER);
875 + // if asterisk signals us to answer:
876 + // ast_setstate(ast, AST_STATE_UP);
878 + /* Start SCO link */
879 + sco_start(dev, -1);
883 +/* ---------------------------------- */
886 +blt_write(struct ast_channel * ast, struct ast_frame * frame)
888 + blt_dev_t * dev = ast->tech_pvt;
890 + /* Write a frame of (presumably voice) data */
892 + if (frame->frametype != AST_FRAME_VOICE) {
893 + ast_log(LOG_WARNING, "Don't know what to do with frame type '%d'\n", frame->frametype);
897 + if (!(frame->subclass & BLUETOOTH_FORMAT)) {
898 + static int fish = 5;
900 + ast_log(LOG_WARNING, "Cannot handle frames in format %d\n", frame->subclass);
906 + if (ast->_state != AST_STATE_UP) {
910 + ast_mutex_lock(&(dev->sco_lock));
911 + set_buffer(dev->sco_buf_out, frame->data, BUFLEN, &(dev->sco_pos_out), MIN(frame->datalen, BUFLEN));
912 + ast_mutex_unlock(&(dev->sco_lock));
918 +static struct ast_frame *
919 +blt_read(struct ast_channel * ast)
921 + blt_dev_t * dev = ast->tech_pvt;
924 + static int fish = 0;
925 + /* Some nice norms */
927 + dev->fr.datalen = 0;
928 + dev->fr.samples = 0;
929 + dev->fr.data = NULL;
930 + dev->fr.src = BLT_CHAN_NAME;
931 + dev->fr.offset = 0;
932 + dev->fr.mallocd = AST_MALLOCD_DATA;
933 + dev->fr.delivery.tv_sec = 0;
934 + dev->fr.delivery.tv_usec = 0;
935 + read(dev->sco_pipe[0], &c, 1);
936 + ast_mutex_lock(&(dev->sco_lock));
937 + dev->sco_sending = 1;
939 + if (dev->sco_pos_inrcv < dev->sco_pos_in) {
940 + /* Buffer wrapped. Read only till the end */
941 + len = BUFLEN - dev->sco_pos_in + dev->sco_pos_inrcv;
943 + len = dev->sco_pos_inrcv - dev->sco_pos_in;
945 + dev->fr.data = malloc(AST_FRIENDLY_OFFSET+len) + AST_FRIENDLY_OFFSET;
947 + get_buffer(dev->fr.data, dev->sco_buf_in, BUFLEN, &(dev->sco_pos_in), len);
949 + ast_mutex_unlock(&(dev->sco_lock));
951 + unsigned char *x = dev->fr.data;
952 + ast_log(LOG_WARNING, "blt_read %d: %02x %02x %02x %02x %02x %02x\n",
953 + dev->fr.datalen, x[0], x[1], x[2], x[3], x[4], x[5]);
957 + dev->fr.samples = len / 2;
958 + dev->fr.datalen = len;
959 + dev->fr.frametype = AST_FRAME_VOICE;
960 + dev->fr.subclass = BLUETOOTH_FORMAT;
961 + dev->fr.offset = AST_FRIENDLY_OFFSET;
965 +/* Escape Any '"' in str. Return malloc()ed string */
967 +escape_str(char * str)
981 + ret = malloc(len + 1);
982 + pret = memset(ret, 0, len + 1);
996 +ring_hs(blt_dev_t * dev)
998 +#if (ASTERISK_VERSION_NUM < 010100)
999 + char tmp[AST_MAX_EXTENSION];
1003 + ast_mutex_lock(&(dev->lock));
1005 + if (dev->owner == NULL) {
1006 + ast_mutex_unlock(&(dev->lock));
1011 + dev->status = BLT_STATUS_RINGING;
1013 + send_atcmd(dev, "RING");
1015 + dev->owner->rings++;
1017 + // XXX:T: '"' needs to be escaped in ELIP.
1019 +#if (ASTERISK_VERSION_NUM < 010100)
1021 + if (dev->owner->callerid) {
1023 + memset(tmp, 0, sizeof(tmp));
1024 + strncpy(tmp, dev->owner->callerid, sizeof(tmp)-1);
1026 + if (!ast_callerid_parse(tmp, &name, &num)) {
1028 + if (dev->clip && num)
1029 + send_atcmd(dev, "+CLIP: \"%s\",129", num);
1031 + if (dev->elip && name) {
1032 + char * esc = escape_str(name);
1033 + send_atcmd(dev, "*ELIP: \"%s\"", esc);
1042 + if (dev->clip && dev->owner->cid.cid_num)
1043 + send_atcmd(dev, "+CLIP: \"%s\",129", dev->owner->cid.cid_num);
1045 + if (dev->elip && dev->owner->cid.cid_name) {
1046 + char * esc = escape_str(dev->owner->cid.cid_name);
1047 + send_atcmd(dev, "*ELIP: \"%s\"", esc);
1053 + ast_mutex_unlock(&(dev->lock));
1059 + * If the HS is already connected, then just send RING, otherwise, things get a
1060 + * little more sticky. We first have to find the channel for HS using SDP,
1061 + * then initiate the connection. Once we've done that, we can start the call.
1065 +blt_call(struct ast_channel * ast, char * dest, int timeout)
1067 + blt_dev_t * dev = ast->tech_pvt;
1069 + if ((ast->_state != AST_STATE_DOWN) && (ast->_state != AST_STATE_RESERVED)) {
1070 + ast_log(LOG_WARNING, "blt_call called on %s, neither down nor reserved\n", ast->name);
1074 + ast_log(LOG_DEBUG, "Calling %s on %s [t: %d]\n", dest, ast->name, timeout);
1076 + if (ast_mutex_lock(&iface_lock)) {
1077 + ast_log(LOG_ERROR, "Failed to get iface_lock.\n");
1081 +// ast_mutex_lock(&(dev->lock));
1083 + if (dev->ready == 0) {
1084 + ast_log(LOG_WARNING, "Tried to call a device not ready/connected.\n");
1085 + ast_setstate(ast, AST_CONTROL_CONGESTION);
1086 +// ast_mutex_unlock(&(dev->lock));
1087 + ast_mutex_unlock(&iface_lock);
1091 + if (dev->role == BLT_ROLE_HS) {
1093 + send_atcmd(dev, "+CIEV: 3,1");
1095 + dev->ring_timer = ast_sched_add(sched, 5000, AST_SCHED_CB(ring_hs), dev);
1099 + ast_setstate(ast, AST_STATE_RINGING);
1100 + ast_queue_control(ast, AST_CONTROL_RINGING);
1102 + } else if (dev->role == BLT_ROLE_AG) {
1104 + send_atcmd(dev, "ATD%s;", dev->dnid);
1105 +// it does not seem like we should start the audio until the call is connected
1106 +// sco_start(dev, -1);
1107 + } else if (dev->role == BLT_ROLE_GUI) {
1109 + send_atcmd(dev, "ATD%s;", dev->dnid);
1113 + ast_setstate(ast, AST_CONTROL_CONGESTION);
1114 + ast_log(LOG_ERROR, "Unknown device role\n");
1118 +// ast_mutex_unlock(&(dev->lock));
1119 + ast_mutex_unlock(&iface_lock);
1125 +blt_hangup(struct ast_channel * ast)
1127 + blt_dev_t * dev = ast->tech_pvt;
1129 + ast_log(LOG_DEBUG, "blt_hangup(%s)\n", ast->name);
1131 + if (!ast->tech_pvt) {
1132 + ast_log(LOG_WARNING, "Asked to hangup channel not connected\n");
1136 + if (ast_mutex_lock(&iface_lock)) {
1137 + ast_log(LOG_ERROR, "Failed to get iface_lock\n");
1141 + ast_mutex_lock(&(dev->lock));
1144 + dev->sco_sending = 0;
1146 + if (dev->role == BLT_ROLE_HS) {
1148 + if (dev->ringing == 0) {
1149 + // Actual call in progress
1150 + send_atcmd(dev, "+CIEV: 2,0");
1153 + // Just ringing still
1155 + if (dev->role == BLT_ROLE_HS)
1156 + send_atcmd(dev, "+CIEV: 3,0");
1158 + if (dev->ring_timer >= 0)
1159 + ast_sched_del(sched, dev->ring_timer);
1161 + dev->ring_timer = -1;
1166 + } else if (dev->role == BLT_ROLE_AG) {
1169 + send_atcmd(dev, "ATH");
1170 + send_atcmd(dev, "AT+CHUP");
1174 + if (dev->status == BLT_STATUS_IN_CALL || dev->status == BLT_STATUS_RINGING)
1175 + dev->status = BLT_STATUS_READY;
1177 + ast->tech_pvt = NULL;
1178 + dev->owner = NULL;
1179 + ast_mutex_unlock(&(dev->lock));
1180 + ast_setstate(ast, AST_STATE_DOWN);
1181 + ast_mutex_unlock(&(iface_lock));
1187 +blt_indicate(struct ast_channel * c, int condition)
1189 + ast_log(LOG_DEBUG, "blt_indicate (%d)\n", condition);
1191 + switch(condition) {
1192 + case AST_CONTROL_RINGING:
1195 + ast_log(LOG_WARNING, "Don't know how to condition %d\n", condition);
1202 +blt_answer(struct ast_channel * ast)
1204 + blt_dev_t * dev = ast->tech_pvt;
1206 + ast_mutex_lock(&dev->lock);
1208 + // if (dev->ring_timer >= 0)
1209 + // ast_sched_del(sched, dev->ring_timer);
1210 + // dev->ring_timer = -1;
1212 + ast_log(LOG_DEBUG, "Answering interface\n");
1214 + if (ast->_state != AST_STATE_UP) {
1215 + send_atcmd(dev, "+CIEV: 2,1");
1216 + send_atcmd(dev, "+CIEV: 3,0");
1217 + sco_start(dev, -1);
1218 + ast_setstate(ast, AST_STATE_UP);
1221 + ast_mutex_unlock(&dev->lock);
1226 +static struct ast_channel *
1227 +blt_new(blt_dev_t * dev, int state, const char * context, const char * number)
1229 + struct ast_channel * ast;
1232 + if ((ast = ast_channel_alloc(1)) == NULL) {
1233 + ast_log(LOG_WARNING, "Unable to allocate channel structure\n");
1237 + snprintf(ast->name, sizeof(ast->name), "BLT/%s", dev->name);
1239 + // ast->fds[0] = socket(PF_BLUETOOTH, SOCK_SEQPACKET, BTPROTO_SCO);
1241 + ast->nativeformats = BLUETOOTH_FORMAT;
1242 + //ast->rawreadformat = BLUETOOTH_FORMAT;
1243 + //ast->rawwriteformat = BLUETOOTH_FORMAT;
1244 + ast->writeformat = BLUETOOTH_FORMAT;
1245 + ast->readformat = BLUETOOTH_FORMAT;
1247 + ast_setstate(ast, state);
1249 + ast->type = BLT_CHAN_NAME;
1251 + ast->tech_pvt = dev;
1252 +#if ASTERISK_VERSION_NUM > 010107
1253 + ast->tech = &blt_tech;
1255 + ast->pvt->call = blt_call;
1256 + ast->pvt->indicate = blt_indicate;
1257 + ast->pvt->hangup = blt_hangup;
1258 + ast->pvt->read = blt_read;
1259 + ast->pvt->write = blt_write;
1260 + ast->pvt->answer = blt_answer;
1262 + strncpy(ast->context, context, sizeof(ast->context)-1);
1263 + strncpy(ast->exten, number, sizeof(ast->exten) - 1);
1264 + if(0 == strcmp(number, "s"))
1266 + //ast_set_callerid(ast, dev->cid_num, dev->cid_name, dev->cid_num);
1269 + ast->language[0] = '\0';
1271 + ast->fds[0] = dev->sco_pipe[0];
1272 + write(dev->sco_pipe[1], &c, 1);
1276 + ast_mutex_lock(&usecnt_lock);
1278 + ast_mutex_unlock(&usecnt_lock);
1280 + ast_update_use_count();
1282 + if (state != AST_STATE_DOWN) {
1283 + if (ast_pbx_start(ast)) {
1284 + ast_log(LOG_WARNING, "Unable to start PBX on %s\n", ast->name);
1292 +static struct ast_channel *
1293 +#if (ASTERISK_VERSION_NUM < 010100)
1294 +blt_request(char * type, int format, void * local_data)
1295 +#elif (ASTERISK_VERSION_NUM <= 010107)
1296 +blt_request(const char * type, int format, void * local_data)
1298 +blt_request(const char * type, int format, void * local_data, int *cause)
1301 + char * data = (char*)local_data;
1303 + blt_dev_t * dev = NULL;
1304 + struct ast_channel * ast = NULL;
1305 + char * number = data, * dname;
1307 + dname = strsep(&number, "/");
1309 + oldformat = format;
1311 + format &= BLUETOOTH_FORMAT;
1314 + ast_log(LOG_NOTICE, "Asked to get a channel of unsupported format '%d'\n", oldformat);
1318 + ast_log(LOG_DEBUG, "Dialing '%s' via '%s'\n", number, dname);
1320 + if (ast_mutex_lock(&iface_lock)) {
1321 + ast_log(LOG_ERROR, "Unable to lock iface_list\n");
1328 + if (strcmp(dev->name, dname) == 0) {
1329 + ast_mutex_lock(&(dev->lock));
1330 + if (!dev->ready) {
1331 + ast_log(LOG_ERROR, "Device %s is not connected\n", dev->name);
1332 + ast_mutex_unlock(&(dev->lock));
1333 + ast_mutex_unlock(&iface_lock);
1341 + ast_mutex_unlock(&iface_lock);
1344 + ast_log(LOG_WARNING, "Failed to find device named '%s'\n", dname);
1348 + if (number && dev->role != BLT_ROLE_AG) {
1349 + ast_log(LOG_WARNING, "Tried to send a call out on non AG\n");
1350 + ast_mutex_unlock(&(dev->lock));
1354 + if (dev->role == BLT_ROLE_AG)
1355 + strncpy(dev->dnid, number, sizeof(dev->dnid) - 1);
1357 + ast = blt_new(dev, AST_STATE_DOWN, dev->context, "s");
1359 + ast_mutex_unlock(&(dev->lock));
1364 +/* ---------------------------------- */
1367 +/* ---- AT COMMAND SOCKET STUFF ---- */
1370 +send_atcmd(blt_dev_t * dev, const char * fmt, ...)
1376 + va_start(ap, fmt);
1377 + len = vsnprintf(buf, 1023, fmt, ap);
1380 + if (option_verbose)
1381 + ast_verbose(VERBOSE_PREFIX_1 "[%s] %*s < %s\n", role2str(dev->role), 10, dev->name, buf);
1383 + write(dev->rd, "\r\n", 2);
1384 + len = write(dev->rd, buf, len);
1385 + write(dev->rd, "\r\n", 2);
1386 + return (len) ? 0 : -1;
1391 +send_atcmd_ok(blt_dev_t * dev, const char * cmd)
1394 + strncpy(dev->last_ok_cmd, cmd, BLT_RDBUFF_MAX - 1);
1395 + if (option_verbose)
1396 + ast_verbose(VERBOSE_PREFIX_1 "[%s] %*s < OK\n", role2str(dev->role), 10, dev->name);
1397 + len = write(dev->rd, "\r\nOK\r\n", 6);
1398 + return (len) ? 0 : -1;
1402 +send_atcmd_error(blt_dev_t * dev)
1406 + if (option_verbose)
1407 + ast_verbose(VERBOSE_PREFIX_1 "[%s] %*s < ERROR\n", role2str(dev->role), 10, dev->name);
1409 +// write(dev->rd, "\r\n", 2);
1410 +// len = write(dev->rd, dev->last_ok_cmd, 5);
1411 + write(dev->rd, "\r\n", 2);
1412 + len = write(dev->rd, "ERROR", 5);
1413 + write(dev->rd, "\r\n", 2);
1415 + return (len) ? 0 : -1;
1419 +/* ---------------------------------- */
1421 +/* -- Handle negotiation when we're an AG -- */
1423 +/* Bluetooth Support */
1426 +atcmd_brsf_set(blt_dev_t * dev, const char * arg, int len)
1428 + ast_log(LOG_DEBUG, "Device Supports: %s\n", arg);
1429 + dev->brsf = atoi(arg);
1430 + send_atcmd(dev, "+BRSF: %d", 23);
1434 +/* Bluetooth Voice Recognition */
1437 +atcmd_bvra_set(blt_dev_t * dev, const char * arg, int len)
1439 + ast_log(LOG_WARNING, "+BVRA Not Yet Supported\n");
1442 + // XXX:T: Fix voice recognition somehow!
1443 + int action = atoi(arg);
1444 + ast_log(LOG_DEBUG, "Voice Recognition: %s\n", (a) ? "ACTIVATED" : "DEACTIVATED");
1445 + if ((action == 0) & (dev->bvra == 1)) {
1448 + // XXX:T: Shutdown any active bvra channel
1449 + ast_log(LOG_DEBUG, "Voice Recognition: DISABLED\n");
1450 + } else if ((action == 1) && (dev->bvra == 0)) {
1453 + // XXX:T: Schedule connection to voice recognition extension/application
1454 + ast_log(LOG_DEBUG, "Voice Recognition: ENABLED\n");
1456 + ast_log(LOG_ERROR, "+BVRA out of sync (we think %d, but HS wants %d)\n", dev->bvra, action);
1466 +atcmd_cclk_read(blt_dev_t * dev)
1469 + const time_t ti = time(0);
1470 + tp = localtime_r(&ti, &t);
1471 + send_atcmd(dev, "+CCLK: \"%02d/%02d/%02d,%02d:%02d:%02d+%02d\"",
1472 + (tp->tm_year % 100), (tp->tm_mon + 1), (tp->tm_mday),
1473 + tp->tm_hour, tp->tm_min, tp->tm_sec, ((tp->tm_gmtoff / 60) / 15));
1477 +/* CHUP - Hangup Call */
1480 +atcmd_chup_execute(blt_dev_t * dev, const char * data)
1482 + if (!dev->owner) {
1483 + ast_log(LOG_ERROR, "Request to hangup call when none in progress\n");
1486 + ast_log(LOG_DEBUG, "Hangup Call\n");
1487 + ast_queue_control(dev->owner, AST_CONTROL_HANGUP);
1491 +/* CIND - Call Indicator */
1494 +atcmd_cind_read(blt_dev_t * dev)
1496 + send_atcmd(dev, "+CIND: 1,0,0");
1501 +atcmd_cind_test(blt_dev_t * dev)
1503 + send_atcmd(dev, "+CIND: (\"service\",(0,1)),(\"call\",(0,1)),(\"callsetup\",(0-4))");
1510 +atcmd_clan_read(blt_dev_t * dev)
1512 + send_atcmd(dev, "+CLAN: \"en\"");
1516 +/* Caller Id Presentation */
1519 +atcmd_clip_set(blt_dev_t * dev, const char * arg, int len)
1521 + dev->clip = atoi(arg);
1525 +/* Connected Line Identification Presentation */
1528 +atcmd_colp_set(blt_dev_t * dev, const char * arg, int len)
1530 + dev->colp = atoi(arg);
1534 +/* CMER - Mobile Equipment Event Reporting */
1537 +atcmd_cmer_set(blt_dev_t * dev, const char * arg, int len)
1540 + dev->status = BLT_STATUS_READY;
1544 +/* PhoneBook Types:
1546 + * - FD - SIM Fixed Dialing Phone Book
1547 + * - ME - ME Phone book
1548 + * - SM - SIM Phone Book
1549 + * - DC - ME dialled-calls list
1550 + * - RC - ME recieved-calls lisr
1551 + * - MC - ME missed-calls list
1552 + * - MV - ME Voice Activated Dialing List
1553 + * - HP - Hierachial Phone Book
1554 + * - BC - Own Business Card (PIN2 required)
1558 +/* Read Phone Book Entry */
1561 +atcmd_cpbr_set(blt_dev_t * dev, const char * arg, int len)
1563 + // XXX:T: Fix the phone book!
1564 + // * Maybe add res_phonebook or something? */
1565 + send_atcmd(dev, "+CPBR: %d,\"%s\",128,\"%s\"", atoi(arg), arg, arg);
1569 +/* Select Phone Book */
1572 +atcmd_cpbs_set(blt_dev_t * dev, const char * arg, int len)
1574 + // XXX:T: I guess we'll just accept any?
1579 +atcmd_cscs_set(blt_dev_t * dev, const char * arg, int len)
1581 + // XXX:T: Language
1586 +atcmd_eips_set(blt_dev_t * dev, const char * arg, int len)
1588 + ast_log(LOG_DEBUG, "Identify Presentation Set: %s=%s\n",
1589 + (*(arg) == 49) ? "ELIP" : "EOLP",
1590 + (*(arg+2) == 49) ? "ON" : "OFF");
1593 + dev->eolp = (*(arg+2) == 49) ? 1 : 0;
1595 + dev->elip = (*(arg+2) == 49) ? 1 : 0;
1600 +/* VGS - Speaker Volume Gain */
1603 +atcmd_vgs_set(blt_dev_t * dev, const char * arg, int len)
1605 + dev->gain_speaker = atoi(arg);
1610 +gui_eaid_response(blt_dev_t * dev, char * cmd)
1612 + ast_log(LOG_NOTICE, "Submenu displayed.\n");
1616 +atcmd_eami_execute(blt_dev_t * dev, const char * data)
1618 + char * number = NULL;
1620 + number = strndup(data, strlen(data));
1621 + int menuitem = atoi(number);
1623 + ast_log(LOG_NOTICE, "Menu Item '%d'.\n", menuitem);
1625 + dev->cb = gui_eaid_response;
1627 + if (menuitem == 1) {
1628 + char command[1024] = "";
1629 + const char* c1 = "AT*EAID=8,1,\"Make a SIP call\",\"Number\",\"";
1630 + const char* c2 = "\"";
1632 + (void)strncat(command, c1, sizeof(command) - strlen(command) - 1);
1633 + (void)strncat(command, gui_default_sip_number, sizeof(command) - strlen(command) - 1);
1634 + (void)strncat(command, c2, sizeof(command) - strlen(command) - 1);
1636 + //strcat(command, "AT*EAID=8,1,\"Make a SIP call\",\"Number\",\"");
1637 + //strcat(command, gui_default_sip_number);
1638 + //strcat(command, "\"");
1639 + send_atcmd(dev, command);
1640 + } else if (menuitem == 2) {
1641 + char command[1024] = "";
1642 + const char* c1 = "AT*EAID=11,1,\"Make a SIP call\",\"SIP Address\",100,\"";
1643 + const char* c2 = "\"";
1645 + (void)strncat(command, c1, sizeof(command) - strlen(command) - 1);
1646 + (void)strncat(command, gui_default_sip_address, sizeof(command) - strlen(command) - 1);
1647 + (void)strncat(command, c2, sizeof(command) - strlen(command) - 1);
1649 + //strcat(command, "AT*EAID=11,1,\"Make a SIP call\",\"SIP Address\",100,\"");
1650 + //strcat(command, gui_default_sip_address);
1651 + //strcat(command, "\"");
1652 + send_atcmd(dev, command);
1653 + } else if (menuitem == 0) {
1654 + dev->cb = gui_easm_response;
1655 +// send_atcmd(dev,"AT*EASM=\"SIP Menu\",1,1,3,\"Call Number\",\"Call Address\",\"More Options\",1");
1656 + send_atcmd(dev,"AT*EASM=\"SIP Menu\",1,1,2,\"Call Number\",\"Call Address\",1");
1658 + ast_log(LOG_ERROR, "Menu item not implementented.\n");
1664 +atcmd_eaii_execute(blt_dev_t * dev, const char * data)
1666 + int pos = 1, len = 0;
1669 + const char * start = data;
1670 + struct sockaddr_in addr;
1673 + if (*data == ',') {
1674 + memset(type, 0, 128);
1675 + strncpy(type, start, len);
1677 + ast_log(LOG_NOTICE, "Number(8)/Address(11): '%s'.\n", type);
1689 + memset(val, 0, 128);
1690 + strncpy(val, start, len);
1694 + address = strtok(val, del);
1695 + int type_int = atoi(type);
1697 + if (strcmp(address, " 0") == 0) {
1698 + ast_log(LOG_NOTICE, "Spurious EAII:\n");
1699 + ast_log(LOG_NOTICE, data);
1703 + if (type_int == 8) {
1704 + (void)strncat(address, "@sipgate.de", sizeof(address) - strlen(address) - 1);
1707 + ast_log(LOG_NOTICE, "SIP number/address: '%i','%s'.\n", type_int, address);
1709 + if (type_int == 8 || type_int == 11) {
1711 + char messagebox[1024] = "";
1712 + const char* mb1 = "AT*EAID=1,1,\"Setting up SIP call to ";
1713 + const char* mb2 = "\",30";
1715 + (void)strncat(messagebox, mb1, sizeof(messagebox) - strlen(messagebox) - 1);
1716 + (void)strncat(messagebox, address, sizeof(messagebox) - strlen(messagebox) - 1);
1717 + (void)strncat(messagebox, mb2, sizeof(messagebox) - strlen(messagebox) - 1);
1719 + //strcat(messagebox, "AT*EAID=1,1,\"Setting up SIP call to ");
1720 + //strcat(messagebox, address);
1721 + //strcat(messagebox, "\",30");
1722 + send_atcmd(dev, messagebox);
1724 + send_atcmd(dev, "AT*ESKS=2");
1725 + send_atcmd(dev, "AT*EKSP");
1726 + send_atcmd(dev, "AT*ESKS=0");
1728 + //Create manager connection to create call
1729 + int s = socket(AF_INET,SOCK_STREAM,0);
1731 + ast_log(LOG_ERROR, "Manager connection failed.");
1733 + dev->cb = ag_cgmi_response;
1734 + send_atcmd(dev, "AT*EAID=1,1,\"Call failed\"");
1737 + addr.sin_family = AF_INET;
1738 + addr.sin_port = htons(5038);
1739 + addr.sin_addr.s_addr = inet_addr("127.0.0.1");
1740 + memset(&(addr.sin_zero), '\0', 8);
1742 + if (connect(s, (struct sockaddr *)&addr, sizeof(addr)) < 0) {
1743 + ast_log(LOG_ERROR, "Manager connection failed. (2)");
1744 + dev->cb = ag_cgmi_response;
1745 + send_atcmd(dev, "AT*EAID=1,1,\"Call failed\"");
1748 + char* command = "Action: login\r\nUsername: markus\r\nSecret: supAEr\r\n\r\n";
1749 + if (write(s,command,strlen(command)) < 0) {
1750 + ast_log(LOG_ERROR, "Manager connection failed. (3)");
1751 + dev->cb = ag_cgmi_response;
1752 + send_atcmd(dev, "AT*EAID=1,1,\"Call failed\"");
1756 + char command3[1024] = "";
1757 + const char* action = "Action: Originate\r\nChannel: SIP/";
1758 + const char* action2 = "\r\nExten: 1235\r\nPriority: 1\r\nContext: sipgate.de\r\n\r\nAction: logoff\r\n\r\n";
1760 + (void)strncat(command3, action, sizeof(command3) - strlen(command3) - 1);
1761 + (void)strncat(command3, address, sizeof(command3) - strlen(command3) - 1);
1762 + (void)strncat(command3, action2, sizeof(command3) - strlen(command3) - 1);
1764 + //strcat(command3, "Action: Originate\r\nChannel: SIP/");
1765 + //strcat(command3, address);
1766 + //strcat(command3, "\r\nExten: 1235\r\nPriority: 1\r\nContext: sipgate.de\r\n\r\n");
1767 + ast_log(LOG_NOTICE, command3);
1769 + if (write(s,command3,strlen(command3)) < 0) {
1770 + ast_log(LOG_ERROR, "Manager connection failed. (5)");
1774 + //dev->cb = ag_cgmi_response;
1780 +atcmd_dial_execute(blt_dev_t * dev, const char * data)
1782 + char * number = NULL;
1784 + /* Make sure there is a ';' at the end of the line */
1785 + if (*(data + (strlen(data) - 1)) != ';') {
1786 + ast_log(LOG_WARNING, "Can't dial non-voice right now: %s\n", data);
1790 + number = strndup(data, strlen(data) - 1);
1791 + ast_log(LOG_NOTICE, "Dial: [%s]\n", number);
1793 + send_atcmd(dev, "+CIEV: 2,1");
1794 + send_atcmd(dev, "+CIEV: 3,0");
1796 + sco_start(dev, -1);
1798 + if (blt_new(dev, AST_STATE_UP, dev->context, number) == NULL) {
1807 +static int atcmd_bldn_execute(blt_dev_t * dev, const char *data)
1809 + return atcmd_dial_execute(dev, "bldn;");
1815 +atcmd_answer_execute(blt_dev_t * dev, const char * data)
1818 + if (!dev->ringing || !dev->owner) {
1819 + ast_log(LOG_WARNING, "Can't answer non existant call\n");
1825 + if (dev->ring_timer >= 0)
1826 + ast_sched_del(sched, dev->ring_timer);
1828 + dev->ring_timer = -1;
1830 + send_atcmd(dev, "+CIEV: 2,1");
1831 + send_atcmd(dev, "+CIEV: 3,0");
1833 + return answer(dev);
1837 +ag_unsol_ciev(blt_dev_t * dev, const char * data)
1839 + const char * orig = data;
1843 + while (*(data) && *(data) == ' ')
1846 + if (*(data) == 0) {
1847 + ast_log(LOG_WARNING, "Invalid value[1] for '+CIEV:%s'\n", orig);
1851 + indicator = *(data++) - 48;
1853 + if (*(data++) != ',') {
1854 + ast_log(LOG_WARNING, "Invalid value[2] for '+CIEV:%s'\n", orig);
1858 + if (*(data) == 0) {
1859 + ast_log(LOG_WARNING, "Invalid value[3] for '+CIEV:%s'\n", orig);
1863 + status = *(data) - 48;
1865 + set_cind(dev, indicator, status);
1871 +ag_unsol_cind(blt_dev_t * dev, const char * data)
1874 + while (*(data) && *(data) == ' ')
1878 + if (dev->cind == 0)
1883 + while ((data = parse_cind(data, name, 1023)) != NULL) {
1884 + ast_log(LOG_DEBUG, "CIND: %d=%s\n", pos, name);
1885 + if (strcmp(name, "call") == 0)
1886 + dev->call_pos = pos;
1887 + else if (strcmp(name, "service") == 0)
1888 + dev->service_pos = pos;
1889 + else if (strcmp(name, "call_setup") == 0 || strcmp(name, "callsetup") == 0)
1890 + dev->callsetup_pos = pos;
1894 + ast_log(LOG_DEBUG, "CIND: %d=%s\n", pos, name);
1898 + int pos = 1, len = 0;
1900 + const char * start = data;
1903 + if (*data == ',') {
1904 + memset(val, 0, 128);
1905 + strncpy(val, start, len);
1906 + set_cind(dev, pos, atoi(val));
1917 + memset(val, 0, 128);
1918 + strncpy(val, start, len);
1919 + ast_log(LOG_DEBUG, "CIND IND %d set to %d [%s]\n", pos, atoi(val), val);
1928 + * handle an incoming call
1931 +ag_unsol_clip(blt_dev_t * dev, const char * data)
1933 + const char * orig = data;
1938 + while (*(data) && *(data) == ' ')
1941 + if (*(data) == 0) {
1942 + ast_log(LOG_WARNING, "Invalid value[1] for '+CLIP:%s'\n", orig);
1946 + parse_clip(data, number, sizeof(number)-1, name, sizeof(name)-1, &type);
1947 + ast_log(LOG_NOTICE, "Parsed '+CLIP: %s' number='%s' type='%d' name='%s'\n", data, number, type, name);
1949 + blt_new(dev, AST_STATE_RING, dev->context, "s");
1959 + { "A", NULL, NULL, atcmd_answer_execute, NULL, NULL },
1960 + { "D", NULL, NULL, atcmd_dial_execute, NULL, NULL },
1961 + { "+BRSF", atcmd_brsf_set, NULL, NULL, NULL, NULL },
1962 + { "+BVRA", atcmd_bvra_set, NULL, NULL, NULL, NULL },
1963 + { "+CCLK", NULL, atcmd_cclk_read, NULL, NULL, NULL },
1964 + { "+CHUP", NULL, NULL, atcmd_chup_execute, NULL, NULL },
1965 + { "+CIEV", NULL, NULL, NULL, NULL, ag_unsol_ciev },
1966 + { "+CIND", NULL, atcmd_cind_read, NULL, atcmd_cind_test, ag_unsol_cind },
1967 + { "*EAMI", NULL, NULL, atcmd_eami_execute, NULL, NULL},
1968 + { "*EAII", NULL, NULL, atcmd_eaii_execute, NULL, NULL},
1970 + { "+CLAN", NULL, atcmd_clan_read, NULL, NULL, NULL },
1971 + { "+CLIP", atcmd_clip_set, NULL, NULL, NULL, ag_unsol_clip },
1972 + { "+COLP", atcmd_colp_set, NULL, NULL, NULL, NULL },
1973 + { "+CMER", atcmd_cmer_set, NULL, NULL, NULL, NULL },
1974 + { "+CPBR", atcmd_cpbr_set, NULL, NULL, NULL, NULL },
1975 + { "+CPBS", atcmd_cpbs_set, NULL, NULL, NULL, NULL },
1976 + { "+CSCS", atcmd_cscs_set, NULL, NULL, NULL, NULL },
1977 + { "*EIPS", atcmd_eips_set, NULL, NULL, NULL, NULL },
1978 + { "+VGS", atcmd_vgs_set, NULL, NULL, NULL, NULL },
1979 + { "+BLDN", NULL, NULL, atcmd_bldn_execute, NULL, NULL },
1982 +#define ATCMD_LIST_LEN (sizeof(atcmd_list) / sizeof(blt_atcb_t))
1984 +/* ---------------------------------- */
1986 +/* -- Handle negotiation when we're a HS -- */
1989 +ag_unknown_response(blt_dev_t * dev, char * cmd)
1991 + ast_log(LOG_DEBUG, "Got UNKN response: %s\n", cmd);
1999 +gui_easm_response(blt_dev_t * dev, char * cmd)
2001 + ast_log(LOG_NOTICE, "Menu displayed.\n");
2005 +ag_cgmi_response(blt_dev_t * dev, char * cmd)
2007 + // CGMM - Phone Model
2008 + // CGMR - Phone Revision
2011 + // VTS - send tone
2015 + // CSMS - SMS STUFFS
2019 + // CSCA - sms CENTER NUMBER
2020 + // CNMI - SMS INDICATION
2021 + // ast_log(LOG_DEBUG, "Manufacturer: %s\n", cmd);
2023 + if (dev->role == BLT_ROLE_GUI) {
2024 + ast_log(LOG_NOTICE, "Displaying Menu.\n");
2025 + dev->cb = gui_easm_response;
2026 +// send_atcmd(dev,"AT*EASM=\"SIP Menu\",1,1,3,\"Call Number\",\"Call Address\",\"More Options\",1");
2027 + send_atcmd(dev,"AT*EASM=\"SIP Menu\",1,1,2,\"Call Number\",\"Call Address\",1");
2029 + dev->cb = ag_unknown_response;
2034 +ag_cgmi_valid_response(blt_dev_t * dev, char * cmd)
2036 + // send_atcmd(dev, "AT+WS46?");
2037 + // send_atcmd(dev, "AT+CRC=1");
2038 + // send_atcmd(dev, "AT+CNUM");
2040 + if (strcmp(cmd, "OK") == 0) {
2041 + send_atcmd(dev, "AT+CGMI");
2042 + dev->cb = ag_cgmi_response;
2044 + dev->cb = ag_unknown_response;
2049 +ag_clip_response(blt_dev_t * dev, char * cmd)
2051 + send_atcmd(dev, "AT+CGMI=?");
2052 + dev->cb = ag_cgmi_valid_response;
2056 +ag_cmer_response(blt_dev_t * dev, char * cmd)
2058 + dev->cb = ag_clip_response;
2060 + dev->status = BLT_STATUS_READY;
2061 + send_atcmd(dev, "AT+CLIP=1");
2065 +ag_cind_status_response(blt_dev_t * dev, char * cmd)
2067 + // XXX:T: Handle response.
2068 + dev->cb = ag_cmer_response;
2069 + send_atcmd(dev, "AT+CMER=3,0,0,1");
2070 + // Initiliase SCO link!
2074 +ag_cind_response(blt_dev_t * dev, char * cmd)
2076 + dev->cb = ag_cind_status_response;
2078 + send_atcmd(dev, "AT+CIND?");
2082 +ag_brsf_response(blt_dev_t * dev, char * cmd)
2084 + dev->cb = ag_cind_response;
2085 + ast_log(LOG_DEBUG, "Bluetooth features: %s\n", cmd);
2087 + send_atcmd(dev, "AT+CIND=?");
2090 +/* ---------------------------------- */
2093 +sdp_register(sdp_session_t * session)
2095 + // XXX:T: Fix this horrible function so it makes some sense and is extensible!
2096 + sdp_list_t *svclass_id, *pfseq, *apseq, *root;
2097 + uuid_t root_uuid, svclass_uuid, ga_svclass_uuid, l2cap_uuid, rfcomm_uuid;
2098 + sdp_profile_desc_t profile;
2099 + sdp_list_t *aproto, *proto[2];
2100 + sdp_record_t record;
2101 + uint8_t u8 = rfcomm_channel_ag;
2102 + uint8_t u8_hs = rfcomm_channel_hs;
2103 + sdp_data_t *channel;
2106 + memset((void *)&record, 0, sizeof(sdp_record_t));
2107 + record.handle = 0xffffffff;
2108 + sdp_uuid16_create(&root_uuid, PUBLIC_BROWSE_GROUP);
2109 + root = sdp_list_append(0, &root_uuid);
2110 + sdp_set_browse_groups(&record, root);
2112 + // Register as an AG
2114 + sdp_uuid16_create(&svclass_uuid, HANDSFREE_AUDIO_GW_SVCLASS_ID);
2115 + svclass_id = sdp_list_append(0, &svclass_uuid);
2116 + sdp_uuid16_create(&ga_svclass_uuid, GENERIC_AUDIO_SVCLASS_ID);
2117 + svclass_id = sdp_list_append(svclass_id, &ga_svclass_uuid);
2118 + sdp_set_service_classes(&record, svclass_id);
2119 + sdp_uuid16_create(&profile.uuid, 0x111f);
2120 + profile.version = 0x0100;
2121 + pfseq = sdp_list_append(0, &profile);
2123 + sdp_set_profile_descs(&record, pfseq);
2125 + sdp_uuid16_create(&l2cap_uuid, L2CAP_UUID);
2126 + proto[0] = sdp_list_append(0, &l2cap_uuid);
2127 + apseq = sdp_list_append(0, proto[0]);
2129 + sdp_uuid16_create(&rfcomm_uuid, RFCOMM_UUID);
2130 + proto[1] = sdp_list_append(0, &rfcomm_uuid);
2131 + channel = sdp_data_alloc(SDP_UINT8, &u8);
2132 + proto[1] = sdp_list_append(proto[1], channel);
2133 + apseq = sdp_list_append(apseq, proto[1]);
2135 + aproto = sdp_list_append(0, apseq);
2136 + sdp_set_access_protos(&record, aproto);
2138 + sdp_set_info_attr(&record, "Voice Gateway", 0, 0);
2140 + if (sdp_record_register(session, &record, SDP_RECORD_PERSIST) < 0) {
2141 + ast_log(LOG_ERROR, "Service Record registration failed\n");
2146 + sdp_record_ag = record.handle;
2147 + sdp_record_gui = record.handle;
2149 + ast_log(LOG_NOTICE, "HeadsetAudioGateway service registered\n");
2151 + sdp_data_free(channel);
2152 + sdp_list_free(proto[0], 0);
2153 + sdp_list_free(proto[1], 0);
2154 + sdp_list_free(apseq, 0);
2155 + sdp_list_free(aproto, 0);
2159 + memset((void *)&record, 0, sizeof(sdp_record_t));
2160 + record.handle = 0xffffffff;
2161 + sdp_uuid16_create(&root_uuid, PUBLIC_BROWSE_GROUP);
2162 + root = sdp_list_append(0, &root_uuid);
2163 + sdp_set_browse_groups(&record, root);
2165 + // Register as an HS
2167 + sdp_uuid16_create(&svclass_uuid, HANDSFREE_AUDIO_GW_SVCLASS_ID);
2168 + svclass_id = sdp_list_append(0, &svclass_uuid);
2169 + sdp_uuid16_create(&ga_svclass_uuid, GENERIC_AUDIO_SVCLASS_ID);
2170 + svclass_id = sdp_list_append(svclass_id, &ga_svclass_uuid);
2171 + sdp_set_service_classes(&record, svclass_id);
2172 + sdp_uuid16_create(&profile.uuid, 0x111e);
2173 + profile.version = 0x0100;
2174 + pfseq = sdp_list_append(0, &profile);
2175 + sdp_set_profile_descs(&record, pfseq);
2177 + sdp_uuid16_create(&l2cap_uuid, L2CAP_UUID);
2178 + proto[0] = sdp_list_append(0, &l2cap_uuid);
2179 + apseq = sdp_list_append(0, proto[0]);
2181 + sdp_uuid16_create(&rfcomm_uuid, RFCOMM_UUID);
2182 + proto[1] = sdp_list_append(0, &rfcomm_uuid);
2183 + channel = sdp_data_alloc(SDP_UINT8, &u8_hs);
2184 + proto[1] = sdp_list_append(proto[1], channel);
2185 + apseq = sdp_list_append(apseq, proto[1]);
2187 + aproto = sdp_list_append(0, apseq);
2188 + sdp_set_access_protos(&record, aproto);
2189 + sdp_set_info_attr(&record, "Voice Gateway", 0, 0);
2191 + if (sdp_record_register(session, &record, SDP_RECORD_PERSIST) < 0) {
2192 + ast_log(LOG_ERROR, "Service Record registration failed\n");
2197 + sdp_record_hs = record.handle;
2199 + ast_log(LOG_NOTICE, "HeadsetAudioGateway service registered\n");
2202 + sdp_data_free(channel);
2203 + sdp_list_free(proto[0], 0);
2204 + sdp_list_free(proto[1], 0);
2205 + sdp_list_free(apseq, 0);
2206 + sdp_list_free(aproto, 0);
2212 +rfcomm_listen(bdaddr_t * bdaddr, int channel)
2216 + struct sockaddr_rc loc_addr;
2219 + if ((sock = socket(PF_BLUETOOTH, SOCK_STREAM, BTPROTO_RFCOMM)) < 0) {
2220 + ast_log(LOG_ERROR, "Can't create socket: %s (errno: %d)\n", strerror(errno), errno);
2224 + loc_addr.rc_family = AF_BLUETOOTH;
2226 + /* Local Interface Address */
2227 + bacpy(&loc_addr.rc_bdaddr, bdaddr);
2230 + loc_addr.rc_channel = channel;
2232 + if (bind(sock, (struct sockaddr *)&loc_addr, sizeof(loc_addr)) < 0) {
2233 + ast_log(LOG_ERROR, "Can't bind socket: %s (errno: %d)\n", strerror(errno), errno);
2238 + if (setsockopt(sock, SOL_SOCKET, SO_REUSEADDR, &on, sizeof(on)) == -1) {
2239 + ast_log(LOG_ERROR, "Set socket SO_REUSEADDR option on failed: errno %d, %s", errno, strerror(errno));
2244 + if (fcntl(sock, F_SETFL, O_RDWR|O_NONBLOCK) != 0)
2245 + ast_log(LOG_ERROR, "Can't set RFCOMM socket to NBIO\n");
2247 + if (listen(sock, 10) < 0) {
2248 + ast_log(LOG_ERROR,"Can not listen on the socket. %s(%d)\n", strerror(errno), errno);
2253 + ast_log(LOG_NOTICE, "Listening for RFCOMM channel %d connections on FD %d\n", channel, sock);
2260 +sco_listen(bdaddr_t * bdaddr)
2264 + struct sockaddr_sco loc_addr;
2266 + memset(&loc_addr, 0, sizeof(loc_addr));
2268 + if ((sock = socket(PF_BLUETOOTH, SOCK_SEQPACKET, BTPROTO_SCO)) < 0) {
2269 + ast_log(LOG_ERROR, "Can't create SCO socket: %s (errno: %d)\n", strerror(errno), errno);
2273 + loc_addr.sco_family = AF_BLUETOOTH;
2274 + bacpy(&loc_addr.sco_bdaddr, BDADDR_ANY);
2276 + if (bind(sock, (struct sockaddr *)&loc_addr, sizeof(loc_addr)) < 0) {
2277 + ast_log(LOG_ERROR, "Can't bind SCO socket: %s (errno: %d)\n", strerror(errno), errno);
2282 + if (setsockopt(sock, SOL_SOCKET, SO_REUSEADDR, &on, sizeof(on)) == -1) {
2283 + ast_log(LOG_ERROR, "Set SCO socket SO_REUSEADDR option on failed: errno %d, %s", errno, strerror(errno));
2288 + if (fcntl(sock, F_SETFL, O_RDWR|O_NONBLOCK) != 0)
2289 + ast_log(LOG_ERROR, "Can't set SCO socket to NBIO\n");
2291 + if (listen(sock, 10) < 0) {
2292 + ast_log(LOG_ERROR,"Can not listen on SCO socket: %s(%d)\n", strerror(errno), errno);
2297 + ast_log(LOG_NOTICE, "Listening for SCO connections on FD %d\n", sock);
2303 +rfcomm_connect(bdaddr_t * src, bdaddr_t * dst, int channel, int nbio)
2305 + struct sockaddr_rc addr;
2308 + if ((s = socket(PF_BLUETOOTH, SOCK_STREAM, BTPROTO_RFCOMM)) < 0) {
2312 + memset(&addr, 0, sizeof(addr));
2313 + addr.rc_family = AF_BLUETOOTH;
2314 + bacpy(&addr.rc_bdaddr, src);
2315 + addr.rc_channel = 0;
2317 + if (bind(s, (struct sockaddr *)&addr, sizeof(addr)) < 0) {
2322 + memset(&addr, 0, sizeof(addr));
2323 + addr.rc_family = AF_BLUETOOTH;
2324 + bacpy(&addr.rc_bdaddr, dst);
2325 + addr.rc_channel = channel;
2328 + if (fcntl(s, F_SETFL, O_RDWR|O_NONBLOCK) != 0)
2329 + ast_log(LOG_ERROR, "Can't set RFCOMM socket to NBIO\n");
2332 + if (connect(s, (struct sockaddr *)&addr, sizeof(addr)) < 0 && (nbio != 1 || (errno != EAGAIN))) {
2340 +/* Must be called with dev->lock held */
2343 +sco_connect(blt_dev_t * dev)
2345 + struct sockaddr_sco addr;
2346 + // struct sco_conninfo conn;
2347 + // struct sco_options opts;
2349 + // bdaddr_t * src = &local_bdaddr;
2352 + bdaddr_t * dst = &(dev->bdaddr);
2354 + if (dev->sco != -1) {
2355 + ast_log(LOG_ERROR, "SCO fd already open.\n");
2359 + if ((s = socket(PF_BLUETOOTH, SOCK_SEQPACKET, BTPROTO_SCO)) < 0) {
2360 + ast_log(LOG_ERROR, "Can't create SCO socket(): %s\n", strerror(errno));
2364 + memset(&addr, 0, sizeof(addr));
2366 + addr.sco_family = AF_BLUETOOTH;
2367 + bacpy(&addr.sco_bdaddr, BDADDR_ANY);
2369 + if (bind(s, (struct sockaddr *)&addr, sizeof(addr)) < 0) {
2370 + ast_log(LOG_ERROR, "Can't bind() SCO socket: %s\n", strerror(errno));
2375 + memset(&addr, 0, sizeof(addr));
2376 + addr.sco_family = AF_BLUETOOTH;
2377 + bacpy(&addr.sco_bdaddr, dst);
2379 + if (fcntl(s, F_SETFL, O_RDWR|O_NONBLOCK) != 0)
2380 + ast_log(LOG_ERROR, "Can't set SCO socket to NBIO\n");
2382 + if ((connect(s, (struct sockaddr *)&addr, sizeof(addr)) < 0) && (errno != EAGAIN)) {
2383 + ast_log(LOG_ERROR, "Can't connect() SCO socket: %s (errno %d)\n", strerror(errno), errno);
2388 + //size = sizeof(conn);
2391 +/* XXX:T: HERE, fix getting SCO conninfo.
2393 + if (getsockopt(s, SOL_SCO, SCO_CONNINFO, &conn, &size) < 0) {
2394 + ast_log(LOG_ERROR, "Can't getsockopt SCO_CONNINFO on SCO socket: %s\n", strerror(errno));
2399 + size = sizeof(opts);
2401 + if (getsockopt(s, SOL_SCO, SCO_OPTIONS, &opts, &size) < 0) {
2402 + ast_log(LOG_ERROR, "Can't getsockopt SCO_OPTIONS on SCO socket: %s\n", strerror(errno));
2407 + dev->sco_handle = conn.hci_handle;
2408 + dev->sco_mtu = opts.mtu;
2412 + ast_log(LOG_DEBUG, "SCO: %d\n", s);
2420 +/* ---------------------------------- */
2422 +/* Non blocking (async) outgoing bluetooth connection */
2425 +try_connect(blt_dev_t * dev)
2428 + ast_mutex_lock(&(dev->lock));
2430 + if (dev->status != BLT_STATUS_CONNECTING && dev->status != BLT_STATUS_DOWN) {
2431 + ast_mutex_unlock(&(dev->lock));
2435 + if (dev->rd != -1) {
2438 + struct pollfd pfd;
2440 + if (dev->status != BLT_STATUS_CONNECTING) {
2441 + ast_mutex_unlock(&(dev->lock));
2442 + dev->outgoing_id = -1;
2446 + // ret = connect(dev->rd, (struct sockaddr *)&(dev->addr), sizeof(struct sockaddr_rc)); //
2449 + pfd.events = POLLIN | POLLOUT;
2451 + ret = poll(&pfd, 1, 0);
2456 + dev->status = BLT_STATUS_DOWN;
2457 + dev->outgoing_id = ast_sched_add(sched, 10000, AST_SCHED_CB(try_connect), dev);
2458 + ast_mutex_unlock(&(dev->lock));
2464 + int len = sizeof(ret);
2465 + getsockopt(dev->rd, SOL_SOCKET, SO_ERROR, &ret, &len);
2469 + ast_log(LOG_NOTICE, "Initialised bluetooth link to device %s\n", dev->name);
2473 + struct hci_conn_info_req * cr;
2477 + cr = malloc(sizeof(*cr) + sizeof(struct hci_conn_info));
2478 + dd = hci_open_dev(hcidev_id);
2479 + cr->type = ACL_LINK;
2480 + bacpy(&cr->bdaddr, &(dev->bdaddr));
2482 + if (ioctl(dd, HCIGETCONNINFO, (unsigned long)cr) < 0) {
2483 + ast_log(LOG_ERROR, "Failed to get connection info: %s\n", strerror(errno));
2485 + ast_log(LOG_DEBUG, "HCI Handle: %d\n", cr->conn_info->handle);
2488 + if (hci_read_remote_name(dd, &(dev->bdaddr), sizeof(name), name, 25000) == 0)
2489 + ast_log(LOG_DEBUG, "Remote Name: %s\n", name);
2494 + dev->status = BLT_STATUS_NEGOTIATING;
2496 + /* If this device is an AG/GUI, we initiate the negotiation. */
2498 + if (dev->role == BLT_ROLE_AG ||
2499 + dev->role == BLT_ROLE_GUI) {
2500 + dev->cb = ag_brsf_response;
2501 + send_atcmd(dev, "AT+BRSF=23");
2504 + dev->outgoing_id = -1;
2505 + ast_mutex_unlock(&(dev->lock));
2510 + if (ret != EHOSTDOWN)
2511 + ast_log(LOG_NOTICE, "Connect to device %s failed: %s (errno %d)\n", dev->name, strerror(ret), ret);
2515 + dev->status = BLT_STATUS_DOWN;
2516 + dev->outgoing_id = ast_sched_add(sched, (ret == EHOSTDOWN) ? 10000 : 2500, AST_SCHED_CB(try_connect), dev);
2517 + ast_mutex_unlock(&(dev->lock));
2524 + dev->outgoing_id = ast_sched_add(sched, 100, AST_SCHED_CB(try_connect), dev);
2525 + ast_mutex_unlock(&(dev->lock));
2529 + ast_log(LOG_NOTICE, "RFCOMM connect start.\n");
2530 + fd = rfcomm_connect(&local_bdaddr, &(dev->bdaddr), dev->channel, 1);
2531 + ast_log(LOG_NOTICE, "RFCOMM connect done.\n");
2534 + ast_log(LOG_WARNING, "NBIO connect() to %s returned %d: %s\n", dev->name, errno, strerror(errno));
2535 + dev->outgoing_id = ast_sched_add(sched, 5000, AST_SCHED_CB(try_connect), dev);
2536 + ast_mutex_unlock(&(dev->lock));
2541 + dev->status = BLT_STATUS_CONNECTING;
2542 + dev->outgoing_id = ast_sched_add(sched, 100, AST_SCHED_CB(try_connect), dev);
2543 + ast_mutex_unlock(&(dev->lock));
2548 +/* Called whenever a new command is received while we're the AG */
2552 +process_rfcomm_cmd(blt_dev_t * dev, char * cmd)
2555 + char * fullcmd = cmd;
2557 + if (option_verbose)
2558 + ast_verbose(VERBOSE_PREFIX_1 "[%s] %*s > %s\n", role2str(dev->role), 10, dev->name, cmd);
2560 + /* Read the 'AT' from the start of the string */
2561 + if (strncmp(cmd, "AT", 2)) {
2562 + ast_log(LOG_WARNING, "Unknown command without 'AT': %s\n", cmd);
2563 + send_atcmd_error(dev);
2569 + // Don't forget 'AT' on its own is OK.
2571 + if (strlen(cmd) == 0) {
2572 + send_atcmd_ok(dev, fullcmd);
2576 + for (i = 0 ; i < ATCMD_LIST_LEN ; i++) {
2577 + if (strncmp(atcmd_list[i].str, cmd, strlen(atcmd_list[i].str)) == 0) {
2578 + char * pos = (cmd + strlen(atcmd_list[i].str));
2579 + if ((strncmp(pos, "=?", 2) == 0) && (strlen(pos) == 2)) {
2580 + /* TEST command */
2581 + if (atcmd_list[i].test) {
2582 + if (atcmd_list[i].test(dev) == 0)
2583 + send_atcmd_ok(dev, fullcmd);
2585 + send_atcmd_error(dev);
2587 + send_atcmd_ok(dev, fullcmd);
2589 + } else if ((strncmp(pos, "?", 1) == 0) && (strlen(pos) == 1)) {
2590 + /* READ command */
2591 + if (atcmd_list[i].read) {
2592 + if (atcmd_list[i].read(dev) == 0)
2593 + send_atcmd_ok(dev, fullcmd);
2595 + send_atcmd_error(dev);
2597 + ast_log(LOG_WARNING, "AT Command: '%s' missing READ function\n", fullcmd);
2598 + send_atcmd_error(dev);
2600 + } else if (strncmp(pos, "=", 1) == 0) {
2602 + if (atcmd_list[i].set) {
2603 + if (atcmd_list[i].set(dev, (pos + 1), (*(pos + 1)) ? strlen(pos + 1) : 0) == 0)
2604 + send_atcmd_ok(dev, fullcmd);
2606 + send_atcmd_error(dev);
2608 + ast_log(LOG_WARNING, "AT Command: '%s' missing SET function\n", fullcmd);
2609 + send_atcmd_error(dev);
2612 + /* EXECUTE command */
2613 + if (atcmd_list[i].execute) {
2614 + if (atcmd_list[i].execute(dev, cmd + strlen(atcmd_list[i].str)) == 0)
2615 + send_atcmd_ok(dev, fullcmd);
2617 + send_atcmd_error(dev);
2619 + ast_log(LOG_WARNING, "AT Command: '%s' missing EXECUTE function\n", fullcmd);
2620 + send_atcmd_error(dev);
2627 + ast_log(LOG_NOTICE, "Unknown AT Command: '%s' (%s)\n", fullcmd, cmd);
2628 + send_atcmd_error(dev);
2633 +/* Called when a socket is incoming */
2636 +handle_incoming(int fd, blt_role_t role)
2639 + struct sockaddr_rc addr;
2640 + int len = sizeof(addr);
2642 + // Got a new incoming socket.
2643 + ast_log(LOG_DEBUG, "Incoming RFCOMM socket\n");
2645 + ast_mutex_lock(&iface_lock);
2647 + fd = accept(fd, (struct sockaddr*)&addr, &len);
2651 + if (bacmp(&(dev->bdaddr), &addr.rc_bdaddr) == 0) {
2652 + ast_log(LOG_DEBUG, "Connect from %s\n", dev->name);
2653 + ast_mutex_lock(&(dev->lock));
2654 + /* Kill any outstanding connect attempt. */
2655 + if (dev->outgoing_id > -1) {
2656 + ast_sched_del(sched, dev->outgoing_id);
2657 + dev->outgoing_id = -1;
2660 + rd_close(dev, 0, 0);
2662 + dev->status = BLT_STATUS_NEGOTIATING;
2665 + if (dev->role == BLT_ROLE_AG ||
2666 + dev->role == BLT_ROLE_GUI) {
2667 + dev->cb = ag_brsf_response;
2668 + send_atcmd(dev, "AT+BRSF=23");
2670 + ast_mutex_unlock(&(dev->lock));
2676 + if (dev == NULL) {
2677 + ast_log(LOG_WARNING, "Connect from unknown device\n");
2680 + ast_mutex_unlock(&iface_lock);
2686 +handle_incoming_sco(int master)
2690 + struct sockaddr_sco addr;
2691 + struct sco_conninfo conn;
2692 + struct sco_options opts;
2693 + int len = sizeof(addr);
2696 + ast_log(LOG_DEBUG, "Incoming SCO socket\n");
2698 + fd = accept(master, (struct sockaddr*)&addr, &len);
2700 + if (fcntl(fd, F_SETFL, O_RDWR|O_NONBLOCK) != 0) {
2701 + ast_log(LOG_ERROR, "Can't set SCO socket to NBIO\n");
2706 + len = sizeof(conn);
2708 + if (getsockopt(fd, SOL_SCO, SCO_CONNINFO, &conn, &len) < 0) {
2709 + ast_log(LOG_ERROR, "Can't getsockopt SCO_CONNINFO on SCO socket: %s\n", strerror(errno));
2714 + len = sizeof(opts);
2716 + if (getsockopt(fd, SOL_SCO, SCO_OPTIONS, &opts, &len) < 0) {
2717 + ast_log(LOG_ERROR, "Can't getsockopt SCO_OPTIONS on SCO socket: %s\n", strerror(errno));
2722 + ast_mutex_lock(&iface_lock);
2725 + if (bacmp(&(dev->bdaddr), &addr.sco_bdaddr) == 0) {
2726 + ast_log(LOG_DEBUG, "SCO Connect from %s\n", dev->name);
2727 + ast_mutex_lock(&(dev->lock));
2728 + if (dev->sco_running != -1) {
2729 + ast_log(LOG_ERROR, "Incoming SCO socket, but SCO thread already running.\n");
2731 + sco_start(dev, fd);
2733 + ast_mutex_unlock(&(dev->lock));
2739 + ast_mutex_unlock(&iface_lock);
2741 + if (dev == NULL) {
2742 + ast_log(LOG_WARNING, "SCO Connect from unknown device\n");
2745 + // XXX:T: We need to handle the fact we might have an outgoing connection attempt in progress.
2746 + ast_log(LOG_DEBUG, "SCO: %d, HCIHandle=%d, MUT=%d\n", fd, conn.hci_handle, opts.mtu);
2754 +/* Called when there is data waiting on a socket */
2757 +handle_rd_data(blt_dev_t * dev)
2762 + while ((ret = read(dev->rd, &c, 1)) == 1) {
2764 + // log_buf[i++] = c;
2766 + if (dev->role == BLT_ROLE_HS) {
2769 + ret = process_rfcomm_cmd(dev, dev->rd_buff);
2770 + dev->rd_buff_pos = 0;
2771 + memset(dev->rd_buff, 0, BLT_RDBUFF_MAX);
2775 + if (dev->rd_buff_pos >= BLT_RDBUFF_MAX)
2778 + dev->rd_buff[dev->rd_buff_pos++] = c;
2780 + } else if (dev->role == BLT_ROLE_AG ||
2781 + dev->role == BLT_ROLE_GUI) {
2783 + //ast_log(LOG_ERROR, "%s: %c\n", dev->name, c);
2785 + switch (dev->state) {
2786 + case BLT_STATE_WANT_R:
2787 + if (c == '\r' || c == 10) {
2788 + dev->state = BLT_STATE_WANT_N;
2789 + } else if (c == '+') {
2790 + dev->state = BLT_STATE_WANT_CMD;
2791 + dev->rd_buff[dev->rd_buff_pos++] = '+';
2793 + ast_log(LOG_ERROR, "Device %s: Expected '\\r', got %d. state=BLT_STATE_WANT_R\n", dev->name, c);
2798 + case BLT_STATE_WANT_N:
2799 + if (c == '\n' || c == 13)
2800 + dev->state = BLT_STATE_WANT_CMD;
2802 + ast_log(LOG_ERROR, "Device %s: Expected '\\n', got %d. state=BLT_STATE_WANT_N\n", dev->name, c);
2807 + case BLT_STATE_WANT_CMD:
2808 + if (c == '\r' || c == 10)
2809 + dev->state = BLT_STATE_WANT_N2;
2811 + if (dev->rd_buff_pos >= BLT_RDBUFF_MAX) {
2812 + ast_log(LOG_ERROR, "Device %s: Buffer exceeded\n", dev->name);
2815 + dev->rd_buff[dev->rd_buff_pos++] = c;
2819 + case BLT_STATE_WANT_N2:
2820 + if (c == '\n' || c == 13) {
2822 + dev->state = BLT_STATE_WANT_R;
2824 + if (dev->rd_buff[0] == '+') {
2826 + // find unsolicited
2827 + for (i = 0 ; i < ATCMD_LIST_LEN ; i++) {
2828 + if (strncmp(atcmd_list[i].str, dev->rd_buff, strlen(atcmd_list[i].str)) == 0) {
2829 + if (atcmd_list[i].unsolicited)
2830 + atcmd_list[i].unsolicited(dev, dev->rd_buff + strlen(atcmd_list[i].str) + 1);
2832 + ast_log(LOG_WARNING, "Device %s: Unhandled Unsolicited: %s\n", dev->name, dev->rd_buff);
2837 + if (option_verbose)
2838 + ast_verbose(VERBOSE_PREFIX_1 "[%s] %*s > %s\n", role2str(dev->role), 10, dev->name, dev->rd_buff);
2840 + if (i == ATCMD_LIST_LEN)
2841 + ast_log(LOG_NOTICE, "Device %s: Got unsolicited message: %s\n", dev->name, dev->rd_buff);
2843 + } else if (dev->rd_buff[0] == '*') {
2844 + if (option_verbose)
2845 + ast_verbose(VERBOSE_PREFIX_1 "[%s]* %*s > %s\n", role2str(dev->role), 9, dev->name, dev->rd_buff);
2849 + for (i = 0 ; i < ATCMD_LIST_LEN ; i++) {
2850 + if (strncmp(atcmd_list[i].str, dev->rd_buff, strlen(atcmd_list[i].str)) == 0) {
2851 + if (atcmd_list[i].execute)
2852 + atcmd_list[i].execute(dev, dev->rd_buff + strlen(atcmd_list[i].str) + 1);
2854 + ast_log(LOG_ERROR, "Device %s: Unhandled Execute: %s\n", dev->name, dev->rd_buff);
2863 + strcmp(dev->rd_buff, "OK") != 0 &&
2864 + strcmp(dev->rd_buff, "CONNECT") != 0 &&
2865 + strcmp(dev->rd_buff, "RING") != 0 &&
2866 + strcmp(dev->rd_buff, "NO CARRIER") != 0 &&
2867 + strcmp(dev->rd_buff, "ERROR") != 0 &&
2868 + strcmp(dev->rd_buff, "NO DIALTONE") != 0 &&
2869 + strcmp(dev->rd_buff, "BUSY") != 0 &&
2870 + strcmp(dev->rd_buff, "NO ANSWER") != 0 &&
2871 + strcmp(dev->rd_buff, "DELAYED") != 0
2873 + // It must be a multiline error
2874 + strncpy(dev->last_err_cmd, dev->rd_buff, 1023);
2875 + if (option_verbose)
2876 + ast_verbose(VERBOSE_PREFIX_1 "[%s] %*s > %s\n", role2str(dev->role), 10, dev->name, dev->rd_buff);
2877 + } else if (dev->cb) {
2878 + if (option_verbose)
2879 + ast_verbose(VERBOSE_PREFIX_1 "[%s] %*s > %s\n", role2str(dev->role), 10, dev->name, dev->rd_buff);
2880 + dev->cb(dev, dev->rd_buff);
2882 + ast_log(LOG_ERROR, "Device %s: Data on socket in HS mode, but no callback\n", dev->name);
2887 + dev->rd_buff_pos = 0;
2888 + memset(dev->rd_buff, 0, BLT_RDBUFF_MAX);
2891 + ast_log(LOG_ERROR, "Device %s: Expected '\\n' got %d. state = BLT_STATE_WANT_N2:\n", dev->name, c);
2899 + ast_log(LOG_ERROR, "Device %s: Unknown device state %d\n", dev->name, dev->state);
2911 +/* Close the devices RFCOMM socket, and SCO if it exists. Must hold dev->lock */
2914 +rd_close(blt_dev_t * dev, int reconnect, int e)
2923 + dev->status = BLT_STATUS_DOWN;
2928 + ast_setstate(dev->owner, AST_STATE_DOWN);
2929 + ast_queue_control(dev->owner, AST_CONTROL_HANGUP);
2932 + /* Schedule a reconnect */
2933 + if (reconnect && dev->autoconnect) {
2934 + dev->outgoing_id = ast_sched_add(sched, 5000, AST_SCHED_CB(try_connect), dev);
2936 + if (monitor_thread == pthread_self()) {
2937 + // Because we're not the monitor thread, we needd to inturrupt poll().
2938 + pthread_kill(monitor_thread, SIGURG);
2942 + ast_log(LOG_NOTICE, "Device %s disconnected, scheduled reconnect in 5 seconds: %s (errno %d)\n", dev->name, strerror(e), e);
2944 + ast_log(LOG_NOTICE, "Device %s disconnected: %s (errno %d)\n", dev->name, strerror(e), e);
2951 + * Remember that we can only add to the scheduler from
2952 + * the do_monitor thread, as it calculates time to next one from
2957 +do_monitor(void * data)
2959 +#define SRV_SOCK_CNT 4
2963 + struct pollfd * pfds = malloc(sizeof(struct pollfd) * (ifcount + SRV_SOCK_CNT));
2965 + /* -- We start off by trying to connect all of our devices (non blocking) -- */
2967 + monitor_pid = getpid();
2969 + if (ast_mutex_lock(&iface_lock)) {
2970 + ast_log(LOG_ERROR, "Failed to get iface_lock.\n");
2977 + if (socketpair(PF_UNIX, SOCK_STREAM, 0, dev->sco_pipe) != 0) {
2978 + ast_log(LOG_ERROR, "Failed to create socket pair: %s (errno %d)\n", strerror(errno), errno);
2979 + ast_mutex_unlock(&iface_lock);
2983 + if (dev->autoconnect && dev->status == BLT_STATUS_DOWN)
2984 + dev->outgoing_id = ast_sched_add(sched, 1500, AST_SCHED_CB(try_connect), dev);
2987 + ast_mutex_unlock(&iface_lock);
2989 + /* -- Now, Scan all sockets, and service scheduler -- */
2991 + pfds[0].fd = rfcomm_sock_ag;
2992 + pfds[0].events = POLLIN;
2994 + pfds[1].fd = rfcomm_sock_hs;
2995 + pfds[1].events = POLLIN;
2997 + pfds[2].fd = rfcomm_sock_gui;
2998 + pfds[2].events = POLLIN;
3000 + pfds[3].fd = sco_socket;
3001 + pfds[3].events = POLLIN;
3004 + int cnt = SRV_SOCK_CNT;
3007 + /* -- Build pfds -- */
3009 + if (ast_mutex_lock(&iface_lock)) {
3010 + ast_log(LOG_ERROR, "Failed to get iface_lock.\n");
3015 + ast_mutex_lock(&(dev->lock));
3016 + if (dev->rd > 0 && ((dev->status != BLT_STATUS_DOWN) && (dev->status != BLT_STATUS_CONNECTING))) {
3017 + pfds[cnt].fd = dev->rd;
3018 + pfds[cnt].events = POLLIN;
3021 + ast_mutex_unlock(&(dev->lock));
3024 + ast_mutex_unlock(&iface_lock);
3026 + /* -- End Build pfds -- */
3028 + res = ast_sched_wait(sched);
3029 + res = poll(pfds, cnt, MAX(100, MIN(100, res)));
3032 + ast_sched_runq(sched);
3034 + if (pfds[0].revents) {
3035 + handle_incoming(rfcomm_sock_ag, BLT_ROLE_AG);
3039 + if (pfds[1].revents) {
3040 + handle_incoming(rfcomm_sock_hs, BLT_ROLE_HS);
3044 + if (pfds[2].revents) {
3045 + handle_incoming(rfcomm_sock_gui, BLT_ROLE_GUI);
3049 + if (pfds[3].revents) {
3050 + handle_incoming_sco(sco_socket);
3057 + for (i = SRV_SOCK_CNT ; i < cnt ; i++) {
3059 + /* Optimise a little bit */
3062 + else if (pfds[i].revents == 0)
3065 + /* -- Find the socket that has activity -- */
3067 + if (ast_mutex_lock(&iface_lock)) {
3068 + ast_log(LOG_ERROR, "Failed to get iface_lock.\n");
3075 + if (pfds[i].fd == dev->rd) {
3076 + ast_mutex_lock(&(dev->lock));
3077 + if (pfds[i].revents & POLLIN) {
3078 + if (handle_rd_data(dev) == -1) {
3079 + rd_close(dev, 0, 0);
3082 + rd_close(dev, 1, sock_err(dev->rd));
3084 + ast_mutex_unlock(&(dev->lock));
3091 + if (dev == NULL) {
3092 + ast_log(LOG_ERROR, "Unhandled fd from poll()\n");
3093 + close(pfds[i].fd);
3096 + ast_mutex_unlock(&iface_lock);
3098 + /* -- End find socket with activity -- */
3108 +restart_monitor(void)
3111 + if (monitor_thread == AST_PTHREADT_STOP)
3114 + if (ast_mutex_lock(&monitor_lock)) {
3115 + ast_log(LOG_WARNING, "Unable to lock monitor\n");
3119 + if (monitor_thread == pthread_self()) {
3120 + ast_mutex_unlock(&monitor_lock);
3121 + ast_log(LOG_WARNING, "Cannot kill myself\n");
3125 + if (monitor_thread != AST_PTHREADT_NULL) {
3127 + /* Just signal it to be sure it wakes up */
3128 + pthread_cancel(monitor_thread);
3129 + pthread_kill(monitor_thread, SIGURG);
3130 + ast_log(LOG_DEBUG, "Waiting for monitor thread to join...\n");
3131 + pthread_join(monitor_thread, NULL);
3132 + ast_log(LOG_DEBUG, "joined\n");
3136 + /* Start a new monitor */
3137 + if (ast_pthread_create(&monitor_thread, NULL, do_monitor, NULL) < 0) {
3138 + ast_mutex_unlock(&monitor_lock);
3139 + ast_log(LOG_ERROR, "Unable to start monitor thread.\n");
3145 + ast_mutex_unlock(&monitor_lock);
3150 +blt_parse_config(void)
3152 + struct ast_config * cfg;
3153 + struct ast_variable * v;
3156 + cfg = ast_load(BLT_CONFIG_FILE);
3159 + ast_log(LOG_NOTICE, "Unable to load Bluetooth config: %s. Bluetooth disabled\n", BLT_CONFIG_FILE);
3163 + v = ast_variable_browse(cfg, "general");
3166 + if (!strcasecmp(v->name, "rfchannel_ag")) {
3167 + rfcomm_channel_ag = atoi(v->value);
3168 + } else if (!strcasecmp(v->name, "rfchannel_hs")) {
3169 + rfcomm_channel_hs = atoi(v->value);
3170 + } else if (!strcasecmp(v->name, "rfchannel_gui")) {
3171 + rfcomm_channel_gui = atoi(v->value);
3172 + } else if (!strcasecmp(v->name, "interface")) {
3173 + hcidev_id = atoi(v->value);
3174 + } else if (!strcasecmp(v->name, "gui_default_sip_number")) {
3175 + gui_default_sip_number = v->value;
3176 + } else if (!strcasecmp(v->name, "gui_default_sip_address")) {
3177 + gui_default_sip_address = v->value;
3179 + ast_log(LOG_WARNING, "Unknown config key '%s' in section [general]\n", v->name);
3183 + cat = ast_category_browse(cfg, NULL);
3189 + if (strcasecmp(cat, "general")) {
3190 + blt_dev_t * device = malloc(sizeof(blt_dev_t));
3191 + memset(device, 0, sizeof(blt_dev_t));
3192 + device->sco_running = -1;
3195 + device->outgoing_id = -1;
3196 + device->status = BLT_STATUS_DOWN;
3197 + str2ba(cat, &(device->bdaddr));
3198 + device->name = ast_variable_retrieve(cfg, cat, "name");
3200 + str = ast_variable_retrieve(cfg, cat, "type");
3202 + if (str == NULL) {
3203 + ast_log(LOG_ERROR, "Device [%s] has no role. Specify type=<HS/AG>\n", cat);
3205 + } else if (strcasecmp(str, "HS") == 0) {
3206 + device->role = BLT_ROLE_HS;
3207 + } else if (strcasecmp(str, "AG") == 0) {
3208 + device->role = BLT_ROLE_AG;
3209 + } else if (strcasecmp(str, "GUI") == 0) {
3210 + device->role = BLT_ROLE_GUI;
3212 + ast_log(LOG_ERROR, "Device [%s] has invalid role '%s'\n", cat, str);
3216 + /* XXX:T: Find channel to use using SDP.
3217 + * However, this needs to be non blocking, and I can't see
3218 + * anything in sdp_lib.h that will allow non blocking calls.
3221 + device->channel = 1;
3223 + if ((str = ast_variable_retrieve(cfg, cat, "channel")) != NULL)
3224 + device->channel = atoi(str);
3226 + if ((str = ast_variable_retrieve(cfg, cat, "autoconnect")) != NULL)
3227 + device->autoconnect = (strcasecmp(str, "yes") == 0 || strcmp(str, "1") == 0) ? 1 : 0;
3229 + if ((str = ast_variable_retrieve(cfg, cat, "context")) != NULL)
3230 + device->context = str;
3232 + device->context = "bluetooth";
3234 + device->next = iface_head;
3235 + iface_head = device;
3239 + cat = ast_category_browse(cfg, cat);
3246 +blt_show_peers(int fd, int argc, char *argv[])
3250 + if (ast_mutex_lock(&iface_lock)) {
3251 + ast_log(LOG_ERROR, "Failed to get Iface lock\n");
3252 + ast_cli(fd, "Failed to get iface lock\n");
3253 + return RESULT_FAILURE;
3258 + ast_cli(fd, "BDAddr Name Role Status A/C SCOCon/Fd/Th Sig\n");
3259 + ast_cli(fd, "----------------- ---------- ---- ----------- --- ------------ ---\n");
3263 + ba2str(&(dev->bdaddr), b1);
3264 + ast_cli(fd, "%s %-10s %-4s %-11s %-3s %2d/%02d/%-6ld %s\n",
3266 +// (dev->role == BLT_ROLE_HS) ? "HS" : "AG",
3267 + (dev->role == BLT_ROLE_HS) ? "HS" : (dev->role == BLT_ROLE_AG) ? "AG" : "GUI",
3268 + status2str(dev->status),
3269 + (dev->autoconnect) ? "Yes" : "No",
3273 + (dev->role == BLT_ROLE_AG) ? (dev->service) ? "Yes" : "No" : "N/A"
3278 + ast_mutex_unlock(&iface_lock);
3279 + return RESULT_SUCCESS;
3283 +blt_show_information(int fd, int argc, char *argv[])
3286 + ba2str(&local_bdaddr, b1);
3287 + ast_cli(fd, "-------------------------------------------\n");
3288 + ast_cli(fd, " Version : %s\n", BLT_SVN_REVISION);
3289 + ast_cli(fd, " Monitor PID : %d\n", monitor_pid);
3290 + ast_cli(fd, " RFCOMM AG : Channel %d, FD %d\n", rfcomm_channel_ag, rfcomm_sock_ag);
3291 + ast_cli(fd, " RFCOMM HS : Channel %d, FD %d\n", rfcomm_channel_hs, rfcomm_sock_hs);
3292 + ast_cli(fd, " RFCOMM GUI : Channel %d, FD %d\n", rfcomm_channel_gui, rfcomm_sock_gui);
3293 + ast_cli(fd, " Device : hci%d, MAC Address %s\n", hcidev_id, b1);
3294 + ast_cli(fd, "-------------------------------------------\n");
3295 + return RESULT_SUCCESS;
3299 +blt_ag_sendcmd(int fd, int argc, char *argv[])
3304 + return RESULT_SHOWUSAGE;
3306 + ast_mutex_lock(&iface_lock);
3309 + if (!strcasecmp(argv[2], dev->name))
3313 + ast_mutex_unlock(&iface_lock);
3316 + ast_cli(fd, "Device '%s' does not exist\n", argv[2]);
3317 + return RESULT_FAILURE;
3320 + if ((dev->role != BLT_ROLE_AG) && (dev->role != BLT_ROLE_GUI)) {
3321 + ast_cli(fd, "Device '%s' is not an AG or GUI\n", argv[2]);
3322 + return RESULT_FAILURE;
3325 + if (dev->status == BLT_STATUS_DOWN || dev->status == BLT_STATUS_NEGOTIATING) {
3326 + ast_cli(fd, "Device '%s' is not connected\n", argv[2]);
3327 + return RESULT_FAILURE;
3330 + if (*(argv[3] + strlen(argv[3]) - 1) == '.')
3331 + *(argv[3] + strlen(argv[3]) - 1) = '?';
3333 + ast_cli(fd, "Sending AT command to %s: %s\n", dev->name, argv[3]);
3335 + ast_mutex_lock(&(dev->lock));
3336 + send_atcmd(dev, argv[3]);
3337 + ast_mutex_unlock(&(dev->lock));
3339 + return RESULT_SUCCESS;
3343 +complete_device(char * line, char * word, int pos, int state, int rpos, blt_role_t role)
3352 + ast_mutex_lock(&iface_lock);
3358 + if ((dev->role == role) && (!strncasecmp(word, dev->name, strlen(word)))) {
3359 + if (++which > state)
3367 + ret = strdup(dev->name);
3371 + ast_mutex_unlock(&iface_lock);
3377 +complete_device_2_ag_gui(char * line, char * word, int pos, int state)
3379 + return complete_device(line, word, pos, state, 2, BLT_ROLE_AG);
3382 +static char show_peers_usage[] =
3383 +"Usage: bluetooth show peers\n"
3384 +" List all bluetooth peers and their status\n";
3386 +static struct ast_cli_entry
3388 + { { "bluetooth", "show", "peers", NULL }, blt_show_peers, "List Bluetooth Peers", show_peers_usage };
3391 +static char ag_sendcmd[] =
3392 +"Usage: bluetooth <device> sendcmd <cmd>\n"
3393 +" Sends a AT cmd over the RFCOMM link, and print result (AG only)\n";
3395 +static struct ast_cli_entry
3397 + { { "bluetooth", "sendcmd", NULL }, blt_ag_sendcmd, "Send AG/GUI an AT command", ag_sendcmd, complete_device_2_ag_gui };
3399 +static char show_information[] =
3400 +"Usage: bluetooth show information\n"
3401 +" Lists information about the bluetooth subsystem\n";
3403 +static struct ast_cli_entry
3404 +cli_show_information =
3405 + { { "bluetooth", "show", "information", NULL }, blt_show_information, "List Bluetooth Info", show_information };
3408 +remove_sdp_records(void)
3411 + sdp_session_t * sdp;
3412 + sdp_list_t * attr;
3413 + sdp_record_t * rec;
3415 + uint32_t range = 0x0000ffff;
3417 + if (sdp_record_ag == -1 || sdp_record_gui == -1 || sdp_record_hs == -1)
3420 + ast_log(LOG_DEBUG, "Removing SDP records\n");
3422 + sdp = sdp_connect(BDADDR_ANY, BDADDR_LOCAL, SDP_RETRY_IF_BUSY);
3427 + attr = sdp_list_append(0, &range);
3428 + rec = sdp_service_attr_req(sdp, sdp_record_ag, SDP_ATTR_REQ_RANGE, attr);
3429 + sdp_list_free(attr, 0);
3432 + if (sdp_record_unregister(sdp, rec) == 0)
3435 + rec = sdp_service_attr_req(sdp, sdp_record_gui, SDP_ATTR_REQ_RANGE, attr);
3436 + sdp_list_free(attr, 0);
3439 + if (sdp_record_unregister(sdp, rec) == 0)
3442 + attr = sdp_list_append(0, &range);
3443 + rec = sdp_service_attr_req(sdp, sdp_record_hs, SDP_ATTR_REQ_RANGE, attr);
3444 + sdp_list_free(attr, 0);
3447 + if (sdp_record_unregister(sdp, rec) == 0)
3453 + ast_log(LOG_NOTICE, "Removed SDP records\n");
3455 + ast_log(LOG_ERROR, "Failed to remove SDP records\n");
3460 +__unload_module(void)
3463 +#if ASTERISK_VERSION_NUM <= 010107
3464 + ast_channel_unregister(BLT_CHAN_NAME);
3466 + ast_channel_unregister(&blt_tech);
3469 + if (monitor_thread != AST_PTHREADT_NULL) {
3471 + if (ast_mutex_lock(&monitor_lock)) {
3473 + if (monitor_thread && (monitor_thread != AST_PTHREADT_STOP) && (monitor_thread != AST_PTHREADT_NULL)) {
3474 + pthread_cancel(monitor_thread);
3475 + pthread_kill(monitor_thread, SIGURG);
3476 + fprintf(stderr, "Waiting for monitor thread to join...\n");
3477 + pthread_join(monitor_thread, NULL);
3478 + fprintf(stderr, "joined\n");
3480 + monitor_thread = AST_PTHREADT_STOP;
3481 + ast_mutex_unlock(&monitor_lock);
3485 + ast_log(LOG_WARNING, "Unable to lock the monitor\n");
3492 + ast_unregister_atexit(remove_sdp_records);
3493 + remove_sdp_records();
3500 + sdp_session_t * sess;
3504 + hcidev_id = BLT_DEFAULT_HCI_DEV;
3506 + if (blt_parse_config() != 0) {
3507 + ast_log(LOG_ERROR, "Bluetooth configuration error. Bluetooth Disabled\n");
3508 + return unload_module();
3511 + dd = hci_open_dev(hcidev_id);
3513 + ast_log(LOG_ERROR, "Unable to open interface hci%d: %s.\n", hcidev_id, strerror(errno));
3517 + hci_read_voice_setting(dd, &vs, 1000);
3521 + if (vs != 0x0060) {
3522 + ast_log(LOG_ERROR, "Bluetooth voice setting must be 0x0060, not 0x%04x\n", vs);
3527 + if ((sched = sched_context_create()) == NULL) {
3528 + ast_log(LOG_WARNING, "Unable to create schedule context\n");
3532 + memset(&local_bdaddr, 0, sizeof(local_bdaddr));
3534 + hci_devba(hcidev_id, &local_bdaddr);
3536 + /* --- Add SDP record --- */
3538 + sess = sdp_connect(&local_bdaddr, BDADDR_LOCAL, SDP_RETRY_IF_BUSY);
3540 + if ((rfcomm_sock_ag = rfcomm_listen(&local_bdaddr, rfcomm_channel_ag)) < 0) {
3544 + if ((rfcomm_sock_hs = rfcomm_listen(&local_bdaddr, rfcomm_channel_hs)) < 0)
3547 + if ((rfcomm_sock_gui = rfcomm_listen(&local_bdaddr, rfcomm_channel_gui)) < 0)
3550 + if ((sco_socket = sco_listen(&local_bdaddr)) < 0)
3554 + ast_log(LOG_ERROR, "Failed to connect to SDP server: %s\n", strerror(errno));
3558 + if (sdp_register(sess) != 0) {
3559 + ast_log(LOG_ERROR, "Failed to register HeadsetAudioGateway in SDP\n");
3565 + if (restart_monitor() != 0)
3568 +#if ASTERISK_VERSION_NUM <= 010107
3569 + if (ast_channel_register(BLT_CHAN_NAME, "Bluetooth Driver", BLUETOOTH_FORMAT, blt_request)) {
3571 + if (ast_channel_register(&blt_tech)) {
3573 + ast_log(LOG_ERROR, "Unable to register channel class BTL\n");
3574 + __unload_module();
3578 + ast_cli_register(&cli_show_information);
3579 + ast_cli_register(&cli_show_peers);
3580 + ast_cli_register(&cli_ag_sendcmd);
3582 + ast_register_atexit(remove_sdp_records);
3584 + ast_log(LOG_NOTICE, "Loaded Bluetooth support, %s\n", BLT_SVN_REVISION + 1);
3590 +unload_module(void)
3592 + ast_cli_unregister(&cli_ag_sendcmd);
3593 + ast_cli_unregister(&cli_show_peers);
3594 + ast_cli_unregister(&cli_show_information);
3595 + return __unload_module();
3602 + ast_mutex_lock(&usecnt_lock);
3604 + ast_mutex_unlock(&usecnt_lock);
3608 +char *description()
3610 + return "Bluetooth Channel Driver";
3616 + return ASTERISK_GPL_KEY;
3620 diff -ruN asterisk-1.0.9-old/configs/bluetooth.conf asterisk-1.0.9-new/configs/bluetooth.conf
3621 --- asterisk-1.0.9-old/configs/bluetooth.conf 1970-01-01 01:00:00.000000000 +0100
3622 +++ asterisk-1.0.9-new/configs/bluetooth.conf 2005-09-06 22:51:38.000000000 +0200
3625 +; Channel we listen on as a HS (Headset)
3627 +; Channel we listen on as an AG (AudioGateway)
3629 +; Channel we listen on as GUI
3631 +; hci interface to use (number - e.g '0')
3634 +; RFCOMM channel to connect to. For a HandsSet:
3635 +; sdptool search --bdaddr xx:xx:xx:xx:xx:xx 0x111E
3636 +; or,for an AudioGateway (Phone):
3637 +; sdptool search --bdaddr xx:xx:xx:xx:xx:xx 0x111F
3639 +; Find the 'channel' value under RFCOMM.
3642 +; Automatically connect?
3645 +;example for a SonyEricsson mobile as a GUI device
3646 +[00:0F:DE:6E:77:6B]
3653 +;[00:0E:6D:1A:3D:86]
3659 +[00:0E:A1:01:49:AE]
3665 +;[00:0A:D9:EB:FD:D8]