Project

General

Profile

Statistics
| Revision:

root / lab4 / lab4.c @ 87

History | View | Annotate | Download (9.15 KB)

1 53 up20180655
#include <lcom/lcf.h>
2
3
#include <stdint.h>
4
#include <stdio.h>
5
6 58 up20180642
#include "mouse.h"
7
#include "kbc.h"
8 70 up20180655
#include "errors.h"
9
#include "mouse_macros.h"
10 78 up20180655
#include "kbc_macros.h"
11 74 up20180642
#include "timer.h"
12 78 up20180655
#include "state_machine.h"
13 53 up20180655
14
int main(int argc, char *argv[]) {
15
  // sets the language of LCF messages (can be either EN-US or PT-PT)
16
  lcf_set_language("EN-US");
17
18
  // enables to log function invocations that are being "wrapped" by LCF
19
  // [comment this out if you don't want/need/ it]
20
  lcf_trace_calls("/home/lcom/labs/lab4/trace.txt");
21
22
  // enables to save the output of printf function calls on a file
23
  // [comment this out if you don't want/need it]
24
  lcf_log_output("/home/lcom/labs/lab4/output.txt");
25
26
  // handles control over to LCF
27
  // [LCF handles command line arguments and invokes the right function]
28
  if (lcf_start(argc, argv))
29
    return 1;
30
31
  // LCF clean up tasks
32
  // [must be the last statement before return]
33
  lcf_cleanup();
34
35
  return 0;
36
}
37
38
int (mouse_test_packet)(uint32_t cnt) {
39 77 up20180642
    int ret = 0;
40 55 up20180642
    /// loop stuff
41
    int ipc_status, r;
42
    message msg;
43 74 up20180642
    /// Mouse interrupt handling
44 55 up20180642
    uint8_t mouse_irq_bit = 12;
45
    int mouse_id = 0;
46
    int mouse_irq = BIT(mouse_irq_bit);
47 76 up20180655
48 87 up20180655
    if (subscribe_mouse_interrupt(mouse_irq_bit, &mouse_id)) return 1; // subscribes mouse interrupts in exclusive mode
49
    if (sys_irqdisable(&mouse_id)) return 1; // temporarily disables our interrupts notifications
50
    if ((ret = mouse_set_data_report(true))) return ret; // enables mouse data reporting
51
    if (sys_irqenable(&mouse_id)) return 1; // re-enables our interrupts notifications
52
53 55 up20180642
    /// cycle
54 87 up20180655
    int good = (cnt != 0);
55 56 up20180642
    uint32_t cnt_now = 0;
56 55 up20180642
    while (good) {
57
        /* Get a request message. */
58
        if ((r = driver_receive(ANY, &msg, &ipc_status)) != 0) {
59
            printf("driver_receive failed with %d", r);
60
            continue;
61
        }
62
        if (is_ipc_notify(ipc_status)) { /* received notification */
63
            switch (_ENDPOINT_P(msg.m_source)) {
64
                case HARDWARE: /* hardware interrupt notification */
65
                    if (msg.m_notify.interrupts & mouse_irq) { /* subscribed interrupt */
66
                        mouse_ih();
67 80 up20180642
                        if(counter_mouse_ih >= 3){
68
                            struct packet pp = mouse_parse_packet(packet_mouse_ih);
69 56 up20180642
                            mouse_print_packet(&pp);
70
                            cnt_now++;
71
                            if(cnt == cnt_now) good = 0;
72
                        }
73 55 up20180642
                    }
74
                    break;
75
                default:
76
                    break; /* no other notifications expected: do nothing */
77
            }
78
        } else { /* received standart message, not a notification */
79
            /* no standart message expected: do nothing */
80
        }
81
    }
82 87 up20180655
    if (sys_irqdisable(&mouse_id)) return 1; // temporarily disables our interrupts notifications
83
    if (mouse_set_data_report(false)) return 1; // enables mouse data reporting
84
    if (sys_irqenable(&mouse_id)) return 1; // re-enables our interrupts notifications
85
    if (unsubscribe_interrupt(&mouse_id)) return 1; // unsubscribes interrupts
86 55 up20180642
    return 0;
87 53 up20180655
}
88
89
int (mouse_test_remote)(uint16_t period, uint8_t cnt) {
90 70 up20180655
    int ret;
91 80 up20180642
    //if ((ret = mouse_issue_cmd(SET_REMOTE_MD))) return ret;
92
    //if ((ret = mouse_set_data_report(true))) return ret;
93 70 up20180655
94 80 up20180642
    struct packet pp;
95
    while(cnt--){
96
        if(mouse_poll(&pp, period)) return 1;
97
        mouse_print_packet(&pp);
98 85 up20180655
        tickdelay(micros_to_ticks(period*1000));
99 80 up20180642
    }
100 70 up20180655
    // Set Stream mode
101
    if ((ret = mouse_issue_cmd(SET_STREAM_MD))) return ret;
102
    // Disable data reporting
103 73 up20180642
    if ((ret = mouse_set_data_report(false))) return ret;
104 70 up20180655
    uint8_t cmd_byte = minix_get_dflt_kbc_cmd_byte();
105
    if ((ret = kbc_change_cmd(cmd_byte))) return ret;
106
107
    return SUCCESS;
108 53 up20180655
}
109
110
int (mouse_test_async)(uint8_t idle_time) {
111 74 up20180642
    /// loop stuff
112
    int ipc_status, r;
113
    message msg;
114
    /// Timer interrupt handling
115
    const uint32_t frequency = sys_hz(); // Frequency asummed at 60Hz
116
    uint8_t timer_irq_bit = 0;
117
    int timer_id = 0;
118
    int timer_irq = BIT(timer_irq_bit);
119
    if(subscribe_timer_interrupt(timer_irq_bit, &timer_id)) return 1;
120
121
    no_interrupts = 0;
122
    int time = 0;
123
    /// Mouse interrupt handling
124
    uint8_t mouse_irq_bit = 12;
125
    int mouse_id = 0;
126
    int mouse_irq = BIT(mouse_irq_bit);
127 76 up20180655
128 87 up20180655
    if (subscribe_mouse_interrupt(mouse_irq_bit, &mouse_id)) return 1; // subscribes mouse interrupts in exclusive mode
129
    if (sys_irqdisable(&mouse_id)) return 1; // temporarily disables our interrupts notifications
130
    if (mouse_set_data_report(true)) return 1; // enables mouse data reporting
131
    if (sys_irqenable(&mouse_id)) return 1; // re-enables our interrupts notifications
132
133 74 up20180642
    /// cycle
134
    int good = 1;
135
    while (good) {
136
        /* Get a request message. */
137
        if ((r = driver_receive(ANY, &msg, &ipc_status)) != 0) {
138
            printf("driver_receive failed with %d", r);
139
            continue;
140
        }
141
        if (is_ipc_notify(ipc_status)) { /* received notification */
142
            switch (_ENDPOINT_P(msg.m_source)) {
143
                case HARDWARE: /* hardware interrupt notification */
144
                    if (msg.m_notify.interrupts & timer_irq) { /* subscribed interrupt */
145
                        timer_int_handler();
146
                        if (no_interrupts%frequency == 0) time++;
147
                        if(time >= idle_time) good = 0;
148
                    }
149
                    if (msg.m_notify.interrupts & mouse_irq) { /// subscribed interrupt
150
                        mouse_ih();
151 80 up20180642
                        if(counter_mouse_ih >= 3){
152
                            struct packet pp = mouse_parse_packet(packet_mouse_ih);
153 74 up20180642
                            mouse_print_packet(&pp);
154
                            time = 0;
155
                            no_interrupts = 0;
156
                        }
157
                    }
158
                    break;
159
                default:
160
                    break; /* no other notifications expected: do nothing */
161
            }
162
        } else { /* received standart message, not a notification */
163
            /* no standart message expected: do nothing */
164
        }
165
    }
166
167 87 up20180655
    if (sys_irqdisable(&mouse_id)) return 1; // temporarily disables our interrupts notifications
168
    if (mouse_set_data_report(false)) return 1; // enables mouse data reporting
169
    if (sys_irqenable(&mouse_id)) return 1; // re-enables our interrupts notifications
170
    if (unsubscribe_interrupt(&mouse_id)) return 1; // unsubscribes interrupts
171 74 up20180642
172
    if (unsubscribe_interrupt(&timer_id)) return 1;
173
174
    return 0;
175 53 up20180655
}
176
177 54 up20180642
int (mouse_test_gesture)(uint8_t x_len, uint8_t tolerance) {
178 79 up20180655
    int ret;
179 77 up20180642
    /// loop stuff
180
    int ipc_status, r;
181
    message msg;
182
    /// Mouse interrupt handling
183
    uint8_t mouse_irq_bit = 12;
184
    int mouse_id = 0;
185
    int mouse_irq = BIT(mouse_irq_bit);
186
187 87 up20180655
    if (subscribe_mouse_interrupt(mouse_irq_bit, &mouse_id)) return 1; // subscribes mouse interrupts in exclusive mode
188
    if (sys_irqdisable(&mouse_id)) return 1; // temporarily disables our interrupts notifications
189
    if ((ret = mouse_set_data_report(true))) return ret; // enables mouse data reporting
190
    if (sys_irqenable(&mouse_id)) return 1; // re-enables our interrupts notifications
191
192 77 up20180642
    /// cycle
193
    int good = 1;
194 78 up20180655
195
    // mouse event gesture
196
    struct mouse_ev *event;
197
    struct packet pp;
198
    int response;
199 77 up20180642
    while (good) {
200
        /* Get a request message. */
201
        if ((r = driver_receive(ANY, &msg, &ipc_status)) != 0) {
202
            printf("driver_receive failed with %d", r);
203
            continue;
204
        }
205
        if (is_ipc_notify(ipc_status)) { /* received notification */
206
            switch (_ENDPOINT_P(msg.m_source)) {
207
                case HARDWARE: /* hardware interrupt notification */
208
                    if (msg.m_notify.interrupts & mouse_irq) { /* subscribed interrupt */
209
                        mouse_ih();
210 80 up20180642
                        if(counter_mouse_ih >= 3) {
211
                            pp = mouse_parse_packet(packet_mouse_ih);
212 79 up20180655
                            mouse_print_packet(&pp);
213 78 up20180655
                            event = mouse_get_event(&pp);
214
215
                            response = state_machine(event, x_len, tolerance);
216
217
                            if (response == SUCCESS)
218
                                good = 0;
219
                            else if (response == INVALID_STATE)
220
                                return response;
221 77 up20180642
                        }
222
                    }
223
                    break;
224
                default:
225
                    break; /* no other notifications expected: do nothing */
226
            }
227
        } else { /* received standart message, not a notification */
228
            /* no standart message expected: do nothing */
229
        }
230
    }
231
232 87 up20180655
    if (sys_irqdisable(&mouse_id)) return 1; // temporarily disables our interrupts notifications
233
    if (mouse_set_data_report(false)) return 1; // enables mouse data reporting
234
    if (sys_irqenable(&mouse_id)) return 1; // re-enables our interrupts notifications
235
    if (unsubscribe_interrupt(&mouse_id)) return 1; // unsubscribes interrupts
236
237 77 up20180642
    return 0;
238 53 up20180655
}