164 lines
4.8 KiB
C++
164 lines
4.8 KiB
C++
|
|
/**
|
||
|
|
* @file test_device.cpp
|
||
|
|
* @brief 设备管理层测试
|
||
|
|
*/
|
||
|
|
|
||
|
|
#include "../include/device/Usb2CanDevice.h"
|
||
|
|
#include "../include/device/DeviceManager.h"
|
||
|
|
#include "../include/platform/Platform.h"
|
||
|
|
#include <iostream>
|
||
|
|
#include <cassert>
|
||
|
|
|
||
|
|
/**
|
||
|
|
* @brief 测试USB2CAN设备创建功能
|
||
|
|
* @return 测试结果
|
||
|
|
*/
|
||
|
|
bool test_usb2can_device_creation() {
|
||
|
|
std::cout << " [TEST] Testing USB2CAN device creation..." << std::endl;
|
||
|
|
|
||
|
|
// 创建平台实例
|
||
|
|
usb2can::platform::Platform* platform = usb2can::platform::Platform::createPlatform();
|
||
|
|
if (!platform) {
|
||
|
|
std::cout << " [FAIL] Failed to create platform instance" << std::endl;
|
||
|
|
return false;
|
||
|
|
}
|
||
|
|
|
||
|
|
// 创建USB2CAN设备实例
|
||
|
|
usb2can::device::Usb2CanDevice* device = new usb2can::device::Usb2CanDevice(platform);
|
||
|
|
|
||
|
|
// 初始化设备
|
||
|
|
if (!device->initialize(usb2can::device::DeviceType::CAN)) {
|
||
|
|
std::cout << " [FAIL] Failed to initialize USB2CAN device" << std::endl;
|
||
|
|
delete device;
|
||
|
|
delete platform;
|
||
|
|
return false;
|
||
|
|
}
|
||
|
|
|
||
|
|
std::cout << " [PASS] USB2CAN device created and initialized successfully" << std::endl;
|
||
|
|
|
||
|
|
// 清理资源
|
||
|
|
delete device;
|
||
|
|
delete platform;
|
||
|
|
|
||
|
|
return true;
|
||
|
|
}
|
||
|
|
|
||
|
|
/**
|
||
|
|
* @brief 测试设备管理器创建功能
|
||
|
|
* @return 测试结果
|
||
|
|
*/
|
||
|
|
bool test_device_manager_creation() {
|
||
|
|
std::cout << " [TEST] Testing device manager creation..." << std::endl;
|
||
|
|
|
||
|
|
// 创建设备管理器实例
|
||
|
|
usb2can::device::DeviceManager* manager = new usb2can::device::DeviceManager();
|
||
|
|
|
||
|
|
if (!manager) {
|
||
|
|
std::cout << " [FAIL] Failed to create device manager instance" << std::endl;
|
||
|
|
return false;
|
||
|
|
}
|
||
|
|
|
||
|
|
std::cout << " [PASS] Device manager created successfully" << std::endl;
|
||
|
|
|
||
|
|
// 清理资源
|
||
|
|
delete manager;
|
||
|
|
|
||
|
|
return true;
|
||
|
|
}
|
||
|
|
|
||
|
|
/**
|
||
|
|
* @brief 测试设备扫描功能
|
||
|
|
* @return 测试结果
|
||
|
|
*/
|
||
|
|
bool test_device_scan() {
|
||
|
|
std::cout << " [TEST] Testing device scanning..." << std::endl;
|
||
|
|
|
||
|
|
// 创建设备管理器实例
|
||
|
|
usb2can::device::DeviceManager manager;
|
||
|
|
|
||
|
|
// 扫描设备
|
||
|
|
std::vector<usb2can::device::Usb2CanDeviceInfo> devices = manager.scanDevices(usb2can::device::DeviceType::CAN);
|
||
|
|
std::cout << " [INFO] Found " << devices.size() << " CAN devices" << std::endl;
|
||
|
|
|
||
|
|
// 扫描CANFD设备
|
||
|
|
std::vector<usb2can::device::Usb2CanDeviceInfo> fdDevices = manager.scanDevices(usb2can::device::DeviceType::CANFD);
|
||
|
|
std::cout << " [INFO] Found " << fdDevices.size() << " CANFD devices" << std::endl;
|
||
|
|
|
||
|
|
return true;
|
||
|
|
}
|
||
|
|
|
||
|
|
/**
|
||
|
|
* @brief 测试CAN消息发送和接收功能
|
||
|
|
* @return 测试结果
|
||
|
|
*/
|
||
|
|
bool test_can_message_send_receive() {
|
||
|
|
std::cout << " [TEST] Testing CAN message send/receive..." << std::endl;
|
||
|
|
|
||
|
|
// 创建设备管理器实例
|
||
|
|
usb2can::device::DeviceManager manager;
|
||
|
|
|
||
|
|
// 扫描设备
|
||
|
|
std::vector<usb2can::device::Usb2CanDeviceInfo> devices = manager.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 = manager.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 = device->sendCanMessage(message);
|
||
|
|
std::cout << " [INFO] Send result: " << (sendResult ? "Success" : "Failed") << std::endl;
|
||
|
|
|
||
|
|
// 接收消息
|
||
|
|
usb2can::can::CanMessage receivedMessage;
|
||
|
|
bool receiveResult = device->receiveCanMessage(receivedMessage, 1000);
|
||
|
|
std::cout << " [INFO] Receive result: " << (receiveResult ? "Success" : "Failed") << std::endl;
|
||
|
|
|
||
|
|
if (receiveResult) {
|
||
|
|
std::cout << " [INFO] Received message: " << receivedMessage.toString() << std::endl;
|
||
|
|
}
|
||
|
|
|
||
|
|
// 关闭设备
|
||
|
|
manager.closeDevice(device);
|
||
|
|
|
||
|
|
return true;
|
||
|
|
}
|
||
|
|
|
||
|
|
/**
|
||
|
|
* @brief 测试设备管理层
|
||
|
|
* @return 测试结果
|
||
|
|
*/
|
||
|
|
bool test_device() {
|
||
|
|
std::cout << "\n[TEST] Starting device management layer tests..." << std::endl;
|
||
|
|
|
||
|
|
bool all_passed = true;
|
||
|
|
|
||
|
|
// 运行测试
|
||
|
|
all_passed &= test_usb2can_device_creation();
|
||
|
|
all_passed &= test_device_manager_creation();
|
||
|
|
all_passed &= test_device_scan();
|
||
|
|
all_passed &= test_can_message_send_receive();
|
||
|
|
|
||
|
|
std::cout << "[TEST] Device management layer tests " << (all_passed ? "PASSED" : "FAILED") << std::endl;
|
||
|
|
|
||
|
|
return all_passed;
|
||
|
|
}
|