summaryrefslogtreecommitdiffstats
path: root/chromeos/binder/driver.h
blob: 0940f4eb5a411415d6572d783b8db910d1550416 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
// 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.

#ifndef CHROMEOS_BINDER_DRIVER_H_
#define CHROMEOS_BINDER_DRIVER_H_

#include <stddef.h>

#include "base/files/scoped_file.h"
#include "base/macros.h"
#include "chromeos/chromeos_export.h"

namespace binder {

// Use this class to communicate with the binder driver provided by the kernel.
// This class is stateless and it's safe to access this class from multiple
// threads.
class CHROMEOS_EXPORT Driver {
 public:
  Driver();
  ~Driver();

  // Opens the binder driver, performs necessary initialization, and returns
  // true on success.
  // This method must be called before calling any other method.
  bool Initialize();

  // Returns the file descriptor of the binder driver.
  int GetFD();

  // Sets the maximum number of additional service threads.
  // By default the number is set to 0.
  bool SetMaxThreads(int max_threads);

  // Writes and reads data to/from the driver.
  // The data in the buffer specified by write_buf and write_buf_size will be
  // written, and the number of bytes written will be stored in
  // num_written_bytes. The read data will be stored in the buffer specified by
  // read_buf and read_buf_size, and the number of bytes read will be stored in
  // num_read_bytes.
  bool WriteRead(const char* write_buf,
                 size_t write_buf_size,
                 char* read_buf,
                 size_t read_buf_size,
                 size_t* num_written_bytes,
                 size_t* num_read_bytes);

  // Waits for some data to be available for reading.
  bool Poll();

  // Lets the driver know the current thread is exiting.
  bool NotifyCurrentThreadExiting();

 private:
  // Returns the size of the mmap region for transaction data.
  size_t GetBinderMmapSize() const;

  base::ScopedFD fd_;
  void* mmap_address_;

  DISALLOW_COPY_AND_ASSIGN(Driver);
};

}  // namespace binder

#endif  // CHROMEOS_BINDER_DRIVER_H_