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