aboutsummaryrefslogtreecommitdiff
path: root/hal/include/nvram/hal/nvram_device_adapter.h
blob: b99ab9c10006ac98f1e80bdd10b2cf52fa35d878 (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 (C) 2016 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.
 */

#ifndef NVRAM_HAL_NVRAM_DEVICE_ADAPTER_H_
#define NVRAM_HAL_NVRAM_DEVICE_ADAPTER_H_

#include <UniquePtr.h>

#include <hardware/nvram.h>
#include <nvram/messages/nvram_messages.h>

namespace nvram {

// |NvramImplementation| subclasses provide an implementation of the NVRAM HAL
// logic.
class NvramImplementation {
 public:
  virtual ~NvramImplementation() = default;

  // This function services all operations defined for the NVRAM HAL. The input
  // parameters are passed in |request| and |response| will be filled in with
  // the result and output parameters of the operation.
  virtual void Execute(const nvram::Request& request,
                       nvram::Response* response) = 0;
};

// |NvramDeviceAdapater| provides glue to turn an |NvramImplementation| object
// into an |nvram_device_t| as defined by the NVRAM HAL C API. This is intended
// to be used in the HAL module's |open()| operation. To obtain the desired
// |hw_device_t|, just create an |NvramDeviceAdapter| with a suitable
// |NvramImplementation| and call |as_device()| to get the HAL device pointer.
struct NvramDeviceAdapter {
 public:
  // Takes ownership of |implementation|.
  NvramDeviceAdapter(const hw_module_t* module,
                     NvramImplementation* implementation);

  hw_device_t* as_device() { return &device_.common; }
  NvramImplementation* nvram_implementation() { return implementation_.get(); }

 private:
  nvram_device_t device_;
  UniquePtr<NvramImplementation> implementation_;
};

// Make sure |NvramDeviceAdapter| is a standard layout type. This guarantees
// that casting from/to the type of the first non-static member (i.e. |device_|)
// works as expected.
static_assert(std::is_standard_layout<NvramDeviceAdapter>::value,
              "NvramDeviceAdapater must be a standard layout type.");

}  // namespace nvram

#endif  // NVRAM_HAL_NVRAM_DEVICE_ADAPTER_H_