1/*
2 * Copyright (C) 2017 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_loop_manager.h"
18#include "chre/core/init.h"
19#include "chre/core/static_nanoapps.h"
20#include "chre/platform/android/host_link.h"
21#include "chre/platform/linux/preloaded_nanoapps.h"
22#include "chre/platform/shared/platform_log.h"
23#include "chre_host/host_protocol_host.h"
24
25#include <csignal>
26#include <thread>
27
28using android::chre::HostProtocolHost;
29using chre::EventLoopManagerSingleton;
30
31namespace {
32
33void onMessageReceivedFromClient(uint16_t clientId, void *data, size_t length) {
34  if (!HostProtocolHost::mutateHostClientId(data, length, clientId)) {
35    LOGE("Couldn't set host client ID in message container!");
36  } else {
37    LOGD("Delivering message from host (size %zu)", length);
38    if (!chre::handleMessageFromHost(data, length)) {
39      LOGE("Failed to decode message from host");
40    }
41  }
42}
43
44}
45
46int main(int argc, char **argv) {
47  // Initilize CHRE.
48  chre::init();
49  chre::loadStaticNanoapps();
50  chre::loadPreloadedNanoapps();
51
52  // Initialize the socket server.
53  chre::SocketServerSingleton::init();
54
55  // Setup the socket server for communications with the HAL.
56  std::thread socketServerThread([&]() {
57    chre::SocketServerSingleton::get()->run(
58        "chre", true, onMessageReceivedFromClient);
59    EventLoopManagerSingleton::get()->getEventLoop().stop();
60  });
61
62  EventLoopManagerSingleton::get()->getEventLoop().run();
63
64  chre::deinit();
65  return 0;
66}
67