{"id":996,"date":"2022-05-22T02:47:37","date_gmt":"2022-05-22T06:47:37","guid":{"rendered":"https:\/\/jausoft.com\/blog\/?p=996"},"modified":"2022-05-22T22:39:43","modified_gmt":"2022-05-23T02:39:43","slug":"direct-bt-bluetooth-server-and-client-programming-in-cpp-and-java_pt1","status":"publish","type":"post","link":"https:\/\/jausoft.com\/blog\/2022\/05\/22\/direct-bt-bluetooth-server-and-client-programming-in-cpp-and-java_pt1\/","title":{"rendered":"Direct-BT, Bluetooth Server and Client Programming in C++ and Java (Part 1)"},"content":{"rendered":"<p>This is the first article covering <a href=\"https:\/\/jausoft.com\/cgit\/direct_bt.git\/about\/\"><em>Direct-BT<\/em><\/a>\u00a0 using version 2.7.1 and may give you a little introduction into this project.<\/p>\n<p>See <a href=\"https:\/\/jausoft.com\/blog\/2022\/05\/22\/direct-bt-implementation-details-pt1\/\">Direct-BT C++ Implementation Details (Part 1)<\/a> for some insight view.<\/p>\n<h2>Overview<\/h2>\n<p><a href=\"https:\/\/jausoft.com\/cgit\/direct_bt.git\/about\/\"><em>Direct-BT<\/em><\/a> provides direct <a href=\"https:\/\/www.bluetooth.com\/specifications\/bluetooth-core-specification\/\">Bluetooth LE and BREDR<\/a> programming, offering robust high-performance support for embedded &amp; desktop with zero overhead via C++ and Java.<\/p>\n<p>It supports a fully event driven workflow from adapter management, device discovery to GATT programming, using its platform agnostic HCI, L2CAP, SMP and GATT protocol implementation.<\/p>\n<p>Multiple Bluetooth adapter are handled, as well as multiple concurrent connections per adapter.<\/p>\n<p>Peripheral server device programming is supported as well as the central client, which is also used for <a href=\"http:\/\/jausoft.goethel.localnet\/cgit\/direct_bt.git\/tree\/trial\/java\/trial\/org\/direct_bt\">Java<\/a> and <a href=\"http:\/\/jausoft.goethel.localnet\/cgit\/direct_bt.git\/tree\/trial\/direct_bt\">C++ self unit testing<\/a> across two or more Bluetooth adapter.<!--more--><\/p>\n<p><em>LE legacy pairing<\/em> as well as <em>SMP LE Secure Connections <\/em>is fully supported.<\/p>\n<p>Only BREDR support is currently incomplete and not yet usable, even though the API has been prepared.<\/p>\n<p>Direct-BT&#8217;s <a href=\"https:\/\/jausoft.com\/projects\/direct_bt\/build\/documentation\/cpp\/html\/index.html#direct_bt_apidoc\">API and implementation<\/a> closely follows and references the <a href=\"https:\/\/www.bluetooth.com\/specifications\/bluetooth-core-specification\/\">official Bluetooth Specification<\/a>.<\/p>\n<p>Direct-BT has been used successfully in a medical trial, as well as in a <a href=\"https:\/\/www.zafena.se\/en\/product\/zafena-552-poc-workstation\/\">connected medical device application<\/a>.<\/p>\n<p>The <a href=\"https:\/\/jausoft.com\/cgit\/jaulib.git\/about\/\">Jau C++ and Java support library<\/a> has been extracted to encapsulate its generic use-cases and some of its artifacts are discussed here in the implementation section.<\/p>\n<h2>Repeater, a Bluetooth LE Proxy \/ Forwarder<\/h2>\n<p>The <a href=\"https:\/\/jausoft.com\/projects\/direct_bt\/build\/documentation\/cpp\/html\/dbt_repeater00_8cpp-example.html\">provided dbt_repeater<\/a> application allows to connect between a Bluetooth client and server to analyze their protocol.<\/p>\n<p>In detail, <em>dbt_repeater<\/em> connects to a given peripheral-server device and thereafter advertises itself using same advertising and scan-response properties except the device-name to a given central-client.<br \/>\nAfter the central-client connected to <em>dbt_repeater<\/em>, all GATT requests are forwarded to the given peripheral-server and their replies are sent back.<br \/>\nHence repeater requires two Bluetooth LE adapter to simultaneously maintain its client &amp; server connection.<\/p>\n<p>Besides (GATT) protocol analysis of 3rd party devices, <em>dbt_repeater<\/em> is also useful to extend peripheral-server device&#8217;s range. A third application of this idea, using TCP\/IP forwarding in-between a split-repeater is under investigation. The latter would surely be a limitless range extender as well as ensuring operation without radio interference.<\/p>\n<h2 id=\"direct-bt-origins\">Direct-BT Origins<\/h2>\n<p><em>Direct-BT<\/em> development started around April 2020, initially as an alternative <em>TinyB<\/em> Java-API implementation.<\/p>\n<p>The work was motivated due to strict performance, discovery- and connection timing requirements, as well as being able to handle multiple devices concurrently using a real-time event driven low-overhead architecture.<\/p>\n<p>Zafena&#8217;s <a href=\"https:\/\/www.zafena.se\/en\/product\/zafena-552-poc-workstation\/\">POC-Workstation<\/a> was originally implemented using <em>TinyB<\/em> and hence the D-Bus layer to the <em>BlueZ<\/em> client library.<\/p>\n<p>Real time knowledge when devices are discovered and connected were not available and <em>cloaked<\/em> by the caching mechanism. Advertising package details were not exposed.<\/p>\n<p>Connections attempts often took up to 10 seconds to be completed. Detailed information from the <em>Bluetooth<\/em> layer were inaccessible including detailed error states.<\/p>\n<p>Fine grained control about discovery and connection parameter were not exposed by the D-Bus API and hence <em>TinyB<\/em>.<\/p>\n<p>In January 2020 we tried to remedy certain aspects to meet our goals, but concluded to require direct <em>Bluetooth<\/em> control via the <em>BlueZ<\/em>\/<em>Linux<\/em> kernel implementation.<\/p>\n<p><em>Direct-BT<\/em> was born.<\/p>\n<p>We then implemented data types for<\/p>\n<ul>\n<li><em>HCI Packets<\/em> to handle HCI communication with the adapter<\/li>\n<li><em>Mgmt Packets<\/em> to support <em>BlueZ<\/em>\/Linux communication<\/li>\n<li><em>ATT PDU Messages<\/em> to handle GATT communication with the remote device<\/li>\n<li><em>SMP Packets<\/em> to implement <em>Secure Connections (SC)<\/em> and <em>Legacy pairing<\/em>.<\/li>\n<\/ul>\n<p>Last but not least we added<\/p>\n<ul>\n<li><em>Bluetooth<\/em> version 5 support<\/li>\n<li><em>GATT-Server<\/em> support to enable implementing <em>peripheral<\/em> devices, as well as to allow self-testing of <em>Direct-BT<\/em>.<\/li>\n<\/ul>\n<p>Today, <em>Direct-BT<\/em>&#8216;s C++ and Java API match 1:1 and shall not contain legacy API artifacts.<\/p>\n<h2 id=\"required-permissions-for-direct-bt-applications\">Required Permissions for Direct-BT Applications<\/h2>\n<p>As described above, we were required to utilize the host Bluetooth implementation in the GNU\/Linux kernel, i.e. BlueZ\/Kernel directly without D-Bus to achieve best performance and access the native HCI, L2CAP\/GATT and SMP communication channels directly. Such direct access requires root permissions to certain Bluetooth network device facilities, non-root user require to be granted such permissions.<\/p>\n<p>For GNU\/Linux, these permissions are called <a href=\"https:\/\/linux.die.net\/man\/7\/capabilities\">capabilities<\/a>. The following capabilites are required<\/p>\n<ul>\n<li><em>CAP_NET_RAW<\/em> (Raw HCI access)<\/li>\n<li><em>CAP_NET_ADMIN<\/em> (Additional raw HCI access plus (re-)setting the adapter etc)<\/li>\n<\/ul>\n<p>For details you may <a href=\"https:\/\/jausoft.com\/cgit\/direct_bt.git\/about\/#required-permissions-for-direct-bt-applications\">continue reading here<\/a>.<\/p>\n<h2>Unit-Testing Trials with two Bluetooth Adapter<\/h2>\n<p>The <em>trial<\/em> tests utilize one or more actual Bluetooth adapter, hence using the <em>capsh<\/em> launch for the required permissions as described above. Therefor, <em>sudo<\/em> will be called and a user interaction to enter the <em>sudo<\/em> password may occur.<\/p>\n<p>The <em>trial<\/em> tests cover <em>Direct-BT<\/em>&#8216;s Bluetooth functionality, having its <em>master\/client<\/em> and <em>slave\/server peripheral<\/em> facilities communicating via actual adapter, supporting regression testing of the API, its implementation and adapter.<\/p>\n<p>The tests are implemented in both, C++ and Java. The C++ unit tests are also being used for <em>valgrind<\/em> memory leak and data race validation. At this point we are free of leaks and use-after-free issues.<\/p>\n<h2 id=\"tested-bluetooth-adapter\">Tested Bluetooth Adapter<\/h2>\n<ul>\n<li>Bluetooth 4.0\n<ul>\n<li>Intel Bluemoon Bluetooth Adapter (Internal, ID: 8087:0a2a) <em>OK<\/em><\/li>\n<li>Intel Wireless (Internal, ID: 8087:07dc) <em>OK<\/em><\/li>\n<li>CSR Bluetooth Adapter (USB-A, ID: 0a12:0001, CSR8510) <em>OK<\/em><\/li>\n<li>Raspberry Pi Bluetooth Adapter (Internal, BCM43455 on 3+, 4) <em>OK<\/em><\/li>\n<li>Asus BT-400 Broadcom BCM20702A Bluetooth (USB-A, ID 0b05:17cb, BCM20702A1) <em>OK<\/em><\/li>\n<li>Broadcom Corp. BCM2046B1, part of BCM2046 Bluetooth (Internal, ID 0a5c:4500) <em>OK<\/em><\/li>\n<\/ul>\n<\/li>\n<li>Bluetooth 5.0\n<ul>\n<li>Intel AX200 (Internal, ID 8087:0029) <em>OK<\/em><\/li>\n<li>Intel AX201 (Internal, ID 8087:0026) <em>OK<\/em><\/li>\n<li>Asus BT-500 (USB-A, ID 0b05:190e, RTL8761BU) <em>OK on Debian12\/Kernel 5.14)<\/em><\/li>\n<li>Realtek RTL8761BU <em>OK<\/em> (May need manual power-up, depending on firmware)<\/li>\n<\/ul>\n<\/li>\n<\/ul>\n<p>Please check the <a href=\"https:\/\/jausoft.com\/cgit\/direct_bt.git\/about\/doc\/adapter\/adapter.md\">adapter list<\/a> for more details.<\/p>\n<h2 id=\"supported-platforms\">Supported Compiler and OpenJDK<\/h2>\n<p>OpenJDK 11 and OpenJDK 17 have been used to pass unit testing.<\/p>\n<p>GCC &gt;= 8.3.0 and clang &gt;= 10.0 define the minimum C++ compiler versions used.<\/p>\n<h2 id=\"supported-platforms\">Supported Platforms<\/h2>\n<p>The following <strong>platforms<\/strong> are tested and hence supported<\/p>\n<p><strong>Debian 12 Bookworm (GNU\/Linux)<\/strong><\/p>\n<ul>\n<li>amd64 (validated, Generic)<\/li>\n<\/ul>\n<p><strong>Debian 11 Bullseye (GNU\/Linux)<\/strong><\/p>\n<ul>\n<li>amd64 (validated, Generic)<\/li>\n<li>arm64 (should work, Raspberry Pi 3+ and 4)<\/li>\n<li>arm32 (should work, Raspberry Pi 3+ and 4)<\/li>\n<\/ul>\n<p><strong>Debian 10 Buster (GNU\/Linux)<\/strong><\/p>\n<ul>\n<li>amd64 (validated, Generic)<\/li>\n<li>arm64 (validated, Raspberry Pi 3+ and 4)<\/li>\n<li>arm32 (validated, Raspberry Pi 3+ and 4)<\/li>\n<li>potential issues with <em>capsh<\/em>, see below.<\/li>\n<\/ul>\n<p><strong>Ubuntu 20.04 (GNU\/Linux)<\/strong><\/p>\n<ul>\n<li>amd64 (validated, Generic)<\/li>\n<\/ul>\n<p><strong>Ubuntu 18.04 (GNU\/Linux)<\/strong><\/p>\n<ul>\n<li>amd64 (validated, Generic)<\/li>\n<li>potential issues with <em>capsh<\/em>, see above.<\/li>\n<\/ul>\n<h2>Next &#8230;.<\/h2>\n<p>In part 2 I like to show how to actually use Direct-BT, comparing its C++ and Java examples for client- and server mode programming.<\/p>\n<p>In parallel, you might like to read <a href=\"https:\/\/jausoft.com\/blog\/2022\/05\/22\/direct-bt-implementation-details-pt1\/\">Direct-BT Implementation Details (Part 1)<\/a>.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>This is the first article covering Direct-BT\u00a0 using version 2.7.1 and may give you a little introduction into this project. See Direct-BT C++ Implementation Details (Part 1) for some insight view. Overview Direct-BT provides direct Bluetooth LE and BREDR programming, offering robust high-performance support for embedded &amp; desktop with zero overhead via C++ and Java.&hellip; <a class=\"more-link\" href=\"https:\/\/jausoft.com\/blog\/2022\/05\/22\/direct-bt-bluetooth-server-and-client-programming-in-cpp-and-java_pt1\/\">Continue reading <span class=\"screen-reader-text\">Direct-BT, Bluetooth Server and Client Programming in C++ and Java (Part 1)<\/span> <span class=\"meta-nav\" aria-hidden=\"true\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"inline_featured_image":false,"footnotes":""},"categories":[49,59,3,50,58,41],"tags":[30,53,51,52,13,16,33],"class_list":["post-996","post","type-post","status-publish","format-standard","hentry","category-bluetooth","category-c-language","category-computer-stuff","category-direct_bt","category-computer-languages","category-security","tag-arm","tag-bluetooth","tag-c","tag-direct-bt","tag-embedded-device","tag-java","tag-linux"],"_links":{"self":[{"href":"https:\/\/jausoft.com\/blog\/wp-json\/wp\/v2\/posts\/996","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/jausoft.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/jausoft.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/jausoft.com\/blog\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/jausoft.com\/blog\/wp-json\/wp\/v2\/comments?post=996"}],"version-history":[{"count":11,"href":"https:\/\/jausoft.com\/blog\/wp-json\/wp\/v2\/posts\/996\/revisions"}],"predecessor-version":[{"id":1025,"href":"https:\/\/jausoft.com\/blog\/wp-json\/wp\/v2\/posts\/996\/revisions\/1025"}],"wp:attachment":[{"href":"https:\/\/jausoft.com\/blog\/wp-json\/wp\/v2\/media?parent=996"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/jausoft.com\/blog\/wp-json\/wp\/v2\/categories?post=996"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/jausoft.com\/blog\/wp-json\/wp\/v2\/tags?post=996"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}