<commit_before>#include "serial.h"
#include "packet.h"

#ifdef _WIN32

#include <windows.h>
#include <stdlib.h>

HANDLE serialHandle;
DCB serialParameters = {0};
COMMTIMEOUTS serialTimeouts = {0};

unsigned int serial_connect(char device[]) {
    serialHandle = CreateFile(device, GENERIC_READ | GENERIC_WRITE, 0, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);

    if (serialHandle == INVALID_HANDLE_VALUE) {
        return SERIAL_ERROR;
    }

    serialParameters.DCBlength = sizeof(serialParameters);

    if (GetCommState(serialHandle, &serialParameters) == 0) {
        CloseHandle(serialHandle);

        return SERIAL_ERROR;
    }

    serialParameters.BaudRate = CBR_9600;
    serialParameters.ByteSize = 8;
    serialParameters.StopBits = ONESTOPBIT;
    serialParameters.Parity = NOPARITY;

    if (SetCommState(serialHandle, &serialParameters) == 0) {
        CloseHandle(serialHandle);

        return SERIAL_ERROR;
    }

    serialTimeouts.ReadIntervalTimeout = 50;
    serialTimeouts.ReadTotalTimeoutMultiplier = 10;
    serialTimeouts.ReadTotalTimeoutConstant = 50;
    serialTimeouts.WriteTotalTimeoutMultiplier = 10;
    serialTimeouts.WriteTotalTimeoutConstant = 50;

    if (SetCommTimeouts(serialHandle, &serialTimeouts) == 0) {
        CloseHandle(serialHandle);

        return SERIAL_ERROR;
    }

    return SERIAL_SUCCESS;
}

unsigned int serial_transmit(struct Packet packet) {
    char bytes[] = { packet_to_binary(packet) };

    DWORD bytes_written;

    if (!WriteFile(serialHandle, bytes, 1, &bytes_written, NULL)) {
        CloseHandle(serialHandle);

        return SERIAL_ERROR;
    }

    return SERIAL_SUCCESS;
}

unsigned int serial_close() {
    if (CloseHandle(serialHandle) == 0) {
        return SERIAL_ERROR;
    }

    return SERIAL_SUCCESS;
}

#elif __linux

unsigned int serial_connect(char *name) {
    return SERIAL_ERROR;
}

unsigned int serial_transmit(struct Packet packet) {
    return SERIAL_ERROR;
}

unsigned int serial_close() {
    return SERIAL_ERROR;
}

#else
#error Operating System not supported!
#endif
<commit_msg>Add linux serial implementation<commit_after>#include "serial.h"
#include "packet.h"

#ifdef _WIN32

#include <windows.h>
#include <stdlib.h>

HANDLE serialHandle;
DCB serialParameters = {0};
COMMTIMEOUTS serialTimeouts = {0};

unsigned int serial_connect(char device[]) {
    serialHandle = CreateFile(device, GENERIC_READ | GENERIC_WRITE, 0, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);

    if (serialHandle == INVALID_HANDLE_VALUE) {
        return SERIAL_ERROR;
    }

    serialParameters.DCBlength = sizeof(serialParameters);

    if (GetCommState(serialHandle, &serialParameters) == 0) {
        CloseHandle(serialHandle);

        return SERIAL_ERROR;
    }

    serialParameters.BaudRate = CBR_9600;
    serialParameters.ByteSize = 8;
    serialParameters.StopBits = ONESTOPBIT;
    serialParameters.Parity = NOPARITY;

    if (SetCommState(serialHandle, &serialParameters) == 0) {
        CloseHandle(serialHandle);

        return SERIAL_ERROR;
    }

    serialTimeouts.ReadIntervalTimeout = 50;
    serialTimeouts.ReadTotalTimeoutMultiplier = 10;
    serialTimeouts.ReadTotalTimeoutConstant = 50;
    serialTimeouts.WriteTotalTimeoutMultiplier = 10;
    serialTimeouts.WriteTotalTimeoutConstant = 50;

    if (SetCommTimeouts(serialHandle, &serialTimeouts) == 0) {
        CloseHandle(serialHandle);

        return SERIAL_ERROR;
    }

    return SERIAL_SUCCESS;
}

unsigned int serial_transmit(struct Packet packet) {
    char bytes[] = { packet_to_binary(packet) };

    DWORD bytes_written;

    if (!WriteFile(serialHandle, bytes, 1, &bytes_written, NULL)) {
        CloseHandle(serialHandle);

        return SERIAL_ERROR;
    }

    return SERIAL_SUCCESS;
}

unsigned int serial_close() {
    if (CloseHandle(serialHandle) == 0) {
        return SERIAL_ERROR;
    }

    return SERIAL_SUCCESS;
}

#elif __linux

#include <unistd.h>
#include <termios.h>

int fd = -1;

unsigned int serial_connect(char *name) {
    fd = open(name, O_WRONLY | O_NOCTTY | O_SYNC);

    if (fd < 0) {
        return SERIAL_ERROR;
    }

    struct termios tty = {0};

    if (tcgetattr(fd, &tty) != 0) {
        close(fd);

        return SERIAL_ERROR;
    }

    if (cfsetspeed(&tty, B9600) != 0) {
        close(fd);

        return SERIAL_ERROR;
    }

    tty.c_cflag &= ~(CSIZE | CREAD | PARENB);
    tty.c_cflag |= (CS8 | CLOCAL);

    tty.c_oflag = 0;

    if (tcsetattr(fd, TCSANOW, &tty) != 0) {
        close(fd);

        return SERIAL_ERROR;
    }

    return SERIAL_SUCCESS;
}

unsigned int serial_transmit(struct Packet packet) {
    char bytes[] = { packet_to_binary(packet) };

    if (write(fd, bytes, 1) != 1) {
        close(fd);

        return SERIAL_ERROR;
    }

    return SERIAL_SUCCESS;
}

unsigned int serial_close() {
    if (close(fd) != 0) {
        return SERIAL_ERROR;
    }

    return SERIAL_SUCCESS;
}

#else
#error Operating System not supported!
#endif

{'fraction_non_alphanumeric': 0.10184813177983126, 'fraction_numerical': 0.013057452792286059, 'mean_word_length': 3.386784140969163, 'pattern_counts': {'":': 0, '<': 10, '<?xml version=': 0, '>': 9, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5951990', 'n_tokens_mistral': 1685, 'n_tokens_neox': 1581, 'n_words': 345}