xref: /aosp_15_r20/system/chre/host/hal_generic/common/chre_connection.h (revision 84e339476a462649f82315436d70fd732297a399)
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 #ifndef ANDROID_HARDWARE_CONTEXTHUB_COMMON_CHRE_CONNECTION_H_
17 #define ANDROID_HARDWARE_CONTEXTHUB_COMMON_CHRE_CONNECTION_H_
18 
19 #include <flatbuffers/flatbuffers.h>
20 #include <chrono>
21 #include <cstddef>
22 #include <cstdint>
23 #include <future>
24 #include <string>
25 #include "chre_host/fragmented_load_transaction.h"
26 #include "hal_client_id.h"
27 
28 namespace android::hardware::contexthub::common::implementation {
29 
30 /** This abstract class defines the interface between HAL and CHRE. */
31 class ChreConnection {
32  public:
33   virtual ~ChreConnection() = default;
34 
35   /**
36    * Initializes the connection between HAL and CHRE.
37    *
38    * @return true if success, otherwise false.
39    */
40   virtual bool init() = 0;
41 
42   /**
43    * Sends a message to CHRE.
44    *
45    * @return true if success, otherwise false.
46    */
47   virtual bool sendMessage(void *data, size_t length) = 0;
48 
49   /**
50    * @return The nanoapp loading fragment size in bytes.
51    */
getLoadFragmentSizeBytes()52   virtual size_t getLoadFragmentSizeBytes() const {
53     static_assert(CHRE_HOST_DEFAULT_FRAGMENT_SIZE > 0);
54     return CHRE_HOST_DEFAULT_FRAGMENT_SIZE;
55   }
56 
57   /**
58    * Sends a message encapsulated in a FlatBufferBuilder to CHRE.
59    *
60    * @return true if success, otherwise false.
61    */
sendMessage(const flatbuffers::FlatBufferBuilder & builder)62   inline bool sendMessage(const flatbuffers::FlatBufferBuilder &builder) {
63     return sendMessage(builder.GetBufferPointer(), builder.GetSize());
64   }
65 
66   /**
67    * Gets the offset between the Context hub and Android time in nanoseconds.
68    *
69    * This function may be used for synchronizing timestamps between the Context
70    * hub and Android.
71    *
72    * @param offset points to the address storing the offset in nanoseconds which
73    * is defined as android time - context hub time.
74    * @return true on success, otherwise false.
75    */
getTimeOffset(int64_t *)76   virtual bool getTimeOffset(int64_t * /*offset*/) {
77     return false;
78   }
79 
80   /**
81    * Returns true if time sync is required by the platform, false otherwise.
82    *
83    * When this function returns false, getTimeOffset may not be implemented.
84    */
isTimeSyncNeeded()85   virtual bool isTimeSyncNeeded() {
86     return false;
87   }
88 
89   /**
90    * Returns debug information.
91    */
dump()92   virtual std::string dump() {
93     return {};
94   }
95 };
96 
97 }  // namespace android::hardware::contexthub::common::implementation
98 #endif  // ANDROID_HARDWARE_CONTEXTHUB_COMMON_CHRE_CONNECTION_H_
99