root / lab4 / lab4.c @ 16
History | View | Annotate | Download (5.87 KB)
1 |
// IMPORTANT: you must include the following line in all your C files
|
---|---|
2 |
#include <lcom/lcf.h> |
3 |
|
4 |
#include <stdint.h> |
5 |
#include <stdio.h> |
6 |
#include "mouse.h" |
7 |
#include "timer.h" |
8 |
|
9 |
// Any header files included below this line should have been created by you
|
10 |
|
11 |
int main(int argc, char *argv[]) { |
12 |
// sets the language of LCF messages (can be either EN-US or PT-PT)
|
13 |
lcf_set_language("EN-US");
|
14 |
|
15 |
// enables to log function invocations that are being "wrapped" by LCF
|
16 |
// [comment this out if you don't want/need/ it]
|
17 |
lcf_trace_calls("/home/lcom/labs/lab4/trace.txt");
|
18 |
|
19 |
// enables to save the output of printf function calls on a file
|
20 |
// [comment this out if you don't want/need it]
|
21 |
lcf_log_output("/home/lcom/labs/lab4/output.txt");
|
22 |
|
23 |
// handles control over to LCF
|
24 |
// [LCF handles command line arguments and invokes the right function]
|
25 |
if (lcf_start(argc, argv))
|
26 |
return 1; |
27 |
|
28 |
// LCF clean up tasks
|
29 |
// [must be the last statement before return]
|
30 |
lcf_cleanup(); |
31 |
|
32 |
return 0; |
33 |
} |
34 |
|
35 |
extern uint8_t byte;
|
36 |
extern int mouse_id; |
37 |
|
38 |
int (mouse_test_packet)(uint32_t cnt) {
|
39 |
int r, ipc_status;
|
40 |
uint8_t mouse_irq; |
41 |
unsigned int size=0; //size of mouse packet is 3 byte long |
42 |
message msg; |
43 |
uint32_t counter_pp=0;
|
44 |
|
45 |
struct packet pp;
|
46 |
|
47 |
|
48 |
//mouse_enable_data_reporting();
|
49 |
|
50 |
//sys_irqdisable(&mouse_id);
|
51 |
if(write_cmd_mouse(0xF4)!=0){ //check if disabling stream mode worked (0xF4 is the byte to disable data report) |
52 |
printf("Error disabling stream mode\n");
|
53 |
return 1; |
54 |
} |
55 |
//sys_irqenable(&mouse_id);
|
56 |
|
57 |
if(mouse_subscribe_int(&mouse_irq)!=0){ //check if subscription worked |
58 |
printf("Error subscribing int\n");
|
59 |
return 1; |
60 |
} |
61 |
|
62 |
|
63 |
while (cnt > counter_pp)
|
64 |
{ /* ends when it has printed the number in ctn */
|
65 |
/*Get a request message.*/
|
66 |
if ((r = driver_receive(ANY, &msg, &ipc_status)) != 0) |
67 |
{ |
68 |
printf("ERROR: driver_receive failed with: %d", r);
|
69 |
continue;
|
70 |
} |
71 |
if (is_ipc_notify(ipc_status))
|
72 |
{ // received notification
|
73 |
switch (_ENDPOINT_P(msg.m_source))
|
74 |
{ |
75 |
case HARDWARE: // hardware interrupt notification |
76 |
if (msg.m_notify.interrupts & mouse_irq)
|
77 |
{ // subscribed interrupt
|
78 |
mouse_ih(); //must read only one byte
|
79 |
if ((size == 0 && (BIT(3) & byte)) || size == 1 ||size ==2) |
80 |
{ //access the struct bytes
|
81 |
pp.bytes[size] = byte; // bytes 0, 1 or 2
|
82 |
size++; // increments the counter to know in the next interrupt
|
83 |
} |
84 |
if (size == 3) // 3 bytes haven been read and there's no error |
85 |
{ |
86 |
size = 0; // reset to start counting new packets |
87 |
parse_packet(&pp); // parses packet
|
88 |
mouse_print_packet(&pp); // prints packet
|
89 |
counter_pp++; //counts the prints for the end of the loop
|
90 |
} |
91 |
} |
92 |
break;
|
93 |
default:
|
94 |
break; // no other notifications expected: do nothing |
95 |
} |
96 |
} |
97 |
else
|
98 |
{ // received a standard message, not a notification
|
99 |
// no standard messages expected: do nothing
|
100 |
} |
101 |
} |
102 |
|
103 |
if (mouse_unsubscribe_int() != 0) { //check if unsubscription worked |
104 |
printf("Error unsubscribing int \n");
|
105 |
return 1; |
106 |
} |
107 |
|
108 |
if(write_cmd_mouse(0xF5)!=0){ //check if disabling stream mode worked (0xF5 is the byte to disable data report) |
109 |
printf("Error disabling stream mode\n");
|
110 |
return 1; |
111 |
} |
112 |
|
113 |
|
114 |
byte=0;
|
115 |
|
116 |
return 0; |
117 |
} |
118 |
|
119 |
int (mouse_test_remote)(uint16_t period, uint8_t cnt) {
|
120 |
uint8_t counter_pp=0, status;
|
121 |
|
122 |
struct packet pp;
|
123 |
|
124 |
/*if(write_cmd_mouse(0xF5)!=0){ //check if disabling stream mode worked (0xF5 is the byte to disable data report)
|
125 |
printf("Error disabling stream mode\n");
|
126 |
return 1;
|
127 |
}
|
128 |
|
129 |
if(write_cmd_mouse(0xF0)!=0){ //check if enabling remote mode worked (0xF0 is the byte to enable remote mode)
|
130 |
printf("Error disabling stream mode\n");
|
131 |
return 1;
|
132 |
}*/
|
133 |
|
134 |
|
135 |
while (cnt > counter_pp)
|
136 |
{ /* ends when it has printed the number in ctn */
|
137 |
if(write_cmd_mouse(0xEB)!=0){ //check if enabling remote mode worked (0xF0 is the byte to enable remote mode) |
138 |
printf("Error disabling stream mode\n");
|
139 |
return 1; |
140 |
} |
141 |
|
142 |
if(util_sys_inb(STATUS_REG,&status)!=0){ |
143 |
printf("Error verifying the status of buffer\n");
|
144 |
return 1; |
145 |
} |
146 |
|
147 |
if((status & 0x01)==0x01){ |
148 |
|
149 |
if((status & (STAT_REG_PAR|STAT_REG_TIMEOUT))==0){ |
150 |
|
151 |
for(int size=0;size<3;size++){ |
152 |
|
153 |
if(util_sys_inb(OUTPUT_BUF,&byte)!=0){ |
154 |
printf("Error reading output_buf\n");
|
155 |
return 1; |
156 |
} |
157 |
|
158 |
if (size == 0 && (BIT(3) & byte)){ // access the struct bytes |
159 |
pp.bytes[size] = byte; // bytes 0, 1 or 2
|
160 |
//continue;
|
161 |
} |
162 |
if(size==1){ |
163 |
pp.bytes[size] = byte; // bytes 0, 1 or 2
|
164 |
//continue;
|
165 |
} |
166 |
if(size==2){ |
167 |
pp.bytes[size] = byte; |
168 |
|
169 |
} |
170 |
} |
171 |
parse_packet(&pp); // parses packet
|
172 |
mouse_print_packet(&pp); // prints packet
|
173 |
|
174 |
} |
175 |
} |
176 |
counter_pp++; //counts the prints for the end of the loop
|
177 |
tickdelay(micros_to_ticks(period*1000));
|
178 |
|
179 |
} |
180 |
|
181 |
if (write_cmd_mouse(0xEA)!=0) { //check if set stream mode worked (0xEA is the byte to enable) |
182 |
printf("Error unsubscribing int \n");
|
183 |
return 1; |
184 |
} |
185 |
|
186 |
if(write_cmd_mouse(0xF5)!=0){ //check if disabling data report worked (0xF5 is the byte to disable data report) |
187 |
printf("Error disabling stream mode\n");
|
188 |
return 1; |
189 |
} |
190 |
|
191 |
if (sys_outb(0X64,0x60)!=0) |
192 |
{ // Sends Write Command
|
193 |
printf("Could not reset KBC command byte to default!");
|
194 |
return 1; |
195 |
} |
196 |
uint8_t default_cmd=minix_get_dflt_kbc_cmd_byte(); |
197 |
if (sys_outb(0x60,default_cmd)!=0) |
198 |
{ // Writes Command Byte
|
199 |
printf("Could not reset KBC command byte to default!");
|
200 |
return 1; |
201 |
} |
202 |
|
203 |
byte=0;
|
204 |
|
205 |
return 0; |
206 |
} |
207 |
|
208 |
int (mouse_test_async)(uint8_t idle_time) {
|
209 |
/* To be completed */
|
210 |
printf("%s(%u): under construction\n", __func__, idle_time);
|
211 |
return 1; |
212 |
} |
213 |
|
214 |
int (mouse_test_gesture)(uint8_t x_len, uint8_t tolerance) {
|
215 |
/* To be completed */
|
216 |
printf("%s: under construction\n", __func__);
|
217 |
return 1; |
218 |
} |