From e5f957677957a8dbc31036d226268094318c7917 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 | 78 +++++++++++++++++++++++++++++++++++++++++++--- 1 file changed, 73 insertions(+), 5 deletions(-) diff --git a/plugins/adrv9002.c b/plugins/adrv9002.c index 5ac61936..f3ccb612 100644 --- a/plugins/adrv9002.c +++ b/plugins/adrv9002.c @@ -1192,7 +1192,8 @@ static void load_profile(GtkFileChooser *chooser, gpointer data) static void profile_gen_set_debug_info(gpointer data, char *string) { struct plugin_private *priv = data; - gtk_label_set_text(GTK_LABEL(gtk_builder_get_object(priv->builder, "label_profile_debug")), string); + gchar *message = g_locale_to_utf8(string, -1, NULL, NULL, NULL); + gtk_label_set_text(GTK_LABEL(gtk_builder_get_object(priv->builder, "label_profile_debug")), message); } static void profile_gen_save_type_changed(GtkComboBoxText* self, struct plugin_private *priv) @@ -2019,7 +2020,7 @@ static char *profile_gen_cli_get_cmd() fflush(file); char out[BUFSIZ]; int ret = fread(out, sizeof(char), sizeof(out), file); - fclose(file); + pclose(file); if (ret == 0) { goto err; @@ -2059,7 +2060,7 @@ static int profile_gen_save_to_file(gchar *filename, struct adrv9002_config *cfg fflush(file); ret = fread(out, sizeof(char), sizeof(out), file); remove(config_filename); - fclose(file); + pclose(file); if (ret == 0) { profile_gen_set_debug_info(data, "\nFailed to read the command output!"); @@ -2088,12 +2089,73 @@ static void profile_gen_save_dialog_response(GtkDialog *dialog, gint response_id // 0 if stream image // 1 if profile - bool file_type = gtk_combo_box_get_active_id(GTK_COMBO_BOX(gtk_builder_get_object(priv->builder, "cb_save_type"))); + bool file_type = atoi(gtk_combo_box_get_active_id(GTK_COMBO_BOX(gtk_builder_get_object(priv->builder, "cb_save_type")))); profile_gen_save_to_file(filename, &cfg, data, file_type); 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); + pclose(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, "\nOnly API version %s is 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 +2169,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], @@ -2129,7 +2197,7 @@ static void profile_gen_load_config_to_device(GtkButton *self, gpointer data) fflush(file); ret = fread(out, sizeof(char), sizeof(out), file); remove(config_filename); - fclose(file); + pclose(file); if (ret == 0) { strcat(message, "\nFailed to read the command output!");