#include "iec_include.h"
#include "usart.h"

struct sSerialTransceiverFT12 {
    int messageTimeout;
    int characterTimeout;
    LinkLayerParameters linkLayerParameters;
    SerialPort serialPort;
    IEC60870_RawMessageHandler rawMessageHandler;
    void* rawMessageHandlerParameter;
};

SerialTransceiverFT12
SerialTransceiverFT12_create(SerialPort serialPort, LinkLayerParameters linkLayerParameters)
{
    SerialTransceiverFT12 self = (SerialTransceiverFT12) GLOBAL_MALLOC(sizeof(struct sSerialTransceiverFT12));

    if (self != NULL) {
        self->messageTimeout = 10;
        self->characterTimeout = 300;
        self->linkLayerParameters = linkLayerParameters;
        self->serialPort = serialPort;
        self->rawMessageHandler = NULL;
    }

    return self;
}
void
SerialTransceiverFT12_setRawMessageHandler(SerialTransceiverFT12 self, IEC60870_RawMessageHandler handler, void* parameter)
{
    self->rawMessageHandler = handler;
    self->rawMessageHandlerParameter = parameter;
}

void
SerialTransceiverFT12_sendMessage(SerialTransceiverFT12 self, uint8_t* msg, int msgSize)
{
    if (self->rawMessageHandler)
        self->rawMessageHandler(self->rawMessageHandlerParameter, msg, msgSize, true);
		USART_232_Send(msg, msgSize);
    //SerialPort_write(self->serialPort, msg, 0, msgSize);
}
static int
readBytesWithTimeout(SerialTransceiverFT12 self, uint8_t* buffer, int startIndex, int count)
{
    uint8_t readByte;
		uint8_t startBytr = startIndex;
    int readBytes = 0;

    while (1) {
        buffer[startIndex++] = rx_232_Buffer[startBytr + readBytes];

        readBytes++;

        if (readBytes >= count)
            break;
    }

    return readBytes;
}

// 开始解析数据
void
SerialTransceiverFT12_readNextMessage(SerialTransceiverFT12 self, uint8_t* buffer,
        SerialTXMessageHandler messageHandler, void* parameter)
{	
//    int read = SerialPort_readByte(self->serialPort);
		uint8_t read = rx_232_Buffer[0];

    if (read != 0) {

        if (read == 0x68) {

            //int msgSize = SerialPort_readByte(self->serialPort);
						uint8_t msgSize = rx_232_Buffer[1];		

            if (msgSize < 1)
                goto sync_error;

            buffer[0] = (uint8_t) 0x68;
            buffer[1] = (uint8_t) msgSize;

            msgSize += 4;

            int readBytes = readBytesWithTimeout(self, buffer, 2, msgSize);

            if (readBytes == msgSize) {
                msgSize += 2;

                if (self->rawMessageHandler)
                    self->rawMessageHandler(self->rawMessageHandlerParameter, buffer, msgSize, false);

                messageHandler(parameter, buffer, msgSize);
            }
            else {
                printf("RECV: Timeout reading variable length frame size = %i (expected = %i)\n", readBytes, msgSize);
            }

        }
        else if (read == 0x10) {


            buffer[0] = 0x10;

            int msgSize = 3 + self->linkLayerParameters->addressLength;

            int readBytes = readBytesWithTimeout(self, buffer, 1, msgSize);

            if (readBytes == msgSize) {
                msgSize += 1;

                if (self->rawMessageHandler)
                    self->rawMessageHandler(self->rawMessageHandlerParameter, buffer, msgSize, false);

                messageHandler(parameter, buffer, msgSize);
            }
            else {
                printf("RECV: Timeout reading fixed length frame size = %i (expected = %i)\n", readBytes, msgSize);
            }

        }
        else if (read == 0xe5) {
            int msgSize = 1;
            buffer[0] = (uint8_t) read;

            if (self->rawMessageHandler)
                self->rawMessageHandler(self->rawMessageHandlerParameter, buffer, msgSize, false);

            messageHandler(parameter, buffer, msgSize);
        }
        else {
            goto sync_error;
        }

    }
    return;

sync_error:

    printf("RECV: SYNC ERROR\n");


    return;
}


void
SerialTransceiverFT12_destroy(SerialTransceiverFT12 self)
{
    if (self != NULL)
        GLOBAL_FREEMEM(self);
}