1 /* Copyright (C) 2011 Circuits At Home, LTD. All rights reserved.
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
14 Web : http://www.circuitsathome.com
15 e-mail : support@circuitsathome.com
18 #if !defined(_usb_h_) || defined(__PARSETOOLS_H__)
19 #error "Never include parsetools.h directly; include Usb.h instead"
21 #define __PARSETOOLS_H__
23 struct MultiValueBuffer
{
26 } __attribute__((packed
));
28 class MultiByteValueParser
{
35 MultiByteValueParser() : pBuf(NULL
), countDown(0), valueSize(0) {
38 const uint8_t* GetBuffer() {
42 void Initialize(MultiValueBuffer
* const pbuf
) {
43 pBuf
= (uint8_t*)pbuf
->pValue
;
44 countDown
= valueSize
= pbuf
->valueSize
;
47 bool Parse(uint8_t **pp
, uint16_t *pcntdn
);
57 ByteSkipper() : pBuf(NULL
), nStage(0), countDown(0) {
60 void Initialize(MultiValueBuffer
*pbuf
) {
61 pBuf
= (uint8_t*)pbuf
->pValue
;
65 bool Skip(uint8_t **pp
, uint16_t *pcntdn
, uint16_t bytes_to_skip
) {
68 countDown
= bytes_to_skip
;
71 for(; countDown
&& (*pcntdn
); countDown
--, (*pp
)++, (*pcntdn
)--);
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
);
87 modeArray
, modeRange
/*, modeEnum*/
97 uint8_t lenSize
; // size of the array length field in bytes
98 uint8_t valSize
; // size of the array element in bytes
100 MultiValueBuffer
*pBuf
;
102 // The only parser for both size and array element parsing
103 MultiByteValueParser theParser
;
105 uint8_t /*ParseMode*/ prsMode
;
120 void Initialize(const uint8_t len_size
, const uint8_t val_size
, MultiValueBuffer
* const p
, const uint8_t mode
= modeArray
) {
126 if(prsMode
== modeRange
) {
127 arLenCntdn
= arLen
= 3;
130 arLenCntdn
= arLen
= 0;
134 theParser
.Initialize(p
);
137 bool Parse(uint8_t **pp
, uint16_t *pcntdn
, PTP_ARRAY_EL_FUNC pf
, const void *me
= NULL
);
140 #endif // __PARSETOOLS_H__