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 
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  // fall through
83  case 1:
84  for(; countDown && (*pcntdn); countDown--, (*pp)++, (*pcntdn)--);
85 
86  if(!countDown)
87  nStage = 0;
88  };
89  return (!countDown);
90  };
91 };
92 
93 // Pointer to a callback function triggered for each element of PTP array when used with PTPArrayParser
94 typedef void (*PTP_ARRAY_EL_FUNC)(const MultiValueBuffer * const p, uint32_t count, const void *me);
95 
97 public:
98 
99  enum ParseMode {
100  modeArray, modeRange/*, modeEnum*/
101  };
102 
103 private:
104  uint8_t nStage;
105  uint8_t enStage;
106 
107  uint32_t arLen;
108  uint32_t arLenCntdn;
109 
110  uint8_t lenSize; // size of the array length field in bytes
111  uint8_t valSize; // size of the array element in bytes
112 
113  MultiValueBuffer *pBuf;
114 
115  // The only parser for both size and array element parsing
116  MultiByteValueParser theParser;
117 
118  uint8_t /*ParseMode*/ prsMode;
119 
120 public:
121 
123  nStage(0),
124  enStage(0),
125  arLen(0),
126  arLenCntdn(0),
127  lenSize(0),
128  valSize(0),
129  pBuf(NULL),
130  prsMode(modeArray) {
131  };
132 
133  void Initialize(const uint8_t len_size, const uint8_t val_size, MultiValueBuffer * const p, const uint8_t mode = modeArray) {
134  pBuf = p;
135  lenSize = len_size;
136  valSize = val_size;
137  prsMode = mode;
138 
139  if(prsMode == modeRange) {
140  arLenCntdn = arLen = 3;
141  nStage = 2;
142  } else {
143  arLenCntdn = arLen = 0;
144  nStage = 0;
145  }
146  enStage = 0;
147  theParser.Initialize(p);
148  };
149 
150  bool Parse(uint8_t **pp, uint16_t *pcntdn, PTP_ARRAY_EL_FUNC pf, const void *me = NULL);
151 };
152 
153 #endif // __PARSETOOLS_H__
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(MultiValueBuffer *const pbuf)
Definition: parsetools.h:54
bool Parse(uint8_t **pp, uint16_t *pcntdn)
Definition: parsetools.cpp:26
const uint8_t * GetBuffer()
Definition: parsetools.h:50
bool Parse(uint8_t **pp, uint16_t *pcntdn, PTP_ARRAY_EL_FUNC pf, const void *me=NULL)
Definition: parsetools.cpp:41
void Initialize(const uint8_t len_size, const uint8_t val_size, MultiValueBuffer *const p, const uint8_t mode=modeArray)
Definition: parsetools.h:133
void(* PTP_ARRAY_EL_FUNC)(const MultiValueBuffer *const p, uint32_t count, const void *me)
Definition: parsetools.h:94
uint8_t valueSize
Definition: parsetools.h:31