• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 
2 /*
3  * Copyright (C) 2017 The Android Open Source Project
4  *
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at
8  *
9  *      http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  */
17 
18 #ifndef CHRE_HOST_HOST_PROTOCOL_HOST_H_
19 #define CHRE_HOST_HOST_PROTOCOL_HOST_H_
20 
21 #include <stdint.h>
22 
23 #include "chre/platform/shared/host_protocol_common.h"
24 #include "chre_host/fragmented_load_transaction.h"
25 #include "chre_host/generated/host_messages_generated.h"
26 #include "flatbuffers/flatbuffers.h"
27 
28 #include <vector>
29 
30 namespace android {
31 namespace chre {
32 
33 /**
34  * Checks that a string encapsulated as a byte vector is null-terminated, and
35  * if it is, returns a pointer to the vector's data. Otherwise returns null.
36  *
37  * This is similar to getStringFromByteVector in host_protocol_chre.h. Ensure
38  * that method's implementation is kept in sync with this.
39  *
40  * @param vec Target vector, can be null
41  *
42  * @return Pointer to the vector's data, or null
43  */
44 const char *getStringFromByteVector(const std::vector<int8_t> &vec);
45 
46 /**
47  * Calling code should provide an implementation of this interface to handle
48  * parsed results from decodeMessageFromChre().
49  */
50 class IChreMessageHandlers {
51  public:
52   virtual ~IChreMessageHandlers() = default;
53 
handleNanoappMessage(const::chre::fbs::NanoappMessageT &)54   virtual void handleNanoappMessage(
55       const ::chre::fbs::NanoappMessageT & /*message*/) {}
56 
handleHubInfoResponse(const::chre::fbs::HubInfoResponseT &)57   virtual void handleHubInfoResponse(
58       const ::chre::fbs::HubInfoResponseT & /*response*/) {}
59 
handleNanoappListResponse(const::chre::fbs::NanoappListResponseT &)60   virtual void handleNanoappListResponse(
61       const ::chre::fbs::NanoappListResponseT & /*response*/) {}
62 
handleLoadNanoappResponse(const::chre::fbs::LoadNanoappResponseT &)63   virtual void handleLoadNanoappResponse(
64       const ::chre::fbs::LoadNanoappResponseT & /*response*/) {}
65 
handleUnloadNanoappResponse(const::chre::fbs::UnloadNanoappResponseT &)66   virtual void handleUnloadNanoappResponse(
67       const ::chre::fbs::UnloadNanoappResponseT & /*response*/) {}
68 
handleDebugDumpData(const::chre::fbs::DebugDumpDataT &)69   virtual void handleDebugDumpData(
70       const ::chre::fbs::DebugDumpDataT & /*data*/) {}
71 
handleDebugDumpResponse(const::chre::fbs::DebugDumpResponseT &)72   virtual void handleDebugDumpResponse(
73       const ::chre::fbs::DebugDumpResponseT & /*response*/) {}
74 
handleSelfTestResponse(const::chre::fbs::SelfTestResponseT &)75   virtual void handleSelfTestResponse(
76       const ::chre::fbs::SelfTestResponseT & /*response*/) {}
77 
handleContextHubV4Message(const::chre::fbs::ChreMessageUnion &)78   virtual bool handleContextHubV4Message(
79       const ::chre::fbs::ChreMessageUnion & /*msg*/) {
80     return false;
81   }
82 
handleBluetoothSocketMessage(const void *,size_t)83   virtual void handleBluetoothSocketMessage(const void * /*message*/,
84                                             size_t /*messageLen*/) {}
85 };
86 
87 /**
88  * A set of helper methods that simplify the encode/decode of FlatBuffers
89  * messages used in communication with CHRE from the host.
90  */
91 class HostProtocolHost : public ::chre::HostProtocolCommon {
92  public:
93   /**
94    * Decodes a message sent from CHRE and invokes the appropriate handler
95    * function in the provided interface implementation to handle the parsed
96    * result.
97    *
98    * @param message Buffer containing a complete FlatBuffers CHRE message
99    * @param messageLen Size of the message, in bytes
100    * @param handlers Set of callbacks to handle the parsed message. If this
101    *        function returns success, then exactly one of these functions was
102    *        called.
103    *
104    * @return true if the message was parsed successfully and passed to a
105    *         handler
106    */
107   static bool decodeMessageFromChre(const void *message, size_t messageLen,
108                                     IChreMessageHandlers &handlers);
109 
110   /**
111    * Encodes a message requesting pulse from CHRE
112    *
113    * @param builder A newly constructed FlatBufferBuilder that will be used to
114    *        construct the message
115    */
116   static void encodePulseRequest(flatbuffers::FlatBufferBuilder &builder);
117 
118   /**
119    * Encodes a message requesting hub information from CHRE
120    *
121    * @param builder A newly constructed FlatBufferBuilder that will be used to
122    *        construct the message
123    */
124   static void encodeHubInfoRequest(flatbuffers::FlatBufferBuilder &builder);
125 
126   /**
127    * Encodes a message sending boot debug configuration to CHRE
128    *
129    * @param builder A newly constructed FlatBufferBuilder that will be used to
130    *        construct the message
131    */
132   static void encodeDebugConfiguration(flatbuffers::FlatBufferBuilder &builder);
133 
134   /**
135    * Encodes a message requesting to load a nanoapp specified by the included
136    * (possibly fragmented) binary payload and metadata.
137    *
138    * @param builder A newly constructed FlatBufferBuilder that will be used to
139    *        construct the message
140    * @param request The FragmentedLoadRequest object with the binary and the
141    *        metadata
142    * @param respondBeforeStart See LoadNanoappRequest.respond_before_start in
143    *        flatbuffers message.
144    */
145   static void encodeFragmentedLoadNanoappRequest(
146       flatbuffers::FlatBufferBuilder &builder,
147       const FragmentedLoadRequest &request, bool respondBeforeStart = false);
148 
149   /**
150    * Encodes a message requesting the list of loaded nanoapps from CHRE
151    *
152    * @param builder A newly constructed FlatBufferBuilder that will be used to
153    *        construct the message
154    */
155   static void encodeNanoappListRequest(flatbuffers::FlatBufferBuilder &builder);
156 
157   /**
158    * Encodes a message requesting to unload a nanoapp specified by app ID.
159    *
160    * @param builder A newly constructed FlatBufferBuilder that will be used to
161    *        construct the message
162    * @param transactionId A transaction identifier to tie the subsequent
163    *        response to this request
164    * @param appId Identifier for the app to unload
165    * @param allowSystemNanoappUnload Whether this request should be allowed to
166    *        result in unloading a system nanoapp (e.g. requests from the
167    *        context hub HAL should have set this to false, as system nanoapps
168    *        are not expected to be managed through that HAL)
169    */
170   static void encodeUnloadNanoappRequest(
171       flatbuffers::FlatBufferBuilder &builder, uint32_t transactionId,
172       uint64_t appId, bool allowSystemNanoappUnload);
173 
174   /**
175    * Encodes a message to send the AP timestamp to CHRE
176    *
177    * @param builder A newly constructed FlatBufferBuilder that will be used to
178    *        construct the message
179    * @param offset The AP to SLPI offset in nanoseconds
180    */
181   static void encodeTimeSyncMessage(flatbuffers::FlatBufferBuilder &builder,
182                                     int64_t offset);
183 
184   /**
185    * Encodes a message requesting debugging information from CHRE
186    *
187    * @param builder A newly constructed FlatBufferBuilder that will be used to
188    *        construct the message
189    */
190   static void encodeDebugDumpRequest(flatbuffers::FlatBufferBuilder &builder);
191 
192   /**
193    * Decodes the host client ID included in the message container
194    *
195    * @param message Buffer containing a complete FlatBuffers CHRE message
196    * @param messageLen Size of the message, in bytes
197    * @param hostClientId Output parameter that will be populated with the
198    *        client ID included in the message on success
199    *
200    * @return true if the host client ID was successfully decoded from the
201    *         message
202    */
203   static bool extractHostClientIdAndType(const void *message, size_t messageLen,
204                                          uint16_t *hostClientId,
205                                          ::chre::fbs::ChreMessage *messageType);
206 
207   /**
208    * Update the host client ID field in the MessageContainer.
209    *
210    * @param message Buffer containing a complete FlatBuffers CHRE message
211    * @param messageLen Size of the message, in bytes
212    * @param hostClientId The value to set the host client ID to
213    *
214    * @return true if the message was verified successfully, and we were able
215    *         to modify the host client ID field
216    */
217   static bool mutateHostClientId(void *message, size_t messageLen,
218                                  uint16_t hostClientId);
219 
220   /**
221    * Encodes a message requesting to load a nanoapp specified by the included
222    * binary payload and metadata.
223    *
224    * @param builder A newly constructed FlatBufferBuilder that will be used to
225    *        construct the message
226    */
227   static void encodeLoadNanoappRequestForBinary(
228       flatbuffers::FlatBufferBuilder &builder, uint32_t transactionId,
229       uint64_t appId, uint32_t appVersion, uint32_t appFlags,
230       uint32_t targetApiVersion, const std::vector<uint8_t> &nanoappBinary,
231       uint32_t fragmentId, size_t appTotalSizeBytes, bool respondBeforeStart);
232 
233   /**
234    * Encodes a message requesting to load a nanoapp specified by the included
235    * binary filename and metadata.
236    *
237    * @param builder A newly constructed FlatBufferBuilder that will be used to
238    *        construct the message
239    */
240   static void encodeLoadNanoappRequestForFile(
241       flatbuffers::FlatBufferBuilder &builder, uint32_t transactionId,
242       uint64_t appId, uint32_t appVersion, uint32_t targetApiVersion,
243       const char *nanoappBinaryName);
244 
245   /**
246    * Encodes a message notifying CHRE of a user setting change.
247    *
248    * @param builder A newly constructed FlatBufferBuilder that will be used to
249    *        construct the message
250    * @param setting The setting value that the user changed
251    * @param newState The state that the user change the setting to.
252    */
253   static void encodeSettingChangeNotification(
254       flatbuffers::FlatBufferBuilder &builder, ::chre::fbs::Setting setting,
255       ::chre::fbs::SettingState newState);
256 
257   /**
258    * Encodes a message to request CHRE to perform a self test.
259    */
260   static void encodeSelfTestRequest(flatbuffers::FlatBufferBuilder &builder);
261 
262   /**
263    * Encodes a message notifying CHRE of a host endpoint that connected.
264    *
265    * @param builder A newly constructed FlatBufferBuilder that will be used to
266    *        construct the message
267    * @param hostEndpointId The ID of the host endpoint that connected.
268    * @param type The CHRE_HOST_ENDPOINT_TYPE_* value that describes the
269    * endpoint.
270    * @param packageName The (optional) package name of the endpoint.
271    * @param attributionTag The (optional) attribution tag of the endpoint.
272    */
273   static void encodeHostEndpointConnected(
274       flatbuffers::FlatBufferBuilder &builder, uint16_t hostEndpointId,
275       uint8_t type, const std::string &packageName,
276       const std::string &attributionTag);
277 
278   /**
279    * Encodes a message notifying CHRE of a host endpoint that disconnected.
280    *
281    * @param builder A newly constructed FlatBufferBuilder that will be used to
282    *        construct the message
283    * @param hostEndpointId The ID of the host endpoint that disconnected.
284    */
285   static void encodeHostEndpointDisconnected(
286       flatbuffers::FlatBufferBuilder &builder, uint16_t hostEndpointId);
287 
288   static void encodeNanconfigurationUpdate(
289       flatbuffers::FlatBufferBuilder &builder, bool nanEnabled);
290 };
291 
292 }  // namespace chre
293 }  // namespace android
294 
295 #endif  // CHRE_HOST_HOST_PROTOCOL_HOST_H_
296