2
* thd_sys_fs.cpp: sysfs class implementation
4
* Copyright (C) 2012 Intel Corporation. All rights reserved.
6
* This program is free software; you can redistribute it and/or
7
* modify it under the terms of the GNU General Public License version
8
* 2 or later as published by the Free Software Foundation.
10
* This program is distributed in the hope that it will be useful,
11
* but WITHOUT ANY WARRANTY; without even the implied warranty of
12
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13
* GNU General Public License for more details.
15
* You should have received a copy of the GNU General Public License
16
* along with this program; if not, write to the Free Software
17
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
21
* Author Name <Srinivas.Pandruvada@linux.intel.com>
25
#include "thd_sys_fs.h"
26
#include "thd_common.h"
29
int csys_fs::write(const std::string &path, const std::string &buf) {
30
std::string p = base_path + path;
31
int fd = ::open(p.c_str(), O_WRONLY);
33
thd_log_warn("sysfs write failed %s\n", path.c_str());
36
int ret = ::write(fd, buf.c_str(), buf.size());
38
thd_log_warn("sysfs write failed %s\n", path.c_str());
44
int csys_fs::write(const std::string &path, unsigned int position, unsigned
46
std::string p = base_path + path;
47
int fd = ::open(p.c_str(), O_WRONLY);
49
thd_log_warn("sysfs write failed %s\n", path.c_str());
52
if (::lseek(fd, position, SEEK_CUR) == -1) {
53
thd_log_warn("sysfs write failed %s\n", path.c_str());
56
int ret = ::write(fd, &data, sizeof(data));
58
thd_log_warn("sysfs write failed %s\n", path.c_str());
64
int csys_fs::write(const std::string &path, unsigned int data) {
65
std::ostringstream os;
67
return csys_fs::write(path, os.str());
70
int csys_fs::read(const std::string &path, char *buf, int len) {
71
std::string p = base_path + path;
72
int fd = ::open(p.c_str(), O_RDONLY);
74
thd_log_warn("sysfs read failed %s\n", path.c_str());
77
int ret = ::read(fd, buf, len);
79
thd_log_warn("sysfs read failed %s\n", path.c_str());
85
int csys_fs::read(const std::string &path, unsigned int position, char *buf,
87
std::string p = base_path + path;
88
int fd = ::open(p.c_str(), O_RDONLY);
90
thd_log_warn("sysfs read failed %s\n", path.c_str());
93
if (::lseek(fd, position, SEEK_CUR) == -1) {
94
thd_log_warn("sysfs read failed %s\n", path.c_str());
97
int ret = ::read(fd, buf, len);
99
thd_log_warn("sysfs read failed %s\n", path.c_str());
105
int csys_fs::read(const std::string &path, unsigned int *ptr_val) {
106
std::string p = base_path + path;
110
int fd = ::open(p.c_str(), O_RDONLY);
112
thd_log_warn("sysfs read failed %s\n", path.c_str());
115
ret = ::read(fd, str, sizeof(str));
117
*ptr_val = atoi(str);
119
thd_log_warn("sysfs read failed %s\n", path.c_str());
125
int csys_fs::read(const std::string &path, unsigned long *ptr_val) {
126
std::string p = base_path + path;
130
int fd = ::open(p.c_str(), O_RDONLY);
132
thd_log_warn("sysfs read failed %s\n", path.c_str());
135
ret = ::read(fd, str, sizeof(str));
137
*ptr_val = atol(str);
139
thd_log_warn("sysfs read failed %s\n", path.c_str());
145
int csys_fs::read(const std::string &path, std::string &buf) {
146
std::string p = base_path + path;
148
std::ifstream f(p.c_str(), std::fstream::in);
150
thd_log_warn("sysfs read failed %s\n", path.c_str());
156
thd_log_warn("sysfs read failed %s\n", path.c_str());
164
bool csys_fs::exists(const std::string &path) {
167
return (bool) (stat((base_path + path).c_str(), &s) == 0);
170
bool csys_fs::exists() {
171
return csys_fs::exists("");
174
int csys_fs::read_symbolic_link_value(const std::string &path, char *buf,
176
std::string p = base_path + path;
177
int ret = ::readlink(p.c_str(), buf, len);
179
thd_log_warn("read_symbolic_link %s\n", path.c_str());