Ian Coolidge | 611fcf9 | 2015-06-03 17:20:30 -0700 | [diff] [blame] | 1 | // |
| 2 | // Copyright (C) 2015 Google, Inc. |
| 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 | // |
Arman Uguray | ae43de6 | 2015-09-24 15:05:21 -0700 | [diff] [blame] | 16 | |
Pavlin Radoslavov | 1377f93 | 2016-02-11 19:44:47 -0800 | [diff] [blame^] | 17 | #ifdef BT_LIBCHROME_NDEBUG |
| 18 | #define NDEBUG 1 |
| 19 | #endif |
| 20 | |
Arman Uguray | 065d0f7 | 2015-07-16 18:12:13 -0700 | [diff] [blame] | 21 | #include <base/at_exit.h> |
| 22 | #include <base/command_line.h> |
| 23 | #include <base/files/scoped_file.h> |
Pavlin Radoslavov | 1377f93 | 2016-02-11 19:44:47 -0800 | [diff] [blame^] | 24 | #include <base/logging.h> |
Arman Uguray | 065d0f7 | 2015-07-16 18:12:13 -0700 | [diff] [blame] | 25 | |
Ian Coolidge | 611fcf9 | 2015-06-03 17:20:30 -0700 | [diff] [blame] | 26 | // For system properties |
| 27 | // TODO(icoolidge): abstraction or non-cutils stub. |
Arman Uguray | f2d6434 | 2015-07-08 15:47:39 -0700 | [diff] [blame] | 28 | #if !defined(OS_GENERIC) |
Ian Coolidge | 611fcf9 | 2015-06-03 17:20:30 -0700 | [diff] [blame] | 29 | #include <cutils/properties.h> |
Arman Uguray | f2d6434 | 2015-07-08 15:47:39 -0700 | [diff] [blame] | 30 | #endif // !defined(OS_GENERIC) |
Ian Coolidge | 611fcf9 | 2015-06-03 17:20:30 -0700 | [diff] [blame] | 31 | |
Arman Uguray | fe65fb7 | 2015-07-24 19:14:42 -0700 | [diff] [blame] | 32 | #include "service/daemon.h" |
Arman Uguray | 065d0f7 | 2015-07-16 18:12:13 -0700 | [diff] [blame] | 33 | #include "service/switches.h" |
Ian Coolidge | 611fcf9 | 2015-06-03 17:20:30 -0700 | [diff] [blame] | 34 | |
| 35 | namespace { |
| 36 | |
Arman Uguray | f2d6434 | 2015-07-08 15:47:39 -0700 | [diff] [blame] | 37 | // TODO(armansito): None of these should be hardcoded here. Instead, pass these |
| 38 | // via commandline. |
Ian Coolidge | 611fcf9 | 2015-06-03 17:20:30 -0700 | [diff] [blame] | 39 | const char kDisableProperty[] = "persist.bluetooth.disable"; |
Ian Coolidge | 611fcf9 | 2015-06-03 17:20:30 -0700 | [diff] [blame] | 40 | |
| 41 | } // namespace |
| 42 | |
Arman Uguray | 065d0f7 | 2015-07-16 18:12:13 -0700 | [diff] [blame] | 43 | int main(int argc, char *argv[]) { |
| 44 | base::AtExitManager exit_manager; |
| 45 | base::CommandLine::Init(argc, argv); |
| 46 | |
Arman Uguray | 503baaa | 2015-08-20 11:49:00 -0700 | [diff] [blame] | 47 | logging::LoggingSettings log_settings; |
| 48 | if (!logging::InitLogging(log_settings)) { |
| 49 | LOG(ERROR) << "Failed to set up logging"; |
| 50 | return EXIT_FAILURE; |
| 51 | } |
| 52 | |
Arman Uguray | 065d0f7 | 2015-07-16 18:12:13 -0700 | [diff] [blame] | 53 | // TODO(armansito): Initialize base/logging. By default it will dump to stdout |
| 54 | // but we might want to change that based on a command-line switch. Figure out |
| 55 | // how to route the logging to Android's syslog. Once that's done, we won't |
| 56 | // need to use osi/include/log.h anymore. |
| 57 | |
| 58 | // TODO(armansito): Register exit-time clean-up handlers for the IPC sockets. |
| 59 | // Register signal handlers. |
| 60 | auto command_line = base::CommandLine::ForCurrentProcess(); |
| 61 | if (command_line->HasSwitch(bluetooth::switches::kHelpLong) || |
| 62 | command_line->HasSwitch(bluetooth::switches::kHelpShort)) { |
| 63 | LOG(INFO) << bluetooth::switches::kHelpMessage; |
| 64 | return EXIT_SUCCESS; |
| 65 | } |
| 66 | |
Arman Uguray | f2d6434 | 2015-07-08 15:47:39 -0700 | [diff] [blame] | 67 | #if !defined(OS_GENERIC) |
Arman Uguray | 065d0f7 | 2015-07-16 18:12:13 -0700 | [diff] [blame] | 68 | // TODO(armansito): Remove Chromecast specific property out of here. This |
| 69 | // should just be obtained from global config. |
Ian Coolidge | 611fcf9 | 2015-06-03 17:20:30 -0700 | [diff] [blame] | 70 | char disable_value[PROPERTY_VALUE_MAX]; |
Arman Uguray | fe65fb7 | 2015-07-24 19:14:42 -0700 | [diff] [blame] | 71 | int status = property_get(kDisableProperty, disable_value, nullptr); |
Ian Coolidge | 611fcf9 | 2015-06-03 17:20:30 -0700 | [diff] [blame] | 72 | if (status && !strcmp(disable_value, "1")) { |
Arman Uguray | 065d0f7 | 2015-07-16 18:12:13 -0700 | [diff] [blame] | 73 | LOG(INFO) << "service disabled"; |
Ian Coolidge | 611fcf9 | 2015-06-03 17:20:30 -0700 | [diff] [blame] | 74 | return EXIT_SUCCESS; |
| 75 | } |
Arman Uguray | 065d0f7 | 2015-07-16 18:12:13 -0700 | [diff] [blame] | 76 | #endif // !defined(OS_GENERIC) |
Ian Coolidge | 611fcf9 | 2015-06-03 17:20:30 -0700 | [diff] [blame] | 77 | |
Arman Uguray | fe65fb7 | 2015-07-24 19:14:42 -0700 | [diff] [blame] | 78 | if (!bluetooth::Daemon::Initialize()) { |
| 79 | LOG(ERROR) << "Failed to initialize Daemon"; |
Arman Uguray | f2d6434 | 2015-07-08 15:47:39 -0700 | [diff] [blame] | 80 | return EXIT_FAILURE; |
| 81 | } |
| 82 | |
Arman Uguray | fe65fb7 | 2015-07-24 19:14:42 -0700 | [diff] [blame] | 83 | // Start the main event loop. |
| 84 | bluetooth::Daemon::Get()->StartMainLoop(); |
Arman Uguray | f2d6434 | 2015-07-08 15:47:39 -0700 | [diff] [blame] | 85 | |
Arman Uguray | fe65fb7 | 2015-07-24 19:14:42 -0700 | [diff] [blame] | 86 | // The main message loop has exited; clean up the Daemon. |
| 87 | bluetooth::Daemon::Get()->ShutDown(); |
Ian Coolidge | 611fcf9 | 2015-06-03 17:20:30 -0700 | [diff] [blame] | 88 | |
Ian Coolidge | 611fcf9 | 2015-06-03 17:20:30 -0700 | [diff] [blame] | 89 | return EXIT_SUCCESS; |
| 90 | } |