Beispiel #1
0
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;
}