Features: - Cross-platform support (Windows/Linux/macOS) - CAN and CANFD protocol support - USB communication (WinUSB/libusb) - Device management and configuration - Message transmission and reception - Filter configuration - CMake build system - Comprehensive examples and tests
278 lines
8.6 KiB
C++
278 lines
8.6 KiB
C++
/**
|
|
* @file test_api.cpp
|
|
* @brief 应用接口层测试
|
|
*/
|
|
|
|
#include "../include/api/Usb2Can.h"
|
|
#include "../include/can/CanMessage.h"
|
|
#include "../include/can/CanFdMessage.h"
|
|
#include <iostream>
|
|
#include <cassert>
|
|
#include <cstring>
|
|
|
|
/**
|
|
* @brief 测试USB2CAN API初始化功能
|
|
* @return 测试结果
|
|
*/
|
|
bool test_usb2can_initialization() {
|
|
std::cout << " [TEST] Testing USB2CAN API initialization..." << std::endl;
|
|
|
|
// 创建USB2CAN实例
|
|
usb2can::api::Usb2Can usb2can;
|
|
|
|
// 初始化
|
|
if (!usb2can.initialize()) {
|
|
std::cout << " [FAIL] Failed to initialize USB2CAN API" << std::endl;
|
|
return false;
|
|
}
|
|
|
|
std::cout << " [PASS] USB2CAN API initialized successfully" << std::endl;
|
|
|
|
return true;
|
|
}
|
|
|
|
/**
|
|
* @brief 测试设备扫描功能
|
|
* @return 测试结果
|
|
*/
|
|
bool test_device_scanning() {
|
|
std::cout << " [TEST] Testing device scanning..." << std::endl;
|
|
|
|
// 创建USB2CAN实例
|
|
usb2can::api::Usb2Can usb2can;
|
|
|
|
// 初始化
|
|
if (!usb2can.initialize()) {
|
|
std::cout << " [FAIL] Failed to initialize USB2CAN API" << std::endl;
|
|
return false;
|
|
}
|
|
|
|
// 扫描CAN设备
|
|
std::vector<usb2can::device::Usb2CanDeviceInfo> canDevices = usb2can.scanDevices(usb2can::device::DeviceType::CAN);
|
|
std::cout << " [INFO] Found " << canDevices.size() << " CAN devices" << std::endl;
|
|
|
|
// 扫描CANFD设备
|
|
std::vector<usb2can::device::Usb2CanDeviceInfo> canFdDevices = usb2can.scanDevices(usb2can::device::DeviceType::CANFD);
|
|
std::cout << " [INFO] Found " << canFdDevices.size() << " CANFD devices" << std::endl;
|
|
|
|
return true;
|
|
}
|
|
|
|
/**
|
|
* @brief 测试设备打开和关闭功能
|
|
* @return 测试结果
|
|
*/
|
|
bool test_device_open_close() {
|
|
std::cout << " [TEST] Testing device open/close..." << std::endl;
|
|
|
|
// 创建USB2CAN实例
|
|
usb2can::api::Usb2Can usb2can;
|
|
|
|
// 初始化
|
|
if (!usb2can.initialize()) {
|
|
std::cout << " [FAIL] Failed to initialize USB2CAN API" << std::endl;
|
|
return false;
|
|
}
|
|
|
|
// 扫描CAN设备
|
|
std::vector<usb2can::device::Usb2CanDeviceInfo> devices = usb2can.scanDevices(usb2can::device::DeviceType::CAN);
|
|
|
|
if (devices.empty()) {
|
|
std::cout << " [WARN] No CAN devices found, skipping open/close test" << std::endl;
|
|
return true;
|
|
}
|
|
|
|
// 打开第一个设备
|
|
usb2can::device::Usb2CanDevice* device = usb2can.openDevice(devices[0].devicePath, usb2can::device::DeviceType::CAN);
|
|
|
|
if (!device) {
|
|
std::cout << " [FAIL] Failed to open device" << std::endl;
|
|
return false;
|
|
}
|
|
|
|
std::cout << " [PASS] Device opened successfully" << std::endl;
|
|
|
|
// 关闭设备
|
|
usb2can.closeDevice(devices[0].devicePath);
|
|
std::cout << " [PASS] Device closed successfully" << std::endl;
|
|
|
|
return true;
|
|
}
|
|
|
|
/**
|
|
* @brief 测试CAN消息发送和接收功能
|
|
* @return 测试结果
|
|
*/
|
|
bool test_can_message_send_receive() {
|
|
std::cout << " [TEST] Testing CAN message send/receive..." << std::endl;
|
|
|
|
// 创建USB2CAN实例
|
|
usb2can::api::Usb2Can usb2can;
|
|
|
|
// 初始化
|
|
if (!usb2can.initialize()) {
|
|
std::cout << " [FAIL] Failed to initialize USB2CAN API" << std::endl;
|
|
return false;
|
|
}
|
|
|
|
// 扫描CAN设备
|
|
std::vector<usb2can::device::Usb2CanDeviceInfo> devices = usb2can.scanDevices(usb2can::device::DeviceType::CAN);
|
|
|
|
if (devices.empty()) {
|
|
std::cout << " [WARN] No CAN devices found, skipping send/receive test" << std::endl;
|
|
return true;
|
|
}
|
|
|
|
// 打开第一个设备
|
|
usb2can::device::Usb2CanDevice* device = usb2can.openDevice(devices[0].devicePath, usb2can::device::DeviceType::CAN);
|
|
|
|
if (!device) {
|
|
std::cout << " [FAIL] Failed to open device" << std::endl;
|
|
return false;
|
|
}
|
|
|
|
// 创建CAN消息
|
|
usb2can::can::CanMessage message;
|
|
message.id = 0x123;
|
|
message.dlc = 8;
|
|
message.extended = true;
|
|
message.rtr = false;
|
|
|
|
// 设置数据
|
|
uint8_t data[8] = {0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08};
|
|
std::memcpy(message.data, data, sizeof(data));
|
|
|
|
// 发送消息
|
|
bool sendResult = usb2can.sendCanMessage(devices[0].devicePath, message);
|
|
std::cout << " [INFO] Send result: " << (sendResult ? "Success" : "Failed") << std::endl;
|
|
|
|
// 接收消息
|
|
usb2can::can::CanMessage receivedMessage;
|
|
bool receiveResult = usb2can.receiveCanMessage(devices[0].devicePath, receivedMessage, 1000);
|
|
std::cout << " [INFO] Receive result: " << (receiveResult ? "Success" : "Failed") << std::endl;
|
|
|
|
if (receiveResult) {
|
|
std::cout << " [INFO] Received message: " << receivedMessage.toString() << std::endl;
|
|
}
|
|
|
|
// 关闭设备
|
|
usb2can.closeDevice(devices[0].devicePath);
|
|
|
|
return true;
|
|
}
|
|
|
|
/**
|
|
* @brief 测试CAN配置设置和获取功能
|
|
* @return 测试结果
|
|
*/
|
|
bool test_can_config_set_get() {
|
|
std::cout << " [TEST] Testing CAN config set/get..." << std::endl;
|
|
|
|
// 创建USB2CAN实例
|
|
usb2can::api::Usb2Can usb2can;
|
|
|
|
// 初始化
|
|
if (!usb2can.initialize()) {
|
|
std::cout << " [FAIL] Failed to initialize USB2CAN API" << std::endl;
|
|
return false;
|
|
}
|
|
|
|
// 扫描CAN设备
|
|
std::vector<usb2can::device::Usb2CanDeviceInfo> devices = usb2can.scanDevices(usb2can::device::DeviceType::CAN);
|
|
|
|
if (devices.empty()) {
|
|
std::cout << " [WARN] No CAN devices found, skipping config test" << std::endl;
|
|
return true;
|
|
}
|
|
|
|
// 打开第一个设备
|
|
usb2can::device::Usb2CanDevice* device = usb2can.openDevice(devices[0].devicePath, usb2can::device::DeviceType::CAN);
|
|
|
|
if (!device) {
|
|
std::cout << " [FAIL] Failed to open device" << std::endl;
|
|
return false;
|
|
}
|
|
|
|
// 创建CAN配置
|
|
usb2can::can::CanConfig config;
|
|
config.bitrate = 500000; // 500kbps
|
|
config.mode = usb2can::can::CanMode::Normal;
|
|
config.filterId = 0x123;
|
|
config.filterMask = 0x7FF;
|
|
|
|
// 设置配置
|
|
bool setResult = usb2can.setCanConfig(devices[0].devicePath, config);
|
|
std::cout << " [INFO] Set config result: " << (setResult ? "Success" : "Failed") << std::endl;
|
|
|
|
// 获取配置
|
|
usb2can::can::CanConfig retrievedConfig;
|
|
bool getResult = usb2can.getCanConfig(devices[0].devicePath, retrievedConfig);
|
|
std::cout << " [INFO] Get config result: " << (getResult ? "Success" : "Failed") << std::endl;
|
|
|
|
if (getResult) {
|
|
std::cout << " [INFO] Retrieved config - Bitrate: " << retrievedConfig.bitrate
|
|
<< ", Mode: " << static_cast<int>(retrievedConfig.mode)
|
|
<< ", Filter ID: 0x" << std::hex << retrievedConfig.filterId
|
|
<< ", Filter Mask: 0x" << retrievedConfig.filterMask << std::dec << std::endl;
|
|
}
|
|
|
|
// 关闭设备
|
|
usb2can.closeDevice(devices[0].devicePath);
|
|
|
|
return true;
|
|
}
|
|
|
|
/**
|
|
* @brief 测试热插拔回调功能
|
|
* @return 测试结果
|
|
*/
|
|
bool test_hotplug_callback() {
|
|
std::cout << " [TEST] Testing hotplug callback..." << std::endl;
|
|
|
|
// 创建USB2CAN实例
|
|
usb2can::api::Usb2Can usb2can;
|
|
|
|
// 初始化
|
|
if (!usb2can.initialize()) {
|
|
std::cout << " [FAIL] Failed to initialize USB2CAN API" << std::endl;
|
|
return false;
|
|
}
|
|
|
|
// 定义热插拔回调函数
|
|
auto callback = [](usb2can::device::HotplugEvent event, const std::string& devicePath, void* userData) {
|
|
std::cout << " [INFO] Hotplug event: " << static_cast<int>(event)
|
|
<< ", Device: " << devicePath << std::endl;
|
|
};
|
|
|
|
// 注册热插拔回调
|
|
bool registerResult = usb2can.registerHotplugCallback(callback, nullptr);
|
|
std::cout << " [INFO] Register hotplug callback result: " << (registerResult ? "Success" : "Failed") << std::endl;
|
|
|
|
// 注销热插拔回调
|
|
bool unregisterResult = usb2can.unregisterHotplugCallback();
|
|
std::cout << " [INFO] Unregister hotplug callback result: " << (unregisterResult ? "Success" : "Failed") << std::endl;
|
|
|
|
return true;
|
|
}
|
|
|
|
/**
|
|
* @brief 测试应用接口层
|
|
* @return 测试结果
|
|
*/
|
|
bool test_api() {
|
|
std::cout << "\n[TEST] Starting API layer tests..." << std::endl;
|
|
|
|
bool all_passed = true;
|
|
|
|
// 运行测试
|
|
all_passed &= test_usb2can_initialization();
|
|
all_passed &= test_device_scanning();
|
|
all_passed &= test_device_open_close();
|
|
all_passed &= test_can_message_send_receive();
|
|
all_passed &= test_can_config_set_get();
|
|
all_passed &= test_hotplug_callback();
|
|
|
|
std::cout << "[TEST] API layer tests " << (all_passed ? "PASSED" : "FAILED") << std::endl;
|
|
|
|
return all_passed;
|
|
} |