diff --git a/CMakeLists.txt b/CMakeLists.txt index ec708e9..36eb96d 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -9,13 +9,14 @@ ADD_DEFINITIONS(-Wall -O3) set(CMAKE_INSTALL_PREFIX /usr) -set(SRC_FILES +set(SRC_FILES src/txtempus.cc src/dcf77-source.cc src/wwvb-source.cc src/jjy-source.cc src/msf-source.cc - src/hardware-control.cc) + src/hardware-control.cc + src/user-input.cc) set(INCLUDE_DIRS ${CMAKE_SOURCE_DIR}/include) set(PLATFORM_DEPENDENCIES "") diff --git a/include/user-input.h b/include/user-input.h new file mode 100644 index 0000000..1ad23d4 --- /dev/null +++ b/include/user-input.h @@ -0,0 +1,40 @@ +// -*- mode: c++; c-basic-offset: 2; indent-tabs-mode: nil; -*- +// Part of txtempus, a LF time signal transmitter. +// Copyright (C) 2018 Henner Zeller +// +// This program is free software: you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation, either version 3 of the License, or +// (at your option) any later version. +// +// This program is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. +// +// You should have received a copy of the GNU General Public License +// along with this program. If not, see . + +#ifndef USER_INPUT_H +#define USER_INPUT_H + +#include +#include + +class UserInput { +public: + UserInput(int argc, char *argv[]); + std::string chosen_time; + std::string service; + int zone_offset = 0; + int ttl; + bool verbose = false; + bool dryrun = false; + bool show_help = false; + bool show_version = false; + +private: + void Parse(int argc, char *argv[]); +}; + +#endif diff --git a/include/version.h b/include/version.h new file mode 100644 index 0000000..d086e0e --- /dev/null +++ b/include/version.h @@ -0,0 +1,23 @@ +// -*- mode: c++; c-basic-offset: 2; indent-tabs-mode: nil; -*- +// Part of txtempus, a LF time signal transmitter. +// Copyright (C) 2018 Henner Zeller +// +// This program is free software: you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation, either version 3 of the License, or +// (at your option) any later version. +// +// This program is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. +// +// You should have received a copy of the GNU General Public License +// along with this program. If not, see . + +#ifndef VERSION_H +#define VERSION_H + +constexpr auto VERSION = "txtempus 1.0.0.0"; + +#endif diff --git a/src/txtempus.cc b/src/txtempus.cc index c75c132..191a0f6 100644 --- a/src/txtempus.cc +++ b/src/txtempus.cc @@ -19,7 +19,6 @@ // as DCF77, WWVB, ... to be run on the Raspberry Pi. // Make sure to stay within the regulation limits of HF transmissions! -#include #include #include #include @@ -29,12 +28,15 @@ #include #include #include +#include #include #include #include "hardware-control.h" #include "time-signal-source.h" +#include "user-input.h" +#include "version.h" static bool verbose = false; static bool dryrun = false; @@ -128,51 +130,44 @@ int usage(const char *msg, const char *progname) { "\t-v : Verbose.\n" "\t-n : Dryrun, only showing modulation " "envelope.\n" - "\t-h : This help.\n", + "\t-h : This help \n" + "\t--version : Show version info.\n", msg, progname); return 1; } +void ShowVersionInfo() +{ + std::cout << VERSION << std::endl; +} + } // end anonymous namespace int main(int argc, char *argv[]) { const time_t now = TruncateTo(time(NULL), 60); // Time signals: full minute - std::unique_ptr time_source{}; - time_t chosen_time = now; - int zone_offset = 0; - int ttl = INT_MAX; - int opt; - while ((opt = getopt(argc, argv, "t:z:r:vs:hn")) != -1) { - switch (opt) { - case 'v': - verbose = true; - break; - case 't': - chosen_time = ParseLocalTime(optarg); - if (chosen_time <= 0) return usage("Invalid time string\n", argv[0]); - break; - case 'z': - zone_offset = atoi(optarg); - break; - case 'r': - ttl = atoi(optarg); - break; - case 's': - time_source = CreateTimeSourceFromName(optarg); - break; - case 'n': - dryrun = true; - verbose = true; - ttl = 1; - break; - default: - return usage("", argv[0]); - } + UserInput input(argc, argv); + + if(input.show_version) + { + ShowVersionInfo(); + return 0; } - chosen_time += zone_offset * 60; + time_t chosen_time = now; + if(input.chosen_time != "") + chosen_time = ParseLocalTime(input.chosen_time.c_str()); + + verbose = input.verbose; + dryrun = input.dryrun; + + if (chosen_time <= 0) return usage("Invalid time string\n", argv[0]); + + if (input.show_help) return usage("", argv[0]); + + chosen_time += input.zone_offset * 60; const int time_offset = chosen_time - now; + auto time_source = CreateTimeSourceFromName(input.service.c_str()); if (!time_source) return usage("Please choose a service name with -s option\n", argv[0]); @@ -192,6 +187,7 @@ int main(int argc, char *argv[]) { StartCarrier(&hw, time_source->GetCarrierFrequencyHz()); + int ttl = input.ttl; struct timespec target_wait; for (time_t minute_start = now; !interrupted && ttl--; minute_start += 60) { const time_t transmit_time = minute_start + time_offset; diff --git a/src/user-input.cc b/src/user-input.cc new file mode 100644 index 0000000..c5e25cd --- /dev/null +++ b/src/user-input.cc @@ -0,0 +1,85 @@ +// -*- mode: c++; c-basic-offset: 2; indent-tabs-mode: nil; -*- +// This is txtempus, a LF time signal transmitter. +// Copyright (C) 2018 Henner Zeller +// +// This program is free software: you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation, either version 3 of the License, or +// (at your option) any later version. +// +// This program is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. +// +// You should have received a copy of the GNU General Public License +// along with this program. If not, see . +// +// Time-signal simulating transmitter, supporting various types such +// as DCF77, WWVB, ... to be run on the Raspberry Pi. +// Make sure to stay within the regulation limits of HF transmissions! + +#include "user-input.h" +#include +#include + +UserInput::UserInput(int argc, char *argv[]) + : chosen_time(""), + service{}, + zone_offset(0), + ttl(INT_MAX), + verbose(false), + dryrun(false), + show_help(false), + show_version(false) { + Parse(argc, argv); +} + + +void UserInput::Parse(int argc, char *argv[]) { + int option_flag = 0; + struct option long_options[] = + { + {"version", no_argument, &option_flag, 1}, + {nullptr, 0, nullptr, 0} + }; + + constexpr auto short_options = "t:z:r:vs:hn"; + + int option_index = 0; + int opt{}; + + while ((opt = getopt_long(argc, argv, short_options, long_options, &option_index)) != -1) { + switch (opt) { + case 0: // long options + if (option_flag == 1) + show_version = true; + break; + + // short options + case 'v': + verbose = true; + break; + case 't': + chosen_time = optarg; + break; + case 'z': + zone_offset = atoi(optarg); + break; + case 'r': + ttl = atoi(optarg); + break; + case 's': + service = optarg; + break; + case 'n': + dryrun = true; + verbose = true; + ttl = 1; + break; + default: + show_help = true; + return; + } + } +}