source/test/base.cpp
source/test/blank_panel.cpp
source/test/keying_sink.cpp
+ source/test/ptt_constant.cpp
+ source/test/text_constant.cpp
source/test/tone.cpp
)
/// Push-to-talk input driver.
class PTTInput : public ::FreeDV::IODevice {
- private:
- /// Coroutine to be called when the sense of the push-to-talk switch
- /// changes.
- void (*callback)(bool);
-
protected:
- /// The driver calls this member to inform FreeDV that the PTT switch
- /// sense has changed. The value is true for key-down,
- /// false for key-up.
- /// \param value True for key-down, false for key-up.
- void changed(bool value);
-
/// Create a push-to-talk switch instance.
/// \param name Name of the driver. This is expected to be a single
/// constant static string per driver class.
public:
virtual ~PTTInput() = 0;
- /// Set the function that will be called when the push-to-talk input
- /// changes its value.
- void set_callback(void (*value)(bool));
+ virtual bool state() = 0;
};
/// Driver for the text message source function.
TextInput(const char * name, const char * parameters);
public:
+ /// Read the text data.
+ virtual size_t read(char * buffer, size_t length) = 0;
+
virtual ~TextInput() = 0;
};
/// The constant PTT driver, for testing.
#include "drivers.h"
+#include <iostream>
+#include <stdexcept>
namespace FreeDV {
/// PTT driver that is constant transmit or constant receive. For testing.
class PTTConstant : public PTTInput {
+ private:
+ /// This is true if ready has not yet been sent.
+ bool ready_one_shot;
+ bool pressed;
public:
/// Instantiate push-to-talk source with constant input, for testing.
PTTConstant(const char * parameters);
/// Return the amount of bytes ready for read.
size_t ready();
+
+ /// Return true if the PTT input is pressed.
+ bool state();
};
PTTConstant::PTTConstant(const char * parameters)
- : PTTInput("constant", parameters)
+ : PTTInput("constant", parameters), ready_one_shot(true)
{
+ if ( *parameters == 't' )
+ pressed = true;
+ else if ( *parameters == 'r' || *parameters == '\0' )
+ pressed = false;
+ else {
+ std::cerr << "PTT input constant: bad parameter string "
+ << parameters << std::endl;
+ pressed = false;
+ }
}
PTTConstant::~PTTConstant()
size_t
PTTConstant::ready()
{
- return SIZE_MAX;
+ if ( ready_one_shot )
+ return 1;
+ else
+ return 0;
+ }
+
+ bool
+ PTTConstant::state()
+ {
+ if ( ready_one_shot )
+ ready_one_shot = false;
+ else {
+ /// A real I/O source would block, so throw an error here.
+ throw std::runtime_error("PTT constant state read with no I/O ready.");
+ }
+
+ return pressed;
}
PTTInput *
PTTInput::~PTTInput()
{
}
-
- void
- PTTInput::changed(bool value)
- {
- if ( callback )
- (*callback)(value);
- }
-
- void
- PTTInput::set_callback(void (*c)(bool))
- {
- callback = c;
- }
}
int
run(Interfaces * i)
{
- if ( i->ptt_input_digital )
- i->ptt_input_digital->set_callback(ptt_digital);
-
- if ( i->ptt_input_ssb )
- i->ptt_input_ssb->set_callback(ptt_ssb);
-
return 0;
}
}
--- /dev/null
+// Tests for the PTTInput class.
+#include <drivers.h>
+#include <gtest/gtest.h>
+#include <climits>
+
+using namespace FreeDV;
+
+const char text[] =
+"The time is out of joint. O cursed spite,"
+"That ever I was born to set it right!";
+
+class PTTInputTransmitTest : public ::testing::Test {
+protected:
+ PTTInput * i;
+
+ PTTInputTransmitTest()
+ : i(0)
+ {
+ }
+
+ void SetUp() {
+ i = Driver::PTTConstant("t");
+ }
+
+ void TearDown() {
+ delete i;
+ }
+};
+
+class PTTInputReceiveTest : public ::testing::Test {
+protected:
+ PTTInput * i;
+
+ PTTInputReceiveTest()
+ : i(0)
+ {
+ }
+
+ void SetUp() {
+ i = Driver::PTTConstant("r");
+ }
+
+ void TearDown() {
+ delete i;
+ }
+};
+
+TEST_F(PTTInputTransmitTest, ReadyAndRead) {
+ EXPECT_EQ(1, i->ready());
+ EXPECT_EQ(1, i->ready());
+ EXPECT_TRUE(i->state());
+ EXPECT_EQ(0, i->ready());
+ EXPECT_EQ(0, i->ready());
+ EXPECT_THROW(i->state(), std::runtime_error);
+}
+
+TEST_F(PTTInputReceiveTest, ReadyAndRead) {
+ EXPECT_EQ(1, i->ready());
+ EXPECT_EQ(1, i->ready());
+ EXPECT_FALSE(i->state());
+ EXPECT_EQ(0, i->ready());
+ EXPECT_EQ(0, i->ready());
+ EXPECT_THROW(i->state(), std::runtime_error);
+}
--- /dev/null
+// Tests for the TextInput class.
+#include <drivers.h>
+#include <gtest/gtest.h>
+#include <climits>
+
+using namespace FreeDV;
+
+const char text[] =
+"The time is out of joint. O cursed spite,"
+"That ever I was born to set it right!";
+
+class TextConstantTest : public ::testing::Test {
+protected:
+ TextInput * i;
+
+ TextConstantTest()
+ : i(0)
+ {
+ }
+
+ void SetUp() {
+ i = Driver::TextConstant(text);
+ }
+
+ void TearDown() {
+ delete i;
+ }
+};
+
+TEST_F(TextConstantTest, ReadyAndRead) {
+ char buffer[1024];
+
+ EXPECT_EQ(sizeof(text) - 1, i->ready());
+ EXPECT_EQ(10, i->read(buffer, 10));
+ EXPECT_EQ(sizeof(text) - 1 - 10, i->ready());
+ EXPECT_EQ(0, i->read(buffer, 0));
+ EXPECT_EQ(sizeof(text) - 1 - 10, i->ready());
+ EXPECT_EQ(sizeof(text) - 1 - 10, i->read(buffer, sizeof(buffer)));
+ EXPECT_EQ(0, i->ready());
+ EXPECT_THROW(i->read(buffer, sizeof(buffer)), std::runtime_error);
+}
/// The constant text driver, just outputs the same text over and over.
#include "drivers.h"
+#include <stdexcept>
+#include <cstring>
namespace FreeDV {
/// This driver provides constant text.
class TextConstant : public TextInput {
+ private:
+ const size_t length;
+ size_t index;
+
public:
/// Instantiate the constant text driver.
TextConstant(const char * parameter);
virtual ~TextConstant();
- /// Return the amount of bytes ready for read. In this case, it always
- /// returns SIZE_MAX.
- size_t ready();
+ /// Read the text data.
+ size_t read(char * buffer, size_t size);
+
+ /// Return the amount of bytes ready for read.
+ size_t ready();
};
TextConstant::TextConstant(const char * parameters)
- : TextInput("constant", parameters)
+ : TextInput("constant", parameters), index(0), length(strlen(parameters))
{
}
{
}
+ size_t
+ TextConstant::read(char * buffer, size_t size)
+ {
+ const size_t available = length - index;
+
+ if ( available == 0 ) {
+ /// A real I/O device would block if this happened, so throw an error.
+ throw std::runtime_error("Text constant: read with no I/O ready.");
+ }
+
+ if ( size > available )
+ size = available;
+
+ if ( size > 0 ) {
+ memcpy(buffer, ¶meters[index], size);
+ index += size;
+ }
+ return size;
+ }
+
size_t
TextConstant::ready()
{
- return SIZE_MAX;
+ return length - index;
}
TextInput *