diff --git a/CANBridge.code-workspace b/CANBridge.code-workspace index 1e48cc1..55a92ce 100644 --- a/CANBridge.code-workspace +++ b/CANBridge.code-workspace @@ -63,7 +63,10 @@ "xtree": "cpp", "xutility": "cpp", "array": "cpp", - "iterator": "cpp" + "iterator": "cpp", + "cstdarg": "cpp", + "fstream": "cpp", + "sstream": "cpp" }, "java.configuration.updateBuildConfiguration": "disabled" } diff --git a/build.gradle b/build.gradle index 9cd6fdb..8befc35 100644 --- a/build.gradle +++ b/build.gradle @@ -23,6 +23,12 @@ nativeUtils { exportsFile = project.file("src/main/native/c/candlelib/symbols.txt") } } + privateExportsConfigs { + SerialLib { + exportsFile = project.file("src/main/native/cpp/serial/symbols.txt") + } + } + } model { @@ -50,6 +56,7 @@ model { } binaries.all { lib library: "CandleLib", linkage: 'static' + lib library: "SerialLib", linkage: 'static' if (it.targetPlatform.name == nativeUtils.wpi.platforms.roborio) { it.buildable = false } @@ -75,6 +82,24 @@ model { } } } + SerialLib(NativeLibrarySpec) { + sources { + cpp { + source { + srcDirs 'src/main/native/cpp/serial/' + include '**/*.cc' + } + exportedHeaders { + srcDirs 'src/main/native/include/serial' + } + } + } + binaries.all { + if (it.targetPlatform.name == nativeUtils.wpi.platforms.roborio) { + it.buildable = false + } + } + } } @@ -93,6 +118,7 @@ model { binaries.all { lib library: "CandleLib", linkage: 'static' + lib library: "SerialLib", linkage: 'static' if (it.targetPlatform.name == nativeUtils.wpi.platforms.roborio) { it.buildable = false } diff --git a/src/main/native/cpp/CANBridgeUtils.cpp b/src/main/native/cpp/CANBridgeUtils.cpp index a37b880..e6caa3c 100644 --- a/src/main/native/cpp/CANBridgeUtils.cpp +++ b/src/main/native/cpp/CANBridgeUtils.cpp @@ -41,6 +41,11 @@ void convert_wstring_to_string(const std::wstring& in, std::string& out) out = converter.to_bytes(in.c_str()); } +void convert_string_to_wstring(const std::string& in, std::wstring& out) { + std::wstring_convert> converter; + out = converter.from_bytes(in.c_str()); +} + bool CANBridge_ProcessMask(const CANBridge_CANFilter& filter, uint32_t id) { bool result = (filter.messageMask & id) == (filter.messageMask & filter.messageId); diff --git a/src/main/native/cpp/Drivers/Serial/SerialDevice.cpp b/src/main/native/cpp/Drivers/Serial/SerialDevice.cpp index 216d12d..f7ab741 100644 --- a/src/main/native/cpp/Drivers/Serial/SerialDevice.cpp +++ b/src/main/native/cpp/Drivers/Serial/SerialDevice.cpp @@ -36,9 +36,7 @@ #include #include -#include "rev/Drivers/Serial/serial.h" - - +#include "serial/serial.h" namespace rev { namespace usb { @@ -49,8 +47,8 @@ SerialDevice::SerialDevice(std::string port) : m_device = new serial::Serial(port, 9600, serial::Timeout::simpleTimeout(1000)); try { - if (!m_device.isOpen()) { - m_device.open(); + if (!m_device->isOpen()) { + m_device->open(); } } catch(const std::exception& e) { std::cout << e.what() << std::endl; @@ -58,8 +56,8 @@ SerialDevice::SerialDevice(std::string port) : } - m_descriptor = std::wstring() - m_name = m_device.getPort(); + m_descriptor = std::wstring(); + m_name = m_device->getPort(); m_thread.Start(); } diff --git a/src/main/native/cpp/Drivers/Serial/SerialDriver.cpp b/src/main/native/cpp/Drivers/Serial/SerialDriver.cpp index 52184b6..fd7c7bf 100644 --- a/src/main/native/cpp/Drivers/Serial/SerialDriver.cpp +++ b/src/main/native/cpp/Drivers/Serial/SerialDriver.cpp @@ -30,8 +30,9 @@ #include "rev/Drivers/Serial/SerialDriver.h" #include "rev/Drivers/Serial/SerialDevice.h" +#include "rev/CANBridgeUtils.h" -#include "rev/Drivers/Serial/serial.h" +#include "serial/serial.h" #include #include @@ -45,11 +46,12 @@ std::vector SerialDriver::GetDevices() // Search driver layer for devices std::vector retval; - std::vector found = serial::list_ports(); + std::vector found = serial::list_ports(); for (auto& dev : found) { - std::wstring desc(dev.hardware_id.c_str()); - std::string name(dev.port.c_str()); - retval.push_back({desc, name, this->GetName()}); + std::wstring desc; + convert_string_to_wstring(dev.hardware_id.c_str(), desc); + std::string name(dev.port.c_str()); // SPARK MAX + retval.push_back({desc, name, this->GetName()}); // SPARK MAX Legacy } return retval; @@ -59,11 +61,12 @@ std::unique_ptr SerialDriver::CreateDeviceFromDescriptor(const wchar_ { // Search driver layer for devices - std::vector found = serial::list_ports(); + std::vector found = serial::list_ports(); for (auto& dev : found) { - std::wstring path(dev.hardware_id.c_str()); + std::wstring path; + convert_string_to_wstring(dev.hardware_id.c_str(), path); if (path == std::wstring(descriptor)) { - return std::make_unique(dev); + return std::make_unique(dev.port); } } diff --git a/src/main/native/cpp/Drivers/Serial/list_ports_linux.cc b/src/main/native/cpp/serial/list_ports_linux.cc similarity index 95% rename from src/main/native/cpp/Drivers/Serial/list_ports_linux.cc rename to src/main/native/cpp/serial/list_ports_linux.cc index 9779d5e..349e36f 100644 --- a/src/main/native/cpp/Drivers/Serial/list_ports_linux.cc +++ b/src/main/native/cpp/serial/list_ports_linux.cc @@ -22,7 +22,7 @@ #include #include -#include "serial/serial.h" +#include "serial.h" using serial::PortInfo; using std::istringstream; @@ -54,16 +54,16 @@ glob(const vector& patterns) glob_t glob_results; - int glob_retval = glob(patterns[0].c_str(), 0, NULL, &glob_results); + glob(patterns[0].c_str(), 0, NULL, &glob_results); vector::const_iterator iter = patterns.begin(); while(++iter != patterns.end()) { - glob_retval = glob(iter->c_str(), GLOB_APPEND, NULL, &glob_results); + glob(iter->c_str(), GLOB_APPEND, NULL, &glob_results); } - for(int path_index = 0; path_index < glob_results.gl_pathc; path_index++) + for(uint path_index = 0; path_index < glob_results.gl_pathc; path_index++) { paths_found.push_back(glob_results.gl_pathv[path_index]); } @@ -243,7 +243,7 @@ format(const char* format, ...) { done = true; } - else if( return_value >= buffer_size_bytes ) + else if( return_value >= static_cast(buffer_size_bytes) ) { // Realloc and try again. diff --git a/src/main/native/cpp/Drivers/Serial/list_ports_osx.cc b/src/main/native/cpp/serial/list_ports_osx.cc similarity index 100% rename from src/main/native/cpp/Drivers/Serial/list_ports_osx.cc rename to src/main/native/cpp/serial/list_ports_osx.cc diff --git a/src/main/native/cpp/Drivers/Serial/list_ports_win.cc b/src/main/native/cpp/serial/list_ports_win.cc similarity index 95% rename from src/main/native/cpp/Drivers/Serial/list_ports_win.cc rename to src/main/native/cpp/serial/list_ports_win.cc index 7da40c4..ad5c40f 100644 --- a/src/main/native/cpp/Drivers/Serial/list_ports_win.cc +++ b/src/main/native/cpp/serial/list_ports_win.cc @@ -7,7 +7,7 @@ * http://opensource.org/licenses/MIT */ -#include "serial/serial.h" +#include "serial.h" #include #include #include @@ -15,6 +15,13 @@ #include #include +#ifdef _MSC_VER +#pragma comment(lib, "winusb.lib") +#pragma comment(lib, "setupapi.lib") +#pragma comment(lib, "ole32.lib") +#pragma comment(lib, "advapi32.lib") +#endif + using serial::PortInfo; using std::vector; using std::string; diff --git a/src/main/native/cpp/Drivers/Serial/serial.cc b/src/main/native/cpp/serial/serial.cc similarity index 98% rename from src/main/native/cpp/Drivers/Serial/serial.cc rename to src/main/native/cpp/serial/serial.cc index 4ce8392..504098a 100644 --- a/src/main/native/cpp/Drivers/Serial/serial.cc +++ b/src/main/native/cpp/serial/serial.cc @@ -9,12 +9,12 @@ # define alloca __builtin_alloca #endif -#include "serial/serial.h" +#include "serial.h" #ifdef _WIN32 -#include "rev/Drivers/Serial/win.h" +#include "win.h" #else -#include "rev/Drivers/Serial/unix.h" +#include "unix.h" #endif using std::invalid_argument; @@ -139,7 +139,7 @@ Serial::read (std::vector &buffer, size_t size) } catch (const std::exception &e) { delete[] buffer_; - throw; + throw e; } buffer.insert (buffer.end (), buffer_, buffer_+bytes_read); @@ -158,7 +158,7 @@ Serial::read (std::string &buffer, size_t size) } catch (const std::exception &e) { delete[] buffer_; - throw; + throw e; } buffer.append (reinterpret_cast(buffer_), bytes_read); delete[] buffer_; diff --git a/src/main/native/cpp/serial/symbols.txt b/src/main/native/cpp/serial/symbols.txt new file mode 100644 index 0000000..eed099f --- /dev/null +++ b/src/main/native/cpp/serial/symbols.txt @@ -0,0 +1 @@ +list_ports \ No newline at end of file diff --git a/src/main/native/cpp/Drivers/Serial/unix.cc b/src/main/native/cpp/serial/unix.cc similarity index 99% rename from src/main/native/cpp/Drivers/Serial/unix.cc rename to src/main/native/cpp/serial/unix.cc index 4309aa6..4283548 100644 --- a/src/main/native/cpp/Drivers/Serial/unix.cc +++ b/src/main/native/cpp/serial/unix.cc @@ -32,7 +32,7 @@ #include #endif -#include "serial/impl/unix.h" +#include "unix.h" #ifndef TIOCINQ #ifdef FIONREAD diff --git a/src/main/native/cpp/Drivers/Serial/win.cc b/src/main/native/cpp/serial/win.cc similarity index 99% rename from src/main/native/cpp/Drivers/Serial/win.cc rename to src/main/native/cpp/serial/win.cc index 786f4f6..9147f5a 100644 --- a/src/main/native/cpp/Drivers/Serial/win.cc +++ b/src/main/native/cpp/serial/win.cc @@ -4,7 +4,7 @@ #include -#include "serial/impl/win.h" +#include "win.h" using std::string; using std::wstring; diff --git a/src/main/native/include/rev/CANBridgeUtils.h b/src/main/native/include/rev/CANBridgeUtils.h index ce41d23..605f67d 100644 --- a/src/main/native/include/rev/CANBridgeUtils.h +++ b/src/main/native/include/rev/CANBridgeUtils.h @@ -49,6 +49,7 @@ namespace usb { }; void convert_wstring_to_string(const std::wstring& in, std::string& out); + void convert_string_to_wstring(const std::string& in, std::wstring& out); bool CANBridge_ProcessMask(const CANBridge_CANFilter& filter, uint32_t id); diff --git a/src/main/native/include/rev/Drivers/CandleWinUSB/CandleWinUSBDriver.h b/src/main/native/include/rev/Drivers/CandleWinUSB/CandleWinUSBDriver.h index aae88d9..bfcb0d3 100644 --- a/src/main/native/include/rev/Drivers/CandleWinUSB/CandleWinUSBDriver.h +++ b/src/main/native/include/rev/Drivers/CandleWinUSB/CandleWinUSBDriver.h @@ -39,7 +39,7 @@ namespace usb { class CandleWinUSBDriver : public CANDriver { public: CandleWinUSBDriver() {} - ~CandleWinUSBDriver() {} + ~CandleWinUSBDriver() override {} virtual std::string GetName() const {return "Candle WINUSB";} diff --git a/src/main/native/include/rev/Drivers/Serial/SerialDevice.h b/src/main/native/include/rev/Drivers/Serial/SerialDevice.h index 6f20fe7..69872cd 100644 --- a/src/main/native/include/rev/Drivers/Serial/SerialDevice.h +++ b/src/main/native/include/rev/Drivers/Serial/SerialDevice.h @@ -60,7 +60,7 @@ class SerialDevice : public CANDevice { virtual bool IsConnected(); private: - serial::Serial m_device; + serial::Serial* m_device; SerialDeviceThread m_thread; std::wstring m_descriptor; std::string m_name; diff --git a/src/main/native/include/rev/Drivers/Serial/SerialDeviceThread.h b/src/main/native/include/rev/Drivers/Serial/SerialDeviceThread.h index 45a62a4..b4a7b49 100644 --- a/src/main/native/include/rev/Drivers/Serial/SerialDeviceThread.h +++ b/src/main/native/include/rev/Drivers/Serial/SerialDeviceThread.h @@ -45,7 +45,7 @@ #include "rev/CANBridgeUtils.h" #include "utils/CircularBuffer.h" -#include "serial.h" +#include "serial/serial.h" #include #include diff --git a/src/main/native/include/rev/Drivers/Serial/SerialDriver.h b/src/main/native/include/rev/Drivers/Serial/SerialDriver.h index 698c844..5a677dc 100644 --- a/src/main/native/include/rev/Drivers/Serial/SerialDriver.h +++ b/src/main/native/include/rev/Drivers/Serial/SerialDriver.h @@ -39,12 +39,12 @@ namespace usb { class SerialDriver : public CANDriver { public: SerialDriver() {} - ~SerialDriver() {} + virtual ~SerialDriver() override {} virtual std::string GetName() const {return "Serial Port";} - virtual std::vector GetDevices(); - virtual std::unique_ptr CreateDeviceFromDescriptor(const wchar_t* descriptor); + virtual std::vector GetDevices() override; + virtual std::unique_ptr CreateDeviceFromDescriptor(const wchar_t* descriptor) override; }; } // namespace usb diff --git a/src/main/native/include/rev/Drivers/Serial/serial.h b/src/main/native/include/serial/serial.h similarity index 100% rename from src/main/native/include/rev/Drivers/Serial/serial.h rename to src/main/native/include/serial/serial.h diff --git a/src/main/native/include/rev/Drivers/Serial/unix.h b/src/main/native/include/serial/unix.h similarity index 98% rename from src/main/native/include/rev/Drivers/Serial/unix.h rename to src/main/native/include/serial/unix.h index 0fb38f2..f1c461f 100644 --- a/src/main/native/include/rev/Drivers/Serial/unix.h +++ b/src/main/native/include/serial/unix.h @@ -37,10 +37,10 @@ #if !defined(_WIN32) -#ifndef SERIAL_IMPL_UNIX_H -#define SERIAL_IMPL_UNIX_H +#ifndef SERIAL_UNIX_H +#define SERIAL_UNIX_H -#include "serial/serial.h" +#include "serial.h" #include diff --git a/src/main/native/include/rev/Drivers/Serial/v8stdint.h b/src/main/native/include/serial/v8stdint.h similarity index 100% rename from src/main/native/include/rev/Drivers/Serial/v8stdint.h rename to src/main/native/include/serial/v8stdint.h diff --git a/src/main/native/include/rev/Drivers/Serial/win.h b/src/main/native/include/serial/win.h similarity index 97% rename from src/main/native/include/rev/Drivers/Serial/win.h rename to src/main/native/include/serial/win.h index 2c0c6cd..f4ac21c 100644 --- a/src/main/native/include/rev/Drivers/Serial/win.h +++ b/src/main/native/include/serial/win.h @@ -36,10 +36,10 @@ #if defined(_WIN32) -#ifndef SERIAL_IMPL_WINDOWS_H -#define SERIAL_IMPL_WINDOWS_H +#ifndef SERIAL_WINDOWS_H +#define SERIAL_WINDOWS_H -#include "serial/serial.h" +#include "serial.h" #include "windows.h"