-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathlab4.c
executable file
·343 lines (258 loc) · 8.05 KB
/
lab4.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
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
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
// IMPORTANT: you must include the following line in all your C files
#include <lcom/lcf.h>
#include <stdint.h>
#include <stdio.h>
// Any header files included below this line should have been created by you
#include "i8042.h"
#include "mouse.h"
#include "mouse_gesture.h"
#include "timer.h"
int main(int argc, char *argv[]) {
// sets the language of LCF messages (can be either EN-US or PT-PT)
lcf_set_language("EN-US");
// enables to log function invocations that are being "wrapped" by LCF
// [comment this out if you don't want/need/ it]
lcf_trace_calls("/home/lcom/labs/lab4/trace.txt");
// enables to save the output of printf function calls on a file
// [comment this out if you don't want/need it]
lcf_log_output("/home/lcom/labs/lab4/output.txt");
// handles control over to LCF
// [LCF handles command line arguments and invokes the right function]
if (lcf_start(argc, argv))
return 1;
// LCF clean up tasks
// [must be the last statement before return]
lcf_cleanup();
return 0;
}
int (mouse_test_packet)(uint32_t cnt) {
uint8_t mouse_bit_no = MOUSE_IRQ;
// Only avoids making this operation on every notification
int mouse_bit_mask = BIT(mouse_bit_no);
if (mouse_subscribe_int(&mouse_bit_no))
return 1;
if (mouse_disable_int())
return 1;
// Set mouse to streaming mode with data reporting enabled
if (mouse_set_stream_mode())
return 1;
if (mouse_data_reporting_enable())
return 1;
if (mouse_enable_int())
return 1;
int r, ipc_status;
message msg;
// Interrupt loop
while (cnt) {
if ((r = driver_receive(ANY, &msg, &ipc_status)) != 0) {
printf("driver_receive failed with: %d", r);
continue;
}
if (is_ipc_notify(ipc_status)) {
switch (_ENDPOINT_P(msg.m_source)) {
case HARDWARE: /* hardware interrupt notification */
if (msg.m_notify.interrupts & mouse_bit_mask) { /* subscribed interrupt */
mouse_ih();
if (mouse_ih_error)
return 1;
if (mouse_data_handler())
return 1;
if (is_mouse_packet_complete) {
cnt--;
mouse_print_packet(&mouse_parsed_packet);
}
}
break;
default:
break; /* no other notifications expected: do nothing */
}
}
else { /* received a standard message, not a notification */
/* no standard messages expected: do nothing */
}
}
if (mouse_unsubscribe_int())
return 1;
if (mouse_data_reporting_disable())
return 1;
return 0;
}
int (mouse_test_remote)(uint16_t period, uint8_t cnt) {
// Set mouse to remote mode and disable data reporting
if (mouse_data_reporting_disable()) {
printf("Reporting failed\n");
return 1;
}
if (mouse_set_remote_mode()) {
return 1;
}
uint8_t status;
uint8_t bytes_read;
while (cnt) {
// Send cmd to read a packet to the mouse
if (mouse_read_data())
return 1;
bytes_read = 3;
while (bytes_read) { // We will read 3 bytes (a complete packet)
// Read status
if (util_sys_inb(MOUSE_STAT_REG, &status))
return 1;
// Check if the output buffer is full with mouse data
if (status & (ST_MOUSE_DATA | ST_OUT_BUF)) {
// Pretty much same treatment as with the interrupts
mouse_ih();
if (mouse_ih_error)
return 1;
if (mouse_data_handler())
return 1;
--bytes_read;
}
}
if (is_mouse_packet_complete) {
cnt--;
// printf("%x, %x, %x\n", mouse_parsed_packet.bytes[0], mouse_parsed_packet.bytes[1], mouse_parsed_packet.bytes[2]);
mouse_print_packet(&mouse_parsed_packet);
}
// We have read out packet, so we wait for period miliseconds
tickdelay(micros_to_ticks(period * 1000)); // *1000 to convert to microseconds
}
// Reenable stream mode
if (mouse_set_stream_mode())
return 1;
// Disable data reporting
if (mouse_data_reporting_disable())
return 1;
if (restore_kbc_byte())
return 1;
return 0;
}
int (mouse_test_async)(uint8_t idle_time) {
uint8_t mouse_bit_no = MOUSE_IRQ;
uint8_t timer0_bit_no = TIMER0_IRQ;
// Subscribe to timer0
if (timer_subscribe_int(&timer0_bit_no))
return 1;
if (mouse_subscribe_int(&mouse_bit_no)) {
timer_unsubscribe_int(); // If it fails, we need to unsubscribe from timer0
return 1;
}
if (mouse_disable_int())
return 1;
// Set mouse to streaming mode with data reporting enabled
if (mouse_set_stream_mode())
return 1;
if (mouse_data_reporting_enable())
return 1;
if (mouse_enable_int())
return 1;
// Only avoids making this operation on every notification
int mouse_bit_mask = BIT(mouse_bit_no);
int timer0_bit_mask = BIT(timer0_bit_no);
uint8_t idle_countdown = idle_time;
int r, ipc_status;
message msg;
// Interrupt loop
while (idle_countdown) {
if ((r = driver_receive(ANY, &msg, &ipc_status)) != 0) {
printf("driver_receive failed with: %d", r);
continue;
}
if (is_ipc_notify(ipc_status)) {
switch (_ENDPOINT_P(msg.m_source)) {
case HARDWARE: /* hardware interrupt notification */
/* -------- Timer0 Interrupts -------- */
if (msg.m_notify.interrupts & timer0_bit_mask) {
timer_int_handler();
if (global_timer0_counter == 60) {
global_timer0_counter = 0;
--idle_countdown;
}
}
/* -------- Mouse Interrupts -------- */
if (msg.m_notify.interrupts & mouse_bit_mask) { /* subscribed interrupt */
mouse_ih();
if (mouse_ih_error)
return 1;
if (mouse_data_handler())
return 1;
if (is_mouse_packet_complete) {
idle_countdown = idle_time;
global_timer0_counter = 0;
// printf("%x, %x, %x\n", mouse_parsed_packet.bytes[0], mouse_parsed_packet.bytes[1], mouse_parsed_packet.bytes[2]);
mouse_print_packet(&mouse_parsed_packet);
}
}
break;
default:
break; /* no other notifications expected: do nothing */
}
}
else { /* received a standard message, not a notification */
/* no standard messages expected: do nothing */
}
}
// Unsubscribe from timer0 interrupts
if (timer_unsubscribe_int())
return 1;
// Unsubscribe from mouse interrupts
if (mouse_unsubscribe_int())
return 1;
// Mouse is already on streaming mode, only need to disable data reporting
if (mouse_data_reporting_disable())
return 1;
return 0;
}
int (mouse_test_gesture)(uint8_t x_len, uint8_t tolerance) {
uint8_t mouse_bit_no = MOUSE_IRQ;
// Only avoids making this operation on every notification
int mouse_bit_mask = BIT(mouse_bit_no);
if (mouse_subscribe_int(&mouse_bit_no))
return 1;
if (mouse_disable_int())
return 1;
// Set mouse to streaming mode with data reporting enabled
if (mouse_set_stream_mode())
return 1;
if (mouse_data_reporting_enable())
return 1;
if (mouse_enable_int())
return 1;
int r, ipc_status;
message msg;
bool notFoundGesture = true;
// Interrupt loop
while (notFoundGesture) {
if ((r = driver_receive(ANY, &msg, &ipc_status)) != 0) {
printf("driver_receive failed with: %d", r);
continue;
}
if (is_ipc_notify(ipc_status)) {
switch (_ENDPOINT_P(msg.m_source)) {
case HARDWARE: /* hardware interrupt notification */
if (msg.m_notify.interrupts & mouse_bit_mask) { /* subscribed interrupt */
mouse_ih();
if (mouse_ih_error)
return 1;
if (mouse_data_handler())
return 1;
}
break;
default:
break; /* no other notifications expected: do nothing */
}
}
else { /* received a standard message, not a notification */
/* no standard messages expected: do nothing */
}
if (is_mouse_packet_complete) { // Time for state machine stuff :D
notFoundGesture = !gesture_sm(x_len, tolerance);
is_mouse_packet_complete = false;
mouse_print_packet(&mouse_parsed_packet);
}
}
if (mouse_unsubscribe_int())
return 1;
if (mouse_data_reporting_disable())
return 1;
return 0;
}