-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathwrite_int_time.c
More file actions
185 lines (147 loc) · 3.22 KB
/
write_int_time.c
File metadata and controls
185 lines (147 loc) · 3.22 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
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
185
#include <string.h>
#include <stdlib.h>
#include <stdio.h>
#include <stdint.h>
#include <dirent.h>
#include <errno.h>
#include <ctype.h>
/**
* read_sysfs_string() - read a string from file
* @filename: name of file to read from
* @basedir: the sysfs directory in which the file is to be found
* @str: output the read string
*
* Returns a value >= 0 on success, otherwise a negative error code.
**/
int read_sysfs_string(const char *filename, char *str)
{
int ret = 0;
FILE *sysfsfp;
sysfsfp = fopen(filename, "r");
if (!sysfsfp) {
ret = -errno;
goto error_free;
}
errno = 0;
if (fscanf(sysfsfp, "%s\n", str) != 1) {
ret = errno ? -errno : -ENODATA;
if (fclose(sysfsfp))
perror("read_sysfs_string(): Failed to close dir");
goto error_free;
}
if (fclose(sysfsfp))
ret = -errno;
error_free:
return ret;
}
static int write_sysfs_float(const char *filename, float val)
{
int ret = 0;
FILE *sysfsfp;
int test;
char *temp = malloc(strlen(filename) + 2);
if (!temp)
return -ENOMEM;
sysfsfp = fopen(filename, "w");
if (!sysfsfp) {
ret = -errno;
fprintf(stderr, "failed to open %s\n", filename);
goto error_free;
}
ret = fprintf(sysfsfp, "%f", val);
if (ret < 0) {
if (fclose(sysfsfp))
perror("_write_sysfs_int(): Failed to close dir");
goto error_free;
}
if (fclose(sysfsfp)) {
ret = -errno;
goto error_free;
}
error_free:
free(temp);
}
static int write_sysfs_string(const char *filename, const char *val)
{
int ret = 0;
FILE *sysfsfp;
int test;
char *temp = malloc(strlen(filename) + 2);
if (!temp)
return -ENOMEM;
sysfsfp = fopen(filename, "w");
if (!sysfsfp) {
ret = -errno;
fprintf(stderr, "failed to open %s\n", filename);
goto error_free;
}
#if 1
ret = fprintf(sysfsfp, "%s", val);
if (ret < 0) {
if (fclose(sysfsfp))
perror("_write_sysfs_int(): Failed to close dir");
goto error_free;
}
#endif
if (fclose(sysfsfp)) {
ret = -errno;
goto error_free;
}
error_free:
free(temp);
}
static int write_sysfs_int(const char *filename, int val)
{
int ret = 0;
FILE *sysfsfp;
int test;
char *temp = malloc(strlen(filename) + 2);
if (!temp)
return -ENOMEM;
sysfsfp = fopen(filename, "w");
if (!sysfsfp) {
ret = -errno;
fprintf(stderr, "failed to open %s\n", filename);
goto error_free;
}
ret = fprintf(sysfsfp, "%d", val);
if (ret < 0) {
if (fclose(sysfsfp))
perror("_write_sysfs_int(): Failed to close dir");
goto error_free;
}
if (fclose(sysfsfp)) {
ret = -errno;
goto error_free;
}
error_free:
free(temp);
}
int main(int argc, char **argv)
{
if (argc < 2) {
printf("No time passed as arg\n");
return -1;
}
char int_time_filename[] = "/sys/bus/iio/devices/iio:device0/in_illuminance_integration_time";
char buf[128] = {'\0'};
char *int_time = argv[1];
int ret;
ret = read_sysfs_string(int_time_filename, buf);
if (ret < 0) {
printf("read sysfs failed\n");
return ret;
}
printf("before: int_time=%s\n", buf);
printf("target time: %s\n", int_time);
ret = write_sysfs_string(int_time_filename, int_time);
if (ret < 0)
printf("write sys fs int failed\n");
ret = read_sysfs_string(int_time_filename, buf);
if (ret < 0) {
printf("read sysfs failed\n");
return ret;
}
printf("after: int_time=%s\n", buf);
return ret;
}