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 #include "chre_host/host_protocol_host.h"
18 
19 #include <inttypes.h>
20 #include <string.h>
21 
22 #include "chre_host/log.h"
23 
24 using flatbuffers::FlatBufferBuilder;
25 using flatbuffers::Offset;
26 
27 // Aliased for consistency with the way these symbols are referenced in
28 // CHRE-side code
29 namespace fbs = ::chre::fbs;
30 
31 namespace android {
32 namespace chre {
33 
34 // This is similar to getStringFromByteVector in host_protocol_chre.h. Ensure
35 // that method's implementation is kept in sync with this.
getStringFromByteVector(const std::vector<int8_t> & vec)36 const char *getStringFromByteVector(const std::vector<int8_t> &vec) {
37   constexpr int8_t kNullChar = static_cast<int8_t>('\0');
38   const char *str = nullptr;
39 
40   // Check that the vector is present, non-empty, and null-terminated
41   if (vec.size() > 0 && vec[vec.size() - 1] == kNullChar) {
42     str = reinterpret_cast<const char *>(vec.data());
43   }
44 
45   return str;
46 }
47 
decodeMessageFromChre(const void * message,size_t messageLen,IChreMessageHandlers & handlers)48 bool HostProtocolHost::decodeMessageFromChre(const void *message,
49                                              size_t messageLen,
50                                              IChreMessageHandlers &handlers) {
51   bool success = verifyMessage(message, messageLen);
52   if (success) {
53     std::unique_ptr<fbs::MessageContainerT> container =
54         fbs::UnPackMessageContainer(message);
55     fbs::ChreMessageUnion &msg = container->message;
56 
57     switch (container->message.type) {
58       case fbs::ChreMessage::NanoappMessage:
59         handlers.handleNanoappMessage(*msg.AsNanoappMessage());
60         break;
61 
62       case fbs::ChreMessage::HubInfoResponse:
63         handlers.handleHubInfoResponse(*msg.AsHubInfoResponse());
64         break;
65 
66       case fbs::ChreMessage::NanoappListResponse:
67         handlers.handleNanoappListResponse(*msg.AsNanoappListResponse());
68         break;
69 
70       case fbs::ChreMessage::LoadNanoappResponse:
71         handlers.handleLoadNanoappResponse(*msg.AsLoadNanoappResponse());
72         break;
73 
74       case fbs::ChreMessage::UnloadNanoappResponse:
75         handlers.handleUnloadNanoappResponse(*msg.AsUnloadNanoappResponse());
76         break;
77 
78       case fbs::ChreMessage::DebugDumpData:
79         handlers.handleDebugDumpData(*msg.AsDebugDumpData());
80         break;
81 
82       case fbs::ChreMessage::DebugDumpResponse:
83         handlers.handleDebugDumpResponse(*msg.AsDebugDumpResponse());
84         break;
85 
86       case fbs::ChreMessage::SelfTestResponse:
87         handlers.handleSelfTestResponse(*msg.AsSelfTestResponse());
88         break;
89 
90       default:
91         LOGW("Got invalid/unexpected message type %" PRIu8,
92              static_cast<uint8_t>(msg.type));
93         success = false;
94     }
95   }
96 
97   return success;
98 }
99 
encodeHubInfoRequest(FlatBufferBuilder & builder)100 void HostProtocolHost::encodeHubInfoRequest(FlatBufferBuilder &builder) {
101   auto request = fbs::CreateHubInfoRequest(builder);
102   finalize(builder, fbs::ChreMessage::HubInfoRequest, request.Union());
103 }
104 
encodeFragmentedLoadNanoappRequest(flatbuffers::FlatBufferBuilder & builder,const FragmentedLoadRequest & request,bool respondBeforeStart)105 void HostProtocolHost::encodeFragmentedLoadNanoappRequest(
106     flatbuffers::FlatBufferBuilder &builder,
107     const FragmentedLoadRequest &request, bool respondBeforeStart) {
108   encodeLoadNanoappRequestForBinary(
109       builder, request.transactionId, request.appId, request.appVersion,
110       request.appFlags, request.targetApiVersion, request.binary,
111       request.fragmentId, request.appTotalSizeBytes, respondBeforeStart);
112 }
113 
encodeNanoappListRequest(FlatBufferBuilder & builder)114 void HostProtocolHost::encodeNanoappListRequest(FlatBufferBuilder &builder) {
115   auto request = fbs::CreateNanoappListRequest(builder);
116   finalize(builder, fbs::ChreMessage::NanoappListRequest, request.Union());
117 }
118 
encodeUnloadNanoappRequest(FlatBufferBuilder & builder,uint32_t transactionId,uint64_t appId,bool allowSystemNanoappUnload)119 void HostProtocolHost::encodeUnloadNanoappRequest(
120     FlatBufferBuilder &builder, uint32_t transactionId, uint64_t appId,
121     bool allowSystemNanoappUnload) {
122   auto request = fbs::CreateUnloadNanoappRequest(builder, transactionId, appId,
123                                                  allowSystemNanoappUnload);
124   finalize(builder, fbs::ChreMessage::UnloadNanoappRequest, request.Union());
125 }
126 
encodeTimeSyncMessage(FlatBufferBuilder & builder,int64_t offset)127 void HostProtocolHost::encodeTimeSyncMessage(FlatBufferBuilder &builder,
128                                              int64_t offset) {
129   auto request = fbs::CreateTimeSyncMessage(builder, offset);
130   finalize(builder, fbs::ChreMessage::TimeSyncMessage, request.Union());
131 }
132 
encodeDebugDumpRequest(FlatBufferBuilder & builder)133 void HostProtocolHost::encodeDebugDumpRequest(FlatBufferBuilder &builder) {
134   auto request = fbs::CreateDebugDumpRequest(builder);
135   finalize(builder, fbs::ChreMessage::DebugDumpRequest, request.Union());
136 }
137 
extractHostClientIdAndType(const void * message,size_t messageLen,uint16_t * hostClientId,::chre::fbs::ChreMessage * messageType)138 bool HostProtocolHost::extractHostClientIdAndType(
139     const void *message, size_t messageLen, uint16_t *hostClientId,
140     ::chre::fbs::ChreMessage *messageType) {
141   bool success = false;
142   if (hostClientId != nullptr && messageType != nullptr) {
143     success = verifyMessage(message, messageLen);
144 
145     if (success) {
146       const fbs::MessageContainer *container =
147           fbs::GetMessageContainer(message);
148       // host_addr guaranteed to be non-null via verifyMessage (it's a required
149       // field)
150       *hostClientId = container->host_addr()->client_id();
151       *messageType = container->message_type();
152     }
153   }
154 
155   return success;
156 }
157 
mutateHostClientId(void * message,size_t messageLen,uint16_t hostClientId)158 bool HostProtocolHost::mutateHostClientId(void *message, size_t messageLen,
159                                           uint16_t hostClientId) {
160   bool success = verifyMessage(message, messageLen);
161 
162   if (!success) {
163     LOGE("Message verification failed - can't mutate host ID");
164   } else {
165     fbs::MessageContainer *container = fbs::GetMutableMessageContainer(message);
166     // host_addr guaranteed to be non-null via verifyMessage (it's a required
167     // field)
168     container->mutable_host_addr()->mutate_client_id(hostClientId);
169     success = true;
170   }
171 
172   return success;
173 }
174 
encodeLoadNanoappRequestForBinary(FlatBufferBuilder & builder,uint32_t transactionId,uint64_t appId,uint32_t appVersion,uint32_t appFlags,uint32_t targetApiVersion,const std::vector<uint8_t> & nanoappBinary,uint32_t fragmentId,size_t appTotalSizeBytes,bool respondBeforeStart)175 void HostProtocolHost::encodeLoadNanoappRequestForBinary(
176     FlatBufferBuilder &builder, uint32_t transactionId, uint64_t appId,
177     uint32_t appVersion, uint32_t appFlags, uint32_t targetApiVersion,
178     const std::vector<uint8_t> &nanoappBinary, uint32_t fragmentId,
179     size_t appTotalSizeBytes, bool respondBeforeStart) {
180   auto appBinary = builder.CreateVector(nanoappBinary);
181   auto request = fbs::CreateLoadNanoappRequest(
182       builder, transactionId, appId, appVersion, targetApiVersion, appBinary,
183       fragmentId, appTotalSizeBytes, 0 /* app_binary_file_name */, appFlags,
184       respondBeforeStart);
185   finalize(builder, fbs::ChreMessage::LoadNanoappRequest, request.Union());
186 }
187 
encodeLoadNanoappRequestForFile(flatbuffers::FlatBufferBuilder & builder,uint32_t transactionId,uint64_t appId,uint32_t appVersion,uint32_t targetApiVersion,const char * nanoappBinaryName)188 void HostProtocolHost::encodeLoadNanoappRequestForFile(
189     flatbuffers::FlatBufferBuilder &builder, uint32_t transactionId,
190     uint64_t appId, uint32_t appVersion, uint32_t targetApiVersion,
191     const char *nanoappBinaryName) {
192   const std::vector<uint8_t> emptyAppBinary;
193   auto appBinary = builder.CreateVector(emptyAppBinary);
194   auto appBinaryName = addStringAsByteVector(builder, nanoappBinaryName);
195   auto request = fbs::CreateLoadNanoappRequest(
196       builder, transactionId, appId, appVersion, targetApiVersion, appBinary,
197       0 /* fragmentId */, 0 /* appTotalSizeBytes */, appBinaryName);
198   finalize(builder, fbs::ChreMessage::LoadNanoappRequest, request.Union());
199 }
200 
encodeSettingChangeNotification(flatbuffers::FlatBufferBuilder & builder,::chre::fbs::Setting setting,::chre::fbs::SettingState newState)201 void HostProtocolHost::encodeSettingChangeNotification(
202     flatbuffers::FlatBufferBuilder &builder, ::chre::fbs::Setting setting,
203     ::chre::fbs::SettingState newState) {
204   auto notification =
205       fbs::CreateSettingChangeMessage(builder, setting, newState);
206   finalize(builder, fbs::ChreMessage::SettingChangeMessage,
207            notification.Union());
208 }
209 
encodeSelfTestRequest(flatbuffers::FlatBufferBuilder & builder)210 void HostProtocolHost::encodeSelfTestRequest(
211     flatbuffers::FlatBufferBuilder &builder) {
212   auto request = fbs::CreateSelfTestRequest(builder);
213   finalize(builder, fbs::ChreMessage::SelfTestRequest, request.Union());
214 }
215 
216 }  // namespace chre
217 }  // namespace android
218