<commit_before>#include "comm.h"
#include <inttypes.h>
#include <stddef.h>
#include <stdlib.h>
#include <stdnoreturn.h>
#include <string.h>
#include "base/os.h"
#include "base/reader.h"
#include "base/writer.h"
#include "logger/logger.h"
#include "system.h"

typedef enum { CommReceiver = 0x60, CommTransmitter = 0x61 } CommAddress;

typedef enum {
    CommHardReset = 0xAB,
    ReceiverSoftReset = 0xAA,
    ReceiverGetFrameCount = 0x21,
    ReceiverGetFrame = 0x22,
    ReceiverRemoveFrame = 0x24,
    ReceiverGetTelemetry = 0x1A,
} ReceiverCommand;

typedef enum {
    TransmitterSoftReset = 0xAA,
    TransmitterSendFrame = 0x10,
    TransmitterGetTelemetry = 0x25,
    TransmitterSetBeacon = 0x14,
    TransmitterClearBeacon = 0x1f,
    TransmitterSetIdleState = 0x24,
    TransmitterSetBitRate = 0x28,
    TransmitterGetState = 0x41
} TransmitterCommand;

static noreturn void CommTask(void* param);

static bool SendCommand(CommObject* object, CommAddress address, uint8_t command)
{
    return object->low.writeProc(address, &command, sizeof(command)) == i2cTransferDone;
}

static bool SendCommandWithResponse(
    CommObject* object, CommAddress address, uint8_t command, uint8_t* outBuffer, uint8_t outBufferSize)
{
    return object->low.readProc(address, &command, sizeof(command), outBuffer, outBufferSize) == i2cTransferDone;
}

void CommInitialize(CommObject* comm, const CommLowInterface* lowerInterface)
{
    memset(comm, 0, sizeof(CommObject));
    comm->low = *lowerInterface;
}

bool CommRestart(CommObject* comm)
{
    if (comm->commTask == NULL)
    {
        const OSResult result = System.CreateTask(CommTask, "COMM Task", 512, comm, 4, &comm->commTask);
        if (result != OSResultSuccess)<commit_msg>[comm] Comm update.<commit_after>#include "comm.h"
#include <inttypes.h>
#include <stddef.h>
#include <stdlib.h>
#include <stdnoreturn.h>
#include <string.h>
#include "base/os.h"
#include "base/reader.h"
#include "base/writer.h"
#include "logger/logger.h"
#include "system.h"

typedef enum { CommReceiver = 0x60, CommTransmitter = 0x61 } CommAddress;

typedef enum {
    CommHardReset = 0xAB,
    ReceiverSoftReset = 0xAA,
    ReceiverGetFrameCount = 0x21,
    ReceiverGetFrame = 0x22,
    ReceiverRemoveFrame = 0x24,
    ReceiverGetTelemetry = 0x1A,
} ReceiverCommand;

typedef enum {
    TransmitterSoftReset = 0xAA,
    TransmitterSendFrame = 0x10,
    TransmitterGetTelemetry = 0x25,
    TransmitterSetBeacon = 0x14,
    TransmitterClearBeacon = 0x1f,
    TransmitterSetIdleState = 0x24,
    TransmitterSetBitRate = 0x28,
    TransmitterGetState = 0x41
} TransmitterCommand;

static noreturn void CommTask(void* param);

static bool SendCommand(CommObject* object, CommAddress address, uint8_t command)
{
    const I2C_TransferReturn_TypeDef result = object->low.writeProc(address, &command, sizeof(command));
    const bool status = (result == i2cTransferDone);
    if (!status)
    {
        LOGF(LOG_LEVEL_ERROR, "[comm] Unable to send command %d to %d, Reason: %d", command, address, result);
    }

    return status;
}

static bool SendCommandWithResponse(
    CommObject* object, CommAddress address, uint8_t command, uint8_t* outBuffer, uint8_t outBufferSize)
{
    const I2C_TransferReturn_TypeDef result =
        object->low.readProc(address, &command, sizeof(command), outBuffer, outBufferSize);
    const bool status = (result == i2cTransferDone);
    if (!status)
    {
        LOGF(LOG_LEVEL_ERROR, "[comm] Unable to send command %d to %d, Reason: %d", command, address, result);
    }

    return status;
}

void CommInitialize(CommObject* comm, const CommLowInterface* lowerInterface)
{
    memset(comm, 0, sizeof(CommObject));
    comm->low = *lowerInterface;
}

bool CommRestart(CommObject* comm)
{
    if (comm->commTask == NULL)
    {
        const OSResult result = System.CreateTask(CommTask, "COMM Task", 512, comm, 4, &comm->commTask);
        if (result != OSResultSuccess)
{'fraction_non_alphanumeric': 0.11006609049313676, 'fraction_numerical': 0.026436197254702594, 'mean_word_length': 4.465277777777778, 'pattern_counts': {'":': 0, '<': 13, '<?xml version=': 0, '>': 23, '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': '3361246', 'n_tokens_mistral': 1405, 'n_tokens_neox': 1290, 'n_words': 311}