@@ -35,42 +35,60 @@ void gpio_unexport(__attribute__((unused)) const int signal) {
35
35
exit (0 );
36
36
}
37
37
38
+ bool set_direction (const char * direction_path , const char * direction ) {
39
+ int direction_data = open (direction_path , O_WRONLY );
40
+ if (direction_data == -1 || write (direction_data , direction , 3 ) != 3 ) {
41
+ fprintf (stderr , "Error writing to %s\n" , direction_path );
42
+ close (direction_data );
43
+ return 0 ;
44
+ }
45
+ close (direction_data );
46
+ return 1 ;
47
+ }
48
+
38
49
bool gpio_set_active (const char * gpio , bool active ) {
39
50
// Checking existance of provided relay
40
51
for (unsigned short i = 0 ; i <= RELAYS_COUNT ; i ++ ) {
41
52
if (i == RELAYS_COUNT ) {
42
53
fprintf (stderr , "GPIO pin %d isn't configured.\n" , i );
43
54
return 0 ;
44
55
} else if (strcmp (RELAYS [i ].id , gpio ) == 0 ) {
56
+ #ifdef SET_DIRECTION
45
57
// Setting direction "out" or "in" for this GPIO pin
46
- int direction_data = open (RELAYS [i ].direction_path , O_WRONLY );
47
- char * direction = active ? "out" : "in" ;
48
- if (direction_data == -1 || write (direction_data , direction , 3 ) != 3 ) {
49
- fprintf (stderr , "Error writing to %s\n" , RELAYS [i ].direction_path );
50
- close (direction_data );
58
+ char direction [4 ] = {0 };
59
+ if (active ) {
60
+ strncpy (direction , "out" , 4 );
61
+ } else {
62
+ strncpy (direction , "in" , 4 );
63
+ }
64
+
65
+ if (!set_direction (RELAYS [i ].direction_path , direction )) {
51
66
return 0 ;
52
67
}
53
- close (direction_data );
54
68
if (active ) {
69
+ #endif
55
70
int value_data = open (RELAYS [i ].value_path , O_WRONLY );
56
- if (value_data == -1 || write (value_data , "1 " , 1 ) != 1 ) {
71
+ if (value_data == -1 || write (value_data , active ? "1" : "0 " , 1 ) != 1 ) {
57
72
fprintf (stderr , "Error writing to %s\n" , RELAYS [i ].value_path );
58
73
close (value_data );
59
74
return 0 ;
60
75
}
61
76
close (value_data );
77
+ #ifdef SET_DIRECTION
62
78
}
79
+ #endif
63
80
break ;
64
81
}
65
82
}
66
83
return 1 ;
67
84
}
68
85
69
- bool gpio_get_active (const char * direction_path ) {
86
+ bool gpio_get_active (const char * path ) {
87
+ #ifdef SET_DIRECTION
70
88
char direction [4 ] = {0 };
71
- int direction_data = open (direction_path , O_RDONLY );
89
+ int direction_data = open (path , O_RDONLY );
72
90
if (direction_data == -1 || read (direction_data , direction , 3 ) != 3 ) {
73
- fprintf (stderr , "Error while reading %s\n" , direction_path );
91
+ fprintf (stderr , "Error while reading %s\n" , path );
74
92
close (direction_data );
75
93
return 0 ;
76
94
}
@@ -79,6 +97,20 @@ bool gpio_get_active(const char* direction_path) {
79
97
return 1 ;
80
98
else
81
99
return 0 ;
100
+ #else
101
+ char value [2 ] = {0 };
102
+ int value_data = open (path , O_RDONLY );
103
+ if (value_data == -1 || read (value_data , value , 1 ) != 1 ) {
104
+ fprintf (stderr , "Error while reading %s\n" , path );
105
+ close (value_data );
106
+ return 0 ;
107
+ }
108
+ close (value_data );
109
+ if (strcmp (value , "1" ) == 0 )
110
+ return 1 ;
111
+ else
112
+ return 0 ;
113
+ #endif
82
114
}
83
115
84
116
bool starts_with (const char * a , const char * b ) {
@@ -111,11 +143,16 @@ int main() {
111
143
if (export_data == -1 )
112
144
fail ("Unable to open /sys/class/gpio/export\n" );
113
145
114
- for (unsigned short i = 0 ; i < RELAYS_COUNT ; i ++ )
146
+ for (unsigned short i = 0 ; i < RELAYS_COUNT ; i ++ ) {
115
147
if (write (export_data , RELAYS [i ].id , 4 ) != 4 ) {
116
148
close (export_data );
117
149
fail ("Error writing to /sys/class/gpio/export\n" );
118
150
}
151
+ #ifndef SET_DIRECTION
152
+ if (!set_direction (RELAYS [i ].direction_path , "out" ))
153
+ fail ("Unable to set direction\n" );
154
+ #endif
155
+ }
119
156
close (export_data );
120
157
// Unexport GPIO on Ctrl + C
121
158
signal (SIGINT , gpio_unexport );
@@ -139,15 +176,21 @@ int main() {
139
176
while (FCGX_Accept_r (& request ) >= 0 ) {
140
177
char * query_string = FCGX_GetParam ("QUERY_STRING" , request .envp );
141
178
char * request_method = FCGX_GetParam ("REQUEST_METHOD" , request .envp );
142
- if (strcmp (query_string , "status=csv" ) == 0 && strcmp (request_method , "GET" ) == 0 ) {
179
+ if (strcmp (query_string , "status=csv" ) == 0 &&
180
+ strcmp (request_method , "GET" ) == 0 ) {
143
181
FCGX_PutS (
144
182
"Status: 200 OK\r\n"
145
183
"Content-type: text/csv\r\n\r\n"
146
184
"relay,active,name\n" ,
147
185
request .out );
148
186
for (unsigned short i = 0 ; i < RELAYS_COUNT ; i ++ )
149
187
FCGX_FPrintF (request .out , "%s,%d,%s\n" , RELAYS [i ].id ,
150
- gpio_get_active (RELAYS [i ].direction_path ), RELAYS [i ].name );
188
+ #ifdef SET_DIRECTION
189
+ gpio_get_active (RELAYS [i ].direction_path ),
190
+ #else
191
+ gpio_get_active (RELAYS [i ].value_path ),
192
+ #endif
193
+ RELAYS [i ].name );
151
194
continue ;
152
195
} else if (strcmp (request_method , "POST" ) == 0 ) {
153
196
// Get POST body data
0 commit comments