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

Commit fa76e582 authored by Chienyuan Huang's avatar Chienyuan Huang Committed by Automerger Merge Worker
Browse files

Merge "CS: Update RAS module" into main am: 2fb33335 am: b274aaaa

parents 690e0cf6 b274aaaa
Loading
Loading
Loading
Loading
+36 −6
Original line number Diff line number Diff line
@@ -52,6 +52,7 @@ class RasClientImpl : public bluetooth::ras::RasClient {
    uint32_t remote_supported_features_;
    uint16_t latest_ranging_counter_ = 0;
    bool handling_on_demand_data_ = false;
    bool is_connected_ = false;
    std::vector<VendorSpecificCharacteristic> vendor_specific_characteristics_;
    uint8_t writeReplyCounter_ = 0;
    uint8_t writeReplySuccessCounter_ = 0;
@@ -113,6 +114,15 @@ class RasClientImpl : public bluetooth::ras::RasClient {
    if (tracker == nullptr) {
      trackers_.emplace_back(
          std::make_shared<RasTracker>(ble_bd_addr.bda, address));
    } else if (tracker->is_connected_) {
      log::info("Already connected");
      uint16_t att_handle =
          tracker
              ->FindCharacteristicByUuid(kRasRealTimeRangingDataCharacteristic)
              ->value_handle;
      callbacks_->OnConnected(address, att_handle,
                              tracker->vendor_specific_characteristics_);
      return;
    }
    BTA_GATTC_Open(gatt_if_, ble_bd_addr.bda, BTM_BLE_DIRECT_CONNECTION, false);
  }
@@ -138,7 +148,7 @@ class RasClientImpl : public bluetooth::ras::RasClient {
                 vendor_specific_characteristic.value_.size());
      BTA_GATTC_WriteCharValue(
          tracker->conn_id_, characteristic->value_handle, GATT_WRITE,
          vendor_specific_characteristic.value_, GATT_AUTH_REQ_MITM,
          vendor_specific_characteristic.value_, GATT_AUTH_REQ_NO_MITM,
          GattWriteCallback, &gatt_write_callback_data_);
    }
  }
@@ -149,6 +159,10 @@ class RasClientImpl : public bluetooth::ras::RasClient {
      case BTA_GATTC_OPEN_EVT: {
        OnGattConnected(p_data->open);
      } break;
      case BTA_GATTC_CLOSE_EVT: {
        OnGattDisconnected(p_data->close);
        break;
      }
      case BTA_GATTC_SEARCH_CMPL_EVT: {
        OnGattServiceSearchComplete(p_data->search_cmpl);
      } break;
@@ -184,10 +198,25 @@ class RasClientImpl : public bluetooth::ras::RasClient {
      return;
    }
    tracker->conn_id_ = evt.conn_id;
    tracker->is_connected_ = true;
    log::info("Search service");
    BTA_GATTC_ServiceSearchRequest(tracker->conn_id_, kRangingService);
  }

  void OnGattDisconnected(const tBTA_GATTC_CLOSE& evt) {
    log::info("{}, conn_id=0x{:04x}, status:{}, reason:{}", evt.remote_bda,
              evt.conn_id, gatt_status_text(evt.status),
              gatt_disconnection_reason_text(evt.reason));

    auto tracker = FindTrackerByAddress(evt.remote_bda);
    if (tracker == nullptr) {
      log::warn("Skipping unknown device, address: {}", evt.remote_bda);
      BTA_GATTC_Close(evt.conn_id);
      return;
    }
    tracker->is_connected_ = false;
  }

  void OnGattServiceSearchComplete(const tBTA_GATTC_SEARCH_CMPL& evt) {
    auto tracker = FindTrackerByHandle(evt.conn_id);
    if (tracker == nullptr) {
@@ -225,7 +254,8 @@ class RasClientImpl : public bluetooth::ras::RasClient {
            vendor_specific_characteristic.characteristicUuid_);

        BTA_GATTC_ReadCharacteristic(
            tracker->conn_id_, characteristic->value_handle, GATT_AUTH_REQ_MITM,
            tracker->conn_id_, characteristic->value_handle,
            GATT_AUTH_REQ_NO_MITM,
            [](uint16_t conn_id, tGATT_STATUS status, uint16_t handle,
               uint16_t len, uint8_t* value, void* data) {
              instance->OnReadCharacteristicCallback(conn_id, status, handle,
@@ -244,7 +274,7 @@ class RasClientImpl : public bluetooth::ras::RasClient {
      return;
    }
    BTA_GATTC_ReadCharacteristic(
        tracker->conn_id_, characteristic->value_handle, GATT_AUTH_REQ_MITM,
        tracker->conn_id_, characteristic->value_handle, GATT_AUTH_REQ_NO_MITM,
        [](uint16_t conn_id, tGATT_STATUS status, uint16_t handle, uint16_t len,
           uint8_t* value, void* data) {
          instance->OnReadCharacteristicCallback(conn_id, status, handle, len,
@@ -352,7 +382,7 @@ class RasClientImpl : public bluetooth::ras::RasClient {
    value[1] = (uint8_t)(ranging_counter & 0xFF);
    value[2] = (uint8_t)((ranging_counter >> 8) & 0xFF);
    BTA_GATTC_WriteCharValue(tracker->conn_id_, characteristic->value_handle,
                             GATT_WRITE, value, GATT_AUTH_REQ_MITM,
                             GATT_WRITE, value, GATT_AUTH_REQ_NO_MITM,
                             GattWriteCallback, nullptr);
  }

@@ -371,7 +401,7 @@ class RasClientImpl : public bluetooth::ras::RasClient {
    value[1] = (uint8_t)(ranging_counter & 0xFF);
    value[2] = (uint8_t)((ranging_counter >> 8) & 0xFF);
    BTA_GATTC_WriteCharValue(tracker->conn_id_, characteristic->value_handle,
                             GATT_WRITE, value, GATT_AUTH_REQ_MITM,
                             GATT_WRITE, value, GATT_AUTH_REQ_NO_MITM,
                             GattWriteCallback, nullptr);
    if (ranging_counter != tracker->latest_ranging_counter_) {
      GetRangingData(tracker->latest_ranging_counter_, tracker);
@@ -671,4 +701,4 @@ bluetooth::ras::RasClient* bluetooth::ras::GetRasClient() {
    instance = new RasClientImpl();
  }
  return instance;
};
}
+46 −14
Original line number Diff line number Diff line
@@ -103,7 +103,7 @@ class RasServerImpl : public bluetooth::ras::RasServer {
    if (trackers_.find(ble_bd_addr.bda) == trackers_.end()) {
      log::warn("Can't find tracker for address {}", address);
      return;
    };
    }
    auto response = trackers_[ble_bd_addr.bda].pending_write_response_;
    tGATTS_RSP p_msg;
    p_msg.attr_value.handle = response.write_req_handle_;
@@ -131,11 +131,11 @@ class RasServerImpl : public bluetooth::ras::RasServer {
        tracker.ccc_values_[kRasRangingDataOverWrittenCharacteristic];

    if (ccc_real_time != GATT_CLT_CONFIG_NONE) {
      bool need_confirm = ccc_real_time == GATT_CHAR_CLIENT_CONFIG_INDICTION;
      bool need_confirm = ccc_real_time & GATT_CLT_CONFIG_INDICATION;
      uint16_t attr_id =
          GetCharacteristic(kRasRealTimeRangingDataCharacteristic)
              ->attribute_handle_;
      log::debug("Send Real-time Ranging Data");
      log::debug("Send Real-time Ranging Data is_last {}", is_last);
      BTA_GATTS_HandleValueIndication(tracker.conn_id_, attr_id, data,
                                      need_confirm);
    }
@@ -151,11 +151,13 @@ class RasServerImpl : public bluetooth::ras::RasServer {

    // Send data ready
    if (is_last) {
      if (ccc_data_ready == GATT_CLT_CONFIG_NONE) {
      if (ccc_data_ready == GATT_CLT_CONFIG_NONE ||
          ccc_real_time != GATT_CLT_CONFIG_NONE) {
        log::debug("Skip Ranging Data Ready");
      } else {
        bool need_confirm = ccc_data_ready & GATT_CLT_CONFIG_INDICATION;
        log::debug("Send data ready, ranging_counter {}", procedure_counter);
        log::debug("Send data ready, ranging_counter {}, total fragment {}",
                   procedure_counter, data_buffer.segments_.size());
        uint16_t attr_id = GetCharacteristic(kRasRangingDataReadyCharacteristic)
                               ->attribute_handle_;
        std::vector<uint8_t> value(kRingingCounterSize);
@@ -169,7 +171,8 @@ class RasServerImpl : public bluetooth::ras::RasServer {
    // Send data overwritten
    if (tracker.buffers_.size() > kBufferSize) {
      auto begin = tracker.buffers_.begin();
      if (ccc_data_over_written == GATT_CLT_CONFIG_NONE) {
      if (ccc_data_over_written == GATT_CLT_CONFIG_NONE ||
          ccc_real_time != GATT_CLT_CONFIG_NONE) {
        log::debug("Skip Ranging Data Over Written");
        tracker.buffers_.erase(begin);
        return;
@@ -532,12 +535,38 @@ class RasServerImpl : public bluetooth::ras::RasServer {
                        &p_msg);
      return;
    }

    if (trackers_.find(remote_bda) == trackers_.end()) {
      BTA_GATTS_SendRsp(conn_id, p_data->req_data.trans_id,
                        GATT_ILLEGAL_PARAMETER, &p_msg);
      return;
    }
    ClientTracker* tracker = &trackers_[p_data->req_data.remote_bda];
    const uint8_t* value = p_data->req_data.p_data->write_req.value;
    uint16_t ccc_value;
    STREAM_TO_UINT16(ccc_value, value);
    if (trackers_.find(remote_bda) != trackers_.end()) {
      trackers_[remote_bda].ccc_values_[characteristic->uuid_] = ccc_value;

    // Check that On-demand and Real-time are not registered at the same time
    uint16_t ccc_on_demand_temp =
        tracker->ccc_values_[kRasOnDemandDataCharacteristic];
    uint16_t ccc_real_time_temp =
        tracker->ccc_values_[kRasRealTimeRangingDataCharacteristic];
    if (characteristic->uuid_ == kRasRealTimeRangingDataCharacteristic) {
      ccc_real_time_temp = ccc_value;
    } else if (characteristic->uuid_ == kRasOnDemandDataCharacteristic) {
      ccc_on_demand_temp = ccc_value;
    }
    if (ccc_real_time_temp != GATT_CLT_CONFIG_NONE &&
        ccc_on_demand_temp != GATT_CLT_CONFIG_NONE) {
      log::warn(
          "Client Characteristic Configuration Descriptor Improperly "
          "Configured");
      BTA_GATTS_SendRsp(conn_id, p_data->req_data.trans_id, GATT_CCC_CFG_ERR,
                        &p_msg);
      return;
    }

    trackers_[remote_bda].ccc_values_[characteristic->uuid_] = ccc_value;
    log::info("Write CCC for {}, conn_id:{}, value:0x{:04x}",
              getUuidName(characteristic->uuid_), conn_id, ccc_value);
    BTA_GATTS_SendRsp(conn_id, p_data->req_data.trans_id, GATT_SUCCESS, &p_msg);
@@ -545,7 +574,10 @@ class RasServerImpl : public bluetooth::ras::RasServer {

  void HandleControlPoint(ClientTracker* tracker, tGATT_WRITE_REQ* write_req) {
    ControlPointCommand command;
    if (!ParseControlPointCommand(&command, write_req->value, write_req->len)) {
    ParseControlPointCommand(&command, write_req->value, write_req->len);

    if (!command.isValid_) {
      SendResponseCode(ResponseCodeValue::INVALID_PARAMETER, tracker);
      return;
    }

@@ -600,7 +632,7 @@ class RasServerImpl : public bluetooth::ras::RasServer {
      }
      log::info("Send COMPLETE_RANGING_DATA_RESPONSE, ranging_counter:{}",
                ranging_counter);
      std::vector<uint8_t> response(8, 0);
      std::vector<uint8_t> response(3, 0);
      response[0] = (uint8_t)EventCode::COMPLETE_RANGING_DATA_RESPONSE;
      response[1] = (ranging_counter & 0xFF);
      response[2] = (ranging_counter >> 8) & 0xFF;
@@ -614,7 +646,7 @@ class RasServerImpl : public bluetooth::ras::RasServer {
      log::warn("No Records Found");
      SendResponseCode(ResponseCodeValue::NO_RECORDS_FOUND, tracker);
    }
  };
  }

  void OnAckRangingData(ControlPointCommand* command, ClientTracker* tracker) {
    const uint8_t* value = command->parameter_;
@@ -636,13 +668,13 @@ class RasServerImpl : public bluetooth::ras::RasServer {
      log::warn("No Records Found");
      SendResponseCode(ResponseCodeValue::NO_RECORDS_FOUND, tracker);
    }
  };
  }

  void SendResponseCode(ResponseCodeValue response_code_value,
                        ClientTracker* tracker) {
    log::info("0x{:02x}, {}", (uint16_t)response_code_value,
              GetResponseOpcodeValueText(response_code_value));
    std::vector<uint8_t> response(8, 0);
    std::vector<uint8_t> response(2, 0);
    response[0] = (uint8_t)EventCode::RESPONSE_CODE;
    response[1] = (uint8_t)response_code_value;
    BTA_GATTS_HandleValueIndication(
@@ -742,4 +774,4 @@ bluetooth::ras::RasServer* bluetooth::ras::GetRasServer() {
    instance = new RasServerImpl();
  }
  return instance;
};
}
+7 −7
Original line number Diff line number Diff line
@@ -85,13 +85,12 @@ enum class ResponseCodeValue : uint8_t {
  RESERVED_FOR_FUTURE_USE = 0x00,
  SUCCESS = 0x01,
  OP_CODE_NOT_SUPPORTED = 0x02,
  INVALID_OPERATOR = 0x03,
  OPERATOR_NOT_SUPPORTED = 0x04,
  INVALID_OPERAND = 0x05,
  ABORT_UNSUCCESSFUL = 0x06,
  PROCEDURE_NOT_COMPLETED = 0x07,
  OPERAND_NOT_SUPPORTED = 0x08,
  NO_RECORDS_FOUND = 0x09,
  INVALID_PARAMETER = 0x03,
  PERSISTED = 0x04,
  ABORT_UNSUCCESSFUL = 0x05,
  PROCEDURE_NOT_COMPLETED = 0x06,
  OPERAND_NOT_SUPPORTED = 0x07,
  NO_RECORDS_FOUND = 0x08,
};

std::string GetResponseOpcodeValueText(ResponseCodeValue response_code_value);
@@ -99,6 +98,7 @@ std::string GetResponseOpcodeValueText(ResponseCodeValue response_code_value);
struct ControlPointCommand {
  Opcode opcode_;
  uint8_t parameter_[4];
  bool isValid_;
};

struct ControlPointResponse {
+12 −10
Original line number Diff line number Diff line
@@ -57,24 +57,28 @@ std::string uuid::getUuidName(const bluetooth::Uuid& uuid) {

bool ParseControlPointCommand(ControlPointCommand* command,
                              const uint8_t* value, uint16_t len) {
  command->opcode_ = static_cast<Opcode>(value[0]);
  // Check for minimum expected length
  switch (value[0]) {
    case (uint8_t)Opcode::ABORT_OPERATION:
      if (len != 1) {
        return false;
      }
      break;
    case (uint8_t)Opcode::PCT_FORMAT: {
      if (len < 2) {
      if (len != 2) {
        return false;
      }
    } break;
    case (uint8_t)Opcode::GET_RANGING_DATA:
    case (uint8_t)Opcode::ACK_RANGING_DATA:
    case (uint8_t)Opcode::FILTER: {
      if (len < 3) {
      if (len != 3) {
        return false;
      }
    } break;
    case (uint8_t)Opcode::RETRIEVE_LOST_RANGING_DATA_SEGMENTS: {
      if (len < 5) {
      if (len != 5) {
        return false;
      }
    } break;
@@ -82,8 +86,8 @@ bool ParseControlPointCommand(ControlPointCommand* command,
      log::warn("unknown opcode 0x{:02x}", value[0]);
      return false;
  }
  command->opcode_ = static_cast<Opcode>(value[0]);
  std::memcpy(command->parameter_, value + 1, len - 1);
  command->isValid_ = true;
  return true;
}

@@ -114,12 +118,10 @@ std::string GetResponseOpcodeValueText(ResponseCodeValue response_code_value) {
      return "SUCCESS";
    case ResponseCodeValue::OP_CODE_NOT_SUPPORTED:
      return "OP_CODE_NOT_SUPPORTED";
    case ResponseCodeValue::INVALID_OPERATOR:
      return "INVALID_OPERATOR";
    case ResponseCodeValue::OPERATOR_NOT_SUPPORTED:
      return "OPERATOR_NOT_SUPPORTED";
    case ResponseCodeValue::INVALID_OPERAND:
      return "INVALID_OPERAND";
    case ResponseCodeValue::INVALID_PARAMETER:
      return "INVALID_PARAMETER";
    case ResponseCodeValue::PERSISTED:
      return "PERSISTED";
    case ResponseCodeValue::ABORT_UNSUCCESSFUL:
      return "ABORT_UNSUCCESSFUL";
    case ResponseCodeValue::PROCEDURE_NOT_COMPLETED: