| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202 |
- #include "Bluetooth/Driver.h"
- #include "Util/StaticDevice.h"
- #include "WiFi/Driver.h"
- #include <clipp.h>
- #include <INIReader.h>
- #include <Logging.h>
- #include <unistd.h>
- #include <sys/types.h>
- #include <sys/stat.h>
- #include <iostream>
- #include <string>
- #include <signal.h>
- #include <syslog.h>
- #include <vector>
- int main(int argc, char** argv)
- {
- try
- {
- Logging::OpenLog();
- Logging::SetLogMask(Logging::Severity::Info);
- bool daemon = false;
- clipp::group cli {
- clipp::option("-d", "--daemon").set(daemon).doc("Run the process as Daemon")
- };
- if (!clipp::parse(argc, argv, cli))
- {
- std::cout << clipp::make_man_page(cli, argv[0]) << std::endl;
- return 1;
- }
- INIReader iniReader("PresenceDetection.ini");
- if (iniReader.ParseError() != 0)
- {
- std::cerr << "Can't read PresenceDetection.ini" << std::endl;
- return 1;
- }
- if (daemon)
- {
- Logging::Log(Logging::Severity::Info, "Starting PresenceDetection 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);
- }
- else
- {
- Logging::Log(Logging::Severity::Info, "Starting PresenceDetection");
- }
- std::string target = iniReader.Get("PresenceDetection", "Target", "");
- bool wifi = iniReader.GetBoolean("PresenceDetection", "WiFi", false);
- bool bluetooth = iniReader.GetBoolean("PresenceDetection", "Bluetooth", false);
- std::vector<PresenceDetection::Util::StaticDevice> staticDevices;
- auto sections = iniReader.Sections();
- for (auto section : sections)
- {
- if (section != "PresenceDetection" &&
- section != "WiFi" &&
- section != "Bluetooth")
- {
- std::string WifiMac = iniReader.Get(section, "WifiMac", "");
- std::string BluetoothMac = iniReader.Get(section, "BluetoothMac", "");
- if (WifiMac.empty() && BluetoothMac.empty())
- {
- std::stringstream ss;
- ss << section << ": No Wifi or Bluetooth Mac address defined" << std::endl;
- throw std::runtime_error(ss.str());
- }
- auto staticDevice = PresenceDetection::Util::StaticDevice(WifiMac, BluetoothMac);
- std::string OnlineURL = iniReader.Get(section, "OnlineURL", "");
- if (!OnlineURL.empty())
- staticDevice.SetOnlineURL(OnlineURL);
- std::string WifiOnlineURL = iniReader.Get(section, "WifiOnlineURL", "");
- if (!WifiOnlineURL.empty())
- staticDevice.SetWifiOnlineURL(WifiOnlineURL);
- std::string BluetoothOnlineURL = iniReader.Get(section, "BluetoothOnlineURL", "");
- if (!BluetoothOnlineURL.empty())
- staticDevice.SetBluetoothOnlineURL(BluetoothOnlineURL);
- std::string OfflineURL = iniReader.Get(section, "OfflineURL", "");
- if (!OfflineURL.empty())
- staticDevice.SetOfflineURL(OfflineURL);
- std::string WifiOfflineURL = iniReader.Get(section, "WifiOfflineURL", "");
- if (!WifiOfflineURL.empty())
- staticDevice.SetWifiOfflineURL(WifiOfflineURL);
- std::string BluetoothOfflineURL = iniReader.Get(section, "BluetoothOfflineURL", "");
- if (!BluetoothOfflineURL.empty())
- staticDevice.SetBluetoothOfflineURL(BluetoothOfflineURL);
- staticDevices.push_back(staticDevice);
- }
- }
- std::shared_ptr<PresenceDetection::WiFi::Driver> pWiFiDriver;
- if (wifi)
- {
- std::string hostname = iniReader.Get("WiFi", "Hostname", "");
- if (hostname.empty())
- throw std::runtime_error("WiFi Hostname directive missing in ini file.");
- int port = iniReader.GetInteger("WiFi", "Port", 8443);
- std::string username = iniReader.Get("WiFi", "Username", "");
- if (username.empty())
- throw std::runtime_error("WiFi Username directive missing in ini file.");
- std::string password = iniReader.Get("WiFi", "Password", "");
- if (password.empty())
- throw std::runtime_error("WiFi Password directive missing in ini file.");
- std::string cookiefile = iniReader.Get("WiFi", "CookieFile", "");
- if (cookiefile.empty())
- throw std::runtime_error("WiFi CookieFile directive missing in ini file.");
- int timeout = iniReader.GetInteger("WiFi", "Timeout", 150);
- std::string inventoryURL = iniReader.Get("WiFi", "Inventory", "");
- pWiFiDriver = std::make_shared<PresenceDetection::WiFi::Driver>(hostname, port, username, password, cookiefile, timeout, inventoryURL, target, staticDevices);
- }
- std::shared_ptr<PresenceDetection::Bluetooth::Driver> pBluetoothDriver;
- if (bluetooth)
- {
- int timeout = iniReader.GetInteger("Bluetooth", "Timeout", 150);
- std::string inventoryURL = iniReader.Get("Bluetooth", "Inventory", "");
- pBluetoothDriver = std::make_shared<PresenceDetection::Bluetooth::Driver>(timeout, inventoryURL, target, staticDevices);
- }
- sigset_t wset;
- sigemptyset(&wset);
- sigaddset(&wset,SIGHUP);
- sigaddset(&wset,SIGINT);
- sigaddset(&wset,SIGTERM);
- int sig;
- sigwait(&wset,&sig);
- Logging::Log(Logging::Severity::Info, "Stopping PresenceDetection...");
- if (pWiFiDriver)
- pWiFiDriver->Stop();
- if (pBluetoothDriver)
- pBluetoothDriver->Stop();
- if (pWiFiDriver)
- pWiFiDriver->Wait();
- if (pBluetoothDriver)
- pBluetoothDriver->Wait();
- Logging::CloseLog();
- }
- catch (const std::exception& e)
- {
- std::stringstream ss;
- ss << "ERROR: " << e.what() << std::endl;
- std::cerr << ss.str() << std::endl;
- Logging::Log(Logging::Severity::Error, ss.str());
- Logging::CloseLog();
- return -1;
- }
- return 0;
- }
|