Browse Source

init

master
Robin Thoni 8 years ago
commit
003a67e5bb

+ 52
- 0
.gitignore View File

@@ -0,0 +1,52 @@
1
+# Created by .ignore support plugin (hsz.mobi)
2
+### C template
3
+# Object files
4
+*.o
5
+*.ko
6
+*.obj
7
+*.elf
8
+
9
+# Precompiled Headers
10
+*.gch
11
+*.pch
12
+
13
+# Libraries
14
+*.lib
15
+*.a
16
+*.la
17
+*.lo
18
+
19
+# Shared objects (inc. Windows DLLs)
20
+*.dll
21
+*.so
22
+*.so.*
23
+*.dylib
24
+
25
+# Executables
26
+*.exe
27
+*.out
28
+*.app
29
+*.i*86
30
+*.x86_64
31
+*.hex
32
+
33
+# Debug files
34
+*.dSYM/
35
+### C++ template
36
+# Compiled Object files
37
+*.slo
38
+
39
+# Precompiled Headers
40
+
41
+# Compiled Dynamic libraries
42
+
43
+# Fortran module files
44
+*.mod
45
+
46
+# Compiled Static libraries
47
+*.lai
48
+
49
+# Executables
50
+
51
+digicode_host
52
+.idea

+ 8
- 0
CMakeLists.txt View File

@@ -0,0 +1,8 @@
1
+cmake_minimum_required(VERSION 2.8)
2
+project(digicode_host)
3
+find_package(GTest)
4
+add_subdirectory(src)
5
+if (GTEST_FOUND)
6
+  enable_testing()
7
+  add_subdirectory(tests)
8
+endif (GTEST_FOUND)

+ 7
- 0
src/CMakeLists.txt View File

@@ -0,0 +1,7 @@
1
+cmake_minimum_required(VERSION 3.3)
2
+project(digicode_host)
3
+
4
+set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11")
5
+
6
+set(SOURCE_FILES main.cpp DataAccess/ArduinoSerial.cpp)
7
+add_executable(digicode_host ${SOURCE_FILES})

+ 52
- 0
src/DBO/Result.h View File

@@ -0,0 +1,52 @@
1
+//
2
+// Created by robin on 8/9/15.
3
+//
4
+
5
+#ifndef PDNS_SLAVE_RESULT_H
6
+#define PDNS_SLAVE_RESULT_H
7
+
8
+# include <string>
9
+# include <vector>
10
+
11
+template <class T> class Result
12
+{
13
+public:
14
+    Result();
15
+
16
+    Result(const T& data);
17
+
18
+    virtual ~Result();
19
+
20
+    Result<T>& ok(const T& data);
21
+
22
+    T& getData();
23
+
24
+    bool isSuccess() const;
25
+
26
+    Result<T>& addWarning(const std::string& warning);
27
+
28
+    const std::vector<std::string>& getWarnings() const;
29
+
30
+    Result<T>& error(const std::string& error);
31
+
32
+    const std::string& getError() const;
33
+
34
+    bool operator !() const;
35
+
36
+    operator bool() const;
37
+
38
+    const Result<T>& print() const;
39
+
40
+private:
41
+    T _data;
42
+
43
+    std::vector<std::string> _warnings;
44
+
45
+    std::string _error;
46
+};
47
+
48
+typedef Result<bool> BResult;
49
+
50
+# include "DBO/Result.hxx"
51
+
52
+#endif //PDNS_SLAVE_RESULT_H

+ 89
- 0
src/DBO/Result.hxx View File

@@ -0,0 +1,89 @@
1
+//
2
+// Created by robin on 8/9/15.
3
+//
4
+
5
+#include <iostream>
6
+#include "Result.h"
7
+
8
+template<class T>
9
+Result<T>::Result()
10
+{
11
+}
12
+
13
+template<class T>
14
+Result<T>::Result(const T &data)
15
+        : _data(data)
16
+{
17
+}
18
+
19
+template<class T>
20
+Result<T>::~Result()
21
+{
22
+}
23
+
24
+template<class T>
25
+Result<T> &Result<T>::ok(const T &data)
26
+{
27
+    _data = data;
28
+    return *this;
29
+}
30
+
31
+template<class T>
32
+T &Result<T>::getData()
33
+{
34
+    return _data;
35
+}
36
+
37
+template<class T>
38
+bool Result<T>::isSuccess() const
39
+{
40
+    return _error.empty();
41
+}
42
+
43
+template<class T>
44
+const std::vector<std::string>& Result<T>::getWarnings() const
45
+{
46
+    return _warnings;
47
+}
48
+
49
+template<class T>
50
+Result<T>& Result<T>::addWarning(const std::string &warning)
51
+{
52
+    _warnings.push_back(warning);
53
+    return *this;
54
+}
55
+
56
+template<class T>
57
+bool Result<T>::operator!() const
58
+{
59
+    return !isSuccess();
60
+}
61
+
62
+template<class T>
63
+Result<T>::operator bool() const
64
+{
65
+    return isSuccess();
66
+}
67
+
68
+template<class T>
69
+Result<T> &Result<T>::error(const std::string &error)
70
+{
71
+    _error = error;
72
+    return *this;
73
+}
74
+
75
+template<class T>
76
+const std::string &Result<T>::getError() const
77
+{
78
+    return _error;
79
+}
80
+
81
+template<class T>
82
+const Result<T>& Result<T>::print() const
83
+{
84
+    for (auto warning : _warnings)
85
+        std::cerr << "WARNING: " << warning << std::endl;
86
+    if (!isSuccess())
87
+        std::cerr << "ERROR: " << _error << std::endl;
88
+    return *this;
89
+}

+ 120
- 0
src/DataAccess/ArduinoSerial.cpp View File

@@ -0,0 +1,120 @@
1
+//
2
+// Created by robin on 5/6/16.
3
+//
4
+
5
+
6
+#include <stdio.h>    // Standard input/output definitions
7
+#include <unistd.h>   // UNIX standard function definitions
8
+#include <fcntl.h>    // File control definitions
9
+#include <errno.h>    // Error number definitions
10
+#include <termios.h>  // POSIX terminal control definitions
11
+#include <string.h>   // String function definitions
12
+#include <sys/ioctl.h>
13
+
14
+#include "ArduinoSerial.h"
15
+
16
+ArduinoSerial::ArduinoSerial(std::string port, int baudrate)
17
+    : port_(port)
18
+    , bauderate_(baudrate)
19
+    , fd_(0)
20
+{
21
+}
22
+
23
+ArduinoSerial::~ArduinoSerial()
24
+{
25
+    close();
26
+}
27
+
28
+int ArduinoSerial::open()
29
+{
30
+    struct termios toptions;
31
+
32
+    //fd = open(serialport, O_RDWR | O_NOCTTY | O_NDELAY);
33
+    fd_ = ::open(port_.c_str(), O_RDWR | O_NONBLOCK);
34
+
35
+    if (fd_ == -1)  {
36
+        perror("serialport_init: Unable to open port ");
37
+        return -1;
38
+    }
39
+
40
+    //int iflags = TIOCM_DTR;
41
+    //ioctl(fd, TIOCMBIS, &iflags);     // turn on DTR
42
+    //ioctl(fd, TIOCMBIC, &iflags);    // turn off DTR
43
+
44
+    if (tcgetattr(fd_, &toptions) < 0) {
45
+        perror("serialport_init: Couldn't get term attributes");
46
+        return -1;
47
+    }
48
+    speed_t brate;
49
+    switch(bauderate_) {
50
+        case 4800:
51
+            brate=B4800;
52
+            break;
53
+        case 9600:
54
+            brate=B9600;
55
+            break;
56
+#ifdef B14400
57
+            case 14400:
58
+            brate=B14400;
59
+            break;
60
+#endif
61
+        case 19200:
62
+            brate=B19200;
63
+            break;
64
+#ifdef B28800
65
+            case 28800:
66
+            brate=B28800;
67
+            break;
68
+#endif
69
+        case 38400:
70
+            brate=B38400;
71
+            break;
72
+        case 57600:
73
+            brate=B57600;
74
+            break;
75
+        case 115200:
76
+            brate=B115200;
77
+            break;
78
+        default:
79
+            brate = (speed_t)bauderate_;
80
+    }
81
+    cfsetispeed(&toptions, brate);
82
+    cfsetospeed(&toptions, brate);
83
+
84
+    // 8N1
85
+    toptions.c_cflag &= ~PARENB;
86
+    toptions.c_cflag &= ~CSTOPB;
87
+    toptions.c_cflag &= ~CSIZE;
88
+    toptions.c_cflag |= CS8;
89
+    // no flow control
90
+    toptions.c_cflag &= ~CRTSCTS;
91
+
92
+    //toptions.c_cflag &= ~HUPCL; // disable hang-up-on-close to avoid reset
93
+
94
+    toptions.c_cflag |= CREAD | CLOCAL;  // turn on READ & ignore ctrl lines
95
+    toptions.c_iflag &= ~(IXON | IXOFF | IXANY); // turn off s/w flow ctrl
96
+
97
+    toptions.c_lflag &= ~(ICANON | ECHO | ECHOE | ISIG); // make raw
98
+    toptions.c_oflag &= ~OPOST; // make raw
99
+
100
+    // see: http://unixwiz.net/techtips/termios-vmin-vtime.html
101
+    toptions.c_cc[VMIN]  = 0;
102
+    toptions.c_cc[VTIME] = 0;
103
+    //toptions.c_cc[VTIME] = 20;
104
+
105
+    tcsetattr(fd_, TCSANOW, &toptions);
106
+    if (tcsetattr(fd_, TCSAFLUSH, &toptions) < 0) {
107
+        perror("init_serialport: Couldn't set term attributes");
108
+        return -1;
109
+    }
110
+
111
+    return 0;
112
+}
113
+
114
+void ArduinoSerial::close()
115
+{
116
+    if (fd_ != 0) {
117
+        ::close(fd_);
118
+        fd_ = 0;
119
+    }
120
+}

+ 28
- 0
src/DataAccess/ArduinoSerial.h View File

@@ -0,0 +1,28 @@
1
+//
2
+// Created by robin on 5/6/16.
3
+//
4
+
5
+#ifndef DIGICODE_HOST_ARDUINOSERIAL_H
6
+#define DIGICODE_HOST_ARDUINOSERIAL_H
7
+
8
+
9
+#include <string>
10
+
11
+class ArduinoSerial
12
+{
13
+public:
14
+    ArduinoSerial(std::string port, int baudrate);
15
+    virtual ~ArduinoSerial();
16
+
17
+    int open();
18
+    void close();
19
+
20
+protected:
21
+    std::string port_;
22
+    int bauderate_;
23
+
24
+    int fd_;
25
+};
26
+
27
+
28
+#endif //DIGICODE_HOST_ARDUINOSERIAL_H

+ 74
- 0
src/Interface/CommandLineParser.cpp View File

@@ -0,0 +1,74 @@
1
+//
2
+// Created by robin on 8/8/15.
3
+//
4
+
5
+#include <getopt.h>
6
+#include <iostream>
7
+#include "CommandLineParser.h"
8
+
9
+CommandLineParser::CommandLineParser(int argc, char **argv)
10
+    : _argc(argc)
11
+    , _argv(argv)
12
+{
13
+}
14
+
15
+bool CommandLineParser::parse()
16
+{
17
+    std::string shortOpts;
18
+    option opts[_options.size() + 1];
19
+    for (unsigned i = 0; i < _options.size(); ++i)
20
+    {
21
+        auto opt = _options[i];
22
+        shortOpts += opt->getShortName();
23
+        if (opt->hasValue())
24
+            shortOpts += ":";
25
+        opts[i].name = opt->getLongName().c_str();
26
+        opts[i].has_arg = opt->hasValue();
27
+        opts[i].flag = 0;
28
+        opts[i].val = opt->getShortName();
29
+    }
30
+    opts[_options.size()] = {0, 0, 0, 0};
31
+    int option;
32
+    extern int optind;
33
+    extern char* optarg;
34
+    bool valid = true;
35
+    while ((option = getopt_long(_argc, _argv, shortOpts.c_str(), opts, 0)) != -1)
36
+    {
37
+        bool optValid = false;
38
+        for (unsigned i = 0; i < _options.size(); ++i)
39
+        {
40
+            auto opt = _options[i];
41
+            if (opt->getShortName() == option)
42
+            {
43
+                optValid = true;
44
+                opt->setIsSet(true);
45
+                if (opt->hasValue())
46
+                    opt->addValue(optarg);
47
+            }
48
+        }
49
+        if (!optValid)
50
+            valid = false;
51
+    }
52
+    return optind == _argc && valid;
53
+}
54
+
55
+void CommandLineParser::addOption(CommandLineOption* opt)
56
+{
57
+    _options.push_back(opt);
58
+}
59
+
60
+int CommandLineParser::showHelp(int status, bool stdErr)
61
+{
62
+    auto& out = stdErr ? std::cerr : std::cout;
63
+    out << "Options:" << std::endl;
64
+    for (auto opt : _options)
65
+    {
66
+        out << "  -" << opt->getShortName() << ", --" << opt->getLongName();
67
+        if (opt->hasValue())
68
+        {
69
+            out << " <" << opt->getValueName() << "=" << opt->getDefaultValue() << ">";
70
+        }
71
+        out << " " << opt->getDescription() << std::endl;
72
+    }
73
+    return status;
74
+}

+ 30
- 0
src/Interface/CommandLineParser.h View File

@@ -0,0 +1,30 @@
1
+//
2
+// Created by robin on 8/8/15.
3
+//
4
+
5
+#ifndef PDNS_SLAVE_COMMANDLINEPARSER_H
6
+#define PDNS_SLAVE_COMMANDLINEPARSER_H
7
+
8
+# include "DBO/CommandLineOption.h"
9
+
10
+class CommandLineParser
11
+{
12
+public:
13
+    CommandLineParser(int argc, char** argv);
14
+
15
+    bool parse();
16
+
17
+    int showHelp(int status = 0, bool stdErr = true);
18
+
19
+    void addOption(CommandLineOption* opt);
20
+
21
+private:
22
+    int _argc;
23
+
24
+    char** _argv;
25
+
26
+    std::vector<CommandLineOption*> _options;
27
+};
28
+
29
+
30
+#endif //PDNS_SLAVE_COMMANDLINEPARSER_H

+ 10
- 0
src/main.cpp View File

@@ -0,0 +1,10 @@
1
+#include <iostream>
2
+
3
+#include "DataAccess/ArduinoSerial.h"
4
+
5
+int main()
6
+{
7
+    ArduinoSerial arduinoSerial("/dev/ttyUSB0", 9600);
8
+    std::cout << arduinoSerial.open() << std::endl;
9
+    return 0;
10
+}

+ 11
- 0
tests/CMakeLists.txt View File

@@ -0,0 +1,11 @@
1
+enable_testing()
2
+include_directories(${CHECK_INCLUDE_DIRS})
3
+include_directories(. ../src)
4
+find_package (Threads)
5
+set(LIBS ${LIBS} gtest pthread)
6
+set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11")
7
+add_executable(test-pdns-slave
8
+  test-pdns-slave.cpp
9
+  )
10
+target_link_libraries(test-pdns-slave ${LIBS})
11
+add_test(test-pdns-slave ${CMAKE_CURRENT_BINARY_DIR}/test-pdns-slave)

+ 14
- 0
tests/test-digicode-keyboard.cpp View File

@@ -0,0 +1,14 @@
1
+#include <iostream>
2
+#include <string.h>
3
+#include <gtest/gtest.h>
4
+
5
+TEST(None, None)
6
+{
7
+  ASSERT_TRUE(true);
8
+}
9
+
10
+int main(int argc, char* argv[])
11
+{
12
+  ::testing::InitGoogleTest(&argc, argv);
13
+  return RUN_ALL_TESTS();
14
+}

Loading…
Cancel
Save