Exemplo n.º 1
0
static Port *
OpenPortInternal(const DeviceConfig &config, PortListener *listener,
                 DataHandler &handler)
{
  const TCHAR *path = nullptr;
  TCHAR buffer[MAX_PATH];

  switch (config.port_type) {
  case DeviceConfig::PortType::DISABLED:
    return nullptr;

  case DeviceConfig::PortType::SERIAL:
    if (config.path.empty())
      return nullptr;

    path = config.path.c_str();
    break;

  case DeviceConfig::PortType::RFCOMM:
#ifdef ANDROID
    if (config.bluetooth_mac.empty()) {
      LogFormat("No Bluetooth MAC configured");
      return nullptr;
    }

    return OpenAndroidBluetoothPort(config.bluetooth_mac, listener, handler);
#else
    LogFormat("Bluetooth not available on this platform");
    return nullptr;
#endif

  case DeviceConfig::PortType::RFCOMM_SERVER:
#ifdef ANDROID
    return OpenAndroidBluetoothServerPort(listener, handler);
#else
    LogFormat("Bluetooth not available on this platform");
    return nullptr;
#endif

  case DeviceConfig::PortType::IOIOUART:
#if defined(ANDROID)
    if (config.ioio_uart_id >= AndroidIOIOUartPort::getNumberUarts()) {
      LogFormat("No IOIOUart configured in profile");
      return nullptr;
    }

    return OpenAndroidIOIOUartPort(config.ioio_uart_id, config.baud_rate,
                                   listener, handler);
#else
    LogFormat("IOIO Uart not available on this platform or version");
    return nullptr;
#endif

  case DeviceConfig::PortType::AUTO:
    if (!DetectGPS(buffer, sizeof(buffer))) {
      LogFormat("no GPS detected");
      return nullptr;
    }

    LogFormat(_T("GPS detected: %s"), buffer);

    path = buffer;
    break;

  case DeviceConfig::PortType::INTERNAL:
  case DeviceConfig::PortType::DROIDSOAR_V2:
  case DeviceConfig::PortType::NUNCHUCK:
  case DeviceConfig::PortType::I2CPRESSURESENSOR:
  case DeviceConfig::PortType::IOIOVOLTAGE:
    break;

  case DeviceConfig::PortType::TCP_CLIENT: {
    const WideToUTF8Converter ip_address(config.ip_address);
    if (!ip_address.IsValid())
      return nullptr;

    auto port = new TCPClientPort(listener, handler);
    if (!port->Connect(ip_address, config.tcp_port)) {
      delete port;
      return nullptr;
    }

    return port;
  }

  case DeviceConfig::PortType::TCP_LISTENER: {
    TCPPort *port = new TCPPort(listener, handler);
    if (!port->Open(config.tcp_port)) {
      delete port;
      return nullptr;
    }

    return port;
  }

  case DeviceConfig::PortType::UDP_LISTENER: {
    SocketPort *port = new SocketPort(listener, handler);
    if (!port->OpenUDPListener(config.tcp_port)) {
      delete port;
      return nullptr;
    }

    return port;
  }

  case DeviceConfig::PortType::PTY: {
#if defined(HAVE_POSIX) && !defined(ANDROID)
    if (config.path.empty())
      return nullptr;

    if (unlink(config.path.c_str()) < 0 && errno != ENOENT)
      return nullptr;

    TTYPort *port = new TTYPort(listener, handler);
    const char *slave_path = port->OpenPseudo();
    if (slave_path == nullptr) {
      delete port;
      return nullptr;
    }

    if (symlink(slave_path, config.path.c_str()) < 0) {
      delete port;
      return nullptr;
    }

    return port;
#else
    return nullptr;
#endif
  }
  }

  if (path == nullptr)
    return nullptr;

#ifdef HAVE_POSIX
  TTYPort *port = new TTYPort(listener, handler);
#else
  SerialPort *port = new SerialPort(listener, handler);
#endif
  if (!port->Open(path, config.baud_rate)) {
    delete port;
    return nullptr;
  }

  return port;
}
Exemplo n.º 2
0
static Port *
OpenPortInternal(const DeviceConfig &config, Port::Handler &handler)
{
  const TCHAR *path = NULL;
  TCHAR buffer[MAX_PATH];

  switch (config.port_type) {
  case DeviceConfig::PortType::DISABLED:
    return NULL;

  case DeviceConfig::PortType::SERIAL:
    if (config.path.empty())
      return NULL;

    path = config.path.c_str();
    break;

  case DeviceConfig::PortType::RFCOMM:
#ifdef ANDROID
    if (config.bluetooth_mac.empty()) {
      LogStartUp(_T("No Bluetooth MAC configured"));
      return NULL;
    }

    return OpenAndroidBluetoothPort(config.bluetooth_mac, handler);
#else
    LogStartUp(_T("Bluetooth not available on this platform"));
    return NULL;
#endif

  case DeviceConfig::PortType::RFCOMM_SERVER:
#ifdef ANDROID
    return OpenAndroidBluetoothServerPort(handler);
#else
    LogStartUp(_T("Bluetooth not available on this platform"));
    return NULL;
#endif

  case DeviceConfig::PortType::IOIOUART:
#if defined(ANDROID) && defined(IOIOLIB)
    if (config.ioio_uart_id >= AndroidIOIOUartPort::getNumberUarts()) {
      LogStartUp(_T("No IOIOUart configured in profile"));
      return NULL;
    }

    return OpenAndroidIOIOUartPort(config.ioio_uart_id, config.baud_rate,
                                   handler);
#else
    LogStartUp(_T("IOIO Uart not available on this platform or version"));
    return NULL;
#endif

  case DeviceConfig::PortType::AUTO:
    if (!detect_gps(buffer, sizeof(buffer))) {
      LogStartUp(_T("no GPS detected"));
      return NULL;
    }

    LogStartUp(_T("GPS detected: %s"), buffer);

    path = buffer;
    break;

  case DeviceConfig::PortType::INTERNAL:
    break;

  case DeviceConfig::PortType::NETWORK_LISTENER: {
    TCPPort *tcp_port;
    UDPPort *udp_port;
    switch(config.network_protocol){
    case DeviceConfig::NetworkProtocolType::NETWORK_TCP:
      tcp_port = new TCPPort(handler);
      if (!tcp_port->Open(config.network_port)) {
        delete tcp_port;
        return NULL;
      }
      return tcp_port;
    case DeviceConfig::NetworkProtocolType::NETWORK_UDP:
      udp_port = new UDPPort(handler);
      if (!udp_port->Open(config.network_port)) {
        delete udp_port;
        return NULL;
      }
      return udp_port;
    }
  }

  case DeviceConfig::PortType::PTY: {
#if defined(HAVE_POSIX) && !defined(ANDROID)
    if (config.path.empty())
      return NULL;

    if (unlink(config.path.c_str()) < 0 && errno != ENOENT)
      return NULL;

    TTYPort *port = new TTYPort(handler);
    const char *slave_path = port->OpenPseudo();
    if (slave_path == NULL) {
      delete port;
      return NULL;
    }

    if (symlink(slave_path, config.path.c_str()) < 0) {
      delete port;
      return NULL;
    }

    return port;
#else
    return NULL;
#endif
  }
  }

  if (path == NULL)
    return NULL;

#ifdef HAVE_POSIX
  TTYPort *port = new TTYPort(handler);
#else
  SerialPort *port = new SerialPort(handler);
#endif
  if (!port->Open(path, config.baud_rate)) {
    delete port;
    return NULL;
  }

  return port;
}