Files
PX4-Autopilot/src/modules/commander/HealthAndArmingChecks/checks/sdcardCheck.cpp
T
2026-03-11 19:37:01 -07:00

223 lines
7.3 KiB
C++

/****************************************************************************
*
* Copyright (c) 2021 PX4 Development Team. All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in
* the documentation and/or other materials provided with the
* distribution.
* 3. Neither the name PX4 nor the names of its contributors may be
* used to endorse or promote products derived from this software
* without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
* FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
* COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
* BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
* OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
* AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
* ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*
****************************************************************************/
#include "sdcardCheck.hpp"
#include <dirent.h>
#include <string.h>
#ifdef __PX4_DARWIN
#include <sys/param.h>
#include <sys/mount.h>
#elif defined(PX4_STORAGEDIR)
#include <sys/statfs.h>
#endif
void SdCardChecks::checkAndReport(const Context &context, Report &reporter)
{
#ifdef PX4_STORAGEDIR
if (_param_com_arm_sdcard.get() > 0) {
struct statfs statfs_buf;
if (!_sdcard_detected && statfs(PX4_STORAGEDIR, &statfs_buf) == 0) {
// on NuttX we get a data block count f_blocks and byte count per block f_bsize if an SD card is inserted
_sdcard_detected = (statfs_buf.f_blocks > 0) && (statfs_buf.f_bsize > 0);
}
if (!_sdcard_detected) {
NavModes affected_modes{NavModes::None};
if (_param_com_arm_sdcard.get() == 2) {
// disallow arming without sd card
affected_modes = NavModes::All;
}
/* EVENT
* @description
* Insert an SD Card to the autopilot and reboot the system.
*
* <profile name="dev">
* This check can be configured via <param>COM_ARM_SDCARD</param> parameter.
* </profile>
*/
reporter.armingCheckFailure(affected_modes, health_component_t::system,
events::ID("check_missing_fmu_sdcard"),
events::Log::Error, "Missing FMU SD Card");
if (reporter.mavlink_log_pub()) {
mavlink_log_critical(reporter.mavlink_log_pub(), "Preflight Fail: Missing FMU SD Card");
}
}
}
#ifdef __PX4_NUTTX
// Check for hardfault files
if (!_hardfault_checked_once && _param_com_arm_hardfault_check.get()) {
_hardfault_checked_once = true;
DIR *dp = opendir(PX4_STORAGEDIR);
if (dp != nullptr) {
struct dirent *result;
while ((result = readdir(dp)) && !_hardfault_file_present) {
// Check for pattern fault_*.log
if (strncmp("fault_", result->d_name, 6) == 0 && strcmp(result->d_name + strlen(result->d_name) - 4, ".log") == 0) {
_hardfault_file_present = true;
}
}
closedir(dp);
}
}
if (_hardfault_file_present && _param_com_arm_hardfault_check.get()) {
/* EVENT
* @description
* The SD card contains crash dump files, service the vehicle before continuing to fly.
*
* <profile name="dev">
* Check how to debug hardfaults on <a>https://docs.px4.io/main/en/debug/gdb_debugging.html#hard-fault-debugging</a>.
* When completed, remove the files 'fault_*.log' on the SD card.
*
* This check can be configured via <param>COM_ARM_HFLT_CHK</param> parameter.
* </profile>
*/
reporter.healthFailure(NavModes::All, health_component_t::system,
events::ID("check_hardfault_present"),
events::Log::Error, "Crash dumps present on SD card");
if (reporter.mavlink_log_pub()) {
mavlink_log_critical(reporter.mavlink_log_pub(), "Preflight Fail: Crash dumps present on SD");
}
}
#if CONFIG_MODULES_TASK_WATCHDOG
// Check for task watchdog dump files
if (!_watchdog_checked_once && _param_com_arm_hardfault_check.get()) {
_watchdog_checked_once = true;
DIR *wdp = opendir(PX4_STORAGEDIR "/task_watchdog");
if (wdp != nullptr) {
struct dirent *wresult;
while ((wresult = readdir(wdp)) && !_watchdog_file_present) {
// Check for pattern wdg_*.log or load_*.log
const size_t len = strlen(wresult->d_name);
if (len > 5 && strcmp(wresult->d_name + len - 4, ".log") == 0) {
if (strncmp("wdg_", wresult->d_name, 4) == 0
|| strncmp("load_", wresult->d_name, 5) == 0) {
_watchdog_file_present = true;
}
}
}
closedir(wdp);
}
}
if (_watchdog_file_present && _param_com_arm_hardfault_check.get()) {
/* EVENT
* @description
* The SD card contains task watchdog dump files from a previous task starvation event.
*
* <profile name="dev">
* Remove the files in the 'task_watchdog' directory on the SD card after analysis.
*
* This check can be configured via <param>COM_ARM_HFLT_CHK</param> parameter.
* </profile>
*/
reporter.healthFailure(NavModes::All, health_component_t::system,
events::ID("check_task_watchdog_present"),
events::Log::Error, "Task watchdog dumps present on SD card");
if (reporter.mavlink_log_pub()) {
mavlink_log_critical(reporter.mavlink_log_pub(), "Preflight Fail: Task watchdog dumps present on SD");
}
}
#endif /* CONFIG_MODULES_TASK_WATCHDOG */
#endif /* __PX4_NUTTX */
// Check if SDLOG parameters are set to non-default values while SD card logging is disabled
int32_t sdlog_backend = 0;
param_get(_param_sdlog_backend, &sdlog_backend);
const bool sdcard_logging_enabled = (sdlog_backend & 1) != 0;
if (!sdcard_logging_enabled) {
// Check if any SDLOG parameter differs from its default
const param_t sdlog_params[] = {_param_sdlog_mode, _param_sdlog_profile, _param_sdlog_mission, _param_sdlog_dirs_max};
bool sdlog_params_changed = false;
for (auto p : sdlog_params) {
int32_t val, def;
if (param_get(p, &val) == 0 && param_get_default_value(p, &def) == 0 && val != def) {
sdlog_params_changed = true;
break;
}
}
if (sdlog_params_changed) {
/* EVENT
* @description
* SD logging parameters (SDLOG_*) have been modified, but SD card logging
* is not enabled in <param>SDLOG_BACKEND</param>.
*
* <profile name="dev">
* These parameters will have no effect.
* </profile>
*/
reporter.healthFailure(NavModes::None, health_component_t::logging,
events::ID("check_sdlog_params_no_sdcard"),
events::Log::Warning, "SDLOG parameters ineffective without SD card logging");
if (reporter.mavlink_log_pub()) {
mavlink_log_warning(reporter.mavlink_log_pub(), "SDLOG parameters set but SD card logging disabled");
}
}
}
#endif /* PX4_STORAGEDIR */
}