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