summaryrefslogtreecommitdiff
path: root/firmware/target/hosted/agptek/sysfs.c
diff options
context:
space:
mode:
Diffstat (limited to 'firmware/target/hosted/agptek/sysfs.c')
-rw-r--r--firmware/target/hosted/agptek/sysfs.c186
1 files changed, 186 insertions, 0 deletions
diff --git a/firmware/target/hosted/agptek/sysfs.c b/firmware/target/hosted/agptek/sysfs.c
new file mode 100644
index 0000000000..ad4635ac57
--- /dev/null
+++ b/firmware/target/hosted/agptek/sysfs.c
@@ -0,0 +1,186 @@
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) < 1)
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 *value = '\0';
102 FILE *f = open_read(path);
103 if(f == NULL)
104 {
105 return false;
106 }
107
108 bool success = true;
109 if(fscanf(f, "%c", value) == EOF)
110 {
111 DEBUGF("ERROR %s: Read failed for %s.", __func__, path);
112 success = false;
113 }
114
115 fclose(f);
116 return success;
117}
118
119
120bool sysfs_set_char(const char *path, char value)
121{
122 FILE *f = open_write(path);
123 if(f == NULL)
124 {
125 return false;
126 }
127
128 bool success = true;
129 if(fprintf(f, "%c", value) < 1)
130 {
131 DEBUGF("ERROR %s: Write failed for %s.", __func__, path);
132 success = false;
133 }
134
135 fclose(f);
136 return success;
137}
138
139
140bool sysfs_get_string(const char *path, char *value, int size)
141{
142 value[0] = '\0';
143 FILE *f = open_read(path);
144 if(f == NULL)
145 {
146 return false;
147 }
148
149 bool success = true;
150 if(fgets(value, size, f) == NULL)
151 {
152 DEBUGF("ERROR %s: Read failed for %s.", __func__, path);
153 success = false;
154 }
155 else
156 {
157 size_t length = strlen(value);
158 if((length > 0) && value[length - 1] == '\n')
159 {
160 value[length - 1] = '\0';
161 }
162 }
163
164 fclose(f);
165 return success;
166}
167
168
169bool sysfs_set_string(const char *path, char *value)
170{
171 FILE *f = open_write(path);
172 if(f == NULL)
173 {
174 return false;
175 }
176
177 bool success = true;
178 if(fprintf(f, "%s", value) < 1)
179 {
180 DEBUGF("ERROR %s: Write failed for %s.", __func__, path);
181 success = false;
182 }
183
184 fclose(f);
185 return success;
186}