First Commit

This commit is contained in:
Justin Hammond 2015-10-14 23:12:36 +08:00
parent c1cf11b7c9
commit a7996e3c6c
40 changed files with 11973 additions and 0 deletions

20
config/LMB.conf Normal file
View file

@ -0,0 +1,20 @@
{
"serial": {
"port": "\/dev\/ttyUSB0"
},
"debug": {
"level": "0"
},
"username": "Admin",
"password": "password",
"enablepass": "enable",
"monitorpath": "\/tmp\/test\/",
"startupmessage": {
"1": "\\DW - \\DD\/\\DL\/\\DY - \\DH:\\DM:\\DS",
"2": "No Messages ~S",
"3": "test message 3",
"4": "",
"5": "",
"6": ""
}
}

View file

@ -0,0 +1,85 @@
Pos 00 01 02 03 04 05 06 07 08
M1 Data 00 02 31 06 00 31 31 45 06
M2 Data XX 02 31 07 00 31 32 45 06
M3 Data XX 02 31 08 00 31 33 45 06
M4 Data XX 02 31 09 00 31 34 45 07
M5 Data XX 02 31 0A 00 31 35 42 06
M6 Data XX 02 31 0B 00 31 36 42 06
00 - Start
01 - Seems to be Command? 0x02
02 - Program? 0x31
03 - Memory Position In Decimal. Start at 06
04 - Start Position (every 64 bytes send new packet)
05 - Speed?
06 - Memory Position in Ascii
07 - Special Effect (A - E)
08 - Length of String
09 - Data (60 Bytes Chunk? - 64 Bytes from Pos 04)
68 - CRC?
Follow Up with
Pos 00 01 02 03
M1 Data 02 31 08 40
Then
Pos 00 01 02 03
M1 Data 02 31 08 80
Then
Pos 00 01 02 03
M1 Data 02 31 08 C0
Exit?
Pos 00 01 02 03
Data 02 33 3F
00 - Command? - 0x02
01 - Run? 0x33
02 - Which Programs to Run (Bitmask) eg 07 0b00000111 or 1F 0b00011111
Program Time
Pos 00 01 02 03 04 05 06 07 08 09
Data 02 34 15 03 18 22 22 48 03 F3
00 - Command - 0x02
01 - Program Time - 34
02 - Year
03 - Month
04 - Day
05 - Hour
06 - Min
07 - Sec
08 - Day of Week
09 - CRC?
012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789
00 02 31 06 00 35 00 42 D2 30 31 32 33 34 35 36
37 38 39 30 31 32 33 34 35 36 37 38 39 30 31 32
33 34 35 36 37 38 39 30 31 32 33 34 35 36 37 38
39 30 31 32 33 34 35 36 37 38 39 30 31 32 33 34
35 36 37 38 39 CE 02 31 06 40 30 31 32 33 34 35
36 37 38 39 30 31 32 33 34 35 36 37 38 39 30 31
32 33 34 35 36 37 38 39 30 31 32 33 34 35 36 37
38 39 30 31 32 33 34 35 36 37 38 39 30 31 32 33
34 35 36 37 38 39 30 31 32 33 8B 02 31 06 80 34
35 36 37 38 39 30 31 32 33 34 35 36 37 38 39 30
31 32 33 34 35 36 37 38 39 30 31 32 33 34 35 36
37 38 39 30 31 32 33 34 35 36 37 38 39 30 31 32
33 34 35 36 37 38 39 30 31 32 33 34 35 36 37 DB
02 31 06 C0 38 39 30 31 32 33 34 35 36 37 38 39
30 31 32 33 34 35 36 37 38 39 00 00 00 00 00 00
00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
00 00 00 00 82 02 33 01

4
scripts/zabbix_stats.cfg Normal file
View file

@ -0,0 +1,4 @@
[Server]
zabbixurl = http://zabbix/zabbix/
username = admin
password = password

135
scripts/zabbix_stats.py Executable file
View file

@ -0,0 +1,135 @@
#!/usr/bin/python
"""
Shows a list of all current issues (AKA tripped triggers)
"""
from getpass import getpass
from pyzabbix import ZabbixAPI
from hurry.filesize import size
import numpy as np
import time
import pprint
import re
import ConfigParser
pp = pprint.PrettyPrinter(indent=4)
config = ConfigParser.ConfigParser()
config.read('zabbix_stats.cfg')
# The hostname at which the Zabbix web interface is available
ZABBIX_SERVER = 'http://10.1.1.7/zabbix/'
zapi = ZabbixAPI(config.get('Server', 'zabbixurl'))
zapi.session.verify = False
# Login to the Zabbix API
zapi.login(config.get('Server', 'username'), config.get('Server', 'password'))
def Stats():
BWVals = {}
results = {}
BWVals['MikroTik'] = {'key':{'if*Octets[Wan2 - Client]', 'if*Octets[Wan1 - Server]'}, 'limit':10}
BWVals['R510'] = {'key':{'system.cpu.util[,idle]', 'DellTemp[1]'}, 'limit':10}
BWVals['nx5010'] = {'key':{'nexus.temp3','nexus.temp1'}, 'limit':1}
BWVals['H2216-N3'] = {'key':{'system.cpu.util[,idle]', 'bb_inlet_temp', 'exit_air_temp'}, 'limit':10}
BWVals['H2216-N2'] = {'key':{'system.cpu.util[,idle]'}, 'limit':10}
BWVals['H2216-N1'] = {'key':{'system.cpu.util[,idle]'}, 'limit':10}
for vals in BWVals:
hosts = zapi.host.get(filter=({'name':vals}),
output=['hostid', 'name'],
)
results[vals] = {}
for key in BWVals[vals]['key']:
items = zapi.item.get(hostids=hosts[0]['hostid'],
search=({"key_":key}),
searchWildcardsEnabled=1,
output=['name', 'key_'],
)
for t in items:
results[vals][t['key_']] = {}
asfloat=0
results[vals][t['key_']]['values'] = zapi.history.get(itemids=t['itemid'],
limit=int(BWVals[vals]['limit']),
sortfield='clock',
sortorder='DESC',
)
if not len(results[vals][t['key_']]['values']):
results[vals][t['key_']]['values'] = zapi.history.get(itemids=t['itemid'],
limit=int(BWVals[vals]['limit']),
history=0,
sortfield='clock',
sortorder='DESC',
)
values = []
# pp.pprint(vals + " " + t['key_'])
for s in results[vals][t['key_']]['values']:
# pp.pprint(time.strftime('%Y-%m-%d %H:%M:%S', time.localtime(float(s['clock']))))
if re.match("system.cpu.util.*", t['key_']):
s['value'] = 100 - float(s['value'])
values.append(float(s['value']))
results[vals][t['key_']]['average'] = np.average(values)
# pp.pprint(results)
stats = str("WAN Stats - Server: {0}bps/{1}bps - Client: {2}bps/{3}bps Server Load: R510 - {4:.0f}% H2216-N1 - {5:.0f}% H2216-N2 - {6:.0f}% H2216-N3 - {7:.0f}% Temps: Nexus - {8:.0f}/{9:.0f} R510 - {10:.0f} H2216 - {11:.0f}/{12:.0f}"
.format(size(results['MikroTik']['ifInOctets[Wan1 - Server]']['average']),
size(results['MikroTik']['ifOutOctets[Wan1 - Server]']['average']),
size(results['MikroTik']['ifInOctets[Wan2 - Client]']['average']),
size(results['MikroTik']['ifOutOctets[Wan2 - Client]']['average']),
results['R510']['system.cpu.util[,idle]']['average'],
results['H2216-N1']['system.cpu.util[,idle]']['average'],
results['H2216-N2']['system.cpu.util[,idle]']['average'],
results['H2216-N3']['system.cpu.util[,idle]']['average'],
results['nx5010']['nexus.temp3']['average'],
results['nx5010']['nexus.temp1']['average'],
results['R510']['DellTemp[1]']['average'],
results['H2216-N3']['bb_inlet_temp']['average'],
results['H2216-N3']['exit_air_temp']['average'],
))
f1=open('/tmp/test/message-2', 'w')
if len(stats) > 0:
f1.write(stats)
f1.close()
def Alerts():
# Get a list of all issues (AKA tripped triggers)
triggers = zapi.trigger.get(only_true=1,
skipDependent=1,
monitored=1,
active=1,
output='extend',
expandDescription=1,
expandData='host',
withLastEventUnacknowledged=1,
sortfield='priority',
sortorder='DESC',
)
prioritylist = ['N/A', 'Info', 'Warn', 'Average', 'High', 'Critical']
errorlist = []
for t in triggers:
if (int(t['value']) == 1 and int(t['priority']) > 1):
errorlist.append(str("Error:{0}, Priority:{1}".format(
t['description'],
prioritylist[int(t['priority'])])
))
for i in range(0, len(errorlist),3):
Stats()
f1=open('/tmp/test/message-3', 'w')
if i < len(errorlist):
f1.write(errorlist[i])
f1.close()
f2=open('/tmp/test/message-4', 'w')
if i+1 < len(errorlist):
f2.write(errorlist[i+1])
f2.close()
f3=open('/tmp/test/message-5', 'w')
if i+2 < len(errorlist):
f3.write(errorlist[i+2])
f3.close()
time.sleep(60)
while(1):
Alerts()

275
src/Driver-DX.cpp Normal file
View file

@ -0,0 +1,275 @@
/* LED Message Board - Driver-DX.cpp
** Copyright (c) 2014 Justin Hammond
**
** This program is free software; you can redistribute it and/or modify
** it under the terms of the GNU General Public License as published by
** the Free Software Foundation; either version 2 of the License, or
** (at your option) any later version.
**
** This program is distributed in the hope that it will be useful,
** but WITHOUT ANY WARRANTY; without even the implied warranty of
** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
** GNU General Public License for more details.
**
** You should have received a copy of the GNU General Public License
** along with this program; if not, write to the Free Software
** Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
** USA
**
** LED Message Board SVN Identification:
** $Rev$
*/
/** @file Driver-DX.cpp
* @brief
*/
#include <iostream>
#include "Driver-DX.hpp"
void doChkSum(uint8_t *data, uint8_t length) {
uint8_t csum = 0;
int i;
for (i = 1; i < length; i++) {
csum += data[i];
}
BOOST_LOG_TRIVIAL(trace) << "csum " << std::hex << (int)csum << " old " << std::hex << (int)data[i];
data[i] = csum;
}
void insertMessage(uint8_t part, uint8_t *data, std::string message) {
uint8_t startpos = 4;
uint8_t size = 60;
uint8_t messagestart = 0;
BOOST_LOG_TRIVIAL(trace) << "message length: " << std::dec << message.length();
if (part == 1) {
startpos = 8;
data[7] = message.length();
} else if (part == 2) {
messagestart = 60;
size = 65;
} else if (part == 3) {
messagestart = 124;
size = 65;
} else if (part == 4) {
messagestart = 188;
size = 65;
}
if ((message.length() - messagestart) < size)
size = (message.length() - messagestart);
BOOST_LOG_TRIVIAL(trace) << "size " << (int)message.length() << std::dec << " StartPos: " << (int)startpos << " size " << (int)size << " messagestart " << (int)messagestart ;
if (size > message.length())
return;
if (messagestart > message.length())
return;
for (int i = 0; i < size; i++) {
data[startpos++] = message.at(i + messagestart);
}
}
void printMessage(uint8_t *data, size_t length) {
std::stringstream ss;
BOOST_LOG_TRIVIAL(trace) << "Raw Message: ";
for (unsigned int i = 0; i < length; ++i) {
ss << std::setw(2) << std::uppercase << std::hex << std::setfill('0') << (int)data[i] << " ";
if ((i+1) % 16 == 0) {
BOOST_LOG_TRIVIAL(trace) << ss.str();
ss.str("");
}
}
if (ss.gcount() > 0)
BOOST_LOG_TRIVIAL(trace) << ss.str();
}
void setMessageNumber(uint8_t *data, uint8_t pos, bool firstmsg) {
data[2] = (uint8_t)5+pos;
}
bool Driver_DX::Init(LMBCTX *lmbctx) {
this->lmbctx = lmbctx;
if (this->lmbctx->sp == NULL && this->lmbctx->port.length() != 0) {
this->lmbctx->sp = new serial::Serial(this->lmbctx->port, 38400, serial::Timeout::simpleTimeout(10));
} else if (this->lmbctx->sp == NULL){
return false;
}
if (!this->lmbctx->sp->isOpen()) {
this->lmbctx->sp->open();
}
this->lmbctx->sp->setFlowcontrol(serial::flowcontrol_software);
this->lmbctx->sp->setRTS(true);
this->lmbctx->sp->setDTR(true);
/* construct the Message Header */
BOOST_LOG_TRIVIAL(trace) << "Port Open: " << this->lmbctx->sp->isOpen();
std::vector<uint8_t> data;
uint8_t start[] = { 0x54 };
this->lmbctx->sp->write(start, 1);
this->lmbctx->sp->flush();
std::string query = this->lmbctx->sp->read(1);
BOOST_LOG_TRIVIAL(trace) << "Reply to Inquiry: " << std::hex << query.at(1);
BOOST_LOG_TRIVIAL(trace) << "Send Out Start Sequence";
this->lmbctx->sp->flush();
start[0] = 0x41;
this->lmbctx->sp->write(start, 1);
start[0] = 0x42;
this->lmbctx->sp->write(start, 1);
start[0] = 0x43;
this->lmbctx->sp->write(start, 1);
start[0] = 0x44;
this->lmbctx->sp->write(start, 1);
start[0] = 0x45;
this->lmbctx->sp->write(start, 1);
this->lmbctx->sp->flush();
usleep(150 * 1000);
return true;
}
#include <boost/algorithm/string/replace.hpp>
bool Driver_DX::setMessage(int pos, std::string message) {
if (this->lmbctx->messages < (unsigned int)pos) {
BOOST_LOG_TRIVIAL(warning) << "Invalid Message Position " << pos;
return false;
}
if (message.length() > 250) {
BOOST_LOG_TRIVIAL(warning) << "Message is " << std::dec << message.length() << " chars long. Trimming to 250 Chars";
message = message.substr(0, 250);
}
if (message.length() > 0) {
this->lmbctx->displayedmsgs[pos] = message;
this->lmbctx->msgdisplay[pos-1] = 1;
//boost::replace_all(message, "~+", "\xc0\x00");
boost::replace_all(message, "~H", "\xc0\x02");
boost::replace_all(message, "~<", "\xc0\x04");
boost::replace_all(message, "~>", "\xc0\x06");
boost::replace_all(message, "~P", "\xc0\x08");
boost::replace_all(message, "~x", "\xc0\x0A");
boost::replace_all(message, "~S", "\xc0\x0C");
boost::replace_all(message, "~O", "\xc0\x0E");
boost::replace_all(message, "~o", "\xc0\x10");
boost::replace_all(message, "~M", "\xc0\x12");
boost::replace_all(message, "~Q", "\xc0\x14");
boost::replace_all(message, "~n", "\xc0\x16");
uint8_t header1[] = { 0x02, 0x31, 0x06, 0x00, 0x35, 0x00, 0x42, 0x00, 0x00, 0x00, 0x00, 0x00, 00, 00, 00, 00, 00, 00, 00, 00, 00, 00, 00, 00, 00, 00, 00, 00, 00, 00, 00, 00, 00, 00, 00, 00, 00, 00, 00, 00, 00, 00, 00, 00, 00, 00, 00, 00, 00, 00, 00, 00, 00, 00, 00, 00, 00, 00, 00, 00, 00, 00, 00, 00, 00, 00, 00, 00, 0x72};
insertMessage(1, &header1[0], message);
setMessageNumber(&header1[0], pos, true);
doChkSum(&header1[0], 68);
printMessage(header1, 70);
uint8_t header2[] = { 0x02, 0x31, 0x06, 0x40, 00, 00, 00, 00, 00, 00, 00, 00, 00, 00, 00, 00, 00, 00, 00, 00, 00, 00, 00, 00, 00, 00, 00, 00, 00, 00, 00, 00, 00, 00, 00, 00, 00, 00, 00, 00, 00, 00, 00, 00, 00, 00, 00, 00, 00, 00, 00, 00, 00, 00, 00, 00, 00, 00, 00, 00, 00, 00, 00, 00, 00, 00, 00, 00, 0x71};
insertMessage(2, &header2[0], message);
setMessageNumber(&header2[0], pos, false);
doChkSum(&header2[0], 68);
printMessage(header2, 69);
uint8_t header3[] = { 0x02, 0x31, 0x06, 0x80, 00, 00, 00, 00, 00, 00, 00, 00, 00, 00, 00, 00, 00, 00, 00, 00, 00, 00, 00, 00, 00, 00, 00, 00, 00, 00, 00, 00, 00, 00, 00, 00, 00, 00, 00, 00, 00, 00, 00, 00, 00, 00, 00, 00, 00, 00, 00, 00, 00, 00, 00, 00, 00, 00, 00, 00, 00, 00, 00, 00, 00, 00, 00, 00, 0xB7};
insertMessage(3, &header3[0], message);
setMessageNumber(&header3[0], pos, false);
doChkSum(&header3[0], 68);
printMessage(header3, 69);
uint8_t header4[] = { 0x02, 0x31, 0x06, 0xC0, 00, 00, 00, 00, 00, 00, 00, 00, 00, 00, 00, 00, 00, 00, 00, 00, 00, 00, 00, 00, 00, 00, 00, 00, 00, 00, 00, 00, 00, 00, 00, 00, 00, 00, 00, 00, 00, 00, 00, 00, 00, 00, 00, 00, 00, 00, 00, 00, 00, 00, 00, 00, 00, 00, 00, 00, 00, 00, 00, 00, 00, 00, 00, 00, 0xF7};
insertMessage(4, &header4[0], message);
setMessageNumber(&header4[0], pos, false);
doChkSum(&header4[0], 68);
printMessage(header4, 69);
uint8_t *finalpck = (uint8_t*)malloc(281);
finalpck[0] = 0x00;
for (int i = 0; i < 69; i++)
finalpck[1+i] = header1[i];
for (int i = 0; i < 69; i++)
finalpck[1+69+i] = header2[i];
for (int i = 0; i < 69; i++)
finalpck[1+69+69+i] = header3[i];
for (int i = 0; i < 69; i++)
finalpck[1+69+69+69+i] = header4[i];
printMessage(finalpck, 277);
BOOST_LOG_TRIVIAL(trace) << "sending packets: ";
int sent = (int)this->lmbctx->sp->write(header1, 69);
BOOST_LOG_TRIVIAL(trace) << "size: " << std::dec << sent;
usleep(100 * 1000);
sent = (int)this->lmbctx->sp->write(header2, 69);
BOOST_LOG_TRIVIAL(trace) << "size: " << std::dec << sent;
usleep(100 * 1000);
sent = (int)this->lmbctx->sp->write(header3, 69);
BOOST_LOG_TRIVIAL(trace) << "size: " << std::dec << sent;
usleep(100 * 1000);
sent =(int)this->lmbctx->sp->write(header4, 69);
BOOST_LOG_TRIVIAL(trace) << "size: " << std::dec << sent;
usleep(100 * 1000);
} else {
BOOST_LOG_TRIVIAL(trace) << "Clearing Message " << pos;
this->lmbctx->displayedmsgs[pos] = "";
this->lmbctx->msgdisplay[pos-1] = 0;
}
uint8_t header5[] = { 0x02, 0x33, (uint8_t)this->lmbctx->msgdisplay.to_ulong() };
int sent = (int)this->lmbctx->sp->write(header5, 3);
BOOST_LOG_TRIVIAL(trace) << "size: " << std::dec << sent;
BOOST_LOG_TRIVIAL(info) << "Set Message " << pos << " to " << this->lmbctx->displayedmsgs[pos];
return true;
}
bool Driver_DX::Fini() {
usleep(150 * 1000);
lmbctx->sp->close();
return true;
}
#include <boost/lexical_cast.hpp>
class uint_from_hex // For use with boost::lexical_cast
{
unsigned int value;
public:
operator unsigned int() const { return value; }
friend std::istream& operator>>( std::istream& in, uint_from_hex& outValue )
{
in >> std::hex >> outValue.value;
}
};
#define STOH(x) boost::lexical_cast<uint_from_hex>(x)
uint8_t InsertString(unsigned int value) {
std::stringstream ss;
ss << "0x" << std::setfill('0') << std::setw(2) << value;
return (STOH(ss.str()) & 0xFF);
}
bool Driver_DX::StartUp() {
std::stringstream ss;
time_t t = time(NULL);
tm* timePtr = localtime(&t);
uint8_t setTime[] = { 0x02, 0x34, InsertString(timePtr->tm_year -100), InsertString(timePtr->tm_mon +1), InsertString(timePtr->tm_mday), InsertString(timePtr->tm_hour), InsertString(timePtr->tm_min), InsertString(timePtr->tm_sec), InsertString(timePtr->tm_wday), 0x00 };
doChkSum(&setTime[0], 9);
int sent = (int)this->lmbctx->sp->write(setTime, 10);
BOOST_LOG_TRIVIAL(trace) << "Set Clock: " << std::dec << sent;
return true;
}

49
src/Driver-DX.hpp Normal file
View file

@ -0,0 +1,49 @@
/* LED Message Board - Driver-DX.hpp
** Copyright (c) 2014 Justin Hammond
**
** This program is free software; you can redistribute it and/or modify
** it under the terms of the GNU General Public License as published by
** the Free Software Foundation; either version 2 of the License, or
** (at your option) any later version.
**
** This program is distributed in the hope that it will be useful,
** but WITHOUT ANY WARRANTY; without even the implied warranty of
** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
** GNU General Public License for more details.
**
** You should have received a copy of the GNU General Public License
** along with this program; if not, write to the Free Software
** Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
** USA
**
** LED Message Board SVN Identification:
** $Rev$
*/
/** @file Driver-DX.hpp
* @brief
*/
#ifndef SRC_DRIVER_DX_HPP_
#define SRC_DRIVER_DX_HPP_
#include "Driver.hpp"
class Driver_DX : public iDriver {
public:
Driver_DX() {};
~Driver_DX() {};
bool Init(LMBCTX *lmbctx);
bool Fini();
bool setMessage(int pos, std::string message);
virtual bool StartUp();
private:
LMBCTX *lmbctx;
};
#endif /* SRC_DRIVER_DX_HPP_ */

51
src/Driver.hpp Normal file
View file

@ -0,0 +1,51 @@
/* LED Message Board - Driver.hpp
** Copyright (c) 2014 Justin Hammond
**
** This program is free software; you can redistribute it and/or modify
** it under the terms of the GNU General Public License as published by
** the Free Software Foundation; either version 2 of the License, or
** (at your option) any later version.
**
** This program is distributed in the hope that it will be useful,
** but WITHOUT ANY WARRANTY; without even the implied warranty of
** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
** GNU General Public License for more details.
**
** You should have received a copy of the GNU General Public License
** along with this program; if not, write to the Free Software
** Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
** USA
**
** LED Message Board SVN Identification:
** $Rev$
*/
/** @file Driver.hpp
* @brief
*/
#ifndef SRC_DRIVER_HPP_
#define SRC_DRIVER_HPP_
#include "lmbd.hpp"
class iDriver {
public:
iDriver() {}
virtual ~iDriver() {}
virtual bool Init(LMBCTX *lmbctx) = 0;
virtual bool Fini() = 0;
virtual bool setMessage(int pos, std::string message) = 0;
virtual bool StartUp() =0;
};
#endif /* SRC_DRIVER_HPP_ */

92
src/FileMonitor.cpp Normal file
View file

@ -0,0 +1,92 @@
/* LED Message Board - FileMonitor.cpp
** Copyright (c) 2014 Justin Hammond
**
** This program is free software; you can redistribute it and/or modify
** it under the terms of the GNU General Public License as published by
** the Free Software Foundation; either version 2 of the License, or
** (at your option) any later version.
**
** This program is distributed in the hope that it will be useful,
** but WITHOUT ANY WARRANTY; without even the implied warranty of
** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
** GNU General Public License for more details.
**
** You should have received a copy of the GNU General Public License
** along with this program; if not, write to the Free Software
** Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
** USA
**
** LED Message Board SVN Identification:
** $Rev$
*/
/** @file FileMonitor.cpp
* @brief
*/
#include <boost/filesystem/fstream.hpp>
#include <boost/lexical_cast.hpp>
#include "FileMonitor.hpp"
#include "Driver.hpp"
Inotify inotify(IN_CLOSE_WRITE);
void MonitorFiles(LMBCTX *lmbctx) {
while(true) {
FileSystemEvent event = inotify.getNextEvent();
BOOST_LOG_TRIVIAL(debug) << "Event wd(" << event.wd << ") " << event.getMaskString() << " for " << event.path << " was triggered!";
boost::filesystem::ifstream ifs;
ifs.open(event.path, std::ios::in);
std::stringstream ss;
ss << ifs.rdbuf();
std::string message(ss.str());
message.erase(std::remove(message.begin(), message.end(), '\n'), message.end());
// int messageid = boost::lexical_cast<int>(event.path.generic_string().at(event.path.generic_string().length()-1));
int messageid = event.wd;
BOOST_LOG_TRIVIAL(info) << "Message in Slot " << messageid << " was " << message;
{
boost::unique_lock<boost::mutex> scoped_lock(lmbctx->io_mutex);
lmbctx->driver->Init(lmbctx);
lmbctx->driver->setMessage(messageid, message);
lmbctx->driver->Fini();
}
}
}
bool FileMonitor::init(LMBCTX *lmbctx) {
boost::unique_lock<boost::mutex> scoped_lock(lmbctx->io_mutex);
boost::filesystem::path monpath(lmbctx->monitorpath);
if(fs::exists(monpath)) {
if(fs::is_directory(monpath)) {
for (fs::directory_iterator end_dir_it, it(monpath); it!=end_dir_it; ++it) {
fs::remove_all(it->path());
}
for (unsigned int i = 1; i <= lmbctx->messages; i++) {
char filename[255];
snprintf(filename, 255, "message-%d", i);
boost::filesystem::path messagefile = monpath;
messagefile /= filename;
BOOST_LOG_TRIVIAL(info) << "Creating Message File: " << messagefile;
std::ofstream f{messagefile.c_str(), std::ios::app};
f.close();
inotify.watchFile(messagefile);
}
} else {
BOOST_LOG_TRIVIAL(error) << "Monitor Path is not a directory";
return false;
}
} else {
BOOST_LOG_TRIVIAL(error) << "Monitor Path does not exist";
return false;
}
/* Now Start up our Monitoring Thread */
boost::thread(MonitorFiles, boost::ref(lmbctx));
return true;
}

45
src/FileMonitor.hpp Normal file
View file

@ -0,0 +1,45 @@
/* LED Message Board - FileMonitor.hpp
** Copyright (c) 2014 Justin Hammond
**
** This program is free software; you can redistribute it and/or modify
** it under the terms of the GNU General Public License as published by
** the Free Software Foundation; either version 2 of the License, or
** (at your option) any later version.
**
** This program is distributed in the hope that it will be useful,
** but WITHOUT ANY WARRANTY; without even the implied warranty of
** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
** GNU General Public License for more details.
**
** You should have received a copy of the GNU General Public License
** along with this program; if not, write to the Free Software
** Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
** USA
**
** LED Message Board SVN Identification:
** $Rev$
*/
/** @file FileMonitor.hpp
* @brief
*/
#ifndef SRC_FILEMONITOR_HPP_
#define SRC_FILEMONITOR_HPP_
#include <iNotifyEngine.hpp>
#include <iNotifyEvent.hpp>
#include <boost/filesystem.hpp>
#include "lmbd.hpp"
class FileMonitor {
public:
FileMonitor() {};
~FileMonitor() {};
bool init(LMBCTX *lmbctx);
};
#endif /* SRC_FILEMONITOR_HPP_ */

412
src/iNotifyEngine.hpp Normal file
View file

@ -0,0 +1,412 @@
/* LED Message Board - iNotifyEngine.hpp
** Copyright (c) 2014 Justin Hammond
**
** This program is free software; you can redistribute it and/or modify
** it under the terms of the GNU General Public License as published by
** the Free Software Foundation; either version 2 of the License, or
** (at your option) any later version.
**
** This program is distributed in the hope that it will be useful,
** but WITHOUT ANY WARRANTY; without even the implied warranty of
** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
** GNU General Public License for more details.
**
** You should have received a copy of the GNU General Public License
** along with this program; if not, write to the Free Software
** Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
** USA
**
** LED Message Board SVN Identification:
** $Rev$
*/
/** @file iNotifyEngine.hpp
* @brief
*/
#ifndef SRC_INOTIFYENGINE_HPP_
#define SRC_INOTIFYENGINE_HPP_
/**
* @file Inotify.h
* @author Erik Zenker
* @date 02.11.2012
* @copyright Gnu Public License
**/
#pragma once
#include <sys/inotify.h>
#include <string>
#include <queue>
#include <map>
#include <vector>
#include <boost/filesystem.hpp>
#include <assert.h>
#include <errno.h>
#include <time.h>
#include <errno.h>
#include <string>
#include <exception>
#include <sstream>
#include <boost/filesystem.hpp>
#include <iNotifyEvent.hpp>
#define MAX_EVENTS 4096
#define EVENT_SIZE (sizeof (inotify_event))
#define EVENT_BUF_LEN (MAX_EVENTS * (EVENT_SIZE + 16))
namespace fs = boost::filesystem;
/**
* @brief C++ wrapper for linux inotify interface
* @class Inotify
* Inotify.h
* "include/Inotify.h"
*
* folders will be watched by watchFolderRecursively or
* files by watchFile. If there are changes inside this
* folder or files events will be raised. This events
* can be get by getNextEvent.
*
* @eventMask
*
* IN_ACCESS File was accessed (read) (*).
* IN_ATTRIB Metadata changedfor example, permissions,
* timestamps, extended attributes, link count
* (since Linux 2.6.25), UID, or GID. (*).
* IN_CLOSE_WRITE File opened for writing was closed (*).
* IN_CLOSE_NOWRITE File not opened for writing was closed (*).
* IN_CREATE File/directory created in watched directory(*).
* IN_DELETE File/directory deleted from watched directory(*).
* IN_DELETE_SELF Watched file/directory was itself deleted.
* IN_MODIFY File was modified (*).
* IN_MOVE_SELF Watched file/directory was itself moved.
* IN_MOVED_FROM Generated for the directory containing the old
* filename when a file is renamed (*).
* IN_MOVED_TO Generated for the directory containing the new
* filename when a file is renamed (*).
* IN_OPEN File was opened (*).
* IN_ALL_EVENTS macro is defined as a bit mask of all of the above
* events
* IN_MOVE IN_MOVED_FROM|IN_MOVED_TO
* IN_CLOSE IN_CLOSE_WRITE | IN_CLOSE_NOWRITE
*
* See inotify manpage for more event details
*
*/
class Inotify {
public:
Inotify();
Inotify(uint32_t eventMask);
Inotify(std::vector< std::string> ignoredDirectories, unsigned eventTimeout, uint32_t eventMask);
~Inotify();
void watchDirectoryRecursively(fs::path path);
void watchFile(fs::path file);
void ignoreFileOnce(fs::path file);
FileSystemEvent getNextEvent();
int getLastErrno();
private:
fs::path wdToPath(int wd);
bool isIgnored(std::string file);
bool onTimeout(time_t eventTime);
void removeWatch(int wd); // TODO
void init();
// Member
int mError;
time_t mEventTimeout;
time_t mLastEventTime;
uint32_t mEventMask;
std::vector<std::string> mIgnoredDirectories;
std::vector<std::string> mOnceIgnoredDirectories;
std::queue<FileSystemEvent> mEventQueue;
std::map<int, fs::path> mDirectorieMap;
int mInotifyFd;
};
inline Inotify::Inotify() :
mError(0),
mEventTimeout(0),
mLastEventTime(0),
mEventMask(IN_ALL_EVENTS),
mIgnoredDirectories(std::vector<std::string>()),
mInotifyFd(0){
// Initialize inotify
init();
}
inline Inotify::Inotify(uint32_t eventMask) :
mError(0),
mEventTimeout(0),
mLastEventTime(0),
mEventMask(eventMask),
mIgnoredDirectories(std::vector<std::string>()),
mInotifyFd(0){
// Initialize inotify
init();
}
inline Inotify::Inotify(std::vector<std::string> ignoredDirectories, unsigned eventTimeout, uint32_t eventMask) :
mError(0),
mEventTimeout(eventTimeout),
mLastEventTime(0),
mEventMask(eventMask),
mIgnoredDirectories(ignoredDirectories),
mInotifyFd(0){
// Initialize inotify
init();
}
inline Inotify::~Inotify(){
if(!close(mInotifyFd)){
mError = errno;
}
}
inline void Inotify::init(){
mInotifyFd = inotify_init();
if(mInotifyFd == -1){
mError = errno;
std::stringstream errorStream;
errorStream << "Can't initialize inotify ! " << strerror(mError) << ".";
throw std::runtime_error(errorStream.str());
}
}
/**
* @brief Adds the given path and all files and subdirectories
* to the set of watched files/directories.
* Symlinks will be followed!
*
* @param path that will be watched recursively
*
*/
inline void Inotify::watchDirectoryRecursively(fs::path path){
if(fs::exists(path)){
if(fs::is_directory(path)){
fs::recursive_directory_iterator it(path, fs::symlink_option::recurse);
fs::recursive_directory_iterator end;
while(it != end){
fs::path currentPath = *it;
if(fs::is_directory(currentPath)){
watchFile(currentPath);
}
if(fs::is_symlink(currentPath)){
watchFile(currentPath);
}
it++;
}
}
watchFile(path);
}
else {
throw std::invalid_argument("Can´t watch Path! Path does not exist. Path: " + path.string());
}
}
/**
* @brief Adds a single file/directorie to the list of
* watches. Path and corresponding watchdescriptor
* will be stored in the directorieMap. This is done
* because events on watches just return this
* watchdescriptor.
*
* @param path that will be watched
*
*/
inline void Inotify::watchFile(fs::path filePath){
if(fs::exists(filePath)){
mError = 0;
int wd = 0;
if(!isIgnored(filePath.string())){
wd = inotify_add_watch(mInotifyFd, filePath.string().c_str(), mEventMask);
}
if(wd == -1){
mError = errno;
std::stringstream errorStream;
if(mError == 28){
errorStream << "Failed to watch! " << strerror(mError) << ". Please increase number of watches in \"/proc/sys/fs/inotify/max_user_watches\".";
throw std::runtime_error(errorStream.str());
}
errorStream << "Failed to watch! " << strerror(mError) << ". Path: " << filePath.string();
throw std::runtime_error(errorStream.str());
}
mDirectorieMap[wd] = filePath;
}
}
inline void Inotify::ignoreFileOnce(fs::path file){
mOnceIgnoredDirectories.push_back(file.string());
}
/**
* @brief Removes watch from set of watches. This
* is not done recursively!
*
* @param wd watchdescriptor
*
*/
inline void Inotify::removeWatch(int wd){
int result = inotify_rm_watch(mInotifyFd, wd);
if(result == -1){
mError = errno;
std::stringstream errorStream;
errorStream << "Failed to remove watch! " << strerror(mError) << ".";
throw std::runtime_error(errorStream.str());
}
mDirectorieMap.erase(wd);
}
inline fs::path Inotify::wdToPath(int wd){
return mDirectorieMap[wd];
}
/**
* @brief Blocking wait on new events of watched files/directories
* specified on the eventmask. FileSystemEvents
* will be returned one by one. Thus this
* function can be called in some while(true)
* loop.
*
* @return A new FileSystemEvent
*
*/
inline FileSystemEvent Inotify::getNextEvent(){
int length = 0;
char buffer[EVENT_BUF_LEN];
time_t currentEventTime = time(NULL);
std::vector<FileSystemEvent> events;
// Read Events from fd into buffer
while(mEventQueue.empty()){
length = 0;
memset(&buffer, 0, EVENT_BUF_LEN);
while(length <= 0 ){
length = read(mInotifyFd, buffer, EVENT_BUF_LEN);
currentEventTime = time(NULL);
if(length == -1){
mError = errno;
if(mError != EINTR){
continue;
}
}
}
// Read events from buffer into queue
currentEventTime = time(NULL);
int i = 0;
while(i < length){
inotify_event *event = ((struct inotify_event*) &buffer[i]);
fs::path path(wdToPath(event->wd)); /* / std::string(event->name)); */
if(fs::is_directory(path)){
event->mask |= IN_ISDIR;
}
FileSystemEvent fsEvent(event->wd, event->mask, path);
if(!fsEvent.path.empty()){
events.push_back(fsEvent);
}
else{
// Event is not complete --> ignore
}
i += EVENT_SIZE + event->len;
}
// Filter events
for(auto eventIt = events.begin(); eventIt < events.end(); ++eventIt){
FileSystemEvent currentEvent = *eventIt;
if(onTimeout(currentEventTime)){
events.erase(eventIt);
}
else if(isIgnored(currentEvent.path.string())){
events.erase(eventIt);
}
else{
mLastEventTime = currentEventTime;
mEventQueue.push(currentEvent);
}
}
}
// Return next event
FileSystemEvent event = mEventQueue.front();
mEventQueue.pop();
return event;
}
inline int Inotify::getLastErrno(){
return mError;
}
inline bool Inotify::isIgnored(std::string file){
if(mIgnoredDirectories.empty() and mOnceIgnoredDirectories.empty()){
return false;
}
for(unsigned i = 0; i < mOnceIgnoredDirectories.size(); ++i){
size_t pos = file.find(mOnceIgnoredDirectories[i]);
if(pos!= std::string::npos){
mOnceIgnoredDirectories.erase(mOnceIgnoredDirectories.begin() + i);
return true;
}
}
for(unsigned i = 0; i < mIgnoredDirectories.size(); ++i){
size_t pos = file.find(mIgnoredDirectories[i]);
if(pos!= std::string::npos){
return true;
}
}
return false;
}
inline bool Inotify::onTimeout(time_t eventTime){
return (mLastEventTime + mEventTimeout) > eventTime;
}
#endif /* SRC_INOTIFYENGINE_HPP_ */

135
src/iNotifyEvent.hpp Normal file
View file

@ -0,0 +1,135 @@
/* LED Message Board - iNotifyEvent.hpp
** Copyright (c) 2014 Justin Hammond
**
** This program is free software; you can redistribute it and/or modify
** it under the terms of the GNU General Public License as published by
** the Free Software Foundation; either version 2 of the License, or
** (at your option) any later version.
**
** This program is distributed in the hope that it will be useful,
** but WITHOUT ANY WARRANTY; without even the implied warranty of
** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
** GNU General Public License for more details.
**
** You should have received a copy of the GNU General Public License
** along with this program; if not, write to the Free Software
** Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
** USA
**
** LED Message Board SVN Identification:
** $Rev$
*/
/** @file iNotifyEvent.hpp
* @brief
*/
#ifndef SRC_INOTIFYEVENT_HPP_
#define SRC_INOTIFYEVENT_HPP_
/**
* @file FileSystemEvent.h
* @author Erik Zenker
* @date 01.11.2012
* @copyright Gnu Public License
**/
#pragma once
#include <string>
#include <boost/filesystem.hpp>
#include <sys/inotify.h>
/**
* @brief Container for events on filesystem.
* @class FileSystemEvent
* FileSystemEvent.h
* "include/FileSystemEvent.h"
*
*
**/
class FileSystemEvent {
public:
FileSystemEvent(int wd, uint32_t mask, const boost::filesystem::path path);
~FileSystemEvent();
std::string getMaskString() const;
/* Member */
bool isRecursive;
int wd;
uint32_t mask;
boost::filesystem::path path;
private:
std::string maskToString(uint32_t events) const;
};
inline FileSystemEvent::FileSystemEvent(const int wd, uint32_t mask, const boost::filesystem::path path) :
isRecursive(false),
wd(wd),
mask(mask),
path(path){
}
inline FileSystemEvent::~FileSystemEvent(){
}
inline std::string FileSystemEvent::getMaskString() const{
return maskToString(mask);
}
inline std::string FileSystemEvent::maskToString(uint32_t mask) const{
std::string maskString = "";
if(IN_ACCESS & mask)
maskString.append("IN_ACCESS ");
if(IN_ATTRIB & mask)
maskString.append("IN_ATTRIB ");
if(IN_CLOSE_WRITE & mask)
maskString.append("IN_CLOSE_WRITE ");
if(IN_CLOSE_NOWRITE & mask)
maskString.append("IN_CLOSE_NOWRITE ");
if(IN_CREATE & mask)
maskString.append("IN_CREATE ");
if(IN_DELETE & mask)
maskString.append("IN_DELETE ");
if(IN_DELETE_SELF & mask)
maskString.append("IN_DELETE_SELF ");
if(IN_MODIFY & mask)
maskString.append("IN_MODIFY ");
if(IN_MOVE_SELF & mask)
maskString.append("IN_MOVE_SELF ");
if(IN_MOVED_FROM & mask)
maskString.append("IN_MOVED_FROM ");
if(IN_MOVED_TO & mask)
maskString.append("IN_MOVED_TO ");
if(IN_OPEN & mask)
maskString.append("IN_OPEN ");
if(IN_ISDIR & mask)
maskString.append("IN_ISDIR ");
if(IN_UNMOUNT & mask)
maskString.append("IN_UNMOUNT ");
if(IN_Q_OVERFLOW & mask)
maskString.append("IN_Q_OVERFLOW ");
if(IN_CLOSE & mask)
maskString.append("IN_CLOSE ");
if(IN_IGNORED & mask)
maskString.append("IN_IGNORED ");
if(IN_ONESHOT & mask)
maskString.append("IN_ONESHOT ");
return maskString;
}
#endif /* SRC_INOTIFYEVENT_HPP_ */

510
src/libcli/COPYING Normal file
View file

@ -0,0 +1,510 @@
GNU LESSER GENERAL PUBLIC LICENSE
Version 2.1, February 1999
Copyright (C) 1991, 1999 Free Software Foundation, Inc.
59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
Everyone is permitted to copy and distribute verbatim copies
of this license document, but changing it is not allowed.
[This is the first released version of the Lesser GPL. It also counts
as the successor of the GNU Library Public License, version 2, hence
the version number 2.1.]
Preamble
The licenses for most software are designed to take away your
freedom to share and change it. By contrast, the GNU General Public
Licenses are intended to guarantee your freedom to share and change
free software--to make sure the software is free for all its users.
This license, the Lesser General Public License, applies to some
specially designated software packages--typically libraries--of the
Free Software Foundation and other authors who decide to use it. You
can use it too, but we suggest you first think carefully about whether
this license or the ordinary General Public License is the better
strategy to use in any particular case, based on the explanations
below.
When we speak of free software, we are referring to freedom of use,
not price. Our General Public Licenses are designed to make sure that
you have the freedom to distribute copies of free software (and charge
for this service if you wish); that you receive source code or can get
it if you want it; that you can change the software and use pieces of
it in new free programs; and that you are informed that you can do
these things.
To protect your rights, we need to make restrictions that forbid
distributors to deny you these rights or to ask you to surrender these
rights. These restrictions translate to certain responsibilities for
you if you distribute copies of the library or if you modify it.
For example, if you distribute copies of the library, whether gratis
or for a fee, you must give the recipients all the rights that we gave
you. You must make sure that they, too, receive or can get the source
code. If you link other code with the library, you must provide
complete object files to the recipients, so that they can relink them
with the library after making changes to the library and recompiling
it. And you must show them these terms so they know their rights.
We protect your rights with a two-step method: (1) we copyright the
library, and (2) we offer you this license, which gives you legal
permission to copy, distribute and/or modify the library.
To protect each distributor, we want to make it very clear that
there is no warranty for the free library. Also, if the library is
modified by someone else and passed on, the recipients should know
that what they have is not the original version, so that the original
author's reputation will not be affected by problems that might be
introduced by others.
Finally, software patents pose a constant threat to the existence of
any free program. We wish to make sure that a company cannot
effectively restrict the users of a free program by obtaining a
restrictive license from a patent holder. Therefore, we insist that
any patent license obtained for a version of the library must be
consistent with the full freedom of use specified in this license.
Most GNU software, including some libraries, is covered by the
ordinary GNU General Public License. This license, the GNU Lesser
General Public License, applies to certain designated libraries, and
is quite different from the ordinary General Public License. We use
this license for certain libraries in order to permit linking those
libraries into non-free programs.
When a program is linked with a library, whether statically or using
a shared library, the combination of the two is legally speaking a
combined work, a derivative of the original library. The ordinary
General Public License therefore permits such linking only if the
entire combination fits its criteria of freedom. The Lesser General
Public License permits more lax criteria for linking other code with
the library.
We call this license the "Lesser" General Public License because it
does Less to protect the user's freedom than the ordinary General
Public License. It also provides other free software developers Less
of an advantage over competing non-free programs. These disadvantages
are the reason we use the ordinary General Public License for many
libraries. However, the Lesser license provides advantages in certain
special circumstances.
For example, on rare occasions, there may be a special need to
encourage the widest possible use of a certain library, so that it
becomes a de-facto standard. To achieve this, non-free programs must
be allowed to use the library. A more frequent case is that a free
library does the same job as widely used non-free libraries. In this
case, there is little to gain by limiting the free library to free
software only, so we use the Lesser General Public License.
In other cases, permission to use a particular library in non-free
programs enables a greater number of people to use a large body of
free software. For example, permission to use the GNU C Library in
non-free programs enables many more people to use the whole GNU
operating system, as well as its variant, the GNU/Linux operating
system.
Although the Lesser General Public License is Less protective of the
users' freedom, it does ensure that the user of a program that is
linked with the Library has the freedom and the wherewithal to run
that program using a modified version of the Library.
The precise terms and conditions for copying, distribution and
modification follow. Pay close attention to the difference between a
"work based on the library" and a "work that uses the library". The
former contains code derived from the library, whereas the latter must
be combined with the library in order to run.
GNU LESSER GENERAL PUBLIC LICENSE
TERMS AND CONDITIONS FOR COPYING, DISTRIBUTION AND MODIFICATION
0. This License Agreement applies to any software library or other
program which contains a notice placed by the copyright holder or
other authorized party saying it may be distributed under the terms of
this Lesser General Public License (also called "this License").
Each licensee is addressed as "you".
A "library" means a collection of software functions and/or data
prepared so as to be conveniently linked with application programs
(which use some of those functions and data) to form executables.
The "Library", below, refers to any such software library or work
which has been distributed under these terms. A "work based on the
Library" means either the Library or any derivative work under
copyright law: that is to say, a work containing the Library or a
portion of it, either verbatim or with modifications and/or translated
straightforwardly into another language. (Hereinafter, translation is
included without limitation in the term "modification".)
"Source code" for a work means the preferred form of the work for
making modifications to it. For a library, complete source code means
all the source code for all modules it contains, plus any associated
interface definition files, plus the scripts used to control
compilation and installation of the library.
Activities other than copying, distribution and modification are not
covered by this License; they are outside its scope. The act of
running a program using the Library is not restricted, and output from
such a program is covered only if its contents constitute a work based
on the Library (independent of the use of the Library in a tool for
writing it). Whether that is true depends on what the Library does
and what the program that uses the Library does.
1. You may copy and distribute verbatim copies of the Library's
complete source code as you receive it, in any medium, provided that
you conspicuously and appropriately publish on each copy an
appropriate copyright notice and disclaimer of warranty; keep intact
all the notices that refer to this License and to the absence of any
warranty; and distribute a copy of this License along with the
Library.
You may charge a fee for the physical act of transferring a copy,
and you may at your option offer warranty protection in exchange for a
fee.
2. You may modify your copy or copies of the Library or any portion
of it, thus forming a work based on the Library, and copy and
distribute such modifications or work under the terms of Section 1
above, provided that you also meet all of these conditions:
a) The modified work must itself be a software library.
b) You must cause the files modified to carry prominent notices
stating that you changed the files and the date of any change.
c) You must cause the whole of the work to be licensed at no
charge to all third parties under the terms of this License.
d) If a facility in the modified Library refers to a function or a
table of data to be supplied by an application program that uses
the facility, other than as an argument passed when the facility
is invoked, then you must make a good faith effort to ensure that,
in the event an application does not supply such function or
table, the facility still operates, and performs whatever part of
its purpose remains meaningful.
(For example, a function in a library to compute square roots has
a purpose that is entirely well-defined independent of the
application. Therefore, Subsection 2d requires that any
application-supplied function or table used by this function must
be optional: if the application does not supply it, the square
root function must still compute square roots.)
These requirements apply to the modified work as a whole. If
identifiable sections of that work are not derived from the Library,
and can be reasonably considered independent and separate works in
themselves, then this License, and its terms, do not apply to those
sections when you distribute them as separate works. But when you
distribute the same sections as part of a whole which is a work based
on the Library, the distribution of the whole must be on the terms of
this License, whose permissions for other licensees extend to the
entire whole, and thus to each and every part regardless of who wrote
it.
Thus, it is not the intent of this section to claim rights or contest
your rights to work written entirely by you; rather, the intent is to
exercise the right to control the distribution of derivative or
collective works based on the Library.
In addition, mere aggregation of another work not based on the Library
with the Library (or with a work based on the Library) on a volume of
a storage or distribution medium does not bring the other work under
the scope of this License.
3. You may opt to apply the terms of the ordinary GNU General Public
License instead of this License to a given copy of the Library. To do
this, you must alter all the notices that refer to this License, so
that they refer to the ordinary GNU General Public License, version 2,
instead of to this License. (If a newer version than version 2 of the
ordinary GNU General Public License has appeared, then you can specify
that version instead if you wish.) Do not make any other change in
these notices.
Once this change is made in a given copy, it is irreversible for
that copy, so the ordinary GNU General Public License applies to all
subsequent copies and derivative works made from that copy.
This option is useful when you wish to copy part of the code of
the Library into a program that is not a library.
4. You may copy and distribute the Library (or a portion or
derivative of it, under Section 2) in object code or executable form
under the terms of Sections 1 and 2 above provided that you accompany
it with the complete corresponding machine-readable source code, which
must be distributed under the terms of Sections 1 and 2 above on a
medium customarily used for software interchange.
If distribution of object code is made by offering access to copy
from a designated place, then offering equivalent access to copy the
source code from the same place satisfies the requirement to
distribute the source code, even though third parties are not
compelled to copy the source along with the object code.
5. A program that contains no derivative of any portion of the
Library, but is designed to work with the Library by being compiled or
linked with it, is called a "work that uses the Library". Such a
work, in isolation, is not a derivative work of the Library, and
therefore falls outside the scope of this License.
However, linking a "work that uses the Library" with the Library
creates an executable that is a derivative of the Library (because it
contains portions of the Library), rather than a "work that uses the
library". The executable is therefore covered by this License.
Section 6 states terms for distribution of such executables.
When a "work that uses the Library" uses material from a header file
that is part of the Library, the object code for the work may be a
derivative work of the Library even though the source code is not.
Whether this is true is especially significant if the work can be
linked without the Library, or if the work is itself a library. The
threshold for this to be true is not precisely defined by law.
If such an object file uses only numerical parameters, data
structure layouts and accessors, and small macros and small inline
functions (ten lines or less in length), then the use of the object
file is unrestricted, regardless of whether it is legally a derivative
work. (Executables containing this object code plus portions of the
Library will still fall under Section 6.)
Otherwise, if the work is a derivative of the Library, you may
distribute the object code for the work under the terms of Section 6.
Any executables containing that work also fall under Section 6,
whether or not they are linked directly with the Library itself.
6. As an exception to the Sections above, you may also combine or
link a "work that uses the Library" with the Library to produce a
work containing portions of the Library, and distribute that work
under terms of your choice, provided that the terms permit
modification of the work for the customer's own use and reverse
engineering for debugging such modifications.
You must give prominent notice with each copy of the work that the
Library is used in it and that the Library and its use are covered by
this License. You must supply a copy of this License. If the work
during execution displays copyright notices, you must include the
copyright notice for the Library among them, as well as a reference
directing the user to the copy of this License. Also, you must do one
of these things:
a) Accompany the work with the complete corresponding
machine-readable source code for the Library including whatever
changes were used in the work (which must be distributed under
Sections 1 and 2 above); and, if the work is an executable linked
with the Library, with the complete machine-readable "work that
uses the Library", as object code and/or source code, so that the
user can modify the Library and then relink to produce a modified
executable containing the modified Library. (It is understood
that the user who changes the contents of definitions files in the
Library will not necessarily be able to recompile the application
to use the modified definitions.)
b) Use a suitable shared library mechanism for linking with the
Library. A suitable mechanism is one that (1) uses at run time a
copy of the library already present on the user's computer system,
rather than copying library functions into the executable, and (2)
will operate properly with a modified version of the library, if
the user installs one, as long as the modified version is
interface-compatible with the version that the work was made with.
c) Accompany the work with a written offer, valid for at least
three years, to give the same user the materials specified in
Subsection 6a, above, for a charge no more than the cost of
performing this distribution.
d) If distribution of the work is made by offering access to copy
from a designated place, offer equivalent access to copy the above
specified materials from the same place.
e) Verify that the user has already received a copy of these
materials or that you have already sent this user a copy.
For an executable, the required form of the "work that uses the
Library" must include any data and utility programs needed for
reproducing the executable from it. However, as a special exception,
the materials to be distributed need not include anything that is
normally distributed (in either source or binary form) with the major
components (compiler, kernel, and so on) of the operating system on
which the executable runs, unless that component itself accompanies
the executable.
It may happen that this requirement contradicts the license
restrictions of other proprietary libraries that do not normally
accompany the operating system. Such a contradiction means you cannot
use both them and the Library together in an executable that you
distribute.
7. You may place library facilities that are a work based on the
Library side-by-side in a single library together with other library
facilities not covered by this License, and distribute such a combined
library, provided that the separate distribution of the work based on
the Library and of the other library facilities is otherwise
permitted, and provided that you do these two things:
a) Accompany the combined library with a copy of the same work
based on the Library, uncombined with any other library
facilities. This must be distributed under the terms of the
Sections above.
b) Give prominent notice with the combined library of the fact
that part of it is a work based on the Library, and explaining
where to find the accompanying uncombined form of the same work.
8. You may not copy, modify, sublicense, link with, or distribute
the Library except as expressly provided under this License. Any
attempt otherwise to copy, modify, sublicense, link with, or
distribute the Library is void, and will automatically terminate your
rights under this License. However, parties who have received copies,
or rights, from you under this License will not have their licenses
terminated so long as such parties remain in full compliance.
9. You are not required to accept this License, since you have not
signed it. However, nothing else grants you permission to modify or
distribute the Library or its derivative works. These actions are
prohibited by law if you do not accept this License. Therefore, by
modifying or distributing the Library (or any work based on the
Library), you indicate your acceptance of this License to do so, and
all its terms and conditions for copying, distributing or modifying
the Library or works based on it.
10. Each time you redistribute the Library (or any work based on the
Library), the recipient automatically receives a license from the
original licensor to copy, distribute, link with or modify the Library
subject to these terms and conditions. You may not impose any further
restrictions on the recipients' exercise of the rights granted herein.
You are not responsible for enforcing compliance by third parties with
this License.
11. If, as a consequence of a court judgment or allegation of patent
infringement or for any other reason (not limited to patent issues),
conditions are imposed on you (whether by court order, agreement or
otherwise) that contradict the conditions of this License, they do not
excuse you from the conditions of this License. If you cannot
distribute so as to satisfy simultaneously your obligations under this
License and any other pertinent obligations, then as a consequence you
may not distribute the Library at all. For example, if a patent
license would not permit royalty-free redistribution of the Library by
all those who receive copies directly or indirectly through you, then
the only way you could satisfy both it and this License would be to
refrain entirely from distribution of the Library.
If any portion of this section is held invalid or unenforceable under
any particular circumstance, the balance of the section is intended to
apply, and the section as a whole is intended to apply in other
circumstances.
It is not the purpose of this section to induce you to infringe any
patents or other property right claims or to contest validity of any
such claims; this section has the sole purpose of protecting the
integrity of the free software distribution system which is
implemented by public license practices. Many people have made
generous contributions to the wide range of software distributed
through that system in reliance on consistent application of that
system; it is up to the author/donor to decide if he or she is willing
to distribute software through any other system and a licensee cannot
impose that choice.
This section is intended to make thoroughly clear what is believed to
be a consequence of the rest of this License.
12. If the distribution and/or use of the Library is restricted in
certain countries either by patents or by copyrighted interfaces, the
original copyright holder who places the Library under this License
may add an explicit geographical distribution limitation excluding those
countries, so that distribution is permitted only in or among
countries not thus excluded. In such case, this License incorporates
the limitation as if written in the body of this License.
13. The Free Software Foundation may publish revised and/or new
versions of the Lesser General Public License from time to time.
Such new versions will be similar in spirit to the present version,
but may differ in detail to address new problems or concerns.
Each version is given a distinguishing version number. If the Library
specifies a version number of this License which applies to it and
"any later version", you have the option of following the terms and
conditions either of that version or of any later version published by
the Free Software Foundation. If the Library does not specify a
license version number, you may choose any version ever published by
the Free Software Foundation.
14. If you wish to incorporate parts of the Library into other free
programs whose distribution conditions are incompatible with these,
write to the author to ask for permission. For software which is
copyrighted by the Free Software Foundation, write to the Free
Software Foundation; we sometimes make exceptions for this. Our
decision will be guided by the two goals of preserving the free status
of all derivatives of our free software and of promoting the sharing
and reuse of software generally.
NO WARRANTY
15. BECAUSE THE LIBRARY IS LICENSED FREE OF CHARGE, THERE IS NO
WARRANTY FOR THE LIBRARY, TO THE EXTENT PERMITTED BY APPLICABLE LAW.
EXCEPT WHEN OTHERWISE STATED IN WRITING THE COPYRIGHT HOLDERS AND/OR
OTHER PARTIES PROVIDE THE LIBRARY "AS IS" WITHOUT WARRANTY OF ANY
KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING, BUT NOT LIMITED TO, THE
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
PURPOSE. THE ENTIRE RISK AS TO THE QUALITY AND PERFORMANCE OF THE
LIBRARY IS WITH YOU. SHOULD THE LIBRARY PROVE DEFECTIVE, YOU ASSUME
THE COST OF ALL NECESSARY SERVICING, REPAIR OR CORRECTION.
16. IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN
WRITING WILL ANY COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MAY MODIFY
AND/OR REDISTRIBUTE THE LIBRARY AS PERMITTED ABOVE, BE LIABLE TO YOU
FOR DAMAGES, INCLUDING ANY GENERAL, SPECIAL, INCIDENTAL OR
CONSEQUENTIAL DAMAGES ARISING OUT OF THE USE OR INABILITY TO USE THE
LIBRARY (INCLUDING BUT NOT LIMITED TO LOSS OF DATA OR DATA BEING
RENDERED INACCURATE OR LOSSES SUSTAINED BY YOU OR THIRD PARTIES OR A
FAILURE OF THE LIBRARY TO OPERATE WITH ANY OTHER SOFTWARE), EVEN IF
SUCH HOLDER OR OTHER PARTY HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH
DAMAGES.
END OF TERMS AND CONDITIONS
How to Apply These Terms to Your New Libraries
If you develop a new library, and you want it to be of the greatest
possible use to the public, we recommend making it free software that
everyone can redistribute and change. You can do so by permitting
redistribution under these terms (or, alternatively, under the terms
of the ordinary General Public License).
To apply these terms, attach the following notices to the library.
It is safest to attach them to the start of each source file to most
effectively convey the exclusion of warranty; and each file should
have at least the "copyright" line and a pointer to where the full
notice is found.
<one line to give the library's name and a brief idea of what it does.>
Copyright (C) <year> <name of author>
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2.1 of the License, or (at your option) any later version.
This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public
License along with this library; if not, write to the Free Software
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
Also add information on how to contact you by electronic and paper mail.
You should also get your employer (if you work as a programmer) or
your school, if any, to sign a "copyright disclaimer" for the library,
if necessary. Here is a sample; alter the names:
Yoyodyne, Inc., hereby disclaims all copyright interest in the
library `Frob' (a library for tweaking knobs) written by James
Random Hacker.
<signature of Ty Coon>, 1 April 1990
Ty Coon, President of Vice
That's all there is to it!

156
src/libcli/README Normal file
View file

@ -0,0 +1,156 @@
libcli
libcli emulates a cisco style telnet command-line interface.
To compile:
make
make install
This will install libcli.so into /usr/local/lib. If you want to change
the location, edit Makefile.
There is a test application built called clitest. Run this and telnet
to port 8000.
By default, a single username and password combination is enabled.
Username: fred
Password: nerk
Get help by entering "help" or hitting ?.
libcli provides support for using the arrow keys for command-line editing. Up
and Down arrows will cycle through the command history, and Left & Right can be
used for editing the current command line.
libcli also works out the shortest way of entering a command, so if you have a
command "show users grep foobar" defined, you can enter "sh us g foobar" if that
is the shortest possible way of doing it.
Enter "sh?" at the command line to get a list of commands starting with "sh"
A few commands are defined in every libcli program:
help
quit
exit
logout
history
Use in your own code:
First of all, make sure you #include <libcli.h> in your C code, and link
with -lcli.
If you have any trouble with this, have a look at clitest.c for a
demonstration.
Start your program off with a cli_init().
This sets up the internal data structures required.
When a user connects, they are presented with a greeting if one is set using the
cli_set_banner(banner) function.
By default, the command-line session is not authenticated, which means users
will get full access as soon as they connect. As this may not be always the best
thing, 2 methods of authentication are available.
First, you can add username / password combinations with the
cli_allow_user(username, password) function. When a user connects, they can
connect with any of these username / password combinations.
Secondly, you can add a callback using the cli_set_auth_callback(callback)
function. This function is passed the username and password as char *, and must
return CLI_OK if the user is to have access and CLI_ERROR if they are not.
The library itself will take care of prompting the user for credentials.
Commands are built using a tree-like structure. You define commands with the
cli_register_command(parent, command, callback, privilege, mode, help) function.
parent is a cli_command * reference to a previously added command. Using a
parent you can build up complex commands.
e.g. to provide commands "show users", "show sessions" and "show people", use
the following sequence:
cli_command *c = cli_register_command(NULL, "show", NULL, PRIVILEGE_UNPRIVILEGED, MODE_EXEC, NULL);
cli_register_command(c, "sessions", fn_sessions, PRIVILEGE_UNPRIVILEGED, MODE_EXEC, "Show the sessions connected");
cli_register_command(c, "users", fn_users, PRIVILEGE_UNPRIVILEGED, MODE_EXEC, "Show the users connected");
cli_register_command(c, "people", fn_people, PRIVILEGE_UNPRIVILEGED, MODE_EXEC, "Show a list of the people I like");
If callback is NULL, the command can be used as part of a tree, but cannot be
individually run.
If you decide later that you don't want a command to be run, you can call
cli_unregister_command(command).
You can use this to build dynamic command trees.
It is possible to carry along a user-defined context to all command callbacks
using cli_set_context(cli, context) and cli_get_context(cli) functions.
Hooks may be defined for default commands (help, quit, logout, exit, history,
enable, disable) as follows:
cli_register_hook(cli, "exit", hook_exit);
The hook is defined the same as a command callback. It must return one of
the following, which defines what libcli will do upon completion of the hook
callback:
CLI_HOOK_CONTINUE -- Continue with default implmentation
CLI_HOOK_STOP -- Return CLI_OK immediately upon completion
CLI_HOOK_ERROR -- Return CLI_ERROR immediately upon completion
Command-specific completion:
It is possible to specify a callback for processing tab-completion on
parameter values using the following:
void cli_register_completion_cb(struct cli_command *cmd, int (*callback)(struct cli_def *, const char *, char **, int, char **, int));
The completion callback is defined as follows:
int completion_cb(struct cli_def *cli, const char *command, char *argv[], int argc, char **completion_buf, int max_completions)
In order to define a completion callback, a function must be defined to free()
memory that it allocates. Do this via:
void cli_register_completion_free(struct cli_def *cli, void (*callback)(char **, int));
The completion-free callback is defined as follows:
void completion_free_cb(char **completion_buf, int num_completions)
You are responsible for accepting a TCP connection, and for creating a
process or thread to run the cli. Once you are ready to process the
connection, call cli_loop(cli, sock) to interact with the user on the
given socket.
This function will return when the user exits the cli, either by breaking the
connection or entering "quit".
Call cli_done() to free the data structures.
- David Parrish (david@dparrish.com)
Fork: https://github.com/kevkeys/libcli
Maintainer: Kevin Barrett <kevin@baymicrosystems.com>
Changelog:
2014-Dec-05 Fork from https://github.com/dparrish/libcli
ID a61be59b8b7c40b3acf1dc4acaafc247da22e5d3
2014-Dec-07 Add support for hooks for default commands
2014-Dec-07 Add command-specific completion

2610
src/libcli/libcli.c Normal file

File diff suppressed because it is too large Load diff

154
src/libcli/libcli.h Normal file
View file

@ -0,0 +1,154 @@
#ifndef __LIBCLI_H__
#define __LIBCLI_H__
// vim:sw=4 tw=120 et
#ifdef __cplusplus
extern "C" {
#endif
#include <stdio.h>
#include <stdarg.h>
#include <sys/time.h>
#define CLI_OK 0
#define CLI_ERROR -1
#define CLI_QUIT -2
#define CLI_ERROR_ARG -3
#define CLI_HOOK_CONTINUE 0
#define CLI_HOOK_STOP 1
#define CLI_HOOK_ERROR 2
#define MAX_HISTORY 256
#define PRIVILEGE_UNPRIVILEGED 0
#define PRIVILEGE_PRIVILEGED 15
#define MODE_ANY -1
#define MODE_EXEC 0
#define MODE_CONFIG 1
#define LIBCLI_HAS_ENABLE 1
#define PRINT_PLAIN 0
#define PRINT_FILTERED 0x01
#define PRINT_BUFFERED 0x02
#define CLI_MAX_LINE_LENGTH 4096
#define CLI_MAX_LINE_WORDS 128
struct cli_def {
int completion_callback;
struct cli_command *commands;
int (*auth_callback)(struct cli_def *cli, const char *, const char *);
int (*regular_callback)(struct cli_def *cli);
int (*enable_callback)(const char *);
char *banner;
struct unp *users;
char *enable_password;
char *history[MAX_HISTORY];
char showprompt;
char *promptchar;
char *hostname;
char *modestring;
int privilege;
int mode;
int state;
struct cli_filter *filters;
void (*print_callback)(struct cli_def *cli, const char *string);
FILE *client;
/* internal buffers */
void *conn;
void *service;
char *commandname; // temporary buffer for cli_command_name() to prevent leak
char *buffer;
unsigned buf_size;
struct timeval timeout_tm;
time_t idle_timeout;
int (*idle_timeout_callback)(struct cli_def *);
time_t last_action;
int telnet_protocol;
void *hooks;
void (*user_completion_free)(char **, int);
char *request_prompt;
int request_prior_state;
int (*request_callback)(struct cli_def *, const char *);
int (*request_completion_cb)(struct cli_def *, const char *, char **, int);
void (*request_abort_cb)(struct cli_def *);
int (*config_mode_callback)(struct cli_def *);
void *user_context;
};
struct cli_filter {
int (*filter)(struct cli_def *cli, const char *string, void *data);
void *data;
struct cli_filter *next;
};
struct cli_command {
char *command;
int (*callback)(struct cli_def *, const char *, char **, int);
unsigned int unique_len;
char *help;
int privilege;
int mode;
int (*get_completions)(struct cli_def *, const char *, char **, int, char **, int);
struct cli_command *next;
struct cli_command *children;
struct cli_command *parent;
};
struct cli_def *cli_init();
int cli_done(struct cli_def *cli);
struct cli_command *cli_register_command(struct cli_def *cli, struct cli_command *parent, const char *command,
int (*callback)(struct cli_def *, const char *, char **, int), int privilege,
int mode, const char *help);
int cli_unregister_command(struct cli_def *cli, const char *command);
int cli_run_command(struct cli_def *cli, const char *command);
int cli_loop(struct cli_def *cli, int sockfd);
int cli_file(struct cli_def *cli, FILE *fh, int privilege, int mode);
void cli_set_auth_callback(struct cli_def *cli, int (*auth_callback)(struct cli_def *cli, const char *, const char *));
void cli_set_enable_callback(struct cli_def *cli, int (*enable_callback)(const char *));
void cli_allow_user(struct cli_def *cli, const char *username, const char *password);
void cli_allow_enable(struct cli_def *cli, const char *password);
void cli_deny_user(struct cli_def *cli, const char *username);
void cli_set_banner(struct cli_def *cli, const char *banner);
void cli_set_hostname(struct cli_def *cli, const char *hostname);
void cli_set_promptchar(struct cli_def *cli, const char *promptchar);
void cli_set_modestring(struct cli_def *cli, const char *modestring);
int cli_set_privilege(struct cli_def *cli, int privilege);
int cli_set_configmode(struct cli_def *cli, int mode, const char *config_desc);
void cli_reprompt(struct cli_def *cli);
void cli_regular(struct cli_def *cli, int (*callback)(struct cli_def *cli));
void cli_regular_interval(struct cli_def *cli, int seconds);
void cli_print(struct cli_def *cli, const char *format, ...) __attribute__((format (printf, 2, 3)));
void cli_bufprint(struct cli_def *cli, const char *format, ...) __attribute__((format (printf, 2, 3)));
void cli_vabufprint(struct cli_def *cli, const char *format, va_list ap);
void cli_error(struct cli_def *cli, const char *format, ...) __attribute__((format (printf, 2, 3)));
void cli_print_callback(struct cli_def *cli, void (*callback)(struct cli_def *, const char *));
void cli_free_history(struct cli_def *cli);
void cli_set_idle_timeout(struct cli_def *cli, unsigned int seconds);
void cli_set_idle_timeout_callback(struct cli_def *cli, unsigned int seconds, int (*callback)(struct cli_def *));
int cli_register_hook(struct cli_def *cli, const char *command,
int (*hook)(struct cli_def *, const char *, char **, int));
void cli_register_completion_cb(struct cli_command *cmd,
int (*callback)(struct cli_def *, const char *, char **, int, char **, int));
void cli_register_completion_free(struct cli_def *cli, void (*callback)(char **, int));
void cli_register_configmode_cb(struct cli_def *cli, int (*callback)(struct cli_def *));
// Enable or disable telnet protocol negotiation.
// Note that this is enabled by default and must be changed before cli_loop() is run.
void cli_telnet_protocol(struct cli_def *cli, int telnet_protocol);
// Set/get user context
void cli_set_context(struct cli_def *cli, void *context);
void *cli_get_context(struct cli_def *cli);
// Prompt user for information
int cli_request(struct cli_def *cli, int (*callback)(struct cli_def *, const char *), int (*completion_cb)(struct cli_def *, const char *, char **, int), void (*abort_cb)(struct cli_def *), const char *format, ...);
#ifdef __cplusplus
}
#endif
#endif

156
src/libcli/libcli.spec Normal file
View file

@ -0,0 +1,156 @@
Version: 1.9.7
Summary: Cisco-like telnet command-line library
Name: libcli
Release: 1
License: LGPL
Group: Library/Communication
Source: %{name}-%{version}.tar.gz
URL: http://code.google.com/p/libcli
Packager: David Parrish <david@dparrish.com>
BuildRoot: %{_tmppath}/%{name}-%{version}-%(%__id -un)
%description
libcli provides a shared library for including a Cisco-like command-line
interface into other software. It's a telnet interface which supports
command-line editing, history, authentication and callbacks for a
user-definable function tree.
%prep
%setup
%build
make
%install
rm -rf $RPM_BUILD_ROOT
make DESTDIR=$RPM_BUILD_ROOT PREFIX=/usr install
find $RPM_BUILD_ROOT/usr ! -type d -print | grep -v '\/(README|\.html)$' | \
sed "s@^$RPM_BUILD_ROOT@@g" | sed "s/^\(.*\)$/\1\*/" > %{name}-%{version}-filelist
%post
ldconfig
%clean
rm -rf $RPM_BUILD_ROOT
%files -f %{name}-%{version}-filelist
%defattr(-, root, root)
%changelog
* Mon Feb 1 2010 David Parrish <david@dparrish.com> 1.9.7-1
- Fix memory leak in cli_get_completions - fengxj325@gmail.com
* Tue Jun 5 2012 Teemu Karimerto <teemu.karimerto@steo.fi> 1.9.6-1
- Added a user-definable context to struct cli_def
- Added cli_set_context/cli_get_context for user context handling
- Added a test for user context
* Mon Feb 1 2010 David Parrish <david@dparrish.com> 1.9.5-1
- Removed dependence on "quit" command
- Added cli_set_idle_timeout_callback() for custom timeout handling
- Fixed an error caused by vsnprintf() overwriting it's input data
- Added #ifdef __cplusplus which should allow linking with C++ now
* Thu Oct 9 2008 David Parrish <david@dparrish.com> 1.9.4-1
- cli_regular() failures now close client connections
- Migrate development to Google Code
- Remove docs as they were out of date and now migrated to Google Code wiki
* Fri Jul 28 2008 David Parrish <david@dparrish.com> 1.9.3-1
- Add support for compiling on WIN32 (Thanks Hamish Coleman)
- Fix cli_build_shortest() length handling
- Don't call cli_build_shortest() when registering every command
- Disable TAB completion during username entry
* Fri Jun 2 2008 David Parrish <david@dparrish.com> 1.9.2-1
- Add configurable timeout for cli_regular() - defaults to 1 second
- Add idle timeout support
* Thu Jul 5 2007 Brendan O'Dea <bod@optus.net> 1.9.1-1
- Revert callback argument passing to match 1.8.x
- Recalculate unique_len on change of priv/mode
- Fixes for tab completion
* Thu Jun 07 2007 David Parrish <david@dparrish.com> 1.9.0-1
- Implemented tab completion - Thanks Marc Donner, Andrew Silent, Yuriy N. Shkandybin and others
- Filters are now extendable
- Rename internal functions to all be cli_xxxx()
- Many code cleanups and optimisations
- Fix memory leak calling cli_loop() repeatedly - Thanks Qiang Wu
* Mon Jan 19 2007 David Parrish <david@dparrish.com> 1.8.8-1
- Fix broken auth_callback logic - Thanks Ben Menchaca
* Sat Jun 17 2006 Brendan O'Dea <bod@optus.net> 1.8.7-1
- Code cleanups.
- Declare internal functions static.
- Use private data in cli_def rather than static buffers for do_print
and command_name functions.
* Mon Mar 06 2006 David Parrish <david@dparrish.com> 1.8.6-1
- Fix file descriptor leak in cli_loop() - Thanks Liam Widdowson
- Fix memory leak when calling cli_init() and cli_done() repeatedly.
* Fri Nov 25 2005 Brendan O'Dea <bod@optus.net> 1.8.5-2
- Apply spec changes from Charlie Brady: use License header, change
BuildRoot to include username.
* Mon May 2 2005 Brendan O'Dea <bod@optusnet.com.au> 1.8.5-1
- Add cli_error function which does not filter output.
* Wed Jan 5 2005 Brendan O'Dea <bod@optusnet.com.au> 1.8.4-1
- Add printf attribute to cli_print prototype
* Fri Nov 19 2004 Brendan O'Dea <bod@optusnet.com.au> 1.8.3-1
- Free help if set in cli_unregister_command (reported by Jung-Che Vincent Li)
- Correct auth_callback() documentation (reported by Serge B. Khvatov)
* Thu Nov 11 2004 Brendan O'Dea <bod@optusnet.com.au> 1.8.2-1
- Allow config commands to exit a submode
- Make "exit" work in exec/config/submodes
- Add ^K (kill to EOL)
* Mon Jul 12 2004 Brendan O'Dea <bod@optusnet.com.au> 1.8.1-1
- Documentation update.
- Allow NULL or "" to be passed to cli_set_banner() and
cli_set_hostname() to clear a previous value.
* Sun Jul 11 2004 Brendan O'Dea <bod@optusnet.com.au> 1.8.0-1
- Dropped prompt arg from cli_loop now that prompt is set by
hostname/mode/priv level; bump soname. Fixes ^L and ^A.
- Reworked parsing/filters to allow multiple filters (cmd|inc X|count).
- Made "grep" use regex, added -i, -v and -e args.
- Added "egrep" filter.
- Added "exclude" filter.
* Fri Jul 2 2004 Brendan O'Dea <bod@optusnet.com.au> 1.7.0-1
- Add mode argument to cli_file(), bump soname.
- Return old value from cli_set_privilege(), cli_set_configmode().
* Fri Jun 25 2004 Brendan O'Dea <bod@optusnet.com.au> 1.6.2-1
- Small cosmetic changes to output.
- Exiting configure/^Z shouldn't disable.
- Support encrypted password.
* Fri Jun 25 2004 David Parrish <david@dparrish.com> 1.6.0
- Add support for privilege levels and nested config levels. Thanks to Friedhelm
Düsterhöft for most of the code.
* Tue Feb 24 2004 David Parrish <david@dparrish.com>
- Add cli_print_callback() for overloading the output
- Don't pass around the FILE * handle anymore, it's in the cli_def struct anyway
- Add cli_file() to execute every line read from a file handle
- Add filter_count
* Sat Feb 14 2004 Brendan O'Dea <bod@optusnet.com.au> 1.4.0-1
- Add more line editing support: ^W, ^A, ^E, ^P, ^N, ^F, ^B
- Modify cli_print() to add \r\n and to split on \n to allow inc/begin
to work with multi-line output (note: API change, client code
should not include trailing \r\n; version bump)
- Use libcli.so.M.m as the soname
* Fri Jul 25 2003 David Parrish <david@dparrish.com>
- Add cli_regular to enable regular processing while cli is connected
* Wed Jun 25 2003 David Parrish <david@dparrish.com>
- Stop random stack smashing in cli_command_name.
- Stop memory leak by allocating static variable in cli_command_name.

302
src/lmbcli.cpp Normal file
View file

@ -0,0 +1,302 @@
#include <stdio.h>
#include <sys/types.h>
#include <iostream>
#include <iomanip>
#ifdef WIN32
#include <winsock2.h>
#include <windows.h>
#else
#include <netinet/in.h>
#include <sys/socket.h>
#include <arpa/inet.h>
#endif
#include <signal.h>
#include <strings.h>
#include <string.h>
#include <stdlib.h>
#include <unistd.h>
#include "libcli/libcli.h"
#include "serial/serial.h"
#include "lmbd.hpp"
#include "Driver-DX.hpp"
// vim:sw=4 tw=120 et
#define MODE_CONFIG_INT 10
#ifdef __GNUC__
# define UNUSED(d) d __attribute__ ((unused))
#else
# define UNUSED(d) d
#endif
unsigned int regular_count = 0;
unsigned int debug_regular = 0;
#ifdef WIN32
typedef int socklen_t;
int winsock_init()
{
WORD wVersionRequested;
WSADATA wsaData;
int err;
// Start up sockets
wVersionRequested = MAKEWORD(2, 2);
err = WSAStartup(wVersionRequested, &wsaData);
if (err != 0)
{
// Tell the user that we could not find a usable WinSock DLL.
return 0;
}
/*
* Confirm that the WinSock DLL supports 2.2
* Note that if the DLL supports versions greater than 2.2 in addition to
* 2.2, it will still return 2.2 in wVersion since that is the version we
* requested.
* */
if (LOBYTE(wsaData.wVersion) != 2 || HIBYTE(wsaData.wVersion) != 2)
{
// Tell the user that we could not find a usable WinSock DLL.
WSACleanup();
return 0;
}
return 1;
}
#endif
int cmd_save(struct cli_def *cli, const char *command, char *argv[], int argc)
{
struct LMBCTX *lmbctx = (struct LMBCTX *)cli_get_context(cli);
boost::unique_lock<boost::mutex> scoped_lock(lmbctx->io_mutex);
lmbctx->save("LMB.conf");
cli_print(cli, "Saved Config");
BOOST_LOG_TRIVIAL(info) << "Saved Config From Terminal";
return CLI_OK;
}
int cmd_show_ports(struct cli_def *cli, const char *command, char *argv[], int argc)
{
int i;
cli_print(cli, "called %s with \"%s\"", __func__, command);
cli_print(cli, "%d arguments:", argc);
for (i = 0; i < argc; i++)
cli_print(cli, " %s", argv[i]);
std::vector<serial::PortInfo> pi;
pi = serial::list_ports();
std::vector<serial::PortInfo>::iterator pii;
for (pii = pi.begin(); pii != pi.end(); pii++) {
cli_print(cli, "Port: %s Description: %s", pii->port.c_str(), pii->description.c_str());
}
return CLI_OK;
}
int cmd_show_port(struct cli_def *cli, const char *command, char *argv[], int argc)
{
struct LMBCTX *lmbctx = (struct LMBCTX *)cli_get_context(cli);
boost::unique_lock<boost::mutex> scoped_lock(lmbctx->io_mutex);
cli_print(cli, "Port: %s", lmbctx->port.c_str());
return CLI_OK;
}
int cmd_show_messages(struct cli_def *cli, const char *command, char *argv[], int argc)
{
struct LMBCTX *lmbctx = (struct LMBCTX *)cli_get_context(cli);
boost::unique_lock<boost::mutex> scoped_lock(lmbctx->io_mutex);
for (unsigned int i = 1; i <= lmbctx->messages; i++) {
if (lmbctx->msgdisplay[i-1] == 1) {
cli_print(cli, "Message %d: %s", i, lmbctx->displayedmsgs[i].c_str());
}
}
return CLI_OK;
}
int cmd_show_startupmsgs(struct cli_def *cli, const char *command, char *argv[], int argc)
{
struct LMBCTX *lmbctx = (struct LMBCTX *)cli_get_context(cli);
boost::unique_lock<boost::mutex> scoped_lock(lmbctx->io_mutex);
for (unsigned int i = 1; i <= lmbctx->messages; i++) {
if (lmbctx->startupmsg[i].length() > 0) {
cli_print(cli, "Startup Message %d: %s", i, lmbctx->startupmsg[i].c_str());
}
}
return CLI_OK;
}
int cmd_set(struct cli_def *cli, const char *command, char *argv[], int argc)
{
return CLI_OK;
}
int cmd_set_port(struct cli_def *cli, UNUSED(const char *command), char *argv[],
int argc)
{
if (strcmp(argv[0], "?") == 0)
{
cli_print(cli, "Specify a value");
return CLI_OK;
}
if (!argv[0] && !&argv[0])
{
cli_print(cli, "Specify a serial port to open");
return CLI_OK;
}
std::vector<serial::PortInfo> pi;
pi = serial::list_ports();
std::vector<serial::PortInfo>::iterator pii;
for (pii = pi.begin(); pii != pi.end(); pii++) {
if (!pii->port.compare(argv[0])) {
cli_print(cli, "Setting Serial Port to %s", pii->port.c_str());
struct LMBCTX *lmbctx = (struct LMBCTX *)cli_get_context(cli);
boost::unique_lock<boost::mutex> scoped_lock(lmbctx->io_mutex);
lmbctx->port = pii->port;
BOOST_LOG_TRIVIAL(info) << "Set a New Serial Port from Terminal: " << lmbctx->port;
return CLI_OK;
}
}
cli_print(cli, "No Serial Port Matching %s was found", argv[1]);
return CLI_OK;
}
int cmd_set_startupmsg(struct cli_def *cli, UNUSED(const char *command), char *argv[],
int argc)
{
if (argc < 2) {
cli_print(cli, "usage: set startupmsg <pos> <message>");
return CLI_OK;
}
struct LMBCTX *lmbctx = (struct LMBCTX *)cli_get_context(cli);
boost::unique_lock<boost::mutex> scoped_lock(lmbctx->io_mutex);
if (atoi(argv[0]) > lmbctx->messages) {
cli_print(cli, "Position out of Range");
return CLI_OK;
}
lmbctx->startupmsg[atoi(argv[0])] = std::string(argv[1]);
return CLI_OK;
}
int cmd_message(struct cli_def *cli, UNUSED(const char *command), UNUSED(char *argv[]), UNUSED(int argc)) {
if (argc < 2) {
cli_print(cli, "usage: message <pos> <message>");
return CLI_OK;
}
struct LMBCTX *lmbctx = (struct LMBCTX *)cli_get_context(cli);
boost::unique_lock<boost::mutex> scoped_lock(lmbctx->io_mutex);
lmbctx->driver->Init(lmbctx);
lmbctx->driver->setMessage(atoi(argv[0]), argv[1]);
lmbctx->driver->Fini();
return CLI_OK;
}
int cmd_clear(struct cli_def *cli, UNUSED(const char *command), UNUSED(char *argv[]), UNUSED(int argc)) {
if (argc < 1) {
cli_print(cli, "usage: clear <pos>");
return CLI_OK;
}
struct LMBCTX *lmbctx = (struct LMBCTX *)cli_get_context(cli);
boost::unique_lock<boost::mutex> scoped_lock(lmbctx->io_mutex);
lmbctx->driver->Init(lmbctx);
lmbctx->driver->setMessage(atoi(argv[0]), "");
lmbctx->driver->Fini();
return CLI_OK;
}
int cmd_context(struct cli_def *cli, UNUSED(const char *command), UNUSED(char *argv[]), UNUSED(int argc))
{
// struct my_context *myctx = (struct my_context *)cli_get_context(cli);
//cli_print(cli, "User context has a value of %d and message saying %s", myctx->value, myctx->message);
return CLI_OK;
}
int check_auth(const char *username, const char *password)
{
return CLI_OK;
#if 0
if (strcasecmp(username, "fred") != 0)
return CLI_ERROR;
if (strcasecmp(password, "nerk") != 0)
return CLI_ERROR;
return CLI_OK;
#endif
}
int idle_timeout(struct cli_def *cli)
{
cli_print(cli, "Custom idle timeout");
return CLI_QUIT;
}
int Startcliloop(LMBCTX *lmbctx, int sockfd) {
struct cli_command *c;
struct cli_def *cli;
cli = cli_init();
cli_set_banner(cli, "Led Message Board");
cli_set_hostname(cli, "LMB");
cli_telnet_protocol(cli, 1);
{
boost::unique_lock<boost::mutex> scoped_lock(lmbctx->io_mutex);
cli_allow_user(cli, lmbctx->username.c_str(), lmbctx->password.c_str());
cli_allow_enable(cli, lmbctx->enablepass.c_str());
}
cli_set_idle_timeout_callback(cli, 60, idle_timeout); // 60 second idle timeout
c = cli_register_command(cli, NULL, "set", cmd_set, PRIVILEGE_PRIVILEGED, MODE_EXEC, NULL);
cli_register_command(cli, c, "port", cmd_set_port, PRIVILEGE_PRIVILEGED, MODE_EXEC, "Set Serial Port");
cli_register_command(cli, c, "startupmsg", cmd_set_startupmsg, PRIVILEGE_PRIVILEGED, MODE_EXEC, "Set Serial Port");
cli_register_command(cli, NULL, "save", cmd_save, PRIVILEGE_PRIVILEGED, MODE_EXEC, "Save Config");
c = cli_register_command(cli, NULL, "show", NULL, PRIVILEGE_UNPRIVILEGED, MODE_EXEC, NULL);
cli_register_command(cli, c, "ports", cmd_show_ports, PRIVILEGE_UNPRIVILEGED, MODE_EXEC, "Show available Serial Ports");
cli_register_command(cli, c, "port", cmd_show_port, PRIVILEGE_UNPRIVILEGED, MODE_EXEC, "Show Configured Serial Port");
cli_register_command(cli, c, "messages", cmd_show_messages, PRIVILEGE_UNPRIVILEGED, MODE_EXEC, "Show Current Messages");
cli_register_command(cli, c, "startupmsg", cmd_show_startupmsgs, PRIVILEGE_UNPRIVILEGED, MODE_EXEC, "Show Current Messages");
cli_register_command(cli, NULL, "message", cmd_message, PRIVILEGE_UNPRIVILEGED, MODE_EXEC, "Set a Message to be displayed");
cli_register_command(cli, NULL, "clear", cmd_clear, PRIVILEGE_UNPRIVILEGED, MODE_EXEC, "Clear a Message");
cli_set_context(cli, (void*)lmbctx);
BOOST_LOG_TRIVIAL(info) << "accepted new CLI Connection";
cli_loop(cli, sockfd);
BOOST_LOG_TRIVIAL(info) << "closed CLI Connection";
cli_done(cli);
return 0;
}

155
src/lmbd.cpp Normal file
View file

@ -0,0 +1,155 @@
/* LED Message Board - lmbd.cpp
** Copyright (c) 2014 Justin Hammond
**
** This program is free software; you can redistribute it and/or modify
** it under the terms of the GNU General Public License as published by
** the Free Software Foundation; either version 2 of the License, or
** (at your option) any later version.
**
** This program is distributed in the hope that it will be useful,
** but WITHOUT ANY WARRANTY; without even the implied warranty of
** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
** GNU General Public License for more details.
**
** You should have received a copy of the GNU General Public License
** along with this program; if not, write to the Free Software
** Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
** USA
**
** LED Message Board SVN Identification:
** $Rev$
*/
/** @file lmbd.cpp
* @brief
*/
#include <stdio.h>
#include <sys/types.h>
#include <iostream>
#include <iomanip>
#ifdef WIN32
#include <winsock2.h>
#include <windows.h>
#else
#include <netinet/in.h>
#include <sys/socket.h>
#include <arpa/inet.h>
#endif
#include <signal.h>
#include <strings.h>
#include <string.h>
#include <stdlib.h>
#include <unistd.h>
#include "libcli/libcli.h"
#include "serial/serial.h"
#include "lmbd.hpp"
#include "Driver-DX.hpp"
#include "FileMonitor.hpp"
#define CLITEST_PORT 8000
int main()
{
int s, x;
struct sockaddr_in addr;
int on = 1;
#ifndef WIN32
signal(SIGCHLD, SIG_IGN);
#endif
#ifdef WIN32
if (!winsock_init()) {
printf("Error initialising winsock\n");
return 1;
}
#endif
boost::log::core::get()->set_filter
(
boost::log::trivial::severity >= boost::log::trivial::info
);
struct LMBCTX *lmbctx = new LMBCTX;
lmbctx->driver = new Driver_DX();
lmbctx->sp = NULL;
lmbctx->port = "/dev/ttyUSB0";
lmbctx->debug_level = 0;
lmbctx->username = "Admin";
lmbctx->password = "password";
lmbctx->enablepass = "enable";
lmbctx->max_cli = 5;
lmbctx->monitorpath = "/tmp/test/";
lmbctx->messages = 6;
lmbctx->msgdisplay.resize(6);
std::vector<std::string> displayedmsgs;
try {
lmbctx->load("LMB.conf");
} catch (...) {
BOOST_LOG_TRIVIAL(warning) << "Could Not Load Config File";
}
lmbctx->driver->Init(lmbctx);
lmbctx->driver->StartUp();
lmbctx->driver->Fini();
for (unsigned int i = 1; i <= lmbctx->messages; i++) {
if (lmbctx->startupmsg[i].length() > 0) {
lmbctx->driver->Init(lmbctx);
lmbctx->driver->setMessage(i, lmbctx->startupmsg[i]);
lmbctx->driver->Fini();
}
}
FileMonitor fm;
fm.init(lmbctx);
if ((s = socket(AF_INET, SOCK_STREAM, 0)) < 0)
{
BOOST_LOG_TRIVIAL(error) << "Socket Error";
return 1;
}
setsockopt(s, SOL_SOCKET, SO_REUSEADDR, &on, sizeof(on));
memset(&addr, 0, sizeof(addr));
addr.sin_family = AF_INET;
addr.sin_addr.s_addr = htonl(INADDR_ANY);
addr.sin_port = htons(CLITEST_PORT);
if (bind(s, (struct sockaddr *) &addr, sizeof(addr)) < 0)
{
BOOST_LOG_TRIVIAL(error) << "bind Error";
return 1;
}
if (listen(s, 50) < 0)
{
BOOST_LOG_TRIVIAL(error) << "listen Error";
return 1;
}
BOOST_LOG_TRIVIAL(info) << "Listening on port " << CLITEST_PORT;
while ((x = accept(s, NULL, 0)))
{
if (lmbctx->Clients.size() < lmbctx->max_cli) {
lmbctx->Clients.push_back(boost::make_shared<boost::thread>(Startcliloop, boost::ref(lmbctx), x));
} else {
BOOST_LOG_TRIVIAL(warning) << "Dropping Client Connection as we are at our Max";
close(x);
}
}
delete lmbctx;
return 0;
}

129
src/lmbd.hpp Normal file
View file

@ -0,0 +1,129 @@
/* LED Message Board - lmbd.hpp
** Copyright (c) 2014 Justin Hammond
**
** This program is free software; you can redistribute it and/or modify
** it under the terms of the GNU General Public License as published by
** the Free Software Foundation; either version 2 of the License, or
** (at your option) any later version.
**
** This program is distributed in the hope that it will be useful,
** but WITHOUT ANY WARRANTY; without even the implied warranty of
** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
** GNU General Public License for more details.
**
** You should have received a copy of the GNU General Public License
** along with this program; if not, write to the Free Software
** Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
** USA
**
** LED Message Board SVN Identification:
** $Rev$
*/
/** @file lmbd.hpp
* @brief
*/
#ifndef SRC_LMBD_HPP_
#define SRC_LMBD_HPP_
#include <boost/make_shared.hpp>
#include <boost/log/core.hpp>
#include <boost/log/trivial.hpp>
#include <boost/log/expressions.hpp>
#include <boost/property_tree/ptree.hpp>
#include <boost/property_tree/json_parser.hpp>
#include <boost/foreach.hpp>
#include <boost/thread/thread.hpp>
#include <boost/thread/mutex.hpp>
#include <boost/dynamic_bitset.hpp>
#include "serial/serial.h"
#include "libcli/libcli.h"
class iDriver;
struct LMBCTX {
serial::Serial *sp;
boost::mutex io_mutex;
std::string port;
int debug_level;
std::string username;
std::string password;
std::string enablepass;
struct cli_def *cli;
iDriver *driver;
std::string monitorpath;
unsigned int messages;
boost::dynamic_bitset<> msgdisplay;
std::map<int, std::string> displayedmsgs;
std::map<int, std::string> startupmsg;
std::vector<boost::shared_ptr<boost::thread> > Clients;
unsigned int max_cli;
void load(const std::string &filename)
{
// Create empty property tree object
boost::property_tree::ptree tree;
// Parse the XML into the property tree.
boost::property_tree::read_json(filename, tree);
// Use the throwing version of get to find the debug filename.
// If the path cannot be resolved, an exception is thrown.
port= tree.get<std::string>("serial.port");
// Use the default-value version of get to find the debug level.
// Note that the default value is used to deduce the target type.
debug_level = tree.get("debug.level", 0);
username = tree.get<std::string>("username");
password = tree.get<std::string>("password");
enablepass = tree.get<std::string>("enablepass");
monitorpath = tree.get<std::string>("monitorpath");
for (unsigned int i = 1; i <= this->messages; i++) {
std::stringstream ss;
ss << "startupmessage." << std::dec << i;
this->startupmsg[i] = tree.get<std::string>(ss.str());
}
}
void save(const std::string &filename)
{
// Create an empty property tree object.
boost::property_tree::ptree tree;
// Put the simple values into the tree. The integer is automatically
// converted to a string. Note that the "debug" node is automatically
// created if it doesn't exist.
tree.put("serial.port", port);
tree.put("debug.level", debug_level);
tree.put("username", username);
tree.put("password", password);
tree.put("enablepass", enablepass);
tree.put("monitorpath", monitorpath);
for (unsigned int i = 1; i <= this->messages; i++) {
std::stringstream ss;
ss << "startupmessage." << std::dec << i;
tree.put(ss.str(), this->startupmsg[i]);
}
// Write property tree to XML file
boost::property_tree::write_json(filename, tree);
}
};
int Startcliloop(LMBCTX *lmbctx, int sockfd);
#endif /* SRC_LMBD_HPP_ */

85
src/serial/CHANGELOG.rst Normal file
View file

@ -0,0 +1,85 @@
^^^^^^^^^^^^^^^^^^^^^^^^^^^^
Changelog for package serial
^^^^^^^^^^^^^^^^^^^^^^^^^^^^
1.2.1 (2015-04-21)
------------------
* Removed the use of a C++11 feature for compatibility with older browsers.
* Fixed an issue with cross compiling with mingw on Windows.
* Restructured Visual Studio project layout.
* Added include of ``#include <AvailabilityMacros.h>`` on OS X (listing of ports).
* Fixed MXE for the listing of ports on Windows.
* Now closes file device if ``reconfigureDevice`` fails (Windows).
* Added the MARK/SPACE parity bit option, also made it optional.
Adding the enumeration values for MARK and SPACE was the only code change to an API header.
It should not affect ABI or API.
* Added support for 576000 baud on Linux.
* Now releases iterator properly in listing of ports code for OS X.
* Fixed the ability to open COM ports over COM10 on Windows.
* Fixed up some documentation about exceptions in ``serial.h``.
1.2.0 (2014-07-02)
------------------
* Removed vestigial ``read_cache_`` private member variable from Serial::Serial
* Fixed usage of scoped locks
Previously they were getting destroyed immediately because they were not stored in a temporary scope variable
* Added check of return value from close in Serial::SerialImpl::close () in unix.cc and win.cc
* Added ability to enumerate ports on linux and windows.
Updated serial_example.cc to show example of port enumeration.
* Fixed compile on VS2013
* Added functions ``waitReadable`` and ``waitByteTimes`` with implemenations for Unix to support high performance reading
* Contributors: Christopher Baker, Craig Lilley, Konstantina Kastanara, Mike Purvis, William Woodall
1.1.7 (2014-02-20)
------------------
* Improved support for mingw (mxe.cc)
* Fix compilation warning
See issue `#53 <https://github.com/wjwwood/serial/issues/53>`_
* Improved timer handling in unix implementation
* fix broken ifdef _WIN32
* Fix broken ioctl calls, add exception handling.
* Code guards for platform-specific implementations. (when not using cmake / catkin)
* Contributors: Christopher Baker, Mike Purvis, Nicolas Bigaouette, William Woodall, dawid
1.1.6 (2013-10-17)
------------------
* Move stopbits_one_point_five to the end of the enum, so that it doesn't alias with stopbits_two.
1.1.5 (2013-09-23)
------------------
* Fix license labeling, I put BSD, but the license has always been MIT...
* Added Microsoft Visual Studio 2010 project to make compiling on Windows easier.
* Implemented Serial::available() for Windows
* Update how custom baudrates are handled on OS X
This is taken from the example serial program on Apple's developer website, see:
http://free-pascal-general.1045716.n5.nabble.com/Non-standard-baud-rates-in-OS-X-IOSSIOSPEED-IOCTL-td4699923.html
* Timout settings are now applied by reconfigurePort
* Pass LPCWSTR to CreateFile in Windows impl
* Use wstring for ``port_`` type in Windows impl
1.1.4 (2013-06-12 00:13:18 -0600)
---------------------------------
* Timing calculation fix for read and write.
Fixes `#27 <https://github.com/wjwwood/serial/issues/27>`_
* Update list of exceptions thrown from constructor.
* fix, by Thomas Hoppe <thomas.hoppe@cesys.com>
For SerialException's:
* The name was misspelled...
* Use std::string's for error messages to prevent corruption of messages on some platforms
* alloca.h does not exist on OpenBSD either.
1.1.3 (2013-01-09 10:54:34 -0800)
---------------------------------
* Install headers
1.1.2 (2012-12-14 14:08:55 -0800)
---------------------------------
* Fix buildtool depends
1.1.1 (2012-12-03)
------------------
* Removed rt linking on OS X. Fixes `#24 <https://github.com/wjwwood/serial/issues/24>`_.
1.1.0 (2012-10-24)
------------------
* Previous history is unstructured and therefore has been truncated. See the commit messages for more info.

70
src/serial/README.md Normal file
View file

@ -0,0 +1,70 @@
# 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.com/serial/
API Documentation: http://wjwwood.github.com/serial/doc/1.1.0/index.html
### Dependencies
* [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
### 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 docs
Install:
make install
Uninstall:
make uninstall
### 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.
### Authors
William Woodall <wjwwood@gmail.com>
John Harrison <ash.gti@gmail.com>
### Contact
William Woodall <william@osrfoundation.org>

10
src/serial/changes.txt Normal file
View file

@ -0,0 +1,10 @@
# 1.1.0 10-24-2012
* Converted the build system to catkin
# v1.0.1 8-27-2012
* Added baudrates: 1000000, 11520000, 2000000, 2500000, 3000000, 3500000, and 4000000
* Linking related bug fixes on Linux and OS X
* Custom baudrate bug fix. Closes issue #18.

1716
src/serial/doc/Doxyfile Normal file

File diff suppressed because it is too large Load diff

64
src/serial/doc/serial.dox Normal file
View file

@ -0,0 +1,64 @@
/*!
\mainpage Serial Library
\author William Woodall <wjwwood@gmail.com>, John Harrison <ash@greaterthaninfinity.com>
\section what_is What is serial?
Serial is a cross-platform, simple to use library for using serial ports on computers. This library provides a C++, object oriented interface for interacting with RS-232 like devices on Linux and Windows.
Want to use it with ROS(Robot Operating System)? No problem, it compiles as a unary stack.
\section getting_started Getting Started
Ready to jump in?
- Checkout our examples: \ref serial_example.cc
- Look at the main class documentation: \ref serial::Serial
\section features Features
- Linux, Mac OS X, and Windows Support
- Easy to use interface (modeled after PySerial)
- Minimal dependencies (cmake)
- Complete timeout control \ref serial::Serial::setTimeout
- Check and set handshaking lines (CTS, DSR, RI, CD and RTS, DTR)
- Block for changes in handshaking lines (Linux and Windows)
- Flush I/O separately and block until all writing done
\section install Installation
\subsection Dependencies
- CMake:
- CMake is required for building the system and can be located here: http://www.cmake.org/
\subsection building Compiling
Once you have gathered the dependencies, you need to checkout the software from github.com:
<pre>
git clone git://github.com/wjwwood/serial.git
</pre>
Once you have checked out the source code from github.com you can enter the directory and build the software.
<pre>
cd serial
make
make test # (optional) builds the example and tests, and runs the tests.
make doc # (optional) builds _this_ documentation.
</pre>
\subsection installing Installing
To install simply:
<pre>
sudo make install
</pre>
To uninstall simply:
<pre>
sudo make uninstall
</pre>
*/

View file

@ -0,0 +1,182 @@
/***
* This example expects the serial port has a loopback on it.
*
* Alternatively, you could use an Arduino:
*
* <pre>
* void setup() {
* Serial.begin(<insert your baudrate here>);
* }
*
* void loop() {
* if (Serial.available()) {
* Serial.write(Serial.read());
* }
* }
* </pre>
*/
#include <string>
#include <iostream>
#include <cstdio>
// OS Specific sleep
#ifdef _WIN32
#include <windows.h>
#else
#include <unistd.h>
#endif
#include "serial/serial.h"
using std::string;
using std::exception;
using std::cout;
using std::cerr;
using std::endl;
using std::vector;
void my_sleep(unsigned long milliseconds) {
#ifdef _WIN32
Sleep(milliseconds); // 100 ms
#else
usleep(milliseconds*1000); // 100 ms
#endif
}
void enumerate_ports()
{
vector<serial::PortInfo> devices_found = serial::list_ports();
vector<serial::PortInfo>::iterator iter = devices_found.begin();
while( iter != devices_found.end() )
{
serial::PortInfo device = *iter++;
printf( "(%s, %s, %s)\n", device.port.c_str(), device.description.c_str(),
device.hardware_id.c_str() );
}
}
void print_usage()
{
cerr << "Usage: test_serial {-e|<serial port address>} ";
cerr << "<baudrate> [test string]" << endl;
}
int run(int argc, char **argv)
{
if(argc < 2) {
print_usage();
return 0;
}
// Argument 1 is the serial port or enumerate flag
string port(argv[1]);
if( port == "-e" ) {
enumerate_ports();
return 0;
}
else if( argc < 3 ) {
print_usage();
return 1;
}
// Argument 2 is the baudrate
unsigned long baud = 0;
#if defined(WIN32) && !defined(__MINGW32__)
sscanf_s(argv[2], "%lu", &baud);
#else
sscanf(argv[2], "%lu", &baud);
#endif
// port, baudrate, timeout in milliseconds
serial::Serial my_serial(port, baud, serial::Timeout::simpleTimeout(1000));
cout << "Is the serial port open?";
if(my_serial.isOpen())
cout << " Yes." << endl;
else
cout << " No." << endl;
// Get the Test string
int count = 0;
string test_string;
if (argc == 4) {
test_string = argv[3];
} else {
test_string = "Testing.";
}
// Test the timeout, there should be 1 second between prints
cout << "Timeout == 1000ms, asking for 1 more byte than written." << endl;
while (count < 10) {
size_t bytes_wrote = my_serial.write(test_string);
string result = my_serial.read(test_string.length()+1);
cout << "Iteration: " << count << ", Bytes written: ";
cout << bytes_wrote << ", Bytes read: ";
cout << result.length() << ", String read: " << result << endl;
count += 1;
}
// Test the timeout at 250ms
my_serial.setTimeout(serial::Timeout::max(), 250, 0, 250, 0);
count = 0;
cout << "Timeout == 250ms, asking for 1 more byte than written." << endl;
while (count < 10) {
size_t bytes_wrote = my_serial.write(test_string);
string result = my_serial.read(test_string.length()+1);
cout << "Iteration: " << count << ", Bytes written: ";
cout << bytes_wrote << ", Bytes read: ";
cout << result.length() << ", String read: " << result << endl;
count += 1;
}
// Test the timeout at 250ms, but asking exactly for what was written
count = 0;
cout << "Timeout == 250ms, asking for exactly what was written." << endl;
while (count < 10) {
size_t bytes_wrote = my_serial.write(test_string);
string result = my_serial.read(test_string.length());
cout << "Iteration: " << count << ", Bytes written: ";
cout << bytes_wrote << ", Bytes read: ";
cout << result.length() << ", String read: " << result << endl;
count += 1;
}
// Test the timeout at 250ms, but asking for 1 less than what was written
count = 0;
cout << "Timeout == 250ms, asking for 1 less than was written." << endl;
while (count < 10) {
size_t bytes_wrote = my_serial.write(test_string);
string result = my_serial.read(test_string.length()-1);
cout << "Iteration: " << count << ", Bytes written: ";
cout << bytes_wrote << ", Bytes read: ";
cout << result.length() << ", String read: " << result << endl;
count += 1;
}
return 0;
}
int main(int argc, char **argv) {
try {
return run(argc, argv);
} catch (exception &e) {
cerr << "Unhandled Exception: " << e.what() << endl;
}
}

View file

@ -0,0 +1,221 @@
/*!
* \file serial/impl/unix.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 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)

View file

@ -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)

View file

@ -0,0 +1,773 @@
/*!
* \file serial/serial.h
* \author William Woodall <wjwwood@gmail.com>
* \author John Harrison <ash.gti@gmail.com>
* \version 0.1
*
* \section LICENSE
*
* The MIT License
*
* Copyright (c) 2012 William Woodall
*
* 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 cross platform interface for interacting with Serial Ports.
*/
#ifndef SERIAL_H
#define SERIAL_H
#include <limits>
#include <vector>
#include <string>
#include <cstring>
#include <sstream>
#include <exception>
#include <stdexcept>
#include <serial/v8stdint.h>
#define THROW(exceptionClass, message) throw exceptionClass(__FILE__, \
__LINE__, (message) )
namespace serial {
/*!
* Enumeration defines the possible bytesizes for the serial port.
*/
typedef enum {
fivebits = 5,
sixbits = 6,
sevenbits = 7,
eightbits = 8
} bytesize_t;
/*!
* Enumeration defines the possible parity types for the serial port.
*/
typedef enum {
parity_none = 0,
parity_odd = 1,
parity_even = 2,
parity_mark = 3,
parity_space = 4
} parity_t;
/*!
* Enumeration defines the possible stopbit types for the serial port.
*/
typedef enum {
stopbits_one = 1,
stopbits_two = 2,
stopbits_one_point_five
} stopbits_t;
/*!
* Enumeration defines the possible flowcontrol types for the serial port.
*/
typedef enum {
flowcontrol_none = 0,
flowcontrol_software,
flowcontrol_hardware
} flowcontrol_t;
/*!
* Structure for setting the timeout of the serial port, times are
* in milliseconds.
*
* In order to disable the interbyte timeout, set it to Timeout::max().
*/
struct Timeout {
#ifdef max
# undef max
#endif
static uint32_t max() {return std::numeric_limits<uint32_t>::max();}
/*!
* Convenience function to generate Timeout structs using a
* single absolute timeout.
*
* \param timeout A long that defines the time in milliseconds until a
* timeout occurs after a call to read or write is made.
*
* \return Timeout struct that represents this simple timeout provided.
*/
static Timeout simpleTimeout(uint32_t timeout) {
return Timeout(max(), timeout, 0, timeout, 0);
}
/*! Number of milliseconds between bytes received to timeout on. */
uint32_t inter_byte_timeout;
/*! A constant number of milliseconds to wait after calling read. */
uint32_t read_timeout_constant;
/*! A multiplier against the number of requested bytes to wait after
* calling read.
*/
uint32_t read_timeout_multiplier;
/*! A constant number of milliseconds to wait after calling write. */
uint32_t write_timeout_constant;
/*! A multiplier against the number of requested bytes to wait after
* calling write.
*/
uint32_t write_timeout_multiplier;
explicit Timeout (uint32_t inter_byte_timeout_=0,
uint32_t read_timeout_constant_=0,
uint32_t read_timeout_multiplier_=0,
uint32_t write_timeout_constant_=0,
uint32_t write_timeout_multiplier_=0)
: inter_byte_timeout(inter_byte_timeout_),
read_timeout_constant(read_timeout_constant_),
read_timeout_multiplier(read_timeout_multiplier_),
write_timeout_constant(write_timeout_constant_),
write_timeout_multiplier(write_timeout_multiplier_)
{}
};
/*!
* Class that provides a portable serial port interface.
*/
class Serial {
public:
/*!
* Creates a Serial object and opens the port if a port is specified,
* otherwise it remains closed until serial::Serial::open is called.
*
* \param port A std::string containing the address of the serial port,
* which would be something like 'COM1' on Windows and '/dev/ttyS0'
* on Linux.
*
* \param baudrate An unsigned 32-bit integer that represents the baudrate
*
* \param timeout A serial::Timeout struct that defines the timeout
* conditions for the serial port. \see serial::Timeout
*
* \param bytesize Size of each byte in the serial transmission of data,
* default is eightbits, possible values are: fivebits, sixbits, sevenbits,
* eightbits
*
* \param parity Method of parity, default is parity_none, possible values
* are: parity_none, parity_odd, parity_even
*
* \param stopbits Number of stop bits used, default is stopbits_one,
* possible values are: stopbits_one, stopbits_one_point_five, stopbits_two
*
* \param flowcontrol Type of flowcontrol used, default is
* flowcontrol_none, possible values are: flowcontrol_none,
* flowcontrol_software, flowcontrol_hardware
*
* \throw serial::PortNotOpenedException
* \throw serial::IOException
* \throw std::invalid_argument
*/
Serial (const std::string &port = "",
uint32_t baudrate = 9600,
Timeout timeout = Timeout(),
bytesize_t bytesize = eightbits,
parity_t parity = parity_none,
stopbits_t stopbits = stopbits_one,
flowcontrol_t flowcontrol = flowcontrol_none);
/*! Destructor */
virtual ~Serial ();
/*!
* Opens the serial port as long as the port is set and the port isn't
* already open.
*
* If the port is provided to the constructor then an explicit call to open
* is not needed.
*
* \see Serial::Serial
*
* \throw std::invalid_argument
* \throw serial::SerialException
* \throw serial::IOException
*/
void
open ();
/*! Gets the open status of the serial port.
*
* \return Returns true if the port is open, false otherwise.
*/
bool
isOpen () const;
/*! Closes the serial port. */
void
close ();
/*! Return the number of characters in the buffer. */
size_t
available ();
/*! Block until there is serial data to read or read_timeout_constant
* number of milliseconds have elapsed. The return value is true when
* the function exits with the port in a readable state, false otherwise
* (due to timeout or select interruption). */
bool
waitReadable ();
/*! Block for a period of time corresponding to the transmission time of
* count characters at present serial settings. This may be used in con-
* junction with waitReadable to read larger blocks of data from the
* port. */
void
waitByteTimes (size_t count);
/*! Read a given amount of bytes from the serial port into a given buffer.
*
* The read function will return in one of three cases:
* * The number of requested bytes was read.
* * In this case the number of bytes requested will match the size_t
* returned by read.
* * A timeout occurred, in this case the number of bytes read will not
* match the amount requested, but no exception will be thrown. One of
* two possible timeouts occurred:
* * The inter byte timeout expired, this means that number of
* milliseconds elapsed between receiving bytes from the serial port
* exceeded the inter byte timeout.
* * The total timeout expired, which is calculated by multiplying the
* read timeout multiplier by the number of requested bytes and then
* added to the read timeout constant. If that total number of
* milliseconds elapses after the initial call to read a timeout will
* occur.
* * An exception occurred, in this case an actual exception will be thrown.
*
* \param buffer An uint8_t array of at least the requested size.
* \param size A size_t defining how many bytes to be read.
*
* \return A size_t representing the number of bytes read as a result of the
* call to read.
*
* \throw serial::PortNotOpenedException
* \throw serial::SerialException
*/
size_t
read (uint8_t *buffer, size_t size);
/*! Read a given amount of bytes from the serial port into a give buffer.
*
* \param buffer A reference to a std::vector of uint8_t.
* \param size A size_t defining how many bytes to be read.
*
* \return A size_t representing the number of bytes read as a result of the
* call to read.
*
* \throw serial::PortNotOpenedException
* \throw serial::SerialException
*/
size_t
read (std::vector<uint8_t> &buffer, size_t size = 1);
/*! Read a given amount of bytes from the serial port into a give buffer.
*
* \param buffer A reference to a std::string.
* \param size A size_t defining how many bytes to be read.
*
* \return A size_t representing the number of bytes read as a result of the
* call to read.
*
* \throw serial::PortNotOpenedException
* \throw serial::SerialException
*/
size_t
read (std::string &buffer, size_t size = 1);
/*! Read a given amount of bytes from the serial port and return a string
* containing the data.
*
* \param size A size_t defining how many bytes to be read.
*
* \return A std::string containing the data read from the port.
*
* \throw serial::PortNotOpenedException
* \throw serial::SerialException
*/
std::string
read (size_t size = 1);
/*! Reads in a line or until a given delimiter has been processed.
*
* Reads from the serial port until a single line has been read.
*
* \param buffer A std::string reference used to store the data.
* \param size A maximum length of a line, defaults to 65536 (2^16)
* \param eol A string to match against for the EOL.
*
* \return A size_t representing the number of bytes read.
*
* \throw serial::PortNotOpenedException
* \throw serial::SerialException
*/
size_t
readline (std::string &buffer, size_t size = 65536, std::string eol = "\n");
/*! Reads in a line or until a given delimiter has been processed.
*
* Reads from the serial port until a single line has been read.
*
* \param size A maximum length of a line, defaults to 65536 (2^16)
* \param eol A string to match against for the EOL.
*
* \return A std::string containing the line.
*
* \throw serial::PortNotOpenedException
* \throw serial::SerialException
*/
std::string
readline (size_t size = 65536, std::string eol = "\n");
/*! Reads in multiple lines until the serial port times out.
*
* This requires a timeout > 0 before it can be run. It will read until a
* timeout occurs and return a list of strings.
*
* \param size A maximum length of combined lines, defaults to 65536 (2^16)
*
* \param eol A string to match against for the EOL.
*
* \return A vector<string> containing the lines.
*
* \throw serial::PortNotOpenedException
* \throw serial::SerialException
*/
std::vector<std::string>
readlines (size_t size = 65536, std::string eol = "\n");
/*! Write a string to the serial port.
*
* \param data A const reference containing the data to be written
* to the serial port.
*
* \param size A size_t that indicates how many bytes should be written from
* the given data buffer.
*
* \return A size_t representing the number of bytes actually written to
* the serial port.
*
* \throw serial::PortNotOpenedException
* \throw serial::SerialException
* \throw serial::IOException
*/
size_t
write (const uint8_t *data, size_t size);
/*! Write a string to the serial port.
*
* \param data A const reference containing the data to be written
* to the serial port.
*
* \return A size_t representing the number of bytes actually written to
* the serial port.
*
* \throw serial::PortNotOpenedException
* \throw serial::SerialException
* \throw serial::IOException
*/
size_t
write (const std::vector<uint8_t> &data);
/*! Write a string to the serial port.
*
* \param data A const reference containing the data to be written
* to the serial port.
*
* \return A size_t representing the number of bytes actually written to
* the serial port.
*
* \throw serial::PortNotOpenedException
* \throw serial::SerialException
* \throw serial::IOException
*/
size_t
write (const std::string &data);
/*! Sets the serial port identifier.
*
* \param port A const std::string reference containing the address of the
* serial port, which would be something like 'COM1' on Windows and
* '/dev/ttyS0' on Linux.
*
* \throw std::invalid_argument
*/
void
setPort (const std::string &port);
/*! Gets the serial port identifier.
*
* \see Serial::setPort
*
* \throw std::invalid_argument
*/
std::string
getPort () const;
/*! Sets the timeout for reads and writes using the Timeout struct.
*
* There are two timeout conditions described here:
* * The inter byte timeout:
* * The inter_byte_timeout component of serial::Timeout defines the
* maximum amount of time, in milliseconds, between receiving bytes on
* the serial port that can pass before a timeout occurs. Setting this
* to zero will prevent inter byte timeouts from occurring.
* * Total time timeout:
* * The constant and multiplier component of this timeout condition,
* for both read and write, are defined in serial::Timeout. This
* timeout occurs if the total time since the read or write call was
* made exceeds the specified time in milliseconds.
* * The limit is defined by multiplying the multiplier component by the
* number of requested bytes and adding that product to the constant
* component. In this way if you want a read call, for example, to
* timeout after exactly one second regardless of the number of bytes
* you asked for then set the read_timeout_constant component of
* serial::Timeout to 1000 and the read_timeout_multiplier to zero.
* This timeout condition can be used in conjunction with the inter
* byte timeout condition with out any problems, timeout will simply
* occur when one of the two timeout conditions is met. This allows
* users to have maximum control over the trade-off between
* responsiveness and efficiency.
*
* Read and write functions will return in one of three cases. When the
* reading or writing is complete, when a timeout occurs, or when an
* exception occurs.
*
* \param timeout A serial::Timeout struct containing the inter byte
* timeout, and the read and write timeout constants and multipliers.
*
* \see serial::Timeout
*/
void
setTimeout (Timeout &timeout);
/*! Sets the timeout for reads and writes. */
void
setTimeout (uint32_t inter_byte_timeout, uint32_t read_timeout_constant,
uint32_t read_timeout_multiplier, uint32_t write_timeout_constant,
uint32_t write_timeout_multiplier)
{
Timeout timeout(inter_byte_timeout, read_timeout_constant,
read_timeout_multiplier, write_timeout_constant,
write_timeout_multiplier);
return setTimeout(timeout);
}
/*! Gets the timeout for reads in seconds.
*
* \return A Timeout struct containing the inter_byte_timeout, and read
* and write timeout constants and multipliers.
*
* \see Serial::setTimeout
*/
Timeout
getTimeout () const;
/*! Sets the baudrate for the serial port.
*
* Possible baudrates depends on the system but some safe baudrates include:
* 110, 300, 600, 1200, 2400, 4800, 9600, 14400, 19200, 28800, 38400, 56000,
* 57600, 115200
* Some other baudrates that are supported by some comports:
* 128000, 153600, 230400, 256000, 460800, 921600
*
* \param baudrate An integer that sets the baud rate for the serial port.
*
* \throw std::invalid_argument
*/
void
setBaudrate (uint32_t baudrate);
/*! Gets the baudrate for the serial port.
*
* \return An integer that sets the baud rate for the serial port.
*
* \see Serial::setBaudrate
*
* \throw std::invalid_argument
*/
uint32_t
getBaudrate () const;
/*! Sets the bytesize for the serial port.
*
* \param bytesize Size of each byte in the serial transmission of data,
* default is eightbits, possible values are: fivebits, sixbits, sevenbits,
* eightbits
*
* \throw std::invalid_argument
*/
void
setBytesize (bytesize_t bytesize);
/*! Gets the bytesize for the serial port.
*
* \see Serial::setBytesize
*
* \throw std::invalid_argument
*/
bytesize_t
getBytesize () const;
/*! Sets the parity for the serial port.
*
* \param parity Method of parity, default is parity_none, possible values
* are: parity_none, parity_odd, parity_even
*
* \throw std::invalid_argument
*/
void
setParity (parity_t parity);
/*! Gets the parity for the serial port.
*
* \see Serial::setParity
*
* \throw std::invalid_argument
*/
parity_t
getParity () const;
/*! Sets the stopbits for the serial port.
*
* \param stopbits Number of stop bits used, default is stopbits_one,
* possible values are: stopbits_one, stopbits_one_point_five, stopbits_two
*
* \throw std::invalid_argument
*/
void
setStopbits (stopbits_t stopbits);
/*! Gets the stopbits for the serial port.
*
* \see Serial::setStopbits
*
* \throw std::invalid_argument
*/
stopbits_t
getStopbits () const;
/*! Sets the flow control for the serial port.
*
* \param flowcontrol Type of flowcontrol used, default is flowcontrol_none,
* possible values are: flowcontrol_none, flowcontrol_software,
* flowcontrol_hardware
*
* \throw std::invalid_argument
*/
void
setFlowcontrol (flowcontrol_t flowcontrol);
/*! Gets the flow control for the serial port.
*
* \see Serial::setFlowcontrol
*
* \throw std::invalid_argument
*/
flowcontrol_t
getFlowcontrol () const;
/*! Flush the input and output buffers */
void
flush ();
/*! Flush only the input buffer */
void
flushInput ();
/*! Flush only the output buffer */
void
flushOutput ();
/*! Sends the RS-232 break signal. See tcsendbreak(3). */
void
sendBreak (int duration);
/*! Set the break condition to a given level. Defaults to true. */
void
setBreak (bool level = true);
/*! Set the RTS handshaking line to the given level. Defaults to true. */
void
setRTS (bool level = true);
/*! Set the DTR handshaking line to the given level. Defaults to true. */
void
setDTR (bool level = true);
/*!
* Blocks until CTS, DSR, RI, CD changes or something interrupts it.
*
* Can throw an exception if an error occurs while waiting.
* You can check the status of CTS, DSR, RI, and CD once this returns.
* Uses TIOCMIWAIT via ioctl if available (mostly only on Linux) with a
* resolution of less than +-1ms and as good as +-0.2ms. Otherwise a
* polling method is used which can give +-2ms.
*
* \return Returns true if one of the lines changed, false if something else
* occurred.
*
* \throw SerialException
*/
bool
waitForChange ();
/*! Returns the current status of the CTS line. */
bool
getCTS ();
/*! Returns the current status of the DSR line. */
bool
getDSR ();
/*! Returns the current status of the RI line. */
bool
getRI ();
/*! Returns the current status of the CD line. */
bool
getCD ();
private:
// Disable copy constructors
Serial(const Serial&);
Serial& operator=(const Serial&);
// Pimpl idiom, d_pointer
class SerialImpl;
SerialImpl *pimpl_;
// Scoped Lock Classes
class ScopedReadLock;
class ScopedWriteLock;
// Read common function
size_t
read_ (uint8_t *buffer, size_t size);
// Write common function
size_t
write_ (const uint8_t *data, size_t length);
};
class SerialException : public std::exception
{
// Disable copy constructors
SerialException& operator=(const SerialException&);
std::string e_what_;
public:
SerialException (const char *description) {
std::stringstream ss;
ss << "SerialException " << description << " failed.";
e_what_ = ss.str();
}
SerialException (const SerialException& other) : e_what_(other.e_what_) {}
virtual ~SerialException() throw() {}
virtual const char* what () const throw () {
return e_what_.c_str();
}
};
class IOException : public std::exception
{
// Disable copy constructors
IOException& operator=(const IOException&);
std::string file_;
int line_;
std::string e_what_;
int errno_;
public:
explicit IOException (std::string file, int line, int errnum)
: file_(file), line_(line), errno_(errnum) {
std::stringstream ss;
#if defined(_WIN32) && !defined(__MINGW32__)
char error_str [1024];
strerror_s(error_str, 1024, errnum);
#else
char * error_str = strerror(errnum);
#endif
ss << "IO Exception (" << errno_ << "): " << error_str;
ss << ", file " << file_ << ", line " << line_ << ".";
e_what_ = ss.str();
}
explicit IOException (std::string file, int line, const char * description)
: file_(file), line_(line), errno_(0) {
std::stringstream ss;
ss << "IO Exception: " << description;
ss << ", file " << file_ << ", line " << line_ << ".";
e_what_ = ss.str();
}
virtual ~IOException() throw() {}
IOException (const IOException& other) : line_(other.line_), e_what_(other.e_what_), errno_(other.errno_) {}
int getErrorNumber () { return errno_; }
virtual const char* what () const throw () {
return e_what_.c_str();
}
};
class PortNotOpenedException : public std::exception
{
// Disable copy constructors
const PortNotOpenedException& operator=(PortNotOpenedException);
std::string e_what_;
public:
PortNotOpenedException (const char * description) {
std::stringstream ss;
ss << "PortNotOpenedException " << description << " failed.";
e_what_ = ss.str();
}
PortNotOpenedException (const PortNotOpenedException& other) : e_what_(other.e_what_) {}
virtual ~PortNotOpenedException() throw() {}
virtual const char* what () const throw () {
return e_what_.c_str();
}
};
/*!
* Structure that describes a serial device.
*/
struct PortInfo {
/*! Address of the serial port (this can be passed to the constructor of Serial). */
std::string port;
/*! Human readable description of serial device if available. */
std::string description;
/*! Hardware ID (e.g. VID:PID of USB serial devices) or "n/a" if not available. */
std::string hardware_id;
};
/* Lists the serial ports available on the system
*
* Returns a vector of available serial ports, each represented
* by a serial::PortInfo data structure:
*
* \return vector of serial::PortInfo.
*/
std::vector<PortInfo>
list_ports();
} // namespace serial
#endif

View file

@ -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_

View file

@ -0,0 +1,335 @@
#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.*");
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__)

View file

@ -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__)

View file

@ -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)

1058
src/serial/src/impl/unix.cc Executable file

File diff suppressed because it is too large Load diff

640
src/serial/src/impl/win.cc Normal file
View file

@ -0,0 +1,640 @@
#if defined(_WIN32)
/* Copyright 2012 William Woodall and John Harrison */
#include <sstream>
#include "serial/impl/win.h"
using std::string;
using std::wstring;
using std::stringstream;
using std::invalid_argument;
using serial::Serial;
using serial::Timeout;
using serial::bytesize_t;
using serial::parity_t;
using serial::stopbits_t;
using serial::flowcontrol_t;
using serial::SerialException;
using serial::PortNotOpenedException;
using serial::IOException;
inline wstring
_prefix_port_if_needed(const wstring &input)
{
static wstring windows_com_port_prefix = L"\\\\.\\";
if (input.compare(windows_com_port_prefix) != 0)
{
return windows_com_port_prefix + input;
}
return input;
}
Serial::SerialImpl::SerialImpl (const string &port, unsigned long baudrate,
bytesize_t bytesize,
parity_t parity, stopbits_t stopbits,
flowcontrol_t flowcontrol)
: port_ (port.begin(), port.end()), fd_ (INVALID_HANDLE_VALUE), is_open_ (false),
baudrate_ (baudrate), parity_ (parity),
bytesize_ (bytesize), stopbits_ (stopbits), flowcontrol_ (flowcontrol)
{
read_mutex = CreateMutex(NULL, false, NULL);
write_mutex = CreateMutex(NULL, false, NULL);
if (port_.empty () == false)
open ();
}
Serial::SerialImpl::~SerialImpl ()
{
this->close();
CloseHandle(read_mutex);
CloseHandle(write_mutex);
}
void
Serial::SerialImpl::open ()
{
if (port_.empty ()) {
throw invalid_argument ("Empty port is invalid.");
}
if (is_open_ == true) {
throw SerialException ("Serial port already open.");
}
// See: https://github.com/wjwwood/serial/issues/84
wstring port_with_prefix = _prefix_port_if_needed(port_);
LPCWSTR lp_port = port_with_prefix.c_str();
fd_ = CreateFileW(lp_port,
GENERIC_READ | GENERIC_WRITE,
0,
0,
OPEN_EXISTING,
FILE_ATTRIBUTE_NORMAL,
0);
if (fd_ == INVALID_HANDLE_VALUE) {
DWORD errno_ = GetLastError();
stringstream ss;
switch (errno_) {
case ERROR_FILE_NOT_FOUND:
// Use this->getPort to convert to a std::string
ss << "Specified port, " << this->getPort() << ", does not exist.";
THROW (IOException, ss.str().c_str());
default:
ss << "Unknown error opening the serial port: " << errno;
THROW (IOException, ss.str().c_str());
}
}
reconfigurePort();
is_open_ = true;
}
void
Serial::SerialImpl::reconfigurePort ()
{
if (fd_ == INVALID_HANDLE_VALUE) {
// Can only operate on a valid file descriptor
THROW (IOException, "Invalid file descriptor, is the serial port open?");
}
DCB dcbSerialParams = {0};
dcbSerialParams.DCBlength=sizeof(dcbSerialParams);
if (!GetCommState(fd_, &dcbSerialParams)) {
//error getting state
THROW (IOException, "Error getting the serial port state.");
}
// setup baud rate
switch (baudrate_) {
#ifdef CBR_0
case 0: dcbSerialParams.BaudRate = CBR_0; break;
#endif
#ifdef CBR_50
case 50: dcbSerialParams.BaudRate = CBR_50; break;
#endif
#ifdef CBR_75
case 75: dcbSerialParams.BaudRate = CBR_75; break;
#endif
#ifdef CBR_110
case 110: dcbSerialParams.BaudRate = CBR_110; break;
#endif
#ifdef CBR_134
case 134: dcbSerialParams.BaudRate = CBR_134; break;
#endif
#ifdef CBR_150
case 150: dcbSerialParams.BaudRate = CBR_150; break;
#endif
#ifdef CBR_200
case 200: dcbSerialParams.BaudRate = CBR_200; break;
#endif
#ifdef CBR_300
case 300: dcbSerialParams.BaudRate = CBR_300; break;
#endif
#ifdef CBR_600
case 600: dcbSerialParams.BaudRate = CBR_600; break;
#endif
#ifdef CBR_1200
case 1200: dcbSerialParams.BaudRate = CBR_1200; break;
#endif
#ifdef CBR_1800
case 1800: dcbSerialParams.BaudRate = CBR_1800; break;
#endif
#ifdef CBR_2400
case 2400: dcbSerialParams.BaudRate = CBR_2400; break;
#endif
#ifdef CBR_4800
case 4800: dcbSerialParams.BaudRate = CBR_4800; break;
#endif
#ifdef CBR_7200
case 7200: dcbSerialParams.BaudRate = CBR_7200; break;
#endif
#ifdef CBR_9600
case 9600: dcbSerialParams.BaudRate = CBR_9600; break;
#endif
#ifdef CBR_14400
case 14400: dcbSerialParams.BaudRate = CBR_14400; break;
#endif
#ifdef CBR_19200
case 19200: dcbSerialParams.BaudRate = CBR_19200; break;
#endif
#ifdef CBR_28800
case 28800: dcbSerialParams.BaudRate = CBR_28800; break;
#endif
#ifdef CBR_57600
case 57600: dcbSerialParams.BaudRate = CBR_57600; break;
#endif
#ifdef CBR_76800
case 76800: dcbSerialParams.BaudRate = CBR_76800; break;
#endif
#ifdef CBR_38400
case 38400: dcbSerialParams.BaudRate = CBR_38400; break;
#endif
#ifdef CBR_115200
case 115200: dcbSerialParams.BaudRate = CBR_115200; break;
#endif
#ifdef CBR_128000
case 128000: dcbSerialParams.BaudRate = CBR_128000; break;
#endif
#ifdef CBR_153600
case 153600: dcbSerialParams.BaudRate = CBR_153600; break;
#endif
#ifdef CBR_230400
case 230400: dcbSerialParams.BaudRate = CBR_230400; break;
#endif
#ifdef CBR_256000
case 256000: dcbSerialParams.BaudRate = CBR_256000; break;
#endif
#ifdef CBR_460800
case 460800: dcbSerialParams.BaudRate = CBR_460800; break;
#endif
#ifdef CBR_921600
case 921600: dcbSerialParams.BaudRate = CBR_921600; break;
#endif
default:
// Try to blindly assign it
dcbSerialParams.BaudRate = baudrate_;
}
// setup char len
if (bytesize_ == eightbits)
dcbSerialParams.ByteSize = 8;
else if (bytesize_ == sevenbits)
dcbSerialParams.ByteSize = 7;
else if (bytesize_ == sixbits)
dcbSerialParams.ByteSize = 6;
else if (bytesize_ == fivebits)
dcbSerialParams.ByteSize = 5;
else
throw invalid_argument ("invalid char len");
// setup stopbits
if (stopbits_ == stopbits_one)
dcbSerialParams.StopBits = ONESTOPBIT;
else if (stopbits_ == stopbits_one_point_five)
dcbSerialParams.StopBits = ONE5STOPBITS;
else if (stopbits_ == stopbits_two)
dcbSerialParams.StopBits = TWOSTOPBITS;
else
throw invalid_argument ("invalid stop bit");
// setup parity
if (parity_ == parity_none) {
dcbSerialParams.Parity = NOPARITY;
} else if (parity_ == parity_even) {
dcbSerialParams.Parity = EVENPARITY;
} else if (parity_ == parity_odd) {
dcbSerialParams.Parity = ODDPARITY;
} else if (parity_ == parity_mark) {
dcbSerialParams.Parity = MARKPARITY;
} else if (parity_ == parity_space) {
dcbSerialParams.Parity = SPACEPARITY;
} else {
throw invalid_argument ("invalid parity");
}
// setup flowcontrol
if (flowcontrol_ == flowcontrol_none) {
dcbSerialParams.fOutxCtsFlow = false;
dcbSerialParams.fRtsControl = 0x00;
dcbSerialParams.fOutX = false;
dcbSerialParams.fInX = false;
}
if (flowcontrol_ == flowcontrol_software) {
dcbSerialParams.fOutxCtsFlow = false;
dcbSerialParams.fRtsControl = 0x00;
dcbSerialParams.fOutX = true;
dcbSerialParams.fInX = true;
}
if (flowcontrol_ == flowcontrol_hardware) {
dcbSerialParams.fOutxCtsFlow = true;
dcbSerialParams.fRtsControl = 0x03;
dcbSerialParams.fOutX = false;
dcbSerialParams.fInX = false;
}
// activate settings
if (!SetCommState(fd_, &dcbSerialParams)){
CloseHandle(fd_);
THROW (IOException, "Error setting serial port settings.");
}
// Setup timeouts
COMMTIMEOUTS timeouts = {0};
timeouts.ReadIntervalTimeout = timeout_.inter_byte_timeout;
timeouts.ReadTotalTimeoutConstant = timeout_.read_timeout_constant;
timeouts.ReadTotalTimeoutMultiplier = timeout_.read_timeout_multiplier;
timeouts.WriteTotalTimeoutConstant = timeout_.write_timeout_constant;
timeouts.WriteTotalTimeoutMultiplier = timeout_.write_timeout_multiplier;
if (!SetCommTimeouts(fd_, &timeouts)) {
THROW (IOException, "Error setting timeouts.");
}
}
void
Serial::SerialImpl::close ()
{
if (is_open_ == true) {
if (fd_ != INVALID_HANDLE_VALUE) {
int ret;
ret = CloseHandle(fd_);
if (ret == 0) {
stringstream ss;
ss << "Error while closing serial port: " << GetLastError();
THROW (IOException, ss.str().c_str());
} else {
fd_ = INVALID_HANDLE_VALUE;
}
}
is_open_ = false;
}
}
bool
Serial::SerialImpl::isOpen () const
{
return is_open_;
}
size_t
Serial::SerialImpl::available ()
{
if (!is_open_) {
return 0;
}
COMSTAT cs;
if (!ClearCommError(fd_, NULL, &cs)) {
stringstream ss;
ss << "Error while checking status of the serial port: " << GetLastError();
THROW (IOException, ss.str().c_str());
}
return static_cast<size_t>(cs.cbInQue);
}
bool
Serial::SerialImpl::waitReadable (uint32_t timeout)
{
THROW (IOException, "waitReadable is not implemented on Windows.");
return false;
}
void
Serial::SerialImpl::waitByteTimes (size_t count)
{
THROW (IOException, "waitByteTimes is not implemented on Windows.");
}
size_t
Serial::SerialImpl::read (uint8_t *buf, size_t size)
{
if (!is_open_) {
throw PortNotOpenedException ("Serial::read");
}
DWORD bytes_read;
if (!ReadFile(fd_, buf, static_cast<DWORD>(size), &bytes_read, NULL)) {
stringstream ss;
ss << "Error while reading from the serial port: " << GetLastError();
THROW (IOException, ss.str().c_str());
}
return (size_t) (bytes_read);
}
size_t
Serial::SerialImpl::write (const uint8_t *data, size_t length)
{
if (is_open_ == false) {
throw PortNotOpenedException ("Serial::write");
}
DWORD bytes_written;
if (!WriteFile(fd_, data, static_cast<DWORD>(length), &bytes_written, NULL)) {
stringstream ss;
ss << "Error while writing to the serial port: " << GetLastError();
THROW (IOException, ss.str().c_str());
}
return (size_t) (bytes_written);
}
void
Serial::SerialImpl::setPort (const string &port)
{
port_ = wstring(port.begin(), port.end());
}
string
Serial::SerialImpl::getPort () const
{
return string(port_.begin(), port_.end());
}
void
Serial::SerialImpl::setTimeout (serial::Timeout &timeout)
{
timeout_ = timeout;
if (is_open_) {
reconfigurePort ();
}
}
serial::Timeout
Serial::SerialImpl::getTimeout () const
{
return timeout_;
}
void
Serial::SerialImpl::setBaudrate (unsigned long baudrate)
{
baudrate_ = baudrate;
if (is_open_) {
reconfigurePort ();
}
}
unsigned long
Serial::SerialImpl::getBaudrate () const
{
return baudrate_;
}
void
Serial::SerialImpl::setBytesize (serial::bytesize_t bytesize)
{
bytesize_ = bytesize;
if (is_open_) {
reconfigurePort ();
}
}
serial::bytesize_t
Serial::SerialImpl::getBytesize () const
{
return bytesize_;
}
void
Serial::SerialImpl::setParity (serial::parity_t parity)
{
parity_ = parity;
if (is_open_) {
reconfigurePort ();
}
}
serial::parity_t
Serial::SerialImpl::getParity () const
{
return parity_;
}
void
Serial::SerialImpl::setStopbits (serial::stopbits_t stopbits)
{
stopbits_ = stopbits;
if (is_open_) {
reconfigurePort ();
}
}
serial::stopbits_t
Serial::SerialImpl::getStopbits () const
{
return stopbits_;
}
void
Serial::SerialImpl::setFlowcontrol (serial::flowcontrol_t flowcontrol)
{
flowcontrol_ = flowcontrol;
if (is_open_) {
reconfigurePort ();
}
}
serial::flowcontrol_t
Serial::SerialImpl::getFlowcontrol () const
{
return flowcontrol_;
}
void
Serial::SerialImpl::flush ()
{
if (is_open_ == false) {
throw PortNotOpenedException ("Serial::flush");
}
FlushFileBuffers (fd_);
}
void
Serial::SerialImpl::flushInput ()
{
THROW (IOException, "flushInput is not supported on Windows.");
}
void
Serial::SerialImpl::flushOutput ()
{
THROW (IOException, "flushOutput is not supported on Windows.");
}
void
Serial::SerialImpl::sendBreak (int duration)
{
THROW (IOException, "sendBreak is not supported on Windows.");
}
void
Serial::SerialImpl::setBreak (bool level)
{
if (is_open_ == false) {
throw PortNotOpenedException ("Serial::setBreak");
}
if (level) {
EscapeCommFunction (fd_, SETBREAK);
} else {
EscapeCommFunction (fd_, CLRBREAK);
}
}
void
Serial::SerialImpl::setRTS (bool level)
{
if (is_open_ == false) {
throw PortNotOpenedException ("Serial::setRTS");
}
if (level) {
EscapeCommFunction (fd_, SETRTS);
} else {
EscapeCommFunction (fd_, CLRRTS);
}
}
void
Serial::SerialImpl::setDTR (bool level)
{
if (is_open_ == false) {
throw PortNotOpenedException ("Serial::setDTR");
}
if (level) {
EscapeCommFunction (fd_, SETDTR);
} else {
EscapeCommFunction (fd_, CLRDTR);
}
}
bool
Serial::SerialImpl::waitForChange ()
{
if (is_open_ == false) {
throw PortNotOpenedException ("Serial::waitForChange");
}
DWORD dwCommEvent;
if (!SetCommMask(fd_, EV_CTS | EV_DSR | EV_RING | EV_RLSD)) {
// Error setting communications mask
return false;
}
if (!WaitCommEvent(fd_, &dwCommEvent, NULL)) {
// An error occurred waiting for the event.
return false;
} else {
// Event has occurred.
return true;
}
}
bool
Serial::SerialImpl::getCTS ()
{
if (is_open_ == false) {
throw PortNotOpenedException ("Serial::getCTS");
}
DWORD dwModemStatus;
if (!GetCommModemStatus(fd_, &dwModemStatus)) {
THROW (IOException, "Error getting the status of the CTS line.");
}
return (MS_CTS_ON & dwModemStatus) != 0;
}
bool
Serial::SerialImpl::getDSR ()
{
if (is_open_ == false) {
throw PortNotOpenedException ("Serial::getDSR");
}
DWORD dwModemStatus;
if (!GetCommModemStatus(fd_, &dwModemStatus)) {
THROW (IOException, "Error getting the status of the DSR line.");
}
return (MS_DSR_ON & dwModemStatus) != 0;
}
bool
Serial::SerialImpl::getRI()
{
if (is_open_ == false) {
throw PortNotOpenedException ("Serial::getRI");
}
DWORD dwModemStatus;
if (!GetCommModemStatus(fd_, &dwModemStatus)) {
THROW (IOException, "Error getting the status of the RI line.");
}
return (MS_RING_ON & dwModemStatus) != 0;
}
bool
Serial::SerialImpl::getCD()
{
if (is_open_ == false) {
throw PortNotOpenedException ("Serial::getCD");
}
DWORD dwModemStatus;
if (!GetCommModemStatus(fd_, &dwModemStatus)) {
// Error in GetCommModemStatus;
THROW (IOException, "Error getting the status of the CD line.");
}
return (MS_RLSD_ON & dwModemStatus) != 0;
}
void
Serial::SerialImpl::readLock()
{
if (WaitForSingleObject(read_mutex, INFINITE) != WAIT_OBJECT_0) {
THROW (IOException, "Error claiming read mutex.");
}
}
void
Serial::SerialImpl::readUnlock()
{
if (!ReleaseMutex(read_mutex)) {
THROW (IOException, "Error releasing read mutex.");
}
}
void
Serial::SerialImpl::writeLock()
{
if (WaitForSingleObject(write_mutex, INFINITE) != WAIT_OBJECT_0) {
THROW (IOException, "Error claiming write mutex.");
}
}
void
Serial::SerialImpl::writeUnlock()
{
if (!ReleaseMutex(write_mutex)) {
THROW (IOException, "Error releasing write mutex.");
}
}
#endif // #if defined(_WIN32)

414
src/serial/src/serial.cc Executable file
View file

@ -0,0 +1,414 @@
/* Copyright 2012 William Woodall and John Harrison */
#include <algorithm>
#if !defined(_WIN32) && !defined(__OpenBSD__)
# include <alloca.h>
#endif
#if defined (__MINGW32__)
# define alloca __builtin_alloca
#endif
#include "serial/serial.h"
#ifdef _WIN32
#include "serial/impl/win.h"
#else
#include "serial/impl/unix.h"
#endif
using std::invalid_argument;
using std::min;
using std::numeric_limits;
using std::vector;
using std::size_t;
using std::string;
using serial::Serial;
using serial::SerialException;
using serial::IOException;
using serial::bytesize_t;
using serial::parity_t;
using serial::stopbits_t;
using serial::flowcontrol_t;
class Serial::ScopedReadLock {
public:
ScopedReadLock(SerialImpl *pimpl) : pimpl_(pimpl) {
this->pimpl_->readLock();
}
~ScopedReadLock() {
this->pimpl_->readUnlock();
}
private:
// Disable copy constructors
ScopedReadLock(const ScopedReadLock&);
const ScopedReadLock& operator=(ScopedReadLock);
SerialImpl *pimpl_;
};
class Serial::ScopedWriteLock {
public:
ScopedWriteLock(SerialImpl *pimpl) : pimpl_(pimpl) {
this->pimpl_->writeLock();
}
~ScopedWriteLock() {
this->pimpl_->writeUnlock();
}
private:
// Disable copy constructors
ScopedWriteLock(const ScopedWriteLock&);
const ScopedWriteLock& operator=(ScopedWriteLock);
SerialImpl *pimpl_;
};
Serial::Serial (const string &port, uint32_t baudrate, serial::Timeout timeout,
bytesize_t bytesize, parity_t parity, stopbits_t stopbits,
flowcontrol_t flowcontrol)
: pimpl_(new SerialImpl (port, baudrate, bytesize, parity,
stopbits, flowcontrol))
{
pimpl_->setTimeout(timeout);
}
Serial::~Serial ()
{
delete pimpl_;
}
void
Serial::open ()
{
pimpl_->open ();
}
void
Serial::close ()
{
pimpl_->close ();
}
bool
Serial::isOpen () const
{
return pimpl_->isOpen ();
}
size_t
Serial::available ()
{
return pimpl_->available ();
}
bool
Serial::waitReadable ()
{
serial::Timeout timeout(pimpl_->getTimeout ());
return pimpl_->waitReadable(timeout.read_timeout_constant);
}
void
Serial::waitByteTimes (size_t count)
{
pimpl_->waitByteTimes(count);
}
size_t
Serial::read_ (uint8_t *buffer, size_t size)
{
return this->pimpl_->read (buffer, size);
}
size_t
Serial::read (uint8_t *buffer, size_t size)
{
ScopedReadLock lock(this->pimpl_);
return this->pimpl_->read (buffer, size);
}
size_t
Serial::read (std::vector<uint8_t> &buffer, size_t size)
{
ScopedReadLock lock(this->pimpl_);
uint8_t *buffer_ = new uint8_t[size];
size_t bytes_read = this->pimpl_->read (buffer_, size);
buffer.insert (buffer.end (), buffer_, buffer_+bytes_read);
delete[] buffer_;
return bytes_read;
}
size_t
Serial::read (std::string &buffer, size_t size)
{
ScopedReadLock lock(this->pimpl_);
uint8_t *buffer_ = new uint8_t[size];
size_t bytes_read = this->pimpl_->read (buffer_, size);
buffer.append (reinterpret_cast<const char*>(buffer_), bytes_read);
delete[] buffer_;
return bytes_read;
}
string
Serial::read (size_t size)
{
std::string buffer;
this->read (buffer, size);
return buffer;
}
size_t
Serial::readline (string &buffer, size_t size, string eol)
{
ScopedReadLock lock(this->pimpl_);
size_t eol_len = eol.length ();
uint8_t *buffer_ = static_cast<uint8_t*>
(alloca (size * sizeof (uint8_t)));
size_t read_so_far = 0;
while (true)
{
size_t bytes_read = this->read_ (buffer_ + read_so_far, 1);
read_so_far += bytes_read;
if (bytes_read == 0) {
break; // Timeout occured on reading 1 byte
}
if (string (reinterpret_cast<const char*>
(buffer_ + read_so_far - eol_len), eol_len) == eol) {
break; // EOL found
}
if (read_so_far == size) {
break; // Reached the maximum read length
}
}
buffer.append(reinterpret_cast<const char*> (buffer_), read_so_far);
return read_so_far;
}
string
Serial::readline (size_t size, string eol)
{
std::string buffer;
this->readline (buffer, size, eol);
return buffer;
}
vector<string>
Serial::readlines (size_t size, string eol)
{
ScopedReadLock lock(this->pimpl_);
std::vector<std::string> lines;
size_t eol_len = eol.length ();
uint8_t *buffer_ = static_cast<uint8_t*>
(alloca (size * sizeof (uint8_t)));
size_t read_so_far = 0;
size_t start_of_line = 0;
while (read_so_far < size) {
size_t bytes_read = this->read_ (buffer_+read_so_far, 1);
read_so_far += bytes_read;
if (bytes_read == 0) {
if (start_of_line != read_so_far) {
lines.push_back (
string (reinterpret_cast<const char*> (buffer_ + start_of_line),
read_so_far - start_of_line));
}
break; // Timeout occured on reading 1 byte
}
if (string (reinterpret_cast<const char*>
(buffer_ + read_so_far - eol_len), eol_len) == eol) {
// EOL found
lines.push_back(
string(reinterpret_cast<const char*> (buffer_ + start_of_line),
read_so_far - start_of_line));
start_of_line = read_so_far;
}
if (read_so_far == size) {
if (start_of_line != read_so_far) {
lines.push_back(
string(reinterpret_cast<const char*> (buffer_ + start_of_line),
read_so_far - start_of_line));
}
break; // Reached the maximum read length
}
}
return lines;
}
size_t
Serial::write (const string &data)
{
ScopedWriteLock lock(this->pimpl_);
return this->write_ (reinterpret_cast<const uint8_t*>(data.c_str()),
data.length());
}
size_t
Serial::write (const std::vector<uint8_t> &data)
{
ScopedWriteLock lock(this->pimpl_);
return this->write_ (&data[0], data.size());
}
size_t
Serial::write (const uint8_t *data, size_t size)
{
ScopedWriteLock lock(this->pimpl_);
return this->write_(data, size);
}
size_t
Serial::write_ (const uint8_t *data, size_t length)
{
return pimpl_->write (data, length);
}
void
Serial::setPort (const string &port)
{
ScopedReadLock rlock(this->pimpl_);
ScopedWriteLock wlock(this->pimpl_);
bool was_open = pimpl_->isOpen ();
if (was_open) close();
pimpl_->setPort (port);
if (was_open) open ();
}
string
Serial::getPort () const
{
return pimpl_->getPort ();
}
void
Serial::setTimeout (serial::Timeout &timeout)
{
pimpl_->setTimeout (timeout);
}
serial::Timeout
Serial::getTimeout () const {
return pimpl_->getTimeout ();
}
void
Serial::setBaudrate (uint32_t baudrate)
{
pimpl_->setBaudrate (baudrate);
}
uint32_t
Serial::getBaudrate () const
{
return uint32_t(pimpl_->getBaudrate ());
}
void
Serial::setBytesize (bytesize_t bytesize)
{
pimpl_->setBytesize (bytesize);
}
bytesize_t
Serial::getBytesize () const
{
return pimpl_->getBytesize ();
}
void
Serial::setParity (parity_t parity)
{
pimpl_->setParity (parity);
}
parity_t
Serial::getParity () const
{
return pimpl_->getParity ();
}
void
Serial::setStopbits (stopbits_t stopbits)
{
pimpl_->setStopbits (stopbits);
}
stopbits_t
Serial::getStopbits () const
{
return pimpl_->getStopbits ();
}
void
Serial::setFlowcontrol (flowcontrol_t flowcontrol)
{
pimpl_->setFlowcontrol (flowcontrol);
}
flowcontrol_t
Serial::getFlowcontrol () const
{
return pimpl_->getFlowcontrol ();
}
void Serial::flush ()
{
ScopedReadLock rlock(this->pimpl_);
ScopedWriteLock wlock(this->pimpl_);
pimpl_->flush ();
}
void Serial::flushInput ()
{
ScopedReadLock lock(this->pimpl_);
pimpl_->flushInput ();
}
void Serial::flushOutput ()
{
ScopedWriteLock lock(this->pimpl_);
pimpl_->flushOutput ();
}
void Serial::sendBreak (int duration)
{
pimpl_->sendBreak (duration);
}
void Serial::setBreak (bool level)
{
pimpl_->setBreak (level);
}
void Serial::setRTS (bool level)
{
pimpl_->setRTS (level);
}
void Serial::setDTR (bool level)
{
pimpl_->setDTR (level);
}
bool Serial::waitForChange()
{
return pimpl_->waitForChange();
}
bool Serial::getCTS ()
{
return pimpl_->getCTS ();
}
bool Serial::getDSR ()
{
return pimpl_->getDSR ();
}
bool Serial::getRI ()
{
return pimpl_->getRI ();
}
bool Serial::getCD ()
{
return pimpl_->getCD ();
}

View file

@ -0,0 +1 @@
#include ""

View file

@ -0,0 +1,15 @@
#!/usr/bin/env python
import serial, sys
if len(sys.argv) != 2:
print "python: Usage_serial_test <port name like: /dev/ttyUSB0>"
sys.exit(1)
sio = serial.Serial(sys.argv[1], 115200)
sio.timeout = 250
while True:
sio.write("Testing.")
print sio.read(8)

View file

@ -0,0 +1,31 @@
#include <iostream>
#include <string>
#include <vector>
#include <boost/bind.hpp>
#include <boost/function.hpp>
#include <boost/algorithm/string.hpp>
#include <boost/foreach.hpp>
void
_delimeter_tokenizer (std::string &data, std::vector<std::string> &tokens,
std::string delimeter)
{
boost::split(tokens, data, boost::is_any_of(delimeter));
}
typedef boost::function<void(std::string&,std::vector<std::string>&)> TokenizerType;
int main(void) {
std::string data = "a\rb\rc\r";
std::vector<std::string> tokens;
std::string delimeter = "\r";
TokenizerType f = boost::bind(_delimeter_tokenizer, _1, _2, delimeter);
f(data, tokens);
BOOST_FOREACH(std::string token, tokens)
std::cout << token << std::endl;
return 0;
}

View file

@ -0,0 +1,63 @@
#include "gtest/gtest.h"
#include "serial/impl/unix.h"
#include <unistd.h>
#include <stdlib.h>
using serial::MillisecondTimer;
namespace {
/**
* Do 100 trials of timing gaps between 0 and 19 milliseconds.
* Expect accuracy within one millisecond.
*/
TEST(timer_tests, short_intervals) {
for (int trial = 0; trial < 100; trial++)
{
uint32_t ms = rand() % 20;
MillisecondTimer mt(ms);
usleep(1000 * ms);
int32_t r = mt.remaining();
// 1ms slush, for the cost of calling usleep.
EXPECT_NEAR(r+1, 0, 1);
}
}
TEST(timer_tests, overlapping_long_intervals) {
MillisecondTimer* timers[10];
// Experimentally determined. Corresponds to the extra time taken by the loops,
// the big usleep, and the test infrastructure itself.
const int slush_factor = 14;
// Set up the timers to each time one second, 1ms apart.
for (int t = 0; t < 10; t++)
{
timers[t] = new MillisecondTimer(1000);
usleep(1000);
}
// Check in on them after 500ms.
usleep(500000);
for (int t = 0; t < 10; t++)
{
EXPECT_NEAR(timers[t]->remaining(), 500 - slush_factor + t, 5);
}
// Check in on them again after another 500ms and free them.
usleep(500000);
for (int t = 0; t < 10; t++)
{
EXPECT_NEAR(timers[t]->remaining(), -slush_factor + t, 5);
delete timers[t];
}
}
} // namespace
int main(int argc, char **argv) {
::testing::InitGoogleTest(&argc, argv);
return RUN_ALL_TESTS();
}

View file

@ -0,0 +1,118 @@
/* To run these tests you need to change the define below to the serial port
* with a loop back device attached.
*
* Alternatively you could use an Arduino:
void setup()
{
Serial.begin(115200);
}
void loop()
{
while (Serial.available() > 0) {
Serial.write(Serial.read());
}
}
*/
#include <string>
#include "gtest/gtest.h"
#include <boost/bind.hpp>
// Use FRIEND_TEST... its not as nasty, thats what friends are for
// // OMG this is so nasty...
// #define private public
// #define protected public
#include "serial/serial.h"
#if defined(__linux__)
#include <pty.h>
#else
#include <util.h>
#endif
using namespace serial;
using std::string;
namespace {
class SerialTests : public ::testing::Test {
protected:
virtual void SetUp() {
if (openpty(&master_fd, &slave_fd, name, NULL, NULL) == -1) {
perror("openpty");
exit(127);
}
ASSERT_TRUE(master_fd > 0);
ASSERT_TRUE(slave_fd > 0);
ASSERT_TRUE(string(name).length() > 0);
port1 = new Serial(string(name), 115200, Timeout::simpleTimeout(250));
}
virtual void TearDown() {
port1->close();
delete port1;
}
Serial * port1;
int master_fd;
int slave_fd;
char name[100];
};
TEST_F(SerialTests, readWorks) {
write(master_fd, "abc\n", 4);
string r = port1->read(4);
EXPECT_EQ(r, string("abc\n"));
}
TEST_F(SerialTests, writeWorks) {
char buf[5] = "";
port1->write("abc\n");
read(master_fd, buf, 4);
EXPECT_EQ(string(buf, 4), string("abc\n"));
}
TEST_F(SerialTests, timeoutWorks) {
// Timeout a read, returns an empty string
string empty = port1->read();
EXPECT_EQ(empty, string(""));
// Ensure that writing/reading still works after a timeout.
write(master_fd, "abc\n", 4);
string r = port1->read(4);
EXPECT_EQ(r, string("abc\n"));
}
TEST_F(SerialTests, partialRead) {
// Write some data, but request more than was written.
write(master_fd, "abc\n", 4);
// Should timeout, but return what was in the buffer.
string empty = port1->read(10);
EXPECT_EQ(empty, string("abc\n"));
// Ensure that writing/reading still works after a timeout.
write(master_fd, "abc\n", 4);
string r = port1->read(4);
EXPECT_EQ(r, string("abc\n"));
}
} // namespace
int main(int argc, char **argv) {
try {
::testing::InitGoogleTest(&argc, argv);
return RUN_ALL_TESTS();
} catch (std::exception &e) {
std::cerr << "Unhandled Exception: " << e.what() << std::endl;
}
return 1;
}