Repository navigation
Expand file tree
/
Copy pathexample_parse_file.cpp
More file actions
161 lines (142 loc) · 6.85 KB
/
Copy pathexample_parse_file.cpp
File metadata and controls
161 lines (142 loc) · 6.85 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
// Copyright 2026 momentics <momentics@gmail.com>
// Copyright libgsml3parser contributors
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in all
// copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
// SOFTWARE.
// Demonstrates parsing L3 messages from hex strings or files, typed access
// via tryGet<>, round-trip serialization, and batch parsing across all 13 PD
// domains (RR, MM, CC, SS, GMM, SM, SMS, BCC, GCC, LS, PDSS1, EXT, TST).
#include <gsml3parser/gsml3parser.hpp>
#include <fstream>
#include <iostream>
#include <sstream>
#include <string>
#include <string_view>
#include <vector>
using namespace gsml3parser;
namespace {
std::string readFileAsString(std::string_view path) {
std::ifstream ifs(path.data());
if (!ifs.is_open()) return {};
std::ostringstream ss;
ss << ifs.rdbuf();
return ss.str();
}
void printMsgDetails(const ParsedMessage& msg) {
std::cout << " PD: " << messagePD(msg) << "\n";
std::cout << " MTI: 0x" << std::hex << messageMTI(msg) << std::dec << "\n";
std::cout << " Name: " << messageName(msg) << "\n";
}
// Demo: parse multiple hex frames from all 13 PD domains and display their types
void demoBatchParse(const std::vector<std::pair<std::string, std::string>>& hexFrames) {
for (const auto& [label, hex] : hexFrames) {
auto result = parseL3Hex(hex);
if (result) {
const auto& msg = *result;
std::cout << "[OK] " << label << ": " << messageName(msg)
<< " (PD=" << messagePD(msg)
<< ", MTI=0x" << std::hex << messageMTI(msg) << std::dec << ")\n";
// Round-trip: serialize back to hex and compare
auto reHex = writeL3Hex(msg);
if (reHex) {
std::cout << " Round-trip: " << *reHex << "\n";
}
} else {
std::cout << "[FAIL] " << label << ": " << result.error().message << "\n";
}
}
}
} // anonymous namespace
int main(int argc, char* argv[]) {
if (argc < 2) {
std::cerr << "Usage: " << argv[0] << " <hex_string_or_file>\n";
return 1;
}
std::string_view input{argv[1]};
std::string content;
// Try to read as file first
content = readFileAsString(input);
if (content.empty()) {
content = std::string{input};
}
// Parse using the Expected<ParsedMessage> API
auto result = parseL3Hex(content);
if (result) {
const auto& msg = *result;
// Typed access via tryGet - covers all 13 PD domains
if (auto* cr = tryGet<L3ChannelRelease>(msg)) {
std::cout << "RR: Channel Release detected\n";
} else if (auto* paging = tryGet<L3PagingRequestType1>(msg)) {
std::cout << "RR: Paging Request Type 1 detected\n";
} else if (auto* setup = tryGet<L3Setup>(msg)) {
std::cout << "CC: Setup detected\n";
} else if (auto* fac = tryGet<L3SupServFacilityMessage>(msg)) {
std::cout << "SS: Facility detected\n";
} else if (auto* attach = tryGet<L3AttachRequest>(msg)) {
std::cout << "GMM: Attach Request detected\n";
} else if (auto* pdu = tryGet<L3ActivatePDPContextRequest>(msg)) {
std::cout << "SM: Activate PDP Context Request detected\n";
} else if (auto* cpd = tryGet<L3CPData>(msg)) {
std::cout << "SMS: CP Data detected\n";
} else if (auto* bccSetup = tryGet<L3BCCSetup>(msg)) {
std::cout << "BCC: Setup detected\n";
} else if (auto* gccSetup = tryGet<L3GCCSetup>(msg)) {
std::cout << "GCC: Setup detected\n";
} else if (auto* lsReq = tryGet<L3LocationServiceRequest>(msg)) {
std::cout << "LS: LocationServiceRequest detected\n";
} else if (auto* lsProv = tryGet<L3LocationServiceProviderMessage>(msg)) {
std::cout << "LS: LocationServiceProviderMessage detected\n";
} else if (auto* pdss1 = tryGet<L3PDSS1Message>(msg)) {
std::cout << "PDSS1: PDSS1Message detected\n";
} else if (auto* ext = tryGet<L3ExtendedMessage>(msg)) {
std::cout << "EXT: ExtendedMessage detected\n";
} else if (auto* tst = tryGet<L3TestProcedureMessage>(msg)) {
std::cout << "TST: TestProcedureMessage detected\n";
} else {
std::cout << "Parsed message type: " << messageName(msg) << "\n";
}
printMsgDetails(msg);
// Serialize back to hex for round-trip verification
auto hexOut = writeL3Hex(msg);
if (hexOut) {
std::cout << "Serialized: " << *hexOut << "\n";
}
} else {
std::cerr << "Failed to parse: " << result.error().message << "\n";
return 1;
}
// Demo batch parsing with representative messages from all 13 PD domains
std::cout << "\n--- Batch Parse Demo (All 13 PD Domains) ---\n";
std::vector<std::pair<std::string, std::string>> batch{
{"RR", "06 0D 00"}, // Channel Release
{"MM", "05 21"}, // CM Service Accept
{"CC", "E3 25 02 61 90"}, // Disconnect (TI=7)
{"SS", "0B 3A"}, // Facility
{"GMM", "08 20 05"}, // GMM Status (cause=5)
{"SM", "0A 55 05"}, // SM Status (cause=5)
{"SMS", "09 04 01 02"}, // CP Ack (ref=2)
{"BCC", "01 00"}, // BCC Setup
{"GCC", "00 00 02"}, // GCC Setup
{"LS", "0C 01"}, // LocationServiceRequest
{"PDSS1", "0D 42"}, // PDSS1Message (passthrough)
{"EXT", "0E 01"}, // ExtendedMessage
{"TST", "0F 01"}, // TestProcedureMessage
};
demoBatchParse(batch);
return 0;
}