1
0
mirror of https://github.com/binary-kitchen/doorlockd synced 2024-11-01 06:57:04 +01:00
doorlockd-mirror/doorlockd/lib/door.h

80 lines
1.7 KiB
C
Raw Normal View History

2015-05-11 00:18:22 +02:00
#ifndef DOOR_H
#define DOOR_H
#include <chrono>
#include <condition_variable>
#include <functional>
#include <mutex>
#include <string>
#include <thread>
2015-05-11 00:18:22 +02:00
#include <boost/asio.hpp>
#include <boost/asio/serial_port.hpp>
2015-05-11 00:18:22 +02:00
#include "logger.h"
#include "doormessage.h"
2015-05-11 00:18:22 +02:00
class Door final
{
2015-05-11 00:18:22 +02:00
public:
2015-09-24 18:55:47 +02:00
using DoorCallback = std::function<void(Doormessage)>;
enum class State {Unlocked, Locked};
Door(const std::string &serDev,
2015-09-23 15:33:27 +02:00
unsigned int baudrate);
2015-05-11 00:18:22 +02:00
~Door();
State state() const;
2015-09-24 18:55:47 +02:00
void setDoorCallback(DoorCallback doorCallback);
2015-05-11 00:18:22 +02:00
void lock();
void unlock();
private:
using Milliseconds = std::chrono::milliseconds;
const unsigned int _baudrate;
// To prevent concurrent writes
std::mutex _serialMutex = { };
boost::asio::io_service _ioService = { };
boost::asio::serial_port _port;
std::mutex _stateMutex = { };
volatile State _state = { State::Locked };
std::thread _heartbeatThread = { };
std::mutex _heartbeatMutex = { };
std::condition_variable _heartbeatCondition = { };
2015-05-11 00:18:22 +02:00
std::thread _ioThread = { };
void _asyncRead();
volatile bool _schnapper = { false };
2015-05-21 13:35:30 +02:00
// Indicates if recvBuf contains a valid response from AVR Board
volatile bool _byteReady = { false };
// Actual response
char recvBuf = { };
std::condition_variable _receivedCondition = { };
std::mutex _receiveLock = { };
2015-09-24 18:55:47 +02:00
DoorCallback _doorCallback = { };
Logger &_logger;
// Writed command to AVR board
bool _writeCMD(char c);
// Receives one byte and returns true or returns false on timeout
bool _readByte(char &byte, Milliseconds timeout);
void _exec_and_log(const std::string &filename);
2015-05-11 00:18:22 +02:00
};
#endif