Commit 92ecd033 authored by Tristan Gingold's avatar Tristan Gingold

Simplify fsm-table

parent b12dd381
...@@ -68,32 +68,25 @@ static void pp_diag_fsm(struct pp_instance *ppi, const char *name, int sequence, ...@@ -68,32 +68,25 @@ static void pp_diag_fsm(struct pp_instance *ppi, const char *name, int sequence,
static const struct pp_state_table_item * static const struct pp_state_table_item *
get_current_state_table_item(struct pp_instance *ppi) get_current_state_table_item(struct pp_instance *ppi)
{ {
const struct pp_state_table_item *ip = ppi->current_state_item; const struct pp_state_table_item *cur_ip = ppi->current_state_item;
const struct pp_state_table_item *nxt_ip = &pp_state_table[ppi->state];
/* Avoid searching if we already know where we are */
ppi->is_new_state = 0;
if (ip && ip->state == ppi->state)
return ip;
ppi->is_new_state = 1;
/* a linear search is affordable up to a few dozen items */
for (ip = pp_state_table; ip->state != PPS_END_OF_TABLE; ip++)
if (ip->state == ppi->state) {
ppi->current_state_item = ip;
return ip;
}
return NULL;
}
const char *get_state_as_string(struct pp_instance *ppi, int state) { if (cur_ip == nxt_ip) {
static char *def="INVALID"; ppi->is_new_state = 0;
const struct pp_state_table_item *ip = ppi->current_state_item; }
else {
ppi->is_new_state = 1;
ppi->current_state_item = nxt_ip;
}
for (ip = pp_state_table; ip->state != PPS_END_OF_TABLE; ip++) return nxt_ip;
if (ip->state == state) { }
return ip->name;
} const char *get_state_as_string(struct pp_instance *ppi, int state)
return def; {
if (state >= 0 && state < PPS_LAST_STATE)
return pp_state_table[state].name;
return "INVALID";
} }
/* /*
...@@ -111,7 +104,8 @@ int pp_leave_current_state(struct pp_instance *ppi) ...@@ -111,7 +104,8 @@ int pp_leave_current_state(struct pp_instance *ppi)
} }
/* if the next or old state is non standard PTP reset all timeouts */ /* if the next or old state is non standard PTP reset all timeouts */
if ((ppi->state > PPS_LAST_STATE) || (ppi->next_state > PPS_LAST_STATE)) if ((ppi->state >= PPS_LAST_STATE)
|| (ppi->next_state >= PPS_LAST_STATE))
pp_timeout_setall(ppi); pp_timeout_setall(ppi);
ppi->state = ppi->next_state; ppi->state = ppi->next_state;
......
...@@ -372,10 +372,8 @@ typedef enum { ...@@ -372,10 +372,8 @@ typedef enum {
PPS_SLAVE, PPS_SLAVE,
#ifdef CONFIG_ABSCAL #ifdef CONFIG_ABSCAL
PPS_ABSCAL, /* not standard */ PPS_ABSCAL, /* not standard */
PPS_LAST_STATE=PPS_ABSCAL
#else
PPS_LAST_STATE=PPS_SLAVE
#endif #endif
PPS_LAST_STATE /* unused */
}pp_std_states; }pp_std_states;
typedef enum { typedef enum {
......
...@@ -425,12 +425,11 @@ int is_timestamp_incorrect_thres(struct pp_instance *ppsi, int *err_count, int t ...@@ -425,12 +425,11 @@ int is_timestamp_incorrect_thres(struct pp_instance *ppsi, int *err_count, int t
typedef int pp_action(struct pp_instance *ppi, void *buf, int len); typedef int pp_action(struct pp_instance *ppi, void *buf, int len);
struct pp_state_table_item { struct pp_state_table_item {
int state;
const char *name; const char *name;
pp_action *f1; pp_action *f1;
}; };
extern const struct pp_state_table_item pp_state_table[]; /* 0-terminated */ extern const struct pp_state_table_item pp_state_table[];
/* Convert current state as a string value */ /* Convert current state as a string value */
const char *get_state_as_string(struct pp_instance *ppi, int state); const char *get_state_as_string(struct pp_instance *ppi, int state);
......
...@@ -11,17 +11,16 @@ ...@@ -11,17 +11,16 @@
* This is the state machine table. */ * This is the state machine table. */
const struct pp_state_table_item pp_state_table[] = { const struct pp_state_table_item pp_state_table[] = {
{ PPS_INITIALIZING, "initializing", pp_initializing}, [PPS_INITIALIZING] = { "initializing", pp_initializing},
{ PPS_FAULTY, "faulty", pp_faulty}, [PPS_FAULTY] = { "faulty", pp_faulty},
{ PPS_DISABLED, "disabled", pp_disabled}, [PPS_DISABLED] = { "disabled", pp_disabled},
{ PPS_LISTENING, "listening", pp_listening}, [PPS_LISTENING] = { "listening", pp_listening},
{ PPS_PRE_MASTER, "pre-master", pp_master}, [PPS_PRE_MASTER] = { "pre-master", pp_master},
{ PPS_MASTER, "master", pp_master}, [PPS_MASTER] = { "master", pp_master},
{ PPS_PASSIVE, "passive", pp_passive}, [PPS_PASSIVE] = { "passive", pp_passive},
{ PPS_UNCALIBRATED, "uncalibrated", pp_slave}, [PPS_UNCALIBRATED] = { "uncalibrated", pp_slave},
{ PPS_SLAVE, "slave", pp_slave}, [PPS_SLAVE] = { "slave", pp_slave},
#ifdef CONFIG_ABSCAL #ifdef CONFIG_ABSCAL
{ PPS_ABSCAL, "abscal", pp_abscal}, [PPS_ABSCAL] = { "abscal", pp_abscal},
#endif #endif
{ PPS_END_OF_TABLE,}
}; };
...@@ -751,7 +751,6 @@ void dump_one_field_ppsi_wrpc(int type, int size, void *p, int i) ...@@ -751,7 +751,6 @@ void dump_one_field_ppsi_wrpc(int type, int size, void *p, int i)
case dump_type_ppi_state: case dump_type_ppi_state:
case dump_type_ppi_state_Enumeration8: case dump_type_ppi_state_Enumeration8:
switch(i) { switch(i) {
ENUM_TO_P_IN_CASE(PPS_END_OF_TABLE, char_p);
ENUM_TO_P_IN_CASE(PPS_INITIALIZING, char_p); ENUM_TO_P_IN_CASE(PPS_INITIALIZING, char_p);
ENUM_TO_P_IN_CASE(PPS_FAULTY, char_p); ENUM_TO_P_IN_CASE(PPS_FAULTY, char_p);
ENUM_TO_P_IN_CASE(PPS_DISABLED, char_p); ENUM_TO_P_IN_CASE(PPS_DISABLED, char_p);
...@@ -761,6 +760,7 @@ void dump_one_field_ppsi_wrpc(int type, int size, void *p, int i) ...@@ -761,6 +760,7 @@ void dump_one_field_ppsi_wrpc(int type, int size, void *p, int i)
ENUM_TO_P_IN_CASE(PPS_PASSIVE, char_p); ENUM_TO_P_IN_CASE(PPS_PASSIVE, char_p);
ENUM_TO_P_IN_CASE(PPS_UNCALIBRATED, char_p); ENUM_TO_P_IN_CASE(PPS_UNCALIBRATED, char_p);
ENUM_TO_P_IN_CASE(PPS_SLAVE, char_p); ENUM_TO_P_IN_CASE(PPS_SLAVE, char_p);
ENUM_TO_P_IN_CASE(PPS_LAST_STATE, char_p);
default: default:
char_p = "Unknown"; char_p = "Unknown";
} }
......
...@@ -571,7 +571,6 @@ void dump_one_field_ppsi_wrs(int type, int size, void *p, int i) ...@@ -571,7 +571,6 @@ void dump_one_field_ppsi_wrs(int type, int size, void *p, int i)
case dump_type_ppi_state: case dump_type_ppi_state:
case dump_type_ppi_state_Enumeration8: case dump_type_ppi_state_Enumeration8:
switch(i) { switch(i) {
ENUM_TO_P_IN_CASE(PPS_END_OF_TABLE, char_p);
ENUM_TO_P_IN_CASE(PPS_INITIALIZING, char_p); ENUM_TO_P_IN_CASE(PPS_INITIALIZING, char_p);
ENUM_TO_P_IN_CASE(PPS_FAULTY, char_p); ENUM_TO_P_IN_CASE(PPS_FAULTY, char_p);
ENUM_TO_P_IN_CASE(PPS_DISABLED, char_p); ENUM_TO_P_IN_CASE(PPS_DISABLED, char_p);
...@@ -581,6 +580,7 @@ void dump_one_field_ppsi_wrs(int type, int size, void *p, int i) ...@@ -581,6 +580,7 @@ void dump_one_field_ppsi_wrs(int type, int size, void *p, int i)
ENUM_TO_P_IN_CASE(PPS_PASSIVE, char_p); ENUM_TO_P_IN_CASE(PPS_PASSIVE, char_p);
ENUM_TO_P_IN_CASE(PPS_UNCALIBRATED, char_p); ENUM_TO_P_IN_CASE(PPS_UNCALIBRATED, char_p);
ENUM_TO_P_IN_CASE(PPS_SLAVE, char_p); ENUM_TO_P_IN_CASE(PPS_SLAVE, char_p);
ENUM_TO_P_IN_CASE(PPS_LAST_STATE, char_p);
default: default:
char_p = "Unknown"; char_p = "Unknown";
} }
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment