Open Lighting Architecture  0.9.4
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
BaseInflator.h
1 /*
2  * This program is free software; you can redistribute it and/or modify
3  * it under the terms of the GNU General Public License as published by
4  * the Free Software Foundation; either version 2 of the License, or
5  * (at your option) any later version.
6  *
7  * This program is distributed in the hope that it will be useful,
8  * but WITHOUT ANY WARRANTY; without even the implied warranty of
9  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10  * GNU Library General Public License for more details.
11  *
12  * You should have received a copy of the GNU General Public License
13  * along with this program; if not, write to the Free Software
14  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
15  *
16  * BaseInflator.h
17  * Provides the base class for inflating PDU blocks.
18  * Copyright (C) 2007 Simon Newton
19  *
20  * The BaseInflator takes care of most of the heavy lifting when inflating PDU
21  * blocks. To create a specific Inflator, subclass BaseInflator and implement
22  * the Id() and DecodeHeader() methods.
23  */
24 
25 #ifndef PLUGINS_E131_E131_BASEINFLATOR_H_
26 #define PLUGINS_E131_E131_BASEINFLATOR_H_
27 
28 #include <stdint.h>
29 #include <map>
30 #include "plugins/e131/e131/HeaderSet.h"
31 #include "plugins/e131/e131/PDU.h"
32 
33 namespace ola {
34 namespace plugin {
35 namespace e131 {
36 
37 class BaseInflatorTest;
38 
39 
44  public:
45  virtual ~InflatorInterface() {}
46 
47  /*
48  * Return the id for this inflator
49  */
50  virtual uint32_t Id() const = 0;
51 
52  /*
53  * Parse a block of PDU data
54  */
55  virtual unsigned int InflatePDUBlock(HeaderSet *headers,
56  const uint8_t *data,
57  unsigned int len) = 0;
58 };
59 
60 
61 /*
62  * An abstract PDU inflator
63  */
65  friend class BaseInflatorTest;
66 
67  public:
68  explicit BaseInflator(PDU::vector_size v_size = PDU::FOUR_BYTES);
69  virtual ~BaseInflator() {}
70 
71  /*
72  * Add another inflator as a handler. Ownership is not transferred.
73  */
74  bool AddInflator(InflatorInterface *inflator);
75 
76  /*
77  * Return the inflator used for a particular vector.
78  */
79  class InflatorInterface *GetInflator(uint32_t vector) const;
80 
81  /*
82  * Parse a block of PDU data
83  */
84  virtual unsigned int InflatePDUBlock(HeaderSet *headers,
85  const uint8_t *data,
86  unsigned int len);
87 
88  // masks for the flag fields
89  // This indicates a 20 bit length field (default is 12 bits)
90  static const uint8_t LFLAG_MASK = 0x80;
91  // This masks the first 4 bits of the length field
92  static const uint8_t LENGTH_MASK = 0x0F;
93 
94  protected:
95  uint32_t m_last_vector;
96  bool m_vector_set;
97  PDU::vector_size m_vector_size; // size of the vector field
98  // map protos to inflators
99  std::map<uint32_t, InflatorInterface*> m_proto_map;
100 
101  // Reset repeated pdu fields
102  virtual void ResetPDUFields();
103  virtual void ResetHeaderField() = 0;
104 
105  // determine the length of a pdu
106  bool DecodeLength(const uint8_t *data,
107  unsigned int data_length,
108  unsigned int *pdu_length,
109  unsigned int *bytes_used) const;
110 
111  // determine the vector of a pdu
112  bool DecodeVector(uint8_t flags,
113  const uint8_t *data,
114  unsigned int length,
115  uint32_t *vector,
116  unsigned int *bytes_used);
117 
118  // Decode a header block and adds any PduHeaders to the HeaderSet object
119  virtual bool DecodeHeader(HeaderSet *headers,
120  const uint8_t *data,
121  unsigned int len,
122  unsigned int *bytes_used) = 0;
123 
124  // parse the body of a pdu
125  bool InflatePDU(HeaderSet *headers,
126  uint8_t flags,
127  const uint8_t *data,
128  unsigned int pdu_len);
129 
130  // called after the header is parsed
131  virtual bool PostHeader(uint32_t vector, const HeaderSet &headers);
132 
133  // called in the absence of an inflator to handle the pdu data
134  virtual bool HandlePDUData(uint32_t vector,
135  const HeaderSet &headers,
136  const uint8_t *data,
137  unsigned int pdu_len);
138 };
139 } // namespace e131
140 } // namespace plugin
141 } // namespace ola
142 #endif // PLUGINS_E131_E131_BASEINFLATOR_H_