| 1 |
/*! |
| 2 |
\file |
| 3 |
\brief URG 用ポートの探索 |
| 4 |
|
| 5 |
\author Satofumi KAMIMURA |
| 6 |
|
| 7 |
$Id$ |
| 8 |
*/ |
| 9 |
|
| 10 |
#include "SearchUrgPort.h" |
| 11 |
#include "DetectOS.h" |
| 12 |
#if defined(WINDOWS_OS) |
| 13 |
#include "FindComPorts.h" |
| 14 |
#include "IsUsbUrgCom.h" |
| 15 |
#elif defined(LINUX_OS) || defined(MAC_OS) |
| 16 |
#include "FindFiles.h" |
| 17 |
#endif |
| 18 |
#include <boost/regex.hpp> |
| 19 |
|
| 20 |
using namespace beego; |
| 21 |
#if defined(LINUX_OS) || defined(MAC_OS) |
| 22 |
using namespace boost::xpressive; |
| 23 |
#endif |
| 24 |
|
| 25 |
|
| 26 |
struct SearchUrgPort::pImpl { |
| 27 |
std::string searched_pattern; |
| 28 |
|
| 29 |
pImpl(void) : searched_pattern("no port.") { |
| 30 |
} |
| 31 |
}; |
| 32 |
|
| 33 |
|
| 34 |
SearchUrgPort::SearchUrgPort(void) : pimpl(new pImpl) { |
| 35 |
} |
| 36 |
|
| 37 |
|
| 38 |
SearchUrgPort::~SearchUrgPort(void) { |
| 39 |
} |
| 40 |
|
| 41 |
|
| 42 |
#ifdef WINDOWS_OS |
| 43 |
std::vector<std::string> SearchUrgPort::search(void) { |
| 44 |
std::vector<std::string> ports = findComPorts(); |
| 45 |
|
| 46 |
// URG の COM ポートを先頭に持ってくる |
| 47 |
if (! ports.empty()) { |
| 48 |
size_t urg_ports_num = 0; |
| 49 |
for (std::vector<std::string>::iterator it = ports.begin(); |
| 50 |
it != ports.end(); ++it) { |
| 51 |
if (isUsbUrgCom(it->c_str())) { |
| 52 |
if (strcmp(ports[urg_ports_num].c_str(), it->c_str())) { |
| 53 |
std::swap(ports[urg_ports_num], *it); |
| 54 |
++urg_ports_num; |
| 55 |
} |
| 56 |
} |
| 57 |
} |
| 58 |
} |
| 59 |
return ports; |
| 60 |
} |
| 61 |
#else |
| 62 |
std::vector<std::string> SearchUrgPort::search(void) { |
| 63 |
std::vector<std::string> ports; |
| 64 |
|
| 65 |
// Linux の場合、/dev/ttyACMx, /dev/ttyUSBx, /dev/usb/ttyUSBx を列挙する |
| 66 |
findFiles(ports, "/dev/", sregex::compile("ttyACM[0-9]")); |
| 67 |
findFiles(ports, "/dev/", sregex::compile("ttyUSB[0-9]")); |
| 68 |
findFiles(ports, "/dev/usb", sregex::compile("ttyUSB[0-9]")); |
| 69 |
|
| 70 |
// !!! 時間があったら、上記の処理を配列化して、以下の情報を生成するようにする |
| 71 |
pimpl->searched_pattern = |
| 72 |
"/dev/ttyACM[0-9], /dev/ttyUSB[0-9], /dev/usb/ttyUSB[0-9]"; |
| 73 |
|
| 74 |
return ports; |
| 75 |
} |
| 76 |
#endif |
| 77 |
|
| 78 |
|
| 79 |
const char* SearchUrgPort::getSearchedPattern(void) { |
| 80 |
return pimpl->searched_pattern.c_str(); |
| 81 |
} |