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/debug_log.txt b/debug_log.txt deleted file mode 100644 index 9c6b103..0000000 --- a/debug_log.txt +++ /dev/null @@ -1,39 +0,0 @@ -With broker present - -Jul 02 00:50:19 intelnuc64.osdev.nl test_connection[30797]: MQTT Client started[30797]: [MqttClient::MqttClient] -Jul 02 00:50:19 intelnuc64.osdev.nl [MqttClient::eventHandler][30797]: ConnectionTest - starting event handler. -Jul 02 00:50:19 intelnuc64.osdev.nl MqttClient[30797]: ConnectionTest - Request connect -Jul 02 00:50:19 intelnuc64.osdev.nl [ClientPaho][30797]: ConnectionTest_1_83746b87-431f-4ce1-82e3-2543e9b0a37a - Setting the extra onConnected callback. -Jul 02 00:50:19 intelnuc64.osdev.nl [MqttClient::connectionStatusChanged][30797]: ConnectionTest - connection status of wrapped client ConnectionTest_1_83746b87-431f-4ce1-82e3-2543e9b0a37a changed to 2 -Jul 02 00:50:19 intelnuc64.osdev.nl ClientPaho[30797]: ConnectionTest_1_83746b87-431f-4ce1-82e3-2543e9b0a37a - starting callback event handler -Jul 02 00:50:19 intelnuc64.osdev.nl [ClientPaho::onConnectSuccess][30797]: onConnectSucces triggered.. -Jul 02 00:50:19 intelnuc64.osdev.nl [ClientPaho][30797]: onConnectSuccessOnInstance ConnectionTest_1_83746b87-431f-4ce1-82e3-2543e9b0a37a - connected to endpoint localhost:1883 (mqtt version 4, session present FALSE ) -Jul 02 00:50:19 intelnuc64.osdev.nl [MqttClient::connectionStatusChanged][30797]: ConnectionTest - connection status of wrapped client ConnectionTest_1_83746b87-431f-4ce1-82e3-2543e9b0a37a changed to 4 -Jul 02 00:50:30 intelnuc64.osdev.nl mosquitto[29463]: 1656715830: Client ConnectionTest_1_83746b87-431f-4ce1-82e3-2543e9b0a37a closed its connection. - - -Without a broker present - -Jul 02 00:55:33 intelnuc64.osdev.nl test_connection[31574]: MQTT Client started[31574]: [MqttClient::MqttClient] -Jul 02 00:55:33 intelnuc64.osdev.nl [MqttClient::eventHandler][31574]: ConnectionTest - starting event handler. -Jul 02 00:55:33 intelnuc64.osdev.nl MqttClient[31574]: ConnectionTest - Request connect -Jul 02 00:55:33 intelnuc64.osdev.nl [ClientPaho][31574]: ConnectionTest_1_9eaee6bb-325b-406a-ae70-b4f3e7a41268 - Setting the extra onConnected callback. -Jul 02 00:55:33 intelnuc64.osdev.nl [MqttClient::connectionStatusChanged][31574]: ConnectionTest - connection status of wrapped client ConnectionTest_1_9eaee6bb-325b-406a-ae70-b4f3e7a41268 changed to 2 -Jul 02 00:55:33 intelnuc64.osdev.nl ClientPaho[31574]: ConnectionTest_1_9eaee6bb-325b-406a-ae70-b4f3e7a41268 - starting callback event handler -Jul 02 00:55:33 intelnuc64.osdev.nl ClientPaho[31574]: onConnectFailureOnInstanceConnectionTest_1_9eaee6bb-325b-406a-ae70-b4f3e7a41268 - connection failed with code MQTTASYNC_FAILURE (TCP/TLS connect failure) -Jul 02 00:55:33 intelnuc64.osdev.nl [MqttClient::connectionStatusChanged][31574]: ConnectionTest - connection status of wrapped client ConnectionTest_1_9eaee6bb-325b-406a-ae70-b4f3e7a41268 changed to 0 - -Jul 02 00:55:42 intelnuc64.osdev.nl systemd[1]: Starting Mosquitto MQTT Broker... -Jul 02 00:55:42 intelnuc64.osdev.nl mosquitto[31610]: 1656716142: mosquitto version 2.0.14 starting -Jul 02 00:55:42 intelnuc64.osdev.nl mosquitto[31610]: 1656716142: Config loaded from /etc/mosquitto/mosquitto.conf. -Jul 02 00:55:42 intelnuc64.osdev.nl mosquitto[31610]: 1656716142: Starting in local only mode. Connections will only be possible from clients running on this machine. -Jul 02 00:55:42 intelnuc64.osdev.nl mosquitto[31610]: 1656716142: Create a configuration file which defines a listener to allow remote access. -Jul 02 00:55:42 intelnuc64.osdev.nl mosquitto[31610]: 1656716142: For more details see https://mosquitto.org/documentation/authentication-methods/ -Jul 02 00:55:42 intelnuc64.osdev.nl mosquitto[31610]: 1656716142: Opening ipv4 listen socket on port 1883. -Jul 02 00:55:42 intelnuc64.osdev.nl mosquitto[31610]: 1656716142: Opening ipv6 listen socket on port 1883. -Jul 02 00:55:42 intelnuc64.osdev.nl mosquitto[31610]: 1656716142: mosquitto version 2.0.14 running -Jul 02 00:55:45 intelnuc64.osdev.nl mosquitto[31610]: 1656716145: New connection from 127.0.0.1:42196 on port 1883. -Jul 02 00:55:45 intelnuc64.osdev.nl mosquitto[31610]: 1656716145: New client connected from 127.0.0.1:42196 as ConnectionTest_1_9eaee6bb-325b-406a-ae70-b4f3e7a41268 (p2, c1, k5). -Jul 02 00:55:53 intelnuc64.osdev.nl mosquitto[31610]: 1656716153: Client ConnectionTest_1_9eaee6bb-325b-406a-ae70-b4f3e7a41268 closed its connection. - - diff --git a/tools/publish_cli/CMakeLists.txt b/tools/publish_cli/CMakeLists.txt new file mode 100644 index 0000000..2a3e998 --- /dev/null +++ b/tools/publish_cli/CMakeLists.txt @@ -0,0 +1,43 @@ +cmake_minimum_required(VERSION 3.12) +LIST(APPEND CMAKE_MODULE_PATH ${CMAKE_SOURCE_DIR}/submodules/cmake) + +include(projectheader) +project_header(publish_cli) + +find_package( Boost REQUIRED COMPONENTS regex ) + +include(compiler) + +include_directories( + ${CMAKE_CURRENT_SOURCE_DIR}/../../include +) + +set(SRC_LIST + ${CMAKE_CURRENT_SOURCE_DIR}/argumentparserbase.h + ${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 +) + +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_application() + diff --git a/tools/publish_cli/argumentparser.cpp b/tools/publish_cli/argumentparser.cpp new file mode 100644 index 0000000..377f6af --- /dev/null +++ b/tools/publish_cli/argumentparser.cpp @@ -0,0 +1,143 @@ +/* **************************************************************************** + * Copyright 2024 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 "argumentparser.h" + +#include + +ArgumentParser::ArgumentParser(int num_of_args, char* argv[]) + : ArgumentParserBase(num_of_args, argv) +{ + if (count() == 0) + { + printUsage(); + } +} + +std::string ArgumentParser::getHost() +{ + // -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() +{ + // -p | --port + std::string port = getArgument("-p"); + if (port.empty()) + { + port = getArgument("--port"); + if(port.empty()) + { + port = "1883"; + } + } + + return port; +} + +std::string ArgumentParser::getUserName() +{ + // -u | --username + std::string username = getArgument("-u"); + if (username.empty()) + { + username = getArgument("--username"); + } + + return username; +} + +std::string ArgumentParser::getPassword() +{ + // -pw | --password + std::string password = getArgument("-pw"); + if (password.empty()) + { + password = getArgument("--password"); + } + + return password; +} + +std::string ArgumentParser::getTopic() +{ + // -t | --topic + std::string topic = getArgument("-t"); + if (topic.empty()) + { + topic = getArgument("--topic"); + } + + return topic; +} + +std::string ArgumentParser::getFile() +{ + // -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 new file mode 100644 index 0000000..40e94e1 --- /dev/null +++ b/tools/publish_cli/argumentparser.h @@ -0,0 +1,41 @@ +/* **************************************************************************** + * Copyright 2024 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 + +#include "argumentparserbase.h" + +class ArgumentParser : public ArgumentParserBase +{ +public: + ArgumentParser(int num_of_args, char* argv[]); + virtual ~ArgumentParser() {} + + std::string getHost(); + std::string getPort(); + std::string getUserName(); + std::string getPassword(); + std::string getTopic(); + std::string getFile(); + std::string getMessage(); + + void printUsage(); +}; \ No newline at end of file diff --git a/tools/publish_cli/argumentparserbase.cpp b/tools/publish_cli/argumentparserbase.cpp new file mode 100644 index 0000000..2e1b861 --- /dev/null +++ b/tools/publish_cli/argumentparserbase.cpp @@ -0,0 +1,62 @@ +/* **************************************************************************** + * Copyright 2024 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 "argumentparserbase.h" + +ArgumentParserBase::ArgumentParserBase(int num_of_args, char* argv[]) +{ + // Store the program name + m_programName = argv[0]; + + // Store the arguments + for (int i = 1; i < num_of_args; i++) + { + std::string arg = argv[i]; + if (arg[0] == '-') + { + if (i + 1 < num_of_args) + { + m_arguments[arg] = argv[i + 1]; + } + else + { + m_arguments[arg] = ""; + } + } + } +} + +ArgumentParserBase::~ArgumentParserBase() +{ + m_arguments.clear(); + m_programName.clear(); +} + +std::string ArgumentParserBase::getArgument(const std::string &option) +{ + auto it = m_arguments.find(option); + if (it != m_arguments.end()) + { + return it->second; + } + return ""; +} diff --git a/tools/publish_cli/argumentparserbase.h b/tools/publish_cli/argumentparserbase.h new file mode 100644 index 0000000..6ca016b --- /dev/null +++ b/tools/publish_cli/argumentparserbase.h @@ -0,0 +1,47 @@ +/* **************************************************************************** + * Copyright 2024 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 +#include +#include + +/*! + * @brief ArgumentParserBase class + */ +class ArgumentParserBase +{ +public: + ArgumentParserBase(int num_of_args, char* argv[]); + virtual ~ArgumentParserBase(); + + /// @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::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 new file mode 100644 index 0000000..0d5dcf4 --- /dev/null +++ b/tools/publish_cli/filereader.h @@ -0,0 +1,40 @@ +/* **************************************************************************** + * Copyright 2024 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 + +#include + +class FileReader +{ + public: + static std::string read_file(const std::string &file_name) + { + std::ifstream file(file_name); + if (!file.is_open()) + { + throw std::runtime_error("Could not open file: " + file_name); + } + + std::string content((std::istreambuf_iterator(file)), std::istreambuf_iterator()); + return content; + } +}; \ No newline at end of file diff --git a/tools/publish_cli/main.cpp b/tools/publish_cli/main.cpp new file mode 100644 index 0000000..0502ba6 --- /dev/null +++ b/tools/publish_cli/main.cpp @@ -0,0 +1,122 @@ +/* **************************************************************************** + * Copyright 2024 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 + +#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[]) +{ + 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