static void dxco_widgets_update(void) { char val[64]; int ret; ret = iio_device_attr_read(dev, "dcxo_tune_coarse", val, sizeof(val)); if (ret > 0) gtk_widget_show(glb_widgets[dcxo_coarse_num].widget); ret = iio_device_attr_read(dev, "dcxo_tune_fine", val, sizeof(val)); if (ret > 0) gtk_widget_show(glb_widgets[dcxo_fine_num].widget); }
static void mcs_cb (GtkWidget *widget, gpointer data) { unsigned step; char temp[40], ensm_mode[40]; iio_device_attr_read(dev, "ensm_mode", ensm_mode, sizeof(ensm_mode)); /* Move the parts int ALERT for MCS */ iio_device_attr_write(dev, "ensm_mode", "alert"); iio_device_attr_write(dev_slave, "ensm_mode", "alert"); for (step = 1; step <= 5; step++) { sprintf(temp, "%d", step); /* Don't change the order here - the master controls the SYNC GPIO */ iio_device_debug_attr_write(dev_slave, "multichip_sync", temp); iio_device_debug_attr_write(dev, "multichip_sync", temp); sleep(0.1); } iio_device_attr_write(dev, "ensm_mode", ensm_mode); iio_device_attr_write(dev_slave, "ensm_mode", ensm_mode); /* The two DDSs are synced by toggling the ENABLE bit */ if (dev_dds_master) dds_sync(); }
static void iio_combo_box_update_value(struct iio_widget *widget, const char *src, size_t len) { int (*compare)(const char *, const char *); GtkComboBox *combo_box; GtkTreeIter iter; GtkTreeModel *model; char text2[1024], *item; gchar **items_avail = NULL, **saveditems_avail; gboolean has_iter; ssize_t ret; combo_box = GTK_COMBO_BOX(widget->widget); model = gtk_combo_box_get_model(combo_box); if (widget->attr_name_avail) { if (widget->chn) ret = iio_channel_attr_read(widget->chn, widget->attr_name_avail, text2, sizeof(text2)); else ret = iio_device_attr_read(widget->dev, widget->attr_name_avail, text2, sizeof(text2)); if (ret < 0) return; /* may use gtk_combo_box_text_remove_all gtk3 only */ gtk_list_store_clear (GTK_LIST_STORE (model)); saveditems_avail = items_avail = g_strsplit (text2, " ", 0); for (; NULL != *items_avail; items_avail++) { if (*items_avail[0] == '\0') continue; gtk_combo_box_text_append_text(GTK_COMBO_BOX_TEXT(widget->widget), *items_avail); } if (saveditems_avail) g_strfreev(saveditems_avail); } if (widget->priv) compare = widget->priv; else compare = strcmp; has_iter = gtk_tree_model_get_iter_first(model, &iter); while (has_iter) { gtk_tree_model_get(model, &iter, 0, &item, -1); if (compare (src, item) == 0) { gtk_combo_box_set_active_iter(combo_box, &iter); g_free(item); break; } g_free(item); has_iter = gtk_tree_model_iter_next(model, &iter); } }
int iio_device_attr_read_double(const struct iio_device *dev, const char *attr, double *val) { char buf[1024]; ssize_t ret = iio_device_attr_read(dev, attr, buf, sizeof(buf)); if (ret < 0) return (int) ret; else return read_double(buf, val); }
static void mcs_cb (GtkWidget *widget, gpointer data) { unsigned step; struct iio_channel *tx_sample_master, *tx_sample_slave; long long tx_sample_master_freq, tx_sample_slave_freq; char temp[40], ensm_mode[40]; unsigned tmp; static int fix_slave_tune = 1; tx_sample_master = iio_device_find_channel(dev, "voltage0", true); tx_sample_slave = iio_device_find_channel(dev_slave, "voltage0", true); iio_channel_attr_read_longlong(tx_sample_master, "sampling_frequency", &tx_sample_master_freq); iio_channel_attr_read_longlong(tx_sample_slave, "sampling_frequency", &tx_sample_slave_freq); if (tx_sample_master_freq != tx_sample_slave_freq) { printf("tx_sample_master_freq != tx_sample_slave_freq\nUpdating...\n"); iio_channel_attr_write_longlong(tx_sample_slave, "sampling_frequency", tx_sample_master_freq); } if (fix_slave_tune) { iio_device_reg_read(dev, 0x6, &tmp); iio_device_reg_write(dev_slave, 0x6, tmp); iio_device_reg_read(dev, 0x7, &tmp); iio_device_reg_write(dev_slave, 0x7, tmp); fix_slave_tune = 0; } iio_device_attr_read(dev, "ensm_mode", ensm_mode, sizeof(ensm_mode)); /* Move the parts int ALERT for MCS */ iio_device_attr_write(dev, "ensm_mode", "alert"); iio_device_attr_write(dev_slave, "ensm_mode", "alert"); for (step = 1; step <= 5; step++) { sprintf(temp, "%d", step); /* Don't change the order here - the master controls the SYNC GPIO */ iio_device_debug_attr_write(dev_slave, "multichip_sync", temp); iio_device_debug_attr_write(dev, "multichip_sync", temp); sleep(0.1); } iio_device_attr_write(dev, "ensm_mode", ensm_mode); iio_device_attr_write(dev_slave, "ensm_mode", ensm_mode); #if 0 iio_device_debug_attr_write(dev, "multichip_sync", "6"); iio_device_debug_attr_write(dev_slave, "multichip_sync", "6"); #endif }
static void iio_combo_box_update(struct iio_widget *widget) { ssize_t len; char text[1024]; if (widget->chn) len = iio_channel_attr_read(widget->chn, widget->attr_name, text, sizeof(text)); else len = iio_device_attr_read(widget->dev, widget->attr_name, text, sizeof(text)); if (len > 0) iio_combo_box_update_value(widget, text, len); }
int iioc_read_attr(struct iio_device *dev, const char *attr_name, char *attr_val) { char *ret; if (attr_val == NULL || attr_val == "" || attr_name == NULL || attr_name == "") { IIOC_DBG("Parameter attr_name or attr_val is NULL.\n"); return -EINVAL; } ret = (char *)iio_device_find_attr(dev, attr_name); if (!ret) { IIOC_DBG("%s attribute is not found.\n", attr_name); return -EINVAL; } return iio_device_attr_read(dev, attr_name, attr_val, 30); }
int iio_device_attr_read_longlong(const struct iio_device *dev, const char *attr, long long *val) { char *end, buf[1024]; long long value; ssize_t ret = iio_device_attr_read(dev, attr, buf, sizeof(buf)); if (ret < 0) return (int) ret; value = strtoll(buf, &end, 0); if (end == buf) return -EINVAL; *val = value; return 0; }
static int read_each_attr(struct iio_device *dev, bool is_debug, int (*cb)(struct iio_device *dev, const char *attr, const char *val, size_t len, void *d), void *data) { int ret; char *buf, *ptr; unsigned int i, count; /* We need a big buffer here; 1 MiB should be enough */ buf = malloc(0x100000); if (!buf) return -ENOMEM; if (is_debug) { count = iio_device_get_debug_attrs_count(dev); ret = (int) iio_device_debug_attr_read(dev, NULL, buf, 0x100000); } else { count = iio_device_get_attrs_count(dev); ret = (int) iio_device_attr_read(dev, NULL, buf, 0x100000); } if (ret < 0) goto err_free_buf; ptr = buf; for (i = 0; i < count; i++) { const char *attr; int32_t len = (int32_t) ntohl(*(uint32_t *) ptr); if (is_debug) attr = iio_device_get_debug_attr(dev, i); else attr = iio_device_get_attr(dev, i); ptr += 4; if (len > 0) { ret = cb(dev, attr, ptr, (size_t) len, data); if (ret < 0) goto err_free_buf; if (len & 0x3) len = ((len >> 2) + 1) << 2; ptr += len; } }
static void iio_toggle_button_update(struct iio_widget *widget) { char buf[0x100]; ssize_t ret; if (widget->chn) ret = iio_channel_attr_read(widget->chn, widget->attr_name, buf, sizeof(buf)); else ret = iio_device_attr_read(widget->dev, widget->attr_name, buf, sizeof(buf)); if (ret > 0) iio_toggle_button_update_value(widget, buf, ret); else if (ret == -ENODEV) gtk_widget_hide(widget->widget); }
static void glb_settings_update_labels(void) { float rates[6]; char tmp[160], buf[1024]; ssize_t ret; ret = iio_device_attr_read(dev, "ensm_mode", buf, sizeof(buf)); if (ret > 0) gtk_label_set_text(GTK_LABEL(ensm_mode), buf); else gtk_label_set_text(GTK_LABEL(ensm_mode), "<error>"); ret = iio_device_attr_read(dev, "calib_mode", buf, sizeof(buf)); if (ret > 0) gtk_label_set_text(GTK_LABEL(calib_mode), buf); else gtk_label_set_text(GTK_LABEL(calib_mode), "<error>"); ret = iio_device_attr_read(dev, "trx_rate_governor", buf, sizeof(buf)); if (ret > 0) gtk_label_set_text(GTK_LABEL(trx_rate_governor), buf); else gtk_label_set_text(GTK_LABEL(trx_rate_governor), "<error>"); ret = iio_channel_attr_read( iio_device_find_channel(dev, "voltage0", false), "gain_control_mode", buf, sizeof(buf)); if (ret > 0) gtk_label_set_text(GTK_LABEL(rx_gain_control_rx1), buf); else gtk_label_set_text(GTK_LABEL(rx_gain_control_rx1), "<error>"); if (is_2rx_2tx) { ret = iio_channel_attr_read( iio_device_find_channel(dev, "voltage1", false), "gain_control_mode", buf, sizeof(buf)); if (ret > 0) gtk_label_set_text(GTK_LABEL(rx_gain_control_rx2), buf); else gtk_label_set_text(GTK_LABEL(rx_gain_control_rx2), "<error>"); } ret = iio_device_attr_read(dev, "rx_path_rates", buf, sizeof(buf)); if (ret > 0) { sscanf(buf, "BBPLL:%f ADC:%f R2:%f R1:%f RF:%f RXSAMP:%f", &rates[0], &rates[1], &rates[2], &rates[3], &rates[4], &rates[5]); sprintf(tmp, "BBPLL: %4.3f ADC: %4.3f R2: %4.3f R1: %4.3f RF: %4.3f RXSAMP: %4.3f", rates[0] / 1e6, rates[1] / 1e6, rates[2] / 1e6, rates[3] / 1e6, rates[4] / 1e6, rates[5] / 1e6); gtk_label_set_text(GTK_LABEL(rx_path_rates), tmp); } else { gtk_label_set_text(GTK_LABEL(rx_path_rates), "<error>"); } ret = iio_device_attr_read(dev, "tx_path_rates", buf, sizeof(buf)); if (ret > 0) { sscanf(buf, "BBPLL:%f DAC:%f T2:%f T1:%f TF:%f TXSAMP:%f", &rates[0], &rates[1], &rates[2], &rates[3], &rates[4], &rates[5]); sprintf(tmp, "BBPLL: %4.3f DAC: %4.3f T2: %4.3f T1: %4.3f TF: %4.3f TXSAMP: %4.3f", rates[0] / 1e6, rates[1] / 1e6, rates[2] / 1e6, rates[3] / 1e6, rates[4] / 1e6, rates[5] / 1e6); gtk_label_set_text(GTK_LABEL(tx_path_rates), tmp); } else { gtk_label_set_text(GTK_LABEL(tx_path_rates), "<error>"); } iio_widget_update(&rx_widgets[rx1_gain]); if (is_2rx_2tx) iio_widget_update(&rx_widgets[rx2_gain]); }
int main(int argc, char **argv) { struct iio_context *ctx; int c, option_index = 0, arg_index = 0; enum backend backend = LOCAL; unsigned int major, minor; char git_tag[8]; int ret; while ((c = getopt_long(argc, argv, "+hn:x:", options, &option_index)) != -1) { switch (c) { case 'h': usage(); return EXIT_SUCCESS; case 'n': if (backend != LOCAL) { ERROR("-x and -n are mutually exclusive\n"); return EXIT_FAILURE; } backend = NETWORK; arg_index += 2; break; case 'x': if (backend != LOCAL) { ERROR("-x and -n are mutually exclusive\n"); return EXIT_FAILURE; } backend = XML; arg_index += 2; break; case '?': return EXIT_FAILURE; } } if (arg_index >= argc) { fprintf(stderr, "Incorrect number of arguments.\n\n"); usage(); return EXIT_FAILURE; } iio_library_get_version(&major, &minor, git_tag); INFO("Library version: %u.%u (git tag: %s)\n", major, minor, git_tag); if (backend == XML) ctx = iio_create_xml_context(argv[arg_index]); else if (backend == NETWORK) ctx = iio_create_network_context(argv[arg_index]); else ctx = iio_create_local_context(); if (!ctx) { ERROR("Unable to create IIO context\n"); return EXIT_FAILURE; } INFO("IIO context created with %s backend.\n", iio_context_get_name(ctx)); ret = iio_context_get_version(ctx, &major, &minor, git_tag); if (!ret) INFO("Backend version: %u.%u (git tag: %s)\n", major, minor, git_tag); else ERROR("Unable to get backend version: %i\n", ret); unsigned int nb_devices = iio_context_get_devices_count(ctx); INFO("IIO context has %u devices:\n", nb_devices); unsigned int i; for (i = 0; i < nb_devices; i++) { const struct iio_device *dev = iio_context_get_device(ctx, i); const char *name = iio_device_get_name(dev); INFO("\t%s: %s\n", iio_device_get_id(dev), name ? name : "" ); unsigned int nb_channels = iio_device_get_channels_count(dev); INFO("\t\t%u channels found:\n", nb_channels); unsigned int j; for (j = 0; j < nb_channels; j++) { struct iio_channel *ch = iio_device_get_channel(dev, j); const char *type_name; if (iio_channel_is_output(ch)) type_name = "output"; else type_name = "input"; name = iio_channel_get_name(ch); INFO("\t\t\t%s: %s (%s)\n", iio_channel_get_id(ch), name ? name : "", type_name); unsigned int nb_attrs = iio_channel_get_attrs_count(ch); if (!nb_attrs) continue; INFO("\t\t\t%u channel-specific attributes found:\n", nb_attrs); unsigned int k; for (k = 0; k < nb_attrs; k++) { const char *attr = iio_channel_get_attr(ch, k); char buf[1024]; ret = (int) iio_channel_attr_read(ch, attr, buf, 1024); if (ret > 0) INFO("\t\t\t\tattr %u: %s" " value: %s\n", k, attr, buf); else if (ret == -ENOSYS) INFO("\t\t\t\tattr %u: %s\n", k, attr); else ERROR("Unable to read attribute %s\n", attr); } } unsigned int nb_attrs = iio_device_get_attrs_count(dev); if (!nb_attrs) continue; INFO("\t\t%u device-specific attributes found:\n", nb_attrs); for (j = 0; j < nb_attrs; j++) { const char *attr = iio_device_get_attr(dev, j); char buf[1024]; ret = (int) iio_device_attr_read(dev, attr, buf, 1024); if (ret > 0) INFO("\t\t\t\tattr %u: %s value: %s\n", j, attr, buf); else if (ret == -ENOSYS) INFO("\t\t\t\tattr %u: %s\n", j, attr); else ERROR("Unable to read attribute: %s\n", attr); } } iio_context_destroy(ctx); return EXIT_SUCCESS; }