1 // Copyright 2015 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
11 #include <linux/binder.h>
21 int fd
= open("/dev/binder", O_RDONLY
);
27 if (fcntl(fd
, F_SETFD
, FD_CLOEXEC
)) {
28 AERR("fcntl FD_CLOEXEC");
31 struct binder_version version
;
32 int rv
= ioctl(fd
, BINDER_VERSION
, &version
);
34 AERR("ioctl BINDER_VERSION");
37 ALOG("Binder version: %d", version
.protocol_version
);
38 if (version
.protocol_version
!= BINDER_CURRENT_PROTOCOL_VERSION
) {
39 ALOG("Version is not compatible with current protocol: %d", BINDER_CURRENT_PROTOCOL_VERSION
);
43 void* vm
= mmap(0, BINDER_VM_SIZE
, PROT_READ
, MAP_PRIVATE
| MAP_NORESERVE
, fd
, 0);
44 if (vm
== MAP_FAILED
) {
45 AERR("mmap BINDER_VM_SIZE");
52 void CopyCommandsToBuffer(const std
::vector
<Command
*>& commands
,
53 std
::vector
<unsigned char>* buffer
) {
54 size_t last_command_size
= 0;
55 for (const Command
* command
: commands
) {
56 memcpy(&(*buffer
)[last_command_size
], reinterpret_cast<void*>(command
->GetData()), command
->GetSize());
57 last_command_size
+= command
->GetSize();
63 Channel
::Channel(uint32_t handle
)
65 driver_(ConnectDriver()),
66 write_commands_size_(0),
69 reader_
.SetDataSize(256);
75 void Channel
::QueueCommand(Command
* cmd
) {
76 write_commands_size_
+= cmd
->GetSize();
77 write_commands_
.push_back(cmd
);
80 int Channel
::TransactCommands() {
81 struct binder_write_read bwr
= {};
83 std
::vector
<unsigned char> write_data(write_commands_size_
, 0);
84 CopyCommandsToBuffer(write_commands_
, &write_data
);
86 bwr
.write_size
= write_commands_size_
;
87 bwr
.write_buffer
= reinterpret_cast<binder_uintptr_t
>(&write_data
[0]);
88 bwr
.read_size
= reader_
.DataSize();
89 bwr
.read_buffer
= reader_
.DataPointer();
91 int rv
= ioctl(driver_
, BINDER_WRITE_READ
, &bwr
);
92 ALOG("BINDER_WRITE_READ %d", rv
);
96 } // namespace minibind