fprime/Ref/Main.cpp
2025-09-10 15:02:07 -07:00

99 lines
3.2 KiB
C++
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

// ======================================================================
// \title Main.cpp
// \author mstarch
// \brief main program for reference application. Intended for CLI-based systems (Linux, macOS)
//
// \copyright
// Copyright 2009-2022, by the California Institute of Technology.
// ALL RIGHTS RESERVED. United States Government Sponsorship
// acknowledged.
// ======================================================================
// Used to access topology functions
#include <Ref/Top/RefTopology.hpp>
// Used for signal handling shutdown
#include <signal.h>
// Used for command line argument processing
#include <getopt.h>
// Used for printf functions
#include <cstdlib>
// Used to get the Os::Console
#include <Os/Os.hpp>
/**
* \brief print commandline help message
*
* This will print a command line help message including the available command line arguments.
*
* @param app: name of application
*/
void print_usage(const char* app) {
(void)printf("Usage: ./%s [options]\n-a\thostname/IP address\n-p\tport_number\n", app);
}
/**
* \brief shutdown topology cycling on signal
*
* The reference topology allows for a simulated cycling of the rate groups. This simulated cycling needs to be stopped
* in order for the program to shutdown. This is done via handling signals such that it is performed via Ctrl-C
*
* @param signum
*/
static void signalHandler(int signum) {
Ref::stopRateGroups();
}
/**
* \brief execute the program
*
* This F´ program is designed to run in standard environments (e.g. Linux/macOs running on a laptop). Thus it uses
* command line inputs to specify how to connect.
*
* @param argc: argument count supplied to program
* @param argv: argument values supplied to program
* @return: 0 on success, something else on failure
*/
int main(int argc, char* argv[]) {
Os::init();
U16 port_number = 0;
I32 option = 0;
char* hostname = nullptr;
// Loop while reading the getopt supplied options
while ((option = getopt(argc, argv, "hp:a:")) != -1) {
switch (option) {
// Handle the -a argument for address/hostname
case 'a':
hostname = optarg;
break;
// Handle the -p port number argument
case 'p':
port_number = static_cast<U16>(atoi(optarg));
break;
// Cascade intended: help output
case 'h':
// Cascade intended: help output
case '?':
// Default case: output help and exit
default:
print_usage(argv[0]);
return (option == 'h') ? 0 : 1;
}
}
// Object for communicating state to the reference topology
Ref::TopologyState inputs;
inputs.hostname = hostname;
inputs.port = port_number;
// Setup program shutdown via Ctrl-C
signal(SIGINT, signalHandler);
signal(SIGTERM, signalHandler);
(void)printf("Hit Ctrl-C to quit\n");
// Setup, cycle, and teardown topology
Ref::setupTopology(inputs);
Ref::startRateGroups(Fw::TimeInterval(1, 0)); // Program loop cycling rate groups at 1Hz
Ref::teardownTopology(inputs);
(void)printf("Exiting...\n");
return 0;
}