• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1#!/usr/bin/env python3
2#
3#   Copyright 2021 - The Android Open Source Project
4#
5#   Licensed under the Apache License, Version 2.0 (the 'License');
6#   you may not use this file except in compliance with the License.
7#   You may obtain a copy of the License at
8#
9#       http://www.apache.org/licenses/LICENSE-2.0
10#
11#   Unless required by applicable law or agreed to in writing, software
12#   distributed under the License is distributed on an 'AS IS' BASIS,
13#   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14#   See the License for the specific language governing permissions and
15#   limitations under the License.
16
17import acts.controllers.cellular_lib.BaseCellConfig as base_cell
18import acts.controllers.cellular_lib.LteSimulation as lte_sim
19import math
20
21
22class LteCellConfig(base_cell.BaseCellConfig):
23    """ Extension of the BaseBtsConfig to implement parameters that are
24         exclusive to LTE.
25
26    Attributes:
27        band: an integer indicating the required band number.
28        dlul_config: an integer indicating the TDD config number.
29        ssf_config: an integer indicating the Special Sub-Frame config.
30        bandwidth: a float indicating the required channel bandwidth.
31        mimo_mode: an instance of LteSimulation.MimoMode indicating the
32            required MIMO mode for the downlink signal.
33        transmission_mode: an instance of LteSimulation.TransmissionMode
34            indicating the required TM.
35        scheduling_mode: an instance of LteSimulation.SchedulingMode
36            indicating whether to use Static or Dynamic scheduling.
37        dl_rbs: an integer indicating the number of downlink RBs
38        ul_rbs: an integer indicating the number of uplink RBs
39        dl_mcs: an integer indicating the MCS for the downlink signal
40        ul_mcs: an integer indicating the MCS for the uplink signal
41        dl_256_qam_enabled: a boolean indicating if 256 QAM is enabled
42        ul_64_qam_enabled: a boolean indicating if 256 QAM is enabled
43        mac_padding: a boolean indicating whether RBs should be allocated
44            when there is no user data in static scheduling
45        dl_channel: an integer indicating the downlink channel number
46        cfi: an integer indicating the Control Format Indicator
47        paging_cycle: an integer indicating the paging cycle duration in
48            milliseconds
49        phich: a string indicating the PHICH group size parameter
50        drx_connected_mode: a boolean indicating whether cDRX mode is
51            on or off
52        drx_on_duration_timer: number of PDCCH subframes representing
53            DRX on duration
54        drx_inactivity_timer: number of PDCCH subframes to wait before
55            entering DRX mode
56        drx_retransmission_timer: number of consecutive PDCCH subframes
57            to wait for retransmission
58        drx_long_cycle: number of subframes representing one long DRX cycle.
59            One cycle consists of DRX sleep + DRX on duration
60        drx_long_cycle_offset: number representing offset in range
61            0 to drx_long_cycle - 1
62    """
63    PARAM_FRAME_CONFIG = "tddconfig"
64    PARAM_BW = "bw"
65    PARAM_SCHEDULING = "scheduling"
66    PARAM_SCHEDULING_STATIC = "static"
67    PARAM_SCHEDULING_DYNAMIC = "dynamic"
68    PARAM_PATTERN = "pattern"
69    PARAM_TM = "tm"
70    PARAM_BAND = "band"
71    PARAM_MIMO = "mimo"
72    PARAM_DL_MCS = 'dlmcs'
73    PARAM_UL_MCS = 'ulmcs'
74    PARAM_SSF = 'ssf'
75    PARAM_CFI = 'cfi'
76    PARAM_PAGING = 'paging'
77    PARAM_PHICH = 'phich'
78    PARAM_DRX = 'drx'
79    PARAM_PADDING = 'mac_padding'
80    PARAM_DL_256_QAM_ENABLED = "256_qam_dl_enabled"
81    PARAM_UL_64_QAM_ENABLED = "64_qam_ul_enabled"
82    PARAM_DL_EARFCN = 'dl_earfcn'
83
84    def __init__(self, log):
85        """ Initialize the base station config by setting all its
86        parameters to None.
87        Args:
88            log: logger object.
89        """
90        super().__init__(log)
91        self.band = None
92        self.dlul_config = None
93        self.ssf_config = None
94        self.bandwidth = None
95        self.mimo_mode = None
96        self.transmission_mode = None
97        self.scheduling_mode = None
98        self.dl_rbs = None
99        self.ul_rbs = None
100        self.dl_mcs = None
101        self.ul_mcs = None
102        self.dl_256_qam_enabled = None
103        self.ul_64_qam_enabled = None
104        self.mac_padding = None
105        self.dl_channel = None
106        self.cfi = None
107        self.paging_cycle = None
108        self.phich = None
109        self.drx_connected_mode = None
110        self.drx_on_duration_timer = None
111        self.drx_inactivity_timer = None
112        self.drx_retransmission_timer = None
113        self.drx_long_cycle = None
114        self.drx_long_cycle_offset = None
115
116    def __str__(self):
117        return str(vars(self))
118
119    def configure(self, parameters):
120        """ Configures an LTE cell using a dictionary of parameters.
121
122        Args:
123            parameters: a configuration dictionary
124        """
125        # Setup band
126        if self.PARAM_BAND not in parameters:
127            raise ValueError(
128                "The configuration dictionary must include a key '{}' with "
129                "the required band number.".format(self.PARAM_BAND))
130
131        self.band = parameters[self.PARAM_BAND]
132
133        if self.PARAM_DL_EARFCN not in parameters:
134            band = int(self.band)
135            channel = int(lte_sim.LteSimulation.LOWEST_DL_CN_DICTIONARY[band] +
136                          lte_sim.LteSimulation.LOWEST_DL_CN_DICTIONARY[band +
137                                                                        1]) / 2
138            self.log.warning(
139                "Key '{}' was not set. Using center band channel {} by default."
140                .format(self.PARAM_DL_EARFCN, channel))
141            self.dl_channel = channel
142        else:
143            self.dl_channel = parameters[self.PARAM_DL_EARFCN]
144
145        # Set TDD-only configs
146        if self.get_duplex_mode() == lte_sim.DuplexMode.TDD:
147
148            # Sub-frame DL/UL config
149            if self.PARAM_FRAME_CONFIG not in parameters:
150                raise ValueError("When a TDD band is selected the frame "
151                                 "structure has to be indicated with the '{}' "
152                                 "key with a value from 0 to 6.".format(
153                                     self.PARAM_FRAME_CONFIG))
154
155            self.dlul_config = int(parameters[self.PARAM_FRAME_CONFIG])
156
157            # Special Sub-Frame configuration
158            if self.PARAM_SSF not in parameters:
159                self.log.warning(
160                    'The {} parameter was not provided. Setting '
161                    'Special Sub-Frame config to 6 by default.'.format(
162                        self.PARAM_SSF))
163                self.ssf_config = 6
164            else:
165                self.ssf_config = int(parameters[self.PARAM_SSF])
166
167        # Setup bandwidth
168        if self.PARAM_BW not in parameters:
169            raise ValueError(
170                "The config dictionary must include parameter {} with an "
171                "int value (to indicate 1.4 MHz use 14).".format(
172                    self.PARAM_BW))
173
174        bw = float(parameters[self.PARAM_BW])
175
176        if abs(bw - 14) < 0.00000000001:
177            bw = 1.4
178
179        self.bandwidth = bw
180
181        # Setup mimo mode
182        if self.PARAM_MIMO not in parameters:
183            raise ValueError(
184                "The config dictionary must include parameter '{}' with the "
185                "mimo mode.".format(self.PARAM_MIMO))
186
187        for mimo_mode in lte_sim.MimoMode:
188            if parameters[self.PARAM_MIMO] == mimo_mode.value:
189                self.mimo_mode = mimo_mode
190                break
191        else:
192            raise ValueError("The value of {} must be one of the following:"
193                             "1x1, 2x2 or 4x4.".format(self.PARAM_MIMO))
194
195        # Setup transmission mode
196        if self.PARAM_TM not in parameters:
197            raise ValueError(
198                "The config dictionary must include key {} with an "
199                "int value from 1 to 4 indicating transmission mode.".format(
200                    self.PARAM_TM))
201
202        for tm in lte_sim.TransmissionMode:
203            if parameters[self.PARAM_TM] == tm.value[2:]:
204                self.transmission_mode = tm
205                break
206        else:
207            raise ValueError(
208                "The {} key must have one of the following values:"
209                "1, 2, 3, 4, 7, 8 or 9.".format(self.PARAM_TM))
210
211        # Setup scheduling mode
212        if self.PARAM_SCHEDULING not in parameters:
213            self.scheduling_mode = lte_sim.SchedulingMode.STATIC
214            self.log.warning(
215                "The test config does not include the '{}' key. Setting to "
216                "static by default.".format(self.PARAM_SCHEDULING))
217        elif parameters[
218                self.PARAM_SCHEDULING] == self.PARAM_SCHEDULING_DYNAMIC:
219            self.scheduling_mode = lte_sim.SchedulingMode.DYNAMIC
220        elif parameters[self.PARAM_SCHEDULING] == self.PARAM_SCHEDULING_STATIC:
221            self.scheduling_mode = lte_sim.SchedulingMode.STATIC
222        else:
223            raise ValueError("Key '{}' must have a value of "
224                             "'dynamic' or 'static'.".format(
225                                 self.PARAM_SCHEDULING))
226
227        if self.scheduling_mode == lte_sim.SchedulingMode.STATIC:
228
229            if self.PARAM_PADDING not in parameters:
230                self.log.warning(
231                    "The '{}' parameter was not set. Enabling MAC padding by "
232                    "default.".format(self.PARAM_PADDING))
233                self.mac_padding = True
234            else:
235                self.mac_padding = parameters[self.PARAM_PADDING]
236
237            if self.PARAM_PATTERN not in parameters:
238                self.log.warning(
239                    "The '{}' parameter was not set, using 100% RBs for both "
240                    "DL and UL. To set the percentages of total RBs include "
241                    "the '{}' key with a list of two ints indicating downlink "
242                    "and uplink percentages.".format(self.PARAM_PATTERN,
243                                                     self.PARAM_PATTERN))
244                dl_pattern = 100
245                ul_pattern = 100
246            else:
247                dl_pattern = int(parameters[self.PARAM_PATTERN][0])
248                ul_pattern = int(parameters[self.PARAM_PATTERN][1])
249
250            if not (0 <= dl_pattern <= 100 and 0 <= ul_pattern <= 100):
251                raise ValueError(
252                    "The scheduling pattern parameters need to be two "
253                    "positive numbers between 0 and 100.")
254
255            self.dl_rbs, self.ul_rbs = (self.allocation_percentages_to_rbs(
256                dl_pattern, ul_pattern))
257
258            # Check if 256 QAM is enabled for DL MCS
259            if self.PARAM_DL_256_QAM_ENABLED not in parameters:
260                self.log.warning("The key '{}' is not set in the test config. "
261                                 "Setting to false by default.".format(
262                                     self.PARAM_DL_256_QAM_ENABLED))
263
264            self.dl_256_qam_enabled = parameters.get(
265                self.PARAM_DL_256_QAM_ENABLED, False)
266
267            # Look for a DL MCS configuration in the test parameters. If it is
268            # not present, use a default value.
269            if self.PARAM_DL_MCS in parameters:
270                self.dl_mcs = int(parameters[self.PARAM_DL_MCS])
271            else:
272                self.log.warning(
273                    'The test config does not include the {} key. Setting '
274                    'to the max value by default'.format(self.PARAM_DL_MCS))
275                if self.dl_256_qam_enabled and self.bandwidth == 1.4:
276                    self.dl_mcs = 26
277                elif (not self.dl_256_qam_enabled and self.mac_padding
278                      and self.bandwidth != 1.4):
279                    self.dl_mcs = 28
280                else:
281                    self.dl_mcs = 27
282
283            # Check if 64 QAM is enabled for UL MCS
284            if self.PARAM_UL_64_QAM_ENABLED not in parameters:
285                self.log.warning("The key '{}' is not set in the config file. "
286                                 "Setting to false by default.".format(
287                                     self.PARAM_UL_64_QAM_ENABLED))
288
289            self.ul_64_qam_enabled = parameters.get(
290                self.PARAM_UL_64_QAM_ENABLED, False)
291
292            # Look for an UL MCS configuration in the test parameters. If it is
293            # not present, use a default value.
294            if self.PARAM_UL_MCS in parameters:
295                self.ul_mcs = int(parameters[self.PARAM_UL_MCS])
296            else:
297                self.log.warning(
298                    'The test config does not include the {} key. Setting '
299                    'to the max value by default'.format(self.PARAM_UL_MCS))
300                if self.ul_64_qam_enabled:
301                    self.ul_mcs = 28
302                else:
303                    self.ul_mcs = 23
304
305        # Configure the simulation for DRX mode
306        if self.PARAM_DRX in parameters and len(
307                parameters[self.PARAM_DRX]) == 5:
308            self.drx_connected_mode = True
309            self.drx_on_duration_timer = parameters[self.PARAM_DRX][0]
310            self.drx_inactivity_timer = parameters[self.PARAM_DRX][1]
311            self.drx_retransmission_timer = parameters[self.PARAM_DRX][2]
312            self.drx_long_cycle = parameters[self.PARAM_DRX][3]
313            try:
314                long_cycle = int(parameters[self.PARAM_DRX][3])
315                long_cycle_offset = int(parameters[self.PARAM_DRX][4])
316                if long_cycle_offset in range(0, long_cycle):
317                    self.drx_long_cycle_offset = long_cycle_offset
318                else:
319                    self.log.error(
320                        ("The cDRX long cycle offset must be in the "
321                         "range 0 to (long cycle  - 1). Setting "
322                         "long cycle offset to 0"))
323                    self.drx_long_cycle_offset = 0
324
325            except ValueError:
326                self.log.error(("cDRX long cycle and long cycle offset "
327                                "must be integers. Disabling cDRX mode."))
328                self.drx_connected_mode = False
329        else:
330            self.log.warning(
331                ("DRX mode was not configured properly. "
332                 "Please provide a list with the following values: "
333                 "1) DRX on duration timer "
334                 "2) Inactivity timer "
335                 "3) Retransmission timer "
336                 "4) Long DRX cycle duration "
337                 "5) Long DRX cycle offset "
338                 "Example: [2, 6, 16, 20, 0]."))
339
340        # Channel Control Indicator
341        if self.PARAM_CFI not in parameters:
342            self.log.warning('The {} parameter was not provided. Setting '
343                             'CFI to BESTEFFORT.'.format(self.PARAM_CFI))
344            self.cfi = 'BESTEFFORT'
345        else:
346            self.cfi = parameters[self.PARAM_CFI]
347
348        # PHICH group size
349        if self.PARAM_PHICH not in parameters:
350            self.log.warning('The {} parameter was not provided. Setting '
351                             'PHICH group size to 1 by default.'.format(
352                                 self.PARAM_PHICH))
353            self.phich = '1'
354        else:
355            if parameters[self.PARAM_PHICH] == '16':
356                self.phich = '1/6'
357            elif parameters[self.PARAM_PHICH] == '12':
358                self.phich = '1/2'
359            elif parameters[self.PARAM_PHICH] in ['1/6', '1/2', '1', '2']:
360                self.phich = parameters[self.PARAM_PHICH]
361            else:
362                raise ValueError('The {} parameter can only be followed by 1,'
363                                 '2, 1/2 (or 12) and 1/6 (or 16).'.format(
364                                     self.PARAM_PHICH))
365
366        # Paging cycle duration
367        if self.PARAM_PAGING not in parameters:
368            self.log.warning('The {} parameter was not provided. Setting '
369                             'paging cycle duration to 1280 ms by '
370                             'default.'.format(self.PARAM_PAGING))
371            self.paging_cycle = 1280
372        else:
373            try:
374                self.paging_cycle = int(parameters[self.PARAM_PAGING])
375            except ValueError:
376                raise ValueError(
377                    'The {} key has to be followed by the paging cycle '
378                    'duration in milliseconds.'.format(self.PARAM_PAGING))
379
380    def get_duplex_mode(self):
381        """ Determines if the cell uses FDD or TDD duplex mode
382
383        Returns:
384          an variable of class DuplexMode indicating if band is FDD or TDD
385        """
386        if 33 <= int(self.band) <= 46:
387            return lte_sim.DuplexMode.TDD
388        else:
389            return lte_sim.DuplexMode.FDD
390
391    def allocation_percentages_to_rbs(self, dl, ul):
392        """ Converts usage percentages to number of DL/UL RBs
393
394        Because not any number of DL/UL RBs can be obtained for a certain
395        bandwidth, this function calculates the number of RBs that most
396        closely matches the desired DL/UL percentages.
397
398        Args:
399            dl: desired percentage of downlink RBs
400            ul: desired percentage of uplink RBs
401        Returns:
402            a tuple indicating the number of downlink and uplink RBs
403        """
404
405        # Validate the arguments
406        if (not 0 <= dl <= 100) or (not 0 <= ul <= 100):
407            raise ValueError("The percentage of DL and UL RBs have to be two "
408                             "positive between 0 and 100.")
409
410        # Get min and max values from tables
411        max_rbs = lte_sim.TOTAL_RBS_DICTIONARY[self.bandwidth]
412        min_dl_rbs = lte_sim.MIN_DL_RBS_DICTIONARY[self.bandwidth]
413        min_ul_rbs = lte_sim.MIN_UL_RBS_DICTIONARY[self.bandwidth]
414
415        def percentage_to_amount(min_val, max_val, percentage):
416            """ Returns the integer between min_val and max_val that is closest
417            to percentage/100*max_val
418            """
419
420            # Calculate the value that corresponds to the required percentage.
421            closest_int = round(max_val * percentage / 100)
422            # Cannot be less than min_val
423            closest_int = max(closest_int, min_val)
424            # RBs cannot be more than max_rbs
425            closest_int = min(closest_int, max_val)
426
427            return closest_int
428
429        # Calculate the number of DL RBs
430
431        # Get the number of DL RBs that corresponds to
432        #  the required percentage.
433        desired_dl_rbs = percentage_to_amount(min_val=min_dl_rbs,
434                                              max_val=max_rbs,
435                                              percentage=dl)
436
437        if self.transmission_mode == lte_sim.TransmissionMode.TM3 or \
438                self.transmission_mode == lte_sim.TransmissionMode.TM4:
439
440            # For TM3 and TM4 the number of DL RBs needs to be max_rbs or a
441            # multiple of the RBG size
442
443            if desired_dl_rbs == max_rbs:
444                dl_rbs = max_rbs
445            else:
446                dl_rbs = (math.ceil(
447                    desired_dl_rbs / lte_sim.RBG_DICTIONARY[self.bandwidth]) *
448                          lte_sim.RBG_DICTIONARY[self.bandwidth])
449
450        else:
451            # The other TMs allow any number of RBs between 1 and max_rbs
452            dl_rbs = desired_dl_rbs
453
454        # Calculate the number of UL RBs
455
456        # Get the number of UL RBs that corresponds
457        # to the required percentage
458        desired_ul_rbs = percentage_to_amount(min_val=min_ul_rbs,
459                                              max_val=max_rbs,
460                                              percentage=ul)
461
462        # Create a list of all possible UL RBs assignment
463        # The standard allows any number that can be written as
464        # 2**a * 3**b * 5**c for any combination of a, b and c.
465
466        def pow_range(max_value, base):
467            """ Returns a range of all possible powers of base under
468              the given max_value.
469          """
470            return range(int(math.ceil(math.log(max_value, base))))
471
472        possible_ul_rbs = [
473            2 ** a * 3 ** b * 5 ** c for a in pow_range(max_rbs, 2)
474            for b in pow_range(max_rbs, 3)
475            for c in pow_range(max_rbs, 5)
476            if 2 ** a * 3 ** b * 5 ** c <= max_rbs]  # yapf: disable
477
478        # Find the value in the list that is closest to desired_ul_rbs
479        differences = [abs(rbs - desired_ul_rbs) for rbs in possible_ul_rbs]
480        ul_rbs = possible_ul_rbs[differences.index(min(differences))]
481
482        # Report what are the obtained RB percentages
483        self.log.info("Requested a {}% / {}% RB allocation. Closest possible "
484                      "percentages are {}% / {}%.".format(
485                          dl, ul, round(100 * dl_rbs / max_rbs),
486                          round(100 * ul_rbs / max_rbs)))
487
488        return dl_rbs, ul_rbs
489