blob: 6538597bbac2a1cf4a8889825c4c10c5f3684744 [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"
8namespace open_power
9{
10namespace occ
11{
12
13namespace fs = std::experimental::filesystem;
14
15/** @class Device
16 * @brief Binds and unbinds the OCC driver upon request
17 */
18class Device
19{
20 public:
21 Device() = delete;
22 ~Device() = default;
23 Device(const Device&) = delete;
24 Device& operator=(const Device&) = delete;
25 Device(Device&&) = default;
26 Device& operator=(Device&&) = default;
27
28 /** @brief Constructs the Device object
29 *
Vishwanatha Subbannaee4d83d2017-06-29 18:35:00 +053030 * @param[in] event - Unique ptr reference to sd_event
31 * @param[in] name - OCC instance name
32 * @param[in] callback - Optional callback on errors
Vishwanatha Subbanna32e84e92017-06-28 19:17:28 +053033 */
Vishwanatha Subbannaee4d83d2017-06-29 18:35:00 +053034 Device(EventPtr& event,
35 const std::string& name,
36 std::function<void()> callBack = nullptr) :
37 config(name + '-' + "dev0"),
38 errorFile(fs::path(config) / "occ_error"),
39 error(event, errorFile, callBack)
Vishwanatha Subbanna32e84e92017-06-28 19:17:28 +053040 {
41 // Nothing to do here
42 }
43
44 /** @brief Binds device to the OCC driver */
45 inline void bind()
46 {
Vishwanatha Subbannaee4d83d2017-06-29 18:35:00 +053047 // Bind the device
Vishwanatha Subbanna32e84e92017-06-28 19:17:28 +053048 return write(bindPath, config);
49 }
50
51 /** @brief Un-binds device from the OCC driver */
52 inline void unBind()
53 {
Vishwanatha Subbannaee4d83d2017-06-29 18:35:00 +053054 // Unbind the device
55 return write(unBindPath, config);
56 }
57
58 /** @brief Starts to monitor for errors */
59 inline void addErrorWatch()
60 {
61 return error.addWatch();
62 }
63
64 /** @brief stops monitoring for errors */
65 inline void removeErrorWatch()
66 {
67 return error.removeWatch();
Vishwanatha Subbanna32e84e92017-06-28 19:17:28 +053068 }
69
70 private:
71 /** @brief Config value to be used to do bind and unbind */
72 const std::string config;
73
Vishwanatha Subbannaee4d83d2017-06-29 18:35:00 +053074 /** @brief This file contains 0 for success, non-zero for errors */
75 const fs::path errorFile;
76
Vishwanatha Subbanna32e84e92017-06-28 19:17:28 +053077 /** @brief To bind the device to the OCC driver, do:
Vishwanatha Subbannaee4d83d2017-06-29 18:35:00 +053078 *
Vishwanatha Subbanna32e84e92017-06-28 19:17:28 +053079 * Write occ<#>-dev0 to: /sys/bus/platform/drivers/occ-hwmon/bind
80 */
81 static fs::path bindPath;
82
83 /** @brief To un-bind the device from the OCC driver, do:
84 * Write occ<#>-dev0 to: /sys/bus/platform/drivers/occ-hwmon/unbind
85 */
86 static fs::path unBindPath;
87
Vishwanatha Subbannaee4d83d2017-06-29 18:35:00 +053088 /** Abstraction of error monitoring */
89 Error error;
90
Vishwanatha Subbanna32e84e92017-06-28 19:17:28 +053091 /** @brief file writer to achieve bind and unbind
92 *
93 * @param[in] filename - Name of file to be written
94 * @param[in] data - Data to be written to
95 * @return - None
96 */
97 void write(const fs::path& fileName, const std::string& data)
98 {
99 // If there is an error, move the exception all the way up
100 std::ofstream file(fileName, std::ios::out);
101 file << data;
102 file.close();
103 return;
104 }
105};
106
107} // namespace occ
108} // namespace open_power