1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
|
#pragma once
#include <fstream>
#include <experimental/filesystem>
#include "occ_events.hpp"
#include "occ_errors.hpp"
#include "config.h"
namespace open_power
{
namespace occ
{
namespace fs = std::experimental::filesystem;
/** @class Device
* @brief Binds and unbinds the OCC driver upon request
*/
class Device
{
public:
Device() = delete;
~Device() = default;
Device(const Device&) = delete;
Device& operator=(const Device&) = delete;
Device(Device&&) = default;
Device& operator=(Device&&) = default;
/** @brief Constructs the Device object
*
* @param[in] event - Unique ptr reference to sd_event
* @param[in] name - OCC instance name
* @param[in] callback - Optional callback on errors
*/
Device(EventPtr& event,
const std::string& name,
std::function<void()> callBack = nullptr) :
#ifdef I2C_OCC
config(name),
#else
config(name + '-' + "dev0"),
#endif
errorFile(fs::path(config) / "occ_error"),
error(event, errorFile, callBack)
{
// Nothing to do here
}
/** @brief Binds device to the OCC driver */
inline void bind()
{
// Bind the device
return write(bindPath, config);
}
/** @brief Un-binds device from the OCC driver */
inline void unBind()
{
// Unbind the device
return write(unBindPath, config);
}
/** @brief Returns if device is already bound.
*
* On device bind, a soft link by the name $config
* gets created in OCC_HWMON_PATH and gets removed
* on unbind
*
* @return true if bound, else false
*/
inline bool bound() const
{
return fs::exists(OCC_HWMON_PATH + config);
}
/** @brief Starts to monitor for errors */
inline void addErrorWatch()
{
return error.addWatch();
}
/** @brief stops monitoring for errors */
inline void removeErrorWatch()
{
return error.removeWatch();
}
private:
/** @brief Config value to be used to do bind and unbind */
const std::string config;
/** @brief This file contains 0 for success, non-zero for errors */
const fs::path errorFile;
/** @brief To bind the device to the OCC driver, do:
*
* Write occ<#>-dev0 to: /sys/bus/platform/drivers/occ-hwmon/bind
*/
static fs::path bindPath;
/** @brief To un-bind the device from the OCC driver, do:
* Write occ<#>-dev0 to: /sys/bus/platform/drivers/occ-hwmon/unbind
*/
static fs::path unBindPath;
/** Abstraction of error monitoring */
Error error;
/** @brief file writer to achieve bind and unbind
*
* @param[in] filename - Name of file to be written
* @param[in] data - Data to be written to
* @return - None
*/
void write(const fs::path& fileName, const std::string& data)
{
// If there is an error, move the exception all the way up
std::ofstream file(fileName, std::ios::out);
file << data;
file.close();
return;
}
};
} // namespace occ
} // namespace open_power
|