/** * Find USB serial devices via the USB vendor ID and product ID. * * @param[in] vendor_id Vendor ID of the USB device. * @param[in] product_id Product ID of the USB device. * * @return A GSList of strings containing the path of the serial device or * NULL if no serial device is found. The returned list must be freed * by the caller. */ SR_PRIV GSList *sr_serial_find_usb(uint16_t vendor_id, uint16_t product_id) { GSList *tty_devs = NULL; struct sp_port **ports; int i, vid, pid; if (sp_list_ports(&ports) != SP_OK) return NULL; for (i=0; ports[i]; i++) if (sp_get_port_transport(ports[i]) == SP_TRANSPORT_USB && sp_get_port_usb_vid_pid(ports[i], &vid, &pid) == SP_OK && vid == vendor_id && pid == product_id) tty_devs = g_slist_prepend(tty_devs, g_strdup(sp_get_port_name(ports[i]))); sp_free_port_list(ports); return tty_devs; }
int navilink_get_serial_list(NavilinkSerialPort*** list) { struct sp_port** sp_list = NULL; sp_list_ports(&sp_list); size_t realloc_size = 5; NavilinkSerialPort** local_list = (NavilinkSerialPort**)malloc(realloc_size * sizeof(NavilinkSerialPort*)); NavilinkSerialPort* port = NULL; int i = 0; struct sp_port* iter = sp_list[i]; while (iter) { if (i > realloc_size - 1) { realloc_size += realloc_size; list = realloc(list, realloc_size); } port = (NavilinkSerialPort*)malloc(sizeof(NavilinkSerialPort)); int result = sp_copy_port(iter, &port->serial_port); if (result < 0) { goto last_error; } strcpy(&port->name[0], sp_get_port_name(iter)); local_list[i] = port; iter = sp_list[++i]; } sp_free_port_list(sp_list); if (i > realloc_size - 1) { realloc_size += 1; list = realloc(list, realloc_size); } local_list[i] = NULL; *list = local_list; last_error: return -1; }
bool FlotillaDock::set_port(sp_port *new_port){ if (state != Disconnected) return false; state = Connecting; if (sp_copy_port(new_port, &port) == SP_OK){ const char* port_name = sp_get_port_name(port); sp_set_baudrate(port, BAUD_RATE); if (sp_open(port, SP_MODE_READ_WRITE) == SP_OK){ if (get_version_info()){ state = Connected; return true; } else { std::ostringstream msg; msg << GetTimestamp() << "Warning: Failed to get dock version information" << std::endl; std::cout << msg.str(); } } else { std::ostringstream msg; msg << GetTimestamp() << "Warning: Failed to open port " << port_name << std::endl; std::cout << msg.str(); } } else{ std::ostringstream msg; msg << GetTimestamp() << "Warning: Failed to copy port!?" << std::endl; std::cout << msg.str(); } state = Disconnected; return false; }
/** * List available serial devices. * * @return A GSList of strings containing the path of the serial devices or * NULL if no serial device is found. The returned list must be freed * by the caller. */ SR_API GSList *sr_serial_list(const struct sr_dev_driver *driver) { GSList *tty_devs = NULL; struct sp_port **ports; struct sr_serial_port *port; int i; /* Currently unused, but will be used by some drivers later on. */ (void)driver; if (sp_list_ports(&ports) != SP_OK) return NULL; for (i = 0; ports[i]; i++) { port = sr_serial_new(sp_get_port_name(ports[i]), sp_get_port_description(ports[i])); tty_devs = g_slist_append(tty_devs, port); } sp_free_port_list(ports); return tty_devs; }