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