#include "can_to_usb.h"

/* Private include */
#include "../../../../USB/usb_message_defines.h"

static QSerialPort* this_serial_port;

void QT_USB_set_serial_handler(QSerialPort* serial_port){
    this_serial_port = serial_port;
}

ENUM_J1939_STATUS_CODES QT_USB_Transmit(uint32_t ID, uint8_t data[], uint8_t DLC){
    uint8_t send_data_array[14] = {0}; /* We cannot send more than 14 bytes */
    send_data_array[0] = J1939_MESSAGE_TYPE;
    send_data_array[1] = ID >> 24;  /* Most significant bit */
    send_data_array[2] = ID >> 16;
    send_data_array[3] = ID >> 8;
    send_data_array[4] = ID;        /* Least significant bit */
    send_data_array[5] = DLC;       /* Data length */
    for(uint8_t i = 0; i < DLC; i++)
           send_data_array[6+i] = data[i];
    this_serial_port->write((char*)send_data_array, 6+DLC);
    return STATUS_SEND_OK;
}

void QT_USB_Get_ID_Data(uint32_t *ID, uint8_t data[], bool* is_new_message){
    uint8_t receive_buf[13] = {0}; /* From STM32 PLC, we have send 14 bytes, but byte 0 is the message type, which we have already read */
    this_serial_port->read((char*)receive_buf, 13);
    *ID = (receive_buf[0] << 24) | (receive_buf[1] << 16) | (receive_buf[2] << 8) | receive_buf[3];
    uint8_t DLC = receive_buf[4];
    if(DLC == 0){
        *is_new_message = false;
    }else{
        for(uint8_t i = 0; i < 8; i++)
            if(i < DLC)
                data[i] = receive_buf[5+i];
            else
                data[i] = 0;
        *is_new_message = true;
    }
}



////////////////////////////////////////////////////////////////////////////////////////////

#ifndef CAN_BUS_H
#define CAN_BUS_H

#include "../../../../J1939/SAE_J1939/SAE_J1939_Enums/Enum_Send_Status.h"
#include <stdint.h>
#include <stdbool.h>

#ifdef __cplusplus
#include <QSerialPort>
void QT_USB_set_serial_handler(QSerialPort* serial_port);
extern "C" {
#endif

ENUM_J1939_STATUS_CODES QT_USB_Transmit(uint32_t ID, uint8_t data[], uint8_t DLC);
void QT_USB_Get_ID_Data(uint32_t *ID, uint8_t data[], bool* is_new_message);

#ifdef __cplusplus
}

#endif

#endif // CAN_BUS_H



///////////////////////////////////////////////////////////////////////////////////////////

#ifndef USB_MESSAGE_DEFINES_H
#define USB_MESSAGE_DEFINES_H

/* Message length from STM32 PLC */
#define J1939_MESSAGE_FROM_STM32_PLC 14
#define MEASUREMENT_MESSAGE_FROM_STM32_PLC 51

/* Message types for STM32 PLC */
#define J1939_MESSAGE_TYPE 0
#define CAN_BUS_MESSAGE_TYPE 1
#define PWM_MESSAGE_TYPE 2
#define ANALOG_OUTPUT_MESSAGE_TYPE 3
#define SEND_BACK_MEASUREMENT_MESSAGE_TYPE 4

#endif // USB_MESSAGE_DEFINES_H
