Skip to content

Commit

Permalink
#63 - Mocks for SPISDCard/MMCSDCard/SPIFlash added.
Browse files Browse the repository at this point in the history
  • Loading branch information
PerMalmberg committed Aug 18, 2019
1 parent ae82b06 commit 4de738b
Show file tree
Hide file tree
Showing 20 changed files with 278 additions and 40 deletions.
13 changes: 7 additions & 6 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,10 @@ cmake_minimum_required(VERSION 3.10)
set(CMAKE_CXX_STANDARD 17)

# Select the test project to build
set(selected_test_project hw_interrupt_queue)
set(selected_test_project hw_jsonfile_test)

# For Linux builds, you may enable address sanitizer
set(SMOOTH_ENABLE_ASAN 1)
set(SMOOTH_ENABLE_ASAN 0)
set(SMOOTH_ASAN_OPTIMIZATION_LEVEL 1)

list(APPEND available_tests
Expand All @@ -41,14 +41,15 @@ list(APPEND available_tests
http_server_test
destructing_event_queues
destructing_subscribing_event_queues
hw_interrupt_queue)
hw_interrupt_queue
hw_sdcard_test
hw_spiflash
hw_jsonfile_test)

# These tests needs actual hardware to run
if(${ESP_PLATFORM})
list(APPEND available_tests
hw_spiflash
hw_sdcard_test
hw_jsonfile_test

hw_sntp
hw_security
hw_wrover_kit_blinky
Expand Down
6 changes: 5 additions & 1 deletion lib/files.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,10 @@ set(SMOOTH_SOURCES
${CMAKE_CURRENT_LIST_DIR}/smooth/include/smooth/core/filesystem/MountPoint.h
${CMAKE_CURRENT_LIST_DIR}/smooth/include/smooth/core/io/mock/Input.h
${CMAKE_CURRENT_LIST_DIR}/smooth/include/smooth/core/io/mock/Output.h
${CMAKE_CURRENT_LIST_DIR}/smooth/include/smooth/core/filesystem/mock/SDCard.h
${CMAKE_CURRENT_LIST_DIR}/smooth/include/smooth/core/filesystem/mock/MMCSDCard.h
${CMAKE_CURRENT_LIST_DIR}/smooth/include/smooth/core/filesystem/mock/SPISDCard.h
${CMAKE_CURRENT_LIST_DIR}/smooth/include/smooth/core/filesystem/mock/SPIFlash.h
$ENV{IDF_PATH}/components/json/cJSON/cJSON.c
$ENV{IDF_PATH}/components/json/cJSON/cJSON.h
)
Expand All @@ -147,10 +151,10 @@ if (${ESP_PLATFORM})
${CMAKE_CURRENT_LIST_DIR}/smooth/include/smooth/core/filesystem/SPIFlash.h
${CMAKE_CURRENT_LIST_DIR}/smooth/include/smooth/core/filesystem/SDCard.h
${CMAKE_CURRENT_LIST_DIR}/smooth/include/smooth/core/filesystem/SPISDCard.h
${CMAKE_CURRENT_LIST_DIR}/smooth/include/smooth/core/filesystem/MMCSDCard.h
${CMAKE_CURRENT_LIST_DIR}/smooth/core/sntp/Sntp.cpp
${CMAKE_CURRENT_LIST_DIR}/smooth/include/smooth/core/sntp/Sntp.h
${CMAKE_CURRENT_LIST_DIR}/smooth/core/filesystem/MMCSDCard.cpp
${CMAKE_CURRENT_LIST_DIR}/smooth/include/smooth/core/filesystem/MMCSDCard.h
${CMAKE_CURRENT_LIST_DIR}/smooth/include/smooth/core/io/Input.h
${CMAKE_CURRENT_LIST_DIR}/smooth/include/smooth/core/io/Output.h
${CMAKE_CURRENT_LIST_DIR}/smooth/include/smooth/core/io/i2c/I2CCommandLink.h
Expand Down
6 changes: 6 additions & 0 deletions lib/smooth/core/filesystem/filesystem.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -59,4 +59,10 @@ namespace smooth::core::filesystem
FileInfo fi{path};
return fi.exists();
}

bool is_directory(const Path& path)
{
FileInfo fi{path};
return fi.exists() && fi.is_directory();
}
}
4 changes: 4 additions & 0 deletions lib/smooth/include/smooth/core/filesystem/MMCSDCard.h
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,9 @@

#pragma once

#ifndef ESP_PLATFORM
#include "mock/MMCSDCard.h"
#else
#include "SDCard.h"
#include "driver/sdmmc_host.h"

Expand Down Expand Up @@ -47,3 +50,4 @@ namespace smooth::core::filesystem
gpio_num_t write_protect;
};
}
#endif
15 changes: 12 additions & 3 deletions lib/smooth/include/smooth/core/filesystem/MountPoint.h
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,18 @@
#pragma once

#include <utility>

#include "Path.h"

#ifdef ESP_PLATFORM
static smooth::core::filesystem::Path FlashMount{"/flash}";
static smooth::core::filesystem::Path SDMount{"/sdcard"};
#else
// Place files in home folder on Linux.
static smooth::core::filesystem::Path FMount = smooth::core::filesystem::Path{getenv("HOME")} / "smooth-data" / "flash";
static smooth::core::filesystem::Path SDMount = smooth::core::filesystem::Path{getenv("HOME")} / "smooth-data" / "sdcard";
#endif


namespace smooth::core::filesystem
{
/**
Expand Down Expand Up @@ -61,7 +70,7 @@ namespace smooth::core::filesystem
: public MountPoint
{
public:
static const SDCardMount& instance(Path mount_point = "/sdcard") noexcept
static const SDCardMount& instance(Path mount_point = SDMount) noexcept
{
static SDCardMount sdcm{std::move(mount_point)};
return sdcm;
Expand All @@ -77,7 +86,7 @@ namespace smooth::core::filesystem
: public MountPoint
{
public:
static const FlashMount& instance(Path mount_point = "/flash") noexcept
static const FlashMount& instance(Path mount_point = FMount) noexcept
{
static FlashMount sdcm{std::move(mount_point)};
return sdcm;
Expand Down
13 changes: 10 additions & 3 deletions lib/smooth/include/smooth/core/filesystem/SDCard.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,10 @@

#include "MountPoint.h"

#ifndef ESP_PLATFORM
#include "mock/SDCard.h"
#else

#include <sdmmc_cmd.h>

#pragma GCC diagnostic push
Expand All @@ -41,8 +45,10 @@ namespace smooth::core::filesystem

virtual bool init(const SDCardMount& mount, bool format_on_mount_failure, int max_file_count) = 0;

virtual bool is_initialized() const
{ return initialized; }
[[nodiscard]] virtual bool is_initialized() const
{
return initialized;
}

virtual bool deinit();

Expand All @@ -57,4 +63,5 @@ namespace smooth::core::filesystem
bool initialized{};
private:
};
}
}
#endif
7 changes: 6 additions & 1 deletion lib/smooth/include/smooth/core/filesystem/SPIFlash.h
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,10 @@

#pragma once

#ifndef ESP_PLATFORM
#include "mock/SPIFlash.h"
#else

#include "esp_vfs.h"

#pragma GCC diagnostic push
Expand Down Expand Up @@ -63,4 +67,5 @@ namespace smooth::core::filesystem
bool format_on_mount_failure;
wl_handle_t wl_instance = WL_INVALID_HANDLE;
};
}
}
#endif
6 changes: 6 additions & 0 deletions lib/smooth/include/smooth/core/filesystem/SPISDCard.h
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,10 @@

#pragma once

#ifndef ESP_PLATFORM
#include "mock/SPISDCard.h"
#else

#include "SDCard.h"
#include <driver/gpio.h>
#include "MountPoint.h"
Expand Down Expand Up @@ -45,3 +49,5 @@ namespace smooth::core::filesystem
sdmmc_card_t* card;
};
}

#endif
1 change: 1 addition & 0 deletions lib/smooth/include/smooth/core/filesystem/filesystem.h
Original file line number Diff line number Diff line change
Expand Up @@ -24,4 +24,5 @@ namespace smooth::core::filesystem
bool create_directory(Path&& path);
bool exists(Path&& path);
bool exists(const Path& path);
bool is_directory(const Path& path);
}
43 changes: 43 additions & 0 deletions lib/smooth/include/smooth/core/filesystem/mock/MMCSDCard.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
// Smooth - C++ framework for writing applications based on Espressif's ESP-IDF.
// Copyright (C) 2017 Per Malmberg (https://github.com/PerMalmberg)
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <http://www.gnu.org/licenses/>.

#pragma once

#include "SDCard.h"
#include <smooth/core/io/mock/gpio.h>

namespace smooth::core::filesystem
{
class MMCSDCard
: public SDCard
{
public:
MMCSDCard(gpio_num_t /*command*/,
gpio_num_t /*data0*/,
gpio_num_t /*data1*/,
gpio_num_t /*data2*/,
gpio_num_t /*data3*/,
bool use_1_line_mode = false,
gpio_num_t card_detect = static_cast<gpio_num_t>(-1),
gpio_num_t write_protect = static_cast<gpio_num_t>(-1))
:SDCard()
{
(void)use_1_line_mode;
(void)card_detect;
(void)write_protect;
}
};
}
56 changes: 56 additions & 0 deletions lib/smooth/include/smooth/core/filesystem/mock/SDCard.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
// Smooth - C++ framework for writing applications based on Espressif's ESP-IDF.
// Copyright (C) 2019 Per Malmberg (https://github.com/PerMalmberg)
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <http://www.gnu.org/licenses/>.

#pragma once

#include <smooth/core/filesystem/MountPoint.h>
#include <smooth/core/filesystem/FSLock.h>
#include <smooth/core/logging/log.h>
#include <smooth/core/filesystem/filesystem.h>

namespace smooth::core::filesystem
{
class SDCard
{
public:
virtual ~SDCard() = default;

virtual bool init(const SDCardMount& mount, bool /*format_on_mount_failure*/, int max_file_count)
{
initialized = is_directory(mount.mount_point());

if (!initialized)
{
logging::Log::error("Mock-SDCard", mount.mount_point().str() +
" does not exist, please create it before running.");
}
FSLock::init(max_file_count);

return initialized;
};

[[nodiscard]] virtual bool is_initialized() const
{
return initialized;
}

void deinit() {};

protected:
bool initialized{};
private:
};
}
53 changes: 53 additions & 0 deletions lib/smooth/include/smooth/core/filesystem/mock/SPIFlash.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
// Smooth - C++ framework for writing applications based on Espressif's ESP-IDF.
// Copyright (C) 2017 Per Malmberg (https://github.com/PerMalmberg)
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <http://www.gnu.org/licenses/>.

#pragma once

#include <smooth/core/filesystem/MountPoint.h>
#include <smooth/core/filesystem/FSLock.h>
#include <smooth/core/filesystem/filesystem.h>

namespace smooth::core::filesystem
{
class SPIFlash
{
public:
SPIFlash(const FlashMount& mount_point,
const char* /*partition_name*/,
int max_file_count,
bool /*format_on_mount_failure*/)
{
initialized = is_directory(mount_point.mount_point());

if(!initialized)
{
Log::error("Mock-SPIFlash",
mount_point.mount_point().str() + " does not exist, please create it before running.");
}
FSLock::init(max_file_count);
}

bool mount()
{
return initialized;
}

void unmount() {}

private:
bool initialized{};
};
}
41 changes: 41 additions & 0 deletions lib/smooth/include/smooth/core/filesystem/mock/SPISDCard.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
// Smooth - C++ framework for writing applications based on Espressif's ESP-IDF.
// Copyright (C) 2017 Per Malmberg (https://github.com/PerMalmberg)
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <http://www.gnu.org/licenses/>.

#pragma once

#include "SDCard.h"
#include <smooth/core/filesystem/MountPoint.h>
#include <smooth/core/io/mock/gpio.h>

namespace smooth::core::filesystem
{
class SPISDCard
: public SDCard
{
public:
SPISDCard(gpio_num_t /*miso*/,
gpio_num_t /*mosi*/,
gpio_num_t /*serial_clock*/,
gpio_num_t /*chip_select*/,
gpio_num_t card_detect = GPIO_NUM_NC,
gpio_num_t write_protect = GPIO_NUM_NC)
: SDCard()
{
(void) card_detect;
(void) write_protect;
}
};
}
Loading

0 comments on commit 4de738b

Please sign in to comment.