Wei-Yu Chen | 49950b9 | 2021-11-08 19:19:18 +0800 | [diff] [blame^] | 1 | """ |
| 2 | Copyright 2020 The Magma Authors. |
| 3 | |
| 4 | This source code is licensed under the BSD-style license found in the |
| 5 | LICENSE file in the root directory of this source tree. |
| 6 | |
| 7 | Unless required by applicable law or agreed to in writing, software |
| 8 | distributed under the License is distributed on an "AS IS" BASIS, |
| 9 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 10 | See the License for the specific language governing permissions and |
| 11 | limitations under the License. |
| 12 | """ |
| 13 | import textwrap |
| 14 | from typing import Optional, Union |
| 15 | |
| 16 | from exceptions import ConfigurationError |
| 17 | from logger import EnodebdLogger as logger |
| 18 | |
| 19 | DUPLEX_MAP = { |
| 20 | '01': 'TDDMode', |
| 21 | '02': 'FDDMode', |
| 22 | } |
| 23 | |
| 24 | BANDWIDTH_RBS_TO_MHZ_MAP = { |
| 25 | 'n6': 1.4, |
| 26 | 'n15': 3, |
| 27 | 'n25': 5, |
| 28 | 'n50': 10, |
| 29 | 'n75': 15, |
| 30 | 'n100': 20, |
| 31 | } |
| 32 | |
| 33 | BANDWIDTH_MHZ_LIST = {1.4, 3, 5, 10, 15, 20} |
| 34 | |
| 35 | |
| 36 | def duplex_mode(value: str) -> Optional[str]: |
| 37 | return DUPLEX_MAP.get(value) |
| 38 | |
| 39 | |
| 40 | def band_capability(value: str) -> str: |
| 41 | return ','.join([str(int(b, 16)) for b in textwrap.wrap(value, 2)]) |
| 42 | |
| 43 | |
| 44 | def gps_tr181(value: str) -> str: |
| 45 | """Convert GPS value (lat or lng) to float |
| 46 | |
| 47 | Per TR-181 specification, coordinates are returned in degrees, |
| 48 | multiplied by 1,000,000. |
| 49 | |
| 50 | Args: |
| 51 | value (string): GPS value (latitude or longitude) |
| 52 | Returns: |
| 53 | str: GPS value (latitude/longitude) in degrees |
| 54 | """ |
| 55 | try: |
| 56 | return str(float(value) / 1e6) |
| 57 | except Exception: # pylint: disable=broad-except |
| 58 | return value |
| 59 | |
| 60 | |
| 61 | def bandwidth(bandwidth_rbs: Union[str, int, float]) -> float: |
| 62 | """ |
| 63 | Map bandwidth in number of RBs to MHz |
| 64 | TODO: TR-196 spec says this should be '6' rather than 'n6', but |
| 65 | BaiCells eNodeB uses 'n6'. Need to resolve this. |
| 66 | |
| 67 | Args: |
| 68 | bandwidth_rbs (str): Bandwidth in number of RBs |
| 69 | Returns: |
| 70 | str: Bandwidth in MHz |
| 71 | """ |
| 72 | if bandwidth_rbs in BANDWIDTH_RBS_TO_MHZ_MAP: |
| 73 | return BANDWIDTH_RBS_TO_MHZ_MAP[bandwidth_rbs] |
| 74 | |
| 75 | logger.warning('Unknown bandwidth_rbs (%s)', str(bandwidth_rbs)) |
| 76 | if bandwidth_rbs in BANDWIDTH_MHZ_LIST: |
| 77 | return bandwidth_rbs |
| 78 | elif isinstance(bandwidth_rbs, str): |
| 79 | mhz = None |
| 80 | if bandwidth_rbs.isdigit(): |
| 81 | mhz = int(bandwidth_rbs) |
| 82 | elif bandwidth_rbs.replace('.', '', 1).isdigit(): |
| 83 | mhz = float(bandwidth_rbs) |
| 84 | if mhz in BANDWIDTH_MHZ_LIST: |
| 85 | return mhz |
| 86 | raise ConfigurationError( |
| 87 | 'Unknown bandwidth specification (%s)' % |
| 88 | str(bandwidth_rbs), |
| 89 | ) |