2023-03-03 00:29:00 +01:00
|
|
|
//---------------------------------------------------------------------------//
|
|
|
|
// ==>/[_]\ fxlink: A community communication tool for CASIO calculators. //
|
|
|
|
// |:::| Made by Lephe' as part of the fxSDK. //
|
|
|
|
// \___/ License: MIT <https://opensource.org/licenses/MIT> //
|
|
|
|
//---------------------------------------------------------------------------//
|
|
|
|
|
2021-04-03 11:58:30 +02:00
|
|
|
#include "fxlink.h"
|
2023-03-03 00:29:00 +01:00
|
|
|
#include <fxlink/filter.h>
|
|
|
|
#include <fxlink/logging.h>
|
2021-04-03 11:58:30 +02:00
|
|
|
|
|
|
|
#include <libusb.h>
|
|
|
|
#include <getopt.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <errno.h>
|
2023-03-03 00:29:00 +01:00
|
|
|
#include <locale.h>
|
2021-04-03 11:58:30 +02:00
|
|
|
|
2024-08-26 12:00:24 +02:00
|
|
|
#ifndef FXLINK_DISABLE_SDL2
|
|
|
|
/* Grab main()-related macros */
|
|
|
|
#include <SDL2/SDL.h>
|
|
|
|
#endif
|
|
|
|
|
2021-04-03 11:58:30 +02:00
|
|
|
static const char *help_string =
|
2023-03-03 00:29:00 +01:00
|
|
|
"usage: %1$s (-l|-b|-t) [General options]\n"
|
|
|
|
" %1$s -i [-r] [--fxlink-log[=<FILE>]] [General options]\n"
|
|
|
|
" %1$s -p <FILE> [General options]\n"
|
2024-01-30 22:19:19 +01:00
|
|
|
" %1$s -s <FILES>... [--folder=OUTFOLDER] [General options]\n"
|
2023-03-03 00:29:00 +01:00
|
|
|
"\n"
|
|
|
|
"fxlink interacts with CASIO calculators of the fx and fx-CG families over\n"
|
|
|
|
"the USB port, using libusb. It can also transfer files for Mass Storage\n"
|
|
|
|
"calculators using UDisks2.\n"
|
2021-04-03 11:58:30 +02:00
|
|
|
"\n"
|
2023-03-03 00:29:00 +01:00
|
|
|
"Standard (libusb) modes:\n"
|
|
|
|
" -l, --list List detected calculators on the USB ports\n"
|
|
|
|
" -i, --interactive Messaging with a gint add-in (calc -> PC only)\n"
|
|
|
|
" -t, --tui TUI interactive mode\n"
|
|
|
|
" -p, --push Push a .bin file to the Add-In Push app\n"
|
2021-04-03 11:58:30 +02:00
|
|
|
"\n"
|
2023-03-03 00:29:00 +01:00
|
|
|
"Mass Storage (UDisks2) modes:\n"
|
|
|
|
" -b, --blocks List detected Mass Storage calculators\n"
|
|
|
|
" -s, --send Send a file to a Mass Storage calc and unmount it\n"
|
2021-04-03 11:58:30 +02:00
|
|
|
"\n"
|
|
|
|
"General options:\n"
|
2023-03-03 00:29:00 +01:00
|
|
|
" -v, --verbose Increased verbosity (mostly in -i/-t)\n"
|
|
|
|
" -w <SECONDS> Wait this many seconds for a calculator to connect\n"
|
|
|
|
" -w Wait indefinitely for a calculator to connect\n"
|
|
|
|
" -f <FILTER> Filter which calculators we connect to (see below)\n"
|
|
|
|
" --libusb-log=LEVEL libusb log level (NONE, ERROR, WARNING, INFO, DEBUG)\n"
|
2021-04-03 11:58:30 +02:00
|
|
|
"\n"
|
2023-03-03 00:29:00 +01:00
|
|
|
"Mode-specific options:\n"
|
|
|
|
" --fxlink-log[=FILE] -i: Append fxlink text messages to FILE. Without\n"
|
|
|
|
" argument, a unique name is generated.\n"
|
|
|
|
" -r, --repeat -i: Reconnect if the calc disconnects (implies -w)\n"
|
2024-01-30 22:19:19 +01:00
|
|
|
" --folder=FOLDER -s: Select destination folder for files\n"
|
2021-04-03 11:58:30 +02:00
|
|
|
"\n"
|
2023-03-03 00:29:00 +01:00
|
|
|
"Device filters:\n"
|
|
|
|
" A device filter narrows down what devices we list or connect to by\n"
|
|
|
|
" requiring a list of properties, such as \"p7,serial=00000001\". The\n"
|
|
|
|
" following properties can be tested:\n"
|
|
|
|
" - p7 Protocol 7 calcs (all fx models that use FA-124)\n"
|
|
|
|
" - mass_storage Mass Storage calcs (fx-CG models and the G-III)\n"
|
|
|
|
" - series_cg fx-CG models [udisks2 only]\n"
|
|
|
|
" - series_g3 G-III models [udisks2 only]\n"
|
|
|
|
" - serial=<SERIAL> This serial number (needs write access in libusb)\n";
|
2021-04-03 11:58:30 +02:00
|
|
|
|
2022-05-01 17:20:47 +02:00
|
|
|
/* Global options */
|
|
|
|
struct fxlink_options options;
|
2022-04-19 18:36:09 +02:00
|
|
|
|
2021-04-03 11:58:30 +02:00
|
|
|
int main(int argc, char **argv)
|
|
|
|
{
|
2023-03-03 00:29:00 +01:00
|
|
|
int rc=1, mode=0, error=0, option=0, loglevel=LIBUSB_LOG_LEVEL_WARNING;
|
2021-04-03 11:58:30 +02:00
|
|
|
delay_t delay = delay_seconds(0);
|
2023-03-03 00:29:00 +01:00
|
|
|
struct fxlink_filter *filter = NULL;
|
2022-12-03 13:34:06 +01:00
|
|
|
bool repeat = false;
|
2024-01-30 22:19:19 +01:00
|
|
|
char *outfolder = NULL;
|
2021-04-03 11:58:30 +02:00
|
|
|
|
2022-05-01 17:20:47 +02:00
|
|
|
options.log_file = NULL;
|
2023-03-03 00:29:00 +01:00
|
|
|
options.verbose = false;
|
|
|
|
|
|
|
|
setlocale(LC_ALL, "");
|
2022-05-01 17:20:47 +02:00
|
|
|
|
2021-04-03 11:58:30 +02:00
|
|
|
//---
|
|
|
|
// Command-line argument parsing
|
|
|
|
//---
|
|
|
|
|
2024-01-30 22:19:19 +01:00
|
|
|
enum { LIBUSB_LOG=1, LOG_TO_FILE=2, OUT_FOLDER=3 };
|
2021-04-03 11:58:30 +02:00
|
|
|
const struct option longs[] = {
|
2022-05-01 17:20:47 +02:00
|
|
|
{ "help", no_argument, NULL, 'h' },
|
|
|
|
{ "list", no_argument, NULL, 'l' },
|
|
|
|
{ "blocks", no_argument, NULL, 'b' },
|
|
|
|
{ "send", no_argument, NULL, 's' },
|
|
|
|
{ "interactive", no_argument, NULL, 'i' },
|
2023-03-03 00:29:00 +01:00
|
|
|
{ "tui", no_argument, NULL, 't' },
|
2023-01-18 00:02:29 +01:00
|
|
|
{ "push", no_argument, NULL, 'p' },
|
2021-05-25 20:46:27 +02:00
|
|
|
{ "libusb-log", required_argument, NULL, LIBUSB_LOG },
|
2022-05-01 17:20:47 +02:00
|
|
|
{ "fxlink-log", optional_argument, NULL, LOG_TO_FILE },
|
2022-12-03 13:34:06 +01:00
|
|
|
{ "repeat", no_argument, NULL, 'r' },
|
2023-03-03 00:29:00 +01:00
|
|
|
{ "verbose", no_argument, NULL, 'v' },
|
2024-01-30 22:19:19 +01:00
|
|
|
{ "folder", required_argument, NULL, OUT_FOLDER },
|
2023-03-03 00:29:00 +01:00
|
|
|
/* Deprecated options ignored for compatibility: */
|
|
|
|
{ "quiet", no_argument, NULL, 'q' },
|
|
|
|
{ "unmount", no_argument, NULL, 'u' },
|
|
|
|
{ NULL },
|
2021-04-03 11:58:30 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
while(option >= 0 && option != '?')
|
2023-03-03 00:29:00 +01:00
|
|
|
switch((option = getopt_long(argc, argv, "hlbsitpquf:w::rv", longs, NULL)))
|
2021-04-03 11:58:30 +02:00
|
|
|
{
|
|
|
|
case 'h':
|
|
|
|
fprintf(stderr, help_string, argv[0]);
|
|
|
|
return 0;
|
|
|
|
case 'l':
|
|
|
|
case 'b':
|
|
|
|
case 's':
|
2021-05-25 20:46:27 +02:00
|
|
|
case 'i':
|
2023-03-03 00:29:00 +01:00
|
|
|
case 't':
|
2023-01-18 00:02:29 +01:00
|
|
|
case 'p':
|
2021-04-03 11:58:30 +02:00
|
|
|
mode = option;
|
|
|
|
break;
|
|
|
|
case LIBUSB_LOG:
|
|
|
|
if(!strcmp(optarg, "NONE"))
|
|
|
|
loglevel = LIBUSB_LOG_LEVEL_NONE;
|
|
|
|
else if(!strcmp(optarg, "ERROR"))
|
|
|
|
loglevel = LIBUSB_LOG_LEVEL_ERROR;
|
|
|
|
else if(!strcmp(optarg, "WARNING"))
|
|
|
|
loglevel = LIBUSB_LOG_LEVEL_WARNING;
|
|
|
|
else if(!strcmp(optarg, "INFO"))
|
|
|
|
loglevel = LIBUSB_LOG_LEVEL_INFO;
|
|
|
|
else if(!strcmp(optarg, "DEBUG"))
|
|
|
|
loglevel = LIBUSB_LOG_LEVEL_DEBUG;
|
|
|
|
else fprintf(stderr, "warning: ignoring log level '%s'; should be "
|
|
|
|
"NONE, ERROR, WARNING, INFO or DEBUG\n", optarg);
|
|
|
|
break;
|
2022-04-20 13:07:30 +02:00
|
|
|
case 'q':
|
2023-03-03 00:29:00 +01:00
|
|
|
/* Ignored: -q, --quiet used to control some messages but is now
|
|
|
|
supplanted by not setting -v */
|
2022-04-19 18:36:09 +02:00
|
|
|
break;
|
2022-04-20 18:07:58 +02:00
|
|
|
case 'u':
|
2023-03-03 00:29:00 +01:00
|
|
|
/* Ignored: -u, --unmount used to force unmounting filesystems after -s
|
|
|
|
(which is now the only behavior) */
|
2022-04-20 18:07:58 +02:00
|
|
|
break;
|
2022-12-03 13:34:06 +01:00
|
|
|
case 'r':
|
|
|
|
repeat = true;
|
|
|
|
delay = delay_infinite();
|
|
|
|
break;
|
2023-03-03 00:29:00 +01:00
|
|
|
case 'v':
|
|
|
|
options.verbose = true;
|
|
|
|
break;
|
2022-05-01 17:20:47 +02:00
|
|
|
case LOG_TO_FILE:
|
|
|
|
if(optarg)
|
|
|
|
options.log_file = fopen(optarg, "a");
|
2022-04-20 13:07:30 +02:00
|
|
|
else {
|
2023-03-03 00:29:00 +01:00
|
|
|
char *name = fxlink_gen_file_name(".", "logfile", ".log");
|
2022-05-01 17:20:47 +02:00
|
|
|
printf("--fxlink-log will output in '%s'\n", name);
|
|
|
|
options.log_file = fopen(name, "a");
|
|
|
|
free(name);
|
2022-04-19 18:36:09 +02:00
|
|
|
}
|
|
|
|
break;
|
2021-04-03 11:58:30 +02:00
|
|
|
case 'w':
|
|
|
|
if(!optarg) {
|
|
|
|
delay = delay_infinite();
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
char *end;
|
|
|
|
int seconds = strtol(optarg, &end, 10);
|
|
|
|
if(seconds < 0 || *end != 0) {
|
2023-03-03 00:29:00 +01:00
|
|
|
error = elog("invalid delay '%s'\n", optarg);
|
2021-04-03 11:58:30 +02:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
delay = delay_seconds(seconds);
|
|
|
|
break;
|
|
|
|
case 'f':
|
2023-03-03 00:29:00 +01:00
|
|
|
filter = fxlink_filter_parse(optarg);
|
2021-04-03 11:58:30 +02:00
|
|
|
break;
|
2024-01-30 22:19:19 +01:00
|
|
|
case OUT_FOLDER:
|
|
|
|
outfolder = strdup(optarg);
|
|
|
|
break;
|
2021-04-03 11:58:30 +02:00
|
|
|
case '?':
|
|
|
|
error = 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
if(mode == 's' && optind == argc)
|
2023-03-03 00:29:00 +01:00
|
|
|
error = elog("send mode requires additional arguments (file names)\n");
|
2021-04-03 11:58:30 +02:00
|
|
|
|
2023-01-18 00:02:29 +01:00
|
|
|
if(mode == 'p' && optind == argc)
|
2023-03-03 00:29:00 +01:00
|
|
|
error = elog("push mode requires a file name\n");
|
2023-01-18 00:02:29 +01:00
|
|
|
if(mode == 'p' && optind < argc-1)
|
2023-03-03 00:29:00 +01:00
|
|
|
error = elog("push mode only accepts one file name\n");
|
2023-01-18 00:02:29 +01:00
|
|
|
|
2021-04-03 11:58:30 +02:00
|
|
|
/* No arguments or bad arguments */
|
|
|
|
if(error)
|
|
|
|
return 1;
|
|
|
|
if(!mode) {
|
|
|
|
fprintf(stderr, help_string, argv[0]);
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2023-03-03 00:29:00 +01:00
|
|
|
/* Default filter */
|
|
|
|
if(filter == NULL)
|
|
|
|
filter = calloc(1, sizeof *filter);
|
|
|
|
|
2021-04-03 11:58:30 +02:00
|
|
|
//---
|
|
|
|
// libusb initialization
|
|
|
|
//---
|
|
|
|
|
|
|
|
libusb_context *context = NULL;
|
|
|
|
|
|
|
|
/* Initialize libusb for corresponding modes */
|
2023-03-03 00:29:00 +01:00
|
|
|
if(mode == 'l' || mode == 'i' || mode == 't' || mode == 'p') {
|
2021-04-03 11:58:30 +02:00
|
|
|
if((rc = libusb_init(&context)))
|
2023-03-03 00:29:00 +01:00
|
|
|
return elog_libusb(rc, "error initializing libusb");
|
2021-04-03 11:58:30 +02:00
|
|
|
libusb_set_option(context, LIBUSB_OPTION_LOG_LEVEL, loglevel);
|
2023-03-03 00:29:00 +01:00
|
|
|
fxlink_log_grab_libusb_logs();
|
2021-04-03 11:58:30 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
//---
|
|
|
|
// Main functions
|
|
|
|
//---
|
|
|
|
|
|
|
|
if(mode == 'l') {
|
|
|
|
rc = main_list(filter, &delay, context);
|
|
|
|
}
|
|
|
|
else if(mode == 'b') {
|
|
|
|
#ifndef FXLINK_DISABLE_UDISKS2
|
|
|
|
rc = main_blocks(filter, &delay);
|
|
|
|
#else
|
2023-03-03 00:29:00 +01:00
|
|
|
rc = elog("this fxlink was built without UDisks2; -b is disabled");
|
2021-04-03 11:58:30 +02:00
|
|
|
#endif
|
|
|
|
}
|
|
|
|
else if(mode == 's') {
|
|
|
|
#ifndef FXLINK_DISABLE_UDISKS2
|
2024-01-30 22:19:19 +01:00
|
|
|
rc = main_send(filter, &delay, argv + optind, outfolder);
|
2021-04-03 11:58:30 +02:00
|
|
|
#else
|
2023-03-03 00:29:00 +01:00
|
|
|
rc = elog("this fxlink was built without UDisks2; -s is disabled");
|
2021-04-03 11:58:30 +02:00
|
|
|
#endif
|
|
|
|
}
|
2021-05-25 20:46:27 +02:00
|
|
|
else if(mode == 'i') {
|
2022-12-03 13:34:06 +01:00
|
|
|
do {
|
|
|
|
rc = main_interactive(filter, &delay, context);
|
|
|
|
}
|
|
|
|
while(repeat);
|
2021-04-03 11:58:30 +02:00
|
|
|
}
|
2023-03-03 00:29:00 +01:00
|
|
|
else if(mode == 't') {
|
2024-08-25 23:10:51 +02:00
|
|
|
#ifndef FXLINK_DISABLE_POLL
|
2023-03-03 00:29:00 +01:00
|
|
|
rc = main_tui_interactive(context);
|
2024-08-25 23:10:51 +02:00
|
|
|
#endif
|
2023-03-03 00:29:00 +01:00
|
|
|
}
|
2023-01-18 00:02:29 +01:00
|
|
|
else if(mode == 'p') {
|
|
|
|
rc = main_push(filter, &delay, context, argv + optind);
|
|
|
|
}
|
2021-04-03 11:58:30 +02:00
|
|
|
|
2023-03-03 00:29:00 +01:00
|
|
|
fxlink_filter_free(filter);
|
2022-05-01 17:20:47 +02:00
|
|
|
if(context)
|
|
|
|
libusb_exit(context);
|
|
|
|
if(options.log_file)
|
|
|
|
fclose(options.log_file);
|
2021-04-03 11:58:30 +02:00
|
|
|
return rc;
|
|
|
|
}
|