• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2017 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_PLATFORM_SHARED_HOST_PROTOCOL_CHRE_H_
18 #define CHRE_PLATFORM_SHARED_HOST_PROTOCOL_CHRE_H_
19 
20 #include <stdint.h>
21 
22 #include "chre/core/settings.h"
23 #include "chre/platform/shared/generated/host_messages_generated.h"
24 #include "chre/platform/shared/host_protocol_common.h"
25 #include "chre/util/dynamic_vector.h"
26 #include "chre/util/flatbuffers/helpers.h"
27 #include "chre_api/chre/event.h"
28 #include "flatbuffers/flatbuffers.h"
29 
30 namespace chre {
31 
32 typedef flatbuffers::Offset<fbs::NanoappListEntry> NanoappListEntryOffset;
33 
34 /**
35  * Checks that a string encapsulated as a byte vector is null-terminated, and
36  * if it is, returns a pointer to the vector's data. Otherwise returns null.
37  *
38  * This is similar to getStringFromByteVector in host_protocol_host.h. Ensure
39  * that method's implementation is kept in sync with this.
40  *
41  * @param vec Target vector, can be null
42  *
43  * @return Pointer to the vector's data, or null
44  */
45 const char *getStringFromByteVector(const flatbuffers::Vector<int8_t> *vec);
46 
47 /**
48  * These methods are called from decodeMessageFromHost() and must be implemented
49  * by the code that calls it to handle parsed messages.
50  */
51 class HostMessageHandlers {
52  public:
53   static void handleNanoappMessage(uint64_t appId, uint32_t messageType,
54                                    uint16_t hostEndpoint,
55                                    const void *messageData,
56                                    size_t messageDataLen);
57 
58   static void handleHubInfoRequest(uint16_t hostClientId);
59 
60   static void handleNanoappListRequest(uint16_t hostClientId);
61 
62   static void handleLoadNanoappRequest(
63       uint16_t hostClientId, uint32_t transactionId, uint64_t appId,
64       uint32_t appVersion, uint32_t appFlags, uint32_t targetApiVersion,
65       const void *buffer, size_t bufferLen, const char *appFileName,
66       uint32_t fragmentId, size_t appBinaryLen, bool respondBeforeStart);
67 
68   static void handleUnloadNanoappRequest(uint16_t hostClientId,
69                                          uint32_t transactionId, uint64_t appId,
70                                          bool allowSystemNanoappUnload);
71 
72   static void handleTimeSyncMessage(int64_t offset);
73 
74   static void handleDebugDumpRequest(uint16_t hostClientId);
75 
76   static void handleSettingChangeMessage(fbs::Setting setting,
77                                          fbs::SettingState state);
78 
79   static void handleSelfTestRequest(uint16_t hostClientId);
80 
81   static void handleNanConfigurationUpdate(bool enabled);
82 };
83 
84 /**
85  * A set of helper methods that simplify the encode/decode of FlatBuffers
86  * messages used in communications with the host from CHRE.
87  */
88 class HostProtocolChre : public HostProtocolCommon {
89  public:
90   /**
91    * Verifies and decodes a FlatBuffers-encoded CHRE message.
92    *
93    * @param message Buffer containing message
94    * @param messageLen Size of the message, in bytes
95    * @param handlers Contains callbacks to process a decoded message
96    *
97    * @return bool true if the message was successfully decoded, false if it was
98    *         corrupted/invalid/unrecognized
99    */
100   static bool decodeMessageFromHost(const void *message, size_t messageLen);
101 
102   /**
103    * Refer to the context hub HAL definition for a details of these parameters.
104    *
105    * @param builder A newly constructed ChreFlatBufferBuilder that will be used
106    * to encode the message
107    */
108   static void encodeHubInfoResponse(
109       ChreFlatBufferBuilder &builder, const char *name, const char *vendor,
110       const char *toolchain, uint32_t legacyPlatformVersion,
111       uint32_t legacyToolchainVersion, float peakMips, float stoppedPower,
112       float sleepPower, float peakPower, uint32_t maxMessageLen,
113       uint64_t platformId, uint32_t version, uint16_t hostClientId);
114 
115   /**
116    * Supports construction of a NanoappListResponse by adding a single
117    * NanoappListEntry to the response. The offset for the newly added entry is
118    * maintained in the given vector until finishNanoappListResponse() is called.
119    * Example usage:
120    *
121    *   ChreFlatBufferBuilder builder;
122    *   DynamicVector<NanoappListEntryOffset> vector;
123    *   for (auto app : appList) {
124    *     HostProtocolChre::addNanoppListEntry(builder, vector, ...);
125    *   }
126    *   HostProtocolChre::finishNanoappListResponse(builder, vector);
127    *
128    * @param builder A ChreFlatBufferBuilder to use for encoding the message
129    * @param offsetVector A vector to track the offset to the newly added
130    *        NanoappListEntry, which be passed to finishNanoappListResponse()
131    *        once all entries are added
132    */
133   static void addNanoappListEntry(
134       ChreFlatBufferBuilder &builder,
135       DynamicVector<NanoappListEntryOffset> &offsetVector, uint64_t appId,
136       uint32_t appVersion, bool enabled, bool isSystemNanoapp,
137       uint32_t appPermissions,
138       const DynamicVector<struct chreNanoappRpcService> &rpcServices);
139 
140   /**
141    * Finishes encoding a NanoappListResponse message after all NanoappListEntry
142    * elements have already been added to the builder.
143    *
144    * @param builder The ChreFlatBufferBuilder used with addNanoappListEntry()
145    * @param offsetVector The vector used with addNanoappListEntry()
146    * @param hostClientId
147    *
148    * @see addNanoappListEntry()
149    */
150   static void finishNanoappListResponse(
151       ChreFlatBufferBuilder &builder,
152       DynamicVector<NanoappListEntryOffset> &offsetVector,
153       uint16_t hostClientId);
154 
155   /**
156    * Encodes a response to the host communicating the result of dynamically
157    * loading a nanoapp.
158    */
159   static void encodeLoadNanoappResponse(ChreFlatBufferBuilder &builder,
160                                         uint16_t hostClientId,
161                                         uint32_t transactionId, bool success,
162                                         uint32_t fragmentId);
163 
164   /**
165    * Encodes a response to the host communicating the result of dynamically
166    * unloading a nanoapp.
167    */
168   static void encodeUnloadNanoappResponse(ChreFlatBufferBuilder &builder,
169                                           uint16_t hostClientId,
170                                           uint32_t transactionId, bool success);
171 
172   /**
173    * Encodes a buffer of log messages to the host.
174    */
175   static void encodeLogMessages(ChreFlatBufferBuilder &builder,
176                                 const uint8_t *logBuffer, size_t bufferSize);
177 
178   /**
179    * Encodes a buffer of V2 log messages to the host.
180    */
181   static void encodeLogMessagesV2(ChreFlatBufferBuilder &builder,
182                                   const uint8_t *logBuffer, size_t bufferSize,
183                                   uint32_t numLogsDropped);
184 
185   /**
186    * Encodes a string into a DebugDumpData message.
187    *
188    * @param debugStr Null-terminated ASCII string containing debug information
189    * @param debugStrSize Size of the debugStr buffer, including null termination
190    */
191   static void encodeDebugDumpData(ChreFlatBufferBuilder &builder,
192                                   uint16_t hostClientId, const char *debugStr,
193                                   size_t debugStrSize);
194 
195   /**
196    * Encodes the final response to a debug dump request.
197    */
198   static void encodeDebugDumpResponse(ChreFlatBufferBuilder &builder,
199                                       uint16_t hostClientId, bool success,
200                                       uint32_t dataCount);
201 
202   /**
203    * Encodes a message requesting time sync from host.
204    */
205   static void encodeTimeSyncRequest(ChreFlatBufferBuilder &builder);
206 
207   /**
208    * Encodes a message notifying the host that audio has been requested by a
209    * nanoapp, so the low-power microphone needs to be powered on.
210    */
211   static void encodeLowPowerMicAccessRequest(ChreFlatBufferBuilder &builder);
212 
213   /**
214    * Encodes a message notifying the host that no nanoapps are requesting audio
215    * anymore, so the low-power microphone may be powered off.
216    */
217   static void encodeLowPowerMicAccessRelease(ChreFlatBufferBuilder &builder);
218 
219   /**
220    * @param state The fbs::Setting value.
221    * @param chreSetting If success, stores the corresponding
222    * chre::Setting value.
223    *
224    * @return true if state was a valid fbs::Setting value.
225    */
226   static bool getSettingFromFbs(fbs::Setting setting, Setting *chreSetting);
227 
228   /**
229    * @param state The fbs::SettingState value.
230    * @param chreSettingEnabled If success, stores the value indicating whether
231    *     the setting is enabled or not.
232    *
233    * @return true if state was a valid fbs::SettingState value.
234    */
235   static bool getSettingEnabledFromFbs(fbs::SettingState state,
236                                        bool *chreSettingEnabled);
237 
238   /**
239    * Encodes a message notifying the result of a self test.
240    */
241   static void encodeSelfTestResponse(ChreFlatBufferBuilder &builder,
242                                      uint16_t hostClientId, bool success);
243 
244   /**
245    * Encodes a metric message using custon-defined protocol
246    */
247   static void encodeMetricLog(ChreFlatBufferBuilder &builder, uint32_t metricId,
248                               const uint8_t *encodedMsg, size_t metricSize);
249 
250   /**
251    * Encodes a NAN configuration request.
252    *
253    * @param builder An instance of the CHRE Flatbuffer builder.
254    * @param enable Boolean to indicate the enable/disable operation being
255    *        requested.
256    */
257   static void encodeNanConfigurationRequest(ChreFlatBufferBuilder &builder,
258                                             bool enable);
259 };
260 
261 }  // namespace chre
262 
263 #endif  // CHRE_PLATFORM_SHARED_HOST_PROTOCOL_CHRE_H_
264