<<

Playing BlueZ on the D-Bus

Marcel Holtmann BlueZ Project [email protected]

Abstract possible tasks and configuration options are ac- cessed via the D-Bus interface. This will al- low to abstract the internals of GNOME and The integration of the Bluetooth technology KDE applications from any technical details of into the kernel and the major Linux dis- the Bluetooth specification. Even other appli- tributions has progressed really fast over the cation will get access to the Bluetooth technol- last two years. The technology is present al- ogy without any hassle. most everywhere. All modern notebooks and mobile phones are shipped with built-in Blue- tooth. The use of Bluetooth with a Linux based system is easy and in most cases it only needs 1 Introduction an one-time setup, but all the tools are still com- mand line based. In general this is not so bad, but for a greater success it is needed to seam- The Bluetooth specification [1] defines a clear lessly integrate the Bluetooth technology into abstraction layer for accessing different Blue- the desktop. There have been approaches for tooth hardware options. It is called the Host the GNOME and KDE desktops. Both have Controller Interface (HCI) and is the basis of been quite successful and made the use of Blue- all Bluetooth protocols stacks (see Figure 1). tooth easy. The problem however is that both implemented their own framework around the This interface consists of commands and events Bluetooth and its daemons and there that provide support for configuring the local were no possibilities for programs from one device and creating connections to other Blue- system to talk to the other. With the final ver- tooth devices. The commands are split into six sion of the D-Bus framework and its adaption different groups: into the Bluetooth subsystem of Linux, it will be simple to make all applications Bluetooth • Link Control Commands aware. • Link Policy Commands The idea is to establish one central Bluetooth that takes care of all task that can’t or • Host Controller and Baseband Commands shouldn’t be handled inside the Linux kernel. These jobs include PIN code and link key man- • Informational Parameters agement for the authentication and encryption, • Status Parameters caching of device names and services and also central control of the Bluetooth hardware. All • Testing Commands 422 • Playing BlueZ on the D-Bus

code or to notify of a changed power state. Applications and Profiles Every Bluetooth implementation must imple- OBEX ment the Host Controller Interface and for Linux a specific set of commands has been in- SDP RFCOMM tegrated into the Linux kernel. Another set of commands are implemented through the Blue- L2CAP tooth library. And some of the commands are not implemented at all. This is because they are HCI not needed or because they have been depre- cated by the latest Bluetooth specification. The Link Manager range of commands implemented in the kernel are mostly dealing with Bluetooth connection Baseband handling. The commands in the Bluetooth li- brary are for configuration of the local device Radio and handling of authentication and encryption.

While the Host Controller Interface is a clean hardware abstraction, it is not a clean or easy Figure 1: Simple Bluetooth stack programming interface. The Bluetooth library provides an interface to HCI and an applica- With the Link Control Commands it is pos- tion programmer has to a lot of code to sible to search for other Bluetooth devices in get Bluetooth specific tasks done via HCI. To range and to establish connections to other de- make it easy for application programmers and vices. This group also includes commands to also end users, a task based interface to Blue- handle authentication and encryption. The Link tooth has been designed. The definition of this Policy Commands are controlling the estab- tasks has been done from an application per- lished connections between two or more Blue- spective and they are exported through D-Bus tooth devices. They also control the different via methods and . power modes. All local settings of a Blue- tooth device are modified with commands from the Host Controller and Baseband Commands 2 D-Bus integration group. This includes for example the friendly name and the class of device. For detailed in- formation of the local device, the commands The hcid daemon is the main daemon when from the Informational Paramters group can be running Bluetooth on Linux. It handles all de- used. The Status Parameters group provides vice configuration and authentication tasks. All commands for detailed information from the re- configuration is done via a simple configuration mote device. This includes the link quality and file and the PIN code is handled via PIN helper the RSSI value. With the group Testing Com- script. This means that every the configuration mands the device provides commands for Blue- option needed to be changed, it was needed to tooth qualification testing. All commands are edit the configuration file (/etc/bluetooth/ answered by an that returns the requested hcid.conf) and to restart hcid. The config- value or information. Some events can also ar- uration file still configures the basic and also rive at any time. For example to request a PIN default settings of hcid, but with the D-Bus 2006 Linux Symposium, Volume One • 423 integration all other settings are configurable is attached. With ListAdapters() it is pos- through the D-Bus API. The current API con- sible to get a complete list of paths of the at- sists of three interfaces: tached adapters.

If the path is known, it is possible to use the • org.bluez.Manager full Adapter interface to configure the local de- vice or handle tasks like pairing or searching • org.bluez.Adapter for other devices. An example task would be • org.bluez.Security the configuration of the device name. With GetName() the current name can be retrieved and with SetName() it can be changed. The Manager interface provides basic meth- Changing the name results in storing it on the ods for listing all attached adapter and getting filesystem and changing the name with an ap- the default adapter. In the D-Bus API terms propriate HCI command. If the local device al- an adapter is the local Bluetooth device. In ready supports the Bluetooth Lisbon specifica- most cases this might be an USB dongle or tion, then the Extended Inquiry Response will a PCMCIA card. The Adapter interface pro- be also modified. vides methods for configuration of the local device, searching for remote device and han- With the DiscoverDevices() method it dling of remote devices. The Security interface is possible to start the search for other Blue- provides methods to register passkey agents. tooth devices in range. This method call actu- These agents can provide fixed PIN codes, di- ally doesn’t return any remote devices. It only alog boxes or wizards for specific remote de- starts the inquiry procedure of the Bluetooth vices. All Bluetooth applications using the D- chip and every found device is returned via the Bus API don’t have to worry about any Blue- RemoteDeviceFound signal. This allows tooth specific details or details of the Linux spe- all applications to handle new devices even if cific implementation (see Figure 2). the discovery procedure has been initiated by a different application. Besides the provided methods, every interface contains also signals to broadcast changes or events from the HCI. This allows passive ap- plications to get the information without ac- 3 Current status tively interacting with any Bluetooth related task. An example for this would be an applet The methods and signals for the D-Bus API that changes its icon depending on if the local for Bluetooth were chosen very carefully. The device is idle, connected or searching for other goal was to design it with current application devices. needs in mind. It also aims to fulfill the needs of current established desktop frameworks like Every local device is identified by its path. the GNOME Bluetooth subsystem and the KDE For the first Bluetooth adapter, this would Bluetooth framework. So it covers the common be /org/bluez/hci0 and this path will be tasks and on purpose not everything that might used for all methods of the Adapter inter- be possible. The API can be divided into the face. The best way to get this path is to call following sections: DefaultAdapter() from the Manager in- terface. This will always return the current de- fault adapter or in error if no Bluetooth adapter • Local 424 • Playing BlueZ on the D-Bus

applications

org.bluez.Manager org.bluez.Adapter org.bluez.Security hcid

Passkey Manager

Core Manager Adapter Manager Security Manager

Host Controller Interface kernel

Bluetooth Core

Bluetooth Drivers

Figure 2: D-Bus API overview

– version, revision, manufacturer ming languages. With the integration of D- – mode, name, class of device Bus into the Bluetooth subsystem, the use of Bluetooth from various languages becomes re- • Remote ality. The Figure 3 shows an example of chang- My Blue- – version, revision, manufacturer ing the name of the local device into tooth dongle using the Python programming – name, class of device language. – aliases The example in Python is straight forward and – device discovery simple. Using the D-Bus API within a C pro- – pairing, bondings gram is a little bit more complex, but it is still • Security easier than using the native Bluetooth library API. Figure 4 shows an example on how to get – passkey agent the name of the local device.

With these methods and signals all standard tasks are covered. The Manager, Adapter and 5 Conclusion Security interfaces are feature complete at the moment. The integration of a D-Bus API into the Blue- tooth subsystem makes it easy for applications to access the Bluetooth technology. The cur- 4 Example application rent API is a big step into the right direction, but it is still limited. The Bluetooth technology The big advantage of the D-Bus framework is complex and Bluetooth services needs to be is that it has bindings for multiple program- extended with an easy to use D-Bus API. 2006 Linux Symposium, Volume One • 425

#!/usr/bin/python #include #include import dbus #include bus = dbus.SystemBus(); main(int argc, char **argv) { DBusConnection *conn; DBusMessage *msg, *reply; obj = bus.get_object(’org.bluez’, const char *name; ’/org/bluez’) conn = dbus_bus_get(DBUS_BUS_SYSTEM, NULL); manager = dbus.Interface(obj, msg = dbus_message_new_method_call( "org.bluez", ’org.bluez.Manager’) "/org/bluez/hci0", "org.bluez.Adapter", "GetName"); obj = bus.get_object(’org.bluez’, reply = manager.DefaultAdapter()) dbus_connection_send_with_reply_and_block( conn, msg, -1, NULL); adapter = dbus.Interface(obj, ’org.bluez.Adapter’) dbus_message_get_args(reply, NULL, DBUS_TYPE_STRING, &name, DBUS_TYPE_INVALID); adapter.SetName(’My Bluetooth dongle’) printf("%s\n", name);

dbus_message_unref(msg); Figure 3: Example in Python dbus_message_unref(reply); dbus_connection_close(conn);

return 0; } The next steps would be integration of D-Bus into the Bluetooth mouse and keyboard ser- vice. Another goal is the seamless integration Figure 4: Example in C into the Network Manager. This would allow to connect to Bluetooth access points like any other WiFi access point.

The current version of the D-Bus API for Blue- tooth will be used in the next generation of the Maemo platform which is that basis for the Nokia 770 Internet tablet.

References

[1] Special Interest Group Bluetooth: Bluetooth Core Specification Version 2.0 + EDR, November 2004.

[2] freedesktop.org: D-BUS Specification Version 0.11. 426 • Playing BlueZ on the D-Bus Proceedings of the Linux Symposium

Volume One

July 19th–22nd, 2006 Ottawa, Ontario Canada Conference Organizers

Andrew J. Hutton, Steamballoon, Inc. C. Craig Ross, Linux Symposium

Review Committee

Jeff Garzik, Red Hat Software Gerrit Huizenga, IBM Dave Jones, Red Hat Software Ben LaHaise, Intel Corporation Matt Mackall, Selenic Consulting Patrick Mochel, Intel Corporation C. Craig Ross, Linux Symposium Andrew Hutton, Steamballoon, Inc.

Proceedings Formatting Team

John W. Lockhart, Red Hat, Inc. David M. Fellows, Fellows and Carr, Inc. Kyle McMartin

Authors retain copyright to all submitted papers, but have granted unlimited redistribution rights to all as a condition of submission.