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

Commit 46da3a5f authored by Arman Uguray's avatar Arman Uguray
Browse files

service: Implement IBluetooth IsEnabled, Enable, and Disable

Implemented the IsEnabled, Enable, and Disable methods of the IBluetooth
interface. Added a simple REPL logic to client/main.cpp so that these commands
can be tested real-time via command-line.

Bug: 23169366
Change-Id: Id27a82e5cdadc5ea0b6f88d3ab3a6b7882f6212a
parent 360b22f8
Loading
Loading
Loading
Loading
+73 −5
Original line number Diff line number Diff line
@@ -14,17 +14,68 @@
//  limitations under the License.
//

#include <iostream>
#include <string>

#include <base/logging.h>

#include "service/ipc/binder/IBluetooth.h"

using namespace std;

using android::sp;

using ipc::binder::IBluetooth;

// TODO(armansito): Build a REPL into this client so that we make Binder calls
// based on user input. For now this just tests the IsEnabled() method and
// exits.
#define COLOR_OFF       "\x1B[0m"
#define COLOR_RED       "\x1B[0;91m"
#define COLOR_GREEN     "\x1B[0;92m"
#define COLOR_YELLOW    "\x1B[0;93m"
#define COLOR_BLUE      "\x1B[0;94m"
#define COLOR_MAGENTA   "\x1B[0;95m"
#define COLOR_BOLDGRAY  "\x1B[1;30m"
#define COLOR_BOLDWHITE "\x1B[1;37m"

const char kCommandDisable[] = "disable";
const char kCommandEnable[] = "enable";
const char kCommandGetState[] = "get-state";
const char kCommandIsEnabled[] = "is-enabled";

void PrintCommandStatus(bool status) {
  cout << COLOR_BOLDWHITE "Command status: " COLOR_OFF
       << (status ? (COLOR_GREEN "success") : (COLOR_RED "failure"))
       << COLOR_OFF << endl << endl;
}

void HandleDisable(IBluetooth* bt_iface) {
  PrintCommandStatus(bt_iface->Disable());
}

void HandleEnable(IBluetooth* bt_iface) {
  PrintCommandStatus(bt_iface->Enable());
}

void HandleGetState(IBluetooth* bt_iface) {
  // TODO(armansito): Implement.
}

void HandleIsEnabled(IBluetooth* bt_iface) {
  bool enabled = bt_iface->IsEnabled();
  cout << COLOR_BOLDWHITE "Adapter power state: " COLOR_OFF
       << (enabled ? "enabled" : "disabled") << endl
       << endl;
}

struct {
  string command;
  void (*func)(IBluetooth*);
} kCommandMap[] = {
  { "disable", HandleDisable },
  { "enable", HandleEnable },
  { "get-state", HandleGetState },
  { "is-enabled", HandleIsEnabled },
  {},
};

int main() {
  sp<IBluetooth> bt_iface = IBluetooth::getClientInterface();
@@ -33,8 +84,25 @@ int main() {
    return EXIT_FAILURE;
  }

  bool enabled = bt_iface->IsEnabled();
  LOG(INFO) << "IsEnabled(): " << enabled;
  cout << COLOR_BOLDWHITE << "Fluoride Command-Line Interface\n" << COLOR_OFF
       << endl;

  while (true) {
    string command;
    cout << COLOR_BLUE << "[FCLI] " << COLOR_OFF;
    getline(cin, command);

    bool command_handled = false;
    for (int i = 0; kCommandMap[i].func; i++) {
      if (command == kCommandMap[i].command) {
        kCommandMap[i].func(bt_iface.get());
        command_handled = true;
      }
    }

    if (!command_handled)
      cout << "Unrecognized command: " << command << endl;
  }

  return EXIT_SUCCESS;
}
+15 −1
Original line number Diff line number Diff line
@@ -75,7 +75,21 @@ android::status_t BnBluetooth::onTransact(
      reply->writeInt32(is_enabled);
      return android::NO_ERROR;
    }
    // TODO(armansito): Implement other functions here.
    case GET_STATE_TRANSACTION: {
      int state = GetState();
      reply->writeInt32(state);
      return android::NO_ERROR;
    }
    case ENABLE_TRANSACTION: {
      bool result = Enable();
      reply->writeInt32(result);
      return android::NO_ERROR;
    }
    case DISABLE_TRANSACTION: {
      bool result = Disable();
      reply->writeInt32(result);
      return android::NO_ERROR;
    }
    default:
      return BBinder::onTransact(code, data, reply, flags);
  }
+2 −0
Original line number Diff line number Diff line
@@ -56,6 +56,8 @@ class IBluetooth : public android::IInterface {
  DISALLOW_COPY_AND_ASSIGN(IBluetooth);
};

// TODO(armansito): Implement notification for when the process dies.

// The Binder server interface to IBluetooth. A class that implements IBluetooth
// must inherit from this class.
class BnBluetooth : public android::BnInterface<IBluetooth> {
+9 −6
Original line number Diff line number Diff line
@@ -18,6 +18,8 @@

#include <base/logging.h>

#include "service/adapter.h"

namespace ipc {

BluetoothBinderServer::BluetoothBinderServer(bluetooth::Adapter* adapter)
@@ -30,29 +32,30 @@ BluetoothBinderServer::~BluetoothBinderServer() {

// binder::BnBluetooth overrides:
bool BluetoothBinderServer::IsEnabled() {
  // TODO(armansito): Implement.
  VLOG(2) << __func__;
  return false;
  return adapter_->IsEnabled();
}

int BluetoothBinderServer::GetState() {
  VLOG(2) << __func__;
  // TODO(armansito): Implement.
  return -1;
}

bool BluetoothBinderServer::Enable() {
  // TODO(armansito): Implement.
  return false;
  VLOG(2) << __func__;
  return adapter_->Enable();
}

bool BluetoothBinderServer::EnableNoAutoConnect() {
  VLOG(2) << __func__;
  // TODO(armansito): Implement.
  return false;
}

bool BluetoothBinderServer::Disable() {
  // TODO(armansito): Implement.
  return false;
  VLOG(2) << __func__;
  return adapter_->Disable();
}

}  // namespace ipc
+5 −0
Original line number Diff line number Diff line
@@ -82,6 +82,11 @@ const char *BtStatusText(const bt_status_t status) {
    CASE_RETURN_TEXT(BT_STATUS_DONE);
    CASE_RETURN_TEXT(BT_STATUS_BUSY);
    CASE_RETURN_TEXT(BT_STATUS_UNSUPPORTED);
    CASE_RETURN_TEXT(BT_STATUS_PARM_INVALID);
    CASE_RETURN_TEXT(BT_STATUS_UNHANDLED);
    CASE_RETURN_TEXT(BT_STATUS_AUTH_FAILURE);
    CASE_RETURN_TEXT(BT_STATUS_RMT_DEV_DOWN);
    CASE_RETURN_TEXT(BT_STATUS_AUTH_REJECTED);
    default:
      return "unknown status code";
  }