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
2417static 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
6140static 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
8867static 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+
125116void si_command_process ()
126117{
127118 if (bus_state != BUS_STATE_IDLE )
0 commit comments