Skip to content

Commit

Permalink
Added in the serial communication library and basic driver/device code
Browse files Browse the repository at this point in the history
  • Loading branch information
kellyostrom committed Sep 24, 2019
1 parent d317384 commit 2468f05
Show file tree
Hide file tree
Showing 21 changed files with 85 additions and 41 deletions.
5 changes: 4 additions & 1 deletion CANBridge.code-workspace
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,10 @@
"xtree": "cpp",
"xutility": "cpp",
"array": "cpp",
"iterator": "cpp"
"iterator": "cpp",
"cstdarg": "cpp",
"fstream": "cpp",
"sstream": "cpp"
},
"java.configuration.updateBuildConfiguration": "disabled"
}
Expand Down
26 changes: 26 additions & 0 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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
}
Expand All @@ -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
}
}
}

}

Expand All @@ -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
}
Expand Down
5 changes: 5 additions & 0 deletions src/main/native/cpp/CANBridgeUtils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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<std::codecvt_utf8_utf16<wchar_t>> 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);
Expand Down
12 changes: 5 additions & 7 deletions src/main/native/cpp/Drivers/Serial/SerialDevice.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -36,9 +36,7 @@
#include <mockdata/CanData.h>
#include <hal/CAN.h>

#include "rev/Drivers/Serial/serial.h"


#include "serial/serial.h"

namespace rev {
namespace usb {
Expand All @@ -49,17 +47,17 @@ 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;
throw "Failed to open device!";
}


m_descriptor = std::wstring()
m_name = m_device.getPort();
m_descriptor = std::wstring();
m_name = m_device->getPort();
m_thread.Start();
}

Expand Down
19 changes: 11 additions & 8 deletions src/main/native/cpp/Drivers/Serial/SerialDriver.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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 <map>
#include <iostream>
Expand All @@ -45,11 +46,12 @@ std::vector<CANDeviceDetail> SerialDriver::GetDevices()
// Search driver layer for devices
std::vector<CANDeviceDetail> retval;

std::vector<serial::PortInfo> found = serial::list_ports();
std::vector<serial::PortInfo> 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;
Expand All @@ -59,11 +61,12 @@ std::unique_ptr<CANDevice> SerialDriver::CreateDeviceFromDescriptor(const wchar_
{
// Search driver layer for devices

std::vector<serial::PortInfo> found = serial::list_ports();
std::vector<serial::PortInfo> 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<SerialDevice>(dev);
return std::make_unique<SerialDevice>(dev.port);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
#include <sys/stat.h>
#include <unistd.h>

#include "serial/serial.h"
#include "serial.h"

using serial::PortInfo;
using std::istringstream;
Expand Down Expand Up @@ -54,16 +54,16 @@ glob(const vector<string>& 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<string>::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]);
}
Expand Down Expand Up @@ -243,7 +243,7 @@ format(const char* format, ...)
{
done = true;
}
else if( return_value >= buffer_size_bytes )
else if( return_value >= static_cast<int>(buffer_size_bytes) )
{
// Realloc and try again.

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,21 @@
* http://opensource.org/licenses/MIT
*/

#include "serial/serial.h"
#include "serial.h"
#include <tchar.h>
#include <windows.h>
#include <setupapi.h>
#include <initguid.h>
#include <devguid.h>
#include <cstring>

#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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -139,7 +139,7 @@ Serial::read (std::vector<uint8_t> &buffer, size_t size)
}
catch (const std::exception &e) {
delete[] buffer_;
throw;
throw e;
}

buffer.insert (buffer.end (), buffer_, buffer_+bytes_read);
Expand All @@ -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<const char*>(buffer_), bytes_read);
delete[] buffer_;
Expand Down
1 change: 1 addition & 0 deletions src/main/native/cpp/serial/symbols.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
list_ports
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@
#include <mach/mach.h>
#endif

#include "serial/impl/unix.h"
#include "unix.h"

#ifndef TIOCINQ
#ifdef FIONREAD
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

#include <sstream>

#include "serial/impl/win.h"
#include "win.h"

using std::string;
using std::wstring;
Expand Down
1 change: 1 addition & 0 deletions src/main/native/include/rev/CANBridgeUtils.h
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ namespace usb {
class CandleWinUSBDriver : public CANDriver {
public:
CandleWinUSBDriver() {}
~CandleWinUSBDriver() {}
~CandleWinUSBDriver() override {}

virtual std::string GetName() const {return "Candle WINUSB";}

Expand Down
2 changes: 1 addition & 1 deletion src/main/native/include/rev/Drivers/Serial/SerialDevice.h
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@
#include "rev/CANBridgeUtils.h"
#include "utils/CircularBuffer.h"

#include "serial.h"
#include "serial/serial.h"

#include <mockdata/CanData.h>
#include <hal/CAN.h>
Expand Down
6 changes: 3 additions & 3 deletions src/main/native/include/rev/Drivers/Serial/SerialDriver.h
Original file line number Diff line number Diff line change
Expand Up @@ -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<CANDeviceDetail> GetDevices();
virtual std::unique_ptr<CANDevice> CreateDeviceFromDescriptor(const wchar_t* descriptor);
virtual std::vector<CANDeviceDetail> GetDevices() override;
virtual std::unique_ptr<CANDevice> CreateDeviceFromDescriptor(const wchar_t* descriptor) override;
};

} // namespace usb
Expand Down
File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -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 <pthread.h>

Expand Down
File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -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"

Expand Down

0 comments on commit 2468f05

Please sign in to comment.