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/unique_fd.h>
10#include <zircon/device/nand.h>
11
12// The nand device that will be used as the parent of the broker device. This
13// can be a ram-nand device instantiated for the test, or any nand device
14// already on the system.
15class ParentDevice {
16  public:
17    struct TestConfig {
18        nand_info_t info;       // Configuration for a new ram-nand.
19        const char* path;       // Path to an existing device.
20        bool is_broker;         // True is the device is a broker (not a nand).
21        uint32_t num_blocks;    // Number of blocks to use.
22        uint32_t first_block;   // First block to use.
23    };
24
25    explicit ParentDevice(const TestConfig& config);
26    ~ParentDevice();
27
28    const char* Path() const { return path_; }
29
30    bool IsValid() const { return ram_nand_ || device_; }
31    bool IsExternal() const { return device_ ?  true : false; }
32    bool IsBroker() const { return config_.is_broker; }
33
34    // Returns a file descriptor for the device.
35    int get() const { return ram_nand_ ? ram_nand_.get() : device_.get(); }
36
37    const nand_info_t& Info() const { return config_.info; }
38    void SetInfo(const nand_info_t& info);
39
40    uint32_t NumBlocks() const { return config_.num_blocks; }
41    uint32_t FirstBlock() const { return config_.first_block; }
42
43  private:
44    fbl::unique_fd ram_nand_;
45    fbl::unique_fd device_;
46    TestConfig config_;
47    char path_[PATH_MAX];
48};
49
50extern ParentDevice* g_parent_device_;
51