1 /* 2 * Copyright (C) 2016 The Android Open Source Project 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 "chre/core/init.h" 18 #include "chre/core/event.h" 19 #include "chre/core/event_loop.h" 20 #include "chre/core/event_loop_manager.h" 21 #include "chre/core/nanoapp.h" 22 #include "chre/core/static_nanoapps.h" 23 #ifdef CHRE_AUDIO_SUPPORT_ENABLED 24 #include "chre/platform/platform_audio.h" 25 #endif // CHRE_AUDIO_SUPPORT_ENABLED 26 #include "chre/platform/context.h" 27 #include "chre/platform/fatal_error.h" 28 #include "chre/platform/log.h" 29 #include "chre/platform/shared/platform_log.h" 30 #include "chre/platform/system_timer.h" 31 #include "chre/util/time.h" 32 33 #include <tclap/CmdLine.h> 34 #include <csignal> 35 #include <thread> 36 37 using chre::EventLoopManagerSingleton; 38 using chre::Milliseconds; 39 40 //! A description of the simulator. 41 constexpr char kSimDescription[] = 42 "A simulation environment for the Context Hub Runtime Environment (CHRE)"; 43 44 //! The version of the simulator. This is not super important but is assigned by 45 //! rules of semantic versioning. 46 constexpr char kSimVersion[] = "0.1.0"; 47 48 namespace { 49 50 extern "C" void signalHandler(int sig) { 51 (void)sig; 52 LOGI("Stop request received"); 53 EventLoopManagerSingleton::get()->getEventLoop().stop(); 54 } 55 56 } // namespace 57 58 int main(int argc, char **argv) { 59 try { 60 // Parse command-line arguments. 61 TCLAP::CmdLine cmd(kSimDescription, ' ', kSimVersion); 62 TCLAP::SwitchArg noStaticNanoappsArg("", "no_static_nanoapps", 63 "disable running static nanoapps", cmd, 64 false); 65 TCLAP::MultiArg<std::string> nanoappsArg( 66 "", "nanoapp", "nanoapp shared object to load and execute", false, 67 "path", cmd); 68 #ifdef CHRE_AUDIO_SUPPORT_ENABLED 69 TCLAP::ValueArg<std::string> audioFileArg( 70 "", "audio_file", "WAV file to open for audio simulation", false, "", 71 "path", cmd); 72 TCLAP::ValueArg<double> minAudioBufSizeArg( 73 "", "min_audio_buf_size", "min buffer size for audio simulation", false, 74 1.0, "seconds", cmd); 75 TCLAP::ValueArg<double> maxAudioBufSizeArg( 76 "", "max_audio_buf_size", "max buffer size for audio simulation", false, 77 10.0, "seconds", cmd); 78 #endif // CHRE_AUDIO_SUPPORT_ENABLED 79 cmd.parse(argc, argv); 80 81 // Initialize logging. 82 chre::PlatformLogSingleton::init(); 83 84 #ifdef CHRE_AUDIO_SUPPORT_ENABLED 85 // Initialize audio sources. 86 if (!audioFileArg.getValue().empty()) { 87 auto audioSource = chre::MakeUnique<chre::AudioSource>( 88 audioFileArg.getValue(), minAudioBufSizeArg.getValue(), 89 maxAudioBufSizeArg.getValue()); 90 chre::PlatformAudio::addAudioSource(audioSource); 91 } 92 93 // TODO(P1-d24c82): Add another command line argument that takes a json 94 // configuration to support multiple sources. 95 #endif // CHRE_AUDIO_SUPPORT_ENABLED 96 97 // Initialize the system. 98 chre::init(); 99 100 // Register a signal handler. 101 std::signal(SIGINT, signalHandler); 102 103 // Load any static nanoapps and start the event loop. 104 std::thread chreThread([&]() { 105 EventLoopManagerSingleton::get()->lateInit(); 106 107 // Load static nanoapps unless they are disabled by a command-line flag. 108 if (!noStaticNanoappsArg.getValue()) { 109 chre::loadStaticNanoapps(); 110 } 111 112 // Load dynamic nanoapps specified on the command-line. 113 chre::DynamicVector<chre::UniquePtr<chre::Nanoapp>> dynamicNanoapps; 114 for (const auto &nanoapp : nanoappsArg.getValue()) { 115 dynamicNanoapps.push_back(chre::MakeUnique<chre::Nanoapp>()); 116 dynamicNanoapps.back()->loadFromFile(nanoapp); 117 EventLoopManagerSingleton::get()->getEventLoop().startNanoapp( 118 dynamicNanoapps.back()); 119 } 120 121 EventLoopManagerSingleton::get()->getEventLoop().run(); 122 }); 123 chreThread.join(); 124 125 chre::deinit(); 126 chre::PlatformLogSingleton::deinit(); 127 } catch (TCLAP::ExitException) { 128 } 129 130 return 0; 131 } 132