aboutsummaryrefslogtreecommitdiff
path: root/recipes-demo/de10-nano-gpio-apps/files/show_KEY0_pb_state.c
blob: c243a7315c7e4e81a16e5c22fd5cf5bc9f59c119 (plain)
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
186
#include <stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <error.h>
#include <errno.h>
#include <limits.h>
#include <unistd.h>
#include <dirent.h>
#include <string.h>

int main(void) {
	DIR *gpio_dir;
	const char *gpio_dir_path = "/sys/class/gpio";
	const char *gpiochip_str = "gpiochip";
	const char *gpio_label = "gpio@0x100005000";
	size_t gpiochip_str_len = strlen(gpiochip_str);
	int result;
	struct dirent *dir_entry;
	char path[PATH_MAX];
	int path_length;
	int file_fd;
	char buffer[PATH_MAX + 1] = {0};
	char gpio_number_buffer[PATH_MAX + 1] = {0};
	char *str_result = NULL;
	char *newline_ptr;

	// open the sysfs gpio directory
	gpio_dir = opendir(gpio_dir_path);
	if(gpio_dir == NULL)
		error(1, errno, "could not open directory '%s'", gpio_dir_path);
	
	// find the gpio controller for the KEY0 push button 'gpio@0x100005000'
	while(1) {
		// read the next directory entry
		errno = 0;
		dir_entry = readdir(gpio_dir);
		if(dir_entry == NULL) {
			if(errno != 0)
				error(1, errno, "reading directory '%s'",
						gpio_dir_path);
			else
				break;
		}
		
		// check if this is a gpio controller entry
		result = strncmp(dir_entry->d_name, gpiochip_str, 
				gpiochip_str_len);
		if(result != 0)
			continue;
			
		// open the gpio controller label file and read label value
		path_length = snprintf(path, PATH_MAX, "%s/%s/label",
				gpio_dir_path, dir_entry->d_name);
		if(path_length < 0)
			error(1, 0, "path output error");
		if(path_length >= PATH_MAX)
			error(1, 0, "path length overflow");

		file_fd = open(path, O_RDONLY | O_SYNC);
		if(file_fd < 0)
			error(1, errno, "could not open file '%s'", path);
		
		result = read(file_fd, buffer, PATH_MAX);
		if(result < 0)
			error(1, errno, "reading from '%s'", path);
		if(result == PATH_MAX)
			error(1, errno, "buffer overflow reading '%s'", path);

		result = close(file_fd);
		if(result < 0)
			error(1, errno, "could not close file '%s'", path);

		buffer[PATH_MAX] = 0;
		// test the gpio controller label value for our gpio controller
		str_result = strstr(buffer, gpio_label);
		if(str_result != NULL)
			break;
	}

	if(str_result == NULL)
		error(1, 0, "unable to locate gpio controller");

	// open the gpio controller base file and read base value
	path_length = snprintf(path, PATH_MAX, "%s/%s/base",
			gpio_dir_path, dir_entry->d_name);
	if(path_length < 0)
		error(1, 0, "path output error");
	if(path_length >= PATH_MAX)
		error(1, 0, "path length overflow");

	file_fd = open(path, O_RDONLY | O_SYNC);
	if(file_fd < 0)
		error(1, errno, "could not open file '%s'", path);
	
	result = read(file_fd, gpio_number_buffer, PATH_MAX);
	if(result < 0)
		error(1, errno, "reading from '%s'", path);
	if(result == PATH_MAX)
		error(1, errno, "buffer overflow reading '%s'", path);

	result = close(file_fd);
	if(result < 0)
		error(1, errno, "could not close file '%s'", path);

	gpio_number_buffer[PATH_MAX] = 0;	
	// remove the newline at the end of the string
	newline_ptr = strchr(gpio_number_buffer,'\n');
	if(newline_ptr != NULL)
		memset(newline_ptr, '\0', 1);
	
	// open the gpio export file and write our gpio number
	path_length = snprintf(path, PATH_MAX, "%s/export",
			gpio_dir_path);
	if(path_length < 0)
		error(1, 0, "path output error");
	if(path_length >= PATH_MAX)
		error(1, 0, "path length overflow");

	file_fd = open(path, O_WRONLY | O_SYNC);
	if(file_fd < 0)
		error(1, errno, "could not open file '%s'", path);
	
	result = write(file_fd, gpio_number_buffer, strlen(gpio_number_buffer));
	// NOTE: we don't bother checking for errors here because if this gpio
	// has already been exported this write will receive a device busy error
	// which is perfectly normal.

	result = close(file_fd);
	if(result < 0)
		error(1, errno, "could not close file '%s'", path);

	// open the gpio value file and read the value
	path_length = snprintf(path, PATH_MAX, "%s/gpio%s/value",
			gpio_dir_path, gpio_number_buffer);
	if(path_length < 0)
		error(1, 0, "path output error");
	if(path_length >= PATH_MAX)
		error(1, 0, "path length overflow");

	file_fd = open(path, O_RDONLY | O_SYNC);
	if(file_fd < 0)
		error(1, errno, "could not open file '%s'", path);
	
	result = read(file_fd, buffer, PATH_MAX);
	if(result < 0)
		error(1, errno, "reading from '%s'", path);
	if(result == PATH_MAX)
		error(1, errno, "buffer overflow reading '%s'", path);

	result = close(file_fd);
	if(result < 0)
		error(1, errno, "could not close file '%s'", path);

	buffer[PATH_MAX] = 0;	
	// remove the newline at the end of the string
	newline_ptr = strchr(buffer,'\n');
	if(newline_ptr != NULL)
		memset(newline_ptr, '\0', 1);

	// print the KEY0 push button state value
	printf("KEY0 push button is at state '%s'\n", buffer);
	
	// open the gpio unexport file and write our gpio number
	path_length = snprintf(path, PATH_MAX, "%s/unexport",
			gpio_dir_path);
	if(path_length < 0)
		error(1, 0, "path output error");
	if(path_length >= PATH_MAX)
		error(1, 0, "path length overflow");

	file_fd = open(path, O_WRONLY | O_SYNC);
	if(file_fd < 0)
		error(1, errno, "could not open file '%s'", path);
	
	result = write(file_fd, gpio_number_buffer, strlen(gpio_number_buffer));
	if(result < 0)
		error(1, errno, "writing to '%s'", path);
	if((size_t)(result) != strlen(gpio_number_buffer))
		error(1, errno, "buffer underflow writing '%s'", path);

	result = close(file_fd);
	if(result < 0)
		error(1, errno, "could not close file '%s'", path);
}