1/*
2 * Copyright (C) 2016 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 *      http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#ifndef NVRAM_HAL_NVRAM_DEVICE_ADAPTER_H_
18#define NVRAM_HAL_NVRAM_DEVICE_ADAPTER_H_
19
20#include <memory>
21
22#include <hardware/nvram.h>
23#include <nvram/messages/nvram_messages.h>
24
25namespace nvram {
26
27// |NvramImplementation| subclasses provide an implementation of the NVRAM HAL
28// logic.
29class NvramImplementation {
30 public:
31  virtual ~NvramImplementation() = default;
32
33  // This function services all operations defined for the NVRAM HAL. The input
34  // parameters are passed in |request| and |response| will be filled in with
35  // the result and output parameters of the operation.
36  virtual void Execute(const nvram::Request& request,
37                       nvram::Response* response) = 0;
38};
39
40// |NvramDeviceAdapater| provides glue to turn an |NvramImplementation| object
41// into an |nvram_device_t| as defined by the NVRAM HAL C API. This is intended
42// to be used in the HAL module's |open()| operation. To obtain the desired
43// |hw_device_t|, just create an |NvramDeviceAdapter| with a suitable
44// |NvramImplementation| and call |as_device()| to get the HAL device pointer.
45struct NvramDeviceAdapter {
46 public:
47  // Takes ownership of |implementation|.
48  NvramDeviceAdapter(const hw_module_t* module,
49                     NvramImplementation* implementation);
50
51  hw_device_t* as_device() { return &device_.common; }
52  NvramImplementation* nvram_implementation() { return implementation_.get(); }
53
54 private:
55  nvram_device_t device_;
56  std::unique_ptr<NvramImplementation> implementation_;
57};
58
59// Make sure |NvramDeviceAdapter| is a standard layout type. This guarantees
60// that casting from/to the type of the first non-static member (i.e. |device_|)
61// works as expected.
62static_assert(std::is_standard_layout<NvramDeviceAdapter>::value,
63              "NvramDeviceAdapater must be a standard layout type.");
64
65}  // namespace nvram
66
67#endif  // NVRAM_HAL_NVRAM_DEVICE_ADAPTER_H_
68