USB Host Shield 2.0
 All Classes Files Functions Variables Typedefs Enumerations Enumerator Macros Pages
parsetools.h
Go to the documentation of this file.
1 /* Copyright (C) 2011 Circuits At Home, LTD. All rights reserved.
2 
3 This software may be distributed and modified under the terms of the GNU
4 General Public License version 2 (GPL2) as published by the Free Software
5 Foundation and appearing in the file GPL2.TXT included in the packaging of
6 this file. Please note that GPL2 Section 2[b] requires that all works based
7 on this software must also be made publicly available under the terms of
8 the GPL2 ("Copyleft").
9 
10 Contact information
11 -------------------
12 
13 Circuits At Home, LTD
14 Web : http://www.circuitsathome.com
15 e-mail : support@circuitsathome.com
16 */
17 #if !defined(__PARSETOOLS_H__)
18 #define __PARSETOOLS_H__
19 
20 #include <inttypes.h>
21 #include <avr/pgmspace.h>
22 #include "printhex.h"
23 #include "hexdump.h"
24 #include "message.h"
25 
26 #if defined(ARDUINO) && ARDUINO >=100
27 #include "Arduino.h"
28 #else
29 #include <WProgram.h>
30 #endif
31 
33 {
34  uint8_t valueSize;
35  void *pValue;
36 };
37 
39 {
40  uint8_t * pBuf;
41  uint8_t countDown;
42  uint8_t valueSize;
43 
44 public:
45  MultiByteValueParser() : pBuf(NULL), countDown(0), valueSize(0) {};
46 
47  const uint8_t* GetBuffer() { return pBuf; };
48 
49  void Initialize(MultiValueBuffer * const pbuf)
50  {
51  pBuf = (uint8_t*)pbuf->pValue;
52  countDown = valueSize = pbuf->valueSize;
53  };
54 
55  bool Parse(uint8_t **pp, uint16_t *pcntdn);
56 };
57 
59 {
60  uint8_t *pBuf;
61  uint8_t nStage;
62  uint16_t countDown;
63 
64 public:
65  ByteSkipper() : pBuf(NULL), nStage(0), countDown(0) {};
66 
68  {
69  pBuf = (uint8_t*)pbuf->pValue;
70  countDown = 0;
71  };
72 
73  bool Skip(uint8_t **pp, uint16_t *pcntdn, uint16_t bytes_to_skip)
74  {
75  switch (nStage)
76  {
77  case 0:
78  countDown = bytes_to_skip;
79  nStage ++;
80  case 1:
81  for (; countDown && (*pcntdn); countDown--, (*pp)++, (*pcntdn)--);
82 
83  if (!countDown)
84  nStage = 0;
85  };
86  return (!countDown);
87  };
88 };
89 
90 // Pointer to a callback function triggered for each element of PTP array when used with PTPArrayParser
91 typedef void (*PTP_ARRAY_EL_FUNC)(const MultiValueBuffer * const p, uint32_t count, const void *me);
92 
94 {
95 public:
96  enum ParseMode { modeArray, modeRange/*, modeEnum*/ };
97 
98 private:
99  uint8_t nStage;
100  uint8_t enStage;
101 
102  uint32_t arLen;
103  uint32_t arLenCntdn;
104 
105  uint8_t lenSize; // size of the array length field in bytes
106  uint8_t valSize; // size of the array element in bytes
107 
108  MultiValueBuffer *pBuf;
109 
110  // The only parser for both size and array element parsing
111  MultiByteValueParser theParser;
112 
113  uint8_t /*ParseMode*/ prsMode;
114 
115 public:
117  pBuf(NULL),
118  nStage(0),
119  enStage(0),
120  arLenCntdn(0),
121  arLen(0),
122  lenSize(0),
123  valSize(0),
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  {
129  pBuf = p;
130  lenSize = len_size;
131  valSize = val_size;
132  prsMode = mode;
133 
134  if (prsMode == modeRange)
135  {
136  arLenCntdn = arLen = 3;
137  nStage = 2;
138  }
139  else
140  {
141  arLenCntdn = arLen = 0;
142  nStage = 0;
143  }
144  enStage = 0;
145  theParser.Initialize(p);
146  };
147 
148  bool Parse(uint8_t **pp, uint16_t *pcntdn, PTP_ARRAY_EL_FUNC pf, const void *me = NULL);
149 };
150 
151 #endif // __PARSETOOLS_H__