OSDN Git Service

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