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