Skip to content

Commit

Permalink
mock PacketSerial
Browse files Browse the repository at this point in the history
  • Loading branch information
t0mpr1c3 committed Oct 2, 2023
1 parent c023174 commit 69d0ad6
Show file tree
Hide file tree
Showing 16 changed files with 481 additions and 87 deletions.
2 changes: 0 additions & 2 deletions src/ayab/analogReadAsyncWrapper.cpp
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
/*!
* \file analogReadAsyncWrapper.cpp
* \brief Class containing methods to actuate a analogReadAsyncWrapper connected
* to PIEZO_PIN.
*
* This file is part of AYAB.
*
Expand Down
14 changes: 7 additions & 7 deletions src/ayab/com.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@
* http://ayab-knitting.com
*/

#include "packetSerialWrapper.h"

#include "beeper.h"
#include "com.h"
#include "controller.h"
Expand All @@ -34,17 +36,15 @@
* \brief Initialize serial communication.
*/
void Com::init() {
m_packetSerial.begin(SERIAL_BAUDRATE);
#ifndef AYAB_TESTS
m_packetSerial.setPacketHandler(GlobalCom::onPacketReceived);
#endif // AYAB_TESTS
GlobalPacketSerialWrapper::begin(SERIAL_BAUDRATE);
GlobalPacketSerialWrapper::setPacketHandler(GlobalCom::onPacketReceived);
}

/*!
* \brief Service the serial connection.
*/
void Com::update() {
m_packetSerial.update();
GlobalPacketSerialWrapper::update();
}

/*!
Expand Down Expand Up @@ -96,7 +96,7 @@ void Com::send(uint8_t *payload, size_t length) const {
Serial.print(", Encoded as: ");
#endif
*/
m_packetSerial.send(payload, length);
GlobalPacketSerialWrapper::send(payload, length);
}

/*!
Expand All @@ -110,7 +110,7 @@ void Com::sendMsg(API_t id, const char *msg) {
while (*msg) {
msgBuffer[length++] = static_cast<uint8_t>(*msg++);
}
m_packetSerial.send(msgBuffer, length);
GlobalPacketSerialWrapper::send(msgBuffer, length);
}
void Com::sendMsg(API_t id, char *msg) {
sendMsg(id, static_cast<const char *>(msg));
Expand Down
4 changes: 0 additions & 4 deletions src/ayab/com.h
Original file line number Diff line number Diff line change
Expand Up @@ -132,9 +132,6 @@ class GlobalCom final {
static void h_reqTest();
static void h_quitCmd();
static void h_unrecognized();

private:
static SLIPPacketSerial m_packetSerial;
};

class Com : public ComInterface {
Expand All @@ -158,7 +155,6 @@ class Com : public ComInterface {
void h_unrecognized() const final;

private:
SLIPPacketSerial m_packetSerial;
uint8_t lineBuffer[MAX_LINE_BUFFER_LEN] = {0};
uint8_t msgBuffer[MAX_MSG_BUFFER_LEN] = {0};

Expand Down
2 changes: 0 additions & 2 deletions src/ayab/global_analogReadAsyncWrapper.cpp
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
/*!
* \file global_analogReadAsyncWrapper.cpp
* \brief Singleton class containing methods to actuate a analogReadAsyncWrapper
* connected to PIEZO_PIN.
*
* This file is part of AYAB.
*
Expand Down
42 changes: 42 additions & 0 deletions src/ayab/global_packetSerialWrapper.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
/*!
* \file global_packetSerialWrapper.cpp
*
* This file is part of AYAB.
*
* AYAB 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 3 of the License, or
* (at your option) any later version.
*
* AYAB 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 AYAB. If not, see <http://www.gnu.org/licenses/>.
*
* Original Work Copyright 2013 Christian Obersteiner, Andreas Müller
* Modified Work Copyright 2020-3 Sturla Lange, Tom Price
* http://ayab-knitting.com
*/

#include "packetSerialWrapper.h"

// static member functions

void GlobalPacketSerialWrapper::begin(uint32_t speed) {
m_instance->begin(speed);
}

void GlobalPacketSerialWrapper::send(const uint8_t *buffer, size_t size) {
m_instance->send(buffer, size);
}

void GlobalPacketSerialWrapper::setPacketHandler(SLIPPacketSerial::PacketHandlerFunction onPacketFunction) {
m_instance->setPacketHandler(onPacketFunction);
}

void GlobalPacketSerialWrapper::update() {
m_instance->update();
}
6 changes: 6 additions & 0 deletions src/ayab/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@
#include <Arduino.h>

#include "analogReadAsyncWrapper.h"
#include "packetSerialWrapper.h"

#include "beeper.h"
#include "com.h"
#include "controller.h"
Expand All @@ -42,6 +44,8 @@
// Each of the following is a pointer to a singleton class
// containing static methods.
constexpr GlobalAnalogReadAsyncWrapper *analogReadAsyncWrapper;
constexpr GlobalPacketSerialWrapper *packetSerialWrapper;

constexpr GlobalBeeper *beeper;
constexpr GlobalCom *com;
constexpr GlobalController *controller;
Expand All @@ -60,6 +64,8 @@ constexpr GlobalOpError *opError;
// that implements a public interface. When testing, a pointer
// to an instance of a mock class can be substituted.
AnalogReadAsyncWrapperInterface *GlobalAnalogReadAsyncWrapper::m_instance = new AnalogReadAsyncWrapper();
PacketSerialWrapperInterface *GlobalPacketSerialWrapper::m_instance = new PacketSerialWrapper();

BeeperInterface *GlobalBeeper::m_instance = new Beeper();
ComInterface *GlobalCom::m_instance = new Com();
EncodersInterface *GlobalEncoders::m_instance = new Encoders();
Expand Down
67 changes: 67 additions & 0 deletions src/ayab/packetSerialWrapper.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
/*!
* \file packetSerialWrapper.cpp
*
* This file is part of AYAB.
*
* AYAB 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 3 of the License, or
* (at your option) any later version.
*
* AYAB 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 AYAB. If not, see <http://www.gnu.org/licenses/>.
*
* Original Work Copyright 2013 Christian Obersteiner, Andreas Müller
* Modified Work Copyright 2020-3 Sturla Lange, Tom Price
* http://ayab-knitting.com
*/

#include "packetSerialWrapper.h"

/*!
* \brief Wrapper for PacketSerial::begin
*/
void PacketSerialWrapper::begin(uint32_t speed) {
#ifndef AYAB_TESTS
m_packetSerial.begin(speed);
#else
(void) speed;
#endif
}

/*!
* \brief Wrapper for PacketSerial::send
*/
void PacketSerialWrapper::send(const uint8_t *buffer, size_t size) const {
#ifndef AYAB_TESTS
m_packetSerial.send(buffer, size);
#else
(void) buffer;
(void) size;
#endif
}

/*!
* \brief Wrapper for PacketSerial::setPacketHandler
*/
void PacketSerialWrapper::setPacketHandler(SLIPPacketSerial::PacketHandlerFunction onPacketFunction) {
#ifndef AYAB_TESTS
m_packetSerial.setPacketHandler(onPacketFunction);
#else
(void) onPacketFunction;
#endif
}

/*!
* \brief Wrapper for PacketSerial::update
*/
void PacketSerialWrapper::update() {
#ifndef AYAB_TESTS
m_packetSerial.update();
#endif
}
76 changes: 76 additions & 0 deletions src/ayab/packetSerialWrapper.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
/*!
* \file packetSerialWrapper.h
*
* This file is part of AYAB.
*
* AYAB 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 3 of the License, or
* (at your option) any later version.
*
* AYAB 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 AYAB. If not, see <http://www.gnu.org/licenses/>.
*
* Original Work Copyright 2013 Christian Obersteiner, Andreas Müller
* Modified Work Copyright 2020-3 Sturla Lange, Tom Price
* http://ayab-knitting.com
*/

#ifndef PACKETSERIALWRAPPER_H_
#define PACKETSERIALWRAPPER_H_

#include <Arduino.h>
#include <PacketSerial.h>

class PacketSerialWrapperInterface {
public:
virtual ~PacketSerialWrapperInterface() = default;

// any methods that need to be mocked should go here
virtual void begin(uint32_t speed) = 0;
virtual void send(const uint8_t *buffer, size_t size) const = 0;
virtual void setPacketHandler(SLIPPacketSerial::PacketHandlerFunction onPacketFunction) = 0;
virtual void update() = 0;
};

// Container class for the static method packetSerial.
// Dependency injection is enabled using a pointer to a global instance of
// either `PacketSerialWrapper` or `PacketSerialWrapperMock`,
// both of which classes implement the
// pure virtual methods of `PacketSerialWrapperInterface`.

class GlobalPacketSerialWrapper final {
private:
// singleton class so private constructor is appropriate
GlobalPacketSerialWrapper() = default;

public:
// pointer to global instance whose methods are implemented
static PacketSerialWrapperInterface *m_instance;

static void begin(uint32_t speed);
static void send(const uint8_t *buffer, size_t size);
static void setPacketHandler(SLIPPacketSerial::PacketHandlerFunction onPacketFunction);
static void update();
};

/*!
* \brief Wrapper for packetSerial method
*/
class PacketSerialWrapper : public PacketSerialWrapperInterface {
public:
void begin(uint32_t speed) final;
void send(const uint8_t *buffer, size_t size) const final;
void setPacketHandler(SLIPPacketSerial::PacketHandlerFunction onPacketFunction) final;
void update() final;

private:
SLIPPacketSerial m_packetSerial;
};

#endif // PACKETSERIALWRAPPER_H_
8 changes: 7 additions & 1 deletion test/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,9 @@ set(COMMON_SOURCES

${SOURCE_DIRECTORY}/global_analogReadAsyncWrapper.cpp
${PROJECT_SOURCE_DIR}/mocks/analogReadAsyncWrapper_mock.cpp

${SOURCE_DIRECTORY}/global_packetSerialWrapper.cpp
${PROJECT_SOURCE_DIR}/mocks/packetSerialWrapper_mock.cpp
)
set(COMMON_DEFINES
ARDUINO=1819
Expand Down Expand Up @@ -181,8 +184,11 @@ add_executable(${PROJECT_NAME}_knit

${SOURCE_DIRECTORY}/analogReadAsyncWrapper.cpp
${SOURCE_DIRECTORY}/global_analogReadAsyncWrapper.cpp
#${LIBRARY_DIRECTORY}/AnalogReadAsync/src/analogReadAsync.cpp
#${PROJECT_SOURCE_DIR}/test_analogReadAsyncWrapper.cpp

${SOURCE_DIRECTORY}/packetSerialWrapper.cpp
${SOURCE_DIRECTORY}/global_packetSerialWrapper.cpp
#${PROJECT_SOURCE_DIR}/test_packetSerialWrapper.cpp
)
target_include_directories(${PROJECT_NAME}_knit
PRIVATE
Expand Down
59 changes: 59 additions & 0 deletions test/mocks/packetSerialWrapper_mock.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
/*!`
* \file packetSerialWrapper_mock.cpp
*
* This file is part of AYAB.
*
* AYAB 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 3 of the License, or
* (at your option) any later version.
*
* AYAB 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 AYAB. If not, see <http://www.gnu.org/licenses/>.
*
* Original Work Copyright 2013 Christian Obersteiner, Andreas Müller
* Modified Work Copyright 2020-3 Sturla Lange, Tom Price
* http://ayab-knitting.com
*/

#include <packetSerialWrapper_mock.h>

static PacketSerialWrapperMock *gPacketSerialWrapperMock = nullptr;
PacketSerialWrapperMock *packetSerialWrapperMockInstance() {
if (!gPacketSerialWrapperMock) {
gPacketSerialWrapperMock = new PacketSerialWrapperMock();
}
return gPacketSerialWrapperMock;
}

void releasePacketSerialWrapperMock() {
if (gPacketSerialWrapperMock) {
delete gPacketSerialWrapperMock;
gPacketSerialWrapperMock = nullptr;
}
}

void PacketSerialWrapper::begin(uint32_t speed) {
assert(gPacketSerialWrapperMock != nullptr);
gPacketSerialWrapperMock->begin(speed);
}

void PacketSerialWrapper::send(const uint8_t *buffer, size_t size) const {
assert(gPacketSerialWrapperMock != nullptr);
gPacketSerialWrapperMock->send(buffer, size);
}

void PacketSerialWrapper::setPacketHandler(SLIPPacketSerial::PacketHandlerFunction onPacketFunction) {
assert(gPacketSerialWrapperMock != nullptr);
gPacketSerialWrapperMock->setPacketHandler(onPacketFunction);
}

void PacketSerialWrapper::update() {
assert(gPacketSerialWrapperMock != nullptr);
gPacketSerialWrapperMock->update();
}
Loading

0 comments on commit 69d0ad6

Please sign in to comment.