1432a24e4167d9114e9c64cb094bdd32d5d2ba52cAndrew Rossignol/*
2432a24e4167d9114e9c64cb094bdd32d5d2ba52cAndrew Rossignol * Copyright (C) 2017 The Android Open Source Project
3432a24e4167d9114e9c64cb094bdd32d5d2ba52cAndrew Rossignol *
4432a24e4167d9114e9c64cb094bdd32d5d2ba52cAndrew Rossignol * Licensed under the Apache License, Version 2.0 (the "License");
5432a24e4167d9114e9c64cb094bdd32d5d2ba52cAndrew Rossignol * you may not use this file except in compliance with the License.
6432a24e4167d9114e9c64cb094bdd32d5d2ba52cAndrew Rossignol * You may obtain a copy of the License at
7432a24e4167d9114e9c64cb094bdd32d5d2ba52cAndrew Rossignol *
8432a24e4167d9114e9c64cb094bdd32d5d2ba52cAndrew Rossignol *      http://www.apache.org/licenses/LICENSE-2.0
9432a24e4167d9114e9c64cb094bdd32d5d2ba52cAndrew Rossignol *
10432a24e4167d9114e9c64cb094bdd32d5d2ba52cAndrew Rossignol * Unless required by applicable law or agreed to in writing, software
11432a24e4167d9114e9c64cb094bdd32d5d2ba52cAndrew Rossignol * distributed under the License is distributed on an "AS IS" BASIS,
12432a24e4167d9114e9c64cb094bdd32d5d2ba52cAndrew Rossignol * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13432a24e4167d9114e9c64cb094bdd32d5d2ba52cAndrew Rossignol * See the License for the specific language governing permissions and
14432a24e4167d9114e9c64cb094bdd32d5d2ba52cAndrew Rossignol * limitations under the License.
15432a24e4167d9114e9c64cb094bdd32d5d2ba52cAndrew Rossignol */
16432a24e4167d9114e9c64cb094bdd32d5d2ba52cAndrew Rossignol
17432a24e4167d9114e9c64cb094bdd32d5d2ba52cAndrew Rossignol#include "chre/core/event_loop_manager.h"
18432a24e4167d9114e9c64cb094bdd32d5d2ba52cAndrew Rossignol#include "chre/core/init.h"
19432a24e4167d9114e9c64cb094bdd32d5d2ba52cAndrew Rossignol#include "chre/core/static_nanoapps.h"
2053b98346214effa969c45e95ae417c08fe492779Andrew Rossignol#include "chre/platform/android/host_link.h"
217106cc7319ecf8135390708b9523351ed957caaeAndrew Rossignol#include "chre/platform/linux/preloaded_nanoapps.h"
22432a24e4167d9114e9c64cb094bdd32d5d2ba52cAndrew Rossignol#include "chre/platform/shared/platform_log.h"
2353b98346214effa969c45e95ae417c08fe492779Andrew Rossignol#include "chre_host/host_protocol_host.h"
24432a24e4167d9114e9c64cb094bdd32d5d2ba52cAndrew Rossignol
25432a24e4167d9114e9c64cb094bdd32d5d2ba52cAndrew Rossignol#include <csignal>
26432a24e4167d9114e9c64cb094bdd32d5d2ba52cAndrew Rossignol#include <thread>
27432a24e4167d9114e9c64cb094bdd32d5d2ba52cAndrew Rossignol
2853b98346214effa969c45e95ae417c08fe492779Andrew Rossignolusing android::chre::HostProtocolHost;
29432a24e4167d9114e9c64cb094bdd32d5d2ba52cAndrew Rossignolusing chre::EventLoopManagerSingleton;
30432a24e4167d9114e9c64cb094bdd32d5d2ba52cAndrew Rossignol
31432a24e4167d9114e9c64cb094bdd32d5d2ba52cAndrew Rossignolnamespace {
32432a24e4167d9114e9c64cb094bdd32d5d2ba52cAndrew Rossignol
3353b98346214effa969c45e95ae417c08fe492779Andrew Rossignolvoid onMessageReceivedFromClient(uint16_t clientId, void *data, size_t length) {
3453b98346214effa969c45e95ae417c08fe492779Andrew Rossignol  if (!HostProtocolHost::mutateHostClientId(data, length, clientId)) {
3553b98346214effa969c45e95ae417c08fe492779Andrew Rossignol    LOGE("Couldn't set host client ID in message container!");
3653b98346214effa969c45e95ae417c08fe492779Andrew Rossignol  } else {
3753b98346214effa969c45e95ae417c08fe492779Andrew Rossignol    LOGD("Delivering message from host (size %zu)", length);
3853b98346214effa969c45e95ae417c08fe492779Andrew Rossignol    if (!chre::handleMessageFromHost(data, length)) {
3953b98346214effa969c45e95ae417c08fe492779Andrew Rossignol      LOGE("Failed to decode message from host");
4053b98346214effa969c45e95ae417c08fe492779Andrew Rossignol    }
4153b98346214effa969c45e95ae417c08fe492779Andrew Rossignol  }
42432a24e4167d9114e9c64cb094bdd32d5d2ba52cAndrew Rossignol}
43432a24e4167d9114e9c64cb094bdd32d5d2ba52cAndrew Rossignol
44432a24e4167d9114e9c64cb094bdd32d5d2ba52cAndrew Rossignol}
45432a24e4167d9114e9c64cb094bdd32d5d2ba52cAndrew Rossignol
46432a24e4167d9114e9c64cb094bdd32d5d2ba52cAndrew Rossignolint main(int argc, char **argv) {
4753b98346214effa969c45e95ae417c08fe492779Andrew Rossignol  // Initilize CHRE.
48432a24e4167d9114e9c64cb094bdd32d5d2ba52cAndrew Rossignol  chre::init();
49432a24e4167d9114e9c64cb094bdd32d5d2ba52cAndrew Rossignol  chre::loadStaticNanoapps();
507106cc7319ecf8135390708b9523351ed957caaeAndrew Rossignol  chre::loadPreloadedNanoapps();
5153b98346214effa969c45e95ae417c08fe492779Andrew Rossignol
5253b98346214effa969c45e95ae417c08fe492779Andrew Rossignol  // Initialize the socket server.
5353b98346214effa969c45e95ae417c08fe492779Andrew Rossignol  chre::SocketServerSingleton::init();
5453b98346214effa969c45e95ae417c08fe492779Andrew Rossignol
5553b98346214effa969c45e95ae417c08fe492779Andrew Rossignol  // Setup the socket server for communications with the HAL.
5653b98346214effa969c45e95ae417c08fe492779Andrew Rossignol  std::thread socketServerThread([&]() {
5753b98346214effa969c45e95ae417c08fe492779Andrew Rossignol    chre::SocketServerSingleton::get()->run(
5853b98346214effa969c45e95ae417c08fe492779Andrew Rossignol        "chre", true, onMessageReceivedFromClient);
5953b98346214effa969c45e95ae417c08fe492779Andrew Rossignol    EventLoopManagerSingleton::get()->getEventLoop().stop();
6053b98346214effa969c45e95ae417c08fe492779Andrew Rossignol  });
6153b98346214effa969c45e95ae417c08fe492779Andrew Rossignol
62432a24e4167d9114e9c64cb094bdd32d5d2ba52cAndrew Rossignol  EventLoopManagerSingleton::get()->getEventLoop().run();
63432a24e4167d9114e9c64cb094bdd32d5d2ba52cAndrew Rossignol
64432a24e4167d9114e9c64cb094bdd32d5d2ba52cAndrew Rossignol  chre::deinit();
65432a24e4167d9114e9c64cb094bdd32d5d2ba52cAndrew Rossignol  return 0;
66432a24e4167d9114e9c64cb094bdd32d5d2ba52cAndrew Rossignol}
67