Skip to content

Commit

Permalink
Implement function which deternimes if the antenna should be deployed…
Browse files Browse the repository at this point in the history
… on startup. Deploys accordingly
  • Loading branch information
Makiv1 committed Nov 30, 2024
1 parent d4d1f8b commit 759f685
Show file tree
Hide file tree
Showing 2 changed files with 124 additions and 55 deletions.
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#ifndef __INCLUDE_GUARD__ANTENNA_DEPLOY_STARTUP_H__
#define __INCLUDE_GUARD__ANTENNA_DEPLOY_STARTUP_H__
#include <stdint.h>
uint8_t START_antenna_deploy();
int16_t START_antenna_deploy();


#endif // __INCLUDE_GUARD__ANTENNA_DEPLOY_STARTUP_H__
177 changes: 123 additions & 54 deletions firmware/Core/Src/startup_sequence/antenna_deploy_startup.c
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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,
Expand All @@ -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) {
Expand All @@ -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,
Expand All @@ -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,
Expand All @@ -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;
}

0 comments on commit 759f685

Please sign in to comment.