blob: 2f28815ad7b80d2630328de85e7e6cb14e83e01c [file] [log] [blame]
Ian Coolidge611fcf92015-06-03 17:20:30 -07001//
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 Uguray065d0f72015-07-16 18:12:13 -070016#include <base/at_exit.h>
17#include <base/command_line.h>
18#include <base/files/scoped_file.h>
19
Ian Coolidge611fcf92015-06-03 17:20:30 -070020// For system properties
21// TODO(icoolidge): abstraction or non-cutils stub.
Arman Ugurayf2d64342015-07-08 15:47:39 -070022#if !defined(OS_GENERIC)
Ian Coolidge611fcf92015-06-03 17:20:30 -070023#include <cutils/properties.h>
Arman Ugurayf2d64342015-07-08 15:47:39 -070024#endif // !defined(OS_GENERIC)
Ian Coolidge611fcf92015-06-03 17:20:30 -070025
Arman Ugurayfe65fb72015-07-24 19:14:42 -070026#include "service/daemon.h"
Arman Uguray065d0f72015-07-16 18:12:13 -070027#include "service/switches.h"
Ian Coolidge611fcf92015-06-03 17:20:30 -070028
29namespace {
30
Arman Ugurayf2d64342015-07-08 15:47:39 -070031// TODO(armansito): None of these should be hardcoded here. Instead, pass these
32// via commandline.
Ian Coolidge611fcf92015-06-03 17:20:30 -070033const char kDisableProperty[] = "persist.bluetooth.disable";
Ian Coolidge611fcf92015-06-03 17:20:30 -070034
35} // namespace
36
Arman Uguray065d0f72015-07-16 18:12:13 -070037int main(int argc, char *argv[]) {
38 base::AtExitManager exit_manager;
39 base::CommandLine::Init(argc, argv);
40
41 // TODO(armansito): Initialize base/logging. By default it will dump to stdout
42 // but we might want to change that based on a command-line switch. Figure out
43 // how to route the logging to Android's syslog. Once that's done, we won't
44 // need to use osi/include/log.h anymore.
45
46 // TODO(armansito): Register exit-time clean-up handlers for the IPC sockets.
47 // Register signal handlers.
48 auto command_line = base::CommandLine::ForCurrentProcess();
49 if (command_line->HasSwitch(bluetooth::switches::kHelpLong) ||
50 command_line->HasSwitch(bluetooth::switches::kHelpShort)) {
51 LOG(INFO) << bluetooth::switches::kHelpMessage;
52 return EXIT_SUCCESS;
53 }
54
Arman Ugurayf2d64342015-07-08 15:47:39 -070055#if !defined(OS_GENERIC)
Arman Uguray065d0f72015-07-16 18:12:13 -070056 // TODO(armansito): Remove Chromecast specific property out of here. This
57 // should just be obtained from global config.
Ian Coolidge611fcf92015-06-03 17:20:30 -070058 char disable_value[PROPERTY_VALUE_MAX];
Arman Ugurayfe65fb72015-07-24 19:14:42 -070059 int status = property_get(kDisableProperty, disable_value, nullptr);
Ian Coolidge611fcf92015-06-03 17:20:30 -070060 if (status && !strcmp(disable_value, "1")) {
Arman Uguray065d0f72015-07-16 18:12:13 -070061 LOG(INFO) << "service disabled";
Ian Coolidge611fcf92015-06-03 17:20:30 -070062 return EXIT_SUCCESS;
63 }
Arman Uguray065d0f72015-07-16 18:12:13 -070064#endif // !defined(OS_GENERIC)
Ian Coolidge611fcf92015-06-03 17:20:30 -070065
Arman Ugurayfe65fb72015-07-24 19:14:42 -070066 if (!bluetooth::Daemon::Initialize()) {
67 LOG(ERROR) << "Failed to initialize Daemon";
Arman Ugurayf2d64342015-07-08 15:47:39 -070068 return EXIT_FAILURE;
69 }
70
Arman Ugurayfe65fb72015-07-24 19:14:42 -070071 // Start the main event loop.
72 bluetooth::Daemon::Get()->StartMainLoop();
Arman Ugurayf2d64342015-07-08 15:47:39 -070073
Arman Ugurayfe65fb72015-07-24 19:14:42 -070074 // The main message loop has exited; clean up the Daemon.
75 bluetooth::Daemon::Get()->ShutDown();
Ian Coolidge611fcf92015-06-03 17:20:30 -070076
Ian Coolidge611fcf92015-06-03 17:20:30 -070077 return EXIT_SUCCESS;
78}