• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2022 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_FBS_DAEMON_BASE_H_
18 #define CHRE_FBS_DAEMON_BASE_H_
19 
20 /**
21  * @file fbs_daemon_base.h
22  * This header defines a base class for all CHRE daemon variants that use
23  * flatbuffers as the codec scheme for communicating with CHRE.
24  */
25 
26 #include "chre_host/daemon_base.h"
27 #include "chre_host/host_protocol_host.h"
28 
29 namespace android {
30 namespace chre {
31 
32 class FbsDaemonBase : public ChreDaemonBase {
33  public:
~FbsDaemonBase()34   virtual ~FbsDaemonBase() {}
35 
36   /**
37    * Send a message to CHRE
38    *
39    * @param clientId The client ID that this message originates from.
40    * @param data The data to pass down.
41    * @param length The size of the data to send.
42    * @return true if successful, false otherwise.
43    */
44   bool sendMessageToChre(uint16_t clientId, void *data,
45                          size_t dataLen) override;
46 
47   /**
48    * Enables or disables LPMA (low power microphone access).
49    */
50   virtual void configureLpma(bool enabled) = 0;
51 
52  protected:
53   /**
54    * Loads a nanoapp by sending the nanoapp filename to the CHRE framework. This
55    * method will return after sending the request so no guarantee is made that
56    * the nanoapp is loaded until after the response is received.
57    *
58    * @param appId The ID of the nanoapp to load.
59    * @param appVersion The version of the nanoapp to load.
60    * @param appTargetApiVersion The version of the CHRE API that the app
61    * targets.
62    * @param appBinaryName The name of the binary as stored in the filesystem.
63    * This will be used to load the nanoapp into CHRE.
64    * @param transactionId The transaction ID to use when loading.
65    * @return true if a request was successfully sent, false otherwise.
66    */
67   bool sendNanoappLoad(uint64_t appId, uint32_t appVersion,
68                        uint32_t appTargetApiVersion,
69                        const std::string &appBinaryName,
70                        uint32_t transactionId) override;
71 
72   /**
73    * Send a time sync message to CHRE
74    *
75    * @param logOnError If true, logs an error message on failure.
76    *
77    * @return true if the time sync message was successfully sent to CHRE.
78    */
79   bool sendTimeSync(bool logOnError) override;
80 
81   /**
82    * Interface to a callback that is called when the Daemon receives a message.
83    *
84    * @param message A buffer containing the message
85    * @param messageLen size of the message buffer in bytes
86    */
87   void onMessageReceived(const unsigned char *message,
88                          size_t messageLen) override;
89 
90   /**
91    * Handles a message that is directed towards the daemon.
92    *
93    * @param message The message sent to the daemon.
94    */
95   void handleDaemonMessage(const uint8_t *message) override;
96 
97   /**
98    * Platform-specific method to actually do the message sending requested by
99    * sendMessageToChre.
100    *
101    * @return true if message was sent successfully, false otherwise.
102    */
103   virtual bool doSendMessage(void *data, size_t dataLen) = 0;
104 
105  private:
106   //! Contains a set of transaction IDs and app IDs used to load the preloaded
107   //! nanoapps. The IDs are stored in the order they are sent.
108   std::queue<Transaction> mPreloadedNanoappPendingTransactions;
109 };
110 
111 }  // namespace chre
112 }  // namespace android
113 
114 #endif  // CHRE_FBS_DAEMON_BASE_H_
115