//
// Created by yelv1 on 24-12-31.
//

#include "rs485_protocol.h"

extern uint8_t g_temperature_uint8[2];

void process_command(uint8_t *cmd, size_t length) {
    char combined_str[3];
    validation_result_t validate = VALIDATION_SUCCESS;

    validate = (validate_package_header(cmd) |
                validate_package_type(cmd) |
                validate_data_length(cmd) |
                validate_package_crc(cmd, length));

    switch (validate) {
    case VALIDATION_SUCCESS:
        // printf("%d", length);
            sprintf(combined_str, "%c%c", cmd[3], cmd[4]);
        if (strcmp(combined_str, "M1") == 0) {
            ultrasonic_distance_report();
        } else if (strcmp(combined_str, "M2") == 0) {
            gd60914_tempture_report();
        } else if (strcmp(combined_str, "M3") == 0)
        {
            printf("%c%c%c%c%c%c", 0xB5, 0xF1, 0x02, 0x6F, 0x6B, 0xCC);
            fwdgt_reset_mcu();
        } else {
            printf("%c%c%c%c%c%c%c", 0xB5, 0xF0, 0x03, 0x65, 0x72, 0x72, 0x3C);
            return;
        }
        break;
    case VALIDATION_CRC_ERROR:
        printf("%c%c%c%c%c%c%c", 0xB5, 0xF1, 0x03, 0x65, 0x72, 0x72, 0x3D);
        break;
    case VALIDATION_HEADER_ERROR:
        printf("%c%c%c%c%c%c%c", 0xB5, 0xF2, 0x03, 0x65, 0x72, 0x72, 0x3E);
        break;
    case VALIDATION_TYPE_ERROR:
        printf("%c%c%c%c%c%c%c", 0xB5, 0xF3, 0x03, 0x65, 0x72, 0x72, 0x3F);
        break;
    case VALIDATION_LENGTH_ERROR:
        printf("%c%c%c%c%c%c%c", 0xB5, 0xF4, 0x03, 0x65, 0x72, 0x72, 0x40);
        break;
    default:
        break;
    }
}

uint8_t calculate_crc(uint8_t data[], uint8_t data_length) {
    uint8_t crc = 0;

    for (uint8_t i = 1; i < data_length - 1; i++) {
        crc += data[i];
    }

    return (uint8_t) (crc & 0xFF);
}

validation_result_t validate_package_crc(uint8_t *data, uint8_t data_length) {
    if (data[data_length - 1] == calculate_crc(data, data_length) && data_length == 3 + data[2] + 1) {
        return VALIDATION_SUCCESS;
    } else {
        return VALIDATION_CRC_ERROR;
    }
}

validation_result_t validate_package_header(uint8_t *data) {
    if (data[0] == PROTOCOL_PACKAGE_HEADER) {
        return VALIDATION_SUCCESS;
    } else {
        return VALIDATION_HEADER_ERROR;
    }
}

validation_result_t validate_package_type(uint8_t *data) {
    if (data[1] == PROTOCOL_BOARD_TYPE) {
        return VALIDATION_SUCCESS;
    } else {
        return VALIDATION_TYPE_ERROR;
    }
}

validation_result_t validate_data_length(uint8_t *data) {
    if (data[2] == PROTOCOL_PACKAGE_LENGTH) {
        return VALIDATION_SUCCESS;
    } else {
        return VALIDATION_LENGTH_ERROR;
    }
}

void gd60914_tempture_report(void) {
    static uint8_t package_header[3] = {0xB5, 0xF0, 0x02};

    uint8_t combined_data[5];
    memcpy(combined_data, package_header, 3);
    memcpy(combined_data + 3, g_temperature_uint8, 2);

    printf("%c%c%c", package_header[0], package_header[1], package_header[2]);
    printf("%c%c", g_temperature_uint8[1], g_temperature_uint8[0]);
    printf("%c", calculate_crc(combined_data, 6));
}

void ultrasonic_distance_report(void) {
    static uint16_t distance_uint16 = 0;
    static uint8_t package_header[3] = {0xB5, 0xF0, 0x02};
    static uint8_t package_data[4] = {0};

    distance_uint16 = ultrasonic_calc_distance();

    package_data[0] = (distance_uint16 >> 8) & 0xFF;
    package_data[1] = distance_uint16 & 0xFF;

    uint8_t combined_data[7];
    memcpy(combined_data, package_header, 3);
    memcpy(combined_data + 3, package_data, 2);

    printf("%c%c%c", package_header[0], package_header[1], package_header[2]);
    printf("%c%c", package_data[0], package_data[1]);
    printf("%c", calculate_crc(combined_data, 6));
}
