Created
January 14, 2025 05:35
-
-
Save aneury1/b05f4fce28a9f6fe54df23d3b7dc7923 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <iostream> | |
#include <fstream> | |
#include <vector> | |
#include <map> | |
#include <string> | |
#include <dlt/dlt.h> | |
#include "tinyxml2.h" | |
using namespace tinyxml2; | |
// Structure to hold Fibex signal mappings | |
struct SignalDefinition { | |
std::string name; | |
uint32_t serviceId; | |
uint32_t eventId; | |
}; | |
// Map to hold signal definitions | |
std::map<std::pair<uint32_t, uint32_t>, SignalDefinition> signalMap; | |
// Parse Fibex file and populate signalMap | |
void parseFibex(const std::string& filepath) { | |
XMLDocument doc; | |
if (doc.LoadFile(filepath.c_str()) != XML_SUCCESS) { | |
std::cerr << "Failed to load Fibex file: " << filepath << std::endl; | |
return; | |
} | |
XMLElement* root = doc.RootElement(); | |
if (!root) { | |
std::cerr << "Invalid Fibex XML structure." << std::endl; | |
return; | |
} | |
for (XMLElement* serviceElement = root->FirstChildElement("SERVICE"); serviceElement != nullptr; | |
serviceElement = serviceElement->NextSiblingElement("SERVICE")) { | |
uint32_t serviceId = serviceElement->UnsignedAttribute("ID"); | |
for (XMLElement* eventElement = serviceElement->FirstChildElement("EVENT"); eventElement != nullptr; | |
eventElement = eventElement->NextSiblingElement("EVENT")) { | |
uint32_t eventId = eventElement->UnsignedAttribute("ID"); | |
const char* name = eventElement->Attribute("NAME"); | |
if (name) { | |
signalMap[{serviceId, eventId}] = {name, serviceId, eventId}; | |
} | |
} | |
} | |
std::cout << "Fibex parsing completed. Loaded " << signalMap.size() << " signals." << std::endl; | |
} | |
// Decode SOME/IP message | |
void decodeSomeIpMessage(const std::vector<uint8_t>& payload) { | |
if (payload.size() < 16) { // Basic SOME/IP header size | |
std::cerr << "Payload too small for SOME/IP message." << std::endl; | |
return; | |
} | |
uint32_t serviceId = (payload[0] << 8) | payload[1]; | |
uint32_t eventId = (payload[2] << 8) | payload[3]; | |
auto it = signalMap.find({serviceId, eventId}); | |
if (it != signalMap.end()) { | |
const SignalDefinition& signal = it->second; | |
std::cout << "Decoded SOME/IP Message: " << std::endl; | |
std::cout << " Signal Name: " << signal.name << std::endl; | |
std::cout << " Service ID: " << serviceId << std::endl; | |
std::cout << " Event ID: " << eventId << std::endl; | |
} else { | |
std::cout << "Unknown SOME/IP signal (Service ID: " << serviceId | |
<< ", Event ID: " << eventId << ")." << std::endl; | |
} | |
} | |
// Parse DLT file and extract SOME/IP payloads | |
void parseDLTFile(const std::string& dltFilePath) { | |
FILE* dltFile = fopen(dltFilePath.c_str(), "rb"); | |
if (!dltFile) { | |
std::cerr << "Failed to open DLT file: " << dltFilePath << std::endl; | |
return; | |
} | |
char buffer[4096]; | |
while (true) { | |
size_t bytesRead = fread(buffer, 1, sizeof(buffer), dltFile); | |
if (bytesRead == 0) break; | |
DltMessage msg; | |
DltReturnValue ret = dlt_message_parse(reinterpret_cast<unsigned char*>(buffer), bytesRead, &msg); | |
if (ret == DLT_RETURN_OK && msg.payload_size > 0) { | |
std::vector<uint8_t> payload(msg.payload, msg.payload + msg.payload_size); | |
decodeSomeIpMessage(payload); | |
} else if (ret != DLT_RETURN_OK) { | |
std::cerr << "Error parsing DLT message: " << ret << std::endl; | |
} | |
} | |
fclose(dltFile); | |
} | |
int main() { | |
std::string fibexFile = "path_to_fibex.xml"; | |
std::string dltFile = "path_to_dlt_file.dlt"; | |
// Parse the Fibex file | |
parseFibex(fibexFile); | |
// Parse the DLT file and decode SOME/IP messages | |
parseDLTFile(dltFile); | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment