/*********************************************************************************
 * Copyright(C),Dalian Joynext Technology Co., Ltd.
 * FileName:    CAN_Sender.c
 * Author:      huo_h1
 * Version :    Ver0.1
 * Date:        2020-10-15
 * Description:
 *
 *********************************************************************************/
/*********************************************************************************
 *                      Include                                                  *
**********************************************************************************/
#include "CAN_Sender.h"

/*********************************************************************************
 *                      Typedef/Struct                                           *
**********************************************************************************/
struct SignalSender
{
    BufferObj*  buffer;
    SendProc    send_message;
    uint16_t    event_code;
};

/*********************************************************************************
*                      Function                                                  *
**********************************************************************************/
SignalSender* SignalSender_alloc()
{
    return OS_malloc(sizeof(SignalSender));
}

void SignalSender_init(SignalSender* sender, uint8_t buffer_size, SendProc send_msg_proc, uint16_t event_code)
{
    if(NULL != sender)
    {
        sender->send_message = send_msg_proc;
        sender->event_code = event_code;
        sender->buffer = BufferObj_alloc();

        BufferObj_init(sender->buffer, buffer_size);
    }
}

void SignalSender_release(SignalSender* sender)
{
    if(NULL != sender)
    {
        if(NULL != sender->buffer)
        {
            BufferObj_release(sender->buffer);
            sender->buffer = NULL;
        }

        sender->send_message = NULL;
        sender->event_code = 0;

        OS_free(sender);
    }
}

void SignalSender_clear(SignalSender* sender)
{
    if(NULL != sender)
    {
        BufferObj_clear(sender->buffer);
    }
}

void SignalSender_flush(SignalSender* sender)
{  
    if(NULL != sender && sender->send_message != NULL)
    {
        if(BufferObj_getBufferSize(sender->buffer) != 0)
        {
            sender->send_message(sender->event_code, BufferObj_getBuffer(sender->buffer), BufferObj_getBufferSize(sender->buffer));         
            SignalSender_clear(sender);
        }
    }
}

uint8_t SignalSender_addSignal(SignalSender* sender, uint16_t signal_id, uint8_t* signal_value, uint8_t signal_size)
{
    uint8_t number = 0;

    if(NULL != sender)
    {
        if (BufferObj_getSpace(sender->buffer) < sizeof(signal_id) + sizeof(signal_size) + signal_size)
        {
            SignalSender_flush(sender);
        }

        if(NULL != sender->buffer)
        {
            number = *BufferObj_getBuffer(sender->buffer) + 1;
        }

        BufferObj_update(sender->buffer, 0, &number, sizeof(number));
        BufferObj_pushBack(sender->buffer, (uint8_t*)&signal_id, sizeof(signal_id));
        BufferObj_pushBack(sender->buffer, &signal_size, sizeof(signal_size));
        BufferObj_pushBack(sender->buffer, signal_value, signal_size);

        return number;
    }

    return 0;
}

BufferObj* SignalSender_getBuffer(SignalSender* sender)
{
	if(sender) {
		return sender->buffer;
	}

	return NULL;
}

