Skip to content

Commit c348a95

Browse files
committed
refactor(fl): replace int return codes with fl_error_t enum
Extract fl_error_t into standalone fl_error.h to avoid header coupling between fl.h and fl_stream.h. - FL_OK(0), FL_ERR_ARGS(-1), FL_ERR_CRC(-2), FL_ERR_PARSE(-3), FL_ERR_UNKNOWN(-4) - cmd_handler_t, verify_args_crc, verify_patch_crc, fl_exec_cmd, fl_stream_exec_line all return fl_error_t - fl_port_nuttx.c uses fl_error_t for fl_exec_cmd return value - All test assertions updated: int result -> fl_error_t result, 0 -> FL_OK, != 0 -> != FL_OK, -1 -> FL_ERR_ARGS - Single-line if bodies wrapped with braces
1 parent fd7fd51 commit c348a95

File tree

8 files changed

+186
-132
lines changed

8 files changed

+186
-132
lines changed

App/func_loader/fl.c

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1240,9 +1240,10 @@ static const cmd_entry_t s_cmd_table[] = {
12401240

12411241
#define CMD_TABLE_SIZE (sizeof(s_cmd_table) / sizeof(s_cmd_table[0]))
12421242

1243-
int fl_exec_cmd(fl_context_t* ctx, int argc, const char** argv) {
1244-
if (argc == 0)
1245-
return -1;
1243+
fl_error_t fl_exec_cmd(fl_context_t* ctx, int argc, const char** argv) {
1244+
if (argc == 0) {
1245+
return FL_ERR_ARGS;
1246+
}
12461247

12471248
cmd_args_t args = {0};
12481249
args.crc = -1;
@@ -1273,7 +1274,7 @@ int fl_exec_cmd(fl_context_t* ctx, int argc, const char** argv) {
12731274
fl_argparse_init(&ap, opts, NULL, 0);
12741275
if (fl_argparse_parse(&ap, argc, argv) > 0) {
12751276
fl_response(false, "Invalid arguments");
1276-
return -1;
1277+
return FL_ERR_PARSE;
12771278
}
12781279

12791280
if (!args.cmd) {
@@ -1282,7 +1283,7 @@ int fl_exec_cmd(fl_context_t* ctx, int argc, const char** argv) {
12821283
fl_println(" %s", s_cmd_table[i].name);
12831284
}
12841285
fl_response(false, "Missing --cmd");
1285-
return -1;
1286+
return FL_ERR_ARGS;
12861287
}
12871288

12881289
/* Lookup command in dispatch table */
@@ -1293,5 +1294,5 @@ int fl_exec_cmd(fl_context_t* ctx, int argc, const char** argv) {
12931294
}
12941295

12951296
fl_response(false, "Unknown: %s", args.cmd);
1296-
return -1;
1297+
return FL_ERR_UNKNOWN;
12971298
}

App/func_loader/fl.h

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ extern "C" {
3636
#include <stdbool.h>
3737
#include <stddef.h>
3838
#include <stdint.h>
39+
#include "fl_error.h"
3940
#include "fl_file.h"
4041

4142
/* Maximum slot count (FPB v1: 6, v2: 8) */
@@ -118,9 +119,9 @@ bool fl_is_inited(fl_context_t* ctx);
118119

119120
/**
120121
* @brief Execute command from argc/argv
121-
* @return 0 on success, -1 on error
122+
* @return FL_OK on success, negative fl_error_t on error
122123
*/
123-
int fl_exec_cmd(fl_context_t* ctx, int argc, const char** argv);
124+
fl_error_t fl_exec_cmd(fl_context_t* ctx, int argc, const char** argv);
124125

125126
#ifdef __cplusplus
126127
}

App/func_loader/fl_error.h

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
/*
2+
* MIT License
3+
* Copyright (c) 2026 VIFEX
4+
*
5+
* Permission is hereby granted, free of charge, to any person obtaining a copy
6+
* of this software and associated documentation files (the "Software"), to deal
7+
* in the Software without restriction, including without limitation the rights
8+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
* copies of the Software, and to permit persons to whom the Software is
10+
* furnished to do so, subject to the following conditions:
11+
*
12+
* The above copyright notice and this permission notice shall be included in
13+
* all copies or substantial portions of the Software.
14+
*
15+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21+
* SOFTWARE.
22+
*/
23+
24+
/**
25+
* @file fl_error.h
26+
* @brief Error codes for func_loader
27+
*/
28+
29+
#ifndef FL_ERROR_H
30+
#define FL_ERROR_H
31+
32+
#ifdef __cplusplus
33+
extern "C" {
34+
#endif
35+
36+
/**
37+
* @brief Error codes for fl_exec_cmd and command handlers
38+
*/
39+
typedef enum {
40+
FL_OK = 0, /* Command executed (success or handled error) */
41+
FL_ERR_ARGS = -1, /* Missing or invalid required arguments */
42+
FL_ERR_CRC = -2, /* CRC verification failed */
43+
FL_ERR_PARSE = -3, /* Argument parsing failed */
44+
FL_ERR_UNKNOWN = -4, /* Unknown command */
45+
} fl_error_t;
46+
47+
#ifdef __cplusplus
48+
}
49+
#endif
50+
51+
#endif /* FL_ERROR_H */

App/func_loader/fl_port_nuttx.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -188,8 +188,8 @@ static int interactive_mode(fl_context_t* ctx, int argc_first, char** argv_first
188188

189189
int argc = parse_line(line, argv, sizeof(argv) / sizeof(argv[0]));
190190
if (argc > 0) {
191-
const int ret = fl_exec_cmd(ctx, argc, argv);
192-
if (ret != 0) {
191+
const fl_error_t ret = fl_exec_cmd(ctx, argc, argv);
192+
if (ret != FL_OK) {
193193
printf("Type 'q' to exit\n\n");
194194
}
195195
}

App/func_loader/fl_stream.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -84,13 +84,13 @@ static int parse_line(char* line, const char** argv, int max_argc) {
8484
return argc;
8585
}
8686

87-
int fl_stream_exec_line(fl_stream_t* s, char* line) {
87+
fl_error_t fl_stream_exec_line(fl_stream_t* s, char* line) {
8888
static const char* argv[FL_MAX_ARGC];
8989
int argc = parse_line(line, argv, FL_MAX_ARGC);
9090
if (argc > 0) {
9191
return fl_exec_cmd(s->ctx, argc, argv);
9292
}
93-
return 0;
93+
return FL_OK;
9494
}
9595

9696
void fl_stream_process(fl_stream_t* s) {

App/func_loader/fl_stream.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ extern "C" {
3535

3636
#include <stdint.h>
3737
#include <stddef.h>
38+
#include "fl_error.h"
3839

3940
/* Serial callbacks */
4041
typedef int (*fl_serial_read_cb_t)(uint8_t* buf, size_t len);
@@ -71,7 +72,7 @@ void fl_stream_process(fl_stream_t* s);
7172
/**
7273
* @brief Parse line and execute
7374
*/
74-
int fl_stream_exec_line(fl_stream_t* s, char* line);
75+
fl_error_t fl_stream_exec_line(fl_stream_t* s, char* line);
7576

7677
#ifdef __cplusplus
7778
}

0 commit comments

Comments
 (0)