static struct sr_dev_inst *sr_scpi_scan_resource(struct drv_context *drvc, const char *resource, const char *serialcomm, struct sr_dev_inst *(*probe_device)(struct sr_scpi_dev_inst *scpi)) { struct sr_scpi_dev_inst *scpi; struct sr_dev_inst *sdi; if (!(scpi = scpi_dev_inst_new(drvc, resource, serialcomm))) return NULL; if (sr_scpi_open(scpi) != SR_OK) { sr_info("Couldn't open SCPI device."); sr_scpi_free(scpi); return NULL; }; sdi = probe_device(scpi); sr_scpi_close(scpi); if (sdi) sdi->status = SR_ST_INACTIVE; else sr_scpi_free(scpi); return sdi; }
static int discovery_usb_scan(struct jaylink_context *ctx) { ssize_t ret; struct libusb_device **devs; struct jaylink_device *dev; size_t num; size_t i; ret = libusb_get_device_list(ctx->usb_ctx, &devs); if (ret < 0) { log_err(ctx, "Failed to retrieve device list: %s.", libusb_error_name(ret)); return JAYLINK_ERR; } num = 0; for (i = 0; devs[i]; i++) { dev = probe_device(ctx, devs[i]); if (!dev) continue; ctx->discovered_devs = list_prepend(ctx->discovered_devs, dev); num++; } libusb_free_device_list(devs, true); log_dbg(ctx, "Found %zu USB device(s).", num); return JAYLINK_OK; }
static struct sr_dev_inst *sr_modbus_scan_resource(const char *resource, const char *serialcomm, int modbusaddr, struct sr_dev_inst *(*probe_device)(struct sr_modbus_dev_inst *modbus)) { struct sr_modbus_dev_inst *modbus; struct sr_dev_inst *sdi; if (!(modbus = modbus_dev_inst_new(resource, serialcomm, modbusaddr))) return NULL; if (sr_modbus_open(modbus) != SR_OK) { sr_info("Couldn't open Modbus device."); sr_modbus_free(modbus); return NULL; }; if ((sdi = probe_device(modbus))) return sdi; sr_modbus_close(modbus); sr_modbus_free(modbus); return NULL; }
static void scan_devices(int ipv4, /* I - SNMP IPv4 socket */ int ipv6) /* I - SNMP IPv6 socket */ { int fd, /* File descriptor for this address */ busy; /* Are we busy processing something? */ char *address, /* Current address */ *community; /* Current community */ fd_set input; /* Input set for select() */ struct timeval timeout; /* Timeout for select() */ time_t endtime; /* End time for scan */ http_addrlist_t *addrs, /* List of addresses */ *addr; /* Current address */ snmp_cache_t *device; /* Current device */ char temp[1024]; /* Temporary address string */ gettimeofday(&StartTime, NULL); /* * First send all of the broadcast queries... */ for (address = (char *)cupsArrayFirst(Addresses); address; address = (char *)cupsArrayNext(Addresses)) { if (!strcmp(address, "@LOCAL")) addrs = get_interface_addresses(NULL); else if (!strncmp(address, "@IF(", 4)) { char ifname[255]; /* Interface name */ strlcpy(ifname, address + 4, sizeof(ifname)); if (ifname[0]) ifname[strlen(ifname) - 1] = '\0'; addrs = get_interface_addresses(ifname); } else addrs = httpAddrGetList(address, AF_UNSPEC, NULL); if (!addrs) { fprintf(stderr, "ERROR: Unable to scan \"%s\"!\n", address); continue; } for (community = (char *)cupsArrayFirst(Communities); community; community = (char *)cupsArrayNext(Communities)) { debug_printf("DEBUG: Scanning for devices in \"%s\" via \"%s\"...\n", community, address); for (addr = addrs; addr; addr = addr->next) { #ifdef AF_INET6 if (httpAddrFamily(&(addr->addr)) == AF_INET6) fd = ipv6; else #endif /* AF_INET6 */ fd = ipv4; debug_printf("DEBUG: Sending get request to %s...\n", httpAddrString(&(addr->addr), temp, sizeof(temp))); _cupsSNMPWrite(fd, &(addr->addr), CUPS_SNMP_VERSION_1, community, CUPS_ASN1_GET_REQUEST, DEVICE_TYPE, DeviceTypeOID); } } httpAddrFreeList(addrs); } /* * Then read any responses that come in over the next 3 seconds... */ endtime = time(NULL) + MaxRunTime; FD_ZERO(&input); while (time(NULL) < endtime) { timeout.tv_sec = 2; timeout.tv_usec = 0; FD_SET(ipv4, &input); if (ipv6 >= 0) FD_SET(ipv6, &input); fd = ipv4 > ipv6 ? ipv4 : ipv6; if (select(fd + 1, &input, NULL, NULL, &timeout) < 0) { fprintf(stderr, "ERROR: %.3f select() for %d/%d failed: %s\n", run_time(), ipv4, ipv6, strerror(errno)); break; } busy = 0; if (FD_ISSET(ipv4, &input)) { read_snmp_response(ipv4); busy = 1; } if (ipv6 >= 0 && FD_ISSET(ipv6, &input)) { read_snmp_response(ipv6); busy = 1; } if (!busy) { /* * List devices with complete information... */ int sent_something = 0; for (device = (snmp_cache_t *)cupsArrayFirst(Devices); device; device = (snmp_cache_t *)cupsArrayNext(Devices)) if (!device->sent && device->info && device->make_and_model) { if (device->uri) list_device(device); else probe_device(device); device->sent = sent_something = 1; } if (!sent_something) break; } } debug_printf("DEBUG: %.3f Scan complete!\n", run_time()); }