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