Skip to content

Commit

Permalink
feat: Added file publish dump
Browse files Browse the repository at this point in the history
  • Loading branch information
Nathan Hui committed Sep 12, 2023
1 parent 6344465 commit 263381a
Show file tree
Hide file tree
Showing 3 changed files with 99 additions and 10 deletions.
96 changes: 96 additions & 0 deletions src/fileCLI/fileCLI.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,10 @@
#include "cli/conio.hpp"
#include "cli/flog.hpp"
#include "consts.hpp"
#include "cellular/encoding/base64.h"
#include "cellular/encoding/base85.h"
#include "product.hpp"
#include "system.hpp"

#include "Particle.h"

Expand All @@ -18,6 +22,7 @@ static char path_buffer[PATH_MAX];
FileCLI::menu_t FileCLI::fsExplorerMenu[] =
{
{'c', &FileCLI::change_dir},
{'d', &FileCLI::dumpBase85},
{'h', &FileCLI::dumpHex},
{'l', &FileCLI::list_dir},
{'p', &FileCLI::print_dir},
Expand Down Expand Up @@ -48,6 +53,7 @@ void FileCLI::execute(void)
{
FLOG_AddError(FLOG_DEBUG, 5);
SF_OSAL_printf(":>");
memset(input_buffer, 0, FILE_CLI_INPUT_BUFFER_LEN);
getline(input_buffer, FILE_CLI_INPUT_BUFFER_LEN);
cmd = findCommand(input_buffer);
if (!cmd)
Expand Down Expand Up @@ -375,3 +381,93 @@ void FileCLI::dumpHex(void)

close(fp);
}

void base85dump(int fp, size_t file_len)
{
size_t file_idx = 0, bytes_read;
uint8_t byte_buffer[512];
char encoded_buffer[1024];
size_t totalEncodedLen = 0;
size_t n_packets = 0;
size_t encodedLen = 0;
for(file_idx = 0; file_idx < file_len; file_idx += bytes_read)
{
bytes_read = read(fp, byte_buffer, SF_BLOCK_SIZE);
#if SF_UPLOAD_ENCODING == SF_UPLOAD_BASE85
encodedLen = bintob85(encoded_buffer, byte_buffer, bytes_read) - encodedBuffer;
#elif SF_UPLOAD_ENCODING == SF_UPLOAD_BASE64
encodedLen = 1024;
b64_encode(byte_buffer, bytes_read, encoded_buffer, &encodedLen);
#elif SF_UPLOAD_ENCODING == SF_UPLOAD_BASE64URL
encodedLen = 1024;
urlsafe_b64_encode(byte_buffer, bytes_read, encoded_buffer, &encodedLen);
#endif
totalEncodedLen += encodedLen;
SF_OSAL_printf("%s" __NL__, encoded_buffer);
n_packets++;
}

SF_OSAL_printf(__NL__ "%d chars of encoded data" __NL__, totalEncodedLen);
SF_OSAL_printf("%d packets" __NL__, n_packets);
}

void FileCLI::dumpBase85(void)
{
char input_buffer[FILE_CLI_INPUT_BUFFER_LEN];
DIR* cwd = this->dir_stack[this->current_dir];
struct dirent* dirent;
long idx;
int cmd_val;
const char* path;
int fp;
struct stat fstats;

idx = telldir(cwd);
while ((dirent = readdir(cwd)))
{
if (dirent->d_type != DT_REG)
{
idx = telldir(cwd);
continue;
}
strncpy(this->path_stack[this->current_dir], dirent->d_name, NAME_MAX);
SF_OSAL_printf("%d: %-16s" __NL__,
idx,
dirent->d_name);
idx = telldir(cwd);
}
rewinddir(cwd);
memset(this->path_stack[this->current_dir], 0, NAME_MAX);

SF_OSAL_printf("Enter the number of the file to dump: ");
getline(input_buffer, FILE_CLI_INPUT_BUFFER_LEN);
cmd_val = atoi(input_buffer);

seekdir(cwd, cmd_val);
dirent = readdir(cwd);
rewinddir(cwd);
strncpy(this->path_stack[this->current_dir], dirent->d_name, NAME_MAX);
this->current_dir++;
path = buildPath(false);

fp = open(path, O_RDONLY);
if(-1 == fp)
{
SF_OSAL_printf("Unable to open %s: %s" __NL__, path, strerror(errno));
return;
}

if(fstat(fp, &fstats))
{
SF_OSAL_printf("Unable to stat file: %s" __NL__, strerror(errno));
close(fp);
return;
}

SF_OSAL_printf("Publish Header: %s-%s" __NL__,
pSystemDesc->deviceID,
dirent->d_name);
base85dump(fp, fstats.st_size);

close(fp);
}
12 changes: 2 additions & 10 deletions src/fileCLI/fileCLI.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -48,21 +48,13 @@ class FileCLI{
*
*/
void dumpBase85(void);
/**
* @brief Dumps the current file as a comma separate sequence of byte values
*
*/
void dumpDecimal(void);

/**
* @brief Dumps the current file as a hexdump
*
*/
void dumpHex(void);
/**
* @brief Dumps the current file to console as ASCII
*
*/
void dumpAscii(void);

/**
* @brief Removes the current file from the filesystem
*
Expand Down
1 change: 1 addition & 0 deletions src/product.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -214,4 +214,5 @@

#define SF_CLI_MAX_CMD_LEN 100

#define SF_BLOCK_SIZE 496
#endif

0 comments on commit 263381a

Please sign in to comment.