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 <thread>
28
29 #include <cutils/sockets.h>
30 #include <utils/StrongPointer.h>
31
32 /**
33 * @file
34 * A test utility that connects to the CHRE daemon that runs on the apps
35 * processor of MSM chipsets, which is used to help test basic functionality.
36 */
37
38 using android::sp;
39 using android::chre::IChreMessageHandlers;
40 using android::chre::SocketClient;
41 using android::chre::HostProtocolHost;
42 using flatbuffers::FlatBufferBuilder;
43
44 // Aliased for consistency with the way these symbols are referenced in
45 // CHRE-side code
46 namespace fbs = ::chre::fbs;
47
48 namespace {
49
50 //! The host endpoint we use when sending; set to CHRE_HOST_ENDPOINT_UNSPECIFIED
51 constexpr uint16_t kHostEndpoint = 0xfffe;
52
53 class SocketCallbacks : public SocketClient::ICallbacks,
54 public IChreMessageHandlers {
55 public:
onMessageReceived(const void * data,size_t length)56 void onMessageReceived(const void *data, size_t length) override {
57 if (!HostProtocolHost::decodeMessageFromChre(data, length, *this)) {
58 LOGE("Failed to decode message");
59 }
60 }
61
onConnected()62 void onConnected() override {
63 LOGI("Socket (re)connected");
64 }
65
onConnectionAborted()66 void onConnectionAborted() override {
67 LOGI("Socket (re)connection aborted");
68 }
69
onDisconnected()70 void onDisconnected() override {
71 LOGI("Socket disconnected");
72 }
73
handleNanoappMessage(uint64_t appId,uint32_t messageType,uint16_t hostEndpoint,const void *,size_t messageLen)74 void handleNanoappMessage(
75 uint64_t appId, uint32_t messageType, uint16_t hostEndpoint,
76 const void * /*messageData*/, size_t messageLen) override {
77 LOGI("Got message from nanoapp 0x%" PRIx64 " to endpoint 0x%" PRIx16
78 " with type 0x%" PRIx32 " and length %zu", appId, hostEndpoint,
79 messageType, messageLen);
80 }
81
handleHubInfoResponse(const char * name,const char * vendor,const char * toolchain,uint32_t legacyPlatformVersion,uint32_t legacyToolchainVersion,float peakMips,float stoppedPower,float sleepPower,float peakPower,uint32_t maxMessageLen,uint64_t platformId,uint32_t version)82 void handleHubInfoResponse(
83 const char *name, const char *vendor,
84 const char *toolchain, uint32_t legacyPlatformVersion,
85 uint32_t legacyToolchainVersion, float peakMips, float stoppedPower,
86 float sleepPower, float peakPower, uint32_t maxMessageLen,
87 uint64_t platformId, uint32_t version) override {
88 LOGI("Got hub info response:");
89 LOGI(" Name: '%s'", name);
90 LOGI(" Vendor: '%s'", vendor);
91 LOGI(" Toolchain: '%s'", toolchain);
92 LOGI(" Legacy versions: platform 0x%08" PRIx32 " toolchain 0x%08" PRIx32,
93 legacyPlatformVersion, legacyToolchainVersion);
94 LOGI(" MIPS %.2f Power (mW): stopped %.2f sleep %.2f peak %.2f",
95 peakMips, stoppedPower, sleepPower, peakPower);
96 LOGI(" Max message len: %" PRIu32, maxMessageLen);
97 LOGI(" Platform ID: 0x%016" PRIx64 " Version: 0x%08x" PRIx32,
98 platformId, version);
99 }
100
handleNanoappListResponse(const fbs::NanoappListResponseT & response)101 void handleNanoappListResponse(
102 const fbs::NanoappListResponseT& response) override {
103 LOGI("Got nanoapp list response with %zu apps:", response.nanoapps.size());
104 for (const std::unique_ptr<fbs::NanoappListEntryT>& nanoapp
105 : response.nanoapps) {
106 LOGI(" App ID 0x%016" PRIx64 " version 0x%" PRIx32 " enabled %d system "
107 "%d", nanoapp->app_id, nanoapp->version, nanoapp->enabled,
108 nanoapp->is_system);
109 }
110 }
111
handleLoadNanoappResponse(const::chre::fbs::LoadNanoappResponseT & response)112 void handleLoadNanoappResponse(
113 const ::chre::fbs::LoadNanoappResponseT& response) override {
114 LOGI("Got load nanoapp response, transaction ID 0x%" PRIx32 " result %d",
115 response.transaction_id, response.success);
116 }
117 };
118
requestHubInfo(SocketClient & client)119 void requestHubInfo(SocketClient& client) {
120 FlatBufferBuilder builder(64);
121 HostProtocolHost::encodeHubInfoRequest(builder);
122
123 LOGI("Sending hub info request (%u bytes)", builder.GetSize());
124 if (!client.sendMessage(builder.GetBufferPointer(), builder.GetSize())) {
125 LOGE("Failed to send message");
126 }
127 }
128
requestNanoappList(SocketClient & client)129 void requestNanoappList(SocketClient& client) {
130 FlatBufferBuilder builder(64);
131 HostProtocolHost::encodeNanoappListRequest(builder);
132
133 LOGI("Sending app list request (%u bytes)", builder.GetSize());
134 if (!client.sendMessage(builder.GetBufferPointer(), builder.GetSize())) {
135 LOGE("Failed to send message");
136 }
137 }
138
sendMessageToNanoapp(SocketClient & client)139 void sendMessageToNanoapp(SocketClient& client) {
140 FlatBufferBuilder builder(64);
141 uint8_t messageData[] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11};
142 HostProtocolHost::encodeNanoappMessage(
143 builder, chre::kMessageWorldAppId, kHostEndpoint, 1234, messageData,
144 sizeof(messageData));
145
146 LOGI("Sending message to nanoapp (%u bytes w/%zu bytes of payload)",
147 builder.GetSize(), sizeof(messageData));
148 if (!client.sendMessage(builder.GetBufferPointer(), builder.GetSize())) {
149 LOGE("Failed to send message");
150 }
151 }
152
sendLoadNanoappRequest(SocketClient & client,const char * filename)153 void sendLoadNanoappRequest(SocketClient& client, const char *filename) {
154 std::ifstream file(filename, std::ios::binary | std::ios::ate);
155 if (!file) {
156 LOGE("Couldn't open file '%s': %s", filename, strerror(errno));
157 return;
158 }
159 ssize_t size = file.tellg();
160 file.seekg(0, std::ios::beg);
161
162 std::vector<uint8_t> buffer(size);
163 if (!file.read(reinterpret_cast<char *>(buffer.data()), size)) {
164 LOGE("Couldn't read from file: %s", strerror(errno));
165 return;
166 }
167
168 FlatBufferBuilder builder(size + 128);
169 HostProtocolHost::encodeLoadNanoappRequest(
170 builder, 1, 0x476f6f676c00100b, 0, 0x01000000, buffer);
171
172 LOGI("Sending load nanoapp request (%u bytes total w/%zu bytes of payload)",
173 builder.GetSize(), buffer.size());
174 if (!client.sendMessage(builder.GetBufferPointer(), builder.GetSize())) {
175 LOGE("Failed to send message");
176 }
177 }
178
179 } // anonymous namespace
180
main()181 int main() {
182 int ret = -1;
183 SocketClient client;
184 sp<SocketCallbacks> callbacks = new SocketCallbacks();
185
186 if (!client.connect("chre", callbacks)) {
187 LOGE("Couldn't connect to socket");
188 } else {
189 requestHubInfo(client);
190 requestNanoappList(client);
191 sendMessageToNanoapp(client);
192 sendLoadNanoappRequest(client, "/data/activity.so");
193
194 LOGI("Sleeping, waiting on responses");
195 std::this_thread::sleep_for(std::chrono::seconds(5));
196 }
197
198 return 0;
199 }
200