Commit bf7151226a6b67a6170d8e56c5fe19922cc00657

Authored by Peter M. Groen
1 parent 1238cbed

Added CLI Tools

CMakeLists.txt
... ... @@ -9,16 +9,20 @@ project_header(osdev_mqtt)
9 9  
10 10 add_subdirectory(src)
11 11  
12   -if(ENABLE_EXAMPLES)
  12 +if(ENABLE_EXAMPLES STREQUAL "ON")
13 13 add_subdirectory(examples/connect)
14 14 add_subdirectory(examples/pub)
15 15 add_subdirectory(examples/sub)
16 16 add_subdirectory(examples/subunsub)
17 17 endif()
18 18  
19   -if(ENABLE_TESTS)
  19 +if(ENABLE_TESTS STREQUAL "ON")
20 20 add_subdirectory(test)
21 21 endif()
22 22  
  23 +if(ENABLE_TOOLS STREQUAL "ON")
  24 + add_subdirectory(tools/publish_cli)
  25 +endif()
  26 +
23 27 include(packaging)
24 28 package_component()
... ...
README.md
... ... @@ -33,10 +33,13 @@ Just check your package manager for the correct package-name.
33 33  
34 34 ## Building the examples and tests
35 35 ### Tests
36   -Normally only the library is being build. By adding `-DENABLE_TESTING` to the cmake step, the unittests are being build also. Make sure a broker is running on localhost and type `test/mqtt_test` to run the tests.
  36 +Normally only the library is being build. By adding `-DENABLE_TESTING=ON` to the cmake step, the unittests are being build also. Make sure a broker is running on localhost and type `test/mqtt_test` to run the tests.
37 37  
38 38 ### Examples
39   -By adding the flag `-DENABLE_EXAMPLES` to the cmake step, the examples are being build together with the library.
  39 +By adding the flag `-DENABLE_EXAMPLES=ON` to the cmake step, the examples are being build together with the library.
  40 +
  41 +### Tools
  42 +By adding the flag `-DENABLE_TOOLS=ON` to the cmake step, the tools are being build together with the library.
40 43  
41 44 In build/bin there are two examples, test_mqtt_pu and test_mqtt_sub. Have a broker running, like mosquitto or flashmq capable of accepting anonymous connections. Start the "sub" part and couple of moments later the "pub" part. If all went well, you should see two screens in sync running.
42 45  
... ...
tools/publish_cli/CMakeLists.txt
... ... @@ -17,17 +17,27 @@ set(SRC_LIST
17 17 ${CMAKE_CURRENT_SOURCE_DIR}/argumentparserbase.cpp
18 18 ${CMAKE_CURRENT_SOURCE_DIR}/argumentparser.h
19 19 ${CMAKE_CURRENT_SOURCE_DIR}/argumentparser.cpp
  20 + ${CMAKE_CURRENT_SOURCE_DIR}/filereader.h
  21 + ${CMAKE_CURRENT_SOURCE_DIR}/publisher.h
  22 + ${CMAKE_CURRENT_SOURCE_DIR}/publisher.cpp
20 23 ${CMAKE_CURRENT_SOURCE_DIR}/main.cpp
21 24 )
22 25  
23   -include(library)
24   -add_libraries(
25   - PUBLIC
26   - Boost::boost
27   - Boost::regex
28   - mqtt-cpp
29   - paho-mqtt3a
  26 +add_executable( ${PROJECT_NAME}
  27 + ${SRC_LIST}
  28 +)
  29 +
  30 +target_link_libraries(
  31 + ${PROJECT_NAME}
  32 + mqtt-cpp
  33 +)
  34 +
  35 +set_target_properties( ${PROJECT_NAME} PROPERTIES
  36 + RUNTIME_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/bin
  37 + LIBRARY_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/lib
  38 + ARCHIVE_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/lib
30 39 )
31 40  
32 41 include(installation)
33   -install_component()
  42 +install_application()
  43 +
... ...
tools/publish_cli/argumentparser.cpp
... ... @@ -23,32 +23,121 @@
23 23  
24 24 #include <iostream>
25 25  
  26 +ArgumentParser::ArgumentParser(int num_of_args, char* argv[])
  27 + : ArgumentParserBase(num_of_args, argv)
  28 +{
  29 + if (count() == 0)
  30 + {
  31 + printUsage();
  32 + }
  33 +}
  34 +
26 35 std::string ArgumentParser::getHost()
27 36 {
28   - return getArgument("-h");
  37 + // -h | --host
  38 + std::string host = getArgument("-h");
  39 + if (host.empty())
  40 + {
  41 + host = getArgument("--host");
  42 + if(host.empty())
  43 + {
  44 + host = "127.0.0.1";
  45 + }
  46 + }
  47 +
  48 + return host;
29 49 }
30 50  
31 51 std::string ArgumentParser::getPort()
32 52 {
33   - return getArgument("-p");
  53 + // -p | --port
  54 + std::string port = getArgument("-p");
  55 + if (port.empty())
  56 + {
  57 + port = getArgument("--port");
  58 + if(port.empty())
  59 + {
  60 + port = "1883";
  61 + }
  62 + }
  63 +
  64 + return port;
34 65 }
35 66  
36 67 std::string ArgumentParser::getUserName()
37 68 {
38   - return getArgument("-u");
  69 + // -u | --username
  70 + std::string username = getArgument("-u");
  71 + if (username.empty())
  72 + {
  73 + username = getArgument("--username");
  74 + }
  75 +
  76 + return username;
39 77 }
40 78  
41 79 std::string ArgumentParser::getPassword()
42 80 {
43   - return getArgument("-pw");
  81 + // -pw | --password
  82 + std::string password = getArgument("-pw");
  83 + if (password.empty())
  84 + {
  85 + password = getArgument("--password");
  86 + }
  87 +
  88 + return password;
44 89 }
45 90  
46 91 std::string ArgumentParser::getTopic()
47 92 {
48   - return getArgument("-t");
  93 + // -t | --topic
  94 + std::string topic = getArgument("-t");
  95 + if (topic.empty())
  96 + {
  97 + topic = getArgument("--topic");
  98 + }
  99 +
  100 + return topic;
49 101 }
50 102  
51 103 std::string ArgumentParser::getFile()
52 104 {
53   - return getArgument("-f");}
54   -std::string ArgumentParser::getMessage();
55 105 \ No newline at end of file
  106 + // -f | --file
  107 + std::string file = getArgument("-f");
  108 + if (file.empty())
  109 + {
  110 + file = getArgument("--file");
  111 + }
  112 +
  113 + return file;
  114 +}
  115 +
  116 +std::string ArgumentParser::getMessage()
  117 +{
  118 + // -m | --message
  119 + std::string message = getArgument("-m");
  120 + if (message.empty())
  121 + {
  122 + message = getArgument("--message");
  123 + }
  124 +
  125 + return message;
  126 +}
  127 +
  128 +void ArgumentParser::printUsage()
  129 +{
  130 + std::cout << "Usage: " << getProgramName() << " [options]" << std::endl;
  131 + std::cout << "Options:" << std::endl;
  132 + std::cout << " -h | --host <host> Hostname or IP address of the MQTT broker. (Default 127.0.0.1)" << std::endl;
  133 + std::cout << " -p | --port <port> Port number of the MQTT broker. (Default 1883)" << std::endl;
  134 + std::cout << " -u | --username <user> Username for authentication" << std::endl;
  135 + std::cout << " -pw | --password <pass> Password for authentication" << std::endl;
  136 + std::cout << " -t | --topic <topic> Topic to publish the message to" << std::endl;
  137 + std::cout << " -f | --file <file> File to publish" << std::endl;
  138 + std::cout << " -m | --message <message> Message to publish" << std::endl;
  139 + std::cout << std::endl;
  140 + std::cout << "Example(s): " << getProgramName() << " -h localhost -p 1883 -u user -pw password -t topic -f file.txt" << std::endl;
  141 + std::cout << " " << getProgramName() << " -h localhost -p 1883 -t topic -f file.txt" << std::endl;
  142 + std::cout << " " << getProgramName() << " -t topic -f file.txt" << std::endl;
  143 + std::cout << std::endl;
  144 +}
56 145 \ No newline at end of file
... ...
tools/publish_cli/argumentparser.h
... ... @@ -26,7 +26,7 @@
26 26 class ArgumentParser : public ArgumentParserBase
27 27 {
28 28 public:
29   - ArgumentParser(int num_of_args, char* argv[]) : ArgumentParserBase(num_of_args, argv) {}
  29 + ArgumentParser(int num_of_args, char* argv[]);
30 30 virtual ~ArgumentParser() {}
31 31  
32 32 std::string getHost();
... ... @@ -36,4 +36,6 @@ public:
36 36 std::string getTopic();
37 37 std::string getFile();
38 38 std::string getMessage();
  39 +
  40 + void printUsage();
39 41 };
40 42 \ No newline at end of file
... ...
tools/publish_cli/argumentparserbase.h
... ... @@ -35,10 +35,13 @@ public:
35 35 /// @brief Returns the argument for the given option
36 36 std::string getArgument(const std::string &option);
37 37  
  38 + /// @brief Returns the program name
  39 + std::string getProgramName() const { return m_programName; }
  40 +
38 41 /// @brief Returns the number of arguments
39 42 int count() const { return m_arguments.size(); }
40 43  
41 44 private: // members (Giggity)
42   - std::string m_programName;
43   - std::unordered_map<std::string, std::string> m_arguments;
  45 + std::string m_programName = std::string();
  46 + std::unordered_map<std::string, std::string> m_arguments = {};
44 47 };
45 48 \ No newline at end of file
... ...
tools/publish_cli/filereader.h
... ... @@ -21,6 +21,8 @@
21 21 * ***************************************************************************/
22 22 #pragma once
23 23  
  24 +#include <fstream>
  25 +
24 26 class FileReader
25 27 {
26 28 public:
... ... @@ -35,4 +37,4 @@ class FileReader
35 37 std::string content((std::istreambuf_iterator<char>(file)), std::istreambuf_iterator<char>());
36 38 return content;
37 39 }
38   -}
39 40 \ No newline at end of file
  41 +};
40 42 \ No newline at end of file
... ...
tools/publish_cli/main.cpp
... ... @@ -21,8 +21,102 @@
21 21 * ***************************************************************************/
22 22 #include <iostream>
23 23  
  24 +#include "argumentparser.h"
  25 +#include "filereader.h"
  26 +#include "publisher.h"
  27 +
  28 +enum TIME_RES
  29 +{
  30 + T_MICRO,
  31 + T_MILLI,
  32 + T_SECONDS
  33 +};
  34 +
  35 +std::uint64_t getEpochUSecs()
  36 +{
  37 + auto tsUSec =std::chrono::time_point_cast<std::chrono::microseconds>(std::chrono::system_clock::now());
  38 + return static_cast<std::uint64_t>(tsUSec.time_since_epoch().count());
  39 +}
  40 +
  41 +
  42 +void sleepcp( int number, TIME_RES resolution = T_MILLI ) // Cross-platform sleep function
  43 +{
  44 + int factor = 0; // Should not happen..
  45 +
  46 + switch( resolution )
  47 + {
  48 + case T_MICRO:
  49 + factor = 1;
  50 + break;
  51 +
  52 + case T_MILLI:
  53 + factor = 1000;
  54 + break;
  55 +
  56 + case T_SECONDS:
  57 + factor = 1000000;
  58 + break;
  59 + }
  60 +
  61 + usleep( number * factor );
  62 +}
  63 +
24 64 int main(int argc, char* argv[])
25 65 {
26   - std::cout << "Hello, World!" << std::endl;
  66 + ArgumentParser parser(argc, argv);
  67 +
  68 + std::string host = parser.getHost();
  69 + std::string port = parser.getPort();
  70 + std::string user = parser.getUserName();
  71 + std::string pass = parser.getPassword();
  72 + std::string topic = parser.getTopic();
  73 + std::string file = parser.getFile();
  74 + std::string message = parser.getMessage();
  75 +
  76 + if(!file.empty() && !message.empty())
  77 + {
  78 + std::cerr << "Error: Cannot specify both file and message" << std::endl;
  79 + return -1;
  80 + }
  81 +
  82 + if(file.empty() && message.empty())
  83 + {
  84 + std::cerr << "Error: Must specify either file or message" << std::endl;
  85 + return -1;
  86 + }
  87 +
  88 + std::string content = std::string();
  89 + if(!file.empty())
  90 + {
  91 + content = FileReader::read_file(file);
  92 + if(content.empty())
  93 + {
  94 + std::cerr << "Error: File is empty" << std::endl;
  95 + return -1;
  96 + }
  97 + }
  98 +
  99 + if(!message.empty())
  100 + {
  101 + content = message;
  102 + }
  103 +
  104 + // Ok, we have all the options, lets check the mandatory ones
  105 + if(topic.empty())
  106 + {
  107 + std::cerr << "Error: Must specify a topic" << std::endl;
  108 + return -1;
  109 + }
  110 +
  111 + // Create the MQTT client
  112 + Publisher oPublisher;
  113 + oPublisher.connect(host, std::stoi(port), user, pass);
  114 +
  115 + // Wait a couple of seconds to make sure the connection is established
  116 + sleepcp(2, T_SECONDS);
  117 +
  118 + oPublisher.publish(topic, content);
  119 + sleepcp(2, T_SECONDS);
  120 +
27 121 return 0;
28 122 }
29 123 \ No newline at end of file
... ...
tools/publish_cli/publisher.cpp 0 → 100644
  1 +/* ****************************************************************************
  2 + * Copyright 2019 Open Systems Development BV *
  3 + * *
  4 + * Permission is hereby granted, free of charge, to any person obtaining a *
  5 + * copy of this software and associated documentation files (the "Software"), *
  6 + * to deal in the Software without restriction, including without limitation *
  7 + * the rights to use, copy, modify, merge, publish, distribute, sublicense, *
  8 + * and/or sell copies of the Software, and to permit persons to whom the *
  9 + * Software is furnished to do so, subject to the following conditions: *
  10 + * *
  11 + * The above copyright notice and this permission notice shall be included in *
  12 + * all copies or substantial portions of the Software. *
  13 + * *
  14 + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR *
  15 + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, *
  16 + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL *
  17 + * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER *
  18 + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING *
  19 + * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER *
  20 + * DEALINGS IN THE SOFTWARE. *
  21 + * ***************************************************************************/
  22 +
  23 +#include "publisher.h"
  24 +
  25 +Publisher::Publisher()
  26 + : m_mqtt_client( "publish_cli" )
  27 +{
  28 +
  29 +}
  30 +
  31 +void Publisher::connect(const std::string &hostname, int portnumber, const std::string &username, const std::string &password, const std::string &lwt_topic, const std::string &lwt_message)
  32 +{
  33 + m_mqtt_client.connect( hostname, portnumber,
  34 + osdev::components::mqtt::Credentials( username, password ),
  35 + osdev::components::mqtt::mqtt_LWT( lwt_topic, lwt_message ), true,
  36 + osdev::components::log::LogSettings{ osdev::components::log::LogLevel::Debug, osdev::components::log::LogMask::None } );
  37 +
  38 + std::cout << "Client state : " << m_mqtt_client.state() << std::endl;
  39 +}
  40 +
  41 +void Publisher::publish( const std::string &message_topic, const std::string &message_payload )
  42 +{
  43 + osdev::components::mqtt::MqttMessage message( message_topic, true, false, message_payload );
  44 + std::cout << "[Publisher::publish] - Publising message : " << message_payload << " to topic : " << message_topic << std::endl;
  45 + osdev::components::mqtt::Token t_result = m_mqtt_client.publish( message, 0 );
  46 +}
... ...
tools/publish_cli/publisher.h 0 → 100644
  1 +/* ****************************************************************************
  2 + * Copyright 2019 Open Systems Development BV *
  3 + * *
  4 + * Permission is hereby granted, free of charge, to any person obtaining a *
  5 + * copy of this software and associated documentation files (the "Software"), *
  6 + * to deal in the Software without restriction, including without limitation *
  7 + * the rights to use, copy, modify, merge, publish, distribute, sublicense, *
  8 + * and/or sell copies of the Software, and to permit persons to whom the *
  9 + * Software is furnished to do so, subject to the following conditions: *
  10 + * *
  11 + * The above copyright notice and this permission notice shall be included in *
  12 + * all copies or substantial portions of the Software. *
  13 + * *
  14 + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR *
  15 + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, *
  16 + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL *
  17 + * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER *
  18 + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING *
  19 + * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER *
  20 + * DEALINGS IN THE SOFTWARE. *
  21 + * ***************************************************************************/
  22 +#pragma once
  23 +
  24 +// std
  25 +#include <memory>
  26 +#include <string>
  27 +
  28 +// osdev::components::mqtt
  29 +#include "mqttclient.h"
  30 +#include "compat-c++14.h"
  31 +
  32 +class Publisher
  33 +{
  34 +public:
  35 + Publisher();
  36 +
  37 + virtual ~Publisher() {}
  38 +
  39 + void connect( const std::string &hostname, int portnumber = 1883, const std::string &username = std::string(), const std::string &password = std::string()
  40 + , const std::string &lwt_topic = std::string(), const std::string &lwt_message = std::string() );
  41 +
  42 + void publish( const std::string &message_topic, const std::string &message_payload );
  43 +
  44 +private:
  45 + osdev::components::mqtt::MqttClient m_mqtt_client;
  46 +};
0 47 \ No newline at end of file
... ...