blob: dc057be4a52a6e280e18f2032eab08621e4635a2 [file] [log] [blame]
Vishwanatha Subbanna32e84e92017-06-28 19:17:28 +05301#pragma once
2
3#include <fstream>
4#include <experimental/filesystem>
5#include "config.h"
6namespace open_power
7{
8namespace occ
9{
10
11namespace fs = std::experimental::filesystem;
12
13/** @class Device
14 * @brief Binds and unbinds the OCC driver upon request
15 */
16class Device
17{
18 public:
19 Device() = delete;
20 ~Device() = default;
21 Device(const Device&) = delete;
22 Device& operator=(const Device&) = delete;
23 Device(Device&&) = default;
24 Device& operator=(Device&&) = default;
25
26 /** @brief Constructs the Device object
27 *
28 * @param[in] name - OCC instance name
29 */
30 Device(const std::string& name) :
31 config(name + '-' + "dev0")
32 {
33 // Nothing to do here
34 }
35
36 /** @brief Binds device to the OCC driver */
37 inline void bind()
38 {
39 return write(bindPath, config);
40 }
41
42 /** @brief Un-binds device from the OCC driver */
43 inline void unBind()
44 {
45 return write(unBindPath, config);
46 }
47
48 private:
49 /** @brief Config value to be used to do bind and unbind */
50 const std::string config;
51
52 /** @brief To bind the device to the OCC driver, do:
53 * Write occ<#>-dev0 to: /sys/bus/platform/drivers/occ-hwmon/bind
54 */
55 static fs::path bindPath;
56
57 /** @brief To un-bind the device from the OCC driver, do:
58 * Write occ<#>-dev0 to: /sys/bus/platform/drivers/occ-hwmon/unbind
59 */
60 static fs::path unBindPath;
61
62 /** @brief file writer to achieve bind and unbind
63 *
64 * @param[in] filename - Name of file to be written
65 * @param[in] data - Data to be written to
66 * @return - None
67 */
68 void write(const fs::path& fileName, const std::string& data)
69 {
70 // If there is an error, move the exception all the way up
71 std::ofstream file(fileName, std::ios::out);
72 file << data;
73 file.close();
74 return;
75 }
76};
77
78} // namespace occ
79} // namespace open_power