Add support for Multi-A2DP state machines per device

* Reimplement most of the BTIF AV state machinery in C++
 - Separate the BTIF AV Source from BTIF AV Sink state:
   btif_av_source and btif_av_sink
 - Remove the single btif_av_cb control block instance
 - Add C++ based BtifStateMachine and remove the older C-based
   btif_sm state machine
 - Introduce C++ class BtifAvPeer to keep state per peer, and use
   a single state machine instance per peer
 - Update BTA_AvStart() and BTA_AvStop() to take tBTA_AV_HNDL handle
   argument
 - Register the BTA handles when enabling the Source/Sink service.
   This needs to be done in advance during service startup, otherwise
   a connection to a remote device will fail.
 - Move local event_handler functions inside C++ ProcessEvent methods,
   and pocess the BTIF, BTA AV and BTA AVRCP events per state machine
   instance
 - Cleanup callbacks and use do_in_jni_thread() to schedule the processing
 - Add BTIF AV API that needs to be called by the AVRCP module for certain
   AVRCP events: btif_av_avrcp_event_open(), btif_av_avrcp_event_close()
   btif_av_avrcp_event_remote_play()
 - Add a mechanism to set the active device
 - Add BluetoothDevice argument to the codec-related internal Binder
   APIs: getCodecStatus(), enableOptionalCodecs(),
   disableOptionalCodecs(), setCodecConfigPreference()
 - Rename btif_av_move_idle() to btif_av_acl_disconnected() and
   refactor the processing to happen within each state machine on the
   correct thread.
 - Process cleanup_src and cleanup_sink on the JNI thread
 - Add a mechanism to track the maximum number of connected devices.
 - Add unit tests for the new BtifStateMachine class

* Redesign some of the AVDTP internals so it can handle multiple connected
  devices
 - Change some of the AVDTP struct entries to C++ classes.
   This simplifies significantly the design fixes
 - Reorganize the AvdtpScb entries: for each connection the
   corresponding SEP entries are within the corresponding AvdtpCcb entry.
 - Pass peer address as argument to the API functions inside bta_av_co.cc,
   and update the peer address inside the corresponding peer entry in
   bta_av_co.cc
 - Add various log mesages
 - Store precomputed BTA AV SCB index in the AvdtpCcb entry and use it as
   appropriate.
 - Fix the TCID computation and handling for the AvdtpAdaptationLayer
 - Fix the computed value for AVDT_NUM_RT_TBL
 - Remove video-related code inside AVDTP
 - Refactor/cleanup hard-coded callbacks that depend on the BTA_AV_NUM_STRS
   value.
 - Update various #define values to support a larger number of connected
   devices:
   - Maximum number of streams:
     BTA_AV_NUM_STRS: 2 -> 6
   - BT_RC_NUM_APP: 1 -> 12 (AVRCP-related - 2 * MaxDevices)
   - MAX_L2CAP_LINKS: 7 -> 13
   - MAX_L2CAP_CHANNELS: 16 -> 32
   - AVDT_NUM_LINKS: 2 -> 6
   - AVDT_NUM_SEPS: (6 * AVDT_NUM_LINKS) -> 6
     Now the value is used for the maximum number of SEPs per device
   - AVDT_NUM_TC_TBL: 6 -> (AVDT_NUM_SEPS + AVDT_NUM_LINKS)
   - AVCT_NUM_LINKS: 2 -> 6
   - AVCT_NUM_CONN: 3 -> 14 (2 * MaxDevices + 2)

Also:
 - Update the JNI codec-related calls to use const reference
   instead of a pointer
 - Update the implementation of bta_av_chk_2nd_start() so it is aligned
   with bta_av_chk_start() and cleaned up both functions.
 - Rename btif_dispatch_sm_event() to btif_av_dispatch_sm_event() and
   btif_report_source_codec_state() to btif_av_report_source_codec_state()
   for consistency with the rest of the BTIF AV API.
 - Add new function btif_rc_is_connected_peer() and remove
   btif_rc_get_connected_peer()
 - Add new AVRCP header file btif/include/btif_rc.h
   and move the AVRCP "extern" declarations from btif_av.cc there.
 - Rename btif_av_execute_service() to btif_av_source_execute_service()
 - Cleanup the btif_av.h API descriptions
 - Print the BTIF AV state in the "dumpsys bluetooth_manager" output
 - Print the BTA AV state in the "dumpsys bluetooth_manager" output
 - Print the AVDTP state in the "dumpsys bluetooth_manager" output
 - Refactor btif_a2dp_source_cb into a class with internal state.
 - Refactor A2DP Source worker thread handling and replace it with
   libchrome message handler
 - Refactor BtaAvCo component (bta_av_co.cc)
   - C++ classes and state
   - Rename bta_av_co_audio_src_data_path to
     bta_av_co_audio_source_data_path
   - Remove most BTA_AV_CO_CP_SCMS_T checks
   - Replace mutex_global_lock() usage with local mutex
   - Keep codec-specific state per peer
   - Keep state about the active peer - the first connected peer is the
     default active peer
   - Report source codec state only for valid peers; i.e., don't report
     source codec state for empty RawAddress
   - Keep the contect_protect_active flag per peer
   - Print the BTA AV CO state in the dumpsys bluetooth_manager" output
 - Misc cleanup in BTA AV
 - Add ToString() method to struct btav_a2dp_codec_config_t
 - Additional cleanup

Bug: 70350399
Test: Manual and unit tests
Change-Id: Icecd7fd44a222d939b63a7473a2239ae0679f08c
Merged-In: Icecd7fd44a222d939b63a7473a2239ae0679f08c
(cherry picked from commit c7242818d4180dec4eae2e75f9fb91f7f6a160aa)
68 files changed
tree: 5fbb0fdf1bbcee74ed698e03cd6e5ec3bb8d241a
  1. audio_a2dp_hw/
  2. binder/
  3. bta/
  4. btcore/
  5. btif/
  6. build/
  7. conf/
  8. device/
  9. doc/
  10. embdrv/
  11. hci/
  12. include/
  13. internal_include/
  14. main/
  15. osi/
  16. service/
  17. stack/
  18. test/
  19. tools/
  20. types/
  21. udrv/
  22. utils/
  23. vendor_libs/
  24. vnd/
  25. .clang-format
  26. .gitignore
  27. .gn
  28. Android.bp
  29. Android.mk
  30. BUILD.gn
  31. CleanSpec.mk
  32. EventLogTags.logtags
  33. MODULE_LICENSE_APACHE2
  34. NOTICE
  35. OWNERS
  36. PREUPLOAD.cfg
  37. README.md
README.md

Fluoride Bluetooth stack

Building and running on AOSP

Just build AOSP - Fluoride is there by default.

Building and running on Linux

Instructions for Ubuntu, tested on 14.04 with Clang 3.5.0 and 16.10 with Clang 3.8.0

Download source

mkdir ~/fluoride
cd ~/fluoride
git clone https://android.googlesource.com/platform/system/bt

Install dependencies (require sudo access):

cd ~/fluoride/bt
build/install_deps.sh

Then fetch third party dependencies:

cd ~/fluoride/bt
mkdir third_party
cd third_party
git clone https://github.com/google/googletest.git
git clone https://android.googlesource.com/platform/external/aac
git clone https://android.googlesource.com/platform/external/libchrome
git clone https://android.googlesource.com/platform/external/libldac
git clone https://android.googlesource.com/platform/external/modp_b64
git clone https://android.googlesource.com/platform/external/tinyxml2

And third party dependencies of third party dependencies:

cd fluoride/bt/third_party/libchrome/base/third_party
mkdir valgrind
cd valgrind
curl https://chromium.googlesource.com/chromium/src/base/+/master/third_party/valgrind/valgrind.h?format=TEXT | base64 -d > valgrind.h
curl https://chromium.googlesource.com/chromium/src/base/+/master/third_party/valgrind/memcheck.h?format=TEXT | base64 -d > memcheck.h

NOTE: If system/bt is checked out under AOSP, then create symbolic links instead of downloading sources

cd system/bt
mkdir third_party
cd third_party
ln -s ../../../external/aac aac
ln -s ../../../external/libchrome libchrome
ln -s ../../../external/libldac libldac
ln -s ../../../external/modp_b64 modp_b64
ln -s ../../../external/tinyxml2 tinyxml2
ln -s ../../../external/googletest googletest

Generate your build files

cd ~/fluoride/bt
gn gen out/Default

Build

cd ~/fluoride/bt
ninja -C out/Default all

This will build all targets (the shared library, executables, tests, etc) and put them in out/Default. To build an individual target, replace "all" with the target of your choice, e.g. ninja -C out/Default net_test_osi.

Run

cd ~/fluoride/bt/out/Default
LD_LIBRARY_PATH=./ ./bluetoothtbd -create-ipc-socket=fluoride

Eclipse IDE Support

  1. Follows the Chromium project Eclipse Setup Instructions until "Optional: Building inside Eclipse" section (don't do that section, we will set it up differently)

  2. Generate Eclipse settings:

cd system/bt
gn gen --ide=eclipse out/Default
  1. In Eclipse, do File->Import->C/C++->C/C++ Project Settings, choose the XML location under system/bt/out/Default

  2. Right click on the project. Go to Preferences->C/C++ Build->Builder Settings. Uncheck "Use default build command", but instead using "ninja -C out/Default"

  3. Goto Behaviour tab, change clean command to "-t clean"