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