Add FatFS library to the Webserver project, extend the HTTP server so that it now...
[pub/USBasp.git] / LUFA / Drivers / USB / Class / Host / HIDParser.c
index 6b370af..3ef9c78 100644 (file)
@@ -1,21 +1,21 @@
 /*\r
              LUFA Library\r
 /*\r
              LUFA Library\r
-     Copyright (C) Dean Camera, 2009.\r
+     Copyright (C) Dean Camera, 2010.\r
               \r
   dean [at] fourwalledcubicle [dot] com\r
       www.fourwalledcubicle.com\r
 */\r
 \r
 /*\r
               \r
   dean [at] fourwalledcubicle [dot] com\r
       www.fourwalledcubicle.com\r
 */\r
 \r
 /*\r
-  Copyright 2009  Dean Camera (dean [at] fourwalledcubicle [dot] com)\r
-\r
-  Permission to use, copy, modify, and distribute this software\r
-  and its documentation for any purpose and without fee is hereby\r
-  granted, provided that the above copyright notice appear in all\r
-  copies and that both that the copyright notice and this\r
-  permission notice and warranty disclaimer appear in supporting\r
-  documentation, and that the name of the author not be used in\r
-  advertising or publicity pertaining to distribution of the\r
+  Copyright 2010  Dean Camera (dean [at] fourwalledcubicle [dot] com)\r
+\r
+  Permission to use, copy, modify, distribute, and sell this \r
+  software and its documentation for any purpose is hereby granted\r
+  without fee, provided that the above copyright notice appear in \r
+  all copies and that both that the copyright notice and this\r
+  permission notice and warranty disclaimer appear in supporting \r
+  documentation, and that the name of the author not be used in \r
+  advertising or publicity pertaining to distribution of the \r
   software without specific, written prior permission.\r
 \r
   The author disclaim all warranties with regard to this\r
   software without specific, written prior permission.\r
 \r
   The author disclaim all warranties with regard to this\r
@@ -39,19 +39,16 @@ uint8_t USB_ProcessHIDReport(const uint8_t* ReportData, uint16_t ReportSize, HID
        HID_StateTable_t*     CurrStateTable          = &StateTable[0];\r
        HID_CollectionPath_t* CurrCollectionPath      = NULL;\r
        HID_ReportSizeInfo_t* CurrReportIDInfo        = &ParserData->ReportIDSizes[0];                  \r
        HID_StateTable_t*     CurrStateTable          = &StateTable[0];\r
        HID_CollectionPath_t* CurrCollectionPath      = NULL;\r
        HID_ReportSizeInfo_t* CurrReportIDInfo        = &ParserData->ReportIDSizes[0];                  \r
-       uint16_t              UsageStack[HID_USAGE_STACK_DEPTH];\r
-       uint8_t               UsageStackSize          = 0;\r
-\r
-       ParserData->TotalReportItems   = 0;\r
-       ParserData->TotalDeviceReports = 1;\r
-       ParserData->UsingReportIDs     = false;\r
-       \r
-       for (uint8_t CurrCollection = 0; CurrCollection < HID_MAX_COLLECTIONS; CurrCollection++)\r
-         ParserData->CollectionPaths[CurrCollection].Parent = NULL;\r
+       uint16_t              UsageList[HID_USAGE_STACK_DEPTH];\r
+       uint8_t               UsageListSize           = 0;\r
+       HID_MinMax_t          UsageMinMax             = {0, 0};\r
 \r
 \r
+       memset(ParserData,       0x00, sizeof(HID_ReportInfo_t));\r
        memset(CurrStateTable,   0x00, sizeof(HID_StateTable_t));\r
        memset(CurrReportIDInfo, 0x00, sizeof(HID_ReportSizeInfo_t));\r
 \r
        memset(CurrStateTable,   0x00, sizeof(HID_StateTable_t));\r
        memset(CurrReportIDInfo, 0x00, sizeof(HID_ReportSizeInfo_t));\r
 \r
+       ParserData->TotalDeviceReports = 1;     \r
+\r
        while (ReportSize)\r
        {\r
                uint8_t  HIDReportItem  = *ReportData;\r
        while (ReportSize)\r
        {\r
                uint8_t  HIDReportItem  = *ReportData;\r
@@ -142,29 +139,29 @@ uint8_t USB_ProcessHIDReport(const uint8_t* ReportData, uint16_t ReportSize, HID
                                        \r
                                        if (CurrReportIDInfo == NULL)\r
                                        {\r
                                        \r
                                        if (CurrReportIDInfo == NULL)\r
                                        {\r
-                                               if (ParserData->TotalDeviceReports++ > HID_MAX_REPORT_IDS)\r
+                                               if (ParserData->TotalDeviceReports == HID_MAX_REPORT_IDS)\r
                                                  return HID_PARSE_InsufficientReportIDItems;\r
                                        \r
                                                  return HID_PARSE_InsufficientReportIDItems;\r
                                        \r
-                                               CurrReportIDInfo = &ParserData->ReportIDSizes[ParserData->TotalDeviceReports - 1];\r
+                                               CurrReportIDInfo = &ParserData->ReportIDSizes[ParserData->TotalDeviceReports++];\r
                                                memset(CurrReportIDInfo, 0x00, sizeof(HID_ReportSizeInfo_t));\r
                                        }\r
                                }\r
 \r
                                ParserData->UsingReportIDs = true;                              \r
 \r
                                                memset(CurrReportIDInfo, 0x00, sizeof(HID_ReportSizeInfo_t));\r
                                        }\r
                                }\r
 \r
                                ParserData->UsingReportIDs = true;                              \r
 \r
-                               CurrReportIDInfo->ReportID     = CurrStateTable->ReportID;\r
+                               CurrReportIDInfo->ReportID = CurrStateTable->ReportID;\r
                                break;\r
                        case (TYPE_LOCAL | TAG_LOCAL_USAGE):\r
                                break;\r
                        case (TYPE_LOCAL | TAG_LOCAL_USAGE):\r
-                               if (UsageStackSize == HID_USAGE_STACK_DEPTH)\r
-                                 return HID_PARSE_UsageStackOverflow;\r
+                               if (UsageListSize == HID_USAGE_STACK_DEPTH)\r
+                                 return HID_PARSE_UsageListOverflow;\r
                        \r
                        \r
-                               UsageStack[UsageStackSize++] = ReportItemData;\r
+                               UsageList[UsageListSize++] = ReportItemData;\r
                                break;\r
                        case (TYPE_LOCAL | TAG_LOCAL_USAGEMIN):\r
                                break;\r
                        case (TYPE_LOCAL | TAG_LOCAL_USAGEMIN):\r
-                               CurrStateTable->Attributes.Usage.MinMax.Minimum = ReportItemData;\r
+                               UsageMinMax.Minimum = ReportItemData;\r
                                break;\r
                        case (TYPE_LOCAL | TAG_LOCAL_USAGEMAX):\r
                                break;\r
                        case (TYPE_LOCAL | TAG_LOCAL_USAGEMAX):\r
-                               CurrStateTable->Attributes.Usage.MinMax.Maximum = ReportItemData;\r
+                               UsageMinMax.Maximum = ReportItemData;\r
                                break;\r
                        case (TYPE_MAIN | TAG_MAIN_COLLECTION):\r
                                if (CurrCollectionPath == NULL)\r
                                break;\r
                        case (TYPE_MAIN | TAG_MAIN_COLLECTION):\r
                                if (CurrCollectionPath == NULL)\r
@@ -177,7 +174,7 @@ uint8_t USB_ProcessHIDReport(const uint8_t* ReportData, uint16_t ReportSize, HID
                        \r
                                        CurrCollectionPath = &ParserData->CollectionPaths[1];\r
 \r
                        \r
                                        CurrCollectionPath = &ParserData->CollectionPaths[1];\r
 \r
-                                       while (CurrCollectionPath->Parent != NULL);\r
+                                       while (CurrCollectionPath->Parent != NULL)\r
                                        {\r
                                                if (CurrCollectionPath == &ParserData->CollectionPaths[HID_MAX_COLLECTIONS - 1])\r
                                                  return HID_PARSE_InsufficientCollectionPaths;\r
                                        {\r
                                                if (CurrCollectionPath == &ParserData->CollectionPaths[HID_MAX_COLLECTIONS - 1])\r
                                                  return HID_PARSE_InsufficientCollectionPaths;\r
@@ -191,18 +188,18 @@ uint8_t USB_ProcessHIDReport(const uint8_t* ReportData, uint16_t ReportSize, HID
                                CurrCollectionPath->Type = ReportItemData;\r
                                CurrCollectionPath->Usage.Page = CurrStateTable->Attributes.Usage.Page;\r
                                \r
                                CurrCollectionPath->Type = ReportItemData;\r
                                CurrCollectionPath->Usage.Page = CurrStateTable->Attributes.Usage.Page;\r
                                \r
-                               if (UsageStackSize)\r
+                               if (UsageListSize)\r
                                {\r
                                {\r
-                                       CurrCollectionPath->Usage.Usage = UsageStack[0];\r
+                                       CurrCollectionPath->Usage.Usage = UsageList[0];\r
 \r
 \r
-                                       for (uint8_t i = 0; i < UsageStackSize; i++)\r
-                                         UsageStack[i] = UsageStack[i + 1];\r
-                                         \r
-                                       UsageStackSize--;\r
+                                       for (uint8_t i = 0; i < UsageListSize; i++)\r
+                                         UsageList[i] = UsageList[i + 1];\r
+                                       \r
+                                       UsageListSize--;\r
                                }\r
                                }\r
-                               else\r
+                               else if (UsageMinMax.Minimum <= UsageMinMax.Maximum)\r
                                {\r
                                {\r
-                                       CurrCollectionPath->Usage.Usage = 0;\r
+                                       CurrCollectionPath->Usage.Usage = UsageMinMax.Minimum++;\r
                                }\r
                                \r
                                break;\r
                                }\r
                                \r
                                break;\r
@@ -211,13 +208,11 @@ uint8_t USB_ProcessHIDReport(const uint8_t* ReportData, uint16_t ReportSize, HID
                                  return HID_PARSE_UnexpectedEndCollection;\r
                \r
                                CurrCollectionPath = CurrCollectionPath->Parent;\r
                                  return HID_PARSE_UnexpectedEndCollection;\r
                \r
                                CurrCollectionPath = CurrCollectionPath->Parent;\r
-\r
                                break;\r
                        case (TYPE_MAIN | TAG_MAIN_INPUT):\r
                        case (TYPE_MAIN | TAG_MAIN_OUTPUT):\r
                        case (TYPE_MAIN | TAG_MAIN_FEATURE):\r
                                break;\r
                        case (TYPE_MAIN | TAG_MAIN_INPUT):\r
                        case (TYPE_MAIN | TAG_MAIN_OUTPUT):\r
                        case (TYPE_MAIN | TAG_MAIN_FEATURE):\r
-                               uint8_t Count = CurrStateTable->ReportCount;\r
-                               for (uint8_t ReportItemNum = 0; ReportItemNum < Count; ReportItemNum++)\r
+                               for (uint8_t ReportItemNum = 0; ReportItemNum < CurrStateTable->ReportCount; ReportItemNum++)\r
                                {\r
                                        HID_ReportItem_t NewReportItem;\r
                                  \r
                                {\r
                                        HID_ReportItem_t NewReportItem;\r
                                  \r
@@ -229,50 +224,37 @@ uint8_t USB_ProcessHIDReport(const uint8_t* ReportData, uint16_t ReportSize, HID
                                        NewReportItem.CollectionPath = CurrCollectionPath;\r
                                        NewReportItem.ReportID       = CurrStateTable->ReportID;\r
 \r
                                        NewReportItem.CollectionPath = CurrCollectionPath;\r
                                        NewReportItem.ReportID       = CurrStateTable->ReportID;\r
 \r
-                                       if (UsageStackSize)\r
+                                       if (UsageListSize)\r
                                        {\r
                                        {\r
-                                               NewReportItem.Attributes.Usage.Usage = UsageStack[0];\r
+                                               NewReportItem.Attributes.Usage.Usage = UsageList[0];\r
 \r
 \r
-                                               for (uint8_t i = 0; i < UsageStackSize; i++)\r
-                                                 UsageStack[i] = UsageStack[i + 1];\r
+                                               for (uint8_t i = 0; i < UsageListSize; i++)\r
+                                                 UsageList[i] = UsageList[i + 1];\r
                                                  \r
                                                  \r
-                                               UsageStackSize--;\r
+                                               UsageListSize--;\r
                                        }\r
                                        }\r
-                                       else\r
+                                       else if (UsageMinMax.Minimum <= UsageMinMax.Maximum)\r
                                        {\r
                                        {\r
-                                               NewReportItem.Attributes.Usage.Usage = 0;\r
+                                               NewReportItem.Attributes.Usage.Usage = UsageMinMax.Minimum++;\r
                                        }\r
                                        }\r
+                                       \r
+                                       uint8_t ItemTag = (HIDReportItem & TAG_MASK);\r
 \r
 \r
-                                       uint8_t ReportSizeIndex = 0;\r
-\r
-                                       switch (HIDReportItem & TAG_MASK)\r
-                                       {\r
-                                               case TAG_MAIN_INPUT:\r
-                                                       NewReportItem.ItemType  = REPORT_ITEM_TYPE_In;\r
-                                                       NewReportItem.BitOffset = CurrReportIDInfo->ReportSizeBits[REPORT_ITEM_TYPE_In];\r
-                                                               \r
-                                                       ReportSizeIndex = REPORT_ITEM_TYPE_In;\r
-                                                       break;\r
-                                               case TAG_MAIN_OUTPUT:\r
-                                                       NewReportItem.ItemType  = REPORT_ITEM_TYPE_Out;\r
-                                                       NewReportItem.BitOffset = CurrReportIDInfo->ReportSizeBits[REPORT_ITEM_TYPE_Out];\r
-                                                               \r
-                                                       ReportSizeIndex = REPORT_ITEM_TYPE_Out;\r
-                                                       break;\r
-                                               case TAG_MAIN_FEATURE:\r
-                                                       NewReportItem.ItemType  = REPORT_ITEM_TYPE_Feature;                                             \r
-                                                       NewReportItem.BitOffset = CurrReportIDInfo->ReportSizeBits[REPORT_ITEM_TYPE_Feature];\r
-                                                               \r
-                                                       ReportSizeIndex = REPORT_ITEM_TYPE_Feature;\r
-                                                       break;\r
-                                       }\r
+                                       if (ItemTag == TAG_MAIN_INPUT)\r
+                                         NewReportItem.ItemType = REPORT_ITEM_TYPE_In;\r
+                                       else if (ItemTag == TAG_MAIN_OUTPUT)\r
+                                         NewReportItem.ItemType = REPORT_ITEM_TYPE_Out;\r
+                                       else\r
+                                         NewReportItem.ItemType = REPORT_ITEM_TYPE_Feature;\r
                                        \r
                                        \r
-                                       CurrReportIDInfo->ReportSizeBits[ReportSizeIndex] += CurrStateTable->Attributes.BitSize;\r
+                                       NewReportItem.BitOffset = CurrReportIDInfo->ReportSizeBits[NewReportItem.ItemType];\r
+\r
+                                       CurrReportIDInfo->ReportSizeBits[NewReportItem.ItemType] += CurrStateTable->Attributes.BitSize;\r
 \r
 \r
-                                       if (ParserData->LargestReportSizeBits < CurrReportIDInfo->ReportSizeBits[ReportSizeIndex])\r
-                                         ParserData->LargestReportSizeBits = CurrReportIDInfo->ReportSizeBits[ReportSizeIndex];\r
+                                       if (ParserData->LargestReportSizeBits < NewReportItem.BitOffset)\r
+                                         ParserData->LargestReportSizeBits = NewReportItem.BitOffset;\r
                                        \r
                                        \r
-                                       if (!(ReportItemData & IOF_CONSTANT) && CALLBACK_HIDParser_FilterHIDReportItem(&CurrStateTable->Attributes))\r
+                                       if (!(ReportItemData & IOF_CONSTANT) && CALLBACK_HIDParser_FilterHIDReportItem(&NewReportItem))\r
                                        {                                       \r
                                                if (ParserData->TotalReportItems == HID_MAX_REPORTITEMS)\r
                                                  return HID_PARSE_InsufficientReportItems;\r
                                        {                                       \r
                                                if (ParserData->TotalReportItems == HID_MAX_REPORTITEMS)\r
                                                  return HID_PARSE_InsufficientReportItems;\r
@@ -284,16 +266,14 @@ uint8_t USB_ProcessHIDReport(const uint8_t* ReportData, uint16_t ReportSize, HID
                                        }\r
                                }\r
                                \r
                                        }\r
                                }\r
                                \r
-                               UsageStackSize = 0;\r
-                               \r
                                break;\r
                }\r
          \r
                if ((HIDReportItem & TYPE_MASK) == TYPE_MAIN)\r
                {\r
                                break;\r
                }\r
          \r
                if ((HIDReportItem & TYPE_MASK) == TYPE_MAIN)\r
                {\r
-                       CurrStateTable->Attributes.Usage.MinMax.Minimum = 0;\r
-                       CurrStateTable->Attributes.Usage.MinMax.Maximum = 0;\r
-                       UsageStackSize = 0;\r
+                       UsageMinMax.Minimum = 0;\r
+                       UsageMinMax.Maximum = 0;\r
+                       UsageListSize   = 0;\r
                }\r
        }\r
        \r
                }\r
        }\r
        \r
@@ -309,6 +289,7 @@ bool USB_GetHIDReportItemInfo(const uint8_t* ReportData, HID_ReportItem_t* const
        uint16_t CurrentBit   = ReportItem->BitOffset;\r
        uint32_t BitMask      = (1 << 0);\r
 \r
        uint16_t CurrentBit   = ReportItem->BitOffset;\r
        uint32_t BitMask      = (1 << 0);\r
 \r
+       ReportItem->PreviousValue = ReportItem->Value;\r
        ReportItem->Value = 0;\r
        \r
        if (ReportItem->ReportID)\r
        ReportItem->Value = 0;\r
        \r
        if (ReportItem->ReportID)\r
@@ -331,7 +312,7 @@ bool USB_GetHIDReportItemInfo(const uint8_t* ReportData, HID_ReportItem_t* const
        return true;\r
 }\r
 \r
        return true;\r
 }\r
 \r
-void USB_SetHIDReportItemInfo(uint8_t* ReportData, const HID_ReportItem_t* ReportItem)\r
+void USB_SetHIDReportItemInfo(uint8_t* ReportData, HID_ReportItem_t* const ReportItem)\r
 {\r
        uint16_t DataBitsRem  = ReportItem->Attributes.BitSize;\r
        uint16_t CurrentBit   = ReportItem->BitOffset;\r
 {\r
        uint16_t DataBitsRem  = ReportItem->Attributes.BitSize;\r
        uint16_t CurrentBit   = ReportItem->BitOffset;\r
@@ -343,6 +324,8 @@ void USB_SetHIDReportItemInfo(uint8_t* ReportData, const HID_ReportItem_t* Repor
                ReportData++;\r
        }\r
 \r
                ReportData++;\r
        }\r
 \r
+       ReportItem->PreviousValue = ReportItem->Value;\r
+\r
        while (DataBitsRem--)\r
        {\r
                if (ReportItem->Value & (1 << (CurrentBit % 8)))\r
        while (DataBitsRem--)\r
        {\r
                if (ReportItem->Value & (1 << (CurrentBit % 8)))\r