2015-05-11 00:18:22 +02:00
|
|
|
#ifndef LOGIC_H
|
|
|
|
#define LOGIC_H
|
|
|
|
|
2015-05-12 17:35:57 +02:00
|
|
|
#include <condition_variable>
|
|
|
|
#include <mutex>
|
2015-10-01 22:09:55 +02:00
|
|
|
#include <string>
|
|
|
|
#include <thread>
|
2015-05-11 00:18:22 +02:00
|
|
|
|
|
|
|
#include "config.h"
|
2015-10-01 22:09:55 +02:00
|
|
|
#include "clientmessage.h"
|
2015-05-11 00:18:22 +02:00
|
|
|
#include "door.h"
|
|
|
|
#include "logger.h"
|
2015-10-01 22:09:55 +02:00
|
|
|
#include "request.h"
|
2015-09-22 21:25:45 +02:00
|
|
|
#include "response.h"
|
2015-05-11 00:18:22 +02:00
|
|
|
|
2015-05-21 13:35:30 +02:00
|
|
|
/* The "Logic" class
|
|
|
|
*
|
|
|
|
* This class is initilized by all settings.
|
|
|
|
*
|
2015-10-01 22:09:55 +02:00
|
|
|
* It handles incoming requests and allows modifications of the door
|
2015-05-21 13:35:30 +02:00
|
|
|
*/
|
2015-05-11 00:18:22 +02:00
|
|
|
class Logic
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
|
2015-05-13 16:40:30 +02:00
|
|
|
Logic(const std::chrono::seconds tokenTimeout,
|
2015-05-24 19:15:47 +02:00
|
|
|
const std::string &ldapUri,
|
2015-05-13 16:40:30 +02:00
|
|
|
const std::string &bindDN,
|
2015-09-16 22:59:14 +02:00
|
|
|
const std::string &webPrefix,
|
2016-04-03 17:03:50 +02:00
|
|
|
const unsigned int tokenLength,
|
2015-09-22 18:08:12 +02:00
|
|
|
const std::string &serDev,
|
2015-09-23 15:33:27 +02:00
|
|
|
const unsigned int baudrate,
|
2015-09-24 18:15:05 +02:00
|
|
|
std::condition_variable &onClientUpdate);
|
2015-05-11 00:18:22 +02:00
|
|
|
~Logic();
|
|
|
|
|
2015-05-21 13:35:30 +02:00
|
|
|
// Parse incoming JSON Requests
|
2015-10-01 22:09:55 +02:00
|
|
|
Response request(const Request &request);
|
|
|
|
|
|
|
|
// Send direct command to door without credential checks
|
|
|
|
enum class DoorCommand { Lock, Unlock };
|
|
|
|
Response processDoor(const DoorCommand &doorCommand);
|
2015-05-11 00:18:22 +02:00
|
|
|
|
2015-09-22 18:08:12 +02:00
|
|
|
// Returns the current Token
|
2015-09-24 21:12:59 +02:00
|
|
|
Clientmessage getClientMessage();
|
2015-09-22 18:08:12 +02:00
|
|
|
|
2015-05-11 00:18:22 +02:00
|
|
|
private:
|
|
|
|
|
2015-05-21 13:35:30 +02:00
|
|
|
// Internal lock wrapper
|
2015-05-12 15:59:04 +02:00
|
|
|
Response _lock();
|
2015-05-21 13:35:30 +02:00
|
|
|
// Internal unlock wrapper
|
2015-05-12 15:59:04 +02:00
|
|
|
Response _unlock();
|
2015-05-11 00:18:22 +02:00
|
|
|
|
2015-05-21 13:35:30 +02:00
|
|
|
// Checks if the incoming token is valid
|
2015-10-01 22:09:55 +02:00
|
|
|
Response _checkToken(std::string token) const;
|
2015-05-21 13:35:30 +02:00
|
|
|
|
|
|
|
// Checks if incoming credentials against LDAP
|
2015-05-13 16:40:30 +02:00
|
|
|
Response _checkLDAP(const std::string &user,
|
|
|
|
const std::string &password);
|
2015-05-21 13:35:30 +02:00
|
|
|
|
|
|
|
// Creates a new random token and draws it on the epaper.
|
|
|
|
// stillValid indicates whether the old (previous) token is still valid
|
2015-05-12 17:35:57 +02:00
|
|
|
void _createNewToken(const bool stillValid);
|
2015-05-11 00:18:22 +02:00
|
|
|
|
2015-09-24 21:12:59 +02:00
|
|
|
void _doorCallback(Doormessage doormessage);
|
2015-09-24 18:57:32 +02:00
|
|
|
|
2015-10-02 17:25:16 +02:00
|
|
|
Logger &_logger;
|
2015-05-21 13:35:30 +02:00
|
|
|
|
2015-09-16 22:59:14 +02:00
|
|
|
// The door
|
|
|
|
Door _door;
|
2015-05-11 00:18:22 +02:00
|
|
|
|
2015-05-21 13:35:30 +02:00
|
|
|
// The current token
|
2016-04-03 17:03:50 +02:00
|
|
|
std::string _curToken = {};
|
2015-05-21 13:35:30 +02:00
|
|
|
// The previous token
|
2016-04-03 17:03:50 +02:00
|
|
|
std::string _prevToken = {};
|
2015-05-21 13:35:30 +02:00
|
|
|
// Indicates whether the previous token is valid
|
|
|
|
bool _prevValid = { false };
|
2015-05-11 00:18:22 +02:00
|
|
|
|
2015-05-21 13:35:30 +02:00
|
|
|
// Tokens are refreshed all tokenTimout seconds
|
2015-05-12 17:35:57 +02:00
|
|
|
const std::chrono::seconds _tokenTimeout;
|
2015-05-21 13:35:30 +02:00
|
|
|
// Thread for asynchronosly updating tokens
|
|
|
|
std::thread _tokenUpdater = {};
|
|
|
|
// Thread can be force-triggered for updates using the condition variable
|
|
|
|
std::condition_variable _tokenCondition = {};
|
|
|
|
// stop indicator for the thread
|
|
|
|
bool _run = true;
|
2015-09-24 18:57:32 +02:00
|
|
|
// General mutex for concurrent data access
|
|
|
|
mutable std::mutex _mutex = {};
|
|
|
|
|
2015-09-24 21:12:59 +02:00
|
|
|
Doormessage _doormessage = {};
|
2015-05-21 13:35:30 +02:00
|
|
|
|
2015-09-22 18:08:12 +02:00
|
|
|
// This variable gets notified on token updates
|
2015-09-24 18:15:05 +02:00
|
|
|
std::condition_variable &_onClientUpdate;
|
2015-09-22 18:08:12 +02:00
|
|
|
|
2015-05-21 13:35:30 +02:00
|
|
|
// The URI of the ldap server
|
2015-05-24 19:15:47 +02:00
|
|
|
const std::string _ldapUri;
|
2015-05-21 13:35:30 +02:00
|
|
|
// LDAP bindDN
|
2015-05-13 16:40:30 +02:00
|
|
|
const std::string _bindDN;
|
2015-05-21 13:35:30 +02:00
|
|
|
// Prefix of the website
|
2015-05-13 16:40:30 +02:00
|
|
|
const std::string _webPrefix;
|
2016-04-03 17:03:50 +02:00
|
|
|
// Length of the token in bytes
|
|
|
|
const unsigned int _tokenLength;
|
2015-05-11 00:18:22 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
#endif
|