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 configure(self, parameters): 117 """ Configures an LTE cell using a dictionary of parameters. 118 119 Args: 120 parameters: a configuration dictionary 121 """ 122 # Setup band 123 if self.PARAM_BAND not in parameters: 124 raise ValueError( 125 "The configuration dictionary must include a key '{}' with " 126 "the required band number.".format(self.PARAM_BAND)) 127 128 self.band = parameters[self.PARAM_BAND] 129 130 if self.PARAM_DL_EARFCN not in parameters: 131 band = int(self.band) 132 channel = int(lte_sim.LteSimulation.LOWEST_DL_CN_DICTIONARY[band] + 133 lte_sim.LteSimulation.LOWEST_DL_CN_DICTIONARY[band + 134 1]) / 2 135 self.log.warning( 136 "Key '{}' was not set. Using center band channel {} by default." 137 .format(self.PARAM_DL_EARFCN, channel)) 138 self.dl_channel = channel 139 else: 140 self.dl_channel = parameters[self.PARAM_DL_EARFCN] 141 142 # Set TDD-only configs 143 if self.get_duplex_mode() == lte_sim.DuplexMode.TDD: 144 145 # Sub-frame DL/UL config 146 if self.PARAM_FRAME_CONFIG not in parameters: 147 raise ValueError("When a TDD band is selected the frame " 148 "structure has to be indicated with the '{}' " 149 "key with a value from 0 to 6.".format( 150 self.PARAM_FRAME_CONFIG)) 151 152 self.dlul_config = int(parameters[self.PARAM_FRAME_CONFIG]) 153 154 # Special Sub-Frame configuration 155 if self.PARAM_SSF not in parameters: 156 self.log.warning( 157 'The {} parameter was not provided. Setting ' 158 'Special Sub-Frame config to 6 by default.'.format( 159 self.PARAM_SSF)) 160 self.ssf_config = 6 161 else: 162 self.ssf_config = int(parameters[self.PARAM_SSF]) 163 164 # Setup bandwidth 165 if self.PARAM_BW not in parameters: 166 raise ValueError( 167 "The config dictionary must include parameter {} with an " 168 "int value (to indicate 1.4 MHz use 14).".format( 169 self.PARAM_BW)) 170 171 bw = float(parameters[self.PARAM_BW]) 172 173 if abs(bw - 14) < 0.00000000001: 174 bw = 1.4 175 176 self.bandwidth = bw 177 178 # Setup mimo mode 179 if self.PARAM_MIMO not in parameters: 180 raise ValueError( 181 "The config dictionary must include parameter '{}' with the " 182 "mimo mode.".format(self.PARAM_MIMO)) 183 184 for mimo_mode in lte_sim.MimoMode: 185 if parameters[self.PARAM_MIMO] == mimo_mode.value: 186 self.mimo_mode = mimo_mode 187 break 188 else: 189 raise ValueError("The value of {} must be one of the following:" 190 "1x1, 2x2 or 4x4.".format(self.PARAM_MIMO)) 191 192 # Setup transmission mode 193 if self.PARAM_TM not in parameters: 194 raise ValueError( 195 "The config dictionary must include key {} with an " 196 "int value from 1 to 4 indicating transmission mode.".format( 197 self.PARAM_TM)) 198 199 for tm in lte_sim.TransmissionMode: 200 if parameters[self.PARAM_TM] == tm.value[2:]: 201 self.transmission_mode = tm 202 break 203 else: 204 raise ValueError( 205 "The {} key must have one of the following values:" 206 "1, 2, 3, 4, 7, 8 or 9.".format(self.PARAM_TM)) 207 208 # Setup scheduling mode 209 if self.PARAM_SCHEDULING not in parameters: 210 self.scheduling_mode = lte_sim.SchedulingMode.STATIC 211 self.log.warning( 212 "The test config does not include the '{}' key. Setting to " 213 "static by default.".format(self.PARAM_SCHEDULING)) 214 elif parameters[ 215 self.PARAM_SCHEDULING] == self.PARAM_SCHEDULING_DYNAMIC: 216 self.scheduling_mode = lte_sim.SchedulingMode.DYNAMIC 217 elif parameters[self.PARAM_SCHEDULING] == self.PARAM_SCHEDULING_STATIC: 218 self.scheduling_mode = lte_sim.SchedulingMode.STATIC 219 else: 220 raise ValueError("Key '{}' must have a value of " 221 "'dynamic' or 'static'.".format( 222 self.PARAM_SCHEDULING)) 223 224 if self.scheduling_mode == lte_sim.SchedulingMode.STATIC: 225 226 if self.PARAM_PADDING not in parameters: 227 self.log.warning( 228 "The '{}' parameter was not set. Enabling MAC padding by " 229 "default.".format(self.PARAM_PADDING)) 230 self.mac_padding = True 231 else: 232 self.mac_padding = parameters[self.PARAM_PADDING] 233 234 if self.PARAM_PATTERN not in parameters: 235 self.log.warning( 236 "The '{}' parameter was not set, using 100% RBs for both " 237 "DL and UL. To set the percentages of total RBs include " 238 "the '{}' key with a list of two ints indicating downlink " 239 "and uplink percentages.".format(self.PARAM_PATTERN, 240 self.PARAM_PATTERN)) 241 dl_pattern = 100 242 ul_pattern = 100 243 else: 244 dl_pattern = int(parameters[self.PARAM_PATTERN][0]) 245 ul_pattern = int(parameters[self.PARAM_PATTERN][1]) 246 247 if not (0 <= dl_pattern <= 100 and 0 <= ul_pattern <= 100): 248 raise ValueError( 249 "The scheduling pattern parameters need to be two " 250 "positive numbers between 0 and 100.") 251 252 self.dl_rbs, self.ul_rbs = (self.allocation_percentages_to_rbs( 253 dl_pattern, ul_pattern)) 254 255 # Check if 256 QAM is enabled for DL MCS 256 if self.PARAM_DL_256_QAM_ENABLED not in parameters: 257 self.log.warning("The key '{}' is not set in the test config. " 258 "Setting to false by default.".format( 259 self.PARAM_DL_256_QAM_ENABLED)) 260 261 self.dl_256_qam_enabled = parameters.get( 262 self.PARAM_DL_256_QAM_ENABLED, False) 263 264 # Look for a DL MCS configuration in the test parameters. If it is 265 # not present, use a default value. 266 if self.PARAM_DL_MCS in parameters: 267 self.dl_mcs = int(parameters[self.PARAM_DL_MCS]) 268 else: 269 self.log.warning( 270 'The test config does not include the {} key. Setting ' 271 'to the max value by default'.format(self.PARAM_DL_MCS)) 272 if self.dl_256_qam_enabled and self.bandwidth == 1.4: 273 self.dl_mcs = 26 274 elif (not self.dl_256_qam_enabled and self.mac_padding 275 and self.bandwidth != 1.4): 276 self.dl_mcs = 28 277 else: 278 self.dl_mcs = 27 279 280 # Check if 64 QAM is enabled for UL MCS 281 if self.PARAM_UL_64_QAM_ENABLED not in parameters: 282 self.log.warning("The key '{}' is not set in the config file. " 283 "Setting to false by default.".format( 284 self.PARAM_UL_64_QAM_ENABLED)) 285 286 self.ul_64_qam_enabled = parameters.get( 287 self.PARAM_UL_64_QAM_ENABLED, False) 288 289 # Look for an UL MCS configuration in the test parameters. If it is 290 # not present, use a default value. 291 if self.PARAM_UL_MCS in parameters: 292 self.ul_mcs = int(parameters[self.PARAM_UL_MCS]) 293 else: 294 self.log.warning( 295 'The test config does not include the {} key. Setting ' 296 'to the max value by default'.format(self.PARAM_UL_MCS)) 297 if self.ul_64_qam_enabled: 298 self.ul_mcs = 28 299 else: 300 self.ul_mcs = 23 301 302 # Configure the simulation for DRX mode 303 if self.PARAM_DRX in parameters and len( 304 parameters[self.PARAM_DRX]) == 5: 305 self.drx_connected_mode = True 306 self.drx_on_duration_timer = parameters[self.PARAM_DRX][0] 307 self.drx_inactivity_timer = parameters[self.PARAM_DRX][1] 308 self.drx_retransmission_timer = parameters[self.PARAM_DRX][2] 309 self.drx_long_cycle = parameters[self.PARAM_DRX][3] 310 try: 311 long_cycle = int(parameters[self.PARAM_DRX][3]) 312 long_cycle_offset = int(parameters[self.PARAM_DRX][4]) 313 if long_cycle_offset in range(0, long_cycle): 314 self.drx_long_cycle_offset = long_cycle_offset 315 else: 316 self.log.error( 317 ("The cDRX long cycle offset must be in the " 318 "range 0 to (long cycle - 1). Setting " 319 "long cycle offset to 0")) 320 self.drx_long_cycle_offset = 0 321 322 except ValueError: 323 self.log.error(("cDRX long cycle and long cycle offset " 324 "must be integers. Disabling cDRX mode.")) 325 self.drx_connected_mode = False 326 else: 327 self.log.warning( 328 ("DRX mode was not configured properly. " 329 "Please provide a list with the following values: " 330 "1) DRX on duration timer " 331 "2) Inactivity timer " 332 "3) Retransmission timer " 333 "4) Long DRX cycle duration " 334 "5) Long DRX cycle offset " 335 "Example: [2, 6, 16, 20, 0].")) 336 337 # Channel Control Indicator 338 if self.PARAM_CFI not in parameters: 339 self.log.warning('The {} parameter was not provided. Setting ' 340 'CFI to BESTEFFORT.'.format(self.PARAM_CFI)) 341 self.cfi = 'BESTEFFORT' 342 else: 343 self.cfi = parameters[self.PARAM_CFI] 344 345 # PHICH group size 346 if self.PARAM_PHICH not in parameters: 347 self.log.warning('The {} parameter was not provided. Setting ' 348 'PHICH group size to 1 by default.'.format( 349 self.PARAM_PHICH)) 350 self.phich = '1' 351 else: 352 if parameters[self.PARAM_PHICH] == '16': 353 self.phich = '1/6' 354 elif parameters[self.PARAM_PHICH] == '12': 355 self.phich = '1/2' 356 elif parameters[self.PARAM_PHICH] in ['1/6', '1/2', '1', '2']: 357 self.phich = parameters[self.PARAM_PHICH] 358 else: 359 raise ValueError('The {} parameter can only be followed by 1,' 360 '2, 1/2 (or 12) and 1/6 (or 16).'.format( 361 self.PARAM_PHICH)) 362 363 # Paging cycle duration 364 if self.PARAM_PAGING not in parameters: 365 self.log.warning('The {} parameter was not provided. Setting ' 366 'paging cycle duration to 1280 ms by ' 367 'default.'.format(self.PARAM_PAGING)) 368 self.paging_cycle = 1280 369 else: 370 try: 371 self.paging_cycle = int(parameters[self.PARAM_PAGING]) 372 except ValueError: 373 raise ValueError( 374 'The {} key has to be followed by the paging cycle ' 375 'duration in milliseconds.'.format(self.PARAM_PAGING)) 376 377 def get_duplex_mode(self): 378 """ Determines if the cell uses FDD or TDD duplex mode 379 380 Returns: 381 an variable of class DuplexMode indicating if band is FDD or TDD 382 """ 383 if 33 <= int(self.band) <= 46: 384 return lte_sim.DuplexMode.TDD 385 else: 386 return lte_sim.DuplexMode.FDD 387 388 def allocation_percentages_to_rbs(self, dl, ul): 389 """ Converts usage percentages to number of DL/UL RBs 390 391 Because not any number of DL/UL RBs can be obtained for a certain 392 bandwidth, this function calculates the number of RBs that most 393 closely matches the desired DL/UL percentages. 394 395 Args: 396 dl: desired percentage of downlink RBs 397 ul: desired percentage of uplink RBs 398 Returns: 399 a tuple indicating the number of downlink and uplink RBs 400 """ 401 402 # Validate the arguments 403 if (not 0 <= dl <= 100) or (not 0 <= ul <= 100): 404 raise ValueError("The percentage of DL and UL RBs have to be two " 405 "positive between 0 and 100.") 406 407 # Get min and max values from tables 408 max_rbs = lte_sim.TOTAL_RBS_DICTIONARY[self.bandwidth] 409 min_dl_rbs = lte_sim.MIN_DL_RBS_DICTIONARY[self.bandwidth] 410 min_ul_rbs = lte_sim.MIN_UL_RBS_DICTIONARY[self.bandwidth] 411 412 def percentage_to_amount(min_val, max_val, percentage): 413 """ Returns the integer between min_val and max_val that is closest 414 to percentage/100*max_val 415 """ 416 417 # Calculate the value that corresponds to the required percentage. 418 closest_int = round(max_val * percentage / 100) 419 # Cannot be less than min_val 420 closest_int = max(closest_int, min_val) 421 # RBs cannot be more than max_rbs 422 closest_int = min(closest_int, max_val) 423 424 return closest_int 425 426 # Calculate the number of DL RBs 427 428 # Get the number of DL RBs that corresponds to 429 # the required percentage. 430 desired_dl_rbs = percentage_to_amount(min_val=min_dl_rbs, 431 max_val=max_rbs, 432 percentage=dl) 433 434 if self.transmission_mode == lte_sim.TransmissionMode.TM3 or \ 435 self.transmission_mode == lte_sim.TransmissionMode.TM4: 436 437 # For TM3 and TM4 the number of DL RBs needs to be max_rbs or a 438 # multiple of the RBG size 439 440 if desired_dl_rbs == max_rbs: 441 dl_rbs = max_rbs 442 else: 443 dl_rbs = (math.ceil( 444 desired_dl_rbs / lte_sim.RBG_DICTIONARY[self.bandwidth]) * 445 lte_sim.RBG_DICTIONARY[self.bandwidth]) 446 447 else: 448 # The other TMs allow any number of RBs between 1 and max_rbs 449 dl_rbs = desired_dl_rbs 450 451 # Calculate the number of UL RBs 452 453 # Get the number of UL RBs that corresponds 454 # to the required percentage 455 desired_ul_rbs = percentage_to_amount(min_val=min_ul_rbs, 456 max_val=max_rbs, 457 percentage=ul) 458 459 # Create a list of all possible UL RBs assignment 460 # The standard allows any number that can be written as 461 # 2**a * 3**b * 5**c for any combination of a, b and c. 462 463 def pow_range(max_value, base): 464 """ Returns a range of all possible powers of base under 465 the given max_value. 466 """ 467 return range(int(math.ceil(math.log(max_value, base)))) 468 469 possible_ul_rbs = [ 470 2 ** a * 3 ** b * 5 ** c for a in pow_range(max_rbs, 2) 471 for b in pow_range(max_rbs, 3) 472 for c in pow_range(max_rbs, 5) 473 if 2 ** a * 3 ** b * 5 ** c <= max_rbs] # yapf: disable 474 475 # Find the value in the list that is closest to desired_ul_rbs 476 differences = [abs(rbs - desired_ul_rbs) for rbs in possible_ul_rbs] 477 ul_rbs = possible_ul_rbs[differences.index(min(differences))] 478 479 # Report what are the obtained RB percentages 480 self.log.info("Requested a {}% / {}% RB allocation. Closest possible " 481 "percentages are {}% / {}%.".format( 482 dl, ul, round(100 * dl_rbs / max_rbs), 483 round(100 * ul_rbs / max_rbs))) 484 485 return dl_rbs, ul_rbs 486