Max | 842d781 | 2017-11-01 18:11:24 +0100 | [diff] [blame] | 1 | /* mslot_class.c |
| 2 | * |
| 3 | * Copyright (C) 2012 Ivan Klyuchnikov |
| 4 | * Copyright (C) 2012 Andreas Eversberg <jolly@eversberg.eu> |
| 5 | * Copyright (C) 2013 by Holger Hans Peter Freyther |
| 6 | * Copyright (C) 2017 by sysmocom - s.f.m.c. GmbH <info@sysmocom.de> |
| 7 | * |
| 8 | * This program is free software; you can redistribute it and/or |
| 9 | * modify it under the terms of the GNU General Public License |
| 10 | * as published by the Free Software Foundation; either version 2 |
| 11 | * of the License, or (at your option) any later version. |
| 12 | * |
| 13 | * This program is distributed in the hope that it will be useful, |
| 14 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 15 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 16 | * GNU General Public License for more details. |
| 17 | * |
| 18 | * You should have received a copy of the GNU General Public License |
| 19 | * along with this program; if not, write to the Free Software |
| 20 | * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. |
| 21 | */ |
| 22 | |
| 23 | #include <mslot_class.h> |
Max | f633b8d | 2018-01-31 15:28:53 +0100 | [diff] [blame] | 24 | #include <gprs_debug.h> |
Max | 842d781 | 2017-11-01 18:11:24 +0100 | [diff] [blame] | 25 | |
| 26 | #include <osmocom/core/utils.h> |
| 27 | #include <osmocom/core/logging.h> |
| 28 | |
| 29 | #include <errno.h> |
| 30 | |
Max | 01bd0cc | 2018-01-23 20:58:49 +0100 | [diff] [blame] | 31 | /* 3GPP TS 45.002 Annex B Table B.1 */ |
Max | 842d781 | 2017-11-01 18:11:24 +0100 | [diff] [blame] | 32 | |
| 33 | struct gprs_ms_multislot_class { |
| 34 | uint8_t rx, tx, sum; /* Maximum Number of Slots: RX, Tx, Sum Rx+Tx */ |
| 35 | uint8_t ta, tb, ra, rb; /* Minimum Number of Slots */ |
| 36 | uint8_t type; /* Type of Mobile */ |
| 37 | }; |
| 38 | |
Max | 01bd0cc | 2018-01-23 20:58:49 +0100 | [diff] [blame] | 39 | static const struct gprs_ms_multislot_class gprs_ms_multislot_class[] = { |
Max | 842d781 | 2017-11-01 18:11:24 +0100 | [diff] [blame] | 40 | /* M-S Class | Max # of slots | Min # of slots | Type */ |
| 41 | /* | Rx Tx Sum | Tta Ttb Tra Trb | */ |
| 42 | /* N/A */ { MS_NA, MS_NA, MS_NA, MS_NA, MS_NA, MS_NA, MS_NA, MS_NA }, |
| 43 | /* 1 */ { 1, 1, 2, 3, 2, 4, 2, 1 }, |
| 44 | /* 2 */ { 2, 1, 3, 3, 2, 3, 1, 1 }, |
| 45 | /* 3 */ { 2, 2, 3, 3, 2, 3, 1, 1 }, |
| 46 | /* 4 */ { 3, 1, 4, 3, 1, 3, 1, 1 }, |
| 47 | /* 5 */ { 2, 2, 4, 3, 1, 3, 1, 1 }, |
| 48 | /* 6 */ { 3, 2, 4, 3, 1, 3, 1, 1 }, |
| 49 | /* 7 */ { 3, 3, 4, 3, 1, 3, 1, 1 }, |
| 50 | /* 8 */ { 4, 1, 5, 3, 1, 2, 1, 1 }, |
| 51 | /* 9 */ { 3, 2, 5, 3, 1, 2, 1, 1 }, |
| 52 | /* 10 */ { 4, 2, 5, 3, 1, 2, 1, 1 }, |
| 53 | /* 11 */ { 4, 3, 5, 3, 1, 2, 1, 1 }, |
| 54 | /* 12 */ { 4, 4, 5, 2, 1, 2, 1, 1 }, |
| 55 | /* 13 */ { 3, 3, MS_NA, MS_NA, MS_A, 3, MS_A, 2 }, |
| 56 | /* 14 */ { 4, 4, MS_NA, MS_NA, MS_A, 3, MS_A, 2 }, |
| 57 | /* 15 */ { 5, 5, MS_NA, MS_NA, MS_A, 3, MS_A, 2 }, |
| 58 | /* 16 */ { 6, 6, MS_NA, MS_NA, MS_A, 2, MS_A, 2 }, |
| 59 | /* 17 */ { 7, 7, MS_NA, MS_NA, MS_A, 1, 0, 2 }, |
| 60 | /* 18 */ { 8, 8, MS_NA, MS_NA, 0, 0, 0, 2 }, |
| 61 | /* 19 */ { 6, 2, MS_NA, 3, MS_B, 2, MS_C, 1 }, |
| 62 | /* 20 */ { 6, 3, MS_NA, 3, MS_B, 2, MS_C, 1 }, |
| 63 | /* 21 */ { 6, 4, MS_NA, 3, MS_B, 2, MS_C, 1 }, |
| 64 | /* 22 */ { 6, 4, MS_NA, 2, MS_B, 2, MS_C, 1 }, |
| 65 | /* 23 */ { 6, 6, MS_NA, 2, MS_B, 2, MS_C, 1 }, |
| 66 | /* 24 */ { 8, 2, MS_NA, 3, MS_B, 2, MS_C, 1 }, |
| 67 | /* 25 */ { 8, 3, MS_NA, 3, MS_B, 2, MS_C, 1 }, |
| 68 | /* 26 */ { 8, 4, MS_NA, 3, MS_B, 2, MS_C, 1 }, |
| 69 | /* 27 */ { 8, 4, MS_NA, 2, MS_B, 2, MS_C, 1 }, |
| 70 | /* 28 */ { 8, 6, MS_NA, 2, MS_B, 2, MS_C, 1 }, |
| 71 | /* 29 */ { 8, 8, MS_NA, 2, MS_B, 2, MS_C, 1 }, |
Max | 01bd0cc | 2018-01-23 20:58:49 +0100 | [diff] [blame] | 72 | /* 30 */ { 5, 1, 6, 2, 1, 1, 1, 1 }, |
| 73 | /* 31 */ { 5, 2, 6, 2, 1, 1, 1, 1 }, |
| 74 | /* 32 */ { 5, 3, 6, 2, 1, 1, 1, 1 }, |
| 75 | /* 33 */ { 5, 4, 6, 2, 1, 1, 1, 1 }, |
| 76 | /* 34 */ { 5, 5, 6, 2, 1, 1, 1, 1 }, |
| 77 | /* 35 */ { 5, 1, 6, 2, 1, MS_TO, 1, 1 }, |
| 78 | /* 36 */ { 5, 2, 6, 2, 1, MS_TO, 1, 1 }, |
| 79 | /* 37 */ { 5, 3, 6, 2, 1, MS_TO, 1, 1 }, |
| 80 | /* 38 */ { 5, 4, 6, 2, 1, MS_TO, 1, 1 }, |
| 81 | /* 39 */ { 5, 5, 6, 2, 1, MS_TO, 1, 1 }, |
| 82 | /* 40 */ { 6, 1, 7, 1, 1, 1, MS_TO, 1 }, |
| 83 | /* 41 */ { 6, 2, 7, 1, 1, 1, MS_TO, 1 }, |
| 84 | /* 42 */ { 6, 3, 7, 1, 1, 1, MS_TO, 1 }, |
| 85 | /* 43 */ { 6, 4, 7, 1, 1, 1, MS_TO, 1 }, |
| 86 | /* 44 */ { 6, 5, 7, 1, 1, 1, MS_TO, 1 }, |
| 87 | /* 45 */ { 6, 6, 7, 1, 1, 1, MS_TO, 1 }, |
Max | 842d781 | 2017-11-01 18:11:24 +0100 | [diff] [blame] | 88 | }; |
| 89 | |
| 90 | static inline const struct gprs_ms_multislot_class *get_mslot_table(uint8_t ms_cl) |
| 91 | { |
| 92 | uint8_t index = ms_cl ? ms_cl : DEFAULT_MSLOT_CLASS; |
| 93 | |
| 94 | if (ms_cl >= ARRAY_SIZE(gprs_ms_multislot_class)) |
| 95 | index = 0; |
| 96 | |
| 97 | return &gprs_ms_multislot_class[index]; |
| 98 | } |
| 99 | |
Max | 327e121 | 2017-11-21 13:01:19 +0100 | [diff] [blame] | 100 | uint8_t mslot_class_max() |
| 101 | { |
| 102 | return ARRAY_SIZE(gprs_ms_multislot_class); |
| 103 | } |
| 104 | |
Max | 842d781 | 2017-11-01 18:11:24 +0100 | [diff] [blame] | 105 | uint8_t mslot_class_get_ta(uint8_t ms_cl) |
| 106 | { |
| 107 | return get_mslot_table(ms_cl)->ta; |
| 108 | } |
| 109 | |
| 110 | /* TODO: Set it to 1 if FH is implemented and enabled |
| 111 | * MS_A and MS_B are 0 iff FH is disabled and there is no Tx/Rx change. |
| 112 | * This is never the case with the current implementation, so 1 will always be used. */ |
| 113 | uint8_t mslot_class_get_tb(uint8_t ms_cl) |
| 114 | { |
| 115 | const struct gprs_ms_multislot_class *t = get_mslot_table(ms_cl); |
| 116 | |
| 117 | switch (t->tb) { |
| 118 | case MS_A: |
| 119 | return 0; |
| 120 | case MS_B: |
| 121 | return 1; |
| 122 | default: |
| 123 | return t->tb; |
| 124 | } |
| 125 | } |
| 126 | |
Max | 01bd0cc | 2018-01-23 20:58:49 +0100 | [diff] [blame] | 127 | uint8_t mslot_class_get_ra(uint8_t ms_cl, uint8_t ta) |
Max | 842d781 | 2017-11-01 18:11:24 +0100 | [diff] [blame] | 128 | { |
Max | 01bd0cc | 2018-01-23 20:58:49 +0100 | [diff] [blame] | 129 | const struct gprs_ms_multislot_class *t = get_mslot_table(ms_cl); |
| 130 | |
| 131 | switch (t->ra) { |
| 132 | case MS_TO: |
| 133 | return ta + 1; |
| 134 | default: |
| 135 | return t->ra; |
| 136 | } |
Max | 842d781 | 2017-11-01 18:11:24 +0100 | [diff] [blame] | 137 | } |
| 138 | |
Max | 01bd0cc | 2018-01-23 20:58:49 +0100 | [diff] [blame] | 139 | uint8_t mslot_class_get_rb(uint8_t ms_cl, uint8_t ta) |
Max | 842d781 | 2017-11-01 18:11:24 +0100 | [diff] [blame] | 140 | { |
| 141 | const struct gprs_ms_multislot_class *t = get_mslot_table(ms_cl); |
| 142 | |
| 143 | switch (t->rb) { |
| 144 | case MS_A: |
| 145 | return 0; |
| 146 | case MS_C: |
| 147 | return 1; |
Max | 01bd0cc | 2018-01-23 20:58:49 +0100 | [diff] [blame] | 148 | case MS_TO: |
| 149 | return ta; |
Max | 842d781 | 2017-11-01 18:11:24 +0100 | [diff] [blame] | 150 | default: |
| 151 | return t->rb; |
| 152 | } |
| 153 | } |
| 154 | |
| 155 | uint8_t mslot_class_get_tx(uint8_t ms_cl) |
| 156 | { |
| 157 | return get_mslot_table(ms_cl)->tx; |
| 158 | } |
| 159 | |
| 160 | uint8_t mslot_class_get_rx(uint8_t ms_cl) |
| 161 | { |
| 162 | return get_mslot_table(ms_cl)->rx; |
| 163 | } |
| 164 | |
| 165 | uint8_t mslot_class_get_sum(uint8_t ms_cl) |
| 166 | { |
| 167 | return get_mslot_table(ms_cl)->sum; |
| 168 | } |
| 169 | |
| 170 | uint8_t mslot_class_get_type(uint8_t ms_cl) |
| 171 | { |
| 172 | return get_mslot_table(ms_cl)->type; |
| 173 | } |
Max | f633b8d | 2018-01-31 15:28:53 +0100 | [diff] [blame] | 174 | |
| 175 | /*! Fill in RX mask table for a given MS Class |
| 176 | * |
| 177 | * \param[in] ms_cl MS Class pointer |
| 178 | * \param[in] num_tx Number of TX slots to consider |
| 179 | * \param[out] rx_mask RX mask table |
| 180 | */ |
| 181 | void mslot_fill_rx_mask(uint8_t mslot_class, uint8_t num_tx, uint8_t *rx_mask) |
| 182 | { |
| 183 | static const char *digit[10] = { "0","1","2","3","4","5","6","7","8","9" }; |
| 184 | uint8_t Tx = mslot_class_get_tx(mslot_class), /* Max number of Tx slots */ |
| 185 | Sum = mslot_class_get_sum(mslot_class), /* Max number of Tx + Rx slots */ |
| 186 | Type = mslot_class_get_type(mslot_class), /* Type of Mobile */ |
| 187 | Tta = mslot_class_get_ta(mslot_class), /* Minimum number of slots */ |
| 188 | Ttb = mslot_class_get_tb(mslot_class), |
| 189 | /* FIXME: use actual TA offset for computation - make sure to adjust "1 + MS_TO" accordingly |
| 190 | see also "Offset required" bit in 3GPP TS 24.008 ยง10.5.1.7 */ |
| 191 | Tra = mslot_class_get_ra(mslot_class, 0), |
| 192 | Trb = mslot_class_get_rb(mslot_class, 0); |
| 193 | |
| 194 | if (num_tx == 1) /* it's enough to log this once per TX slot set iteration */ |
| 195 | LOGP(DRLCMAC, LOGL_DEBUG, |
| 196 | "Rx=%d Tx=%d Sum Rx+Tx=%s, Tta=%s Ttb=%d, Tra=%d Trb=%d, Type=%d\n", |
| 197 | mslot_class_get_rx(mslot_class), Tx, |
| 198 | (Sum == MS_NA) ? "N/A" : digit[Sum], |
| 199 | (Tta == MS_NA) ? "N/A" : digit[Tta], Ttb, Tra, Trb, Type); |
| 200 | |
| 201 | if (Type == 1) { |
| 202 | rx_mask[MASK_TT] = (0x100 >> OSMO_MAX(Ttb, Tta)) - 1; |
| 203 | rx_mask[MASK_TT] &= ~((1 << (Trb + num_tx)) - 1); |
| 204 | rx_mask[MASK_TR] = (0x100 >> Ttb) - 1; |
| 205 | rx_mask[MASK_TR] &= ~((1 << (OSMO_MAX(Trb, Tra) + num_tx)) - 1); |
| 206 | } else { |
| 207 | /* Class type 2 MS have independant RX and TX */ |
| 208 | rx_mask[MASK_TT] = 0xff; |
| 209 | rx_mask[MASK_TR] = 0xff; |
| 210 | } |
| 211 | |
| 212 | rx_mask[MASK_TT] = (rx_mask[MASK_TT] << 3) | (rx_mask[MASK_TT] >> 5); |
| 213 | rx_mask[MASK_TR] = (rx_mask[MASK_TR] << 3) | (rx_mask[MASK_TR] >> 5); |
| 214 | } |
Max | adca67b | 2018-01-31 15:22:36 +0100 | [diff] [blame] | 215 | |
| 216 | /* look for USF, don't use USF=7 */ |
| 217 | int8_t find_free_usf(uint8_t usf_map) |
| 218 | { |
| 219 | uint8_t usf; |
| 220 | |
| 221 | if (usf_map == (1 << 7) - 1) |
| 222 | return -1; |
| 223 | |
| 224 | for (usf = 0; usf < 7; usf++) { |
| 225 | if (!(usf_map & (1 << usf))) |
| 226 | return usf; |
| 227 | } |
| 228 | |
| 229 | return -1; |
| 230 | } |
Max | 0cc7212 | 2018-01-31 17:00:06 +0100 | [diff] [blame] | 231 | |
| 232 | void masked_override_with(char *buf, uint8_t mask, char set_char) |
| 233 | { |
| 234 | int i; |
| 235 | for (i = 0; mask; i++, mask >>= 1) |
| 236 | if (mask & 1) |
| 237 | buf[i] = set_char; |
| 238 | } |