using System; using System.IO; using System.Linq; namespace V2GProtocol { class Program { static void Main(string[] args) { Console.WriteLine("=== V2G Transfer Protocol Decoder/Encoder ==="); Console.WriteLine("ISO 15118-2 / DIN SPEC 70121 / SAP Protocol"); Console.WriteLine(); try { if (args.Length == 0) { ShowUsage(); return; } var command = args[0].ToLower(); // 첫 번째 인자가 옵션인 경우 (--decode, --encode, --help) if (command.StartsWith("--") || command.StartsWith("-")) { switch (command) { case "--decode" or "-d": HandleDecodeCommand(args); break; case "--encode" or "-e": HandleEncodeCommand(args); break; case "--help" or "-h": ShowUsage(); break; default: Console.WriteLine($"Error: Unknown option: {args[0]}"); ShowUsage(); break; } } // 두 번째 인자가 옵션인 경우 (filename --decode, filename --encode) else if (args.Length >= 2 && (args[1].ToLower() == "--decode" || args[1].ToLower() == "-d")) { // filename --decode 형태 string[] newArgs = { args[1], args[0] }; // 순서를 바꿔서 --decode filename 형태로 만듦 HandleDecodeCommand(newArgs); } else if (args.Length >= 2 && (args[1].ToLower() == "--encode" || args[1].ToLower() == "-e")) { // filename --encode 형태 string[] newArgs = { args[1], args[0] }; // 순서를 바꿔서 --encode filename 형태로 만듦 HandleEncodeCommand(newArgs); } // 파일명만 전달된 경우 (분석 모드) else if (File.Exists(args[0])) { AnalyzeFile(args[0]); } else { Console.WriteLine($"Error: Unknown option or file not found: {args[0]}"); ShowUsage(); } } catch (Exception ex) { Console.WriteLine($"Error: {ex.Message}"); } } static void ShowUsage() { Console.WriteLine("Usage:"); Console.WriteLine(" V2GDecoder.exe [file.txt] # Analyze hex dump file"); Console.WriteLine(" V2GDecoder.exe --decode # Decode EXI to XML"); Console.WriteLine(" V2GDecoder.exe --encode # Encode XML to EXI"); Console.WriteLine(" V2GDecoder.exe --decode # Decode file content to XML"); Console.WriteLine(" V2GDecoder.exe --encode # Encode file content to EXI"); Console.WriteLine(" V2GDecoder.exe --help # Show this help"); Console.WriteLine(); Console.WriteLine("Pipe Usage:"); Console.WriteLine(" type file.xml | V2GDecoder.exe --encode # Pipe XML file to encode"); Console.WriteLine(" echo \"hex_string\" | V2GDecoder.exe --decode # Pipe hex string to decode"); Console.WriteLine(); Console.WriteLine("Examples:"); Console.WriteLine(" V2GDecoder.exe data/dump2.txt # Full analysis"); Console.WriteLine(" V2GDecoder.exe -d \"8098021050908C0C0C0E0C5211003200\" # Decode hex string"); Console.WriteLine(" V2GDecoder.exe -d data/dump2.txt # Extract & decode from file"); Console.WriteLine(" V2GDecoder.exe -e \"...\" # Encode XML string"); Console.WriteLine(" V2GDecoder.exe -e message.xml # Encode XML file"); Console.WriteLine(" V2GDecoder.exe data/encode0.xml --encode # Encode XML file to EXI"); Console.WriteLine(" V2GDecoder.exe data/dump2.txt --decode # Decode file to XML only"); Console.WriteLine(" type data\\encode0.xml | V2GDecoder.exe --encode # Pipe file content to encode"); Console.WriteLine(" echo \"8098021050908C0C0C0E0C5211003200\" | V2GDecoder.exe -d # Pipe hex to decode"); } static void HandleDecodeCommand(string[] args) { string input; byte[] exiBytes; // Check if input is coming from stdin (pipe) if (args.Length < 2) { if (!Console.IsInputRedirected) { Console.WriteLine("Error: EXI hex string or file required for decode operation"); Console.WriteLine("Usage: V2GDecoder.exe --decode "); Console.WriteLine(" or: echo \"hex_string\" | V2GDecoder.exe --decode"); return; } // Read from stdin Console.WriteLine("Reading EXI data from stdin (pipe input)..."); input = Console.In.ReadToEnd().Trim(); if (string.IsNullOrEmpty(input)) { Console.WriteLine("Error: No input data received from stdin"); return; } } else { input = args[1]; } // Check if input is a file if (File.Exists(input)) { Console.WriteLine($"Reading EXI data from file: {input}"); // Handle different file extensions string extension = Path.GetExtension(input).ToLower(); if (extension == ".dump") { // Hex dump file format with arrows (→) Console.WriteLine("Processing hex dump format file..."); byte[] rawData = V2GDecoder.ParseHexFile(input); // Find V2G message in the hex dump var v2gMessage = ExtractV2GMessageFromHexDump(rawData); if (v2gMessage != null) { exiBytes = v2gMessage; Console.WriteLine($"Extracted V2G EXI message from hex dump ({exiBytes.Length} bytes)."); } else { Console.WriteLine("Warning: No V2G message header (01 FE) found in hex dump. Attempting to decode raw data."); // Try to decode the raw data directly (might be pure EXI without V2G header) exiBytes = rawData; } } else if (extension == ".hex") { // Plain hex file (pure hex string) Console.WriteLine("Processing hex string file..."); string fileContent = File.ReadAllText(input).Trim(); fileContent = fileContent.Replace(" ", "").Replace("-", "").Replace("0x", "").Replace("\n", "").Replace("\r", ""); exiBytes = Convert.FromHexString(fileContent); Console.WriteLine($"Parsed hex string from file ({exiBytes.Length} bytes)."); } else if (extension == ".txt") { // Legacy support for .txt files - check content format string fileContent = File.ReadAllText(input).Trim(); if (fileContent.Contains("→")) { // Hex dump format Console.WriteLine("Processing legacy hex dump format file..."); byte[] rawData = V2GDecoder.ParseHexFile(input); var v2gMessage = ExtractV2GMessageFromHexDump(rawData); if (v2gMessage != null) { exiBytes = v2gMessage; Console.WriteLine($"Extracted V2G EXI message from hex dump ({exiBytes.Length} bytes)."); } else { exiBytes = rawData; } } else { // Plain hex fileContent = fileContent.Replace(" ", "").Replace("-", "").Replace("0x", "").Replace("\n", "").Replace("\r", ""); exiBytes = Convert.FromHexString(fileContent); Console.WriteLine($"Parsed hex string from file ({exiBytes.Length} bytes)."); } } else { // Unknown extension, try as plain hex Console.WriteLine($"Unknown file extension '{extension}', attempting to parse as hex string..."); string fileContent = File.ReadAllText(input).Trim(); fileContent = fileContent.Replace(" ", "").Replace("-", "").Replace("0x", "").Replace("\n", "").Replace("\r", ""); exiBytes = Convert.FromHexString(fileContent); } } else { // Direct hex string input string exiHexString = input.Replace(" ", "").Replace("-", "").Replace("0x", ""); if (exiHexString.Length % 2 != 0) { Console.WriteLine("Error: Invalid hex string (odd number of characters)"); return; } exiBytes = Convert.FromHexString(exiHexString); Console.WriteLine("Processing direct hex string input."); } try { Console.WriteLine($"Decoding EXI data ({exiBytes.Length} bytes):"); Console.WriteLine($"Input: {Convert.ToHexString(exiBytes)}"); Console.WriteLine(); // Decode EXI to XML var decodedXml = V2GDecoder.DecodeEXIToXML(exiBytes); Console.WriteLine(decodedXml); } catch (Exception ex) { Console.WriteLine($"Error decoding EXI: {ex.Message}"); } } static byte[]? ExtractV2GMessageFromHexDump(byte[] data) { // Find V2G Transfer Protocol signature (0x01FE) for (int i = 0; i < data.Length - 8; i++) { if (data[i] == 0x01 && data[i + 1] == 0xFE) { // Get payload length uint payloadLength = (uint)((data[i + 4] << 24) | (data[i + 5] << 16) | (data[i + 6] << 8) | data[i + 7]); // Extract EXI payload if (i + 8 + payloadLength <= data.Length) { byte[] exiPayload = new byte[payloadLength]; Array.Copy(data, i + 8, exiPayload, 0, (int)payloadLength); return exiPayload; } } } return null; } static void HandleEncodeCommand(string[] args) { string xmlInput; string xmlContent; // Check if input is coming from stdin (pipe) if (args.Length < 2) { if (!Console.IsInputRedirected) { Console.WriteLine("Error: XML string or file required for encode operation"); Console.WriteLine("Usage: V2GDecoder.exe --encode "); Console.WriteLine(" or: type file.xml | V2GDecoder.exe --encode"); return; } // Read from stdin Console.WriteLine("Reading XML data from stdin (pipe input)..."); xmlContent = Console.In.ReadToEnd().Trim(); if (string.IsNullOrEmpty(xmlContent)) { Console.WriteLine("Error: No input data received from stdin"); return; } } else { xmlInput = args[1]; // Check if input is a file if (File.Exists(xmlInput)) { string extension = Path.GetExtension(xmlInput).ToLower(); if (extension == ".xml") { Console.WriteLine($"Reading XML from file: {xmlInput}"); xmlContent = File.ReadAllText(xmlInput); } else { // For non-XML extensions, still try to read as text file Console.WriteLine($"Reading content from file: {xmlInput} (extension: {extension})"); xmlContent = File.ReadAllText(xmlInput); } } else { Console.WriteLine("Processing XML string:"); xmlContent = xmlInput; } } try { Console.WriteLine("Input XML:"); Console.WriteLine(xmlContent); Console.WriteLine(); // Encode XML to EXI var exiBytes = V2GDecoder.EncodeXMLToEXI(xmlContent); var exiHexString = Convert.ToHexString(exiBytes); Console.WriteLine($"Encoded EXI data ({exiBytes.Length} bytes):"); Console.WriteLine(exiHexString); // Also show formatted hex Console.WriteLine(); Console.WriteLine("Formatted hex:"); Console.WriteLine(V2GDecoder.BytesToHex(exiBytes)); } catch (Exception ex) { Console.WriteLine($"Error encoding XML: {ex.Message}"); } } static void AnalyzeFile(string dataFilePath) { if (!File.Exists(dataFilePath)) { Console.WriteLine($"Error: Data file not found: {dataFilePath}"); return; } Console.WriteLine($"Analyzing file: {dataFilePath}"); // 헥스 파일에서 바이너리 데이터 파싱 byte[] rawData = V2GDecoder.ParseHexFile(dataFilePath); Console.WriteLine($"Parsed {rawData.Length} bytes from hex file"); Console.WriteLine(); // 전체 데이터 헥스 덤프 Console.WriteLine("=== Raw Data Hex Dump ==="); Console.WriteLine(V2GDecoder.BytesToHex(rawData)); Console.WriteLine(); // V2G 메시지 디코딩 시도 Console.WriteLine("=== V2G Message Analysis ==="); // 전체 데이터에서 V2G 메시지 찾기 AnalyzeV2GMessages(rawData); // 네트워크 패킷 분석 (이더넷/IPv6/TCP 헤더 포함인 경우) AnalyzeNetworkPacket(rawData); } static void AnalyzeV2GMessages(byte[] data) { // V2G Transfer Protocol 시그니처 찾기 for (int i = 0; i < data.Length - 8; i++) { if (data[i] == 0x01 && data[i + 1] == 0xFE) // V2G TP Header { Console.WriteLine($"Potential V2G message found at offset 0x{i:X4}"); try { byte[] messageData = new byte[data.Length - i]; Array.Copy(data, i, messageData, 0, messageData.Length); var message = V2GDecoder.DecodeMessage(messageData); Console.WriteLine($" Version: 0x{message.Version:X2}"); Console.WriteLine($" Inverse Version: 0x{message.InverseVersion:X2}"); Console.WriteLine($" Payload Type: 0x{(ushort)message.PayloadType:X4} ({message.PayloadType})"); Console.WriteLine($" Payload Length: {message.PayloadLength} bytes"); Console.WriteLine($" Valid: {message.IsValid}"); Console.WriteLine(); if (message.IsValid && message.Payload != null) { Console.WriteLine("=== Decoded Message Content ==="); Console.WriteLine(message.DecodedContent); Console.WriteLine(); } } catch (Exception ex) { Console.WriteLine($" Error decoding message: {ex.Message}"); } } } } static void AnalyzeNetworkPacket(byte[] data) { Console.WriteLine("=== Network Packet Analysis ==="); if (data.Length < 54) // Minimum Ethernet + IPv6 + TCP header size { Console.WriteLine("Data too short for network packet analysis"); return; } try { // 이더넷 헤더 분석 (14 bytes) Console.WriteLine("Ethernet Header:"); Console.WriteLine($" Destination MAC: {string.Join(":", data[0..6].Select(b => b.ToString("X2")))}"); Console.WriteLine($" Source MAC: {string.Join(":", data[6..12].Select(b => b.ToString("X2")))}"); Console.WriteLine($" EtherType: 0x{(data[12] << 8 | data[13]):X4}"); ushort etherType = (ushort)(data[12] << 8 | data[13]); if (etherType == 0x86DD) // IPv6 { Console.WriteLine("\nIPv6 Header:"); int ipv6Offset = 14; byte versionTrafficClass = data[ipv6Offset]; Console.WriteLine($" Version: {(versionTrafficClass >> 4) & 0xF}"); Console.WriteLine($" Traffic Class: 0x{((versionTrafficClass & 0xF) << 4 | (data[ipv6Offset + 1] >> 4)):X2}"); ushort payloadLength = (ushort)(data[ipv6Offset + 4] << 8 | data[ipv6Offset + 5]); byte nextHeader = data[ipv6Offset + 6]; byte hopLimit = data[ipv6Offset + 7]; Console.WriteLine($" Payload Length: {payloadLength}"); Console.WriteLine($" Next Header: 0x{nextHeader:X2} ({(nextHeader == 6 ? "TCP" : "Other")})"); Console.WriteLine($" Hop Limit: {hopLimit}"); // IPv6 주소는 16바이트씩 var srcAddr = data[(ipv6Offset + 8)..(ipv6Offset + 24)]; var dstAddr = data[(ipv6Offset + 24)..(ipv6Offset + 40)]; Console.WriteLine($" Source Address: {string.Join(":", Enumerable.Range(0, 8).Select(i => $"{srcAddr[i*2]:X2}{srcAddr[i*2+1]:X2}"))}"); Console.WriteLine($" Destination Address: {string.Join(":", Enumerable.Range(0, 8).Select(i => $"{dstAddr[i*2]:X2}{dstAddr[i*2+1]:X2}"))}"); if (nextHeader == 6) // TCP { int tcpOffset = ipv6Offset + 40; if (data.Length > tcpOffset + 20) { Console.WriteLine("\nTCP Header:"); ushort srcPort = (ushort)(data[tcpOffset] << 8 | data[tcpOffset + 1]); ushort dstPort = (ushort)(data[tcpOffset + 2] << 8 | data[tcpOffset + 3]); Console.WriteLine($" Source Port: {srcPort}"); Console.WriteLine($" Destination Port: {dstPort}"); // V2G는 일반적으로 포트 15118을 사용 if (srcPort == 15118 || dstPort == 15118) { Console.WriteLine(" -> V2G Communication detected (port 15118)!"); } // TCP 데이터 시작 위치 계산 byte dataOffset = (byte)((data[tcpOffset + 12] >> 4) * 4); int tcpDataOffset = tcpOffset + dataOffset; if (tcpDataOffset < data.Length) { Console.WriteLine($"\nTCP Payload (starting at offset 0x{tcpDataOffset:X4}):"); byte[] tcpPayload = data[tcpDataOffset..]; // TCP 페이로드에서 V2G 메시지 찾기 if (tcpPayload.Length > 8 && tcpPayload[0] == 0x01 && tcpPayload[1] == 0xFE) { Console.WriteLine("V2G Message found in TCP payload!"); var v2gMessage = V2GDecoder.DecodeMessage(tcpPayload); Console.WriteLine(v2gMessage.DecodedContent); } else { Console.WriteLine("TCP Payload hex dump:"); Console.WriteLine(V2GDecoder.BytesToHex(tcpPayload)); } } } } } } catch (Exception ex) { Console.WriteLine($"Error analyzing network packet: {ex.Message}"); } } } }