Skip to content

Commit

Permalink
Switch to using l2cap_can_send_packet_now()
Browse files Browse the repository at this point in the history
  • Loading branch information
shermp committed Aug 16, 2024
1 parent 30a3888 commit 59e8f65
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 31 deletions.
14 changes: 2 additions & 12 deletions src/asha_bt.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -216,12 +216,8 @@ extern "C" void bt_main()
if (write_index == 0) continue;

ha.set_write_index(write_index);
if (ha.state == AudioPacketReady) {
LOG_AUDIO("%s: AudioPacketReady\n", ha.side_str);
ha.set_volume(vol);
ha.write_volume();
ha.send_audio_packet();
}
ha.set_volume(vol);
ha.send_audio_packet();
}
}
}
Expand Down Expand Up @@ -489,12 +485,6 @@ static void l2cap_cbm_event_handler (uint8_t packet_type, uint16_t channel, uint
}
break;
}
case L2CAP_EVENT_CAN_SEND_NOW:
{
LOG_AUDIO("L2CAP_EVENT_CAN_SEND_NOW\n");
HA& ha = ha_mgr.get_by_cid(l2cap_event_can_send_now_get_local_cid(packet));
ha.on_can_send_audio_packet_now();
}
case L2CAP_EVENT_PACKET_SENT:
{
LOG_AUDIO("L2CAP_EVENT_PACKET_SENT\n");
Expand Down
35 changes: 17 additions & 18 deletions src/hearing_aid.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -237,7 +237,7 @@ void HA::set_volume(AudioBuffer::Volume& vol)

void HA::write_volume()
{
if (change_vol && is_streaming_audio()) {
if (change_vol) {
change_vol = false;
uint8_t res = gatt_client_write_value_of_characteristic_without_response(conn_handle,
chars.vol.value_handle,
Expand All @@ -258,30 +258,29 @@ void HA::set_write_index(uint32_t write_index)

void HA::send_audio_packet()
{
if (state != State::AudioPacketReady) return;
if (curr_read_index < curr_write_index) {
switch (state) {
case State::AudioPacketReady:
{
if (curr_read_index >= curr_write_index) {
break;
}
if ((curr_write_index - curr_read_index) >= 4) {
curr_read_index = curr_write_index - 1;
}
state = State::AudioPacketSending;
AudioBuffer::G722Buff& packet = audio_buff.get_g_buff(curr_read_index);
audio_packet = side() == Side::Left ? packet.l.data() : packet.r.data();
++curr_read_index;
l2cap_request_can_send_now_event(cid);
write_volume();
state = State::AudioPacketSending;
}
// fallthrough
case State::AudioPacketSending:
if (l2cap_can_send_packet_now(cid)) {
LOG_AUDIO("%s: Sending audio packet. Seq Num: %d\n", side_str, (int)audio_packet[0]);
state = State::AudioPacketSent;
l2cap_send(cid, audio_packet, sdu_size_bytes);
}
}

}

void HA::on_can_send_audio_packet_now()
{
if (state != State::AudioPacketSending) return;
LOG_AUDIO("%s: Sending audio packet. Seq Num: %d\n", side_str, (int)audio_packet[0]);
state = State::AudioPacketSent;
l2cap_send(cid, audio_packet, sdu_size_bytes);
// if (res != ERROR_CODE_SUCCESS) {
// LOG_ERROR("%s: Error sending audio packet with error code: 0x%02x\n", side_str, (unsigned int)res);
// state = State::AudioPacketReady;
// }

}

Expand Down
1 change: 0 additions & 1 deletion src/hearing_aid.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,6 @@ class HA

void set_write_index(uint32_t write_index);
void send_audio_packet();
void on_can_send_audio_packet_now();
void on_audio_packet_sent();

bool is_streaming_audio();
Expand Down

0 comments on commit 59e8f65

Please sign in to comment.