struct iio_context * iio_create_context_from_uri(const char *uri) { #ifdef WITH_LOCAL_BACKEND if (strcmp(uri, "local:") == 0) /* No address part */ return iio_create_local_context(); #endif #ifdef WITH_XML_BACKEND if (strncmp(uri, "xml:", sizeof("xml:") - 1) == 0) return iio_create_xml_context(uri + sizeof("xml:") - 1); #endif #ifdef WITH_NETWORK_BACKEND if (strncmp(uri, "ip:", sizeof("ip:") - 1) == 0) return iio_create_network_context(uri+3); #endif #ifdef WITH_USB_BACKEND if (strncmp(uri, "usb:", sizeof("usb:") - 1) == 0) return usb_create_context_from_uri(uri); #endif #ifdef WITH_SERIAL_BACKEND if (strncmp(uri, "serial:", sizeof("serial:") - 1) == 0) return serial_create_context_from_uri(uri); #endif errno = ENOSYS; return NULL; }
static struct iio_context * network_clone(const struct iio_context *ctx) { if (ctx->description) { char *ptr = strchr(ctx->description, ' '); if (ptr) { #ifdef HAVE_IPV6 char buf[INET6_ADDRSTRLEN + IF_NAMESIZE + 2]; #else char buf[INET_ADDRSTRLEN + 1]; #endif strncpy(buf, ctx->description, sizeof(buf) - 1); buf[ptr - ctx->description] = '\0'; return iio_create_network_context(buf); } } return iio_create_network_context(ctx->description); }
static struct iio_context * get_context(Dialogs *data) { if (gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(dialogs.connect_net))) { const char *hostname = gtk_entry_get_text(GTK_ENTRY(dialogs.net_ip)); if (!hostname[0]) hostname = NULL; return iio_create_network_context(hostname); } else { return iio_create_local_context(); } }
struct iio_context * iio_create_default_context(void) { #if NETWORK_BACKEND char *hostname = getenv("IIOD_REMOTE"); if (hostname) { /* If the environment variable is an empty string, we will * discover the server using ZeroConf */ if (strlen(hostname) == 0) hostname = NULL; return iio_create_network_context(hostname); } #endif return iio_create_local_context(); }
struct iio_context * iio_create_default_context(void) { char *hostname = getenv("IIOD_REMOTE"); if (hostname) { struct iio_context *ctx; ctx = iio_create_context_from_uri(hostname); if (ctx) return ctx; #ifdef WITH_NETWORK_BACKEND /* If the environment variable is an empty string, we will * discover the server using ZeroConf */ if (strlen(hostname) == 0) hostname = NULL; return iio_create_network_context(hostname); #endif } return iio_create_local_context(); }
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; }
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; }
gint main (int argc, char **argv) { int c; char *profile = NULL; init_signal_handlers(argv[0]); opterr = 0; while ((c = getopt (argc, argv, "c:p:u:")) != -1) switch (c) { case 'c': ctx = iio_create_network_context(optarg); if (!ctx) { printf("Failed connecting to remote device: %s\n", optarg); exit(-1); } break; case 'u': ctx = iio_create_context_from_uri(optarg); if (!ctx) { printf("Failed connecting to remote device: %s\n", optarg); exit(-1); } break; case 'p': profile = strdup(optarg); break; case '?': usage(argv[0]); break; default: printf("Unknown command line option\n"); usage(argv[0]); break; } #ifndef __MINGW32__ /* XXX: Enabling threading when compiling for Windows will lock the UI * as soon as the main window is moved. */ gdk_threads_init(); #endif gtk_init(&argc, &argv); signal(SIGTERM, sigterm); signal(SIGINT, sigterm); #ifndef __MINGW32__ signal(SIGHUP, sigterm); #endif gdk_threads_enter(); init_application(); c = load_default_profile(profile, true); if (!ctx_destroyed_by_do_quit) { if (!ctx) connect_dialog(false); create_default_plot(); if (c == 0) { if (gtk_check_menu_item_get_active( GTK_CHECK_MENU_ITEM(versioncheck_en))) version_check_start(NULL); gtk_main(); } else application_quit(); } gdk_threads_leave(); if (profile) free(profile); if (c == 0 || c == -ENOTTY) return 0; else return -1; }
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; }