Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add version option #27

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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 "")
Expand Down
40 changes: 40 additions & 0 deletions include/user-input.h
Original file line number Diff line number Diff line change
@@ -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 <[email protected]>
//
// 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 <https://www.gnu.org/licenses/>.

#ifndef USER_INPUT_H
#define USER_INPUT_H

#include <time.h>
#include <string>

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
23 changes: 23 additions & 0 deletions include/version.h
Original file line number Diff line number Diff line change
@@ -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 <[email protected]>
//
// 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 <https://www.gnu.org/licenses/>.

#ifndef VERSION_H
#define VERSION_H

constexpr auto VERSION = "txtempus 1.0.0.0";

#endif
64 changes: 30 additions & 34 deletions src/txtempus.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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 <getopt.h>
#include <limits.h>
#include <sched.h>
#include <signal.h>
Expand All @@ -29,12 +28,15 @@
#include <time.h>
#include <strings.h>
#include <string.h>
#include <iostream>

#include <string>
#include <memory>

#include "hardware-control.h"
#include "time-signal-source.h"
#include "user-input.h"
#include "version.h"

static bool verbose = false;
static bool dryrun = false;
Expand Down Expand Up @@ -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<TimeSignalSource> 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]);

Expand All @@ -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;
Expand Down
85 changes: 85 additions & 0 deletions src/user-input.cc
Original file line number Diff line number Diff line change
@@ -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 <[email protected]>
//
// 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 <https://www.gnu.org/licenses/>.
//
// 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 <limits.h>
#include <getopt.h>

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;
}
}
}