Skip to content

Commit 6fa31f9

Browse files
SP2FETSP2FET
andauthored
[EGD-3772] added HSP sink and source (#918)
Added HSP playback to the BT device and stream of the BT device microphone back to the OS Co-authored-by: SP2FET <[email protected]>
1 parent 0646b99 commit 6fa31f9

File tree

17 files changed

+692
-87
lines changed

17 files changed

+692
-87
lines changed

.gitmodules

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,3 +59,4 @@
5959
[submodule "module-utils/tinyexpr"]
6060
path = module-utils/tinyexpr
6161
url = https://github.com/codeplea/tinyexpr.git
62+

changelog.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@
99
* `[messages][db]` File Indexer db Agent
1010
* `[testing]` Added key release simulation through service-desktop developerMode
1111
* `[cellular]` Set pin functionality with Action mockup
12+
* `[bluetooth]` Added HSP playback and recording
13+
1214
### Changed
1315

1416
* `[cellular]` Handled properly SIM READY and SIM PIN URC messages with Action mockup

module-bluetooth/Bluetooth/BluetoothWorker.cpp

Lines changed: 14 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,8 @@
44
#include "BluetoothWorker.hpp"
55
#include "BtCommand.hpp"
66
#include "log/log.hpp"
7-
#include "module-bluetooth/Bluetooth/interface/profiles/A2DP/A2DP.hpp"
8-
7+
#include "interface/profiles/A2DP/A2DP.hpp"
8+
#include "interface/profiles/HSP/HSP.hpp"
99
extern "C"
1010
{
1111
#include "module-bluetooth/lib/btstack/src/btstack_util.h"
@@ -28,7 +28,7 @@ const char *c_str(Bt::Error::Code code)
2828
return "";
2929
}
3030

31-
BluetoothWorker::BluetoothWorker(sys::Service *service) : Worker(service), currentProfile(std::make_shared<Bt::A2DP>())
31+
BluetoothWorker::BluetoothWorker(sys::Service *service) : Worker(service), currentProfile(std::make_shared<Bt::HSP>())
3232
{
3333
init({
3434
{"qBtIO", sizeof(Bt::Message), 10},
@@ -86,18 +86,18 @@ bool BluetoothWorker::scan()
8686
}
8787
}
8888

89-
void BluetoothWorker::stop_scan()
89+
void BluetoothWorker::stopScan()
9090
{
9191
Bt::GAP::stop_scan();
9292
}
9393

94-
bool BluetoothWorker::set_visible()
94+
bool BluetoothWorker::toggleVisibility()
9595
{
9696
static bool visibility = true;
9797
Bt::GAP::set_visibility(visibility);
9898
visibility = !visibility;
9999

100-
return false;
100+
return visibility;
101101
}
102102

103103
bool BluetoothWorker::start_pan()
@@ -110,16 +110,6 @@ bool BluetoothWorker::start_pan()
110110
return false;
111111
}
112112

113-
BluetoothWorker::Error BluetoothWorker::aud_init()
114-
{
115-
LOG_INFO("%s", __PRETTY_FUNCTION__);
116-
Error err = SuccessBt;
117-
LOG_INFO("AUDIO - TODO");
118-
// start GAVD
119-
// && ASSIGN_CLASS_OF_DEVICE(ClassOfDevice, 0x28, 0x04, 0x10);
120-
return err;
121-
}
122-
123113
#include <sstream>
124114

125115
bool BluetoothWorker::handleMessage(uint32_t queueID)
@@ -189,36 +179,22 @@ bool BluetoothWorker::handleMessage(uint32_t queueID)
189179
return true;
190180
}
191181

192-
void BluetoothWorker::initAudioBT()
193-
{}
194-
195-
bool BluetoothWorker::play_audio()
182+
bool BluetoothWorker::establishAudioConnection()
196183
{
197-
auto profile = dynamic_cast<Bt::A2DP *>(currentProfile.get());
198-
if (profile == nullptr) {
184+
currentProfile->setOwnerService(service);
185+
if (currentProfile->init() != Bt::Error::Success) {
199186
return false;
200187
}
201-
202-
profile->init();
203-
profile->setOwnerService(service);
204-
profile->start();
188+
currentProfile->connect();
205189
return true;
206190
}
207-
bool BluetoothWorker::stop_audio()
191+
bool BluetoothWorker::disconnectAudioConnection()
208192
{
209-
auto profile = dynamic_cast<Bt::A2DP *>(currentProfile.get());
210-
if (profile == nullptr) {
211-
return false;
212-
}
213-
214-
profile->stop();
193+
currentProfile->disconnect();
215194
return true;
216195
}
217-
void BluetoothWorker::set_addr(bd_addr_t addr)
196+
void BluetoothWorker::setDeviceAddress(bd_addr_t addr)
218197
{
219198
Bt::GAP::do_pairing(addr);
220-
auto profile = dynamic_cast<Bt::A2DP *>(currentProfile.get());
221-
if (profile != nullptr) {
222-
profile->setDeviceAddress(addr);
223-
}
199+
currentProfile->setDeviceAddress(addr);
224200
}

module-bluetooth/Bluetooth/BluetoothWorker.hpp

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -92,19 +92,19 @@ class BluetoothWorker : private sys::Worker
9292

9393
bool scan();
9494

95-
bool set_visible();
95+
bool toggleVisibility();
9696

9797
bool start_pan();
9898

99-
bool play_audio();
99+
bool establishAudioConnection();
100100

101-
bool stop_audio();
101+
bool disconnectAudioConnection();
102102

103103
Error aud_init();
104104
/// bluetooth stack id in use
105105
unsigned long active_features;
106-
void stop_scan();
107-
void set_addr(bd_addr_t addr);
106+
void stopScan();
107+
void setDeviceAddress(bd_addr_t addr);
108108
void initAudioBT();
109109

110110
std::shared_ptr<Bt::Profile> currentProfile;

module-bluetooth/Bluetooth/Device.hpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,4 +53,5 @@ constexpr unsigned int DATA_BUFFER_SIZE = 256 * 2;
5353
struct AudioData_t
5454
{
5555
std::array<int16_t, DATA_BUFFER_SIZE> data;
56+
uint16_t bytesSent;
5657
};

module-bluetooth/Bluetooth/interface/profiles/A2DP/A2DP.cpp

Lines changed: 35 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -12,11 +12,11 @@
1212
#include <Bluetooth/Device.hpp>
1313
#include <Bluetooth/Error.hpp>
1414
#include <log/log.hpp>
15-
#include <module-sys/Service/Bus.hpp>
15+
#include <Service/Bus.hpp>
1616
#include <service-bluetooth/messages/BluetoothMessage.hpp>
17-
#include <module-audio/Audio/AudioCommon.hpp>
18-
#include <module-services/service-audio/messages/AudioMessage.hpp>
19-
#include <module-services/service-evtmgr/Constants.hpp>
17+
#include <Audio/AudioCommon.hpp>
18+
#include <service-audio/messages/AudioMessage.hpp>
19+
#include <service-evtmgr/Constants.hpp>
2020
extern "C"
2121
{
2222
#include "module-bluetooth/lib/btstack/src/btstack.h"
@@ -55,32 +55,37 @@ namespace Bt
5555
return *this;
5656
}
5757

58-
auto A2DP::init() -> Error
58+
auto A2DP::init() -> Error::Code
5959
{
6060
return pimpl->init();
6161
}
62-
void A2DP::start()
63-
{
64-
pimpl->start();
65-
}
66-
void A2DP::stop()
67-
{
68-
pimpl->stop();
69-
}
62+
7063
void A2DP::setDeviceAddress(uint8_t *addr)
7164
{
7265
pimpl->setDeviceAddress(addr);
7366
}
74-
void A2DP::setOwnerService(sys::Service *service)
67+
68+
void A2DP::setOwnerService(const sys::Service *service)
7569
{
7670
pimpl->setOwnerService(service);
7771
}
72+
7873
auto A2DP::getStreamData() -> std::shared_ptr<BluetoothStreamData>
7974
{
8075
return pimpl->getStreamData();
8176
}
8277

83-
sys::Service *A2DP::A2DPImpl::ownerService;
78+
void A2DP::connect()
79+
{
80+
pimpl->start();
81+
}
82+
83+
void A2DP::disconnect()
84+
{
85+
pimpl->stop();
86+
}
87+
88+
const sys::Service *A2DP::A2DPImpl::ownerService;
8489
QueueHandle_t A2DP::A2DPImpl::sourceQueue = nullptr;
8590
QueueHandle_t A2DP::A2DPImpl::sinkQueue = nullptr;
8691
bd_addr_t A2DP::A2DPImpl::deviceAddr;
@@ -95,7 +100,7 @@ namespace Bt
95100

96101
/* LISTING_START(MainConfiguration): Setup Audio Source and AVRCP Target services */
97102

98-
auto A2DP::A2DPImpl::init() -> Error
103+
auto A2DP::A2DPImpl::init() -> Error::Code
99104
{
100105
// request role change on reconnecting headset to always use them in slave mode
101106
hci_set_master_slave_policy(0);
@@ -115,7 +120,7 @@ namespace Bt
115120
AVDTP::sbcCodecConfiguration.size());
116121
if (local_stream_endpoint == nullptr) {
117122
LOG_INFO("A2DP Source: not enough memory to create local stream endpoint\n");
118-
return Error(Bt::Error::SystemError);
123+
return Bt::Error::SystemError;
119124
}
120125
AVRCP::mediaTracker.local_seid = avdtp_local_seid(local_stream_endpoint);
121126
avdtp_source_register_delay_reporting_category(AVRCP::mediaTracker.local_seid);
@@ -160,7 +165,7 @@ namespace Bt
160165

161166
LOG_INFO("Init done!");
162167

163-
return Error();
168+
return Bt::Error::Success;
164169
}
165170

166171
void A2DP::A2DPImpl::sendMediaPacket()
@@ -450,9 +455,7 @@ namespace Bt
450455
sourceQueue = xQueueCreate(5, sizeof(AudioData_t));
451456
sinkQueue = nullptr;
452457
if (sourceQueue != nullptr) {
453-
auto event = std::make_shared<audio::Event>(audio::EventType::BTA2DPOn);
454-
auto msg = std::make_shared<AudioEventRequest>(std::move(event));
455-
sys::Bus::SendUnicast(std::move(msg), service::name::evt_manager, ownerService);
458+
sendAudioEvent(audio::EventType::BTA2DPOn);
456459
}
457460
else {
458461
LOG_ERROR("failed to create queue!");
@@ -534,11 +537,7 @@ namespace Bt
534537
cid,
535538
AVRCP::mediaTracker.local_seid,
536539
local_seid);
537-
{
538-
auto event = std::make_shared<audio::Event>(audio::EventType::BTA2DPOff);
539-
auto msg = std::make_shared<AudioEventRequest>(std::move(event));
540-
sys::Bus::SendUnicast(std::move(msg), service::name::evt_manager, ownerService);
541-
}
540+
sendAudioEvent(audio::EventType::BTA2DPOff);
542541
if (cid == AVRCP::mediaTracker.a2dp_cid) {
543542
AVRCP::mediaTracker.stream_opened = 0;
544543
LOG_INFO("A2DP Source: Stream released.\n");
@@ -581,13 +580,22 @@ namespace Bt
581580
bd_addr_copy(deviceAddr, addr);
582581
LOG_INFO("Address set!");
583582
}
584-
void A2DP::A2DPImpl::setOwnerService(sys::Service *service)
583+
584+
void A2DP::A2DPImpl::setOwnerService(const sys::Service *service)
585585
{
586586
ownerService = service;
587587
}
588+
588589
auto A2DP::A2DPImpl::getStreamData() -> std::shared_ptr<BluetoothStreamData>
589590
{
590591
return std::make_shared<BluetoothStreamData>(sinkQueue, sourceQueue, metadata);
591592
}
592593

594+
void A2DP::A2DPImpl::sendAudioEvent(audio::EventType event)
595+
{
596+
auto evt = std::make_shared<audio::Event>(event);
597+
auto msg = std::make_shared<AudioEventRequest>(std::move(evt));
598+
sys::Bus::SendUnicast(std::move(msg), service::name::evt_manager, const_cast<sys::Service *>(ownerService));
599+
}
600+
593601
} // namespace Bt

module-bluetooth/Bluetooth/interface/profiles/A2DP/A2DP.hpp

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
#include "Service/Service.hpp"
99
#include "queue.h"
1010
#include <memory>
11-
#include <module-services/service-bluetooth/ServiceBluetoothCommon.hpp>
11+
#include <service-bluetooth/ServiceBluetoothCommon.hpp>
1212
namespace Bt
1313
{
1414
class A2DP : public Profile
@@ -22,13 +22,13 @@ namespace Bt
2222
A2DP(A2DP &&other) noexcept;
2323
auto operator=(A2DP &&other) noexcept -> A2DP &;
2424

25-
auto init() -> Error override;
25+
auto init() -> Error::Code override;
2626
void setDeviceAddress(uint8_t *addr) override;
27-
void setOwnerService(sys::Service *service);
28-
auto getStreamData() -> std::shared_ptr<BluetoothStreamData> override;
27+
void setOwnerService(const sys::Service *service) override;
28+
[[nodiscard]] auto getStreamData() -> std::shared_ptr<BluetoothStreamData> override;
2929

30-
void start();
31-
void stop();
30+
void connect() override;
31+
void disconnect() override;
3232

3333
private:
3434
class A2DPImpl;

module-bluetooth/Bluetooth/interface/profiles/A2DP/A2DPImpl.hpp

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@
77
#include <Bluetooth/Error.hpp>
88
#include <BtCommand.hpp>
99
#include <log/log.hpp>
10+
#include <Audio/AudioCommon.hpp>
11+
1012
extern "C"
1113
{
1214
#include <btstack.h>
@@ -53,7 +55,7 @@ namespace Bt
5355
static bd_addr_t deviceAddr;
5456
static btstack_packet_callback_registration_t hciEventCallbackRegistration;
5557
static std::array<uint8_t, MEDIA_CAP_SIZE> mediaSbcCodecCapabilities;
56-
static sys::Service *ownerService;
58+
static const sys::Service *ownerService;
5759
static QueueHandle_t sourceQueue;
5860
static QueueHandle_t sinkQueue;
5961
static DeviceMetadata_t metadata;
@@ -65,13 +67,14 @@ namespace Bt
6567
static void hciPacketHandler(uint8_t packetType, uint16_t channel, uint8_t *packet, uint16_t size);
6668
static void sendMediaPacket();
6769
static auto fillSbcAudioBuffer(MediaContext *context) -> int;
70+
static void sendAudioEvent(audio::EventType event);
6871

6972
public:
70-
auto init() -> Error;
73+
auto init() -> Error::Code;
7174
void start();
7275
void stop();
7376
void setDeviceAddress(bd_addr_t addr);
74-
void setOwnerService(sys::Service *service);
77+
void setOwnerService(const sys::Service *service);
7578
auto getStreamData() -> std::shared_ptr<BluetoothStreamData>;
7679
};
7780
} // namespace Bt

0 commit comments

Comments
 (0)