Skip to content

Added support for physical Ticket Dispenser / Coin Hopper #13512

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
53 changes: 53 additions & 0 deletions 3rdparty/serial/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
cmake_minimum_required(VERSION 3.15)

project(serial CXX)

# Platform-specific source files
set(SERIAL_PLATFORM_SRCS)
if(WINDOWS)
list(APPEND SERIAL_PLATFORM_SRCS
src/impl/win.cc
src/impl/list_ports/list_ports_win.cc
)
elseif(UNIX)
list(APPEND SERIAL_PLATFORM_SRCS
src/impl/unix.cc
src/impl/list_ports/list_ports_linux.cc
)
if(APPLE)
list(APPEND SERIAL_PLATFORM_SRCS src/impl/list_ports/list_ports_osx.cc)
endif()
endif()

# Core serial library sources
set(SERIAL_SRCS
src/serial.cc
${SERIAL_PLATFORM_SRCS}
)

# Create static library
add_library(serial STATIC ${SERIAL_SRCS})

# Include directories
target_include_directories(serial PUBLIC
${CMAKE_CURRENT_SOURCE_DIR}/include
)

# Set C++ standard
set_target_properties(serial PROPERTIES
CXX_STANDARD 11
CXX_STANDARD_REQUIRED ON
)

# Platform-specific libraries
if(WINDOWS)
target_link_libraries(serial PRIVATE setupapi)
elseif(UNIX)
if(APPLE)
find_library(IOKIT_LIBRARY IOKit)
find_library(FOUNDATION_LIBRARY Foundation)
target_link_libraries(serial PRIVATE ${FOUNDATION_LIBRARY} ${IOKIT_LIBRARY})
else()
target_link_libraries(serial PRIVATE rt pthread)
endif()
endif()
7 changes: 7 additions & 0 deletions 3rdparty/serial/LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
Copyright (c) 2012 William Woodall, John Harrison

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
63 changes: 63 additions & 0 deletions 3rdparty/serial/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
# Serial Communication Library

[![Build Status](https://travis-ci.org/wjwwood/serial.svg?branch=master)](https://travis-ci.org/wjwwood/serial)*(Linux and OS X)* [![Build Status](https://ci.appveyor.com/api/projects/status/github/wjwwood/serial)](https://ci.appveyor.com/project/wjwwood/serial)*(Windows)*

This is a cross-platform library for interfacing with rs-232 serial like ports written in C++. It provides a modern C++ interface with a workflow designed to look and feel like PySerial, but with the speed and control provided by C++.

This library is in use in several robotics related projects and can be built and installed to the OS like most unix libraries with make and then sudo make install, but because it is a catkin project it can also be built along side other catkin projects in a catkin workspace.

Serial is a class that provides the basic interface common to serial libraries (open, close, read, write, etc..) and requires no extra dependencies. It also provides tight control over timeouts and control over handshaking lines.

### Documentation

Website: http://wjwwood.github.io/serial/

API Documentation: http://wjwwood.github.io/serial/doc/1.1.0/index.html

### Dependencies

Required:
* [catkin](http://www.ros.org/wiki/catkin) - cmake and Python based buildsystem
* [cmake](http://www.cmake.org) - buildsystem
* [Python](http://www.python.org) - scripting language
* [empy](http://www.alcyone.com/pyos/empy/) - Python templating library
* [catkin_pkg](http://pypi.python.org/pypi/catkin_pkg/) - Runtime Python library for catkin

Optional (for documentation):
* [Doxygen](http://www.doxygen.org/) - Documentation generation tool
* [graphviz](http://www.graphviz.org/) - Graph visualization software

### Install

Get the code:

git clone https://github.com/wjwwood/serial.git

Build:

make

Build and run the tests:

make test

Build the documentation:

make doc

Install:

make install

### License

[The MIT License](LICENSE)

### Authors

William Woodall <[email protected]>
John Harrison <[email protected]>

### Contact

William Woodall <[email protected]>
221 changes: 221 additions & 0 deletions 3rdparty/serial/include/serial/impl/unix.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,221 @@
/*!
* \file serial/impl/unix.h
* \author William Woodall <[email protected]>
* \author John Harrison <[email protected]>
* \version 0.1
*
* \section LICENSE
*
* The MIT License
*
* Copyright (c) 2012 William Woodall, John Harrison
*
* Permission is hereby granted, free of charge, to any person obtaining a
* copy of this software and associated documentation files (the "Software"),
* to deal in the Software without restriction, including without limitation
* the rights to use, copy, modify, merge, publish, distribute, sublicense,
* and/or sell copies of the Software, and to permit persons to whom the
* Software is furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
* DEALINGS IN THE SOFTWARE.
*
* \section DESCRIPTION
*
* This provides a unix based pimpl for the Serial class. This implementation is
* based off termios.h and uses select for multiplexing the IO ports.
*
*/

#if !defined(_WIN32)

#ifndef SERIAL_IMPL_UNIX_H
#define SERIAL_IMPL_UNIX_H

#include "serial/serial.h"

#include <pthread.h>

namespace serial {

using std::size_t;
using std::string;
using std::invalid_argument;

using serial::SerialException;
using serial::IOException;

class MillisecondTimer {
public:
MillisecondTimer(const uint32_t millis);
int64_t remaining();

private:
static timespec timespec_now();
timespec expiry;
};

class serial::Serial::SerialImpl {
public:
SerialImpl (const string &port,
unsigned long baudrate,
bytesize_t bytesize,
parity_t parity,
stopbits_t stopbits,
flowcontrol_t flowcontrol);

virtual ~SerialImpl ();

void
open ();

void
close ();

bool
isOpen () const;

size_t
available ();

bool
waitReadable (uint32_t timeout);

void
waitByteTimes (size_t count);

size_t
read (uint8_t *buf, size_t size = 1);

size_t
write (const uint8_t *data, size_t length);

void
flush ();

void
flushInput ();

void
flushOutput ();

void
sendBreak (int duration);

void
setBreak (bool level);

void
setRTS (bool level);

void
setDTR (bool level);

bool
waitForChange ();

bool
getCTS ();

bool
getDSR ();

bool
getRI ();

bool
getCD ();

void
setPort (const string &port);

string
getPort () const;

void
setTimeout (Timeout &timeout);

Timeout
getTimeout () const;

void
setBaudrate (unsigned long baudrate);

unsigned long
getBaudrate () const;

void
setBytesize (bytesize_t bytesize);

bytesize_t
getBytesize () const;

void
setParity (parity_t parity);

parity_t
getParity () const;

void
setStopbits (stopbits_t stopbits);

stopbits_t
getStopbits () const;

void
setFlowcontrol (flowcontrol_t flowcontrol);

flowcontrol_t
getFlowcontrol () const;

void
readLock ();

void
readUnlock ();

void
writeLock ();

void
writeUnlock ();

protected:
void reconfigurePort ();

private:
string port_; // Path to the file descriptor
int fd_; // The current file descriptor

bool is_open_;
bool xonxoff_;
bool rtscts_;

Timeout timeout_; // Timeout for read operations
unsigned long baudrate_; // Baudrate
uint32_t byte_time_ns_; // Nanoseconds to transmit/receive a single byte

parity_t parity_; // Parity
bytesize_t bytesize_; // Size of the bytes
stopbits_t stopbits_; // Stop Bits
flowcontrol_t flowcontrol_; // Flow Control

// Mutex used to lock the read functions
pthread_mutex_t read_mutex;
// Mutex used to lock the write functions
pthread_mutex_t write_mutex;
};

}

#endif // SERIAL_IMPL_UNIX_H

#endif // !defined(_WIN32)
207 changes: 207 additions & 0 deletions 3rdparty/serial/include/serial/impl/win.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,207 @@
/*!
* \file serial/impl/windows.h
* \author William Woodall <wjwwood@gmail.com>
* \author John Harrison <ash@greaterthaninfinity.com>
* \version 0.1
*
* \section LICENSE
*
* The MIT License
*
* Copyright (c) 2012 William Woodall, John Harrison
*
* Permission is hereby granted, free of charge, to any person obtaining a
* copy of this software and associated documentation files (the "Software"),
* to deal in the Software without restriction, including without limitation
* the rights to use, copy, modify, merge, publish, distribute, sublicense,
* and/or sell copies of the Software, and to permit persons to whom the
* Software is furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
* DEALINGS IN THE SOFTWARE.
*
* \section DESCRIPTION
*
* This provides a windows implementation of the Serial class interface.
*
*/

#if defined(_WIN32)

#ifndef SERIAL_IMPL_WINDOWS_H
#define SERIAL_IMPL_WINDOWS_H

#include "serial/serial.h"

#include "windows.h"

namespace serial {

using std::string;
using std::wstring;
using std::invalid_argument;

using serial::SerialException;
using serial::IOException;

class serial::Serial::SerialImpl {
public:
SerialImpl (const string &port,
unsigned long baudrate,
bytesize_t bytesize,
parity_t parity,
stopbits_t stopbits,
flowcontrol_t flowcontrol);

virtual ~SerialImpl ();

void
open ();

void
close ();

bool
isOpen () const;

size_t
available ();

bool
waitReadable (uint32_t timeout);

void
waitByteTimes (size_t count);

size_t
read (uint8_t *buf, size_t size = 1);

size_t
write (const uint8_t *data, size_t length);

void
flush ();

void
flushInput ();

void
flushOutput ();

void
sendBreak (int duration);

void
setBreak (bool level);

void
setRTS (bool level);

void
setDTR (bool level);

bool
waitForChange ();

bool
getCTS ();

bool
getDSR ();

bool
getRI ();

bool
getCD ();

void
setPort (const string &port);

string
getPort () const;

void
setTimeout (Timeout &timeout);

Timeout
getTimeout () const;

void
setBaudrate (unsigned long baudrate);

unsigned long
getBaudrate () const;

void
setBytesize (bytesize_t bytesize);

bytesize_t
getBytesize () const;

void
setParity (parity_t parity);

parity_t
getParity () const;

void
setStopbits (stopbits_t stopbits);

stopbits_t
getStopbits () const;

void
setFlowcontrol (flowcontrol_t flowcontrol);

flowcontrol_t
getFlowcontrol () const;

void
readLock ();

void
readUnlock ();

void
writeLock ();

void
writeUnlock ();

protected:
void reconfigurePort ();

private:
wstring port_; // Path to the file descriptor
HANDLE fd_;

bool is_open_;

Timeout timeout_; // Timeout for read operations
unsigned long baudrate_; // Baudrate

parity_t parity_; // Parity
bytesize_t bytesize_; // Size of the bytes
stopbits_t stopbits_; // Stop Bits
flowcontrol_t flowcontrol_; // Flow Control

// Mutex used to lock the read functions
HANDLE read_mutex;
// Mutex used to lock the write functions
HANDLE write_mutex;
};

}

#endif // SERIAL_IMPL_WINDOWS_H

#endif // if defined(_WIN32)
775 changes: 775 additions & 0 deletions 3rdparty/serial/include/serial/serial.h

Large diffs are not rendered by default.

57 changes: 57 additions & 0 deletions 3rdparty/serial/include/serial/v8stdint.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
// This header is from the v8 google project:
// http://code.google.com/p/v8/source/browse/trunk/include/v8stdint.h

// Copyright 2012 the V8 project authors. All rights reserved.
// Redistribution and use in source and binary forms, with or without
// modification, are permitted provided that the following conditions are
// met:
//
// * Redistributions of source code must retain the above copyright
// notice, this list of conditions and the following disclaimer.
// * Redistributions in binary form must reproduce the above
// copyright notice, this list of conditions and the following
// disclaimer in the documentation and/or other materials provided
// with the distribution.
// * Neither the name of Google Inc. nor the names of its
// contributors may be used to endorse or promote products derived
// from this software without specific prior written permission.
//
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

// Load definitions of standard types.

#ifndef V8STDINT_H_
#define V8STDINT_H_

#include <stddef.h>
#include <stdio.h>

#if defined(_WIN32) && !defined(__MINGW32__)

typedef signed char int8_t;
typedef unsigned char uint8_t;
typedef short int16_t; // NOLINT
typedef unsigned short uint16_t; // NOLINT
typedef int int32_t;
typedef unsigned int uint32_t;
typedef __int64 int64_t;
typedef unsigned __int64 uint64_t;
// intptr_t and friends are defined in crtdefs.h through stdio.h.

#else

#include <stdint.h>

#endif

#endif // V8STDINT_H_
336 changes: 336 additions & 0 deletions 3rdparty/serial/src/impl/list_ports/list_ports_linux.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,336 @@
#if defined(__linux__)

/*
* Copyright (c) 2014 Craig Lilley <cralilley@gmail.com>
* This software is made available under the terms of the MIT licence.
* A copy of the licence can be obtained from:
* http://opensource.org/licenses/MIT
*/

#include <vector>
#include <string>
#include <sstream>
#include <stdexcept>
#include <iostream>
#include <fstream>
#include <cstdio>
#include <cstdarg>
#include <cstdlib>

#include <glob.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>

#include "serial/serial.h"

using serial::PortInfo;
using std::istringstream;
using std::ifstream;
using std::getline;
using std::vector;
using std::string;
using std::cout;
using std::endl;

static vector<string> glob(const vector<string>& patterns);
static string basename(const string& path);
static string dirname(const string& path);
static bool path_exists(const string& path);
static string realpath(const string& path);
static string usb_sysfs_friendly_name(const string& sys_usb_path);
static vector<string> get_sysfs_info(const string& device_path);
static string read_line(const string& file);
static string usb_sysfs_hw_string(const string& sysfs_path);
static string format(const char* format, ...);

vector<string>
glob(const vector<string>& patterns)
{
vector<string> paths_found;

if(patterns.size() == 0)
return paths_found;

glob_t glob_results;

int glob_retval = 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);
}

for(int path_index = 0; path_index < glob_results.gl_pathc; path_index++)
{
paths_found.push_back(glob_results.gl_pathv[path_index]);
}

globfree(&glob_results);

return paths_found;
}

string
basename(const string& path)
{
size_t pos = path.rfind("/");

if(pos == std::string::npos)
return path;

return string(path, pos+1, string::npos);
}

string
dirname(const string& path)
{
size_t pos = path.rfind("/");

if(pos == std::string::npos)
return path;
else if(pos == 0)
return "/";

return string(path, 0, pos);
}

bool
path_exists(const string& path)
{
struct stat sb;

if( stat(path.c_str(), &sb ) == 0 )
return true;

return false;
}

string
realpath(const string& path)
{
char* real_path = realpath(path.c_str(), NULL);

string result;

if(real_path != NULL)
{
result = real_path;

free(real_path);
}

return result;
}

string
usb_sysfs_friendly_name(const string& sys_usb_path)
{
unsigned int device_number = 0;

istringstream( read_line(sys_usb_path + "/devnum") ) >> device_number;

string manufacturer = read_line( sys_usb_path + "/manufacturer" );

string product = read_line( sys_usb_path + "/product" );

string serial = read_line( sys_usb_path + "/serial" );

if( manufacturer.empty() && product.empty() && serial.empty() )
return "";

return format("%s %s %s", manufacturer.c_str(), product.c_str(), serial.c_str() );
}

vector<string>
get_sysfs_info(const string& device_path)
{
string device_name = basename( device_path );

string friendly_name;

string hardware_id;

string sys_device_path = format( "/sys/class/tty/%s/device", device_name.c_str() );

if( device_name.compare(0,6,"ttyUSB") == 0 )
{
sys_device_path = dirname( dirname( realpath( sys_device_path ) ) );

if( path_exists( sys_device_path ) )
{
friendly_name = usb_sysfs_friendly_name( sys_device_path );

hardware_id = usb_sysfs_hw_string( sys_device_path );
}
}
else if( device_name.compare(0,6,"ttyACM") == 0 )
{
sys_device_path = dirname( realpath( sys_device_path ) );

if( path_exists( sys_device_path ) )
{
friendly_name = usb_sysfs_friendly_name( sys_device_path );

hardware_id = usb_sysfs_hw_string( sys_device_path );
}
}
else
{
// Try to read ID string of PCI device

string sys_id_path = sys_device_path + "/id";

if( path_exists( sys_id_path ) )
hardware_id = read_line( sys_id_path );
}

if( friendly_name.empty() )
friendly_name = device_name;

if( hardware_id.empty() )
hardware_id = "n/a";

vector<string> result;
result.push_back(friendly_name);
result.push_back(hardware_id);

return result;
}

string
read_line(const string& file)
{
ifstream ifs(file.c_str(), ifstream::in);

string line;

if(ifs)
{
getline(ifs, line);
}

return line;
}

string
format(const char* format, ...)
{
va_list ap;

size_t buffer_size_bytes = 256;

string result;

char* buffer = (char*)malloc(buffer_size_bytes);

if( buffer == NULL )
return result;

bool done = false;

unsigned int loop_count = 0;

while(!done)
{
va_start(ap, format);

int return_value = vsnprintf(buffer, buffer_size_bytes, format, ap);

if( return_value < 0 )
{
done = true;
}
else if( return_value >= buffer_size_bytes )
{
// Realloc and try again.

buffer_size_bytes = return_value + 1;

char* new_buffer_ptr = (char*)realloc(buffer, buffer_size_bytes);

if( new_buffer_ptr == NULL )
{
done = true;
}
else
{
buffer = new_buffer_ptr;
}
}
else
{
result = buffer;
done = true;
}

va_end(ap);

if( ++loop_count > 5 )
done = true;
}

free(buffer);

return result;
}

string
usb_sysfs_hw_string(const string& sysfs_path)
{
string serial_number = read_line( sysfs_path + "/serial" );

if( serial_number.length() > 0 )
{
serial_number = format( "SNR=%s", serial_number.c_str() );
}

string vid = read_line( sysfs_path + "/idVendor" );

string pid = read_line( sysfs_path + "/idProduct" );

return format("USB VID:PID=%s:%s %s", vid.c_str(), pid.c_str(), serial_number.c_str() );
}

vector<PortInfo>
serial::list_ports()
{
vector<PortInfo> results;

vector<string> search_globs;
search_globs.push_back("/dev/ttyACM*");
search_globs.push_back("/dev/ttyS*");
search_globs.push_back("/dev/ttyUSB*");
search_globs.push_back("/dev/tty.*");
search_globs.push_back("/dev/cu.*");
search_globs.push_back("/dev/rfcomm*");

vector<string> devices_found = glob( search_globs );

vector<string>::iterator iter = devices_found.begin();

while( iter != devices_found.end() )
{
string device = *iter++;

vector<string> sysfs_info = get_sysfs_info( device );

string friendly_name = sysfs_info[0];

string hardware_id = sysfs_info[1];

PortInfo device_entry;
device_entry.port = device;
device_entry.description = friendly_name;
device_entry.hardware_id = hardware_id;

results.push_back( device_entry );

}

return results;
}

#endif // defined(__linux__)
286 changes: 286 additions & 0 deletions 3rdparty/serial/src/impl/list_ports/list_ports_osx.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,286 @@
#if defined(__APPLE__)

#include <sys/param.h>
#include <stdint.h>

#include <CoreFoundation/CoreFoundation.h>
#include <IOKit/IOKitLib.h>
#include <IOKit/serial/IOSerialKeys.h>
#include <IOKit/IOBSD.h>

#include <iostream>
#include <string>
#include <vector>

#include "serial/serial.h"

using serial::PortInfo;
using std::string;
using std::vector;

#define HARDWARE_ID_STRING_LENGTH 128

string cfstring_to_string( CFStringRef cfstring );
string get_device_path( io_object_t& serial_port );
string get_class_name( io_object_t& obj );
io_registry_entry_t get_parent_iousb_device( io_object_t& serial_port );
string get_string_property( io_object_t& device, const char* property );
uint16_t get_int_property( io_object_t& device, const char* property );
string rtrim(const string& str);

string
cfstring_to_string( CFStringRef cfstring )
{
char cstring[MAXPATHLEN];
string result;

if( cfstring )
{
Boolean success = CFStringGetCString( cfstring,
cstring,
sizeof(cstring),
kCFStringEncodingASCII );

if( success )
result = cstring;
}

return result;
}

string
get_device_path( io_object_t& serial_port )
{
CFTypeRef callout_path;
string device_path;

callout_path = IORegistryEntryCreateCFProperty( serial_port,
CFSTR(kIOCalloutDeviceKey),
kCFAllocatorDefault,
0 );

if (callout_path)
{
if( CFGetTypeID(callout_path) == CFStringGetTypeID() )
device_path = cfstring_to_string( static_cast<CFStringRef>(callout_path) );

CFRelease(callout_path);
}

return device_path;
}

string
get_class_name( io_object_t& obj )
{
string result;
io_name_t class_name;
kern_return_t kern_result;

kern_result = IOObjectGetClass( obj, class_name );

if( kern_result == KERN_SUCCESS )
result = class_name;

return result;
}

io_registry_entry_t
get_parent_iousb_device( io_object_t& serial_port )
{
io_object_t device = serial_port;
io_registry_entry_t parent = 0;
io_registry_entry_t result = 0;
kern_return_t kern_result = KERN_FAILURE;
string name = get_class_name(device);

// Walk the IO Registry tree looking for this devices parent IOUSBDevice.
while( name != "IOUSBDevice" )
{
kern_result = IORegistryEntryGetParentEntry( device,
kIOServicePlane,
&parent );

if(kern_result != KERN_SUCCESS)
{
result = 0;
break;
}

device = parent;

name = get_class_name(device);
}

if(kern_result == KERN_SUCCESS)
result = device;

return result;
}

string
get_string_property( io_object_t& device, const char* property )
{
string property_name;

if( device )
{
CFStringRef property_as_cfstring = CFStringCreateWithCString (
kCFAllocatorDefault,
property,
kCFStringEncodingASCII );

CFTypeRef name_as_cfstring = IORegistryEntryCreateCFProperty(
device,
property_as_cfstring,
kCFAllocatorDefault,
0 );

if( name_as_cfstring )
{
if( CFGetTypeID(name_as_cfstring) == CFStringGetTypeID() )
property_name = cfstring_to_string( static_cast<CFStringRef>(name_as_cfstring) );

CFRelease(name_as_cfstring);
}

if(property_as_cfstring)
CFRelease(property_as_cfstring);
}

return property_name;
}

uint16_t
get_int_property( io_object_t& device, const char* property )
{
uint16_t result = 0;

if( device )
{
CFStringRef property_as_cfstring = CFStringCreateWithCString (
kCFAllocatorDefault,
property,
kCFStringEncodingASCII );

CFTypeRef number = IORegistryEntryCreateCFProperty( device,
property_as_cfstring,
kCFAllocatorDefault,
0 );

if(property_as_cfstring)
CFRelease(property_as_cfstring);

if( number )
{
if( CFGetTypeID(number) == CFNumberGetTypeID() )
{
bool success = CFNumberGetValue( static_cast<CFNumberRef>(number),
kCFNumberSInt16Type,
&result );

if( !success )
result = 0;
}

CFRelease(number);
}

}

return result;
}

string rtrim(const string& str)
{
string result = str;

string whitespace = " \t\f\v\n\r";

std::size_t found = result.find_last_not_of(whitespace);

if (found != std::string::npos)
result.erase(found+1);
else
result.clear();

return result;
}

vector<PortInfo>
serial::list_ports(void)
{
vector<PortInfo> devices_found;
CFMutableDictionaryRef classes_to_match;
io_iterator_t serial_port_iterator;
io_object_t serial_port;
mach_port_t master_port;
kern_return_t kern_result;

kern_result = IOMasterPort(MACH_PORT_NULL, &master_port);

if(kern_result != KERN_SUCCESS)
return devices_found;

classes_to_match = IOServiceMatching(kIOSerialBSDServiceValue);

if (classes_to_match == NULL)
return devices_found;

CFDictionarySetValue( classes_to_match,
CFSTR(kIOSerialBSDTypeKey),
CFSTR(kIOSerialBSDAllTypes) );

kern_result = IOServiceGetMatchingServices(master_port, classes_to_match, &serial_port_iterator);

if (KERN_SUCCESS != kern_result)
return devices_found;

while ( (serial_port = IOIteratorNext(serial_port_iterator)) )
{
string device_path = get_device_path( serial_port );
io_registry_entry_t parent = get_parent_iousb_device( serial_port );
IOObjectRelease(serial_port);

if( device_path.empty() )
continue;

PortInfo port_info;
port_info.port = device_path;
port_info.description = "n/a";
port_info.hardware_id = "n/a";

string device_name = rtrim( get_string_property( parent, "USB Product Name" ) );
string vendor_name = rtrim( get_string_property( parent, "USB Vendor Name") );
string description = rtrim( vendor_name + " " + device_name );
if( !description.empty() )
port_info.description = description;

string serial_number = rtrim(get_string_property( parent, "USB Serial Number" ) );
uint16_t vendor_id = get_int_property( parent, "idVendor" );
uint16_t product_id = get_int_property( parent, "idProduct" );

if( vendor_id && product_id )
{
char cstring[HARDWARE_ID_STRING_LENGTH];

if(serial_number.empty())
serial_number = "None";

int ret = snprintf( cstring, HARDWARE_ID_STRING_LENGTH, "USB VID:PID=%04x:%04x SNR=%s",
vendor_id,
product_id,
serial_number.c_str() );

if( (ret >= 0) && (ret < HARDWARE_ID_STRING_LENGTH) )
port_info.hardware_id = cstring;
}

devices_found.push_back(port_info);
}

IOObjectRelease(serial_port_iterator);
return devices_found;
}

#endif // defined(__APPLE__)
152 changes: 152 additions & 0 deletions 3rdparty/serial/src/impl/list_ports/list_ports_win.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,152 @@
#if defined(_WIN32)

/*
* Copyright (c) 2014 Craig Lilley <cralilley@gmail.com>
* This software is made available under the terms of the MIT licence.
* A copy of the licence can be obtained from:
* http://opensource.org/licenses/MIT
*/

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

using serial::PortInfo;
using std::vector;
using std::string;

static const DWORD port_name_max_length = 256;
static const DWORD friendly_name_max_length = 256;
static const DWORD hardware_id_max_length = 256;

// Convert a wide Unicode string to an UTF8 string
std::string utf8_encode(const std::wstring &wstr)
{
int size_needed = WideCharToMultiByte(CP_UTF8, 0, &wstr[0], (int)wstr.size(), NULL, 0, NULL, NULL);
std::string strTo( size_needed, 0 );
WideCharToMultiByte (CP_UTF8, 0, &wstr[0], (int)wstr.size(), &strTo[0], size_needed, NULL, NULL);
return strTo;
}

vector<PortInfo>
serial::list_ports()
{
vector<PortInfo> devices_found;

HDEVINFO device_info_set = SetupDiGetClassDevs(
(const GUID *) &GUID_DEVCLASS_PORTS,
NULL,
NULL,
DIGCF_PRESENT);

unsigned int device_info_set_index = 0;
SP_DEVINFO_DATA device_info_data;

device_info_data.cbSize = sizeof(SP_DEVINFO_DATA);

while(SetupDiEnumDeviceInfo(device_info_set, device_info_set_index, &device_info_data))
{
device_info_set_index++;

// Get port name

HKEY hkey = SetupDiOpenDevRegKey(
device_info_set,
&device_info_data,
DICS_FLAG_GLOBAL,
0,
DIREG_DEV,
KEY_READ);

TCHAR port_name[port_name_max_length];
DWORD port_name_length = port_name_max_length;

LONG return_code = RegQueryValueEx(
hkey,
_T("PortName"),
NULL,
NULL,
(LPBYTE)port_name,
&port_name_length);

RegCloseKey(hkey);

if(return_code != EXIT_SUCCESS)
continue;

if(port_name_length > 0 && port_name_length <= port_name_max_length)
port_name[port_name_length-1] = '\0';
else
port_name[0] = '\0';

// Ignore parallel ports

if(_tcsstr(port_name, _T("LPT")) != NULL)
continue;

// Get port friendly name

TCHAR friendly_name[friendly_name_max_length];
DWORD friendly_name_actual_length = 0;

BOOL got_friendly_name = SetupDiGetDeviceRegistryProperty(
device_info_set,
&device_info_data,
SPDRP_FRIENDLYNAME,
NULL,
(PBYTE)friendly_name,
friendly_name_max_length,
&friendly_name_actual_length);

if(got_friendly_name == TRUE && friendly_name_actual_length > 0)
friendly_name[friendly_name_actual_length-1] = '\0';
else
friendly_name[0] = '\0';

// Get hardware ID

TCHAR hardware_id[hardware_id_max_length];
DWORD hardware_id_actual_length = 0;

BOOL got_hardware_id = SetupDiGetDeviceRegistryProperty(
device_info_set,
&device_info_data,
SPDRP_HARDWAREID,
NULL,
(PBYTE)hardware_id,
hardware_id_max_length,
&hardware_id_actual_length);

if(got_hardware_id == TRUE && hardware_id_actual_length > 0)
hardware_id[hardware_id_actual_length-1] = '\0';
else
hardware_id[0] = '\0';

#ifdef UNICODE
std::string portName = utf8_encode(port_name);
std::string friendlyName = utf8_encode(friendly_name);
std::string hardwareId = utf8_encode(hardware_id);
#else
std::string portName = port_name;
std::string friendlyName = friendly_name;
std::string hardwareId = hardware_id;
#endif

PortInfo port_entry;
port_entry.port = portName;
port_entry.description = friendlyName;
port_entry.hardware_id = hardwareId;

devices_found.push_back(port_entry);
}

SetupDiDestroyDeviceInfoList(device_info_set);

return devices_found;
}

#endif // #if defined(_WIN32)
1,084 changes: 1,084 additions & 0 deletions 3rdparty/serial/src/impl/unix.cc

Large diffs are not rendered by default.

646 changes: 646 additions & 0 deletions 3rdparty/serial/src/impl/win.cc

Large diffs are not rendered by default.

432 changes: 432 additions & 0 deletions 3rdparty/serial/src/serial.cc

Large diffs are not rendered by default.

34 changes: 34 additions & 0 deletions scripts/src/osd/modules.lua
Original file line number Diff line number Diff line change
@@ -101,6 +101,7 @@ function osdmodulesbuild()
MAME_DIR .. "src/osd/modules/input/input_xinput.h",
MAME_DIR .. "src/osd/modules/lib/osdobj_common.cpp",
MAME_DIR .. "src/osd/modules/lib/osdobj_common.h",
MAME_DIR .. "src/osd/modules/lib/osdlib.h",
MAME_DIR .. "src/osd/modules/midi/midi_module.h",
MAME_DIR .. "src/osd/modules/midi/none.cpp",
MAME_DIR .. "src/osd/modules/midi/portmidi.cpp",
@@ -119,6 +120,7 @@ function osdmodulesbuild()
MAME_DIR .. "src/osd/modules/output/network.cpp",
MAME_DIR .. "src/osd/modules/output/none.cpp",
MAME_DIR .. "src/osd/modules/output/output_module.h",
MAME_DIR .. "src/osd/modules/output/serial.cpp",
MAME_DIR .. "src/osd/modules/output/win32_output.cpp",
MAME_DIR .. "src/osd/modules/output/win32_output.h",
MAME_DIR .. "src/osd/modules/render/blit13.ipp",
@@ -137,10 +139,12 @@ function osdmodulesbuild()
MAME_DIR .. "src/osd/modules/sound/sdl_sound.cpp",
MAME_DIR .. "src/osd/modules/sound/sound_module.h",
MAME_DIR .. "src/osd/modules/sound/xaudio2_sound.cpp",
MAME_DIR .. "3rdparty/serial/src/serial.cc",
}
includedirs {
MAME_DIR .. "src/osd",
ext_includedir("asio"),
MAME_DIR .. "3rdparty/serial/include",
}

if _OPTIONS["gcc"]~=nil and string.find(_OPTIONS["gcc"], "clang") then
@@ -159,8 +163,38 @@ function osdmodulesbuild()
includedirs {
MAME_DIR .. "3rdparty/compat/winsdk-override",
}

files {
MAME_DIR .. "3rdparty/serial/src/impl/win.cc",
MAME_DIR .. "3rdparty/serial/src/impl/list_ports/list_ports_win.cc",
}
else
files {
MAME_DIR .. "3rdparty/serial/src/impl/unix.cc",
MAME_DIR .. "3rdparty/serial/src/impl/list_ports/list_ports_linux.cc",
}
if _OPTIONS["targetos"]=="macosx" then
files {
MAME_DIR .. "3rdparty/serial/src/impl/list_ports/list_ports_osx.cc",
}
end
end

-- Disable warnings for third-party serial library
configuration { "3rdparty/serial/**" }
buildoptions {
"-Wno-error",
"-Wno-unused-parameter",
"-Wno-redefine",
"-Wno-macro-redefined",
"-Wno-redundant-decls",
}
defines {
"__STDC_WANT_LIB_EXT1__=1",
"NOMINMAX", -- Prevent Windows min/max macro definitions
}
configuration { }

if _OPTIONS["NO_OPENGL"]=="1" then
defines {
"USE_OPENGL=0",
5 changes: 5 additions & 0 deletions src/osd/modules/lib/osdobj_common.cpp
Original file line number Diff line number Diff line change
@@ -46,6 +46,10 @@ const options_entry osd_options::s_option_entries[] =
{ nullptr, nullptr, core_options::option_type::HEADER, "OSD OUTPUT OPTIONS" },
{ OSD_OUTPUT_PROVIDER, OSDOPTVAL_AUTO, core_options::option_type::STRING, "provider for output notifications: " },

{ nullptr, nullptr, core_options::option_type::HEADER, "OSD SERIAL OPTIONS" },
{ OSDOPTION_SERIAL_PORT, "COM1", core_options::option_type::STRING, "serial port path (e.g. COM1 on Windows or /dev/ttyS0 on Linux)" },
{ OSDOPTION_SERIAL_BAUDRATE, "9600", core_options::option_type::INTEGER, "serial port baud rate (default: 9600)" },

{ nullptr, nullptr, core_options::option_type::HEADER, "OSD INPUT OPTIONS" },
{ OSD_KEYBOARDINPUT_PROVIDER, OSDOPTVAL_AUTO, core_options::option_type::STRING, "provider for keyboard input: " },
{ OSD_MOUSEINPUT_PROVIDER, OSDOPTVAL_AUTO, core_options::option_type::STRING, "provider for mouse input: " },
@@ -326,6 +330,7 @@ void osd_common_t::register_options()
REGISTER_MODULE(m_mod_man, OUTPUT_CONSOLE);
REGISTER_MODULE(m_mod_man, OUTPUT_NETWORK);
REGISTER_MODULE(m_mod_man, OUTPUT_WIN32);
REGISTER_MODULE(m_mod_man, OUTPUT_SERIAL);


// after initialization we know which modules are supported
10 changes: 10 additions & 0 deletions src/osd/modules/lib/osdobj_common.h
Original file line number Diff line number Diff line change
@@ -92,6 +92,9 @@

#define OSDOPTION_NETWORK_PROVIDER "networkprovider"

#define OSDOPTION_SERIAL_PORT "serial_port"
#define OSDOPTION_SERIAL_BAUDRATE "serial_baudrate"

#define OSDOPTION_BGFX_PATH "bgfx_path"
#define OSDOPTION_BGFX_BACKEND "bgfx_backend"
#define OSDOPTION_BGFX_DEBUG "bgfx_debug"
@@ -187,6 +190,13 @@ class osd_options : public emu_options
const char *pa_device() const { return value(OSDOPTION_PA_DEVICE); }
float pa_latency() const { return float_value(OSDOPTION_PA_LATENCY); }

// serial options
const char *serial_port() const { return value(OSDOPTION_SERIAL_PORT); }
int serial_baudrate() const { return int_value(OSDOPTION_SERIAL_BAUDRATE); }

// MIDI options
const char *midi_provider() const { return value(OSDOPTION_MIDI_PROVIDER); }

static const options_entry s_option_entries[];
};

119 changes: 119 additions & 0 deletions src/osd/modules/output/serial.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,119 @@
// license:BSD-3-Clause
// copyright-holders:Nadav Waisbrod
/***************************************************************************
serial.cpp
Serial output interface.
***************************************************************************/

#include "output_module.h"
#include "modules/osdmodule.h"
#include "osdcore.h"
#include "modules/lib/osdobj_common.h" // For osd_options definition
#include "serial/serial.h"
#include "util/strformat.h"

#include <memory>
#include <string>
#include <stdexcept>

namespace osd {

namespace {

class output_serial : public osd_module, public output_module
{
public:
output_serial() :
osd_module(OSD_OUTPUT_PROVIDER, "serial"),
m_serial(nullptr),
m_port(""),
m_baudrate(9600)
{
}

virtual ~output_serial()
{
close_port();
}

virtual int init(osd_interface &osd, const osd_options &options) override
{
// Get port and baudrate from options
m_port = options.serial_port();
m_baudrate = options.serial_baudrate();

if (m_port.empty())
{
osd_printf_warning("Serial output: No port specified, using COM1\n");
m_port = "COM1";
}

// Try to open the port
open_port();

return 0;
}

virtual void exit() override
{
close_port();
}

// output_module
virtual void notify(const char *outname, int32_t value) override
{
if (!m_serial || !m_serial->isOpen())
return;

try {
// Format a message to send over serial
std::string message = util::string_format("%s=%d\n", (outname == nullptr) ? "none" : outname, value);
m_serial->write(message);
}
catch (const std::exception &e) {
osd_printf_error("Serial output error: %s\n", e.what());
close_port();
}
}

private:
void open_port()
{
try {
close_port(); // Close if already open
m_serial = std::make_unique<serial::Serial>(m_port, m_baudrate);
osd_printf_info("Serial output: Connected to %s at %d baud\n", m_port.c_str(), m_baudrate);
}
catch (const std::exception &e) {
osd_printf_error("Serial output: Failed to open %s - %s\n", m_port.c_str(), e.what());
}
}

void close_port()
{
if (m_serial && m_serial->isOpen())
{
try {
m_serial->close();
osd_printf_info("Serial output: Disconnected from %s\n", m_port.c_str());
}
catch (const std::exception &e) {
osd_printf_error("Serial output: Error closing port - %s\n", e.what());
}
}
m_serial.reset();
}

std::unique_ptr<serial::Serial> m_serial;
std::string m_port;
uint32_t m_baudrate;
};

} // anonymous namespace

} // namespace osd

MODULE_DEFINITION(OUTPUT_SERIAL, osd::output_serial)