USB Host Shield 2.0
parsetools.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 
25 #if !defined(_usb_h_) || defined(__PARSETOOLS_H__)
26 #error "Never include parsetools.h directly; include Usb.h instead"
27 #else
28 #define __PARSETOOLS_H__
29 
31  uint8_t valueSize;
32  void *pValue;
33 
34 public:
35 
36  MultiValueBuffer() : valueSize(0), pValue(NULL) {
37  };
38 } __attribute__((packed));
39 
41  uint8_t * pBuf;
42  uint8_t countDown;
43  uint8_t valueSize;
44 
45 public:
46 
47  MultiByteValueParser() : pBuf(NULL), countDown(0), valueSize(0) {
48  };
49 
50  const uint8_t* GetBuffer() {
51  return pBuf;
52  };
53 
54  void Initialize(MultiValueBuffer * const pbuf) {
55  pBuf = (uint8_t*)pbuf->pValue;
56  countDown = valueSize = pbuf->valueSize;
57  };
58 
59  bool Parse(uint8_t **pp, uint16_t *pcntdn);
60 };
61 
62 class ByteSkipper {
63  uint8_t *pBuf;
64  uint8_t nStage;
65  uint16_t countDown;
66 
67 public:
68 
69  ByteSkipper() : pBuf(NULL), nStage(0), countDown(0) {
70  };
71 
73  pBuf = (uint8_t*)pbuf->pValue;
74  countDown = 0;
75  };
76 
77  bool Skip(uint8_t **pp, uint16_t *pcntdn, uint16_t bytes_to_skip) {
78  switch(nStage) {
79  case 0:
80  countDown = bytes_to_skip;
81  nStage++;
82  case 1:
83  for(; countDown && (*pcntdn); countDown--, (*pp)++, (*pcntdn)--);
84 
85  if(!countDown)
86  nStage = 0;
87  };
88  return (!countDown);
89  };
90 };
91 
92 // Pointer to a callback function triggered for each element of PTP array when used with PTPArrayParser
93 typedef void (*PTP_ARRAY_EL_FUNC)(const MultiValueBuffer * const p, uint32_t count, const void *me);
94 
96 public:
97 
98  enum ParseMode {
99  modeArray, modeRange/*, modeEnum*/
100  };
101 
102 private:
103  uint8_t nStage;
104  uint8_t enStage;
105 
106  uint32_t arLen;
107  uint32_t arLenCntdn;
108 
109  uint8_t lenSize; // size of the array length field in bytes
110  uint8_t valSize; // size of the array element in bytes
111 
112  MultiValueBuffer *pBuf;
113 
114  // The only parser for both size and array element parsing
115  MultiByteValueParser theParser;
116 
117  uint8_t /*ParseMode*/ prsMode;
118 
119 public:
120 
122  nStage(0),
123  enStage(0),
124  arLen(0),
125  arLenCntdn(0),
126  lenSize(0),
127  valSize(0),
128  pBuf(NULL),
129  prsMode(modeArray) {
130  };
131 
132  void Initialize(const uint8_t len_size, const uint8_t val_size, MultiValueBuffer * const p, const uint8_t mode = modeArray) {
133  pBuf = p;
134  lenSize = len_size;
135  valSize = val_size;
136  prsMode = mode;
137 
138  if(prsMode == modeRange) {
139  arLenCntdn = arLen = 3;
140  nStage = 2;
141  } else {
142  arLenCntdn = arLen = 0;
143  nStage = 0;
144  }
145  enStage = 0;
146  theParser.Initialize(p);
147  };
148 
149  bool Parse(uint8_t **pp, uint16_t *pcntdn, PTP_ARRAY_EL_FUNC pf, const void *me = NULL);
150 };
151 
152 #endif // __PARSETOOLS_H__
void(* PTP_ARRAY_EL_FUNC)(const MultiValueBuffer *const p, uint32_t count, const void *me)
Definition: parsetools.h:93
const uint8_t * GetBuffer()
Definition: parsetools.h:50
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
void Initialize(const uint8_t len_size, const uint8_t val_size, MultiValueBuffer *const p, const uint8_t mode=modeArray)
Definition: parsetools.h:132
uint8_t valueSize
Definition: parsetools.h:31
void Initialize(MultiValueBuffer *const pbuf)
Definition: parsetools.h:54