From 5c9ac0f1b37857827bf64d87b4db7879ee9635ac Mon Sep 17 00:00:00 2001 From: Robert Sesek Date: Wed, 21 Jan 2015 23:52:59 -0500 Subject: [PATCH] Begin processing read commands. --- jni/minibind/channel.cc | 72 ++++++++++++++++++++++++++++++++++++++++ jni/minibind/channel.h | 2 ++ jni/minibind/command.cc | 3 +- jni/minibind/common.h | 4 ++- jni/minibind/minibind.cc | 12 ++----- jni/minibind/parcel.cc | 4 +++ jni/minibind/parcel.h | 2 ++ run_tests.sh | 5 +-- 8 files changed, 90 insertions(+), 14 deletions(-) diff --git a/jni/minibind/channel.cc b/jni/minibind/channel.cc index 717cbb8..ab9b4c2 100644 --- a/jni/minibind/channel.cc +++ b/jni/minibind/channel.cc @@ -107,7 +107,79 @@ int Channel::Call(uint32_t code, const Parcel& in, Parcel* out) { int rv = ioctl(driver_, BINDER_WRITE_READ, &bwr); ALOG("BINDER_WRITE_READ %d (written %d/%d, read %d/%d)", rv, bwr.write_consumed, bwr.write_size, bwr.read_consumed, bwr.read_size); + if (rv != 0) + return -errno; + + for (binder_size_t index = 0; + index < bwr.read_consumed; + index = reader_.DataPosition()) { + size_t remaining = bwr.read_consumed - index; + if (remaining < sizeof(uint32_t)) + break; + + uint32_t command = 0; + if (!reader_.ReadUInt32(&command)) + break; + + if (!ProcessCommand(command, remaining)) + break; + } + return rv; } +bool Channel::ProcessCommand(uint32_t command, size_t data_remaining) { + ALOG("ProcessCommand: 0x%x", command); + switch (command) { + case BR_ERROR: { + if (data_remaining < sizeof(int)) + return false; + + uint32_t error_code = 0; + if (!reader_.ReadUInt32(&error_code)) + return false; + + ALOG("BR_ERROR: %d", error_code); + break; + } + case BR_OK: + ALOG("BR_OK"); + break; + case BR_TRANSACTION: + break; + case BR_REPLY: + break; + case BR_ACQUIRE_RESULT: + break; + case BR_DEAD_REPLY: + break; + case BR_TRANSACTION_COMPLETE: + break; + case BR_INCREFS: + break; + case BR_ACQUIRE: + break; + case BR_RELEASE: + break; + case BR_DECREFS: + break; + case BR_ATTEMPT_ACQUIRE: + break; + case BR_NOOP: + ALOG("BR_NOOP"); + break; + case BR_SPAWN_LOOPER: + break; + case BR_FINISHED: + break; + case BR_DEAD_BINDER: + break; + case BR_CLEAR_DEATH_NOTIFICATION_DONE: + break; + case BR_FAILED_REPLY: + ALOG("BR_FAILED_REPLY"); + break; + }; +} + } // namespace minibind diff --git a/jni/minibind/channel.h b/jni/minibind/channel.h index edfa500..1f8a7df 100644 --- a/jni/minibind/channel.h +++ b/jni/minibind/channel.h @@ -34,6 +34,8 @@ class Channel { uint32_t handle() const { return handle_; } private: + bool ProcessCommand(uint32_t command, size_t data_remaining); + uint32_t handle_; int driver_; diff --git a/jni/minibind/command.cc b/jni/minibind/command.cc index d6f060d..2465ecf 100644 --- a/jni/minibind/command.cc +++ b/jni/minibind/command.cc @@ -10,7 +10,8 @@ namespace minibind { TransactionCommand::TransactionCommand(TransactionCommand::Type type) - : command_(type == TWO_WAY ? BC_TRANSACTION : BC_REPLY) { + : command_(type == TWO_WAY ? BC_TRANSACTION : BC_REPLY), + transaction_() { } TransactionCommand::~TransactionCommand() {} diff --git a/jni/minibind/common.h b/jni/minibind/common.h index 2384af9..f9002f8 100644 --- a/jni/minibind/common.h +++ b/jni/minibind/common.h @@ -6,8 +6,10 @@ #include #include +#include -#define ALOG(...) __android_log_print(ANDROID_LOG_WARN, "minibind", __VA_ARGS__) +//#define ALOG(...) __android_log_print(ANDROID_LOG_WARN, "minibind", __VA_ARGS__) +#define ALOG(...) printf(__VA_ARGS__); putc('\n', stdout) #define AERR(msg) ALOG(msg ": %s", strerror(errno)) diff --git a/jni/minibind/minibind.cc b/jni/minibind/minibind.cc index d39b3d8..fdc23a9 100644 --- a/jni/minibind/minibind.cc +++ b/jni/minibind/minibind.cc @@ -20,7 +20,7 @@ Channel* GetServiceManager() { Parcel in, out; service_manager->Call(Channel::PING_TRANSACTION, in, &out); - service_manager->reader()->Print(); + //service_manager->reader()->Print(); } return service_manager; @@ -33,18 +33,10 @@ Channel* LookupService(const std::string& name) { data.WriteInterfaceToken("android.os.IServiceManager"); data.WriteUTF8(name); + Channel* channel = GetServiceManager(); Parcel reply; - Channel* channel = GetServiceManager(); channel->Call(2 /*CHECK_SERVICE_TRANSACTION*/, data, &reply); - - channel->reader()->Print(); - - uint32_t cmd = 0; - CHECK(channel->reader()->ReadUInt32(&cmd)); - ALOG("Got command: %x (dec %d %u)", cmd, cmd, cmd); - CHECK(channel->reader()->ReadUInt32(&cmd)); - ALOG("Got command: %x (dec %d %u)", cmd, cmd, cmd); } } // namespace minibind diff --git a/jni/minibind/parcel.cc b/jni/minibind/parcel.cc index 9e2367a..89f72dc 100644 --- a/jni/minibind/parcel.cc +++ b/jni/minibind/parcel.cc @@ -61,6 +61,10 @@ void Parcel::SetDataPosition(size_t pos) { data_position_ = pos; } +size_t Parcel::DataPosition() const { + return data_position_; +} + void Parcel::Print() const { for (size_t i = 0; i < data_.size(); i += 4) { ALOG("%0x %0x %0x %0x", data_[i], data_[i+1], data_[i+2], data_[i+3]); diff --git a/jni/minibind/parcel.h b/jni/minibind/parcel.h index ba017f2..5c2af75 100644 --- a/jni/minibind/parcel.h +++ b/jni/minibind/parcel.h @@ -28,9 +28,11 @@ class Parcel { bool ReadBytes(size_t count, void* buffer); binder_uintptr_t DataPointer() const; + size_t DataSize() const; void SetDataSize(size_t size); + size_t DataPosition() const; void SetDataPosition(size_t pos); void Print() const; diff --git a/run_tests.sh b/run_tests.sh index feee2b3..e0ad7db 100755 --- a/run_tests.sh +++ b/run_tests.sh @@ -1,5 +1,6 @@ -set -e +set -ex +ndk-build adb push ./libs/armeabi/minibind_test /data/local/tmp adb shell /data/local/tmp/minibind_test -adb logcat -s -d minibind:V +#adb logcat -s -d minibind:V -- 2.43.5