debug support using NumLock
authorNIIBE Yutaka <gniibe@fsij.org>
Fri, 28 Oct 2011 08:39:16 +0000 (17:39 +0900)
committerNIIBE Yutaka <gniibe@fsij.org>
Fri, 28 Oct 2011 08:39:16 +0000 (17:39 +0900)
src/gone.h
src/main.c
src/pin-cir.c

index c70937c..6962858 100644 (file)
@@ -52,5 +52,8 @@ extern void sha1_write (void *context, const void *inbuf_arg, size_t inlen);
 extern void sha1_final (void *context);
 extern unsigned char *sha1_read (void *context);
 
-const uint8_t *
+extern const uint8_t *
 compute_hmac (const uint8_t *key, int key_len, const uint8_t *msg, int msg_len);
+
+extern uint8_t gone_mode;
+#define HID_LED_NUMLOCK (1<<0)
index b1cd91b..daaa2ca 100644 (file)
@@ -59,9 +59,9 @@ static const USBDescriptor gone_device_descriptor = {
 
 
 /* Configuration Descriptor tree for a HID.*/
-static const uint8_t hid_configuration_descriptor_data[34] = {
+static const uint8_t hid_configuration_descriptor_data[41] = {
   /* Configuration Descriptor.*/
-  USB_DESC_CONFIGURATION(34,            /* wTotalLength.                    */
+  USB_DESC_CONFIGURATION(41,            /* wTotalLength.                    */
                          0x01,          /* bNumInterfaces.                  */
                          0x01,          /* bConfigurationValue.             */
                          4,             /* iConfiguration.                  */
@@ -70,7 +70,7 @@ static const uint8_t hid_configuration_descriptor_data[34] = {
   /* Interface Descriptor.*/
   USB_DESC_INTERFACE    (0x00,          /* bInterfaceNumber.                */
                          0x00,          /* bAlternateSetting.               */
-                        0x01,          /* bNumEndpoints. */
+                        0x02,          /* bNumEndpoints. */
                         0x03,          /* bInterfaceClass: HID. */
                         0x00,          /* bInterfaceSubClass:
                                           1=BOOT, 0=no boot    */
@@ -84,12 +84,17 @@ static const uint8_t hid_configuration_descriptor_data[34] = {
   USB_DESC_BYTE         (0x00),         /* bCountryCode: Hardware target country */
   USB_DESC_BYTE         (0x01),         /* bNumDescriptors: Number of HID class descriptors to follow */
   USB_DESC_BYTE         (0x22),         /* bDescriptorType */
-  USB_DESC_WORD         (45),           /* wItemLength: Total length of Report descriptor */
+  USB_DESC_WORD         (63),           /* wItemLength: Total length of Report descriptor */
   /* Endpoint Descriptor.  */
   USB_DESC_ENDPOINT     (0x81,          /* bEndpointAddress (IN).  */
                          0x03,          /* bmAttributes (Interrupt).        */
                          0x0008,        /* wMaxPacketSize.                  */
                          0x20),         /* bInterval (32ms).                */
+  /* Endpoint Descriptor.  */
+  USB_DESC_ENDPOINT     (0x01,          /* bEndpointAddress (IN).  */
+                         0x03,          /* bmAttributes (Interrupt).        */
+                         0x0001,        /* wMaxPacketSize.                  */
+                         0x20),         /* bInterval (32ms).                */
 };
 
 /*
@@ -100,7 +105,7 @@ static const USBDescriptor hid_configuration_descriptor = {
   hid_configuration_descriptor_data
 };
 
-static const uint8_t hid_report_descriptor_data[45] = {
+static const uint8_t hid_report_descriptor_data[63] = {
   0x05, 0x01,      /* USAGE_PAGE (Generic Desktop) */
   0x09, 0x06,      /* USAGE (Keyboard) */
   0xa1, 0x01,      /* COLLECTION (Application) */
@@ -115,6 +120,18 @@ static const uint8_t hid_report_descriptor_data[45] = {
   0x95, 0x01,      /*   REPORT_COUNT (1) */
   0x75, 0x08,      /*   REPORT_SIZE (8) */
   0x81, 0x01,      /*   INPUT (Constant); Reserved byte */
+
+  /* NumLock, CapsLock, ScrollLock, Compose, Kana */
+  0x95, 0x05,      /*   REPORT_COUNT (5) */
+  0x75, 0x01,      /*   REPORT_SIZE (1) */
+  0x05, 0x08,      /*   USAGE_PAGE (LEDs) */
+  0x19, 0x01,      /*   USAGE_MINIMUM (1) */
+  0x29, 0x05,      /*   USAGE_MAXIMUM (5) */
+  0x91, 0x02,      /*   OUTPUT (Data,Var,Abs); LED report */
+  0x95, 0x01,      /*   REPORT_COUNT (1) */
+  0x75, 0x03,      /*   REPORT_SIZE (3) */
+  0x91, 0x01,      /*   OUTPUT (Constant); LED report padding */
+
   0x95, 0x06,      /*   REPORT_COUNT (6) */
   0x75, 0x08,      /*   REPORT_SIZE (8) */
   0x15, 0x00,      /*   LOGICAL_MINIMUM (0) */
@@ -265,6 +282,19 @@ hid_data_transmitted (USBDriver *usbp, usbep_t ep)
   chSysUnlockFromIsr();
 }
 
+uint8_t gone_mode;
+
+static void
+hid_data_received (USBDriver *usbp, usbep_t ep)
+{
+  size_t n;
+  uint8_t buf[8];
+
+  n = usbReadPacketI (usbp, ep, buf, 8);
+  if (n != USB_ENDPOINT_BUSY)
+    gone_mode = buf[0];
+}
+
 
 /**
  * @brief   EP1 initialization structure (IN and OUT).
@@ -273,9 +303,9 @@ static const USBEndpointConfig hid_ep1config = {
   USB_EP_MODE_TYPE_INTR | USB_EP_MODE_PACKET,
   NULL,
   hid_data_transmitted,
-  NULL,
+  hid_data_received,
+  0x0040,
   0x0040,
-  0x0000,
   NULL,
   NULL
 };
@@ -733,67 +763,69 @@ main (int argc, char **argv)
          chThdSleep (MS2ST (500));
        }
 
-
       if (gone_usb_configuration == 1) /* HID */
        while (USBP->state == USB_ACTIVE)
          {
            int len = pinpad_getline (MS2ST (5000));
            if (len)
              {
-#if 1
-               static uint32_t hotp_counter = 1;
-               uint8_t key[20] = {
-                 0xc9, 0x89, 0x39, 0xf2, 0xf2, 0x9b, 0x95, 0xee,
-                 0x15, 0x28, 0xb9, 0x1a, 0x02, 0xca, 0x0e, 0x95,
-                 0x00, 0x00, 0x00, 0x00 
-               };
-               uint8_t msg[8] = { 0, 0, 0, 0, 0, 0, 0, 0 };
-               const uint8_t *hmac;
-               uint32_t pin_number;
-               uint32_t digits;
-               uint8_t output[7+1];
-               int i;
-               int offset;
-
-               pin_number = (pin_input_buffer[0]-'0')*1000
-                 + (pin_input_buffer[1]-'0')*100
-                 + (pin_input_buffer[2]-'0')*10
-                 + (pin_input_buffer[3]-'0');
-               key[16] = (pin_number >> 24)&0xff;
-               key[17] = (pin_number >> 16)&0xff;
-               key[18] = (pin_number >>  8)&0xff;
-               key[19] = (pin_number >>  0)&0xff;
-               msg[4] = (hotp_counter >> 24)&0xff;
-               msg[5] = (hotp_counter >> 16)&0xff;
-               msg[6] = (hotp_counter >>  8)&0xff;
-               msg[7] = (hotp_counter >>  0)&0xff;
-               hmac = compute_hmac (key, 20, msg, 8);
-               offset = hmac[19] & 0x0f;
-               digits =
-                 ((hmac[offset]&0x7f)<<24) | (hmac[offset+1]<<16)
-                 | (hmac[offset+2]<<8) | hmac[offset+3];
-
-               for (i = 0; i < 6; i++)
+               if ((gone_mode & HID_LED_NUMLOCK))
+                 {             /* Debug output */
+                   pin_input_buffer[len] = 0;
+                   emit_key_strokes (pin_input_buffer);
+                 }
+               else
                  {
-                   output[6-i] = (digits % 10) + '0';
-                   digits /= 10;
+                   static uint32_t hotp_counter = 1;
+                   uint8_t key[20] = {
+                     0xc9, 0x89, 0x39, 0xf2, 0xf2, 0x9b, 0x95, 0xee,
+                     0x15, 0x28, 0xb9, 0x1a, 0x02, 0xca, 0x0e, 0x95,
+                     0x00, 0x00, 0x00, 0x00 
+                   };
+                   uint8_t msg[8] = { 0, 0, 0, 0, 0, 0, 0, 0 };
+                   const uint8_t *hmac;
+                   uint32_t pin_number;
+                   uint32_t digits;
+                   uint8_t output[7+1];
+                   int i;
+                   int offset;
+
+                   pin_number = (pin_input_buffer[0]-'0')*1000
+                     + (pin_input_buffer[1]-'0')*100
+                     + (pin_input_buffer[2]-'0')*10
+                     + (pin_input_buffer[3]-'0');
+                   key[16] = (pin_number >> 24)&0xff;
+                   key[17] = (pin_number >> 16)&0xff;
+                   key[18] = (pin_number >>  8)&0xff;
+                   key[19] = (pin_number >>  0)&0xff;
+                   msg[4] = (hotp_counter >> 24)&0xff;
+                   msg[5] = (hotp_counter >> 16)&0xff;
+                   msg[6] = (hotp_counter >>  8)&0xff;
+                   msg[7] = (hotp_counter >>  0)&0xff;
+                   hmac = compute_hmac (key, 20, msg, 8);
+                   offset = hmac[19] & 0x0f;
+                   digits =
+                     ((hmac[offset]&0x7f)<<24) | (hmac[offset+1]<<16)
+                     | (hmac[offset+2]<<8) | hmac[offset+3];
+
+                   for (i = 0; i < 6; i++)
+                     {
+                       output[6-i] = (digits % 10) + '0';
+                       digits /= 10;
+                     }
+                   output[0] = '0';
+                   output[7] = 0;
+                   emit_key_strokes (output);
+
+                   hotp_counter++;
                  }
-               output[0] = '0';
-               output[7] = 0;
-               emit_key_strokes (output);
-
-               hotp_counter++;
-#else
-               pin_input_buffer[len] = 0;
-               emit_key_strokes (pin_input_buffer);
-#endif
              }
            else
              {
                led_blink (1);
                chThdSleep (MS2ST (500));
              }
-         }
+          }
       else if (gone_usb_configuration == 2) /* CDC-ACM */
        while (USBP->state == USB_ACTIVE)
          {
index df46ef7..de9e1ca 100644 (file)
@@ -517,6 +517,9 @@ cir_getchar (systime_t timeout)
   DEBUG_BYTE (cir_seq);
 #endif
 
+  if ((gone_mode & HID_LED_NUMLOCK))
+    goto err;
+
   switch (cir_proto)
     {
     case CIR_PROTO_RC5: