Prusa MINI Firmware overview
parsetools.h
Go to the documentation of this file.
1 /**
2  * Copyright (C) 2011 Circuits At Home, LTD. All rights reserved.
3  *
4  * This program is free software; you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License as published by
6  * the Free Software Foundation; either version 2 of the License, or
7  * (at your option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program; if not, write to the Free Software
16  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17  *
18  * Contact information
19  * -------------------
20  *
21  * Circuits At Home, LTD
22  * Web : http://www.circuitsathome.com
23  * e-mail : support@circuitsathome.com
24  */
25 #pragma once
26 
27 #ifndef _usb_h_
28  #error "Never include parsetools.h directly; include Usb.h instead"
29 #endif
30 
33  void *pValue;
34 } __attribute__((packed));
35 
37  uint8_t * pBuf;
38  uint8_t countDown;
39  uint8_t valueSize;
40 
41 public:
42 
43  MultiByteValueParser() : pBuf(nullptr), countDown(0), valueSize(0) {
44  };
45 
46  const uint8_t* GetBuffer() { return pBuf; }
47 
49  pBuf = (uint8_t*)pbuf->pValue;
50  countDown = valueSize = pbuf->valueSize;
51  }
52 
53  bool Parse(uint8_t **pp, uint16_t *pcntdn);
54 };
55 
56 class ByteSkipper {
57  uint8_t *pBuf;
58  uint8_t nStage;
59  uint16_t countDown;
60 
61 public:
62 
63  ByteSkipper() : pBuf(nullptr), nStage(0), countDown(0) {
64  }
65 
67  pBuf = (uint8_t*)pbuf->pValue;
68  countDown = 0;
69  }
70 
71  bool Skip(uint8_t **pp, uint16_t *pcntdn, uint16_t bytes_to_skip) {
72  switch (nStage) {
73  case 0:
74  countDown = bytes_to_skip;
75  nStage++;
76  case 1:
77  for (; countDown && (*pcntdn); countDown--, (*pp)++, (*pcntdn)--);
78 
79  if (!countDown)
80  nStage = 0;
81  }
82  return (!countDown);
83  }
84 };
85 
86 // Pointer to a callback function triggered for each element of PTP array when used with PTPArrayParser
87 typedef void (*PTP_ARRAY_EL_FUNC)(const MultiValueBuffer * const p, uint32_t count, const void *me);
88 
90 public:
91 
92  enum ParseMode {
93  modeArray, modeRange/*, modeEnum*/
94  };
95 
96 private:
97  uint8_t nStage;
98  uint8_t enStage;
99 
100  uint32_t arLen;
101  uint32_t arLenCntdn;
102 
103  uint8_t lenSize; // size of the array length field in bytes
104  uint8_t valSize; // size of the array element in bytes
105 
106  MultiValueBuffer *pBuf;
107 
108  // The only parser for both size and array element parsing
109  MultiByteValueParser theParser;
110 
111  uint8_t /*ParseMode*/ prsMode;
112 
113 public:
114 
116  nStage(0),
117  enStage(0),
118  arLen(0),
119  arLenCntdn(0),
120  lenSize(0),
121  valSize(0),
122  pBuf(nullptr),
123  prsMode(modeArray) {}
124  ;
125 
126  void Initialize(const uint8_t len_size, const uint8_t val_size, MultiValueBuffer * const p, const uint8_t mode = modeArray) {
127  pBuf = p;
128  lenSize = len_size;
129  valSize = val_size;
130  prsMode = mode;
131 
132  if (prsMode == modeRange) {
133  arLenCntdn = arLen = 3;
134  nStage = 2;
135  }
136  else {
137  arLenCntdn = arLen = 0;
138  nStage = 0;
139  }
140  enStage = 0;
141  theParser.Initialize(p);
142  }
143 
144  bool Parse(uint8_t **pp, uint16_t *pcntdn, PTP_ARRAY_EL_FUNC pf, const void *me = nullptr);
145 };
PTPListParser::ParseMode
ParseMode
Definition: parsetools.h:92
ByteSkipper::Initialize
void Initialize(MultiValueBuffer *pbuf)
Definition: parsetools.h:66
PTPListParser::Initialize
void Initialize(const uint8_t len_size, const uint8_t val_size, MultiValueBuffer *const p, const uint8_t mode=modeArray)
Definition: parsetools.h:126
Notify
#define Notify(...)
Definition: message.h:53
MultiByteValueParser::MultiByteValueParser
MultiByteValueParser()
Definition: parsetools.h:43
MultiByteValueParser::Parse
bool Parse(uint8_t **pp, uint16_t *pcntdn)
PSTR
#define PSTR(str)
Definition: pgmspace.h:31
MultiByteValueParser::Initialize
void Initialize(MultiValueBuffer *const pbuf)
Definition: parsetools.h:48
void
void
Definition: png.h:1083
ByteSkipper
Definition: parsetools.h:56
__attribute__
class MultiByteValueParser __attribute__
PTP_ARRAY_EL_FUNC
void(* PTP_ARRAY_EL_FUNC)(const MultiValueBuffer *const p, uint32_t count, const void *me)
Definition: parsetools.h:87
MultiByteValueParser::GetBuffer
const uint8_t * GetBuffer()
Definition: parsetools.h:46
valueSize
uint8_t valueSize
Definition: parsetools.h:79
uint8_t
const uint8_t[]
Definition: 404_html.c:3
ByteSkipper::ByteSkipper
ByteSkipper()
Definition: parsetools.h:63
ByteSkipper::Skip
bool Skip(uint8_t **pp, uint16_t *pcntdn, uint16_t bytes_to_skip)
Definition: parsetools.h:71
Usb.h
MultiValueBuffer::pValue
void * pValue
Definition: parsetools.h:33
PTPListParser::Parse
bool Parse(uint8_t **pp, uint16_t *pcntdn, PTP_ARRAY_EL_FUNC pf, const void *me=nullptr)
MultiValueBuffer
Definition: parsetools.h:31
MultiValueBuffer::valueSize
uint8_t valueSize
Definition: parsetools.h:32
PTPListParser::PTPListParser
PTPListParser()
Definition: parsetools.h:115
mode
png_structrp int mode
Definition: png.h:1139
pbuf
Definition: pbuf.h:142
PTPListParser::modeRange
Definition: parsetools.h:93
MultiByteValueParser
Definition: parsetools.h:36
PTPListParser::modeArray
Definition: parsetools.h:93
PTPListParser
Definition: parsetools.h:89