Browse Source

Transférer les fichiers vers 'src'

master
fabien 1 year ago
parent
commit
9f9f3c01bc
4 changed files with 185 additions and 0 deletions
  1. +45
    -0
      src/RfidCommand.cpp
  2. +40
    -0
      src/RfidCommand.h
  3. +61
    -0
      src/RfidCommandAck.cpp
  4. +39
    -0
      src/RfidCommandAck.h

+ 45
- 0
src/RfidCommand.cpp View File

@ -0,0 +1,45 @@
#include <stdio.h>
#include <bitset>
#include "RfidCommand.h"
uint8_t RfidCommand::crc5(const std::string & bitstr)
{
uint8_t poly = 0x9U;
uint8_t crc = poly;
uint8_t mask = ~poly & ((1 << 5) - 1);
for (const char & c : bitstr) {
uint8_t bit = bool(c-'0');
uint8_t crc4 = (crc >> 4) & 1;
bit ^= crc4;
uint8_t crc2 = (crc >> 2) & 1;
crc = (crc << 1) & mask;
uint8_t crc3 = ((crc2 ^ bit) << 3);
crc |= bit | crc3;
}
return crc;
}
uint16_t RfidCommand::crc16(const std::string & bitstr)
{
uint16_t crc = 0xFFFF;
for (const char & c : bitstr) {
uint8_t bit = bool(c-'0');
uint8_t lsb = crc & 1U; // Store LSB
crc >>= 1U; // Shift right 1 bit
if ( bit ) {
crc |= 0x8000U; // Shift in next bit
}
if ( lsb ) {
// Check stored LSB
crc ^= 0x8000U; // Invert MSB
}
if (0x8000 == (crc & 0x8000U)) {
// Check MSB
crc ^= 0x0408U; // Invert bits 3 and 10
}
}
return crc;
}

+ 40
- 0
src/RfidCommand.h View File

@ -0,0 +1,40 @@
#ifndef _RFID_COMMAND_H_
#define _RFID_COMMAND_H_
#include <stdint.h>
#include <string>
enum RfidCrc
{
CRC_NONE,
CRC_5,
CRC_16,
};
class RfidCommand
{
protected:
RfidCommand() {};
struct part {
uint16_t offset;
uint16_t size;
};
public:
virtual ~RfidCommand() {};
virtual void push(bool bit) = 0;
virtual char marker() const = 0;
virtual size_t size() const { return 0; }
virtual size_t length() const { return 0; }
virtual bool is_ready() const = 0;
virtual std::string to_str() const = 0;
virtual uint16_t code() const { return 0; }
virtual bool is_valid() const = 0;
public:
static uint8_t crc5(const std::string & bitstr);
static uint16_t crc16(const std::string & bitstr);
};
#endif // _RFID_COMMAND_H_

+ 61
- 0
src/RfidCommandAck.cpp View File

@ -0,0 +1,61 @@
#include "RfidCommandAck.h"
#include "RfidTools.h"
RfidCommandAck::RfidCommandAck() :
_data_length(0U)
{
}
const RfidCommandAck::part RfidCommandAck::PARTS[] = {
{0, 16}, // RN
};
void RfidCommandAck::push(bool bit)
{
if ( _data_length < _data.size() ) {
_data_length++;
_data[_data.size()-_data_length] = bit;
}
}
char RfidCommandAck::marker() const
{
return is_valid() ? 'A' : 'a';
}
size_t RfidCommandAck::size() const
{
return _data.size();
}
size_t RfidCommandAck::length() const
{
return _data_length;
}
bool RfidCommandAck::is_ready() const
{
return _data_length >= _data.size();
}
bool RfidCommandAck::is_valid() const
{
return is_ready();
}
std::string RfidCommandAck::to_str() const
{
char buf[80];
snprintf(buf, sizeof(buf), "ACK Rn:0x%04x", rn());
return std::string(buf);
}
uint16_t RfidCommandAck::rn() const
{
auto bs = _data >> (_data.size()-(PARTS[RN].offset+PARTS[RN].size));
std::bitset<16> mask((1U<<PARTS[RN].size)-1U);
bs &= mask;
return (uint16_t)bs.to_ulong();
}

+ 39
- 0
src/RfidCommandAck.h View File

@ -0,0 +1,39 @@
#ifndef _RFID_COMMAND_ACK_H_
#define _RFID_COMMAND_ACK_H_
#include <stdint.h>
#include <bitset>
#include "RfidCommand.h"
class RfidCommandAck : public RfidCommand
{
public:
static const uint16_t CODE = 0b01;
public:
RfidCommandAck();
virtual void push(bool bit);
virtual char marker() const;
virtual size_t size() const;
virtual size_t length() const;
virtual bool is_ready() const;
virtual bool is_valid() const;
virtual std::string to_str() const;
virtual uint16_t code() const{ return CODE; }
uint16_t rn() const;
enum PART {
RN,
_COUNT,
};
public:
static const part PARTS[_COUNT];
protected:
std::bitset<16> _data; // 2 bits are used for command
size_t _data_length;
};
#endif // _RFID_COMMAND_ACK_H_

Loading…
Cancel
Save