286 lines
8.9 KiB
C
286 lines
8.9 KiB
C
#include "midea_protocol.h"
|
|
#include <stdio.h>
|
|
#include <string.h>
|
|
|
|
// Test initialization of control structure
|
|
void test_midea_control_init() {
|
|
midea_control_t control;
|
|
midea_control_init(&control);
|
|
|
|
// Check initial values
|
|
if (control.mode != MODE_OFF) {
|
|
printf("FAIL: test_midea_control_init - Expected mode MODE_OFF, got %d\n", control.mode);
|
|
return;
|
|
}
|
|
|
|
if (control.target_temp != 0) {
|
|
printf("FAIL: test_midea_control_init - Expected target_temp 0, got %d\n", control.target_temp);
|
|
return;
|
|
}
|
|
|
|
if (control.mode_change != 0) {
|
|
printf("FAIL: test_midea_control_init - Expected mode_change 0, got %d\n", control.mode_change);
|
|
return;
|
|
}
|
|
|
|
if (control.temp_change != 0) {
|
|
printf("FAIL: test_midea_control_init - Expected temp_change 0, got %d\n", control.temp_change);
|
|
return;
|
|
}
|
|
|
|
if (control.power_state != 0) {
|
|
printf("FAIL: test_midea_control_init - Expected power_state 0, got %d\n", control.power_state);
|
|
return;
|
|
}
|
|
|
|
printf("PASS: test_midea_control_init\n");
|
|
}
|
|
|
|
// Test setting mode
|
|
void test_midea_control_set_mode() {
|
|
midea_control_t control;
|
|
midea_control_init(&control);
|
|
|
|
midea_control_set_mode(&control, MODE_COOL);
|
|
|
|
if (control.mode != MODE_COOL) {
|
|
printf("FAIL: test_midea_control_set_mode - Expected mode MODE_COOL, got %d\n", control.mode);
|
|
return;
|
|
}
|
|
|
|
if (control.mode_change != 1) {
|
|
printf("FAIL: test_midea_control_set_mode - Expected mode_change 1, got %d\n", control.mode_change);
|
|
return;
|
|
}
|
|
|
|
printf("PASS: test_midea_control_set_mode\n");
|
|
}
|
|
|
|
// Test setting temperature
|
|
void test_midea_control_set_temperature() {
|
|
midea_control_t control;
|
|
midea_control_init(&control);
|
|
|
|
midea_control_set_temperature(&control, 25.5f);
|
|
|
|
// 25.5°C * 100 = 2550
|
|
if (control.target_temp != 2550) {
|
|
printf("FAIL: test_midea_control_set_temperature - Expected target_temp 2550, got %d\n", control.target_temp);
|
|
return;
|
|
}
|
|
|
|
if (control.temp_change != 1) {
|
|
printf("FAIL: test_midea_control_set_temperature - Expected temp_change 1, got %d\n", control.temp_change);
|
|
return;
|
|
}
|
|
|
|
printf("PASS: test_midea_control_set_temperature\n");
|
|
}
|
|
|
|
// Test setting power
|
|
void test_midea_control_set_power() {
|
|
midea_control_t control;
|
|
midea_control_init(&control);
|
|
|
|
midea_control_set_power(&control, true);
|
|
|
|
if (control.power_state != 1) {
|
|
printf("FAIL: test_midea_control_set_power - Expected power_state 1, got %d\n", control.power_state);
|
|
return;
|
|
}
|
|
|
|
midea_control_set_power(&control, false);
|
|
|
|
if (control.power_state != 0) {
|
|
printf("FAIL: test_midea_control_set_power - Expected power_state 0, got %d\n", control.power_state);
|
|
return;
|
|
}
|
|
|
|
printf("PASS: test_midea_control_set_power\n");
|
|
}
|
|
|
|
// Test encoding and decoding roundtrip
|
|
void test_midea_protocol_encode_decode() {
|
|
midea_control_t control;
|
|
midea_control_init(&control);
|
|
|
|
// Set up a control command
|
|
midea_control_set_mode(&control, MODE_COOL);
|
|
midea_control_set_temperature(&control, 24.0f);
|
|
midea_control_set_power(&control, true);
|
|
control.mode_change = 1;
|
|
control.temp_change = 1;
|
|
|
|
uint8_t buffer[50];
|
|
size_t encoded_len = midea_protocol_encode(&control, buffer, sizeof(buffer));
|
|
|
|
printf("Encoded %zu bytes: ", encoded_len);
|
|
for (size_t i = 0; i < encoded_len; i++) {
|
|
printf("%02X ", buffer[i]);
|
|
}
|
|
printf("\n");
|
|
|
|
if (encoded_len < 7) { // Minimum packet size
|
|
printf("FAIL: test_midea_protocol_encode_decode - Encoded length too small: %zu\n", encoded_len);
|
|
return;
|
|
}
|
|
|
|
// Check header
|
|
if (buffer[0] != 0xAA || buffer[1] != 0x55) {
|
|
printf("FAIL: test_midea_protocol_encode_decode - Invalid header: 0x%02X 0x%02X\n", buffer[0], buffer[1]);
|
|
return;
|
|
}
|
|
|
|
// Decode the message
|
|
midea_status_t status;
|
|
if (!midea_protocol_decode(buffer, encoded_len, &status)) {
|
|
printf("FAIL: test_midea_protocol_encode_decode - Failed to decode message\n");
|
|
return;
|
|
}
|
|
|
|
// Check decoded values
|
|
if (status.mode != MODE_COOL) {
|
|
printf("FAIL: test_midea_protocol_encode_decode - Expected mode MODE_COOL, got %d\n", status.mode);
|
|
return;
|
|
}
|
|
|
|
if (status.power_state != 1) {
|
|
printf("FAIL: test_midea_protocol_encode_decode - Expected power_state 1, got %d\n", status.power_state);
|
|
return;
|
|
}
|
|
|
|
// 24.0°C * 100 = 2400
|
|
if (status.target_temp != 2400) {
|
|
printf("FAIL: test_midea_protocol_encode_decode - Expected target_temp 2400, got %d\n", status.target_temp);
|
|
return;
|
|
}
|
|
|
|
printf("PASS: test_midea_protocol_encode_decode\n");
|
|
}
|
|
|
|
// Test encoding with different modes
|
|
void test_midea_protocol_modes() {
|
|
const midea_mode_t modes[] = {MODE_OFF, MODE_COOL, MODE_HEAT, MODE_AUTO, MODE_DRY, MODE_FAN, MODE_SLEEP, MODE_TURBO};
|
|
const char* mode_names[] = {"OFF", "COOL", "HEAT", "AUTO", "DRY", "FAN", "SLEEP", "TURBO"};
|
|
|
|
for (int i = 0; i < 8; i++) {
|
|
midea_control_t control;
|
|
midea_control_init(&control);
|
|
|
|
midea_control_set_mode(&control, modes[i]);
|
|
midea_control_set_temperature(&control, 22.0f);
|
|
midea_control_set_power(&control, true);
|
|
|
|
uint8_t buffer[50];
|
|
size_t encoded_len = midea_protocol_encode(&control, buffer, sizeof(buffer));
|
|
|
|
printf("Mode %s: Encoded %zu bytes: ", mode_names[i], encoded_len);
|
|
for (size_t j = 0; j < encoded_len; j++) {
|
|
printf("%02X ", buffer[j]);
|
|
}
|
|
printf("\n");
|
|
|
|
if (encoded_len < 7) {
|
|
printf("FAIL: test_midea_protocol_modes - Mode %s failed to encode\n", mode_names[i]);
|
|
return;
|
|
}
|
|
|
|
midea_status_t status;
|
|
if (!midea_protocol_decode(buffer, encoded_len, &status)) {
|
|
printf("FAIL: test_midea_protocol_modes - Mode %s failed to decode\n", mode_names[i]);
|
|
return;
|
|
}
|
|
|
|
if (status.mode != modes[i]) {
|
|
printf("FAIL: test_midea_protocol_modes - Mode %s: expected %d, got %d\n", mode_names[i], modes[i], status.mode);
|
|
return;
|
|
}
|
|
}
|
|
|
|
printf("PASS: test_midea_protocol_modes\n");
|
|
}
|
|
|
|
// Test temperature encoding precision
|
|
void test_midea_protocol_temperature_precision() {
|
|
float test_temps[] = {16.0f, 16.5f, 22.0f, 25.5f, 30.0f};
|
|
int16_t expected_values[] = {1600, 1650, 2200, 2550, 3000}; // * 100
|
|
|
|
for (int i = 0; i < 5; i++) {
|
|
midea_control_t control;
|
|
midea_control_init(&control);
|
|
|
|
midea_control_set_temperature(&control, test_temps[i]);
|
|
|
|
if (control.target_temp != expected_values[i]) {
|
|
printf("FAIL: test_midea_protocol_temperature_precision - Temp %.1fC: expected %d, got %d\n",
|
|
test_temps[i], expected_values[i], control.target_temp);
|
|
return;
|
|
}
|
|
}
|
|
|
|
printf("PASS: test_midea_protocol_temperature_precision\n");
|
|
}
|
|
|
|
// Test null pointer handling
|
|
void test_midea_protocol_null_pointers() {
|
|
// Test encoding with NULL control
|
|
size_t len = midea_protocol_encode(NULL, NULL, 0);
|
|
if (len != 0) {
|
|
printf("FAIL: test_midea_protocol_null_pointers - Encoding with NULL control should return 0, got %zu\n", len);
|
|
return;
|
|
}
|
|
|
|
// Test decoding with NULL buffer
|
|
midea_status_t status;
|
|
bool result = midea_protocol_decode(NULL, 10, &status);
|
|
if (result) {
|
|
printf("FAIL: test_midea_protocol_null_pointers - Decoding with NULL buffer should return false\n");
|
|
return;
|
|
}
|
|
|
|
// Test decoding with NULL status
|
|
uint8_t dummy_buffer[10] = {0};
|
|
result = midea_protocol_decode(dummy_buffer, 10, NULL);
|
|
if (result) {
|
|
printf("FAIL: test_midea_protocol_null_pointers - Decoding with NULL status should return false\n");
|
|
return;
|
|
}
|
|
|
|
printf("PASS: test_midea_protocol_null_pointers\n");
|
|
}
|
|
|
|
// Test buffer size limits
|
|
void test_midea_protocol_buffer_limits() {
|
|
midea_control_t control;
|
|
midea_control_init(&control);
|
|
midea_control_set_mode(&control, MODE_COOL);
|
|
midea_control_set_temperature(&control, 25.0f);
|
|
|
|
// Test with too small buffer
|
|
uint8_t small_buffer[5];
|
|
size_t len = midea_protocol_encode(&control, small_buffer, sizeof(small_buffer));
|
|
if (len != 0) {
|
|
printf("FAIL: test_midea_protocol_buffer_limits - Encoding with too small buffer should return 0, got %zu\n", len);
|
|
return;
|
|
}
|
|
|
|
printf("PASS: test_midea_protocol_buffer_limits\n");
|
|
}
|
|
|
|
// Main test runner
|
|
int main() {
|
|
printf("Running MideaUART Protocol Tests...\n\n");
|
|
|
|
test_midea_control_init();
|
|
test_midea_control_set_mode();
|
|
test_midea_control_set_temperature();
|
|
test_midea_control_set_power();
|
|
test_midea_protocol_encode_decode();
|
|
test_midea_protocol_modes();
|
|
test_midea_protocol_temperature_precision();
|
|
test_midea_protocol_null_pointers();
|
|
test_midea_protocol_buffer_limits();
|
|
|
|
printf("\nAll tests completed!\n");
|
|
return 0;
|
|
} |