123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219 |
-
- #ifndef __CSERIALPORTINFOUNIXBASE_H__
- #define __CSERIALPORTINFOUNIXBASE_H__
- #include "SerialPortInfoBase.h"
- #include "CSerialPort/osplatformutil.h"
- #ifdef I_OS_LINUX
- std::string get_driver(const std::string &tty)
- {
- struct stat st;
- std::string devicedir = tty;
-
-
- devicedir += "/device";
-
- if (lstat(devicedir.c_str(), &st) == 0 && S_ISLNK(st.st_mode))
- {
- char buffer[1024];
- memset(buffer, 0, sizeof(buffer));
-
-
-
- devicedir += "/driver";
- if (readlink(devicedir.c_str(), buffer, sizeof(buffer)) > 0)
- {
-
- return basename(buffer);
- }
- else
- {
-
- }
- }
- else
- {
-
- }
- return "";
- }
- void register_comport(vector<std::string> &comList, vector<std::string> &comList8250, const std::string &dir)
- {
-
- std::string driver = get_driver(dir);
-
- if (driver.size() > 0)
- {
- std::string devfile = std::string("/dev/") + basename(dir.c_str());
-
-
- if (driver == "serial8250")
- {
- comList8250.push_back(devfile);
- }
- else
- {
- comList.push_back(devfile);
- }
- }
- }
- void probe_serial8250_comports(vector<std::string> &comList, vector<std::string> comList8250)
- {
- struct serial_struct serinfo;
- vector<std::string>::iterator it = comList8250.begin();
-
- while (it != comList8250.end())
- {
-
- int fd = open((*it).c_str(), O_RDWR | O_NONBLOCK | O_NOCTTY);
- if (fd >= 0)
- {
-
- if (ioctl(fd, TIOCGSERIAL, &serinfo) == 0)
- {
-
- if (serinfo.type != PORT_UNKNOWN)
- {
- comList.push_back(*it);
-
- }
- else
- {
-
- }
- }
- close(fd);
- }
- ++it;
- }
- }
- vector<std::string> getPortInfoListLinux()
- {
-
- int n = -1;
- struct dirent **namelist;
- vector<std::string> comList;
- vector<std::string> comList8250;
- const char *sysDir = "/sys/class/tty/";
- const char *ptsDir = "/dev/pts/";
-
- n = scandir(sysDir, &namelist, NULL, NULL);
- if (n >= 0)
- {
- while (n--)
- {
- if (strcmp(namelist[n]->d_name, "..") && strcmp(namelist[n]->d_name, "."))
- {
-
- std::string devicedir = sysDir;
- devicedir += namelist[n]->d_name;
-
- register_comport(comList, comList8250, devicedir);
- }
- free(namelist[n]);
- }
- free(namelist);
- }
-
-
- probe_serial8250_comports(comList, comList8250);
-
- n = scandir(ptsDir, &namelist, NULL, NULL);
- if (n >= 0)
- {
- while (n--)
- {
- if (strcmp(namelist[n]->d_name, "..") && strcmp(namelist[n]->d_name, ".") &&
- strcmp(namelist[n]->d_name, "ptmx"))
- {
-
- std::string ptsName = ptsDir;
- ptsName += namelist[n]->d_name;
- comList.push_back(ptsName);
- }
- free(namelist[n]);
- }
- free(namelist);
- }
-
- return comList;
- }
- #endif
- vector<SerialPortInfo> getPortInfoList()
- {
- vector<SerialPortInfo> portInfoList;
- #ifdef I_OS_LINUX
-
- SerialPortInfo m_serialPort;
- vector<std::string> portList = getPortInfoListLinux();
- int count = portList.size();
- for (int i = 0; i < count; i++)
- {
- m_serialPort.portName = portList[i];
- portInfoList.push_back(m_serialPort);
- }
- #endif
- return portInfoList;
- }
- class CSerialPortInfoUnixBase : public CSerialPortInfoBase
- {
- public:
- CSerialPortInfoUnixBase(){}
- ~CSerialPortInfoUnixBase(){}
-
- static std::vector<SerialPortInfo> availablePortInfos(){
- return getPortInfoList();
- }
- };
- #endif
|