Index: lab4/errors.h
===================================================================
--- lab4/errors.h	(revision 67)
+++ lab4/errors.h	(revision 68)
@@ -1,5 +1,5 @@
-#ifndef _ERRORS_H_
-#define _ERRORS_H_
+#ifndef ERRORS_H_INCLUDED
+#define ERRORS_H_INCLUDED
 
 enum errors {
     SUCCESS = 0,    /* @brief Sucessful */
@@ -6,9 +6,11 @@
     NULL_PTR,       /* @brief Null Pointer Error */
     LCF_ERROR,      /* @brief Error originated on LCF */
     SBCR_ERROR,     /* @brief Error on Subscribing Interrupt */
-    UNSBCR_ERROR,    /* @brief Error on Unsubscring Interrupt*/
+    UNSBCR_ERROR,   /* @brief Error on Unsubscring Interrupt*/
     READ_ERROR,     /* @brief Error on Reading from Port */
     WRITE_ERROR,    /* @brief Error on Writing to Port */
+    TIMEOUT_ERROR,  /* @brief Timeout error */
+    OTHER_ERROR,    /* @brief Unspecified error */
 };
 
-#endif /* end of include guard: _ERRORS_H_ */
+#endif //ERRORS_H_INCLUDED
Index: lab4/kbc.c
===================================================================
--- lab4/kbc.c	(revision 67)
+++ lab4/kbc.c	(revision 68)
@@ -3,54 +3,77 @@
 #include "kbc.h"
 
 #include "kbc_macros.h"
+#include "utils.h"
+#include "errors.h"
 
 int (unsubscribe_interrupt)(int *interrupt_id) {
-    if (interrupt_id == NULL) return 1;
-    return sys_irqrmpolicy(interrupt_id);
+    if (interrupt_id == NULL) return NULL_PTR;
+    if(sys_irqrmpolicy(interrupt_id)) return UNSBCR_ERROR;
+    return SUCCESS;
 }
 
 int (kbc_read_cmd)(uint8_t *cmd){
-    if(kbc_issue_cmd(READ_KBC_CMD)) return 1;
-    if(kbc_read_byte(cmd)) return 1;
-    return 0;
+    int ret = 0;
+    if((ret = kbc_issue_cmd(READ_KBC_CMD))) return ret;
+    if((ret = kbc_read_byte(cmd))) return ret;
+    return SUCCESS;
 }
 
 int (kbc_change_cmd)(uint8_t cmd){
-    if(kbc_issue_cmd(WRITE_KBC_CMD)) return 1;
-    if(sys_outb(KBC_CMD_ARG, cmd)) return 1;
-    return 0;
+    int ret = 0;
+    if((ret = kbc_issue_cmd(WRITE_KBC_CMD))) return ret;
+    if((ret = kbc_issue_arg(cmd))) return ret;
+    return SUCCESS;
 }
 
 int (kbc_restore_kbd)(){
+    int ret = 0;
     uint8_t cmd = 0;
-    if(kbc_read_cmd(&cmd)) return 1;
+    if((ret = kbc_read_cmd(&cmd))) return ret;
     cmd = (cmd | INT_KBD) & (~DIS_KBD) ;
-    if(kbc_change_cmd(cmd)) return 1;
-    return 0;
+    if((ret = kbc_change_cmd(cmd))) return ret;
+    return SUCCESS;
 }
 
 int (kbc_issue_cmd)(uint8_t cmd){
+    int ret = 0;
     uint8_t stat;
     for(int i = 0; i < KBC_NUM_TRIES; ++i){
-        if(util_sys_inb(STATUS_REG, &stat)) return 1;
+        if((ret = util_sys_inb(STATUS_REG, &stat))) return ret;
         if((stat&IN_BUF_FULL) == 0){
-            if(sys_outb(KBC_CMD, cmd)) return 1;
-            return 0;
+            if(sys_outb(KBC_CMD, cmd)) return WRITE_ERROR;
+            return SUCCESS;
         }
         tickdelay(micros_to_ticks(DELAY));
     }
-    return 1;
+    return TIMEOUT_ERROR;
 }
 
+int (kbc_issue_arg)(uint8_t arg){
+    int ret = 0;
+    uint8_t stat;
+    for(int i = 0; i < KBC_NUM_TRIES; ++i){
+        if((ret = util_sys_inb(STATUS_REG, &stat))) return ret;
+        if((stat&IN_BUF_FULL) == 0){
+            if(sys_outb(KBC_CMD_ARG, arg)) return WRITE_ERROR;
+            return SUCCESS;
+        }
+        tickdelay(micros_to_ticks(DELAY));
+    }
+    return TIMEOUT_ERROR;
+}
+
 int (kbc_read_byte)(uint8_t *byte){
+    int ret = 0;
     uint8_t stat;
-    while(true){
-        if(util_sys_inb(STATUS_REG, &stat)) return 1;
+    for(int i = 0; i < KBC_NUM_TRIES; ++i){
+        if((ret = util_sys_inb(STATUS_REG, &stat))) return ret;
         if((stat&OUT_BUF_FUL) && (stat&AUX_MOUSE)==0){
             if(stat & (PARITY_ERROR | TIME_OUT_REC)) return 1;
-            if(util_sys_inb(OUTPUT_BUF, byte)) return 1;
-            else return 0;
+            if((ret = util_sys_inb(OUTPUT_BUF, byte))) return ret;
+            else return SUCCESS;
         }
         tickdelay(micros_to_ticks(DELAY));
     }
+    return TIMEOUT_ERROR;
 }
Index: lab4/kbc.h
===================================================================
--- lab4/kbc.h	(revision 67)
+++ lab4/kbc.h	(revision 68)
@@ -1,19 +1,11 @@
-#ifndef _KBC_FUNC_H_
-#define _KBC_FUNC_H_
-
-#include <stdint.h>
-
-#define GET         0 /* @brief Argument to get counter without incrementing */
-#define INCREMENT   1 /* @brief Argument for incrementing counter */
-
 /**
- * @brief Subscribes KBC Interrupts and disables Minix Default Keyboard IH
- * @param interrupt_bit Bit of Interrupt Vector that will be set when KBC Interrupt is pending
- * @param interrupt_id KBC Interrupt ID to specify the KBC Interrupt in other calls
- * @return Whether operation was sucessful or not
+ * This file concerns everything related to the KBC (KeyBoard Controller, which
+ * actually also manages the mouse)
  */
-int (subscribe_kbc_interrupt)(uint8_t interrupt_bit, int *interrupt_id);
 
+#ifndef KBC_H_INCLUDED
+#define KBC_H_INCLUDED
+
 /**
  * @brief Unsubcribes Interrupts
  * @param interrupt_id Interrupt ID, value via arguments on subscription of the interrupt_id
@@ -23,21 +15,6 @@
 int (unsubscribe_interrupt)(int *interrupt_id);
 
 /**
- * @brief KBC Interrupt Handler
- */
-void (kbc_ih)(void);
-
-/**
- * @brief High-level function that polls keyboard for scancode
- * High-level function that polls keyboard for scancode of up to 2 bytes. If
- * scancode has only 1 byte, the second byte is set to 0x00.
- * @param bytes Array of at least 2 bytes to store scancode
- * @param size Size of scancode in bytes
- * @return 0 if operation was successful, 1 otherwise
- */
-int (kbd_poll)(uint8_t bytes[], uint8_t *size);
-
-/**
  * @brief High-level function that reads the command byte of the KBC
  * @param cmd Pointer to variable where command byte read from KBC will be stored
  * @return 0 if operation was successful, 1 otherwise
@@ -58,7 +35,7 @@
  * chance that the keyboard and keyboard interrupts remain disabled.
  * @return 1 if operation was successful, 1 otherwise
  */
-int (kbc_restore_kbd)();
+int (kbc_restore_keyboard)();
 
 /**
  * @brief Low-level function to issue a command to keyboard
@@ -68,6 +45,13 @@
 int (kbc_issue_cmd)(uint8_t cmd);
 
 /**
+ * @brief Low-level function to issue an argument of a command
+ * @param cmd argument to be issued
+ * @return 0 if operation was successful, 1 otherwise
+ */
+int (kbc_issue_arg)(uint8_t arg);
+
+/**
  * @brief Low-level function for reading byte from keyboard
  * Low-level function for reading byte from keyboard. Waits until output buffer
  * is full
@@ -76,5 +60,4 @@
  */
 int (kbc_read_byte)(uint8_t *byte);
 
-
-#endif
+#endif //KBC_H_INCLUDED
Index: lab4/kbc_macros.h
===================================================================
--- lab4/kbc_macros.h	(revision 67)
+++ lab4/kbc_macros.h	(revision 68)
@@ -1,5 +1,5 @@
-#ifndef _KBC_H_
-#define _KBC_H_
+#ifndef KBC_MACROS_H_INCLUDED
+#define KBC_MACROS_H_INCLUDED
 
 /* KBC IRQ Line */
 
@@ -53,4 +53,4 @@
 #define DIS_KBD         BIT(4)  /* @brief Disable Keyboard */
 #define DIS_MOU         BIT(5)  /* @brief Disable Mouse */
 
-#endif
+#endif //KBC_MACROS_H_INCLUDED
Index: lab4/keyboard.c
===================================================================
--- lab4/keyboard.c	(nonexistent)
+++ lab4/keyboard.c	(revision 68)
@@ -0,0 +1,57 @@
+#include <lcom/lcf.h>
+
+#include "keyboard.h"
+
+#include "kbc.h"
+#include "kbc_macros.h"
+#include "utils.h"
+#include "errors.h"
+
+int (subscribe_kbc_interrupt)(uint8_t interrupt_bit, int *interrupt_id) {
+    if (interrupt_id == NULL) return NULL_PTR;
+    *interrupt_id = interrupt_bit;
+    if(sys_irqsetpolicy(KBC_IRQ, IRQ_REENABLE | IRQ_EXCLUSIVE, interrupt_id)) return SBCR_ERROR;
+    return SUCCESS;
+}
+
+int done = 1;
+int sz = 1;
+int got_error_keyboard = SUCCESS;
+
+void (kbc_ih)(void) {
+    if(done) sz = 1;
+    else     sz++;
+    uint8_t status = 0;
+    got_error_keyboard = SUCCESS;
+    if ((got_error_keyboard = util_sys_inb(STATUS_REG, &status))) return;
+    if (status & (TIME_OUT_REC | PARITY_ERROR)) {
+        got_error_keyboard = 1;
+        return;
+    }
+    if ((status & OUT_BUF_FUL) == 0 || (status & AUX_MOUSE) != 0) {
+        got_error_keyboard = OTHER_ERROR;
+        return;
+    }
+    uint8_t byte = 0;
+    if ((got_error_keyboard = util_sys_inb(OUTPUT_BUF, &byte))) return;
+
+    scancode[sz-1] = byte;
+    done = !(TWO_BYTE_CODE == byte);
+}
+
+int (keyboard_poll)(uint8_t bytes[], uint8_t *size){
+    int ret = 0;
+    if(bytes == NULL || size == NULL) return NULL_PTR;
+    uint8_t c;
+    if((ret = kbc_read_byte(&c))) return ret;
+    if(c == TWO_BYTE_CODE){
+        if((ret = kbc_read_byte(&bytes[1]))) return ret;
+        bytes[0] = c;
+        *size = 2;
+    }else{
+        bytes[1] = 0;
+        bytes[0] = c;
+        *size = 1;
+    }
+    return SUCCESS;
+}

Property changes on: lab4/keyboard.c
___________________________________________________________________
Added: svn:executable
## -0,0 +1 ##
+*
\ No newline at end of property
Index: lab4/keyboard.h
===================================================================
--- lab4/keyboard.h	(nonexistent)
+++ lab4/keyboard.h	(revision 68)
@@ -0,0 +1,19 @@
+/**
+ * This file concerns everything related to the keyboard
+ */
+
+#ifndef KEYBOARD_H_INCLUDED
+#define KEYBOARD_H_INCLUDED
+
+int (subscribe_kbc_interrupt)(uint8_t interrupt_bit, int *interrupt_id);
+
+uint8_t scancode[2];
+int done;
+int sz;
+int got_error_keyboard;
+
+void (kbc_ih)(void);
+
+int (keyboard_poll)(uint8_t bytes[], uint8_t *size);
+
+#endif //KEYBOARD_H_INCLUDED

Property changes on: lab4/keyboard.h
___________________________________________________________________
Added: svn:executable
## -0,0 +1 ##
+*
\ No newline at end of property
Index: lab4/mouse.c
===================================================================
--- lab4/mouse.c	(revision 67)
+++ lab4/mouse.c	(revision 68)
@@ -23,22 +23,16 @@
     got_error_mouse_ih = 0;
     if(counter >= 3) counter = 0;
 
-    if (util_sys_inb(STATUS_REG, &status)) {
-        got_error_mouse_ih = 1;
-        return;
-    }
+    if ((got_error_mouse_ih = util_sys_inb(STATUS_REG, &status))) return;
 
     if (status & (TIME_OUT_REC | PARITY_ERROR)) {
-        got_error_mouse_ih = 1;
+        got_error_mouse_ih = OTHER_ERROR;
         return;
     }
 
     uint8_t byte = 0;
 
-    if (util_sys_inb(OUTPUT_BUF, &byte)) {
-        got_error_mouse_ih = 1;
-        return;
-    }
+    if ((got_error_mouse_ih = util_sys_inb(OUTPUT_BUF, &byte))) return;
 
     /// This does not run if: I was expecting the first one but what I get is definitely not the first byte
     if((byte & FIRST_BYTE_ID)  || counter){
@@ -65,6 +59,6 @@
 int (mouse_set_data_report)(int on){
     int ret = 0;
     if((ret = kbc_issue_cmd(MOUSE_WRITE_B))) return ret;
-    if(sys_outb(KBC_CMD_ARG, DIS_DATA_REP)) return WRITE_ERROR;
+    if((ret = kbc_issue_arg(DIS_DATA_REP))) return ret;
     return SUCCESS;
 }
Index: lab4/mouse.h
===================================================================
--- lab4/mouse.h	(revision 67)
+++ lab4/mouse.h	(revision 68)
@@ -1,5 +1,5 @@
-#ifndef _MOUSE_FUNC_H_
-#define _MOUSE_FUNC_H_
+#ifndef MOUSE_H_INCLUDED
+#define MOUSE_H_INCLUDED
 
 #include <stdint.h>
 
@@ -16,4 +16,4 @@
 
 int (mouse_set_data_report)(int on);
 
-#endif /* end of include guard: _MOUSE_FUNC_H_ */
+#endif //MOUSE_H_INCLUDED
Index: lab4/mouse_macros.h
===================================================================
--- lab4/mouse_macros.h	(revision 67)
+++ lab4/mouse_macros.h	(revision 68)
@@ -1,5 +1,5 @@
-#ifndef _MOUSE_H_
-#define _MOUSE_H_
+#ifndef MOUSE_MACROS_H_INCLUDED
+#define MOUSE_MACROS_H_INCLUDED
 
 /* Mouse Data Packet */
 // Byte 0 - Button States
@@ -34,4 +34,4 @@
 #define ACK_INVALID 0xFE /* @brief Invalid Byte, first occurence */
 #define ACK_ERROR   0xFC /* @brief Invalid Byte on resend */
 
-#endif // _MOUSE_H_
+#endif // MOUSE_MACROS_H_INCLUDED
Index: lab4/utils.c
===================================================================
--- lab4/utils.c	(revision 67)
+++ lab4/utils.c	(revision 68)
@@ -1,17 +1,20 @@
 #include <lcom/lcf.h>
 
+#include "utils.h"
+
 #include <stdint.h>
+#include "errors.h"
 
 int(util_get_LSB)(uint16_t val, uint8_t *lsb) {
-    if (lsb == NULL) return 1;
+    if (lsb == NULL) return NULL_PTR;
     *lsb = val;
-    return 0;
+    return SUCCESS;
 }
 
 int(util_get_MSB)(uint16_t val, uint8_t *msb) {
-    if (msb == NULL) return 1;
+    if (msb == NULL) return NULL_PTR;
     *msb = (val >> 8);
-    return 0;
+    return SUCCESS;
 }
 
 #ifdef LAB3
@@ -19,12 +22,12 @@
 #endif
 
 int (util_sys_inb)(int port, uint8_t *value) {
-    if(value == NULL) return 1;
+    if(value == NULL) return NULL_PTR;
     uint32_t n = 0;
-    if(sys_inb(port, &n)) return 1;
+    if(sys_inb(port, &n)) return READ_ERROR;
     *value = n;
     #ifdef LAB3
         ++sys_inb_counter;
     #endif
-    return 0;
+    return SUCCESS;
 }
Index: lab4/utils.h
===================================================================
--- lab4/utils.h	(nonexistent)
+++ lab4/utils.h	(revision 68)
@@ -0,0 +1,14 @@
+#ifndef UTILS_H_INCLUDED
+#define UTILS_H_INCLUDED
+
+int(util_get_LSB)(uint16_t val, uint8_t *lsb);
+
+int(util_get_MSB)(uint16_t val, uint8_t *msb);
+
+#ifdef LAB3
+    uint32_t sys_inb_counter;
+#endif
+
+int (util_sys_inb)(int port, uint8_t *value);
+
+#endif //UTILS_H_INCLUDED

Property changes on: lab4/utils.h
___________________________________________________________________
Added: svn:executable
## -0,0 +1 ##
+*
\ No newline at end of property
