View | Details | Raw Unified | Return to bug 52514 | Differences between
and this patch

Collapse All | Expand All

(-)chapter.sgml (+382 lines)
Lines 6687-6692 Link Here
6687
	support AAAA records.</para>
6687
	support AAAA records.</para>
6688
    </sect2>
6688
    </sect2>
6689
  </sect1>
6689
  </sect1>
6690
6691
  <sect1 id="network-bluetooth">
6692
    <sect1info>
6693
      <authorgroup>
6694
        <author>
6695
          <firstname>Pav</firstname>
6696
          <surname>Lucistnik</surname>
6697
          <contrib>Written by </contrib>
6698
          <affiliation>
6699
            <address><email>pav@oook.cz</email></address>
6700
          </affiliation>
6701
        </author>
6702
      </authorgroup>
6703
    </sect1info>
6704
    <title>Bluetooth</title>
6705
    
6706
    <sect2>
6707
      <title>Introduction</title>
6708
      <para>Bluetooth is a wireless technology for creating personal networks
6709
        operating in the 2.4 GHz unlicensed band, with a range of 10 meters.
6710
        Networks are usually formed ad-hoc from portable devices like mobile
6711
        phones, handhelds and laptops.  Unlike the other popular wireless
6712
        technology, Wi-Fi, Bluetooth offers higher level service profiles,
6713
        e.g.  FTP-like file servers, file pushing, voice transport, serial
6714
        line emulation and more.</para>
6715
      
6716
      <para>The Bluetooth stack in FreeBSD is implemented using Netgraph.
6717
        A Broad variety of USB dongles is supported by the &man.ng.ubt.4; driver.
6718
        The 3Com PC Card 3CRWB60-A is supported by the &man.ng.bt3c.4; driver.
6719
        Serial and UART based Bluetooth devices are supported via
6720
        &man.ng.h4.4; and &man.hcseriald.8;.  This chapter describes using
6721
        a USB Bluetooth dongle.  Bluetooth support is available only on 
6722
	FreeBSD 5.0 and newer systems.</para>
6723
    </sect2>
6724
    
6725
    <sect2>
6726
      <title>Plugging in the Device</title>
6727
      <para>Device drivers are by default available as kernel modules.
6728
        Before attaching a device, you need to load the driver into the
6729
	kernel:</para>
6730
      
6731
      <screen>&prompt.root; <userinput>kldload ng_ubt</userinput></screen>
6732
      
6733
      <para>If the Bluetooth device is present in the system during system
6734
        startup, load the module from <filename>/boot/loader.conf</filename>:</para>
6735
        
6736
      <programlisting>ng_ubt_load="YES"</programlisting>
6737
        
6738
      <para>Plug in your USB dongle.  Similar output will appear on the console
6739
        (or in syslog):</para>
6740
      
6741
      <screen>ubt0: vendor 0x0a12 product 0x0001, rev 1.10/5.25, addr 2
6742
ubt0: Interface 0 endpoints: interrupt=0x81, bulk-in=0x82, bulk-out=0x2
6743
ubt0: Interface 1 (alt.config 5) endpoints: isoc-in=0x83, isoc-out=0x3;
6744
      wMaxPacketSize=49; nframes=6, buffer size=294</screen>
6745
      
6746
      <para>Copy <filename>/usr/src/share/examples/netgraph/bluetooth/rc.bluetooth</filename>
6747
        to some convenient place, like <filename>/etc/rc.bluetooth</filename>.
6748
        This script is used to start and stop the Bluetooth stack.  It is a good idea
6749
        to stop the stack before unplugging the device, but it is not (usually)
6750
        fatal.  When starting the stack, you will receive output similar to this:</para>
6751
      
6752
      <screen>&prompt.root; <userinput>/etc/rc.bluetooth start ubt0</userinput>
6753
BD_ADDR: 00:02:72:00:d4:1a
6754
Features: 0xff 0xff 0xf 00 00 00 00 00 
6755
&lt;3-Slot&gt; &lt;5-Slot&gt; &lt;Encryption&gt; &lt;Slot offset&gt;
6756
&lt;Timing accuracy&gt; &lt;Switch&gt; &lt;Hold mode&gt; &lt;Sniff mode&gt;
6757
&lt;Park mode&gt; &lt;RSSI&gt; &lt;Channel quality&gt; &lt;SCO link&gt;
6758
&lt;HV2 packets&gt; &lt;HV3 packets&gt; &lt;u-law log&gt; &lt;A-law log&gt; &lt;CVSD&gt;
6759
&lt;Paging scheme&gt; &lt;Power control&gt; &lt;Transparent SCO data&gt; 
6760
Max. ACL packet size: 192 bytes
6761
Number of ACL packets: 8
6762
Max. SCO packet size: 64 bytes
6763
Number of SCO packets: 8</screen>
6764
      
6765
    </sect2>
6766
    
6767
    <sect2>
6768
      <title>HCI and Inquiry</title>
6769
6770
      <para>Now it is time to discover some nearby bluetooth devices.
6771
        Discovering devices and many other interesting tasks is done with
6772
        the &man.hccontrol.8; utility.  You will receive a list of discoverable
6773
        devices in a few seconds:</para>
6774
6775
      <screen>&prompt.user; <userinput>hccontrol -n ubt0hci inquiry</userinput>
6776
Inquiry result, num_responses=1
6777
Inquiry result #0
6778
        BD_ADDR: 00:80:37:29:19:a4
6779
        Page Scan Rep. Mode: 0x1
6780
        Page Scan Period Mode: 00
6781
        Page Scan Mode: 00
6782
        Class: 52:02:04
6783
        Clock offset: 0x78ef
6784
Inquiry complete. Status: No error [00]</screen>
6785
6786
      <para>BD_ADDR is the unique address of a bluetooth device, similar to MAC
6787
        addresses of network cards.  This address is needed for further
6788
        communication with a device.  Let us try to read the device's name:</para>
6789
6790
      <screen>&prompt.user; <userinput>hccontrol -n ubt0hci remote_name_request 00:80:37:29:19:a4 0 0 0</userinput>
6791
BD_ADDR: 00:80:37:29:19:a4
6792
Name: Pav's T39</screen>
6793
6794
      <para>If you perform a discovery on a different bluetooth device, it will find
6795
        your computer as <quote>your.host.name (ubt0)</quote>.</para>
6796
6797
      <para>You can list active baseband connections:</para>
6798
6799
      <screen>&prompt.user; <userinput>hccontrol -n ubt0hci read_connection_list</userinput>
6800
Remote BD_ADDR    Handle Type Mode Role Encrypt Pending Queue State
6801
00:80:37:29:19:a4     41  ACL    0 MAST    NONE       0     0 OPEN</screen>
6802
6803
      <para>Handle is useful for manually disconnecting a connection:</para>
6804
6805
      <screen>&prompt.root; <userinput>hccontrol -n ubt0hci disconnect 41</userinput>
6806
Connection handle: 41
6807
Reason: Connection terminated by local host [0x16]</screen>
6808
6809
      <para>Refer to <command>hccontrol help</command> for a complete listing of
6810
        available commands.  Note that the majority of commands does not require
6811
        superuser privileges.</para>
6812
                                
6813
    </sect2>
6814
    
6815
    <sect2>
6816
      <title>L2CAP</title>
6817
6818
      <para>L2CAP is a higher level of connection in Bluetooth standards.
6819
        A useful command is &man.l2ping.8;, which can be used to ping
6820
        other devices.  Some devices might not return all of the data
6821
        send to them, so <emphasis>0 bytes</emphasis> as in this example
6822
        is a normal state.</para>
6823
6824
      <screen>&prompt.root; <userinput>l2ping -a 00:80:37:29:19:a4</userinput>
6825
0 bytes from 0:80:37:29:19:a4 seq_no=0 time=48.633 ms result=0 
6826
0 bytes from 0:80:37:29:19:a4 seq_no=1 time=37.551 ms result=0 
6827
0 bytes from 0:80:37:29:19:a4 seq_no=2 time=28.324 ms result=0 
6828
0 bytes from 0:80:37:29:19:a4 seq_no=3 time=46.150 ms result=0</screen>
6829
6830
      <para>The &man.l2control.8; utility is used to configure L2CAP nodes
6831
        and read their state.  This example shows file transfer to a Palm
6832
        handheld:</para>
6833
6834
      <screen>&prompt.user; <userinput>l2control -a 00:02:72:00:d4:1a read_channel_list</userinput>
6835
L2CAP channels:
6836
Remote BD_ADDR     SCID/ DCID   PSM  IMTU/ OMTU State
6837
00:07:e0:00:0b:ca    66/   64     3   132/  672 OPEN
6838
&prompt.user; <userinput>l2control -a 00:02:72:00:d4:1a read_connection_list</userinput>
6839
L2CAP connections:
6840
Remote BD_ADDR    Handle Flags Pending State
6841
00:07:e0:00:0b:ca     41 O           0 OPEN</screen>
6842
6843
      <para>Another diagnostic tool is &man.btsockstat.1;.  It does a similar
6844
        job as &man.netstat.1; does, but for Bluetooth sockets, logical
6845
        connections on top of baseband connections.  The example output shows
6846
        the same connection as l2control above:</para>
6847
6848
      <screen>&prompt.user; <userinput>btsockstat</userinput>
6849
Active L2CAP sockets
6850
PCB      Recv-Q Send-Q Local address/PSM       Foreign address   CID   State
6851
c2afe900      0      0 00:02:72:00:d4:1a/3     00:07:e0:00:0b:ca 66    OPEN
6852
Active RFCOMM sessions
6853
L2PCB    PCB      Flag MTU   Out-Q DLCs State
6854
c2afe900 c2b53380 1    127   0     Yes  OPEN
6855
Active RFCOMM sockets
6856
PCB      Recv-Q Send-Q Local address     Foreign address   Chan DLCI State
6857
c2e8bc80      0    250 00:02:72:00:d4:1a 00:07:e0:00:0b:ca 3    6    OPEN</screen>
6858
6859
    </sect2>
6860
    
6861
    <sect2>
6862
      <title>Pairing of Devices</title>
6863
6864
      <para>By default, Bluetooth communication is not authorized and any device
6865
        can talk to any other device.  Some devices, like mobile phones, require
6866
        authentication for some functionality, like Internet connections.  This
6867
        is done with PIN numbers - you enter the same (up to 16 digits long)
6868
        number on both devices.  This operation is called <emphasis>pairing</emphasis>.
6869
        The daemon that answers pairing requests is &man.hcsecd.8;.  Copy
6870
        <filename>/usr/src/usr.sbin/bluetooth/hcsecd/hcsecd.conf</filename>
6871
        to <filename>/usr/local/etc</filename> and edit it.  The following is an
6872
	example section for a mobile phone, with the PIN arbitrarily set to 1234:</para>
6873
6874
      <programlisting>device {
6875
        bdaddr  00:80:37:29:19:a4;
6876
        name    "Pav's T39";
6877
        key     nokey;
6878
        pin     "1234";
6879
}</programlisting>
6880
6881
      <para>You can choose any PIN you like.  Note that some devices, like
6882
        headsets, have a fixed PIN built in.  Start <command>hcsecd -d</command>.
6883
        The <option>-d</option> switch forces the daemon to stay in the
6884
        terminal and not fork to the background, so we can see what is happening.
6885
        Set the remote device to receive pairing and initiate the HCI connection
6886
        to the remote device.  The remote device should say that pairing was
6887
        accepted, and let you enter the PIN.  Enter the same PIN as you have in your
6888
        <filename>hcsecd.conf</filename>.  Now your PC and remote device are paired.
6889
        Alternatively, you can initiate pairing on the remote device.
6890
        This will appear in the <command>hcsecd</command> output:</para>
6891
6892
<programlisting>hcsecd[16484]: Got Link_Key_Request event from 'ubt0hci', remote bdaddr 0:80:37:29:19:a4
6893
hcsecd[16484]: Found matching entry, remote bdaddr 0:80:37:29:19:a4, name 'Pav's T39', link key doesn't exist
6894
hcsecd[16484]: Sending Link_Key_Negative_Reply to 'ubt0hci' for remote bdaddr 0:80:37:29:19:a4
6895
hcsecd[16484]: Got PIN_Code_Request event from 'ubt0hci', remote bdaddr 0:80:37:29:19:a4
6896
hcsecd[16484]: Found matching entry, remote bdaddr 0:80:37:29:19:a4, name 'Pav's T39', PIN code exists
6897
hcsecd[16484]: Sending PIN_Code_Reply to 'ubt0hci' for remote bdaddr 0:80:37:29:19:a4</programlisting>
6898
6899
    </sect2>
6900
    
6901
    <sect2>
6902
      <title>Service Discovery Protocol (SDP)</title>
6903
      <para>If you want to know which services a Bluetooth device offers, and
6904
        on which RFCOMM channels, build <application>libbluetooth</application>
6905
        and <application>sdp-1.0rc3</application> from <ulink
6906
        url="http://www.geocities.com/m_evmenkin/">Maksim Evmenkin's
6907
        snapshot</ulink>.  Then, run <application>sdptool</application> and
6908
        observe (the output is snipped a bit, as this tool is quite talky):</para>
6909
6910
      <screen>&prompt.root; <userinput>sdptool browse 00:80:37:29:19:a4</userinput>
6911
Browsing 00:80:37:29:19:A4 ...
6912
Service Name: Dial-up Networking
6913
Protocol Descriptor List:
6914
  "L2CAP" (0x0100)
6915
  "RFCOMM" (0x0003)
6916
    Channel: 1
6917
6918
Service Name: Fax
6919
Protocol Descriptor List:
6920
  "L2CAP" (0x0100)
6921
  "RFCOMM" (0x0003)
6922
    Channel: 2
6923
6924
Service Name: Voice gateway
6925
Service Class ID List:
6926
  "Headset Audio Gateway" (0x1112)
6927
  "Generic Audio" (0x1203)
6928
Protocol Descriptor List: 
6929
  "L2CAP" (0x0100)
6930
  "RFCOMM" (0x0003)
6931
    Channel: 3
6932
</screen>
6933
6934
      <para>... and so on.  You will need the channel number later for using
6935
        a given service.  Some devices do not support browsing, they return
6936
        an empty list, but you can try searching for a specific service.</para>
6937
6938
      <screen>&prompt.root; <userinput>sdptool search --bdaddr 00:07:e0:00:0b:ca OPUSH</userinput></screen>
6939
      
6940
      <para>Offering services on FreeBSD to other devices is done using the
6941
        <application>sdpd</application> server.</para>
6942
      <screen>&prompt.root; <userinput>sdpd</userinput></screen>
6943
      
6944
      <para>Registering a given Bluetooth service to a RFCOMM channel number:</para>
6945
      <screen>&prompt.root; <userinput>sdptool add --channel=7 LAN</userinput></screen>
6946
      
6947
      <para>Checking services offered by our computer:</para>
6948
      <screen>&prompt.root; <userinput>sdptool browse ff:ff:ff:00:00:00</userinput></screen>
6949
    </sect2>
6950
6951
    <sect2>
6952
      <title>Dial-up Networking (DUN) and Local Area Network (LAN)</title>
6953
6954
      <para>Bluetooth can be used for connecting to the Internet, either over
6955
        PPP (mobile phones) or the local network (access points).  The Dial-up Networking
6956
        profile on FreeBSD is implemented with &man.ppp.8; and
6957
        &man.rfcomm.pppd.8;, a wrapper that converts RFCOMM Bluetooth connections
6958
        to something ppp can operate with.  Create ppp labels in
6959
        <filename>/etc/ppp/ppp.conf</filename>, examples from the &man.rfcomm.pppd.8;
6960
        manual page can be used.</para>
6961
        
6962
      <para>Connecting to the Internet through a mobile phone (DUN profile).  First, find
6963
        out the correct RFCOMM channel on the remote device using
6964
        <application>sdptool</application>.  Then, use &man.rfcomm.pppd.8;:</para>
6965
6966
      <screen>&prompt.root; <userinput>rfcomm_pppd -a 00:80:37:29:19:a4 -c -C 1 -l rfcomm-dialup</userinput></screen>
6967
      
6968
      <para>Running a Bluetooth access point on FreeBSD.  First, register a
6969
        RFCOMM channel for LAN service on the local <application>sdpd</application>.
6970
        Then, start the ppp server.  Use BD_ADDR of the local Bluetooth device and
6971
        the channel number registered with <application>sdpd</application>.</para>
6972
      
6973
      <screen>&prompt.root; <userinput>rfcomm_pppd -a 00:02:72:00:d4:1a -s -C 7 -l rfcomm-server</userinput></screen>
6974
        
6975
    </sect2>
6976
6977
    <sect2>
6978
      <title>OBEX Push (OPUSH)</title>
6979
      <para>OBEX is a widely used protocol for simple file transfers between
6980
        mobile devices.  It's main use is in infrared communication, where it is
6981
        used for generic file transfers between notebooks or Palm handhelds,
6982
        and for sending business cards or calendar entries between mobile
6983
        phones and other devices with PIM applications.</para>
6984
      
6985
      <para>The OBEX client is implemented in the
6986
        <application>obexapp</application> utility from <ulink                  
6987
        url="http://www.geocities.com/m_evmenkin/">Maksim Evmenkin's
6988
        snapshot</ulink>.  It needs the <application>openobex</application>
6989
        library from same package and the
6990
        <filename role="package">devel/glib12</filename> port.  Note that
6991
        <application>obexapp</application> does not require root privileges
6992
        to operate.</para>
6993
      
6994
      <para>OBEX client.  First, find which channel on the remote device is IrMC
6995
        Synchronization or OBEX Object Push.  After that, use
6996
        <application>obexapp</application>.  Here is an example session where
6997
        we download a file (device info from a mobile phone) and send
6998
        a file (business card to the phone's directory):</para>
6999
        
7000
      <screen>&prompt.user; <userinput>obexapp -a 00:80:37:29:19:a4 -C 10</userinput>
7001
obex&gt; get
7002
get: remote file&gt; telecom/devinfo.txt
7003
get: local file&gt; devinfo-t39.txt
7004
Success, response: OK, Success (0x20)
7005
obex&gt; put
7006
put: local file&gt; new.vcf
7007
put: remote file&gt; new.vcf
7008
Success, response: OK, Success (0x20)
7009
obex&gt; di
7010
Success, response: OK, Success (0x20)</screen>
7011
7012
      <para>OBEX server.  First, register the OPUSH service with the local
7013
        <application>sdpd</application>.  If OPUSH does not work,
7014
        you can try the FTRN service instead.  Then, start the OBEX daemon
7015
        using the channel number registered with sdpd:</para>
7016
      
7017
      <screen>&prompt.root; <userinput>obexapp -s -C 10</userinput></screen>
7018
      
7019
      <para>Received files will appear in <filename>/var/spool/obex</filename>.
7020
        This can be overriden with the <option>-r</option> switch.  Make sure
7021
        the directory exists, <application>obexapp</application> will not
7022
        create it.  On a typical workstation with a single user it is useful
7023
        to set a default owner of received files.  See obexapp(1)
7024
        for details.</para>
7025
      
7026
    </sect2>
7027
7028
    <sect2>
7029
      <title>Serial Port Profile (SP)</title>
7030
      <para>Bluetooth can be used to emulate serial port connections.
7031
        To connect to a remote device, first locate the RFCOMM channel with the
7032
        Serial Port profile.  Then, start the Serial Port Profile Daemon
7033
        &man.rfcomm.sppd.1; with a free pseudo tty:</para>
7034
7035
      <screen>&prompt.root; <userinput>rfcomm_sppd -a 00:07:E0:00:0B:CA -c 1 -t /dev/ttyp6</userinput>
7036
rfcomm_sppd[94692]: Starting on /dev/ttyp6...</screen>
7037
7038
      <para>Now connect this pseudo tty to your actual terminal:</para>
7039
      
7040
      <screen>&prompt.root; <userinput>cu -l ttyp6</userinput></screen>
7041
7042
    </sect2>
7043
    
7044
    <sect2>
7045
      <title>Troubleshooting</title>
7046
      
7047
      <sect3>
7048
        <title>A remote device cannot connect to us</title>
7049
        <para>Some older devices do not support role switching.  By default,
7050
          when FreeBSD is accepting a connection, it tries to switch roles
7051
          to become a master.  Devices which do not support this will not
7052
          be able to connect.  Role switching is performed when a connection
7053
          is being established, so we cannot ask the remote device if it does
7054
          support role switching.  There is a driver option to disable role
7055
          switching on our side:</para>
7056
        <screen>&prompt.root; <userinput>hccontrol -n ubt0hci write_node_role_switch 0</userinput></screen>
7057
      </sect3>
7058
      
7059
      <sect3>
7060
        <title>Something is going wrong, can I see what exactly is happening?</title>
7061
        <para>Yes, you can.  Use the <application>hcidump</application> tool
7062
        from <ulink url="http://www.geocities.com/m_evmenkin/">Maksim Evmenkin's
7063
        snapshot</ulink>, which works much like &man.tcpdump.1;.  You can
7064
        use it to display the content of Bluetooth packets on the terminal
7065
        and to record Bluetooth communication for later analyzation.</para>
7066
      </sect3>
7067
      
7068
    </sect2>
7069
7070
  </sect1>
7071
6690
</chapter>
7072
</chapter>
6691
7073
6692
<!--
7074
<!--

Return to bug 52514