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

OpenLightingProject / ola / 26710769780

31 May 2026 11:00AM UTC coverage: 44.862%. Remained the same
26710769780

Pull #2052

github

web-flow
Merge 5a30b1245 into 185b10f22
Pull Request #2052: Modify compiler flags to suppress variable length array error

8554 of 19846 branches covered (43.1%)

3 of 6 new or added lines in 2 files covered. (50.0%)

10 existing lines in 1 file now uncovered.

22105 of 49273 relevant lines covered (44.86%)

47.59 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

NEW
103
  tr.tx_buf        = (uint64_t) tx_buf;
×
NEW
104
  tr.rx_buf        = (uint64_t) rx_buf;
×
NEW
105
  tr.len           = blocklength;
×
106
  tr.speed_hz      = SPI_SPEED;
×
107
  tr.delay_usecs   = SPI_DELAY;
×
108
  tr.bits_per_word = SPI_BITS_PER_WORD;
×
109
  tr.cs_change     = SPI_CS_CHANGE;
×
110
  tr.pad           = SPI_PAD;
×
111

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

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

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

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

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

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

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

178

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

183
}  // namespace spidmx
184
}  // namespace plugin
185
}  // 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