1 /**
2  * Copyright © 2019 IBM Corporation
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 #pragma once
17 
18 #include "i2c_interface.hpp"
19 
20 #include <sdbusplus/bus.hpp>
21 
22 #include <filesystem>
23 #include <string>
24 
25 class TestUpdater;
26 
27 namespace updater
28 {
29 
30 namespace fs = std::filesystem;
31 
32 /**
33  * Update PSU firmware
34  *
35  * @param[in] psuInventoryPath - The inventory path of the PSU
36  * @param[in] imageDir - The directory containing the PSU image
37  *
38  * @return true if successful, otherwise false
39  */
40 bool update(const std::string& psuInventoryPath, const std::string& imageDir);
41 
42 class Updater
43 {
44   public:
45     friend TestUpdater;
46     Updater() = delete;
47     Updater(const Updater&) = delete;
48     Updater& operator=(const Updater&) = delete;
49     Updater(Updater&&) = default;
50     Updater& operator=(Updater&&) = default;
51 
52     /**
53      * @brief Constructor
54      *
55      * @param psuInventoryPath - The PSU inventory path
56      * @param devPath - The PSU device path
57      * @param imageDir - The update image directory
58      */
59     Updater(const std::string& psuInventoryPath, const std::string& devPath,
60             const std::string& imageDir);
61 
62     /** @brief Destructor */
63     ~Updater() = default;
64 
65     /** @brief Bind or unbind the driver
66      *
67      * @param doBind - indicate if it's going to bind or unbind the driver
68      */
69     void bindUnbind(bool doBind);
70 
71     /** @brief Set the PSU inventory present property
72      *
73      * @param present - The present state to set
74      */
75     void setPresent(bool present);
76 
77     /** @brief Check if it's ready to update the PSU
78      *
79      * @return true if it's ready, otherwise false
80      */
81     bool isReadyToUpdate();
82 
83     /** @brief Do the PSU update
84      *
85      * @return 0 if success, otherwise non-zero
86      */
87     int doUpdate();
88 
89     /** @brief Create I2C device
90      *
91      * Creates the I2C device based on the device name.
92      * e.g. It opens busId 3, address 0x68 for "3-0068"
93      */
94     void createI2CDevice();
95 
96   private:
97     /** @brief The sdbusplus DBus bus connection */
98     sdbusplus::bus::bus bus;
99 
100     /** @brief The PSU inventory path */
101     std::string psuInventoryPath;
102 
103     /** @brief The PSU device path
104      *
105      * Usually it is a device in i2c subsystem, e.g.
106      *   /sys/bus/i2c/devices/3-0068
107      */
108     std::string devPath;
109 
110     /** @brief The PSU device name
111      *
112      * Usually it is a i2c device name, e.g.
113      *   3-0068
114      */
115     std::string devName;
116 
117     /** @brief The PSU image directory */
118     std::string imageDir;
119 
120     /** @brief The PSU device driver's path
121      *
122      * Usually it is the PSU driver, e.g.
123      *   /sys/bus/i2c/drivers/ibm-cffps
124      */
125     fs::path driverPath;
126 
127     /** @brief The i2c device interface */
128     std::unique_ptr<i2c::I2CInterface> i2c;
129 };
130 
131 } // namespace updater
132