init.cc revision 53b98346214effa969c45e95ae417c08fe492779
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"
21432a24e4167d9114e9c64cb094bdd32d5d2ba52cAndrew Rossignol#include "chre/platform/shared/platform_log.h"
2253b98346214effa969c45e95ae417c08fe492779Andrew Rossignol#include "chre_host/host_protocol_host.h"
23432a24e4167d9114e9c64cb094bdd32d5d2ba52cAndrew Rossignol
24432a24e4167d9114e9c64cb094bdd32d5d2ba52cAndrew Rossignol#include <csignal>
25432a24e4167d9114e9c64cb094bdd32d5d2ba52cAndrew Rossignol#include <thread>
26432a24e4167d9114e9c64cb094bdd32d5d2ba52cAndrew Rossignol
2753b98346214effa969c45e95ae417c08fe492779Andrew Rossignolusing android::chre::HostProtocolHost;
28432a24e4167d9114e9c64cb094bdd32d5d2ba52cAndrew Rossignolusing chre::EventLoopManagerSingleton;
29432a24e4167d9114e9c64cb094bdd32d5d2ba52cAndrew Rossignol
30432a24e4167d9114e9c64cb094bdd32d5d2ba52cAndrew Rossignolnamespace {
31432a24e4167d9114e9c64cb094bdd32d5d2ba52cAndrew Rossignol
3253b98346214effa969c45e95ae417c08fe492779Andrew Rossignolvoid onMessageReceivedFromClient(uint16_t clientId, void *data, size_t length) {
3353b98346214effa969c45e95ae417c08fe492779Andrew Rossignol  if (!HostProtocolHost::mutateHostClientId(data, length, clientId)) {
3453b98346214effa969c45e95ae417c08fe492779Andrew Rossignol    LOGE("Couldn't set host client ID in message container!");
3553b98346214effa969c45e95ae417c08fe492779Andrew Rossignol  } else {
3653b98346214effa969c45e95ae417c08fe492779Andrew Rossignol    LOGD("Delivering message from host (size %zu)", length);
3753b98346214effa969c45e95ae417c08fe492779Andrew Rossignol    if (!chre::handleMessageFromHost(data, length)) {
3853b98346214effa969c45e95ae417c08fe492779Andrew Rossignol      LOGE("Failed to decode message from host");
3953b98346214effa969c45e95ae417c08fe492779Andrew Rossignol    }
4053b98346214effa969c45e95ae417c08fe492779Andrew Rossignol  }
41432a24e4167d9114e9c64cb094bdd32d5d2ba52cAndrew Rossignol}
42432a24e4167d9114e9c64cb094bdd32d5d2ba52cAndrew Rossignol
43432a24e4167d9114e9c64cb094bdd32d5d2ba52cAndrew Rossignol}
44432a24e4167d9114e9c64cb094bdd32d5d2ba52cAndrew Rossignol
45432a24e4167d9114e9c64cb094bdd32d5d2ba52cAndrew Rossignolint main(int argc, char **argv) {
4653b98346214effa969c45e95ae417c08fe492779Andrew Rossignol  // Initilize CHRE.
47432a24e4167d9114e9c64cb094bdd32d5d2ba52cAndrew Rossignol  chre::init();
48432a24e4167d9114e9c64cb094bdd32d5d2ba52cAndrew Rossignol  chre::loadStaticNanoapps();
4953b98346214effa969c45e95ae417c08fe492779Andrew Rossignol
5053b98346214effa969c45e95ae417c08fe492779Andrew Rossignol  // Initialize the socket server.
5153b98346214effa969c45e95ae417c08fe492779Andrew Rossignol  chre::SocketServerSingleton::init();
5253b98346214effa969c45e95ae417c08fe492779Andrew Rossignol
5353b98346214effa969c45e95ae417c08fe492779Andrew Rossignol  // Setup the socket server for communications with the HAL.
5453b98346214effa969c45e95ae417c08fe492779Andrew Rossignol  std::thread socketServerThread([&]() {
5553b98346214effa969c45e95ae417c08fe492779Andrew Rossignol    chre::SocketServerSingleton::get()->run(
5653b98346214effa969c45e95ae417c08fe492779Andrew Rossignol        "chre", true, onMessageReceivedFromClient);
5753b98346214effa969c45e95ae417c08fe492779Andrew Rossignol    EventLoopManagerSingleton::get()->getEventLoop().stop();
5853b98346214effa969c45e95ae417c08fe492779Andrew Rossignol  });
5953b98346214effa969c45e95ae417c08fe492779Andrew Rossignol
60432a24e4167d9114e9c64cb094bdd32d5d2ba52cAndrew Rossignol  EventLoopManagerSingleton::get()->getEventLoop().run();
61432a24e4167d9114e9c64cb094bdd32d5d2ba52cAndrew Rossignol
62432a24e4167d9114e9c64cb094bdd32d5d2ba52cAndrew Rossignol  chre::deinit();
63432a24e4167d9114e9c64cb094bdd32d5d2ba52cAndrew Rossignol  return 0;
64432a24e4167d9114e9c64cb094bdd32d5d2ba52cAndrew Rossignol}
65