Skip to content
Open
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
3 changes: 2 additions & 1 deletion arch/stm32/Adafruit_LittleFS_stm32/src/Adafruit_LittleFS.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -160,7 +160,8 @@ bool Adafruit_LittleFS::mkdir (char const *filepath)
// make intermediate parent directory(ies)
while ( NULL != (slash = strchr(slash, '/')) )
{
char parent[slash - filepath + 1] = { 0 };
char parent[slash - filepath + 1];
parent[0] = 0;
memcpy(parent, filepath, slash - filepath);

int rc = lfs_mkdir(&_lfs, parent);
Expand Down
29 changes: 23 additions & 6 deletions build.sh
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ Commands:
build-companion-firmwares: Build all companion firmwares for all build targets.
build-repeater-firmwares: Build all repeater firmwares for all build targets.
build-room-server-firmwares: Build all chat room server firmwares for all build targets.
test: Run test on the given target (typically 'native')

Examples:
Build firmware for the "RAK_4631_repeater" device target
Expand Down Expand Up @@ -68,9 +69,7 @@ get_pio_envs_ending_with_string() {
done
}

# build firmware for the provided pio env in $1
build_firmware() {

set_build_env() {
# get git commit sha
COMMIT_HASH=$(git rev-parse --short HEAD)

Expand All @@ -87,13 +86,17 @@ build_firmware() {
# e.g: v1.0.0-abcdef
FIRMWARE_VERSION_STRING="${FIRMWARE_VERSION}-${COMMIT_HASH}"

# add firmware version info to end of existing platformio build flags in environment vars
export PLATFORMIO_BUILD_FLAGS="${PLATFORMIO_BUILD_FLAGS} -DFIRMWARE_BUILD_DATE='\"${FIRMWARE_BUILD_DATE}\"' -DFIRMWARE_VERSION='\"${FIRMWARE_VERSION_STRING}\"'"
}

# build firmware for the provided pio env in $1
build_firmware() {

# craft filename
# e.g: RAK_4631_Repeater-v1.0.0-SHA
FIRMWARE_FILENAME="$1-${FIRMWARE_VERSION_STRING}"

# add firmware version info to end of existing platformio build flags in environment vars
export PLATFORMIO_BUILD_FLAGS="${PLATFORMIO_BUILD_FLAGS} -DFIRMWARE_BUILD_DATE='\"${FIRMWARE_BUILD_DATE}\"' -DFIRMWARE_VERSION='\"${FIRMWARE_VERSION_STRING}\"'"

# build firmware target
pio run -e $1

Expand Down Expand Up @@ -189,6 +192,18 @@ build_firmwares() {
build_room_server_firmwares
}

run_tests() {
envs=($(get_pio_envs_containing_string "$1"))
for env in "${envs[@]}"; do
run_test $env
done
}

run_test() {
set_build_env
pio test -e $1
}

# clean build dir
rm -rf out
mkdir -p out
Expand Down Expand Up @@ -219,4 +234,6 @@ elif [[ $1 == "build-repeater-firmwares" ]]; then
build_repeater_firmwares
elif [[ $1 == "build-room-server-firmwares" ]]; then
build_room_server_firmwares
elif [[ $1 == "test" ]] ; then
run_tests "$2"
fi
27 changes: 27 additions & 0 deletions platformio.ini
Original file line number Diff line number Diff line change
Expand Up @@ -146,3 +146,30 @@ lib_deps =
stevemarple/MicroNMEA @ ^2.0.6
adafruit/Adafruit BME680 Library @ ^2.0.4
adafruit/Adafruit BMP085 Library @ ^1.2.4

; ----------------- Native (for tests) -----------------
[env]
test_framework = googletest
test_speed = 115200

[env:native]
platform = native
lib_compat_mode = off
test_build_src = true
lib_deps =
${arduino_base.lib_deps}
skaygin/ArduinoNative
file://arch/stm32/Adafruit_LittleFS_stm32
file://test/mocks/Wire
file://test/mocks/SPI
build_flags = ${arduino_base.build_flags}
-D_USE_MATH_DEFINES -DNATIVE_PLATFORM -DINPUT_PULLDOWN=0x3
build_src_filter = ${arduino_base.build_src_filter}
-<helpers/AutoDiscoverRTCClock.cpp>

[env:native-asan]
extends = env:native
platform = native
build_flags = ${env:native.build_flags}
-fsanitize=address
-fsanitize=bounds
6 changes: 3 additions & 3 deletions src/Identity.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -61,8 +61,8 @@ bool LocalIdentity::writeTo(Stream& s) const {
}

void LocalIdentity::printTo(Stream& s) const {
s.print("pub_key: "); Utils::printHex(s, pub_key, PUB_KEY_SIZE); s.println();
s.print("prv_key: "); Utils::printHex(s, prv_key, PRV_KEY_SIZE); s.println();
s.print("pub_key: "); Utils::printHex(s, pub_key, PUB_KEY_SIZE); s.println("");
s.print("prv_key: "); Utils::printHex(s, prv_key, PRV_KEY_SIZE); s.println("");
}

size_t LocalIdentity::writeTo(uint8_t* dest, size_t max_len) {
Expand Down Expand Up @@ -96,4 +96,4 @@ void LocalIdentity::calcSharedSecret(uint8_t* secret, const uint8_t* other_pub_k
ed25519_key_exchange(secret, other_pub_key, prv_key);
}

}
}
3 changes: 2 additions & 1 deletion src/helpers/AdvertDataHelpers.cpp
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
#include <helpers/AdvertDataHelpers.h>
#include <stdio.h>

uint8_t AdvertDataBuilder::encodeTo(uint8_t app_data[]) {
app_data[0] = _type;
Expand Down Expand Up @@ -84,4 +85,4 @@ void AdvertTimeHelper::formatRelativeTimeDiff(char dest[], int32_t seconds_from_
}
}
}
}
}
2 changes: 1 addition & 1 deletion src/helpers/BaseChatMesh.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -159,7 +159,7 @@ void BaseChatMesh::onPeerDataRecv(mesh::Packet* packet, uint8_t type, int sender
if (type == PAYLOAD_TYPE_TXT_MSG && len > 5) {
uint32_t timestamp;
memcpy(&timestamp, data, 4); // timestamp (by sender's RTC clock - which could be wrong)
uint flags = data[4] >> 2; // message attempt number, and other flags
uint8_t flags = data[4] >> 2; // message attempt number, and other flags

// len can be > original length, but 'text' will be padded with zeroes
data[len] = 0; // need to make a C string again, with null terminator
Expand Down
2 changes: 1 addition & 1 deletion src/helpers/ClientACL.cpp
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#include "ClientACL.h"

static File openWrite(FILESYSTEM* _fs, const char* filename) {
#if defined(NRF52_PLATFORM) || defined(STM32_PLATFORM)
#if defined(NRF52_PLATFORM) || defined(STM32_PLATFORM) || defined(NATIVE_PLATFORM)
_fs->remove(filename);
return _fs->open(filename, FILE_O_WRITE);
#elif defined(RP2040_PLATFORM)
Expand Down
2 changes: 1 addition & 1 deletion src/helpers/CommonCLI.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ void CommonCLI::loadPrefsInt(FILESYSTEM* fs, const char* filename) {
}

void CommonCLI::savePrefs(FILESYSTEM* fs) {
#if defined(NRF52_PLATFORM) || defined(STM32_PLATFORM)
#if defined(NRF52_PLATFORM) || defined(STM32_PLATFORM) || defined(NATIVE_PLATFORM)
fs->remove("/com_prefs");
File file = fs->open("/com_prefs", FILE_O_WRITE);
#elif defined(RP2040_PLATFORM)
Expand Down
4 changes: 2 additions & 2 deletions src/helpers/IdentityStore.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ bool IdentityStore::save(const char *name, const mesh::LocalIdentity& id) {
char filename[40];
sprintf(filename, "%s/%s.id", _dir, name);

#if defined(NRF52_PLATFORM) || defined(STM32_PLATFORM)
#if defined(NRF52_PLATFORM) || defined(STM32_PLATFORM) || defined(NATIVE_PLATFORM)
_fs->remove(filename);
File file = _fs->open(filename, FILE_O_WRITE);
#elif defined(RP2040_PLATFORM)
Expand All @@ -68,7 +68,7 @@ bool IdentityStore::save(const char *name, const mesh::LocalIdentity& id, const
char filename[40];
sprintf(filename, "%s/%s.id", _dir, name);

#if defined(NRF52_PLATFORM) || defined(STM32_PLATFORM)
#if defined(NRF52_PLATFORM) || defined(STM32_PLATFORM) || defined(NATIVE_PLATFORM)
_fs->remove(filename);
File file = _fs->open(filename, FILE_O_WRITE);
#elif defined(RP2040_PLATFORM)
Expand Down
2 changes: 1 addition & 1 deletion src/helpers/IdentityStore.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
#if defined(ESP32) || defined(RP2040_PLATFORM)
#include <FS.h>
#define FILESYSTEM fs::FS
#elif defined(NRF52_PLATFORM) || defined(STM32_PLATFORM)
#elif defined(NRF52_PLATFORM) || defined(STM32_PLATFORM) || defined(NATIVE_PLATFORM)
#include <Adafruit_LittleFS.h>
#define FILESYSTEM Adafruit_LittleFS

Expand Down
1 change: 1 addition & 0 deletions src/helpers/radiolib/RadioLibWrappers.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

#include <Mesh.h>
#include <RadioLib.h>
#include <Arduino.h>

class RadioLibWrapper : public mesh::Radio {
protected:
Expand Down
44 changes: 44 additions & 0 deletions test/mocks/SPI/SPI.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
#ifndef SPI_H
#define SPI_H

typedef int BitOrder;


typedef enum {
SPI_MODE0 = 0,
SPI_MODE1 = 1,
SPI_MODE2 = 2,
SPI_MODE3 = 3,
} SPIMode;

class SPISettings {
public:
SPISettings(uint32_t clock, BitOrder bitOrder, SPIMode dataMode) {}
SPISettings(uint32_t clock, BitOrder bitOrder, uint8_t dataMode) {}
};

class SPIClass
{
public:
uint8_t transfer(uint8_t data) { return 0; }
uint16_t transfer16(uint16_t data) { return 0; }
void transfer(void *buf, size_t count) {}

void transfer(const void *txbuf, void *rxbuf, size_t count) {}

void usingInterrupt(int interruptNumber) {}
void notUsingInterrupt(int interruptNumber) {}
void beginTransaction(SPISettings settings) {}
void endTransaction(void) {}

void attachInterrupt() {}
void detachInterrupt() {}

void begin() {}
void end() {}
};

SPIClass SPI;


#endif
39 changes: 39 additions & 0 deletions test/mocks/Wire/Wire.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
#ifndef Wire_h
#define Wire_h

#include "Stream.h"

class TwoWire : public Stream
{
public:
TwoWire(uint8_t bus_num){};
~TwoWire(){};
bool setPins(int sda, int scl){};
bool begin(){return true;}
bool begin(uint8_t addr){return true;}
void beginTransmission(uint16_t address){}
void beginTransmission(uint8_t address){}
void beginTransmission(int address){}
uint8_t endTransmission(bool sendStop) { return 0; }
uint8_t endTransmission(void) { return 0; }
size_t requestFrom(uint16_t address, size_t size, bool sendStop) { return 0; }
uint8_t requestFrom(uint16_t address, uint8_t size, bool sendStop) { return 0; }
uint8_t requestFrom(uint16_t address, uint8_t size, uint8_t sendStop) { return 0; }
size_t requestFrom(uint8_t address, size_t len, bool stopBit) { return 0; }
uint8_t requestFrom(uint16_t address, uint8_t size) { return 0; }
uint8_t requestFrom(uint8_t address, uint8_t size, uint8_t sendStop) { return 0; }
uint8_t requestFrom(uint8_t address, uint8_t size) { return 0; }
uint8_t requestFrom(int address, int size, int sendStop) { return 0; }
size_t write(uint8_t) { return 1; }
size_t write(const uint8_t *b, size_t n) { return n; }
int available(){ return 0; }
int read(void) { return 0; }
int peek(void) { return 0; }
bool end(){};
};

extern TwoWire Wire;
extern TwoWire Wire1;

#endif

8 changes: 8 additions & 0 deletions test/test_common/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
# Common tests

This directory holds tests that are expected to pass on all platforms,
including native and on-device tests.

Tests that exercise device-specific features should should not go here,
and should be capable of passing with hardware features mocked out
(e.g. SPI or Wire are present but return fake responses.)
62 changes: 62 additions & 0 deletions test/test_common/mock_streams.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
#pragma once

#include <gtest/gtest.h>
#include <gmock/gmock.h>

class MockStream : public Stream {
public:
uint8_t *buffer;
size_t pos;
MockStream(uint8_t *b)
:buffer(b),pos(0)
{
buffer[0] = 0;
}

void clear() {
pos = 0;
buffer[0] = 0;
}

size_t write(uint8_t c) {
buffer[pos++] = c;
buffer[pos] = 0;
return 1;
}

size_t write(const uint8_t *src, size_t size) override {
memcpy(buffer, src, size);
pos += size;
return size;
}

MOCK_METHOD(int, available, (), (override));
MOCK_METHOD(int, availableForWrite, (), (override));
MOCK_METHOD(int, read, (), (override));
MOCK_METHOD(int, peek, (), (override));
};

class ConstantValueStream : public Stream {
public:
const uint8_t *buffer;
size_t pos, len;

ConstantValueStream(const uint8_t *b, size_t l)
:buffer(b),pos(0),len(l)
{}

int available() {
return (int)(len - pos);
}
MOCK_METHOD(size_t, write, (uint8_t c), (override));
MOCK_METHOD(size_t, write, (const uint8_t *buffer, size_t size), (override));
MOCK_METHOD(int, availableForWrite, (), (override));
int read() {
if (pos >= len) {
return 0;
}
return (int)buffer[pos++];
}
MOCK_METHOD(int, peek, (), (override));
};

Loading