summaryrefslogtreecommitdiff
path: root/drivers/misc/apds990x_proxy.c
blob: 5474f5e518090cdeaac54cd8ffbededd5d3c0cca (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
/*
 * apds990x proximity sensor testing module
 * Copyright (C) 2013  Andi Shyti <andi@etezian.org>
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
 */

#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <unistd.h>
#include <fcntl.h>
#include <errno.h>

#define MAX_N	100
#define ENABLE	"1"
#define DISABLE	"0"

struct device {
	unsigned int n;

	int prox;

	char *power;
	char *prox_en;
	char *prox_raw;
};

struct device apds990x_dev = {
	.n = 10,
	.power = "/sys/class/i2c-adapter/i2c-12/12-0039/power_state",
	.prox_en = "/sys/class/i2c-adapter/i2c-12/12-0039/prox0_raw_en",
	.prox_raw = "/sys/class/i2c-adapter/i2c-12/12-0039/prox0_raw",
};

int write_file(char *fname, const char *val)
{
	int fd;
	ssize_t ret;
	int retval = 0;

	fd = open(fname, O_WRONLY);
	if (fd < 0) {
		retval = errno;
		fprintf(stderr, "%s: %s\n", fname, strerror(errno));
		return retval;
	}

	ret = write(fd, val, 1);
	if (ret != 1) {
		retval = errno;
		fprintf(stderr, "%s: %s\n", fname, strerror(errno));
		close(fd);
		return retval;
	}

	fd = close(fd);
	if (fd < 0) {
		retval = errno;
		fprintf(stderr, "%s: %s\n", fname, strerror(errno));
	}

	return retval;
}

int read_proxy(void)
{
	unsigned int i;
	int fd, retval = 0;
	ssize_t ret;
	char val[6];

	for (i = 0; i < apds990x_dev.n; i++) {
		sleep(1);

		fd = open(apds990x_dev.prox_raw, O_RDONLY);
		if (fd < 0) {
			retval = errno;
			fprintf(stderr, "%s: %s\n", apds990x_dev.prox_raw,
							strerror(errno));
			return retval;
		}

		ret = read(fd, val, sizeof(val));
		if (ret < 0) {
			retval = errno;
			fprintf(stderr, "%s: %s\n",
				apds990x_dev.prox_raw, strerror(errno));
			break;
		}

		/* replace EOF with \n */
		val[ret-1] = '\n';
		apds990x_dev.prox = (int) strtol(val, NULL, 10);
		printf("%d\n", apds990x_dev.prox);

		fd = close(fd);
		if (fd < 0) {
			retval = errno;
			fprintf(stderr, "%s: %s\n", apds990x_dev.prox_raw,
							strerror(errno));
			break;
		}
	}

	return retval;
}

void print_usage(char *name)
{
	printf("Usage: %s [-n <number>]\n", name);
	printf("\n");
	printf("	-n <number>	- number of readings on the device\n");
	printf("	-h, --help	- print help menu\n");
}

int parse_cmdline(int argc, char *argv[])
{
	int i;
	unsigned long int n;

	for (i = 1; i < argc; i++) {
		if (!strcmp(argv[i], "-n")) {
			n = strtoul(argv[++i], NULL, 10);
			/* check for overflow */
			if (errno)
				return errno;

			apds990x_dev.n = ((n > MAX_N) || (n <= 0)) ?
						MAX_N : (unsigned int) n;
		} else if (!strcmp(argv[i], "-h") ||
						!strcmp(argv[i], "--help")) {
			print_usage(argv[0]);
			exit(EXIT_SUCCESS);
		} else {
			fprintf(stderr, "%s: %s\n\n",
						argv[i], strerror(EINVAL));
			print_usage(argv[0]);
			return EINVAL;
		}
	}

	return 0;
}

int main(int argc, char *argv[])
{
	int err;

	if (argc > 1) {
		err = parse_cmdline(argc, argv);
		if (err)
			return err;
	}

	err = write_file(apds990x_dev.power, ENABLE);
	if (err)
		return err;

	err = write_file(apds990x_dev.prox_en, ENABLE);
	if (!err) {
		read_proxy();
		write_file(apds990x_dev.prox_en, DISABLE);
	}

	err = write_file(apds990x_dev.power, DISABLE);

	return err;
}