Open Lighting Architecture
 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., 59 Temple Place - Suite 330, Boston, MA 02111-1307, 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 
33 using std::string;
34 
39  public:
40  explicit FakeSPIWriter(const string &device_path)
41  : m_device_path(device_path),
42  m_write_pending(0),
43  m_writes(0),
44  m_last_write_size(0),
45  m_data(NULL) {
46  }
47 
48  ~FakeSPIWriter() {
49  delete[] m_data;
50  }
51 
52  bool Init() { return true; }
53 
54  string DevicePath() const { return m_device_path; }
55 
56  bool WriteSPIData(const uint8_t *data, unsigned int length);
57 
58  // Methods used for testing
59  void BlockWriter();
60  void UnblockWriter();
61 
62  void ResetWrite();
63  void WaitForWrite();
64 
65  unsigned int WriteCount() const;
66  unsigned int LastWriteSize() const;
67  void CheckDataMatches(unsigned int line, const uint8_t *data,
68  unsigned int length);
69 
70  private:
71  const string m_device_path;
72  bool m_write_pending; // GUARDED_BY(m_mutex)
73  unsigned int m_writes; // GUARDED_BY(m_mutex)
74  unsigned int m_last_write_size; // GUARDED_BY(m_mutex)
75  uint8_t *m_data; // GUARDED_BY(m_mutex)
76 
77  ola::thread::Mutex m_write_lock;
78  mutable ola::thread::Mutex m_mutex;
80 };
81 
82 } // namespace spi
83 } // namespace plugin
84 } // namespace ola
85 #endif // PLUGINS_SPI_FAKESPIWRITER_H_