- Implement complete CurrentDemandReq/CurrentDemandRes parsing (24 total fields) - Add enhanced_exi_viewer.c with detailed message analysis - Support -decode option for clean XML output (file-ready format) - Enable ISO1, ISO2, DIN codec support in build configuration - Fix C99 compatibility issues in makefiles (change -ansi to -std=c99) - Create test utilities for hex string to EXI conversion - Generate test files: test3.exi (CurrentDemandRes), test4.exi (CurrentDemandReq) Features: * Dual output modes: detailed analysis (default) vs XML (-decode) * Complete V2G message type detection and parsing * Session ID display in hex and ASCII formats * Voltage/current/power readings with proper units * All optional fields and status flags supported 🤖 Generated with [Claude Code](https://claude.ai/code) Co-Authored-By: Claude <noreply@anthropic.com>
297 lines
17 KiB
C
297 lines
17 KiB
C
#include <stdio.h>
|
|
#include <stdlib.h>
|
|
#include <string.h>
|
|
|
|
/* EXI codec headers */
|
|
#include "iso1EXIDatatypes.h"
|
|
#include "iso1EXIDatatypesDecoder.h"
|
|
#include "ByteStream.h"
|
|
|
|
#define BUFFER_SIZE 4096
|
|
|
|
void print_xml_header() {
|
|
printf("<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n");
|
|
printf("<V2G_Message xmlns=\"urn:iso:15118:2:2013:MsgDef\"\n");
|
|
printf(" xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\">\n");
|
|
}
|
|
|
|
void print_xml_footer() {
|
|
printf("</V2G_Message>\n");
|
|
}
|
|
|
|
void print_session_id_xml(uint8_t *bytes, size_t len) {
|
|
printf(" <SessionID>");
|
|
for(size_t i = 0; i < len; i++) {
|
|
printf("%02X", bytes[i]);
|
|
}
|
|
printf("</SessionID>\n");
|
|
}
|
|
|
|
void print_iso1_xml(struct iso1EXIDocument *doc) {
|
|
if (!doc->V2G_Message_isUsed) return;
|
|
|
|
print_xml_header();
|
|
|
|
// Header
|
|
printf(" <Header>\n");
|
|
print_session_id_xml(doc->V2G_Message.Header.SessionID.bytes,
|
|
doc->V2G_Message.Header.SessionID.bytesLen);
|
|
|
|
if (doc->V2G_Message.Header.Notification_isUsed) {
|
|
printf(" <Notification>\n");
|
|
printf(" <FaultCode>%d</FaultCode>\n", doc->V2G_Message.Header.Notification.FaultCode);
|
|
printf(" <FaultMsg>%s</FaultMsg>\n", doc->V2G_Message.Header.Notification.FaultMsg.characters);
|
|
printf(" </Notification>\n");
|
|
}
|
|
printf(" </Header>\n");
|
|
|
|
// Body
|
|
printf(" <Body>\n");
|
|
|
|
if (doc->V2G_Message.Body.SessionSetupReq_isUsed) {
|
|
printf(" <SessionSetupReq>\n");
|
|
printf(" <EVCCID>");
|
|
for(size_t i = 0; i < doc->V2G_Message.Body.SessionSetupReq.EVCCID.bytesLen; i++) {
|
|
printf("%02X", doc->V2G_Message.Body.SessionSetupReq.EVCCID.bytes[i]);
|
|
}
|
|
printf("</EVCCID>\n");
|
|
printf(" </SessionSetupReq>\n");
|
|
}
|
|
else if (doc->V2G_Message.Body.SessionSetupRes_isUsed) {
|
|
printf(" <SessionSetupRes>\n");
|
|
printf(" <ResponseCode>%d</ResponseCode>\n", doc->V2G_Message.Body.SessionSetupRes.ResponseCode);
|
|
printf(" <EVSEID>%s</EVSEID>\n", doc->V2G_Message.Body.SessionSetupRes.EVSEID.characters);
|
|
if (doc->V2G_Message.Body.SessionSetupRes.EVSETimeStamp_isUsed) {
|
|
printf(" <EVSETimeStamp>%ld</EVSETimeStamp>\n", doc->V2G_Message.Body.SessionSetupRes.EVSETimeStamp);
|
|
}
|
|
printf(" </SessionSetupRes>\n");
|
|
}
|
|
else if (doc->V2G_Message.Body.ServiceDiscoveryReq_isUsed) {
|
|
printf(" <ServiceDiscoveryReq/>\n");
|
|
}
|
|
else if (doc->V2G_Message.Body.ServiceDiscoveryRes_isUsed) {
|
|
printf(" <ServiceDiscoveryRes>\n");
|
|
printf(" <ResponseCode>%d</ResponseCode>\n", doc->V2G_Message.Body.ServiceDiscoveryRes.ResponseCode);
|
|
printf(" </ServiceDiscoveryRes>\n");
|
|
}
|
|
else if (doc->V2G_Message.Body.AuthorizationReq_isUsed) {
|
|
printf(" <AuthorizationReq>\n");
|
|
if (doc->V2G_Message.Body.AuthorizationReq.Id_isUsed) {
|
|
printf(" <Id>%s</Id>\n", doc->V2G_Message.Body.AuthorizationReq.Id.characters);
|
|
}
|
|
if (doc->V2G_Message.Body.AuthorizationReq.GenChallenge_isUsed) {
|
|
printf(" <GenChallenge>");
|
|
for(size_t i = 0; i < doc->V2G_Message.Body.AuthorizationReq.GenChallenge.bytesLen; i++) {
|
|
printf("%02X", doc->V2G_Message.Body.AuthorizationReq.GenChallenge.bytes[i]);
|
|
}
|
|
printf("</GenChallenge>\n");
|
|
}
|
|
printf(" </AuthorizationReq>\n");
|
|
}
|
|
else if (doc->V2G_Message.Body.AuthorizationRes_isUsed) {
|
|
printf(" <AuthorizationRes>\n");
|
|
printf(" <ResponseCode>%d</ResponseCode>\n", doc->V2G_Message.Body.AuthorizationRes.ResponseCode);
|
|
printf(" <EVSEProcessing>%d</EVSEProcessing>\n", doc->V2G_Message.Body.AuthorizationRes.EVSEProcessing);
|
|
printf(" </AuthorizationRes>\n");
|
|
}
|
|
else if (doc->V2G_Message.Body.PreChargeReq_isUsed) {
|
|
printf(" <PreChargeReq>\n");
|
|
printf(" <EVTargetVoltage>\n");
|
|
printf(" <Multiplier>%d</Multiplier>\n", doc->V2G_Message.Body.PreChargeReq.EVTargetVoltage.Multiplier);
|
|
printf(" <Unit>%d</Unit>\n", doc->V2G_Message.Body.PreChargeReq.EVTargetVoltage.Unit);
|
|
printf(" <Value>%d</Value>\n", doc->V2G_Message.Body.PreChargeReq.EVTargetVoltage.Value);
|
|
printf(" </EVTargetVoltage>\n");
|
|
printf(" <EVTargetCurrent>\n");
|
|
printf(" <Multiplier>%d</Multiplier>\n", doc->V2G_Message.Body.PreChargeReq.EVTargetCurrent.Multiplier);
|
|
printf(" <Unit>%d</Unit>\n", doc->V2G_Message.Body.PreChargeReq.EVTargetCurrent.Unit);
|
|
printf(" <Value>%d</Value>\n", doc->V2G_Message.Body.PreChargeReq.EVTargetCurrent.Value);
|
|
printf(" </EVTargetCurrent>\n");
|
|
printf(" </PreChargeReq>\n");
|
|
}
|
|
else if (doc->V2G_Message.Body.PreChargeRes_isUsed) {
|
|
printf(" <PreChargeRes>\n");
|
|
printf(" <ResponseCode>%d</ResponseCode>\n", doc->V2G_Message.Body.PreChargeRes.ResponseCode);
|
|
printf(" </PreChargeRes>\n");
|
|
}
|
|
else if (doc->V2G_Message.Body.PowerDeliveryReq_isUsed) {
|
|
printf(" <PowerDeliveryReq>\n");
|
|
printf(" <ChargeProgress>%d</ChargeProgress>\n", doc->V2G_Message.Body.PowerDeliveryReq.ChargeProgress);
|
|
printf(" <SAScheduleTupleID>%d</SAScheduleTupleID>\n", doc->V2G_Message.Body.PowerDeliveryReq.SAScheduleTupleID);
|
|
printf(" </PowerDeliveryReq>\n");
|
|
}
|
|
else if (doc->V2G_Message.Body.PowerDeliveryRes_isUsed) {
|
|
printf(" <PowerDeliveryRes>\n");
|
|
printf(" <ResponseCode>%d</ResponseCode>\n", doc->V2G_Message.Body.PowerDeliveryRes.ResponseCode);
|
|
printf(" </PowerDeliveryRes>\n");
|
|
}
|
|
else if (doc->V2G_Message.Body.CurrentDemandReq_isUsed) {
|
|
printf(" <CurrentDemandReq>\n");
|
|
printf(" <EVTargetCurrent>\n");
|
|
printf(" <Multiplier>%d</Multiplier>\n", doc->V2G_Message.Body.CurrentDemandReq.EVTargetCurrent.Multiplier);
|
|
printf(" <Unit>%d</Unit>\n", doc->V2G_Message.Body.CurrentDemandReq.EVTargetCurrent.Unit);
|
|
printf(" <Value>%d</Value>\n", doc->V2G_Message.Body.CurrentDemandReq.EVTargetCurrent.Value);
|
|
printf(" </EVTargetCurrent>\n");
|
|
printf(" <EVTargetVoltage>\n");
|
|
printf(" <Multiplier>%d</Multiplier>\n", doc->V2G_Message.Body.CurrentDemandReq.EVTargetVoltage.Multiplier);
|
|
printf(" <Unit>%d</Unit>\n", doc->V2G_Message.Body.CurrentDemandReq.EVTargetVoltage.Unit);
|
|
printf(" <Value>%d</Value>\n", doc->V2G_Message.Body.CurrentDemandReq.EVTargetVoltage.Value);
|
|
printf(" </EVTargetVoltage>\n");
|
|
if (doc->V2G_Message.Body.CurrentDemandReq.EVMaximumVoltageLimit_isUsed) {
|
|
printf(" <EVMaximumVoltageLimit>\n");
|
|
printf(" <Multiplier>%d</Multiplier>\n", doc->V2G_Message.Body.CurrentDemandReq.EVMaximumVoltageLimit.Multiplier);
|
|
printf(" <Unit>%d</Unit>\n", doc->V2G_Message.Body.CurrentDemandReq.EVMaximumVoltageLimit.Unit);
|
|
printf(" <Value>%d</Value>\n", doc->V2G_Message.Body.CurrentDemandReq.EVMaximumVoltageLimit.Value);
|
|
printf(" </EVMaximumVoltageLimit>\n");
|
|
}
|
|
if (doc->V2G_Message.Body.CurrentDemandReq.EVMaximumCurrentLimit_isUsed) {
|
|
printf(" <EVMaximumCurrentLimit>\n");
|
|
printf(" <Multiplier>%d</Multiplier>\n", doc->V2G_Message.Body.CurrentDemandReq.EVMaximumCurrentLimit.Multiplier);
|
|
printf(" <Unit>%d</Unit>\n", doc->V2G_Message.Body.CurrentDemandReq.EVMaximumCurrentLimit.Unit);
|
|
printf(" <Value>%d</Value>\n", doc->V2G_Message.Body.CurrentDemandReq.EVMaximumCurrentLimit.Value);
|
|
printf(" </EVMaximumCurrentLimit>\n");
|
|
}
|
|
if (doc->V2G_Message.Body.CurrentDemandReq.EVMaximumPowerLimit_isUsed) {
|
|
printf(" <EVMaximumPowerLimit>\n");
|
|
printf(" <Multiplier>%d</Multiplier>\n", doc->V2G_Message.Body.CurrentDemandReq.EVMaximumPowerLimit.Multiplier);
|
|
printf(" <Unit>%d</Unit>\n", doc->V2G_Message.Body.CurrentDemandReq.EVMaximumPowerLimit.Unit);
|
|
printf(" <Value>%d</Value>\n", doc->V2G_Message.Body.CurrentDemandReq.EVMaximumPowerLimit.Value);
|
|
printf(" </EVMaximumPowerLimit>\n");
|
|
}
|
|
printf(" </CurrentDemandReq>\n");
|
|
}
|
|
else if (doc->V2G_Message.Body.CurrentDemandRes_isUsed) {
|
|
printf(" <CurrentDemandRes>\n");
|
|
printf(" <ResponseCode>%d</ResponseCode>\n", doc->V2G_Message.Body.CurrentDemandRes.ResponseCode);
|
|
printf(" <DC_EVSEStatus>\n");
|
|
printf(" <EVSENotification>%d</EVSENotification>\n", doc->V2G_Message.Body.CurrentDemandRes.DC_EVSEStatus.EVSENotification);
|
|
printf(" <NotificationMaxDelay>%d</NotificationMaxDelay>\n", doc->V2G_Message.Body.CurrentDemandRes.DC_EVSEStatus.NotificationMaxDelay);
|
|
if (doc->V2G_Message.Body.CurrentDemandRes.DC_EVSEStatus.EVSEIsolationStatus_isUsed) {
|
|
printf(" <EVSEIsolationStatus>%d</EVSEIsolationStatus>\n", doc->V2G_Message.Body.CurrentDemandRes.DC_EVSEStatus.EVSEIsolationStatus);
|
|
}
|
|
printf(" <EVSEStatusCode>%d</EVSEStatusCode>\n", doc->V2G_Message.Body.CurrentDemandRes.DC_EVSEStatus.EVSEStatusCode);
|
|
printf(" </DC_EVSEStatus>\n");
|
|
printf(" <EVSEPresentVoltage>\n");
|
|
printf(" <Multiplier>%d</Multiplier>\n", doc->V2G_Message.Body.CurrentDemandRes.EVSEPresentVoltage.Multiplier);
|
|
printf(" <Unit>%d</Unit>\n", doc->V2G_Message.Body.CurrentDemandRes.EVSEPresentVoltage.Unit);
|
|
printf(" <Value>%d</Value>\n", doc->V2G_Message.Body.CurrentDemandRes.EVSEPresentVoltage.Value);
|
|
printf(" </EVSEPresentVoltage>\n");
|
|
printf(" <EVSEPresentCurrent>\n");
|
|
printf(" <Multiplier>%d</Multiplier>\n", doc->V2G_Message.Body.CurrentDemandRes.EVSEPresentCurrent.Multiplier);
|
|
printf(" <Unit>%d</Unit>\n", doc->V2G_Message.Body.CurrentDemandRes.EVSEPresentCurrent.Unit);
|
|
printf(" <Value>%d</Value>\n", doc->V2G_Message.Body.CurrentDemandRes.EVSEPresentCurrent.Value);
|
|
printf(" </EVSEPresentCurrent>\n");
|
|
printf(" <EVSECurrentLimitAchieved>%s</EVSECurrentLimitAchieved>\n", doc->V2G_Message.Body.CurrentDemandRes.EVSECurrentLimitAchieved ? "true" : "false");
|
|
printf(" <EVSEVoltageLimitAchieved>%s</EVSEVoltageLimitAchieved>\n", doc->V2G_Message.Body.CurrentDemandRes.EVSEVoltageLimitAchieved ? "true" : "false");
|
|
printf(" <EVSEPowerLimitAchieved>%s</EVSEPowerLimitAchieved>\n", doc->V2G_Message.Body.CurrentDemandRes.EVSEPowerLimitAchieved ? "true" : "false");
|
|
if (doc->V2G_Message.Body.CurrentDemandRes.EVSEMaximumVoltageLimit_isUsed) {
|
|
printf(" <EVSEMaximumVoltageLimit>\n");
|
|
printf(" <Multiplier>%d</Multiplier>\n", doc->V2G_Message.Body.CurrentDemandRes.EVSEMaximumVoltageLimit.Multiplier);
|
|
printf(" <Unit>%d</Unit>\n", doc->V2G_Message.Body.CurrentDemandRes.EVSEMaximumVoltageLimit.Unit);
|
|
printf(" <Value>%d</Value>\n", doc->V2G_Message.Body.CurrentDemandRes.EVSEMaximumVoltageLimit.Value);
|
|
printf(" </EVSEMaximumVoltageLimit>\n");
|
|
}
|
|
if (doc->V2G_Message.Body.CurrentDemandRes.EVSEMaximumCurrentLimit_isUsed) {
|
|
printf(" <EVSEMaximumCurrentLimit>\n");
|
|
printf(" <Multiplier>%d</Multiplier>\n", doc->V2G_Message.Body.CurrentDemandRes.EVSEMaximumCurrentLimit.Multiplier);
|
|
printf(" <Unit>%d</Unit>\n", doc->V2G_Message.Body.CurrentDemandRes.EVSEMaximumCurrentLimit.Unit);
|
|
printf(" <Value>%d</Value>\n", doc->V2G_Message.Body.CurrentDemandRes.EVSEMaximumCurrentLimit.Value);
|
|
printf(" </EVSEMaximumCurrentLimit>\n");
|
|
}
|
|
if (doc->V2G_Message.Body.CurrentDemandRes.EVSEMaximumPowerLimit_isUsed) {
|
|
printf(" <EVSEMaximumPowerLimit>\n");
|
|
printf(" <Multiplier>%d</Multiplier>\n", doc->V2G_Message.Body.CurrentDemandRes.EVSEMaximumPowerLimit.Multiplier);
|
|
printf(" <Unit>%d</Unit>\n", doc->V2G_Message.Body.CurrentDemandRes.EVSEMaximumPowerLimit.Unit);
|
|
printf(" <Value>%d</Value>\n", doc->V2G_Message.Body.CurrentDemandRes.EVSEMaximumPowerLimit.Value);
|
|
printf(" </EVSEMaximumPowerLimit>\n");
|
|
}
|
|
printf(" <EVSEID>%.*s</EVSEID>\n",
|
|
doc->V2G_Message.Body.CurrentDemandRes.EVSEID.charactersLen,
|
|
doc->V2G_Message.Body.CurrentDemandRes.EVSEID.characters);
|
|
printf(" <SAScheduleTupleID>%d</SAScheduleTupleID>\n", doc->V2G_Message.Body.CurrentDemandRes.SAScheduleTupleID);
|
|
if (doc->V2G_Message.Body.CurrentDemandRes.MeterInfo_isUsed) {
|
|
printf(" <MeterInfo>\n");
|
|
printf(" <MeterID>%.*s</MeterID>\n",
|
|
doc->V2G_Message.Body.CurrentDemandRes.MeterInfo.MeterID.charactersLen,
|
|
doc->V2G_Message.Body.CurrentDemandRes.MeterInfo.MeterID.characters);
|
|
if (doc->V2G_Message.Body.CurrentDemandRes.MeterInfo.MeterReading_isUsed) {
|
|
printf(" <MeterReading>%llu</MeterReading>\n",
|
|
doc->V2G_Message.Body.CurrentDemandRes.MeterInfo.MeterReading);
|
|
}
|
|
if (doc->V2G_Message.Body.CurrentDemandRes.MeterInfo.SigMeterReading_isUsed) {
|
|
printf(" <SigMeterReading>");
|
|
for(int i = 0; i < doc->V2G_Message.Body.CurrentDemandRes.MeterInfo.SigMeterReading.bytesLen; i++) {
|
|
printf("%02X", doc->V2G_Message.Body.CurrentDemandRes.MeterInfo.SigMeterReading.bytes[i]);
|
|
}
|
|
printf("</SigMeterReading>\n");
|
|
}
|
|
if (doc->V2G_Message.Body.CurrentDemandRes.MeterInfo.MeterStatus_isUsed) {
|
|
printf(" <MeterStatus>%d</MeterStatus>\n", doc->V2G_Message.Body.CurrentDemandRes.MeterInfo.MeterStatus);
|
|
}
|
|
if (doc->V2G_Message.Body.CurrentDemandRes.MeterInfo.TMeter_isUsed) {
|
|
printf(" <TMeter>%lld</TMeter>\n", doc->V2G_Message.Body.CurrentDemandRes.MeterInfo.TMeter);
|
|
}
|
|
printf(" </MeterInfo>\n");
|
|
}
|
|
if (doc->V2G_Message.Body.CurrentDemandRes.ReceiptRequired_isUsed) {
|
|
printf(" <ReceiptRequired>%s</ReceiptRequired>\n", doc->V2G_Message.Body.CurrentDemandRes.ReceiptRequired ? "true" : "false");
|
|
}
|
|
printf(" </CurrentDemandRes>\n");
|
|
}
|
|
else if (doc->V2G_Message.Body.SessionStopReq_isUsed) {
|
|
printf(" <SessionStopReq>\n");
|
|
printf(" <ChargingSession>%d</ChargingSession>\n", doc->V2G_Message.Body.SessionStopReq.ChargingSession);
|
|
printf(" </SessionStopReq>\n");
|
|
}
|
|
else if (doc->V2G_Message.Body.SessionStopRes_isUsed) {
|
|
printf(" <SessionStopRes>\n");
|
|
printf(" <ResponseCode>%d</ResponseCode>\n", doc->V2G_Message.Body.SessionStopRes.ResponseCode);
|
|
printf(" </SessionStopRes>\n");
|
|
}
|
|
else {
|
|
printf(" <!-- Unknown message type -->\n");
|
|
}
|
|
|
|
printf(" </Body>\n");
|
|
print_xml_footer();
|
|
}
|
|
|
|
int main(int argc, char *argv[]) {
|
|
if (argc != 2) {
|
|
printf("Usage: %s input.exi\n", argv[0]);
|
|
printf("Converts EXI files to XML format\n");
|
|
return -1;
|
|
}
|
|
|
|
uint8_t buffer[BUFFER_SIZE];
|
|
bitstream_t stream;
|
|
size_t pos = 0;
|
|
int errn = 0;
|
|
|
|
struct iso1EXIDocument iso1Doc;
|
|
init_iso1EXIDocument(&iso1Doc);
|
|
|
|
// Read file
|
|
errn = readBytesFromFile(argv[1], buffer, BUFFER_SIZE, &pos);
|
|
if (errn != 0) {
|
|
fprintf(stderr, "Error reading file: %s\n", argv[1]);
|
|
return -1;
|
|
}
|
|
|
|
// Setup stream
|
|
pos = 0; // reset position for decoding
|
|
stream.size = BUFFER_SIZE;
|
|
stream.data = buffer;
|
|
stream.pos = &pos;
|
|
stream.buffer = 0;
|
|
stream.capacity = 0;
|
|
|
|
// Try ISO1 decoder
|
|
errn = decode_iso1ExiDocument(&stream, &iso1Doc);
|
|
if (errn == 0) {
|
|
print_iso1_xml(&iso1Doc);
|
|
return 0;
|
|
} else {
|
|
fprintf(stderr, "Error: Could not decode EXI file (error: %d)\n", errn);
|
|
return -1;
|
|
}
|
|
} |