kirr has uploaded this change for review. ( https://gerrit.osmocom.org/c/osmocom-bb/+/40072?usp=email )
Change subject: trx_toolkit/data_msg: Switch Modulation to cdef class ......................................................................
trx_toolkit/data_msg: Switch Modulation to cdef class
- Put fields into the object struct; fields are now accessed directly via that C-level struct instead of via __dict__ lookup - Replace Enum via explicit registry as Enum magic does not work with cdef classes. - switch to invoke .pick and .pick_by_bl via C-level as those functions are only used from pyx code - cimport instead of import Modulation and its instances at the users
Change-Id: I3e42ddac2777d3bbdbcd12bcdb799098a6f54809 --- M src/target/trx_toolkit/_fake_trx.pyx A src/target/trx_toolkit/data_msg.pxd M src/target/trx_toolkit/data_msg.pyx 3 files changed, 45 insertions(+), 27 deletions(-)
git pull ssh://gerrit.osmocom.org:29418/osmocom-bb refs/changes/72/40072/1
diff --git a/src/target/trx_toolkit/_fake_trx.pyx b/src/target/trx_toolkit/_fake_trx.pyx index 5881cd1..fd12863 100644 --- a/src/target/trx_toolkit/_fake_trx.pyx +++ b/src/target/trx_toolkit/_fake_trx.pyx @@ -24,7 +24,7 @@ from burst_fwd cimport BurstForwarder from _clck_gen cimport CLCKGen from transceiver cimport Transceiver -from data_msg import Modulation +from data_msg cimport Modulation, ModGMSK from udp_link cimport _raise_oserr import gsm_shared cdef object TrainingSeqGMSK = gsm_shared.TrainingSeqGMSK @@ -207,7 +207,7 @@ msg.mod_type = Modulation.pick_by_bl(bl)
# Pick TSC (Training Sequence Code) and TSC set - if msg.mod_type is Modulation.ModGMSK: + if msg.mod_type is ModGMSK: ss = TrainingSeqGMSK.pick(src_msg.burst) # TODO -> pyx msg.tsc = ss.tsc if ss is not None else 0 msg.tsc_set = ss.tsc_set if ss is not None else 0 diff --git a/src/target/trx_toolkit/data_msg.pxd b/src/target/trx_toolkit/data_msg.pxd new file mode 100644 index 0000000..a1b2192 --- /dev/null +++ b/src/target/trx_toolkit/data_msg.pxd @@ -0,0 +1,15 @@ +# -*- coding: utf-8 -*- +# cython: language_level=3 + +cdef class Modulation: + cdef readonly int coding # Coding in TRXD header + cdef readonly int bl # Burst length + + @staticmethod + cdef Modulation pick(int coding) # -> Modulation | None + + @staticmethod + cdef Modulation pick_by_bl(int bl) # -> Modulation | None + + +cdef Modulation ModGMSK diff --git a/src/target/trx_toolkit/data_msg.pyx b/src/target/trx_toolkit/data_msg.pyx index 0da4f7c..81f93ca 100644 --- a/src/target/trx_toolkit/data_msg.pyx +++ b/src/target/trx_toolkit/data_msg.pyx @@ -23,7 +23,6 @@ import abc
from typing import List -from enum import Enum from array import array
import gsm_shared @@ -35,36 +34,40 @@ def _bu2s(x): return struct.unpack('b', struct.pack('B', x))[0]
-class Modulation(Enum): - """ Modulation types defined in 3GPP TS 45.002 """ - ModGMSK = (0b0000, 1 * GMSK_BURST_LEN) - Mod8PSK = (0b0100, 3 * GMSK_BURST_LEN) - ModGMSK_AB = (0b0110, 1 * GMSK_BURST_LEN) - # ModRFU = (0b0111, 0) # Reserved for Future Use - Mod16QAM = (0b1000, 4 * GMSK_BURST_LEN) - Mod32QAM = (0b1010, 5 * GMSK_BURST_LEN) - ModAQPSK = (0b1100, 2 * GMSK_BURST_LEN) - - def __init__(self, coding, bl): - # Coding in TRXD header +cdef list[Modulation] _mod_registry = [] +cdef class Modulation: + def __cinit__(self, int coding, int bl): self.coding = coding - # Burst length self.bl = bl + _mod_registry.append(self)
- @classmethod - def pick(self, coding): - for mod in list(self): + @staticmethod + cdef Modulation pick(int coding): # -> Modulation | None + cdef Modulation mod + for mod in _mod_registry: if mod.coding == coding: return mod return None
- @classmethod - def pick_by_bl(self, bl): - for mod in list(self): + @staticmethod + cdef Modulation pick_by_bl(int bl): # -> Modulation | None + cdef Modulation mod + for mod in _mod_registry: if mod.bl == bl: return mod return None
+ +# Modulation types defined in 3GPP TS 45.002 +ModGMSK = Modulation(0b0000, 1 * GMSK_BURST_LEN) +Mod8PSK = Modulation(0b0100, 3 * GMSK_BURST_LEN) +ModGMSK_AB = Modulation(0b0110, 1 * GMSK_BURST_LEN) +# ModRFU = Modulation(0b0111, 0) # Reserved for Future Use +Mod16QAM = Modulation(0b1000, 4 * GMSK_BURST_LEN) +Mod32QAM = Modulation(0b1010, 5 * GMSK_BURST_LEN) +ModAQPSK = Modulation(0b1100, 2 * GMSK_BURST_LEN) + + class Msg(abc.ABC): ''' TRXD (DATA) message coding API (common part). '''
@@ -390,7 +393,7 @@ toa256 = None
# Version 0x01 specific (default values) - mod_type = Modulation.ModGMSK + mod_type = ModGMSK nope_ind = False
tsc_set = None @@ -473,7 +476,7 @@ if self.tsc_set is None: raise ValueError("TSC set is not set")
- if self.mod_type is Modulation.ModGMSK: + if self.mod_type is ModGMSK: if self.tsc_set not in range(0, 4): raise ValueError("TSC set %d is out of range" % self.tsc_set) else: @@ -526,8 +529,8 @@ self.toa256 = self.rand_toa256()
if self.ver >= 0x01: - self.mod_type = random.choice(list(Modulation)) - if self.mod_type is Modulation.ModGMSK: + self.mod_type = random.choice(_mod_registry) + if self.mod_type is ModGMSK: self.tsc_set = random.randint(0, 3) else: self.tsc_set = random.randint(0, 1) @@ -602,7 +605,7 @@ self.tsc_set = mts & 0b1 else: # GMSK: . . . . 0 0 S S - self.mod_type = Modulation.ModGMSK + self.mod_type = ModGMSK self.tsc_set = mts & 0b11
def append_hdr_to(self, buf):