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

Commit bdc23836 authored by Treehugger Robot's avatar Treehugger Robot Committed by Gerrit Code Review
Browse files

Merge "Clean up static variables in scripted beacon"

parents 2ab4b53d ef95498d
Loading
Loading
Loading
Loading
+2 −1
Original line number Original line Diff line number Diff line
@@ -196,7 +196,8 @@ Return<void> BluetoothHci::initialize_impl(
    test_model_.AddDeviceToPhy(controller_index + 2, low_energy_phy_index);
    test_model_.AddDeviceToPhy(controller_index + 2, low_energy_phy_index);
    test_channel_.Add(
    test_channel_.Add(
        {"scripted_beacon", "5b:ea:c1:00:00:03",
        {"scripted_beacon", "5b:ea:c1:00:00:03",
         "/data/vendor/bluetooth/bluetooth_sim_ble_playback_file"});
         "/data/vendor/bluetooth/bluetooth_sim_ble_playback_file",
         "/data/vendor/bluetooth/bluetooth_sim_ble_playback_events"});
    test_model_.AddDeviceToPhy(controller_index + 3, low_energy_phy_index);
    test_model_.AddDeviceToPhy(controller_index + 3, low_energy_phy_index);
    test_channel_.List({});
    test_channel_.List({});
  }
  }
+24 −22
Original line number Original line Diff line number Diff line
@@ -70,17 +70,17 @@ ScriptedBeacon::ScriptedBeacon() {
  properties_.SetLeScanResponse({0x05,  // Length
  properties_.SetLeScanResponse({0x05,  // Length
                                 0x08,  // TYPE_NAME_SHORT
                                 0x08,  // TYPE_NAME_SHORT
                                 'g', 'b', 'e', 'a'});
                                 'g', 'b', 'e', 'a'});
  LOG_INFO("%s scripted_beacon registered %s", __func__, registered_ ? "true" : "false");
}
}


bool ScriptedBeacon::is_config_file_ready() {
bool ScriptedBeacon::is_config_file_ready() {
  static bool file_absence_logged = false;
  if (access(config_file_.c_str(), F_OK) == -1) {
  if (access(config_file_.c_str(), F_OK) == -1) {
   if (!file_absence_logged) {
   if (!file_absence_logged_) {
     LOG_INFO("%s: playback file %s not available",
     LOG_INFO("%s: playback file %s not available",
              __func__,
              __func__,
              config_file_.c_str());
              config_file_.c_str());
     add_event(PlaybackEvent::WAITING_FOR_FILE);
     add_event(PlaybackEvent::WAITING_FOR_FILE);
     file_absence_logged = true;
     file_absence_logged_ = true;
   }
   }
   return false;
   return false;
 }
 }
@@ -109,16 +109,21 @@ bool has_time_elapsed(std::chrono::steady_clock::time_point time_point) {
}
}


void ScriptedBeacon::Initialize(const vector<std::string>& args) {
void ScriptedBeacon::Initialize(const vector<std::string>& args) {
  if (args.size() < 2) return;
  if (args.size() < 2) {
    LOG_ERROR("Initialization failed, need mac address, playback and playback events file arguments %s", __func__);
    return;
  }


  Address addr{};
  Address addr{};
  if (Address::FromString(args[1], addr)) properties_.SetLeAddress(addr);
  if (Address::FromString(args[1], addr)) properties_.SetLeAddress(addr);


  if (args.size() < 4) return;
  if (args.size() < 4) {

    LOG_ERROR("Initialization failed, need playback and playback events file arguments %s", __func__);
  }
  config_file_ = args[2];
  config_file_ = args[2];
  events_file_ = args[3];
  events_file_ = args[3];

  playback_events_.clear_events();
  LOG_INFO("Initialized scripted beacon %s", __func__);
}
}


void ScriptedBeacon::populate_event(PlaybackEvent * event, PlaybackEvent::PlaybackEventType type) {
void ScriptedBeacon::populate_event(PlaybackEvent * event, PlaybackEvent::PlaybackEventType type) {
@@ -137,7 +142,6 @@ void ScriptedBeacon::populate_event(PlaybackEvent * event, PlaybackEvent::Playba
// it to the events file when it becomes available. There after we should be
// it to the events file when it becomes available. There after we should be
// able to write events to file as soon as they are posted.
// able to write events to file as soon as they are posted.
void ScriptedBeacon::add_event(PlaybackEvent::PlaybackEventType type) {
void ScriptedBeacon::add_event(PlaybackEvent::PlaybackEventType type) {
  static PlaybackEvents playback_events;
  PlaybackEvent event;
  PlaybackEvent event;
  if (prev_event_type_ == type) {
  if (prev_event_type_ == type) {
   return;
   return;
@@ -147,12 +151,12 @@ void ScriptedBeacon::add_event(PlaybackEvent::PlaybackEventType type) {
    // Check if we have successfully opened;
    // Check if we have successfully opened;
    if (!events_ostream_.is_open()) {
    if (!events_ostream_.is_open()) {
      LOG_INFO("%s: Events file not opened yet, for event: %d", __func__, type);
      LOG_INFO("%s: Events file not opened yet, for event: %d", __func__, type);
      populate_event(playback_events.add_events(), type);
      populate_event(playback_events_.add_events(), type);
      prev_event_type_ = type;
      prev_event_type_ = type;
      return;
      return;
    } else {
    } else {
      // write all events until now
      // write all events until now
      for (const PlaybackEvent& event : playback_events.events()) {
      for (const PlaybackEvent& event : playback_events_.events()) {
        event.SerializeToOstream(&events_ostream_);
        event.SerializeToOstream(&events_ostream_);
      }
      }
    }
    }
@@ -167,23 +171,22 @@ void ScriptedBeacon::TimerTick() {
  if (!scanned_once_) {
  if (!scanned_once_) {
    Beacon::TimerTick();
    Beacon::TimerTick();
  } else {
  } else {
    static std::chrono::steady_clock::time_point next_check_time =
    next_check_time_ = std::chrono::steady_clock::now();
      std::chrono::steady_clock::now();
    if (!play_back_on_) {
    if (!play_back_on_) {
      if (!has_time_elapsed(next_check_time)) {
      if (!has_time_elapsed(next_check_time_)) {
        return;
        return;
      }
      }
      if (!is_config_file_ready()) {
      if (!is_config_file_ready()) {
        next_check_time = std::chrono::steady_clock::now() +
        next_check_time_ = std::chrono::steady_clock::now() +
            std::chrono::steady_clock::duration(std::chrono::seconds(1));
            std::chrono::steady_clock::duration(std::chrono::seconds(1));
        return;
        return;
      }
      }
      // Give time for the file to be written completely before being read
      // Give time for the file to be written completely before being read
      {
      {
        static std::chrono::steady_clock::time_point write_delay_next_check_time =
        write_delay_next_check_time_ =
            std::chrono::steady_clock::now() +
            std::chrono::steady_clock::now() +
            std::chrono::steady_clock::duration(std::chrono::seconds(1));
            std::chrono::steady_clock::duration(std::chrono::seconds(1));
         if (!has_time_elapsed(write_delay_next_check_time)) {
         if (!has_time_elapsed(write_delay_next_check_time_)) {
           return;
           return;
         }
         }
      }
      }
@@ -241,19 +244,18 @@ void ScriptedBeacon::IncomingPacket(
}
}


void ScriptedBeacon::get_next_advertisement() {
void ScriptedBeacon::get_next_advertisement() {
  static int packet_num = 0;


  if (packet_num < ble_ad_list_.advertisements().size()) {
  if (packet_num_ < ble_ad_list_.advertisements().size()) {
    std::string payload = ble_ad_list_.advertisements(packet_num).payload();
    std::string payload = ble_ad_list_.advertisements(packet_num_).payload();
    std::string mac_address = ble_ad_list_.advertisements(packet_num).mac_address();
    std::string mac_address = ble_ad_list_.advertisements(packet_num_).mac_address();
    uint32_t delay_before_send_ms =
    uint32_t delay_before_send_ms =
        ble_ad_list_.advertisements(packet_num).delay_before_send_ms();
        ble_ad_list_.advertisements(packet_num_).delay_before_send_ms();
    next_ad_.ad.assign(payload.begin(), payload.end());
    next_ad_.ad.assign(payload.begin(), payload.end());
    Address::FromString(mac_address, next_ad_.address);
    Address::FromString(mac_address, next_ad_.address);
    next_ad_.ad_time = std::chrono::steady_clock::now() +
    next_ad_.ad_time = std::chrono::steady_clock::now() +
                      std::chrono::steady_clock::duration(
                      std::chrono::steady_clock::duration(
                          std::chrono::milliseconds(delay_before_send_ms));
                          std::chrono::milliseconds(delay_before_send_ms));
    packet_num++;
    packet_num_++;
  } else {
  } else {
    play_back_complete_ = true;
    play_back_complete_ = true;
    add_event(PlaybackEvent::PLAYBACK_ENDED);
    add_event(PlaybackEvent::PLAYBACK_ENDED);
+6 −0
Original line number Original line Diff line number Diff line
@@ -77,6 +77,12 @@ class ScriptedBeacon : public Beacon {
  void add_event(android::bluetooth::test_vendor_lib::model::devices::ScriptedBeaconBleAdProto::PlaybackEvent::PlaybackEventType type);
  void add_event(android::bluetooth::test_vendor_lib::model::devices::ScriptedBeaconBleAdProto::PlaybackEvent::PlaybackEventType type);


  Advertisement next_ad_{};
  Advertisement next_ad_{};
  int packet_num_{0};
  bool file_absence_logged_{false};
  PlaybackEvents playback_events_{};
  std::chrono::steady_clock::time_point next_check_time_{};
  std::chrono::steady_clock::time_point write_delay_next_check_time_{};



  android::bluetooth::test_vendor_lib::model::devices::ScriptedBeaconBleAdProto::BleAdvertisementList ble_ad_list_;
  android::bluetooth::test_vendor_lib::model::devices::ScriptedBeaconBleAdProto::BleAdvertisementList ble_ad_list_;