Home | History | Annotate | Download | only in chre_host
      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/host_messages_generated.h"
     25 #include "flatbuffers/flatbuffers.h"
     26 
     27 namespace android {
     28 namespace chre {
     29 
     30 /**
     31  * Calling code should provide an implementation of this interface to handle
     32  * parsed results from decodeMessageFromChre().
     33  */
     34 class IChreMessageHandlers {
     35  public:
     36   virtual ~IChreMessageHandlers() = default;
     37 
     38   virtual void handleNanoappMessage(
     39       uint64_t appId, uint32_t messageType, uint16_t hostEndpoint,
     40       const void *messageData, size_t messageDataLen) = 0;
     41 
     42   virtual void handleHubInfoResponse(
     43       const char *name, const char *vendor,
     44       const char *toolchain, uint32_t legacyPlatformVersion,
     45       uint32_t legacyToolchainVersion, float peakMips, float stoppedPower,
     46       float sleepPower, float peakPower, uint32_t maxMessageLen,
     47       uint64_t platformId, uint32_t version) = 0;
     48 
     49   virtual void handleNanoappListResponse(
     50       const ::chre::fbs::NanoappListResponseT& response) = 0;
     51 
     52   virtual void handleLoadNanoappResponse(
     53       const ::chre::fbs::LoadNanoappResponseT& response) = 0;
     54 };
     55 
     56 /**
     57  * A set of helper methods that simplify the encode/decode of FlatBuffers
     58  * messages used in communication with CHRE from the host.
     59  */
     60 class HostProtocolHost : public ::chre::HostProtocolCommon {
     61  public:
     62   /**
     63    * Decodes a message sent from CHRE and invokes the appropriate handler
     64    * function in the provided interface implementation to handle the parsed
     65    * result.
     66    *
     67    * @param message Buffer containing a complete FlatBuffers CHRE message
     68    * @param messageLen Size of the message, in bytes
     69    * @param handlers Set of callbacks to handle the parsed message. If this
     70    *        function returns success, then exactly one of these functions was
     71    *        called.
     72    *
     73    * @return true if the message was parsed successfully and passed to a handler
     74    */
     75   static bool decodeMessageFromChre(const void *message, size_t messageLen,
     76                                     IChreMessageHandlers& handlers);
     77 
     78   /**
     79    * Encodes a message requesting hub information from CHRE
     80    *
     81    * @param builder A newly constructed FlatBufferBuilder that will be used to
     82    *        construct the message
     83    */
     84   static void encodeHubInfoRequest(flatbuffers::FlatBufferBuilder& builder);
     85 
     86   /**
     87    * Encodes a message requesting to load a nanoapp specified by the included
     88    * binary payload and metadata.
     89    *
     90    * @param builder A newly constructed FlatBufferBuilder that will be used to
     91    *        construct the message
     92    */
     93   static void encodeLoadNanoappRequest(
     94       flatbuffers::FlatBufferBuilder& builder, uint32_t transactionId,
     95       uint64_t appId, uint32_t appVersion, uint32_t targetApiVersion,
     96       const std::vector<uint8_t>& nanoappBinary);
     97 
     98   /**
     99    * Encodes a message requesting the list of loaded nanoapps from CHRE
    100    *
    101    * @param builder A newly constructed FlatBufferBuilder that will be used to
    102    *        construct the message
    103    */
    104   static void encodeNanoappListRequest(flatbuffers::FlatBufferBuilder& builder);
    105 
    106   /**
    107    * Decodes the host client ID included in the message container
    108    *
    109    * @param message Buffer containing a complete FlatBuffers CHRE message
    110    * @param messageLen Size of the message, in bytes
    111    * @param hostClientId Output parameter that will be populated with the client
    112    *        ID included in the message on success
    113    *
    114    * @return true if the host client ID was successfully decoded from the
    115    *         message
    116    */
    117   static bool extractHostClientId(const void *message, size_t messageLen,
    118                                   uint16_t *hostClientId);
    119 
    120   /**
    121    * Update the host client ID field in the MessageContainer.
    122    *
    123    * @param message Buffer containing a complete FlatBuffers CHRE message
    124    * @param messageLen Size of the message, in bytes
    125    * @param hostClientId The value to set the host client ID to
    126    *
    127    * @return true if the message was verified successfully, and we were able to
    128    *         modify the host client ID field
    129    */
    130   static bool mutateHostClientId(void *message, size_t messageLen,
    131                                  uint16_t hostClientId);
    132 };
    133 
    134 }  // namespace chre
    135 }  // namespace android
    136 
    137 #endif  // CHRE_HOST_HOST_PROTOCOL_HOST_H_
    138