Skip to content

Commit

Permalink
lib/csp: Add CRC32 option to FILE INFO command
Browse files Browse the repository at this point in the history
This commit adds an option to the File info command to calculate the
CRC32 value. Since CRC32 calculation may take a long time for larger
files, this update enables users to toggle the option on or off when
executing the command.

Signed-off-by: Takuya Sasaki <takuya.sasaki@spacecubics.com>
  • Loading branch information
sasataku committed Nov 9, 2024
1 parent f780b2b commit c5d2d28
Show file tree
Hide file tree
Showing 5 changed files with 69 additions and 4 deletions.
1 change: 1 addition & 0 deletions adcs/prj.conf
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ CONFIG_SPI_EXTENDED_MODES=y
CONFIG_PWM=y
CONFIG_LOG=y
CONFIG_MAIN_STACK_SIZE=4096
CONFIG_CRC=y
# For CSP
CONFIG_LIBCSP=y
CONFIG_CSP_USE_RTABLE=n
Expand Down
12 changes: 12 additions & 0 deletions lib/csp/Kconfig
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,18 @@ config SC_LIB_CSP_MAX_FILE_WORK
help
MAX number of file operation work.

config SC_LIB_CSP_CRC_CHUNK_SIZE
int "Chunk size for CRC32 calculation"
default 256
help
Chunk size for CRC 32 calculation.

config SC_LIB_CSP_CRC_CALC_SLEEP_MSEC
int "Sleep time for CRC32 calculation (msec)"
default 20
help
Sleep time for CRC32 calculation (msec).

module = SC_LIB_CSP
module-str = Space Cubics CSP libraries
source "subsys/logging/Kconfig.template.log_config"
Expand Down
58 changes: 54 additions & 4 deletions lib/csp/file.c
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
*/

#include <zephyr/sys/byteorder.h>
#include <zephyr/sys/crc.h>
#include <csp/csp.h>
#include "file.h"
#include "sc_csp.h"
Expand All @@ -25,15 +26,16 @@ struct file_work file_works[CONFIG_SC_LIB_CSP_MAX_FILE_WORK];

/* Command size */
#define FILE_CMD_MIN_SIZE (1U)
#define FILE_INFO_CMD_SIZE (1U) /* without file name length */
#define FILE_INFO_CMD_SIZE (2U) /* without file name length */
#define FILE_REMOVE_CMD_SIZE (1U) /* without file name length */

/* Command ID */
#define FILE_INFO_CMD (0U)
#define FILE_REMOVE_CMD (1U)

/* Command argument offset */
#define FILE_FNAME_OFFSET (1U)
#define FILE_CRC_OFFSET (1U)
#define FILE_FNAME_OFFSET (2U)

static struct file_work *csp_get_file_work(void)
{
Expand All @@ -56,15 +58,56 @@ static int csp_get_file_info(const char *fname, struct fs_dirent *entry)
ret = fs_stat(fname, entry);
if (ret < 0) {
LOG_ERR("Faild to get the file info %s (%d)", fname, ret);
goto end;
}

LOG_INF("File Info: (entry %d) (size: %u) (name: %s)", entry->type, entry->size,
entry->name);

end:
return ret;
}

static void csp_send_file_info_reply(struct fs_dirent *entry, csp_packet_t *packet,
static int csp_calc_crc32(const char *fname, size_t size, uint32_t *crc32)
{
int ret;
struct fs_file_t file;
uint8_t chunk[CONFIG_SC_LIB_CSP_CRC_CHUNK_SIZE];
ssize_t remainig_size = size;
ssize_t read_size;

fs_file_t_init(&file);

ret = fs_open(&file, fname, FS_O_READ);
if (ret < 0) {
LOG_ERR("Faild to open the file %s (%d)", fname, ret);
goto end;
}

*crc32 = 0;

while (remainig_size) {
read_size = fs_read(&file, chunk, sizeof(chunk));
if (read_size < 0) {
LOG_ERR("Failed to read file %s", fname);
break;
}

*crc32 = crc32_ieee_update(*crc32, chunk, read_size);
remainig_size -= read_size;

k_sleep(K_MSEC(CONFIG_SC_LIB_CSP_CRC_CALC_SLEEP_MSEC));
}

fs_close(&file);

LOG_INF("CRC32: 0x%08x", *crc32);

end:
return ret;
}

static void csp_send_file_info_reply(struct fs_dirent *entry, uint32_t crc32, csp_packet_t *packet,
uint8_t command_id, int err_code)
{
struct file_info_telemetry tlm;
Expand All @@ -80,6 +123,7 @@ static void csp_send_file_info_reply(struct fs_dirent *entry, csp_packet_t *pack

tlm.telemetry_id = command_id;
tlm.error_code = sys_cpu_to_le32(err_code);
tlm.crc32 = sys_cpu_to_le32(crc32);

memcpy(packet->data, &tlm, sizeof(tlm));
packet->length = sizeof(tlm);
Expand All @@ -90,24 +134,30 @@ static void csp_send_file_info_reply(struct fs_dirent *entry, csp_packet_t *pack
static int csp_file_info_cmd(uint8_t command_id, csp_packet_t *packet)
{
int ret = 0;
uint8_t crc_opt;
char fname[CONFIG_SC_LIB_CSP_FILE_NAME_MAX_LEN];
struct fs_dirent entry;
uint32_t crc32 = 0;

if (packet->length != FILE_INFO_CMD_SIZE + CONFIG_SC_LIB_CSP_FILE_NAME_MAX_LEN) {
LOG_ERR("Invalide command size: %d", packet->length);
ret = -EINVAL;
goto end;
}

crc_opt = packet->data[FILE_CRC_OFFSET];
strncpy(fname, &packet->data[FILE_FNAME_OFFSET], CONFIG_SC_LIB_CSP_FILE_NAME_MAX_LEN);
fname[CONFIG_SC_LIB_CSP_FILE_NAME_MAX_LEN - 1] = '\0';

LOG_INF("File info command (fname: %s)", fname);

ret = csp_get_file_info(fname, &entry);
if (ret == 0 && crc_opt) {
(void)csp_calc_crc32(fname, entry.size, &crc32);
}

end:
csp_send_file_info_reply(&entry, packet, command_id, ret);
csp_send_file_info_reply(&entry, crc32, packet, command_id, ret);
return ret;
}

Expand Down
1 change: 1 addition & 0 deletions lib/csp/file.h
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ struct file_info_telemetry {
uint32_t error_code;
uint8_t entry_type;
uint32_t file_size;
uint32_t crc32;
char file_name[CONFIG_SC_LIB_CSP_FILE_NAME_MAX_LEN];
} __attribute__((__packed__));

Expand Down
1 change: 1 addition & 0 deletions main/prj.conf
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ CONFIG_PWM=y
CONFIG_LOG=y
CONFIG_MAIN_STACK_SIZE=4096
CONFIG_PICOLIBC_IO_FLOAT=y
CONFIG_CRC=y
# For CSP
CONFIG_LIBCSP=y
CONFIG_CSP_USE_RTABLE=y
Expand Down

0 comments on commit c5d2d28

Please sign in to comment.