Project

General

Profile

Statistics
| Revision:

root / lab3 / lab3.c @ 43

History | View | Annotate | Download (3.32 KB)

1 30 up20180655
#include <lcom/lcf.h>
2
#include <lcom/lab3.h>
3
4
#include <stdbool.h>
5
#include <stdint.h>
6
7
#include "kbc.h"
8
#include "kbc_func.h"
9
10
int main(int argc, char *argv[]) {
11
  // sets the language of LCF messages (can be either EN-US or PT-PT)
12
  lcf_set_language("EN-US");
13
14
  // enables to log function invocations that are being "wrapped" by LCF
15
  // [comment this out if you don't want/need it]
16
  lcf_trace_calls("/home/lcom/labs/lab3/trace.txt");
17
18
  // enables to save the output of printf function calls on a file
19
  // [comment this out if you don't want/need it]
20
  lcf_log_output("/home/lcom/labs/lab3/output.txt");
21
22
  // handles control over to LCF
23
  // [LCF handles command line arguments and invokes the right function]
24
  if (lcf_start(argc, argv))
25
    return 1;
26
27
  // LCF clean up tasks
28
  // [must be the last statement before return]
29
  lcf_cleanup();
30
31
  return 0;
32
}
33
34
extern uint8_t scancode[2];
35
extern int two_byte_scancode;
36 31 up20180655
extern int got_error;
37 42 up20180642
extern uint32_t sys_inb_counter;
38 30 up20180655
39
int(kbd_test_scan)() {
40
41
    int ipc_status, r;
42
    message msg;
43
44 37 up20180655
    uint8_t kbc_irq_bit = 1;
45 30 up20180655
    int kbc_id = 0;
46 37 up20180655
    int kbc_irq = BIT(kbc_irq_bit);
47 30 up20180655
48
    int got_esc_breakcode = 0;
49
50
    if (subscribe_kbc_interrupt(kbc_irq_bit, &kbc_id)) return 1;
51
52
    while (!got_esc_breakcode) {
53
        /* Get a request message. */
54
        if ((r = driver_receive(ANY, &msg, &ipc_status)) != 0) {
55
            printf("driver_receive failed with %d", r);
56
            continue;
57
        }
58
        if (is_ipc_notify(ipc_status)) { /* received notification */
59
            switch (_ENDPOINT_P(msg.m_source)) {
60
                case HARDWARE: /* hardware interrupt notification */
61 37 up20180655
                    if (msg.m_notify.interrupts & kbc_irq) { /* subscribed interrupt */
62 30 up20180655
63
                        kbc_ih();
64
65 31 up20180655
                        if (!(two_byte_scancode || got_error)) { /* finished processing a scancode */
66 30 up20180655
                            if (scancode[0] == TWO_BYTE_CODE)
67
                                kbd_print_scancode(!(scancode[1] & BREAK_CODE_BIT), 2, scancode);
68
                            else
69
                                kbd_print_scancode(!(scancode[0] & BREAK_CODE_BIT), 1, scancode);
70 31 up20180655
                        } else {
71
                            break;
72 30 up20180655
                        }
73
74
                        if (scancode[0] == ESC_BREAK_CODE)
75
                            got_esc_breakcode = 1;
76
77
                    }
78
                    break;
79
                default:
80
                    break; /* no other notifications expected: do nothing */
81
            }
82
        } else { /* received standart message, not a notification */
83
            /* no standart message expected: do nothing */
84
        }
85
    }
86
87
    if (unsubscribe_kbc_interrupt(&kbc_id)) return 1;
88
89 42 up20180642
    if (kbd_print_no_sysinb(sys_inb_counter)) return 1;
90 30 up20180655
91
    return 0;
92
}
93
94
int(kbd_test_poll)() {
95 36 up20180642
    uint8_t c[2]; uint8_t size;
96
    do{
97
        if(kbd_poll(c, &size)) return 1;
98
        if(kbd_print_scancode((~c[0])&BREAK_CODE_BIT, size, c)) return 1;
99
    }while(!(size == 1 && c[0] == ESC_BREAK_CODE));
100 38 up20180642
101 43 up20180642
    if(kbc_restore_kbd()) return 1;
102 39 up20180642
103 43 up20180642
    if(kbd_print_no_sysinb(sys_inb_counter)) return 1;
104 42 up20180642
105 36 up20180642
    return 0;
106 30 up20180655
}
107
108
int(kbd_test_timed_scan)(uint8_t n) {
109
  /* To be completed by the students */
110
  printf("%s is not yet implemented!\n", __func__);
111
112
  return 1;
113
}