summaryrefslogtreecommitdiff
path: root/firmware/target/hosted/sysfs.c
diff options
context:
space:
mode:
Diffstat (limited to 'firmware/target/hosted/sysfs.c')
-rw-r--r--firmware/target/hosted/sysfs.c200
1 files changed, 200 insertions, 0 deletions
diff --git a/firmware/target/hosted/sysfs.c b/firmware/target/hosted/sysfs.c
new file mode 100644
index 0000000000..177f338911
--- /dev/null
+++ b/firmware/target/hosted/sysfs.c
@@ -0,0 +1,200 @@
1/***************************************************************************
2 * __________ __ ___
3 * Open \______ \ ____ ____ | | _\_ |__ _______ ___
4 * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
5 * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
6 * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
7 * \/ \/ \/ \/ \/
8 *
9 * Copyright (C) 2014 by Ilia Sergachev: Initial Rockbox port to iBasso DX50
10 * Copyright (C) 2014 by Mario Basister: iBasso DX90 port
11 * Copyright (C) 2014 by Simon Rothen: Initial Rockbox repository submission, additional features
12 * Copyright (C) 2014 by Udo Schläpfer: Code clean up, additional features
13 *
14 * This program is free software; you can redistribute it and/or
15 * modify it under the terms of the GNU General Public License
16 * as published by the Free Software Foundation; either version 2
17 * of the License, or (at your option) any later version.
18 *
19 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
20 * KIND, either express or implied.
21 *
22 ****************************************************************************/
23
24
25#include <stdio.h>
26#include <string.h>
27
28#include "config.h"
29#include "debug.h"
30#include "sysfs.h"
31
32
33static FILE* open_read(const char *file_name)
34{
35 FILE *f = fopen(file_name, "r");
36 if(f == NULL)
37 {
38 DEBUGF("ERROR %s: Can not open %s for reading.", __func__, file_name);
39 }
40
41 return f;
42}
43
44
45static FILE* open_write(const char* file_name)
46{
47 FILE *f = fopen(file_name, "w");
48 if(f == NULL)
49 {
50 DEBUGF("ERROR %s: Can not open %s for writing.", __func__, file_name);
51 }
52
53 return f;
54}
55
56
57bool sysfs_get_int(const char *path, int *value)
58{
59 *value = -1;
60
61 FILE *f = open_read(path);
62 if(f == NULL)
63 {
64 return false;
65 }
66
67 bool success = true;
68 if(fscanf(f, "%d", value) == EOF)
69 {
70 DEBUGF("ERROR %s: Read failed for %s.", __func__, path);
71 success = false;
72 }
73
74 fclose(f);
75 return success;
76}
77
78
79bool sysfs_set_int(const char *path, int value)
80{
81 FILE *f = open_write(path);
82 if(f == NULL)
83 {
84 return false;
85 }
86
87 bool success = true;
88 if(fprintf(f, "%d", value) < 0)
89 {
90 DEBUGF("ERROR %s: Write failed for %s.", __func__, path);
91 success = false;
92 }
93
94 fclose(f);
95 return success;
96}
97
98
99bool sysfs_get_char(const char *path, char *value)
100{
101 int c;
102 FILE *f = open_read(path);
103 if(f == NULL)
104 {
105 return false;
106 }
107
108 bool success = true;
109 c = fgetc(f);
110
111 if(c == EOF)
112 {
113 DEBUGF("ERROR %s: Read failed for %s.", __func__, path);
114 success = false;
115 }
116 else
117 {
118 *value = c;
119 }
120
121 fclose(f);
122 return success;
123}
124
125
126bool sysfs_set_char(const char *path, char value)
127{
128 FILE *f = open_write(path);
129 if(f == NULL)
130 {
131 return false;
132 }
133
134 bool success = true;
135 if(fprintf(f, "%c", value) < 1)
136 {
137 DEBUGF("ERROR %s: Write failed for %s.", __func__, path);
138 success = false;
139 }
140
141 fclose(f);
142 return success;
143}
144
145
146bool sysfs_get_string(const char *path, char *value, int size)
147{
148 value[0] = '\0';
149 FILE *f = open_read(path);
150 if(f == NULL)
151 {
152 return false;
153 }
154
155 bool success = true;
156
157 /* fgets returns NULL if en error occured OR
158 * when EOF occurs while no characters have been read.
159 *
160 * Empty string is not an error for us.
161 */
162 if(fgets(value, size, f) == NULL && value[0] != '\0')
163 {
164 DEBUGF("ERROR %s: Read failed for %s.", __func__, path);
165 success = false;
166 }
167 else
168 {
169 size_t length = strlen(value);
170 if((length > 0) && value[length - 1] == '\n')
171 {
172 value[length - 1] = '\0';
173 }
174 }
175
176 fclose(f);
177 return success;
178}
179
180
181bool sysfs_set_string(const char *path, char *value)
182{
183 FILE *f = open_write(path);
184 if(f == NULL)
185 {
186 return false;
187 }
188
189 bool success = true;
190
191 /* If an output error is encountered, a negative value is returned */
192 if(fprintf(f, "%s", value) < 0)
193 {
194 DEBUGF("ERROR %s: Write failed for %s.", __func__, path);
195 success = false;
196 }
197
198 fclose(f);
199 return success;
200}