From 759f685b1728d1954735e1d0ef3e0718eb28af2f Mon Sep 17 00:00:00 2001 From: Makiv1 Date: Sat, 30 Nov 2024 14:44:20 -0700 Subject: [PATCH] Implement function which deternimes if the antenna should be deployed on startup. Deploys accordingly --- .../startup_sequence/antenna_deploy_startup.h | 2 +- .../startup_sequence/antenna_deploy_startup.c | 177 ++++++++++++------ 2 files changed, 124 insertions(+), 55 deletions(-) diff --git a/firmware/Core/Inc/startup_sequence/antenna_deploy_startup.h b/firmware/Core/Inc/startup_sequence/antenna_deploy_startup.h index 9e4e42f2b..c01633ade 100644 --- a/firmware/Core/Inc/startup_sequence/antenna_deploy_startup.h +++ b/firmware/Core/Inc/startup_sequence/antenna_deploy_startup.h @@ -1,7 +1,7 @@ #ifndef __INCLUDE_GUARD__ANTENNA_DEPLOY_STARTUP_H__ #define __INCLUDE_GUARD__ANTENNA_DEPLOY_STARTUP_H__ #include -uint8_t START_antenna_deploy(); +int16_t START_antenna_deploy(); #endif // __INCLUDE_GUARD__ANTENNA_DEPLOY_STARTUP_H__ \ No newline at end of file diff --git a/firmware/Core/Src/startup_sequence/antenna_deploy_startup.c b/firmware/Core/Src/startup_sequence/antenna_deploy_startup.c index fbfcba4ee..950e09343 100644 --- a/firmware/Core/Src/startup_sequence/antenna_deploy_startup.c +++ b/firmware/Core/Src/startup_sequence/antenna_deploy_startup.c @@ -4,41 +4,50 @@ #include "log/log.h" #include "antenna_deploy_drivers/ant_commands.h" #include "antenna_deploy_drivers/ant_internal_drivers.h" -uint8_t START_antenna_deploy() { +/// @brief Attempts to read to the file "lifecycle/deploy_antenna_on_boot_enabled.bool". +/// If 1 is stored in the file then it attempts to deploy the antenna. If the file did not +/// exist: then it creates the file, stores a 1 in it, and attempts to deploy. +/// @return 0 on success, <0 on failure +int16_t START_antenna_deploy() { + //TODO: remove after validation LOG_message( LOG_SYSTEM_LFS, LOG_SEVERITY_NORMAL, LOG_SINK_ALL, "Starting Antenna Deploy" ); + uint8_t LFS_unmount_on_completion = 0; if (!LFS_is_lfs_mounted) { - lfs_mount(&LFS_filesystem, &LFS_cfg); + if (lfs_mount(&LFS_filesystem, &LFS_cfg) != 0) { + return -1; + } LFS_unmount_on_completion = 1; } // Create lifecycle directory if it doesn't exist const int32_t mkdir_result = lfs_mkdir(&LFS_filesystem, "lifecycle"); - if(mkdir_result != LFS_ERR_EXIST && mkdir_result != 0) { + if(mkdir_result == LFS_ERR_EXIST) { LOG_message( LOG_SYSTEM_LFS, LOG_SEVERITY_NORMAL, LOG_SINK_ALL, - "Error %d creating lifecycle directory.", - mkdir_result + "lifecycle directory already exists. Skipping creation." ); - //TODO: what happens if directory creation fails? } - if(mkdir_result == LFS_ERR_EXIST) { + if(mkdir_result != LFS_ERR_EXIST && mkdir_result != 0) { LOG_message( LOG_SYSTEM_LFS, LOG_SEVERITY_NORMAL, LOG_SINK_ALL, - "lifecycle directory already exists. Skipping creation." + "Error %d creating lifecycle directory.", + mkdir_result ); + return -2; } + //TODO: remove after validation if(mkdir_result == 0) { LOG_message( LOG_SYSTEM_LFS, @@ -51,62 +60,78 @@ uint8_t START_antenna_deploy() { // Create deploy_antenna_on_boot_enabled.bool file if it doesn't exist - // lfs_file_t file; int32_t open_result = lfs_file_opencfg(&LFS_filesystem, &file, "lifecycle/deploy_antenna_on_boot_enabled.bool", LFS_O_RDONLY, &LFS_file_cfg); - uint8_t file_did_exist = 1; if ( open_result == LFS_ERR_NOENT) { - // file doesn't exist, create it and open for writing - file_did_exist = 0; + + // if file doesn't exist, create it and open for writing and write 1 open_result = lfs_file_opencfg(&LFS_filesystem, &file, "lifecycle/deploy_antenna_on_boot_enabled.bool", LFS_O_WRONLY | LFS_O_CREAT, &LFS_file_cfg); + if(open_result != 0) { + LOG_message( + LOG_SYSTEM_LFS, + LOG_SEVERITY_WARNING, + LOG_SINK_ALL, + "Error %d opening/creating deploy_antenna_on_boot_enabled.bool file.", + open_result + ); + return open_result; + } + + uint8_t buff = 1; + int32_t write_result = lfs_file_write(&LFS_filesystem, &file, &buff, sizeof(buff)); + if(write_result < 0) { + LOG_message( + LOG_SYSTEM_LFS, + LOG_SEVERITY_WARNING, + LOG_SINK_ALL, + "Error %d writing to newly created deploy_antenna_on_boot_enabled.bool file.", + write_result + ); + return write_result; + } + + //TODO: remove after validation LOG_message( LOG_SYSTEM_LFS, LOG_SEVERITY_NORMAL, LOG_SINK_ALL, "file did not exist, created deploy_antenna_on_boot_enabled.bool file." ); - if (open_result == 0) - { - uint8_t buff = 1; - int32_t write_result = lfs_file_write(&LFS_filesystem, &file, &buff, sizeof(buff)); - if(write_result < 0) { - LOG_message( - LOG_SYSTEM_LFS, - LOG_SEVERITY_WARNING, - LOG_SINK_ALL, - "Error %d writing to newly created deploy_antenna_on_boot_enabled.bool file.", - write_result - ); - //TODO: what happens if write fails? - } - - int32_t close_result = lfs_file_close(&LFS_filesystem, &file); - if(close_result < 0) { + + int32_t close_result = lfs_file_close(&LFS_filesystem, &file); + if(close_result != 0) { LOG_message( - LOG_SYSTEM_LFS, - LOG_SEVERITY_WARNING, - LOG_SINK_ALL, - "Error %d closing newly created deploy_antenna_on_boot_enabled.bool file.", - close_result - ); - //TODO: what happens if close fails? - } + LOG_SYSTEM_LFS, + LOG_SEVERITY_WARNING, + LOG_SINK_ALL, + "Error %d closing newly created deploy_antenna_on_boot_enabled.bool file.", + close_result + ); + return close_result; + } - // the fil has been created and 1 (assuming no errors occured) has been written to it, open for reading - open_result = lfs_file_opencfg(&LFS_filesystem, &file, "lifecycle/deploy_antenna_on_boot_enabled.bool", LFS_O_RDONLY, &LFS_file_cfg); + // the file has been created and 1 has been written to it, open for reading + open_result = lfs_file_opencfg(&LFS_filesystem, &file, "lifecycle/deploy_antenna_on_boot_enabled.bool", LFS_O_RDONLY, &LFS_file_cfg); + if (open_result != 0) { + LOG_message( + LOG_SYSTEM_LFS, + LOG_SEVERITY_WARNING, + LOG_SINK_ALL, + "Error %d opening/creating deploy_antenna_on_boot_enabled.bool file.", + open_result + ); + return -5 ; } - - } - //TODO: should this be else if? - else if(open_result != 0) { LOG_message( LOG_SYSTEM_LFS, - LOG_SEVERITY_WARNING, + LOG_SEVERITY_NORMAL, LOG_SINK_ALL, - "Error %d opening/creating deploy_antenna_on_boot_enabled.bool file.", - open_result + "new file created, 1 written, opend for reading." ); } + + + //TODO: remove after validation else if (open_result == 0) { LOG_message( LOG_SYSTEM_LFS, @@ -115,8 +140,8 @@ uint8_t START_antenna_deploy() { "deploy_antenna_on_boot_enabled.bool file opened." ); } + // At this point the depoy_antenna_on_boot_enabled.bool file is open - uint8_t deploy_antenna_on_boot_enabled; int32_t num_bytes_read = lfs_file_read(&LFS_filesystem, &file, &deploy_antenna_on_boot_enabled, sizeof(deploy_antenna_on_boot_enabled)); if(num_bytes_read < 0) { @@ -127,7 +152,9 @@ uint8_t START_antenna_deploy() { "Error %d reading deploy_antenna_on_boot_enabled.bool file.", num_bytes_read ); + return num_bytes_read; } + //TODO: remove after validation if(num_bytes_read == 0) { LOG_message( LOG_SYSTEM_LFS, @@ -136,6 +163,7 @@ uint8_t START_antenna_deploy() { "deploy_antenna_on_boot_enabled.bool file is empty." ); } + //TODO: remove after validation if(num_bytes_read > 0) { LOG_message( LOG_SYSTEM_LFS, @@ -146,18 +174,59 @@ uint8_t START_antenna_deploy() { deploy_antenna_on_boot_enabled ); } - //reading the file is done now, unmount fs and close the file - lfs_file_close(&LFS_filesystem, &file); + //reading the file is done now, close the file and unmount lfs if needed + int close_status = lfs_file_close(&LFS_filesystem, &file); + if( close_status != 0) { + LOG_message( + LOG_SYSTEM_LFS, + LOG_SEVERITY_ERROR, + LOG_SINK_ALL, + "Error %d closing deploy_antenna_on_boot_enabled.bool file.", + close_status + ); + return close_status; + } + if(LFS_unmount_on_completion) { - lfs_unmount(&LFS_filesystem); + int32_t unmount_status = lfs_unmount(&LFS_filesystem); + if (unmount_status != 0) { + LOG_message( + LOG_SYSTEM_LFS, + LOG_SEVERITY_ERROR, + LOG_SINK_ALL, + "Error %d unmounting lfs.", + unmount_status + ); + return unmount_status; + } } if(deploy_antenna_on_boot_enabled == 1) { - //TODO: which mcu should be armed on the antenna deploy system? Error handling - ANT_CMD_arm_antenna_system(ANT_I2C_BUS_A_MCU_A); - ANT_CMD_start_automated_sequential_deployment(ANT_I2C_BUS_A_MCU_A, 7); - } + //TODO: which mcu should be armed on the antenna deploy system? Error handling. How long to activate? + int8_t arm_result = ANT_CMD_arm_antenna_system(ANT_I2C_BUS_A_MCU_A); + if (arm_result != 0) { + LOG_message( + LOG_SYSTEM_ANTENNA_DEPLOY, + LOG_SEVERITY_ERROR, + LOG_SINK_ALL, + "Error %d arming antenna deploy system.", + arm_result + ); + return arm_result; + } + int8_t deploy_result = ANT_CMD_start_automated_sequential_deployment(ANT_I2C_BUS_A_MCU_A, 7); + if (deploy_result != 0) { + LOG_message( + LOG_SYSTEM_ANTENNA_DEPLOY, + LOG_SEVERITY_ERROR, + LOG_SINK_ALL, + "Error %d deploying antennas.", + deploy_result + ); + return deploy_result; + } + } return 0; } \ No newline at end of file