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 default:
87 LOGW("Got invalid/unexpected message type %" PRIu8,
88 static_cast<uint8_t>(msg.type));
89 success = false;
90 }
91 }
92
93 return success;
94 }
95
encodeHubInfoRequest(FlatBufferBuilder & builder)96 void HostProtocolHost::encodeHubInfoRequest(FlatBufferBuilder &builder) {
97 auto request = fbs::CreateHubInfoRequest(builder);
98 finalize(builder, fbs::ChreMessage::HubInfoRequest, request.Union());
99 }
100
encodeFragmentedLoadNanoappRequest(flatbuffers::FlatBufferBuilder & builder,const FragmentedLoadRequest & request)101 void HostProtocolHost::encodeFragmentedLoadNanoappRequest(
102 flatbuffers::FlatBufferBuilder &builder,
103 const FragmentedLoadRequest &request) {
104 encodeLoadNanoappRequestForBinary(
105 builder, request.transactionId, request.appId, request.appVersion,
106 request.targetApiVersion, request.binary, request.fragmentId,
107 request.appTotalSizeBytes);
108 }
109
encodeNanoappListRequest(FlatBufferBuilder & builder)110 void HostProtocolHost::encodeNanoappListRequest(FlatBufferBuilder &builder) {
111 auto request = fbs::CreateNanoappListRequest(builder);
112 finalize(builder, fbs::ChreMessage::NanoappListRequest, request.Union());
113 }
114
encodeUnloadNanoappRequest(FlatBufferBuilder & builder,uint32_t transactionId,uint64_t appId,bool allowSystemNanoappUnload)115 void HostProtocolHost::encodeUnloadNanoappRequest(
116 FlatBufferBuilder &builder, uint32_t transactionId, uint64_t appId,
117 bool allowSystemNanoappUnload) {
118 auto request = fbs::CreateUnloadNanoappRequest(builder, transactionId, appId,
119 allowSystemNanoappUnload);
120 finalize(builder, fbs::ChreMessage::UnloadNanoappRequest, request.Union());
121 }
122
encodeTimeSyncMessage(FlatBufferBuilder & builder,int64_t offset)123 void HostProtocolHost::encodeTimeSyncMessage(FlatBufferBuilder &builder,
124 int64_t offset) {
125 auto request = fbs::CreateTimeSyncMessage(builder, offset);
126 finalize(builder, fbs::ChreMessage::TimeSyncMessage, request.Union());
127 }
128
encodeDebugDumpRequest(FlatBufferBuilder & builder)129 void HostProtocolHost::encodeDebugDumpRequest(FlatBufferBuilder &builder) {
130 auto request = fbs::CreateDebugDumpRequest(builder);
131 finalize(builder, fbs::ChreMessage::DebugDumpRequest, request.Union());
132 }
133
extractHostClientIdAndType(const void * message,size_t messageLen,uint16_t * hostClientId,::chre::fbs::ChreMessage * messageType)134 bool HostProtocolHost::extractHostClientIdAndType(
135 const void *message, size_t messageLen, uint16_t *hostClientId,
136 ::chre::fbs::ChreMessage *messageType) {
137 bool success = false;
138 if (hostClientId != nullptr && messageType != nullptr) {
139 success = verifyMessage(message, messageLen);
140
141 if (success) {
142 const fbs::MessageContainer *container =
143 fbs::GetMessageContainer(message);
144 // host_addr guaranteed to be non-null via verifyMessage (it's a required
145 // field)
146 *hostClientId = container->host_addr()->client_id();
147 *messageType = container->message_type();
148 }
149 }
150
151 return success;
152 }
153
mutateHostClientId(void * message,size_t messageLen,uint16_t hostClientId)154 bool HostProtocolHost::mutateHostClientId(void *message, size_t messageLen,
155 uint16_t hostClientId) {
156 bool success = verifyMessage(message, messageLen);
157
158 if (!success) {
159 LOGE("Message verification failed - can't mutate host ID");
160 } else {
161 fbs::MessageContainer *container = fbs::GetMutableMessageContainer(message);
162 // host_addr guaranteed to be non-null via verifyMessage (it's a required
163 // field)
164 container->mutable_host_addr()->mutate_client_id(hostClientId);
165 success = true;
166 }
167
168 return success;
169 }
170
encodeLoadNanoappRequestForBinary(FlatBufferBuilder & builder,uint32_t transactionId,uint64_t appId,uint32_t appVersion,uint32_t targetApiVersion,const std::vector<uint8_t> & nanoappBinary,uint32_t fragmentId,size_t appTotalSizeBytes)171 void HostProtocolHost::encodeLoadNanoappRequestForBinary(
172 FlatBufferBuilder &builder, uint32_t transactionId, uint64_t appId,
173 uint32_t appVersion, uint32_t targetApiVersion,
174 const std::vector<uint8_t> &nanoappBinary, uint32_t fragmentId,
175 size_t appTotalSizeBytes) {
176 auto appBinary = builder.CreateVector(nanoappBinary);
177 auto request = fbs::CreateLoadNanoappRequest(
178 builder, transactionId, appId, appVersion, targetApiVersion, appBinary,
179 fragmentId, appTotalSizeBytes);
180 finalize(builder, fbs::ChreMessage::LoadNanoappRequest, request.Union());
181 }
182
encodeLoadNanoappRequestForFile(flatbuffers::FlatBufferBuilder & builder,uint32_t transactionId,uint64_t appId,uint32_t appVersion,uint32_t targetApiVersion,const char * nanoappBinaryName)183 void HostProtocolHost::encodeLoadNanoappRequestForFile(
184 flatbuffers::FlatBufferBuilder &builder, uint32_t transactionId,
185 uint64_t appId, uint32_t appVersion, uint32_t targetApiVersion,
186 const char *nanoappBinaryName) {
187 const std::vector<uint8_t> emptyAppBinary;
188 auto appBinary = builder.CreateVector(emptyAppBinary);
189 auto appBinaryName = addStringAsByteVector(builder, nanoappBinaryName);
190 auto request = fbs::CreateLoadNanoappRequest(
191 builder, transactionId, appId, appVersion, targetApiVersion, appBinary,
192 0 /* fragmentId */, 0 /* appTotalSizeBytes */, appBinaryName);
193 finalize(builder, fbs::ChreMessage::LoadNanoappRequest, request.Union());
194 }
195
encodeSettingChangeNotification(flatbuffers::FlatBufferBuilder & builder,::chre::fbs::Setting setting,::chre::fbs::SettingState newState)196 void HostProtocolHost::encodeSettingChangeNotification(
197 flatbuffers::FlatBufferBuilder &builder, ::chre::fbs::Setting setting,
198 ::chre::fbs::SettingState newState) {
199 auto notification =
200 fbs::CreateSettingChangeMessage(builder, setting, newState);
201 finalize(builder, fbs::ChreMessage::SettingChangeMessage,
202 notification.Union());
203 }
204
205 } // namespace chre
206 } // namespace android
207