init.cc 1.9 KB

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