二维状态表如何工作

时间:2019-05-24 00:57:48

标签: c

我有一个2D状态表,如下面的代码所示。这是用于wifi设备的供应应用程序。我了解1D状态表的想法,但不了解2D状态的想法,这就是我感到困惑的原因。 有人可以解释这是如何工作的吗? “事件”如何确定触发哪个功能? “事件”确定下一个“状态”吗?工作流程的图表可能会有所帮助。

我尝试引用:https://en.wikipedia.org/wiki/State_transition_table#Two-dimensional_state_tables

完整的代码附在这里: https://drive.google.com/file/d/1uvJMmRV7iccjnsIGB0G9wSZSY-DXVMjJ/view?usp=sharing


unsigned char event;
mq_receive(gProvisioningSMQueue, (char *)&event, 1, NULL);
/* Find Next event entry */
pEntry = (Provisioning_TableEntry_t *)&gTransitionTable[g_CurrentState][event];
/* Change state acording to event */
if (pEntry->nextState != g_CurrentState)
{
   g_CurrentState = pEntry->nextState;
}

const Provisioning_TableEntry_t gTransitionTable[AppState_MAX][AppEvent_MAX] =
{
        /* AppState_STARTING */
    {
        /* Event: AppEvent_STARTED */ {StartConnection,
                                       AppState_WAIT_FOR_CONNECTION     },
        /* Event: AppEvent_CONNECTED */ {HandleWaitForIp, AppState_WAIT_FOR_IP },
        /* Event: AppEvent_IP_ACQUIRED */ {ReportError, AppState_ERROR      },
        /* Event: AppEvent_DISCONNECT */ {ReportError, AppState_ERROR       },
        /* Event: AppEvent_PING_COMPLETE */ {ReportError, AppState_ERROR    },
        /* AppEvent_PROVISIONING_STARTED */ {ReportError, AppState_ERROR    },
        /* AppEvent_PROVISIONING_SUCCESS */ {ReportError, AppState_ERROR    },
        /* AppEvent_PROVISIONING_STOPPED */ {ReportError, AppState_ERROR    },
        /* AppEvent_PROVISIONING_WAIT_CONN */ {ReportError, AppState_ERROR  },
        /* Event: AppEvent_TIMEOUT */ {ReportError, AppState_ERROR          },
        /* Event: AppEvent_ERROR */ {ReportError, AppState_ERROR            },
        /* Event: AppEvent_RESTART */ {ProvisioningStart,
                                       AppState_PROVISIONING_IN_PROGRESS}
    },
    /* AppState_WAIT_FOR_CONNECTION */
    {
        /* Event: AppEvent_STARTED */ 
        {StartConnection, AppState_WAIT_FOR_CONNECTION            },
        /** Event: AppEvent_CONNECTED */ 
        {HandleWaitForIp, AppState_WAIT_FOR_IP                    },
        /* Event: AppEvent_IP_ACQUIRED */
         {ReportError, AppState_ERROR                             },
        /* Event: AppEvent_DISCONNECT */ 
        {StartConnection, AppState_WAIT_FOR_CONNECTION            },
        /* Event: AppEvent_PING_COMPLETE */
         {HandleProvisioningComplete, AppState_PINGING_GW         },
        /* AppEvent_PROVISIONING_STARTED */ 
        {ProvisioningStart, AppState_PROVISIONING_IN_PROGRESS     },
        /* AppEvent_PROVISIONING_SUCCESS */ 
        {DoNothing, AppState_WAIT_FOR_CONNECTION                  },
        /* AppEvent_PROVISIONING_STOPPED */ 
         {CheckLanConnection, AppState_WAIT_FOR_CONNECTION        },
        /* AppEvent_PROVISIONING_WAIT_CONN */ 
        {DoNothing, AppState_WAIT_FOR_CONNECTION                  },
        /* Event: AppEvent_TIMEOUT */
         {ProvisioningStart, AppState_PROVISIONING_IN_PROGRESS    },
        /* Event: AppEvent_ERROR */ 
        {ProvisioningStart, AppState_PROVISIONING_IN_PROGRESS     },
        /* Event: AppEvent_RESTART */ 
        {ProcessRestartRequest, AppState_WAIT_FOR_CONNECTION      }
    },
    /* AppState_WAIT_FOR_IP */
    {
        /* Event: AppEvent_STARTED */
         {ProvisioningStart, AppState_PROVISIONING_IN_PROGRESS          },
        /* Event: AppEvent_CONNECTED */ {ReportError, AppState_ERROR    },
        /* Event: AppEvent_IP_ACQUIRED */
         {CheckLanConnection, AppState_PINGING_GW                       },
        /* Event: AppEvent_DISCONNECT */ 
        {StartConnection, AppState_WAIT_FOR_CONNECTION                  },
        /* Event: AppEvent_PING_COMPLETE */ 
        {DoNothing, AppState_WAIT_FOR_IP                                },
        /* AppEvent_PROVISIONING_STARTED */
         {ProvisioningStart, AppState_PROVISIONING_IN_PROGRESS          },
        /* AppEvent_PROVISIONING_SUCCESS */ 
        {DoNothing, AppState_WAIT_FOR_IP                                },
        /* AppEvent_PROVISIONING_STOPPED */ 
        {HandleProvisioningComplete, AppState_PINGING_GW                },
        /* AppEvent_PROVISIONING_WAIT_CONN */ 
        {DoNothing, AppState_WAIT_FOR_IP                                },
        /* Event: AppEvent_TIMEOUT */ 
        {ReportError, AppState_ERROR                                    },
        /* Event: AppEvent_ERROR */
         {ReportError, AppState_ERROR                                   },
        /* Event: AppEvent_RESTART */ 
        {ProcessRestartRequest,
                                       AppState_WAIT_FOR_CONNECTION     }
    },
    /* AppState_PINGING_GW */
    {
        /* Event: AppEvent_STARTED */ 
        {DoNothing, AppState_PINGING_GW                                  },
        /* Event: AppEvent_CONNECTED */ 
        {HandleWaitForIp, AppState_WAIT_FOR_IP                           },
        /* Event: AppEvent_IP_ACQUIRED */
         {CheckInternetConnection, AppState_PINGING_GW                   },
        /* Event: AppEvent_DISCONNECT */ 
        {HandleDiscnctEvt, AppState_WAIT_FOR_CONNECTION                  },
        /* Event: AppEvent_PING_COMPLETE */ 
        {DoNothing, AppState_PINGING_GW                                  },
        /* AppEvent_PROVISIONING_STARTED */ 
        {ProvisioningStart, AppState_PROVISIONING_IN_PROGRESS            },
        /* AppEvent_PROVISIONING_SUCCESS */
         {DoNothing, AppState_PINGING_GW                                 },
        /* AppEvent_PROVISIONING_STOPPED */ 
        {HandleUserApplication, AppState_PINGING_GW                      },
        /* AppEvent_PROVISIONING_WAIT_CONN */ 
        {DoNothing, AppState_PINGING_GW                                  },
        /*  Event: AppEvent_TIMEOUT */
         {SendPingToGW, AppState_PINGING_GW                              },
        /* Event: AppEvent_ERROR */ 
        {ReportError, AppState_ERROR                                     },
        /* Event: AppEvent_RESTART */ 
        {ProcessRestartRequest, AppState_WAIT_FOR_CONNECTION             }
    },
    /* AppState_PROVISIONING_IN_PROGRESS  */
    {
        /* Event: AppEvent_STARTED */ 
        {ProcessStartRequest, AppState_PROVISIONING_IN_PROGRESS    },
        /* Event: AppEvent_CONNECTED */
         {HandleConnection, AppState_PROVISIONING_IN_PROGRESS            },
        /* Event: AppEvent_IP_ACQUIRED */
        {CheckInternetConnection, AppState_PROVISIONING_IN_PROGRESS      },
        /* Event: AppEvent_DISCONNECT */ 
        {HandleDiscnctEvt, AppState_PROVISIONING_IN_PROGRESS             },
        /* Event: AppEvent_PING_COMPLETE */ 
        {DoNothing, AppState_PROVISIONING_IN_PROGRESS                    },
        /* AppEvent_PROVISIONING_STARTED */
         {DoNothing, AppState_PROVISIONING_IN_PROGRESS                   },
        /* AppEvent_PROVISIONING_SUCCESS */ 
        {HandleProvisioningComplete, AppState_PROVISIONING_WAIT_COMPLETE},
        /* AppEvent_PROVISIONING_STOPPED */
         {HandleProvisioningComplete, AppState_PINGING_GW               },
        /* AppEvent_PROVISIONING_WAIT_CONN */
         {DoNothing, AppState_WAIT_FOR_CONNECTION                       },
        /* Event: AppEvent_TIMEOUT */ 
        {ProcessRestartRequest, AppState_WAIT_FOR_CONNECTION            },
        /* Event: AppEvent_ERROR */
        {ReportError, AppState_ERROR                                    },
        /* Event: AppEvent_RESTART */ 
        {ProcessRestartRequest, AppState_WAIT_FOR_CONNECTION            }

    },
    /* AppState_PROVISIONING_WAIT_COMPLETE */
    {
        /* Event: AppEvent_STARTED */
         {DoNothing, AppState_PROVISIONING_WAIT_COMPLETE },
        /* Event: AppEvent_CONNECTED */ 
        {DoNothing, AppState_PROVISIONING_WAIT_COMPLETE  },
        /* Event: AppEvent_IP_ACQUIRED */
         {DoNothing, AppState_PROVISIONING_WAIT_COMPLETE },
        /* Event: AppEvent_DISCONNECT */ 
        {HandleDiscnctEvt, AppState_PROVISIONING_WAIT_COMPLETE },
        /* Event: AppEvent_PING_COMPLETE */ 
        {DoNothing, AppState_PROVISIONING_WAIT_COMPLETE },
        /* AppEvent_PROVISIONING_STARTED */ 
        {DoNothing, AppState_PROVISIONING_WAIT_COMPLETE },
        /* AppEvent_PROVISIONING_SUCCESS */ 
        {DoNothing, AppState_PROVISIONING_WAIT_COMPLETE },
        /* AppEvent_PROVISIONING_STOPPED */ 
        {HandleUserApplication, AppState_PINGING_GW     },
        /* AppEvent_PROVISIONING_WAIT_CONN */ 
        {DoNothing, AppState_WAIT_FOR_CONNECTION        },
        /* Event: AppEvent_TIMEOUT */ 
        {HandleProvisioningTimeOut, AppState_PROVISIONING_WAIT_COMPLETE },
        /* Event: AppEvent_ERROR */
        {ReportError, AppState_ERROR },
        /* Event: AppEvent_RESTART */ 
        {ProcessRestartRequest, AppState_PROVISIONING_WAIT_COMPLETE }
    },
    /* AppState_ERROR */
    /* we will restart connection for all errors */
    {
        /* Event: AppEvent_STARTED */
         {ReportError, AppState_WAIT_FOR_CONNECTION     },
        /* Event: AppEvent_CONNECTED */ 
        {ReportError, AppState_WAIT_FOR_CONNECTION     },
        /* Event: AppEvent_IP_ACQUIRED */ 
        {ReportError, AppState_WAIT_FOR_CONNECTION     },
        /* Event: AppEvent_DISCONNECT */ 
        {ReportError, AppState_WAIT_FOR_CONNECTION     },
        /* Event: AppEvent_PING_COMPLETE */ 
        {ReportError, AppState_WAIT_FOR_CONNECTION     },
        /* AppEvent_PROVISIONING_STARTED */
        {ReportError, AppState_WAIT_FOR_CONNECTION     },
        /* AppEvent_PROVISIONING_SUCCESS */ 
        {ReportError, AppState_WAIT_FOR_CONNECTION     },
        /* AppEvent_PROVISIONING_STOPPED */ 
        {ReportError, AppState_WAIT_FOR_CONNECTION     },
        /* AppEvent_PROVISIONING_WAIT_CONN */ 
        {ReportError, AppState_WAIT_FOR_CONNECTION     },
        /* Event: AppEvent_TIMEOUT */
         {ReportError, AppState_WAIT_FOR_CONNECTION    },
        /* Event: AppEvent_ERROR */
         {ReportError,AppState_WAIT_FOR_CONNECTION     },
        /* Event: AppEvent_RESTART */ 
        {ReportError, AppState_WAIT_FOR_CONNECTION     }
    }
};

0 个答案:

没有答案