• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

OpenLightingProject / ola / 26807464535

02 Jun 2026 08:17AM UTC coverage: 44.862%. Remained the same
26807464535

Pull #2052

github

web-flow
Merge bb22c0cd6 into 6412fb2e4
Pull Request #2052: Modify compiler flags to suppress variable length array error

8554 of 19846 branches covered (43.1%)

3 of 5 new or added lines in 2 files covered. (60.0%)

11 existing lines in 1 file now uncovered.

22105 of 49273 relevant lines covered (44.86%)

48.92 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

0.0
/plugins/spidmx/SPIDMXWidget.cpp
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
 * SPIDMXWidget.cpp
17
 * This is a wrapper around the required SPIDEV calls.
18
 * Copyright (C) 2017 Florian Edelmann
19
 */
20

21
#include <strings.h>
22
#include <assert.h>
23
#include <memory.h>
24
#include <sys/types.h>
25
#include <sys/stat.h>
26
#include <sys/ioctl.h>
27
#include <linux/spi/spidev.h>
28
#include <fcntl.h>
29
#include <termios.h>
30
#include <unistd.h>
31

32
#include <string>
33
#include <algorithm>
34
#include <vector>
35

36
#include "ola/Constants.h"
37
#include "ola/io/ExtendedSerial.h"
38
#include "ola/io/IOUtils.h"
39
#include "ola/Logging.h"
40
#include "plugins/spidmx/SPIDMXWidget.h"
41

42
namespace ola {
43
namespace plugin {
44
namespace spidmx {
45

46
using std::string;
47
using std::vector;
48

49
SPIDMXWidget::SPIDMXWidget(const string& path)
×
50
    : m_path(path),
×
51
      m_fd(NOT_OPEN) {
×
52
}
×
53

54
SPIDMXWidget::~SPIDMXWidget() {
×
55
  if (IsOpen()) {
×
56
    Close();
×
57
  }
58
}
×
59

60

61
bool SPIDMXWidget::Open() {
×
62
  OLA_DEBUG << "Opening SPI port " << Name();
×
63
  if (!ola::io::Open(m_path, O_RDWR, &m_fd)) {
×
64
    m_fd = FAILED_OPEN;
×
65
    OLA_WARN << Name() << " failed to open";
×
66
    return false;
×
67
  }
68

69
  OLA_DEBUG << "Opened SPI port " << Name();
×
70
  return true;
×
71
}
72

73
bool SPIDMXWidget::Close() {
×
74
  if (!IsOpen()) {
×
75
    return true;
76
  }
77

78
  if (close(m_fd) > 0) {
×
79
    OLA_WARN << Name() << " error closing";
×
80
    m_fd = NOT_OPEN;
×
81
    return false;
×
82
  }
83

84
  m_fd = NOT_OPEN;
×
85
  return true;
×
86
}
87

88
bool SPIDMXWidget::IsOpen() const {
×
89
  return m_fd >= 0;
×
90
}
91

92
/**
93
 * Transmit tx_buf to the SPI bus and read data from the SPI bus into rx_buf.
94
 * Both must be of the specified blocklength.
95
 *
96
 * @returns false if send/receive operation failed, true otherwise
97
 */
98
bool SPIDMXWidget::ReadWrite(uint8_t *tx_buf, uint8_t *rx_buf,
×
99
                             uint32_t blocklength) {
100
  struct spi_ioc_transfer tr;
×
101
  memset(&tr, 0, sizeof(spi_ioc_transfer));
×
102

103
  // Changing these to a static_cast breaks the tests at least
NEW
104
  tr.tx_buf        = (uint64_t) tx_buf;   // NOLINT(readability/casting)
×
NEW
105
  tr.rx_buf        = (uint64_t) rx_buf;   // NOLINT(readability/casting)
×
106
  tr.len           = blocklength;
×
107
  tr.speed_hz      = SPI_SPEED;
×
108
  tr.delay_usecs   = SPI_DELAY;
×
109
  tr.bits_per_word = SPI_BITS_PER_WORD;
×
110
  tr.cs_change     = SPI_CS_CHANGE;
×
UNCOV
111
  tr.pad           = SPI_PAD;
×
112

113
  int ret = ioctl(m_fd, SPI_IOC_MESSAGE(1), &tr);
×
114
  if (ret < 1) {
×
UNCOV
115
    OLA_WARN << Name() << " ioctl read/write error. This may be due to an "
×
116
             << "insufficient buffer size configuration; see SPI plugin's "
117
             << "README.";
×
UNCOV
118
    return false;
×
119
  }
120
  return true;
121
}
122

123
/**
124
 * Setup our device for DMX receive.
125
 */
126
bool SPIDMXWidget::SetupOutput() {
×
127
  if (!IsOpen()) {
×
UNCOV
128
    if (!Open()) {
×
129
      return false;
130
    }
131
  }
132

133
  // spi mode
134
  uint8_t mode = SPI_MODE;
×
135
  int ret = ioctl(m_fd, SPI_IOC_WR_MODE, &mode);
×
136
  if (ret == -1) {
×
137
    OLA_WARN << Name() << " can't set spi mode";
×
UNCOV
138
    return false;
×
139
  }
140

141
  // bits per word
142
  uint8_t bits = SPI_BITS_PER_WORD;
×
143
  ret = ioctl(m_fd, SPI_IOC_WR_BITS_PER_WORD, &bits);
×
144
  if (ret == -1) {
×
145
    OLA_WARN << Name() << " can't set bits per word";
×
UNCOV
146
    return false;
×
147
  }
148

149
  ret = ioctl(m_fd, SPI_IOC_RD_BITS_PER_WORD, &bits);
×
150
  if (ret == -1) {
×
151
    OLA_WARN << Name() << " can't get bits per word";
×
UNCOV
152
    return false;
×
153
  }
154
  if (bits != SPI_BITS_PER_WORD) {
×
155
    OLA_WARN << Name() << "'s bits per word (" << bits
×
156
             << ") are not as expected";
×
UNCOV
157
    return false;
×
158
  }
159

160
  // max speed
161
  uint32_t speed = SPI_SPEED;
×
162
  ret = ioctl(m_fd, SPI_IOC_WR_MAX_SPEED_HZ, &speed);
×
163
  if (ret == -1) {
×
164
    OLA_WARN << Name() << " can't set max speed";
×
UNCOV
165
    return false;
×
166
  }
167

168
  ret = ioctl(m_fd, SPI_IOC_RD_MAX_SPEED_HZ, &speed);
×
169
  if (ret == -1) {
×
170
    OLA_WARN << Name() << " can't get max speed";
×
UNCOV
171
    return false;
×
172
  }
173
  if (speed != SPI_SPEED) {
×
174
    OLA_WARN << Name() << "'s max speed (" << speed
×
175
             << ") is not as expected";
×
UNCOV
176
    return false;
×
177
  }
178

179

180
  // everything must have worked to get here
181
  return true;
182
}
183

184
}  // namespace spidmx
185
}  // namespace plugin
186
}  // namespace ola
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc