/**
	sendMessage.c

	This file automatically generated by FSMLang
*/

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

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

static HSM_COMMUNICATOR_EVENT sendMessageFSM(FSM_TYPE_PTR,HSM_COMMUNICATOR_EVENT);

static HSM_COMMUNICATOR_EVENT IDLE_stateFn(pSEND_MESSAGE,HSM_COMMUNICATOR_EVENT);
static HSM_COMMUNICATOR_EVENT IN_SESSION_stateFn(pSEND_MESSAGE,HSM_COMMUNICATOR_EVENT);
static HSM_COMMUNICATOR_EVENT AWAITING_ACK_stateFn(pSEND_MESSAGE,HSM_COMMUNICATOR_EVENT);

static const SEND_MESSAGE_STATE_FN sendMessage_state_fn_array[sendMessage_numStates] = 
{
	  IDLE_stateFn
	, IN_SESSION_stateFn
	, AWAITING_ACK_stateFn
};

HSM_COMMUNICATOR_EVENT THIS(sub_machine_fn)(HSM_COMMUNICATOR_EVENT e)
{
	return sendMessageFSM(psendMessage,e);
}

HSM_COMMUNICATOR_SUB_FSM_IF hsmCommunicator_sendMessage_sub_fsm_if =
{
		.subFSM = THIS(sub_machine_fn)
	, .first_event = THIS(SEND_MESSAGE)
	, .last_event = THIS(SESSION_TIMEOUT)
};

SEND_MESSAGE sendMessage = {
	sendMessage_IDLE,
	THIS(SEND_MESSAGE),
	&sendMessage_state_fn_array,
	sendMessageFSM
};

pSEND_MESSAGE psendMessage = &sendMessage;


#ifndef EVENT_IS_NOT_EXCLUDED_FROM_LOG
#define EVENT_IS_NOT_EXCLUDED_FROM_LOG(e) (e == e)
#endif
HSM_COMMUNICATOR_EVENT sendMessageFSM(pSEND_MESSAGE pfsm, HSM_COMMUNICATOR_EVENT event)
{
	HSM_COMMUNICATOR_EVENT e = event;

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

#ifdef SEND_MESSAGE_DEBUG
if (EVENT_IS_NOT_EXCLUDED_FROM_LOG(e))
{
	DBG_PRINTF("event: %s; state: %s"
,SEND_MESSAGE_EVENT_NAMES[e - THIS(SEND_MESSAGE)]
,SEND_MESSAGE_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(pSEND_MESSAGE pfsm,HSM_COMMUNICATOR_EVENT e)
{
	HSM_COMMUNICATOR_EVENT retVal = THIS(noEvent);

	switch(e)
	{
	case THIS(SEND_MESSAGE):
		retVal = UFMN(queueMessage)(pfsm);
		break;
	case THIS(SESSION_ESTABLISHED):
		retVal = UFMN(checkQueue)(pfsm);
		pfsm->state = sendMessage_IN_SESSION;
		break;
	default:
		DBG_PRINTF("hsmCommunicator_sendMessage_noAction");
		break;
	}

	return retVal;
}

static HSM_COMMUNICATOR_EVENT IN_SESSION_stateFn(pSEND_MESSAGE pfsm,HSM_COMMUNICATOR_EVENT e)
{
	HSM_COMMUNICATOR_EVENT retVal = THIS(noEvent);

	switch(e)
	{
	case THIS(SEND_MESSAGE):
		retVal = UFMN(sendMessage)(pfsm);
		pfsm->state = sendMessage_AWAITING_ACK;
		break;
	case THIS(SESSION_TIMEOUT):
#ifdef HSM_COMMUNICATOR_SEND_MESSAGE
		DBG_PRINTF("hsmCommunicator_sendMessage_noAction");
#endif
		pfsm->state = sendMessage_IDLE;
		break;
	default:
		DBG_PRINTF("hsmCommunicator_sendMessage_noAction");
		break;
	}

	return retVal;
}

static HSM_COMMUNICATOR_EVENT AWAITING_ACK_stateFn(pSEND_MESSAGE pfsm,HSM_COMMUNICATOR_EVENT e)
{
	HSM_COMMUNICATOR_EVENT retVal = THIS(noEvent);

	switch(e)
	{
	case THIS(SEND_MESSAGE):
		retVal = UFMN(queueMessage)(pfsm);
		break;
	case THIS(ACK):
		retVal = UFMN(checkQueue)(pfsm);
		pfsm->state = sendMessage_IN_SESSION;
		break;
	case THIS(SESSION_TIMEOUT):
#ifdef HSM_COMMUNICATOR_SEND_MESSAGE
		DBG_PRINTF("hsmCommunicator_sendMessage_noAction");
#endif
		pfsm->state = sendMessage_IDLE;
		break;
	default:
		DBG_PRINTF("hsmCommunicator_sendMessage_noAction");
		break;
	}

	return retVal;
}


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

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

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


#ifdef SEND_MESSAGE_DEBUG
char *SEND_MESSAGE_EVENT_NAMES[] = {
	 "hsmCommunicator_sendMessage_SEND_MESSAGE"
	,"hsmCommunicator_sendMessage_ACK"
	,"hsmCommunicator_sendMessage_SESSION_ESTABLISHED"
	,"hsmCommunicator_sendMessage_SESSION_TIMEOUT"
	, "sendMessage_noEvent"
	, "sendMessage_numEvents"
};

char *SEND_MESSAGE_STATE_NAMES[] = {
	 "hsmCommunicator_sendMessage_IDLE"
	,"hsmCommunicator_sendMessage_IN_SESSION"
	,"hsmCommunicator_sendMessage_AWAITING_ACK"
};

#endif