1 /*
2  * Copyright (C) 2020 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 /**
18  * @file
19  * The daemon that hosts CHRE on a hexagon DSP via FastRPC. This is typically
20  * the SLPI but could be the ADSP or another DSP that supports FastRPC.
21  */
22 
23 #ifndef CHRE_FASTRPC_DAEMON_H_
24 #define CHRE_FASTRPC_DAEMON_H_
25 
26 #include "chre/platform/slpi/fastrpc.h"
27 #include "chre_host/daemon_base.h"
28 #include "chre_host/st_hal_lpma_handler.h"
29 
30 #include <utils/SystemClock.h>
31 #include <atomic>
32 #include <optional>
33 #include <thread>
34 
35 #ifdef CHRE_USE_TOKENIZED_LOGGING
36 #include "chre_host/tokenized_log_message_parser.h"
37 #else
38 #include "chre_host/log_message_parser_base.h"
39 #endif
40 
41 namespace android {
42 namespace chre {
43 
44 class FastRpcChreDaemon : public ChreDaemonBase {
45  public:
46   FastRpcChreDaemon();
47 
~FastRpcChreDaemon()48   ~FastRpcChreDaemon() {
49     deinit();
50   }
51 
52   /**
53    * Initializes and starts the monitoring and message handling threads,
54    * then proceeds to load any preloaded nanoapps. Also starts LPMA if
55    * it's enabled.
56    *
57    * @return true on successful init
58    */
59   bool init();
60 
61   /**
62    * Starts a socket server receive loop for inbound messages.
63    */
64   void run();
65 
66  protected:
67   bool doSendMessage(void *data, size_t length) override;
68 
configureLpma(bool enabled)69   void configureLpma(bool enabled) override {
70     mLpmaHandler.enable(enabled);
71   }
72 
getLogger()73   ChreLogMessageParserBase *getLogger() override {
74     return &mLogger;
75   }
76 
77  private:
78   std::optional<std::thread> mMonitorThread;
79   std::optional<std::thread> mMsgToHostThread;
80   std::atomic_bool mCrashDetected = false;
81   ChreLogMessageParserBase mLogger;
82   StHalLpmaHandler mLpmaHandler;
83 
84   /**
85    * Shutsdown the daemon, stops all the worker threads created in init()
86    * Since this is to be invoked at exit, it's mostly best effort, and is
87    * invoked by the class destructor
88    */
89   void deinit();
90 
91   /**
92    * Platform specific getTimeOffset for the FastRPC daemon
93    *
94    * @return clock drift offset in nanoseconds
95    */
96   int64_t getTimeOffset(bool *success);
97 
98   /**
99    * Entry point for the thread that blocks in a FastRPC call to monitor for
100    * abnormal exit of CHRE or reboot of the DSP.
101    */
102   void monitorThreadEntry();
103 
104   /**
105    * Entry point for the thread that receives messages sent by CHRE.
106    */
107   void msgToHostThreadEntry();
108 
109   /**
110    * Handles the case where the remote end (SLPI, ADSP, etc) has crashed.
111    */
112   void onRemoteCrashDetected();
113 };
114 
115 }  // namespace chre
116 }  // namespace android
117 
118 #endif  // CHRE_FASTRPC_DAEMON_H_
119