Index: lab4/lab4.c
===================================================================
--- lab4/lab4.c	(revision 15)
+++ lab4/lab4.c	(revision 16)
@@ -4,6 +4,7 @@
 #include <stdint.h>
 #include <stdio.h>
 #include "mouse.h"
+#include "timer.h"
 
 // Any header files included below this line should have been created by you
 
@@ -32,73 +33,178 @@
 }
 
 extern uint8_t byte;
+extern int mouse_id;
 
 int (mouse_test_packet)(uint32_t cnt) {
   int r, ipc_status;
-  uint8_t mouse_irq, size=0; //size of mouse packet is 3 byte long
+  uint8_t mouse_irq;
+  unsigned int size=0; //size of mouse packet is 3 byte long
   message msg;
-  uint32_t counter_copy=cnt;
+  uint32_t counter_pp=0;
 
   struct packet pp;
 
-  if(mouse_enable_data_reporting()!=0){     //check if disabling stream mode worked
+  
+  //mouse_enable_data_reporting();
+  
+  //sys_irqdisable(&mouse_id);
+  if(write_cmd_mouse(0xF4)!=0){     //check if disabling stream mode worked (0xF4 is the byte to disable data report)
     printf("Error disabling stream mode\n");
     return 1;
   }
+  //sys_irqenable(&mouse_id);
 
-  if(mouse_subscribe_int(&kmouse_irq)!=0){    //check if subscription worked
+  if(mouse_subscribe_int(&mouse_irq)!=0){    //check if subscription worked
     printf("Error subscribing int\n");
     return 1;
   }
+  
 
-  while(counter_copy>0){            //looping until counter>0
+  while (cnt > counter_pp)
+	{ /* ends when it has printed the number in ctn */
+		/*Get a request message.*/
+		if ((r = driver_receive(ANY, &msg, &ipc_status)) != 0)
+		{
+			printf("ERROR: driver_receive failed with: %d", r);
+			continue;
+		}
+		if (is_ipc_notify(ipc_status))
+		{ // received notification
+			switch (_ENDPOINT_P(msg.m_source))
+			{
+			case HARDWARE: // hardware interrupt notification
+				if (msg.m_notify.interrupts & mouse_irq)
+				{				// subscribed interrupt
+					mouse_ih(); //must read only one byte
+					if ((size == 0 && (BIT(3) & byte)) || size == 1 ||size ==2)
+					{								   //access the struct bytes
+						pp.bytes[size] = byte; // bytes 0, 1 or 2
+						size++;				   // increments the counter to know in the next interrupt
+					}
+					if (size == 3)  // 3 bytes haven been read and there's no error
+					{							
+						size = 0;			// reset to start counting new packets
+						parse_packet(&pp); // parses packet
+						mouse_print_packet(&pp); // prints packet
+						counter_pp++;			//counts the prints for the end of the loop
+					}
+				}
+				break;
+			default:
+				break; // no other notifications expected: do nothing
+			}
+		}
+		else
+		{   // received a standard message, not a notification
+			// no standard messages expected: do nothing
+		}
+	}
 
-    if ((r = driver_receive(ANY, &msg, &ipc_status))==1){ 
-      printf("driver_receive failed with: %d",r);
-      continue;
+  if (mouse_unsubscribe_int() != 0) {            //check if unsubscription worked
+		printf("Error unsubscribing int \n");
+		return 1;
+	}
+
+  if(write_cmd_mouse(0xF5)!=0){     //check if disabling stream mode worked (0xF5 is the byte to disable data report)
+    printf("Error disabling stream mode\n");
+    return 1;
+  }
+
+
+  byte=0;
+
+  return 0;
+}
+
+int (mouse_test_remote)(uint16_t period, uint8_t cnt) {
+  uint8_t counter_pp=0, status;
+
+  struct packet pp;
+
+  /*if(write_cmd_mouse(0xF5)!=0){     //check if disabling stream mode worked (0xF5 is the byte to disable data report)
+    printf("Error disabling stream mode\n");
+    return 1;
+  }
+
+  if(write_cmd_mouse(0xF0)!=0){     //check if enabling remote mode worked (0xF0 is the byte to enable remote mode)
+    printf("Error disabling stream mode\n");
+    return 1;
+  }*/
+  
+
+  while (cnt > counter_pp)
+	{ /* ends when it has printed the number in ctn */
+    if(write_cmd_mouse(0xEB)!=0){     //check if enabling remote mode worked (0xF0 is the byte to enable remote mode)
+      printf("Error disabling stream mode\n");
+      return 1;
     }
-    if (is_ipc_notify(ipc_status)){                                //received notification
-      switch (_ENDPOINT_P(msg.m_source)){
-        case HARDWARE:                                             //hardware interrupt notification
-                if (msg.m_notify.interrupts &mouse_irq){        // subscribed interrupt
-                  
-                  mouse_ih();
-                  if((size==0 && ((byte&0x08)==0x08)) || size==1 ||size==2){      //0x08 is the mask to check if the 3rd bit of the first byte is set
-                    pp.bytes[size]=byte;
-                    size++;
-                  }
-                  if(size==3){
-                    size=0;
-                    parse_packet(&pp);
-                    mouse_print_packet(&pp);
-                    counter_copy--;
-                  }
-                }
-               break;
-        default:
-              break;                             //no other notifications expected: do nothing	
+
+    if(util_sys_inb(STATUS_REG,&status)!=0){
+      printf("Error verifying the status of buffer\n");
+      return 1;
+    }
+
+    if((status & 0x01)==0x01){
+
+      if((status & (STAT_REG_PAR|STAT_REG_TIMEOUT))==0){
+
+        for(int size=0;size<3;size++){
+
+          if(util_sys_inb(OUTPUT_BUF,&byte)!=0){
+          printf("Error reading output_buf\n");
+          return 1;
+          }
+
+          if (size == 0 && (BIT(3) & byte)){								   //    access the struct bytes
+            pp.bytes[size] = byte; // bytes 0, 1 or 2
+            //continue;
+          }
+          if(size==1){
+            pp.bytes[size] = byte; // bytes 0, 1 or 2
+            //continue;
+          }
+          if(size==2){
+            pp.bytes[size] = byte;
+          
+          }
         }
-    } 
-    else {                                       //received a standard message, not a notification
-      //no standard messages expected: do nothing
+        parse_packet(&pp); // parses packet
+        mouse_print_packet(&pp); // prints packet
+
+      }
     }
-  }
+    counter_pp++;			//counts the prints for the end of the loop
+    tickdelay(micros_to_ticks(period*1000));
+  
+	}
 
-   if (mouse_unsubscribe_int() != 0) {            //check if unsubscription worked
+  if (write_cmd_mouse(0xEA)!=0) {            //check if set stream mode worked (0xEA is the byte to enable)
 		printf("Error unsubscribing int \n");
 		return 1;
 	}
-  scan_code=0;
 
+  if(write_cmd_mouse(0xF5)!=0){     //check if disabling data report worked (0xF5 is the byte to disable data report)
+    printf("Error disabling stream mode\n");
+    return 1;
+  }
+
+  if (sys_outb(0X64,0x60)!=0)
+	{ // Sends Write Command
+		printf("Could not reset KBC command byte to default!");
+		return 1;
+	}
+  uint8_t default_cmd=minix_get_dflt_kbc_cmd_byte();
+	if (sys_outb(0x60,default_cmd)!=0)
+	{ // Writes Command Byte
+		printf("Could not reset KBC command byte to default!");
+		return 1;
+	}
+
+  byte=0;
+
   return 0;
 }
 
-int (mouse_test_remote)(uint16_t period, uint8_t cnt) {
-    /* To be completed */
-    printf("%s(%u, %u): under construction\n", __func__, period, cnt);
-    return 1;
-}
-
 int (mouse_test_async)(uint8_t idle_time) {
     /* To be completed */
     printf("%s(%u): under construction\n", __func__, idle_time);
@@ -105,7 +211,7 @@
     return 1;
 }
 
-int (mouse_test_gesture)() {
+int (mouse_test_gesture)(uint8_t x_len, uint8_t tolerance) {
     /* To be completed */
     printf("%s: under construction\n", __func__);
     return 1;
Index: lab4/mouse.c
===================================================================
--- lab4/mouse.c	(revision 15)
+++ lab4/mouse.c	(revision 16)
@@ -1,6 +1,8 @@
-#include "mouse.c"
+#include "mouse.h"
+#include <lcom/lcf.h>
 
 uint8_t byte;
+int mouse_id=2; //KBD_AUX_IRQ is defined in interrupt.h in .minix-src folder
 
 int (util_sys_inb)(int port, uint8_t *value) { //transform 8 bit into 32 bit
 
@@ -19,26 +21,35 @@
 }
 
 void (mouse_ih)(void){
+  //kbc_read_out_buf(&byte);
   uint8_t status_reg;
 
-  if(util_sys_inb(STATUS_REG,&status_reg)!=0){ //checks if there is an error
-    printf("Error reading status register in the keyboard interruption\n");
-    return;
+  for(int i=0; i<10;i++){
+    if(util_sys_inb(STATUS_REG,&status_reg)!=0){ //checks if there is an error
+      printf("Error reading status register in the mouse interruption\n");
+      return;
+    }
+    if((status_reg & OUTPUT_BUF)!=0){
+      if((status_reg & (STAT_REG_PAR|STAT_REG_TIMEOUT))!=0){ //checks if there is a parity or timeout error (mask -> 0xC0, bit 7 and 6 set), checks if output buffer is empty
+      printf("Parity/Timeout error or output buffer is empty or data coming from the mouse\n");
+      return;
+      }
+
+      if(util_sys_inb(OUTPUT_BUF,&byte)!=0){//checks if there is an error
+      printf("Error reading output buffer in the mouse interruption\n");
+      return;
+      }
+
+    }
+    tickdelay(micros_to_ticks(DELAY_US));
   }
-  if(((status_reg & STAT_REG_OBF)==0) ||((status_reg&(STAT_REG_PAR|STAT_REG_TIMEOUT))!=0)){ //checks if there is a parity or timeout error (mask -> 0xC0, bit 7 and 6 set), checks if output buffer is empty
-    printf("Parity/Timeout error or output buffer is empty or data coming from the mouse\n");
-    return;
-  }
-  if(util_sys_inb(OUTPUT_BUF,&byte)!=0){//checks if there is an error
-    printf("Error reading output buffer in the keyboard interruption\n");
-    return;
-  }
+  return;
 
 }
 
 int (mouse_subscribe_int)(uint8_t *bit_no) {    //similar function to that of timer_subscribe_int
-    *bit_no = BIT(keyboard_id);
-    if(sys_irqsetpolicy(KEYBOARD_IRQ,(IRQ_REENABLE|IRQ_EXCLUSIVE),&keyboard_id)==1){  //operation to subscribe int
+    *bit_no = BIT(mouse_id);
+    if(sys_irqsetpolicy(12,(IRQ_REENABLE|IRQ_EXCLUSIVE),&mouse_id)==1){  //operation to subscribe int
         printf("Error subscribing int\n");
         return 1;
     }
@@ -46,7 +57,7 @@
 }
 
 int (mouse_unsubscribe_int)() {           //similar function to that of timer_unsubscribe_int
-  if(sys_irqrmpolicy(&keyboard_id)==1){
+  if(sys_irqrmpolicy(&mouse_id)==1){
       printf("Error unsubscribing int\n");
       return 1;
   }
@@ -92,21 +103,152 @@
     pp->lb=false;
   }
 
-  if((pp->bytes[0]&0x20)==0x20){ //it means y_delta is negative
-    pp->delta_y=~pp->bytes[2];
-    pp->delta_y+=1;
+  if((pp->bytes[0] &0x10)==0x10){
+    pp->delta_x = pp->bytes[1] -256;
+  }
+  else{
+    pp->delta_x = pp->bytes[1];
+  }
 
+  if((pp->bytes[0]&0x20)==0x20){
+    pp->delta_y = pp->bytes[2] -256;
   }
   else{
-    pp->delta_y=pp->bytes[2];
+    pp->delta_y = pp->bytes[2];
   }
 
-  if((pp->bytes[0]&0x10)==0x10){ //it means x_delta is negative
-    pp->delta_x=~pp->bytes[1];
-    pp->delta_x+=1;
+  return 0;
+}
 
+/*int kbc_write(int port, uint8_t cmd) {
+	uint8_t status;
+	int retry = 0;
+
+	while (retry < 10) {
+		if(util_sys_inb(STATUS_REG, &status)!=0){
+      printf("Error in kbc_write");
+      return 1;
+    }
+
+		//... Checking if IN_BUF is OK to write to
+		if ((status & 0x02)!=0) {
+			//.... Writing the command
+			if (sys_outb(port, cmd)!=0) {
+        printf("Error");
+				return 1;
+			}
+			return 0;
+		}
+
+		//tickdelay(micros_to_ticks(DELAY_US));	// IF NOT EMPTY wait for IN_BUF to be empty
+		retry++;
+	}
+
+	return 1;
+}
+
+int read_out_buf(uint8_t *content) {
+	uint8_t status;
+	uint8_t content8; // 8-bit content
+	int retry = 0;
+
+	while (retry < 4) {
+		util_sys_inb(STATUS_REG, &status);
+
+		//... Checking if there is any error and if the byte came from AUX
+		if (((status_reg & STAT_REG_OBF)==0) ||((status_reg&(STAT_REG_PAR|STAT_REG_TIMEOUT))!=0)||((status_reg&STAT_REG_AUX)==0)) { // Parity or Timeout error, invalid data
+			return 1;
+		}
+
+		//... Checking if OUT_BUF is FULL to read
+		if (status & STAT_REG_OBF) {
+			//... Reading the Output Buffer
+			if (UTIL_sys_inb(OUTPUT_BUF, &content8)) {
+				return 1;
+			}
+			// The information in address content now contains content32 in 8-bit
+			*content = (uint8_t)content8;
+			return 0;
+		}
+
+		tickdelay(micros_to_ticks(DELAY_US));	// IF NOT EMPTY wait for IN_BUF to be empty
+		retry++;
+	}
+
+	return 1;
+}
+
+
+int kbc_write_mouse_command(uint8_t cmd) {
+	uint8_t response;
+
+	if(kbc_write(KBC_CMD_REG, 0xD4)!=0){
+    printf("Error");
+    return 1;
   }
-  else{
-    pp->delta_y=pp->bytes[1];
+	if(kbc_write(INPUT_BUF, cmd)!=0){
+    printf("Error");
+    return 1;
   }
-}
\ No newline at end of file
+	tickdelay(micros_to_ticks(DELAY_US));
+	if(kbc_read_out_buf(&response)!=0){
+    printf("Error");
+    return 1;
+  }
+	
+	// Checking if invalid byte
+	if (response != 0xFA) {
+		// repeat process if invalid
+		kbc_write(KBC_CTRL_REG, 0xD4);
+		kbc_write(INPUT_BUF, cmd);
+		tickdelay(micros_to_ticks(DELAY_US));
+		kbc_read_out_buf(&response);
+		// Checking if second consecutive invalid byte
+		if (response == 0xFC){
+			return 1;
+    }
+	}
+
+	return 0;
+}*/
+
+int write_cmd_mouse(uint8_t cmd)
+{
+  uint8_t status_reg, ack;
+
+  do
+  {
+    if (util_sys_inb(STATUS_REG, &status_reg) != 0){
+      return 1;
+    }
+      
+    if ((status_reg & STAT_REG_IBF) == 0){
+      if (sys_outb(KBC_CMD_REG, 0xD4) != 0){ //0xD4 -> code to write byte to mouse
+        return 1;
+      }
+    }
+    else
+      continue;
+
+    if (util_sys_inb(STATUS_REG, &status_reg) != 0){
+      return 1;
+    }
+
+    if ((status_reg & STAT_REG_IBF) == 0){
+      if (sys_outb(OUTPUT_BUF, cmd) != 0){
+        return 1;
+      }
+    }
+    else
+      continue;
+
+    tickdelay(micros_to_ticks(DELAY_US));
+
+    if (util_sys_inb(INPUT_BUF, &ack) != 0){
+      return 1;
+    }
+
+  } while (ack != 0xFA);            // 0xFA -> mask that checks if everything is ok
+
+  return 0;
+}
Index: lab4/mouse.h
===================================================================
--- lab4/mouse.h	(revision 15)
+++ lab4/mouse.h	(revision 16)
@@ -1,7 +1,7 @@
 #ifndef _MOUSE_H
 #define _MOUSE_H
 
-#include <lcom/lcf.h>
+
 #include <stdbool.h>
 #include <stdint.h>
 #include <minix/syslib.h>
@@ -43,8 +43,26 @@
  */
 int(mouse_unsubscribe_int)();
 /**
+ * @brief Sets up packet
  *
- *
- *
+ * @return Return 0 upon success and non-zero otherwise
  */
-int(parse_packet)(struct packet *pp);
\ No newline at end of file
+int(parse_packet)(struct packet *pp);
+/**
+* @brief Writes commands to the mouse
+*
+* @return Return 0 upon success and non-zero otherwise
+*/
+int(write_cmd_mouse)(uint8_t cmd);
+/**
+*
+*
+*
+*/
+int kbc_write(int port, uint8_t cmd);
+/**
+*
+*
+*
+*/
+#endif /*_MOUSE_H */
