User talk:Gabrys

From Openmoko

(Difference between revisions)
Jump to: navigation, search
(Step three: load ALSA settings to switch codec "mode" to GSM BT)
 
(25 intermediate revisions by one user not shown)
Line 1: Line 1:
= Testing GSM <-> BT =
+
= Testing GSM <-> Bluetooth on FreeRunner =
  
Basics: We need to tell audio codec to route sound from GSM chip to BT chip. Codec is controlled by ALSA and mixer settings.
+
'''Basics:''' We need to tell audio codec to route sound from GSM chip to Bluetooth chip. Codec is controlled by ALSA and mixer settings. The audio chip is described here: http://wiki.openmoko.org/wiki/Neo1973_Audio_Subsystem .
  
Prerequisites: BT device must be paired before the operation.
+
'''Prerequisite:''' Bluetooth device must be paired before the operation.
 +
 
 +
Note: 00:13:17:78:7B:A3 is Bluetooth MAC of my headset, modify accordingly.
  
 
== Step one: powering on bluetooth, setting up bluetoothd, pairing ==
 
== Step one: powering on bluetooth, setting up bluetoothd, pairing ==
Line 9: Line 11:
 
Set /etc/bluetooth/audio.conf like this:
 
Set /etc/bluetooth/audio.conf like this:
  
  # SCO routing. Either PCM or HCI (in which case audio is routed to/from ALSA)   
+
# SCO routing. Either PCM or HCI (in which case audio is routed to/from ALSA)   
  # Defaults to HCI                                                               
+
# Defaults to HCI                                                               
  SCORouting=PCM                                                                   
+
SCORouting=PCM                                                                   
  
 
Then
 
Then
  
  echo 1 > /sys/bus/platform/devices/neo1973-pm-bt.0/power_on
+
echo 1 > /sys/bus/platform/devices/neo1973-pm-bt.0/power_on
  hciconfig hci0 up
+
sleep 1
  bluetoothd
+
bluetoothd
 +
hciconfig hci0 up
  
then use simple-agent from here: http://wiki.openmoko.org/wiki/Manually_using_Bluetooth#Pair to pair the device.
+
Do this ONCE (the prerequisite): use simple-agent from here: http://wiki.openmoko.org/wiki/Manually_using_Bluetooth#Pair to pair the device (or pair in any other valid way).
 +
 
 +
Connect to the headset:
 +
 
 +
BTADAPTER=`dbus-send --system --dest=org.bluez --print-reply / org.bluez.Manager.DefaultAdapter | tail -1 | sed 's/^.*"\(.*\)".*$/\1/'`
 +
dbus-send --system --dest=org.bluez --print-reply $BTADAPTER/dev_00_13_17_78_7B_A3 org.bluez.Headset.Connect
  
 
Test: "hcitool con" should say:
 
Test: "hcitool con" should say:
  
  Connection
+
Connection
      ACL 00:13:17:78:7B:A3 handle 0 state 5 lm MASTER
+
< ACL 00:13:17:78:7B:A3 handle 42 state 1 lm MASTER  
 +
 
 +
Test2:
 +
BTADAPTER=`dbus-send --system --dest=org.bluez --print-reply / org.bluez.Manager.DefaultAdapter | tail -1 | sed 's/^.*"\(.*\)".*$/\1/'`
 +
dbus-send --system --dest=org.bluez --print-reply $BTADAPTER/dev_00_13_17_78_7B_A3 org.bluez.Headset.Connect
 +
 
 +
should say: Error org.bluez.Error.AlreadyConnected: Already Connected
 +
 
 +
 
 +
=== More debug examples ===
 +
 
 +
root@om-gta02 /usr/lib/python2.6/site-packages $ dbus-send --system --dest=org.bluez --print-reply $BTADAPTER/dev_00_13_17_78_7B_A3 org.bluez.Headset.Disconnect
 +
method return sender=:1.18 -> dest=:1.287 reply_serial=2
 +
 +
root@om-gta02 /usr/lib/python2.6/site-packages $ dbus-send --system --dest=org.bluez --print-reply $BTADAPTER/dev_00_13_17_78_7B_A3 org.bluez.Headset.Connect
 +
Error org.bluez.Error.ConnectionAttemptFailed: Input/output error
 +
 +
root@om-gta02 /usr/lib/python2.6/site-packages $ dbus-send --system --dest=org.bluez --print-reply $BTADAPTER/dev_00_13_17_78_7B_A3 org.bluez.Headset.Connect
 +
method return sender=:1.18 -> dest=:1.290 reply_serial=2
 +
 +
root@om-gta02 /usr/lib/python2.6/site-packages $ dbus-send --system --dest=org.bluez --print-reply $BTADAPTER/dev_00_13_17_78_7B_A3 org.bluez.Headset.Play 
 +
method return sender=:1.18 -> dest=:1.292 reply_serial=2
  
 
== Step two: make a call ==
 
== Step two: make a call ==
  
Call to the phone and answer (or place a call, whichever is cheaper).
+
Call to the phone and answer. Calling from FreeRunner seem not to work for me.
  
== Step three: load ALSA settings to switch codec "mode" to GSM <=> BT ==
+
== Step three: load ALSA settings to switch codec "mode" to GSM <=> Bluetooth ==
  
 
Wait till frameworkd sets its statefile and then overwrite it with your own:
 
Wait till frameworkd sets its statefile and then overwrite it with your own:
  
  alsamixer restore /path/to/the/statefile
+
alsactl -f /usr/share/shr/scenarii/gsmbluetooth.state restore
 +
amixer sset "Capture Left Mixer" "Analogue Mix Right"
 +
amixer sset "Capture Left Mixer" "Analogue Mix Left"
 +
 
 +
(last two lines due to kernel bug).
 +
 
 +
Issue dbus call to start exchanging audio between GSM and Bluetooth:
 +
 
 +
BTADAPTER=`dbus-send --system --dest=org.bluez --print-reply / org.bluez.Manager.DefaultAdapter | tail -1 | sed 's/^.*"\(.*\)".*$/\1/'`
 +
dbus-send --system --dest=org.bluez --print-reply $BTADAPTER/dev_00_13_17_78_7B_A3 org.bluez.Headset.Play
 +
 
 +
Set bluetooth IIS to 8000 rate and keep it with a sleep in Python:
 +
 
 +
python set_pcm.py
 +
 
 +
Where the set_pcm.py is:
 +
 
 +
import alsaaudio
 +
import time
 +
 +
pcm_play = alsaaudio.PCM( alsaaudio.PCM_PLAYBACK, alsaaudio.PCM_NONBLOCK, "hw:0,1" )
 +
pcm_play.setchannels(1)
 +
pcm_play.setrate(8000)
 +
pcm_play.setformat(alsaaudio.PCM_FORMAT_S16_LE)
 +
pcm_play.setperiodsize(500000)
 +
 +
pcm_cap = alsaaudio.PCM( alsaaudio.PCM_CAPTURE, alsaaudio.PCM_NONBLOCK, "hw:0,1" )
 +
pcm_cap.setchannels(1)
 +
pcm_cap.setrate(8000)
 +
pcm_cap.setformat(alsaaudio.PCM_FORMAT_S16_LE)
 +
pcm_cap.setperiodsize(500000)
 +
 +
while True:
 +
    time.sleep(999) # wait till user quits this nasty program
 +
 
 +
Now Bluetooth should work. Don't touch the on-screen sliders though, because it will change ALSA settings and you'll lose Bluetooth connection.
  
Then due to kernel bug, do:
+
After that you should have the following command output:
  
  amixer sset "Capture Left Mixer" "Analogue Mix Right"
+
root@om-gta02 ~ $ hcitool con
  amixer sset "Capture Left Mixer" "Analogue Mix Left"
+
Connections:
 +
< SCO 00:13:17:78:7B:A3 handle 45 state 1 lm SLAVE
 +
< ACL 00:13:17:78:7B:A3 handle 42 state 1 lm MASTER
  
Then issue dbus call (check what it is exactly):
+
= Final thoughts =
  
  org.bluez.Headset Play()
+
In 2.6.29 kernel Bluetooth headset won't work for outgoing call before you receive a call using Bluetooth. After receiving call with a Bluetooth headset, you can do both receiving and outgoing calls with the headset. This seems fixed in 2.6.32 -- apply the "fix" and "workaround" described in this page and you can both call and receive calls using BT.
  
We would also need to set bluetooth IIS to 8000 rate (probably left by now).
+
Running a program that keeps the device open consumes some energy, so you normally only want to do this stuff when actually using Bluetooth headset for calling/receiving a call.
  
We should hear something in the BT headset and not in phone's speaker.
+
This fix should already be incorporated to SHR and other FSO-based distributions, but small testing is done on this, so I decided to make fix standalone (so that every distribution can use it). Consult http://github.com/Gabrys/gta02-gsm-bt-fix for more details.

Latest revision as of 17:51, 24 July 2010

Contents

[edit] Testing GSM <-> Bluetooth on FreeRunner

Basics: We need to tell audio codec to route sound from GSM chip to Bluetooth chip. Codec is controlled by ALSA and mixer settings. The audio chip is described here: http://wiki.openmoko.org/wiki/Neo1973_Audio_Subsystem .

Prerequisite: Bluetooth device must be paired before the operation.

Note: 00:13:17:78:7B:A3 is Bluetooth MAC of my headset, modify accordingly.

[edit] Step one: powering on bluetooth, setting up bluetoothd, pairing

Set /etc/bluetooth/audio.conf like this:

# SCO routing. Either PCM or HCI (in which case audio is routed to/from ALSA)   
# Defaults to HCI                                                               
SCORouting=PCM                                                                  

Then

echo 1 > /sys/bus/platform/devices/neo1973-pm-bt.0/power_on
sleep 1
bluetoothd
hciconfig hci0 up

Do this ONCE (the prerequisite): use simple-agent from here: http://wiki.openmoko.org/wiki/Manually_using_Bluetooth#Pair to pair the device (or pair in any other valid way).

Connect to the headset:

BTADAPTER=`dbus-send --system --dest=org.bluez --print-reply / org.bluez.Manager.DefaultAdapter | tail -1 | sed 's/^.*"\(.*\)".*$/\1/'`
dbus-send --system --dest=org.bluez --print-reply $BTADAPTER/dev_00_13_17_78_7B_A3 org.bluez.Headset.Connect

Test: "hcitool con" should say:

Connection
	< ACL 00:13:17:78:7B:A3 handle 42 state 1 lm MASTER 

Test2:

BTADAPTER=`dbus-send --system --dest=org.bluez --print-reply / org.bluez.Manager.DefaultAdapter | tail -1 | sed 's/^.*"\(.*\)".*$/\1/'`
dbus-send --system --dest=org.bluez --print-reply $BTADAPTER/dev_00_13_17_78_7B_A3 org.bluez.Headset.Connect

should say: Error org.bluez.Error.AlreadyConnected: Already Connected


[edit] More debug examples

root@om-gta02 /usr/lib/python2.6/site-packages $ dbus-send --system --dest=org.bluez --print-reply $BTADAPTER/dev_00_13_17_78_7B_A3 org.bluez.Headset.Disconnect
method return sender=:1.18 -> dest=:1.287 reply_serial=2

root@om-gta02 /usr/lib/python2.6/site-packages $ dbus-send --system --dest=org.bluez --print-reply $BTADAPTER/dev_00_13_17_78_7B_A3 org.bluez.Headset.Connect
Error org.bluez.Error.ConnectionAttemptFailed: Input/output error

root@om-gta02 /usr/lib/python2.6/site-packages $ dbus-send --system --dest=org.bluez --print-reply $BTADAPTER/dev_00_13_17_78_7B_A3 org.bluez.Headset.Connect
method return sender=:1.18 -> dest=:1.290 reply_serial=2

root@om-gta02 /usr/lib/python2.6/site-packages $ dbus-send --system --dest=org.bluez --print-reply $BTADAPTER/dev_00_13_17_78_7B_A3 org.bluez.Headset.Play   
method return sender=:1.18 -> dest=:1.292 reply_serial=2

[edit] Step two: make a call

Call to the phone and answer. Calling from FreeRunner seem not to work for me.

[edit] Step three: load ALSA settings to switch codec "mode" to GSM <=> Bluetooth

Wait till frameworkd sets its statefile and then overwrite it with your own:

alsactl -f /usr/share/shr/scenarii/gsmbluetooth.state restore
amixer sset "Capture Left Mixer" "Analogue Mix Right"
amixer sset "Capture Left Mixer" "Analogue Mix Left"

(last two lines due to kernel bug).

Issue dbus call to start exchanging audio between GSM and Bluetooth:

BTADAPTER=`dbus-send --system --dest=org.bluez --print-reply / org.bluez.Manager.DefaultAdapter | tail -1 | sed 's/^.*"\(.*\)".*$/\1/'`
dbus-send --system --dest=org.bluez --print-reply $BTADAPTER/dev_00_13_17_78_7B_A3 org.bluez.Headset.Play

Set bluetooth IIS to 8000 rate and keep it with a sleep in Python:

python set_pcm.py

Where the set_pcm.py is:

import alsaaudio
import time

pcm_play = alsaaudio.PCM( alsaaudio.PCM_PLAYBACK, alsaaudio.PCM_NONBLOCK, "hw:0,1" )
pcm_play.setchannels(1)
pcm_play.setrate(8000)
pcm_play.setformat(alsaaudio.PCM_FORMAT_S16_LE)
pcm_play.setperiodsize(500000)

pcm_cap = alsaaudio.PCM( alsaaudio.PCM_CAPTURE, alsaaudio.PCM_NONBLOCK, "hw:0,1" )
pcm_cap.setchannels(1)
pcm_cap.setrate(8000)
pcm_cap.setformat(alsaaudio.PCM_FORMAT_S16_LE)
pcm_cap.setperiodsize(500000)

while True:
    time.sleep(999) # wait till user quits this nasty program

Now Bluetooth should work. Don't touch the on-screen sliders though, because it will change ALSA settings and you'll lose Bluetooth connection.

After that you should have the following command output:

root@om-gta02 ~ $ hcitool con
Connections:
	< SCO 00:13:17:78:7B:A3 handle 45 state 1 lm SLAVE 
	< ACL 00:13:17:78:7B:A3 handle 42 state 1 lm MASTER

[edit] Final thoughts

In 2.6.29 kernel Bluetooth headset won't work for outgoing call before you receive a call using Bluetooth. After receiving call with a Bluetooth headset, you can do both receiving and outgoing calls with the headset. This seems fixed in 2.6.32 -- apply the "fix" and "workaround" described in this page and you can both call and receive calls using BT.

Running a program that keeps the device open consumes some energy, so you normally only want to do this stuff when actually using Bluetooth headset for calling/receiving a call.

This fix should already be incorporated to SHR and other FSO-based distributions, but small testing is done on this, so I decided to make fix standalone (so that every distribution can use it). Consult http://github.com/Gabrys/gta02-gsm-bt-fix for more details.

Personal tools

Testing GSM <-> BT

Basics: We need to tell audio codec to route sound from GSM chip to BT chip. Codec is controlled by ALSA and mixer settings.

Prerequisites: BT device must be paired before the operation.

Step one: powering on bluetooth, setting up bluetoothd, pairing

Set /etc/bluetooth/audio.conf like this:

 # SCO routing. Either PCM or HCI (in which case audio is routed to/from ALSA)   
 # Defaults to HCI                                                               
 SCORouting=PCM                                                                  

Then

 echo 1 > /sys/bus/platform/devices/neo1973-pm-bt.0/power_on
 hciconfig hci0 up
 bluetoothd

then use simple-agent from here: http://wiki.openmoko.org/wiki/Manually_using_Bluetooth#Pair to pair the device.

Test: "hcitool con" should say:

 Connection
     ACL 00:13:17:78:7B:A3 handle 0 state 5 lm MASTER

Step two: make a call

Call to the phone and answer (or place a call, whichever is cheaper).

Step three: load ALSA settings to switch codec "mode" to GSM <=> BT

Wait till frameworkd sets its statefile and then overwrite it with your own:

 alsamixer restore /path/to/the/statefile

Then due to kernel bug, do:

 amixer sset "Capture Left Mixer" "Analogue Mix Right"
 amixer sset "Capture Left Mixer" "Analogue Mix Left"

Then issue dbus call (check what it is exactly):

 org.bluez.Headset Play()

We would also need to set bluetooth IIS to 8000 rate (probably left by now).

We should hear something in the BT headset and not in phone's speaker.