• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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::FragmentedLoadTransaction;
46 using android::chre::getStringFromByteVector;
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) override {
89     LOGI("Got message from nanoapp 0x%" PRIx64 " to endpoint 0x%" PRIx16
90          " with type 0x%" PRIx32 " and length %zu",
91          message.app_id, message.host_endpoint, message.message_type,
92          message.message.size());
93   }
94 
handleHubInfoResponse(const fbs::HubInfoResponseT & rsp)95   void handleHubInfoResponse(const fbs::HubInfoResponseT &rsp) override {
96     LOGI("Got hub info response:");
97     LOGI("  Name: '%s'", getStringFromByteVector(rsp.name));
98     LOGI("  Vendor: '%s'", getStringFromByteVector(rsp.vendor));
99     LOGI("  Toolchain: '%s'", getStringFromByteVector(rsp.toolchain));
100     LOGI("  Legacy versions: platform 0x%08" PRIx32 " toolchain 0x%08" PRIx32,
101          rsp.platform_version, rsp.toolchain_version);
102     LOGI("  MIPS %.2f Power (mW): stopped %.2f sleep %.2f peak %.2f",
103          rsp.peak_mips, rsp.stopped_power, rsp.sleep_power, rsp.peak_power);
104     LOGI("  Max message len: %" PRIu32, rsp.max_msg_len);
105     LOGI("  Platform ID: 0x%016" PRIx64 " Version: 0x%08" PRIx32,
106          rsp.platform_id, rsp.chre_platform_version);
107   }
108 
handleNanoappListResponse(const fbs::NanoappListResponseT & response)109   void handleNanoappListResponse(
110       const fbs::NanoappListResponseT &response) override {
111     LOGI("Got nanoapp list response with %zu apps:", response.nanoapps.size());
112     for (const std::unique_ptr<fbs::NanoappListEntryT> &nanoapp :
113          response.nanoapps) {
114       LOGI("  App ID 0x%016" PRIx64 " version 0x%" PRIx32
115            " enabled %d system "
116            "%d",
117            nanoapp->app_id, nanoapp->version, nanoapp->enabled,
118            nanoapp->is_system);
119     }
120   }
121 
handleLoadNanoappResponse(const fbs::LoadNanoappResponseT & response)122   void handleLoadNanoappResponse(
123       const fbs::LoadNanoappResponseT &response) override {
124     LOGI("Got load nanoapp response, transaction ID 0x%" PRIx32 " result %d",
125          response.transaction_id, response.success);
126   }
127 
handleUnloadNanoappResponse(const fbs::UnloadNanoappResponseT & response)128   void handleUnloadNanoappResponse(
129       const fbs::UnloadNanoappResponseT &response) override {
130     LOGI("Got unload nanoapp response, transaction ID 0x%" PRIx32 " result %d",
131          response.transaction_id, response.success);
132   }
133 };
134 
requestHubInfo(SocketClient & client)135 void requestHubInfo(SocketClient &client) {
136   FlatBufferBuilder builder(64);
137   HostProtocolHost::encodeHubInfoRequest(builder);
138 
139   LOGI("Sending hub info request (%" PRIu32 " bytes)", builder.GetSize());
140   if (!client.sendMessage(builder.GetBufferPointer(), builder.GetSize())) {
141     LOGE("Failed to send message");
142   }
143 }
144 
requestNanoappList(SocketClient & client)145 void requestNanoappList(SocketClient &client) {
146   FlatBufferBuilder builder(64);
147   HostProtocolHost::encodeNanoappListRequest(builder);
148 
149   LOGI("Sending app list request (%" PRIu32 " bytes)", builder.GetSize());
150   if (!client.sendMessage(builder.GetBufferPointer(), builder.GetSize())) {
151     LOGE("Failed to send message");
152   }
153 }
154 
sendMessageToNanoapp(SocketClient & client)155 void sendMessageToNanoapp(SocketClient &client) {
156   FlatBufferBuilder builder(64);
157   uint8_t messageData[] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11};
158   HostProtocolHost::encodeNanoappMessage(builder, chre::kMessageWorldAppId,
159                                          1234 /* messageType */, kHostEndpoint,
160                                          messageData, sizeof(messageData));
161 
162   LOGI("Sending message to nanoapp (%" PRIu32 " bytes w/%zu bytes of payload)",
163        builder.GetSize(), sizeof(messageData));
164   if (!client.sendMessage(builder.GetBufferPointer(), builder.GetSize())) {
165     LOGE("Failed to send message");
166   }
167 }
168 
sendLoadNanoappRequest(SocketClient & client,const char * filename,uint64_t appId,uint32_t appVersion,uint32_t apiVersion)169 void sendLoadNanoappRequest(SocketClient &client, const char *filename,
170                             uint64_t appId, uint32_t appVersion,
171                             uint32_t apiVersion) {
172   std::ifstream file(filename, std::ios::binary | std::ios::ate);
173   if (!file) {
174     LOGE("Couldn't open file '%s': %s", filename, strerror(errno));
175     return;
176   }
177   ssize_t size = file.tellg();
178   file.seekg(0, std::ios::beg);
179 
180   std::vector<uint8_t> buffer(size);
181   if (!file.read(reinterpret_cast<char *>(buffer.data()), size)) {
182     LOGE("Couldn't read from file: %s", strerror(errno));
183     return;
184   }
185 
186   // Perform loading with 1 fragment for simplicity
187   FlatBufferBuilder builder(size + 128);
188   FragmentedLoadTransaction transaction = FragmentedLoadTransaction(
189       1 /* transactionId */, appId, appVersion, apiVersion, buffer,
190       buffer.size() /* fragmentSize */);
191   HostProtocolHost::encodeFragmentedLoadNanoappRequest(
192       builder, transaction.getNextRequest());
193 
194   LOGI("Sending load nanoapp request (%" PRIu32
195        " bytes total w/%zu bytes of "
196        "payload)",
197        builder.GetSize(), buffer.size());
198   if (!client.sendMessage(builder.GetBufferPointer(), builder.GetSize())) {
199     LOGE("Failed to send message");
200   }
201 }
202 
sendUnloadNanoappRequest(SocketClient & client,uint64_t appId)203 void sendUnloadNanoappRequest(SocketClient &client, uint64_t appId) {
204   FlatBufferBuilder builder(48);
205   constexpr uint32_t kTransactionId = 4321;
206   HostProtocolHost::encodeUnloadNanoappRequest(
207       builder, kTransactionId, appId, true /* allowSystemNanoappUnload */);
208 
209   LOGI("Sending unload request for nanoapp 0x%016" PRIx64 " (size %" PRIu32 ")",
210        appId, builder.GetSize());
211   if (!client.sendMessage(builder.GetBufferPointer(), builder.GetSize())) {
212     LOGE("Failed to send message");
213   }
214 }
215 
216 }  // anonymous namespace
217 
usage(const std::string & name)218 static void usage(const std::string &name) {
219   std::string output;
220 
221   output =
222       "\n"
223       "Usage:\n  " +
224       name +
225       " load <nanoapp-id> <nanoapp-path> "
226       "[app-version] [api-version]\n  " +
227       name + " unload <nanoapp-id>\n";
228 
229   LOGI("%s", output.c_str());
230 }
231 
main(int argc,char * argv[])232 int main(int argc, char *argv[]) {
233   int argi = 0;
234   const std::string name{argv[argi++]};
235   const std::string cmd{argi < argc ? argv[argi++] : ""};
236   const std::string idstr{argi < argc ? argv[argi++] : ""};
237   const std::string path{argi < argc ? argv[argi++] : ""};
238   const std::string appVerStr{argi < argc ? argv[argi++] : ""};
239   const std::string apiVerStr{argi < argc ? argv[argi++] : ""};
240 
241   SocketClient client;
242   sp<SocketCallbacks> callbacks = new SocketCallbacks();
243 
244   if (!client.connect("chre", callbacks)) {
245     LOGE("Couldn't connect to socket");
246     return -1;
247   }
248 
249   if (cmd.empty()) {
250     requestHubInfo(client);
251     requestNanoappList(client);
252     sendMessageToNanoapp(client);
253     sendLoadNanoappRequest(client, "/data/activity.so",
254                            0x476f6f676c00100b /* appId */, 0 /* appVersion */,
255                            0x01000000 /* targetApiVersion */);
256     sendUnloadNanoappRequest(client, 0x476f6f676c00100b /* appId */);
257 
258     LOGI("Sleeping, waiting on responses");
259     std::this_thread::sleep_for(std::chrono::seconds(5));
260   } else if (cmd == "load") {
261     uint64_t id = 0;
262     uint32_t appVersion = kDefaultAppVersion;
263     uint32_t apiVersion = kDefaultApiVersion;
264 
265     if (idstr.empty() || path.empty()) {
266       LOGE("Arguments not provided!");
267       usage(name);
268       return -1;
269     }
270     std::istringstream(idstr) >> std::setbase(0) >> id;
271     if (!appVerStr.empty()) {
272       std::istringstream(appVerStr) >> std::setbase(0) >> appVersion;
273     }
274     if (!apiVerStr.empty()) {
275       std::istringstream(apiVerStr) >> std::setbase(0) >> apiVersion;
276     }
277     sendLoadNanoappRequest(client, path.c_str(), id, appVersion, apiVersion);
278   } else if (cmd == "unload") {
279     uint64_t id = 0;
280 
281     if (idstr.empty()) {
282       LOGE("Arguments not provided!");
283       usage(name);
284       return -1;
285     }
286     std::istringstream(idstr) >> std::setbase(0) >> id;
287     sendUnloadNanoappRequest(client, id);
288   } else {
289     LOGE("Invalid command provided!");
290     usage(name);
291     return -1;
292   }
293 
294   return 0;
295 }
296