int main(int argc, char **argv) { if (argc != 3) { fprintf(stderr, "Usage: %s PORT BAUD\n", argv[0]); return EXIT_FAILURE; } PathName port_name(argv[1]); int baud = atoi(argv[2]); #ifdef HAVE_POSIX TTYPort *port = new TTYPort(port_name, baud, *(Port::Handler *)NULL); #else SerialPort *port = new SerialPort(port_name, baud, *(Port::Handler *)NULL); #endif if (!port->Open()) { delete port; fprintf(stderr, "Failed to open COM port\n"); return EXIT_FAILURE; } port->SetRxTimeout(0x10000); char buffer[4096]; while (true) { int nbytes = port->Read(buffer, sizeof(buffer)); if (nbytes < 0) break; fwrite((const void *)buffer, 1, nbytes, stdout); } delete port; return EXIT_SUCCESS; }
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; }
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; }
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; }
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 }