mirror of
https://github.com/intrepidcs/libicsneo.git
synced 2026-08-05 01:18:36 +02:00
Device: Add ExtendedCommand::GetAllMACAddresses
This commit is contained in:
committed by
Kyle Schwarz
parent
49e578a657
commit
3ab06199c3
@@ -15,6 +15,7 @@ option(LIBICSNEO_BUILD_C2_ETHERNET_RECEIVE_EXAMPLE "Build the C2 ethernet receiv
|
||||
option(LIBICSNEO_BUILD_C2_T1S_LOOPBACK_EXAMPLE "Build the C2 RAD-Comet3 T1S loopback example." ON)
|
||||
option(LIBICSNEO_BUILD_C2_TC10_EXAMPLE "Build the C2 TC10 example." ON)
|
||||
option(LIBICSNEO_BUILD_CPP_SIMPLE_EXAMPLE "Build the simple C++ example." ON)
|
||||
option(LIBICSNEO_BUILD_CPP_DEVICE_INFO_EXAMPLE "Build the C++ device info example." ON)
|
||||
option(LIBICSNEO_BUILD_CPP_INTERACTIVE_EXAMPLE "Build the command-line interactive C++ example." ON)
|
||||
option(LIBICSNEO_BUILD_CPP_A2B_EXAMPLE "Build the A2B example." ON)
|
||||
option(LIBICSNEO_BUILD_CPP_LIN_EXAMPLE "Build the LIN example." ON)
|
||||
@@ -101,6 +102,10 @@ if(LIBICSNEO_BUILD_CPP_SIMPLE_EXAMPLE)
|
||||
add_subdirectory(cpp/simple)
|
||||
endif()
|
||||
|
||||
if(LIBICSNEO_BUILD_CPP_DEVICE_INFO_EXAMPLE)
|
||||
add_subdirectory(cpp/device_info)
|
||||
endif()
|
||||
|
||||
if(LIBICSNEO_BUILD_CPP_INTERACTIVE_EXAMPLE)
|
||||
add_subdirectory(cpp/interactive)
|
||||
endif()
|
||||
|
||||
@@ -102,19 +102,31 @@ int main() {
|
||||
print_error_code("Failed to get PCB serial (device may not support it)", res);
|
||||
}
|
||||
|
||||
/* ===== MAC Address ===== */
|
||||
uint8_t mac[6] = {0};
|
||||
size_t mac_len = sizeof(mac);
|
||||
res = icsneoc2_device_mac_address_get(device, mac, &mac_len);
|
||||
/* ===== MAC Addresses ===== */
|
||||
icsneoc2_mac_addr_entry_t* macs = NULL;
|
||||
res = icsneoc2_device_mac_addresses_enumerate(device, &macs);
|
||||
if(res == icsneoc2_error_success) {
|
||||
printf("MAC: ");
|
||||
for(size_t i = 0; i < mac_len; i++) {
|
||||
if(i > 0) printf(":");
|
||||
printf("%02X", mac[i]);
|
||||
uint16_t mac_count = 0;
|
||||
for(icsneoc2_mac_addr_entry_t* cur = macs; cur; cur = icsneoc2_mac_addresses_next(cur)) {
|
||||
mac_count++;
|
||||
}
|
||||
printf("\n");
|
||||
printf("MACs: %u entr%s\n", mac_count, mac_count == 1 ? "y" : "ies");
|
||||
for(icsneoc2_mac_addr_entry_t* cur = macs; cur; cur = icsneoc2_mac_addresses_next(cur)) {
|
||||
_icsneoc2_netid_t network_id;
|
||||
icsneoc2_mac_network_id_get(cur, &network_id);
|
||||
printf(" Network %-5u ", (unsigned)network_id);
|
||||
uint8_t address[6];
|
||||
size_t address_size = 6;
|
||||
icsneoc2_mac_address_get(cur, address, &address_size);
|
||||
for(int j = 0; j < 6; j++) {
|
||||
if(j > 0) printf(":");
|
||||
printf("%02X", (unsigned)address[j]);
|
||||
}
|
||||
printf("\n");
|
||||
}
|
||||
icsneoc2_mac_addresses_free(macs);
|
||||
} else {
|
||||
print_error_code("Failed to get MAC address (device may not support it)", res);
|
||||
print_error_code("Failed to get MAC addresses (device may not support it)", res);
|
||||
}
|
||||
|
||||
/* Cleanup */
|
||||
|
||||
@@ -0,0 +1,2 @@
|
||||
add_executable(libicsneocpp-device-info-example src/DeviceInfoExample.cpp)
|
||||
target_link_libraries(libicsneocpp-device-info-example icsneocpp)
|
||||
@@ -0,0 +1,80 @@
|
||||
#include <iostream>
|
||||
#include <iomanip>
|
||||
#include <sstream>
|
||||
|
||||
#include "icsneo/icsneocpp.h"
|
||||
|
||||
static std::string formatMAC(const std::array<uint8_t, icsneo::MACAddressLength> addr) {
|
||||
std::ostringstream oss;
|
||||
for(size_t i = 0; i < icsneo::MACAddressLength; i++) {
|
||||
if(i > 0) {
|
||||
oss << ':';
|
||||
}
|
||||
oss << std::hex << std::uppercase << std::setw(2) << std::setfill('0') << static_cast<unsigned>(addr[i]);
|
||||
}
|
||||
return oss.str();
|
||||
}
|
||||
|
||||
int main() {
|
||||
// ===== Device Selection =====
|
||||
std::cout << "Searching for devices...\n";
|
||||
auto devices = icsneo::FindAllDevices();
|
||||
|
||||
if(devices.empty()) {
|
||||
std::cout << "No devices found.\n";
|
||||
return 1;
|
||||
}
|
||||
|
||||
for(size_t i = 0; i < devices.size(); i++) {
|
||||
std::cout << " [" << (i + 1) << "] " << devices[i]->describe() << "\n";
|
||||
}
|
||||
|
||||
int choice;
|
||||
std::cout << "Select device (1-" << devices.size() << "): ";
|
||||
if(!(std::cin >> choice) || choice < 1 || choice > (int)devices.size()) {
|
||||
std::cout << "Invalid selection.\n";
|
||||
return 1;
|
||||
}
|
||||
|
||||
auto& device = devices[choice - 1];
|
||||
|
||||
std::cout << "\nOpening " << device->describe() << "... ";
|
||||
if(!device->open()) {
|
||||
std::cout << "FAIL\n" << icsneo::GetLastError() << "\n";
|
||||
return 1;
|
||||
}
|
||||
std::cout << "OK\n\n";
|
||||
|
||||
// ===== Serial Number =====
|
||||
std::cout << "Serial: " << device->getSerial() << "\n";
|
||||
|
||||
// ===== PCB Serial Number =====
|
||||
auto pcbSerial = device->getPCBSerial();
|
||||
if(pcbSerial.has_value()) {
|
||||
std::cout << "PCB Serial: ";
|
||||
for(auto b : *pcbSerial)
|
||||
std::cout << (char)b;
|
||||
std::cout << "\n";
|
||||
} else {
|
||||
std::cout << "PCB Serial: Not supported\n";
|
||||
}
|
||||
|
||||
auto macs = device->getMACAddresses();
|
||||
if(!macs.empty()) {
|
||||
std::cout << "MACs: " << macs.size()
|
||||
<< (macs.size() == 1 ? " entry" : " entries") << "\n";
|
||||
for(auto macPair : macs) {
|
||||
std::cout << " " << std::left << std::setw(20)
|
||||
<< icsneo::Network::GetNetIDString(static_cast<icsneo::Network::NetID>(macPair.first))
|
||||
<< " " << formatMAC(macPair.second) << "\n";
|
||||
}
|
||||
} else {
|
||||
std::cout << "MACs: Not supported\n";
|
||||
}
|
||||
|
||||
// ===== Cleanup =====
|
||||
std::cout << "\nClosing device... ";
|
||||
std::cout << (device->close() ? "OK" : "FAIL") << "\n";
|
||||
|
||||
return 0;
|
||||
}
|
||||
Reference in New Issue
Block a user