xref: /aosp_15_r20/system/nvram/hal/include/nvram/hal/tests/scoped_nvram_device.h (revision 7ba4dab5cc5e3c8f3eb594dcf3b33f99d9214aee)
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_TESTS_SCOPED_NVRAM_DEVICE_H_
18 #define NVRAM_HAL_TESTS_SCOPED_NVRAM_DEVICE_H_
19 
20 #include <string>
21 #include <vector>
22 
23 #include <android-base/logging.h>
24 #include <hardware/nvram.h>
25 
26 namespace nvram {
27 
28 using IndexList = std::vector<uint32_t>;
29 using ControlList = std::vector<nvram_control_t>;
30 
31 // Wraps an nvram_device_t. The device will be opened on construction and closed
32 // on destruction. Failure to open the device will cause all methods to return
33 // NV_RESULT_INTERNAL_ERROR.
34 // Note: Currently this resides in hal/tests because that's where it is used. It
35 //       may be useful elsewhere but should be moved somewhere more common
36 //       first.
37 class ScopedNvramDevice {
38  public:
39   ScopedNvramDevice();
40   virtual ~ScopedNvramDevice();
41 
42   // Convenience methods which trivially wrap the device functions.
43   virtual nvram_result_t GetTotalSizeInBytes(uint64_t* total_size);
44   virtual nvram_result_t GetAvailableSizeInBytes(uint64_t* available_size);
45   virtual nvram_result_t GetMaxSpaceSizeInBytes(uint64_t* max_space_size);
46   virtual nvram_result_t GetMaxSpaces(uint32_t* num_spaces);
47   virtual nvram_result_t GetSpaceList(std::vector<uint32_t>* space_index_list);
48   virtual nvram_result_t GetSpaceSize(uint32_t index, uint64_t* size);
49   virtual nvram_result_t GetSpaceControls(
50       uint32_t index,
51       std::vector<nvram_control_t>* control_list);
52   virtual nvram_result_t IsSpaceLocked(uint32_t index,
53                                        int* write_lock_enabled,
54                                        int* read_lock_enabled);
55   virtual nvram_result_t CreateSpace(
56       uint32_t index,
57       uint64_t size_in_bytes,
58       const std::vector<nvram_control_t>& control_list,
59       const std::string& authorization_value);
60   virtual nvram_result_t DeleteSpace(uint32_t index,
61                                      const std::string& authorization_value);
62   virtual nvram_result_t DisableCreate();
63   virtual nvram_result_t WriteSpace(uint32_t index,
64                                     const std::string& data,
65                                     const std::string& authorization_value);
66   virtual nvram_result_t ReadSpace(uint32_t index,
67                                    uint64_t num_bytes_to_read,
68                                    const std::string& authorization_value,
69                                    std::string* data);
70   virtual nvram_result_t EnableWriteLock(
71       uint32_t index,
72       const std::string& authorization_value);
73   virtual nvram_result_t EnableReadLock(uint32_t index,
74                                         const std::string& authorization_value);
75 
76  private:
77   nvram_device_t* device_ = nullptr;
78 };
79 
80 }  // namespace nvram
81 
82 #endif  // NVRAM_HAL_TESTS_SCOPED_NVRAM_DEVICE_H_
83