pyosmocom 0.0.8__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
osmocom/gsmtap.py ADDED
@@ -0,0 +1,216 @@
1
+ # -*- coding: utf-8 -*-
2
+
3
+ """ Osmocom GSMTAP python implementation.
4
+ GSMTAP is a packet format used for conveying a number of different
5
+ telecom-related protocol traces over UDP.
6
+ """
7
+
8
+ #
9
+ # Copyright (C) 2022 Harald Welte <laforge@gnumonks.org>
10
+ #
11
+ # This program is free software: you can redistribute it and/or modify
12
+ # it under the terms of the GNU General Public License as published by
13
+ # the Free Software Foundation, either version 2 of the License, or
14
+ # (at your option) any later version.
15
+ #
16
+ # This program is distributed in the hope that it will be useful,
17
+ # but WITHOUT ANY WARRANTY; without even the implied warranty of
18
+ # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19
+ # GNU General Public License for more details.
20
+ #
21
+ # You should have received a copy of the GNU General Public License
22
+ # along with this program. If not, see <http://www.gnu.org/licenses/>.
23
+ #
24
+
25
+ import socket
26
+ from construct import Optional as COptional
27
+ from construct import Int8ub, Int8sb, Int32ub, BitStruct, Enum, GreedyBytes, Struct, Switch
28
+ from construct import this, PaddedString, Flag, BitsInteger, Bytes
29
+ from osmocom.construct import *
30
+
31
+ # The root definition of GSMTAP can be found at
32
+ # https://cgit.osmocom.org/cgit/libosmocore/tree/include/osmocom/core/gsmtap.h
33
+
34
+ GSMTAP_UDP_PORT = 4729
35
+
36
+ # GSMTAP_TYPE_*
37
+ gsmtap_type_construct = Enum(Int8ub,
38
+ gsm_um = 0x01,
39
+ gsm_abis = 0x02,
40
+ gsm_um_burst = 0x03,
41
+ sim = 0x04,
42
+ tetra_i1 = 0x05,
43
+ tetra_i1_burst = 0x06,
44
+ wimax_burst = 0x07,
45
+ gprs_gb_llc = 0x08,
46
+ gprs_gb_sndcp = 0x09,
47
+ gmr1_um = 0x0a,
48
+ umts_rlc_mac = 0x0b,
49
+ umts_rrc = 0x0c,
50
+ lte_rrc = 0x0d,
51
+ lte_mac = 0x0e,
52
+ lte_mac_framed = 0x0f,
53
+ osmocore_log = 0x10,
54
+ qc_diag = 0x11,
55
+ lte_nas = 0x12,
56
+ e1_t1 = 0x13)
57
+
58
+
59
+ # TYPE_UM_BURST
60
+ gsmtap_subtype_burst_construct = Enum(Int8ub,
61
+ unknown = 0x00,
62
+ fcch = 0x01,
63
+ partial_sch = 0x02,
64
+ sch = 0x03,
65
+ cts_sch = 0x04,
66
+ compact_sch = 0x05,
67
+ normal = 0x06,
68
+ dummy = 0x07,
69
+ access = 0x08,
70
+ none = 0x09)
71
+
72
+ gsmtap_subtype_wimax_burst_construct = Enum(Int8ub,
73
+ cdma_code = 0x10,
74
+ fch = 0x11,
75
+ ffb = 0x12,
76
+ pdu = 0x13,
77
+ hack = 0x14,
78
+ phy_attributes = 0x15)
79
+
80
+ # GSMTAP_CHANNEL_*
81
+ gsmtap_subtype_um_construct = Enum(Int8ub,
82
+ unknown = 0x00,
83
+ bcch = 0x01,
84
+ ccch = 0x02,
85
+ rach = 0x03,
86
+ agch = 0x04,
87
+ pch = 0x05,
88
+ sdcch = 0x06,
89
+ sdcch4 = 0x07,
90
+ sdcch8 = 0x08,
91
+ facch_f = 0x09,
92
+ facch_h = 0x0a,
93
+ pacch = 0x0b,
94
+ cbch52 = 0x0c,
95
+ pdtch = 0x0d,
96
+ ptcch = 0x0e,
97
+ cbch51 = 0x0f,
98
+ voice_f = 0x10,
99
+ voice_h = 0x11)
100
+
101
+
102
+ # GSMTAP_SIM_*
103
+ gsmtap_subtype_sim_construct = Enum(Int8ub,
104
+ apdu = 0x00,
105
+ atr = 0x01,
106
+ pps_req = 0x02,
107
+ pps_rsp = 0x03,
108
+ tpdu_hdr = 0x04,
109
+ tpdu_cmd = 0x05,
110
+ tpdu_rsp = 0x06,
111
+ tpdu_sw = 0x07)
112
+
113
+ gsmtap_subtype_tetra_construct = Enum(Int8ub,
114
+ bsch = 0x01,
115
+ aach = 0x02,
116
+ sch_hu = 0x03,
117
+ sch_hd = 0x04,
118
+ sch_f = 0x05,
119
+ bnch = 0x06,
120
+ stch = 0x07,
121
+ tch_f = 0x08,
122
+ dmo_sch_s = 0x09,
123
+ dmo_sch_h = 0x0a,
124
+ dmo_sch_f = 0x0b,
125
+ dmo_stch = 0x0c,
126
+ dmo_tch = 0x0d)
127
+
128
+ gsmtap_subtype_gmr1_construct = Enum(Int8ub,
129
+ unknown = 0x00,
130
+ bcch = 0x01,
131
+ ccch = 0x02,
132
+ pch = 0x03,
133
+ agch = 0x04,
134
+ bach = 0x05,
135
+ rach = 0x06,
136
+ cbch = 0x07,
137
+ sdcch = 0x08,
138
+ tachh = 0x09,
139
+ gbch = 0x0a,
140
+ tch3 = 0x10,
141
+ tch6 = 0x14,
142
+ tch9 = 0x18)
143
+
144
+ gsmtap_subtype_e1t1_construct = Enum(Int8ub,
145
+ lapd = 0x01,
146
+ fr = 0x02,
147
+ raw = 0x03,
148
+ trau16 = 0x04,
149
+ trau8 = 0x05)
150
+
151
+ gsmtap_arfcn_construct = BitStruct('pcs'/Flag, 'uplink'/Flag, 'arfcn'/BitsInteger(14))
152
+
153
+ gsmtap_hdr_construct = Struct('version'/Int8ub,
154
+ 'hdr_len'/Int8ub,
155
+ 'type'/gsmtap_type_construct,
156
+ 'timeslot'/Int8ub,
157
+ 'arfcn'/gsmtap_arfcn_construct,
158
+ 'signal_dbm'/Int8sb,
159
+ 'snr_db'/Int8sb,
160
+ 'frame_nr'/Int32ub,
161
+ 'sub_type'/Switch(this.type, {
162
+ 'gsm_um': gsmtap_subtype_um_construct,
163
+ 'gsm_um_burst': gsmtap_subtype_burst_construct,
164
+ 'sim': gsmtap_subtype_sim_construct,
165
+ 'tetra_i1': gsmtap_subtype_tetra_construct,
166
+ 'tetra_i1_burst': gsmtap_subtype_tetra_construct,
167
+ 'wimax_burst': gsmtap_subtype_wimax_burst_construct,
168
+ 'gmr1_um': gsmtap_subtype_gmr1_construct,
169
+ 'e1_t1': gsmtap_subtype_e1t1_construct,
170
+ }),
171
+ 'antenna_nr'/Int8ub,
172
+ 'sub_slot'/Int8ub,
173
+ 'res'/Int8ub,
174
+ 'body'/GreedyBytes)
175
+
176
+ osmocore_log_ts_construct = Struct('sec'/Int32ub, 'usec'/Int32ub)
177
+ osmocore_log_level_construct = Enum(Int8ub, debug=1, info=3, notice=5, error=7, fatal=8)
178
+ gsmtap_osmocore_log_hdr_construct = Struct('ts'/osmocore_log_ts_construct,
179
+ 'proc_name'/PaddedString(16, 'ascii'),
180
+ 'pid'/Int32ub,
181
+ 'level'/osmocore_log_level_construct,
182
+ Bytes(3),
183
+ 'subsys'/PaddedString(16, 'ascii'),
184
+ 'src_file'/Struct('name'/PaddedString(32, 'ascii'), 'line_nr'/Int32ub))
185
+
186
+
187
+ class GsmtapMessage:
188
+ """Class whose objects represent a single GSMTAP message. Can encode and decode messages."""
189
+ def __init__(self, encoded = None):
190
+ self.encoded = encoded
191
+ self.decoded = None
192
+
193
+ def decode(self):
194
+ self.decoded = parse_construct(gsmtap_hdr_construct, self.encoded)
195
+ return self.decoded
196
+
197
+ def encode(self, decoded):
198
+ self.encoded = gsmtap_hdr_construct.build(decoded)
199
+ return self.encoded
200
+
201
+ class GsmtapReceiver:
202
+ """Simple receive-side socket implementation for GSMTAP messages."""
203
+ def __init__(self, bind_ip:str='127.0.0.1', bind_port:int=4729):
204
+ self.bind_ip = bind_ip
205
+ self.bind_port = bind_port
206
+ self.sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
207
+ self.sock.bind((self.bind_ip, self.bind_port))
208
+
209
+ def read_packet(self) -> GsmtapMessage:
210
+ """Perform a blocking read on the GSMTAP socket and decode it as GSMTAP message."""
211
+ data, addr = self.sock.recvfrom(65535)
212
+ gsmtap_msg = GsmtapMessage(data)
213
+ gsmtap_msg.decode()
214
+ if gsmtap_msg.decoded['version'] != 0x02:
215
+ raise ValueError('Unknown GSMTAP version 0x%02x' % gsmtap_msg.decoded['version'])
216
+ return gsmtap_msg.decoded, addr
File without changes
@@ -0,0 +1,335 @@
1
+ #!/usr/bin/env python3
2
+
3
+ # Python implementation of Osmocom GSUP protocol
4
+ #
5
+ # (C) 2024 by Harald Welte <laforge@osmocom.org>
6
+ #
7
+ # This program is free software: you can redistribute it and/or modify
8
+ # it under the terms of the GNU General Public License as published by
9
+ # the Free Software Foundation, either version 2 of the License, or
10
+ # (at your option) any later version.
11
+ #
12
+ # This program is distributed in the hope that it will be useful,
13
+ # but WITHOUT ANY WARRANTY; without even the implied warranty of
14
+ # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15
+ # GNU General Public License for more details.
16
+ #
17
+ # You should have received a copy of the GNU General Public License
18
+ # along with this program. If not, see <http://www.gnu.org/licenses/>.
19
+
20
+ """
21
+ This is an encoder/decoder implementation for the Osmocom GSUP
22
+ protocol, built upon the osmocom.tlv and osmocom.construct
23
+ infrastructure.
24
+ """
25
+
26
+ from enum import IntEnum
27
+ from typing import Tuple, Union
28
+ from construct import Int8ub, Int32ub, Byte, Bytes, GreedyBytes, BitsInteger, GreedyString
29
+ from construct import this, Struct, BitStruct, Enum, GreedyRange, Default, Switch
30
+ from construct import Optional as COptional
31
+
32
+ from osmocom.tlv import TLV_IE, TLV_IE_Collection
33
+ from osmocom.construct import TonNpi, Rpad, OsmoRatType
34
+ from osmocom.construct import PaddedBcdAdapter, DnsAdapter, Ipv4Adapter, Ipv6Adapter
35
+
36
+ class GSUP_TLV_IE(TLV_IE):
37
+ """Class representing the TLV format as used in Osmocom GSUP. It's a simple
38
+ '8-bit tag / 8-bit length / value' variant."""
39
+
40
+ @classmethod
41
+ def _parse_tag_raw(cls, do: bytes) -> Tuple[int, bytes]:
42
+ return do[0], do[1:]
43
+
44
+ @classmethod
45
+ def _parse_len(cls, do: bytes) -> Tuple[int, bytes]:
46
+ return do[0], do[1:]
47
+
48
+ def _encode_tag(self) -> bytes:
49
+ return bytes([self.tag])
50
+
51
+ def _encode_len(self, val: bytes) -> bytes:
52
+ return bytes([len(val)])
53
+
54
+
55
+ class tlv:
56
+ """TLV definitions for the GSUP IEs, utilizing the osmocom.tlv code. Keep this in sync with
57
+ https://gitea.osmocom.org/osmocom/libosmocore/src/branch/master/include/osmocom/gsm/gsup.h"""
58
+ class auth:
59
+ """Nested TLV IEs within the AuthTuple."""
60
+ class RAND(GSUP_TLV_IE, tag=0x20):
61
+ _construct = GreedyBytes
62
+
63
+ class SRES(GSUP_TLV_IE, tag=0x21):
64
+ _construct = GreedyBytes
65
+
66
+ class Kc(GSUP_TLV_IE, tag=0x22):
67
+ _construct = GreedyBytes
68
+
69
+ class IK(GSUP_TLV_IE, tag=0x23):
70
+ _construct = GreedyBytes
71
+
72
+ class CK(GSUP_TLV_IE, tag=0x24):
73
+ _construct = GreedyBytes
74
+
75
+ class AUTN(GSUP_TLV_IE, tag=0x25):
76
+ _construct = GreedyBytes
77
+
78
+ class AUTS(GSUP_TLV_IE, tag=0x26):
79
+ _construct = GreedyBytes
80
+
81
+ class RES(GSUP_TLV_IE, tag=0x27):
82
+ _construct = GreedyBytes
83
+
84
+ class pdp:
85
+ """Nested TLV IEs within the PdpInfo."""
86
+ class PdpContextId(GSUP_TLV_IE, tag=0x10):
87
+ _construct = Int8ub
88
+
89
+ class PdpAddress(GSUP_TLV_IE, tag=0x11):
90
+ _construct = Struct('hdr'/BitStruct('_spare'/Default(BitsInteger(4), 0xf),
91
+ 'pdp_type_org'/Enum(BitsInteger(4), ietf=1),
92
+ 'pdp_type_nr'/Enum(BitsInteger(8), ipv4=0x21, ipv6=0x57,
93
+ ipv4v6=0x8d)),
94
+ 'address'/COptional(Switch(this.hdr.pdp_type_nr,
95
+ {'ipv4': Ipv4Adapter(Bytes(4)),
96
+ 'ipv6': Ipv6Adapter(Bytes(16)),
97
+ 'ipv4v6': Struct('ipv4'/Ipv4Adapter(Bytes(4)),
98
+ 'ipv6'/Ipv6Adapter(Bytes(16)))})))
99
+
100
+
101
+ class AccessPointName(GSUP_TLV_IE, tag=0x12):
102
+ _construct = DnsAdapter(GreedyBytes)
103
+
104
+ class Qos(GSUP_TLV_IE, tag=0x13):
105
+ _construct = GreedyBytes
106
+
107
+ class PdpChargingCharacteristics(GSUP_TLV_IE, tag=0x14):
108
+ _construct = BitStruct('profile_index'/BitsInteger(4), 'behaviour'/BitsInteger(12))
109
+
110
+ class IMSI(GSUP_TLV_IE, tag=0x01):
111
+ _construct = PaddedBcdAdapter(GreedyBytes)
112
+
113
+ class Cause(GSUP_TLV_IE, tag=0x02):
114
+ _construct = Int8ub
115
+
116
+ # osmocom/gsup/message.py:104:51: E0602: Undefined variable 'auth' (undefined-variable)
117
+ # pylint: disable=undefined-variable
118
+ class AuthTuple(GSUP_TLV_IE, tag=0x03, nested=[auth.RAND, auth.SRES, auth.Kc, auth.IK,
119
+ auth.CK, auth.AUTN, auth.RES]):
120
+ pass
121
+
122
+ class PdpInfoCompl(GSUP_TLV_IE, tag=0x04):
123
+ _construct = None # emtpy
124
+
125
+ class PdpInfo(GSUP_TLV_IE, tag=0x05, nested=[pdp.PdpContextId, pdp.PdpAddress, pdp.AccessPointName,
126
+ pdp.Qos, pdp.PdpChargingCharacteristics]):
127
+ pass
128
+
129
+ class CancelType(GSUP_TLV_IE, tag=0x06):
130
+ _construct = Enum(Int8ub, update=0, withdraw=1)
131
+
132
+ class FreezePTMSI(GSUP_TLV_IE, tag=0x07):
133
+ _construct = None # empty
134
+
135
+ class MSISDN(GSUP_TLV_IE, tag=0x08):
136
+ # TODO: do all existing implementations use ton/npi?
137
+ _construct = Struct('ton_npi'/TonNpi, 'digits'/PaddedBcdAdapter(GreedyBytes))
138
+
139
+ class HlrNumber(GSUP_TLV_IE, tag=0x09):
140
+ _construct = Struct('ton_npi'/TonNpi, 'digits'/PaddedBcdAdapter(GreedyBytes))
141
+
142
+ class MessageClass(GSUP_TLV_IE, tag=0x0a):
143
+ _construct = Enum(Int8ub, subscriber_management=1, sms=2, ussd=3, inter_msc=4, ipsec_epdg=5)
144
+
145
+ class PCO(GSUP_TLV_IE, tag=0x15):
146
+ _construct = GreedyBytes # TODO: further decode
147
+
148
+ class CnDomain(GSUP_TLV_IE, tag=0x28):
149
+ _construct = Enum(Byte, ps=1, cs=2)
150
+
151
+ class SupportedRatTypes(GSUP_TLV_IE, tag=0x29):
152
+ _construct = OsmoRatType
153
+
154
+ class CurrentRatType(GSUP_TLV_IE, tag=0x2a):
155
+ _construct = GreedyRange(OsmoRatType)
156
+
157
+ class SessionId(GSUP_TLV_IE, tag=0x30):
158
+ _construct = Int32ub
159
+
160
+ class SessionState(GSUP_TLV_IE, tag=0x31):
161
+ _construct = Enum(Int8ub, undefined=0, begin=1, Continue=2, end=3)
162
+
163
+ class SupplementaryServiceInfo(GSUP_TLV_IE, tag=0x35):
164
+ _construct = GreedyBytes # TODO: further decode
165
+
166
+ class SmRpMr(GSUP_TLV_IE, tag=0x40):
167
+ _construct = Int8ub
168
+
169
+ class SmRpDa(GSUP_TLV_IE, tag=0x41):
170
+ _construct = Struct('ton_npi'/TonNpi, 'digits'/PaddedBcdAdapter(GreedyBytes))
171
+
172
+ class SmRpOa(GSUP_TLV_IE, tag=0x42):
173
+ _construct = Struct('ton_npi'/TonNpi, 'digits'/PaddedBcdAdapter(GreedyBytes))
174
+
175
+ class SmRpUi(GSUP_TLV_IE, tag=0x43):
176
+ _construct = GreedyBytes
177
+
178
+ class SmRpCause(GSUP_TLV_IE, tag=0x44):
179
+ _construct = Int8ub
180
+
181
+ class SmRpMms(GSUP_TLV_IE, tag=0x45):
182
+ _construct = None # empty
183
+
184
+ class SmAlert(GSUP_TLV_IE, tag=0x46):
185
+ _construct = Enum(Int8ub, ms_present=1, memory_available=2)
186
+
187
+ class IMEI(GSUP_TLV_IE, tag=0x50):
188
+ _construct = PaddedBcdAdapter(GreedyBytes)
189
+
190
+ class ImeiCheckResult(GSUP_TLV_IE, tag=0x51):
191
+ _construct = Enum(Int8ub, ack=0, nack=1)
192
+
193
+ class NumVectorsReq(GSUP_TLV_IE, tag=0x52):
194
+ _construct = Int8ub
195
+
196
+ class SourceName(GSUP_TLV_IE, tag=0x60):
197
+ _construct = GreedyString('ascii')
198
+
199
+ class DestinationName(GSUP_TLV_IE, tag=0x61):
200
+ _construct = GreedyString('ascii')
201
+
202
+ class AnApdu(GSUP_TLV_IE, tag=0x62):
203
+ _construct = Struct('protocol'/Enum(Int8ub, bssap=1, ranap=2),
204
+ 'data'/GreedyBytes)
205
+
206
+ class CauseRr(GSUP_TLV_IE, tag=0x63):
207
+ _construct = Int8ub
208
+
209
+ class CauseBssap(GSUP_TLV_IE, tag=0x64):
210
+ _construct = Int8ub
211
+
212
+ class CauseSm(GSUP_TLV_IE, tag=0x65):
213
+ _construct = Int8ub
214
+
215
+ class IeCollection(TLV_IE_Collection, nested=[IMSI, Cause, AuthTuple, PdpInfoCompl, PdpInfo, CancelType,
216
+ FreezePTMSI, MSISDN, HlrNumber, MessageClass, PCO, CnDomain,
217
+ SupportedRatTypes, CurrentRatType, SessionId, SessionState,
218
+ SupplementaryServiceInfo, SmRpMr, SmRpOa, SmRpUi, SmRpCause,
219
+ SmRpMms, SmAlert, IMEI, ImeiCheckResult, NumVectorsReq,
220
+ SourceName, DestinationName, AnApdu, CauseRr, CauseBssap,
221
+ CauseSm, auth.RAND, auth.AUTS]):
222
+ pass
223
+
224
+ class MsgType(IntEnum):
225
+ """GSUP protocol message type. Keep this in sync with
226
+ https://gitea.osmocom.org/osmocom/libosmocore/src/branch/master/include/osmocom/gsm/gsup.h"""
227
+
228
+ UPDATE_LOCATION_REQUEST = 0x04
229
+ UPDATE_LOCATION_ERROR = 0x05
230
+ UPDATE_LOCATION_RESULT = 0x06
231
+
232
+ SEND_AUTH_INFO_REQUEST = 0x08
233
+ SEND_AUTH_INFO_ERROR = 0x09
234
+ SEND_AUTH_INFO_RESULT = 0x0a
235
+
236
+ AUTH_FAIL_REPORT = 0x0b
237
+
238
+ PURGE_MS_REQUEST = 0x0c
239
+ PURGE_MS_ERROR = 0x0d
240
+ PURGE_MS_RESULT = 0x0e
241
+
242
+ INSERT_DATA_REQUEST = 0x10
243
+ INSERT_DATA_ERROR = 0x11
244
+ INSERT_DATA_RESULT = 0x12
245
+
246
+ DELETE_DATA_REQUEST = 0x14
247
+ DELETE_DATA_ERROR = 0x15
248
+ DELETE_DATA_RESULT = 0x16
249
+
250
+ LOCATION_CANCEL_REQUEST = 0x1c
251
+ LOCATION_CANCEL_ERROR = 0x1d
252
+ LOCATION_CANCEL_RESULT = 0x1e
253
+
254
+ PROC_SS_REQUEST = 0x20
255
+ PROC_SS_ERROR = 0x21
256
+ PROC_SS_RESULT = 0x22
257
+
258
+ MO_FORWARD_SM_REQUEST = 0x24
259
+ MO_FORWARD_SM_ERROR = 0x25
260
+ MO_FORWARD_SM_RESULT = 0x26
261
+
262
+ MT_FORWARD_SM_REQUEST = 0x28
263
+ MT_FORWARD_SM_ERROR = 0x29
264
+ MT_FORWARD_SM_RESULT = 0x2a
265
+
266
+ READY_FOR_SM_REQUEST = 0x2c
267
+ READY_FOR_SM_ERROR = 0x2d
268
+ READY_FOR_SM_RESULT = 0x2e
269
+
270
+ CHECK_IMEI_REQUEST = 0x30
271
+ CHECK_IMEI_ERROR = 0x31
272
+ CHECK_IMEI_RESULT = 0x32
273
+
274
+ E_PREPARE_HANDOVER_REQUEST = 0x34
275
+ E_PREPARE_HANDOVER_ERROR = 0x35
276
+ E_PREPARE_HANDOVER_RESULT = 0x36
277
+
278
+ E_PREPARE_SUBSEQUENT_HANDOVER_REQUEST = 0x38
279
+ E_PREPARE_SUBSEQUENT_HANDOVER_ERROR = 0x39
280
+ E_PREPARE_SUBSEQUENT_HANDOVER_RESULT = 0x3a
281
+
282
+ E_SEND_END_SIGNAL_REQUEST = 0x3c
283
+ E_SEND_END_SIGNAL_ERROR = 0x3d
284
+ E_SEND_END_SIGNAL_RESULT = 0x3e
285
+
286
+ E_PROCESS_ACCESS_SIGNALLING_REQUEST = 0x40
287
+ E_FORWARD_ACCESS_SIGNALLING_REQUEST = 0x44
288
+
289
+ E_CLOSE = 0x47
290
+ E_ABORT = 0x4b
291
+
292
+ ROUTING_ERROR = 0x4e
293
+
294
+ EPDG_TUNNEL_REQUEST = 0x50
295
+ EPDG_TUNNEL_ERROR = 0x51
296
+ EPDG_TUNNEL_RESULT = 0x52
297
+
298
+ class GsupMessage:
299
+ """Represents a single message within the GSUP protocol."""
300
+
301
+ def __init__(self, msg_type: Union[MsgType, int]):
302
+ if isinstance(msg_type, MsgType):
303
+ self.msg_type = msg_type
304
+ else:
305
+ self.msg_type = MsgType(msg_type)
306
+ self.ies = tlv.IeCollection()
307
+
308
+ def __str__(self) -> str:
309
+ return '%s(%s, %s)' % (self.__class__.__name__, self.msg_type, str(self.ies))
310
+
311
+ def __repr__(self) -> str:
312
+ return '%s(%s, %s)' % (self.__class__.__name__, self.msg_type, str(self.ies))
313
+
314
+ @classmethod
315
+ def from_bytes(cls, encoded: bytes) -> 'GsupMessage':
316
+ """Create a GsupMessage instance from the decode of the given bytes."""
317
+ msg = GsupMessage(encoded[0])
318
+ msg.ies.from_bytes(encoded[1:])
319
+ return msg
320
+
321
+ @classmethod
322
+ def from_dict(cls, decoded: dict) -> 'GsupMessage':
323
+ """Create a GsupMessage instance from the decoded dict."""
324
+ msg = GsupMessage(MsgType[decoded['msg_type']])
325
+ msg.ies.from_dict(decoded['ies'])
326
+ return msg
327
+
328
+ def to_bytes(self):
329
+ """Encode a GsupMessage instance into bytes."""
330
+ return bytes([self.msg_type.value]) + self.ies.to_bytes()
331
+
332
+ def to_dict(self):
333
+ """Encode a GsupMessage instance into a json-serializable dict."""
334
+ return {'msg_type': self.msg_type.name,
335
+ 'ies': self.ies.to_dict()}