File: SPIBackend.cpp

package info (click to toggle)
ola 0.10.9.nojsmin-2
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 17,440 kB
  • sloc: cpp: 132,294; python: 14,445; javascript: 7,109; sh: 4,713; ansic: 2,189; java: 518; xml: 253; makefile: 175
file content (504 lines) | stat: -rw-r--r-- 12,743 bytes parent folder | download | duplicates (6)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
/*
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Library General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
 *
 * SPIBackend.cpp
 * The backend for SPI output. These are the classes which write the data to
 * the SPI bus.
 * Copyright (C) 2013 Simon Newton
 */

#include <errno.h>
#include <fcntl.h>
#include <linux/spi/spidev.h>
#include <stdlib.h>
#include <string.h>
#include <sys/ioctl.h>

#include <numeric>
#include <sstream>
#include <string>
#include <vector>

#include "ola/Logging.h"
#include "ola/io/IOUtils.h"
#include "ola/network/SocketCloser.h"
#include "ola/stl/STLUtils.h"
#include "plugins/spi/SPIBackend.h"

namespace ola {
namespace plugin {
namespace spi {

using ola::thread::MutexLocker;
using std::string;
using std::vector;

const char SPIBackendInterface::SPI_DROP_VAR[] = "spi-drops";
const char SPIBackendInterface::SPI_DROP_VAR_KEY[] = "device";

uint8_t *HardwareBackend::OutputData::Resize(unsigned int length) {
  if (length < m_size) {
    m_size = length;
    return m_data;
  } else if (length == m_size) {
    return m_data;
  }

  if (length <= m_actual_size) {
    m_size = length;
    return m_data;
  }

  delete[] m_data;
  m_data = new uint8_t[length];
  if (m_data) {
    m_size = m_data ? length : 0;
    m_actual_size = m_size;
    memset(m_data, 0, length);
  }
  return m_data;
}

void HardwareBackend::OutputData::SetPending() {
  m_write_pending = true;
}

void HardwareBackend::OutputData::SetLatchBytes(unsigned int latch_bytes) {
  m_latch_bytes = latch_bytes;
}

HardwareBackend::OutputData& HardwareBackend::OutputData::operator=(
    const HardwareBackend::OutputData &other) {
  if (this != &other) {
    uint8_t *data = Resize(other.m_size + other.m_latch_bytes);
    if (data) {
      memcpy(data, other.m_data, other.m_size);
      memset(data + other.m_size, 0, other.m_latch_bytes);
      m_write_pending = true;
    } else {
      m_write_pending = false;
    }
  }
  return *this;
}

HardwareBackend::HardwareBackend(const Options &options,
                                 SPIWriterInterface *writer,
                                 ExportMap *export_map)
    : m_spi_writer(writer),
      m_drop_map(NULL),
      m_output_count(1 << options.gpio_pins.size()),
      m_exit(false),
      m_gpio_pins(options.gpio_pins) {
  SetupOutputs(&m_output_data);
  if (export_map) {
    m_drop_map = export_map->GetUIntMapVar(SPI_DROP_VAR,
                                           SPI_DROP_VAR_KEY);
    (*m_drop_map)[m_spi_writer->DevicePath()] = 0;
  }
}


HardwareBackend::~HardwareBackend() {
  {
    MutexLocker lock(&m_mutex);
    m_exit = true;
  }

  m_cond_var.Signal();
  Join();

  STLDeleteElements(&m_output_data);
  CloseGPIOFDs();
}

bool HardwareBackend::Init() {
  if (!(m_spi_writer->Init() && SetupGPIO())) {
    return false;
  }

  if (!Start()) {
    CloseGPIOFDs();
    return false;
  }
  return true;
}

uint8_t *HardwareBackend::Checkout(uint8_t output_id,
                                   unsigned int length,
                                   unsigned int latch_bytes) {
  if (output_id >= m_output_count) {
    return NULL;
  }

  m_mutex.Lock();
  uint8_t *output = m_output_data[output_id]->Resize(length);
  if (!output) {
    m_mutex.Unlock();
  }
  m_output_data[output_id]->SetLatchBytes(latch_bytes);
  // We return with the Mutex locked, the caller must then call Commit()
  // coverity[LOCK]
  return output;
}

void HardwareBackend::Commit(uint8_t output) {
  if (output >= m_output_count) {
    return;
  }

  OutputData *output_data = m_output_data[output];
  if (output_data->IsPending() && m_drop_map) {
    // There was already another write pending which we're now stomping on
    (*m_drop_map)[m_spi_writer->DevicePath()]++;
  }
  output_data->SetPending();
  m_mutex.Unlock();
  m_cond_var.Signal();
}

void *HardwareBackend::Run() {
  Outputs outputs;
  SetupOutputs(&outputs);

  while (true) {
    m_mutex.Lock();

    if (m_exit) {
      m_mutex.Unlock();
      STLDeleteElements(&outputs);
      return NULL;
    }

    bool action_pending = false;
    Outputs::const_iterator iter = m_output_data.begin();
    for (; iter != m_output_data.end(); ++iter) {
      if ((*iter)->IsPending()) {
        action_pending = true;
        break;
      }
    }
    if (!action_pending) {
      m_cond_var.Wait(&m_mutex);
    }

    if (m_exit) {
      m_mutex.Unlock();
      STLDeleteElements(&outputs);
      return NULL;
    }

    for (unsigned int i = 0; i < m_output_data.size(); i++) {
      if (m_output_data[i]->IsPending()) {
        // take a copy
        *outputs[i] = *m_output_data[i];
        m_output_data[i]->ResetPending();
      }
    }
    m_mutex.Unlock();

    for (unsigned int i = 0; i < outputs.size(); i++) {
      if (outputs[i]->IsPending()) {
        WriteOutput(i, outputs[i]);
        outputs[i]->ResetPending();
      }
    }
  }
}

void HardwareBackend::SetupOutputs(Outputs *outputs) {
  for (unsigned int i = 0; i < m_output_count; i++) {
    outputs->push_back(new OutputData());
  }
}

void HardwareBackend::WriteOutput(uint8_t output_id, OutputData *output) {
  const string on("1");
  const string off("0");

  for (unsigned int i = 0; i < m_gpio_fds.size(); i++) {
    uint8_t pin = output_id & (1 << i);

    if (i >= m_gpio_pin_state.size()) {
      m_gpio_pin_state.push_back(!pin);
    }

    if (m_gpio_pin_state[i] != pin) {
      const string &data = pin ? on : off;
      if (write(m_gpio_fds[i], data.c_str(), data.size()) < 0) {
        OLA_WARN << "Failed to toggle SPI GPIO pin "
                 << static_cast<int>(m_gpio_pins[i]) << ": "
                 << strerror(errno);
        return;
      }
      m_gpio_pin_state[i] = pin;
    }
  }

  m_spi_writer->WriteSPIData(output->GetData(), output->Size());
}

bool HardwareBackend::SetupGPIO() {
  /**
   * This relies on the pins being exported:
   *   echo N > /sys/class/gpio/export
   * That requires root access.
   */
  const string direction("out");
  bool failed = false;
  vector<uint16_t>::const_iterator iter = m_gpio_pins.begin();
  for (; iter != m_gpio_pins.end(); ++iter) {
    std::ostringstream str;
    str << "/sys/class/gpio/gpio" << static_cast<int>(*iter) << "/value";
    int fd;
    if (ola::io::Open(str.str(), O_RDWR, &fd)) {
      m_gpio_fds.push_back(fd);
    } else {
      failed = true;
      break;
    }

    // Set dir
    str.str("");
    str << "/sys/class/gpio/gpio" << static_cast<int>(*iter) << "/direction";
    if (!ola::io::Open(str.str(), O_RDWR, &fd)) {
      failed = true;
      break;
    }
    if (write(fd, direction.c_str(), direction.size()) < 0) {
      OLA_WARN << "Failed to enable output on " << str.str() << " : "
               << strerror(errno);
      failed = true;
    }
    close(fd);
  }

  if (failed) {
    CloseGPIOFDs();
    return false;
  }
  return true;
}

void HardwareBackend::CloseGPIOFDs() {
  GPIOFds::iterator iter = m_gpio_fds.begin();
  for (; iter != m_gpio_fds.end(); ++iter) {
    close(*iter);
  }
  m_gpio_fds.clear();
}

SoftwareBackend::SoftwareBackend(const Options &options,
                                 SPIWriterInterface *writer,
                                 ExportMap *export_map)
    : m_spi_writer(writer),
      m_drop_map(NULL),
      m_write_pending(false),
      m_exit(false),
      m_sync_output(options.sync_output),
      m_output_sizes(options.outputs, 0),
      m_latch_bytes(options.outputs, 0),
      m_output(NULL),
      m_length(0) {
  if (export_map) {
    m_drop_map = export_map->GetUIntMapVar(SPI_DROP_VAR,
                                           SPI_DROP_VAR_KEY);
    (*m_drop_map)[m_spi_writer->DevicePath()] = 0;
  }
}

SoftwareBackend::~SoftwareBackend() {
  {
    MutexLocker lock(&m_mutex);
    m_exit = true;
  }

  m_cond_var.Signal();
  Join();

  delete[] m_output;
}

bool SoftwareBackend::Init() {
  if (!m_spi_writer->Init()) {
    return false;
  }

  if (!Start()) {
    return false;
  }
  return true;
}

uint8_t *SoftwareBackend::Checkout(uint8_t output,
                                   unsigned int length,
                                   unsigned int latch_bytes) {
  if (output >= m_output_sizes.size()) {
    OLA_WARN << "Invalid SPI output " << static_cast<int>(output);
    return NULL;
  }

  m_mutex.Lock();

  unsigned int leading = 0;
  unsigned int trailing = 0;
  for (uint8_t i = 0; i < m_output_sizes.size(); i++) {
    if (i < output) {
      leading += m_output_sizes[i];
    } else if (i > output) {
      trailing += m_output_sizes[i];
    }
  }

  m_latch_bytes[output] = latch_bytes;
  const unsigned int total_latch_bytes = std::accumulate(
      m_latch_bytes.begin(), m_latch_bytes.end(), 0);
  const unsigned int required_size = (
      leading + length + trailing + total_latch_bytes);

  // Check if the current buffer is large enough to hold our data.
  if (required_size != m_length) {
    // The length changed
    uint8_t *new_output = new uint8_t[required_size];
    memcpy(new_output, m_output, leading);
    memset(new_output + leading, 0, length);
    memcpy(new_output + leading + length, m_output + leading, trailing);
    memset(new_output + leading + length + trailing, 0, total_latch_bytes);
    delete[] m_output;
    m_output = new_output;
    m_length = required_size;
    m_output_sizes[output] = length;
  }
  return m_output + leading;
}

void SoftwareBackend::Commit(uint8_t output) {
  if (output >= m_output_sizes.size()) {
    OLA_WARN << "Invalid SPI output " << static_cast<int>(output);
    return;
  }

  bool should_write = m_sync_output < 0 || output == m_sync_output;
  if (should_write) {
    if (m_write_pending && m_drop_map) {
      // There was already another write pending which we're now stomping on
      (*m_drop_map)[m_spi_writer->DevicePath()]++;
    }
    m_write_pending = should_write;
  }
  m_mutex.Unlock();
  if (should_write) {
    m_cond_var.Signal();
  }
}

void *SoftwareBackend::Run() {
  uint8_t *output_data = NULL;
  unsigned int length = 0;

  while (true) {
    m_mutex.Lock();

    if (m_exit) {
      m_mutex.Unlock();
      delete output_data;
      return NULL;
    }

    if (!m_write_pending) {
      m_cond_var.Wait(&m_mutex);
    }

    if (m_exit) {
      m_mutex.Unlock();
      delete[] output_data;
      return NULL;
    }

    bool write_pending = m_write_pending;
    m_write_pending = false;
    if (write_pending) {
      if (length < m_length) {
        delete[] output_data;
        output_data = new uint8_t[m_length];
        length = m_length;
      }
      // TODO(simon) Add an actual size here
      length = m_length;
      memcpy(output_data, m_output, m_length);
    }
    m_mutex.Unlock();

    if (write_pending) {
      m_spi_writer->WriteSPIData(output_data, length);
    }
  }
}

FakeSPIBackend::FakeSPIBackend(unsigned int outputs) {
  for (unsigned int i = 0; i < outputs; i++) {
    m_outputs.push_back(new Output());
  }
}

FakeSPIBackend::~FakeSPIBackend() {
  STLDeleteElements(&m_outputs);
}

uint8_t *FakeSPIBackend::Checkout(uint8_t output_id,
                                  unsigned int length,
                                  unsigned int latch_bytes) {
  if (output_id >= m_outputs.size()) {
    return NULL;
  }

  Output *output = m_outputs[output_id];

  if (output->length != length + latch_bytes) {
    delete[] output->data;
    output->data = new uint8_t[length + latch_bytes];
    memset(output->data, 0, length + latch_bytes);
    output->length = length + latch_bytes;
  }
  return output->data;
}

void FakeSPIBackend::Commit(uint8_t output) {
  if (output >= m_outputs.size()) {
    return;
  }
  m_outputs[output]->writes++;
}

const uint8_t *FakeSPIBackend::GetData(uint8_t output_id,
                                       unsigned int *length) {
  if (output_id >= m_outputs.size()) {
    return NULL;
  }
  Output *output = m_outputs[output_id];
  *length = output->length;
  return output->data;
}


unsigned int FakeSPIBackend::Writes(uint8_t output) const {
  if (output >= m_outputs.size()) {
    return 0;
  }
  return m_outputs[output]->writes;
}
}  // namespace spi
}  // namespace plugin
}  // namespace ola