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