Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit 5a44eb6f authored by En-Shuo Hsu's avatar En-Shuo Hsu
Browse files

floss: Fill in sequence number for output packets

Bug: 236413634
Tag: #floss
Test: Build and verify with devices supporting WBS
Change-Id: Id41f07a0877444772b95b029d57f341e81748d54
parent 6de1be21
Loading
Loading
Loading
Loading
+24 −7
Original line number Diff line number Diff line
@@ -104,6 +104,11 @@ static const uint8_t btm_msbc_zero_packet[] = {

static uint8_t btm_msbc_zero_frames[BTM_MSBC_CODE_SIZE];

/* Second octet of H2 header is composed by 4 bits fixed 0x8 and 4 bits
 * sequence number 0000, 0011, 1100, 1111. */
static const uint8_t btm_h2_header_frames_count[] = {0x08, 0x38, 0xc8, 0xf8};

static uint8_t btm_msbc_num_out_frames;
/******************************************************************************/
/*            L O C A L    F U N C T I O N     P R O T O T Y P E S            */
/******************************************************************************/
@@ -196,6 +201,15 @@ static tSCO_CONN* btm_get_active_sco() {
  return nullptr;
}

static bool verify_h2_header_seq_num(const uint8_t num) {
  for (int i = 0; i < 4; i++) {
    if (num == btm_h2_header_frames_count[i]) {
      return true;
    }
  }
  return false;
}

/*******************************************************************************
 *
 * Function         btm_route_sco_data
@@ -211,7 +225,6 @@ void btm_route_sco_data(BT_HDR* p_msg) {
  uint8_t* out_data;
  uint8_t length = 0;
  uint8_t read_buf[BTM_SCO_DATA_SIZE_MAX];
  uint8_t frame_count = 0x08;
  if (p_msg->len < 3) {
    LOG_ERROR("Received incomplete SCO header");
    osi_free(p_msg);
@@ -246,7 +259,6 @@ void btm_route_sco_data(BT_HDR* p_msg) {

    uint8_t h2_header;
    STREAM_TO_UINT8(h2_header, payload);
    STREAM_TO_UINT8(frame_count, payload);
    if (h2_header != BTM_MSBC_H2_HEADER_0) {
      LOG_ERROR("Received invalid mSBC packet with invalid h2 header:%x",
                h2_header);
@@ -254,10 +266,11 @@ void btm_route_sco_data(BT_HDR* p_msg) {
      return;
    }

    if (frame_count != 0x08 && frame_count != 0x38 && frame_count != 0xc8 &&
        frame_count != 0xf8) {
      LOG_ERROR("Received invalid mSBC packet with invalid frame count :%x",
                frame_count);
    uint8_t seq_num;
    STREAM_TO_UINT8(seq_num, payload);
    if (!verify_h2_header_seq_num(seq_num)) {
      LOG_ERROR("Received invalid mSBC packet with invalid sequence number :%x",
                seq_num);
      osi_free(p_msg);
      return;
    }
@@ -282,7 +295,9 @@ void btm_route_sco_data(BT_HDR* p_msg) {
      ESCO_CODING_FORMAT_TRANSPNT /* Inband MSBC */) {
    /* The pre-computed zero input bit stream of mSBC codec, per HFP 1.7 spec.
     * This mSBC frame will be decoded into all-zero input PCM. */
    uint8_t encoded[BTM_MSBC_PKT_LEN] = {BTM_MSBC_H2_HEADER_0, frame_count};
    uint8_t encoded[BTM_MSBC_PKT_LEN] = {
        BTM_MSBC_H2_HEADER_0,
        btm_h2_header_frames_count[btm_msbc_num_out_frames % 4]};

    uint32_t encoded_size;
    if (size_read != BTM_MSBC_CODE_SIZE) {
@@ -301,6 +316,7 @@ void btm_route_sco_data(BT_HDR* p_msg) {
    auto data = std::vector<uint8_t>(encoded, encoded + BTM_MSBC_PKT_LEN);
    btm_send_sco_packet(std::move(data));

    btm_msbc_num_out_frames++;
  } else {
    auto data = std::vector<uint8_t>(read_buf, read_buf + size_read);
    btm_send_sco_packet(std::move(data));
@@ -862,6 +878,7 @@ void btm_sco_connected(const RawAddress& bda, uint16_t hci_handle,
      if (p->esco.setup.input_data_path == ESCO_DATA_PATH_HCI) {
        if (p->esco.setup.transmit_coding_format.coding_format ==
            ESCO_CODING_FORMAT_TRANSPNT) {
          btm_msbc_num_out_frames = 0;
          hfp_msbc_decoder_init();
          hfp_msbc_encoder_init();
        }