Project

General

Profile

Statistics
| Revision:

root / lab3 / lab3.c @ 11

History | View | Annotate | Download (4.83 KB)

1

    
2
#include <lcom/lcf.h>
3
#include <lcom/lab3.h>
4
#include <stdbool.h>
5
#include <stdint.h>
6

    
7
#include "keyboard.h"
8

    
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 scan_code;
35
extern uint32_t cnt;
36

    
37
int(kbd_test_scan)() {
38
  int r, ipc_status;
39
  uint8_t keyboard_irq, size, bytes[2]; //size of scancode can be 2 byte long
40
  message msg;
41
  bool make=false; //to check if code is make or break
42

    
43
  if(kbc_subscribe_int(&keyboard_irq)==1){
44
    printf("Error subscribing int\n");
45
    return 1;
46
  }
47

    
48
  while(scan_code!=ESC_BREAK){      //looping until scan_code is the breakcode of ESC key
49
    if ((r = driver_receive(ANY, &msg, &ipc_status))==1){ 
50
      printf("driver_receive failed with: %d",r);
51
      continue;
52
    }
53
    if (is_ipc_notify(ipc_status)){                             //received notification
54
      switch (_ENDPOINT_P(msg.m_source)){
55
        case HARDWARE:                                          //hardware interrupt notification
56
                if (msg.m_notify.interrupts &keyboard_irq){          // subscribed interrupt
57
                  
58
                  kbc_ih();
59
                  bytes[0]=scan_code;
60
                  bytes[1]=scan_code;
61

    
62
                  if(bytes[0]==TWO_BYTE_SCANCODE){
63
                    size=2;
64
                  }
65
                  else{
66
                    size=1;
67
                  }
68

    
69
                  if (scan_code & MAKE_CODE){        //checks if code is make or break
70
                      make = false;        //break code
71
                    }
72
                  else {
73
                    make = true;  //make code
74
                  }
75

    
76
                  if(kbd_print_scancode(make, size, bytes)!=0){ //prints the code
77
                    printf("Error printing scan code\n");
78
                    return 1;
79
                  }
80
                      }
81
               break;
82
        default:
83
              break;                                            //no other notifications expected: do nothing        
84
        }
85
    } 
86
    else {                                                      //received a standard message, not a notification
87
      //no standard messages expected: do nothing
88
    }
89
  }
90

    
91
   if (kbc_unsubscribe_int() != 0) { // Check if subscription worked
92
                printf("Error unsubscribing int \n");
93
                return 1;
94
        }
95

    
96
  if(kbd_print_no_sysinb(cnt)!=0){
97
    printf("Error printing number of sys used\n");
98
    return 1;
99
  }
100

    
101
  cnt=0;
102
  scan_code=0;
103

    
104
  return 0;
105
}
106

    
107
int(kbd_test_poll)() {
108
  uint8_t status, size, bytes[2]; //size of scancode can be 2 byte long
109
  bool make=false; //to check if code is make or break
110

    
111
  while(scan_code!=ESC_BREAK){      //looping until scan_code is the breakcode of ESC key, can't do interruptions
112
    if(util_sys_inb(STATUS_REG, &status)!=0){     //checks the status register
113
      printf("Error reading status register\n");
114
      return 1;
115
    }                                        
116

    
117
                if ((status & STAT_REG_OBF) && !(status & STAT_REG_AUX) && !(status & STAT_REG_PAR || status & STAT_REG_TIMEOUT)) {      //checks if there is no parity ot timeout error, if the output buffer is full and if there is mouse data
118

    
119
                        if(util_sys_inb(OUTPUT_BUF, &scan_code)!=0){
120
        printf("Error reading output buffer\n");
121
      return 1;
122
      }
123

    
124
                        bytes[0]=scan_code;
125
      bytes[1]=scan_code;
126

    
127
      if(bytes[0]==TWO_BYTE_SCANCODE){
128
        size=2;
129
      }
130
      else{
131
        size=1;
132
      }
133

    
134
      if (scan_code & MAKE_CODE){                   //checks if code is make or break
135
          make = false;                             //break code
136
        }
137
      else {
138
        make = true;                         //make code
139
      }
140

    
141
      if(kbd_print_scancode(make, size, bytes)!=0){ //prints the code
142
        printf("Error printing scan code\n");
143
        return 1;
144
      }
145
        
146
                }
147
                else {                                                                                                                                                 //if the output buffer is not full wait
148
                        tickdelay(micros_to_ticks(DELAY_US));                //makes keyboard respond to a command in 20 ms
149
                }
150
  }
151

    
152
  if(kbd_print_no_sysinb(cnt)!=0){
153
    printf("Error printing number of sys used\n");
154
    return 1;
155
  }
156

    
157
  if(enable_interrupt()!=0){
158
    printf("Coulnd re-enable the interruptions\n");
159
    return 1;
160

    
161
  }
162
  cnt=0;
163
  scan_code=0;
164

    
165
   return 0;
166
}
167

    
168
int(kbd_test_timed_scan)(uint8_t n) {
169
  /* To be completed by the students */
170
  printf("%s is not yet implemented!\n", __func__);
171

    
172
  return 1;
173
}