Initial Commit

This commit is contained in:
2018-12-18 12:07:12 +01:00
commit 7ad040c6c1
31 changed files with 1974 additions and 0 deletions
+1
View File
@@ -0,0 +1 @@
../Makefile
+1
View File
@@ -0,0 +1 @@
../Makefile.conf
+177
View File
@@ -0,0 +1,177 @@
#include <iostream>
#include <string>
#include <signal.h>
#include <syslog.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <boost/lexical_cast.hpp>
#include <boost/program_options.hpp>
#include <boost/property_tree/ptree.hpp>
#include <boost/property_tree/ini_parser.hpp>
#include "UniFi/Device.h"
#include "Bluetooth/Device.h"
int main(int argc, char** argv)
{
try
{
setlogmask(LOG_UPTO(LOG_INFO));
openlog("PresenceDetection", LOG_CONS | LOG_NDELAY | LOG_PERROR | LOG_PID, LOG_USER);
boost::program_options::options_description description("Options");
description.add_options()
("help,h", "Provide Help Message")
("daemon,d", "Run the process as Daemon");
boost::program_options::variables_map vm;
boost::program_options::store(boost::program_options::parse_command_line(argc, argv, description), vm);
boost::program_options::notify(vm);
if (vm.count("help"))
{
std::cerr << description << std::endl;
return 1;
}
bool daemon = false;
if (vm.count("daemon"))
daemon = true;
boost::property_tree::ptree pt;
boost::property_tree::ini_parser::read_ini("PresenceDetection.ini", pt);
if (daemon)
{
syslog(LOG_INFO, "Starting Daemon");
pid_t pid, sid;
pid = fork();
if (pid < 0)
exit(EXIT_FAILURE);
if (pid > 0)
exit(EXIT_SUCCESS);
umask(0);
sid = setsid();
if (sid < 0)
exit(EXIT_FAILURE);
if ((chdir("/")) < 0)
exit(EXIT_FAILURE);
close(STDIN_FILENO);
close(STDOUT_FILENO);
close(STDERR_FILENO);
}
int port;
boost::optional<boost::property_tree::ptree&> presenceDetectionPort = pt.get_child_optional("PresenceDetection.Port");
if (presenceDetectionPort)
port = pt.get<int>("PresenceDetection.Port");
else
throw std::runtime_error("Port directive missing in ini file.");
std::string target;
boost::optional<boost::property_tree::ptree&> presenceDetectionTarget = pt.get_child_optional("PresenceDetection.Target");
if (presenceDetectionTarget)
target = pt.get<std::string>("PresenceDetection.Target");
else
throw std::runtime_error("Target directive missing in ini file.");
bool unifi = false;
boost::optional<boost::property_tree::ptree&> presenceDetectionUniFi = pt.get_child_optional("PresenceDetection.UniFi");
if (presenceDetectionUniFi)
unifi = (pt.get<std::string>("PresenceDetection.UniFi") == "On");
bool bluetooth = false;
boost::optional<boost::property_tree::ptree&> presenceDetectionBluetooth = pt.get_child_optional("PresenceDetection.Bluetooth");
if (presenceDetectionBluetooth)
bluetooth = (pt.get<std::string>("PresenceDetection.Bluetooth") == "On");
std::shared_ptr<PresenceDetection::UniFi::Device> pUniFiDevice;
if (unifi)
{
std::string hostname;
boost::optional<boost::property_tree::ptree&> unifiHostname = pt.get_child_optional("UniFi.Hostname");
if (unifiHostname)
hostname = pt.get<std::string>("UniFi.Hostname");
else
throw std::runtime_error("UniFi Hostname directive missing in ini file.");
std::string username;
boost::optional<boost::property_tree::ptree&> unifiUsername = pt.get_child_optional("UniFi.Username");
if (unifiUsername)
username = pt.get<std::string>("UniFi.Username");
else
throw std::runtime_error("UniFi Username directive missing in ini file.");
std::string password;
boost::optional<boost::property_tree::ptree&> unifiPassword = pt.get_child_optional("UniFi.Password");
if (unifiPassword)
password = pt.get<std::string>("UniFi.Password");
else
throw std::runtime_error("UniFi Password directive missing in ini file.");
std::string cookiefile;
boost::optional<boost::property_tree::ptree&> unifiCookieFile = pt.get_child_optional("UniFi.CookieFile");
if (unifiCookieFile)
cookiefile = pt.get<std::string>("UniFi.CookieFile");
else
throw std::runtime_error("UniFi CookieFile directive missing in ini file.");
pUniFiDevice = std::make_shared<PresenceDetection::UniFi::Device>(hostname, username, password, cookiefile, target);
}
std::shared_ptr<PresenceDetection::Bluetooth::Device> pBluetoothDevice;
if (bluetooth)
{
std::string devices;
boost::optional<boost::property_tree::ptree&> bluetoothDevices = pt.get_child_optional("Bluetooth.Devices");
if (bluetoothDevices)
devices = pt.get<std::string>("Bluetooth.Devices");
else
throw std::runtime_error("Bluetooth Devices directive missing in ini file.");
pBluetoothDevice = std::make_shared<PresenceDetection::Bluetooth::Device>(devices, target);
}
sigset_t wset;
sigemptyset(&wset);
sigaddset(&wset,SIGHUP);
sigaddset(&wset,SIGINT);
sigaddset(&wset,SIGTERM);
int sig;
sigwait(&wset,&sig);
syslog(LOG_INFO, "Stopping...");
if (pUniFiDevice)
pUniFiDevice->Stop();
if (pBluetoothDevice)
pBluetoothDevice->Stop();
if (pUniFiDevice)
pUniFiDevice->Wait();
if (pBluetoothDevice)
pBluetoothDevice->Wait();
closelog();
}
catch (const std::exception& e)
{
std::stringstream ss;
ss << "ERROR: " << e.what() << std::endl;
std::cerr << ss.str() << std::endl;
syslog(LOG_ERR, "%s", ss.str().c_str());
closelog();
return -1;
}
return 0;
}
+236
View File
@@ -0,0 +1,236 @@
#include <string>
#include <stdio.h>
#include <errno.h>
#include <unistd.h>
#include <stdlib.h>
#include <string.h>
#include <getopt.h>
#include <signal.h>
#include <sys/time.h>
#include <sys/poll.h>
#include <sys/socket.h>
#include <bluetooth/bluetooth.h>
#include <bluetooth/hci.h>
#include <bluetooth/hci_lib.h>
#include <bluetooth/l2cap.h>
/* Defaults */
static bdaddr_t bdaddr;
static int size = 44;
static int ident = 200;
static int delay = 1;
static int count = -1;
static int timeout = 10;
static int reverse = 0;
static int verify = 0;
/* Stats */
static int sent_pkt = 0;
static int recv_pkt = 0;
static float tv2fl(struct timeval tv)
{
return (float)(tv.tv_sec*1000.0) + (float)(tv.tv_usec/1000.0);
}
static void stat(int sig)
{
int loss = sent_pkt ? (float)((sent_pkt-recv_pkt)/(sent_pkt/100.0)) : 0;
printf("%d sent, %d received, %d%% loss\n", sent_pkt, recv_pkt, loss);
exit(0);
}
static void ping(const char* svr)
{
struct sigaction sa;
struct sockaddr_l2 addr;
socklen_t optlen;
unsigned char *send_buf;
unsigned char *recv_buf;
char str[18];
int i, sk, lost;
uint8_t id;
memset(&sa, 0, sizeof(sa));
sa.sa_handler = stat;
sigaction(SIGINT, &sa, NULL);
send_buf = static_cast<unsigned char*>(malloc(L2CAP_CMD_HDR_SIZE + size));
recv_buf = static_cast<unsigned char*>(malloc(L2CAP_CMD_HDR_SIZE + size));
if (!send_buf || !recv_buf) {
perror("Can't allocate buffer");
exit(1);
}
/* Create socket */
sk = socket(PF_BLUETOOTH, SOCK_RAW, BTPROTO_L2CAP);
if (sk < 0) {
perror("Can't create socket");
goto error;
}
/* Bind to local address */
memset(&addr, 0, sizeof(addr));
addr.l2_family = AF_BLUETOOTH;
bacpy(&addr.l2_bdaddr, &bdaddr);
if (bind(sk, (struct sockaddr *) &addr, sizeof(addr)) < 0) {
perror("Can't bind socket");
goto error;
}
/* Connect to remote device */
memset(&addr, 0, sizeof(addr));
addr.l2_family = AF_BLUETOOTH;
str2ba(svr, &addr.l2_bdaddr);
if (connect(sk, (struct sockaddr *) &addr, sizeof(addr)) < 0) {
perror("Can't connect");
goto error;
}
/* Get local address */
memset(&addr, 0, sizeof(addr));
optlen = sizeof(addr);
if (getsockname(sk, (struct sockaddr *) &addr, &optlen) < 0) {
perror("Can't get local address");
goto error;
}
ba2str(&addr.l2_bdaddr, str);
printf("Ping: %s from %s (data size %d) ...\n", svr, str, size);
/* Initialize send buffer */
for (i = 0; i < size; i++)
send_buf[L2CAP_CMD_HDR_SIZE + i] = (i % 40) + 'A';
id = ident;
while (count == -1 || count-- > 0) {
struct timeval tv_send, tv_recv, tv_diff;
l2cap_cmd_hdr *send_cmd = (l2cap_cmd_hdr *) send_buf;
l2cap_cmd_hdr *recv_cmd = (l2cap_cmd_hdr *) recv_buf;
/* Build command header */
send_cmd->ident = id;
send_cmd->len = htobs(size);
if (reverse)
send_cmd->code = L2CAP_ECHO_RSP;
else
send_cmd->code = L2CAP_ECHO_REQ;
gettimeofday(&tv_send, NULL);
/* Send Echo Command */
if (send(sk, send_buf, L2CAP_CMD_HDR_SIZE + size, 0) <= 0) {
perror("Send failed");
goto error;
}
/* Wait for Echo Response */
lost = 0;
while (1) {
struct pollfd pf[1];
int err;
pf[0].fd = sk;
pf[0].events = POLLIN;
if ((err = poll(pf, 1, timeout * 1000)) < 0) {
perror("Poll failed");
goto error;
}
if (!err) {
lost = 1;
break;
}
if ((err = recv(sk, recv_buf, L2CAP_CMD_HDR_SIZE + size, 0)) < 0) {
perror("Recv failed");
goto error;
}
if (!err){
printf("Disconnected\n");
goto error;
}
recv_cmd->len = btohs(recv_cmd->len);
/* Check for our id */
if (recv_cmd->ident != id)
continue;
/* Check type */
if (!reverse && recv_cmd->code == L2CAP_ECHO_RSP)
break;
if (recv_cmd->code == L2CAP_COMMAND_REJ) {
printf("Peer doesn't support Echo packets\n");
goto error;
}
}
sent_pkt++;
if (!lost) {
recv_pkt++;
gettimeofday(&tv_recv, NULL);
timersub(&tv_recv, &tv_send, &tv_diff);
if (verify) {
/* Check payload length */
if (recv_cmd->len != size) {
fprintf(stderr, "Received %d bytes, expected %d\n",
recv_cmd->len, size);
goto error;
}
/* Check payload */
if (memcmp(&send_buf[L2CAP_CMD_HDR_SIZE],
&recv_buf[L2CAP_CMD_HDR_SIZE], size)) {
fprintf(stderr, "Response payload different.\n");
goto error;
}
}
printf("%d bytes from %s id %d time %.2fms\n", recv_cmd->len, svr,
id - ident, tv2fl(tv_diff));
if (delay)
sleep(delay);
} else {
printf("no response from %s: id %d\n", svr, id - ident);
}
if (++id > 254)
id = ident;
}
stat(0);
free(send_buf);
free(recv_buf);
return;
error:
close(sk);
free(send_buf);
free(recv_buf);
exit(1);
}
int main(int /*argc*/, char** /*argv[]*/)
{
std::string address = "48:4B:AA:85:30:C5";
ping(address.c_str());
//address = "d0:c5:f3:84:99:de";
//ping(address.c_str());
return 0;
}