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

Commit 139a4d2e authored by Dennis Cheng's avatar Dennis Cheng
Browse files

test_vendor_lib: Add test channel command transport and handler

This upload adds the transport and handler functionality for the test
channel. Test channel commands will be sent by the test channel
program and processed by library objects in a manner analogous to how
packets are received and handled from the HCI. The actual
implementation for the 4 test channel commands included here will be
in the next upload.

Bug: 21586676
Change-Id: I9354baedcbd6e48b8920a3ebf1afa69eb89834d9
parent 70da5c11
Loading
Loading
Loading
Loading
+2 −0
Original line number Diff line number Diff line
@@ -13,6 +13,8 @@ LOCAL_SRC_FILES := \
    src/hci_transport.cc \
    src/packet.cc \
    src/packet_stream.cc \
    src/test_channel_handler.cc \
    src/test_channel_transport.cc \
    src/vendor_manager.cc \

LOCAL_C_INCLUDES := \
+24 −0
Original line number Diff line number Diff line
@@ -17,10 +17,12 @@
#pragma once

#include <cstdint>
#include <string>
#include <vector>
#include <unordered_map>

#include "vendor_libs/test_vendor_lib/include/hci_handler.h"
#include "vendor_libs/test_vendor_lib/include/test_channel_handler.h"

namespace test_vendor_lib {

@@ -48,6 +50,11 @@ class DualModeController {
  // in which commands are made available by which controller.
  void RegisterCommandsWithHandler(HciHandler& handler);

  // Registers test channel command callbacks with the TestChannelHandler
  // instance so that they are fired when the corresponding command name is
  // received from the test channel.
  void RegisterCommandsWithTestChannelHandler(TestChannelHandler& handler);

  // Sets the callback to be used for sending events back to the HCI.
  void RegisterEventChannel(
      std::function<void(std::unique_ptr<EventPacket>)> send_event);
@@ -317,7 +324,17 @@ class DualModeController {
  //           is halted.
  void HciInquiry(const std::vector<std::uint8_t>& args);

  void UciTimeoutAll(const std::vector<std::uint8_t>& args);

 private:
  enum State {
    kStandby,  // Not receiving/transmitting any packets from/to other devices.
    kAdvertising,  // Transmitting advertising packets.
    kScanning,  // Listening for advertising packets.
    kInitiating,  // Listening for advertising packets from a specific device.
    kConnection,  // In a connection.
  };

  // Creates a command complete event and sends it back to the HCI.
  void SendCommandComplete(uint16_t command_opcode,
                           const std::vector<uint8_t>& return_parameters) const;
@@ -348,6 +365,10 @@ class DualModeController {
                     std::function<void(const std::vector<std::uint8_t>&)>>
      active_commands_;

  std::unordered_map<std::string,
                     std::function<void(const std::vector<std::uint8_t>&)>>
      test_channel_active_commands_;

  // Specifies the format of Inquiry Result events to be returned during the
  // Inquiry command.
  // 0x00: Standard Inquiry Result event format (default).
@@ -356,6 +377,9 @@ class DualModeController {
  // 0x03-0xFF: Reserved.
  std::uint8_t inquiry_mode_;

  // Current link layer state of the controller.
  State state_;

  DISALLOW_COPY_AND_ASSIGN(DualModeController);
};

+1 −1
Original line number Diff line number Diff line
@@ -58,7 +58,7 @@ class HciHandler {

 private:
  // Controller callbacks to be executed in handlers and registered in
  // RegisterControllerCallback().
  // RegisterControllerCommand().
  std::unordered_map<std::uint16_t,
                     std::function<void(const std::vector<std::uint8_t> args)> >
      commands_;
+61 −0
Original line number Diff line number Diff line
//
// Copyright 2015 The Android Open Source Project
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//

#pragma once

#include <cstdint>
#include <memory>
#include <string>
#include <unordered_map>
#include <vector>

#include "vendor_libs/test_vendor_lib/include/test_channel_transport.h"

namespace test_vendor_lib {

// Manages mappings from test channel commands to test channel callbacks
// provided by the controller. Parallels the functionality of the HciHandler for
// the test channel.
class TestChannelHandler {
 public:
  TestChannelHandler() = default;

  ~TestChannelHandler() = default;

  // Callback to be fired when a command is received from the test channel.
  void HandleTestCommand(std::string command_name,
                         std::vector<std::uint8_t> args);

  // Creates the mapping from the |command_name| to the method |callback|,
  // which is provided by the controller and will be fired when its command is
  // received from the test channel.
  void RegisterControllerCommand(
      std::string command_name,
      std::function<void(const std::vector<std::uint8_t> args)> callback);

  void RegisterHandlersWithTransport(TestChannelTransport& transport);

 private:
  // Controller callbacks to be executed in handlers and registered in
  // RegisterControllerCommand().
  std::unordered_map<std::string,
                     std::function<void(const std::vector<std::uint8_t> args)> >
      commands_;

  DISALLOW_COPY_AND_ASSIGN(TestChannelHandler);
};

}  // namespace test_vendor_lib
+76 −0
Original line number Diff line number Diff line
//
// Copyright 2015 The Android Open Source Project
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//

#pragma once

#include <string>
#include <memory>

#include "base/files/scoped_file.h"
#include "base/message_loop/message_loop.h"

namespace test_vendor_lib {

// Manages communications between test channel and the controller. Mirrors the
// HciTransport for the test channel.
class TestChannelTransport : public base::MessageLoopForIO::Watcher {
 public:
  TestChannelTransport(bool enabled, int port);

  ~TestChannelTransport() = default;

  // Waits for a connection request from the test channel program and
  // allocates the file descriptor to watch for run-time parameters at. This
  // file descriptor gets stored in |fd_|.
  bool SetUp();

  int GetFd();

  // Because it imposes a different flow of work, the test channel must be
  // actively enabled to be used. |enabled_| is set by the vendor manager.
  bool IsEnabled();

  // Turns the test channel off for use in circumstances where an error occurs
  // and leaving the channel on would crash Bluetooth (e.g. if the test channel
  // is unable to bind to its socket, Bluetooth should still start without the
  // channel enabled).
  void Disable();

  // Sets the callback that fires when data is read in
  // |OnFileCanReadWithoutBlocking|.
  void RegisterCommandHandler(
      std::function<void(std::string, std::vector<std::uint8_t>)> callback);

 private:
  // base::MessageLoopForIO::Watcher overrides:
  void OnFileCanReadWithoutBlocking(int fd) override;

  void OnFileCanWriteWithoutBlocking(int fd) override;

  std::function<void(std::string, std::vector<std::uint8_t>)>
      command_handler_;

  // File descriptor to watch for test hook data.
  std::unique_ptr<base::ScopedFD> fd_;

  // TODO(dennischeng): Get port and enabled flag from a config file.
  int port_;
  bool enabled_;

  DISALLOW_COPY_AND_ASSIGN(TestChannelTransport);
};

}  // namespace test_vendor_lib
Loading