blob: a2b4e8534ded64c58fa9c883d475bb61f52f144b [file] [log] [blame]
Vishwanatha Subbanna32e84e92017-06-28 19:17:28 +05301#pragma once
2
3#include <fstream>
4#include <experimental/filesystem>
Vishwanatha Subbannaee4d83d2017-06-29 18:35:00 +05305#include "occ_events.hpp"
6#include "occ_errors.hpp"
Vishwanatha Subbanna32e84e92017-06-28 19:17:28 +05307#include "config.h"
Lei YU0ab90ca2017-07-13 17:02:23 +08008
9
Vishwanatha Subbanna32e84e92017-06-28 19:17:28 +053010namespace open_power
11{
12namespace occ
13{
14
15namespace fs = std::experimental::filesystem;
16
17/** @class Device
18 * @brief Binds and unbinds the OCC driver upon request
19 */
20class Device
21{
22 public:
23 Device() = delete;
24 ~Device() = default;
25 Device(const Device&) = delete;
26 Device& operator=(const Device&) = delete;
27 Device(Device&&) = default;
28 Device& operator=(Device&&) = default;
29
30 /** @brief Constructs the Device object
31 *
Vishwanatha Subbannaee4d83d2017-06-29 18:35:00 +053032 * @param[in] event - Unique ptr reference to sd_event
33 * @param[in] name - OCC instance name
34 * @param[in] callback - Optional callback on errors
Vishwanatha Subbanna32e84e92017-06-28 19:17:28 +053035 */
Vishwanatha Subbannaee4d83d2017-06-29 18:35:00 +053036 Device(EventPtr& event,
37 const std::string& name,
38 std::function<void()> callBack = nullptr) :
Lei YU0ab90ca2017-07-13 17:02:23 +080039#ifdef I2C_OCC
40 config(name),
41#else
Vishwanatha Subbannaee4d83d2017-06-29 18:35:00 +053042 config(name + '-' + "dev0"),
Lei YU0ab90ca2017-07-13 17:02:23 +080043#endif
Vishwanatha Subbannaee4d83d2017-06-29 18:35:00 +053044 errorFile(fs::path(config) / "occ_error"),
45 error(event, errorFile, callBack)
Vishwanatha Subbanna32e84e92017-06-28 19:17:28 +053046 {
47 // Nothing to do here
48 }
49
50 /** @brief Binds device to the OCC driver */
51 inline void bind()
52 {
Vishwanatha Subbannaee4d83d2017-06-29 18:35:00 +053053 // Bind the device
Vishwanatha Subbanna32e84e92017-06-28 19:17:28 +053054 return write(bindPath, config);
55 }
56
57 /** @brief Un-binds device from the OCC driver */
58 inline void unBind()
59 {
Vishwanatha Subbannaee4d83d2017-06-29 18:35:00 +053060 // Unbind the device
61 return write(unBindPath, config);
62 }
63
64 /** @brief Starts to monitor for errors */
65 inline void addErrorWatch()
66 {
67 return error.addWatch();
68 }
69
70 /** @brief stops monitoring for errors */
71 inline void removeErrorWatch()
72 {
73 return error.removeWatch();
Vishwanatha Subbanna32e84e92017-06-28 19:17:28 +053074 }
75
76 private:
77 /** @brief Config value to be used to do bind and unbind */
78 const std::string config;
79
Vishwanatha Subbannaee4d83d2017-06-29 18:35:00 +053080 /** @brief This file contains 0 for success, non-zero for errors */
81 const fs::path errorFile;
82
Vishwanatha Subbanna32e84e92017-06-28 19:17:28 +053083 /** @brief To bind the device to the OCC driver, do:
Vishwanatha Subbannaee4d83d2017-06-29 18:35:00 +053084 *
Vishwanatha Subbanna32e84e92017-06-28 19:17:28 +053085 * Write occ<#>-dev0 to: /sys/bus/platform/drivers/occ-hwmon/bind
86 */
87 static fs::path bindPath;
88
89 /** @brief To un-bind the device from the OCC driver, do:
90 * Write occ<#>-dev0 to: /sys/bus/platform/drivers/occ-hwmon/unbind
91 */
92 static fs::path unBindPath;
93
Vishwanatha Subbannaee4d83d2017-06-29 18:35:00 +053094 /** Abstraction of error monitoring */
95 Error error;
96
Vishwanatha Subbanna32e84e92017-06-28 19:17:28 +053097 /** @brief file writer to achieve bind and unbind
98 *
99 * @param[in] filename - Name of file to be written
100 * @param[in] data - Data to be written to
101 * @return - None
102 */
103 void write(const fs::path& fileName, const std::string& data)
104 {
105 // If there is an error, move the exception all the way up
106 std::ofstream file(fileName, std::ios::out);
107 file << data;
108 file.close();
109 return;
110 }
111};
112
113} // namespace occ
114} // namespace open_power