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