simulator: more arguments (#432)

* simulator: improve READMEs

* simulator: use argp for argument parsing

* simulator: plumb SocketCAN device argument

* simulator: change verbosity argument

use a --quiet instead, as --verbose is a "set-only" option without
arguments and cannot be "unset" if default

* simulator: timeout is an option, not an argument

* simulator: github workflow uses --timeout option
This commit is contained in:
Nathan Schulte 2024-08-05 13:43:07 -05:00 committed by GitHub
parent 6ba19ac38e
commit be7a2ddc9c
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
8 changed files with 85 additions and 14 deletions

View File

@ -40,7 +40,7 @@ jobs:
- name: Run Linux Simulator for 10 seconds
working-directory: ./simulator/
run: ./build/fome_simulator 10
run: ./build/fome_simulator --timeout 10
- name: Upload Linux built simulator
uses: actions/upload-artifact@v4

View File

@ -1,4 +1,4 @@
Win32 or posix version of firmware allows to explore rusEFI on a PC without any embedded hardware!
Win32 or POSIX version of firmware allows to explore FOME on a PC without any embedded hardware!
Simulator runs a subset of ECU on your pc, easier to debug some things, tighter dev loop.
@ -6,4 +6,3 @@ Simulator runs a subset of ECU on your pc, easier to debug some things, tighter
* mocked analog sensors
* mocked outputs
* SocketCAN integration on Linux

65
simulator/libc_argp.h Normal file
View File

@ -0,0 +1,65 @@
#include <stdlib.h>
#include <argp.h>
/* Program documentation. */
static char doc[] =
"FOME Simulator -- https://wiki.fome.tech/";
/* A description of the arguments we accept. */
static char args_doc[] = "";
/* The options we understand. */
static struct argp_option options[] = {
{"quiet", 'q', 0, 0, "Don't produce verbose output", 0 },
{"socketcan-device",
'd', "DEVICE", 0, "SocketCAN DEVICE (default: can0) to use", 0 },
{"timeout", 't', "SECONDS", 0, "Run for SECONDS and then exit (negative values ignored)", 0 },
{ 0, 0, 0, 0, 0, 0 }
};
/* Used by main to communicate with parse_opt. */
struct arguments
{
int quiet;
char * socketcanDevice;
int timeout;
};
/* Parse a single option. */
static error_t
parse_opt(int key, char * arg, struct argp_state * state)
{
/* Get the input argument from argp_parse, which we
know is a pointer to our arguments structure. */
struct arguments * arguments = (struct arguments *)state->input;
switch (key) {
case 'd':
arguments->socketcanDevice = arg;
break;
case 'q':
arguments->quiet = 1;
break;
case 't':
arguments->timeout = atoi(arg);
break;
case ARGP_KEY_ARG:
/* if (state->arg_num >= 0) */ {
/* Too many arguments. */
argp_usage(state);
}
break;
case ARGP_KEY_END:
case ARGP_KEY_NO_ARGS:
break;
default:
return ARGP_ERR_UNKNOWN;
}
return 0;
}
/* Our argp parser. */
static struct argp argp = { options, parse_opt, args_doc, doc, 0, 0, 0 };

View File

@ -18,6 +18,7 @@
#include "chprintf.h"
#include "rusEfiFunctionalTest.h"
#include "flash_int.h"
#include "libc_argp.h"
#include <iostream>
#include <filesystem>
@ -143,6 +144,14 @@ static virtual_timer_t exitTimer;
int main(int argc, char** argv) {
setbuf(stdout, NULL);
struct arguments arguments;
arguments.quiet = 0;
arguments.socketcanDevice = (char *)"can0";
arguments.timeout = -1;
argp_parse(&argp, argc, argv, 0, 0, &arguments);
verboseMode = arguments.quiet == 0;
/*
* System initializations.
* - HAL initialization, this also initializes the configured device drivers
@ -153,12 +162,10 @@ int main(int argc, char** argv) {
halInit();
chSysInit();
if (argc == 2) {
int timeoutSeconds = atoi(argv[1]);
printf("Running rusEFI simulator for %d seconds, then exiting.\n\n", timeoutSeconds);
if (arguments.timeout >= 0) {
printf("Running rusEFI simulator for %d seconds, then exiting.\n\n", arguments.timeout);
chSysLock();
chVTSetI(&exitTimer, MY_US2ST(timeoutSeconds * 1e6), [](void*) { exit(0); }, nullptr);
chVTSetI(&exitTimer, MY_US2ST(arguments.timeout * 1e6), [](void *) { exit(0); }, nullptr);
chSysUnlock();
}
@ -178,7 +185,7 @@ int main(int argc, char** argv) {
cputs(" - Listening for connections on SD2");
chEvtRegister(chnGetEventSource(&SD2), &sd2fel, 2);
rusEfiFunctionalTest();
rusEfiFunctionalTest(arguments.socketcanDevice);
/*
* Events servicing loop.

View File

@ -0,0 +1 @@
Here we have FOME PC version ("simulator"). Same Makefile would build Win32 or \*nix/POSIX.

View File

@ -1 +0,0 @@
Here we have rusEfi PC version. Same Makefile would build win32 or unux posix.

View File

@ -84,7 +84,7 @@ static void runChprintfTest() {
}
void rusEfiFunctionalTest(void) {
void rusEfiFunctionalTest(char const * const socketcanDevice) {
printToConsole("Running rusEfi simulator version:");
static char versionBuffer[20];
itoa10(versionBuffer, (int)getRusEfiVersion());
@ -128,8 +128,8 @@ void rusEfiFunctionalTest(void) {
#if HAL_USE_CAN
// Set CAN device name
CAND1.deviceName = "can0";
CAND1.deviceName = socketcanDevice;
printf("Using SocketCAN device: %s\n", CAND1.deviceName);
initCan();
#endif // HAL_USE_CAN

View File

@ -7,6 +7,6 @@
#pragma once
void rusEfiFunctionalTest(void);
void rusEfiFunctionalTest(char const * const socketcanDevice);
void printPendingMessages(void);
void logMsg(const char *fmt, ...);