/** Example:
|
/** Example:
|
* http://www.linux-tutorial.info/modules.php?name=Howto&pagename=Serial-Programming-HOWTO/x115.html#AEN125
|
* http://www.linux-tutorial.info/modules.php?name=Howto&pagename=Serial-Programming-HOWTO/x115.html#AEN125
|
*/
|
*/
|
#include "api_types.h"
|
#include "api_types.h"
|
#include "api_core.h"
|
#include "api_core.h"
|
#include "attribute.h"
|
#include "attribute.h"
|
#include "comport.h"
|
#include "comport.h"
|
|
|
#include <stdlib.h>
|
#include <stdlib.h>
|
#include <dirent.h>
|
#include <dirent.h>
|
#include <stdio.h> // standard input/output
|
#include <stdio.h> // standard input/output
|
#include <sys/types.h>
|
#include <sys/types.h>
|
#include <sys/stat.h>
|
#include <sys/stat.h>
|
#include <unistd.h> // unix standard function definition
|
#include <unistd.h> // unix standard function definition
|
#include <string.h> // strcpy, memset, memcpy
|
#include <string.h> // strcpy, memset, memcpy
|
#include <fcntl.h> // file control definition
|
#include <fcntl.h> // file control definition
|
#include <termios.h> // POSIX terminal control definition
|
#include <termios.h> // POSIX terminal control definition
|
#include <errno.h> // error number definition
|
#include <errno.h> // error number definition
|
#include <sys/ioctl.h>
|
#include <sys/ioctl.h>
|
#include <linux/serial.h>
|
#include <linux/serial.h>
|
|
|
#include <iostream>
|
#include <iostream>
|
#include <list>
|
#include <list>
|
|
|
namespace debugger {
|
namespace debugger {
|
|
|
using namespace std;
|
using namespace std;
|
|
|
static std::string checkDriverPresence(const std::string& tty) {
|
static std::string checkDriverPresence(const std::string& tty) {
|
struct stat st;
|
struct stat st;
|
std::string devicedir = tty;
|
std::string devicedir = tty;
|
|
|
// Append '/device' to the tty-path
|
// Append '/device' to the tty-path
|
devicedir += "/device";
|
devicedir += "/device";
|
|
|
// Stat the devicedir and handle it if it is a symlink
|
// Stat the devicedir and handle it if it is a symlink
|
if (lstat(devicedir.c_str(), &st)==0 && S_ISLNK(st.st_mode)) {
|
if (lstat(devicedir.c_str(), &st)==0 && S_ISLNK(st.st_mode)) {
|
char buffer[1024];
|
char buffer[1024];
|
memset(buffer, 0, sizeof(buffer));
|
memset(buffer, 0, sizeof(buffer));
|
|
|
// Append '/driver' and return basename of the target
|
// Append '/driver' and return basename of the target
|
devicedir += "/driver";
|
devicedir += "/driver";
|
|
|
if (readlink(devicedir.c_str(), buffer, sizeof(buffer)) > 0)
|
if (readlink(devicedir.c_str(), buffer, sizeof(buffer)) > 0)
|
return basename(buffer);
|
return basename(buffer);
|
}
|
}
|
return "";
|
return "";
|
}
|
}
|
|
|
/*static void register_comport(list<string>& comList,
|
/*static void register_comport(list<string>& comList,
|
list<string>& comList8250,
|
list<string>& comList8250,
|
const string& dir) {
|
const string& dir) {
|
// Get the driver the device is using
|
// Get the driver the device is using
|
std::string driver = checkDriverPresence(dir);
|
std::string driver = checkDriverPresence(dir);
|
|
|
// Skip devices without a driver
|
// Skip devices without a driver
|
if (driver.size() > 0) {
|
if (driver.size() > 0) {
|
string devfile = string("/dev/") + basename(dir.c_str());
|
string devfile = string("/dev/") + basename(dir.c_str());
|
|
|
// Put serial8250-devices in a seperate list
|
// Put serial8250-devices in a seperate list
|
if (driver == "serial8250") {
|
if (driver == "serial8250") {
|
comList8250.push_back(devfile);
|
comList8250.push_back(devfile);
|
} else
|
} else
|
comList.push_back(devfile);
|
comList.push_back(devfile);
|
}
|
}
|
}
|
}
|
|
|
static void probe_serial8250_comports(list<string>& comList,
|
static void probe_serial8250_comports(list<string>& comList,
|
list<string> comList8250) {
|
list<string> comList8250) {
|
struct serial_struct serinfo;
|
struct serial_struct serinfo;
|
list<string>::iterator it = comList8250.begin();
|
list<string>::iterator it = comList8250.begin();
|
|
|
// Iterate over all serial8250-devices
|
// Iterate over all serial8250-devices
|
while (it != comList8250.end()) {
|
while (it != comList8250.end()) {
|
|
|
// Try to open the device
|
// Try to open the device
|
int fd = open((*it).c_str(), O_RDWR | O_NONBLOCK | O_NOCTTY);
|
int fd = open((*it).c_str(), O_RDWR | O_NONBLOCK | O_NOCTTY);
|
|
|
if (fd >= 0) {
|
if (fd >= 0) {
|
// Get serial_info
|
// Get serial_info
|
if (ioctl(fd, TIOCGSERIAL, &serinfo)==0) {
|
if (ioctl(fd, TIOCGSERIAL, &serinfo)==0) {
|
// If device type is no PORT_UNKNOWN we accept the port
|
// If device type is no PORT_UNKNOWN we accept the port
|
if (serinfo.type != PORT_UNKNOWN)
|
if (serinfo.type != PORT_UNKNOWN)
|
comList.push_back(*it);
|
comList.push_back(*it);
|
}
|
}
|
close(fd);
|
close(fd);
|
}
|
}
|
it ++;
|
it ++;
|
}
|
}
|
}*/
|
}*/
|
|
|
/*
|
/*
|
* Enumerate all files in /sys/class/tty
|
* Enumerate all files in /sys/class/tty
|
*
|
*
|
* For each directory /sys/class/tty/foo, check if /sys/class/tty/foo/device
|
* For each directory /sys/class/tty/foo, check if /sys/class/tty/foo/device
|
* exists using lstat().
|
* exists using lstat().
|
*
|
*
|
* If it does not exist then you are dealing with some kind of virtual tty
|
* If it does not exist then you are dealing with some kind of virtual tty
|
* device (virtual console port, ptmx, etc...) and you can discard it.
|
* device (virtual console port, ptmx, etc...) and you can discard it.
|
* If it exists then retain serial port foo.
|
* If it exists then retain serial port foo.
|
*/
|
*/
|
void ComPortService::getSerialPortList(AttributeType *list) {
|
void ComPortService::getListOfPorts(AttributeType *list) {
|
int n;
|
int n;
|
struct dirent **namelist;
|
struct dirent **namelist;
|
const char* sysdir = "/sys/class/tty/";
|
const char* sysdir = "/sys/class/tty/";
|
AttributeType jsonPortInfo;
|
AttributeType jsonPortInfo;
|
list->make_list(0);
|
list->make_list(0);
|
|
|
// Scan through /sys/class/tty - it contains all tty-devices in the system
|
// Scan through /sys/class/tty - it contains all tty-devices in the system
|
n = scandir(sysdir, &namelist, NULL, NULL);
|
n = scandir(sysdir, &namelist, NULL, NULL);
|
if (n < 0) {
|
if (n < 0) {
|
RISCV_error("Can't scan '%s' directory", sysdir);
|
RISCV_error("Can't scan '%s' directory", sysdir);
|
return;
|
return;
|
}
|
}
|
while (n--) {
|
while (n--) {
|
if (!strcmp(namelist[n]->d_name, "..")
|
if (!strcmp(namelist[n]->d_name, "..")
|
|| !strcmp(namelist[n]->d_name, ".")) {
|
|| !strcmp(namelist[n]->d_name, ".")) {
|
free(namelist[n]);
|
free(namelist[n]);
|
continue;
|
continue;
|
}
|
}
|
|
|
// Construct full absolute file path
|
// Construct full absolute file path
|
std::string devicedir = sysdir;
|
std::string devicedir = sysdir;
|
devicedir += namelist[n]->d_name;
|
devicedir += namelist[n]->d_name;
|
|
|
// Get the driver the device is using.
|
// Get the driver the device is using.
|
// Skip devices without a driver
|
// Skip devices without a driver
|
std::string driver = checkDriverPresence(devicedir);
|
std::string driver = checkDriverPresence(devicedir);
|
if (driver.size() <= 0) {
|
if (driver.size() <= 0) {
|
continue;
|
continue;
|
}
|
}
|
std::string devfile = std::string("/dev/") + basename(devicedir.c_str());
|
std::string devfile = std::string("/dev/") + basename(devicedir.c_str());
|
|
|
jsonPortInfo.make_dict();
|
jsonPortInfo.make_dict();
|
jsonPortInfo["id"] = AttributeType(devfile.c_str());
|
jsonPortInfo["id"] = AttributeType(devfile.c_str());
|
list->add_to_list(&jsonPortInfo);
|
list->add_to_list(&jsonPortInfo);
|
free(namelist[n]);
|
free(namelist[n]);
|
}
|
}
|
free(namelist);
|
free(namelist);
|
|
|
// Only non-serial8250 has been added to comList without any further testing
|
// Only non-serial8250 has been added to comList without any further testing
|
// serial8250-devices must be probe to check for validity
|
// serial8250-devices must be probe to check for validity
|
//probe_serial8250_comports(comList, comList8250);
|
//probe_serial8250_comports(comList, comList8250);
|
}
|
}
|
|
|
|
|
int ComPortService::openSerialPort(const char *port, int baud, void *hdl) {
|
int ComPortService::openPort(const char *port, AttributeType settings) {
|
*((int *)hdl) = 0;
|
closePort();
|
int fd = open(port, O_RDWR | O_NOCTTY);// | O_NONBLOCK);// | O_NDELAY );
|
int fd = open(port, O_RDWR | O_NOCTTY);// | O_NONBLOCK);// | O_NDELAY );
|
//fcntl(fd, F_SETFL, fcntl(fd, F_GETFL) & ~O_NONBLOCK);
|
//fcntl(fd, F_SETFL, fcntl(fd, F_GETFL) & ~O_NONBLOCK);
|
if (fd < 0) {
|
if (fd < 0) {
|
RISCV_error("fopen() failed", NULL);
|
RISCV_error("fopen() failed", NULL);
|
return -1;
|
return -1;
|
}
|
}
|
|
|
struct termios options;
|
struct termios options;
|
memset(&options, 0, sizeof(options));
|
memset(&options, 0, sizeof(options));
|
|
|
/**
|
/**
|
* struct termios {
|
* struct termios {
|
* tcflag_t c_iflag; // input modes
|
* tcflag_t c_iflag; // input modes
|
* tcflag_t c_oflag; // output modes
|
* tcflag_t c_oflag; // output modes
|
* tcflag_t c_cflag; // control modes
|
* tcflag_t c_cflag; // control modes
|
* tcflag_t c_lflag; // local modes
|
* tcflag_t c_lflag; // local modes
|
* speed_t c_ispeed; // input speed
|
* speed_t c_ispeed; // input speed
|
* speed_t c_ospeed; // output speed
|
* speed_t c_ospeed; // output speed
|
* cc_t c_cc[NCCS]; // control characters
|
* cc_t c_cc[NCCS]; // control characters
|
* };
|
* };
|
* Input flags c_iflags:
|
* Input flags c_iflags:
|
* ICRNL - map CR to NL on input (NL = LF)
|
* ICRNL - map CR to NL on input (NL = LF)
|
* IGNCR - ignore CR
|
* IGNCR - ignore CR
|
* INLCR - map NL to CR
|
* INLCR - map NL to CR
|
* IXON - enable start/stop output control
|
* IXON - enable start/stop output control
|
* IXOFF - enable start/stop input control
|
* IXOFF - enable start/stop input control
|
* IXANY - allow any character to restart output
|
* IXANY - allow any character to restart output
|
* ISTRIP - strip character to seven bits
|
* ISTRIP - strip character to seven bits
|
* IGNPAR - ignore characters withparity error
|
* IGNPAR - ignore characters withparity error
|
* INPCK - enable input parity checking
|
* INPCK - enable input parity checking
|
* PARMRK - mark parity error by inserting '\377', '\0'
|
* PARMRK - mark parity error by inserting '\377', '\0'
|
* BRKINT - send SIGINT to the terminal when receiving break condition
|
* BRKINT - send SIGINT to the terminal when receiving break condition
|
* IGNBRK - ignore break condition
|
* IGNBRK - ignore break condition
|
*/
|
*/
|
options.c_iflag = IGNPAR;
|
options.c_iflag = IGNPAR;
|
|
|
/* Output mode c_oflag:
|
/* Output mode c_oflag:
|
* OPOST - perform output processing
|
* OPOST - perform output processing
|
* ONCLR - transform NL to CR NL
|
* ONCLR - transform NL to CR NL
|
* XTABS - Transform TAB into spaces
|
* XTABS - Transform TAB into spaces
|
* ONOEOT - discard EOT (^D) character
|
* ONOEOT - discard EOT (^D) character
|
*/
|
*/
|
options.c_oflag = 0; //
|
options.c_oflag = 0; //
|
|
|
/* Control modes c_cflag:
|
/* Control modes c_cflag:
|
* CLOCAL - ignore modem status lines
|
* CLOCAL - ignore modem status lines
|
* CREAD - enable receiver
|
* CREAD - enable receiver
|
* CSIZE - number of bits mask. Possible values: CS5, CS6, CS7, CS8
|
* CSIZE - number of bits mask. Possible values: CS5, CS6, CS7, CS8
|
* CSTOPB - send 2 stop bits instead of one.
|
* CSTOPB - send 2 stop bits instead of one.
|
* PARENB - enable parity generation
|
* PARENB - enable parity generation
|
* PARODD - generate odd parity if parity is generated
|
* PARODD - generate odd parity if parity is generated
|
* HUPCL - drop modem control lines on the last close of the terminal line
|
* HUPCL - drop modem control lines on the last close of the terminal line
|
*/
|
*/
|
options.c_cflag = B115200 | CRTSCTS | CS8 | CLOCAL | CREAD;
|
options.c_cflag = B115200 | CRTSCTS | CS8 | CLOCAL | CREAD;
|
|
|
/* Local modes c_lflag:
|
/* Local modes c_lflag:
|
* ECHO - enable echoing of input characters
|
* ECHO - enable echoing of input characters
|
* ECHOE - if ICANNON an ECHO are set then echo ERASE and KILL as one
|
* ECHOE - if ICANNON an ECHO are set then echo ERASE and KILL as one
|
* or more backspace-space-backspace sequences (to wipe entire
|
* or more backspace-space-backspace sequences (to wipe entire
|
* line)
|
* line)
|
* ECHOK - output an NL after the KILL character
|
* ECHOK - output an NL after the KILL character
|
* ECHONL - echo NL even if ECHO is not set
|
* ECHONL - echo NL even if ECHO is not set
|
* ICANON - cannonical input. This enables line oriented input (not raw case).
|
* ICANON - cannonical input. This enables line oriented input (not raw case).
|
* IEXTEN - enable implementation defined input extensions
|
* IEXTEN - enable implementation defined input extensions
|
* ISIG - enable signal character INTR, QUIT, SUSP
|
* ISIG - enable signal character INTR, QUIT, SUSP
|
* NOFLSH - disable flushing of the input/output queues that is normally
|
* NOFLSH - disable flushing of the input/output queues that is normally
|
* done if a signal is sent
|
* done if a signal is sent
|
* TOSTOP - send a SIGTTOU if job control is implemented
|
* TOSTOP - send a SIGTTOU if job control is implemented
|
*/
|
*/
|
options.c_lflag = 0; // no signaling chars, no echo
|
options.c_lflag = 0; // no signaling chars, no echo
|
|
|
/* Available fields:
|
/* Available fields:
|
* VEOF, VEOL, VERASE, VINTR, VKILL, VMIN, VQUIT, VTIME, VSUSP, VSTART,
|
* VEOF, VEOL, VERASE, VINTR, VKILL, VMIN, VQUIT, VTIME, VSUSP, VSTART,
|
* VSTOP, VREPRINT, VLNEXT and VDISCARD
|
* VSTOP, VREPRINT, VLNEXT and VDISCARD
|
*/
|
*/
|
options.c_cc[VTIME] = 0; // inter-character timer unesed
|
options.c_cc[VTIME] = 0; // inter-character timer unesed
|
options.c_cc[VMIN] = 0; // blocking read until 5 chars received
|
options.c_cc[VMIN] = 0; // blocking read until 5 chars received
|
|
|
|
|
tcflush(fd, TCIOFLUSH);
|
tcflush(fd, TCIOFLUSH);
|
// TCSANOW - change occurs immediatly
|
// TCSANOW - change occurs immediatly
|
// TCSADRAIN - change occurs after all parameters are written
|
// TCSADRAIN - change occurs after all parameters are written
|
// TCSAFLUSH - ..
|
// TCSAFLUSH - ..
|
if(tcsetattr(fd, TCSANOW, &options) == -1) {
|
if(tcsetattr(fd, TCSANOW, &options) == -1) {
|
RISCV_error("tcsetattr() failed", NULL);
|
RISCV_error("tcsetattr() failed", NULL);
|
return -1;
|
return -1;
|
}
|
}
|
|
|
// Empty buffers
|
// Empty buffers
|
tcflush(fd, TCIOFLUSH);
|
tcflush(fd, TCIOFLUSH);
|
*((int *)hdl) = fd;
|
prtHandler_ = fd;
|
return 0 ;
|
return 0 ;
|
}
|
}
|
|
|
void ComPortService::closeSerialPort(void *hdl) {
|
void ComPortService::closePort() {
|
close(*((int *)hdl));
|
if (prtHandler_) {
|
|
close(prtHandler_);
|
|
}
|
|
prtHandler_ = 0;
|
}
|
}
|
|
|
int ComPortService::readSerialPort(void *hdl, char *buf, int bufsz) {
|
int ComPortService::readSerialPort(void *hdl, char *buf, int bufsz) {
|
return read(*((int *)hdl), buf, bufsz-1);
|
int sz = read(*((int *)hdl), buf, bufsz-1);
|
|
buf[sz] = 0;
|
|
return sz;
|
}
|
}
|
|
|
int ComPortService::writeSerialPort(void *hdl, char *buf, int bufsz) {
|
int ComPortService::writeSerialPort(void *hdl, char *buf, int bufsz) {
|
return write(*((int *)hdl), buf, bufsz);
|
return write(*((int *)hdl), buf, bufsz);
|
}
|
}
|
|
|
void ComPortService::cleanSerialPort(void *hdl) {
|
void ComPortService::cleanSerialPort(void *hdl) {
|
tcflush(*((int *)hdl), TCIOFLUSH);
|
tcflush(*((int *)hdl), TCIOFLUSH);
|
}
|
}
|
|
|
} // namespace debugger
|
} // namespace debugger
|
|
|