Repository navigation
Expand file tree
/
Copy pathSysfs.java
More file actions
152 lines (141 loc) · 4.39 KB
/
Copy pathSysfs.java
File metadata and controls
152 lines (141 loc) · 4.39 KB
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
package ev3dev.utils;
import java.io.File;
import java.io.IOException;
import java.io.PrintWriter;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.nio.file.StandardOpenOption;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import lombok.extern.slf4j.Slf4j;
/**
* The class responsible to interact with Sysfs on EV3Dev
*
* @author Juan Antonio Breña Moral
* @author David Walend
*
*/
@Slf4j
public class Sysfs {
/**
* Write a value in a file.
*
* @param filePath File path
* @param value value to write
* @return A boolean value if the operation was written or not.
*/
public static boolean writeString(final String filePath, final String value) {
if (LOGGER.isTraceEnabled()) {
LOGGER.trace("echo " + value + " > " + filePath);
}
try {
final File file = new File(filePath);
if (file.canWrite()) {
//TODO Review if it possible to improve
PrintWriter out = new PrintWriter(file);
out.println(value);
out.flush();
out.close();
//TODO Review
} else {
LOGGER.error("File: '{}' without write permissions.", filePath);
return false;
}
} catch (IOException e) {
LOGGER.error(e.getLocalizedMessage(), e);
return false;
}
return true;
}
public static boolean writeInteger(final String filePath, final int value) {
return writeString(filePath, new StringBuilder().append(value).toString());
}
/**
* Read an Attribute in the Sysfs with containing String values
*
* @param filePath path
* @return value from attribute
*/
public static String readString(final String filePath) {
if (LOGGER.isTraceEnabled()) {
LOGGER.trace("cat " + filePath);
}
try {
try (DataChannelRereader rereader = new DataChannelRereader(filePath)) {
String result = rereader.readString();
if (LOGGER.isTraceEnabled()) {
LOGGER.trace("value: {}", result);
}
return result;
}
} catch (IOException e) {
LOGGER.error(e.getLocalizedMessage(), e);
throw new RuntimeException("Problem reading path: " + filePath, e);
}
}
/**
* Read an Attribute in the Sysfs with containing Integer values
*
* @param filePath path
* @return value from attribute
*/
public static int readInteger(final String filePath) {
return Integer.parseInt(readString(filePath));
}
/**
* Read an Attribute in the Sysfs with containing Float values
*
* @param filePath path
* @return value from attribute
*/
public static float readFloat(final String filePath) {
return Float.parseFloat(readString(filePath));
}
/**
* Retrieve the elements contained in a path
*
* @param filePath path
* @return an List with options from a path
*/
public static List<File> getElements(final String filePath) {
final File f = new File(filePath);
if (existPath(filePath) && (f.listFiles().length > 0)) {
return new ArrayList<>(Arrays.asList(f.listFiles()));
} else {
throw new RuntimeException("The path doesn't exist: " + filePath);
}
}
/**
* This method is used to detect folders in /sys/class/
*
* @param filePath path
* @return boolean
*/
public static boolean existPath(final String filePath) {
if (LOGGER.isTraceEnabled()) {
LOGGER.trace("ls " + filePath);
}
final File f = new File(filePath);
return f.exists() && f.isDirectory();
}
public static boolean existFile(Path pathToFind) {
return Files.exists(pathToFind);
}
/**
* Method to write bytes in a path
*
* @param path path
* @param value value to write
* @return Result
*/
public static boolean writeBytes(final String path, final byte[] value) {
try {
Files.write(Paths.get(path), value, StandardOpenOption.WRITE);
} catch (IOException e) {
throw new RuntimeException("Unable to draw the LCD", e);
}
return true;
}
}