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/util/nanoapp/app_id.h"
18 #include "chre/util/system/napp_header_utils.h"
19 #include "chre_host/host_protocol_host.h"
20 #include "chre_host/log.h"
21 #include "chre_host/napp_header.h"
22 #include "chre_host/socket_client.h"
23
24 #include <inttypes.h>
25 #include <sys/socket.h>
26 #include <sys/types.h>
27
28 #include <fstream>
29 #include <future>
30 #include <sstream>
31 #include <thread>
32
33 #include <cutils/sockets.h>
34 #include <utils/StrongPointer.h>
35
36 /**
37 * @file
38 * A test utility that connects to the CHRE daemon that runs on the apps
39 * processor of MSM chipsets, which is used to help test basic functionality.
40 *
41 * Usage:
42 * chre_test_client load <nanoapp-id> <nanoapp-so-path> \
43 * [app-version] [api-version] [tcm-capable] [nanoapp-header-path]
44 * chre_test_client load_with_header <nanoapp-header-path> <nanoapp-so-path>
45 * chre_test_client unload <nanoapp-id>
46 */
47
48 using android::sp;
49 using android::chre::FragmentedLoadTransaction;
50 using android::chre::getStringFromByteVector;
51 using android::chre::HostProtocolHost;
52 using android::chre::IChreMessageHandlers;
53 using android::chre::NanoAppBinaryHeader;
54 using android::chre::SocketClient;
55 using flatbuffers::FlatBufferBuilder;
56
57 // Aliased for consistency with the way these symbols are referenced in
58 // CHRE-side code
59 namespace fbs = ::chre::fbs;
60
61 namespace {
62
63 //! The host endpoint we use when sending; set to CHRE_HOST_ENDPOINT_UNSPECIFIED
64 //! Other clients below the HAL may use a value above 0x8000 to enable unicast
65 //! messaging (currently requires internal coordination to avoid conflict;
66 //! in the future these should be assigned by the daemon).
67 constexpr uint16_t kHostEndpoint = 0xfffe;
68
69 constexpr uint32_t kDefaultAppVersion = 1;
70 constexpr uint32_t kDefaultApiVersion = 0x01000000;
71
72 class SocketCallbacks : public SocketClient::ICallbacks,
73 public IChreMessageHandlers {
74 public:
onMessageReceived(const void * data,size_t length)75 void onMessageReceived(const void *data, size_t length) override {
76 if (!HostProtocolHost::decodeMessageFromChre(data, length, *this)) {
77 LOGE("Failed to decode message");
78 }
79 }
80
onConnected()81 void onConnected() override {
82 LOGI("Socket (re)connected");
83 }
84
onConnectionAborted()85 void onConnectionAborted() override {
86 LOGI("Socket (re)connection aborted");
87 }
88
onDisconnected()89 void onDisconnected() override {
90 LOGI("Socket disconnected");
91 }
92
handleNanoappMessage(const fbs::NanoappMessageT & message)93 void handleNanoappMessage(const fbs::NanoappMessageT &message) override {
94 LOGI("Got message from nanoapp 0x%" PRIx64 " to endpoint 0x%" PRIx16
95 " with type 0x%" PRIx32 " and length %zu",
96 message.app_id, message.host_endpoint, message.message_type,
97 message.message.size());
98 }
99
handleHubInfoResponse(const fbs::HubInfoResponseT & rsp)100 void handleHubInfoResponse(const fbs::HubInfoResponseT &rsp) override {
101 LOGI("Got hub info response:");
102 LOGI(" Name: '%s'", getStringFromByteVector(rsp.name));
103 LOGI(" Vendor: '%s'", getStringFromByteVector(rsp.vendor));
104 LOGI(" Toolchain: '%s'", getStringFromByteVector(rsp.toolchain));
105 LOGI(" Legacy versions: platform 0x%08" PRIx32 " toolchain 0x%08" PRIx32,
106 rsp.platform_version, rsp.toolchain_version);
107 LOGI(" MIPS %.2f Power (mW): stopped %.2f sleep %.2f peak %.2f",
108 rsp.peak_mips, rsp.stopped_power, rsp.sleep_power, rsp.peak_power);
109 LOGI(" Max message len: %" PRIu32, rsp.max_msg_len);
110 LOGI(" Platform ID: 0x%016" PRIx64 " Version: 0x%08" PRIx32,
111 rsp.platform_id, rsp.chre_platform_version);
112 }
113
handleNanoappListResponse(const fbs::NanoappListResponseT & response)114 void handleNanoappListResponse(
115 const fbs::NanoappListResponseT &response) override {
116 LOGI("Got nanoapp list response with %zu apps:", response.nanoapps.size());
117 for (const std::unique_ptr<fbs::NanoappListEntryT> &nanoapp :
118 response.nanoapps) {
119 LOGI(" App ID 0x%016" PRIx64 " version 0x%" PRIx32
120 " permissions 0x%" PRIx32 " enabled %d system %d",
121 nanoapp->app_id, nanoapp->version, nanoapp->permissions,
122 nanoapp->enabled, nanoapp->is_system);
123 }
124 }
125
handleLoadNanoappResponse(const fbs::LoadNanoappResponseT & response)126 void handleLoadNanoappResponse(
127 const fbs::LoadNanoappResponseT &response) override {
128 LOGI("Got load nanoapp response, transaction ID 0x%" PRIx32 " result %d",
129 response.transaction_id, response.success);
130 }
131
handleUnloadNanoappResponse(const fbs::UnloadNanoappResponseT & response)132 void handleUnloadNanoappResponse(
133 const fbs::UnloadNanoappResponseT &response) override {
134 LOGI("Got unload nanoapp response, transaction ID 0x%" PRIx32 " result %d",
135 response.transaction_id, response.success);
136 }
137
handleSelfTestResponse(const::chre::fbs::SelfTestResponseT & response)138 void handleSelfTestResponse(const ::chre::fbs::SelfTestResponseT &response) {
139 LOGI("Got self test response with success %d", response.success);
140 mResultPromise.set_value(response.success);
141 }
142
getResultFuture()143 std::future<bool> getResultFuture() {
144 return mResultPromise.get_future();
145 }
146
147 private:
148 std::promise<bool> mResultPromise;
149 };
150
requestHubInfo(SocketClient & client)151 void requestHubInfo(SocketClient &client) {
152 FlatBufferBuilder builder(64);
153 HostProtocolHost::encodeHubInfoRequest(builder);
154
155 LOGI("Sending hub info request (%" PRIu32 " bytes)", builder.GetSize());
156 if (!client.sendMessage(builder.GetBufferPointer(), builder.GetSize())) {
157 LOGE("Failed to send message");
158 }
159 }
160
requestNanoappList(SocketClient & client)161 void requestNanoappList(SocketClient &client) {
162 FlatBufferBuilder builder(64);
163 HostProtocolHost::encodeNanoappListRequest(builder);
164
165 LOGI("Sending app list request (%" PRIu32 " bytes)", builder.GetSize());
166 if (!client.sendMessage(builder.GetBufferPointer(), builder.GetSize())) {
167 LOGE("Failed to send message");
168 }
169 }
170
sendMessageToNanoapp(SocketClient & client)171 void sendMessageToNanoapp(SocketClient &client) {
172 FlatBufferBuilder builder(64);
173 uint8_t messageData[] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11};
174 HostProtocolHost::encodeNanoappMessage(builder, chre::kMessageWorldAppId,
175 1234 /* messageType */, kHostEndpoint,
176 messageData, sizeof(messageData));
177
178 LOGI("Sending message to nanoapp (%" PRIu32 " bytes w/%zu bytes of payload)",
179 builder.GetSize(), sizeof(messageData));
180 if (!client.sendMessage(builder.GetBufferPointer(), builder.GetSize())) {
181 LOGE("Failed to send message");
182 }
183 }
184
readFileContents(const char * filename,std::vector<uint8_t> * buffer)185 bool readFileContents(const char *filename, std::vector<uint8_t> *buffer) {
186 bool success = false;
187 std::ifstream file(filename, std::ios::binary | std::ios::ate);
188 if (!file) {
189 LOGE("Couldn't open file '%s': %d (%s)", filename, errno, strerror(errno));
190 } else {
191 ssize_t size = file.tellg();
192 file.seekg(0, std::ios::beg);
193
194 buffer->resize(size);
195 if (!file.read(reinterpret_cast<char *>(buffer->data()), size)) {
196 LOGE("Couldn't read from file '%s': %d (%s)", filename, errno,
197 strerror(errno));
198 } else {
199 success = true;
200 }
201 }
202
203 return success;
204 }
205
sendNanoappLoad(SocketClient & client,uint64_t appId,uint32_t appVersion,uint32_t apiVersion,uint32_t appFlags,const std::vector<uint8_t> & binary)206 void sendNanoappLoad(SocketClient &client, uint64_t appId, uint32_t appVersion,
207 uint32_t apiVersion, uint32_t appFlags,
208 const std::vector<uint8_t> &binary) {
209 // Perform loading with 1 fragment for simplicity
210 FlatBufferBuilder builder(binary.size() + 128);
211 FragmentedLoadTransaction transaction = FragmentedLoadTransaction(
212 1 /* transactionId */, appId, appVersion, appFlags, apiVersion, binary,
213 binary.size() /* fragmentSize */);
214 HostProtocolHost::encodeFragmentedLoadNanoappRequest(
215 builder, transaction.getNextRequest());
216
217 LOGI("Sending load nanoapp request (%" PRIu32
218 " bytes total w/%zu bytes of "
219 "payload)",
220 builder.GetSize(), binary.size());
221 if (!client.sendMessage(builder.GetBufferPointer(), builder.GetSize())) {
222 LOGE("Failed to send message");
223 }
224 }
225
sendLoadNanoappRequest(SocketClient & client,const char * headerPath,const char * binaryPath)226 void sendLoadNanoappRequest(SocketClient &client, const char *headerPath,
227 const char *binaryPath) {
228 std::vector<uint8_t> headerBuffer;
229 std::vector<uint8_t> binaryBuffer;
230 if (readFileContents(headerPath, &headerBuffer) &&
231 readFileContents(binaryPath, &binaryBuffer)) {
232 if (headerBuffer.size() != sizeof(NanoAppBinaryHeader)) {
233 LOGE("Header size mismatch");
234 } else {
235 // The header blob contains the struct above.
236 const auto *appHeader =
237 reinterpret_cast<const NanoAppBinaryHeader *>(headerBuffer.data());
238
239 // Build the target API version from major and minor.
240 uint32_t targetApiVersion = (appHeader->targetChreApiMajorVersion << 24) |
241 (appHeader->targetChreApiMinorVersion << 16);
242
243 sendNanoappLoad(client, appHeader->appId, appHeader->appVersion,
244 targetApiVersion, appHeader->flags, binaryBuffer);
245 }
246 }
247 }
248
sendLoadNanoappRequest(SocketClient & client,const char * filename,uint64_t appId,uint32_t appVersion,uint32_t apiVersion,bool tcmApp)249 void sendLoadNanoappRequest(SocketClient &client, const char *filename,
250 uint64_t appId, uint32_t appVersion,
251 uint32_t apiVersion, bool tcmApp) {
252 std::vector<uint8_t> buffer;
253 if (readFileContents(filename, &buffer)) {
254 // All loaded nanoapps must be signed currently.
255 uint32_t appFlags = CHRE_NAPP_HEADER_SIGNED;
256 if (tcmApp) {
257 appFlags |= CHRE_NAPP_HEADER_TCM_CAPABLE;
258 }
259
260 sendNanoappLoad(client, appId, appVersion, apiVersion, appFlags, buffer);
261 }
262 }
263
sendUnloadNanoappRequest(SocketClient & client,uint64_t appId)264 void sendUnloadNanoappRequest(SocketClient &client, uint64_t appId) {
265 FlatBufferBuilder builder(48);
266 constexpr uint32_t kTransactionId = 4321;
267 HostProtocolHost::encodeUnloadNanoappRequest(
268 builder, kTransactionId, appId, true /* allowSystemNanoappUnload */);
269
270 LOGI("Sending unload request for nanoapp 0x%016" PRIx64 " (size %" PRIu32 ")",
271 appId, builder.GetSize());
272 if (!client.sendMessage(builder.GetBufferPointer(), builder.GetSize())) {
273 LOGE("Failed to send message");
274 }
275 }
276
sendSelfTestRequest(SocketClient & client)277 void sendSelfTestRequest(SocketClient &client) {
278 FlatBufferBuilder builder(48);
279 HostProtocolHost::encodeSelfTestRequest(builder);
280
281 LOGI("Sending self test");
282 if (!client.sendMessage(builder.GetBufferPointer(), builder.GetSize())) {
283 LOGE("Failed to send message");
284 }
285 }
286
287 } // anonymous namespace
288
usage(const std::string & name)289 static void usage(const std::string &name) {
290 std::string output;
291
292 output =
293 "\n"
294 "Usage:\n " +
295 name +
296 " load <nanoapp-id> <nanoapp-so-path> [app-version] [api-version]\n " +
297 name + " load_with_header <nanoapp-header-path> <nanoapp-so-path>\n " +
298 name + " unload <nanoapp-id>\n " + name + " self_test\n";
299
300 LOGI("%s", output.c_str());
301 }
302
main(int argc,char * argv[])303 int main(int argc, char *argv[]) {
304 int argi = 0;
305 const std::string name{argv[argi++]};
306 const std::string cmd{argi < argc ? argv[argi++] : ""};
307
308 SocketClient client;
309 sp<SocketCallbacks> callbacks = new SocketCallbacks();
310
311 bool success = true;
312 if (!client.connect("chre", callbacks)) {
313 LOGE("Couldn't connect to socket");
314 success = false;
315 } else if (cmd.empty()) {
316 requestHubInfo(client);
317 requestNanoappList(client);
318 sendMessageToNanoapp(client);
319 sendLoadNanoappRequest(client, "/data/activity.so",
320 0x476f6f676c00100b /* appId */, 0 /* appVersion */,
321 0x01000000 /* targetApiVersion */,
322 false /* tcmCapable */);
323 sendUnloadNanoappRequest(client, 0x476f6f676c00100b /* appId */);
324
325 LOGI("Sleeping, waiting on responses");
326 std::this_thread::sleep_for(std::chrono::seconds(5));
327 } else if (cmd == "load_with_header") {
328 const std::string headerPath{argi < argc ? argv[argi++] : ""};
329 const std::string binaryPath{argi < argc ? argv[argi++] : ""};
330
331 if (headerPath.empty() || binaryPath.empty()) {
332 LOGE("Arguments not provided!");
333 usage(name);
334 success = false;
335 } else {
336 sendLoadNanoappRequest(client, headerPath.c_str(), binaryPath.c_str());
337 }
338 } else if (cmd == "load") {
339 const std::string idstr{argi < argc ? argv[argi++] : ""};
340 const std::string path{argi < argc ? argv[argi++] : ""};
341 const std::string appVerStr{argi < argc ? argv[argi++] : ""};
342 const std::string apiVerStr{argi < argc ? argv[argi++] : ""};
343 const std::string tcmCapStr{argi < argc ? argv[argi++] : ""};
344
345 uint64_t id = 0;
346 uint32_t appVersion = kDefaultAppVersion;
347 uint32_t apiVersion = kDefaultApiVersion;
348 bool tcmApp = false;
349
350 if (idstr.empty() || path.empty()) {
351 LOGE("Arguments not provided!");
352 usage(name);
353 success = false;
354 } else {
355 std::istringstream(idstr) >> std::setbase(0) >> id;
356 if (!appVerStr.empty()) {
357 std::istringstream(appVerStr) >> std::setbase(0) >> appVersion;
358 }
359 if (!apiVerStr.empty()) {
360 std::istringstream(apiVerStr) >> std::setbase(0) >> apiVersion;
361 }
362 if (!tcmCapStr.empty()) {
363 std::istringstream(tcmCapStr) >> tcmApp;
364 }
365 sendLoadNanoappRequest(client, path.c_str(), id, appVersion, apiVersion,
366 tcmApp);
367 }
368 } else if (cmd == "unload") {
369 const std::string idstr{argi < argc ? argv[argi++] : ""};
370 uint64_t id = 0;
371
372 if (idstr.empty()) {
373 LOGE("Arguments not provided!");
374 usage(name);
375 success = false;
376 } else {
377 std::istringstream(idstr) >> std::setbase(0) >> id;
378 sendUnloadNanoappRequest(client, id);
379 }
380 } else if (cmd == "self_test") {
381 sendSelfTestRequest(client);
382
383 std::future<bool> future = callbacks->getResultFuture();
384 std::future_status status = future.wait_for(std::chrono::seconds(5));
385
386 if (status != std::future_status::ready) {
387 LOGE("Self test timed out");
388 } else {
389 success = future.get();
390 }
391 } else {
392 LOGE("Invalid command provided!");
393 usage(name);
394 }
395
396 return success ? 0 : -1;
397 }
398