Open Lighting Architecture  0.9.2
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
FakeSPIWriter.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  * FakeSPIWriter.h
17  * The SPIWriter used for testing.
18  * Copyright (C) 2013 Simon Newton
19  */
20 
21 #ifndef PLUGINS_SPI_FAKESPIWRITER_H_
22 #define PLUGINS_SPI_FAKESPIWRITER_H_
23 
24 #include <ola/thread/Mutex.h>
25 #include <stdint.h>
26 #include <string>
27 #include "plugins/spi/SPIWriter.h"
28 
29 namespace ola {
30 namespace plugin {
31 namespace spi {
32 
37  public:
38  explicit FakeSPIWriter(const std::string &device_path)
39  : m_device_path(device_path),
40  m_write_pending(0),
41  m_writes(0),
42  m_last_write_size(0),
43  m_data(NULL) {
44  }
45 
46  ~FakeSPIWriter() {
47  delete[] m_data;
48  }
49 
50  bool Init() { return true; }
51 
52  std::string DevicePath() const { return m_device_path; }
53 
54  bool WriteSPIData(const uint8_t *data, unsigned int length);
55 
56  // Methods used for testing
57  void BlockWriter();
58  void UnblockWriter();
59 
60  void ResetWrite();
61  void WaitForWrite();
62 
63  unsigned int WriteCount() const;
64  unsigned int LastWriteSize() const;
65  void CheckDataMatches(unsigned int line, const uint8_t *data,
66  unsigned int length);
67 
68  private:
69  const std::string m_device_path;
70  bool m_write_pending; // GUARDED_BY(m_mutex)
71  unsigned int m_writes; // GUARDED_BY(m_mutex)
72  unsigned int m_last_write_size; // GUARDED_BY(m_mutex)
73  uint8_t *m_data; // GUARDED_BY(m_mutex)
74 
75  ola::thread::Mutex m_write_lock;
76  mutable ola::thread::Mutex m_mutex;
78 };
79 
80 } // namespace spi
81 } // namespace plugin
82 } // namespace ola
83 #endif // PLUGINS_SPI_FAKESPIWRITER_H_