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

Commit fbd7629e authored by Jorge E. Moreira's avatar Jorge E. Moreira Committed by Myles Watson
Browse files

test_vendor: Refactor scheduling of dual_mode_controller event handling

Moved all controller actions out of read callbacks to task callbacks to
avoid race conditions when modifying internal controller state.

Change-Id: Ie8bb762992755db2dddb6b7bb6a4fe52ab2d0020
parent bd2f6958
Loading
Loading
Loading
Loading
+4 −5
Original line number Diff line number Diff line
@@ -46,6 +46,10 @@ class CommandPacket : public Packet {
  explicit CommandPacket(uint16_t opcode);
  CommandPacket(std::vector<uint8_t> header, std::vector<uint8_t> payload);

  CommandPacket(const CommandPacket&) = default;
  CommandPacket& operator=(const CommandPacket&) = default;
  CommandPacket(CommandPacket&&) = default;
  CommandPacket& operator=(CommandPacket&&) = default;
  virtual ~CommandPacket() override = default;

  // Returns the command opcode as defined in stack/include/hcidefs.h.
@@ -72,11 +76,6 @@ class CommandPacket : public Packet {

  // Size of a command packet header, which consists of a 2 octet opcode
  static const size_t kCommandHeaderSize = 2;

 private:
  // Disallow any copies of the singleton to be made.
  CommandPacket(const CommandPacket& cmdPckt) = delete;
  CommandPacket& operator=(const CommandPacket& cmdPckt) = delete;
};

}  // namespace test_vendor_lib
+0 −9
Original line number Diff line number Diff line
@@ -174,15 +174,6 @@ class DualModeController {
  void HandleTestChannelCommand(const std::string& name,
                                const std::vector<std::string>& args);

  // Sets the controller Handle* methods as callbacks for the transport to call
  // when data is received.
  void RegisterHandlersWithHciTransport(HciTransport& transport);

  // Sets the test channel handler with the transport dedicated to test channel
  // communications.
  void RegisterHandlersWithTestChannelTransport(
      TestChannelTransport& transport);

  // Set the callbacks for scheduling tasks.
  void RegisterTaskScheduler(
      std::function<AsyncTaskId(std::chrono::milliseconds, const TaskCallback&)>
+1 −1
Original line number Diff line number Diff line
@@ -54,7 +54,7 @@ class HciTransport {
      const std::function<void(std::unique_ptr<CommandPacket>)>& callback);

  // Blocks while it tries to writes the event to the vendor file descriptor.
  void PostEvent(const EventPacket& event);
  void SendEvent(std::unique_ptr<EventPacket> event);

  // Called when there is a command to read on |fd|.
  void OnCommandReady(int fd);
+1 −1
Original line number Diff line number Diff line
@@ -45,7 +45,7 @@ class PacketStream {

  // Sends an event to file descriptor |fd|. The ownership of the event is left
  // with the caller.
  bool SendEvent(const EventPacket& event, int fd) const;
  bool SendEvent(std::unique_ptr<EventPacket> event, int fd) const;

 private:
  // Checks if |type| is in the valid range from DATA_TYPE_COMMAND to
+2 −21
Original line number Diff line number Diff line
@@ -168,22 +168,6 @@ DualModeController::DualModeController()
#undef SET_TEST_HANDLER
}

void DualModeController::RegisterHandlersWithHciTransport(
    HciTransport& transport) {
  transport.RegisterCommandHandler(
      [this](std::unique_ptr<CommandPacket> command) {
        HandleCommand(std::move(command));
      });
}

void DualModeController::RegisterHandlersWithTestChannelTransport(
    TestChannelTransport& transport) {
  transport.RegisterCommandHandler(
      [this](const std::string& name, const vector<std::string>& args) {
        HandleTestChannelCommand(name, args);
      });
}

void DualModeController::RegisterTaskScheduler(
    std::function<AsyncTaskId(std::chrono::milliseconds, const TaskCallback&)>
        oneshotScheduler) {
@@ -212,11 +196,8 @@ void DualModeController::HandleTestChannelCommand(
void DualModeController::HandleCommand(
    std::unique_ptr<CommandPacket> command_packet) {
  uint16_t opcode = command_packet->GetOpcode();
  LOG_INFO(LOG_TAG,
           "Command opcode: 0x%04X, OGF: 0x%04X, OCF: 0x%04X",
           opcode,
           command_packet->GetOGF(),
           command_packet->GetOCF());
  LOG_INFO(LOG_TAG, "Command opcode: 0x%04X, OGF: 0x%04X, OCF: 0x%04X", opcode,
           command_packet->GetOGF(), command_packet->GetOCF());

  // The command hasn't been registered with the handler yet. There is nothing
  // to do.
Loading