1611fcf98316e28425abe28dbcc07b8d037653ceeIan Coolidge//
25b790feeeb211c42bf78ca3ae9c26aa30e516765Jakub Pawlowski//  Copyright 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
17065d0f709343129f82ff3d072dcb36f612038598Arman Uguray#include <base/at_exit.h>
18065d0f709343129f82ff3d072dcb36f612038598Arman Uguray#include <base/command_line.h>
19065d0f709343129f82ff3d072dcb36f612038598Arman Uguray#include <base/files/scoped_file.h>
201377f938abf30ab3104571fb8372c657e65237d0Pavlin Radoslavov#include <base/logging.h>
21065d0f709343129f82ff3d072dcb36f612038598Arman Uguray
22b4a42e7b9c277cfd71a575cbdb104013855b80cdBluetooth Build Test#include "osi/include/properties.h"
23fe65fb7978bc9257a36d1e5eae59c5f412dbdb49Arman Uguray#include "service/daemon.h"
24065d0f709343129f82ff3d072dcb36f612038598Arman Uguray#include "service/switches.h"
25611fcf98316e28425abe28dbcc07b8d037653ceeIan Coolidge
26611fcf98316e28425abe28dbcc07b8d037653ceeIan Coolidgenamespace {
27611fcf98316e28425abe28dbcc07b8d037653ceeIan Coolidge
28f2d6434a6b7ad98e20c7a601d2a0950237514837Arman Uguray// TODO(armansito): None of these should be hardcoded here. Instead, pass these
29f2d6434a6b7ad98e20c7a601d2a0950237514837Arman Uguray// via commandline.
30611fcf98316e28425abe28dbcc07b8d037653ceeIan Coolidgeconst char kDisableProperty[] = "persist.bluetooth.disable";
31611fcf98316e28425abe28dbcc07b8d037653ceeIan Coolidge
32611fcf98316e28425abe28dbcc07b8d037653ceeIan Coolidge}  // namespace
33611fcf98316e28425abe28dbcc07b8d037653ceeIan Coolidge
34911d1ae03efec2d54c3b1b605589d790d1745488Myles Watsonint main(int argc, char* argv[]) {
35065d0f709343129f82ff3d072dcb36f612038598Arman Uguray  base::AtExitManager exit_manager;
36065d0f709343129f82ff3d072dcb36f612038598Arman Uguray  base::CommandLine::Init(argc, argv);
37065d0f709343129f82ff3d072dcb36f612038598Arman Uguray
38503baaae63d798a5f25a7faa9206cd72f87927beArman Uguray  logging::LoggingSettings log_settings;
39503baaae63d798a5f25a7faa9206cd72f87927beArman Uguray  if (!logging::InitLogging(log_settings)) {
40503baaae63d798a5f25a7faa9206cd72f87927beArman Uguray    LOG(ERROR) << "Failed to set up logging";
41503baaae63d798a5f25a7faa9206cd72f87927beArman Uguray    return EXIT_FAILURE;
42503baaae63d798a5f25a7faa9206cd72f87927beArman Uguray  }
43503baaae63d798a5f25a7faa9206cd72f87927beArman Uguray
44065d0f709343129f82ff3d072dcb36f612038598Arman Uguray  // TODO(armansito): Initialize base/logging. By default it will dump to stdout
45065d0f709343129f82ff3d072dcb36f612038598Arman Uguray  // but we might want to change that based on a command-line switch. Figure out
46065d0f709343129f82ff3d072dcb36f612038598Arman Uguray  // how to route the logging to Android's syslog. Once that's done, we won't
47065d0f709343129f82ff3d072dcb36f612038598Arman Uguray  // need to use osi/include/log.h anymore.
48065d0f709343129f82ff3d072dcb36f612038598Arman Uguray
49065d0f709343129f82ff3d072dcb36f612038598Arman Uguray  // TODO(armansito): Register exit-time clean-up handlers for the IPC sockets.
50065d0f709343129f82ff3d072dcb36f612038598Arman Uguray  // Register signal handlers.
51065d0f709343129f82ff3d072dcb36f612038598Arman Uguray  auto command_line = base::CommandLine::ForCurrentProcess();
52065d0f709343129f82ff3d072dcb36f612038598Arman Uguray  if (command_line->HasSwitch(bluetooth::switches::kHelpLong) ||
53065d0f709343129f82ff3d072dcb36f612038598Arman Uguray      command_line->HasSwitch(bluetooth::switches::kHelpShort)) {
54065d0f709343129f82ff3d072dcb36f612038598Arman Uguray    LOG(INFO) << bluetooth::switches::kHelpMessage;
55065d0f709343129f82ff3d072dcb36f612038598Arman Uguray    return EXIT_SUCCESS;
56065d0f709343129f82ff3d072dcb36f612038598Arman Uguray  }
57065d0f709343129f82ff3d072dcb36f612038598Arman Uguray
58065d0f709343129f82ff3d072dcb36f612038598Arman Uguray  // TODO(armansito): Remove Chromecast specific property out of here. This
59065d0f709343129f82ff3d072dcb36f612038598Arman Uguray  // should just be obtained from global config.
60611fcf98316e28425abe28dbcc07b8d037653ceeIan Coolidge  char disable_value[PROPERTY_VALUE_MAX];
61158fe8e028c27d507c9bc17851e20d38a43ed153Myles Watson  int status = osi_property_get(kDisableProperty, disable_value, nullptr);
62611fcf98316e28425abe28dbcc07b8d037653ceeIan Coolidge  if (status && !strcmp(disable_value, "1")) {
63065d0f709343129f82ff3d072dcb36f612038598Arman Uguray    LOG(INFO) << "service disabled";
64611fcf98316e28425abe28dbcc07b8d037653ceeIan Coolidge    return EXIT_SUCCESS;
65611fcf98316e28425abe28dbcc07b8d037653ceeIan Coolidge  }
66611fcf98316e28425abe28dbcc07b8d037653ceeIan Coolidge
67fe65fb7978bc9257a36d1e5eae59c5f412dbdb49Arman Uguray  if (!bluetooth::Daemon::Initialize()) {
68fe65fb7978bc9257a36d1e5eae59c5f412dbdb49Arman Uguray    LOG(ERROR) << "Failed to initialize Daemon";
69611fcf98316e28425abe28dbcc07b8d037653ceeIan Coolidge    return EXIT_FAILURE;
70611fcf98316e28425abe28dbcc07b8d037653ceeIan Coolidge  }
71611fcf98316e28425abe28dbcc07b8d037653ceeIan Coolidge
72fe65fb7978bc9257a36d1e5eae59c5f412dbdb49Arman Uguray  // Start the main event loop.
73fe65fb7978bc9257a36d1e5eae59c5f412dbdb49Arman Uguray  bluetooth::Daemon::Get()->StartMainLoop();
74611fcf98316e28425abe28dbcc07b8d037653ceeIan Coolidge
75fe65fb7978bc9257a36d1e5eae59c5f412dbdb49Arman Uguray  // The main message loop has exited; clean up the Daemon.
76fe65fb7978bc9257a36d1e5eae59c5f412dbdb49Arman Uguray  bluetooth::Daemon::Get()->ShutDown();
77611fcf98316e28425abe28dbcc07b8d037653ceeIan Coolidge
78611fcf98316e28425abe28dbcc07b8d037653ceeIan Coolidge  return EXIT_SUCCESS;
79611fcf98316e28425abe28dbcc07b8d037653ceeIan Coolidge}
80