Zoltan Hudak / UsbHostMAX3421E

Dependents:   UsbHostMAX3421E_Hello

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers parsetools.h Source File

parsetools.h

00001 /* Copyright (C) 2011 Circuits At Home, LTD. All rights reserved.
00002 
00003 This program is free software; you can redistribute it and/or modify
00004 it under the terms of the GNU General Public License as published by
00005 the Free Software Foundation; either version 2 of the License, or
00006 (at your option) any later version.
00007 
00008 This program is distributed in the hope that it will be useful,
00009 but WITHOUT ANY WARRANTY; without even the implied warranty of
00010 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00011 GNU General Public License for more details.
00012 
00013 You should have received a copy of the GNU General Public License
00014 along with this program; if not, write to the Free Software
00015 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
00016 
00017 Contact information
00018 -------------------
00019 
00020 Circuits At Home, LTD
00021 Web      :  http://www.circuitsathome.com
00022 e-mail   :  support@circuitsathome.com
00023  */
00024 
00025 #if !defined(_usb_h_) || defined(__PARSETOOLS_H__)
00026 #error "Never include parsetools.h directly; include Usb.h instead"
00027 #else
00028 #define __PARSETOOLS_H__
00029 
00030 struct MultiValueBuffer {
00031         uint8_t valueSize;
00032         void *pValue;
00033 
00034 public:
00035 
00036         MultiValueBuffer() : valueSize(0), pValue(NULL) {
00037         };
00038 } __attribute__((packed));
00039 
00040 class MultiByteValueParser {
00041         uint8_t * pBuf;
00042         uint8_t countDown;
00043         uint8_t valueSize;
00044 
00045 public:
00046 
00047         MultiByteValueParser() : pBuf(NULL), countDown(0), valueSize(0) {
00048         };
00049 
00050         const uint8_t* GetBuffer() {
00051                 return pBuf;
00052         };
00053 
00054         void Initialize(MultiValueBuffer * const pbuf) {
00055                 pBuf = (uint8_t*)pbuf->pValue;
00056                 countDown = valueSize = pbuf->valueSize;
00057         };
00058 
00059         bool Parse(uint8_t **pp, uint16_t *pcntdn);
00060 };
00061 
00062 class ByteSkipper {
00063         uint8_t *pBuf;
00064         uint8_t nStage;
00065         uint16_t countDown;
00066 
00067 public:
00068 
00069         ByteSkipper() : pBuf(NULL), nStage(0), countDown(0) {
00070         };
00071 
00072         void Initialize(MultiValueBuffer *pbuf) {
00073                 pBuf = (uint8_t*)pbuf->pValue;
00074                 countDown = 0;
00075         };
00076 
00077         bool Skip(uint8_t **pp, uint16_t *pcntdn, uint16_t bytes_to_skip) {
00078                 switch(nStage) {
00079                         case 0:
00080                                 countDown = bytes_to_skip;
00081                                 nStage++;
00082                         case 1:
00083                                 for(; countDown && (*pcntdn); countDown--, (*pp)++, (*pcntdn)--);
00084 
00085                                 if(!countDown)
00086                                         nStage = 0;
00087                 };
00088                 return (!countDown);
00089         };
00090 };
00091 
00092 // Pointer to a callback function triggered for each element of PTP array when used with PTPArrayParser
00093 typedef void (*PTP_ARRAY_EL_FUNC)(const MultiValueBuffer * const p, uint32_t count, const void *me);
00094 
00095 class PTPListParser {
00096 public:
00097 
00098         enum ParseMode {
00099                 modeArray, modeRange/*, modeEnum*/
00100         };
00101 
00102 private:
00103         uint8_t nStage;
00104         uint8_t enStage;
00105 
00106         uint32_t arLen;
00107         uint32_t arLenCntdn;
00108 
00109         uint8_t lenSize; // size of the array length field in bytes
00110         uint8_t valSize; // size of the array element in bytes
00111 
00112         MultiValueBuffer *pBuf;
00113 
00114         // The only parser for both size and array element parsing
00115         MultiByteValueParser theParser;
00116 
00117         uint8_t /*ParseMode*/ prsMode;
00118 
00119 public:
00120 
00121         PTPListParser() :
00122         nStage(0),
00123         enStage(0),
00124         arLen(0),
00125         arLenCntdn(0),
00126         lenSize(0),
00127         valSize(0),
00128         pBuf(NULL),
00129         prsMode(modeArray) {
00130         };
00131 
00132         void Initialize(const uint8_t len_size, const uint8_t val_size, MultiValueBuffer * const p, const uint8_t mode = modeArray) {
00133                 pBuf = p;
00134                 lenSize = len_size;
00135                 valSize = val_size;
00136                 prsMode = mode;
00137 
00138                 if(prsMode == modeRange) {
00139                         arLenCntdn = arLen = 3;
00140                         nStage = 2;
00141                 } else {
00142                         arLenCntdn = arLen = 0;
00143                         nStage = 0;
00144                 }
00145                 enStage = 0;
00146                 theParser.Initialize(p);
00147         };
00148 
00149         bool Parse(uint8_t **pp, uint16_t *pcntdn, PTP_ARRAY_EL_FUNC pf, const void *me = NULL);
00150 };
00151 
00152 #endif // __PARSETOOLS_H__