Fix up the incomplete Webserver project so that it integrates with the uIP stack...
[pub/USBasp.git] / LUFA / Drivers / USB / Class / Host / HIDParser.c
index 4293a3d..3ef9c78 100644 (file)
@@ -1,21 +1,21 @@
 /*\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
-  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
@@ -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
-       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
+       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
+       ParserData->TotalDeviceReports = 1;     \r
+\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
-                                               if (ParserData->TotalDeviceReports++ > HID_MAX_REPORT_IDS)\r
+                                               if (ParserData->TotalDeviceReports == HID_MAX_REPORT_IDS)\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
-                               CurrReportIDInfo->ReportID     = CurrStateTable->ReportID;\r
+                               CurrReportIDInfo->ReportID = CurrStateTable->ReportID;\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
-                               UsageStack[UsageStackSize++] = ReportItemData;\r
+                               UsageList[UsageListSize++] = ReportItemData;\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
-                               CurrStateTable->Attributes.Usage.MinMax.Maximum = ReportItemData;\r
+                               UsageMinMax.Maximum = ReportItemData;\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
-                                       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
@@ -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
-                               if (UsageStackSize)\r
+                               if (UsageListSize)\r
                                {\r
-                                       CurrCollectionPath->Usage.Usage = UsageStack[0];\r
+                                       CurrCollectionPath->Usage.Usage = UsageList[0];\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
-                               else\r
+                               else if (UsageMinMax.Minimum <= UsageMinMax.Maximum)\r
                                {\r
-                                       CurrCollectionPath->Usage.Usage = 0;\r
+                                       CurrCollectionPath->Usage.Usage = UsageMinMax.Minimum++;\r
                                }\r
                                \r
                                break;\r
@@ -211,7 +208,6 @@ uint8_t USB_ProcessHIDReport(const uint8_t* ReportData, uint16_t ReportSize, HID
                                  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
@@ -228,43 +224,37 @@ uint8_t USB_ProcessHIDReport(const uint8_t* ReportData, uint16_t ReportSize, HID
                                        NewReportItem.CollectionPath = CurrCollectionPath;\r
                                        NewReportItem.ReportID       = CurrStateTable->ReportID;\r
 \r
-                                       if (UsageStackSize)\r
+                                       if (UsageListSize)\r
                                        {\r
-                                               NewReportItem.Attributes.Usage.Usage = UsageStack[0];\r
+                                               NewReportItem.Attributes.Usage.Usage = UsageList[0];\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
-                                               UsageStackSize--;\r
-                                       }\r
-                                       else\r
-                                       {\r
-                                               NewReportItem.Attributes.Usage.Usage = 0;\r
+                                               UsageListSize--;\r
                                        }\r
-                                                                                       \r
-                                       switch (HIDReportItem & TAG_MASK)\r
+                                       else if (UsageMinMax.Minimum <= UsageMinMax.Maximum)\r
                                        {\r
-                                               case TAG_MAIN_INPUT:\r
-                                                       NewReportItem.ItemType  = REPORT_ITEM_TYPE_In;\r
-                                                       NewReportItem.BitOffset = CurrReportIDInfo->BitsIn;\r
-                                                               \r
-                                                       CurrReportIDInfo->BitsIn += CurrStateTable->Attributes.BitSize;\r
-                                                       break;\r
-                                               case TAG_MAIN_OUTPUT:\r
-                                                       NewReportItem.ItemType  = REPORT_ITEM_TYPE_Out;\r
-                                                       NewReportItem.BitOffset = CurrReportIDInfo->BitsOut;\r
-                                                               \r
-                                                       CurrReportIDInfo->BitsOut += CurrStateTable->Attributes.BitSize;\r
-                                                       break;\r
-                                               case TAG_MAIN_FEATURE:\r
-                                                       NewReportItem.ItemType  = REPORT_ITEM_TYPE_Feature;                                             \r
-                                                       NewReportItem.BitOffset = CurrReportIDInfo->BitsFeature;\r
-                                                               \r
-                                                       CurrReportIDInfo->BitsFeature += CurrStateTable->Attributes.BitSize;\r
-                                                       break;\r
+                                               NewReportItem.Attributes.Usage.Usage = UsageMinMax.Minimum++;\r
                                        }\r
+                                       \r
+                                       uint8_t ItemTag = (HIDReportItem & TAG_MASK);\r
 \r
-                                       if (!(ReportItemData & IOF_CONSTANT) && CALLBACK_HIDParser_FilterHIDReportItem(&CurrStateTable->Attributes))\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
+                                       NewReportItem.BitOffset = CurrReportIDInfo->ReportSizeBits[NewReportItem.ItemType];\r
+\r
+                                       CurrReportIDInfo->ReportSizeBits[NewReportItem.ItemType] += CurrStateTable->Attributes.BitSize;\r
+\r
+                                       if (ParserData->LargestReportSizeBits < NewReportItem.BitOffset)\r
+                                         ParserData->LargestReportSizeBits = NewReportItem.BitOffset;\r
+                                       \r
+                                       if (!(ReportItemData & IOF_CONSTANT) && CALLBACK_HIDParser_FilterHIDReportItem(&NewReportItem))\r
                                        {                                       \r
                                                if (ParserData->TotalReportItems == HID_MAX_REPORTITEMS)\r
                                                  return HID_PARSE_InsufficientReportItems;\r
@@ -276,19 +266,20 @@ uint8_t USB_ProcessHIDReport(const uint8_t* ReportData, uint16_t ReportSize, HID
                                        }\r
                                }\r
                                \r
-                               UsageStackSize = 0;\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
+       if (!(ParserData->TotalReportItems))\r
+         return HID_PARSE_NoUnfilteredReportItems;\r
+       \r
        return HID_PARSE_Successful;\r
 }\r
 \r
@@ -298,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
+       ReportItem->PreviousValue = ReportItem->Value;\r
        ReportItem->Value = 0;\r
        \r
        if (ReportItem->ReportID)\r
@@ -320,7 +312,7 @@ bool USB_GetHIDReportItemInfo(const uint8_t* ReportData, HID_ReportItem_t* const
        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
@@ -332,6 +324,8 @@ void USB_SetHIDReportItemInfo(uint8_t* ReportData, const HID_ReportItem_t* Repor
                ReportData++;\r
        }\r
 \r
+       ReportItem->PreviousValue = ReportItem->Value;\r
+\r
        while (DataBitsRem--)\r
        {\r
                if (ReportItem->Value & (1 << (CurrentBit % 8)))\r
@@ -342,4 +336,15 @@ void USB_SetHIDReportItemInfo(uint8_t* ReportData, const HID_ReportItem_t* Repor
        }\r
 }\r
 \r
+uint16_t USB_GetHIDReportSize(HID_ReportInfo_t* const ParserData, const uint8_t ReportID, const uint8_t ReportType)\r
+{\r
+       for (uint8_t i = 0; i < HID_MAX_REPORT_IDS; i++)\r
+       {\r
+               if (ParserData->ReportIDSizes[i].ReportID == ReportID)\r
+                 return ParserData->ReportIDSizes[i].ReportSizeBits[ReportType];\r
+       }\r
+\r
+       return 0;\r
+}\r
+\r
 #endif\r