#include <stdio.h>
#include <ntcan.h>
/*
* This example demonstrates how the NTCAN-API can be used to open a handle,
* configure a nominal and data phase bit rate and transmit a CAN FD frame
* Finally all proper cleanup operations are performed
*/
int main()
{ 
    int net=0; /* logical net number*/
    uint32_t mode=NTCAN_MODE_FD; /* Mode bits for canOpen */
    int32_t txqueuesize=8; /* maximum number of messages to transmit */
    int32_t rxqueuesize=8; /* maximum number of messages to receive */
    int32_t txtimeout=100; /* timeout for transmit in ms */
    int32_t rxtimeout=1000; /* timeout for receiving data in ms */
    NTCAN_HANDLE txhandle; /* can handle returned by canOpen() */
    NTCAN_RESULT retvalue; /* return values of NTCAN API calls */
    NTCAN_BAUDRATE_X baud; /* Bit rate configuration */
    CMSG_X cmsg[8]; /* can message buffer */
    int no_brs=0; /* No bit rate switch bit */
    int i; /* loop counter */
    int32_t len; /* # of CAN messages */
/* ############################################################### */
    retvalue = canOpen( net,
                        mode,
                        txqueuesize,
                        rxqueuesize,
                        txtimeout,
                        rxtimeout,
                        &txhandle);
    if (retvalue != NTCAN_SUCCESS)
    { 
        printf("canOpen() failed with error %d!\n", retvalue);
        return(-1);
    }
    printf("function canOpen() returned OK !\n");
/* ############################################################### */
    baud.mode = NTCAN_BAUDRATE_MODE_INDEX;
    baud.flags = NTCAN_BAUDRATE_FLAG_FD;
    baud.arb.u.idx = NTCAN_BAUD_1000; /* Nominal bit rate: 1 MBit/s */
    baud.data.u.idx = NTCAN_BAUD_5000; /* Data phase bit rate: 5 MBit/s */
    retvalue = canSetBaudrateX(txhandle, &baud);
    if (retvalue != 0)
    {
        printf("canSetBaudrateX() failed with error %d!\n", retvalue);
        canClose(txhandle);
        return(-1);
    }
    printf("function canSetBaudrateX() returned OK !\n");
/* ############################################################### */
    /*
    * Initialize the first message in buffer to CAN id = 0, len = 12
    * and data0-data12 = 0..11
    */
    cmsg[0].id = 0x00;
    cmsg[0].len = NTCAN_DATASIZE_TO_DLC(12);
    cmsg[0].len |= (NTCAN_FD | (no_brs << 4));
    for (i = 0; i < 12; i++)
        cmsg[0].data[i] = (uint8_t)i;
    len=1; /* Number of valid messages in cmsg buffer*/
    retvalue = canWriteX(txhandle, &cmsg[0], &len, NULL);
    if (retvalue != NTCAN_SUCCESS)
        printf("canWriteX failed() with error %d!\n", retvalue);
    else
        printf("function canWriteX() returned OK !\n");
/* ############################################################### */
    retvalue = canClose (txhandle);
    if (retvalue != NTCAN_SUCCESS)
        printf("canClose() failed with error %d!\n", retvalue);
    else
        printf("canClose() returned OK !\n");
/* ############################################################### */
    return(0);
}