
#include "lib_memory.h"

#include <stdlib.h>
#include <stdio.h>
#include <string.h>

#include "iec_include.h"

struct iectimeval
{
	long tv_sec;
	long tv_usec;
};
struct sSerialPort {
    char interfaceName[100];
    int fd;
    int baudRate;
    uint8_t dataBits;
    char parity;
    uint8_t stopBits;
    uint64_t lastSentTime;
    struct iectimeval timeout;
    SerialPortError lastError;
};



SerialPort SerialPort_create(const char* interfaceName, int baudRate, uint8_t dataBits, char parity, uint8_t stopBits)
{
    SerialPort self = (SerialPort) GLOBAL_MALLOC(sizeof(struct sSerialPort));

    if (self != NULL) {
        self->fd = -1;
        self->baudRate = baudRate;
        self->dataBits = dataBits;
        self->stopBits = stopBits;
        self->parity = parity;
        self->lastSentTime = 0;
        self->timeout.tv_sec = 0;
        self->timeout.tv_usec = 100000; /* 100 ms */
        strncpy(self->interfaceName, interfaceName, 99);
        self->lastError = SERIAL_PORT_ERROR_NONE;
    }

    return self;
}

void SerialPort_destroy(SerialPort self)
{
    if (self != NULL) {
        GLOBAL_FREEMEM(self);
    }
}