~aleteoryx/muditaos

ref: d9a1194e6f203247ebcef4b03f8ce5ebccc7c778 muditaos/module-bluetooth/Bluetooth/interface/profiles/SCO/SCO.cpp -rw-r--r-- 7.7 KiB
d9a1194e — Lukasz Mastalerz [BH-1688] Create a standard for logs 2 years ago
                                                                                
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
// Copyright (c) 2017-2023, Mudita Sp. z.o.o. All rights reserved.
// For licensing, see https://github.com/mudita/MuditaOS/LICENSE.md

#include "SCO.hpp"
#include <cstdio>
#include <Audio/AudioCommon.hpp>
#include <service-evtmgr/ServiceEventManagerName.hpp>
#include <service-audio/AudioMessage.hpp>
#include <cassert>
extern "C"
{
#include "btstack_audio.h"
#include "btstack_debug.h"
#include "btstack_ring_buffer.h"
#include "classic/btstack_cvsd_plc.h"
#include "classic/btstack_sbc.h"
#include "classic/hfp.h"
#include "classic/hfp_msbc.h"
#include "hci.h"
}

namespace bluetooth
{

    class SCO::SCOImpl
    {
      public:
        static void init();
        static void send(hci_con_handle_t scoHandle);
        static void receive(uint8_t *packet, uint16_t size);
        void setOwnerService(const sys::Service *service);
        void setCodec(uint8_t codec);

      private:
        static constexpr auto BYTES_PER_FRAME     = 2;
        static constexpr auto ALL_GOOD_MASK       = 0x30;
        static constexpr auto AUDIO_BUFFER_LENGTH = 128;
        static constexpr auto PACKET_DATA_OFFSET  = 3;
        static constexpr auto VOICE_SETTING_CVSD  = 0x60; // linear, unsigned, 16-bit, CVSD

        static btstack_cvsd_plc_state_t cvsdPlcState;

        static QueueHandle_t sinkQueue;
        static QueueHandle_t sourceQueue;
        static DeviceMetadata_t metadata;
        static const sys::Service *ownerService;
        static uint8_t negotiated_codec;

        static auto audioInitialize(int sampleRate) -> Result;
        static void initCvsd();
        static void receiveCvsd(uint8_t *packet, uint16_t size);
        static void writeToHostEndian(int16_t *buffer, uint8_t *packet, int length);
        static void sendEvent(audio::EventType event, audio::Event::DeviceState state);
        static void flipEndianess(uint16_t *data, size_t length);
    };

    SCO::SCO() : pimpl(std::make_unique<SCOImpl>(SCOImpl()))
    {}

    SCO::SCO(SCO &&other) noexcept : pimpl(std::move(other.pimpl))
    {}
    auto SCO::operator=(SCO &&other) noexcept -> SCO &
    {
        if (&other == this) {
            return *this;
        }

        pimpl       = std::move(other.pimpl);
        other.pimpl = nullptr;
        return *this;
    }

    void SCO::init()
    {
        pimpl->init();
    }
    void SCO::send(hci_con_handle_t sco_handle)
    {
        pimpl->send(sco_handle);
    }
    void SCO::receive(uint8_t *packet, uint16_t size)
    {
        pimpl->receive(packet, size);
    }
    void SCO::setOwnerService(const sys::Service *service)
    {
        pimpl->setOwnerService(service);
    }
    void SCO::setCodec(SCOCodec codec)
    {
        pimpl->setCodec(static_cast<uint8_t>(codec));
    }

    SCO::~SCO() = default;
} // namespace bluetooth

using namespace bluetooth;

btstack_cvsd_plc_state_t SCO::SCOImpl::cvsdPlcState;
QueueHandle_t SCO::SCOImpl::sinkQueue;
QueueHandle_t SCO::SCOImpl::sourceQueue;
const sys::Service *SCO::SCOImpl::ownerService = nullptr;
DeviceMetadata_t SCO::SCOImpl::metadata;
uint8_t SCO::SCOImpl::negotiated_codec;

void SCO::SCOImpl::sendEvent(audio::EventType event, audio::Event::DeviceState state)
{
    auto evt       = std::make_shared<audio::Event>(event, state);
    auto msg       = std::make_shared<AudioEventRequest>(std::move(evt));
    auto &busProxy = const_cast<sys::Service *>(ownerService)->bus;
    busProxy.sendUnicast(std::move(msg), service::name::evt_manager);
}
auto SCO::SCOImpl::audioInitialize(int sampleRate) -> Result
{
    sourceQueue = xQueueCreate(5, sizeof(AudioData_t));
    sinkQueue   = xQueueCreate(5, sizeof(AudioData_t));

    int scoPayloadLength            = hci_get_sco_packet_length() - PACKET_DATA_OFFSET;
    const int audioSamplesPerPacket = scoPayloadLength;

    metadata.sampleRate      = static_cast<unsigned int>(sampleRate);
    metadata.channels        = 1;
    metadata.samplesPerFrame = audioSamplesPerPacket;

    if (sourceQueue == nullptr || sinkQueue == nullptr) {
        LOG_ERROR("Failed to create queue!");
        return Result(Result::Code::SystemError);
    }

    LOG_INFO("Init done!");
    return Result(Result::Code::Success);
}

void SCO::SCOImpl::initCvsd()
{
    btstack_cvsd_plc_init(&cvsdPlcState);
    auto ret = audioInitialize(CVSD_SAMPLE_RATE);
    if (ret.result == Result::Code::Success) {
        LOG_INFO("CVSD init done!");
    }
}
void SCO::SCOImpl::writeToHostEndian(int16_t *buffer, uint8_t *packet, int length)
{
    for (int i = 0; i < length; i++) {
        buffer[i] = little_endian_read_16(packet, PACKET_DATA_OFFSET + i * 2);
    }
}
void SCO::SCOImpl::receiveCvsd(uint8_t *packet, uint16_t size)
{

    std::array<int16_t, AUDIO_BUFFER_LENGTH> audioFrameOut{};

    if (size > audioFrameOut.size()) {
        LOG_WARN("SCO packet larger than local output buffer - dropping data.");
        return;
    }

    const int audioBytesRead = size - PACKET_DATA_OFFSET;
    const int numSamples     = audioBytesRead / BYTES_PER_FRAME;

    std::array<int16_t, AUDIO_BUFFER_LENGTH> audioFrameIn;
    writeToHostEndian(audioFrameIn.data(), packet, numSamples);

    // treat packet as bad frame if controller does not report 'all good'
    bool badFrame         = false;
    auto packetStatusByte = packet[1];
    if ((packetStatusByte & ALL_GOOD_MASK) != 0) {
        badFrame = true;
    }

    btstack_cvsd_plc_process_data(&cvsdPlcState, badFrame, audioFrameIn.data(), numSamples, audioFrameOut.data());

    // Samples in CVSD SCO packet are in little endian
    AudioData_t audioData;
    std::copy_n(std::begin(audioFrameOut), audioBytesRead, std::begin(audioData.data));
    audioData.bytesSent = audioBytesRead;

    if (sinkQueue != nullptr) {
        xQueueSend(sinkQueue, &audioData, 5);
    }
    else {
        LOG_ERROR("Queue is not initialized!");
    }
}

void SCO::SCOImpl::init()
{
    hci_set_sco_voice_setting(VOICE_SETTING_CVSD);
    initCvsd();
}

void SCO::SCOImpl::flipEndianess(uint16_t *data, size_t length)
{
    for (size_t i = 0; i < length; i++) {
        data[i] = __builtin_bswap16(data[i]);
    }
}
void SCO::SCOImpl::send(hci_con_handle_t scoHandle)
{
    if (scoHandle == HCI_CON_HANDLE_INVALID) {
        return;
    }
    assert(sourceQueue != nullptr);

    int scoPacketLength  = hci_get_sco_packet_length();
    int scoPayloadLength = scoPacketLength - PACKET_DATA_OFFSET;

    hci_reserve_packet_buffer();
    auto scoPacket  = hci_get_outgoing_packet_buffer();
    auto sampleData = &scoPacket[3];
    AudioData_t audioData;

    if (xQueueReceive(sourceQueue, &audioData, 1) != pdPASS) {
        auto rangeStart = static_cast<uint8_t *>(sampleData);
        auto rangeEnd   = rangeStart + scoPayloadLength;
        std::fill(rangeStart, rangeEnd, 0);
    }
    else {
        auto dest = static_cast<std::uint8_t *>(sampleData);
        std::copy(std::begin(audioData.data), std::begin(audioData.data) + scoPayloadLength, dest);

        if (btstack_is_big_endian()) {
            flipEndianess(reinterpret_cast<uint16_t *>(sampleData), scoPayloadLength / 2);
        }
    }

    // set handle + flags
    little_endian_store_16(scoPacket, 0, scoHandle);
    // set length
    scoPacket[2] = scoPayloadLength;
    // finally send packet
    hci_send_sco_packet_buffer(scoPacketLength);
    // request another send event
    hci_request_sco_can_send_now_event();
}

void SCO::SCOImpl::receive(uint8_t *packet, uint16_t size)
{
    receiveCvsd(packet, size);
}
void SCO::SCOImpl::setOwnerService(const sys::Service *service)
{
    ownerService = service;
}

void SCO::SCOImpl::setCodec(uint8_t codec)
{
    if (negotiated_codec == codec) {
        return;
    }
    negotiated_codec = codec;

    if (negotiated_codec == HFP_CODEC_MSBC) {
        // btstack_sbc_decoder_init(&decoder_state, SBC_MODE_mSBC, &handle_pcm_data, NULL);
        hfp_msbc_init();
    }
    else {
        initCvsd();
    }
}