2015-07-08 11:39:24 -04:00
|
|
|
/*
|
|
|
|
* Copyright (C) 2014 Jared Boone, ShareBrained Technology, Inc.
|
|
|
|
*
|
|
|
|
* This file is part of PortaPack.
|
|
|
|
*
|
|
|
|
* This program is free software; you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation; either version 2, or (at your option)
|
|
|
|
* any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with this program; see the file COPYING. If not, write to
|
|
|
|
* the Free Software Foundation, Inc., 51 Franklin Street,
|
|
|
|
* Boston, MA 02110-1301, USA.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#ifndef __PACKET_BUILDER_H__
|
|
|
|
#define __PACKET_BUILDER_H__
|
|
|
|
|
|
|
|
#include <cstdint>
|
|
|
|
#include <cstddef>
|
|
|
|
#include <bitset>
|
2015-09-28 15:46:44 -04:00
|
|
|
#include <functional>
|
2015-07-08 11:39:24 -04:00
|
|
|
|
2015-09-28 15:05:36 -04:00
|
|
|
#include "bit_pattern.hpp"
|
2015-09-26 17:15:39 -04:00
|
|
|
|
2015-07-08 11:39:24 -04:00
|
|
|
class PacketBuilder {
|
|
|
|
public:
|
2015-10-03 01:37:05 -04:00
|
|
|
using PayloadType = std::bitset<1024>;
|
2015-09-28 15:46:44 -04:00
|
|
|
using PayloadHandlerFunc = std::function<void(const PayloadType& payload, const size_t bits_received)>;
|
|
|
|
|
|
|
|
PacketBuilder(
|
|
|
|
const PayloadHandlerFunc payload_handler
|
|
|
|
) : payload_handler { payload_handler }
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2015-09-26 17:37:59 -04:00
|
|
|
void configure(
|
2015-09-28 15:05:36 -04:00
|
|
|
const BitPattern preamble,
|
2015-10-02 20:43:02 -04:00
|
|
|
const BitPattern unstuffing
|
2015-09-26 17:37:59 -04:00
|
|
|
);
|
2015-07-08 11:39:24 -04:00
|
|
|
|
|
|
|
void execute(
|
2015-09-28 15:46:44 -04:00
|
|
|
const uint_fast8_t symbol
|
2015-07-08 11:39:24 -04:00
|
|
|
) {
|
2015-09-28 15:05:36 -04:00
|
|
|
bit_history.add(symbol);
|
|
|
|
|
2015-07-08 11:39:24 -04:00
|
|
|
switch(state) {
|
2015-09-28 15:05:36 -04:00
|
|
|
case State::Preamble:
|
|
|
|
if( found_preamble() ) {
|
2015-07-08 11:39:24 -04:00
|
|
|
state = State::Payload;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
case State::Payload:
|
2015-09-28 15:36:04 -04:00
|
|
|
if( !found_stuffing_bit() ) {
|
|
|
|
payload[bits_received++] = symbol;
|
|
|
|
}
|
|
|
|
|
2015-10-03 01:54:59 -04:00
|
|
|
if( found_end_flag() ) {
|
|
|
|
payload_handler(payload, bits_received - 7);
|
2015-07-08 11:39:24 -04:00
|
|
|
reset_state();
|
2015-10-03 01:54:59 -04:00
|
|
|
} else {
|
|
|
|
if( packet_truncated() ) {
|
|
|
|
reset_state();
|
|
|
|
}
|
2015-07-08 11:39:24 -04:00
|
|
|
}
|
2015-10-03 01:54:59 -04:00
|
|
|
|
2015-07-08 11:39:24 -04:00
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
reset_state();
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
enum State {
|
2015-09-28 15:05:36 -04:00
|
|
|
Preamble,
|
2015-07-08 11:39:24 -04:00
|
|
|
Payload,
|
|
|
|
};
|
|
|
|
|
2015-09-28 15:05:36 -04:00
|
|
|
bool packet_truncated() const {
|
2015-09-28 15:36:04 -04:00
|
|
|
return bits_received >= payload.size();
|
2015-09-28 15:05:36 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
bool found_preamble() const {
|
|
|
|
return bit_history.matches(preamble_pattern);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool found_stuffing_bit() const {
|
|
|
|
return bit_history.matches(unstuff_pattern);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool found_end_flag() const {
|
|
|
|
return bit_history.matches(end_flag_pattern);
|
|
|
|
}
|
|
|
|
|
2015-09-28 15:46:44 -04:00
|
|
|
const PayloadHandlerFunc payload_handler;
|
|
|
|
|
2015-09-28 15:05:36 -04:00
|
|
|
BitHistory bit_history;
|
2015-10-03 01:52:05 -04:00
|
|
|
BitPattern preamble_pattern { 0b0101010101111110, 16, 1 };
|
2015-09-28 15:05:36 -04:00
|
|
|
BitPattern unstuff_pattern { 0b111110, 6 };
|
|
|
|
BitPattern end_flag_pattern { 0b01111110, 8 };
|
|
|
|
|
2015-07-08 11:39:24 -04:00
|
|
|
size_t bits_received { 0 };
|
2015-09-28 15:05:36 -04:00
|
|
|
State state { State::Preamble };
|
2015-09-28 15:46:44 -04:00
|
|
|
PayloadType payload;
|
2015-07-08 11:39:24 -04:00
|
|
|
|
|
|
|
void reset_state();
|
|
|
|
};
|
|
|
|
|
|
|
|
#endif/*__PACKET_BUILDER_H__*/
|