Fixed several buffer overflow issues

This commit is contained in:
2024-08-25 12:06:52 -04:00
parent a59a6657e8
commit 4d47b68600
6 changed files with 142 additions and 103 deletions

View File

@@ -11,6 +11,10 @@
#include "BoardTypes.h" #include "BoardTypes.h"
#include "Vector3D.h" #include "Vector3D.h"
// define some important buffer sizes
static constexpr uint32_t SERIAL_ARG_LENGTH{15};
static constexpr uint32_t SERIAL_CHAR_LENGTH{SERIAL_ARG_LENGTH*10};
// define the physical dimensions of the board // define the physical dimensions of the board
static constexpr uint32_t BOARD_WIDTH{3}; static constexpr uint32_t BOARD_WIDTH{3};
static constexpr uint32_t BOARD_LENGTH{3}; static constexpr uint32_t BOARD_LENGTH{3};

View File

@@ -5,6 +5,11 @@
class V3D{ class V3D{
public: public:
constexpr V3D(const V3D& other):
x(other.x),
y(other.y),
z(other.z){}
constexpr V3D(uint32_t x=0, uint32_t y=0, uint32_t z=0): constexpr V3D(uint32_t x=0, uint32_t y=0, uint32_t z=0):
x(x), x(x),
y(y), y(y),
@@ -17,36 +22,35 @@ class V3D{
return *this; return *this;
} }
V3D& operator+(const V3D &other){ V3D& operator+=(const V3D &other){
V3D vector{}; this->x += other.x;
vector.x = this->x + other.x; this->y += other.y;
vector.y = this->y + other.y; this->z += other.z;
vector.z = this->z + other.z; return *this;
return vector;
} }
V3D& operator-(const V3D &other){ V3D& operator-=(const V3D &other){
V3D vector{}; this->x -= other.x;
vector.x = this->x - other.x; this->y -= other.y;
vector.y = this->y - other.y; this->z -= other.z;
vector.z = this->z - other.z; return *this;
return vector;
} }
V3D operator/(const uint32_t scalar){ V3D& operator/=(const uint32_t scalar){
V3D vector{}; if(scalar == 0){
vector.x = this->x / scalar; return *this;
vector.y = this->y / scalar; }
vector.z = this->z / scalar; this->x /= scalar;
return vector; this->y /= scalar;
this->z /= scalar;
return *this;
} }
V3D operator*(const uint32_t scalar){ V3D& operator*=(const uint32_t scalar){
V3D vector{}; this->x *= scalar;
vector.x = this->x * scalar; this->y *= scalar;
vector.y = this->y * scalar; this->z *= scalar;
vector.z = this->z * scalar; return *this;
return vector;
} }
bool operator==(const V3D &other){ bool operator==(const V3D &other){

View File

@@ -21,6 +21,7 @@ class Board{
constexpr const V3D &GetSize() const{return BOARD_DIMS;} constexpr const V3D &GetSize() const{return BOARD_DIMS;}
constexpr uint32_t GetNumberCubes() const{return BOARD_DIMS.x * BOARD_DIMS.y * BOARD_DIMS.z;} constexpr uint32_t GetNumberCubes() const{return BOARD_DIMS.x * BOARD_DIMS.y * BOARD_DIMS.z;}
constexpr uint32_t GetMaxDimension(){return std::max(std::max(BOARD_DIMS.x, BOARD_DIMS.y), BOARD_DIMS.z);}
/** /**
* @brief Returns a string in the format: * @brief Returns a string in the format:
* !a,b,c,d,e,f,g,h,i; * !a,b,c,d,e,f,g,h,i;
@@ -29,11 +30,6 @@ class Board{
*/ */
void ToStackString(String& stringBuffer) const; void ToStackString(String& stringBuffer) const;
/**
* @returns Returns an array which contains how many cubes are in each z column on the board
*/
std::array<uint32_t, BOARD_DIMS.x * BOARD_DIMS.y> &LinearizeBoard() const;
/** /**
* @brief fill the entire board with the given color * @brief fill the entire board with the given color
* @param color the color to fill the board with * @param color the color to fill the board with
@@ -70,14 +66,19 @@ class Board{
void SetStateChanged(bool boardState){this->boardStateHasChanged = boardState;} void SetStateChanged(bool boardState){this->boardStateHasChanged = boardState;}
/** /**
* @brief Get a column along any axis * @brief Get a column along any axis read into the sliceBuffer
* @param column .z specifies the normal direction of the plane (see PLANE_NORMAL), and * @param column .z specifies the normal direction of the plane (see PLANE_NORMAL), and
* the x,y values specify the location of the column in that plane * the x,y values specify the location of the column in that plane
* to fill. IE To fill one stack at 0,2 I would say give V3D(0,2,PLANE_NORMAL::Z) * to fill. IE To fill one stack at 0,2 I would say give V3D(0,2,PLANE_NORMAL::Z)
* @returns an array of cubes along that column * @param sliceBuffer an array of pointers to the cubes along that column
* @returns the number of elements written into the slice buffer
* @note That array is stored locally and will be overwritten everytime this function is called.
* Also, any unused spots at the end of the array will be nullptrs
* @warning allocate the size of the slice buffer using GetMaxDimension if you don't know what you're doing!
*/ */
BOARD_TYPES::Cube ** SliceBoard(const V3D &column); uint32_t SliceBoard(const V3D &column, BOARD_TYPES::Cube ** sliceBuffer);
void PrintEntireBoard() const;
private: private:
// this is a 3d array of cubes to represent the board. Good luck visualizing it // this is a 3d array of cubes to represent the board. Good luck visualizing it
/* _____________ /* _____________
@@ -97,28 +98,25 @@ class Board{
template <const V3D &BOARD_DIMS> template <const V3D &BOARD_DIMS>
void Board<BOARD_DIMS>::ToStackString(String &stringBuffer) const{ void Board<BOARD_DIMS>::ToStackString(String &stringBuffer) const{
std::array<uint32_t, BOARD_DIMS.x*BOARD_DIMS.y> linearizedBoard = this->LinearizeBoard(); std::array<uint32_t, BOARD_DIMS.x * BOARD_DIMS.y> linearizedBoard;
for(uint32_t x{0}; x < BOARD_DIMS.x; x++){
for(uint32_t y{0}; y < BOARD_DIMS.y; y++){
uint32_t boardIndex{x + y*3};
linearizedBoard[boardIndex] = 0;
for(uint32_t z{0}; z < BOARD_DIMS.z; z++){
linearizedBoard[boardIndex] += this->cubes[x][y][z].isOccupied;
}
}
}
stringBuffer += String(linearizedBoard[0]); stringBuffer += String(linearizedBoard[0]);
for(uint32_t i = 0; i < BOARD_DIMS.x * BOARD_DIMS.y; i++){ for(uint32_t i = 0; i < BOARD_DIMS.x * BOARD_DIMS.y; i++){
stringBuffer += "," + String(linearizedBoard[i]); stringBuffer += "," + String(linearizedBoard[i]);
} }
} // TODO: Delete this before merging into develop
this->PrintEntireBoard();
template <const V3D &BOARD_DIMS>
std::array<uint32_t, BOARD_DIMS.x * BOARD_DIMS.y> & Board<BOARD_DIMS>::LinearizeBoard() const{
// convert the board into one array where each entry represents the height of one stack
std::array<uint32_t, BOARD_DIMS.x * BOARD_DIMS.y> linearizedBoard;
for(uint32_t x{0}; x < BOARD_DIMS.x; x++){
for(uint32_t y{0}; y < BOARD_DIMS.y; y++){
for(uint32_t z{0}; z < BOARD_DIMS.z; z++){
bool isOccupied{this->cubes[x][y][z].isOccupied};
linearizedBoard[x + y*3] += static_cast<uint32_t>(isOccupied);
}
}
}
return linearizedBoard;
} }
template <const V3D &BOARD_DIMS> template <const V3D &BOARD_DIMS>
@@ -145,39 +143,58 @@ void Board<BOARD_DIMS>::SetCubeOccupation(const V3D &position, bool occupation){
} }
template <const V3D &BOARD_DIMS> template <const V3D &BOARD_DIMS>
BOARD_TYPES::Cube ** Board<BOARD_DIMS>::SliceBoard(const V3D &column){ uint32_t Board<BOARD_DIMS>::SliceBoard(const V3D &column, BOARD_TYPES::Cube ** sliceBuffer){
uint32_t columnLength{0}; uint32_t columnLength{0};
V3D indexIncriment{}; V3D indexIncrimentVector{};
V3D position{}; V3D indexVector{};
switch(column.z){ switch(column.z){
case BOARD_TYPES::PLANE_NORMAL::X: case BOARD_TYPES::PLANE_NORMAL::X:
columnLength = BOARD_DIMS.x; columnLength = BOARD_DIMS.x;
indexIncriment.x = 1; indexIncrimentVector.x = 1;
position.z = column.x; indexVector.z = column.x;
position.y = column.y; indexVector.y = column.y;
break; break;
case BOARD_TYPES::PLANE_NORMAL::Y: case BOARD_TYPES::PLANE_NORMAL::Y:
columnLength = BOARD_DIMS.y; columnLength = BOARD_DIMS.y;
indexIncriment.y = 1; indexIncrimentVector.y = 1;
position.x = column.x; indexVector.x = column.x;
position.z = column.y; indexVector.z = column.y;
break; break;
default: default:
case BOARD_TYPES::PLANE_NORMAL::Z: case BOARD_TYPES::PLANE_NORMAL::Z:
columnLength = BOARD_DIMS.z; columnLength = BOARD_DIMS.z;
indexIncriment.z = 1; indexIncrimentVector.z = 1;
position.x = column.x; indexVector.x = column.x;
position.y = column.y; indexVector.y = column.y;
break; break;
} }
BOARD_TYPES::Cube* columnSlice[columnLength];
for(uint32_t i = 0; i < columnLength; i++){ for(uint32_t i = 0; i < columnLength; i++){
V3D cubePosition = indexIncriment * i + position; if(indexVector.x >= BOARD_DIMS.x || indexVector.y >= BOARD_DIMS.y || indexVector.z >= BOARD_DIMS.z){
columnSlice[i] = &(this->cubes[cubePosition.x][cubePosition.y][cubePosition.z]); Serial.println("Board::SliceBoard: Index Out of Bounds:" + String(indexVector.x) + "," + String(indexVector.y) + "," + String(indexVector.z));
return 0;
}
sliceBuffer[i] = &(this->cubes[indexVector.x][indexVector.y][indexVector.z]);
indexVector += indexIncrimentVector;
}
return columnLength;
}
template <const V3D &BOARD_DIMS>
void Board<BOARD_DIMS>::PrintEntireBoard() const{
Serial.println("begin");
for(uint32_t x = 0; x < BOARD_DIMS.x; x++){
for(uint32_t y = 0; y < BOARD_DIMS.y; y++){
for(uint32_t z = 0; z < BOARD_DIMS.z; z++){
const BOARD_TYPES::Cube &cube = this->cubes[x][y][z];
Serial.print("Cube X:" + String(x) + ",Y:" + String(y) + ",Z:" + String(z));
Serial.print("\tColor R:" + String(cube.color.x) + ",G:" + String(cube.color.y) + ",B:" + String(cube.color.z));
Serial.println("\tOccupied? " + String(cube.isOccupied));
}
}
} }
return columnSlice;
} }

View File

@@ -90,6 +90,7 @@ template<uint32_t NUM_STACKS>
void BoardDriver<NUM_STACKS>::Init(){ void BoardDriver<NUM_STACKS>::Init(){
for(uint32_t i = 0; i < NUM_STACKS; i++){ for(uint32_t i = 0; i < NUM_STACKS; i++){
pinMode(this->stacks[i].ledPin, OUTPUT); pinMode(this->stacks[i].ledPin, OUTPUT);
filteredReadings[i] = 0;
} }
// begin doesn't really do anything besides setting the pinmode // begin doesn't really do anything besides setting the pinmode
@@ -159,7 +160,6 @@ uint32_t BoardDriver<NUM_STACKS>::GetNumberCubes(uint32_t stackIndex){
(static_cast<float>(this->filteredReadings[stackIndex]) * 0.9) (static_cast<float>(this->filteredReadings[stackIndex]) * 0.9)
+ (static_cast<float>(value) * 0.1) + (static_cast<float>(value) * 0.1)
); );
// temporary definitions to define value ranges: // temporary definitions to define value ranges:
uint16_t zeroCubesHigh = 4095; uint16_t zeroCubesHigh = 4095;
uint16_t zeroCubesLow = 3400; uint16_t zeroCubesLow = 3400;
@@ -182,5 +182,6 @@ uint32_t BoardDriver<NUM_STACKS>::GetNumberCubes(uint32_t stackIndex){
stackHeight = 3; stackHeight = 3;
} }
this->filteredReadings[stackIndex] = lowPassADCRead;
return stackHeight; return stackHeight;
} }

View File

@@ -39,7 +39,7 @@ class BoardManager{
* @param column the column vector * @param column the column vector
* @param color the color you want the column to be * @param color the color you want the column to be
*/ */
void SetColumnColors(const V3D &column, const V3D *color); void SetColumnColors(const V3D &column, const V3D *color, uint32_t numColors);
/** /**
@@ -64,15 +64,13 @@ class BoardManager{
/** /**
* @brief Get the board occupation state returned in the format a,b,c,d.... * @brief Get the board occupation state returned in the format a,b,c,d....
*/ */
String &Board2StackString(); void Board2StackString(String& messageBuffer);
private: private:
BoardDriver<BOARD_WIDTH*BOARD_LENGTH> &driver; BoardDriver<BOARD_WIDTH*BOARD_LENGTH> &driver;
Board<BOARD_DIMS> board{}; Board<BOARD_DIMS> board{};
bool hasBoardChanged{false}; void updateStackColors(const V3D &column);
void updateBoardColors(const V3D &column);
uint32_t getColumnHeight(BOARD_TYPES::PLANE_NORMAL normal){ uint32_t getColumnHeight(BOARD_TYPES::PLANE_NORMAL normal){
switch(normal){ switch(normal){
@@ -110,17 +108,30 @@ void BoardManager<BOARD_DIMS>::Update(){
uint32_t numCubes{this->driver.GetNumberCubes(stackIndex)}; uint32_t numCubes{this->driver.GetNumberCubes(stackIndex)};
for(uint32_t z = 0; z < BOARD_DIMS.z; z++){ for(uint32_t z = 0; z < BOARD_DIMS.z; z++){
V3D cubePosition{x, y, z}; V3D cubePosition{x, y, z};
// update the cube's occupation
this->board.SetCubeOccupation(cubePosition, z < numCubes); this->board.SetCubeOccupation(cubePosition, z < numCubes);
cubePosition.z = BOARD_TYPES::PLANE_NORMAL::Z;
this->driver.UpdateStackLEDs(stackIndex, this->board.SliceBoard(cubePosition), BOARD_DIMS.z);
} }
// create the column vector for the slice direction
V3D sliceVector{x,y,BOARD_TYPES::PLANE_NORMAL::Z};
// create a cube slice array buffer
BOARD_TYPES::Cube* sliceBuffer[BOARD_DIMS.z];
// have the board slice get read into our buffer
this->board.SliceBoard(sliceVector, sliceBuffer);
// send the board slice to the driver to update its LED colors
this->driver.UpdateStackLEDs(stackIndex, sliceBuffer, BOARD_DIMS.z);
} }
} }
} }
template <const V3D &BOARD_DIMS> template <const V3D &BOARD_DIMS>
void BoardManager<BOARD_DIMS>::updateBoardColors(const V3D &column){ void BoardManager<BOARD_DIMS>::updateStackColors(const V3D &column){
BOARD_TYPES::Cube ** cubeSlice{this->board.SliceBoard(column)}; // the only column type allowed here is z.
V3D sliceVector{column.x, column.y, BOARD_TYPES::Z};
// create a buffer for slice board to write the cube slice into
BOARD_TYPES::Cube * cubeSlice[BOARD_DIMS.z];
this->board.SliceBoard(column, cubeSlice);
uint32_t numCubes{this->getColumnHeight(static_cast<BOARD_TYPES::PLANE_NORMAL>(column.z))}; uint32_t numCubes{this->getColumnHeight(static_cast<BOARD_TYPES::PLANE_NORMAL>(column.z))};
this->driver.UpdateStackLEDs(BOARD_DIMS.x, cubeSlice, numCubes); this->driver.UpdateStackLEDs(BOARD_DIMS.x, cubeSlice, numCubes);
} }
@@ -129,45 +140,45 @@ template <const V3D &BOARD_DIMS>
void BoardManager<BOARD_DIMS>::SetCubeColor(const V3D &position, const V3D &color){ void BoardManager<BOARD_DIMS>::SetCubeColor(const V3D &position, const V3D &color){
this->board.SetCubeColor(position, color); this->board.SetCubeColor(position, color);
V3D slice{position.x, position.y, BOARD_TYPES::PLANE_NORMAL::Z}; V3D slice{position.x, position.y, BOARD_TYPES::PLANE_NORMAL::Z};
this->updateBoardColors(slice); this->updateStackColors(slice);
} }
template <const V3D &BOARD_DIMS> template <const V3D &BOARD_DIMS>
void BoardManager<BOARD_DIMS>::SetColumnColors(const V3D &column, const V3D *color){ void BoardManager<BOARD_DIMS>::SetColumnColors(const V3D &column, const V3D *color, uint32_t numColors){
uint32_t columnHeight{this->getColumnHeight(static_cast<BOARD_TYPES::PLANE_NORMAL>(column.z))}; uint32_t columnHeight{this->getColumnHeight(static_cast<BOARD_TYPES::PLANE_NORMAL>(column.z))};
V3D position = column; // create a cube pointer buffer and store a board slice into it
for(uint32_t z = 0; z < columnHeight; z++){ BOARD_TYPES::Cube * slicedBoard[columnHeight];
position.z = z; Serial.println("moments before slicing");
this->board.SetCubeColor(position, color[z]); uint32_t sliceLength{this->board.SliceBoard(column, slicedBoard)};
Serial.println("setting colors");
uint32_t maxIndex{std::min(numColors, columnHeight)};
for(uint32_t i = 0; i < columnHeight; i++){
slicedBoard[i]->color = color[i];
} }
Serial.println("End of SetColumnColors");
this->updateBoardColors(column);
} }
template <const V3D &BOARD_DIMS> template <const V3D &BOARD_DIMS>
void BoardManager<BOARD_DIMS>::FillColumnColor(const V3D &column, const V3D &color){ void BoardManager<BOARD_DIMS>::FillColumnColor(const V3D &column, const V3D &color){
uint32_t columnHeight{this->getColumnHeight(column.z)}; uint32_t columnHeight{this->getColumnHeight(column.z)};
V3D position = column; V3D colors[columnHeight];
for(uint32_t z = 0; z < columnHeight; z++){ for(uint32_t i = 0; i < columnHeight; i++){
position.z = z; colors[i] = color;
this->board.SetCubeColor(position, color);
} }
this->updateBoardColors(column); this->SetColumnColors(column, colors);
} }
template <const V3D &BOARD_DIMS> template <const V3D &BOARD_DIMS>
bool BoardManager<BOARD_DIMS>::HasBoardChanged(){return this->hasBoardChanged;} bool BoardManager<BOARD_DIMS>::HasBoardChanged(){return this->board.BoardStateChanged();}
template <const V3D &BOARD_DIMS> template <const V3D &BOARD_DIMS>
void BoardManager<BOARD_DIMS>::ClearBoardChanged(){this->hasBoardChanged = false;} void BoardManager<BOARD_DIMS>::ClearBoardChanged(){this->board.SetStateChanged(false);}
template <const V3D &BOARD_DIMS> template <const V3D &BOARD_DIMS>
String &BoardManager<BOARD_DIMS>::Board2StackString(){ void BoardManager<BOARD_DIMS>::Board2StackString(String& messageBuffer){
String message{}; this->board.ToStackString(messageBuffer);
this->board.ToStackString(message);
return message;
} }

View File

@@ -27,7 +27,7 @@ TaskHandle_t updateBoardTask;
// BluetoothSerial SerialBT; // BluetoothSerial SerialBT;
// BluetoothSerialMessage serialMessageBT(&SerialBT); // BluetoothSerialMessage serialMessageBT(&SerialBT);
SerialMessage<500, 10> serialMessage(&Serial); SerialMessage<SERIAL_CHAR_LENGTH, SERIAL_ARG_LENGTH> serialMessage(&Serial);
Adafruit_NeoPixel pixelController{BOARD_HEIGHT*2, STACK1_LED_PIN, NEO_GRB + NEO_KHZ800}; Adafruit_NeoPixel pixelController{BOARD_HEIGHT*2, STACK1_LED_PIN, NEO_GRB + NEO_KHZ800};
@@ -64,7 +64,9 @@ void SetupBluetoothModule(){
void printBoardState(){ void printBoardState(){
GlobalPrint::Print("!0,"); GlobalPrint::Print("!0,");
GlobalPrint::Print(boardManager.Board2StackString()); String boardString;
boardManager.Board2StackString(boardString);
GlobalPrint::Print(boardString);
GlobalPrint::Println(";"); GlobalPrint::Println(";");
} }
@@ -80,21 +82,21 @@ void SetStackColor(uint32_t * args, int argsLength){
V3D colors[numColors]; V3D colors[numColors];
for(int i = 0; i < numColors; i++){ for(int i = 0; i < numColors; i++){
int red = args[2 + (i * 3)]; uint32_t red = args[2 + (i * 3)];
int green = args[3 + (i * 3)]; uint32_t green = args[3 + (i * 3)];
int blue = args[4 + (i * 3)]; uint32_t blue = args[4 + (i * 3)];
colors[i] = V3D{red, green, blue}; colors[i] = V3D{red, green, blue};
} }
boardManager.SetColumnColors(V3D{X_COORD, Y_COORD, BOARD_TYPES::PLANE_NORMAL::Z}, colors, numColors);
boardManager.SetColumnColors(V3D{X_COORD, Y_COORD, BOARD_TYPES::PLANE_NORMAL::Z}, colors);
} }
void parseData(Message<500, 10> &message){ void parseData(Message<SERIAL_CHAR_LENGTH, SERIAL_ARG_LENGTH> &message){
int32_t * args{message.GetArgs()}; int32_t * args{message.GetArgs()};
uint32_t argsLength{message.GetArgsLength()}; uint32_t argsLength{message.GetPopulatedArgs()};
uint32_t command = args[0]; uint32_t command = args[0];
switch(command){ switch(command){
case Commands::BoardState: case Commands::BoardState:
GlobalPrint::Println("Test");
printBoardState(); printBoardState();
break; break;
case Commands::PING: case Commands::PING:
@@ -154,7 +156,7 @@ void UpdateBoard(void * params){
boardManager.ClearBoardChanged(); boardManager.ClearBoardChanged();
} }
boardManager.Update(); // boardManager.Update();
boardStateTimer += updateTickRate; boardStateTimer += updateTickRate;
vTaskDelay(updateTickRate.count()); vTaskDelay(updateTickRate.count());