trxcon/l1sched: clarify TDMA Fn (mod 26) maps
[osmocom-bb.git] / src / target / trx_toolkit / ctrl_if_trx.py
blobe6fdaf1d6366fa75983823d9f27520841c2d2c37
1 # -*- coding: utf-8 -*-
3 # TRX Toolkit
4 # CTRL interface implementation (common commands)
6 # (C) 2016-2020 by Vadim Yanitskiy <axilirator@gmail.com>
7 # Contributions by sysmocom - s.f.m.c. GmbH
9 # All Rights Reserved
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.
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.
21 import logging as log
23 from ctrl_if import CTRLInterface
24 from data_msg import Msg
26 class CTRLInterfaceTRX(CTRLInterface):
27 """ CTRL interface handler for common transceiver management commands.
29 The following set of commands is mandatory for every transceiver:
31 - POWERON / POWEROFF - state management (running / idle),
32 - RXTUNE / TXTUNE - RX / TX frequency management,
33 - SETSLOT - timeslot management.
35 Additionally, there is an optional MEASURE command, which is used
36 by OsmocomBB to perform power measurement on a given frequency.
38 A given transceiver may also define its own command handler,
39 that is prioritized, i.e. it can overwrite any commands mentioned
40 above. If None is returned, a command is considered as unhandled.
42 == TRXD header version negotiation
44 Messages on DATA interface may have different header formats,
45 defined by a version number, which can be negotiated on the
46 control interface. By default, the Transceiver will use the
47 legacy header version (0).
49 The header format negotiation can be initiated by the L1
50 using 'SETFORMAT' command. If the requested version is not
51 supported by the transceiver, status code of the response
52 message should indicate a preferred (basically, the latest)
53 version. The format of this message is the following:
55 L1 -> TRX: CMD SETFORMAT VER_REQ
56 L1 <- TRX: RSP SETFORMAT VER_RSP VER_REQ
58 where:
60 - VER_REQ is the requested version (suggested by the L1),
61 - VER_RSP is either the applied version if matches VER_REQ,
62 or a preferred version if VER_REQ is not supported.
64 If the transceiver indicates VER_RSP different than VER_REQ,
65 the L1 is supposed to reinitiate the version negotiation
66 using the suggested VER_RSP. For example:
68 L1 -> TRX: CMD SETFORMAT 2
69 L1 <- TRX: RSP SETFORMAT 1 2
71 L1 -> TRX: CMD SETFORMAT 1
72 L1 <- TRX: RSP SETFORMAT 1 1
74 If no suitable VER_RSP is found, or the VER_REQ is incorrect,
75 the status code in the response shall be -1.
77 As soon as VER_RSP matches VER_REQ in the response, the process
78 of negotiation is complete. Changing the header version is
79 supposed to be done before POWERON, but can be also done after.
81 """
83 def __init__(self, trx, *udp_link_args):
84 CTRLInterface.__init__(self, *udp_link_args)
86 # Link with Transceiver instance we belong to
87 self.trx = trx
89 def parse_cmd(self, request):
90 # Custom command handlers (prioritized)
91 res = self.trx.ctrl_cmd_handler(request)
92 if res is not None:
93 return res
95 # Power control
96 if self.verify_cmd(request, "POWERON", 0):
97 log.debug("(%s) Recv POWERON CMD" % self.trx)
99 # Ensure transceiver isn't working
100 if self.trx.running:
101 log.error("(%s) Transceiver already started" % self.trx)
102 return -1
104 # Ensure that transceiver is ready
105 if not self.trx.ready:
106 log.error("(%s) Transceiver is not ready" % self.trx)
107 return -1
109 log.info("(%s) Starting transceiver..." % self.trx)
110 self.trx.power_event_handler(poweron=True)
112 return 0
114 elif self.verify_cmd(request, "POWEROFF", 0):
115 log.debug("(%s) Recv POWEROFF cmd" % self.trx)
117 log.info("(%s) Stopping transceiver..." % self.trx)
118 self.trx.power_event_handler(poweron=False)
120 return 0
122 # Tuning Control
123 elif self.verify_cmd(request, "RXTUNE", 1):
124 log.debug("(%s) Recv RXTUNE cmd" % self.trx)
126 # TODO: check freq range
127 self.trx._rx_freq = int(request[1]) * 1000
128 return 0
130 elif self.verify_cmd(request, "TXTUNE", 1):
131 log.debug("(%s) Recv TXTUNE cmd" % self.trx)
133 # TODO: check freq range
134 self.trx._tx_freq = int(request[1]) * 1000
135 return 0
137 # Power measurement
138 if self.verify_cmd(request, "MEASURE", 1):
139 log.debug("(%s) Recv MEASURE cmd" % self.trx)
141 # Power Measurement interface is optional
142 # for Transceiver, thus may be uninitialized
143 if self.trx.pwr_meas is None:
144 log.error("(%s) Power Measurement interface is not "
145 "initialized => rejecting command" % self.trx)
146 return -1
148 # TODO: check freq range
149 meas_freq = int(request[1]) * 1000
150 meas_dbm = self.trx.pwr_meas.measure(meas_freq)
152 return (0, [str(meas_dbm)])
154 # Frequency hopping configuration (variable length list):
156 # CMD SETFH <HSN> <MAIO> <RXF1> <TXF1> [... <RXFN> <TXFN>]
158 # where <RXFN> and <TXFN> is a pair of Rx/Tx frequencies (in kHz)
159 # corresponding to one ARFCN the Mobile Allocation. Note that the
160 # channel list is expected to be sorted in ascending order.
161 if self.verify_cmd(request, "SETFH", 4, va = True):
162 log.debug("(%s) Recv SETFH cmd" % self.trx)
164 # Parse HSN and MAIO
165 hsn = int(request[1])
166 maio = int(request[2])
168 # Parse the list of hopping frequencies
169 ma = [int(f) * 1000 for f in request[3:]] # kHz -> Hz
170 ma = [(rx, tx) for rx, tx in zip(ma[0::2], ma[1::2])]
172 # Configure the hopping sequence generator
173 try:
174 self.trx.enable_fh(hsn, maio, ma)
175 return 0
176 except:
177 log.error("(%s) Failed to configure frequency hopping" % self.trx)
178 return -1
180 # TRXD header version negotiation
181 if self.verify_cmd(request, "SETFORMAT", 1):
182 log.debug("(%s) Recv SETFORMAT cmd" % self.trx)
184 # Parse the requested version
185 ver_req = int(request[1])
186 # ... and store current for logging
187 ver_cur = self.trx.data_if._hdr_ver
189 if ver_req < 0 or ver_req > Msg.CHDR_VERSION_MAX:
190 log.error("(%s) Incorrect TRXD header version %u"
191 % (self.trx, ver_req))
192 return -1
194 if not self.trx.data_if.set_hdr_ver(ver_req):
195 ver_rsp = self.trx.data_if.pick_hdr_ver(ver_req)
196 log.warn("(%s) Requested TRXD header version %u "
197 "is not supported, suggesting %u..."
198 % (self.trx, ver_req, ver_rsp))
199 return ver_rsp
201 log.info("(%s) TRXD header version %u -> %u"
202 % (self.trx, ver_cur, ver_req))
203 return ver_req
205 # Set Power Attenuation
206 if self.verify_cmd(request, "SETPOWER", 1):
207 log.debug("(%s) Recv SETPOWER cmd" % self.trx)
208 # Parse the requested Tx Power Attenuation
209 att_req = int(request[1])
210 self.trx.tx_att_base = att_req
211 return 0
213 # Retrieve Nominal Tx power
214 if self.verify_cmd(request, "NOMTXPOWER", 0):
215 log.debug("(%s) Recv NOMTXPOWER cmd" % self.trx)
216 return (0, [str(self.trx.tx_power_base)])
218 # Lock/Unlock RF emission+reception
219 if self.verify_cmd(request, "RFMUTE", 1):
220 log.debug("(%s) Recv RFMUTE cmd" % self.trx)
221 # Parse the requested RFMUTE state (1=locked, 0=unlocked)
222 self.trx.rf_muted = int(request[1]) > 0
223 return 0
225 # Wrong / unknown command
226 else:
227 # We don't care about other commands,
228 # so let's merely ignore them ;)
229 log.debug("(%s) Ignore CMD %s" % (self.trx, request[0]))
230 return 0