Siren.c
1.44 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
#include <jni.h>
#include <stdio.h>
#include "model_SirenJNI.h"
#include <fcntl.h>
#include <errno.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <asm/ioctl.h>
#include <linux/parport.h>
#include <linux/ppdev.h>
#define DEVICE "/dev/parport0"
JNIEXPORT void JNICALL Java_model_SirenJNI_sirenOn(JNIEnv *env, jobject obj) {
int fd = open(DEVICE, O_WRONLY | O_NOCTTY | O_NDELAY);
if (fd < 0) {
// fprintf(stderr, "can not open %s\n", DEVICE);
return;
}
if(ioctl(fd, PPCLAIM)) {
perror("PPCLAIM");
close(fd);
return;
}
unsigned char data = 255;
if (ioctl(fd, PPWDATA, &data) < 0)
perror("ioctl()");
ioctl(fd, PPRELEASE);
close(fd);
return;
}
JNIEXPORT void JNICALL Java_model_SirenJNI_sirenOff (JNIEnv *env, jobject obj) {
int fd = open(DEVICE, O_WRONLY | O_NOCTTY | O_NDELAY);
if (fd < 0) {
// fprintf(stderr, "can not open %s\n", DEVICE);
return;
}
if(ioctl(fd, PPCLAIM)) {
perror("PPCLAIM");
close(fd);
return;
}
unsigned char data = 0;
if (ioctl(fd, PPWDATA, &data) < 0)
perror("ioctl()");
ioctl(fd, PPRELEASE);
close(fd);
return;
}