Home | History | Annotate | Download | only in include
      1 //
      2 // Copyright 2015 The Android Open Source Project
      3 //
      4 // Licensed under the Apache License, Version 2.0 (the "License");
      5 // you may not use this file except in compliance with the License.
      6 // You may obtain a copy of the License at
      7 //
      8 // http://www.apache.org/licenses/LICENSE-2.0
      9 //
     10 // Unless required by applicable law or agreed to in writing, software
     11 // distributed under the License is distributed on an "AS IS" BASIS,
     12 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
     13 // See the License for the specific language governing permissions and
     14 // limitations under the License.
     15 //
     16 
     17 #pragma once
     18 
     19 #include <string>
     20 #include <memory>
     21 
     22 #include "base/files/scoped_file.h"
     23 #include "base/message_loop/message_loop.h"
     24 
     25 namespace test_vendor_lib {
     26 
     27 // Manages communications between test channel and the controller. Mirrors the
     28 // HciTransport for the test channel.
     29 class TestChannelTransport : public base::MessageLoopForIO::Watcher {
     30  public:
     31   TestChannelTransport(bool enabled, int port);
     32 
     33   ~TestChannelTransport() = default;
     34 
     35   // Waits for a connection request from the test channel program and
     36   // allocates the file descriptor to watch for run-time parameters at. This
     37   // file descriptor gets stored in |fd_|.
     38   bool SetUp();
     39 
     40   int GetFd();
     41 
     42   // Because it imposes a different flow of work, the test channel must be
     43   // actively enabled to be used. |enabled_| is set by the vendor manager.
     44   bool IsEnabled();
     45 
     46   // Turns the test channel off for use in circumstances where an error occurs
     47   // and leaving the channel on would crash Bluetooth (e.g. if the test channel
     48   // is unable to bind to its socket, Bluetooth should still start without the
     49   // channel enabled).
     50   void Disable();
     51 
     52   // Sets the callback that fires when data is read in
     53   // |OnFileCanReadWithoutBlocking|.
     54   void RegisterCommandHandler(
     55       std::function<void(const std::string&, const std::vector<std::string>&)>
     56           callback);
     57 
     58  private:
     59   // base::MessageLoopForIO::Watcher overrides:
     60   void OnFileCanReadWithoutBlocking(int fd) override;
     61 
     62   void OnFileCanWriteWithoutBlocking(int fd) override;
     63 
     64   std::function<void(const std::string&, const std::vector<std::string>&)>
     65       command_handler_;
     66 
     67   // File descriptor to watch for test hook data.
     68   std::unique_ptr<base::ScopedFD> fd_;
     69 
     70   // TODO(dennischeng): Get port and enabled flag from a config file.
     71   int port_;
     72   bool enabled_;
     73 
     74   DISALLOW_COPY_AND_ASSIGN(TestChannelTransport);
     75 };
     76 
     77 }  // namespace test_vendor_lib
     78