Make more drivers work.
authorbruceperens <bruceperens@01035d8c-6547-0410-b346-abe4f91aad63>
Tue, 21 Jan 2014 17:53:20 +0000 (17:53 +0000)
committerbruceperens <bruceperens@01035d8c-6547-0410-b346-abe4f91aad63>
Tue, 21 Jan 2014 17:53:20 +0000 (17:53 +0000)
git-svn-id: https://svn.code.sf.net/p/freetel/code@1381 01035d8c-6547-0410-b346-abe4f91aad63

freedv-server/CMakeLists.txt
freedv-server/source/drivers.h
freedv-server/source/ptt_constant.cpp
freedv-server/source/ptt_input.cpp
freedv-server/source/run.cpp
freedv-server/source/test/ptt_constant.cpp [new file with mode: 0644]
freedv-server/source/test/text_constant.cpp [new file with mode: 0644]
freedv-server/source/text_constant.cpp

index b70ca1631cdac659cf25c0cd4a2fc8d014537d9d..0460ef75765727a4469195e26a4d322f49069926 100644 (file)
@@ -129,6 +129,8 @@ set(Test.sources
   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
 )
 
index 580066be3c096f37ba90a1b89a2eff0659623414..25b3766439c2779199a38f404b09edc3abb41c51 100644 (file)
@@ -350,18 +350,7 @@ namespace FreeDV {
 
   /// 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.
@@ -370,9 +359,7 @@ namespace FreeDV {
   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.
@@ -389,6 +376,9 @@ namespace FreeDV {
                        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;
   };
 
index d15c6181b7a88e24a9ea9a6dceb4f27dc1b2d216..2a534d29b9bd3bc5f359ecd1119aa5d4c6dc9494 100644 (file)
@@ -1,10 +1,16 @@
 /// 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);
@@ -12,11 +18,23 @@ namespace FreeDV {
     
     /// 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()
@@ -26,7 +44,23 @@ namespace FreeDV {
   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 *
index 447790ba12b2a25e83c0de05b1b025b505b5be38..815a79ade6eb955af8b4fc527f2085faf97af292 100644 (file)
@@ -11,17 +11,4 @@ namespace FreeDV {
   PTTInput::~PTTInput()
   {
   }
-
-  void
-  PTTInput::changed(bool value)
-  {
-    if ( callback )
-      (*callback)(value);
-  }
-
-  void
-  PTTInput::set_callback(void (*c)(bool))
-  {
-    callback = c;
-  }
 }
index 95b9437d147f49648d45a935edb17dcaa3d909cf..23f2375dca481a902ca55c335fe125993b54ec68 100644 (file)
@@ -16,12 +16,6 @@ namespace FreeDV {
   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;
   }
 }
diff --git a/freedv-server/source/test/ptt_constant.cpp b/freedv-server/source/test/ptt_constant.cpp
new file mode 100644 (file)
index 0000000..d78ea84
--- /dev/null
@@ -0,0 +1,64 @@
+// 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);
+}
diff --git a/freedv-server/source/test/text_constant.cpp b/freedv-server/source/test/text_constant.cpp
new file mode 100644 (file)
index 0000000..0f590e5
--- /dev/null
@@ -0,0 +1,41 @@
+// 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);
+}
index 5923c9c4d1973fb03fe552b7d96032a218487e99..df88db40dbb9b08da46d0f12a49efd4280ad3f74 100644 (file)
@@ -1,22 +1,30 @@
 /// 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))
   {
   }
 
@@ -24,10 +32,30 @@ namespace FreeDV {
   {
   }
 
+  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, &parameters[index], size);
+      index += size;
+    }
+    return size;
+  }
+
   size_t
   TextConstant::ready()
   {
-    return SIZE_MAX;
+    return length - index;
   }
 
   TextInput *