3090774ec82684db47585cfd83c024f815e21c76
[pub/USBasp.git] / LUFA / Drivers / USB / Class / Host / HIDParser.c
1 /*
2 LUFA Library
3 Copyright (C) Dean Camera, 2009.
4
5 dean [at] fourwalledcubicle [dot] com
6 www.fourwalledcubicle.com
7 */
8
9 /*
10 Copyright 2009 Dean Camera (dean [at] fourwalledcubicle [dot] com)
11
12 Permission to use, copy, modify, and distribute this software
13 and its documentation for any purpose and without fee is hereby
14 granted, provided that the above copyright notice appear in all
15 copies and that both that the copyright notice and this
16 permission notice and warranty disclaimer appear in supporting
17 documentation, and that the name of the author not be used in
18 advertising or publicity pertaining to distribution of the
19 software without specific, written prior permission.
20
21 The author disclaim all warranties with regard to this
22 software, including all implied warranties of merchantability
23 and fitness. In no event shall the author be liable for any
24 special, indirect or consequential damages or any damages
25 whatsoever resulting from loss of use, data or profits, whether
26 in an action of contract, negligence or other tortious action,
27 arising out of or in connection with the use or performance of
28 this software.
29 */
30
31 #include "../../HighLevel/USBMode.h"
32 #if defined(USB_CAN_BE_HOST)
33
34 #include "HIDParser.h"
35
36 uint8_t USB_ProcessHIDReport(const uint8_t* ReportData, uint16_t ReportSize, HID_ReportInfo_t* const ParserData)
37 {
38 HID_StateTable_t StateTable[HID_STATETABLE_STACK_DEPTH];
39 HID_StateTable_t* CurrStateTable = &StateTable[0];
40 HID_CollectionPath_t* CurrCollectionPath = NULL;
41 HID_ReportSizeInfo_t* CurrReportIDInfo = &ParserData->ReportIDSizes[0];
42 uint16_t UsageStack[HID_USAGE_STACK_DEPTH];
43 uint8_t UsageStackSize = 0;
44
45 memset(ParserData, 0x00, sizeof(HID_ReportInfo_t));
46 memset(CurrStateTable, 0x00, sizeof(HID_StateTable_t));
47 memset(CurrReportIDInfo, 0x00, sizeof(HID_ReportSizeInfo_t));
48
49 ParserData->TotalDeviceReports = 1;
50
51 while (ReportSize)
52 {
53 uint8_t HIDReportItem = *ReportData;
54 uint32_t ReportItemData = 0;
55
56 ReportData++;
57 ReportSize--;
58
59 switch (HIDReportItem & DATA_SIZE_MASK)
60 {
61 case DATA_SIZE_4:
62 ReportItemData = *((uint32_t*)ReportData);
63 ReportSize -= 4;
64 ReportData += 4;
65 break;
66 case DATA_SIZE_2:
67 ReportItemData = *((uint16_t*)ReportData);
68 ReportSize -= 2;
69 ReportData += 2;
70 break;
71 case DATA_SIZE_1:
72 ReportItemData = *((uint8_t*)ReportData);
73 ReportSize -= 1;
74 ReportData += 1;
75 break;
76 }
77
78 switch (HIDReportItem & (TYPE_MASK | TAG_MASK))
79 {
80 case (TYPE_GLOBAL | TAG_GLOBAL_PUSH):
81 if (CurrStateTable == &StateTable[HID_STATETABLE_STACK_DEPTH - 1])
82 return HID_PARSE_HIDStackOverflow;
83
84 memcpy((CurrStateTable + 1),
85 CurrStateTable,
86 sizeof(HID_ReportItem_t));
87
88 CurrStateTable++;
89 break;
90 case (TYPE_GLOBAL | TAG_GLOBAL_POP):
91 if (CurrStateTable == &StateTable[0])
92 return HID_PARSE_HIDStackUnderflow;
93
94 CurrStateTable--;
95 break;
96 case (TYPE_GLOBAL | TAG_GLOBAL_USAGEPAGE):
97 CurrStateTable->Attributes.Usage.Page = ReportItemData;
98 break;
99 case (TYPE_GLOBAL | TAG_GLOBAL_LOGICALMIN):
100 CurrStateTable->Attributes.Logical.Minimum = ReportItemData;
101 break;
102 case (TYPE_GLOBAL | TAG_GLOBAL_LOGICALMAX):
103 CurrStateTable->Attributes.Logical.Maximum = ReportItemData;
104 break;
105 case (TYPE_GLOBAL | TAG_GLOBAL_PHYSMIN):
106 CurrStateTable->Attributes.Physical.Minimum = ReportItemData;
107 break;
108 case (TYPE_GLOBAL | TAG_GLOBAL_PHYSMAX):
109 CurrStateTable->Attributes.Physical.Maximum = ReportItemData;
110 break;
111 case (TYPE_GLOBAL | TAG_GLOBAL_UNITEXP):
112 CurrStateTable->Attributes.Unit.Exponent = ReportItemData;
113 break;
114 case (TYPE_GLOBAL | TAG_GLOBAL_UNIT):
115 CurrStateTable->Attributes.Unit.Type = ReportItemData;
116 break;
117 case (TYPE_GLOBAL | TAG_GLOBAL_REPORTSIZE):
118 CurrStateTable->Attributes.BitSize = ReportItemData;
119 break;
120 case (TYPE_GLOBAL | TAG_GLOBAL_REPORTCOUNT):
121 CurrStateTable->ReportCount = ReportItemData;
122 break;
123 case (TYPE_GLOBAL | TAG_GLOBAL_REPORTID):
124 CurrStateTable->ReportID = ReportItemData;
125
126 if (ParserData->UsingReportIDs)
127 {
128 CurrReportIDInfo = NULL;
129
130 for (uint8_t i = 0; i < ParserData->TotalDeviceReports; i++)
131 {
132 if (ParserData->ReportIDSizes[i].ReportID == CurrStateTable->ReportID)
133 {
134 CurrReportIDInfo = &ParserData->ReportIDSizes[i];
135 break;
136 }
137 }
138
139 if (CurrReportIDInfo == NULL)
140 {
141 if (ParserData->TotalDeviceReports++ > HID_MAX_REPORT_IDS)
142 return HID_PARSE_InsufficientReportIDItems;
143
144 CurrReportIDInfo = &ParserData->ReportIDSizes[ParserData->TotalDeviceReports - 1];
145 memset(CurrReportIDInfo, 0x00, sizeof(HID_ReportSizeInfo_t));
146 }
147 }
148
149 ParserData->UsingReportIDs = true;
150
151 CurrReportIDInfo->ReportID = CurrStateTable->ReportID;
152 break;
153 case (TYPE_LOCAL | TAG_LOCAL_USAGE):
154 if (UsageStackSize == HID_USAGE_STACK_DEPTH)
155 return HID_PARSE_UsageStackOverflow;
156
157 UsageStack[UsageStackSize++] = ReportItemData;
158 break;
159 case (TYPE_LOCAL | TAG_LOCAL_USAGEMIN):
160 CurrStateTable->Attributes.Usage.MinMax.Minimum = ReportItemData;
161 break;
162 case (TYPE_LOCAL | TAG_LOCAL_USAGEMAX):
163 CurrStateTable->Attributes.Usage.MinMax.Maximum = ReportItemData;
164 break;
165 case (TYPE_MAIN | TAG_MAIN_COLLECTION):
166 if (CurrCollectionPath == NULL)
167 {
168 CurrCollectionPath = &ParserData->CollectionPaths[0];
169 }
170 else
171 {
172 HID_CollectionPath_t* ParentCollectionPath = CurrCollectionPath;
173
174 CurrCollectionPath = &ParserData->CollectionPaths[1];
175
176 while (CurrCollectionPath->Parent != NULL)
177 {
178 if (CurrCollectionPath == &ParserData->CollectionPaths[HID_MAX_COLLECTIONS - 1])
179 return HID_PARSE_InsufficientCollectionPaths;
180
181 CurrCollectionPath++;
182 }
183
184 CurrCollectionPath->Parent = ParentCollectionPath;
185 }
186
187 CurrCollectionPath->Type = ReportItemData;
188 CurrCollectionPath->Usage.Page = CurrStateTable->Attributes.Usage.Page;
189
190 if (UsageStackSize)
191 {
192 CurrCollectionPath->Usage.Usage = UsageStack[0];
193
194 for (uint8_t i = 0; i < UsageStackSize; i++)
195 UsageStack[i] = UsageStack[i + 1];
196
197 UsageStackSize--;
198 }
199
200 break;
201 case (TYPE_MAIN | TAG_MAIN_ENDCOLLECTION):
202 if (CurrCollectionPath == NULL)
203 return HID_PARSE_UnexpectedEndCollection;
204
205 CurrCollectionPath = CurrCollectionPath->Parent;
206 break;
207 case (TYPE_MAIN | TAG_MAIN_INPUT):
208 case (TYPE_MAIN | TAG_MAIN_OUTPUT):
209 case (TYPE_MAIN | TAG_MAIN_FEATURE):
210 for (uint8_t ReportItemNum = 0; ReportItemNum < CurrStateTable->ReportCount; ReportItemNum++)
211 {
212 HID_ReportItem_t NewReportItem;
213
214 memcpy(&NewReportItem.Attributes,
215 &CurrStateTable->Attributes,
216 sizeof(HID_ReportItem_Attributes_t));
217
218 NewReportItem.ItemFlags = ReportItemData;
219 NewReportItem.CollectionPath = CurrCollectionPath;
220 NewReportItem.ReportID = CurrStateTable->ReportID;
221
222 if (UsageStackSize)
223 {
224 NewReportItem.Attributes.Usage.Usage = UsageStack[0];
225
226 for (uint8_t i = 0; i < UsageStackSize; i++)
227 UsageStack[i] = UsageStack[i + 1];
228
229 UsageStackSize--;
230 }
231
232 switch (HIDReportItem & TAG_MASK)
233 {
234 case TAG_MAIN_INPUT:
235 NewReportItem.ItemType = REPORT_ITEM_TYPE_In;
236 break;
237 case TAG_MAIN_OUTPUT:
238 NewReportItem.ItemType = REPORT_ITEM_TYPE_Out;
239 break;
240 case TAG_MAIN_FEATURE:
241 NewReportItem.ItemType = REPORT_ITEM_TYPE_Feature;
242 break;
243 }
244
245 NewReportItem.BitOffset = CurrReportIDInfo->ReportSizeBits[NewReportItem.ItemType];
246
247 CurrReportIDInfo->ReportSizeBits[NewReportItem.ItemType] += CurrStateTable->Attributes.BitSize;
248
249 if (ParserData->LargestReportSizeBits < NewReportItem.BitOffset)
250 ParserData->LargestReportSizeBits = NewReportItem.BitOffset;
251
252 if (!(ReportItemData & IOF_CONSTANT) && CALLBACK_HIDParser_FilterHIDReportItem(&NewReportItem))
253 {
254 if (ParserData->TotalReportItems == HID_MAX_REPORTITEMS)
255 return HID_PARSE_InsufficientReportItems;
256
257 memcpy(&ParserData->ReportItems[ParserData->TotalReportItems],
258 &NewReportItem, sizeof(HID_ReportItem_t));
259
260 ParserData->TotalReportItems++;
261 }
262 }
263
264 break;
265 }
266
267 if ((HIDReportItem & TYPE_MASK) == TYPE_MAIN)
268 {
269 CurrStateTable->Attributes.Usage.MinMax.Minimum = 0;
270 CurrStateTable->Attributes.Usage.MinMax.Maximum = 0;
271 UsageStackSize = 0;
272 }
273 }
274
275 if (!(ParserData->TotalReportItems))
276 return HID_PARSE_NoUnfilteredReportItems;
277
278 return HID_PARSE_Successful;
279 }
280
281 bool USB_GetHIDReportItemInfo(const uint8_t* ReportData, HID_ReportItem_t* const ReportItem)
282 {
283 uint16_t DataBitsRem = ReportItem->Attributes.BitSize;
284 uint16_t CurrentBit = ReportItem->BitOffset;
285 uint32_t BitMask = (1 << 0);
286
287 ReportItem->Value = 0;
288
289 if (ReportItem->ReportID)
290 {
291 if (ReportItem->ReportID != ReportData[0])
292 return false;
293
294 ReportData++;
295 }
296
297 while (DataBitsRem--)
298 {
299 if (ReportData[CurrentBit / 8] & (1 << (CurrentBit % 8)))
300 ReportItem->Value |= BitMask;
301
302 CurrentBit++;
303 BitMask <<= 1;
304 }
305
306 return true;
307 }
308
309 void USB_SetHIDReportItemInfo(uint8_t* ReportData, const HID_ReportItem_t* ReportItem)
310 {
311 uint16_t DataBitsRem = ReportItem->Attributes.BitSize;
312 uint16_t CurrentBit = ReportItem->BitOffset;
313 uint32_t BitMask = (1 << 0);
314
315 if (ReportItem->ReportID)
316 {
317 ReportData[0] = ReportItem->ReportID;
318 ReportData++;
319 }
320
321 while (DataBitsRem--)
322 {
323 if (ReportItem->Value & (1 << (CurrentBit % 8)))
324 ReportData[CurrentBit / 8] |= BitMask;
325
326 CurrentBit++;
327 BitMask <<= 1;
328 }
329 }
330
331 uint16_t USB_GetHIDReportSize(HID_ReportInfo_t* const ParserData, const uint8_t ReportID, const uint8_t ReportType)
332 {
333 for (uint8_t i = 0; i < HID_MAX_REPORT_IDS; i++)
334 {
335 if (ParserData->ReportIDSizes[i].ReportID == ReportID)
336 return ParserData->ReportIDSizes[i].ReportSizeBits[ReportType];
337 }
338
339 return 0;
340 }
341
342 #endif