Blame src/state_machine/main.cpp

Packit Service aa3af4
/*
Packit Service aa3af4
 * Copyright (c) 2001-2020 Mellanox Technologies, Ltd. All rights reserved.
Packit Service aa3af4
 *
Packit Service aa3af4
 * This software is available to you under a choice of one of two
Packit Service aa3af4
 * licenses.  You may choose to be licensed under the terms of the GNU
Packit Service aa3af4
 * General Public License (GPL) Version 2, available from the file
Packit Service aa3af4
 * COPYING in the main directory of this source tree, or the
Packit Service aa3af4
 * BSD license below:
Packit Service aa3af4
 *
Packit Service aa3af4
 *     Redistribution and use in source and binary forms, with or
Packit Service aa3af4
 *     without modification, are permitted provided that the following
Packit Service aa3af4
 *     conditions are met:
Packit Service aa3af4
 *
Packit Service aa3af4
 *      - Redistributions of source code must retain the above
Packit Service aa3af4
 *        copyright notice, this list of conditions and the following
Packit Service aa3af4
 *        disclaimer.
Packit Service aa3af4
 *
Packit Service aa3af4
 *      - Redistributions in binary form must reproduce the above
Packit Service aa3af4
 *        copyright notice, this list of conditions and the following
Packit Service aa3af4
 *        disclaimer in the documentation and/or other materials
Packit Service aa3af4
 *        provided with the distribution.
Packit Service aa3af4
 *
Packit Service aa3af4
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
Packit Service aa3af4
 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
Packit Service aa3af4
 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
Packit Service aa3af4
 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
Packit Service aa3af4
 * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
Packit Service aa3af4
 * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
Packit Service aa3af4
 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
Packit Service aa3af4
 * SOFTWARE.
Packit Service aa3af4
 */
Packit Service aa3af4
Packit Service aa3af4
Packit Service aa3af4
#include <vlogger/vlogger.h>
Packit Service aa3af4
#include <stdio.h>
Packit Service aa3af4
#include <stdlib.h>
Packit Service aa3af4
#include "sm.h"
Packit Service aa3af4
#include "sm_fifo.h"
Packit Service aa3af4
#include <string.h>
Packit Service aa3af4
Packit Service aa3af4
#define MODULE_NAME			"SM_TEST: "
Packit Service aa3af4
Packit Service aa3af4
#define NOT_IN_USE(a)                   ((void)(a))
Packit Service aa3af4
Packit Service aa3af4
/* SM example */
Packit Service aa3af4
Packit Service aa3af4
typedef enum {
Packit Service aa3af4
	SM_ST_A = 0,
Packit Service aa3af4
	SM_ST_B,
Packit Service aa3af4
	SM_ST_C,
Packit Service aa3af4
	SM_ST_LAST
Packit Service aa3af4
} sm_state_e;
Packit Service aa3af4
Packit Service aa3af4
Packit Service aa3af4
Packit Service aa3af4
typedef enum {
Packit Service aa3af4
	SM_EV_1 = 0,
Packit Service aa3af4
	SM_EV_2,
Packit Service aa3af4
	SM_EV_3,
Packit Service aa3af4
	SM_EV_4,
Packit Service aa3af4
	SM_EV_LAST
Packit Service aa3af4
} sm_event_e;
Packit Service aa3af4
Packit Service aa3af4
// Debug functions  definitions
Packit Service aa3af4
const char *state_num_to_str_func(int state);
Packit Service aa3af4
const char* event_num_to_str_func(int event);
Packit Service aa3af4
void  print_event_info(int state, int event, void* app_hndl);
Packit Service aa3af4
Packit Service aa3af4
void sm_st_entry(const sm_info_t& info);
Packit Service aa3af4
void sm_st_leave(const sm_info_t& info);
Packit Service aa3af4
void sm_st_A_ev_1(const sm_info_t& info);
Packit Service aa3af4
void sm_st_A_ev_2(const sm_info_t& info);
Packit Service aa3af4
void sm_st_A_ev_3(const sm_info_t& info);
Packit Service aa3af4
void sm_st_B_ev_1(const sm_info_t& info);
Packit Service aa3af4
void sm_st_B_ev_2(const sm_info_t& info);
Packit Service aa3af4
void sm_st_B_ev_3(const sm_info_t& info);
Packit Service aa3af4
void sm_st_C_ev_1(const sm_info_t& info);
Packit Service aa3af4
void sm_st_C_ev_2(const sm_info_t& info);
Packit Service aa3af4
void sm_st_C_ev_3(const sm_info_t& info);
Packit Service aa3af4
Packit Service aa3af4
void sm_default_trans_func(const sm_info_t& info);
Packit Service aa3af4
Packit Service aa3af4
Packit Service aa3af4
//// The short table
Packit Service aa3af4
sm_short_table_line_t sm_short_table[] = {
Packit Service aa3af4
// 	{curr state,	event, 		next state,	action func  }
Packit Service aa3af4
	{ SM_ST_A,      SM_STATE_ENTRY, SM_NO_ST,       sm_st_entry}, 
Packit Service aa3af4
	{ SM_ST_A,      SM_EV_1,        SM_ST_A,        sm_st_A_ev_1}, 
Packit Service aa3af4
	{ SM_ST_A,      SM_EV_2,        SM_ST_B,sm_st_A_ev_2}, 
Packit Service aa3af4
	{ SM_ST_A,      SM_EV_3,        SM_ST_C,       sm_st_A_ev_3}, 
Packit Service aa3af4
	{ SM_ST_A,      SM_STATE_LEAVE, SM_NO_ST,      sm_st_leave}, 
Packit Service aa3af4
Packit Service aa3af4
	{ SM_ST_B,      SM_STATE_ENTRY, SM_NO_ST,      sm_st_entry}, 
Packit Service aa3af4
	{ SM_ST_B,      SM_EV_1,        SM_ST_B,       sm_st_B_ev_1}, 
Packit Service aa3af4
	{ SM_ST_B,      SM_EV_2,        SM_ST_C,       sm_st_B_ev_2}, 
Packit Service aa3af4
	{ SM_ST_B,      SM_EV_3,        SM_ST_A,       sm_st_B_ev_3}, 
Packit Service aa3af4
	{ SM_ST_B,      SM_STATE_LEAVE, SM_NO_ST,      sm_st_leave}, 
Packit Service aa3af4
Packit Service aa3af4
	{ SM_ST_C,      SM_STATE_ENTRY, SM_NO_ST,      sm_st_entry}, 
Packit Service aa3af4
	{ SM_ST_C,      SM_EV_1,        SM_ST_C,       sm_st_C_ev_1}, 
Packit Service aa3af4
	{ SM_ST_C,      SM_EV_2,        SM_ST_A,       sm_st_C_ev_2}, 
Packit Service aa3af4
	{ SM_ST_C,      SM_EV_3,        SM_ST_B,       sm_st_C_ev_3},
Packit Service aa3af4
	{ SM_ST_C,      SM_STATE_LEAVE, SM_NO_ST,      sm_st_leave},
Packit Service aa3af4
Packit Service aa3af4
	SM_TABLE_END
Packit Service aa3af4
};
Packit Service aa3af4
Packit Service aa3af4
#if 0
Packit Service aa3af4
Packit Service aa3af4
typedef struct {
Packit Service aa3af4
	int  event;             
Packit Service aa3af4
	char* name;
Packit Service aa3af4
} test_entry;
Packit Service aa3af4
Packit Service aa3af4
void fifo_test()
Packit Service aa3af4
{
Packit Service aa3af4
	sm_fifo my_fifo;
Packit Service aa3af4
	int i=0;
Packit Service aa3af4
	fifo_entry_t ret;
Packit Service aa3af4
Packit Service aa3af4
	test_entry arr_num[] = {
Packit Service aa3af4
		{1, "one"},
Packit Service aa3af4
		{2, "two"},
Packit Service aa3af4
		{3, "three"},
Packit Service aa3af4
		{4, "four"},
Packit Service aa3af4
		{5, "five"},
Packit Service aa3af4
		{6, "six"},
Packit Service aa3af4
		{7, "seven"},
Packit Service aa3af4
		{8, "eight"},
Packit Service aa3af4
		{9, "nine"},
Packit Service aa3af4
		{10,"ten"}
Packit Service aa3af4
	};
Packit Service aa3af4
Packit Service aa3af4
Packit Service aa3af4
	vlog_printf(VLOG_INFO, "fifo test\n");
Packit Service aa3af4
Packit Service aa3af4
	while (i<10) {
Packit Service aa3af4
		my_fifo.push_back(arr_num[i].event, (void *) arr_num[i].name );
Packit Service aa3af4
		vlog_printf(VLOG_ERROR, "element %d was inserted\n", arr_num[i]);
Packit Service aa3af4
		my_fifo.debug_print_fifo();
Packit Service aa3af4
		ret = my_fifo.get_front();
Packit Service aa3af4
		vlog_printf(VLOG_ERROR, "element %d was removed (%s)\n", ret.event, ret.ev_data);
Packit Service aa3af4
		my_fifo.debug_print_fifo();
Packit Service aa3af4
		i++;
Packit Service aa3af4
	}
Packit Service aa3af4
	/*while (i>0) {
Packit Service aa3af4
			ret = my_fifo.get_element();
Packit Service aa3af4
			vlog_printf(VLOG_ERROR, "element %d was removeded\n", ret);
Packit Service aa3af4
			my_fifo.debug_print_fifo();
Packit Service aa3af4
			i--;
Packit Service aa3af4
	}*/
Packit Service aa3af4
}
Packit Service aa3af4
Packit Service aa3af4
#endif
Packit Service aa3af4
Packit Service aa3af4
Packit Service aa3af4
#if _BullseyeCoverage
Packit Service aa3af4
    #pragma BullseyeCoverage off
Packit Service aa3af4
#endif
Packit Service aa3af4
Packit Service aa3af4
Packit Service aa3af4
state_machine* g_sm;
Packit Service aa3af4
Packit Service aa3af4
int main(int argc, char *argv[])
Packit Service aa3af4
{
Packit Service aa3af4
	vlog_levels_t log_level = VLOG_DETAILS;
Packit Service aa3af4
Packit Service aa3af4
	if (argc > 1) {
Packit Service aa3af4
		log_level = log_level::from_str(argv[1], VLOG_INIT);
Packit Service aa3af4
		if (log_level == VLOG_INIT ) {
Packit Service aa3af4
			printf("illegal log level %s\n", argv[1]);
Packit Service aa3af4
			return -1;
Packit Service aa3af4
		}
Packit Service aa3af4
	}
Packit Service aa3af4
	vlog_start("SM_TEST", log_level, NULL, 0);
Packit Service aa3af4
	//fifo_test();
Packit Service aa3af4
Packit Service aa3af4
	g_sm = new state_machine(NULL,
Packit Service aa3af4
				 SM_ST_A,
Packit Service aa3af4
				 SM_ST_LAST, 
Packit Service aa3af4
				 SM_EV_LAST, 
Packit Service aa3af4
				 sm_short_table, 
Packit Service aa3af4
				 sm_default_trans_func, 
Packit Service aa3af4
				 NULL, 
Packit Service aa3af4
				 NULL,
Packit Service aa3af4
				 print_event_info);
Packit Service aa3af4
Packit Service aa3af4
	g_sm->process_event(SM_EV_2,(void *)"event 2");
Packit Service aa3af4
Packit Service aa3af4
	delete g_sm;
Packit Service aa3af4
}
Packit Service aa3af4
Packit Service aa3af4
Packit Service aa3af4
Packit Service aa3af4
//// Debug functions  definitions
Packit Service aa3af4
const char* state_num_to_str_func(int state)
Packit Service aa3af4
{
Packit Service aa3af4
	switch (state) {
Packit Service aa3af4
	case SM_ST_A:
Packit Service aa3af4
		return "SM_ST_A";
Packit Service aa3af4
	case SM_ST_B:
Packit Service aa3af4
		return "SM_ST_B";
Packit Service aa3af4
	case SM_ST_C:
Packit Service aa3af4
		return "SM_ST_C";
Packit Service aa3af4
	default:
Packit Service aa3af4
		return "Undefined";
Packit Service aa3af4
	} 
Packit Service aa3af4
Packit Service aa3af4
}
Packit Service aa3af4
Packit Service aa3af4
const char* event_num_to_str_func(int event)
Packit Service aa3af4
{
Packit Service aa3af4
	switch (event) {
Packit Service aa3af4
	case SM_EV_1:
Packit Service aa3af4
		return "SM_EV_1";
Packit Service aa3af4
	case SM_EV_2:
Packit Service aa3af4
		return "SM_EV_2";
Packit Service aa3af4
	case SM_EV_3:
Packit Service aa3af4
		return "SM_EV_3";
Packit Service aa3af4
	case SM_EV_4:
Packit Service aa3af4
		return "SM_EV_4";
Packit Service aa3af4
	default:
Packit Service aa3af4
		return "Undefined";
Packit Service aa3af4
	} 
Packit Service aa3af4
}
Packit Service aa3af4
Packit Service aa3af4
void print_event_info(int state, int event, void* app_hndl)
Packit Service aa3af4
{
Packit Service aa3af4
	NOT_IN_USE(app_hndl);
Packit Service aa3af4
	printf(MODULE_NAME "Got event %s (%d) in state %s (%d)\n", 
Packit Service aa3af4
	       event_num_to_str_func(event), event, state_num_to_str_func(state), state);
Packit Service aa3af4
}
Packit Service aa3af4
Packit Service aa3af4
////////////////////////////////////////
Packit Service aa3af4
// SM Entry Function       
Packit Service aa3af4
void sm_st_entry(const sm_info_t& info)
Packit Service aa3af4
{
Packit Service aa3af4
	printf(MODULE_NAME "State changed %s (%d) => %s (%d)\n", 
Packit Service aa3af4
	       state_num_to_str_func(info.old_state), info.old_state, 
Packit Service aa3af4
	       state_num_to_str_func(info.new_state), info.new_state);
Packit Service aa3af4
}
Packit Service aa3af4
Packit Service aa3af4
////////////////////////////////////////
Packit Service aa3af4
// SM Leave Function
Packit Service aa3af4
void sm_st_leave(const sm_info_t& info)
Packit Service aa3af4
{
Packit Service aa3af4
	printf(MODULE_NAME "State changing %s (%d) => %s (%d)\n", 
Packit Service aa3af4
	       state_num_to_str_func(info.old_state), info.old_state, 
Packit Service aa3af4
	       state_num_to_str_func(info.new_state), info.new_state);
Packit Service aa3af4
}
Packit Service aa3af4
Packit Service aa3af4
////////////////////////////////////////
Packit Service aa3af4
// SM Transition Functions                                        
Packit Service aa3af4
void sm_default_trans_func(const sm_info_t& info)
Packit Service aa3af4
{
Packit Service aa3af4
	printf(MODULE_NAME "Default Transition: Handle event %s (%d) in state %s (%d)\n",
Packit Service aa3af4
	       event_num_to_str_func(info.event), info.event,
Packit Service aa3af4
	       state_num_to_str_func(info.old_state), info.old_state);
Packit Service aa3af4
	if (info.new_state != SM_ST_STAY) {
Packit Service aa3af4
		printf(MODULE_NAME "Default Transition: Moving to state %s (%d)\n", state_num_to_str_func(info.new_state), info.new_state);
Packit Service aa3af4
Packit Service aa3af4
	}
Packit Service aa3af4
}
Packit Service aa3af4
Packit Service aa3af4
void sm_st_A_ev_1(const sm_info_t& info)
Packit Service aa3af4
{
Packit Service aa3af4
	printf(MODULE_NAME "Got event %s in state A\n", (char*)info.ev_data);
Packit Service aa3af4
}
Packit Service aa3af4
Packit Service aa3af4
void sm_st_A_ev_2(const sm_info_t& info)
Packit Service aa3af4
{
Packit Service aa3af4
	printf(MODULE_NAME "Got event %s in state A\n", (char*)info.ev_data);
Packit Service aa3af4
	g_sm->process_event(SM_EV_4, (void*)"event 4"); 
Packit Service aa3af4
	g_sm->process_event(SM_EV_1, (void*)"event 1"); 
Packit Service aa3af4
	g_sm->process_event(SM_EV_2, (void*)"event 2"); 
Packit Service aa3af4
	g_sm->process_event(SM_EV_3, (void*)"event 3"); 
Packit Service aa3af4
	g_sm->process_event(SM_EV_4, (void*)"event 4"); 
Packit Service aa3af4
	//g_sm->m_sm_fifo.debug_print_fifo();
Packit Service aa3af4
}
Packit Service aa3af4
Packit Service aa3af4
void sm_st_A_ev_3(const sm_info_t& info)
Packit Service aa3af4
{
Packit Service aa3af4
	printf(MODULE_NAME "Got event %s\n", (char*)info.ev_data);
Packit Service aa3af4
}
Packit Service aa3af4
Packit Service aa3af4
void sm_st_B_ev_1(const sm_info_t& info)
Packit Service aa3af4
{
Packit Service aa3af4
	NOT_IN_USE(info);
Packit Service aa3af4
	printf(MODULE_NAME "Got event %s\n", event_num_to_str_func(SM_EV_1));
Packit Service aa3af4
}
Packit Service aa3af4
Packit Service aa3af4
void sm_st_B_ev_2(const sm_info_t& info)
Packit Service aa3af4
{
Packit Service aa3af4
	printf(MODULE_NAME "Got event %s\n", (char*)info.ev_data);
Packit Service aa3af4
	g_sm->process_event(SM_EV_1, (void*)"event 1");   
Packit Service aa3af4
}
Packit Service aa3af4
Packit Service aa3af4
void sm_st_B_ev_3(const sm_info_t& info)
Packit Service aa3af4
{
Packit Service aa3af4
	NOT_IN_USE(info);
Packit Service aa3af4
	printf(MODULE_NAME "Got event %s\n", event_num_to_str_func(SM_EV_3));
Packit Service aa3af4
}
Packit Service aa3af4
Packit Service aa3af4
void sm_st_C_ev_1(const sm_info_t& info)
Packit Service aa3af4
{
Packit Service aa3af4
	NOT_IN_USE(info);
Packit Service aa3af4
	printf(MODULE_NAME "Got event %s\n", event_num_to_str_func(SM_EV_1));
Packit Service aa3af4
}
Packit Service aa3af4
Packit Service aa3af4
void sm_st_C_ev_2(const sm_info_t& info)
Packit Service aa3af4
{
Packit Service aa3af4
	NOT_IN_USE(info);
Packit Service aa3af4
	printf(MODULE_NAME "Got event %s\n", event_num_to_str_func(SM_EV_2));
Packit Service aa3af4
	g_sm->process_event(SM_EV_4, (void*)"event 4");   
Packit Service aa3af4
}
Packit Service aa3af4
Packit Service aa3af4
void sm_st_C_ev_3(const sm_info_t& info)
Packit Service aa3af4
{
Packit Service aa3af4
	NOT_IN_USE(info);
Packit Service aa3af4
	printf(MODULE_NAME "Got event %s\n", event_num_to_str_func(SM_EV_3));
Packit Service aa3af4
}
Packit Service aa3af4
Packit Service aa3af4
#if _BullseyeCoverage
Packit Service aa3af4
    #pragma BullseyeCoverage on
Packit Service aa3af4
#endif