2015-04-19 20:16:50 +00:00
|
|
|
#ifndef CDEVICEFINDER_HPP
|
|
|
|
#define CDEVICEFINDER_HPP
|
|
|
|
|
2015-04-21 04:02:43 +00:00
|
|
|
#include <set>
|
|
|
|
#include <mutex>
|
|
|
|
#include <stdexcept>
|
|
|
|
#include "CDeviceToken.hpp"
|
|
|
|
#include "IHIDListener.hpp"
|
2015-04-30 07:01:55 +00:00
|
|
|
#include "SDeviceSignature.hpp"
|
|
|
|
#include <string.h>
|
2015-04-21 04:02:43 +00:00
|
|
|
|
2015-05-04 04:28:07 +00:00
|
|
|
#ifdef _WIN32
|
|
|
|
#define _WIN32_LEAN_AND_MEAN 1
|
|
|
|
#include <windows.h>
|
|
|
|
#include <Dbt.h>
|
|
|
|
#endif
|
|
|
|
|
2015-04-29 10:24:39 +00:00
|
|
|
namespace boo
|
|
|
|
{
|
|
|
|
|
2015-04-21 04:02:43 +00:00
|
|
|
static class CDeviceFinder* skDevFinder = NULL;
|
|
|
|
|
|
|
|
class CDeviceFinder
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
friend class CHIDListenerIOKit;
|
|
|
|
friend class CHIDListenerUdev;
|
2015-05-03 06:40:20 +00:00
|
|
|
friend class CHIDListenerWinUSB;
|
2015-04-21 04:02:43 +00:00
|
|
|
static inline CDeviceFinder* instance() {return skDevFinder;}
|
|
|
|
|
|
|
|
private:
|
|
|
|
|
|
|
|
/* Types this finder is interested in (immutable) */
|
2015-04-30 07:01:55 +00:00
|
|
|
SDeviceSignature::TDeviceSignatureSet m_types;
|
2015-04-21 04:02:43 +00:00
|
|
|
|
|
|
|
/* Platform-specific USB event registration
|
|
|
|
* (for auto-scanning, NULL if not registered) */
|
|
|
|
IHIDListener* m_listener;
|
|
|
|
|
|
|
|
/* Set of presently-connected device tokens */
|
|
|
|
TDeviceTokens m_tokens;
|
|
|
|
std::mutex m_tokensLock;
|
|
|
|
|
|
|
|
/* Friend methods for platform-listener to find/insert/remove
|
|
|
|
* tokens with type-filtering */
|
2015-04-24 00:24:15 +00:00
|
|
|
inline bool _hasToken(const std::string& path)
|
2015-04-21 04:02:43 +00:00
|
|
|
{
|
2015-04-24 00:24:15 +00:00
|
|
|
auto preCheck = m_tokens.find(path);
|
2015-04-21 04:02:43 +00:00
|
|
|
if (preCheck != m_tokens.end())
|
|
|
|
return true;
|
|
|
|
return false;
|
|
|
|
}
|
2015-04-30 23:17:46 +00:00
|
|
|
inline bool _insertToken(CDeviceToken&& token)
|
2015-04-21 04:02:43 +00:00
|
|
|
{
|
2015-04-30 07:01:55 +00:00
|
|
|
if (SDeviceSignature::DeviceMatchToken(token, m_types)) {
|
2015-04-21 04:02:43 +00:00
|
|
|
m_tokensLock.lock();
|
2015-04-30 07:01:55 +00:00
|
|
|
TInsertedDeviceToken inseredTok =
|
|
|
|
m_tokens.insert(std::make_pair(token.getDevicePath(), std::move(token)));
|
2015-04-21 04:02:43 +00:00
|
|
|
m_tokensLock.unlock();
|
2015-04-22 21:48:23 +00:00
|
|
|
deviceConnected(inseredTok.first->second);
|
2015-04-30 23:17:46 +00:00
|
|
|
return true;
|
2015-04-21 04:02:43 +00:00
|
|
|
}
|
2015-04-30 23:17:46 +00:00
|
|
|
return false;
|
2015-04-21 04:02:43 +00:00
|
|
|
}
|
2015-04-24 00:24:15 +00:00
|
|
|
inline void _removeToken(const std::string& path)
|
2015-04-21 04:02:43 +00:00
|
|
|
{
|
2015-04-24 00:24:15 +00:00
|
|
|
auto preCheck = m_tokens.find(path);
|
2015-04-21 04:02:43 +00:00
|
|
|
if (preCheck != m_tokens.end())
|
|
|
|
{
|
|
|
|
CDeviceToken& tok = preCheck->second;
|
2015-04-23 00:46:32 +00:00
|
|
|
CDeviceBase* dev = tok.m_connectedDev;
|
2015-04-21 04:02:43 +00:00
|
|
|
tok._deviceClose();
|
2015-04-23 00:46:32 +00:00
|
|
|
deviceDisconnected(tok, dev);
|
2015-04-21 04:02:43 +00:00
|
|
|
m_tokensLock.lock();
|
|
|
|
m_tokens.erase(preCheck);
|
|
|
|
m_tokensLock.unlock();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public:
|
|
|
|
|
|
|
|
class CDeviceTokensHandle
|
|
|
|
{
|
|
|
|
CDeviceFinder& m_finder;
|
|
|
|
public:
|
|
|
|
inline CDeviceTokensHandle(CDeviceFinder& finder) : m_finder(finder)
|
|
|
|
{m_finder.m_tokensLock.lock();}
|
|
|
|
inline ~CDeviceTokensHandle() {m_finder.m_tokensLock.unlock();}
|
|
|
|
inline TDeviceTokens::iterator begin() {return m_finder.m_tokens.begin();}
|
|
|
|
inline TDeviceTokens::iterator end() {return m_finder.m_tokens.end();}
|
|
|
|
};
|
|
|
|
|
|
|
|
/* Application must specify its interested device-types */
|
2015-04-30 07:01:55 +00:00
|
|
|
CDeviceFinder(std::vector<const char*> types)
|
|
|
|
: m_listener(NULL)
|
2015-04-21 04:02:43 +00:00
|
|
|
{
|
|
|
|
if (skDevFinder)
|
|
|
|
throw std::runtime_error("only one instance of CDeviceFinder may be constructed");
|
|
|
|
skDevFinder = this;
|
2015-04-30 07:01:55 +00:00
|
|
|
for (const char* typeName : types)
|
|
|
|
{
|
|
|
|
const SDeviceSignature* sigIter = BOO_DEVICE_SIGS;
|
|
|
|
while (sigIter->m_name)
|
|
|
|
{
|
|
|
|
if (!strcmp(sigIter->m_name, typeName))
|
|
|
|
m_types.push_back(sigIter);
|
|
|
|
++sigIter;
|
|
|
|
}
|
|
|
|
}
|
2015-04-21 04:02:43 +00:00
|
|
|
}
|
|
|
|
~CDeviceFinder()
|
|
|
|
{
|
|
|
|
if (m_listener)
|
|
|
|
m_listener->stopScanning();
|
|
|
|
delete m_listener;
|
|
|
|
skDevFinder = NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Get interested device-type mask */
|
2015-04-30 07:01:55 +00:00
|
|
|
inline const SDeviceSignature::TDeviceSignatureSet& getTypes() const {return m_types;}
|
2015-04-21 04:02:43 +00:00
|
|
|
|
|
|
|
/* Iterable set of tokens */
|
|
|
|
inline CDeviceTokensHandle getTokens() {return CDeviceTokensHandle(*this);}
|
|
|
|
|
|
|
|
/* Automatic device scanning */
|
|
|
|
inline bool startScanning()
|
|
|
|
{
|
|
|
|
if (!m_listener)
|
|
|
|
m_listener = IHIDListenerNew(*this);
|
|
|
|
if (m_listener)
|
|
|
|
return m_listener->startScanning();
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
inline bool stopScanning()
|
|
|
|
{
|
|
|
|
if (!m_listener)
|
|
|
|
m_listener = IHIDListenerNew(*this);
|
|
|
|
if (m_listener)
|
|
|
|
return m_listener->stopScanning();
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Manual device scanning */
|
|
|
|
inline bool scanNow()
|
|
|
|
{
|
|
|
|
if (!m_listener)
|
|
|
|
m_listener = IHIDListenerNew(*this);
|
|
|
|
if (m_listener)
|
|
|
|
return m_listener->scanNow();
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2015-04-22 21:48:23 +00:00
|
|
|
virtual void deviceConnected(CDeviceToken&) {}
|
2015-04-23 00:46:32 +00:00
|
|
|
virtual void deviceDisconnected(CDeviceToken&, CDeviceBase*) {}
|
2015-05-04 04:28:07 +00:00
|
|
|
|
|
|
|
#if _WIN32
|
|
|
|
/* Windows-specific WM_DEVICECHANGED handler */
|
|
|
|
static LRESULT winDevChangedHandler(WPARAM wParam, LPARAM lParam)
|
|
|
|
{
|
|
|
|
PDEV_BROADCAST_HDR dbh = (PDEV_BROADCAST_HDR)lParam;
|
|
|
|
PDEV_BROADCAST_DEVICEINTERFACE dbhi = (PDEV_BROADCAST_DEVICEINTERFACE)lParam;
|
|
|
|
CDeviceFinder* finder = instance();
|
|
|
|
if (!finder)
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
if (wParam == DBT_DEVICEARRIVAL)
|
|
|
|
{
|
|
|
|
if (dbh->dbch_devicetype == DBT_DEVTYP_DEVICEINTERFACE)
|
|
|
|
{
|
|
|
|
#ifdef UNICODE
|
|
|
|
char devPath[1024];
|
|
|
|
wcstombs(devPath, dbhi->dbcc_name, 1024);
|
|
|
|
finder->m_listener->_extDevConnect(devPath);
|
|
|
|
#else
|
|
|
|
finder->m_listener->_extDevConnect(dbhi->dbcc_name);
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else if (wParam == DBT_DEVICEREMOVECOMPLETE)
|
|
|
|
{
|
|
|
|
if (dbh->dbch_devicetype == DBT_DEVTYP_DEVICEINTERFACE)
|
|
|
|
{
|
|
|
|
#ifdef UNICODE
|
|
|
|
char devPath[1024];
|
|
|
|
wcstombs(devPath, dbhi->dbcc_name, 1024);
|
|
|
|
finder->m_listener->_extDevDisconnect(devPath);
|
|
|
|
#else
|
|
|
|
finder->m_listener->_extDevDisconnect(dbhi->dbcc_name);
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
#endif
|
2015-04-22 21:48:23 +00:00
|
|
|
|
2015-04-21 04:02:43 +00:00
|
|
|
};
|
2015-04-19 20:16:50 +00:00
|
|
|
|
2015-04-29 10:24:39 +00:00
|
|
|
}
|
|
|
|
|
2015-04-19 20:16:50 +00:00
|
|
|
#endif // CDEVICEFINDER_HPP
|