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

Collapse All | Expand All

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

Return to bug 52514