-
Notifications
You must be signed in to change notification settings - Fork 0
/
ppt.c
130 lines (120 loc) · 3.09 KB
/
ppt.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
#include <stdio.h>
#include <stdlib.h>
#include <sys/io.h>
#include <time.h>
#define BASEPORT 0x378
#define STATUS BASEPORT+1
#define CONTROL BASEPORT+2
#define ON 1
#define OFF 0
/*
* These functions do what they say they do.
*
* output_on_time() sends the timestamp to fp with the name of the signal and
* the "ON" string.
*
* output_off_time() sends the timestamp to fp with the name of the signal and
* the "OFF" string.
*/
void output_on_time(char *output, const char *signal_name);
void output_off_time(char *output, const char *signal_name);
int main(int argc, char **argv)
{
int status; /* Container for STATUS bits 3...7 */
char BUSY_STATE = OFF;
char ACK_STATE = OFF;
char ERR_STATE = OFF;
char SLCT_STATE = OFF;
char PE_STATE = OFF;
const char busy[] = "BUSY";
const char ack[] = "ACK";
const char err[] = "ERR";
const char slct[] = "SLCT";
const char pe[] = "PE";
char *of = NULL; /* name of output file defaults to NULL */
struct timespec delay = { 0 };
char c;
int i = argc;
while (--argc > 0 && **++argv == '-')
while ((c = *++argv[0]))
switch (c) {
case 'f':
of = argv[i - argc];
break;
default:
break;
}
if (ioperm(BASEPORT, 3, 1)) {
perror("ioperm");
exit(1);
}
printf("status: %x\n", inb(STATUS));
delay.tv_nsec = 1000000;
while ((status = (inb(STATUS) >> 3) ^ 0x10)) {
nanosleep(&delay, NULL);
if ((~(status >> 4) & 0x01) == ON && (BUSY_STATE != ON)) {
output_on_time(of, busy);
BUSY_STATE = ON;
} else if ((~(status >> 4) & 0x01) == OFF
&& BUSY_STATE == ON) {
output_off_time(of, busy);
BUSY_STATE = OFF;
} else if ((~(status >> 3) & 0x01) == ON
&& (ACK_STATE != ON)) {
output_on_time(of, ack);
ACK_STATE = ON;
} else if ((~(status >> 3) & 0x01) == OFF
&& ACK_STATE == ON) {
output_off_time(of, ack);
ACK_STATE = OFF;
} else if ((~(status >> 1) & 0x01) == ON
&& (SLCT_STATE != ON)) {
output_on_time(of, slct);
SLCT_STATE = ON;
} else if ((~(status >> 1) & 0x01) == OFF
&& SLCT_STATE == ON) {
output_off_time(of, slct);
SLCT_STATE = OFF;
} else if (~(status & 0x01) == ON && (ERR_STATE != ON)) {
output_on_time(of, err);
ERR_STATE = ON;
} else if (~(status & 0x01) == OFF && ERR_STATE == ON) {
output_off_time(of, err);
ERR_STATE = OFF;
} else if ((~(status >> 2) & 0x01) == ON && (PE_STATE != ON)) {
output_on_time(of, pe);
PE_STATE = ON;
} else if ((~(status >> 2) & 0x01) == OFF
&& PE_STATE == ON) {
output_off_time(of, pe);
PE_STATE = OFF;
}
}
if (ioperm(BASEPORT, 3, 0)) {
perror("ioperm");
exit(1);
}
exit(0);
}
void output_on_time(char *fname, const char *sname)
{
if (fname != NULL) {
FILE *fp;
fp = fopen(fname, "a");
fprintf(fp, "%s ON, %d\n", sname, (int) time(NULL));
fclose(fp);
} else {
fprintf(stdout, "%s ON, %d\n", sname, (int) time(NULL));
}
}
void output_off_time(char *fname, const char *sname)
{
if (fname != NULL) {
FILE *fp;
fp = fopen(fname, "a");
fprintf(fp, "%s OFF, %d\n", sname, (int) time(NULL));
fclose(fp);
} else {
fprintf(stdout, "%s OFF, %d\n", sname, (int) time(NULL));
}
}