Fix Bluetooth Ping Timeout
This commit is contained in:
+29
-44
@@ -45,59 +45,44 @@ void Device::UpdatePresentDevices()
|
|||||||
if (Functions::Ping(*it))
|
if (Functions::Ping(*it))
|
||||||
{
|
{
|
||||||
if (std::find(m_presentDevices.begin(), m_presentDevices.end(), *it) == m_presentDevices.end())
|
if (std::find(m_presentDevices.begin(), m_presentDevices.end(), *it) == m_presentDevices.end())
|
||||||
addedDevices.push_back(*it);
|
SendStateChange(true, *it);
|
||||||
presentDevices.push_back(*it);
|
presentDevices.push_back(*it);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
for (std::vector<std::string>::iterator it = m_presentDevices.begin(); it != m_presentDevices.end(); ++it)
|
for (std::vector<std::string>::iterator it = m_presentDevices.begin(); it != m_presentDevices.end(); ++it)
|
||||||
if (std::find(presentDevices.begin(), presentDevices.end(), *it) == presentDevices.end())
|
if (std::find(presentDevices.begin(), presentDevices.end(), *it) == presentDevices.end())
|
||||||
removedDevices.push_back(*it);
|
SendStateChange(false, *it);
|
||||||
|
|
||||||
for (std::vector<std::string>::iterator it = addedDevices.begin(); it != addedDevices.end(); ++it)
|
|
||||||
{
|
|
||||||
std::stringstream ss;
|
|
||||||
ss << "Bluetooth: + " << *it;
|
|
||||||
syslog(LOG_INFO, "%s", ss.str().c_str());
|
|
||||||
|
|
||||||
std::stringstream url;
|
|
||||||
url << m_target << "/Bluetooth/+/" << *it;
|
|
||||||
|
|
||||||
try
|
|
||||||
{
|
|
||||||
m_httpClient.GetUrlSilent(url.str());
|
|
||||||
}
|
|
||||||
catch (const std::exception& e)
|
|
||||||
{
|
|
||||||
std::stringstream ss;
|
|
||||||
ss << "BluetoothDevice::UpdatePresentDevices() - Error: " << e.what() << std::endl;
|
|
||||||
syslog(LOG_ERR, "%s", ss.str().c_str());
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
for (std::vector<std::string>::iterator it = removedDevices.begin(); it != removedDevices.end(); ++it)
|
|
||||||
{
|
|
||||||
std::stringstream ss;
|
|
||||||
ss << "Bluetooth: - " << *it;
|
|
||||||
syslog(LOG_INFO, "%s", ss.str().c_str());
|
|
||||||
|
|
||||||
std::stringstream url;
|
|
||||||
url << m_target << "/Bluetooth/-/" << *it;
|
|
||||||
|
|
||||||
try
|
|
||||||
{
|
|
||||||
m_httpClient.GetUrlSilent(url.str());
|
|
||||||
}
|
|
||||||
catch (const std::exception& e)
|
|
||||||
{
|
|
||||||
std::stringstream ss;
|
|
||||||
ss << "BluetoothDevice::UpdatePresentDevices() - Error: " << e.what() << std::endl;
|
|
||||||
syslog(LOG_ERR, "%s", ss.str().c_str());
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
m_presentDevices.assign(presentDevices.begin(), presentDevices.end());
|
m_presentDevices.assign(presentDevices.begin(), presentDevices.end());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void Device::SendStateChange(bool present, const std::string& macAddress)
|
||||||
|
{
|
||||||
|
char sign;
|
||||||
|
if (present)
|
||||||
|
sign = '+';
|
||||||
|
else
|
||||||
|
sign = '-';
|
||||||
|
|
||||||
|
std::stringstream ss;
|
||||||
|
ss << "Bluetooth: " << sign << " " << macAddress;
|
||||||
|
syslog(LOG_INFO, "%s", ss.str().c_str());
|
||||||
|
|
||||||
|
std::stringstream url;
|
||||||
|
url << m_target << "/Bluetooth/" << sign << "/" << macAddress;
|
||||||
|
|
||||||
|
try
|
||||||
|
{
|
||||||
|
m_httpClient.GetUrlSilent(url.str());
|
||||||
|
}
|
||||||
|
catch (const std::exception& e)
|
||||||
|
{
|
||||||
|
std::stringstream ss;
|
||||||
|
ss << "Bluetooth::Device::SendStateChange() - Error: " << e.what() << std::endl;
|
||||||
|
syslog(LOG_ERR, "%s", ss.str().c_str());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
} // namespace Bluetooth
|
} // namespace Bluetooth
|
||||||
} // namespace PresenceDetection
|
} // namespace PresenceDetection
|
||||||
|
|||||||
@@ -22,6 +22,7 @@ public:
|
|||||||
|
|
||||||
private:
|
private:
|
||||||
void UpdatePresentDevices();
|
void UpdatePresentDevices();
|
||||||
|
void SendStateChange(bool present, const std::string& macAddress);
|
||||||
|
|
||||||
private:
|
private:
|
||||||
Util::Timer m_timer;
|
Util::Timer m_timer;
|
||||||
|
|||||||
+50
-2
@@ -6,6 +6,7 @@
|
|||||||
#include <bluetooth/hci.h>
|
#include <bluetooth/hci.h>
|
||||||
#include <bluetooth/l2cap.h>
|
#include <bluetooth/l2cap.h>
|
||||||
|
|
||||||
|
#include <fcntl.h>
|
||||||
#include <unistd.h>
|
#include <unistd.h>
|
||||||
#include <sys/poll.h>
|
#include <sys/poll.h>
|
||||||
#include "Socket.h"
|
#include "Socket.h"
|
||||||
@@ -42,8 +43,55 @@ bool Functions::Ping(const std::string& macAddress)
|
|||||||
socketAddress.l2_family = AF_BLUETOOTH;
|
socketAddress.l2_family = AF_BLUETOOTH;
|
||||||
str2ba(macAddress.c_str(), &socketAddress.l2_bdaddr);
|
str2ba(macAddress.c_str(), &socketAddress.l2_bdaddr);
|
||||||
|
|
||||||
if (connect(socketDescriptor, reinterpret_cast<struct sockaddr*>(&socketAddress), sizeof(socketAddress)) < 0)
|
struct timeval timeoutStruct;
|
||||||
return false;
|
timeoutStruct.tv_sec = 3;
|
||||||
|
timeoutStruct.tv_usec = 0;
|
||||||
|
|
||||||
|
fd_set writeDescriptor, errorDescriptor;
|
||||||
|
FD_ZERO(&writeDescriptor);
|
||||||
|
FD_ZERO(&errorDescriptor);
|
||||||
|
FD_SET(socketDescriptor, &writeDescriptor);
|
||||||
|
FD_SET(socketDescriptor, &errorDescriptor);
|
||||||
|
|
||||||
|
int flags;
|
||||||
|
if((flags = fcntl(socketDescriptor, F_GETFL, 0) < 0))
|
||||||
|
throw std::runtime_error("Can't get socket options");
|
||||||
|
|
||||||
|
if(fcntl(socketDescriptor, F_SETFL, flags | O_NONBLOCK) < 0)
|
||||||
|
throw std::runtime_error("Can't set socket options");
|
||||||
|
|
||||||
|
int ret = 0;
|
||||||
|
if ((ret = connect(socketDescriptor, reinterpret_cast<struct sockaddr*>(&socketAddress), sizeof(socketAddress)) < 0))
|
||||||
|
{
|
||||||
|
if (errno != EINPROGRESS)
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (ret != 0)
|
||||||
|
{
|
||||||
|
if (select(socketDescriptor + 1, &errorDescriptor, &writeDescriptor, NULL, &timeoutStruct) < 0)
|
||||||
|
return false;
|
||||||
|
|
||||||
|
int error = 0;
|
||||||
|
socklen_t len = sizeof(error);
|
||||||
|
if (FD_ISSET(socketDescriptor, &errorDescriptor) || FD_ISSET(socketDescriptor, &writeDescriptor))
|
||||||
|
{
|
||||||
|
if (getsockopt(socketDescriptor, SOL_SOCKET, SO_ERROR, &error, &len) < 0)
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (error)
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
if(fcntl(socketDescriptor, F_SETFL, flags) < 0)
|
||||||
|
throw std::runtime_error("Can't set socket options");
|
||||||
|
|
||||||
|
connect(socketDescriptor, reinterpret_cast<struct sockaddr*>(&socketAddress), sizeof(socketAddress));
|
||||||
|
|
||||||
memset(&socketAddress, 0, sizeof(socketAddress));
|
memset(&socketAddress, 0, sizeof(socketAddress));
|
||||||
socklen_t optlen = sizeof(socketAddress);
|
socklen_t optlen = sizeof(socketAddress);
|
||||||
|
|||||||
+32
-41
@@ -71,7 +71,7 @@ bool Device::Login()
|
|||||||
catch (const std::exception& e)
|
catch (const std::exception& e)
|
||||||
{
|
{
|
||||||
std::stringstream ss;
|
std::stringstream ss;
|
||||||
ss << "UniFiDevice::Login() - Error: " << e.what() << std::endl;
|
ss << "UniFi::Device::Login() - Error: " << e.what() << std::endl;
|
||||||
syslog(LOG_ERR, "%s", ss.str().c_str());
|
syslog(LOG_ERR, "%s", ss.str().c_str());
|
||||||
std::lock_guard<std::mutex> lock(m_mutex);
|
std::lock_guard<std::mutex> lock(m_mutex);
|
||||||
m_loggedIn = false;
|
m_loggedIn = false;
|
||||||
@@ -98,7 +98,7 @@ void Device::Logout()
|
|||||||
catch (const std::exception& e)
|
catch (const std::exception& e)
|
||||||
{
|
{
|
||||||
std::stringstream ss;
|
std::stringstream ss;
|
||||||
ss << "UniFiDevice::Logout() - Error: " << e.what() << std::endl;
|
ss << "UniFi::Device::Logout() - Error: " << e.what() << std::endl;
|
||||||
syslog(LOG_ERR, "%s", ss.str().c_str());
|
syslog(LOG_ERR, "%s", ss.str().c_str());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -156,7 +156,7 @@ void Device::UpdatePresentDevices()
|
|||||||
catch (const std::exception& e)
|
catch (const std::exception& e)
|
||||||
{
|
{
|
||||||
std::stringstream ss;
|
std::stringstream ss;
|
||||||
ss << "UniFiDevice::IsDevicePresent() - Error: " << e.what() << std::endl;
|
ss << "UniFi::Device::IsDevicePresent() - Error: " << e.what() << std::endl;
|
||||||
syslog(LOG_ERR, "%s", ss.str().c_str());
|
syslog(LOG_ERR, "%s", ss.str().c_str());
|
||||||
Logout();
|
Logout();
|
||||||
return;
|
return;
|
||||||
@@ -167,49 +167,40 @@ void Device::UpdatePresentDevices()
|
|||||||
removedDevices.push_back(*it);
|
removedDevices.push_back(*it);
|
||||||
|
|
||||||
for (std::vector<std::string>::iterator it = addedDevices.begin(); it != addedDevices.end(); ++it)
|
for (std::vector<std::string>::iterator it = addedDevices.begin(); it != addedDevices.end(); ++it)
|
||||||
{
|
SendStateChange(true, *it);
|
||||||
std::stringstream ss;
|
|
||||||
ss << "UniFi: + " << *it;
|
|
||||||
syslog(LOG_INFO, "%s", ss.str().c_str());
|
|
||||||
|
|
||||||
std::stringstream url;
|
|
||||||
url << m_target << "/UniFi/+/" << *it;
|
|
||||||
|
|
||||||
try
|
|
||||||
{
|
|
||||||
m_httpClient.GetUrlSilent(url.str());
|
|
||||||
}
|
|
||||||
catch (const std::exception& e)
|
|
||||||
{
|
|
||||||
std::stringstream ss;
|
|
||||||
ss << "UniFiDevice::UpdatePresentDevices() - Error: " << e.what() << std::endl;
|
|
||||||
syslog(LOG_ERR, "%s", ss.str().c_str());
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
for (std::vector<std::string>::iterator it = removedDevices.begin(); it != removedDevices.end(); ++it)
|
for (std::vector<std::string>::iterator it = removedDevices.begin(); it != removedDevices.end(); ++it)
|
||||||
{
|
SendStateChange(false, *it);
|
||||||
std::stringstream ss;
|
|
||||||
ss << "UniFi: - " << *it;
|
|
||||||
syslog(LOG_INFO, "%s", ss.str().c_str());
|
|
||||||
|
|
||||||
std::stringstream url;
|
|
||||||
url << m_target << "/UniFi/-/" << *it;
|
|
||||||
|
|
||||||
try
|
|
||||||
{
|
|
||||||
m_httpClient.GetUrlSilent(url.str());
|
|
||||||
}
|
|
||||||
catch (const std::exception& e)
|
|
||||||
{
|
|
||||||
std::stringstream ss;
|
|
||||||
ss << "UniFiDevice::UpdatePresentDevices() - Error: " << e.what() << std::endl;
|
|
||||||
syslog(LOG_ERR, "%s", ss.str().c_str());
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
m_presentDevices.assign(presentDevices.begin(), presentDevices.end());
|
m_presentDevices.assign(presentDevices.begin(), presentDevices.end());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void Device::SendStateChange(bool present, const std::string& macAddress)
|
||||||
|
{
|
||||||
|
char sign;
|
||||||
|
if (present)
|
||||||
|
sign = '+';
|
||||||
|
else
|
||||||
|
sign = '-';
|
||||||
|
|
||||||
|
std::stringstream ss;
|
||||||
|
ss << "UniFi: " << sign << " " << macAddress;
|
||||||
|
syslog(LOG_INFO, "%s", ss.str().c_str());
|
||||||
|
|
||||||
|
std::stringstream url;
|
||||||
|
url << m_target << "/UniFi/" << sign << "/" << macAddress;
|
||||||
|
|
||||||
|
try
|
||||||
|
{
|
||||||
|
m_httpClient.GetUrlSilent(url.str());
|
||||||
|
}
|
||||||
|
catch (const std::exception& e)
|
||||||
|
{
|
||||||
|
std::stringstream ss;
|
||||||
|
ss << "UniFi::Device::SendStateChange() - Error: " << e.what() << std::endl;
|
||||||
|
syslog(LOG_ERR, "%s", ss.str().c_str());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
} // namespace UniFi
|
} // namespace UniFi
|
||||||
} // namespace PresenceDetection
|
} // namespace PresenceDetection
|
||||||
|
|||||||
@@ -26,6 +26,7 @@ private:
|
|||||||
void Logout();
|
void Logout();
|
||||||
|
|
||||||
void UpdatePresentDevices();
|
void UpdatePresentDevices();
|
||||||
|
void SendStateChange(bool present, const std::string& macAddress);
|
||||||
|
|
||||||
private:
|
private:
|
||||||
Util::Timer m_timer;
|
Util::Timer m_timer;
|
||||||
|
|||||||
Reference in New Issue
Block a user