| /* |
| * Copyright (C) 2018 The Android Open Source Project |
| * |
| * Licensed under the Apache License, Version 2.0 (the "License"); |
| * you may not use this file except in compliance with the License. |
| * You may obtain a copy of the License at |
| * |
| * http://www.apache.org/licenses/LICENSE-2.0 |
| * |
| * Unless required by applicable law or agreed to in writing, software |
| * distributed under the License is distributed on an "AS IS" BASIS, |
| * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| * See the License for the specific language governing permissions and |
| * limitations under the License. |
| */ |
| |
| #include "chre/util/nanoapp/app_id.h" |
| #include "chre/util/system/napp_header_utils.h" |
| #include "chre_host/host_protocol_host.h" |
| #include "chre_host/log.h" |
| #include "chre_host/socket_client.h" |
| |
| #include <inttypes.h> |
| #include <sys/socket.h> |
| #include <sys/types.h> |
| |
| #include <fstream> |
| #include <thread> |
| |
| #include <cutils/sockets.h> |
| #include <utils/StrongPointer.h> |
| |
| /** |
| * @file |
| * A test utility that loads the audio stress test nanoapp and quits. |
| */ |
| |
| using android::sp; |
| using android::chre::FragmentedLoadTransaction; |
| using android::chre::getStringFromByteVector; |
| using android::chre::HostProtocolHost; |
| using android::chre::IChreMessageHandlers; |
| using android::chre::SocketClient; |
| using flatbuffers::FlatBufferBuilder; |
| |
| // Aliased for consistency with the way these symbols are referenced in |
| // CHRE-side code |
| namespace fbs = ::chre::fbs; |
| |
| namespace { |
| |
| class SocketCallbacks : public SocketClient::ICallbacks, |
| public IChreMessageHandlers { |
| public: |
| void onMessageReceived(const void *data, size_t length) override { |
| if (!HostProtocolHost::decodeMessageFromChre(data, length, *this)) { |
| LOGE("Failed to decode message"); |
| } |
| } |
| |
| void onConnected() override { |
| LOGI("Socket (re)connected"); |
| } |
| |
| void onConnectionAborted() override { |
| LOGI("Socket (re)connection aborted"); |
| } |
| |
| void onDisconnected() override { |
| LOGI("Socket disconnected"); |
| } |
| |
| void handleLoadNanoappResponse( |
| const fbs::LoadNanoappResponseT &response) override { |
| LOGI("Got load nanoapp response, transaction ID 0x%" PRIx32 " result %d", |
| response.transaction_id, response.success); |
| } |
| }; |
| |
| void sendLoadNanoappRequest(SocketClient &client, const char *filename, |
| uint64_t appId, uint32_t appVersion) { |
| std::ifstream file(filename, std::ios::binary | std::ios::ate); |
| if (!file) { |
| LOGE("Couldn't open file '%s': %s", filename, strerror(errno)); |
| return; |
| } |
| ssize_t size = file.tellg(); |
| file.seekg(0, std::ios::beg); |
| |
| std::vector<uint8_t> buffer(size); |
| if (!file.read(reinterpret_cast<char *>(buffer.data()), size)) { |
| LOGE("Couldn't read from file: %s", strerror(errno)); |
| return; |
| } |
| |
| // All loaded nanoapps must be signed currently. |
| uint32_t appFlags = CHRE_NAPP_HEADER_SIGNED; |
| |
| // Perform loading with 1 fragment for simplicity |
| FlatBufferBuilder builder(size + 128); |
| FragmentedLoadTransaction transaction = |
| FragmentedLoadTransaction(1 /* transactionId */, appId, appVersion, |
| appFlags, 0x01000000 /* targetApiVersion */, |
| buffer, buffer.size() /* fragmentSize */); |
| HostProtocolHost::encodeFragmentedLoadNanoappRequest( |
| builder, transaction.getNextRequest()); |
| |
| LOGI("Sending load nanoapp request (%" PRIu32 |
| " bytes total w/%zu bytes of payload)", |
| builder.GetSize(), buffer.size()); |
| if (!client.sendMessage(builder.GetBufferPointer(), builder.GetSize())) { |
| LOGE("Failed to send message"); |
| } |
| } |
| |
| } // anonymous namespace |
| |
| int main() { |
| SocketClient client; |
| sp<SocketCallbacks> callbacks = new SocketCallbacks(); |
| |
| if (!client.connect("chre", callbacks)) { |
| LOGE("Couldn't connect to socket"); |
| } else { |
| sendLoadNanoappRequest(client, "/data/audio_stress_test.so", |
| chre::kAudioStressTestAppId, 1 /* appVersion */); |
| } |
| |
| return 0; |
| } |