1 // Copyright 2018 The Fuchsia 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.
4 
5 #pragma once
6 
7 #include <limits.h>
8 
9 #include <fbl/string_buffer.h>
10 #include <fbl/unique_fd.h>
11 #include <fbl/unique_ptr.h>
12 #include <fs-management/ram-nand.h>
13 #include <fuchsia/hardware/nand/c/fidl.h>
14 
15 // The nand device that will be used as the parent of the broker device. This
16 // can be a ram-nand device instantiated for the test, or any nand device
17 // already on the system.
18 class ParentDevice {
19   public:
20     struct TestConfig {
21         fuchsia_hardware_nand_Info info;                  // Configuration for a new ram-nand.
22         fuchsia_hardware_nand_PartitionMap partition_map; // Configuration for a new ram-nand.
23         const char* path;       // Path to an existing device.
24         bool is_broker;         // True is the device is a broker (not a nand).
25         uint32_t num_blocks;    // Number of blocks to use.
26         uint32_t first_block;   // First block to use.
27     };
28 
29     explicit ParentDevice(const TestConfig& config);
30     ~ParentDevice() = default;
31 
Path()32     const char* Path() const { return path_.c_str(); }
33 
IsValid()34     bool IsValid() const { return ram_nand_ || device_; }
IsExternal()35     bool IsExternal() const { return device_ ?  true : false; }
IsBroker()36     bool IsBroker() const { return config_.is_broker; }
37 
38     // Returns a file descriptor for the device.
get()39     int get() { return ram_nand_ ? ram_nand_->fd().get() : device_.get(); }
40 
Info()41     const fuchsia_hardware_nand_Info& Info() const { return config_.info; }
42     void SetInfo(const fuchsia_hardware_nand_Info& info);
43 
PartitionMap()44     const fuchsia_hardware_nand_PartitionMap& PartitionMap() const { return config_.partition_map; }
45     void SetPartitionMap(const fuchsia_hardware_nand_PartitionMap& partition_map);
46 
NumBlocks()47     uint32_t NumBlocks() const { return config_.num_blocks; }
FirstBlock()48     uint32_t FirstBlock() const { return config_.first_block; }
49 
50   private:
51     std::optional<fs_mgmt::RamNand> ram_nand_;
52     fbl::unique_fd device_;
53     TestConfig config_;
54     fbl::StringBuffer<PATH_MAX> path_;
55 };
56 
57 extern ParentDevice* g_parent_device_;
58