main.cc revision 2117e520c9f5b105ade7e92c4ab4928ea905f176
1f52095257e36b887d7ddfd1f00871b9311dbfa1bArman Uguray//
2f52095257e36b887d7ddfd1f00871b9311dbfa1bArman Uguray//  Copyright (C) 2015 Google, Inc.
3f52095257e36b887d7ddfd1f00871b9311dbfa1bArman Uguray//
4f52095257e36b887d7ddfd1f00871b9311dbfa1bArman Uguray//  Licensed under the Apache License, Version 2.0 (the "License");
5f52095257e36b887d7ddfd1f00871b9311dbfa1bArman Uguray//  you may not use this file except in compliance with the License.
6f52095257e36b887d7ddfd1f00871b9311dbfa1bArman Uguray//  You may obtain a copy of the License at:
7f52095257e36b887d7ddfd1f00871b9311dbfa1bArman Uguray//
8f52095257e36b887d7ddfd1f00871b9311dbfa1bArman Uguray//  http://www.apache.org/licenses/LICENSE-2.0
9f52095257e36b887d7ddfd1f00871b9311dbfa1bArman Uguray//
10f52095257e36b887d7ddfd1f00871b9311dbfa1bArman Uguray//  Unless required by applicable law or agreed to in writing, software
11f52095257e36b887d7ddfd1f00871b9311dbfa1bArman Uguray//  distributed under the License is distributed on an "AS IS" BASIS,
12f52095257e36b887d7ddfd1f00871b9311dbfa1bArman Uguray//  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13f52095257e36b887d7ddfd1f00871b9311dbfa1bArman Uguray//  See the License for the specific language governing permissions and
14f52095257e36b887d7ddfd1f00871b9311dbfa1bArman Uguray//  limitations under the License.
15f52095257e36b887d7ddfd1f00871b9311dbfa1bArman Uguray//
16f52095257e36b887d7ddfd1f00871b9311dbfa1bArman Uguray
17fcf2e0391950a8b140082fbe78688fa89471fbedArman Uguray#include <iostream>
18fcf2e0391950a8b140082fbe78688fa89471fbedArman Uguray#include <string>
19fcf2e0391950a8b140082fbe78688fa89471fbedArman Uguray
20f52095257e36b887d7ddfd1f00871b9311dbfa1bArman Uguray#include <base/logging.h>
21f52095257e36b887d7ddfd1f00871b9311dbfa1bArman Uguray
222117e520c9f5b105ade7e92c4ab4928ea905f176Arman Uguray#include "service/adapter_state.h"
23f52095257e36b887d7ddfd1f00871b9311dbfa1bArman Uguray#include "service/ipc/binder/IBluetooth.h"
24f52095257e36b887d7ddfd1f00871b9311dbfa1bArman Uguray
25fcf2e0391950a8b140082fbe78688fa89471fbedArman Ugurayusing namespace std;
26fcf2e0391950a8b140082fbe78688fa89471fbedArman Uguray
27f52095257e36b887d7ddfd1f00871b9311dbfa1bArman Ugurayusing android::sp;
28f52095257e36b887d7ddfd1f00871b9311dbfa1bArman Uguray
29f52095257e36b887d7ddfd1f00871b9311dbfa1bArman Ugurayusing ipc::binder::IBluetooth;
30f52095257e36b887d7ddfd1f00871b9311dbfa1bArman Uguray
312117e520c9f5b105ade7e92c4ab4928ea905f176Arman Uguraynamespace {
322117e520c9f5b105ade7e92c4ab4928ea905f176Arman Uguray
332117e520c9f5b105ade7e92c4ab4928ea905f176Arman Uguray#define COLOR_OFF         "\x1B[0m"
342117e520c9f5b105ade7e92c4ab4928ea905f176Arman Uguray#define COLOR_RED         "\x1B[0;91m"
352117e520c9f5b105ade7e92c4ab4928ea905f176Arman Uguray#define COLOR_GREEN       "\x1B[0;92m"
362117e520c9f5b105ade7e92c4ab4928ea905f176Arman Uguray#define COLOR_YELLOW      "\x1B[0;93m"
372117e520c9f5b105ade7e92c4ab4928ea905f176Arman Uguray#define COLOR_BLUE        "\x1B[0;94m"
382117e520c9f5b105ade7e92c4ab4928ea905f176Arman Uguray#define COLOR_MAGENTA     "\x1B[0;95m"
392117e520c9f5b105ade7e92c4ab4928ea905f176Arman Uguray#define COLOR_BOLDGRAY    "\x1B[1;30m"
402117e520c9f5b105ade7e92c4ab4928ea905f176Arman Uguray#define COLOR_BOLDWHITE   "\x1B[1;37m"
412117e520c9f5b105ade7e92c4ab4928ea905f176Arman Uguray#define COLOR_BOLDYELLOW  "\x1B[1;93m"
42fcf2e0391950a8b140082fbe78688fa89471fbedArman Uguray
43fcf2e0391950a8b140082fbe78688fa89471fbedArman Ugurayconst char kCommandDisable[] = "disable";
44fcf2e0391950a8b140082fbe78688fa89471fbedArman Ugurayconst char kCommandEnable[] = "enable";
45fcf2e0391950a8b140082fbe78688fa89471fbedArman Ugurayconst char kCommandGetState[] = "get-state";
46fcf2e0391950a8b140082fbe78688fa89471fbedArman Ugurayconst char kCommandIsEnabled[] = "is-enabled";
47fcf2e0391950a8b140082fbe78688fa89471fbedArman Uguray
48fcf2e0391950a8b140082fbe78688fa89471fbedArman Ugurayvoid PrintCommandStatus(bool status) {
49fcf2e0391950a8b140082fbe78688fa89471fbedArman Uguray  cout << COLOR_BOLDWHITE "Command status: " COLOR_OFF
50fcf2e0391950a8b140082fbe78688fa89471fbedArman Uguray       << (status ? (COLOR_GREEN "success") : (COLOR_RED "failure"))
51fcf2e0391950a8b140082fbe78688fa89471fbedArman Uguray       << COLOR_OFF << endl << endl;
52fcf2e0391950a8b140082fbe78688fa89471fbedArman Uguray}
53fcf2e0391950a8b140082fbe78688fa89471fbedArman Uguray
54fcf2e0391950a8b140082fbe78688fa89471fbedArman Ugurayvoid HandleDisable(IBluetooth* bt_iface) {
55fcf2e0391950a8b140082fbe78688fa89471fbedArman Uguray  PrintCommandStatus(bt_iface->Disable());
56fcf2e0391950a8b140082fbe78688fa89471fbedArman Uguray}
57fcf2e0391950a8b140082fbe78688fa89471fbedArman Uguray
58fcf2e0391950a8b140082fbe78688fa89471fbedArman Ugurayvoid HandleEnable(IBluetooth* bt_iface) {
59fcf2e0391950a8b140082fbe78688fa89471fbedArman Uguray  PrintCommandStatus(bt_iface->Enable());
60fcf2e0391950a8b140082fbe78688fa89471fbedArman Uguray}
61fcf2e0391950a8b140082fbe78688fa89471fbedArman Uguray
62fcf2e0391950a8b140082fbe78688fa89471fbedArman Ugurayvoid HandleGetState(IBluetooth* bt_iface) {
632117e520c9f5b105ade7e92c4ab4928ea905f176Arman Uguray  bluetooth::AdapterState state = static_cast<bluetooth::AdapterState>(
642117e520c9f5b105ade7e92c4ab4928ea905f176Arman Uguray      bt_iface->GetState());
652117e520c9f5b105ade7e92c4ab4928ea905f176Arman Uguray  cout << COLOR_BOLDWHITE "Adapter state: " COLOR_OFF
662117e520c9f5b105ade7e92c4ab4928ea905f176Arman Uguray       << COLOR_BOLDYELLOW << bluetooth::AdapterStateToString(state)
672117e520c9f5b105ade7e92c4ab4928ea905f176Arman Uguray       << COLOR_OFF << endl << endl;
68fcf2e0391950a8b140082fbe78688fa89471fbedArman Uguray}
69fcf2e0391950a8b140082fbe78688fa89471fbedArman Uguray
70fcf2e0391950a8b140082fbe78688fa89471fbedArman Ugurayvoid HandleIsEnabled(IBluetooth* bt_iface) {
71fcf2e0391950a8b140082fbe78688fa89471fbedArman Uguray  bool enabled = bt_iface->IsEnabled();
722117e520c9f5b105ade7e92c4ab4928ea905f176Arman Uguray  cout << COLOR_BOLDWHITE "Adapter enabled: " COLOR_OFF
732117e520c9f5b105ade7e92c4ab4928ea905f176Arman Uguray       << (enabled ? "true" : "false") << endl
74fcf2e0391950a8b140082fbe78688fa89471fbedArman Uguray       << endl;
75fcf2e0391950a8b140082fbe78688fa89471fbedArman Uguray}
76fcf2e0391950a8b140082fbe78688fa89471fbedArman Uguray
772117e520c9f5b105ade7e92c4ab4928ea905f176Arman Ugurayvoid HandleHelp(IBluetooth* bt_iface);
782117e520c9f5b105ade7e92c4ab4928ea905f176Arman Uguray
79fcf2e0391950a8b140082fbe78688fa89471fbedArman Uguraystruct {
80fcf2e0391950a8b140082fbe78688fa89471fbedArman Uguray  string command;
81fcf2e0391950a8b140082fbe78688fa89471fbedArman Uguray  void (*func)(IBluetooth*);
822117e520c9f5b105ade7e92c4ab4928ea905f176Arman Uguray  string help;
83fcf2e0391950a8b140082fbe78688fa89471fbedArman Uguray} kCommandMap[] = {
842117e520c9f5b105ade7e92c4ab4928ea905f176Arman Uguray  { "help", HandleHelp, "\t\tDisplay this message" },
852117e520c9f5b105ade7e92c4ab4928ea905f176Arman Uguray  { "disable", HandleDisable, "\t\tDisable Bluetooth" },
862117e520c9f5b105ade7e92c4ab4928ea905f176Arman Uguray  { "enable", HandleEnable, "\t\tEnable Bluetooth" },
872117e520c9f5b105ade7e92c4ab4928ea905f176Arman Uguray  { "get-state", HandleGetState, "\tGet the current adapter state" },
882117e520c9f5b105ade7e92c4ab4928ea905f176Arman Uguray  { "is-enabled", HandleIsEnabled, "\tReturn if Bluetooth is enabled" },
89fcf2e0391950a8b140082fbe78688fa89471fbedArman Uguray  {},
90fcf2e0391950a8b140082fbe78688fa89471fbedArman Uguray};
91f52095257e36b887d7ddfd1f00871b9311dbfa1bArman Uguray
922117e520c9f5b105ade7e92c4ab4928ea905f176Arman Ugurayvoid HandleHelp(IBluetooth* /* bt_iface */) {
932117e520c9f5b105ade7e92c4ab4928ea905f176Arman Uguray  cout << endl;
942117e520c9f5b105ade7e92c4ab4928ea905f176Arman Uguray  for (int i = 0; kCommandMap[i].func; i++)
952117e520c9f5b105ade7e92c4ab4928ea905f176Arman Uguray    cout << "\t" << kCommandMap[i].command << kCommandMap[i].help << endl;
962117e520c9f5b105ade7e92c4ab4928ea905f176Arman Uguray  cout << endl;
972117e520c9f5b105ade7e92c4ab4928ea905f176Arman Uguray}
982117e520c9f5b105ade7e92c4ab4928ea905f176Arman Uguray
992117e520c9f5b105ade7e92c4ab4928ea905f176Arman Uguray}  // namespace
1002117e520c9f5b105ade7e92c4ab4928ea905f176Arman Uguray
101f52095257e36b887d7ddfd1f00871b9311dbfa1bArman Ugurayint main() {
102f52095257e36b887d7ddfd1f00871b9311dbfa1bArman Uguray  sp<IBluetooth> bt_iface = IBluetooth::getClientInterface();
103f52095257e36b887d7ddfd1f00871b9311dbfa1bArman Uguray  if (!bt_iface.get()) {
104f52095257e36b887d7ddfd1f00871b9311dbfa1bArman Uguray    LOG(ERROR) << "Failed to obtain handle on IBluetooth";
105f52095257e36b887d7ddfd1f00871b9311dbfa1bArman Uguray    return EXIT_FAILURE;
106f52095257e36b887d7ddfd1f00871b9311dbfa1bArman Uguray  }
107f52095257e36b887d7ddfd1f00871b9311dbfa1bArman Uguray
108fcf2e0391950a8b140082fbe78688fa89471fbedArman Uguray  cout << COLOR_BOLDWHITE << "Fluoride Command-Line Interface\n" << COLOR_OFF
1092117e520c9f5b105ade7e92c4ab4928ea905f176Arman Uguray       << endl
1102117e520c9f5b105ade7e92c4ab4928ea905f176Arman Uguray       << "Type \"help\" to see possible commands.\n"
111fcf2e0391950a8b140082fbe78688fa89471fbedArman Uguray       << endl;
112fcf2e0391950a8b140082fbe78688fa89471fbedArman Uguray
113fcf2e0391950a8b140082fbe78688fa89471fbedArman Uguray  while (true) {
114fcf2e0391950a8b140082fbe78688fa89471fbedArman Uguray    string command;
115fcf2e0391950a8b140082fbe78688fa89471fbedArman Uguray    cout << COLOR_BLUE << "[FCLI] " << COLOR_OFF;
116fcf2e0391950a8b140082fbe78688fa89471fbedArman Uguray    getline(cin, command);
117fcf2e0391950a8b140082fbe78688fa89471fbedArman Uguray
118fcf2e0391950a8b140082fbe78688fa89471fbedArman Uguray    bool command_handled = false;
1192117e520c9f5b105ade7e92c4ab4928ea905f176Arman Uguray    for (int i = 0; kCommandMap[i].func && !command_handled; i++) {
120fcf2e0391950a8b140082fbe78688fa89471fbedArman Uguray      if (command == kCommandMap[i].command) {
121fcf2e0391950a8b140082fbe78688fa89471fbedArman Uguray        kCommandMap[i].func(bt_iface.get());
122fcf2e0391950a8b140082fbe78688fa89471fbedArman Uguray        command_handled = true;
123fcf2e0391950a8b140082fbe78688fa89471fbedArman Uguray      }
124fcf2e0391950a8b140082fbe78688fa89471fbedArman Uguray    }
125fcf2e0391950a8b140082fbe78688fa89471fbedArman Uguray
126fcf2e0391950a8b140082fbe78688fa89471fbedArman Uguray    if (!command_handled)
127fcf2e0391950a8b140082fbe78688fa89471fbedArman Uguray      cout << "Unrecognized command: " << command << endl;
128fcf2e0391950a8b140082fbe78688fa89471fbedArman Uguray  }
129f52095257e36b887d7ddfd1f00871b9311dbfa1bArman Uguray
130f52095257e36b887d7ddfd1f00871b9311dbfa1bArman Uguray  return EXIT_SUCCESS;
131f52095257e36b887d7ddfd1f00871b9311dbfa1bArman Uguray}
132