From 1cdb02b45fd2afcc98442e4991f08b0c2627c27a Mon Sep 17 00:00:00 2001 From: Nathan Hui Date: Fri, 29 Sep 2023 11:57:59 -0700 Subject: [PATCH] feat: Adds setSessionTime --- src/cellular/recorder.cpp | 51 ++++++++++++++++++++++++++++-------- src/cellular/recorder.hpp | 12 +++++---- src/debug/recorder_debug.cpp | 18 +++++++++---- 3 files changed, 60 insertions(+), 21 deletions(-) diff --git a/src/cellular/recorder.cpp b/src/cellular/recorder.cpp index 88c12b09..41ae8c69 100644 --- a/src/cellular/recorder.cpp +++ b/src/cellular/recorder.cpp @@ -30,7 +30,7 @@ #define DATA_ROOT "/data" #define METADATA_FILE DATA_ROOT "/.metadata" static int REC_create_data_root(void); -static void REC_create_data_filename(int data_index, +static void REC_create_data_filename(int session_idx, char* p_filename, size_t filename_len); @@ -89,11 +89,11 @@ int Recorder::create_metadata_file(void) return 1; } -void REC_create_data_filename(uint32_t data_index, +void REC_create_data_filename(uint32_t session_idx, char* p_filename, size_t filename_len) { - snprintf(p_filename, filename_len, DATA_ROOT "/%010u.bin", data_index); + snprintf(p_filename, filename_len, DATA_ROOT "/%010u.bin", session_idx); } @@ -298,20 +298,47 @@ int Recorder::popLastPacket(size_t len) return 1; } -void Recorder::setSessionName(const char* const sessionName) +void Recorder::setSessionTime(uint32_t session_time) { - // memset(this->currentSessionName, 0, REC_SESSION_NAME_MAX_LEN + 1); - // strncpy(this->currentSessionName, sessionName, REC_SESSION_NAME_MAX_LEN); - // SF_OSAL_printf("Setting session name to %s\n", this->currentSessionName); + int fp; + timestamp_entry_t entry; + + fp = ::open(METADATA_FILE, O_RDWR); + if (-1 == fp) + { + FLOG_AddError(FLOG_FS_OPEN_FAIL, errno); + return; + } + ::lseek(fp, sizeof(metadata_header_t), SEEK_SET); + + for (int entry_idx = 0; entry_idx < this->metadata_header.n_entries; entry_idx++) + { + ::read(fp, &entry, sizeof(timestamp_entry_t)); + if (entry.session_idx == this->current_session_index) + { + break; + } + } + + if (entry.session_idx != this->current_session_index) + { + return; + } + entry.timestamp = session_time; + ::lseek(fp, -1 * sizeof(timestamp_entry_t), SEEK_CUR); + ::write(fp, &entry, sizeof(timestamp_entry_t)); + ::close(fp); } int Recorder::openSession() { - uint32_t data_index = this->metadata_header.next_data_index; int fp; - timestamp_entry_t entry = {data_index, 0}; + timestamp_entry_t entry; - this->metadata_header.next_data_index++; + this->current_session_index = this->metadata_header.next_session_index; + entry.session_idx = this->current_session_index; + entry.timestamp = 0; + this->metadata_header.next_session_index++; this->metadata_header.n_entries++; fp = ::open(METADATA_FILE, O_WRONLY); if (-1 == fp) @@ -324,7 +351,9 @@ int Recorder::openSession() ::write(fp, &entry, sizeof(timestamp_entry_t)); ::close(fp); - REC_create_data_filename(data_index, this->filename_buffer, REC_SESSION_NAME_MAX_LEN); + REC_create_data_filename(this->current_session_index, + this->filename_buffer, + REC_SESSION_NAME_MAX_LEN); this->pSession = &Deployment::getInstance(); if (!this->pSession->open(this->filename_buffer, Deployment::WRITE)) diff --git a/src/cellular/recorder.hpp b/src/cellular/recorder.hpp index 8f85d833..0e2caa34 100644 --- a/src/cellular/recorder.hpp +++ b/src/cellular/recorder.hpp @@ -6,6 +6,7 @@ #include "product.hpp" #include +#include #include /** @@ -60,11 +61,11 @@ class Recorder */ int popLastPacket(size_t len); /** - * @brief Set the current session name + * @brief Set the current session start time * - * @param sessionName Current name to set + * @param session_time Current time to set */ - void setSessionName(const char* const); + void setSessionTime(uint32_t); /** * @brief Gets number of files in filesystem * @@ -103,13 +104,13 @@ class Recorder private: typedef struct timestamp_entry_ { - uint32_t data_index; + uint32_t session_idx; uint32_t timestamp; }timestamp_entry_t; typedef struct metadata_header_ { - uint32_t next_data_index; + uint32_t next_session_index; uint32_t n_entries; }metadata_header_t; @@ -118,6 +119,7 @@ class Recorder uint8_t dataBuffer[REC_MEMORY_BUFFER_SIZE]; uint32_t dataIdx; Deployment* pSession; + uint32_t current_session_index; metadata_header_t metadata_header; bool metadata_header_valid; diff --git a/src/debug/recorder_debug.cpp b/src/debug/recorder_debug.cpp index 0602a3fd..a0b05024 100644 --- a/src/debug/recorder_debug.cpp +++ b/src/debug/recorder_debug.cpp @@ -27,6 +27,7 @@ void REC_testGetLastPacket(void); void REC_testOpen(void); void REC_testClose(void); void REC_testPutBytes(void); +void REC_testSetTime(void); const Menu_t Recorder_debug_menu[] = { @@ -35,6 +36,7 @@ const Menu_t Recorder_debug_menu[] = {3, "Open Session", &REC_testOpen, MENU_CMD}, {4, "Close Session", &REC_testClose, MENU_CMD}, {5, "Put Bytes", &REC_testPutBytes, MENU_CMD}, + {6, "Set Session Time", &REC_testSetTime, MENU_CMD}, // {3, "Get Last Packet", &REC_testGetLastPacket, MENU_CMD}, {0, nullptr, nullptr, MENU_NULL} }; @@ -114,17 +116,23 @@ void REC_testPutBytes(void) for (hex_idx = 0; hex_idx < input_length; hex_idx += 2) { memcpy(hex_buffer, user_input + hex_idx, 2); - hexDump(hex_buffer, 3); user_input[byte_idx] = (uint8_t) strtoul(hex_buffer, nullptr, 16); - hexDump(user_input, input_length); byte_idx++; } - SF_OSAL_printf("Byte IDX: %lu" __NL__, byte_idx); - hexDump(user_input, input_length); - if (1 != pRecorder->putBytes(user_input, byte_idx)) { SF_OSAL_printf("putBytes failed!" __NL__); } } + +void REC_testSetTime(void) +{ + char user_input[REC_MEMORY_BUFFER_SIZE]; + Recorder* pRecorder = pSystemDesc->pRecorder; + + SF_OSAL_printf("Enter start time: "); + getline(user_input, REC_MEMORY_BUFFER_SIZE); + + pRecorder->setSessionTime(atoi(user_input)); +} \ No newline at end of file