Linux bluetooth development
 help / color / mirror / Atom feed
From: Florian Echtler <echtler@fs.tum.de>
To: bmidgley@users.sourceforge.net, bluez-devel@lists.sourceforge.net
Subject: [Bluez-devel] some btsco hacks
Date: Thu, 5 May 2005 20:58:14 +0200 (CEST)	[thread overview]
Message-ID: <Pine.LNX.4.62.0505052044560.10038@block> (raw)

[-- Attachment #1: Type: TEXT/PLAIN, Size: 1471 bytes --]

-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

Hello everyone,

I finally got time to investigate the issues with my headset. Sometimes
after SCO connection establishment, the microphone produces only noise,
and a reconnect is necessary to fix this. The attached patch is against
btsco.c v1.22 from CVS, and does two things:

1. installs a handler for SIGUSR2 which automatically disconnects the
   SCO channel when connected and reconnects it after a short delay

2. allows the command string in .btscorc to contain '%d', which will
   be replaced with the current SCO connection status (0 or 1) upon
   command execution.

1. may be not really useful to most people - as mentioned, my headset
is obviously somewhat buggy and needs a kind of reset sometimes, which
this patch provides (actually, I wrote a shell script which samples
1 second of audio data from the headset, runs it through gzip to determine
whether it's noise and sends SIGUSR2 accordingly - talk about evil hacks ;)

2. was included because I want certain actions to happen only when
the connection is established, and not when it's shutting down.

Brad: if you think this could be useful otherwise, then feel free to 
include the patch into CVS.

Yours, Florian
- -- 
Preserve wildlife - pickle a squirrel today!
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.1 (GNU/Linux)

iD8DBQFCemzI7CzyshGvatgRArpWAJ4g4j5sgUjj3ulgVwxORICyjYdyHgCg5qpr
gE4T1Cniprq057+KD2i79kE=
=jha8
-----END PGP SIGNATURE-----

[-- Attachment #2: Type: TEXT/PLAIN, Size: 2812 bytes --]

diff -u -r1.22 btsco.c
--- btsco.c	3 May 2005 15:37:12 -0000	1.22
+++ btsco.c	5 May 2005 18:46:14 -0000
@@ -80,7 +80,7 @@
 	char *cmd;
 };
 
-static volatile int terminate = 0, ring = 0, hupped = 0;
+static volatile int terminate = 0, ring = 0, hupped = 0, reconnect = 0;
 static int verbose = 0;
 
 static void sig_term(int sig)
@@ -98,6 +98,11 @@
 	hupped = 1;
 }
 
+static void sig_usr(int sig)
+{
+	reconnect = 1;
+}
+
 static int rfcomm_connect(bdaddr_t * src, bdaddr_t * dst, uint8_t channel)
 {
 	struct sockaddr_rc addr;
@@ -573,6 +578,9 @@
 	sa.sa_handler = sig_ring;
 	sigaction(SIGUSR1, &sa, NULL);
 
+	sa.sa_handler = sig_usr;
+	sigaction(SIGUSR2, &sa, NULL);
+
 	sa.sa_handler = sig_hup;
 	sigaction(SIGHUP, &sa, NULL);
 
@@ -704,7 +712,11 @@
 							int substl = 0;
 							
 							subst = NULL;
-							sysbuf = strdup(args);
+							
+							// add the current sco mode to the command string
+							sysbuf = malloc(strlen(args)+1);
+							sprintf(sysbuf,args,sco_mode);
+							
 							for(i = 0; sysbuf[i]; i++) {
 								if((sysbuf[i] == '\\') && (sysbuf[i + 1] >= '0') && (sysbuf[i + 1] <= '9')) {
 									match = sysbuf[i + 1] - '0';
@@ -804,6 +816,40 @@
 
 		}
 
+		// mean little hack to allow for a signal-triggered reconnect
+		// force disconnection of the channel (if connected)
+		if (reconnect == 1) {
+			if (sco_mode == CONNECTED) {
+				force_sco =  0;
+				reconnect = -1;
+			} else reconnect = 0;
+		} 
+		
+		if(((!dr_usage && (force_sco != 1)) || (force_sco == 0)) && (sco_mode == CONNECTED)) {
+			if(verbose) {
+				printf("driver is not in use\n");
+				fflush(stdout);
+			}
+			/* close bt_sco audio handle */
+			bt_sco_set_fd(handle, -1);
+			/* disconnect SCO stream */
+			close(sd);
+			if(verbose) {
+				printf("disconnected SCO channel\n");
+				fflush(stdout);
+			}
+			
+			sco_mode = NOT_CONNECTED;
+		}
+
+		// if a reconnect has been requested, force 
+		// the sco channel to be acquired again
+		if (reconnect == -1) {
+			force_sco = 1;
+			reconnect = 0;
+			sleep(3);
+		}
+
 		if(((dr_usage && (force_sco != 0)) || (force_sco == 1)) && (sco_mode == NOT_CONNECTED)) {
 			if(verbose) {
 				printf("driver is in use\n");
@@ -825,24 +871,6 @@
 				}
 				sco_mode = CONNECTED;
 			}
-			
-		}
-		if(((!dr_usage && (force_sco != 1)) || (force_sco == 0)) && (sco_mode == CONNECTED)) {
-			if(verbose) {
-				printf("driver is not in use\n");
-				fflush(stdout);
-			}
-			/* close bt_sco audio handle */
-			bt_sco_set_fd(handle, -1);
-			/* disconnect SCO stream */
-			close(sd);
-			if(verbose) {
-				printf("disconnected SCO channel\n");
-				fflush(stdout);
-			}
-			
-			sco_mode = NOT_CONNECTED;
-			
 		}
 		
 		if (ring) {

             reply	other threads:[~2005-05-05 18:58 UTC|newest]

Thread overview: 6+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2005-05-05 18:58 Florian Echtler [this message]
2005-05-05 21:09 ` [Bluez-devel] Re: some btsco hacks Brad Midgley
2005-05-06 20:52   ` Florian Echtler
2005-05-09  5:16     ` Brad Midgley
2005-05-09 22:14       ` Florian Echtler
2005-05-09 23:11         ` Brad Midgley

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=Pine.LNX.4.62.0505052044560.10038@block \
    --to=echtler@fs.tum.de \
    --cc=bluez-devel@lists.sourceforge.net \
    --cc=bmidgley@users.sourceforge.net \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox