Skip to content

Commit 5355bfd

Browse files
committed
Fix tests
1 parent 5ebc8bc commit 5355bfd

File tree

4 files changed

+65
-49
lines changed

4 files changed

+65
-49
lines changed

firmware/libsi/include/si/device/commands.h

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,16 +16,35 @@
1616
*/
1717
typedef int (*si_command_handler_fn)(const uint8_t *command, si_complete_cb_t callback, void *context);
1818

19+
/**
20+
* Command structure representing a registered command.
21+
*/
22+
struct si_command {
23+
uint8_t command;
24+
uint8_t length;
25+
si_command_handler_fn handler;
26+
void *user_data;
27+
};
28+
1929
/**
2030
* Register a command handler for commands from an SI host.
2131
*
2232
* @param command the command to handle
23-
* @param command_length the length of the command
33+
* @param command_length the length of the command in bytes
2434
* @param handler the command handler function
2535
*
2636
*/
2737
void si_command_register(uint8_t command, uint8_t length, si_command_handler_fn handler, void *context);
2838

39+
/**
40+
* Look up a command structure by command ID.
41+
*
42+
* @param command the command ID to look up
43+
*
44+
* @return pointer to the command structure, or NULL if not found
45+
*/
46+
struct si_command *si_command_find_by_id(uint8_t command);
47+
2948
/**
3049
* Process a single SI command on the bus.
3150
*

firmware/libsi/src/device/commands.c

Lines changed: 30 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -9,16 +9,9 @@ enum {
99
BUS_STATE_ERROR,
1010
};
1111

12-
struct command_entry {
13-
uint8_t command;
14-
uint8_t length;
15-
si_command_handler_fn handler;
16-
void *context;
17-
};
18-
1912
// Command table for registered SI commands
20-
static struct command_entry command_table[COMMAND_TABLE_SIZE] = {0};
21-
static struct command_entry *current_command = NULL;
13+
static struct si_command command_table[COMMAND_TABLE_SIZE] = {0};
14+
static struct si_command *current_command = NULL;
2215

2316
// Current bus state
2417
static uint8_t bus_state = BUS_STATE_UNKNOWN;
@@ -43,20 +36,6 @@ static inline uint8_t hash_command(uint8_t command)
4336
return (command % (COMMAND_TABLE_SIZE - 2)) + 2;
4437
}
4538

46-
// Find a command entry by command id
47-
static struct command_entry *find_command(uint8_t command)
48-
{
49-
uint8_t index = hash_command(command);
50-
51-
while (command_table[index].handler != NULL) {
52-
if (command_table[index].command == command) {
53-
return &command_table[index];
54-
}
55-
index = (index + 1) % COMMAND_TABLE_SIZE;
56-
}
57-
return NULL;
58-
}
59-
6039
// Command handler TX completion callback
6140
static void on_tx_complete(int result)
6241
{
@@ -73,7 +52,7 @@ static void on_rx_complete(int result)
7352
{
7453
// If we successfully read a command and have a handler, call it
7554
if (result >= 0 && current_command && current_command->handler) {
76-
current_command->handler(command_buffer, on_tx_complete, current_command->context);
55+
current_command->handler(command_buffer, on_tx_complete, current_command->user_data);
7756
return;
7857
}
7958

@@ -87,22 +66,21 @@ static void on_rx_complete(int result)
8766

8867
static bool command_byte_cb(uint8_t byte, uint8_t byte_index)
8968
{
90-
// If this is the first byte, look up the command entry
69+
// If this is the first byte, check if there is a registered command
9170
if (byte_index == 0) {
92-
current_command = find_command(byte);
93-
if (current_command == NULL) {
94-
return false; // Stop reading on unknown command
95-
}
71+
current_command = si_command_find_by_id(byte);
72+
if (current_command == NULL)
73+
return false;
9674
}
9775

98-
if (byte_index == current_command->length - 1) {
99-
return false; // Stop reading after the expected length
100-
}
76+
// Stop reading when we reach the expected length
77+
if (byte_index == current_command->length - 1)
78+
return false;
10179

10280
return true;
10381
}
10482

105-
void si_command_register(uint8_t command, uint8_t length, si_command_handler_fn handler, void *context)
83+
void si_command_register(uint8_t command, uint8_t length, si_command_handler_fn handler, void *user_data)
10684
{
10785
uint8_t index = hash_command(command);
10886

@@ -113,15 +91,28 @@ void si_command_register(uint8_t command, uint8_t length, si_command_handler_fn
11391
index = (index + 1) % COMMAND_TABLE_SIZE;
11492
}
11593

116-
// Store the command entry
117-
command_table[index] = (struct command_entry){
118-
.command = command,
119-
.length = length,
120-
.handler = handler,
121-
.context = context,
94+
// Store the command
95+
command_table[index] = (struct si_command){
96+
.command = command,
97+
.length = length,
98+
.handler = handler,
99+
.user_data = user_data,
122100
};
123101
}
124102

103+
struct si_command *si_command_find_by_id(uint8_t command)
104+
{
105+
uint8_t index = hash_command(command);
106+
107+
while (command_table[index].handler != NULL) {
108+
if (command_table[index].command == command) {
109+
return &command_table[index];
110+
}
111+
index = (index + 1) % COMMAND_TABLE_SIZE;
112+
}
113+
return NULL;
114+
}
115+
125116
void si_command_process()
126117
{
127118
if (bus_state != BUS_STATE_IDLE)

firmware/libsi/test/test_commands.c

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -20,19 +20,25 @@ static int handle_reset(const uint8_t *command, si_complete_cb_t callback, void
2020

2121
static void test_register_command()
2222
{
23+
struct si_command *cmd = NULL;
24+
2325
si_command_register(0x00, 1, handle_info, NULL);
24-
TEST_ASSERT_EQUAL(1, si_command_get_length(0x00));
25-
TEST_ASSERT_EQUAL(handle_info, si_command_get_handler(0x00));
26+
cmd = si_command_find_by_id(0x00);
27+
TEST_ASSERT_NOT_NULL(cmd);
28+
TEST_ASSERT_EQUAL(1, cmd->length);
29+
TEST_ASSERT_EQUAL(handle_info, cmd->handler);
2630

2731
si_command_register(0xFF, 3, handle_reset, NULL);
28-
TEST_ASSERT_EQUAL(3, si_command_get_length(0xFF));
29-
TEST_ASSERT_EQUAL(handle_reset, si_command_get_handler(0xFF));
32+
cmd = si_command_find_by_id(0xFF);
33+
TEST_ASSERT_NOT_NULL(cmd);
34+
TEST_ASSERT_EQUAL(3, cmd->length);
35+
TEST_ASSERT_EQUAL(handle_reset, cmd->handler);
3036
}
3137

3238
static void test_register_command_missing()
3339
{
34-
TEST_ASSERT_EQUAL(0, si_command_get_length(0x69));
35-
TEST_ASSERT_NULL(si_command_get_handler(0x69));
40+
struct si_command *cmd = si_command_find_by_id(0x69);
41+
TEST_ASSERT_NULL(cmd);
3642
}
3743

3844
void test_commands(void)

firmware/libsi/test/test_gc_controller.c

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,15 +17,15 @@ void si_write_bytes(const uint8_t *data, uint8_t length, si_complete_cb_t callba
1717
response_len = length;
1818
}
1919

20-
void si_read_command(uint8_t *data, si_complete_cb_t callback)
20+
void si_read_bytes(uint8_t *buffer, uint8_t max_length, si_byte_cb_t byte_callback, si_complete_cb_t complete_callback)
2121
{
2222
}
2323

2424
// Simulate receiving a command
2525
static int simulate_command(struct si_device_gc_controller *device, uint8_t *command)
2626
{
27-
si_command_handler_fn handler = si_command_get_handler(command[0]);
28-
return handler(command, NULL, device);
27+
struct si_command *cmd = si_command_find_by_id(command[0]);
28+
return cmd->handler(command, NULL, device);
2929
}
3030

3131
// Test that the device info response is correct for a standard GameCube controller

0 commit comments

Comments
 (0)