/*
* co_candebug.c - contains debug functionality over CAN
*
* Copyright (c) 2012-2023 emotas embedded communication GmbH
*-------------------------------------------------------------------
* $Id: co_candebug.c 50703 2023-11-13 06:59:48Z boe $
*
*
*-------------------------------------------------------------------
*
*
*/

/********************************************************************/
/**
* \brief CAN debug functionality
*
* \file co_candebug.c
* Contain functions to send any data over CAN
*
*/

/* header of standard C - libraries
---------------------------------------------------------------------------*/
#include <string.h>

/* header of project specific types
---------------------------------------------------------------------------*/
#include <gen_define.h>

#ifdef CO_CAN_DEBUG_SUPPORTED
#include <co_datatype.h>
#include <co_drv.h>
#include <co_timer.h>
#include <co_candebug.h>
#include "ico_cobhandler.h"
#include "ico_queue.h"
#include "ico_candebug.h"

/* constant definitions
---------------------------------------------------------------------------*/

/* local defined data types
---------------------------------------------------------------------------*/

/* list of external used functions, if not in headers
---------------------------------------------------------------------------*/

/* list of global defined functions
---------------------------------------------------------------------------*/

/* list of local defined functions
---------------------------------------------------------------------------*/

/* external variables
---------------------------------------------------------------------------*/

/* global variables
---------------------------------------------------------------------------*/

/* local defined variables
---------------------------------------------------------------------------*/
static COB_REFERENZ_T	debugCob[CO_LINE_ARRAY_DECL] = {CO_INVALID_COB_REF};


/***************************************************************************/
/**
*
* \brief coCandebugPrint - send debug message over CAN
*
*
* \return RET_T
*
*/
RET_T coCanDebugPrint(
		CO_LINE_TYPE	CO_LINE,	/**< can line */
		UNSIGNED32	canId,			/**< CAN identifier */
		UNSIGNED8	dataLen,		/**< CAN date len */
		const UNSIGNED8	*pData		/**< pointer to data */
	)
{
UNSIGNED8		trData[CO_CAN_MAX_DATA_LEN];	/* data */
RET_T			retVal;

	/* insanity check */
	if ((dataLen != 0u) && (pData == NULL))  {
		return(RET_INVALID_PARAMETER);
	}

	/* test to see if it fits in the internal buffer */
	if (dataLen > CO_CAN_MAX_DATA_LEN)  {
		return(RET_PARAMETER_INCOMPATIBLE);
	}

	if (debugCob[CO_LINE] == CO_INVALID_COB_REF)  {
		debugCob[CO_LINE] = icoCobCreate(CO_LINE, CO_COB_TYPE_TRANSMIT,
			CO_SERVICE_CAN_DEBUG, 0u);
		if (debugCob[CO_LINE] == CO_INVALID_COB_REF)  {
			return(RET_EVENT_NO_RESSOURCE);
		}
	}

	retVal = icoCobSet(CO_LINE, debugCob[CO_LINE], canId, CO_COB_RTR_NONE, dataLen);
	if (retVal != RET_OK)  {
		return(retVal);
	}

	if (dataLen > CO_CAN_MAX_DATA_LEN)  {
		return(RET_INVALID_PARAMETER);
	}

	memcpy(&trData[0], pData, (size_t)dataLen);

	/* transmit data */
	retVal = icoTransmitMessage(CO_LINE, debugCob[CO_LINE], &trData[0], 0u);

	return(retVal);
}


/***************************************************************************/
/**
* \internal
*
* \brief icoDebugVarInit
*
* \return none
*
*/
void icoCanDebugVarInit(
		void
	)
{
CO_LINE_TYPE	CO_LINE;

	for (CO_LINE = 0; CO_LINE < CO_MULTI_LINE_CNT; CO_LINE++)
	{
		debugCob[CO_LINE] = CO_INVALID_COB_REF;
	}
}
#endif /* CO_CAN_DEBUG_SUPPORTED */
