1 /* 2 * Copyright (C) 2010 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 #ifndef CHRE_DAEMON_H_ 18 #define CHRE_DAEMON_H_ 19 20 #include <atomic> 21 #include <cstdint> 22 #include <map> 23 #include <queue> 24 #include <string> 25 26 #include "chre_host/host_protocol_host.h" 27 #include "chre_host/log_message_parser_base.h" 28 #include "chre_host/socket_server.h" 29 30 namespace android { 31 namespace chre { 32 33 class ChreDaemonBase { 34 public: ChreDaemonBase()35 ChreDaemonBase() : mChreShutdownRequested(false) {} ~ChreDaemonBase()36 virtual ~ChreDaemonBase() {} 37 38 /** 39 * Initialize the CHRE daemon. We're expected to fail here and not start 40 * the daemon if we don't get all the resources we're hoping to. 41 * Any resources claimed by this function should be released in the 42 * destructor 43 * 44 * @return true on successful initialization 45 */ 46 virtual bool init() = 0; 47 48 /** 49 * Start the CHRE Daemon. This method must be called after @ref init() has 50 * been called. 51 */ 52 virtual void run() = 0; 53 54 /** 55 * Send a message to CHRE 56 * 57 * @param clientId The client ID that this message originates from. 58 * @param data The data to pass down. 59 * @param length The size of the data to send. 60 * @return true if successful, false otherwise. 61 */ 62 bool sendMessageToChre(uint16_t clientId, void *data, size_t dataLen); 63 64 /** 65 * Function to query if a graceful shutdown of CHRE was requested 66 * 67 * @return true if a graceful shutdown was requested 68 */ wasShutdownRequested()69 bool wasShutdownRequested() const { 70 return mChreShutdownRequested; 71 } 72 73 /** 74 * Loads the supplied file into the provided buffer. 75 * 76 * @param filename The name of the file to load. 77 * @param buffer The buffer to load into. 78 * @return true if successful, false otherwise. 79 */ 80 static bool readFileContents(const char *filename, 81 std::vector<uint8_t> *buffer); 82 83 protected: 84 //! The host ID to use when preloading nanoapps. This is used before the 85 //! server is started and is sufficiently high enough so as to not collide 86 //! with any clients after the server starts. 87 static constexpr uint16_t kHostClientIdDaemon = UINT16_MAX; 88 setShutdownRequested(bool request)89 void setShutdownRequested(bool request) { 90 mChreShutdownRequested = request; 91 } 92 93 /** 94 * Attempts to load all preloaded nanoapps from a config file. The config file 95 * is expected to be valid JSON with the following structure: 96 * 97 * { "nanoapps": [ 98 * "/path/to/nanoapp_1", 99 * "/path/to/nanoapp_2" 100 * ]} 101 * 102 * The napp_header and so files will both be loaded. All errors are logged. 103 * 104 * TODO: This is SLPI specific right now, and needs to be revisited to 105 * implement platform specific loading. 106 */ 107 void loadPreloadedNanoapps(); 108 109 /** 110 * Loads a preloaded nanoapp given a filename to load from. Allows the 111 * transaction to complete before the nanoapp starts so the server can start 112 * serving requests as soon as possible. 113 * 114 * @param directory The directory to load the nanoapp from. 115 * @param name The filename of the nanoapp to load. 116 * @param transactionId The transaction ID to use when loading the app. 117 */ 118 virtual void loadPreloadedNanoapp(const std::string &directory, 119 const std::string &name, 120 uint32_t transactionId); 121 122 /** 123 * Sends a preloaded nanoapp filename / metadata to CHRE. 124 * 125 * @param header The nanoapp header binary blob. 126 * @param nanoappName The filename of the nanoapp to be loaded. 127 * @param transactionId The transaction ID to use when loading the app. 128 * @return true if successful, false otherwise. 129 */ 130 bool loadNanoapp(const std::vector<uint8_t> &header, 131 const std::string &nanoappName, uint32_t transactionId); 132 133 /** 134 * Loads a nanoapp by sending the nanoapp filename to the CHRE framework. This 135 * method will return after sending the request so no guarantee is made that 136 * the nanoapp is loaded until after the response is received. 137 * 138 * @param appId The ID of the nanoapp to load. 139 * @param appVersion The version of the nanoapp to load. 140 * @param appTargetApiVersion The version of the CHRE API that the app 141 * targets. 142 * @param appBinaryName The name of the binary as stored in the filesystem. 143 * This will be used to load the nanoapp into CHRE. 144 * @param transactionId The transaction ID to use when loading. 145 * @return true if a request was successfully sent, false otherwise. 146 */ 147 bool sendNanoappLoad(uint64_t appId, uint32_t appVersion, 148 uint32_t appTargetApiVersion, 149 const std::string &appBinaryName, 150 uint32_t transactionId); 151 152 /** 153 * Send a time sync message to CHRE 154 * 155 * @param logOnError If true, logs an error message on failure. 156 * 157 * @return true if the time sync message was successfully sent to CHRE. 158 */ 159 bool sendTimeSync(bool logOnError); 160 161 /** 162 * Sends a time sync message to CHRE, retrying a specified time until success. 163 * 164 * @param maxNumRetries The number of times to retry sending the message 165 * 166 * @return true if the time sync message was successfully sent to CHRE. 167 */ 168 bool sendTimeSyncWithRetry(size_t numRetries, useconds_t retryDelayUs, 169 bool logOnError); 170 171 /** 172 * Interface to a callback that is called when the Daemon receives a message. 173 * 174 * @param message A buffer containing the message 175 * @param messageLen size of the message buffer in bytes 176 */ 177 void onMessageReceived(const unsigned char *message, size_t messageLen); 178 179 /** 180 * Handles a message that is directed towards the daemon. 181 * 182 * @param message The message sent to the daemon. 183 */ 184 virtual void handleDaemonMessage(const uint8_t *message); 185 186 /** 187 * Platform-specific method to actually do the message sending requested by 188 * sendMessageToChre. 189 */ 190 virtual bool doSendMessage(void *data, size_t dataLen) = 0; 191 192 /** 193 * Enables or disables LPMA (low power microphone access). 194 */ 195 virtual void configureLpma(bool enabled) = 0; 196 197 /** 198 * @return logger used by the underlying platform. 199 */ 200 virtual ChreLogMessageParserBase *getLogger() = 0; 201 202 //! Server used to communicate with daemon clients 203 SocketServer mServer; 204 205 private: 206 //! Set to true when we request a graceful shutdown of CHRE 207 std::atomic<bool> mChreShutdownRequested; 208 209 //! Contains a set of transaction IDs used to load the preloaded nanoapps. 210 //! The IDs are stored in the order they are sent. 211 std::queue<uint32_t> mPreloadedNanoappPendingTransactionIds; 212 213 /** 214 * Computes and returns the clock drift between the system clock 215 * and the processor timer registers 216 * 217 * @return offset in nanoseconds 218 */ 219 virtual int64_t getTimeOffset(bool *success) = 0; 220 }; 221 222 } // namespace chre 223 } // namespace android 224 225 #endif // CHRE_DAEMON_H 226