xref: /aosp_15_r20/system/chre/host/msm/daemon/fastrpc_daemon.h (revision 84e339476a462649f82315436d70fd732297a399)
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/fbs_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 namespace android {
36 namespace chre {
37 
38 class FastRpcChreDaemon : public FbsDaemonBase {
39  public:
40   FastRpcChreDaemon();
41 
~FastRpcChreDaemon()42   ~FastRpcChreDaemon() {
43     deinit();
44   }
45 
46   /**
47    * Initializes and starts the monitoring and message handling threads,
48    * then proceeds to load any preloaded nanoapps. Also starts LPMA if
49    * it's enabled.
50    *
51    * @return true on successful init
52    */
53   bool init();
54 
55   /**
56    * Starts a socket server receive loop for inbound messages.
57    */
58   void run();
59 
60  protected:
61   bool doSendMessage(void *data, size_t length) override;
62 
configureLpma(bool enabled)63   void configureLpma(bool enabled) override {
64     mLpmaHandler.enable(enabled);
65   }
66 
67  private:
68   std::optional<std::thread> mMonitorThread;
69   std::optional<std::thread> mMsgToHostThread;
70   std::atomic_bool mCrashDetected = false;
71   StHalLpmaHandler mLpmaHandler;
72 
73   /**
74    * Shutsdown the daemon, stops all the worker threads created in init()
75    * Since this is to be invoked at exit, it's mostly best effort, and is
76    * invoked by the class destructor
77    */
78   void deinit();
79 
80   /**
81    * Platform specific getTimeOffset for the FastRPC daemon
82    *
83    * @return clock drift offset in nanoseconds
84    */
85   int64_t getTimeOffset(bool *success) override;
86 
87   /**
88    * Entry point for the thread that blocks in a FastRPC call to monitor for
89    * abnormal exit of CHRE or reboot of the DSP.
90    */
91   void monitorThreadEntry();
92 
93   /**
94    * Entry point for the thread that receives messages sent by CHRE.
95    */
96   void msgToHostThreadEntry();
97 
98   /**
99    * Handles the case where the remote end (SLPI, ADSP, etc) has crashed.
100    */
101   void onRemoteCrashDetected();
102 };
103 
104 }  // namespace chre
105 }  // namespace android
106 
107 #endif  // CHRE_FASTRPC_DAEMON_H_
108