From bf7151226a6b67a6170d8e56c5fe19922cc00657 Mon Sep 17 00:00:00 2001 From: Peter M. Groen Date: Thu, 5 Dec 2024 01:11:26 +0100 Subject: [PATCH] Added CLI Tools --- CMakeLists.txt | 8 ++++++-- README.md | 7 +++++-- tools/publish_cli/CMakeLists.txt | 26 ++++++++++++++++++-------- tools/publish_cli/argumentparser.cpp | 103 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++------- tools/publish_cli/argumentparser.h | 4 +++- tools/publish_cli/argumentparserbase.h | 7 +++++-- tools/publish_cli/filereader.h | 4 +++- tools/publish_cli/main.cpp | 96 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++- tools/publish_cli/publisher.cpp | 46 ++++++++++++++++++++++++++++++++++++++++++++++ tools/publish_cli/publisher.h | 46 ++++++++++++++++++++++++++++++++++++++++++++++ 10 files changed, 323 insertions(+), 24 deletions(-) create mode 100644 tools/publish_cli/publisher.cpp create mode 100644 tools/publish_cli/publisher.h diff --git a/CMakeLists.txt b/CMakeLists.txt index 022b8a0..9799955 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -9,16 +9,20 @@ project_header(osdev_mqtt) add_subdirectory(src) -if(ENABLE_EXAMPLES) +if(ENABLE_EXAMPLES STREQUAL "ON") add_subdirectory(examples/connect) add_subdirectory(examples/pub) add_subdirectory(examples/sub) add_subdirectory(examples/subunsub) endif() -if(ENABLE_TESTS) +if(ENABLE_TESTS STREQUAL "ON") add_subdirectory(test) endif() +if(ENABLE_TOOLS STREQUAL "ON") + add_subdirectory(tools/publish_cli) +endif() + include(packaging) package_component() diff --git a/README.md b/README.md index 06d03a0..df306b8 100644 --- a/README.md +++ b/README.md @@ -33,10 +33,13 @@ Just check your package manager for the correct package-name. ## Building the examples and tests ### Tests -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. +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. ### Examples -By adding the flag `-DENABLE_EXAMPLES` to the cmake step, the examples are being build together with the library. +By adding the flag `-DENABLE_EXAMPLES=ON` to the cmake step, the examples are being build together with the library. + +### Tools +By adding the flag `-DENABLE_TOOLS=ON` to the cmake step, the tools are being build together with the library. 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. diff --git a/tools/publish_cli/CMakeLists.txt b/tools/publish_cli/CMakeLists.txt index 8dd52f1..2a3e998 100644 --- a/tools/publish_cli/CMakeLists.txt +++ b/tools/publish_cli/CMakeLists.txt @@ -17,17 +17,27 @@ set(SRC_LIST ${CMAKE_CURRENT_SOURCE_DIR}/argumentparserbase.cpp ${CMAKE_CURRENT_SOURCE_DIR}/argumentparser.h ${CMAKE_CURRENT_SOURCE_DIR}/argumentparser.cpp + ${CMAKE_CURRENT_SOURCE_DIR}/filereader.h + ${CMAKE_CURRENT_SOURCE_DIR}/publisher.h + ${CMAKE_CURRENT_SOURCE_DIR}/publisher.cpp ${CMAKE_CURRENT_SOURCE_DIR}/main.cpp ) -include(library) -add_libraries( - PUBLIC - Boost::boost - Boost::regex - mqtt-cpp - paho-mqtt3a +add_executable( ${PROJECT_NAME} + ${SRC_LIST} +) + +target_link_libraries( + ${PROJECT_NAME} + mqtt-cpp +) + +set_target_properties( ${PROJECT_NAME} PROPERTIES + RUNTIME_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/bin + LIBRARY_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/lib + ARCHIVE_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/lib ) include(installation) -install_component() +install_application() + diff --git a/tools/publish_cli/argumentparser.cpp b/tools/publish_cli/argumentparser.cpp index 0d07e73..377f6af 100644 --- a/tools/publish_cli/argumentparser.cpp +++ b/tools/publish_cli/argumentparser.cpp @@ -23,32 +23,121 @@ #include +ArgumentParser::ArgumentParser(int num_of_args, char* argv[]) + : ArgumentParserBase(num_of_args, argv) +{ + if (count() == 0) + { + printUsage(); + } +} + std::string ArgumentParser::getHost() { - return getArgument("-h"); + // -h | --host + std::string host = getArgument("-h"); + if (host.empty()) + { + host = getArgument("--host"); + if(host.empty()) + { + host = "127.0.0.1"; + } + } + + return host; } std::string ArgumentParser::getPort() { - return getArgument("-p"); + // -p | --port + std::string port = getArgument("-p"); + if (port.empty()) + { + port = getArgument("--port"); + if(port.empty()) + { + port = "1883"; + } + } + + return port; } std::string ArgumentParser::getUserName() { - return getArgument("-u"); + // -u | --username + std::string username = getArgument("-u"); + if (username.empty()) + { + username = getArgument("--username"); + } + + return username; } std::string ArgumentParser::getPassword() { - return getArgument("-pw"); + // -pw | --password + std::string password = getArgument("-pw"); + if (password.empty()) + { + password = getArgument("--password"); + } + + return password; } std::string ArgumentParser::getTopic() { - return getArgument("-t"); + // -t | --topic + std::string topic = getArgument("-t"); + if (topic.empty()) + { + topic = getArgument("--topic"); + } + + return topic; } std::string ArgumentParser::getFile() { - return getArgument("-f");} -std::string ArgumentParser::getMessage(); \ No newline at end of file + // -f | --file + std::string file = getArgument("-f"); + if (file.empty()) + { + file = getArgument("--file"); + } + + return file; +} + +std::string ArgumentParser::getMessage() +{ + // -m | --message + std::string message = getArgument("-m"); + if (message.empty()) + { + message = getArgument("--message"); + } + + return message; +} + +void ArgumentParser::printUsage() +{ + std::cout << "Usage: " << getProgramName() << " [options]" << std::endl; + std::cout << "Options:" << std::endl; + std::cout << " -h | --host Hostname or IP address of the MQTT broker. (Default 127.0.0.1)" << std::endl; + std::cout << " -p | --port Port number of the MQTT broker. (Default 1883)" << std::endl; + std::cout << " -u | --username Username for authentication" << std::endl; + std::cout << " -pw | --password Password for authentication" << std::endl; + std::cout << " -t | --topic Topic to publish the message to" << std::endl; + std::cout << " -f | --file File to publish" << std::endl; + std::cout << " -m | --message Message to publish" << std::endl; + std::cout << std::endl; + std::cout << "Example(s): " << getProgramName() << " -h localhost -p 1883 -u user -pw password -t topic -f file.txt" << std::endl; + std::cout << " " << getProgramName() << " -h localhost -p 1883 -t topic -f file.txt" << std::endl; + std::cout << " " << getProgramName() << " -t topic -f file.txt" << std::endl; + std::cout << std::endl; +} \ No newline at end of file diff --git a/tools/publish_cli/argumentparser.h b/tools/publish_cli/argumentparser.h index 23fb5ca..40e94e1 100644 --- a/tools/publish_cli/argumentparser.h +++ b/tools/publish_cli/argumentparser.h @@ -26,7 +26,7 @@ class ArgumentParser : public ArgumentParserBase { public: - ArgumentParser(int num_of_args, char* argv[]) : ArgumentParserBase(num_of_args, argv) {} + ArgumentParser(int num_of_args, char* argv[]); virtual ~ArgumentParser() {} std::string getHost(); @@ -36,4 +36,6 @@ public: std::string getTopic(); std::string getFile(); std::string getMessage(); + + void printUsage(); }; \ No newline at end of file diff --git a/tools/publish_cli/argumentparserbase.h b/tools/publish_cli/argumentparserbase.h index f2b0e63..6ca016b 100644 --- a/tools/publish_cli/argumentparserbase.h +++ b/tools/publish_cli/argumentparserbase.h @@ -35,10 +35,13 @@ public: /// @brief Returns the argument for the given option std::string getArgument(const std::string &option); + /// @brief Returns the program name + std::string getProgramName() const { return m_programName; } + /// @brief Returns the number of arguments int count() const { return m_arguments.size(); } private: // members (Giggity) - std::string m_programName; - std::unordered_map m_arguments; + std::string m_programName = std::string(); + std::unordered_map m_arguments = {}; }; \ No newline at end of file diff --git a/tools/publish_cli/filereader.h b/tools/publish_cli/filereader.h index b26e9b1..0d5dcf4 100644 --- a/tools/publish_cli/filereader.h +++ b/tools/publish_cli/filereader.h @@ -21,6 +21,8 @@ * ***************************************************************************/ #pragma once +#include + class FileReader { public: @@ -35,4 +37,4 @@ class FileReader std::string content((std::istreambuf_iterator(file)), std::istreambuf_iterator()); return content; } -} \ No newline at end of file +}; \ No newline at end of file diff --git a/tools/publish_cli/main.cpp b/tools/publish_cli/main.cpp index 88eaceb..0502ba6 100644 --- a/tools/publish_cli/main.cpp +++ b/tools/publish_cli/main.cpp @@ -21,8 +21,102 @@ * ***************************************************************************/ #include +#include "argumentparser.h" +#include "filereader.h" +#include "publisher.h" + +enum TIME_RES +{ + T_MICRO, + T_MILLI, + T_SECONDS +}; + +std::uint64_t getEpochUSecs() +{ + auto tsUSec =std::chrono::time_point_cast(std::chrono::system_clock::now()); + return static_cast(tsUSec.time_since_epoch().count()); +} + + +void sleepcp( int number, TIME_RES resolution = T_MILLI ) // Cross-platform sleep function +{ + int factor = 0; // Should not happen.. + + switch( resolution ) + { + case T_MICRO: + factor = 1; + break; + + case T_MILLI: + factor = 1000; + break; + + case T_SECONDS: + factor = 1000000; + break; + } + + usleep( number * factor ); +} + int main(int argc, char* argv[]) { - std::cout << "Hello, World!" << std::endl; + ArgumentParser parser(argc, argv); + + std::string host = parser.getHost(); + std::string port = parser.getPort(); + std::string user = parser.getUserName(); + std::string pass = parser.getPassword(); + std::string topic = parser.getTopic(); + std::string file = parser.getFile(); + std::string message = parser.getMessage(); + + if(!file.empty() && !message.empty()) + { + std::cerr << "Error: Cannot specify both file and message" << std::endl; + return -1; + } + + if(file.empty() && message.empty()) + { + std::cerr << "Error: Must specify either file or message" << std::endl; + return -1; + } + + std::string content = std::string(); + if(!file.empty()) + { + content = FileReader::read_file(file); + if(content.empty()) + { + std::cerr << "Error: File is empty" << std::endl; + return -1; + } + } + + if(!message.empty()) + { + content = message; + } + + // Ok, we have all the options, lets check the mandatory ones + if(topic.empty()) + { + std::cerr << "Error: Must specify a topic" << std::endl; + return -1; + } + + // Create the MQTT client + Publisher oPublisher; + oPublisher.connect(host, std::stoi(port), user, pass); + + // Wait a couple of seconds to make sure the connection is established + sleepcp(2, T_SECONDS); + + oPublisher.publish(topic, content); + sleepcp(2, T_SECONDS); + return 0; } \ No newline at end of file diff --git a/tools/publish_cli/publisher.cpp b/tools/publish_cli/publisher.cpp new file mode 100644 index 0000000..f801ebd --- /dev/null +++ b/tools/publish_cli/publisher.cpp @@ -0,0 +1,46 @@ +/* **************************************************************************** + * Copyright 2019 Open Systems Development BV * + * * + * Permission is hereby granted, free of charge, to any person obtaining a * + * copy of this software and associated documentation files (the "Software"), * + * to deal in the Software without restriction, including without limitation * + * the rights to use, copy, modify, merge, publish, distribute, sublicense, * + * and/or sell copies of the Software, and to permit persons to whom the * + * Software is furnished to do so, subject to the following conditions: * + * * + * The above copyright notice and this permission notice shall be included in * + * all copies or substantial portions of the Software. * + * * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR * + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, * + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL * + * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER * + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING * + * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER * + * DEALINGS IN THE SOFTWARE. * + * ***************************************************************************/ + +#include "publisher.h" + +Publisher::Publisher() + : m_mqtt_client( "publish_cli" ) +{ + +} + +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) +{ + m_mqtt_client.connect( hostname, portnumber, + osdev::components::mqtt::Credentials( username, password ), + osdev::components::mqtt::mqtt_LWT( lwt_topic, lwt_message ), true, + osdev::components::log::LogSettings{ osdev::components::log::LogLevel::Debug, osdev::components::log::LogMask::None } ); + + std::cout << "Client state : " << m_mqtt_client.state() << std::endl; +} + +void Publisher::publish( const std::string &message_topic, const std::string &message_payload ) +{ + osdev::components::mqtt::MqttMessage message( message_topic, true, false, message_payload ); + std::cout << "[Publisher::publish] - Publising message : " << message_payload << " to topic : " << message_topic << std::endl; + osdev::components::mqtt::Token t_result = m_mqtt_client.publish( message, 0 ); +} diff --git a/tools/publish_cli/publisher.h b/tools/publish_cli/publisher.h new file mode 100644 index 0000000..d53812c --- /dev/null +++ b/tools/publish_cli/publisher.h @@ -0,0 +1,46 @@ +/* **************************************************************************** + * Copyright 2019 Open Systems Development BV * + * * + * Permission is hereby granted, free of charge, to any person obtaining a * + * copy of this software and associated documentation files (the "Software"), * + * to deal in the Software without restriction, including without limitation * + * the rights to use, copy, modify, merge, publish, distribute, sublicense, * + * and/or sell copies of the Software, and to permit persons to whom the * + * Software is furnished to do so, subject to the following conditions: * + * * + * The above copyright notice and this permission notice shall be included in * + * all copies or substantial portions of the Software. * + * * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR * + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, * + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL * + * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER * + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING * + * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER * + * DEALINGS IN THE SOFTWARE. * + * ***************************************************************************/ +#pragma once + +// std +#include +#include + +// osdev::components::mqtt +#include "mqttclient.h" +#include "compat-c++14.h" + +class Publisher +{ +public: + Publisher(); + + virtual ~Publisher() {} + + void connect( const std::string &hostname, int portnumber = 1883, const std::string &username = std::string(), const std::string &password = std::string() + , const std::string &lwt_topic = std::string(), const std::string &lwt_message = std::string() ); + + void publish( const std::string &message_topic, const std::string &message_payload ); + +private: + osdev::components::mqtt::MqttClient m_mqtt_client; +}; \ No newline at end of file -- libgit2 0.21.4