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_host/host_protocol_host.h"
19 #include "chre_host/log.h"
20 #include "chre_host/socket_client.h"
21 
22 #include <inttypes.h>
23 #include <sys/socket.h>
24 #include <sys/types.h>
25 
26 #include <fstream>
27 #include <sstream>
28 #include <thread>
29 
30 #include <cutils/sockets.h>
31 #include <utils/StrongPointer.h>
32 
33 /**
34  * @file
35  * A test utility that connects to the CHRE daemon that runs on the apps
36  * processor of MSM chipsets, which is used to help test basic functionality.
37  *
38  * Usage:
39  *  chre_test_client load <nanoapp-id> <nanoapp-path> \
40  *      [app-version] [api-version]
41  *  chre_test_client unload <nanoapp-id>
42  */
43 
44 using android::sp;
45 using android::chre::getStringFromByteVector;
46 using android::chre::FragmentedLoadTransaction;
47 using android::chre::HostProtocolHost;
48 using android::chre::IChreMessageHandlers;
49 using android::chre::SocketClient;
50 using flatbuffers::FlatBufferBuilder;
51 
52 // Aliased for consistency with the way these symbols are referenced in
53 // CHRE-side code
54 namespace fbs = ::chre::fbs;
55 
56 namespace {
57 
58 //! The host endpoint we use when sending; set to CHRE_HOST_ENDPOINT_UNSPECIFIED
59 //! Other clients below the HAL may use a value above 0x8000 to enable unicast
60 //! messaging (currently requires internal coordination to avoid conflict;
61 //! in the future these should be assigned by the daemon).
62 constexpr uint16_t kHostEndpoint = 0xfffe;
63 
64 constexpr uint32_t kDefaultAppVersion = 1;
65 constexpr uint32_t kDefaultApiVersion = 0x01000000;
66 
67 class SocketCallbacks : public SocketClient::ICallbacks,
68                         public IChreMessageHandlers {
69  public:
onMessageReceived(const void * data,size_t length)70   void onMessageReceived(const void *data, size_t length) override {
71     if (!HostProtocolHost::decodeMessageFromChre(data, length, *this)) {
72       LOGE("Failed to decode message");
73     }
74   }
75 
onConnected()76   void onConnected() override {
77     LOGI("Socket (re)connected");
78   }
79 
onConnectionAborted()80   void onConnectionAborted() override {
81     LOGI("Socket (re)connection aborted");
82   }
83 
onDisconnected()84   void onDisconnected() override {
85     LOGI("Socket disconnected");
86   }
87 
handleNanoappMessage(const fbs::NanoappMessageT & message)88   void handleNanoappMessage(const fbs::NanoappMessageT& message)
89       override {
90     LOGI("Got message from nanoapp 0x%" PRIx64 " to endpoint 0x%" PRIx16
91          " with type 0x%" PRIx32 " and length %zu", message.app_id,
92          message.host_endpoint, message.message_type, message.message.size());
93   }
94 
handleHubInfoResponse(const fbs::HubInfoResponseT & rsp)95   void handleHubInfoResponse(const fbs::HubInfoResponseT& rsp)
96       override {
97     LOGI("Got hub info response:");
98     LOGI("  Name: '%s'", getStringFromByteVector(rsp.name));
99     LOGI("  Vendor: '%s'", getStringFromByteVector(rsp.vendor));
100     LOGI("  Toolchain: '%s'", getStringFromByteVector(rsp.toolchain));
101     LOGI("  Legacy versions: platform 0x%08" PRIx32 " toolchain 0x%08" PRIx32,
102          rsp.platform_version, rsp.toolchain_version);
103     LOGI("  MIPS %.2f Power (mW): stopped %.2f sleep %.2f peak %.2f",
104          rsp.peak_mips, rsp.stopped_power, rsp.sleep_power, rsp.peak_power);
105     LOGI("  Max message len: %" PRIu32, rsp.max_msg_len);
106     LOGI("  Platform ID: 0x%016" PRIx64 " Version: 0x%08" PRIx32,
107          rsp.platform_id, rsp.chre_platform_version);
108   }
109 
handleNanoappListResponse(const fbs::NanoappListResponseT & response)110   void handleNanoappListResponse(const fbs::NanoappListResponseT& response)
111       override {
112     LOGI("Got nanoapp list response with %zu apps:", response.nanoapps.size());
113     for (const std::unique_ptr<fbs::NanoappListEntryT>& nanoapp
114            : response.nanoapps) {
115       LOGI("  App ID 0x%016" PRIx64 " version 0x%" PRIx32 " enabled %d system "
116            "%d", nanoapp->app_id, nanoapp->version, nanoapp->enabled,
117            nanoapp->is_system);
118     }
119   }
120 
handleLoadNanoappResponse(const fbs::LoadNanoappResponseT & response)121   void handleLoadNanoappResponse(const fbs::LoadNanoappResponseT& response)
122       override {
123     LOGI("Got load nanoapp response, transaction ID 0x%" PRIx32 " result %d",
124          response.transaction_id, response.success);
125   }
126 
handleUnloadNanoappResponse(const fbs::UnloadNanoappResponseT & response)127   void handleUnloadNanoappResponse(const fbs::UnloadNanoappResponseT& response)
128       override {
129     LOGI("Got unload nanoapp response, transaction ID 0x%" PRIx32 " result %d",
130          response.transaction_id, response.success);
131   }
132 };
133 
requestHubInfo(SocketClient & client)134 void requestHubInfo(SocketClient& client) {
135   FlatBufferBuilder builder(64);
136   HostProtocolHost::encodeHubInfoRequest(builder);
137 
138   LOGI("Sending hub info request (%" PRIu32 " bytes)", builder.GetSize());
139   if (!client.sendMessage(builder.GetBufferPointer(), builder.GetSize())) {
140     LOGE("Failed to send message");
141   }
142 }
143 
requestNanoappList(SocketClient & client)144 void requestNanoappList(SocketClient& client) {
145   FlatBufferBuilder builder(64);
146   HostProtocolHost::encodeNanoappListRequest(builder);
147 
148   LOGI("Sending app list request (%" PRIu32 " bytes)", builder.GetSize());
149   if (!client.sendMessage(builder.GetBufferPointer(), builder.GetSize())) {
150     LOGE("Failed to send message");
151   }
152 }
153 
sendMessageToNanoapp(SocketClient & client)154 void sendMessageToNanoapp(SocketClient& client) {
155   FlatBufferBuilder builder(64);
156   uint8_t messageData[] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11};
157   HostProtocolHost::encodeNanoappMessage(
158       builder, chre::kMessageWorldAppId, 1234 /* messageType */,
159       kHostEndpoint, messageData, sizeof(messageData));
160 
161   LOGI("Sending message to nanoapp (%" PRIu32 " bytes w/%zu bytes of payload)",
162        builder.GetSize(), sizeof(messageData));
163   if (!client.sendMessage(builder.GetBufferPointer(), builder.GetSize())) {
164     LOGE("Failed to send message");
165   }
166 }
167 
sendLoadNanoappRequest(SocketClient & client,const char * filename,uint64_t appId,uint32_t appVersion,uint32_t apiVersion)168 void sendLoadNanoappRequest(SocketClient& client, const char *filename,
169                             uint64_t appId, uint32_t appVersion,
170                             uint32_t apiVersion) {
171   std::ifstream file(filename, std::ios::binary | std::ios::ate);
172   if (!file) {
173     LOGE("Couldn't open file '%s': %s", filename, strerror(errno));
174     return;
175   }
176   ssize_t size = file.tellg();
177   file.seekg(0, std::ios::beg);
178 
179   std::vector<uint8_t> buffer(size);
180   if (!file.read(reinterpret_cast<char *>(buffer.data()), size)) {
181     LOGE("Couldn't read from file: %s", strerror(errno));
182     return;
183   }
184 
185   // Perform loading with 1 fragment for simplicity
186   FlatBufferBuilder builder(size + 128);
187   FragmentedLoadTransaction transaction = FragmentedLoadTransaction(
188       1 /* transactionId */, appId, appVersion, apiVersion,
189       buffer, buffer.size() /* fragmentSize */);
190   HostProtocolHost::encodeFragmentedLoadNanoappRequest(
191       builder, transaction.getNextRequest());
192 
193   LOGI("Sending load nanoapp request (%" PRIu32 " bytes total w/%zu bytes of "
194        "payload)", builder.GetSize(), buffer.size());
195   if (!client.sendMessage(builder.GetBufferPointer(), builder.GetSize())) {
196     LOGE("Failed to send message");
197   }
198 }
199 
sendUnloadNanoappRequest(SocketClient & client,uint64_t appId)200 void sendUnloadNanoappRequest(SocketClient& client, uint64_t appId) {
201   FlatBufferBuilder builder(48);
202   constexpr uint32_t kTransactionId = 4321;
203   HostProtocolHost::encodeUnloadNanoappRequest(
204       builder, kTransactionId, appId, true /* allowSystemNanoappUnload */);
205 
206   LOGI("Sending unload request for nanoapp 0x%016" PRIx64 " (size %" PRIu32 ")",
207        appId, builder.GetSize());
208   if (!client.sendMessage(builder.GetBufferPointer(), builder.GetSize())) {
209     LOGE("Failed to send message");
210   }
211 }
212 
213 }  // anonymous namespace
214 
usage(const std::string & name)215 static void usage(const std::string &name) {
216   std::string output;
217 
218   output =
219     "\n"
220     "Usage:\n  " + name + " load <nanoapp-id> <nanoapp-path> "
221     "[app-version] [api-version]\n  " + name + " unload <nanoapp-id>\n";
222 
223   LOGI("%s", output.c_str());
224 }
225 
main(int argc,char * argv[])226 int main(int argc, char *argv[]) {
227   int argi = 0;
228   const std::string name{argv[argi++]};
229   const std::string cmd{argi < argc ? argv[argi++] : ""};
230   const std::string idstr{argi < argc ? argv[argi++] : ""};
231   const std::string path{argi < argc ? argv[argi++] : ""};
232   const std::string appVerStr{argi < argc ? argv[argi++] : ""};
233   const std::string apiVerStr{argi < argc ? argv[argi++] : ""};
234 
235   SocketClient client;
236   sp<SocketCallbacks> callbacks = new SocketCallbacks();
237 
238   if (!client.connect("chre", callbacks)) {
239     LOGE("Couldn't connect to socket");
240     return -1;
241   }
242 
243   if (cmd.empty()) {
244     requestHubInfo(client);
245     requestNanoappList(client);
246     sendMessageToNanoapp(client);
247     sendLoadNanoappRequest(client, "/data/activity.so",
248         0x476f6f676c00100b /* appId */, 0 /* appVersion */,
249         0x01000000 /* targetApiVersion */);
250     sendUnloadNanoappRequest(client, 0x476f6f676c00100b /* appId */);
251 
252     LOGI("Sleeping, waiting on responses");
253     std::this_thread::sleep_for(std::chrono::seconds(5));
254   } else if (cmd == "load") {
255     uint64_t id = 0;
256     uint32_t appVersion = kDefaultAppVersion;
257     uint32_t apiVersion = kDefaultApiVersion;
258 
259     if (idstr.empty() || path.empty()) {
260       LOGE("Arguments not provided!");
261       usage(name);
262       return -1;
263     }
264     std::istringstream(idstr) >> std::setbase(0) >> id;
265     if (!appVerStr.empty()) {
266         std::istringstream(appVerStr) >> std::setbase(0) >> appVersion;
267     }
268     if (!apiVerStr.empty()) {
269         std::istringstream(apiVerStr) >> std::setbase(0) >> apiVersion;
270     }
271     sendLoadNanoappRequest(client, path.c_str(), id, appVersion, apiVersion);
272   } else if (cmd == "unload") {
273     uint64_t id = 0;
274 
275     if (idstr.empty()) {
276       LOGE("Arguments not provided!");
277       usage(name);
278       return -1;
279     }
280     std::istringstream(idstr) >> std::setbase(0) >> id;
281     sendUnloadNanoappRequest(client, id);
282   } else {
283     LOGE("Invalid command provided!");
284     usage(name);
285     return -1;
286   }
287 
288   return 0;
289 }
290