From eeffbc46f2b18e5463dd5129d10ad445d6f3a6f4 Mon Sep 17 00:00:00 2001 From: harrypotter360 Date: Mon, 2 Sep 2024 23:46:07 +0200 Subject: [PATCH] Potter360 : add -s option to use socket --- fxlink/fxlink.h | 5 +++++ fxlink/main.c | 11 ++++++++++- fxlink/modes/interactive.c | 29 +++++++++++++++++++++++++---- 3 files changed, 40 insertions(+), 5 deletions(-) diff --git a/fxlink/fxlink.h b/fxlink/fxlink.h index 60afbed..650473a 100644 --- a/fxlink/fxlink.h +++ b/fxlink/fxlink.h @@ -16,10 +16,15 @@ struct fxlink_options { FILE *log_file; /* Extra details (mainly interactive messages) */ bool verbose; + bool write_to_socket; }; extern struct fxlink_options options; +/* Input/output socket filename in case of -s option */ +extern char* socket_in; +extern char* socket_out; + /* Main function for -l */ int main_list(struct fxlink_filter *filter, delay_t *delay, libusb_context *context); diff --git a/fxlink/main.c b/fxlink/main.c index 4a49f5d..cd9b287 100644 --- a/fxlink/main.c +++ b/fxlink/main.c @@ -41,6 +41,7 @@ static const char *help_string = " -w Wait this many seconds for a calculator to connect\n" " -w Wait indefinitely for a calculator to connect\n" " -f Filter which calculators we connect to (see below)\n" +" -s Send received data to IN file and send data in OUT file to calculator\n" " --libusb-log=LEVEL libusb log level (NONE, ERROR, WARNING, INFO, DEBUG)\n" "\n" "Mode-specific options:\n" @@ -62,6 +63,10 @@ static const char *help_string = /* Global options */ struct fxlink_options options; +/* Input/output socket filenames for -s option */ +char *socket_in; +char *socket_out; + int main(int argc, char **argv) { int rc=1, mode=0, error=0, option=0, loglevel=LIBUSB_LOG_LEVEL_WARNING; @@ -72,7 +77,7 @@ int main(int argc, char **argv) options.log_file = NULL; options.verbose = false; - + options.write_to_socket = false; setlocale(LC_ALL, ""); //--- @@ -108,6 +113,10 @@ int main(int argc, char **argv) case 'l': case 'b': case 's': + options.write_to_socket = true; + socket_in = argv[optind]; + socket_out = argv[optind+1]; + break; case 'i': case 't': case 'p': diff --git a/fxlink/modes/interactive.c b/fxlink/modes/interactive.c index 3c23c7b..42dbd7c 100644 --- a/fxlink/modes/interactive.c +++ b/fxlink/modes/interactive.c @@ -39,10 +39,18 @@ static void handle_new_message(struct fxlink_device *fdev, } if(fxlink_message_is_fxlink_text(msg)) { - char const *str = msg->data; - + char *str = msg->data; + str[msg->size] = '\0'; if(options.verbose) printf("------------------\n"); + if(options.write_to_socket) + { + FILE *fPtr; + fPtr = fopen(socket_in, "a"); + fputs(str, fPtr); + fputs("\n\0",fPtr); + fclose(fPtr); + } fwrite(str, 1, msg->size, stdout); #if 0 if(str[msg->size - 1] != '\n') { @@ -120,8 +128,20 @@ int main_interactive(struct fxlink_filter *filter, delay_t *delay, static uint8_t buffer[2048]; /* Current message */ struct fxlink_transfer *tr = NULL; - + FILE *fptr; + char c[100]; while(1) { + + if(options.write_to_socket) + { + if ((fptr = fopen(socket_out, "r"))) { + fscanf(fptr, "%[^\n]", c); + printf("%i",strlen(c)); + fxlink_device_start_bulk_OUT(fdev,"fxlink", "text", &c, strlen(c), false); + remove(socket_out); + } + } + fxlink_sdl2_handle_events(); int transferred = -1; @@ -140,6 +160,8 @@ int main_interactive(struct fxlink_filter *filter, delay_t *delay, } if(transferred <= 0) continue; + + /* Either start a new message or continue an unfinished one */ if(tr == NULL) @@ -156,7 +178,6 @@ int main_interactive(struct fxlink_filter *filter, delay_t *delay, tr = NULL; } } - /* Warning for unfinished transfer */ if(tr) { wlog("unfinished transfer interrupted by disconnection\n");