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