Skip to content

Commit

Permalink
lib/csp,main,adcs: Add file handle work queue
Browse files Browse the repository at this point in the history
Commands related to file operations are expected to take time to
process, so a dedicated work queue will be prepared to handle
these operations.

Signed-off-by: Takuya Sasaki <takuya.sasaki@spacecubics.com>
  • Loading branch information
sasataku committed Nov 9, 2024
1 parent 83a92d3 commit f780b2b
Show file tree
Hide file tree
Showing 5 changed files with 90 additions and 3 deletions.
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;
}
18 changes: 18 additions & 0 deletions lib/csp/Kconfig
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,24 @@ 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.

module = SC_LIB_CSP
module-str = Space Cubics CSP libraries
source "subsys/logging/Kconfig.template.log_config"
Expand Down
70 changes: 67 additions & 3 deletions lib/csp/file.c
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,16 @@
#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 */
Expand All @@ -25,6 +35,20 @@ LOG_MODULE_REGISTER(file, CONFIG_SC_LIB_CSP_LOG_LEVEL);
/* Command argument offset */
#define FILE_FNAME_OFFSET (1U)

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)
{
int ret;
Expand Down Expand Up @@ -110,19 +134,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 +169,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);
}
}
1 change: 1 addition & 0 deletions lib/csp/file.h
Original file line number Diff line number Diff line change
Expand Up @@ -17,4 +17,5 @@ struct file_info_telemetry {
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);
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;
}

0 comments on commit f780b2b

Please sign in to comment.