107 lines
4.4 KiB
C++
107 lines
4.4 KiB
C++
#include "mag160c/mag160c.h"
|
|
|
|
#include <cassert>
|
|
#include <cstdint>
|
|
#include <cstring>
|
|
#include <vector>
|
|
|
|
int main() {
|
|
assert(std::strcmp(mag160c_error_name(MAG160C_OK), "MAG160C_OK") == 0);
|
|
assert(std::strcmp(mag160c_error_name(MAG160C_ERR_PROTOCOL_UNKNOWN),
|
|
"MAG160C_ERR_PROTOCOL_UNKNOWN") == 0);
|
|
assert(std::strcmp(mag160c_error_name(static_cast<mag160c_error_t>(9999)),
|
|
"MAG160C_ERR_UNKNOWN_CODE") == 0);
|
|
|
|
assert(mag160c_init(nullptr) == MAG160C_ERR_INVALID_ARGUMENT);
|
|
assert(std::strstr(mag160c_last_error(), "out_ctx") != nullptr);
|
|
|
|
mag160c_context_t* ctx = nullptr;
|
|
assert(mag160c_init(&ctx) == MAG160C_OK);
|
|
assert(ctx != nullptr);
|
|
assert(std::strcmp(mag160c_last_error(), "") == 0);
|
|
|
|
mag160c_shutdown(ctx);
|
|
mag160c_shutdown(nullptr);
|
|
|
|
assert(mag160c_list_devices(nullptr, nullptr, nullptr) == MAG160C_ERR_INVALID_ARGUMENT);
|
|
|
|
ctx = nullptr;
|
|
assert(mag160c_init(&ctx) == MAG160C_OK);
|
|
mag160c_device_info_t* devices = reinterpret_cast<mag160c_device_info_t*>(0x1);
|
|
size_t device_count = 42;
|
|
#if MAG160C_HAS_LIBUSB
|
|
assert(mag160c_list_devices(ctx, &devices, &device_count) == MAG160C_OK);
|
|
mag160c_free_device_list(devices);
|
|
#else
|
|
assert(mag160c_list_devices(ctx, &devices, &device_count) == MAG160C_ERR_UNSUPPORTED);
|
|
assert(devices == nullptr);
|
|
assert(device_count == 0);
|
|
assert(std::strstr(mag160c_last_error(), "libusb") != nullptr);
|
|
mag160c_free_device_list(devices);
|
|
#endif
|
|
mag160c_shutdown(ctx);
|
|
|
|
const uint8_t payload[] = {0x00, 0x05};
|
|
const uint8_t expected[] = {
|
|
0x7e, 0x00, 0x07, 0x85, 0x02, 0x77, 0x00, 0x01, 0x00, 0x05, 0x7f};
|
|
|
|
size_t encoded_size = 0;
|
|
assert(mag160c_tcm_encode_frame(0x02, 0x77, 0x0001, payload, sizeof(payload), nullptr, 0,
|
|
&encoded_size) == MAG160C_ERR_INVALID_ARGUMENT);
|
|
assert(encoded_size == sizeof(expected));
|
|
|
|
uint8_t encoded[sizeof(expected)] = {};
|
|
assert(mag160c_tcm_encode_frame(0x02, 0x77, 0x0001, payload, sizeof(payload), encoded,
|
|
sizeof(encoded), &encoded_size) == MAG160C_OK);
|
|
assert(encoded_size == sizeof(expected));
|
|
assert(std::memcmp(encoded, expected, sizeof(expected)) == 0);
|
|
|
|
uint8_t main_cmd = 0;
|
|
uint8_t sub_cmd = 0;
|
|
uint16_t frame_id = 0;
|
|
size_t payload_size = 0;
|
|
assert(mag160c_tcm_decode_header(encoded, encoded_size, &main_cmd, &sub_cmd, &frame_id,
|
|
&payload_size) == MAG160C_OK);
|
|
assert(main_cmd == 0x02);
|
|
assert(sub_cmd == 0x77);
|
|
assert(frame_id == 0x0001);
|
|
assert(payload_size == sizeof(payload));
|
|
|
|
size_t rotate_size = 0;
|
|
assert(mag160c_tcm_build_rotate_frame(5, nullptr, 0, &rotate_size) ==
|
|
MAG160C_ERR_INVALID_ARGUMENT);
|
|
assert(rotate_size == sizeof(expected));
|
|
|
|
uint8_t rotate_frame[sizeof(expected)] = {};
|
|
assert(mag160c_tcm_build_rotate_frame(5, rotate_frame, sizeof(rotate_frame), &rotate_size) ==
|
|
MAG160C_OK);
|
|
assert(rotate_size == sizeof(expected));
|
|
assert(std::memcmp(rotate_frame, expected, sizeof(expected)) == 0);
|
|
|
|
const uint8_t expected_green_blink[] = {
|
|
0x7e, 0x00, 0x09, 0x87, 0x02, 0x32, 0x00, 0x01, 0x01, 0x00, 0xff, 0x00, 0x35};
|
|
size_t light_size = 0;
|
|
uint8_t light_frame[sizeof(expected_green_blink)] = {};
|
|
assert(mag160c_tcm_build_light_frame(MAG160C_TCM_LIGHT_GREEN, MAG160C_TCM_LIGHT_BLINK,
|
|
light_frame, sizeof(light_frame), &light_size) ==
|
|
MAG160C_OK);
|
|
assert(light_size == sizeof(expected_green_blink));
|
|
assert(std::memcmp(light_frame, expected_green_blink, sizeof(expected_green_blink)) == 0);
|
|
|
|
assert(mag160c_tcm_build_light_frame(static_cast<mag160c_tcm_light_color_t>(99),
|
|
MAG160C_TCM_LIGHT_BLINK, light_frame,
|
|
sizeof(light_frame), &light_size) ==
|
|
MAG160C_ERR_INVALID_ARGUMENT);
|
|
assert(std::strstr(mag160c_last_error(), "color") != nullptr);
|
|
|
|
const std::vector<uint8_t> oversized_payload(65531U, 0xaa);
|
|
encoded_size = 123U;
|
|
assert(mag160c_tcm_encode_frame(0x02, 0x77, 0x0001, oversized_payload.data(),
|
|
oversized_payload.size(), nullptr, 0, &encoded_size) ==
|
|
MAG160C_ERR_INVALID_ARGUMENT);
|
|
assert(encoded_size == 0);
|
|
assert(std::strstr(mag160c_last_error(), "too large") != nullptr);
|
|
|
|
return 0;
|
|
}
|