#include "uart_ctrl.hpp"
#include <string.h>

// This function should be called when a package is sent
void UART_CTRL_Interface::pushPacket(RPCPackage package) {
    char buffer[MAX_PAGE_SIZES + 7];

    buffer[0] = UART_CMD_PACKET;
    buffer[1] = package.length;
    buffer[2] = package.type | (package.rpcNum << 4);
    memcpy(&buffer[3], &package.addr, 4);
    memcpy(&buffer[7], package.buffer, package.length);

    handler->send(buffer, package.length + 2);
}

void UART_CTRL_Interface::pushScanResulst(uint32_t addr, bool running) {
    char buffer[7];

    buffer[0] = UART_CMD_SCAN_RESULT;
    buffer[1] = running ? 1 : 0;
    memcpy(&buffer[2], &addr, 4);
    buffer[6] = 0; // This is where the type would go

    handler->send(buffer, 7);
}

void UART_CTRL_Interface::pushPackageSlotUpdate(bool complete, bool inUse, uint32_t slot, uint32_t addr, uint32_t timestamp, uint32_t bits) {
    char buffer[16];

    buffer[0] = UART_CMD_SLOT_UPDATE;
    buffer[1] = complete ? 1 : 0;
    buffer[2] = inUse ? 1 : 0;
    buffer[3] = slot;
    memcpy(&buffer[4], &addr, 4);
    memcpy(&buffer[8], &timestamp, 4);
    memcpy(&buffer[12], &bits, 4);

    handler->send(buffer, 14);
}

void UART_CTRL_Interface::pushInfo(uint8_t slotNumbers) {
    char buffer[2];

    buffer[0] = UART_CMD_INFO;
    buffer[1] = slotNumbers;

    handler->send(buffer, 2);
}