1611fcf98316e28425abe28dbcc07b8d037653ceeIan Coolidge//
2611fcf98316e28425abe28dbcc07b8d037653ceeIan Coolidge//  Copyright (C) 2015 Google, Inc.
3611fcf98316e28425abe28dbcc07b8d037653ceeIan Coolidge//
4611fcf98316e28425abe28dbcc07b8d037653ceeIan Coolidge//  Licensed under the Apache License, Version 2.0 (the "License");
5611fcf98316e28425abe28dbcc07b8d037653ceeIan Coolidge//  you may not use this file except in compliance with the License.
6611fcf98316e28425abe28dbcc07b8d037653ceeIan Coolidge//  You may obtain a copy of the License at:
7611fcf98316e28425abe28dbcc07b8d037653ceeIan Coolidge//
8611fcf98316e28425abe28dbcc07b8d037653ceeIan Coolidge//  http://www.apache.org/licenses/LICENSE-2.0
9611fcf98316e28425abe28dbcc07b8d037653ceeIan Coolidge//
10611fcf98316e28425abe28dbcc07b8d037653ceeIan Coolidge//  Unless required by applicable law or agreed to in writing, software
11611fcf98316e28425abe28dbcc07b8d037653ceeIan Coolidge//  distributed under the License is distributed on an "AS IS" BASIS,
12611fcf98316e28425abe28dbcc07b8d037653ceeIan Coolidge//  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13611fcf98316e28425abe28dbcc07b8d037653ceeIan Coolidge//  See the License for the specific language governing permissions and
14611fcf98316e28425abe28dbcc07b8d037653ceeIan Coolidge//  limitations under the License.
15611fcf98316e28425abe28dbcc07b8d037653ceeIan Coolidge//
16ae43de627b9c91e54215d439149c0e01599249c0Arman Uguray
170b60bb04b966030fd1dfdffe68099da8f280ade0Pavlin Radoslavov#ifdef BT_LIBCHROME_NDEBUG
180b60bb04b966030fd1dfdffe68099da8f280ade0Pavlin Radoslavov#define NDEBUG 1
190b60bb04b966030fd1dfdffe68099da8f280ade0Pavlin Radoslavov#endif
200b60bb04b966030fd1dfdffe68099da8f280ade0Pavlin Radoslavov
21065d0f709343129f82ff3d072dcb36f612038598Arman Uguray#include <base/at_exit.h>
22065d0f709343129f82ff3d072dcb36f612038598Arman Uguray#include <base/command_line.h>
23065d0f709343129f82ff3d072dcb36f612038598Arman Uguray#include <base/files/scoped_file.h>
240b60bb04b966030fd1dfdffe68099da8f280ade0Pavlin Radoslavov#include <base/logging.h>
25065d0f709343129f82ff3d072dcb36f612038598Arman Uguray
26611fcf98316e28425abe28dbcc07b8d037653ceeIan Coolidge// For system properties
27611fcf98316e28425abe28dbcc07b8d037653ceeIan Coolidge// TODO(icoolidge): abstraction or non-cutils stub.
28f2d6434a6b7ad98e20c7a601d2a0950237514837Arman Uguray#if !defined(OS_GENERIC)
29611fcf98316e28425abe28dbcc07b8d037653ceeIan Coolidge#include <cutils/properties.h>
30f2d6434a6b7ad98e20c7a601d2a0950237514837Arman Uguray#endif  // !defined(OS_GENERIC)
31611fcf98316e28425abe28dbcc07b8d037653ceeIan Coolidge
32fe65fb7978bc9257a36d1e5eae59c5f412dbdb49Arman Uguray#include "service/daemon.h"
33065d0f709343129f82ff3d072dcb36f612038598Arman Uguray#include "service/switches.h"
34611fcf98316e28425abe28dbcc07b8d037653ceeIan Coolidge
35611fcf98316e28425abe28dbcc07b8d037653ceeIan Coolidgenamespace {
36611fcf98316e28425abe28dbcc07b8d037653ceeIan Coolidge
37f2d6434a6b7ad98e20c7a601d2a0950237514837Arman Uguray// TODO(armansito): None of these should be hardcoded here. Instead, pass these
38f2d6434a6b7ad98e20c7a601d2a0950237514837Arman Uguray// via commandline.
39611fcf98316e28425abe28dbcc07b8d037653ceeIan Coolidgeconst char kDisableProperty[] = "persist.bluetooth.disable";
40611fcf98316e28425abe28dbcc07b8d037653ceeIan Coolidge
41611fcf98316e28425abe28dbcc07b8d037653ceeIan Coolidge}  // namespace
42611fcf98316e28425abe28dbcc07b8d037653ceeIan Coolidge
43065d0f709343129f82ff3d072dcb36f612038598Arman Ugurayint main(int argc, char *argv[]) {
44065d0f709343129f82ff3d072dcb36f612038598Arman Uguray  base::AtExitManager exit_manager;
45065d0f709343129f82ff3d072dcb36f612038598Arman Uguray  base::CommandLine::Init(argc, argv);
46065d0f709343129f82ff3d072dcb36f612038598Arman Uguray
47503baaae63d798a5f25a7faa9206cd72f87927beArman Uguray  logging::LoggingSettings log_settings;
48503baaae63d798a5f25a7faa9206cd72f87927beArman Uguray  if (!logging::InitLogging(log_settings)) {
49503baaae63d798a5f25a7faa9206cd72f87927beArman Uguray    LOG(ERROR) << "Failed to set up logging";
50503baaae63d798a5f25a7faa9206cd72f87927beArman Uguray    return EXIT_FAILURE;
51503baaae63d798a5f25a7faa9206cd72f87927beArman Uguray  }
52503baaae63d798a5f25a7faa9206cd72f87927beArman Uguray
53065d0f709343129f82ff3d072dcb36f612038598Arman Uguray  // TODO(armansito): Initialize base/logging. By default it will dump to stdout
54065d0f709343129f82ff3d072dcb36f612038598Arman Uguray  // but we might want to change that based on a command-line switch. Figure out
55065d0f709343129f82ff3d072dcb36f612038598Arman Uguray  // how to route the logging to Android's syslog. Once that's done, we won't
56065d0f709343129f82ff3d072dcb36f612038598Arman Uguray  // need to use osi/include/log.h anymore.
57065d0f709343129f82ff3d072dcb36f612038598Arman Uguray
58065d0f709343129f82ff3d072dcb36f612038598Arman Uguray  // TODO(armansito): Register exit-time clean-up handlers for the IPC sockets.
59065d0f709343129f82ff3d072dcb36f612038598Arman Uguray  // Register signal handlers.
60065d0f709343129f82ff3d072dcb36f612038598Arman Uguray  auto command_line = base::CommandLine::ForCurrentProcess();
61065d0f709343129f82ff3d072dcb36f612038598Arman Uguray  if (command_line->HasSwitch(bluetooth::switches::kHelpLong) ||
62065d0f709343129f82ff3d072dcb36f612038598Arman Uguray      command_line->HasSwitch(bluetooth::switches::kHelpShort)) {
63065d0f709343129f82ff3d072dcb36f612038598Arman Uguray    LOG(INFO) << bluetooth::switches::kHelpMessage;
64065d0f709343129f82ff3d072dcb36f612038598Arman Uguray    return EXIT_SUCCESS;
65065d0f709343129f82ff3d072dcb36f612038598Arman Uguray  }
66065d0f709343129f82ff3d072dcb36f612038598Arman Uguray
67f2d6434a6b7ad98e20c7a601d2a0950237514837Arman Uguray#if !defined(OS_GENERIC)
68065d0f709343129f82ff3d072dcb36f612038598Arman Uguray  // TODO(armansito): Remove Chromecast specific property out of here. This
69065d0f709343129f82ff3d072dcb36f612038598Arman Uguray  // should just be obtained from global config.
70611fcf98316e28425abe28dbcc07b8d037653ceeIan Coolidge  char disable_value[PROPERTY_VALUE_MAX];
71fe65fb7978bc9257a36d1e5eae59c5f412dbdb49Arman Uguray  int status = property_get(kDisableProperty, disable_value, nullptr);
72611fcf98316e28425abe28dbcc07b8d037653ceeIan Coolidge  if (status && !strcmp(disable_value, "1")) {
73065d0f709343129f82ff3d072dcb36f612038598Arman Uguray    LOG(INFO) << "service disabled";
74611fcf98316e28425abe28dbcc07b8d037653ceeIan Coolidge    return EXIT_SUCCESS;
75611fcf98316e28425abe28dbcc07b8d037653ceeIan Coolidge  }
76065d0f709343129f82ff3d072dcb36f612038598Arman Uguray#endif  // !defined(OS_GENERIC)
77611fcf98316e28425abe28dbcc07b8d037653ceeIan Coolidge
78fe65fb7978bc9257a36d1e5eae59c5f412dbdb49Arman Uguray  if (!bluetooth::Daemon::Initialize()) {
79fe65fb7978bc9257a36d1e5eae59c5f412dbdb49Arman Uguray    LOG(ERROR) << "Failed to initialize Daemon";
80611fcf98316e28425abe28dbcc07b8d037653ceeIan Coolidge    return EXIT_FAILURE;
81611fcf98316e28425abe28dbcc07b8d037653ceeIan Coolidge  }
82611fcf98316e28425abe28dbcc07b8d037653ceeIan Coolidge
83fe65fb7978bc9257a36d1e5eae59c5f412dbdb49Arman Uguray  // Start the main event loop.
84fe65fb7978bc9257a36d1e5eae59c5f412dbdb49Arman Uguray  bluetooth::Daemon::Get()->StartMainLoop();
85611fcf98316e28425abe28dbcc07b8d037653ceeIan Coolidge
86fe65fb7978bc9257a36d1e5eae59c5f412dbdb49Arman Uguray  // The main message loop has exited; clean up the Daemon.
87fe65fb7978bc9257a36d1e5eae59c5f412dbdb49Arman Uguray  bluetooth::Daemon::Get()->ShutDown();
88611fcf98316e28425abe28dbcc07b8d037653ceeIan Coolidge
89611fcf98316e28425abe28dbcc07b8d037653ceeIan Coolidge  return EXIT_SUCCESS;
90611fcf98316e28425abe28dbcc07b8d037653ceeIan Coolidge}
91