1
0
mirror of https://github.com/binary-kitchen/doorlockd synced 2024-07-03 04:07:45 +02:00
doorlockd-mirror/doorlockd/lib/door.h

78 lines
1.6 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);
2015-05-11 00:18:22 +02:00
};
#endif