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