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.cpp 5.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  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. ** All right reserved.
  9. ** Web: http://code.google.com/p/qextserialport/
  10. **
  11. ** Permission is hereby granted, free of charge, to any person obtaining
  12. ** a copy of this software and associated documentation files (the
  13. ** "Software"), to deal in the Software without restriction, including
  14. ** without limitation the rights to use, copy, modify, merge, publish,
  15. ** distribute, sublicense, and/or sell copies of the Software, and to
  16. ** permit persons to whom the Software is furnished to do so, subject to
  17. ** the following conditions:
  18. **
  19. ** The above copyright notice and this permission notice shall be
  20. ** included in all copies or substantial portions of the Software.
  21. **
  22. ** THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  23. ** EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  24. ** MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  25. ** NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
  26. ** LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
  27. ** OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  28. ** WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  29. **
  30. ****************************************************************************/
  31. #include "qextserialenumerator.h"
  32. #include "qextserialenumerator_p.h"
  33. #include <QtCore/QDebug>
  34. #include <QtCore/QMetaType>
  35. #include <QtCore/QRegExp>
  36. QextSerialEnumeratorPrivate::QextSerialEnumeratorPrivate(QextSerialEnumerator *enumrator)
  37. :q_ptr(enumrator)
  38. {
  39. platformSpecificInit();
  40. }
  41. QextSerialEnumeratorPrivate::~QextSerialEnumeratorPrivate()
  42. {
  43. platformSpecificDestruct();
  44. }
  45. /*!
  46. \class QextPortInfo
  47. \brief The QextPortInfo class containing port information.
  48. Structure containing port information.
  49. \code
  50. QString portName; ///< Port name.
  51. QString physName; ///< Physical name.
  52. QString friendName; ///< Friendly name.
  53. QString enumName; ///< Enumerator name.
  54. int vendorID; ///< Vendor ID.
  55. int productID; ///< Product ID
  56. \endcode
  57. */
  58. /*! \class QextSerialEnumerator
  59. \brief The QextSerialEnumerator class provides list of ports available in the system.
  60. \section1 Usage
  61. To poll the system for a list of connected devices, simply use getPorts(). Each
  62. QextPortInfo structure will populated with information about the corresponding device.
  63. \bold Example
  64. \code
  65. QList<QextPortInfo> ports = QextSerialEnumerator::getPorts();
  66. foreach( QextPortInfo port, ports ) {
  67. // inspect port...
  68. }
  69. \endcode
  70. To enable event-driven notification of device connection events, first call
  71. setUpNotifications() and then connect to the deviceDiscovered() and deviceRemoved()
  72. signals. Event-driven behavior is currently available only on Windows and OS X.
  73. \bold Example
  74. \code
  75. QextSerialEnumerator* enumerator = new QextSerialEnumerator();
  76. connect(enumerator, SIGNAL(deviceDiscovered(const QextPortInfo &)),
  77. myClass, SLOT(onDeviceDiscovered(const QextPortInfo &)));
  78. connect(enumerator, SIGNAL(deviceRemoved(const QextPortInfo &)),
  79. myClass, SLOT(onDeviceRemoved(const QextPortInfo &)));
  80. \endcode
  81. \section1 Credits
  82. Windows implementation is based on Zach Gorman's work from
  83. \l {http://www.codeproject.com}{The Code Project} (\l http://www.codeproject.com/system/setupdi.asp).
  84. OS X implementation, see \l http://developer.apple.com/documentation/DeviceDrivers/Conceptual/AccessingHardware/AH_Finding_Devices/chapter_4_section_2.html
  85. \bold author Michal Policht, Liam Staskawicz
  86. */
  87. /*!
  88. \fn void QextSerialEnumerator::deviceDiscovered( const QextPortInfo & info )
  89. A new device has been connected to the system.
  90. setUpNotifications() must be called first to enable event-driven device notifications.
  91. Currently only implemented on Windows and OS X.
  92. \a info The device that has been discovered.
  93. */
  94. /*!
  95. \fn void QextSerialEnumerator::deviceRemoved( const QextPortInfo & info );
  96. A device has been disconnected from the system.
  97. setUpNotifications() must be called first to enable event-driven device notifications.
  98. Currently only implemented on Windows and OS X.
  99. \a info The device that was disconnected.
  100. */
  101. /*!
  102. Constructs a QextSerialEnumerator object with the given \a parent.
  103. */
  104. QextSerialEnumerator::QextSerialEnumerator(QObject *parent)
  105. :QObject(parent), d_ptr(new QextSerialEnumeratorPrivate(this))
  106. {
  107. if( !QMetaType::isRegistered( QMetaType::type("QextPortInfo") ) )
  108. qRegisterMetaType<QextPortInfo>("QextPortInfo");
  109. }
  110. /*!
  111. Destructs the QextSerialEnumerator object.
  112. */
  113. QextSerialEnumerator::~QextSerialEnumerator( )
  114. {
  115. delete d_ptr;
  116. }
  117. /*!
  118. Get list of ports.
  119. return list of ports currently available in the system.
  120. */
  121. QList<QextPortInfo> QextSerialEnumerator::getPorts()
  122. {
  123. #if defined(Q_OS_UNIX) && !defined(Q_OS_LINUX) && !defined(Q_OS_MAC)
  124. qCritical("Enumeration for POSIX systems (except Linux) is not implemented yet.");
  125. #endif
  126. return QextSerialEnumeratorPrivate::getPorts_sys();
  127. }
  128. /*!
  129. Enable event-driven notifications of board discovery/removal.
  130. */
  131. void QextSerialEnumerator::setUpNotifications()
  132. {
  133. #if defined(Q_OS_UNIX) && !defined(Q_OS_MAC)
  134. qCritical("Notifications for *Nix/FreeBSD are not implemented yet");
  135. #endif
  136. Q_D(QextSerialEnumerator);
  137. if (!d->setUpNotifications_sys(true))
  138. QESP_WARNING("Setup Notification Failed...");
  139. }