-
Notifications
You must be signed in to change notification settings - Fork 34
/
common.c
156 lines (126 loc) · 2.91 KB
/
common.c
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
#include "common.h"
#include <string.h>
#include <unistd.h>
#include <fcntl.h>
#include <errno.h>
#include <poll.h>
#define LOG_TAG "FPC COMMON"
#if PLATFORM_SDK_VERSION < 28
#include <cutils/log.h>
#else
#include <log/log.h>
#endif
#include <sys/ioctl.h>
err_t fpc_set_power(int poweron)
{
int fd, ret = -1;
fd = open("/dev/fingerprint", O_RDWR);
if (fd < 0) {
ALOGE("Error opening FPC device\n");
return -1;
}
ret = ioctl(fd, FPC_IOCWPREPARE, poweron);
if (ret < 0) {
ALOGE("Error preparing FPC device\n");
close(fd);
return -1;
}
close(fd);
return 1;
}
err_t fpc_get_power(void)
{
int fd, ret = -1;
uint32_t reply = -1;
fd = open("/dev/fingerprint", O_RDWR);
if (fd < 0) {
ALOGE("Error opening FPC device\n");
return -1;
}
ret = ioctl(fd, FPC_IOCRPREPARE, &reply);
if (ret < 0) {
ALOGE("Error preparing FPC device\n");
close(fd);
return -1;
}
close(fd);
if (reply > 1)
return -1;
return reply;
}
err_t fpc_poll_irq(void)
{
int fd, ret = -1;
uint32_t arg = 0;
struct pollfd pfd;
fd = open("/dev/fingerprint", O_RDWR | O_NONBLOCK);
if (fd < 0) {
ALOGE("Error opening FPC device\n");
return -1;
}
ret = ioctl(fd, FPC_IOCRIRQPOLL, &arg);
if (ret < 0) {
ALOGE("Error polling FPC device\n");
close(fd);
return -1;
}
close(fd);
ALOGV("Interrupt status: %d\n", arg);
/* 0 means that the interrupt didn't fire */
if (arg == 0)
return -1;
return (err_t)arg;
}
err_t sysfs_write(char *path, char *s)
{
char buf[80];
ssize_t len;
int ret = 0;
int fd = open(path, O_WRONLY);
if (fd < 0) {
strerror_r(errno, buf, sizeof(buf));
ALOGE("Error opening %s: %s\n", path, buf);
return -1 ;
}
len = write(fd, s, strlen(s));
if (len < 0) {
strerror_r(errno, buf, sizeof(buf));
ALOGE("Error writing to %s: %s\n", path, buf);
ret = -1;
}
close(fd);
return ret;
}
err_t sys_fs_irq_poll(char *path)
{
char buf[80];
int ret = 0;
int result;
struct pollfd pollfds[2];
pollfds[0].fd = open(path, O_RDONLY | O_NONBLOCK);
if (pollfds[0].fd < 0) {
strerror_r(errno, buf, sizeof(buf));
ALOGE("Error opening %s: %s\n", path, buf);
return -1 ;
}
char dummybuf;
read(pollfds[0].fd, &dummybuf, 1);
pollfds[0].events = POLLPRI;
result = poll(pollfds, 1, 1000);
switch (result) {
case 0:
ALOGD ("timeout\n");
close(pollfds[0].fd);
return -1;
case -1:
ALOGE ("poll error \n");
close(pollfds[0].fd);
return -1;
default:
ALOGD ("IRQ GOT \n");
close(pollfds[0].fd);
break;
}
close(pollfds[0].fd);
return ret;
}