/**
	establishSession.c

	This file automatically generated by FSMLang
*/

#include "establishSession_priv.h"
#include <stddef.h>

#ifndef DBG_PRINTF
#define DBG_PRINTF(...)
#endif

static HSM_COMMUNICATOR_EVENT establishSessionFSM(FSM_TYPE_PTR,HSM_COMMUNICATOR_EVENT);

static HSM_COMMUNICATOR_EVENT IDLE_stateFn(pESTABLISH_SESSION,HSM_COMMUNICATOR_EVENT);
static HSM_COMMUNICATOR_EVENT AWAITING_RESPONSE_stateFn(pESTABLISH_SESSION,HSM_COMMUNICATOR_EVENT);

static const ESTABLISH_SESSION_STATE_FN establishSession_state_fn_array[establishSession_numStates] = 
{
	  IDLE_stateFn
	, AWAITING_RESPONSE_stateFn
};

HSM_COMMUNICATOR_EVENT THIS(sub_machine_fn)(HSM_COMMUNICATOR_EVENT e)
{
	return establishSessionFSM(pestablishSession,e);
}

HSM_COMMUNICATOR_SUB_FSM_IF hsmCommunicator_establishSession_sub_fsm_if =
{
		.subFSM = THIS(sub_machine_fn)
	, .first_event = THIS(ESTABLISH_SESSION_REQUEST)
	, .last_event = THIS(STEP1_RESPONSE)
};

ESTABLISH_SESSION establishSession = {
	establishSession_IDLE,
	THIS(ESTABLISH_SESSION_REQUEST),
	&establishSession_state_fn_array,
	establishSessionFSM
};

pESTABLISH_SESSION pestablishSession = &establishSession;


#ifndef EVENT_IS_NOT_EXCLUDED_FROM_LOG
#define EVENT_IS_NOT_EXCLUDED_FROM_LOG(e) (e == e)
#endif
HSM_COMMUNICATOR_EVENT establishSessionFSM(pESTABLISH_SESSION pfsm, HSM_COMMUNICATOR_EVENT event)
{
	HSM_COMMUNICATOR_EVENT e = event;

	while (
		(e != THIS(noEvent))
		&& (e >= THIS(ESTABLISH_SESSION_REQUEST))
	)
	{

#ifdef ESTABLISH_SESSION_DEBUG
if (EVENT_IS_NOT_EXCLUDED_FROM_LOG(e))
{
	DBG_PRINTF("event: %s; state: %s"
,ESTABLISH_SESSION_EVENT_NAMES[e - THIS(ESTABLISH_SESSION_REQUEST)]
,ESTABLISH_SESSION_STATE_NAMES[pfsm->state]
);
}
#endif

	/* This is read-only data to facilitate error reporting in action functions */
	pfsm->event = e;

		e = ((* (*pfsm->statesArray)[pfsm->state])(pfsm,e));

	}

	return e == THIS(noEvent) ? PARENT(noEvent) : e;
}

static HSM_COMMUNICATOR_EVENT IDLE_stateFn(pESTABLISH_SESSION pfsm,HSM_COMMUNICATOR_EVENT e)
{
	HSM_COMMUNICATOR_EVENT retVal = THIS(noEvent);

	switch(e)
	{
	case THIS(ESTABLISH_SESSION_REQUEST):
		retVal = UFMN(sendStep0Message)(pfsm);
		pfsm->state = establishSession_AWAITING_RESPONSE;
		break;
	default:
		DBG_PRINTF("hsmCommunicator_establishSession_noAction");
		break;
	}

	return retVal;
}

static HSM_COMMUNICATOR_EVENT AWAITING_RESPONSE_stateFn(pESTABLISH_SESSION pfsm,HSM_COMMUNICATOR_EVENT e)
{
	HSM_COMMUNICATOR_EVENT retVal = THIS(noEvent);

	switch(e)
	{
	case THIS(STEP0_RESPONSE):
		retVal = UFMN(sendStep1Message)(pfsm);
		break;
	case THIS(STEP1_RESPONSE):
		retVal = UFMN(notifyParent)(pfsm);
		pfsm->state = establishSession_IDLE;
		break;
	default:
		DBG_PRINTF("hsmCommunicator_establishSession_noAction");
		break;
	}

	return retVal;
}


HSM_COMMUNICATOR_EVENT __attribute__((weak)) UFMN(sendStep0Message)(FSM_TYPE_PTR pfsm)
{
	DBG_PRINTF("weak: %s", __func__);
	(void) pfsm;
	return THIS(noEvent);
}

HSM_COMMUNICATOR_EVENT __attribute__((weak)) UFMN(sendStep1Message)(FSM_TYPE_PTR pfsm)
{
	DBG_PRINTF("weak: %s", __func__);
	(void) pfsm;
	return THIS(noEvent);
}

HSM_COMMUNICATOR_EVENT __attribute__((weak)) UFMN(notifyParent)(FSM_TYPE_PTR pfsm)
{
	DBG_PRINTF("weak: %s", __func__);
	(void) pfsm;
	return THIS(noEvent);
}


#ifdef ESTABLISH_SESSION_DEBUG
char *ESTABLISH_SESSION_EVENT_NAMES[] = {
	 "hsmCommunicator_establishSession_ESTABLISH_SESSION_REQUEST"
	,"hsmCommunicator_establishSession_STEP0_RESPONSE"
	,"hsmCommunicator_establishSession_STEP1_RESPONSE"
	, "establishSession_noEvent"
	, "establishSession_numEvents"
};

char *ESTABLISH_SESSION_STATE_NAMES[] = {
	 "hsmCommunicator_establishSession_IDLE"
	,"hsmCommunicator_establishSession_AWAITING_RESPONSE"
};

#endif