Skip to content

Commit

Permalink
Merge branch 'master' of https://github.com/OpenLightingProject/ola i…
Browse files Browse the repository at this point in the history
…nto trivial-fixes
  • Loading branch information
peternewman committed Nov 26, 2014
2 parents 2a0a63b + b3ee60a commit e67b83b
Show file tree
Hide file tree
Showing 18 changed files with 2,386 additions and 2 deletions.
1 change: 1 addition & 0 deletions README
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
Open Lighting Architecture - OLA
===============================================================================
[![Build Status](https://travis-ci.org/OpenLightingProject/ola.svg?branch=master)](https://travis-ci.org/OpenLightingProject/ola) [![Coverage Status](https://coveralls.io/repos/OpenLightingProject/ola/badge.png?branch=master)](https://coveralls.io/r/OpenLightingProject/ola?branch=master)

OLA consists of three parts, the daemon olad, the olad plugins and the client
library, libola.
Expand Down
10 changes: 10 additions & 0 deletions common/network/InterfacePickerTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,7 @@ void InterfacePickerTest::testChooseInterface() {
// now with one iface that doesn't match
Interface iface1;
iface1.name = "eth0";
iface1.index = 1;
OLA_ASSERT_TRUE(IPV4Address::FromString("10.0.0.1", &iface1.ip_address));
interfaces.push_back(iface1);

Expand All @@ -123,6 +124,7 @@ void InterfacePickerTest::testChooseInterface() {
// check that preferred works
Interface iface2;
iface2.name = "eth1";
iface2.index = 2;
OLA_ASSERT_TRUE(IPV4Address::FromString("192.168.1.1", &iface2.ip_address));
interfaces.push_back(iface2);

Expand All @@ -140,4 +142,12 @@ void InterfacePickerTest::testChooseInterface() {
// a invalid address should return the first one
OLA_ASSERT_TRUE(picker3.ChooseInterface(&iface, "foo"));
OLA_ASSERT_TRUE(iface1 == iface);

// now check by iface index
OLA_ASSERT_TRUE(picker3.ChooseInterface(&iface, 2));
OLA_ASSERT_TRUE(iface2 == iface);

// an invalid index should return the first one
OLA_ASSERT_TRUE(picker3.ChooseInterface(&iface, 3));
OLA_ASSERT_TRUE(iface1 == iface);
}
10 changes: 10 additions & 0 deletions common/network/NetworkUtilsTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,16 @@ void NetworkUtilsTest::testToFromLittleEndian() {

uint32_t v3 = 0x01020304;
OLA_ASSERT_EQ(v3, LittleEndianToHost(HostToLittleEndian(v3)));

int8_t v4 = -10;
OLA_ASSERT_EQ(v4, HostToLittleEndian(v4));
OLA_ASSERT_EQ(v4, LittleEndianToHost(HostToLittleEndian(v4)));

int16_t v5 = -0x0102;
OLA_ASSERT_EQ(v5, LittleEndianToHost(HostToLittleEndian(v5)));

int32_t v6 = -0x01020304;
OLA_ASSERT_EQ(v6, LittleEndianToHost(HostToLittleEndian(v6)));
}


Expand Down
10 changes: 10 additions & 0 deletions common/timecode/TimeCodeTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@


using ola::timecode::TimeCode;
using std::ostringstream;
using std::string;

using ola::timecode::TIMECODE_FILM;
Expand All @@ -52,19 +53,28 @@ CPPUNIT_TEST_SUITE_REGISTRATION(TimeCodeTest);
* Test the TimeCodes work.
*/
void TimeCodeTest::testTimeCode() {
ostringstream str1;

TimeCode t1(TIMECODE_FILM, 0, 0, 0, 0);
OLA_ASSERT_EQ(TIMECODE_FILM, t1.Type());
OLA_ASSERT_EQ(static_cast<uint8_t>(0), t1.Hours());
OLA_ASSERT_EQ(static_cast<uint8_t>(0), t1.Minutes());
OLA_ASSERT_EQ(static_cast<uint8_t>(0), t1.Seconds());
OLA_ASSERT_EQ(static_cast<uint8_t>(0), t1.Frames());
OLA_ASSERT_EQ(string("00:00:00:00"), t1.AsString());
str1 << t1;
OLA_ASSERT_EQ(string("00:00:00:00"), str1.str());
OLA_ASSERT_TRUE(t1.IsValid());

ostringstream str3;

TimeCode t2(t1);
OLA_ASSERT_EQ(t1, t2);

TimeCode t3(TIMECODE_SMPTE, 10, 9, 12, 14);
OLA_ASSERT_EQ(string("10:09:12:14"), t3.AsString());
str3 << t3;
OLA_ASSERT_EQ(string("10:09:12:14"), str3.str());
OLA_ASSERT_TRUE(t3.IsValid());
OLA_ASSERT_NE(t1, t3);
t3 = t1;
Expand Down
6 changes: 6 additions & 0 deletions common/utils/DmxBufferTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
#include "ola/DmxBuffer.h"
#include "ola/testing/TestUtils.h"

using std::ostringstream;
using std::string;
using ola::DmxBuffer;

Expand Down Expand Up @@ -512,4 +513,9 @@ void DmxBufferTest::testToString() {

buffer.SetRangeToValue(0, 255, 5);
OLA_ASSERT_EQ(string("255,255,255,255,255"), buffer.ToString());

buffer.SetFromString("1,2,3,4");
ostringstream str;
str << buffer;
OLA_ASSERT_EQ(string("1,2,3,4"), str.str());
}
158 changes: 158 additions & 0 deletions plugins/usbdmx/AsyncUsbSender.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,158 @@
/*
* 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.
*
* AsyncUsbSender.cpp
* An Asynchronous DMX USB sender.
* Copyright (C) 2014 Simon Newton
*/

#include "plugins/usbdmx/AsyncUsbSender.h"

#include "ola/Logging.h"
#include "plugins/usbdmx/LibUsbAdaptor.h"

namespace ola {
namespace plugin {
namespace usbdmx {

namespace {

/*
* Called by libusb when the transfer completes.
*/
void AsyncCallback(struct libusb_transfer *transfer) {
AsyncUsbSender *widget = reinterpret_cast<AsyncUsbSender*>(
transfer->user_data);
widget->TransferComplete(transfer);
}
} // namespace

AsyncUsbSender::AsyncUsbSender(LibUsbAdaptor *adaptor,
libusb_device *usb_device)
: m_adaptor(adaptor),
m_usb_device(usb_device),
m_usb_handle(NULL),
m_transfer_state(IDLE),
m_pending_tx(false) {
m_transfer = m_adaptor->AllocTransfer(0);
m_adaptor->RefDevice(usb_device);
}

AsyncUsbSender::~AsyncUsbSender() {
CancelTransfer();
m_adaptor->Close(m_usb_handle);
m_adaptor->UnrefDevice(m_usb_device);
}

bool AsyncUsbSender::Init() {
m_usb_handle = SetupHandle();
return m_usb_handle ? true : false;
}

bool AsyncUsbSender::SendDMX(const DmxBuffer &buffer) {
if (!m_usb_handle) {
OLA_WARN << "AsyncUsbSender hasn't been initialized";
return false;
}
ola::thread::MutexLocker locker(&m_mutex);
if (m_transfer_state == IDLE) {
PerformTransfer(buffer);
} else {
m_pending_tx = true;
m_tx_buffer.Set(buffer);
}
return true;
}

void AsyncUsbSender::CancelTransfer() {
if (!m_transfer) {
return;
}

bool canceled = false;
while (1) {
ola::thread::MutexLocker locker(&m_mutex);
if (m_transfer_state == IDLE || m_transfer_state == DISCONNECTED) {
break;
}
if (!canceled) {
m_adaptor->CancelTransfer(m_transfer);
canceled = true;
}
}

m_adaptor->FreeTransfer(m_transfer);
m_transfer = NULL;
}

void AsyncUsbSender::FillControlTransfer(unsigned char *buffer,
unsigned int timeout) {
m_adaptor->FillControlTransfer(m_transfer, m_usb_handle, buffer,
&AsyncCallback, this, timeout);
}

void AsyncUsbSender::FillBulkTransfer(unsigned char endpoint,
unsigned char *buffer,
int length,
unsigned int timeout) {
m_adaptor->FillBulkTransfer(m_transfer, m_usb_handle, endpoint, buffer,
length, &AsyncCallback, this, timeout);
}

void AsyncUsbSender::FillInterruptTransfer(unsigned char endpoint,
unsigned char *buffer,
int length,
unsigned int timeout) {
m_adaptor->FillInterruptTransfer(m_transfer, m_usb_handle, endpoint, buffer,
length, &AsyncCallback, this, timeout);
}

int AsyncUsbSender::SubmitTransfer() {
int ret = m_adaptor->SubmitTransfer(m_transfer);
if (ret) {
OLA_WARN << "libusb_submit_transfer returned " << libusb_error_name(ret);
if (ret == LIBUSB_ERROR_NO_DEVICE) {
m_transfer_state = DISCONNECTED;
}
return false;
}
m_transfer_state = IN_PROGRESS;
return ret;
}

void AsyncUsbSender::TransferComplete(struct libusb_transfer *transfer) {
if (transfer != m_transfer) {
OLA_WARN << "Mismatched libusb transfer: " << transfer << " != "
<< m_transfer;
return;
}

if (transfer->status != LIBUSB_TRANSFER_COMPLETED) {
OLA_WARN << "Transfer returned " << transfer->status;
}

ola::thread::MutexLocker locker(&m_mutex);
m_transfer_state = (transfer->status == LIBUSB_TRANSFER_NO_DEVICE ?
DISCONNECTED : IDLE);
PostTransferHook();

if ((m_transfer_state == IDLE) && m_pending_tx) {
m_pending_tx = false;
PerformTransfer(m_tx_buffer);
}
}
} // namespace usbdmx
} // namespace plugin
} // namespace ola
Loading

0 comments on commit e67b83b

Please sign in to comment.