From 53a69f89bca2593b6ee942860a069b4f1029720e Mon Sep 17 00:00:00 2001 From: Andrei Popa Date: Thu, 19 Oct 2023 17:45:01 +0300 Subject: [PATCH] plugins: adrv9002: added api validation to profile generator Signed-off-by: Andrei Popa --- plugins/adrv9002.c | 67 ++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 67 insertions(+) diff --git a/plugins/adrv9002.c b/plugins/adrv9002.c index 5ac61936..98b1977d 100644 --- a/plugins/adrv9002.c +++ b/plugins/adrv9002.c @@ -2094,6 +2094,67 @@ static void profile_gen_save_dialog_response(GtkDialog *dialog, gint response_id gtk_widget_hide(GTK_WIDGET(chooser)); } +static char *profile_gen_cli_get_api() +{ + // run profile gen cli command + FILE *file; + char out[BUFSIZ], command[BUFSIZ]; + + sprintf(command, "%s --version", profile_gen_cli_get_cmd()); + + // open the command for reading + file = popen(command, "r"); + if (file == NULL) { + return NULL; + } + + // read the output + fflush(file); + int ret = fread(out, sizeof(char), sizeof(out), file); + fclose(file); + if (ret == 0) { + return NULL; + } + + // command output may contain characters after the API version + char *version = extract_value_between(out, "Profile generator API: ", "\n"); + if (version == NULL) { + return extract_value_between(out, "Profile generator API: ", ""); + } + + return version; +} + +static bool profile_gen_check_api(gpointer data) +{ + struct plugin_private *priv = data; + char version[256], message[BUFSIZ]; + + if (iio_device_debug_attr_read(priv->adrv9002, "api_version", version, sizeof(version)) < 0) { + sprintf(message, "\nCould not read API version!"); + profile_gen_set_debug_info(data, message); + goto err; + } + + char *supported_version = profile_gen_cli_get_api(); + if (supported_version == NULL) { + sprintf(message, "\nFailed to get profile generator API!"); + profile_gen_set_debug_info(data, message); + goto err; + } + + if (strcmp(version, supported_version) != 0) { + sprintf(message, "\nAPI version %s is not supported, the device uses %s!", + supported_version, version); + profile_gen_set_debug_info(data, message); + goto err; + } + + return true; +err: + return false; +} + static void profile_gen_load_config_to_device(GtkButton *self, gpointer data) { struct plugin_private *priv = data; @@ -2107,6 +2168,12 @@ static void profile_gen_load_config_to_device(GtkButton *self, gpointer data) return; } + // check api version + ret = profile_gen_check_api(priv); + if (!ret) { + return; + } + // run profile gen cli command FILE *file; char out[BUFSIZ], command[BUFSIZ],