You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
This repo is archived. You can view files and clone it, but cannot push or open issues/pull-requests.

parsetools.h 3.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. /* Copyright (C) 2011 Circuits At Home, LTD. All rights reserved.
  2. This software may be distributed and modified under the terms of the GNU
  3. General Public License version 2 (GPL2) as published by the Free Software
  4. Foundation and appearing in the file GPL2.TXT included in the packaging of
  5. this file. Please note that GPL2 Section 2[b] requires that all works based
  6. on this software must also be made publicly available under the terms of
  7. the GPL2 ("Copyleft").
  8. Contact information
  9. -------------------
  10. Circuits At Home, LTD
  11. Web : http://www.circuitsathome.com
  12. e-mail : [email protected]
  13. */
  14. #if !defined(_usb_h_) || defined(__PARSETOOLS_H__)
  15. #error "Never include parsetools.h directly; include Usb.h instead"
  16. #else
  17. #define __PARSETOOLS_H__
  18. struct MultiValueBuffer {
  19. uint8_t valueSize;
  20. void *pValue;
  21. } __attribute__((packed));
  22. class MultiByteValueParser {
  23. uint8_t * pBuf;
  24. uint8_t countDown;
  25. uint8_t valueSize;
  26. public:
  27. MultiByteValueParser() : pBuf(NULL), countDown(0), valueSize(0) {
  28. };
  29. const uint8_t* GetBuffer() {
  30. return pBuf;
  31. };
  32. void Initialize(MultiValueBuffer * const pbuf) {
  33. pBuf = (uint8_t*)pbuf->pValue;
  34. countDown = valueSize = pbuf->valueSize;
  35. };
  36. bool Parse(uint8_t **pp, uint16_t *pcntdn);
  37. };
  38. class ByteSkipper {
  39. uint8_t *pBuf;
  40. uint8_t nStage;
  41. uint16_t countDown;
  42. public:
  43. ByteSkipper() : pBuf(NULL), nStage(0), countDown(0) {
  44. };
  45. void Initialize(MultiValueBuffer *pbuf) {
  46. pBuf = (uint8_t*)pbuf->pValue;
  47. countDown = 0;
  48. };
  49. bool Skip(uint8_t **pp, uint16_t *pcntdn, uint16_t bytes_to_skip) {
  50. switch(nStage) {
  51. case 0:
  52. countDown = bytes_to_skip;
  53. nStage++;
  54. case 1:
  55. for(; countDown && (*pcntdn); countDown--, (*pp)++, (*pcntdn)--);
  56. if(!countDown)
  57. nStage = 0;
  58. };
  59. return (!countDown);
  60. };
  61. };
  62. // Pointer to a callback function triggered for each element of PTP array when used with PTPArrayParser
  63. typedef void (*PTP_ARRAY_EL_FUNC)(const MultiValueBuffer * const p, uint32_t count, const void *me);
  64. class PTPListParser {
  65. public:
  66. enum ParseMode {
  67. modeArray, modeRange/*, modeEnum*/
  68. };
  69. private:
  70. uint8_t nStage;
  71. uint8_t enStage;
  72. uint32_t arLen;
  73. uint32_t arLenCntdn;
  74. uint8_t lenSize; // size of the array length field in bytes
  75. uint8_t valSize; // size of the array element in bytes
  76. MultiValueBuffer *pBuf;
  77. // The only parser for both size and array element parsing
  78. MultiByteValueParser theParser;
  79. uint8_t /*ParseMode*/ prsMode;
  80. public:
  81. PTPListParser() :
  82. nStage(0),
  83. enStage(0),
  84. arLen(0),
  85. arLenCntdn(0),
  86. lenSize(0),
  87. valSize(0),
  88. pBuf(NULL),
  89. prsMode(modeArray) {
  90. };
  91. void Initialize(const uint8_t len_size, const uint8_t val_size, MultiValueBuffer * const p, const uint8_t mode = modeArray) {
  92. pBuf = p;
  93. lenSize = len_size;
  94. valSize = val_size;
  95. prsMode = mode;
  96. if(prsMode == modeRange) {
  97. arLenCntdn = arLen = 3;
  98. nStage = 2;
  99. } else {
  100. arLenCntdn = arLen = 0;
  101. nStage = 0;
  102. }
  103. enStage = 0;
  104. theParser.Initialize(p);
  105. };
  106. bool Parse(uint8_t **pp, uint16_t *pcntdn, PTP_ARRAY_EL_FUNC pf, const void *me = NULL);
  107. };
  108. #endif // __PARSETOOLS_H__