// Copyright 2015 The Chromium Authors. All rights reserved. // Use of this source code is governed by a BSD-style license that can be // found in the LICENSE file. #include "channel.h" #include #include #include #include #include "command.h" #include "common.h" namespace minibind { namespace { int ConnectDriver() { int fd = open("/dev/binder", O_RDONLY); if (fd < 0) { AERR("open driver"); abort(); } if (fcntl(fd, F_SETFD, FD_CLOEXEC)) { AERR("fcntl FD_CLOEXEC"); } struct binder_version version; int rv = ioctl(fd, BINDER_VERSION, &version); if (rv) { AERR("ioctl BINDER_VERSION"); abort(); } ALOG("Binder version: %d", version.protocol_version); if (version.protocol_version != BINDER_CURRENT_PROTOCOL_VERSION) { ALOG("Version is not compatible with current protocol: %d", BINDER_CURRENT_PROTOCOL_VERSION); abort(); } void* vm = mmap(0, BINDER_VM_SIZE, PROT_READ, MAP_PRIVATE | MAP_NORESERVE, fd, 0); if (vm == MAP_FAILED) { AERR("mmap BINDER_VM_SIZE"); abort(); } return fd; } void CopyCommandsToBuffer(const std::vector& commands, std::vector* buffer) { size_t last_command_size = 0; for (const Command* command : commands) { memcpy(&(*buffer)[last_command_size], reinterpret_cast(command->GetData()), command->GetSize()); last_command_size += command->GetSize(); } } } // namespace Channel::Channel(uint32_t handle) : handle_(handle), driver_(ConnectDriver()), write_commands_size_(0), write_commands_(), reader_() { reader_.SetDataSize(256); } Channel::~Channel() { } void Channel::QueueCommand(Command* cmd) { write_commands_size_ += cmd->GetSize(); write_commands_.push_back(cmd); } int Channel::TransactCommands() { struct binder_write_read bwr = {}; std::vector write_data(write_commands_size_, 0); CopyCommandsToBuffer(write_commands_, &write_data); bwr.write_size = write_commands_size_; bwr.write_buffer = reinterpret_cast(&write_data[0]); bwr.read_size = reader_.DataSize(); bwr.read_buffer = reader_.DataPointer(); int rv = ioctl(driver_, BINDER_WRITE_READ, &bwr); ALOG("BINDER_WRITE_READ %d", rv); return rv; } } // namespace minibind