Project

General

Profile

Statistics
| Revision:

root / lab4 / lab4.c @ 229

History | View | Annotate | Download (9.04 KB)

1
#include <lcom/lcf.h>
2

    
3
#include <stdint.h>
4
#include <stdio.h>
5

    
6
#include "mouse.h"
7
#include "kbc.h"
8
#include "errors.h"
9
#include "mouse_macros.h"
10
#include "kbc_macros.h"
11
#include "timer.h"
12
#include "state_machine.h"
13

    
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
    /// loop stuff
40
    int ipc_status, r;
41
    message msg;
42
    /// Mouse interrupt handling
43
    uint8_t mouse_irq_bit = 12;
44
    int mouse_id = 0;
45
    int mouse_irq = BIT(mouse_irq_bit);
46

    
47
    if (subscribe_mouse_interrupt(mouse_irq_bit, &mouse_id)) return 1; // subscribes mouse interrupts in exclusive mode
48
    if (sys_irqdisable(&mouse_id)) return 1; // temporarily disables our interrupts notifications
49
    if (mouse_set_data_report(true)) return 1; // enables mouse data reporting
50
    if (sys_irqenable(&mouse_id)) return 1; // re-enables our interrupts notifications
51

    
52
    /// cycle
53
    int good = (cnt != 0);
54
    uint32_t cnt_now = 0;
55
    while (good) {
56
        /* Get a request message. */
57
        if ((r = driver_receive(ANY, &msg, &ipc_status)) != 0) {
58
            printf("driver_receive failed with %d", r);
59
            continue;
60
        }
61
        if (is_ipc_notify(ipc_status)) { /* received notification */
62
            switch (_ENDPOINT_P(msg.m_source)) {
63
                case HARDWARE: /* hardware interrupt notification */
64
                    if (msg.m_notify.interrupts & mouse_irq) { /* subscribed interrupt */
65
                        mouse_ih();
66
                        if(counter_mouse_ih >= 3){
67
                            struct packet pp = mouse_parse_packet(packet_mouse_ih);
68
                            mouse_print_packet(&pp);
69
                            cnt_now++;
70
                            if(cnt == cnt_now) good = 0;
71
                        }
72
                    }
73
                    break;
74
                default:
75
                    break; /* no other notifications expected: do nothing */
76
            }
77
        } else { /* received standart message, not a notification */
78
            /* no standart message expected: do nothing */
79
        }
80
    }
81
    if (sys_irqdisable(&mouse_id)) return 1; // temporarily disables our interrupts notifications
82
    if (mouse_set_data_report(false)) return 1; // enables mouse data reporting
83
    if (sys_irqenable(&mouse_id)) return 1; // re-enables our interrupts notifications
84
    if (unsubscribe_interrupt(&mouse_id)) return 1; // unsubscribes interrupts
85
    return 0;
86
}
87

    
88
int (mouse_test_remote)(uint16_t period, uint8_t cnt) {
89
    //if (mouse_issue_cmd(SET_REMOTE_MD)) return 1;
90
    //if (mouse_set_data_report(true)) return 1;
91

    
92
    struct packet pp;
93
    while(cnt--){
94
        if(mouse_poll(&pp, period)) return 1;
95
        mouse_print_packet(&pp);
96
        tickdelay(micros_to_ticks(period*1000));
97
    }
98
    // Set Stream mode
99
    if (mouse_issue_cmd(SET_STREAM_MD)) return 1;
100
    // Disable data reporting
101
    if (mouse_set_data_report(false)) return 1;
102
    uint8_t cmd_byte = minix_get_dflt_kbc_cmd_byte();
103
    if (kbc_change_cmd(cmd_byte)) return 1;
104

    
105
    return SUCCESS;
106
}
107

    
108
int (mouse_test_async)(uint8_t idle_time) {
109
    /// loop stuff
110
    int ipc_status, r;
111
    message msg;
112
    /// Timer interrupt handling
113
    const uint32_t frequency = sys_hz(); // Frequency asummed at 60Hz
114
    uint8_t timer_irq_bit = 0;
115
    int timer_id = 0;
116
    int timer_irq = BIT(timer_irq_bit);
117
    if(subscribe_timer_interrupt(timer_irq_bit, &timer_id)) return 1;
118

    
119
    no_interrupts = 0;
120
    int time = 0;
121
    /// Mouse interrupt handling
122
    uint8_t mouse_irq_bit = 12;
123
    int mouse_id = 0;
124
    int mouse_irq = BIT(mouse_irq_bit);
125

    
126
    if (subscribe_mouse_interrupt(mouse_irq_bit, &mouse_id)) return 1; // subscribes mouse interrupts in exclusive mode
127
    if (sys_irqdisable(&mouse_id)) return 1; // temporarily disables our interrupts notifications
128
    if (mouse_set_data_report(true)) return 1; // enables mouse data reporting
129
    if (sys_irqenable(&mouse_id)) return 1; // re-enables our interrupts notifications
130

    
131
    /// cycle
132
    int good = 1;
133
    while (good) {
134
        /* Get a request message. */
135
        if ((r = driver_receive(ANY, &msg, &ipc_status)) != 0) {
136
            printf("driver_receive failed with %d", r);
137
            continue;
138
        }
139
        if (is_ipc_notify(ipc_status)) { /* received notification */
140
            switch (_ENDPOINT_P(msg.m_source)) {
141
                case HARDWARE: /* hardware interrupt notification */
142
                    if (msg.m_notify.interrupts & timer_irq) { /* subscribed interrupt */
143
                        timer_int_handler();
144
                        if (no_interrupts%frequency == 0) time++;
145
                        if(time >= idle_time) good = 0;
146
                    }
147
                    if (msg.m_notify.interrupts & mouse_irq) { /// subscribed interrupt
148
                        mouse_ih();
149
                        if(counter_mouse_ih >= 3){
150
                            struct packet pp = mouse_parse_packet(packet_mouse_ih);
151
                            mouse_print_packet(&pp);
152
                            time = 0;
153
                            no_interrupts = 0;
154
                        }
155
                    }
156
                    break;
157
                default:
158
                    break; /* no other notifications expected: do nothing */
159
            }
160
        } else { /* received standart message, not a notification */
161
            /* no standart message expected: do nothing */
162
        }
163
    }
164

    
165
    if (sys_irqdisable(&mouse_id)) return 1; // temporarily disables our interrupts notifications
166
    if (mouse_set_data_report(false)) return 1; // enables mouse data reporting
167
    if (sys_irqenable(&mouse_id)) return 1; // re-enables our interrupts notifications
168
    if (unsubscribe_interrupt(&mouse_id)) return 1; // unsubscribes interrupts
169

    
170
    if (unsubscribe_interrupt(&timer_id)) return 1;
171

    
172
    return 0;
173
}
174

    
175
int (mouse_test_gesture)(uint8_t x_len, uint8_t tolerance) {
176
    /// loop stuff
177
    int ipc_status, r;
178
    message msg;
179
    /// Mouse interrupt handling
180
    uint8_t mouse_irq_bit = 12;
181
    int mouse_id = 0;
182
    int mouse_irq = BIT(mouse_irq_bit);
183

    
184
    if (subscribe_mouse_interrupt(mouse_irq_bit, &mouse_id)) return 1; // subscribes mouse interrupts in exclusive mode
185
    if (sys_irqdisable(&mouse_id)) return 1; // temporarily disables our interrupts notifications
186
    if (mouse_set_data_report(true)) return 1; // enables mouse data reporting
187
    if (sys_irqenable(&mouse_id)) return 1; // re-enables our interrupts notifications
188

    
189
    /// cycle
190
    int good = 1;
191

    
192
    // mouse event gesture
193
    struct mouse_ev *event;
194
    struct packet pp;
195
    int response;
196
    while (good) {
197
        /* Get a request message. */
198
        if ((r = driver_receive(ANY, &msg, &ipc_status)) != 0) {
199
            printf("driver_receive failed with %d", r);
200
            continue;
201
        }
202
        if (is_ipc_notify(ipc_status)) { /* received notification */
203
            switch (_ENDPOINT_P(msg.m_source)) {
204
                case HARDWARE: /* hardware interrupt notification */
205
                    if (msg.m_notify.interrupts & mouse_irq) { /* subscribed interrupt */
206
                        mouse_ih();
207
                        if(counter_mouse_ih >= 3) {
208
                            pp = mouse_parse_packet(packet_mouse_ih);
209
                            mouse_print_packet(&pp);
210
                            event = mouse_get_event(&pp);
211

    
212
                            response = state_machine(event, x_len, tolerance);
213

    
214
                            if (response == SUCCESS)
215
                                good = 0;
216
                            else if (response == INVALID_STATE)
217
                                return response;
218
                        }
219
                    }
220
                    break;
221
                default:
222
                    break; /* no other notifications expected: do nothing */
223
            }
224
        } else { /* received standart message, not a notification */
225
            /* no standart message expected: do nothing */
226
        }
227
    }
228

    
229
    if (sys_irqdisable(&mouse_id)) return 1; // temporarily disables our interrupts notifications
230
    if (mouse_set_data_report(false)) return 1; // enables mouse data reporting
231
    if (sys_irqenable(&mouse_id)) return 1; // re-enables our interrupts notifications
232
    if (unsubscribe_interrupt(&mouse_id)) return 1; // unsubscribes interrupts
233

    
234
    return 0;
235
}