USB Host Shield 2.0
confdescparser.h
Go to the documentation of this file.
1 /* Copyright (C) 2011 Circuits At Home, LTD. All rights reserved.
2 
3 This program is free software; you can redistribute it and/or modify
4 it under the terms of the GNU General Public License as published by
5 the Free Software Foundation; either version 2 of the License, or
6 (at your option) any later version.
7 
8 This program is distributed in the hope that it will be useful,
9 but WITHOUT ANY WARRANTY; without even the implied warranty of
10 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 GNU General Public License for more details.
12 
13 You should have received a copy of the GNU General Public License
14 along with this program; if not, write to the Free Software
15 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
16 
17 Contact information
18 -------------------
19 
20 Circuits At Home, LTD
21 Web : http://www.circuitsathome.com
22 e-mail : support@circuitsathome.com
23  */
24 #if !defined(_usb_h_) || defined(__CONFDESCPARSER_H__)
25 #error "Never include confdescparser.h directly; include Usb.h instead"
26 #else
27 
28 #define __CONFDESCPARSER_H__
29 
31 public:
32  //virtual void ConfigXtract(const USB_CONFIGURATION_DESCRIPTOR *conf) = 0;
33  //virtual void InterfaceXtract(uint8_t conf, const USB_INTERFACE_DESCRIPTOR *iface) = 0;
34 
35  virtual void EndpointXtract(uint8_t conf __attribute__((unused)), uint8_t iface __attribute__((unused)), uint8_t alt __attribute__((unused)), uint8_t proto __attribute__((unused)), const USB_ENDPOINT_DESCRIPTOR *ep __attribute__((unused))) {
36  };
37 };
38 
39 #define CP_MASK_COMPARE_CLASS 1
40 #define CP_MASK_COMPARE_SUBCLASS 2
41 #define CP_MASK_COMPARE_PROTOCOL 4
42 #define CP_MASK_COMPARE_ALL 7
43 
44 // Configuration Descriptor Parser Class Template
45 
46 template <const uint8_t CLASS_ID, const uint8_t SUBCLASS_ID, const uint8_t PROTOCOL_ID, const uint8_t MASK>
48  UsbConfigXtracter *theXtractor;
49  MultiValueBuffer theBuffer;
50  MultiByteValueParser valParser;
51  ByteSkipper theSkipper;
52  uint8_t varBuffer[16 /*sizeof(USB_CONFIGURATION_DESCRIPTOR)*/];
53 
54  uint8_t stateParseDescr; // ParseDescriptor state
55 
56  uint8_t dscrLen; // Descriptor length
57  uint8_t dscrType; // Descriptor type
58 
59  bool isGoodInterface; // Apropriate interface flag
60  uint8_t confValue; // Configuration value
61  uint8_t protoValue; // Protocol value
62  uint8_t ifaceNumber; // Interface number
63  uint8_t ifaceAltSet; // Interface alternate settings
64 
65  bool UseOr;
66  bool ParseDescriptor(uint8_t **pp, uint16_t *pcntdn);
67  void PrintHidDescriptor(const USB_HID_DESCRIPTOR *pDesc);
68 
69 public:
70 
71  void SetOR(void) {
72  UseOr = true;
73  }
75  void Parse(const uint16_t len, const uint8_t *pbuf, const uint16_t &offset);
76 };
77 
78 template <const uint8_t CLASS_ID, const uint8_t SUBCLASS_ID, const uint8_t PROTOCOL_ID, const uint8_t MASK>
80 theXtractor(xtractor),
81 stateParseDescr(0),
82 dscrLen(0),
83 dscrType(0),
84 UseOr(false) {
85  theBuffer.pValue = varBuffer;
86  valParser.Initialize(&theBuffer);
87  theSkipper.Initialize(&theBuffer);
88 };
89 
90 template <const uint8_t CLASS_ID, const uint8_t SUBCLASS_ID, const uint8_t PROTOCOL_ID, const uint8_t MASK>
91 void ConfigDescParser<CLASS_ID, SUBCLASS_ID, PROTOCOL_ID, MASK>::Parse(const uint16_t len, const uint8_t *pbuf, const uint16_t &offset __attribute__((unused))) {
92  uint16_t cntdn = (uint16_t)len;
93  uint8_t *p = (uint8_t*)pbuf;
94 
95  while(cntdn)
96  if(!ParseDescriptor(&p, &cntdn))
97  return;
98 }
99 
100 /* Parser for the configuration descriptor. Takes values for class, subclass, protocol fields in interface descriptor and
101  compare masks for them. When the match is found, calls EndpointXtract passing buffer containing endpoint descriptor */
102 template <const uint8_t CLASS_ID, const uint8_t SUBCLASS_ID, const uint8_t PROTOCOL_ID, const uint8_t MASK>
104  USB_CONFIGURATION_DESCRIPTOR* ucd = reinterpret_cast<USB_CONFIGURATION_DESCRIPTOR*>(varBuffer);
105  USB_INTERFACE_DESCRIPTOR* uid = reinterpret_cast<USB_INTERFACE_DESCRIPTOR*>(varBuffer);
106  switch(stateParseDescr) {
107  case 0:
108  theBuffer.valueSize = 2;
109  valParser.Initialize(&theBuffer);
110  stateParseDescr = 1;
111  case 1:
112  if(!valParser.Parse(pp, pcntdn))
113  return false;
114  dscrLen = *((uint8_t*)theBuffer.pValue);
115  dscrType = *((uint8_t*)theBuffer.pValue + 1);
116  stateParseDescr = 2;
117  case 2:
118  // This is a sort of hack. Assuming that two bytes are all ready in the buffer
119  // the pointer is positioned two bytes ahead in order for the rest of descriptor
120  // to be read right after the size and the type fields.
121  // This should be used carefully. varBuffer should be used directly to handle data
122  // in the buffer.
123  theBuffer.pValue = varBuffer + 2;
124  stateParseDescr = 3;
125  case 3:
126  switch(dscrType) {
128  isGoodInterface = false;
129  break;
132  case HID_DESCRIPTOR_HID:
133  break;
134  }
135  theBuffer.valueSize = dscrLen - 2;
136  valParser.Initialize(&theBuffer);
137  stateParseDescr = 4;
138  case 4:
139  switch(dscrType) {
141  if(!valParser.Parse(pp, pcntdn))
142  return false;
143  confValue = ucd->bConfigurationValue;
144  break;
146  if(!valParser.Parse(pp, pcntdn))
147  return false;
148  if((MASK & CP_MASK_COMPARE_CLASS) && uid->bInterfaceClass != CLASS_ID)
149  break;
150  if((MASK & CP_MASK_COMPARE_SUBCLASS) && uid->bInterfaceSubClass != SUBCLASS_ID)
151  break;
152  if(UseOr) {
153  if((!((MASK & CP_MASK_COMPARE_PROTOCOL) && uid->bInterfaceProtocol)))
154  break;
155  } else {
156  if((MASK & CP_MASK_COMPARE_PROTOCOL) && uid->bInterfaceProtocol != PROTOCOL_ID)
157  break;
158  }
159  isGoodInterface = true;
160  ifaceNumber = uid->bInterfaceNumber;
161  ifaceAltSet = uid->bAlternateSetting;
162  protoValue = uid->bInterfaceProtocol;
163  break;
165  if(!valParser.Parse(pp, pcntdn))
166  return false;
167  if(isGoodInterface)
168  if(theXtractor)
169  theXtractor->EndpointXtract(confValue, ifaceNumber, ifaceAltSet, protoValue, (USB_ENDPOINT_DESCRIPTOR*)varBuffer);
170  break;
171  //case HID_DESCRIPTOR_HID:
172  // if (!valParser.Parse(pp, pcntdn))
173  // return false;
174  // PrintHidDescriptor((const USB_HID_DESCRIPTOR*)varBuffer);
175  // break;
176  default:
177  if(!theSkipper.Skip(pp, pcntdn, dscrLen - 2))
178  return false;
179  }
180  theBuffer.pValue = varBuffer;
181  stateParseDescr = 0;
182  }
183  return true;
184 }
185 
186 template <const uint8_t CLASS_ID, const uint8_t SUBCLASS_ID, const uint8_t PROTOCOL_ID, const uint8_t MASK>
188  Notify(PSTR("\r\n\r\nHID Descriptor:\r\n"), 0x80);
189  Notify(PSTR("bDescLength:\t\t"), 0x80);
190  PrintHex<uint8_t > (pDesc->bLength, 0x80);
191 
192  Notify(PSTR("\r\nbDescriptorType:\t"), 0x80);
193  PrintHex<uint8_t > (pDesc->bDescriptorType, 0x80);
194 
195  Notify(PSTR("\r\nbcdHID:\t\t\t"), 0x80);
196  PrintHex<uint16_t > (pDesc->bcdHID, 0x80);
197 
198  Notify(PSTR("\r\nbCountryCode:\t\t"), 0x80);
199  PrintHex<uint8_t > (pDesc->bCountryCode, 0x80);
200 
201  Notify(PSTR("\r\nbNumDescriptors:\t"), 0x80);
202  PrintHex<uint8_t > (pDesc->bNumDescriptors, 0x80);
203 
204  for(uint8_t i = 0; i < pDesc->bNumDescriptors; i++) {
206 
207  Notify(PSTR("\r\nbDescrType:\t\t"), 0x80);
208  PrintHex<uint8_t > (pLT[i].bDescrType, 0x80);
209 
210  Notify(PSTR("\r\nwDescriptorLength:\t"), 0x80);
211  PrintHex<uint16_t > (pLT[i].wDescriptorLength, 0x80);
212  }
213  Notify(PSTR("\r\n"), 0x80);
214 }
215 
216 
217 #endif // __CONFDESCPARSER_H__
#define CP_MASK_COMPARE_PROTOCOL
virtual void EndpointXtract(uint8_t conf, uint8_t iface, uint8_t alt, uint8_t proto, const USB_ENDPOINT_DESCRIPTOR *ep)
#define USB_DESCRIPTOR_ENDPOINT
Definition: usb_ch9.h:74
uint8_t bLength
Definition: usb_ch9.h:159
void Parse(const uint16_t len, const uint8_t *pbuf, const uint16_t &offset)
#define CP_MASK_COMPARE_CLASS
#define USB_DESCRIPTOR_CONFIGURATION
Definition: usb_ch9.h:71
#define Notify(...)
Definition: message.h:51
bool Skip(uint8_t **pp, uint16_t *pcntdn, uint16_t bytes_to_skip)
Definition: parsetools.h:77
void Initialize(MultiValueBuffer *pbuf)
Definition: parsetools.h:72
uint8_t bCountryCode
Definition: usb_ch9.h:162
#define CP_MASK_COMPARE_SUBCLASS
void SetOR(void)
uint8_t bNumDescriptors
Definition: usb_ch9.h:163
bool Parse(uint8_t **pp, uint16_t *pcntdn)
Definition: parsetools.cpp:26
uint8_t bDescriptorType
Definition: usb_ch9.h:160
#define PSTR(str)
#define HID_DESCRIPTOR_HID
Definition: usb_ch9.h:80
uint8_t valueSize
Definition: parsetools.h:31
ConfigDescParser(UsbConfigXtracter *xtractor)
void Initialize(MultiValueBuffer *const pbuf)
Definition: parsetools.h:54
uint8_t bInterfaceSubClass
Definition: usb_ch9.h:142
uint16_t bcdHID
Definition: usb_ch9.h:161
uint8_t bInterfaceProtocol
Definition: usb_ch9.h:143
uint8_t bDescrType
Definition: usb_ch9.h:164
#define USB_DESCRIPTOR_INTERFACE
Definition: usb_ch9.h:73