static void context_destroy(const char *ini_fn) { if (dac_buff) { iio_buffer_destroy(dac_buff); dac_buff = NULL; } iio_context_destroy(ctx); iio_context_destroy(thread_ctx); }
static void *monitor_thread_fn(void *device_name) { struct iio_context *ctx; struct iio_device *dev; uint32_t val; int ret; ctx = iio_create_default_context(); if (!ctx) { fprintf(stderr, "Unable to create IIO context\n"); return (void *)-1; } dev = get_device(ctx, device_name); if (!dev) { fprintf(stderr, "Unable to find IIO device\n"); iio_context_destroy(ctx); return (void *)-1; } /* Give the main thread a moment to start the DMA */ sleep(1); /* Clear all status bits */ iio_device_reg_write(dev, 0x80000088, 0x6); while (app_running) { ret = iio_device_reg_read(dev, 0x80000088, &val); if (ret) { fprintf(stderr, "Failed to read status register: %s\n", strerror(-ret)); continue; } if (device_is_tx) { if (val & 1) fprintf(stderr, "Underflow detected\n"); } else { if (val & 4) fprintf(stderr, "Overflow detected\n"); } /* Clear bits */ if (val) iio_device_reg_write(dev, 0x80000088, val); sleep(1); } iio_context_destroy(ctx); return (void *)0; }
void iioc_ctx_close(struct extra_ctx_info *ctx_info) { if (ctx_info->ctx) iio_context_destroy(ctx_info->ctx); if (ctx_info) free(ctx_info); }
static bool fmcomms2adv_identify(void) { /* Use the OSC's IIO context just to detect the devices */ struct iio_context *osc_ctx = get_context_from_osc(); struct iio_device *osc_dev = iio_context_find_device( osc_ctx, "ad9361-phy"); if (!osc_dev || !iio_device_get_debug_attrs_count(osc_dev)) return false; ctx = osc_create_context(); dev = iio_context_find_device(ctx, "ad9361-phy"); dev_slave = iio_context_find_device(ctx, "ad9361-phy-hpc"); if (dev_slave) { cf_ad9361_lpc = iio_context_find_device(ctx, "cf-ad9361-lpc"); cf_ad9361_hpc = iio_context_find_device(ctx, "cf-ad9361-hpc"); dev_dds_master = iio_context_find_device(ctx, "cf-ad9361-dds-core-lpc"); dev_dds_slave = iio_context_find_device(ctx, "cf-ad9361-dds-core-hpc"); if (!(cf_ad9361_lpc && cf_ad9361_hpc && dev_dds_master && dev_dds_slave)) dev = NULL; else if (get_dds_channels()) dev = NULL; } if (dev && !iio_device_get_debug_attrs_count(dev)) dev = NULL; if (!dev) iio_context_destroy(ctx); return !!dev; }
static void context_destroy(const char *ini_fn) { g_source_remove_by_user_data(ctx); if (ini_fn) save_profile(ini_fn); if (dac_tx_manager) { dac_data_manager_free(dac_tx_manager); dac_tx_manager = NULL; } iio_context_destroy(ctx); }
void dialogs_init(GtkBuilder *builder) { const char *name = NULL; struct iio_context *ctx; GtkWidget *tmp; dialogs.builder = builder; dialogs.about = GTK_WIDGET(gtk_builder_get_object(builder, "About_dialog")); dialogs.connect = GTK_WIDGET(gtk_builder_get_object(builder, "connect_dialog")); dialogs.connect_fru = GTK_WIDGET(gtk_builder_get_object(builder, "fru_info")); dialogs.serial_num = GTK_WIDGET(gtk_builder_get_object(builder, "serial_number_popup")); dialogs.connect_iio = GTK_WIDGET(gtk_builder_get_object(builder, "connect_iio_devices")); dialogs.ctx_info = GTK_WIDGET(gtk_builder_get_object(builder, "connect_iio_ctx_info")); dialogs.load_save_profile = GTK_WIDGET(gtk_builder_get_object(builder, "load_save_profile")); dialogs.connect_net = GTK_WIDGET(gtk_builder_get_object(builder, "connect_net")); dialogs.net_ip = GTK_WIDGET(gtk_builder_get_object(builder, "connect_net_IP")); dialogs.ok_btn = GTK_WIDGET(gtk_builder_get_object(builder, "button3")); dialogs.latest_version = GTK_WIDGET(gtk_builder_get_object(builder, "latest_version_popup")); dialogs.ver_progress_window = GTK_WIDGET(gtk_builder_get_object(builder, "progress_window")); dialogs.ver_progress_bar = GTK_WIDGET(gtk_builder_get_object(builder, "progressbar")); gtk_builder_connect_signals(builder, &dialogs); /* Bind some dialogs radio buttons to text/labels */ tmp = GTK_WIDGET(gtk_builder_get_object(builder, "connect_net_label")); serial_num = GTK_WIDGET(gtk_builder_get_object(builder, "serial_number")); fru_date = GTK_WIDGET(gtk_builder_get_object(builder, "fru_date")); fru_file_list = GTK_WIDGET(gtk_builder_get_object(builder, "FRU_files")); g_object_bind_property(dialogs.connect_net, "active", tmp, "sensitive", 0); g_object_bind_property(dialogs.connect_net, "active", dialogs.net_ip, "sensitive", 0); /* Grey out the "local context" option if it is not available */ ctx = get_context_from_osc(); if (ctx) name = iio_context_get_name(ctx); if (!name || strcmp(name, "local")) { ctx = iio_create_local_context(); if (ctx) { iio_context_destroy(ctx); } else { GtkWidget *local = GTK_WIDGET(gtk_builder_get_object( builder, "connect_local")); gtk_widget_set_sensitive(local, false); gtk_toggle_button_set_active( GTK_TOGGLE_BUTTON(dialogs.connect_net), true); } } }
/* cleanup and exit */ static void shutdown() { printf("* Destroying buffers\n"); if (rxbuf) { iio_buffer_destroy(rxbuf); } if (txbuf) { iio_buffer_destroy(txbuf); } printf("* Disabling streaming channels\n"); if (rx0_i) { iio_channel_disable(rx0_i); } if (rx0_q) { iio_channel_disable(rx0_q); } if (tx0_i) { iio_channel_disable(tx0_i); } if (tx0_q) { iio_channel_disable(tx0_q); } printf("* Destroying context\n"); if (ctx) { iio_context_destroy(ctx); } exit(0); }
/* cleanup and exit */ void shutdowniio(int s) { sec_end=time(NULL); sec_elapsed=sec_end-sec_begin; printf("\n\nTime elapsed: %lus \tBytes recived %llu \tDebit %llu MB/sec \n ", sec_elapsed, count/1024/1024, count/sec_elapsed/1024/1024); printf("\nSocket sended %lld",sendcount); printf("* Destroying buffers\n"); if (rxbuf) { iio_buffer_destroy(rxbuf); } if (txbuf) { iio_buffer_destroy(txbuf); } printf("* Disabling streaming channels\n"); if (rx0_i) { iio_channel_disable(rx0_i); } if (rx0_q) { iio_channel_disable(rx0_q); } if (tx0_i) { iio_channel_disable(tx0_i); } if (tx0_q) { iio_channel_disable(tx0_q); } printf("* Destroying context\n"); if (ctx) { iio_context_destroy(ctx); } exit(0); }
static bool pr_config_identify(void) { /* Use the OSC's IIO context just to detect the devices */ struct iio_context *osc_ctx = get_context_from_osc(); if (!iio_context_find_device(osc_ctx, PHY_DEVICE) || !iio_context_find_device(osc_ctx, DEVICE_NAME_ADC) || !iio_context_find_device(osc_ctx, DEVICE_NAME_DAC)) return false; ctx = osc_create_context(); phy = iio_context_find_device(ctx, PHY_DEVICE); adc = iio_context_find_device(ctx, DEVICE_NAME_ADC); dac = iio_context_find_device(ctx, DEVICE_NAME_DAC); context_is_local = !strncmp(iio_context_get_name(ctx), "local", strlen("local")); int id; bool init = true; if (!phy || !adc || !dac) { init = false; } else { id = getPrId(); if ((id != PR_LOGIC_DEFAULT_ID) && (id != PR_LOGIC_BIST_ID) && (id != PR_LOGIC_QPSK_ID)) init = false; } if (phy && !iio_device_get_debug_attrs_count(phy)) init = false; if (!init) iio_context_destroy(ctx); return init; }
int main(int argc, char **argv) { char *xml; struct iio_context *ctx; int c, option_index = 0, arg_index = 0, xml_index = 0, ip_index = 0; enum backend backend = LOCAL; 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) { fprintf(stderr, "-x and -n are mutually exclusive\n"); return EXIT_FAILURE; } backend = NETWORK; arg_index += 2; ip_index = arg_index; break; case 'x': if (backend != LOCAL) { fprintf(stderr, "-x and -n are mutually exclusive\n"); return EXIT_FAILURE; } backend = XML; arg_index += 2; xml_index = arg_index; break; case '?': return EXIT_FAILURE; } } if (arg_index >= argc) { fprintf(stderr, "Incorrect number of arguments.\n\n"); usage(); return EXIT_FAILURE; } if (backend == XML) ctx = iio_create_xml_context(argv[xml_index]); else if (backend == NETWORK) ctx = iio_create_network_context(argv[ip_index]); else ctx = iio_create_default_context(); if (!ctx) { fprintf(stderr, "Unable to create IIO context\n"); return EXIT_FAILURE; } xml = strdup(iio_context_get_xml(ctx)); if (!xml) { iio_context_destroy(ctx); return EXIT_FAILURE; } printf("XML generated:\n\n%s\n\n", xml); iio_context_destroy(ctx); ctx = iio_create_xml_context_mem(xml, strlen(xml)); if (!ctx) { fprintf(stderr, "Unable to re-generate context\n"); } else { printf("Context re-creation from generated XML suceeded!\n"); iio_context_destroy(ctx); } free(xml); return EXIT_SUCCESS; }
static bool connect_fillin(Dialogs *data) { GtkTextBuffer *buf; GtkTextIter iter; char text[256]; unsigned int num; size_t i; struct iio_context *ctx; const char *desc; #ifdef FRU_FILES char eprom_names[128]; unsigned char *raw_input_data = NULL; FILE *efp, *fp; struct stat st; /* flushes all open output streams */ fflush(NULL); #if DEBUG fp = popen("find ./ -name \"fru*.bin\"", "r"); #else fp = popen("find /sys -name eeprom 2>/dev/null", "r"); #endif if(fp == NULL) { fprintf(stderr, "can't execute find\n"); return false; } buf = gtk_text_buffer_new(NULL); gtk_text_buffer_get_iter_at_offset(buf, &iter, 0); num = 0; while(fgets(eprom_names, sizeof(eprom_names), fp) != NULL) { num++; /* strip trailing new lines */ if (eprom_names[strlen(eprom_names) - 1] == '\n') eprom_names[strlen(eprom_names) - 1] = '\0'; /* FRU EEPROMS are exactly 256 */ if(stat(eprom_names, &st) !=0) continue; if(st.st_size != 256) { printf("skipping %s (size == %d)\n", eprom_names, (int)st.st_size); continue; } i = 0; if (!is_eeprom_fru(eprom_names, buf, &iter)) { /* Wasn't a FRU file, but is it a blank, writeable EEPROM? */ efp = fopen(eprom_names, "w+"); if (efp) { i = fread(text, 1, 256, efp); if (i == 256) { for (i = 0; i < 256; i++) { if (!(text[i] == 0x00 || ((unsigned char) text[i]) == 0xFF)) { i = 0; break; } } } fclose(efp); /* dump the info into it */ if (i == 256) { if (write_fru(eprom_names)) if(!is_eeprom_fru(eprom_names, buf, &iter)) i = 0; } } else { int errsv = errno; printf("Can't open %s in %s\n%s\n", eprom_names, __func__, strerror(errsv)); } if (i == 0) { sprintf(text, "No FRU information in %s\n", eprom_names); gtk_text_buffer_insert(buf, &iter, text, -1); } } free (raw_input_data); } pclose(fp); if (!num) { sprintf(text, "No eeprom files found in /sys/\n"); gtk_text_buffer_insert(buf, &iter, text, -1); } gtk_text_view_set_buffer(GTK_TEXT_VIEW(data->connect_fru), buf); g_object_unref(buf); #endif ctx = get_context(data); desc = ctx ? iio_context_get_description(ctx) : ""; buf = gtk_text_buffer_new(NULL); gtk_text_buffer_get_iter_at_offset(buf, &iter, 0); gtk_text_buffer_insert(buf, &iter, desc, -1); gtk_text_view_set_buffer(GTK_TEXT_VIEW(data->ctx_info), buf); g_object_unref(buf); buf = gtk_text_buffer_new(NULL); gtk_text_buffer_get_iter_at_offset(buf, &iter, 0); num = ctx ? iio_context_get_devices_count(ctx) : 0; if (num > 0) { for (i = 0; i < num; i++) { struct iio_device *dev = iio_context_get_device(ctx, i); sprintf(text, "%s\n", iio_device_get_name(dev)); gtk_text_buffer_insert(buf, &iter, text, -1); } } else { sprintf(text, "No iio devices found\n"); gtk_text_buffer_insert(buf, &iter, text, -1); } gtk_text_view_set_buffer(GTK_TEXT_VIEW(data->connect_iio), buf); g_object_unref(buf); if (ctx) iio_context_destroy(ctx); return !!ctx; }
static void context_destroy(const char *ini_fn) { save_profile(ini_fn); iio_context_destroy(ctx); }
int main(int argc, char **argv) { unsigned int buffer_size = 1024 * 1024; int c, option_index = 0, arg_index = 0; unsigned int n_tx = 0, n_rx = 0; static struct iio_context *ctx; unsigned int i, nb_channels; struct iio_buffer *buffer; pthread_t monitor_thread; const char *device_name; struct iio_device *dev; char unit; int ret; while ((c = getopt_long(argc, argv, "+hs:", options, &option_index)) != -1) { switch (c) { case 'h': usage(argv); return EXIT_SUCCESS; case 's': arg_index += 2; ret = sscanf(argv[arg_index], "%u%c", &buffer_size, &unit); if (ret == 0) return EXIT_FAILURE; if (ret == 2) { if (unit == 'k') buffer_size *= 1024; else if (unit == 'M') buffer_size *= 1024 * 1024; } break; case '?': return EXIT_FAILURE; } } if (arg_index + 1 >= argc) { fprintf(stderr, "Incorrect number of arguments.\n\n"); usage(argv); return EXIT_FAILURE; } #ifndef _WIN32 set_handler(SIGHUP, &quit_all); #endif set_handler(SIGINT, &quit_all); set_handler(SIGSEGV, &quit_all); set_handler(SIGTERM, &quit_all); ctx = iio_create_default_context(); if (!ctx) { fprintf(stderr, "Unable to create IIO context\n"); return EXIT_FAILURE; } device_name = argv[arg_index + 1]; dev = get_device(ctx, device_name); if (!dev) { iio_context_destroy(ctx); return EXIT_FAILURE; } nb_channels = iio_device_get_channels_count(dev); for (i = 0; i < nb_channels; i++) { struct iio_channel *ch = iio_device_get_channel(dev, i); if (!iio_channel_is_scan_element(ch)) continue; iio_channel_enable(ch); if (iio_channel_is_output(ch)) n_tx++; else n_rx++; } if (n_tx >= n_rx) device_is_tx = true; else device_is_tx = false; printf("Monitoring %s for underflows/overflows\n", iio_device_get_name(dev)); buffer = iio_device_create_buffer(dev, buffer_size, false); if (!buffer) { fprintf(stderr, "Unable to allocate buffer\n"); iio_context_destroy(ctx); return EXIT_FAILURE; } ret = pthread_create(&monitor_thread, NULL, monitor_thread_fn, (void *)device_name); if (ret) { fprintf(stderr, "Failed to create monitor thread: %s\n", strerror(-ret)); } while (app_running) { if (device_is_tx) { ret = iio_buffer_push(buffer); if (ret < 0) { fprintf(stderr, "Unable to push buffer: %s\n", strerror(-ret)); app_running = false; break; } } else { ret = iio_buffer_refill(buffer); if (ret < 0) { fprintf(stderr, "Unable to refill buffer: %s\n", strerror(-ret)); app_running = false; break; } } } iio_buffer_destroy(buffer); iio_context_destroy(ctx); pthread_join(monitor_thread, NULL); return 0; }
static GtkWidget * fmcomms2_init(GtkWidget *notebook, const char *ini_fn) { GtkBuilder *builder; GtkWidget *dds_container; struct iio_channel *ch0, *ch1; can_update_widgets = false; ctx = osc_create_context(); if (!ctx) return NULL; dev = iio_context_find_device(ctx, PHY_DEVICE); dds = iio_context_find_device(ctx, DDS_DEVICE); cap = iio_context_find_device(ctx, CAP_DEVICE); udc_rx = iio_context_find_device(ctx, UDC_RX_DEVICE); udc_tx = iio_context_find_device(ctx, UDC_TX_DEVICE); has_udc_driver = (udc_rx && udc_tx); ch0 = iio_device_find_channel(dev, "voltage0", false); ch1 = iio_device_find_channel(dev, "voltage1", false); dac_tx_manager = dac_data_manager_new(dds, NULL, ctx); if (!dac_tx_manager) { iio_context_destroy(ctx); return NULL; } const char *env_freq_span = getenv("OSC_UPDN_FREQ_SPAN"); const char *env_freq_mix_sign = getenv("OSC_UPDN_FREQ_MIX_SIGN"); if(!env_freq_span) { updn_freq_span = 2; } else { errno = 0; updn_freq_span = g_strtod(env_freq_span, NULL); if (errno) updn_freq_span = 2; } if(!env_freq_mix_sign) { updn_freq_mix_sign = 1; } else { if (!strncmp(env_freq_mix_sign, "-", 1)) updn_freq_mix_sign = -1; else updn_freq_mix_sign = 1; } builder = gtk_builder_new(); nbook = GTK_NOTEBOOK(notebook); if (!gtk_builder_add_from_file(builder, "fmcomms2.glade", NULL)) gtk_builder_add_from_file(builder, OSC_GLADE_FILE_PATH "fmcomms2.glade", NULL); is_2rx_2tx = ch1 && iio_channel_find_attr(ch1, "hardwaregain"); fmcomms2_panel = GTK_WIDGET(gtk_builder_get_object(builder, "fmcomms2_panel")); /* Global settings */ ensm_mode = GTK_WIDGET(gtk_builder_get_object(builder, "ensm_mode")); ensm_mode_available = GTK_WIDGET(gtk_builder_get_object(builder, "ensm_mode_available")); calib_mode = GTK_WIDGET(gtk_builder_get_object(builder, "calib_mode")); calib_mode_available = GTK_WIDGET(gtk_builder_get_object(builder, "calib_mode_available")); trx_rate_governor = GTK_WIDGET(gtk_builder_get_object(builder, "trx_rate_governor")); trx_rate_governor_available = GTK_WIDGET(gtk_builder_get_object(builder, "trx_rate_governor_available")); tx_path_rates = GTK_WIDGET(gtk_builder_get_object(builder, "label_tx_path")); rx_path_rates = GTK_WIDGET(gtk_builder_get_object(builder, "label_rx_path")); filter_fir_config = GTK_WIDGET(gtk_builder_get_object(builder, "filter_fir_config")); enable_fir_filter_rx = GTK_WIDGET(gtk_builder_get_object(builder, "enable_fir_filter_rx")); fir_filter_en_tx = GTK_WIDGET(gtk_builder_get_object(builder, "fir_filter_en_tx")); enable_fir_filter_rx_tx = GTK_WIDGET(gtk_builder_get_object(builder, "enable_fir_filter_tx_rx")); disable_all_fir_filters = GTK_WIDGET(gtk_builder_get_object(builder, "disable_all_fir_filters")); up_down_converter = GTK_WIDGET(gtk_builder_get_object(builder, "checkbox_up_down_converter")); section_toggle[SECTION_GLOBAL] = GTK_TOGGLE_TOOL_BUTTON(gtk_builder_get_object(builder, "global_settings_toggle")); section_setting[SECTION_GLOBAL] = GTK_WIDGET(gtk_builder_get_object(builder, "global_settings")); section_toggle[SECTION_TX] = GTK_TOGGLE_TOOL_BUTTON(gtk_builder_get_object(builder, "tx_toggle")); section_setting[SECTION_TX] = GTK_WIDGET(gtk_builder_get_object(builder, "tx_settings")); section_toggle[SECTION_RX] = GTK_TOGGLE_TOOL_BUTTON(gtk_builder_get_object(builder, "rx_toggle")); section_setting[SECTION_RX] = GTK_WIDGET(gtk_builder_get_object(builder, "rx_settings")); section_toggle[SECTION_FPGA] = GTK_TOGGLE_TOOL_BUTTON(gtk_builder_get_object(builder, "fpga_toggle")); section_setting[SECTION_FPGA] = GTK_WIDGET(gtk_builder_get_object(builder, "fpga_settings")); /* Receive Chain */ rf_port_select_rx = GTK_WIDGET(gtk_builder_get_object(builder, "rf_port_select_rx")); rx_gain_control_rx1 = GTK_WIDGET(gtk_builder_get_object(builder, "gain_control_mode_rx1")); rx_gain_control_rx2 = GTK_WIDGET(gtk_builder_get_object(builder, "gain_control_mode_rx2")); rx_gain_control_modes_rx1 = GTK_WIDGET(gtk_builder_get_object(builder, "gain_control_mode_available_rx1")); rx_gain_control_modes_rx2 = GTK_WIDGET(gtk_builder_get_object(builder, "gain_control_mode_available_rx2")); rx1_rssi = GTK_WIDGET(gtk_builder_get_object(builder, "rssi_rx1")); rx2_rssi = GTK_WIDGET(gtk_builder_get_object(builder, "rssi_rx2")); rx_fastlock_profile = GTK_WIDGET(gtk_builder_get_object(builder, "rx_fastlock_profile")); /* Transmit Chain */ rf_port_select_tx = GTK_WIDGET(gtk_builder_get_object(builder, "rf_port_select_tx")); tx_fastlock_profile = GTK_WIDGET(gtk_builder_get_object(builder, "tx_fastlock_profile")); tx1_rssi = GTK_WIDGET(gtk_builder_get_object(builder, "rssi_tx1")); tx2_rssi = GTK_WIDGET(gtk_builder_get_object(builder, "rssi_tx2")); dds_container = GTK_WIDGET(gtk_builder_get_object(builder, "dds_transmit_block")); gtk_container_add(GTK_CONTAINER(dds_container), dac_data_manager_get_gui_container(dac_tx_manager)); gtk_widget_show_all(dds_container); rx_phase_rotation[0] = GTK_WIDGET(gtk_builder_get_object(builder, "rx1_phase_rotation")); rx_phase_rotation[1] = GTK_WIDGET(gtk_builder_get_object(builder, "rx2_phase_rotation")); gtk_combo_box_set_active(GTK_COMBO_BOX(ensm_mode_available), 0); gtk_combo_box_set_active(GTK_COMBO_BOX(trx_rate_governor_available), 0); gtk_combo_box_set_active(GTK_COMBO_BOX(rx_gain_control_modes_rx1), 0); gtk_combo_box_set_active(GTK_COMBO_BOX(rx_gain_control_modes_rx2), 0); gtk_combo_box_set_active(GTK_COMBO_BOX(rf_port_select_rx), 0); gtk_combo_box_set_active(GTK_COMBO_BOX(rf_port_select_tx), 0); gtk_combo_box_set_active(GTK_COMBO_BOX(rx_fastlock_profile), 0); gtk_combo_box_set_active(GTK_COMBO_BOX(tx_fastlock_profile), 0); /* Set FMCOMMS2/3 max sampling freq -> 61.44MHz and FMCOMMS4 -> 122.88 */ GtkWidget *sfreq = GTK_WIDGET(gtk_builder_get_object(builder, "sampling_freq_tx")); GtkAdjustment *sfreq_adj = gtk_spin_button_get_adjustment(GTK_SPIN_BUTTON(sfreq)); if (is_2rx_2tx) gtk_adjustment_set_upper(sfreq_adj, 61.44); else gtk_adjustment_set_upper(sfreq_adj, 122.88); /* Bind the IIO device files to the GUI widgets */ glb_widgets = widgets; /* Global settings */ iio_combo_box_init(&glb_widgets[num_glb++], dev, NULL, "ensm_mode", "ensm_mode_available", ensm_mode_available, NULL); iio_combo_box_init(&glb_widgets[num_glb++], dev, NULL, "calib_mode", "calib_mode_available", calib_mode_available, NULL); iio_combo_box_init(&glb_widgets[num_glb++], dev, NULL, "trx_rate_governor", "trx_rate_governor_available", trx_rate_governor_available, NULL); dcxo_coarse_num = num_glb; iio_spin_button_int_init_from_builder(&glb_widgets[num_glb++], dev, NULL, "dcxo_tune_coarse", builder, "dcxo_coarse_tune", 0); dcxo_fine_num = num_glb; iio_spin_button_int_init_from_builder(&glb_widgets[num_glb++], dev, NULL, "dcxo_tune_fine", builder, "dcxo_fine_tune", 0); rx_widgets = &glb_widgets[num_glb]; /* Receive Chain */ iio_combo_box_init(&rx_widgets[num_rx++], dev, ch0, "gain_control_mode", "gain_control_mode_available", rx_gain_control_modes_rx1, NULL); iio_combo_box_init(&rx_widgets[num_rx++], dev, ch0, "rf_port_select", "rf_port_select_available", rf_port_select_rx, NULL); if (is_2rx_2tx) iio_combo_box_init(&rx_widgets[num_rx++], dev, ch1, "gain_control_mode", "gain_control_mode_available", rx_gain_control_modes_rx2, NULL); rx1_gain = num_rx; iio_spin_button_int_init_from_builder(&rx_widgets[num_rx++], dev, ch0, "hardwaregain", builder, "hardware_gain_rx1", NULL); if (is_2rx_2tx) { rx2_gain = num_rx; iio_spin_button_int_init_from_builder(&rx_widgets[num_rx++], dev, ch1, "hardwaregain", builder, "hardware_gain_rx2", NULL); } rx_sample_freq = num_rx; iio_spin_button_int_init_from_builder(&rx_widgets[num_rx++], dev, ch0, "sampling_frequency", builder, "sampling_freq_rx", &mhz_scale); iio_spin_button_add_progress(&rx_widgets[num_rx - 1]); iio_spin_button_int_init_from_builder(&rx_widgets[num_rx++], dev, ch0, "rf_bandwidth", builder, "rf_bandwidth_rx", &mhz_scale); iio_spin_button_add_progress(&rx_widgets[num_rx - 1]); rx_lo = num_rx; ch1 = iio_device_find_channel(dev, "altvoltage0", true); if (iio_channel_find_attr(ch1, "frequency")) freq_name = "frequency"; else freq_name = "RX_LO_frequency"; iio_spin_button_s64_init_from_builder(&rx_widgets[num_rx++], dev, ch1, freq_name, builder, "rx_lo_freq", &mhz_scale); iio_spin_button_add_progress(&rx_widgets[num_rx - 1]); iio_toggle_button_init_from_builder(&rx_widgets[num_rx++], dev, ch0, "quadrature_tracking_en", builder, "quad", 0); iio_toggle_button_init_from_builder(&rx_widgets[num_rx++], dev, ch0, "rf_dc_offset_tracking_en", builder, "rfdc", 0); iio_toggle_button_init_from_builder(&rx_widgets[num_rx++], dev, ch0, "bb_dc_offset_tracking_en", builder, "bbdc", 0); iio_spin_button_init_from_builder(&rx_widgets[num_rx], dev, ch1, "calibphase", builder, "rx1_phase_rotation", NULL); iio_spin_button_add_progress(&rx_widgets[num_rx++]); ch0 = iio_device_find_channel(dev, "altvoltage0", true); if (iio_channel_find_attr(ch0, "fastlock_store")) rx_fastlock_store_name = "fastlock_store"; else rx_fastlock_store_name = "RX_LO_fastlock_store"; if (iio_channel_find_attr(ch0, "fastlock_recall")) rx_fastlock_recall_name = "fastlock_recall"; else rx_fastlock_recall_name = "RX_LO_fastlock_recall"; /* Transmit Chain */ tx_widgets = &rx_widgets[num_rx]; ch0 = iio_device_find_channel(dev, "voltage0", true); if (is_2rx_2tx) ch1 = iio_device_find_channel(dev, "voltage1", true); tx_rssi_available = ch0 && iio_channel_find_attr(ch0, "rssi"); if (is_2rx_2tx) tx_rssi_available = tx_rssi_available && (ch1 && iio_channel_find_attr(ch1, "rssi")); iio_combo_box_init(&tx_widgets[num_tx++], dev, ch0, "rf_port_select", "rf_port_select_available", rf_port_select_tx, NULL); iio_spin_button_init_from_builder(&tx_widgets[num_tx++], dev, ch0, "hardwaregain", builder, "hardware_gain_tx1", &inv_scale); if (is_2rx_2tx) iio_spin_button_init_from_builder(&tx_widgets[num_tx++], dev, ch1, "hardwaregain", builder, "hardware_gain_tx2", &inv_scale); tx_sample_freq = num_tx; iio_spin_button_int_init_from_builder(&tx_widgets[num_tx++], dev, ch0, "sampling_frequency", builder, "sampling_freq_tx", &mhz_scale); iio_spin_button_add_progress(&tx_widgets[num_tx - 1]); iio_spin_button_int_init_from_builder(&tx_widgets[num_tx++], dev, ch0, "rf_bandwidth", builder, "rf_bandwidth_tx", &mhz_scale); iio_spin_button_add_progress(&tx_widgets[num_tx - 1]); tx_lo = num_tx; ch1 = iio_device_find_channel(dev, "altvoltage1", true); if (iio_channel_find_attr(ch1, "frequency")) freq_name = "frequency"; else freq_name = "TX_LO_frequency"; iio_spin_button_s64_init_from_builder(&tx_widgets[num_tx++], dev, ch1, freq_name, builder, "tx_lo_freq", &mhz_scale); iio_spin_button_add_progress(&tx_widgets[num_tx - 1]); ch1 = iio_device_find_channel(dev, "altvoltage1", true); if (ini_fn) load_profile(ini_fn); /* Update all widgets with current values */ printf("Updating widgets...\n"); update_widgets(); rx_freq_info_update(); printf("Updating FIR filter...\n"); filter_fir_update(); gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(disable_all_fir_filters), true); glb_settings_update_labels(); rssi_update_labels(); dac_data_manager_freq_widgets_range_update(dac_tx_manager, get_gui_tx_sampling_freq() / 2.0); dac_data_manager_update_iio_widgets(dac_tx_manager); /* Widgets bindings */ g_builder_bind_property(builder, "rssi_tx1", "visible", "label_rssi_tx1", "sensitive", G_BINDING_DEFAULT); g_builder_bind_property(builder, "rssi_tx2", "visible", "label_rssi_tx2", "sensitive", G_BINDING_DEFAULT); /* Connect signals */ if (iio_channel_find_attr(ch1, "fastlock_store")) tx_fastlock_store_name = "fastlock_store"; else tx_fastlock_store_name = "TX_LO_fastlock_store"; if (iio_channel_find_attr(ch1, "fastlock_recall")) tx_fastlock_recall_name = "fastlock_recall"; else tx_fastlock_recall_name = "TX_LO_fastlock_recall"; g_builder_connect_signal(builder, "rx1_phase_rotation", "value-changed", G_CALLBACK(rx_phase_rotation_set), (gpointer *)0); g_builder_connect_signal(builder, "rx2_phase_rotation", "value-changed", G_CALLBACK(rx_phase_rotation_set), (gpointer *)2); g_builder_connect_signal(builder, "sampling_freq_tx", "value-changed", G_CALLBACK(tx_sample_rate_changed), NULL); g_builder_connect_signal(builder, "fmcomms2_settings_reload", "clicked", G_CALLBACK(reload_button_clicked), NULL); g_builder_connect_signal(builder, "filter_fir_config", "file-set", G_CALLBACK(filter_fir_config_file_set_cb), NULL); g_builder_connect_signal(builder, "rx_fastlock_store", "clicked", G_CALLBACK(fastlock_clicked), (gpointer) 1); g_builder_connect_signal(builder, "tx_fastlock_store", "clicked", G_CALLBACK(fastlock_clicked), (gpointer) 2); g_builder_connect_signal(builder, "rx_fastlock_recall", "clicked", G_CALLBACK(fastlock_clicked), (gpointer) 3); g_builder_connect_signal(builder, "tx_fastlock_recall", "clicked", G_CALLBACK(fastlock_clicked), (gpointer) 4); g_signal_connect_after(section_toggle[SECTION_GLOBAL], "clicked", G_CALLBACK(hide_section_cb), section_setting[SECTION_GLOBAL]); g_signal_connect_after(section_toggle[SECTION_TX], "clicked", G_CALLBACK(hide_section_cb), section_setting[SECTION_TX]); g_signal_connect_after(section_toggle[SECTION_RX], "clicked", G_CALLBACK(hide_section_cb), section_setting[SECTION_RX]); g_signal_connect_after(section_toggle[SECTION_FPGA], "clicked", G_CALLBACK(hide_section_cb), section_setting[SECTION_FPGA]); g_signal_connect_after(ensm_mode_available, "changed", G_CALLBACK(glb_settings_update_labels), NULL); g_signal_connect_after(calib_mode_available, "changed", G_CALLBACK(glb_settings_update_labels), NULL); g_signal_connect_after(trx_rate_governor_available, "changed", G_CALLBACK(glb_settings_update_labels), NULL); g_signal_connect_after(rx_gain_control_modes_rx1, "changed", G_CALLBACK(glb_settings_update_labels), NULL); g_signal_connect_after(rx_gain_control_modes_rx2, "changed", G_CALLBACK(glb_settings_update_labels), NULL); if (tx_rssi_available) g_signal_connect(rf_port_select_rx, "changed", G_CALLBACK(rf_port_select_rx_changed_cb), NULL); g_signal_connect_after(enable_fir_filter_rx, "toggled", G_CALLBACK(filter_fir_enable), NULL); g_signal_connect_after(fir_filter_en_tx, "toggled", G_CALLBACK(filter_fir_enable), NULL); g_signal_connect_after(enable_fir_filter_rx_tx, "toggled", G_CALLBACK(filter_fir_enable), NULL); g_signal_connect_after(disable_all_fir_filters, "toggled", G_CALLBACK(filter_fir_enable), NULL); g_signal_connect(up_down_converter, "toggled", G_CALLBACK(up_down_converter_toggled_cb), NULL); make_widget_update_signal_based(glb_widgets, num_glb); make_widget_update_signal_based(rx_widgets, num_rx); make_widget_update_signal_based(tx_widgets, num_tx); iio_spin_button_set_on_complete_function(&rx_widgets[rx_sample_freq], sample_frequency_changed_cb, NULL); iio_spin_button_set_on_complete_function(&tx_widgets[tx_sample_freq], sample_frequency_changed_cb, NULL); iio_spin_button_set_on_complete_function(&rx_widgets[rx_lo], sample_frequency_changed_cb, NULL); iio_spin_button_set_on_complete_function(&tx_widgets[tx_lo], sample_frequency_changed_cb, NULL); add_ch_setup_check_fct("cf-ad9361-lpc", channel_combination_check); struct iio_device *adc_dev; struct extra_dev_info *adc_info; adc_dev = iio_context_find_device(get_context_from_osc(), CAP_DEVICE); if (adc_dev) { adc_info = iio_device_get_data(adc_dev); if (adc_info) adc_info->plugin_fft_corr = 20 * log10(1/sqrt(HANNING_ENBW)); } block_diagram_init(builder, 2, "fmcomms2.svg", "AD_FMCOMM2S2_RevC.jpg"); gtk_file_chooser_set_current_folder (GTK_FILE_CHOOSER(filter_fir_config), OSC_FILTER_FILE_PATH); dac_data_manager_set_buffer_chooser_current_folder(dac_tx_manager, OSC_WAVEFORM_FILE_PATH); if (!is_2rx_2tx) { gtk_widget_hide(GTK_WIDGET(gtk_builder_get_object(builder, "frame_rx2"))); gtk_widget_hide(GTK_WIDGET(gtk_builder_get_object(builder, "frame_fpga_rx2"))); gtk_widget_hide(GTK_WIDGET(gtk_builder_get_object(builder, "table_hw_gain_tx2"))); } if (!tx_rssi_available) { gtk_widget_hide(GTK_WIDGET(gtk_builder_get_object(builder, "rssi_tx1"))); gtk_widget_hide(GTK_WIDGET(gtk_builder_get_object(builder, "rssi_tx2"))); gtk_widget_hide(GTK_WIDGET(gtk_builder_get_object(builder, "label_rssi_tx1"))); gtk_widget_hide(GTK_WIDGET(gtk_builder_get_object(builder, "label_rssi_tx2"))); } gtk_widget_set_visible(up_down_converter, has_udc_driver); g_timeout_add(1000, (GSourceFunc) update_display, ctx); can_update_widgets = true; return fmcomms2_panel; }
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; }
int main(int argc, char **argv) { unsigned int i, nb_channels; unsigned int buffer_size = SAMPLES_PER_READ; const char *arg_uri = NULL; const char *arg_ip = NULL; int c, option_index = 0; struct iio_device *dev; size_t sample_size; int timeout = -1; bool scan_for_context = false; while ((c = getopt_long(argc, argv, "+hn:u:t:b:s:T:a", options, &option_index)) != -1) { switch (c) { case 'h': usage(); return EXIT_SUCCESS; case 'n': arg_ip = optarg; break; case 'u': arg_uri = optarg; break; case 'a': scan_for_context = true; break; case 't': trigger_name = optarg; break; case 'b': buffer_size = atoi(optarg); break; case 's': num_samples = atoi(optarg); break; case 'T': timeout = atoi(optarg); break; case '?': return EXIT_FAILURE; } } if (argc == optind) { fprintf(stderr, "Incorrect number of arguments.\n\n"); usage(); return EXIT_FAILURE; } setup_sig_handler(); if (scan_for_context) ctx = scan(); else if (arg_uri) ctx = iio_create_context_from_uri(arg_uri); else if (arg_ip) ctx = iio_create_network_context(arg_ip); else ctx = iio_create_default_context(); if (!ctx) { fprintf(stderr, "Unable to create IIO context\n"); return EXIT_FAILURE; } if (timeout >= 0) iio_context_set_timeout(ctx, timeout); dev = iio_context_find_device(ctx, argv[optind]); if (!dev) { fprintf(stderr, "Device %s not found\n", argv[optind]); iio_context_destroy(ctx); return EXIT_FAILURE; } if (trigger_name) { struct iio_device *trigger = iio_context_find_device( ctx, trigger_name); if (!trigger) { fprintf(stderr, "Trigger %s not found\n", trigger_name); iio_context_destroy(ctx); return EXIT_FAILURE; } if (!iio_device_is_trigger(trigger)) { fprintf(stderr, "Specified device is not a trigger\n"); iio_context_destroy(ctx); return EXIT_FAILURE; } /* * Fixed rate for now. Try new ABI first, * fail gracefully to remain compatible. */ if (iio_device_attr_write_longlong(trigger, "sampling_frequency", DEFAULT_FREQ_HZ) < 0) iio_device_attr_write_longlong(trigger, "frequency", DEFAULT_FREQ_HZ); iio_device_set_trigger(dev, trigger); } nb_channels = iio_device_get_channels_count(dev); if (argc == optind + 1) { /* Enable all channels */ for (i = 0; i < nb_channels; i++) iio_channel_enable(iio_device_get_channel(dev, i)); } else { for (i = 0; i < nb_channels; i++) { unsigned int j; struct iio_channel *ch = iio_device_get_channel(dev, i); for (j = optind + 1; j < (unsigned int) argc; j++) { const char *n = iio_channel_get_name(ch); if (!strcmp(argv[j], iio_channel_get_id(ch)) || (n && !strcmp(n, argv[j]))) iio_channel_enable(ch); } } } sample_size = iio_device_get_sample_size(dev); buffer = iio_device_create_buffer(dev, buffer_size, false); if (!buffer) { char buf[256]; iio_strerror(errno, buf, sizeof(buf)); fprintf(stderr, "Unable to allocate buffer: %s\n", buf); iio_context_destroy(ctx); return EXIT_FAILURE; } while (app_running) { int ret = iio_buffer_refill(buffer); if (ret < 0) { if (app_running) { char buf[256]; iio_strerror(-ret, buf, sizeof(buf)); fprintf(stderr, "Unable to refill buffer: %s\n", buf); } break; } /* If there are only the samples we requested, we don't need to * demux */ if (iio_buffer_step(buffer) == sample_size) { void *start = iio_buffer_start(buffer); size_t read_len, len = (intptr_t) iio_buffer_end(buffer) - (intptr_t) start; if (num_samples && len > num_samples * sample_size) len = num_samples * sample_size; for (read_len = len; len; ) { size_t nb = fwrite(start, 1, len, stdout); if (!nb) goto err_destroy_buffer; len -= nb; start = (void *)((intptr_t) start + nb); } if (num_samples) { num_samples -= read_len / sample_size; if (!num_samples) quit_all(EXIT_SUCCESS); } } else { iio_buffer_foreach_sample(buffer, print_sample, NULL); } } err_destroy_buffer: iio_buffer_destroy(buffer); iio_context_destroy(ctx); return exit_code; }
int main(int argc, char **argv) { unsigned int i, nb_channels; unsigned int buffer_size = SAMPLES_PER_READ; int c, option_index = 0, arg_index = 0, ip_index = 0; struct iio_device *dev; size_t sample_size; while ((c = getopt_long(argc, argv, "+hn:t:b:s:", options, &option_index)) != -1) { switch (c) { case 'h': usage(); return EXIT_SUCCESS; case 'n': arg_index += 2; ip_index = arg_index; break; case 't': arg_index += 2; trigger_name = argv[arg_index]; break; case 'b': arg_index += 2; buffer_size = atoi(argv[arg_index]); break; case 's': arg_index += 2; num_samples = atoi(argv[arg_index]); break; case '?': return EXIT_FAILURE; } } if (arg_index + 1 >= argc) { fprintf(stderr, "Incorrect number of arguments.\n\n"); usage(); return EXIT_FAILURE; } if (ip_index) ctx = iio_create_network_context(argv[ip_index]); else ctx = iio_create_default_context(); if (!ctx) { fprintf(stderr, "Unable to create IIO context\n"); return EXIT_FAILURE; } #ifndef _WIN32 set_handler(SIGHUP, &quit_all); #endif set_handler(SIGINT, &quit_all); set_handler(SIGSEGV, &quit_all); set_handler(SIGTERM, &quit_all); dev = get_device(ctx, argv[arg_index + 1]); if (!dev) { iio_context_destroy(ctx); return EXIT_FAILURE; } if (trigger_name) { struct iio_device *trigger = get_device(ctx, trigger_name); if (!trigger) { iio_context_destroy(ctx); return EXIT_FAILURE; } if (!iio_device_is_trigger(trigger)) { fprintf(stderr, "Specified device is not a trigger\n"); iio_context_destroy(ctx); return EXIT_FAILURE; } /* Fixed rate for now */ iio_device_attr_write_longlong(trigger, "frequency", 100); iio_device_set_trigger(dev, trigger); } nb_channels = iio_device_get_channels_count(dev); if (argc == arg_index + 2) { /* Enable all channels */ for (i = 0; i < nb_channels; i++) iio_channel_enable(iio_device_get_channel(dev, i)); } else { for (i = 0; i < nb_channels; i++) { unsigned int j; struct iio_channel *ch = iio_device_get_channel(dev, i); for (j = arg_index + 2; j < argc; j++) { const char *n = iio_channel_get_name(ch); if (!strcmp(argv[j], iio_channel_get_id(ch)) || (n && !strcmp(n, argv[j]))) iio_channel_enable(ch); } } } sample_size = iio_device_get_sample_size(dev); buffer = iio_device_create_buffer(dev, buffer_size, false); if (!buffer) { fprintf(stderr, "Unable to allocate buffer\n"); iio_context_destroy(ctx); return EXIT_FAILURE; } while (app_running) { int ret = iio_buffer_refill(buffer); if (ret < 0) { fprintf(stderr, "Unable to refill buffer: %s\n", strerror(-ret)); break; } /* If there are only the samples we requested, we don't need to * demux */ if (iio_buffer_step(buffer) == sample_size) { void *start = iio_buffer_start(buffer); ptrdiff_t len = (intptr_t) iio_buffer_end(buffer) - (intptr_t) start; size_t read_len; if (num_samples && len > num_samples * sample_size) len = num_samples * sample_size; for (read_len = len; len; ) { ssize_t nb = fwrite(start, 1, len, stdout); if (nb < 0) { fprintf(stderr, "Unable to write data!\n"); goto err_destroy_buffer; } len -= nb; start = (void *)((intptr_t) start + nb); } if (num_samples) { num_samples -= read_len / sample_size; if (!num_samples) quit_all(EXIT_SUCCESS); } } else { iio_buffer_foreach_sample(buffer, print_sample, NULL); } } err_destroy_buffer: iio_buffer_destroy(buffer); iio_context_destroy(ctx); return exit_code; }
static void context_destroy(const char *ini_fn) { g_source_remove_by_user_data(ctx); iio_context_destroy(ctx); }
struct iio_context * network_create_context(const char *host) { struct addrinfo hints, *res; struct iio_context *ctx; struct iio_context_pdata *pdata; size_t i, len; int fd, ret; char *description; #ifdef _WIN32 WSADATA wsaData; ret = WSAStartup(MAKEWORD(2, 0), &wsaData); if (ret < 0) { ERROR("WSAStartup failed with error %i\n", ret); errno = -ret; return NULL; } #endif memset(&hints, 0, sizeof(hints)); hints.ai_family = AF_UNSPEC; hints.ai_socktype = SOCK_STREAM; #ifdef HAVE_AVAHI if (!host) { char addr_str[AVAHI_ADDRESS_STR_MAX]; char port_str[6]; AvahiAddress address; uint16_t port = IIOD_PORT; memset(&address, 0, sizeof(address)); ret = discover_host(&address, &port); if (ret < 0) { DEBUG("Unable to find host: %s\n", strerror(-ret)); errno = -ret; return NULL; } avahi_address_snprint(addr_str, sizeof(addr_str), &address); snprintf(port_str, sizeof(port_str), "%hu", port); ret = getaddrinfo(addr_str, port_str, &hints, &res); } else #endif { ret = getaddrinfo(host, IIOD_PORT_STR, &hints, &res); } if (ret) { ERROR("Unable to find host: %s\n", gai_strerror(ret)); #ifndef _WIN32 if (ret != EAI_SYSTEM) errno = ret; #endif return NULL; } fd = create_socket(res); if (fd < 0) { errno = fd; goto err_free_addrinfo; } pdata = calloc(1, sizeof(*pdata)); if (!pdata) { errno = ENOMEM; goto err_close_socket; } pdata->fd = fd; pdata->addrinfo = res; pdata->lock = iio_mutex_create(); if (!pdata->lock) { errno = ENOMEM; goto err_free_pdata; } pdata->iiod_client = iiod_client_new(pdata, pdata->lock, &network_iiod_client_ops); if (!pdata->iiod_client) goto err_destroy_mutex; DEBUG("Creating context...\n"); ctx = iiod_client_create_context(pdata->iiod_client, fd); if (!ctx) goto err_destroy_iiod_client; /* Override the name and low-level functions of the XML context * with those corresponding to the network context */ ctx->name = "network"; ctx->ops = &network_ops; ctx->pdata = pdata; #ifdef HAVE_IPV6 len = INET6_ADDRSTRLEN + IF_NAMESIZE + 2; #else len = INET_ADDRSTRLEN + 1; #endif description = malloc(len); if (!description) { ret = -ENOMEM; goto err_network_shutdown; } description[0] = '\0'; #ifdef HAVE_IPV6 if (res->ai_family == AF_INET6) { struct sockaddr_in6 *in = (struct sockaddr_in6 *) res->ai_addr; char *ptr; inet_ntop(AF_INET6, &in->sin6_addr, description, INET6_ADDRSTRLEN); ptr = if_indextoname(in->sin6_scope_id, description + strlen(description) + 1); if (!ptr) { ret = -errno; ERROR("Unable to lookup interface of IPv6 address\n"); goto err_free_description; } *(ptr - 1) = '%'; } #endif if (res->ai_family == AF_INET) { struct sockaddr_in *in = (struct sockaddr_in *) res->ai_addr; #if (!_WIN32 || _WIN32_WINNT >= 0x600) inet_ntop(AF_INET, &in->sin_addr, description, INET_ADDRSTRLEN); #else char *tmp = inet_ntoa(in->sin_addr); strncpy(description, tmp, len); #endif } for (i = 0; i < ctx->nb_devices; i++) { struct iio_device *dev = ctx->devices[i]; dev->pdata = calloc(1, sizeof(*dev->pdata)); if (!dev->pdata) { ret = -ENOMEM; goto err_free_description; } dev->pdata->fd = -1; #ifdef WITH_NETWORK_GET_BUFFER dev->pdata->memfd = -1; #endif dev->pdata->lock = iio_mutex_create(); if (!dev->pdata->lock) { ret = -ENOMEM; goto err_free_description; } } ret = iio_context_init(ctx); if (ret < 0) goto err_free_description; if (ctx->description) { size_t desc_len = strlen(description); size_t new_size = desc_len + strlen(ctx->description) + 2; char *ptr, *new_description = realloc(description, new_size); if (!new_description) { ret = -ENOMEM; goto err_free_description; } ptr = strrchr(new_description, '\0'); snprintf(ptr, new_size - desc_len, " %s", ctx->description); free(ctx->description); ctx->description = new_description; } else { ctx->description = description; } iiod_client_set_timeout(pdata->iiod_client, fd, calculate_remote_timeout(DEFAULT_TIMEOUT_MS)); return ctx; err_free_description: free(description); err_network_shutdown: iio_context_destroy(ctx); errno = -ret; return NULL; err_destroy_iiod_client: iiod_client_destroy(pdata->iiod_client); err_destroy_mutex: iio_mutex_destroy(pdata->lock); err_free_pdata: free(pdata); err_close_socket: close(fd); err_free_addrinfo: freeaddrinfo(res); return NULL; }
static void context_destroy(void) { iio_context_destroy(ctx); }