Browse Source

clean up crc

master
Tom Early 1 year ago
parent
commit
92ac65d95e
  1. 1
      .gitignore
  2. 4
      Makefile
  3. BIN
      crc-test
  4. 34
      crc.cpp
  5. 3
      crc.h
  6. 5
      m17protocol.cpp
  7. 1
      notification.h
  8. 59
      packet.h

1
.gitignore

@ -11,3 +11,4 @@ configure.h
configure.sql
reflector.cfg
mrefd
crc-test

4
Makefile

@ -36,7 +36,7 @@ endif
LDFLAGS=-pthread
SRCS = callsign.cpp callsignlist.cpp callsignlistitem.cpp client.cpp clients.cpp crc.cpp gatekeeper.cpp ip.cpp m17client.cpp m17peer.cpp m17protocol.cpp notification.cpp packetstream.cpp peer.cpp peers.cpp peercallsignlist.cpp protocol.cpp reflector.cpp timepoint.cpp udpsocket.cpp user.cpp users.cpp version.cpp main.cpp
SRCS = callsign.cpp callsignlist.cpp callsignlistitem.cpp client.cpp clients.cpp crc.cpp gatekeeper.cpp ip.cpp m17client.cpp m17peer.cpp m17protocol.cpp notification.cpp packet.cpp packetstream.cpp peer.cpp peers.cpp peercallsignlist.cpp protocol.cpp reflector.cpp timepoint.cpp udpsocket.cpp user.cpp users.cpp version.cpp main.cpp
OBJS = $(SRCS:.cpp=.o)
DEPS = $(SRCS:.cpp=.d)
@ -55,7 +55,7 @@ $(EXE) : $(OBJS)
g++ $(CFLAGS) $< -o $@
clean :
$(RM) *.o *.d $(EXE)
$(RM) *.o *.d $(EXE) crc-test
-include $(DEPS)

BIN
crc-test

34
crc.cpp

@ -39,19 +39,17 @@
#include "crc.h"
#define CRC_POLY_16 0x5935U
#define CRC_START_16 0xFFFFU
CCRC::CCRC()
{
uint16_t i;
uint16_t j;
uint16_t crc;
uint16_t c;
for (i=0; i<256; i++)
for (uint16_t i=0; i<256; i++)
{
crc = 0;
c = i << 8;
uint16_t crc = 0;
uint16_t c = i << 8;
for (j=0; j<8; j++)
for (uint16_t j=0; j<8; j++)
{
if ( (crc ^ c) & 0x8000 )
crc = ( crc << 1 ) ^ CRC_POLY_16;
@ -60,24 +58,20 @@ CCRC::CCRC()
c = c << 1;
}
crc_tab16[i] = crc;
}
}
uint16_t CCRC::CalcCRC( const uint8_t *input_str, size_t num_bytes )
{
uint16_t crc;
const unsigned char *ptr;
size_t a;
crc = CRC_START_16;
ptr = input_str;
uint16_t crc = CRC_START_16;
const uint8_t *ptr = input_str;
if ( ptr != NULL ) for (a=0; a<num_bytes; a++)
{
crc = (crc << 8) ^ crc_tab16[ ((crc >> 8) ^ (uint16_t) *ptr++) & 0x00FF ];
}
if ( ptr )
for (size_t a=0; a<num_bytes; a++)
{
crc = (crc << 8) ^ crc_tab16[ ((crc >> 8) ^ (uint16_t) *ptr++) & 0x00FF ];
}
return crc;
}

3
crc.h

@ -37,9 +37,6 @@
#include <cstdint>
#include <cstddef>
#define CRC_POLY_16 0x5935U
#define CRC_START_16 0xFFFFU
class CCRC
{
public:

5
m17protocol.cpp

@ -224,8 +224,9 @@ void CM17Protocol::HandleQueue(void)
// is this client busy ?
if ( !client->IsAMaster() && (client->GetReflectorModule() == packet->GetDestModule()) )
{
client->GetCallsign().EncodeCallsign(packet->GetFrame().lich.addr_dst);
packet->SetCRC(crc.CalcCRC(packet->GetFrame().magic, sizeof(AM17Frame) - 2));
// packet->GetFrame().lich.addr_dst won't be correct after this.
client->GetCallsign().EncodeCallsign(packet->GetFrame().lich.addr_dst); // set the destination
packet->SetCRC(crc.CalcCRC(packet->GetFrame().magic, sizeof(AM17Frame) - 2)); // recalculate the crc
Send(packet->GetFrame().magic, sizeof(AM17Frame), client->GetIp());
}
}

1
notification.h

@ -56,5 +56,4 @@ protected:
// data
int m_iId;
CCallsign m_Callsign;
};

59
packet.h

@ -50,55 +50,16 @@ class CPacket
{
public:
CPacket() {}
CPacket(const uint8_t *buf)
{
memcpy(m17.magic, buf, sizeof(AM17Frame));
destination.DecodeCallsign(m17.lich.addr_dst);
source.DecodeCallsign(m17.lich.addr_src);
}
const CCallsign &GetDestCallsign() const
{
return destination;
}
char GetDestModule() const
{
return destination.GetModule();
}
const CCallsign &GetSourceCallsign() const
{
return source;
}
uint16_t GetStreamId() const
{
return ntohs(m17.streamid);
}
uint16_t GetCRC() const
{
return ntohs(m17.crc);
}
void SetCRC(uint16_t crc)
{
m17.crc = htons(crc);
}
std::unique_ptr<CPacket> Duplicate(void) const
{
return std::unique_ptr<CPacket>(new CPacket(*this));
}
bool IsLastPacket() const
{
return (0x8000u & m17.framenumber == 0x8000u);
}
AM17Frame &GetFrame() { return m17; }
CPacket(const uint8_t *buf);
const CCallsign &GetDestCallsign() const;
char GetDestModule() const;
const CCallsign &GetSourceCallsign() const;
uint16_t GetStreamId() const;
uint16_t GetCRC() const;
void SetCRC(uint16_t crc);
std::unique_ptr<CPacket> Duplicate(void) const;
bool IsLastPacket() const;
AM17Frame &GetFrame();
private:
CCallsign destination, source;

Loading…
Cancel
Save