/**
 * @file log.c
 * @brief Logger implementation
 * @authors Lorenz C. && Sander S.
 */

#include <stdarg.h>
#include <stdint.h>
#include <stdio.h>
#include "stm32f7xx_hal.h"
#include "log.h"

extern UART_HandleTypeDef huart1;

/**
 * @brief Get the current timestamp to be used in the logger
 *
 * @return The current timestamp in milliseconds since boot
 */

int _write(int file, char *data, int len) {
    HAL_StatusTypeDef status;

    switch (file) {
        case STDOUT_FILENO:
        case STDERR_FILENO:
            status = HAL_UART_Transmit(&huart1, (uint8_t*)data, len, HAL_MAX_DELAY);
            if (status != HAL_OK) {
                errno = EIO;
                return -1;
            }
            break;
        default:
            errno = EBADF;
            return -1;
    }
    return len;
}

uint32_t logger_get_timestamp(void) {
    return HAL_GetTick();
}