Skip to content

Commit

Permalink
Merge pull request #64 from AlbertaSat/logger_service_work
Browse files Browse the repository at this point in the history
Logger service work
  • Loading branch information
wagnerd97 authored Aug 25, 2021
2 parents 6d68f32 + cc3d5e2 commit 44566c2
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 17 deletions.
2 changes: 1 addition & 1 deletion Services/include/logger/logger_service.h
Original file line number Diff line number Diff line change
Expand Up @@ -33,4 +33,4 @@ typedef enum {

SAT_returnState start_logger_service(void);

#endif LOGGERSERVICE_H
#endif /* LOGGERSERVICE_H */
4 changes: 2 additions & 2 deletions Services/source/housekeeping/housekeeping_service.c
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ char fileName[] = "VOL0:/tempHKdata.TMP";
uint16_t current_file = 1; //Increments after file write. loops back at MAX_FILES
//1 indexed
char hk_config[] = "VOL0:/HKconfig.TMP";
uint8_t config_loaded = 0; //set to 1 after config is loaded
static uint8_t config_loaded = 0; //set to 1 after config is loaded

uint32_t *timestamps = 0; //This is a dynamic array to handle file search by timestamp
uint16_t hk_timestamp_array_size = 0; //NOT BYTES. stored as number of items. 1 indexed. 0 element unused
Expand Down Expand Up @@ -287,7 +287,7 @@ Result collect_hk_from_devices(All_systems_housekeeping* all_hk_data) {
* @return Found_file
* FILE_EXISTS or FILE_NOT_EXIST
*/
Found_file exists(const char *filename){
static Found_file exists(const char *filename){
int32_t file;
red_errno = 0;
file = red_open(filename, RED_O_CREAT | RED_O_EXCL | RED_O_RDWR); //attempt to create file
Expand Down
66 changes: 52 additions & 14 deletions Services/source/logger/logger_service.c
Original file line number Diff line number Diff line change
Expand Up @@ -20,16 +20,13 @@

#include "logger/logger_service.h"
#include "services.h"
#include "logger/logger.h"
#include <redposix.h>

#include <csp/csp.h>
#include "util/service_utilities.h" //for setting csp packet length

const char log_file[] = "VOL0:/syslog.log"; //replace with getter in logger.c
const char old_log_file[] = "VOL0:/syslog_old.log"; //replace with getter
uint32_t max_file_size = 500; //repalce with getter

uint32_t max_string_length = 500;
uint32_t max_string_length = 500; //limits based on csp packet size


/* @brief
Expand All @@ -52,54 +49,95 @@ int file_exists(const char *filename){
return 1;
}

SAT_returnState get_file(const char *filename, csp_packet_t *packet) {
/**
* @brief
* generic file to open and read any file that would contain text data.
* primarily designed to handle either of the 2 log files that may be read
*
* @param filename
* the name of the file to be opened and read
* @param packet
the packet that holds the service subtype and will be filled with the log data
* @return SAT_returnState
state to define success of the operation
*/
SAT_returnState get_file(char *filename, csp_packet_t *packet) {
uint32_t max_file_size;
int8_t status;
int32_t file;
uint32_t data_size;
char* log_data[500] = {0};
if (file_exists(filename) == 0) {
char* log_data;
get_logger_file_size(&max_file_size);
log_data = (char *)pvPortMalloc(max_file_size);
if (log_data == NULL) {
status = -2;
} else if (file_exists(filename) == 0) {
file = red_open(filename, RED_O_RDONLY);
if(file > -1){
data_size = red_read(file, log_data, max_file_size);
if (data_size == 0){
status = -1;
strncpy(log_data, "Log file is empty\n", max_string_length);
data_size = sprintf(log_data, "Log file %s is empty\n", filename);
} else {
status = 0;
}
} else {
status = -1;
sprintf(log_data, "Can't open log file. red_errno: %d\n", red_errno);
data_size = sprintf(log_data, "Can't open log file. red_errno: %d\n", red_errno);
}
} else {
status = -1;
strncpy(log_data, "Log file does not exist\n", max_string_length);
data_size = sprintf(log_data, "Log file %s does not exist\n", filename);
}
for (uint32_t i = data_size; i < max_file_size; i++) {
log_data[i] = '\0';
}
memcpy(&packet->data[STATUS_BYTE], &status, 1);
memcpy(&packet->data[OUT_DATA_BYTE], log_data, max_string_length);
set_packet_length(packet, max_string_length + 2);
vPortFree(log_data);
return SATR_OK;
}


/**
* @brief
* logger service app to perform operations based on the given service subtype
* @param packet
the packet that holds the service subtype and will be filled with the log data
* @return SAT_returnState
state to define success of the operation
*/
SAT_returnState logger_service_app(csp_packet_t *packet) {
uint8_t ser_subtype = (uint8_t)packet->data[SUBSERVICE_BYTE];
int8_t status;
uint32_t * data32;
int32_t file_size;
char *log_file;

switch (ser_subtype) {
case SET_FILE_SIZE:
status = 0;
//pull param from packet
data32 = (uint32_t *)(packet->data + 1);
file_size = data32[0];
status = set_logger_file_size(file_size);
memcpy(&packet->data[STATUS_BYTE], &status, sizeof(int8_t));
set_packet_length(packet, sizeof(int8_t) + 1); // +1 for subservice
break;
case GET_FILE_SIZE:
status = get_logger_file_size(&file_size);
memcpy(&packet->data[STATUS_BYTE], &status, sizeof(int8_t));
memcpy(&packet->data[OUT_DATA_BYTE], &file_size, sizeof(file_size));
set_packet_length(packet, sizeof(int8_t) + sizeof(file_size) + 1);
break;
case GET_FILE:
log_file = get_logger_file();
get_file(log_file, packet);
break;
case GET_OLD_FILE:
get_file(old_log_file, packet);

log_file = get_logger_old_file();
get_file(log_file, packet);
break;
default:
ex2_log("No such subservice\n");
Expand Down

0 comments on commit 44566c2

Please sign in to comment.