Add background jobs.

Signed-off-by: Pol Henarejos <pol.henarejos@cttc.es>
This commit is contained in:
Pol Henarejos
2026-04-28 00:40:05 +02:00
parent 3fa5204949
commit 0abea5b6b2
3 changed files with 50 additions and 10 deletions

View File

@@ -30,6 +30,8 @@
#define REST_MAX_SESSIONS 4
rest_background_job_t background_jobs[REST_MAX_BACKGROUND_JOBS] = {0};
static rest_session_t rest_sessions[REST_MAX_SESSIONS] = {0};
static int x25519_hkdf_derive_key32(const uint8_t sk[32], const uint8_t pk[32], const uint8_t *salt, size_t salt_len, const uint8_t *info, size_t info_len, uint8_t out_key[32]);
@@ -362,3 +364,27 @@ int rest_session_derive_shared(const rest_session_t *session, uint8_t derived_ke
}
return PICOKEYS_OK;
}
int rest_background_job_push(rest_route_handler_t handler) {
if (handler == NULL) {
return -1;
}
for (int i = 0; i < REST_MAX_BACKGROUND_JOBS; i++) {
if (background_jobs[i].handler == NULL) {
background_jobs[i].handler = handler;
return 0;
}
}
return -1;
}
rest_route_handler_t rest_background_job_pop(void) {
for (int i = 0; i < REST_MAX_BACKGROUND_JOBS; i++) {
if (background_jobs[i].handler != NULL) {
rest_route_handler_t handler = background_jobs[i].handler;
background_jobs[i].handler = NULL;
return handler;
}
}
return NULL;
}

View File

@@ -154,6 +154,15 @@ typedef struct {
rest_session_role_t role; // Minimum required role to access this route (only relevant if REST_ROUTE_REQUIRE_AUTH flag is set)
} rest_route_t;
typedef struct {
rest_route_handler_t handler;
} rest_background_job_t;
#define REST_MAX_BACKGROUND_JOBS 4
extern int rest_background_job_push(rest_route_handler_t handler);
extern rest_route_handler_t rest_background_job_pop(void);
extern int rest_execute_route_handler(const rest_request_t *request, rest_route_handler_t handler, rest_response_t *response);
extern int rest_response_set_error(rest_response_t *response, int status_code, const char *message);
const char *rest_status_text_from_code(uint16_t code);

View File

@@ -92,8 +92,8 @@ static void *rest_core1_thread(void *arg);
static void send_response(rest_conn_t *conn, int status_code, const char *status_text, const char *content_type, const char *body, size_t body_len, char *headers[REST_HEADER_TOTAL_COUNT]);
void rest_close_conn(rest_conn_t *conn);
static int rest_start_core1_job(rest_conn_t *conn, const rest_request_t *request, rest_route_handler_t handler) {
if (request == NULL || handler == NULL || rest_core1_job.pending) {
static int rest_start_core1_job(rest_conn_t *conn, rest_route_handler_t handler) {
if (handler == NULL || rest_core1_job.pending) {
return -1;
}
@@ -154,20 +154,25 @@ static void send_json_error(rest_conn_t *conn, int status_code, const char *erro
}
void rest_task(void) {
int status;
rest_conn_t *conn;
if (!rest_core1_job.pending) {
rest_route_handler_t handler = rest_background_job_pop();
if (handler != NULL) {
if (rest_start_core1_job(NULL, handler) != 0) {
// Failed to start background job, push it back to the queue
rest_background_job_push(handler);
}
}
return;
}
status = card_status(ITF_LWIP);
int status = card_status(ITF_LWIP);
if (status != PICOKEYS_OK) {
return;
}
conn = rest_core1_job.conn;
if (conn == NULL) {
return;
}
rest_conn_t *conn = rest_core1_job.conn;
//if (conn == NULL) {
// return;
//}
rest_response_t *response = &rest_core1_result.response;
if (response == NULL) {
return;
@@ -801,7 +806,7 @@ void rest_handle_request(rest_conn_t *conn) {
}
request->session = session;
}
if (rest_start_core1_job(conn, request, routes[i].handler) != 0) {
if (rest_start_core1_job(conn, routes[i].handler) != 0) {
send_json_error(conn, 500, "internal_error");
}
return;