Repository navigation
Expand file tree
/
Copy pathSysfs.java
More file actions
139 lines (125 loc) · 3.63 KB
/
Copy pathSysfs.java
File metadata and controls
139 lines (125 loc) · 3.63 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
package ev3dev.utils;
import org.slf4j.Logger;
import java.io.*;
import java.nio.charset.Charset;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
/**
* The class responsible to interact with Sysfs on EV3Dev
*
* @author Juan Antonio Breña Moral
*
*/
public class Sysfs {
private static final Logger log = org.slf4j.LoggerFactory.getLogger(Sysfs.class);
/**
* 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(log.isTraceEnabled())
log.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 {
log.error("File: '{}' without write permissions.", filePath);
return false;
}
} catch (IOException ex) {
log.error(ex.getLocalizedMessage());
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(log.isTraceEnabled())
log.trace("cat " + filePath);
try {
final Path path = Paths.get(filePath);
if(existFile(path) && Files.isReadable(path)){
final String result = Files.readAllLines(path, Charset.forName("UTF-8")).get(0);
if(log.isTraceEnabled())
log.trace("value: {}", result);
return result;
}
throw new IOException("Problem reading path: " + filePath);
} catch (IOException ex) {
log.error(ex.getLocalizedMessage());
throw new RuntimeException(ex);
}
}
/**
* 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));
}
public static float readFloat(final String filePath) {
return Float.parseFloat(readString(filePath));
}
/**
*
* @param filePath path
* @return an List with options from a path
*/
public static List<File> getElements(final String filePath){
if(log.isTraceEnabled())
log.trace("ls " + 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(log.isTraceEnabled())
log.trace("ls " + filePath);
final File f = new File(filePath);
return f.exists() && f.isDirectory();
}
public static boolean existFile(Path pathToFind) {
return Files.exists(pathToFind);
}
public static boolean writeBytes(final String path, final byte[] value) {
final File file = new File(path);
try (DataOutputStream out = new DataOutputStream(new FileOutputStream(file))) {
out.write(value);
out.flush();
out.close();
} catch (IOException e) {
throw new RuntimeException("Unable to draw the LCD", e);
}
return true;
}
}