• 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_COMMON_H_
18 #define CHRE_PLATFORM_SHARED_HOST_PROTOCOL_COMMON_H_
19 
20 #include <stdint.h>
21 
22 #include "chre/util/system/napp_permissions.h"
23 #include "flatbuffers/flatbuffers.h"
24 
25 namespace chre {
26 
27 namespace fbs {
28 
29 // Forward declaration of the ChreMessage enum defined in the generated
30 // FlatBuffers header file
31 enum class ChreMessage : uint8_t;
32 
33 }  // namespace fbs
34 
35 //! On a message sent from CHRE, specifies that the host daemon should determine
36 //! which client to send the message to. Usually, this is all clients, but for a
37 //! message from a nanoapp, the host daemon can use the endpoint ID to determine
38 //! the destination client ID.
39 constexpr uint16_t kHostClientIdUnspecified = 0;
40 
41 /**
42  * Functions that are shared between the CHRE and host to assist with
43  * communications between the two. Note that normally these functions are
44  * accessed through a derived class like chre::HostProtocolChre (CHRE-side) or
45  * android::chre:HostProtocolHost (host-side).
46  */
47 class HostProtocolCommon {
48  public:
49   /**
50    * Encodes a message between a nanoapp and a host (in both directions) using
51    * the given FlatBufferBuilder and supplied parameters.
52    * Note that messagePermissions is only applicable to messages from a nanoapp
53    * to the host.
54    *
55    * @param builder A newly constructed FlatBufferBuilder that will be used to
56    *        encode the message. It will be finalized before returning from this
57    *        function.
58    * @param appId Nanoapp ID.
59    * @param messageType Type of message that was constructed.
60    * @param hostEndpoint The host endpoint the data was sent from or that should
61    *        receive this message.
62    * @param messageData Pointer to message payload. Can be null if
63    *        messageDataLen is zero.
64    * @param messageDataLen Size of messageData, in bytes.
65    * @param permissions List of Android permissions declared by the nanoapp or
66    *        granted to the host. For from the nanoapp to the host messages, this
67    *        must be a superset of messagePermissions.
68    * @param messagePermissions Used only for messages from the nanoapp to the
69    *        host. Lists the Android permissions covering the contents of the
70    *        message. These permissions are used to record and attribute access
71    *        to permissions-controlled resources.
72    */
73   static void encodeNanoappMessage(
74       flatbuffers::FlatBufferBuilder &builder, uint64_t appId,
75       uint32_t messageType, uint16_t hostEndpoint, const void *messageData,
76       size_t messageDataLen,
77       uint32_t permissions =
78           static_cast<uint32_t>(chre::NanoappPermissions::CHRE_PERMS_ALL),
79       uint32_t messagePermissions =
80           static_cast<uint32_t>(chre::NanoappPermissions::CHRE_PERMS_ALL));
81 
82   /**
83    * Adds a string to the provided builder as a byte vector.
84    *
85    * @param builder The builder to add the string to.
86    * @param str The string to add.
87    * @return The offset in the builder that the string is stored at.
88    */
89   static flatbuffers::Offset<flatbuffers::Vector<int8_t>> addStringAsByteVector(
90       flatbuffers::FlatBufferBuilder &builder, const char *str);
91 
92   /**
93    * Constructs the message container and finalizes the FlatBufferBuilder
94    *
95    * @param builder The FlatBufferBuilder that was used to construct the
96    *        message prior to adding the container
97    * @param messageType Type of message that was constructed
98    * @param message Offset of the message to include (normally the return value
99    *        of flatbuffers::Offset::Union() on the message offset)
100    * @param hostClientId The source/client ID of the host-side entity that
101    *        sent/should receive this message. Leave unspecified (default 0)
102    *        when constructing a message on the host, as this field will be
103    *        set before the message is sent to CHRE.
104    */
105   static void finalize(flatbuffers::FlatBufferBuilder &builder,
106                        fbs::ChreMessage messageType,
107                        flatbuffers::Offset<void> message,
108                        uint16_t hostClientId = kHostClientIdUnspecified);
109 
110   /**
111    * Verifies that the provided message contains a valid flatbuffers CHRE
112    * protocol message,
113    *
114    * @param message The message to validate.
115    * @param length The size of the message to validate.
116    * @return true if the message is valid, false otherwise.
117    */
118   static bool verifyMessage(const void *message, size_t messageLen);
119 };
120 
121 }  // namespace chre
122 
123 #endif  // CHRE_PLATFORM_SHARED_HOST_PROTOCOL_COMMON_H_
124