Skip to content

Commit

Permalink
TODO Squash
Browse files Browse the repository at this point in the history
TODO This has major changes to serialWrapper
  • Loading branch information
jrvollmer committed Oct 28, 2023
1 parent bcfd5db commit a0d5535
Show file tree
Hide file tree
Showing 5 changed files with 72 additions and 123 deletions.
77 changes: 33 additions & 44 deletions helpers/serialWrapper.cpp
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
#include "serialWrapper.h"
#include <cstring>


/**
Expand All @@ -16,65 +17,53 @@
* * Default timeout is 10000ms (10s)
*
* Returns:
* size_t: Length of string written to buffer or -1 on error or timeout
* ssize_t: Length of string written to buffer or -1 on error or timeout
**/
size_t SerialWrapper::readLine(char* dest, size_t buffSize, int timeout) {
ssize_t SerialWrapper::readLine(char* dest, size_t buffSize, int timeout) {
// Length of string stored in character buffer
size_t strlen = 0;
// Stores value returned by BufferedSerial.read()
int err = 0;
// Calling printf within loop is too slow
bool overflow = false;
// Whether or not there is a timeout
bool canTimeout = timeout >= 0;

while(1) {
// Check for buffer overflow before read
/*TODO if(strlen >= BUFFER_SIZE) {
overflow = true;
break;
}*/

// Read in from serial
if((err = this->read(dest + strlen, buffSize)) > 0) {
strlen += err;
// TODO Check for buffer overflow after read
if(strlen >= buffSize) {
overflow = true;
break;
}
// Convert to string
dest[strlen] = '\0';

// Check for newline
char* newline; // Location of newline
char n[2] {'\n', '\0'}; // Newline character string TODO Any particular reason 0xd was used instead of '\n'?
if((newline = strstr(dest, n)) != NULL) {
// Change \n to \0
*newline = '\0';
strlen = newline - dest;
break;
}
} else if(err == 0) {
// Nothing read
if(canTimeout) {
if(timeout <= 0) {
// Time out
if(this->readable()) {
// There is data to read
if((err = this->read(dest + strlen, buffSize)) > 0) {
strlen += err;
// Check for buffer overflow after read
if(strlen >= buffSize) {
// Buffer overflow
return -1;
}
ThisThread::sleep_for(100ms);
timeout -= 100;
// Convert to string
dest[strlen] = '\0';

// Check for newline
char* newline; // Location of newline
char n[2] {'\n', '\0'}; // Newline character string TODO Any particular reason 0xd was used instead of '\n'?
if((newline = strstr(dest, n)) != NULL) {
// Change \n to \0
*newline = '\0';
strlen = newline - dest;
break;
}
} else {
// Error reading from buffer
return -1;
}
} else if(canTimeout) {
// Nothing to read and timeout enabled
if(timeout <= 0) {
// Time out
return -1;
}
} else {
// Error reading from buffer
return -1;
ThisThread::sleep_for(100ms);
timeout -= 100;
}
}

if(overflow) {
// Buffer overflow
return -1;
}

return strlen;
}
11 changes: 3 additions & 8 deletions helpers/serialWrapper.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,13 @@
#define __SERIAL_WRAPPER_H__

#include "mbed.h"
#include <string.h>

// TODO REMOVE: #define BUFFER_SIZE 16

class SerialWrapper : public BufferedSerial {
public:
SerialWrapper(PinName tx, PinName rx, int baud=MBED_CONF_PLATFORM_DEFAULT_SERIAL_BAUD_RATE): BufferedSerial(tx, rx, baud) {};
size_t readLine(char* dest, size_t buffSize, int timeout=10000);

// TODO private:
// BufferedSerial *serial;

// By default, connect to USB serial
SerialWrapper(PinName tx=USBTX, PinName rx=USBRX, int baud=MBED_CONF_PLATFORM_DEFAULT_SERIAL_BAUD_RATE): BufferedSerial(tx, rx, baud) {};
ssize_t readLine(char* dest, size_t buffSize, int timeout=10000);
};

#endif // __SERIAL_WRAPPER_H__
6 changes: 4 additions & 2 deletions testRunner.cpp
Original file line number Diff line number Diff line change
@@ -1,14 +1,16 @@
#include "mbed.h"
// Helper includes
#include "serialWrapper.h"
// Test includes
#include "serialTest.cpp"

// Serial interface for BTF communication
SerialWrapper pc(USBTX, USBRX);
SerialWrapper pc;


int main()
{
// Test BTF communication before running other tests
// Verify BTF communication before running other tests
SerialTest serialTest = SerialTest();
if(serialTest.execute()) {
// Run tests
Expand Down
53 changes: 28 additions & 25 deletions tests/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,53 +2,56 @@
This is where all test classes will be defined.

## Creating a New Test Class
All custom test classes must inherit the base `Test` class, as it defines a standard test structure that is required for communicating with the [BTF](https://github.com/badgerloop-software/BTF) test runner. Additionally, `test.h` provides a declaration of `pc`, a `SerialWrapper` object for communicating with the BTF test runner. The following is an example custom test class with implementation notes:
All custom test classes must inherit the base `Test` class (see [`test.h`](test.h)), as it defines a standard test structure that is required for communicating with the [BTF](https://github.com/badgerloop-software/BTF) test runner. Additionally, `test.h` provides a declaration of `pc`, a `SerialWrapper` object for communicating with the BTF test runner. The following is an example custom test class with implementation notes:
```cpp
#ifndef __CUSTOM_TEST__
#define __CUSTOM_TEST__

#include <cstring>
#include "test.h"
#include <string>

using namespace std;


class CustomTest : public Test {
public:
// REQUIRED: Pass the name of the test to the base class' constructor
// The test name should be formatted as shown below, using the keys from tests.yml
// NOTE: If multiple tests have the same setup() and teardown(), you could use
// `CustomTest(const string name): Test(name) {` to run different tests in runTest()
// based on the name and save on redefining setup() and teardown()
CustomTest(): Test("<driver>_<test_name>") {
/**
* REQUIRED: Pass the name of the test to the base class' constructor. The test name
* should be formatted as "<driver>_<test_name>", using the keys from tests.yml
*
* NOTE: If multiple tests have the same setup() and teardown(), you could use
* `CustomTest(const string name): Test(name) {` to run different tests in runTest()
* based on the name and save on redefining setup() and teardown()
*/
CustomTest(): Test("custom_drv_test") {
// Add initialization here
};
private:
// OPTIONAL: Test setup. If not defined, there is no setup
// This is called before starting the test in the BTF
/**
* OPTIONAL: Test setup. If not defined, there is no setup
*
* NOTE: This is called before BTF test setup and execution
*/
void setup() {
// Add custom setup here
}

// REQUIRED: Main test content
// Returns true if test passes, false otherwise
/**
* REQUIRED: Main test content
*
* Returns true if test passes, false otherwise
*/
bool runTest() {
char buf[5];
// You can use printf() and the pc object to communicate with the BTF
// You can use printf() and the pc object to communicate with the BTF test runner
printf("foo\n"); // pc.write() also works
pc.readLine(buff, sizeof(buff));
if(strcmp(buff, "bar") == 0) {
// TODO REMOVE Be sure to let the BTF know if the test passes or fails!
// printf("PASS\n");
return true;
}
// TODO REMOVE Be sure to let the BTF know if the test passes or fails!
// printf("FAIL\n");
return false;
return (strcmp(buff, "bar") == 0);
}

// OPTIONAL: Test teardown. If not defined, there is no teardown
// This is called after ending the test in the BTF
/**
* OPTIONAL: Test teardown. If not defined, there is no teardown
*
* NOTE: This is called after BTF test conclusion TODO REVIEW and teardown REVIEW TODO
*/
void teardown() {
// Add custom teardown here
}
Expand Down
48 changes: 4 additions & 44 deletions tests/test.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,9 @@
#define __TEST_H__

#include "mbed.h"
#include "serialWrapper.h"
#include <cstring>
#include <string>
#include "serialWrapper.h"

using namespace std;

Expand Down Expand Up @@ -40,78 +41,37 @@ class Test {
private:
// Setup for a given test. Does nothing by default
virtual void setup() {};
// Main test content
// Main test content. Return true on success, false on failure
virtual bool runTest() = 0;
// Teardown for a given test. Does nothing by default
virtual void teardown() {};

// Signal to the BTF test runner to start the test
bool signalStart() {
// REMOVE bool started = false;
// REMOVE int attempts = 0;
char buff[7];

// Send start message to BTF test runner
printf("START TEST %s\n", testName.c_str());

// Read response from BTF test runner
// REMOVE Need this? memset(buff, '\0', sizeof(buff));
size_t nread = pc.readLine(buff, sizeof(buff));

// The response should be "READY"
return (nread == 5) && (strcmp(buff, "READY") == 0);
/* REMOVE
do {
// TODO Change to readLine with timeout
if(pc.readable()) {
pc.read(buff, sizeof(buff));
started = strncmp(buff, "READY", 5) == 0;
} else {
ThisThread::sleep_for(100);
attempts++;
}
} while(!started && (attempts < 100));
return started;*/
}

// Signal to the BTF test runner to end the test
bool signalEnd(bool result) {
// REMOVE bool ended = false;
// REMOVE int attempts = 0;
char buff[6];

// Send end message to BTF test runner
if(result) {
printf("PASS\n");
} else {
printf("FAIL\n");
}
printf("%s\n", result ? "PASS" : "FAIL");

// Read response from BTF test runner
// REMOVE Need this? memset(buff, '\0', sizeof(buff));
size_t nread = pc.readLine(buff, sizeof(buff));

// The response should be 'DONE'. If so, preserve the test result
return result && (nread == 4) && (strcmp(buff, "DONE") == 0);

/* REMOVE
do {
// TODO Change to readLine with timeout
if(pc.readable()) {
pc.read(buff, sizeof(buff));
ended = strncmp(buff, "DONE", 4) == 0;
} else {
ThisThread::sleep_for(100);
attempts++;
}
} while(!ended && (attempts < 100));
return ended;*/
}
};

Expand Down

0 comments on commit a0d5535

Please sign in to comment.