Typica is a free program for professional coffee roasters. https://typica.us
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

qextserialenumerator_linux.cpp 7.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210
  1. /****************************************************************************
  2. ** Copyright (c) 2000-2003 Wayne Roth
  3. ** Copyright (c) 2004-2007 Stefan Sander
  4. ** Copyright (c) 2007 Michal Policht
  5. ** Copyright (c) 2008 Brandon Fosdick
  6. ** Copyright (c) 2009-2010 Liam Staskawicz
  7. ** Copyright (c) 2011 Debao Zhang
  8. ** Copyright (c) 2012 Doug Brown
  9. ** All right reserved.
  10. ** Web: http://code.google.com/p/qextserialport/
  11. **
  12. ** Permission is hereby granted, free of charge, to any person obtaining
  13. ** a copy of this software and associated documentation files (the
  14. ** "Software"), to deal in the Software without restriction, including
  15. ** without limitation the rights to use, copy, modify, merge, publish,
  16. ** distribute, sublicense, and/or sell copies of the Software, and to
  17. ** permit persons to whom the Software is furnished to do so, subject to
  18. ** the following conditions:
  19. **
  20. ** The above copyright notice and this permission notice shall be
  21. ** included in all copies or substantial portions of the Software.
  22. **
  23. ** THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  24. ** EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  25. ** MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  26. ** NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
  27. ** LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
  28. ** OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  29. ** WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  30. **
  31. ****************************************************************************/
  32. #include "qextserialenumerator.h"
  33. #include "qextserialenumerator_p.h"
  34. #include <QtCore/QDebug>
  35. #include <QtCore/QStringList>
  36. #include <QtCore/QDir>
  37. void QextSerialEnumeratorPrivate::init_sys()
  38. {
  39. #ifndef QESP_NO_UDEV
  40. monitor = NULL;
  41. notifierFd = -1;
  42. notifier = NULL;
  43. udev = udev_new();
  44. if (!udev)
  45. qCritical() << "Unable to initialize udev notifications";
  46. #endif
  47. }
  48. void QextSerialEnumeratorPrivate::destroy_sys()
  49. {
  50. #ifndef QESP_NO_UDEV
  51. if (notifier) {
  52. notifier->setEnabled(false);
  53. delete notifier;
  54. }
  55. if (monitor)
  56. udev_monitor_unref(monitor);
  57. if (udev)
  58. udev_unref(udev);
  59. #endif
  60. }
  61. #ifndef QESP_NO_UDEV
  62. static QextPortInfo portInfoFromDevice(struct udev_device *dev)
  63. {
  64. QString vendor = QString::fromLatin1(udev_device_get_property_value(dev, "ID_VENDOR_ID"));
  65. QString product = QString::fromLatin1(udev_device_get_property_value(dev, "ID_MODEL_ID"));
  66. QextPortInfo pi;
  67. pi.vendorID = vendor.toInt(0, 16);
  68. pi.productID = product.toInt(0, 16);
  69. pi.portName = QString::fromLatin1(udev_device_get_devnode(dev));
  70. pi.physName = pi.portName;
  71. return pi;
  72. }
  73. #endif
  74. QList<QextPortInfo> QextSerialEnumeratorPrivate::getPorts_sys()
  75. {
  76. QList<QextPortInfo> infoList;
  77. #ifndef QESP_NO_UDEV
  78. struct udev *ud = udev_new();
  79. if (!ud) {
  80. qCritical() << "Unable to enumerate ports because udev is not initialized.";
  81. return infoList;
  82. }
  83. struct udev_enumerate *enumerate = udev_enumerate_new(ud);
  84. udev_enumerate_add_match_subsystem(enumerate, "tty");
  85. udev_enumerate_scan_devices(enumerate);
  86. struct udev_list_entry *list = udev_enumerate_get_list_entry(enumerate);
  87. struct udev_list_entry *entry;
  88. udev_list_entry_foreach(entry, list) {
  89. const char *path;
  90. struct udev_device *dev;
  91. // Have to grab the actual udev device here...
  92. path = udev_list_entry_get_name(entry);
  93. dev = udev_device_new_from_syspath(ud, path);
  94. infoList.append(portInfoFromDevice(dev));
  95. // Done with this device
  96. udev_device_unref(dev);
  97. }
  98. // Done with the list and this udev
  99. udev_enumerate_unref(enumerate);
  100. udev_unref(ud);
  101. #else
  102. QStringList portNamePrefixes, portNameList;
  103. portNamePrefixes << QLatin1String("ttyS*"); // list normal serial ports first
  104. QDir dir(QLatin1String("/dev"));
  105. portNameList = dir.entryList(portNamePrefixes, (QDir::System | QDir::Files), QDir::Name);
  106. // remove the values which are not serial ports for e.g. /dev/ttysa
  107. for (int i = 0; i < portNameList.size(); i++) {
  108. bool ok;
  109. QString current = portNameList.at(i);
  110. // remove the ttyS part, and check, if the other part is a number
  111. current.remove(0,4).toInt(&ok, 10);
  112. if (!ok) {
  113. portNameList.removeAt(i);
  114. i--;
  115. }
  116. }
  117. // get the non standard serial ports names
  118. // (USB-serial, bluetooth-serial, 18F PICs, and so on)
  119. // if you know an other name prefix for serial ports please let us know
  120. portNamePrefixes.clear();
  121. portNamePrefixes << QLatin1String("ttyACM*") << QLatin1String("ttyUSB*") << QLatin1String("rfcomm*") << QLatin1String("tnt*");
  122. portNameList += dir.entryList(portNamePrefixes, (QDir::System | QDir::Files), QDir::Name);
  123. foreach (QString str , portNameList) {
  124. QextPortInfo inf;
  125. inf.physName = QLatin1String("/dev/")+str;
  126. inf.portName = str;
  127. if (str.contains(QLatin1String("ttyS"))) {
  128. inf.friendName = QLatin1String("Serial port ")+str.remove(0, 4);
  129. }
  130. else if (str.contains(QLatin1String("ttyUSB"))) {
  131. inf.friendName = QLatin1String("USB-serial adapter ")+str.remove(0, 6);
  132. }
  133. else if (str.contains(QLatin1String("rfcomm"))) {
  134. inf.friendName = QLatin1String("Bluetooth-serial adapter ")+str.remove(0, 6);
  135. }
  136. inf.enumName = QLatin1String("/dev"); // is there a more helpful name for this?
  137. infoList.append(inf);
  138. }
  139. #endif
  140. return infoList;
  141. }
  142. bool QextSerialEnumeratorPrivate::setUpNotifications_sys(bool setup)
  143. {
  144. Q_UNUSED(setup);
  145. #ifndef QESP_NO_UDEV
  146. Q_Q(QextSerialEnumerator);
  147. if (!udev) {
  148. qCritical() << "Unable to initialize notifications because udev is not initialized.";
  149. return false;
  150. }
  151. // Emit signals immediately for devices already connected (Windows version seems to behave
  152. // this way)
  153. foreach (QextPortInfo i, getPorts_sys())
  154. Q_EMIT q->deviceDiscovered(i);
  155. // Look for tty devices from udev.
  156. monitor = udev_monitor_new_from_netlink(udev, "udev");
  157. udev_monitor_filter_add_match_subsystem_devtype(monitor, "tty", NULL);
  158. udev_monitor_enable_receiving(monitor);
  159. notifierFd = udev_monitor_get_fd(monitor);
  160. notifier = new QSocketNotifier(notifierFd, QSocketNotifier::Read);
  161. q->connect(notifier, SIGNAL(activated(int)), q, SLOT(_q_deviceEvent()));
  162. notifier->setEnabled(true);
  163. return true;
  164. #else
  165. return false;
  166. #endif
  167. }
  168. #ifndef QESP_NO_UDEV
  169. void QextSerialEnumeratorPrivate::_q_deviceEvent()
  170. {
  171. Q_Q(QextSerialEnumerator);
  172. struct udev_device *dev = udev_monitor_receive_device(monitor);
  173. if (dev) {
  174. QextPortInfo pi = portInfoFromDevice(dev);
  175. QLatin1String action(udev_device_get_action(dev));
  176. if (action == QLatin1String("add"))
  177. Q_EMIT q->deviceDiscovered(pi);
  178. else if (action == QLatin1String("remove"))
  179. Q_EMIT q->deviceRemoved(pi);
  180. udev_device_unref(dev);
  181. }
  182. }
  183. #endif