Skip to content

Commit

Permalink
[TS-254] Create Notecard::end()
Browse files Browse the repository at this point in the history
  • Loading branch information
zfields committed Jun 22, 2024
1 parent ae1a122 commit 9c674b6
Show file tree
Hide file tree
Showing 9 changed files with 331 additions and 0 deletions.
9 changes: 9 additions & 0 deletions src/NoteI2c_Arduino.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,15 @@ NoteI2c_Arduino::NoteI2c_Arduino
_i2cPort.begin();
}

NoteI2c_Arduino::~NoteI2c_Arduino (
void
)
{
#if WIRE_HAS_END
_i2cPort.end();
#endif
}

const char *
NoteI2c_Arduino::receive (
uint16_t device_address_,
Expand Down
1 change: 1 addition & 0 deletions src/NoteI2c_Arduino.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ class NoteI2c_Arduino final : public NoteI2c
{
public:
NoteI2c_Arduino(TwoWire & i2c_bus);
~NoteI2c_Arduino(void);
const char * receive(uint16_t device_address, uint8_t * buffer, uint16_t requested_byte_count, uint32_t * available) override;
bool reset(uint16_t device_address) override;
const char * transmit(uint16_t device_address, uint8_t * buffer, uint16_t size) override;
Expand Down
7 changes: 7 additions & 0 deletions src/NoteSerial_Arduino.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,13 @@ NoteSerial_Arduino::NoteSerial_Arduino
_notecardSerial.begin(_notecardSerialSpeed);
}

NoteSerial_Arduino::~NoteSerial_Arduino (
void
)
{
_notecardSerial.end();
}

size_t
NoteSerial_Arduino::available (
void
Expand Down
1 change: 1 addition & 0 deletions src/NoteSerial_Arduino.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ class NoteSerial_Arduino final : public NoteSerial
{
public:
NoteSerial_Arduino(HardwareSerial & hw_serial_, size_t baud_rate_);
~NoteSerial_Arduino(void);
size_t available(void) override;
char receive(void) override;
bool reset(void) override;
Expand Down
21 changes: 21 additions & 0 deletions src/Notecard.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -307,6 +307,27 @@ void Notecard::deleteResponse(J *rsp) const
NoteDeleteResponse(rsp);
}

/**************************************************************************/
/*!
@brief Deinitialize the Notecard object communication.
This function clears the Notecard object's communication
interfaces, and frees all associated memory.
*/
/**************************************************************************/
void Notecard::end(void) const
{
// Clear Communication Interfaces
NoteSetFnI2C(0, 0, nullptr, nullptr, nullptr);
NoteSetFnSerial(nullptr, nullptr, nullptr, nullptr);

// Clear Platform Callbacks
platformInit(false);

// Delete Singletons
noteI2c = make_note_i2c(nullptr);
noteSerial = make_note_serial(nullptr);
}

/**************************************************************************/
/*!
@deprecated NoteDebug, which this function wraps, should be treated as an
Expand Down
1 change: 1 addition & 0 deletions src/Notecard.h
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,7 @@ class Notecard
}
bool debugSyncStatus (int pollFrequencyMs, int maxLevel) const;
void deleteResponse(J *rsp) const;
void end(void) const;
NOTE_ARDUINO_DEPRECATED void logDebug(const char *message) const;
NOTE_ARDUINO_DEPRECATED void logDebugf(const char *format, ...) const;
J *newCommand(const char *request) const;
Expand Down
65 changes: 65 additions & 0 deletions test/NoteI2c_Arduino.test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,66 @@ int test_notei2c_arduino_constructor_invokes_twowire_parameter_begin_method()
return result;
}

#if not defined(WIRE_HAS_END)

int test_notei2c_arduino_deconstructor_does_not_invoke_twowire_end_method()
{
int result;

// Arrange
NoteI2c_Arduino * notei2c = new NoteI2c_Arduino(Wire);
twoWireEnd_Parameters.reset();

// Action
delete notei2c;

// Assert
if (!twoWireEnd_Parameters.invoked)
{
result = 0;
}
else
{
result = static_cast<int>('i' + '2' + 'c');
std::cout << "\33[31mFAILED\33[0m] " << __FILE__ << ":" << __LINE__ << std::endl;
std::cout << "\ttwoWireEnd_Parameters.invoked == " << (!!twoWireEnd_Parameters.invoked ? "true" : "false") << ", EXPECTED: false" << std::endl;
std::cout << "[";
}

return result;
}

#else // defined(WIRE_HAS_END)

int test_notei2c_arduino_deconstructor_invokes_twowire_end_method()
{
int result;

// Arrange
NoteI2c_Arduino * notei2c = new NoteI2c_Arduino(Wire);
twoWireEnd_Parameters.reset();

// Action
delete notei2c;

// Assert
if (twoWireEnd_Parameters.invoked)
{
result = 0;
}
else
{
result = static_cast<int>('i' + '2' + 'c');
std::cout << "\33[31mFAILED\33[0m] " << __FILE__ << ":" << __LINE__ << std::endl;
std::cout << "\ttwoWireEnd_Parameters.invoked == " << (!!twoWireEnd_Parameters.invoked ? "true" : "false") << ", EXPECTED: true" << std::endl;
std::cout << "[";
}

return result;
}

#endif // not defined(WIRE_HAS_END)

int test_notei2c_arduino_receive_requests_response_data_from_notecard()
{
int result;
Expand Down Expand Up @@ -1640,6 +1700,11 @@ int main(void)
{test_make_note_i2c_enforces_singleton_by_returning_same_notei2c_object_for_all_calls, "test_make_note_i2c_enforces_singleton_by_returning_same_notei2c_object_for_all_calls"},
{test_make_note_i2c_deletes_singleton_when_nullptr_is_passed_as_parameter, "test_make_note_i2c_deletes_singleton_when_nullptr_is_passed_as_parameter"},
{test_notei2c_arduino_constructor_invokes_twowire_parameter_begin_method, "test_notei2c_arduino_constructor_invokes_twowire_parameter_begin_method"},
#if not defined(WIRE_HAS_END)
{test_notei2c_arduino_deconstructor_does_not_invoke_twowire_end_method, "test_notei2c_arduino_deconstructor_does_not_invoke_twowire_end_method"},
#else // defined(WIRE_HAS_END)
{test_notei2c_arduino_deconstructor_invokes_twowire_end_method, "test_notei2c_arduino_deconstructor_invokes_twowire_end_method"},
#endif // not defined(WIRE_HAS_END)
{test_notei2c_arduino_receive_requests_response_data_from_notecard, "test_notei2c_arduino_receive_requests_response_data_from_notecard"},
{test_notei2c_arduino_receive_will_retry_transmission_on_i2c_failure, "test_notei2c_arduino_receive_will_retry_transmission_on_i2c_failure"},
{test_notei2c_arduino_receive_will_not_retry_transmission_on_i2c_success, "test_notei2c_arduino_receive_will_not_retry_transmission_on_i2c_success"},
Expand Down
28 changes: 28 additions & 0 deletions test/NoteSerial_Arduino.test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -151,6 +151,33 @@ int test_noteserial_arduino_constructor_does_not_modify_baud_parameter_before_pa
return result;
}

int test_noteserial_arduino_deconstructor_invokes_hardware_serial_end_method()
{
int result;

// Arrange
hardwareSerialEnd_Parameters.reset();
NoteSerial_Arduino * noteserial = new NoteSerial_Arduino(Serial, 9600);

// Action
delete noteserial;

// Assert
if (hardwareSerialEnd_Parameters.invoked)
{
result = 0;
}
else
{
result = static_cast<int>('s' + 'e' + 'r' + 'i' + 'a' + 'l');
std::cout << "\33[31mFAILED\33[0m] " << __FILE__ << ":" << __LINE__ << std::endl;
std::cout << "\thardwareSerialEnd_Parameters.invoked == " << !!hardwareSerialEnd_Parameters.invoked << ", EXPECTED: " << true << std::endl;
std::cout << "[";
}

return result;
}

int test_noteserial_arduino_available_invokes_hardware_serial_available()
{
int result;
Expand Down Expand Up @@ -553,6 +580,7 @@ int main(void)
{test_make_note_serial_deletes_singleton_when_nullptr_is_passed_as_parameter, "test_make_note_serial_deletes_singleton_when_nullptr_is_passed_as_parameter"},
{test_noteserial_arduino_constructor_invokes_hardware_serial_parameter_begin_method, "test_noteserial_arduino_constructor_invokes_hardware_serial_parameter_begin_method"},
{test_noteserial_arduino_constructor_does_not_modify_baud_parameter_before_passing_to_hardware_serial_begin, "test_noteserial_arduino_constructor_does_not_modify_baud_parameter_before_passing_to_hardware_serial_begin"},
{test_noteserial_arduino_deconstructor_invokes_hardware_serial_end_method, "test_noteserial_arduino_deconstructor_invokes_hardware_serial_end_method"},
{test_noteserial_arduino_available_invokes_hardware_serial_available, "test_noteserial_arduino_available_invokes_hardware_serial_available"},
{test_noteserial_arduino_available_does_not_modify_hardware_serial_available_result_value_before_returning_to_caller, "test_noteserial_arduino_available_does_not_modify_hardware_serial_available_result_value_before_returning_to_caller"},
{test_noteserial_arduino_receive_invokes_hardware_serial_read, "test_noteserial_arduino_receive_invokes_hardware_serial_read"},
Expand Down
Loading

0 comments on commit 9c674b6

Please sign in to comment.