1
0
mirror of https://github.com/binary-kitchen/doorlockd synced 2024-12-22 18:34:25 +01:00
doorlockd-mirror/doorlockd/logic.h

100 lines
2.5 KiB
C
Raw Normal View History

2015-05-11 00:18:22 +02:00
#ifndef LOGIC_H
#define LOGIC_H
#include <cstdint>
#include <string>
2015-05-12 17:35:57 +02:00
#include <thread>
#include <condition_variable>
#include <mutex>
2015-05-11 00:18:22 +02:00
#include "config.h"
#include "door.h"
#include "logger.h"
#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.
*
* It parses incoming JSON-Requests and returns the Response Code.
*/
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,
const std::string &webPrefix,
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-09-22 18:22:15 +02:00
Response parseRequest(const Json::Value &root);
2015-05-11 00:18:22 +02:00
// Returns the current Token
2015-09-24 18:57:32 +02:00
std::string getClientMessage();
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-05-11 00:18:22 +02:00
bool _checkToken(const std::string &token);
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 18:57:32 +02:00
void _doorCallback(Door::Doormessage doormessage);
2015-05-11 00:18:22 +02:00
const Logger &_logger;
2015-05-21 13:35:30 +02:00
// The door
Door _door;
2015-05-11 00:18:22 +02:00
2015-05-21 13:35:30 +02:00
// Tokens are 64-bit hexadecimal values
2015-05-11 00:18:22 +02:00
using Token = uint64_t;
2015-05-21 13:35:30 +02:00
// The current token
2015-05-12 16:10:26 +02:00
Token _curToken = { 0x0000000000000000 };
2015-05-21 13:35:30 +02:00
// The previous token
2015-05-12 16:10:26 +02:00
Token _prevToken = { 0x0000000000000000 };
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 = {};
Door::Doormessage _doormessage = {};
2015-05-21 13:35:30 +02:00
// This variable gets notified on token updates
2015-09-24 18:15:05 +02:00
std::condition_variable &_onClientUpdate;
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;
2015-05-11 00:18:22 +02:00
};
#endif