Project

General

Profile

Statistics
| Revision:

root / lab4 / lab4.c @ 78

History | View | Annotate | Download (8.26 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
extern uint8_t packet[3];
39
extern int counter;
40

    
41
int (mouse_test_packet)(uint32_t cnt) {
42
    int ret = 0;
43
    /// loop stuff
44
    int ipc_status, r;
45
    message msg;
46
    /// Mouse interrupt handling
47
    uint8_t mouse_irq_bit = 12;
48
    int mouse_id = 0;
49
    int mouse_irq = BIT(mouse_irq_bit);
50
    if ((ret = mouse_set_data_report(true))) return ret;
51
    //if ((ret = mouse_enable_data_reporting())) return ret;
52

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

    
88
int (mouse_test_remote)(uint16_t period, uint8_t cnt) {
89

    
90
    // Mouse packets data
91
    uint8_t packet[3];
92
    int sz = 0;
93
    uint8_t data = 0;
94
    // Cycle
95
    int packetCounter = 0;
96
    int good = 1;
97
    // return value
98
    int ret;
99

    
100
    if ((ret = mouse_issue_cmd(SET_REMOTE_MD))) return ret;
101

    
102
    if ((ret = mouse_set_data_report(true))) return ret;
103

    
104
    uint8_t cmd = 0;
105
    if ((ret = kbc_read_cmd(&cmd))) return ret;
106

    
107
    while (good) {
108

    
109
        if ((ret = mouse_read_data(&data))) return ret;
110

    
111
        if ((data & FIRST_BYTE_ID) || sz) {
112
            packet[sz] = data;
113
            sz++;
114
        }
115
        if (sz == 3) {
116
            struct packet pp = mouse_parse_packet(packet);
117
            mouse_print_packet(&pp);
118
            packetCounter++;
119
            sz = 0;
120
            if (packetCounter == cnt) good = 0;
121
        }
122
        tickdelay(micros_to_ticks(period*1e3));
123
    }
124

    
125
    // Set Stream mode
126
    if ((ret = mouse_issue_cmd(SET_STREAM_MD))) return ret;
127
    // Disable data reporting
128
    if ((ret = mouse_set_data_report(false))) return ret;
129
    uint8_t cmd_byte = minix_get_dflt_kbc_cmd_byte();
130
    if ((ret = kbc_change_cmd(cmd_byte))) return ret;
131

    
132
    return SUCCESS;
133
}
134

    
135
int (mouse_test_async)(uint8_t idle_time) {
136
    /// loop stuff
137
    int ipc_status, r;
138
    message msg;
139
    /// Timer interrupt handling
140
    const uint32_t frequency = sys_hz(); // Frequency asummed at 60Hz
141
    uint8_t timer_irq_bit = 0;
142
    int timer_id = 0;
143
    int timer_irq = BIT(timer_irq_bit);
144
    if(subscribe_timer_interrupt(timer_irq_bit, &timer_id)) return 1;
145

    
146
    no_interrupts = 0;
147
    int time = 0;
148
    /// Mouse interrupt handling
149
    uint8_t mouse_irq_bit = 12;
150
    int mouse_id = 0;
151
    int mouse_irq = BIT(mouse_irq_bit);
152
    if (mouse_set_data_report(true)) return 1;
153

    
154
    if (subscribe_mouse_interrupt(mouse_irq_bit, &mouse_id)) return 1;
155
    /// cycle
156
    int good = 1;
157
    while (good) {
158
        /* Get a request message. */
159
        if ((r = driver_receive(ANY, &msg, &ipc_status)) != 0) {
160
            printf("driver_receive failed with %d", r);
161
            continue;
162
        }
163
        if (is_ipc_notify(ipc_status)) { /* received notification */
164
            switch (_ENDPOINT_P(msg.m_source)) {
165
                case HARDWARE: /* hardware interrupt notification */
166
                    if (msg.m_notify.interrupts & timer_irq) { /* subscribed interrupt */
167
                        timer_int_handler();
168
                        if (no_interrupts%frequency == 0) time++;
169
                        if(time >= idle_time) good = 0;
170
                    }
171
                    if (msg.m_notify.interrupts & mouse_irq) { /// subscribed interrupt
172
                        mouse_ih();
173
                        if(counter >= 3){
174
                            struct packet pp = mouse_parse_packet(packet);
175
                            mouse_print_packet(&pp);
176
                            time = 0;
177
                            no_interrupts = 0;
178
                        }
179
                    }
180
                    break;
181
                default:
182
                    break; /* no other notifications expected: do nothing */
183
            }
184
        } else { /* received standart message, not a notification */
185
            /* no standart message expected: do nothing */
186
        }
187
    }
188

    
189
    if (unsubscribe_interrupt(&mouse_id)) return 1;
190
    if (mouse_set_data_report(false)) return 1;
191

    
192
    if (unsubscribe_interrupt(&timer_id)) return 1;
193

    
194
    return 0;
195
}
196

    
197
int (mouse_test_gesture)(uint8_t x_len, uint8_t tolerance) {
198
    /// loop stuff
199
    int ipc_status, r;
200
    message msg;
201
    /// Mouse interrupt handling
202
    uint8_t mouse_irq_bit = 12;
203
    int mouse_id = 0;
204
    int mouse_irq = BIT(mouse_irq_bit);
205
    //if ((ret = mouse_set_data_report(true))) return ret;
206
    if (mouse_enable_data_reporting()) return 1;
207

    
208
    if (subscribe_mouse_interrupt(mouse_irq_bit, &mouse_id)) return 1;
209
    /// cycle
210
    int good = 1;
211

    
212
    // mouse event gesture
213
    struct mouse_ev *event;
214
    struct packet pp;
215
    int response;
216

    
217
    while (good) {
218
        /* Get a request message. */
219
        if ((r = driver_receive(ANY, &msg, &ipc_status)) != 0) {
220
            printf("driver_receive failed with %d", r);
221
            continue;
222
        }
223
        if (is_ipc_notify(ipc_status)) { /* received notification */
224
            switch (_ENDPOINT_P(msg.m_source)) {
225
                case HARDWARE: /* hardware interrupt notification */
226
                    if (msg.m_notify.interrupts & mouse_irq) { /* subscribed interrupt */
227
                        mouse_ih();
228
                        if(counter >= 3) {
229
                            pp = mouse_parse_packet(packet);
230
                            //mouse_print_packet(&pp);
231
                            event = mouse_get_event(&pp);
232

    
233
                            response = state_machine(event, x_len, tolerance);
234

    
235
                            if (response == SUCCESS)
236
                                good = 0;
237
                            else if (response == INVALID_STATE)
238
                                return response;
239
                        }
240
                    }
241
                    break;
242
                default:
243
                    break; /* no other notifications expected: do nothing */
244
            }
245
        } else { /* received standart message, not a notification */
246
            /* no standart message expected: do nothing */
247
        }
248
    }
249
    if (unsubscribe_interrupt(&mouse_id)) return 1;
250
    if (mouse_set_data_report(false)) return 1;
251

    
252
    return 0;
253
}