1 /*
2 * Copyright (C) 2018 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/socket_client.h"
22
23 #include <inttypes.h>
24 #include <sys/socket.h>
25 #include <sys/types.h>
26
27 #include <fstream>
28 #include <thread>
29
30 #include <cutils/sockets.h>
31 #include <utils/StrongPointer.h>
32
33 /**
34 * @file
35 * A test utility that loads the audio stress test nanoapp and quits.
36 */
37
38 using android::sp;
39 using android::chre::FragmentedLoadTransaction;
40 using android::chre::getStringFromByteVector;
41 using android::chre::HostProtocolHost;
42 using android::chre::IChreMessageHandlers;
43 using android::chre::SocketClient;
44 using flatbuffers::FlatBufferBuilder;
45
46 // Aliased for consistency with the way these symbols are referenced in
47 // CHRE-side code
48 namespace fbs = ::chre::fbs;
49
50 namespace {
51
52 class SocketCallbacks : public SocketClient::ICallbacks,
53 public IChreMessageHandlers {
54 public:
onMessageReceived(const void * data,size_t length)55 void onMessageReceived(const void *data, size_t length) override {
56 if (!HostProtocolHost::decodeMessageFromChre(data, length, *this)) {
57 LOGE("Failed to decode message");
58 }
59 }
60
onConnected()61 void onConnected() override {
62 LOGI("Socket (re)connected");
63 }
64
onConnectionAborted()65 void onConnectionAborted() override {
66 LOGI("Socket (re)connection aborted");
67 }
68
onDisconnected()69 void onDisconnected() override {
70 LOGI("Socket disconnected");
71 }
72
handleLoadNanoappResponse(const fbs::LoadNanoappResponseT & response)73 void handleLoadNanoappResponse(
74 const fbs::LoadNanoappResponseT &response) override {
75 LOGI("Got load nanoapp response, transaction ID 0x%" PRIx32 " result %d",
76 response.transaction_id, response.success);
77 }
78 };
79
sendLoadNanoappRequest(SocketClient & client,const char * filename,uint64_t appId,uint32_t appVersion)80 void sendLoadNanoappRequest(SocketClient &client, const char *filename,
81 uint64_t appId, uint32_t appVersion) {
82 std::ifstream file(filename, std::ios::binary | std::ios::ate);
83 if (!file) {
84 LOGE("Couldn't open file '%s': %s", filename, strerror(errno));
85 return;
86 }
87 ssize_t size = file.tellg();
88 file.seekg(0, std::ios::beg);
89
90 std::vector<uint8_t> buffer(size);
91 if (!file.read(reinterpret_cast<char *>(buffer.data()), size)) {
92 LOGE("Couldn't read from file: %s", strerror(errno));
93 return;
94 }
95
96 // All loaded nanoapps must be signed currently.
97 uint32_t appFlags = CHRE_NAPP_HEADER_SIGNED;
98
99 // Perform loading with 1 fragment for simplicity
100 FlatBufferBuilder builder(size + 128);
101 FragmentedLoadTransaction transaction =
102 FragmentedLoadTransaction(1 /* transactionId */, appId, appVersion,
103 appFlags, 0x01000000 /* targetApiVersion */,
104 buffer, buffer.size() /* fragmentSize */);
105 HostProtocolHost::encodeFragmentedLoadNanoappRequest(
106 builder, transaction.getNextRequest());
107
108 LOGI("Sending load nanoapp request (%" PRIu32
109 " bytes total w/%zu bytes of payload)",
110 builder.GetSize(), buffer.size());
111 if (!client.sendMessage(builder.GetBufferPointer(), builder.GetSize())) {
112 LOGE("Failed to send message");
113 }
114 }
115
116 } // anonymous namespace
117
main()118 int main() {
119 SocketClient client;
120 sp<SocketCallbacks> callbacks = new SocketCallbacks();
121
122 if (!client.connect("chre", callbacks)) {
123 LOGE("Couldn't connect to socket");
124 } else {
125 sendLoadNanoappRequest(client, "/data/audio_stress_test.so",
126 chre::kAudioStressTestAppId, 1 /* appVersion */);
127 }
128
129 return 0;
130 }
131