~ubuntu-branches/ubuntu/trusty/thermald/trusty-updates

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
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
/*
 * thd_sys_fs.cpp: sysfs class implementation
 *
 * Copyright (C) 2012 Intel Corporation. All rights reserved.
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License version
 * 2 or later as published by the Free Software Foundation.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
 * 02110-1301, USA.
 *
 *
 * Author Name <Srinivas.Pandruvada@linux.intel.com>
 *
 */

#include "thd_sys_fs.h"
#include "thd_common.h"
#include <stdlib.h>

int csys_fs::write(const std::string &path, const std::string &buf) {
	std::string p = base_path + path;
	int fd = ::open(p.c_str(), O_WRONLY);
	if (fd < 0) {
		thd_log_warn("sysfs write failed %s\n", path.c_str());
		return -errno;
	}
	int ret = ::write(fd, buf.c_str(), buf.size());
	if (ret < 0)
		thd_log_warn("sysfs write failed %s\n", path.c_str());
	close(fd);

	return ret;
}

int csys_fs::write(const std::string &path, unsigned int position, unsigned
long long data) {
	std::string p = base_path + path;
	int fd = ::open(p.c_str(), O_WRONLY);
	if (fd < 0) {
		thd_log_warn("sysfs write failed %s\n", path.c_str());
		return -errno;
	}
	if (::lseek(fd, position, SEEK_CUR) == -1) {
		thd_log_warn("sysfs write failed %s\n", path.c_str());
		return -errno;
	}
	int ret = ::write(fd, &data, sizeof(data));
	if (ret < 0)
		thd_log_warn("sysfs write failed %s\n", path.c_str());
	close(fd);

	return ret;
}

int csys_fs::write(const std::string &path, unsigned int data) {
	std::ostringstream os;
	os << data;
	return csys_fs::write(path, os.str());
}

int csys_fs::read(const std::string &path, char *buf, int len) {
	std::string p = base_path + path;
	int fd = ::open(p.c_str(), O_RDONLY);
	if (fd < 0) {
		thd_log_warn("sysfs read failed %s\n", path.c_str());
		return -errno;
	}
	int ret = ::read(fd, buf, len);
	if (ret < 0)
		thd_log_warn("sysfs read failed %s\n", path.c_str());
	close(fd);

	return ret;
}

int csys_fs::read(const std::string &path, unsigned int position, char *buf,
		int len) {
	std::string p = base_path + path;
	int fd = ::open(p.c_str(), O_RDONLY);
	if (fd < 0) {
		thd_log_warn("sysfs read failed %s\n", path.c_str());
		return -errno;
	}
	if (::lseek(fd, position, SEEK_CUR) == -1) {
		thd_log_warn("sysfs read failed %s\n", path.c_str());
		return -errno;
	}
	int ret = ::read(fd, buf, len);
	if (ret < 0)
		thd_log_warn("sysfs read failed %s\n", path.c_str());
	close(fd);

	return ret;
}

int csys_fs::read(const std::string &path, unsigned int *ptr_val) {
	std::string p = base_path + path;
	char str[16];
	int ret;

	int fd = ::open(p.c_str(), O_RDONLY);
	if (fd < 0) {
		thd_log_warn("sysfs read failed %s\n", path.c_str());
		return -errno;
	}
	ret = ::read(fd, str, sizeof(str));
	if (ret > 0)
		*ptr_val = atoi(str);
	else
		thd_log_warn("sysfs read failed %s\n", path.c_str());
	close(fd);

	return ret;
}

int csys_fs::read(const std::string &path, unsigned long *ptr_val) {
	std::string p = base_path + path;
	char str[32];
	int ret;

	int fd = ::open(p.c_str(), O_RDONLY);
	if (fd < 0) {
		thd_log_warn("sysfs read failed %s\n", path.c_str());
		return -errno;
	}
	ret = ::read(fd, str, sizeof(str));
	if (ret > 0)
		*ptr_val = atol(str);
	else
		thd_log_warn("sysfs read failed %s\n", path.c_str());
	close(fd);

	return ret;
}

int csys_fs::read(const std::string &path, std::string &buf) {
	std::string p = base_path + path;

	std::ifstream f(p.c_str(), std::fstream::in);
	if (f.fail()) {
		thd_log_warn("sysfs read failed %s\n", path.c_str());
		return -EINVAL;
	}
	int ret = 0;
	f >> buf;
	if (f.bad()) {
		thd_log_warn("sysfs read failed %s\n", path.c_str());
		ret = -EIO;
	}
	f.close();

	return ret;
}

bool csys_fs::exists(const std::string &path) {
	struct stat s;

	return (bool) (stat((base_path + path).c_str(), &s) == 0);
}

bool csys_fs::exists() {
	return csys_fs::exists("");
}

int csys_fs::read_symbolic_link_value(const std::string &path, char *buf,
		int len) {
	std::string p = base_path + path;
	int ret = ::readlink(p.c_str(), buf, len);
	if (ret < 0) {
		thd_log_warn("read_symbolic_link %s\n", path.c_str());
		return -errno;
	}
	buf[ret] = '\0';
	return 0;
}