Details | Last modification | View Log | RSS feed
Rev | Author | Line No. | Line |
---|---|---|---|
1702 | - | 1 | /* Copyright (C) 2010-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 | #ifndef __PTPCALLBACK_H__ |
||
18 | #define __PTPCALLBACK_H__ |
||
19 | |||
20 | #include <inttypes.h> |
||
21 | #include <avr/pgmspace.h> |
||
22 | #include "WProgram.h" |
||
23 | |||
24 | // Base class for incomming data parser |
||
25 | class PTPReadParser |
||
26 | { |
||
27 | public: |
||
28 | virtual void Parse(const uint16_t len, const uint8_t *pbuf, const uint32_t &offset) = 0; |
||
29 | }; |
||
30 | |||
31 | struct MultiValueBuffer |
||
32 | { |
||
33 | uint8_t valueSize; |
||
34 | void *pValue; |
||
35 | }; |
||
36 | |||
37 | class MultiByteValueParser |
||
38 | { |
||
39 | uint8_t * pBuf; |
||
40 | uint8_t countDown; |
||
41 | uint8_t valueSize; |
||
42 | |||
43 | public: |
||
44 | MultiByteValueParser() : pBuf(NULL), countDown(0), valueSize(0) {}; |
||
45 | |||
46 | const uint8_t* GetBuffer() { return pBuf; }; |
||
47 | |||
48 | void Initialize(MultiValueBuffer * const pbuf) |
||
49 | { |
||
50 | pBuf = (uint8_t*)pbuf->pValue; |
||
51 | countDown = valueSize = pbuf->valueSize; |
||
52 | }; |
||
53 | |||
54 | bool Parse(uint8_t **pp, uint16_t *pcntdn); |
||
55 | }; |
||
56 | |||
57 | class ByteSkipper |
||
58 | { |
||
59 | uint8_t *pBuf; |
||
60 | uint8_t nStage; |
||
61 | uint16_t countDown; |
||
62 | |||
63 | public: |
||
64 | ByteSkipper() : pBuf(NULL), nStage(0), countDown(0) {}; |
||
65 | |||
66 | void Initialize(MultiValueBuffer *pbuf) |
||
67 | { |
||
68 | pBuf = (uint8_t*)pbuf->pValue; |
||
69 | countDown = 0; |
||
70 | }; |
||
71 | |||
72 | bool Skip(uint8_t **pp, uint16_t *pcntdn, uint16_t bytes_to_skip) |
||
73 | { |
||
74 | switch (nStage) |
||
75 | { |
||
76 | case 0: |
||
77 | countDown = bytes_to_skip; |
||
78 | nStage ++; |
||
79 | case 1: |
||
80 | for (; countDown && (*pcntdn); countDown--, (*pp)++, (*pcntdn)--); |
||
81 | |||
82 | if (!countDown) |
||
83 | nStage = 0; |
||
84 | }; |
||
85 | return (!countDown); |
||
86 | }; |
||
87 | }; |
||
88 | |||
89 | // Pointer to a callback function triggered for each element of PTP array when used with PTPArrayParser |
||
90 | typedef void (*PTP_ARRAY_EL_FUNC)(const MultiValueBuffer * const p, uint32_t count, const void *me); |
||
91 | |||
92 | class PTPListParser |
||
93 | { |
||
94 | public: |
||
95 | enum ParseMode { modeArray, modeRange/*, modeEnum*/ }; |
||
96 | |||
97 | private: |
||
98 | uint8_t nStage; |
||
99 | uint8_t enStage; |
||
100 | |||
101 | uint32_t arLen; |
||
102 | uint32_t arLenCntdn; |
||
103 | |||
104 | uint8_t lenSize; // size of the array length field in bytes |
||
105 | uint8_t valSize; // size of the array element in bytes |
||
106 | |||
107 | MultiValueBuffer *pBuf; |
||
108 | |||
109 | // The only parser for both size and array element parsing |
||
110 | MultiByteValueParser theParser; |
||
111 | |||
112 | uint8_t /*ParseMode*/ prsMode; |
||
113 | |||
114 | public: |
||
115 | PTPListParser() : |
||
116 | pBuf(NULL), |
||
117 | nStage(0), |
||
118 | enStage(0), |
||
119 | arLenCntdn(0), |
||
120 | arLen(0), |
||
121 | lenSize(0), |
||
122 | valSize(0), |
||
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 | { |
||
128 | pBuf = p; |
||
129 | lenSize = len_size; |
||
130 | valSize = val_size; |
||
131 | prsMode = mode; |
||
132 | |||
133 | if (prsMode == modeRange) |
||
134 | { |
||
135 | arLenCntdn = arLen = 3; |
||
136 | nStage = 2; |
||
137 | } |
||
138 | else |
||
139 | { |
||
140 | arLenCntdn = arLen = 0; |
||
141 | nStage = 0; |
||
142 | } |
||
143 | enStage = 0; |
||
144 | theParser.Initialize(p); |
||
145 | }; |
||
146 | |||
147 | bool Parse(uint8_t **pp, uint16_t *pcntdn, PTP_ARRAY_EL_FUNC pf, const void *me = NULL); |
||
148 | }; |
||
149 | |||
150 | |||
151 | // Base class for outgoing data supplier |
||
152 | class PTPDataSupplier |
||
153 | { |
||
154 | public: |
||
155 | virtual uint32_t GetDataSize() = 0; |
||
156 | virtual void GetData(const uint16_t len, uint8_t *pbuf) = 0; |
||
157 | }; |
||
158 | |||
159 | #endif // __PTPCALLBACK_H__ |