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

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

Merge "Separate End, Reset, and Quit"

parents 8ff78fed 6d8c25ed
Loading
Loading
Loading
Loading
+20 −8
Original line number Diff line number Diff line
@@ -33,7 +33,7 @@ using test_vendor_lib::AsyncTaskId;
using test_vendor_lib::TaskCallback;

void TestEnvironment::initialize(std::promise<void> barrier) {
  LOG_INFO("%s", __func__);
  LOG_INFO();

  barrier_ = std::move(barrier);

@@ -41,12 +41,15 @@ void TestEnvironment::initialize(std::promise<void> barrier) {
  test_channel_transport_.RegisterCommandHandler(
      [this, user_id](const std::string& name,
                      const std::vector<std::string>& args) {
        async_manager_.ExecAsync(
            user_id, std::chrono::milliseconds(0),
            [this, name, args]() { test_channel_.HandleCommand(name, args); });
        async_manager_.ExecAsync(user_id, std::chrono::milliseconds(0),
                                 [this, name, args]() {
                                   if (name == "END_SIMULATION") {
                                     barrier_.set_value();
                                   } else {
                                     test_channel_.HandleCommand(name, args);
                                   }
                                 });
      });

  test_model_.Reset();

  SetUpTestChannel();
  SetUpHciServer([this](int fd) { test_model_.IncomingHciConnection(fd); });
@@ -175,13 +178,22 @@ void TestEnvironment::SetUpTestChannel() {
      return;
    }
    LOG_INFO("Test channel connection accepted.");
    if (test_channel_open_) {
      LOG_WARN("Only one connection at a time is supported");
      async_manager_.StopWatchingFileDescriptor(conn_fd);
      test_channel_transport_.SendResponse(conn_fd, "The connection is broken");
      return;
    }
    test_channel_open_ = true;
    test_channel_.RegisterSendResponse(
        [this, conn_fd](const std::string& response) { test_channel_transport_.SendResponse(conn_fd, response); });
        [this, conn_fd](const std::string& response) {
          test_channel_transport_.SendResponse(conn_fd, response);
        });

    async_manager_.WatchFdForNonBlockingReads(conn_fd, [this](int conn_fd) {
      test_channel_transport_.OnCommandReady(conn_fd, [this, conn_fd]() {
        async_manager_.StopWatchingFileDescriptor(conn_fd);
        barrier_.set_value();
        test_channel_open_ = false;
      });
    });
  });
+1 −0
Original line number Diff line number Diff line
@@ -50,6 +50,7 @@ class TestEnvironment {
  uint16_t hci_server_port_;
  uint16_t link_server_port_;
  std::string default_commands_file_;
  bool test_channel_open_{false};
  std::promise<void> barrier_;

  test_vendor_lib::AsyncManager async_manager_;
+5 −0
Original line number Diff line number Diff line
@@ -99,6 +99,7 @@ void TestChannelTransport::OnCommandReady(int fd, std::function<void(void)> unwa
  int bytes_read = read(fd, &command_name_size, 1);
  if (bytes_read != 1) {
    LOG_INFO("Unexpected (command_name_size) bytes_read: %d != %d", bytes_read, 1);
    close(fd);
  }
  vector<uint8_t> command_name_raw;
  command_name_raw.resize(command_name_size);
@@ -148,6 +149,10 @@ void TestChannelTransport::SendResponse(int fd, const std::string& response) con
  uint8_t size_buf[4] = {static_cast<uint8_t>(size & 0xff), static_cast<uint8_t>((size >> 8) & 0xff),
                         static_cast<uint8_t>((size >> 16) & 0xff), static_cast<uint8_t>((size >> 24) & 0xff)};
  int written = write(fd, size_buf, 4);
  if (written == -1 && errno == EBADFD) {
    LOG_WARN("Unable to send a response.  EBADFD");
    return;
  }
  ASSERT_LOG(written == 4, "What happened? written = %d errno = %d", written, errno);
  written = write(fd, response.c_str(), size);
  ASSERT_LOG(written == static_cast<int>(size), "What happened? written = %d errno = %d", written, errno);
+1 −0
Original line number Diff line number Diff line
@@ -49,6 +49,7 @@ TestCommandHandler::TestCommandHandler(TestModel& test_model) : model_(test_mode
  SET_HANDLER("set_timer_period", SetTimerPeriod);
  SET_HANDLER("start_timer", StartTimer);
  SET_HANDLER("stop_timer", StopTimer);
  SET_HANDLER("reset", Reset);
#undef SET_HANDLER
}

+16 −0
Original line number Diff line number Diff line
@@ -241,6 +241,22 @@ class TestChannelShell(cmd.Cmd):
        sleep_time = float(args.split()[0])
        time.sleep(sleep_time)

    def do_reset(self, args):
        """Arguments: None.

    Resets the simulation.
    """
        self._test_channel.send_command('reset', [])

    def do_end(self, args):
        """Arguments: None.

    Ends the simulation and exits.
    """
        self._test_channel.send_command('END_SIMULATION', [])
        print('Goodbye.')
        return True

    def do_quit(self, args):
        """Arguments: None.