Skip to content

Commit

Permalink
Add more asserts to catch bugs.
Browse files Browse the repository at this point in the history
  • Loading branch information
g4klx committed Mar 7, 2016
1 parent 14c6208 commit 12fb99c
Show file tree
Hide file tree
Showing 12 changed files with 92 additions and 4 deletions.
11 changes: 11 additions & 0 deletions DMRControl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
#include "DMRCSBK.h"
#include "Log.h"

#include <cstdio>
#include <cassert>

CDMRControl::CDMRControl(unsigned int id, unsigned int colorCode, unsigned int timeout, CModem* modem, CDMRIPSC* network, IDisplay* display, bool duplex) :
Expand All @@ -38,6 +39,8 @@ CDMRControl::~CDMRControl()

bool CDMRControl::processWakeup(const unsigned char* data)
{
assert(data != NULL);

// Wakeups always come in on slot 1
if (data[0U] != TAG_DATA || data[1U] != (DMR_IDLE_RX | DMR_SYNC_DATA | DT_CSBK))
return false;
Expand Down Expand Up @@ -65,21 +68,29 @@ bool CDMRControl::processWakeup(const unsigned char* data)

void CDMRControl::writeModemSlot1(unsigned char *data)
{
assert(data != NULL);

m_slot1.writeModem(data);
}

void CDMRControl::writeModemSlot2(unsigned char *data)
{
assert(data != NULL);

m_slot2.writeModem(data);
}

unsigned int CDMRControl::readModemSlot1(unsigned char *data)
{
assert(data != NULL);

return m_slot1.readModem(data);
}

unsigned int CDMRControl::readModemSlot2(unsigned char *data)
{
assert(data != NULL);

return m_slot2.readModem(data);
}

Expand Down
1 change: 1 addition & 0 deletions DMRIPSC.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
#include "Utils.h"
#include "Log.h"

#include <cstdio>
#include <cassert>

const unsigned int BUFFER_LENGTH = 500U;
Expand Down
2 changes: 2 additions & 0 deletions DMRLC.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,8 @@ void CDMRLC::getData(unsigned char* bytes) const

void CDMRLC::getData(bool* bits) const
{
assert(bits != NULL);

unsigned char bytes[9U];
getData(bytes);

Expand Down
8 changes: 8 additions & 0 deletions DMRShortLC.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,8 @@ void CDMRShortLC::encode(const unsigned char* in, unsigned char* out)

void CDMRShortLC::decodeExtractBinary(const unsigned char* in)
{
assert(in != NULL);

CUtils::byteToBitsBE(in[0U], m_rawData + 0U);
CUtils::byteToBitsBE(in[1U], m_rawData + 8U);
CUtils::byteToBitsBE(in[2U], m_rawData + 16U);
Expand Down Expand Up @@ -131,6 +133,8 @@ bool CDMRShortLC::decodeErrorCheck()
// Extract the 36 bits of payload
void CDMRShortLC::decodeExtractData(unsigned char* data) const
{
assert(data != NULL);

bool bData[40U];

for (unsigned int i = 0U; i < 40U; i++)
Expand All @@ -156,6 +160,8 @@ void CDMRShortLC::decodeExtractData(unsigned char* data) const
// Extract the 36 bits of payload
void CDMRShortLC::encodeExtractData(const unsigned char* in) const
{
assert(in != NULL);

bool bData[40U];
CUtils::byteToBitsBE(in[0U], bData + 0U);
CUtils::byteToBitsBE(in[1U], bData + 8U);
Expand Down Expand Up @@ -208,6 +214,8 @@ void CDMRShortLC::encodeInterleave()

void CDMRShortLC::encodeExtractBinary(unsigned char* data)
{
assert(data != NULL);

CUtils::bitsToByteBE(m_rawData + 0U, data[0U]);
CUtils::bitsToByteBE(m_rawData + 8U, data[1U]);
CUtils::bitsToByteBE(m_rawData + 16U, data[2U]);
Expand Down
8 changes: 8 additions & 0 deletions DMRSlot.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,8 @@ CDMRSlot::~CDMRSlot()

void CDMRSlot::writeModem(unsigned char *data)
{
assert(data != NULL);

if (data[0U] == TAG_LOST && m_rfState == RS_RF_AUDIO) {
if (m_rfBits == 0U) m_rfBits = 1U;
LogMessage("DMR Slot %u, RF transmission lost, %.1f seconds, BER: %.1f%%", m_slotNo, float(m_rfFrames) / 16.667F, float(m_rfErrs * 100U) / float(m_rfBits));
Expand Down Expand Up @@ -502,6 +504,8 @@ void CDMRSlot::writeModem(unsigned char *data)

unsigned int CDMRSlot::readModem(unsigned char* data)
{
assert(data != NULL);

if (m_queue.isEmpty())
return 0U;

Expand Down Expand Up @@ -1055,6 +1059,8 @@ void CDMRSlot::clock(unsigned int ms)

void CDMRSlot::writeQueueRF(const unsigned char *data)
{
assert(data != NULL);

if (m_netState != RS_NET_IDLE)
return;

Expand Down Expand Up @@ -1115,6 +1121,8 @@ void CDMRSlot::writeNetworkRF(const unsigned char* data, unsigned char dataType)

void CDMRSlot::writeQueueNet(const unsigned char *data)
{
assert(data != NULL);

unsigned char len = DMR_FRAME_LENGTH_BYTES + 2U;

unsigned int space = m_queue.freeSpace();
Expand Down
5 changes: 5 additions & 0 deletions DStarControl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
#include "Sync.h"
#include "Log.h"

#include <cstdio>
#include <cassert>
#include <ctime>

Expand Down Expand Up @@ -88,6 +89,8 @@ CDStarControl::~CDStarControl()

bool CDStarControl::writeModem(unsigned char *data)
{
assert(data != NULL);

unsigned char type = data[0U];

if (type == TAG_LOST && m_rfState == RS_RF_AUDIO) {
Expand Down Expand Up @@ -343,6 +346,8 @@ bool CDStarControl::writeModem(unsigned char *data)

unsigned int CDStarControl::readModem(unsigned char* data)
{
assert(data != NULL);

if (m_queue.isEmpty())
return 0U;

Expand Down
20 changes: 20 additions & 0 deletions DStarHeader.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -82,51 +82,71 @@ void CDStarHeader::setUnavailable(bool on)

void CDStarHeader::getMyCall1(unsigned char* call1) const
{
assert(call1 != NULL);

::memcpy(call1, m_header + 27U, DSTAR_LONG_CALLSIGN_LENGTH);
}

void CDStarHeader::getMyCall2(unsigned char* call2) const
{
assert(call2 != NULL);

::memcpy(call2, m_header + 35U, DSTAR_SHORT_CALLSIGN_LENGTH);
}

void CDStarHeader::setMyCall1(const unsigned char* call1)
{
assert(call1 != NULL);

::memcpy(m_header + 27U, call1, DSTAR_LONG_CALLSIGN_LENGTH);
}

void CDStarHeader::setMyCall2(const unsigned char* call2)
{
assert(call2 != NULL);

::memcpy(m_header + 35U, call2, DSTAR_SHORT_CALLSIGN_LENGTH);
}

void CDStarHeader::getRPTCall1(unsigned char* call1) const
{
assert(call1 != NULL);

::memcpy(call1, m_header + 11U, DSTAR_LONG_CALLSIGN_LENGTH);
}

void CDStarHeader::getRPTCall2(unsigned char* call2) const
{
assert(call2 != NULL);

::memcpy(call2, m_header + 3U, DSTAR_LONG_CALLSIGN_LENGTH);
}

void CDStarHeader::setRPTCall1(const unsigned char* call1)
{
assert(call1 != NULL);

::memcpy(m_header + 11U, call1, DSTAR_LONG_CALLSIGN_LENGTH);
}

void CDStarHeader::setRPTCall2(const unsigned char* call2)
{
assert(call2 != NULL);

::memcpy(m_header + 3U, call2, DSTAR_LONG_CALLSIGN_LENGTH);
}

void CDStarHeader::getYourCall(unsigned char* call) const
{
assert(call != NULL);

::memcpy(call, m_header + 19U, DSTAR_LONG_CALLSIGN_LENGTH);
}

void CDStarHeader::setYourCall(const unsigned char* call)
{
assert(call != NULL);

::memcpy(m_header + 19U, call, DSTAR_LONG_CALLSIGN_LENGTH);
}

Expand Down
21 changes: 20 additions & 1 deletion Hamming.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (C) 2015 by Jonathan Naylor G4KLX
* Copyright (C) 2015,2016 by Jonathan Naylor G4KLX
*
* 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
Expand All @@ -18,9 +18,14 @@

#include "Hamming.h"

#include <cstdio>
#include <cassert>

// Hamming (15,11,3) check a boolean data array
bool CHamming::decode15113(bool* d)
{
assert(d != NULL);

// Calculate the checksum this row should have
bool c0 = d[0] ^ d[1] ^ d[2] ^ d[3] ^ d[5] ^ d[7] ^ d[8];
bool c1 = d[1] ^ d[2] ^ d[3] ^ d[4] ^ d[6] ^ d[8] ^ d[9];
Expand Down Expand Up @@ -60,6 +65,8 @@ bool CHamming::decode15113(bool* d)

void CHamming::encode15113(bool* d)
{
assert(d != NULL);

// Calculate the checksum this row should have
d[11] = d[0] ^ d[1] ^ d[2] ^ d[3] ^ d[5] ^ d[7] ^ d[8];
d[12] = d[1] ^ d[2] ^ d[3] ^ d[4] ^ d[6] ^ d[8] ^ d[9];
Expand All @@ -70,6 +77,8 @@ void CHamming::encode15113(bool* d)
// Hamming (13,9,3) check a boolean data array
bool CHamming::decode1393(bool* d)
{
assert(d != NULL);

// Calculate the checksum this column should have
bool c0 = d[0] ^ d[1] ^ d[3] ^ d[5] ^ d[6];
bool c1 = d[0] ^ d[1] ^ d[2] ^ d[4] ^ d[6] ^ d[7];
Expand Down Expand Up @@ -107,6 +116,8 @@ bool CHamming::decode1393(bool* d)

void CHamming::encode1393(bool* d)
{
assert(d != NULL);

// Calculate the checksum this column should have
d[9] = d[0] ^ d[1] ^ d[3] ^ d[5] ^ d[6];
d[10] = d[0] ^ d[1] ^ d[2] ^ d[4] ^ d[6] ^ d[7];
Expand All @@ -117,6 +128,8 @@ void CHamming::encode1393(bool* d)
// A Hamming (16,11,4) Check
bool CHamming::decode16114(bool* d)
{
assert(d != NULL);

// Calculate the checksum this column should have
bool c0 = d[0] ^ d[1] ^ d[2] ^ d[3] ^ d[5] ^ d[7] ^ d[8];
bool c1 = d[1] ^ d[2] ^ d[3] ^ d[4] ^ d[6] ^ d[8] ^ d[9];
Expand Down Expand Up @@ -163,6 +176,8 @@ bool CHamming::decode16114(bool* d)

void CHamming::encode16114(bool* d)
{
assert(d != NULL);

d[11] = d[0] ^ d[1] ^ d[2] ^ d[3] ^ d[5] ^ d[7] ^ d[8];
d[12] = d[1] ^ d[2] ^ d[3] ^ d[4] ^ d[6] ^ d[8] ^ d[9];
d[13] = d[2] ^ d[3] ^ d[4] ^ d[5] ^ d[7] ^ d[9] ^ d[10];
Expand All @@ -173,6 +188,8 @@ void CHamming::encode16114(bool* d)
// A Hamming (17,12,3) Check
bool CHamming::decode17123(bool* d)
{
assert(d != NULL);

// Calculate the checksum this column should have
bool c0 = d[0] ^ d[1] ^ d[2] ^ d[3] ^ d[6] ^ d[7] ^ d[9];
bool c1 = d[0] ^ d[1] ^ d[2] ^ d[3] ^ d[4] ^ d[7] ^ d[8] ^ d[10];
Expand Down Expand Up @@ -220,6 +237,8 @@ bool CHamming::decode17123(bool* d)

void CHamming::encode17123(bool* d)
{
assert(d != NULL);

d[12] = d[0] ^ d[1] ^ d[2] ^ d[3] ^ d[6] ^ d[7] ^ d[9];
d[13] = d[0] ^ d[1] ^ d[2] ^ d[3] ^ d[4] ^ d[7] ^ d[8] ^ d[10];
d[14] = d[1] ^ d[2] ^ d[3] ^ d[4] ^ d[5] ^ d[8] ^ d[9] ^ d[11];
Expand Down
3 changes: 3 additions & 0 deletions Modem.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
#include "Log.h"

#include <cmath>
#include <cstdio>
#include <cassert>
#include <cstdint>

Expand Down Expand Up @@ -858,6 +859,8 @@ bool CModem::setFrequency()

RESP_TYPE_MMDVM CModem::getResponse(unsigned char *buffer, unsigned int& length)
{
assert(buffer != NULL);

// Get the start of the frame or nothing at all
int ret = m_serial.read(buffer + 0U, 1U);
if (ret < 0) {
Expand Down
2 changes: 0 additions & 2 deletions TFTSerial.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -65,8 +65,6 @@ bool CTFTSerial::open()

setRotation(ROTATION_LANDSCAPE);

setFontSize(FONT_SMALL);

setBrightness(m_brightness);

// Set background white
Expand Down
10 changes: 9 additions & 1 deletion Utils.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (C) 2009,2014,2015 Jonathan Naylor, G4KLX
* Copyright (C) 2009,2014,2015,2016 Jonathan Naylor, G4KLX
*
* 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
Expand Down Expand Up @@ -91,6 +91,8 @@ void CUtils::dump(int level, const std::string& title, const bool* bits, unsigne

void CUtils::byteToBitsBE(unsigned char byte, bool* bits)
{
assert(bits != NULL);

bits[0U] = (byte & 0x80U) == 0x80U;
bits[1U] = (byte & 0x40U) == 0x40U;
bits[2U] = (byte & 0x20U) == 0x20U;
Expand All @@ -103,6 +105,8 @@ void CUtils::byteToBitsBE(unsigned char byte, bool* bits)

void CUtils::byteToBitsLE(unsigned char byte, bool* bits)
{
assert(bits != NULL);

bits[0U] = (byte & 0x01U) == 0x01U;
bits[1U] = (byte & 0x02U) == 0x02U;
bits[2U] = (byte & 0x04U) == 0x04U;
Expand All @@ -115,6 +119,8 @@ void CUtils::byteToBitsLE(unsigned char byte, bool* bits)

void CUtils::bitsToByteBE(const bool* bits, unsigned char& byte)
{
assert(bits != NULL);

byte = bits[0U] ? 0x80U : 0x00U;
byte |= bits[1U] ? 0x40U : 0x00U;
byte |= bits[2U] ? 0x20U : 0x00U;
Expand All @@ -127,6 +133,8 @@ void CUtils::bitsToByteBE(const bool* bits, unsigned char& byte)

void CUtils::bitsToByteLE(const bool* bits, unsigned char& byte)
{
assert(bits != NULL);

byte = bits[0U] ? 0x01U : 0x00U;
byte |= bits[1U] ? 0x02U : 0x00U;
byte |= bits[2U] ? 0x04U : 0x00U;
Expand Down
Loading

0 comments on commit 12fb99c

Please sign in to comment.