Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add crc calculation option to file info cmd #93

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
2 changes: 2 additions & 0 deletions adcs/src/csp.c
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
#include <zephyr/device.h>
#include "sc_csp.h"
#include "flash.h"
#include "file.h"
#include "cmd/handler.h"

#include <zephyr/logging/log.h>
Expand Down Expand Up @@ -87,6 +88,7 @@ int sc_csp_init(void)
server_start();

csp_flash_handler_init();
csp_file_handler_init();
end:
return ret;
}
30 changes: 30 additions & 0 deletions lib/csp/Kconfig
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,36 @@ config SC_LIB_CSP_READ_TIMEOUT_MSEC
help
Timeout for CSP read (msec).

config SC_LIB_CSP_FILE_THREAD_PRIORITY
int "File operation thread priority"
default 3
help
File operation thread priority

config SC_LIB_CSP_FILE_THREAD_STACK_SIZE
int "File operation thread stack size"
default 4096
help
File operation thread stack size.

config SC_LIB_CSP_MAX_FILE_WORK
int "MAX number of file operation work"
default 10
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
128 changes: 121 additions & 7 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 @@ -13,17 +14,42 @@
#include <zephyr/logging/log.h>
LOG_MODULE_REGISTER(file, CONFIG_SC_LIB_CSP_LOG_LEVEL);

K_THREAD_STACK_DEFINE(file_workq_stack, CONFIG_SC_LIB_CSP_FILE_THREAD_STACK_SIZE);

struct file_work {
struct k_work work;
csp_packet_t *packet;
};

struct k_work_q file_workq;
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)
{
struct file_work *file_work;

for (int i = 0; i < CONFIG_SC_LIB_CSP_MAX_FILE_WORK; i++) {
file_work = &file_works[i];
if (!k_work_is_pending(&file_work->work)) {
return file_work;
}
}

return NULL;
}

static int csp_get_file_info(const char *fname, struct fs_dirent *entry)
{
Expand All @@ -32,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 @@ -56,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 @@ -66,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 All @@ -110,19 +184,26 @@ static int csp_file_remove_cmd(uint8_t command_id, csp_packet_t *packet)
return ret;
}

int csp_file_handler(csp_packet_t *packet)
static void csp_file_work(struct k_work *work)
{
int ret;
int ret = 0;
uint8_t command_id;
struct file_work *file_work;
csp_packet_t *packet;

file_work = CONTAINER_OF(work, struct file_work, work);
packet = file_work->packet;

if (packet == NULL) {
ret = -EINVAL;
command_id = CSP_UNKNOWN_CMD_CODE;
goto end;
}

if (packet->length < FILE_CMD_MIN_SIZE) {
LOG_ERR("Invalide command size: %d", packet->length);
ret = -EINVAL;
command_id = CSP_UNKNOWN_CMD_CODE;
goto free;
}

Expand All @@ -138,12 +219,45 @@ int csp_file_handler(csp_packet_t *packet)
default:
LOG_ERR("Unkown command code: %d", command_id);
ret = -EINVAL;
command_id = CSP_UNKNOWN_CMD_CODE;
break;
}

free:
csp_buffer_free(packet);
if (ret < 0) {
csp_send_std_reply(packet, command_id, ret);
}

end:
return;
}

int csp_file_handler(csp_packet_t *packet)
{
int ret = 0;
struct file_work *file_work;

file_work = csp_get_file_work();
if (file_work == NULL) {
LOG_ERR("File operation work queue is busy");
ret = -EBUSY;
goto end;
}

file_work->packet = packet;
k_work_submit_to_queue(&file_workq, &file_work->work);

end:
return ret;
}

void csp_file_handler_init(void)
{
k_work_queue_start(&file_workq, file_workq_stack, K_THREAD_STACK_SIZEOF(file_workq_stack),
CONFIG_SC_LIB_CSP_FILE_THREAD_PRIORITY, NULL);
k_thread_name_set(&file_workq.thread, "file_workq");

for (int i = 0; i < CONFIG_SC_LIB_CSP_MAX_FILE_WORK; i++) {
k_work_init(&file_works[i].work, csp_file_work);
}
}
2 changes: 2 additions & 0 deletions lib/csp/file.h
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,9 @@ 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__));

void csp_file_handler_init(void);
int csp_file_handler(csp_packet_t *packet);
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
2 changes: 2 additions & 0 deletions main/src/csp.c
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
#include <zephyr/device.h>
#include "sc_csp.h"
#include "flash.h"
#include "file.h"
#include "cmd/handler.h"

#include <zephyr/logging/log.h>
Expand Down Expand Up @@ -110,6 +111,7 @@ int sc_csp_init(void)
server_start();

csp_flash_handler_init();
csp_file_handler_init();
end:
return ret;
}