Esempio n. 1
0
Port *
OpenPort(const DeviceConfig &config, Port::Handler &handler)
{
  if (is_simulator())
    return new NullPort(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;
    }

    {
      AndroidBluetoothPort *port =
        new AndroidBluetoothPort(config.bluetooth_mac, handler);
      if (!port->Open()) {
        delete port;
        return NULL;
      }

      return port;
    }
#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;
      }

      {
        AndroidIOIOUartPort *port =
          new AndroidIOIOUartPort(config.ioio_uart_id, config.baud_rate, handler);

        if (!port->Open()) {
          delete port;
          return NULL;
        }

        return port;
      }
    }
#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::TCP_LISTENER: {
    TCPPort *port = new TCPPort(config.tcp_port, handler);
    if (!port->Open()) {
      delete port;
      return NULL;
    }

    return port;
  }
  }

  if (path == NULL)
    return NULL;

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

  return Com;
}
Esempio n. 2
0
static Port *
OpenPort(const DeviceConfig &config, Port::Handler &handler)
{
  if (is_simulator())
    return new NullPort(handler);

  const TCHAR *path = NULL;
  TCHAR buffer[MAX_PATH];

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

  case DeviceConfig::SERIAL:
    path = COMMPort[config.port_index];
    break;

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

    {
      AndroidBluetoothPort *port =
        new AndroidBluetoothPort(config.bluetooth_mac, handler);
      if (!port->Open()) {
        delete port;
        return NULL;
      }

      return port;
    }
#else
    LogStartUp(_T("Bluetooth not available on this platform"));
    return NULL;
#endif

  case DeviceConfig::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::INTERNAL:
    break;

  case DeviceConfig::TCP_LISTENER: {
    TCPPort *port = new TCPPort(4353, handler);
    if (!port->Open()) {
      delete port;
      return NULL;
    }

    return port;
  }
  }

#ifdef ANDROID
  return NULL;
#else
  if (path == NULL)
    return NULL;

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

  return Com;
#endif
}