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