From 8ec10df7e270ee541f000b215b782a8866b1073b Mon Sep 17 00:00:00 2001 From: Dan Date: Tue, 26 Mar 2024 00:35:47 +0200 Subject: [PATCH 1/2] oscplot.c: Simplify access to iio_device* from struct 'channel_settings' No need to have polymorphism here. Having a variable of type 'iio_device *' is much simpler. Signed-off-by: Dan --- oscplot.c | 58 ++++++++++++++----------------------------------------- 1 file changed, 14 insertions(+), 44 deletions(-) diff --git a/oscplot.c b/oscplot.c index fd091bbd..12a840aa 100644 --- a/oscplot.c +++ b/oscplot.c @@ -192,10 +192,10 @@ struct channel_settings { unsigned type; char *name; char *parent_name; + struct iio_device *dev; struct iio_context *ctx; GdkRGBA graph_color; - struct iio_device * (*get_iio_parent)(PlotChn *); gfloat * (*get_data_ref)(PlotChn *); void (*assert_used_iio_channels)(PlotChn *, bool); void (*destroy)(PlotChn *); @@ -1883,12 +1883,11 @@ bool constellation_transform_function(Transform *tr, gboolean init_transform) /* Plot iio channel definitions */ -static struct iio_device * plot_iio_channel_get_iio_parent(PlotChn *obj); static gfloat* plot_iio_channel_get_data_ref(PlotChn *obj); static void plot_iio_channel_assert_channels(PlotChn *obj, bool assert); static void plot_iio_channel_destroy(PlotChn *obj); -static PlotIioChn * plot_iio_channel_new(struct iio_context *ctx) +static PlotIioChn * plot_iio_channel_new(struct iio_context *ctx, struct iio_device *dev) { PlotIioChn *obj; @@ -1899,8 +1898,8 @@ static PlotIioChn * plot_iio_channel_new(struct iio_context *ctx) } obj->base.type = PLOT_IIO_CHANNEL; + obj->base.dev = dev; obj->base.ctx = ctx; - obj->base.get_iio_parent = *plot_iio_channel_get_iio_parent; obj->base.get_data_ref = *plot_iio_channel_get_data_ref; obj->base.assert_used_iio_channels = *plot_iio_channel_assert_channels; obj->base.destroy = *plot_iio_channel_destroy; @@ -1908,21 +1907,6 @@ static PlotIioChn * plot_iio_channel_new(struct iio_context *ctx) return obj; } -static struct iio_device *plot_iio_channel_get_iio_parent(PlotChn *obj) -{ - PlotIioChn *this = (PlotIioChn *)obj; - struct iio_device *iio_dev = NULL; - struct extra_info *ch_info; - - if (this && this->iio_chn) { - ch_info = iio_channel_get_data(this->iio_chn); - if (ch_info) - iio_dev = ch_info->dev; - } - - return iio_dev; -} - static gfloat* plot_iio_channel_get_data_ref(PlotChn *obj) { PlotIioChn *this = (PlotIioChn *)obj; @@ -1966,7 +1950,6 @@ static void plot_iio_channel_destroy(PlotChn *obj) /* Plot math channel definitions */ -static struct iio_device * plot_math_channel_get_iio_parent(PlotChn *obj); static gfloat * plot_math_channel_get_data_ref(PlotChn *obj); static void plot_math_channel_assert_channels(PlotChn *obj, bool assert); static void plot_math_channel_destroy(PlotChn *obj); @@ -1983,7 +1966,6 @@ static PlotMathChn * plot_math_channel_new(struct iio_context *ctx) obj->base.type = PLOT_MATH_CHANNEL; obj->base.ctx = ctx; - obj->base.get_iio_parent = *plot_math_channel_get_iio_parent; obj->base.get_data_ref = *plot_math_channel_get_data_ref; obj->base.assert_used_iio_channels = *plot_math_channel_assert_channels; obj->base.destroy = *plot_math_channel_destroy; @@ -1991,19 +1973,6 @@ static PlotMathChn * plot_math_channel_new(struct iio_context *ctx) return obj; } -static struct iio_device *plot_math_channel_get_iio_parent(PlotChn *obj) -{ - PlotMathChn *this = (PlotMathChn *)obj; - struct iio_device *iio_dev = NULL; - - if (this && this->iio_device_name) { - iio_dev = iio_context_find_device(this->base.ctx, - this->iio_device_name); - } - - return iio_dev; -} - static gfloat * plot_math_channel_get_data_ref(PlotChn *obj) { PlotMathChn *this = (PlotMathChn *)obj; @@ -2405,17 +2374,10 @@ static void notebook_info_set_page_visibility(GtkNotebook *nb, int page, bool vi static struct iio_device * transform_get_device_parent(Transform *transform) { - struct iio_device *iio_dev = NULL; - PlotChn *plot_ch; - - if (!transform || !transform->plot_channels) + if (!transform || !transform->plot_channels || !transform->plot_channels->data) return NULL; - plot_ch = transform->plot_channels->data; - if (plot_ch) - iio_dev = plot_ch->get_iio_parent(plot_ch); - - return iio_dev; + return ((PlotChn *)transform->plot_channels->data)->dev; } static void update_transform_settings(OscPlot *plot, Transform *transform) @@ -3981,7 +3943,7 @@ static void device_list_treeview_init(OscPlot *plot) iio_channel_get_id(ch); PlotIioChn *pic; - pic = plot_iio_channel_new(priv->ctx); + pic = plot_iio_channel_new(priv->ctx, dev); if (!pic) { fprintf(stderr, "Could not create an iio plot" "channel with name %s in function %s\n", @@ -6340,6 +6302,7 @@ static int math_expression_get_settings(OscPlot *plot, PlotMathChn *pmc) bool invalid_channels; const char *channel_name; char *expression_name; + struct iio_device *dev; math_device_cmb_changed_cb(GTK_COMBO_BOX_TEXT(priv->math_device_select), plot); @@ -6349,6 +6312,13 @@ static int math_expression_get_settings(OscPlot *plot, PlotMathChn *pmc) return -1; } + dev = iio_context_find_device(priv->ctx, active_device); + if (dev) { + pmc->base.dev = dev; + } else { + fprintf(stderr, "Error: Failed to get 'iio_device *' for %s\n", active_device); + } + if (pmc->txt_math_expression) gtk_text_buffer_set_text(priv->math_expression, pmc->txt_math_expression, -1); From f3e8d1756bd727d7f13ed619ae3c62775a2a3106 Mon Sep 17 00:00:00 2001 From: Dan Date: Tue, 26 Mar 2024 01:07:30 +0200 Subject: [PATCH 2/2] osc: Identify iio_device by label 1st, name 2nd, id 3rd These changes help when multiple instances of the same type of driver exist. Otherwise we end up with mutiple iio_device with the same name. Signed-off-by: Dan --- osc.c | 8 ++++---- oscplot.c | 25 +++++++++---------------- 2 files changed, 13 insertions(+), 20 deletions(-) diff --git a/osc.c b/osc.c index 45a26c4b..3c20a0ad 100644 --- a/osc.c +++ b/osc.c @@ -449,7 +449,7 @@ static const char * device_name_check(const char *name) if (!dev) return NULL; - return iio_device_get_name(dev) ?: iio_device_get_id(dev); + return get_iio_device_label_or_name(dev); } /* @@ -1511,7 +1511,7 @@ static int capture_setup(void) min_timeout = timeout; } - rx_update_device_sampling_freq(iio_device_get_id(dev), freq); + rx_update_device_sampling_freq(get_iio_device_label_or_name(dev), freq); } if (ctx) @@ -1818,8 +1818,8 @@ static void init_device_list(struct iio_context *_ctx) iio_channel_set_data(ch, info); } - rx_update_device_sampling_freq(iio_device_get_name(dev) ?: - iio_device_get_id(dev), USE_INTERN_SAMPLING_FREQ); + rx_update_device_sampling_freq( + get_iio_device_label_or_name(dev), USE_INTERN_SAMPLING_FREQ); } } diff --git a/oscplot.c b/oscplot.c index 12a840aa..0774c8e8 100644 --- a/oscplot.c +++ b/oscplot.c @@ -646,8 +646,7 @@ const char * osc_plot_get_active_device (OscPlot *plot) while (next_iter) { gtk_tree_model_get(model, &iter, ELEMENT_REFERENCE, &dev, DEVICE_ACTIVE, &active, -1); if (active) - return iio_device_get_name(dev) ?: - iio_device_get_id(dev); + return get_iio_device_label_or_name(dev); next_iter = gtk_tree_model_iter_next(model, &iter); } @@ -2361,9 +2360,7 @@ static int plot_get_sample_count_for_transform(OscPlot *plot, Transform *transfo if (!iio_dev) iio_dev = priv->current_device; - return plot_get_sample_count_of_device(plot, - iio_device_get_name(iio_dev) ?: - iio_device_get_id(iio_dev)); + return plot_get_sample_count_of_device(plot, get_iio_device_label_or_name(iio_dev)); } static void notebook_info_set_page_visibility(GtkNotebook *nb, int page, bool visbl) @@ -2750,7 +2747,7 @@ static void collect_parameters_from_plot(OscPlot *plot) for (i = 0; i < iio_context_get_devices_count(ctx); i++) { struct iio_device *dev = iio_context_get_device(ctx, i); struct extra_dev_info *info = iio_device_get_data(dev); - const char *dev_name = iio_device_get_name(dev) ?: iio_device_get_id(dev); + const char *dev_name = get_iio_device_label_or_name(dev); if (info->input_device == false) continue; @@ -2998,7 +2995,7 @@ static void device_rx_info_update(OscPlotPrivate *priv) for (i = 0; i < num_devices; i++) { struct iio_device *dev = iio_context_get_device(priv->ctx, i); - const char *name = iio_device_get_name(dev) ?: iio_device_get_id(dev); + const char *name = get_iio_device_label_or_name(dev); struct extra_dev_info *dev_info = iio_device_get_data(dev); double freq, percent, seconds; char freq_prefix, sec_prefix; @@ -3479,7 +3476,7 @@ static bool comboboxtext_input_devices_fill(struct iio_context *iio_ctx, GtkComb if (dev_info->input_device == false) continue; - name = iio_device_get_name(dev) ?: iio_device_get_id(dev); + name = get_iio_device_label_or_name(dev); gtk_combo_box_text_append_text(GTK_COMBO_BOX_TEXT(box), name); } @@ -3919,8 +3916,7 @@ static void device_list_treeview_init(OscPlot *plot) for (i = 0; i < iio_context_get_devices_count(ctx); i++) { struct iio_device *dev = iio_context_get_device(ctx, i); struct extra_dev_info *dev_info = iio_device_get_data(dev); - const char *dev_name = iio_device_get_name(dev) ?: - iio_device_get_id(dev); + const char *dev_name = get_iio_device_label_or_name(dev); if (dev_info->input_device == false) continue; @@ -4029,8 +4025,7 @@ static void saveas_channels_list_fill(OscPlot *plot) for (i = 0; i < num_devices; i++) { struct iio_device *dev = iio_context_get_device(priv->ctx, i); - const char *name = iio_device_get_name(dev) ?: - iio_device_get_id(dev); + const char *name = get_iio_device_label_or_name(dev); struct extra_dev_info *dev_info = iio_device_get_data(dev); if (dev_info->input_device == false) @@ -4722,8 +4717,7 @@ static void save_as(OscPlot *plot, const char *filename, int type) dev = iio_context_get_device(ctx, d); dev_info = iio_device_get_data(dev); - dev_name = iio_device_get_name(dev) ?: - iio_device_get_id(dev); + dev_name = get_iio_device_label_or_name(dev); /* Find which channel need to be saved */ save_channels_mask = get_user_saveas_channel_selection(plot, &nb_channels); @@ -5228,8 +5222,7 @@ static int device_find_by_name(struct iio_context *ctx, const char *name) for (i = 0; i < num_devices; i++) { struct iio_device *dev = iio_context_get_device(ctx, i); - const char *id = iio_device_get_name(dev) ?: - iio_device_get_id(dev); + const char *id = get_iio_device_label_or_name(dev); if (!strcmp(id, name)) return i; }