Skip to content

Commit

Permalink
Merge pull request bdring#1148 from bdring/UseStdError
Browse files Browse the repository at this point in the history
Use std:error for ESP and FluidNC errors, for better error reporting
  • Loading branch information
bdring authored Mar 11, 2024
2 parents 82ab290 + d390be7 commit 49e3c6b
Show file tree
Hide file tree
Showing 11 changed files with 177 additions and 120 deletions.
25 changes: 25 additions & 0 deletions FluidNC/esp32/esp_error.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
// Copyright (c) 202f Mitch Bradley
// Use of this source code is governed by a GPLv3 license that can be found in the LICENSE file.

// Wrapper to make std:error_code from ESP_IDF esp_err_t values

#include "esp_error.hpp" // Adapter

namespace esp_error {
namespace detail {
class category : public std::error_category {
public:
virtual const char* name() const noexcept override { return "esp_error"; }
virtual std::string message(int value) const override {
// Let the native function do the actual work
return ::esp_err_to_name((esp_err_t)value);
}
};
} // namespace detail

const std::error_category& category() {
// The category singleton
static detail::category instance;
return instance;
}
} // namespace esp_error
12 changes: 12 additions & 0 deletions FluidNC/esp32/esp_error.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
// Copyright (c) 2022 Mitch Bradley
// Use of this source code is governed by a GPLv3 license that can be found in the LICENSE file.

// Wrapper to make std:error_code from ESP_IDF esp_err_t values

#include <esp_err.h> // ESP_IDF error definitions
#include <system_error>

namespace esp_error {
const std::error_category& category();
inline std::error_code make_error_code(esp_err_t err) { return std::error_code(err, esp_error::category()); }
}
4 changes: 0 additions & 4 deletions FluidNC/esp32/localfs.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,6 @@ bool localfs_format(const char* fsname) {
return false;
}
}
log_debug("Trying LittleFS in spiffs partition");
if (!littlefs_format(spiffsName)) {
if (!littlefs_mount(spiffsName)) {
localfsName = littlefsName;
Expand Down Expand Up @@ -144,8 +143,6 @@ const char* canonicalPath(const char* filename, const char* defaultFs) {
static char path[128];
strncpy(path, filename, 128);

// log_debug("filename is " << filename << " deffs " << defaultFs);

// Map file system names to canonical form. The input name is case-independent,
// while the canonical name is lower case.
if (!(replacedFsName(path, "localfs", localfsName) || replacedFsName(path, spiffsName, localfsName) ||
Expand All @@ -166,6 +163,5 @@ const char* canonicalPath(const char* filename, const char* defaultFs) {
insertFsName(path, defaultFs);
}
}
// log_debug("path is " << path);
return path;
}
18 changes: 11 additions & 7 deletions FluidNC/esp32/sdspi.cpp
Original file line number Diff line number Diff line change
@@ -1,18 +1,22 @@
// Copyright (c) 2022 Mitch Bradley
// Use of this source code is governed by a GPLv3 license that can be found in the LICENSE file.

#include "vfs_api.h"
#include "esp_vfs_fat.h"
#include "diskio_impl.h"
#include "diskio_sdmmc.h"
#include "ff.h"
#include "sdmmc_cmd.h"
#include "driver/sdspi_host.h"
#include "esp_error.hpp"

#include "Driver/sdspi.h"
#include "src/Config.h"

#define CHECK_EXECUTE_RESULT(err, str) \
do { \
if ((err) != ESP_OK) { \
log_error(str << " code " << to_hex(err)); \
log_error(str << " code " << to_hex(err)); \
goto cleanup; \
} \
} while (0)
Expand Down Expand Up @@ -70,6 +74,8 @@ static void call_host_deinit(const sdmmc_host_t* host_config) {
bool sd_init_slot(uint32_t freq_hz, int cs_pin, int cd_pin, int wp_pin) {
esp_err_t err;

esp_log_level_set("sdmmc_sd", ESP_LOG_NONE);

// Note: esp_vfs_fat_sdmmc/sdspi_mount is all-in-one convenience functions.
// Please check its source code and implement error recovery when developing
// production applications.
Expand Down Expand Up @@ -139,19 +145,19 @@ std::error_code sd_mount(int max_files) {
BYTE pdrv = FF_DRV_NOT_USED;
if ((err = ff_diskio_get_drive(&pdrv)) != ESP_OK) {
log_debug("ff_diskio_get_drive failed");
return std::error_code(err, std::system_category());
return esp_error::make_error_code(err);
}
if (pdrv == FF_DRV_NOT_USED) {
log_debug("the maximum count of volumes is already mounted");
return std::error_code(ESP_FAIL, std::system_category());
return esp_error::make_error_code(ESP_FAIL);
}
// pdrv is now the index of the unused drive slot

// not using ff_memalloc here, as allocation in internal RAM is preferred
card = (sdmmc_card_t*)malloc(sizeof(sdmmc_card_t));
if (card == NULL) {
log_debug("could not allocate new sdmmc_card_t");
return std::error_code(ESP_ERR_NO_MEM, std::system_category());
return esp_error::make_error_code(ESP_ERR_NO_MEM);
}
// /mount_prepare_mem()

Expand All @@ -166,11 +172,10 @@ std::error_code sd_mount(int max_files) {
cleanup:
free(card);
card = NULL;
return std::error_code(err, std::system_category());
return esp_error::make_error_code(err);
}

void sd_unmount() {
log_verbose("Unmount_sd");
BYTE pdrv = ff_diskio_get_pdrv_card(card);
if (pdrv == 0xff) {
return;
Expand All @@ -190,7 +195,6 @@ void sd_unmount() {
}

void sd_deinit_slot() {
// log_debug("Deinit slot");
sdspi_host_remove_device(host_config.slot);
call_host_deinit(&host_config);

Expand Down
1 change: 0 additions & 1 deletion FluidNC/esp32/spiffs.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ bool spiffs_mount(const char* label, bool format) {

esp_err_t err = esp_vfs_spiffs_register(&conf);
if (err) {
log_debug("Spiffs mount failed: " << esp_err_to_name(err));
return true;
}
return false;
Expand Down
21 changes: 21 additions & 0 deletions FluidNC/src/FluidError.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
#include "FluidError.hpp"

namespace FluidErrorCategory {
const char* error_names[] = { "None", "SDCard not configured" };
namespace detail {
class category : public std::error_category {
public:
virtual const char* name() const noexcept override { return "FluidError"; }
virtual std::string message(int value) const override { return error_names[(int)value]; }
};
}
const std::error_category& category() {
// The category singleton
static detail::category instance;
return instance;
}
}

std::error_code make_error_code(FluidError err) {
return std::error_code(static_cast<int>(err), FluidErrorCategory::category());
}
16 changes: 16 additions & 0 deletions FluidNC/src/FluidError.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
#include <system_error>

// The associated message strings are in FluidError.cpp
enum class FluidError { None, SDNotConfigured };

std::error_code make_error_code(FluidError);

// Declare that FluidError is a standard error code
// This makes it possible to assign a FluidError
// directly to std::error_code variable, e.g.
// std::error_code ec = FluidError::SDNotConfigured

namespace std {
template <>
struct is_error_code_enum<FluidError> : true_type {};
}
14 changes: 12 additions & 2 deletions FluidNC/src/FluidPath.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
#include "Driver/sdspi.h"
#include "Config.h"
#include "Error.h"
#include "Machine/MachineConfig.h"
#include "FluidError.hpp"
#include "HashFS.h"

int FluidPath::_refcnt = 0;
Expand All @@ -14,14 +16,22 @@ FluidPath::FluidPath(const char* name, const char* fs, std::error_code* ecptr) :
_isSD = mount == "sd";

if (_isSD) {
if (!config->_sdCard->config_ok) {
std::error_code ec = FluidError::SDNotConfigured;
if (ecptr) {
*ecptr = ec;
return;
}
throw stdfs::filesystem_error { "SD card is inaccessible", name, ec };
}
if (_refcnt == 0) {
std::error_code ec = sd_mount();
auto ec = sd_mount();
if (ec) {
if (ecptr) {
*ecptr = ec;
return;
}
throw stdfs::filesystem_error { "SD card is inaccessible", ec };
throw stdfs::filesystem_error { "SD card is inaccessible", name, ec };
}
}
++_refcnt;
Expand Down
2 changes: 2 additions & 0 deletions FluidNC/src/SDCard.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,8 @@ void SDCard::init() {
return;
}

config_ok = true;

if (_cardDetect.defined()) {
_cardDetect.setAttr(Pin::Attr::Input);
auto cdPin = _cardDetect.getNative(Pin::Capabilities::Input | Pin::Capabilities::Native);
Expand Down
1 change: 1 addition & 0 deletions FluidNC/src/SDCard.h
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ class SDCard : public Configuration::Configurable {
void afterParse() override;

const char* filename();
bool config_ok = false;

// Initializes pins.
void init();
Expand Down
Loading

0 comments on commit 49e3c6b

Please sign in to comment.