Initial Commit
This commit is contained in:
@@ -0,0 +1,103 @@
|
||||
#include <sstream>
|
||||
#include <syslog.h>
|
||||
#include <boost/algorithm/string.hpp>
|
||||
#include "Functions.h"
|
||||
#include "Device.h"
|
||||
|
||||
|
||||
namespace PresenceDetection {
|
||||
namespace Bluetooth {
|
||||
|
||||
Device::Device(const std::string& devices, const std::string& target) :
|
||||
m_target(target)
|
||||
{
|
||||
boost::split(m_devices, devices, boost::is_any_of(","));
|
||||
Start();
|
||||
}
|
||||
|
||||
Device::~Device()
|
||||
{
|
||||
}
|
||||
|
||||
void Device::Start()
|
||||
{
|
||||
m_timer.Start(10000, std::bind(&Device::UpdatePresentDevices, this));
|
||||
}
|
||||
|
||||
void Device::Stop()
|
||||
{
|
||||
m_timer.Stop();
|
||||
}
|
||||
|
||||
void Device::Wait()
|
||||
{
|
||||
m_timer.Wait();
|
||||
}
|
||||
|
||||
void Device::UpdatePresentDevices()
|
||||
{
|
||||
std::vector<std::string> presentDevices;
|
||||
std::vector<std::string> addedDevices;
|
||||
std::vector<std::string> removedDevices;
|
||||
|
||||
for (std::vector<std::string>::iterator it = m_devices.begin(); it != m_devices.end(); ++it)
|
||||
{
|
||||
if (Functions::Ping(*it))
|
||||
{
|
||||
if (std::find(m_presentDevices.begin(), m_presentDevices.end(), *it) == m_presentDevices.end())
|
||||
addedDevices.push_back(*it);
|
||||
presentDevices.push_back(*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())
|
||||
removedDevices.push_back(*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());
|
||||
}
|
||||
|
||||
} // namespace Bluetooth
|
||||
} // namespace PresenceDetection
|
||||
@@ -0,0 +1,40 @@
|
||||
#ifndef BLUETOOTH_DEVICE_H
|
||||
#define BLUETOOTH_DEVICE_H
|
||||
|
||||
#include <string>
|
||||
#include <vector>
|
||||
#include "Util/Timer.h"
|
||||
#include "Util/HttpClient.h"
|
||||
|
||||
|
||||
namespace PresenceDetection {
|
||||
namespace Bluetooth {
|
||||
|
||||
class Device
|
||||
{
|
||||
public:
|
||||
Device(const std::string& devices, const std::string& target);
|
||||
~Device();
|
||||
|
||||
void Start();
|
||||
void Stop();
|
||||
void Wait();
|
||||
|
||||
private:
|
||||
void UpdatePresentDevices();
|
||||
|
||||
private:
|
||||
Util::Timer m_timer;
|
||||
Util::HttpClient m_httpClient;
|
||||
|
||||
std::vector<std::string> m_devices;
|
||||
|
||||
std::string m_target;
|
||||
|
||||
std::vector<std::string> m_presentDevices;
|
||||
};
|
||||
|
||||
} // namespace Bluetooth
|
||||
} // namespace PresenceDetection
|
||||
|
||||
#endif // BLUETOOTH_DEVICE_H
|
||||
@@ -0,0 +1,110 @@
|
||||
#include <sstream>
|
||||
#include <vector>
|
||||
#include <stdexcept>
|
||||
#include <syslog.h>
|
||||
#include <bluetooth/bluetooth.h>
|
||||
#include <bluetooth/hci.h>
|
||||
#include <bluetooth/l2cap.h>
|
||||
|
||||
#include <unistd.h>
|
||||
#include <sys/poll.h>
|
||||
#include "Socket.h"
|
||||
#include "Functions.h"
|
||||
|
||||
|
||||
namespace PresenceDetection {
|
||||
namespace Bluetooth {
|
||||
|
||||
bool Functions::Ping(const std::string& macAddress)
|
||||
{
|
||||
struct sockaddr_l2 socketAddress;
|
||||
|
||||
int size = 44;
|
||||
int messageSize = L2CAP_CMD_HDR_SIZE + size;
|
||||
std::vector<unsigned char> sendBuffer(messageSize);
|
||||
std::vector<unsigned char> receiveBuffer(messageSize);
|
||||
|
||||
Socket socket;
|
||||
int socketDescriptor = socket.Descriptor();
|
||||
if (socketDescriptor < 0)
|
||||
throw std::runtime_error("Can't create Bluetooth socket");
|
||||
|
||||
memset(&socketAddress, 0, sizeof(socketAddress));
|
||||
socketAddress.l2_family = AF_BLUETOOTH;
|
||||
|
||||
bdaddr_t bdaddr;
|
||||
bacpy(&socketAddress.l2_bdaddr, &bdaddr);
|
||||
|
||||
if (bind(socketDescriptor, reinterpret_cast<struct sockaddr*>(&socketAddress), sizeof(socketAddress)) < 0)
|
||||
throw std::runtime_error("Can't bind socket");
|
||||
|
||||
memset(&socketAddress, 0, sizeof(socketAddress));
|
||||
socketAddress.l2_family = AF_BLUETOOTH;
|
||||
str2ba(macAddress.c_str(), &socketAddress.l2_bdaddr);
|
||||
|
||||
if (connect(socketDescriptor, reinterpret_cast<struct sockaddr*>(&socketAddress), sizeof(socketAddress)) < 0)
|
||||
return false;
|
||||
|
||||
memset(&socketAddress, 0, sizeof(socketAddress));
|
||||
socklen_t optlen = sizeof(socketAddress);
|
||||
|
||||
if (getsockname(socketDescriptor, reinterpret_cast<struct sockaddr*>(&socketAddress), &optlen) < 0)
|
||||
throw std::runtime_error("Can't get local address");
|
||||
|
||||
char str[18];
|
||||
ba2str(&socketAddress.l2_bdaddr, str);
|
||||
|
||||
for (int i = 0; i < size; ++i)
|
||||
sendBuffer[L2CAP_CMD_HDR_SIZE + i] = (i % 40) + 'A';
|
||||
|
||||
int ident = 200;
|
||||
uint8_t id = ident;
|
||||
|
||||
l2cap_cmd_hdr* send_cmd = reinterpret_cast<l2cap_cmd_hdr*>(sendBuffer.data());
|
||||
l2cap_cmd_hdr* recv_cmd = reinterpret_cast<l2cap_cmd_hdr*>(receiveBuffer.data());
|
||||
|
||||
send_cmd->ident = id;
|
||||
send_cmd->len = htobs(size);
|
||||
send_cmd->code = L2CAP_ECHO_REQ;
|
||||
|
||||
if (send(socketDescriptor, sendBuffer.data(), messageSize, 0) <= 0)
|
||||
throw std::runtime_error("Send failed");
|
||||
|
||||
while (1)
|
||||
{
|
||||
struct pollfd pf[1];
|
||||
|
||||
pf[0].fd = socketDescriptor;
|
||||
pf[0].events = POLLIN;
|
||||
|
||||
int err;
|
||||
int timeout = 500;
|
||||
if ((err = poll(pf, 1, timeout)) < 0)
|
||||
throw std::runtime_error("Poll failed");
|
||||
|
||||
if (!err)
|
||||
return false;
|
||||
|
||||
if ((err = recv(socketDescriptor, receiveBuffer.data(), messageSize, 0)) < 0)
|
||||
return false;
|
||||
|
||||
if (!err)
|
||||
throw std::runtime_error("Disconnected");
|
||||
|
||||
recv_cmd->len = btohs(recv_cmd->len);
|
||||
|
||||
if (recv_cmd->ident != id)
|
||||
continue;
|
||||
|
||||
if (recv_cmd->code == L2CAP_ECHO_RSP)
|
||||
break;
|
||||
|
||||
if (recv_cmd->code == L2CAP_COMMAND_REJ)
|
||||
throw std::runtime_error("Peer doesn't support Echo packets");
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
} // namespace Bluetooth
|
||||
} // namespace PresenceDetection
|
||||
@@ -0,0 +1,19 @@
|
||||
#ifndef BLUETOOTH_FUNCTIONS_H
|
||||
#define BLUETOOTH_FUNCTIONS_H
|
||||
|
||||
#include <string>
|
||||
|
||||
|
||||
namespace PresenceDetection {
|
||||
namespace Bluetooth {
|
||||
|
||||
class Functions
|
||||
{
|
||||
public:
|
||||
static bool Ping(const std::string& macAddress);
|
||||
};
|
||||
|
||||
} // namespace Bluetooth
|
||||
} // namespace PresenceDetection
|
||||
|
||||
#endif // BLUETOOTH_FUNCTIONS_H
|
||||
Symlink
+1
@@ -0,0 +1 @@
|
||||
../Makefile
|
||||
Symlink
+1
@@ -0,0 +1 @@
|
||||
../Makefile.conf
|
||||
@@ -0,0 +1,30 @@
|
||||
#include <stdexcept>
|
||||
#include <unistd.h>
|
||||
#include <bluetooth/bluetooth.h>
|
||||
#include <bluetooth/hci.h>
|
||||
#include "Socket.h"
|
||||
|
||||
|
||||
namespace PresenceDetection {
|
||||
namespace Bluetooth {
|
||||
|
||||
Socket::Socket()
|
||||
{
|
||||
m_descriptor = socket(PF_BLUETOOTH, SOCK_RAW, BTPROTO_L2CAP);
|
||||
if (m_descriptor < 0)
|
||||
throw std::runtime_error("Can't create Bluetooth socket");
|
||||
}
|
||||
|
||||
Socket::~Socket()
|
||||
{
|
||||
close(m_descriptor);
|
||||
}
|
||||
|
||||
int Socket::Descriptor() const
|
||||
{
|
||||
return m_descriptor;
|
||||
}
|
||||
|
||||
} // namespace Bluetooth
|
||||
} // namespace PresenceDetection
|
||||
|
||||
@@ -0,0 +1,24 @@
|
||||
#ifndef BLUETOOTH_SOCKET_H
|
||||
#define BLUETOOTH_SOCKET_H
|
||||
|
||||
|
||||
namespace PresenceDetection {
|
||||
namespace Bluetooth {
|
||||
|
||||
class Socket
|
||||
{
|
||||
public:
|
||||
Socket();
|
||||
~Socket();
|
||||
|
||||
int Descriptor() const;
|
||||
|
||||
private:
|
||||
int m_descriptor;
|
||||
};
|
||||
|
||||
} // namespace Bluetooth
|
||||
} // namespace PresenceDetection
|
||||
|
||||
#endif // BLUETOOTH_SOCKET_H
|
||||
|
||||
Reference in New Issue
Block a user