Skip to content

Commit

Permalink
feat: Adds setSessionTime
Browse files Browse the repository at this point in the history
  • Loading branch information
Nathan Hui committed Sep 29, 2023
1 parent e3cc6a5 commit 1cdb02b
Show file tree
Hide file tree
Showing 3 changed files with 60 additions and 21 deletions.
51 changes: 40 additions & 11 deletions src/cellular/recorder.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand Down Expand Up @@ -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);
}


Expand Down Expand Up @@ -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)
Expand All @@ -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))
Expand Down
12 changes: 7 additions & 5 deletions src/cellular/recorder.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
#include "product.hpp"

#include <stddef.h>
#include <stdint.h>
#include <limits.h>

/**
Expand Down Expand Up @@ -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
*
Expand Down Expand Up @@ -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;

Expand All @@ -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;

Expand Down
18 changes: 13 additions & 5 deletions src/debug/recorder_debug.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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[] =
{
Expand All @@ -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}
};
Expand Down Expand Up @@ -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));
}

0 comments on commit 1cdb02b

Please sign in to comment.