Home | History | Annotate | Download | only in shared
      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/platform/shared/host_messages_generated.h"
     23 #include "chre/platform/shared/host_protocol_common.h"
     24 #include "flatbuffers/flatbuffers.h"
     25 
     26 namespace chre {
     27 
     28 typedef flatbuffers::Offset<fbs::NanoappListEntry> NanoappListEntryOffset;
     29 
     30 /**
     31  * These methods are called from decodeMessageFromHost() and must be implemented
     32  * by the code that calls it to handle parsed messages.
     33  */
     34 class HostMessageHandlers {
     35  public:
     36   static void handleNanoappMessage(
     37     uint64_t appId, uint32_t messageType, uint16_t hostEndpoint,
     38     const void *messageData, size_t messageDataLen);
     39 
     40   static void handleHubInfoRequest(uint16_t hostClientId);
     41 
     42   static void handleNanoappListRequest(uint16_t hostClientId);
     43 
     44   static void handleLoadNanoappRequest(
     45       uint16_t hostClientId, uint32_t transactionId, uint64_t appId,
     46       uint32_t appVersion, uint32_t targetApiVersion, const void *buffer,
     47       size_t bufferLen, uint32_t fragmentId, size_t appBinaryLen);
     48 
     49   static void handleUnloadNanoappRequest(
     50       uint16_t hostClientId, uint32_t transactionId, uint64_t appId,
     51       bool allowSystemNanoappUnload);
     52 
     53   static void handleTimeSyncMessage(int64_t offset);
     54 
     55   static void handleDebugDumpRequest(uint16_t hostClientId);
     56 };
     57 
     58 /**
     59  * A set of helper methods that simplify the encode/decode of FlatBuffers
     60  * messages used in communications with the host from CHRE.
     61  */
     62 class HostProtocolChre : public HostProtocolCommon {
     63  public:
     64   /**
     65    * Verifies and decodes a FlatBuffers-encoded CHRE message.
     66    *
     67    * @param message Buffer containing message
     68    * @param messageLen Size of the message, in bytes
     69    * @param handlers Contains callbacks to process a decoded message
     70    *
     71    * @return bool true if the message was successfully decoded, false if it was
     72    *         corrupted/invalid/unrecognized
     73    */
     74   static bool decodeMessageFromHost(const void *message, size_t messageLen);
     75 
     76   /**
     77    * Refer to the context hub HAL definition for a details of these parameters.
     78    *
     79    * @param builder A newly constructed FlatBufferBuilder that will be used to
     80    *        encode the message
     81    */
     82   static void encodeHubInfoResponse(
     83       flatbuffers::FlatBufferBuilder& builder, const char *name,
     84       const char *vendor, const char *toolchain, uint32_t legacyPlatformVersion,
     85       uint32_t legacyToolchainVersion, float peakMips, float stoppedPower,
     86       float sleepPower, float peakPower, uint32_t maxMessageLen,
     87       uint64_t platformId, uint32_t version, uint16_t hostClientId);
     88 
     89   /**
     90    * Supports construction of a NanoappListResponse by adding a single
     91    * NanoappListEntry to the response. The offset for the newly added entry is
     92    * maintained in the given vector until finishNanoappListResponse() is called.
     93    * Example usage:
     94    *
     95    *   FlatBufferBuilder builder;
     96    *   DynamicVector<NanoappListEntryOffset> vector;
     97    *   for (auto app : appList) {
     98    *     HostProtocolChre::addNanoppListEntry(builder, vector, ...);
     99    *   }
    100    *   HostProtocolChre::finishNanoappListResponse(builder, vector);
    101    *
    102    * @param builder A FlatBufferBuilder to use for encoding the message
    103    * @param offsetVector A vector to track the offset to the newly added
    104    *        NanoappListEntry, which be passed to finishNanoappListResponse()
    105    *        once all entries are added
    106    */
    107   static void addNanoappListEntry(
    108       flatbuffers::FlatBufferBuilder& builder,
    109       DynamicVector<NanoappListEntryOffset>& offsetVector,
    110       uint64_t appId, uint32_t appVersion, bool enabled, bool isSystemNanoapp);
    111 
    112   /**
    113    * Finishes encoding a NanoappListResponse message after all NanoappListEntry
    114    * elements have already been added to the builder.
    115    *
    116    * @param builder The FlatBufferBuilder used with addNanoappListEntry()
    117    * @param offsetVector The vector used with addNanoappListEntry()
    118    * @param hostClientId
    119    *
    120    * @see addNanoappListEntry()
    121    */
    122   static void finishNanoappListResponse(
    123       flatbuffers::FlatBufferBuilder& builder,
    124       DynamicVector<NanoappListEntryOffset>& offsetVector,
    125       uint16_t hostClientId);
    126 
    127   /**
    128    * Encodes a response to the host communicating the result of dynamically
    129    * loading a nanoapp.
    130    */
    131   static void encodeLoadNanoappResponse(
    132       flatbuffers::FlatBufferBuilder& builder, uint16_t hostClientId,
    133       uint32_t transactionId, bool success, uint32_t fragmentId);
    134 
    135   /**
    136    * Encodes a response to the host communicating the result of dynamically
    137    * unloading a nanoapp.
    138    */
    139   static void encodeUnloadNanoappResponse(
    140       flatbuffers::FlatBufferBuilder& builder, uint16_t hostClientId,
    141       uint32_t transactionId, bool success);
    142 
    143   /**
    144    * Encodes a buffer of log messages to the host.
    145    */
    146   static void encodeLogMessages(
    147       flatbuffers::FlatBufferBuilder& builder, const char *logBuffer,
    148       size_t bufferSize);
    149 
    150   /**
    151    * Encodes a string into a DebugDumpData message.
    152    *
    153    * @param debugStr Null-terminated ASCII string containing debug information
    154    * @param debugStrSize Size of the debugStr buffer, including null termination
    155    */
    156   static void encodeDebugDumpData(
    157       flatbuffers::FlatBufferBuilder& builder, uint16_t hostClientId,
    158       const char *debugStr, size_t debugStrSize);
    159 
    160   /**
    161    * Encodes the final response to a debug dump request.
    162    */
    163   static void encodeDebugDumpResponse(
    164       flatbuffers::FlatBufferBuilder& builder, uint16_t hostClientId,
    165       bool success, uint32_t dataCount);
    166 
    167   /**
    168    * Encodes a message requesting time sync from host.
    169    */
    170   static void encodeTimeSyncRequest(flatbuffers::FlatBufferBuilder& builder);
    171 
    172   /**
    173    * Encodes a message notifying the host that audio has been requested by a
    174    * nanoapp, so the low-power microphone needs to be powered on.
    175    */
    176   static void encodeLowPowerMicAccessRequest(
    177       flatbuffers::FlatBufferBuilder& builder);
    178 
    179   /**
    180    * Encodes a message notifying the host that no nanoapps are requesting audio
    181    * anymore, so the low-power microphone may be powered off.
    182    */
    183   static void encodeLowPowerMicAccessRelease(
    184       flatbuffers::FlatBufferBuilder& builder);
    185 };
    186 
    187 }  // namespace chre
    188 
    189 #endif  // CHRE_PLATFORM_SHARED_HOST_PROTOCOL_CHRE_H_
    190