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

71 lines
1.4 KiB
C
Raw Normal View History

2015-05-11 00:18:22 +02:00
#ifndef DOOR_H
#define DOOR_H
#include <string>
#include <functional>
#include <thread>
#include <mutex>
#include <condition_variable>
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"
2015-05-11 00:18:22 +02:00
class Door final
{
2015-05-11 00:18:22 +02:00
public:
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();
enum class State {Unlocked, Locked};
2015-05-21 13:35:30 +02:00
State state() const;
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 = { };
const 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);
2015-05-11 00:18:22 +02:00
};
#endif