Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 4 additions & 2 deletions src/NoteI2c_Arduino.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
#include "NoteI2c_Arduino.hpp"

#include "Notecard.h"

#if defined(NOTE_C_LOW_MEM)
static const char *i2cerr = "i2c {io}";
#endif
Expand Down Expand Up @@ -105,7 +107,7 @@ NoteI2c_Arduino::receive (
if (!transmission_error) {
// Delay briefly ensuring that the Notecard can
// deliver the data in real-time to the I2C ISR
::delay(2);
NoteDelayMs(2);

const int request_length = requested_byte_count_ + NoteI2c::REQUEST_HEADER_SIZE;
const int response_length = _i2cPort.requestFrom((int)device_address_, request_length);
Expand Down Expand Up @@ -139,7 +141,7 @@ NoteI2c_Arduino::receive (
// Flash stalls have been observed on the Notecard ESP. Delaying
// between retries provides time for the Notecard to recover from
// the resource contention.
::delay(1000);
NoteDelayMs(1000);
NOTE_C_LOG_ERROR(result);
NOTE_C_LOG_WARN("serial-over-i2c: reattempting to read Notecard response");
} while (result && (i++ < retry_count));
Expand Down
2 changes: 0 additions & 2 deletions src/NoteI2c_Arduino.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,6 @@

#include "NoteI2c.hpp"

#include "Notecard.h"

#ifndef NOTE_MOCK
#include <Wire.h>
#else
Expand Down
8 changes: 6 additions & 2 deletions src/NoteSerial_Arduino.cpp
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
#include "NoteSerial_Arduino.hpp"

#include "NoteDefines.h"
#include "Notecard.h"

#ifndef NOTE_MOCK
#ifdef NOTE_ARDUINO_SOFTWARE_SERIAL_SUPPORT
#include <SoftwareSerial.h>
#endif
#else
#include "mock/mock-arduino.hpp"
#include "mock/mock-parameters.hpp"
#endif

#define NOTE_C_SERIAL_TIMEOUT_MS 3500
Expand Down Expand Up @@ -68,7 +69,10 @@ NoteSerial_Arduino<T>::NoteSerial_Arduino
_notecardSerial.begin(_notecardSerialSpeed);

// Wait for the serial port to be ready
for (const size_t startMs = ::millis() ; !_notecardSerial && ((::millis() - startMs) < NOTE_C_SERIAL_TIMEOUT_MS) ;);
for (const size_t startMs = NoteGetMs()
; !_notecardSerial && ((NoteGetMs() - startMs) < NOTE_C_SERIAL_TIMEOUT_MS)
;
);
}

template <typename T>
Expand Down
8 changes: 5 additions & 3 deletions src/NoteTxn_Arduino.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
#include "NoteTxn_Arduino.hpp"

#include "Notecard.h"

#ifndef NOTE_MOCK
#include <Arduino.h>
#else
Expand Down Expand Up @@ -78,9 +80,9 @@ NoteTxn_Arduino::start (

// Await Clear To Transact Signal
::pinMode(_ctx_pin, INPUT_PULLUP);
for (uint32_t timeout = (::millis() + timeout_ms_)
; ::millis() < timeout
; ::delay(1)
for (uint32_t timeout = (NoteGetMs() + timeout_ms_)
; NoteGetMs() < timeout
; NoteDelayMs(1)
) {
if (HIGH == ::digitalRead(_ctx_pin)) {
result = true;
Expand Down
4 changes: 1 addition & 3 deletions test/NoteI2c_Arduino.test.cpp
Original file line number Diff line number Diff line change
@@ -1,12 +1,10 @@
#include "NoteI2c_Arduino.hpp"
#include "TestFunction.hpp"
#include "mock/mock-arduino.hpp"
#include "mock/mock-parameters.hpp"

#include <cassert>
#include <cstring>

// Compile command: g++ -Wall -Wextra -Wpedantic mock/mock-arduino.cpp ../src/NoteI2c_Arduino.cpp NoteI2c_Arduino.test.cpp -std=c++11 -I. -I../src -DNOTE_MOCK -ggdb -O0 -o noteI2c_arduino.tests && ./noteI2c_arduino.tests || echo "Tests Result: $?"
// Compile command: g++ -Wall -Wextra -Wpedantic mock/mock-arduino.cpp mock/mock-note-c-note.c ../src/NoteI2c_Arduino.cpp NoteI2c_Arduino.test.cpp -std=c++11 -I. -I../src -DNOTE_MOCK -ggdb -O0 -o noteI2c_arduino.tests && ./noteI2c_arduino.tests || echo "Tests Result: $?"

int test_make_note_i2c_instantiates_notei2c_object()
{
Expand Down
3 changes: 1 addition & 2 deletions test/NoteLog_Arduino.test.cpp
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
#include "NoteLog_Arduino.hpp"
#include "TestFunction.hpp"
#include "mock/mock-arduino.hpp"
#include "mock/mock-parameters.hpp"

#include <cassert>
#include <cstring>
#include <iostream>

// Compile command: g++ -Wall -Wextra -Wpedantic mock/mock-arduino.cpp ../src/NoteLog_Arduino.cpp NoteLog_Arduino.test.cpp -std=c++11 -I. -I../src -DNOTE_MOCK -ggdb -O0 -o noteLog_arduino.tests && ./noteLog_arduino.tests || echo "Tests Result: $?"

Expand Down
4 changes: 1 addition & 3 deletions test/NoteSerial_Arduino.test.cpp
Original file line number Diff line number Diff line change
@@ -1,13 +1,11 @@
#include "NoteSerial_Arduino.hpp"
#include "TestFunction.hpp"
#include "mock/mock-arduino.hpp"
#include "mock/mock-parameters.hpp"

#include <cassert>
#include <cstring>
#include <iostream>

// Compile command: g++ -Wall -Wextra -Wpedantic mock/mock-arduino.cpp ../src/NoteSerial_Arduino.cpp NoteSerial_Arduino.test.cpp -std=c++11 -I. -I../src -DNOTE_MOCK -ggdb -O0 -o noteSerial_arduino.tests && ./noteSerial_arduino.tests || echo "Tests Result: $?"
// Compile command: g++ -Wall -Wextra -Wpedantic mock/mock-arduino.cpp mock/mock-note-c-note.c ../src/NoteSerial_Arduino.cpp NoteSerial_Arduino.test.cpp -std=c++11 -I. -I../src -DNOTE_MOCK -ggdb -O0 -o noteSerial_arduino.tests && ./noteSerial_arduino.tests || echo "Tests Result: $?"

int test_make_note_serial_instantiates_noteserial_object()
{
Expand Down
53 changes: 27 additions & 26 deletions test/NoteTxn_Arduino.test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

#include <cassert>

// Compile command: g++ -std=c++11 -Wall -Wextra -Wpedantic mock/mock-arduino.cpp ../src/NoteTxn_Arduino.cpp NoteTxn_Arduino.test.cpp -I. -I../src -DNOTE_MOCK -ggdb -O0 -o noteTxn_arduino.tests && ./noteTxn_arduino.tests || echo "Tests Result: $?"
// Compile command: g++ -std=c++11 -Wall -Wextra -Wpedantic mock/mock-arduino.cpp mock/mock-note-c-note.c ../src/NoteTxn_Arduino.cpp NoteTxn_Arduino.test.cpp -I. -I../src -DNOTE_MOCK -ggdb -O0 -o noteTxn_arduino.tests && ./noteTxn_arduino.tests || echo "Tests Result: $?"

int test_make_note_txn_instantiates_notetxn_object()
{
Expand Down Expand Up @@ -288,8 +288,8 @@ int test_notetxn_arduino_start_blocks_until_ctx_pin_goes_high()
digitalRead_Parameters.result[CTX_PIN].push_back(LOW);
digitalRead_Parameters.result[CTX_PIN].push_back(LOW);
digitalRead_Parameters.result[CTX_PIN].push_back(HIGH);
millis_Parameters.reset();
millis_Parameters.default_result = (TIMEOUT_MS - 1);
noteGetMs_Parameters.reset();
noteGetMs_Parameters.default_result = (TIMEOUT_MS - 1);

// Action
notetxn.start(TIMEOUT_MS);
Expand All @@ -298,7 +298,7 @@ int test_notetxn_arduino_start_blocks_until_ctx_pin_goes_high()
if (
digitalRead_Parameters.invoked[CTX_PIN] == digitalRead_Parameters.result[CTX_PIN].size()
&& digitalRead_Parameters.result[CTX_PIN][(digitalRead_Parameters.invoked[CTX_PIN] - 1)] == HIGH
&& millis_Parameters.result.empty() // Only returns value less than `TIMEOUT_MS`
&& noteGetMs_Parameters.result.empty() // Only returns value less than `TIMEOUT_MS`
) {
result = 0;
}
Expand Down Expand Up @@ -330,12 +330,13 @@ int test_notetxn_arduino_start_blocks_until_timeout_ms()
NoteTxn_Arduino notetxn(CTX_PIN, RTX_PIN);
digitalRead_Parameters.reset();
digitalRead_Parameters.default_result[CTX_PIN] = LOW;
millis_Parameters.reset();
millis_Parameters.result.push_back(36);
millis_Parameters.result.push_back(274);
millis_Parameters.result.push_back(515);
millis_Parameters.result.push_back(801);
millis_Parameters.result.push_back(TIMEOUT_MS + millis_Parameters.result[0]);
noteDelayMs_Parameters.reset();
noteGetMs_Parameters.reset();
noteGetMs_Parameters.result.push_back(36);
noteGetMs_Parameters.result.push_back(274);
noteGetMs_Parameters.result.push_back(515);
noteGetMs_Parameters.result.push_back(801);
noteGetMs_Parameters.result.push_back(TIMEOUT_MS + noteGetMs_Parameters.result[0]);

// Action
notetxn.start(TIMEOUT_MS);
Expand All @@ -344,7 +345,7 @@ int test_notetxn_arduino_start_blocks_until_timeout_ms()
if (
digitalRead_Parameters.invoked[CTX_PIN] // Called at least once
&& digitalRead_Parameters.result[CTX_PIN].empty() // Only returns `LOW`
&& millis_Parameters.invoked == millis_Parameters.result.size()
&& noteGetMs_Parameters.invoked == noteGetMs_Parameters.result.size()
) {
result = 0;
}
Expand Down Expand Up @@ -412,7 +413,7 @@ int test_notetxn_arduino_start_leaves_rtx_pin_high_when_ctx_responds_high()
digitalRead_Parameters.reset();
digitalRead_Parameters.default_result[CTX_PIN] = HIGH;
digitalWrite_Parameters.reset();
millis_Parameters.reset();
noteGetMs_Parameters.reset();

// Action
notetxn.start(TIMEOUT_MS);
Expand Down Expand Up @@ -486,12 +487,12 @@ int test_notetxn_arduino_start_leaves_ctx_pin_floating_on_timeout()
NoteTxn_Arduino notetxn(CTX_PIN, RTX_PIN);
digitalRead_Parameters.reset();
digitalRead_Parameters.default_result[CTX_PIN] = LOW;
millis_Parameters.reset();
millis_Parameters.result.push_back(36);
millis_Parameters.result.push_back(274);
millis_Parameters.result.push_back(515);
millis_Parameters.result.push_back(801);
millis_Parameters.result.push_back(TIMEOUT_MS + millis_Parameters.result[0]);
noteGetMs_Parameters.reset();
noteGetMs_Parameters.result.push_back(36);
noteGetMs_Parameters.result.push_back(274);
noteGetMs_Parameters.result.push_back(515);
noteGetMs_Parameters.result.push_back(801);
noteGetMs_Parameters.result.push_back(TIMEOUT_MS + noteGetMs_Parameters.result[0]);
pinMode_Parameters.reset();

// Action
Expand All @@ -501,7 +502,7 @@ int test_notetxn_arduino_start_leaves_ctx_pin_floating_on_timeout()
if (
digitalRead_Parameters.invoked[CTX_PIN] // Called at least once
&& digitalRead_Parameters.result[CTX_PIN].empty() // Only returns `LOW`
&& millis_Parameters.invoked == millis_Parameters.result.size()
&& noteGetMs_Parameters.invoked == noteGetMs_Parameters.result.size()
&& pinMode_Parameters.invoked[CTX_PIN] > 1 // Called at least twice
&& pinMode_Parameters.pin_mode[CTX_PIN][(pinMode_Parameters.invoked[CTX_PIN] - 1)] == INPUT
) {
Expand Down Expand Up @@ -532,12 +533,12 @@ int test_notetxn_arduino_start_floats_rtx_pin_on_timeout()
NoteTxn_Arduino notetxn(CTX_PIN, RTX_PIN);
digitalRead_Parameters.reset();
digitalRead_Parameters.default_result[CTX_PIN] = LOW;
millis_Parameters.reset();
millis_Parameters.result.push_back(36);
millis_Parameters.result.push_back(274);
millis_Parameters.result.push_back(515);
millis_Parameters.result.push_back(801);
millis_Parameters.result.push_back(TIMEOUT_MS + millis_Parameters.result[0]);
noteGetMs_Parameters.reset();
noteGetMs_Parameters.result.push_back(36);
noteGetMs_Parameters.result.push_back(274);
noteGetMs_Parameters.result.push_back(515);
noteGetMs_Parameters.result.push_back(801);
noteGetMs_Parameters.result.push_back(TIMEOUT_MS + noteGetMs_Parameters.result[0]);
pinMode_Parameters.reset();

// Action
Expand All @@ -547,7 +548,7 @@ int test_notetxn_arduino_start_floats_rtx_pin_on_timeout()
if (
digitalRead_Parameters.invoked[CTX_PIN] // Called at least once
&& digitalRead_Parameters.result[CTX_PIN].empty() // Only returns `LOW`
&& millis_Parameters.invoked == millis_Parameters.result.size()
&& noteGetMs_Parameters.invoked == noteGetMs_Parameters.result.size()
&& pinMode_Parameters.pin_mode[RTX_PIN][(pinMode_Parameters.invoked[RTX_PIN] - 1)] == INPUT
) {
result = 0;
Expand Down
1 change: 1 addition & 0 deletions test/mock/mock-arduino.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ struct Delay_Parameters {
void
) {
invoked = 0;
mock_time = false;
ms = 0;
}
size_t invoked;
Expand Down
33 changes: 33 additions & 0 deletions test/mock/mock-note-c-note.c
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,11 @@
JAddIntToObject_Parameters jAddIntToObject_Parameters;
NoteDebug_Parameters noteDebug_Parameters;
NoteDebugSyncStatus_Parameters noteDebugSyncStatus_Parameters;
NoteDelayMs_Parameters noteDelayMs_Parameters;
NoteDeleteResponse_Parameters noteDeleteResponse_Parameters;
NoteGetFnI2C_Parameters noteGetFnI2C_Parameters;
NoteGetFnSerial_Parameters noteGetFnSerial_Parameters;
NoteGetMs_Parameters noteGetMs_Parameters;
NoteNewCommand_Parameters noteNewCommand_Parameters;
NoteNewRequest_Parameters noteNewRequest_Parameters;
NoteRequest_Parameters noteRequest_Parameters;
Expand Down Expand Up @@ -104,6 +106,22 @@ NoteDebugSyncStatus(
return noteDebugSyncStatus_Parameters.result;
}

void
NoteDelayMs (
uint32_t ms_
) {
// Record invocation(s)
++noteDelayMs_Parameters.invoked;

// Stash parameter(s)
noteDelayMs_Parameters.ms = ms_;

// Emulate the passing time for timeout loops
if (noteDelayMs_Parameters.mock_time) {
noteGetMs_Parameters.default_result += ms_;
}
}

void NoteGetFnI2C(uint32_t *notecardAddr, uint32_t *maxTransmitSize,
i2cResetFn *resetFn, i2cTransmitFn *transmitFn,
i2cReceiveFn *receiveFn
Expand Down Expand Up @@ -163,6 +181,21 @@ void NoteGetFnSerial(serialResetFn *resetFn, serialTransmitFn *transmitFn,
}
}

uint32_t
NoteGetMs(
void
) {
// Record invocation(s)
++noteGetMs_Parameters.invoked;

// Return user-supplied result
if (noteGetMs_Parameters.result.size() < noteGetMs_Parameters.invoked) {
return noteGetMs_Parameters.default_result;
} else {
return noteGetMs_Parameters.result[(noteGetMs_Parameters.invoked - 1)];
}
}

J *
NoteNewCommand(
const char * request_
Expand Down
44 changes: 44 additions & 0 deletions test/mock/mock-parameters.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,27 @@ struct NoteDebugSyncStatus_Parameters {
bool result;
};

struct NoteDelayMs_Parameters {
NoteDelayMs_Parameters(
void
) :
invoked(0),
mock_time(false),
ms(0)
{ }
void
reset (
void
) {
invoked = 0;
mock_time = false;
ms = 0;
}
size_t invoked;
bool mock_time;
uint32_t ms;
};

struct NoteDeleteResponse_Parameters {
NoteDeleteResponse_Parameters(
void
Expand Down Expand Up @@ -196,6 +217,27 @@ struct NoteGetFnSerial_Parameters {
serialReceiveFn receiveFn_result;
};

struct NoteGetMs_Parameters {
NoteGetMs_Parameters(
void
) :
invoked(0),
default_result(0),
result(0)
{ }
void
reset (
void
) {
invoked = 0;
default_result = 0;
result.clear();
}
size_t invoked;
uint32_t default_result;
std::vector<uint32_t> result;
};

struct NoteNewCommand_Parameters {
NoteNewCommand_Parameters(
void
Expand Down Expand Up @@ -661,9 +703,11 @@ struct NoteSetUserAgent_Parameters {
extern JAddIntToObject_Parameters jAddIntToObject_Parameters;
extern NoteDebug_Parameters noteDebug_Parameters;
extern NoteDebugSyncStatus_Parameters noteDebugSyncStatus_Parameters;
extern NoteDelayMs_Parameters noteDelayMs_Parameters;
extern NoteDeleteResponse_Parameters noteDeleteResponse_Parameters;
extern NoteGetFnI2C_Parameters noteGetFnI2C_Parameters;
extern NoteGetFnSerial_Parameters noteGetFnSerial_Parameters;
extern NoteGetMs_Parameters noteGetMs_Parameters;
extern NoteNewCommand_Parameters noteNewCommand_Parameters;
extern NoteNewRequest_Parameters noteNewRequest_Parameters;
extern NoteRequest_Parameters noteRequest_Parameters;
Expand Down
Loading
Loading