aboutsummaryrefslogtreecommitdiff
path: root/platform/linux/init.cc
blob: 1e3572de41e56a94b8fb2836f8651c0de34649e1 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
/*
 * Copyright (C) 2016 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#include "chre/core/init.h"
#include "chre/core/event.h"
#include "chre/core/event_loop.h"
#include "chre/core/event_loop_manager.h"
#include "chre/core/nanoapp.h"
#include "chre/core/static_nanoapps.h"
#ifdef CHRE_AUDIO_SUPPORT_ENABLED
#include "chre/platform/platform_audio.h"
#endif  // CHRE_AUDIO_SUPPORT_ENABLED
#include "chre/platform/context.h"
#include "chre/platform/fatal_error.h"
#include "chre/platform/linux/platform_log.h"
#include "chre/platform/log.h"
#include "chre/platform/system_timer.h"
#include "chre/util/time.h"

#include <tclap/CmdLine.h>
#include <csignal>
#include <thread>

using chre::EventLoopManagerSingleton;
using chre::Milliseconds;

//! A description of the simulator.
constexpr char kSimDescription[] =
    "A simulation environment for the Context Hub Runtime Environment (CHRE)";

//! The version of the simulator. This is not super important but is assigned by
//! rules of semantic versioning.
constexpr char kSimVersion[] = "0.1.0";

namespace {

extern "C" void signalHandler(int sig) {
  (void)sig;
  LOGI("Stop request received");
  EventLoopManagerSingleton::get()->getEventLoop().stop();
}

}  // namespace

int main(int argc, char **argv) {
  try {
    // Parse command-line arguments.
    TCLAP::CmdLine cmd(kSimDescription, ' ', kSimVersion);
    TCLAP::SwitchArg noStaticNanoappsArg("", "no_static_nanoapps",
                                         "disable running static nanoapps", cmd,
                                         false);
    TCLAP::MultiArg<std::string> nanoappsArg(
        "", "nanoapp", "nanoapp shared object to load and execute", false,
        "path", cmd);
#ifdef CHRE_AUDIO_SUPPORT_ENABLED
    TCLAP::ValueArg<std::string> audioFileArg(
        "", "audio_file", "WAV file to open for audio simulation", false, "",
        "path", cmd);
    TCLAP::ValueArg<double> minAudioBufSizeArg(
        "", "min_audio_buf_size", "min buffer size for audio simulation", false,
        1.0, "seconds", cmd);
    TCLAP::ValueArg<double> maxAudioBufSizeArg(
        "", "max_audio_buf_size", "max buffer size for audio simulation", false,
        10.0, "seconds", cmd);
#endif  // CHRE_AUDIO_SUPPORT_ENABLED
    cmd.parse(argc, argv);

    // Initialize logging.
    chre::PlatformLogSingleton::init();

#ifdef CHRE_AUDIO_SUPPORT_ENABLED
    // Initialize audio sources.
    if (!audioFileArg.getValue().empty()) {
      auto audioSource = chre::MakeUnique<chre::AudioSource>(
          audioFileArg.getValue(), minAudioBufSizeArg.getValue(),
          maxAudioBufSizeArg.getValue());
      chre::PlatformAudio::addAudioSource(audioSource);
    }

    // TODO(P1-d24c82): Add another command line argument that takes a json
    // configuration to support multiple sources.
#endif  // CHRE_AUDIO_SUPPORT_ENABLED

    // Initialize the system.
    chre::init();

    // Register a signal handler.
    std::signal(SIGINT, signalHandler);

    // Load any static nanoapps and start the event loop.
    std::thread chreThread([&]() {
      EventLoopManagerSingleton::get()->lateInit();

      // Load static nanoapps unless they are disabled by a command-line flag.
      if (!noStaticNanoappsArg.getValue()) {
        chre::loadStaticNanoapps();
      }

      // Load dynamic nanoapps specified on the command-line.
      chre::DynamicVector<chre::UniquePtr<chre::Nanoapp>> dynamicNanoapps;
      for (const auto &nanoapp : nanoappsArg.getValue()) {
        dynamicNanoapps.push_back(chre::MakeUnique<chre::Nanoapp>());
        dynamicNanoapps.back()->loadFromFile(nanoapp);
        EventLoopManagerSingleton::get()->getEventLoop().startNanoapp(
            dynamicNanoapps.back());
      }

      EventLoopManagerSingleton::get()->getEventLoop().run();
    });
    chreThread.join();

    chre::deinit();
    chre::PlatformLogSingleton::deinit();
  } catch (TCLAP::ExitException) {
  }

  return 0;
}