Holger Hans Peter Freyther | 17c31ce | 2013-08-24 18:31:27 +0200 | [diff] [blame] | 1 | /* Copied from gprs_bssgp_pcu.cpp |
| 2 | * |
| 3 | * Copyright (C) 2012 Ivan Klyuchnikov |
| 4 | * Copyright (C) 2013 by Holger Hans Peter Freyther |
| 5 | * |
| 6 | * This program is free software; you can redistribute it and/or |
| 7 | * modify it under the terms of the GNU General Public License |
| 8 | * as published by the Free Software Foundation; either version 2 |
| 9 | * of the License, or (at your option) any later version. |
| 10 | * |
| 11 | * This program is distributed in the hope that it will be useful, |
| 12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 14 | * GNU General Public License for more details. |
| 15 | * |
| 16 | * You should have received a copy of the GNU General Public License |
| 17 | * along with this program; if not, write to the Free Software |
| 18 | * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. |
| 19 | */ |
| 20 | |
| 21 | #include <tbf.h> |
| 22 | #include <gprs_rlcmac.h> |
| 23 | #include <gprs_debug.h> |
| 24 | |
| 25 | extern "C" { |
| 26 | #include <osmocom/core/msgb.h> |
| 27 | } |
| 28 | |
| 29 | #include <errno.h> |
| 30 | #include <string.h> |
| 31 | |
Holger Hans Peter Freyther | d1d114f | 2013-08-24 20:46:18 +0200 | [diff] [blame] | 32 | static inline void tbf_update_ms_class(struct gprs_rlcmac_tbf *tbf, |
| 33 | const uint8_t ms_class) |
| 34 | { |
| 35 | if (!tbf->ms_class && ms_class) |
| 36 | tbf->ms_class = ms_class; |
| 37 | } |
| 38 | |
Holger Hans Peter Freyther | d868928 | 2013-08-24 20:51:06 +0200 | [diff] [blame] | 39 | static inline void tbf_assign_imsi(struct gprs_rlcmac_tbf *tbf, |
| 40 | const char *imsi) |
| 41 | { |
| 42 | strncpy(tbf->meas.imsi, imsi, sizeof(tbf->meas.imsi) - 1); |
| 43 | } |
| 44 | |
Holger Hans Peter Freyther | 31d0df9 | 2013-08-24 20:42:45 +0200 | [diff] [blame] | 45 | static struct gprs_rlcmac_tbf *tbf_lookup_dl(const uint32_t tlli, const char *imsi) |
| 46 | { |
| 47 | /* TODO: look up by IMSI first, then tlli, then old_tlli */ |
| 48 | return tbf_by_tlli(tlli, GPRS_RLCMAC_DL_TBF); |
| 49 | } |
| 50 | |
| 51 | static int tbf_append_data(struct gprs_rlcmac_tbf *tbf, |
| 52 | struct gprs_rlcmac_bts *bts, |
| 53 | const uint8_t ms_class, |
| 54 | const uint16_t pdu_delay_csec, |
| 55 | const uint8_t *data, const uint16_t len) |
| 56 | { |
| 57 | LOGP(DRLCMAC, LOGL_INFO, "TBF: APPEND TFI: %u TLLI: 0x%08x\n", tbf->tfi, tbf->tlli); |
| 58 | if (tbf->state == GPRS_RLCMAC_WAIT_RELEASE) { |
| 59 | LOGP(DRLCMAC, LOGL_DEBUG, "TBF in WAIT RELEASE state " |
| 60 | "(T3193), so reuse TBF\n"); |
| 61 | memcpy(tbf->llc_frame, data, len); |
| 62 | tbf->llc_length = len; |
| 63 | /* reset rlc states */ |
| 64 | memset(&tbf->dir.dl, 0, sizeof(tbf->dir.dl)); |
| 65 | /* keep to flags */ |
| 66 | tbf->state_flags &= GPRS_RLCMAC_FLAG_TO_MASK; |
| 67 | tbf->state_flags &= ~(1 << GPRS_RLCMAC_FLAG_CCCH); |
Holger Hans Peter Freyther | d1d114f | 2013-08-24 20:46:18 +0200 | [diff] [blame] | 68 | tbf_update_ms_class(tbf, ms_class); |
Holger Hans Peter Freyther | 31d0df9 | 2013-08-24 20:42:45 +0200 | [diff] [blame] | 69 | tbf_update(tbf); |
| 70 | gprs_rlcmac_trigger_downlink_assignment(tbf, tbf, NULL); |
| 71 | } else { |
| 72 | /* the TBF exists, so we must write it in the queue |
| 73 | * we prepend lifetime in front of PDU */ |
| 74 | struct timeval *tv; |
| 75 | struct msgb *llc_msg = msgb_alloc(len + sizeof(*tv), |
| 76 | "llc_pdu_queue"); |
| 77 | if (!llc_msg) |
| 78 | return -ENOMEM; |
| 79 | tv = (struct timeval *)msgb_put(llc_msg, sizeof(*tv)); |
| 80 | |
| 81 | uint16_t delay_csec; |
| 82 | if (bts->force_llc_lifetime) |
| 83 | delay_csec = bts->force_llc_lifetime; |
| 84 | else |
| 85 | delay_csec = pdu_delay_csec; |
| 86 | /* keep timestap at 0 for infinite delay */ |
| 87 | if (delay_csec != 0xffff) { |
| 88 | /* calculate timestamp of timeout */ |
| 89 | gettimeofday(tv, NULL); |
| 90 | tv->tv_usec += (delay_csec % 100) * 10000; |
| 91 | tv->tv_sec += delay_csec / 100; |
| 92 | if (tv->tv_usec > 999999) { |
| 93 | tv->tv_usec -= 1000000; |
| 94 | tv->tv_sec++; |
| 95 | } |
| 96 | } |
| 97 | memcpy(msgb_put(llc_msg, len), data, len); |
| 98 | msgb_enqueue(&tbf->llc_queue, llc_msg); |
Holger Hans Peter Freyther | d1d114f | 2013-08-24 20:46:18 +0200 | [diff] [blame] | 99 | tbf_update_ms_class(tbf, ms_class); |
Holger Hans Peter Freyther | 31d0df9 | 2013-08-24 20:42:45 +0200 | [diff] [blame] | 100 | } |
| 101 | |
| 102 | return 0; |
| 103 | } |
| 104 | |
Holger Hans Peter Freyther | 443c822 | 2013-08-24 20:59:46 +0200 | [diff] [blame] | 105 | static int tbf_new_dl_assignment(struct gprs_rlcmac_bts *bts, |
| 106 | const char *imsi, |
| 107 | const uint32_t tlli, const uint8_t ms_class, |
| 108 | const uint8_t *data, const uint16_t len) |
| 109 | { |
| 110 | uint8_t trx, ta, ss; |
| 111 | int8_t use_trx; |
| 112 | struct gprs_rlcmac_tbf *old_tbf, *tbf; |
| 113 | int8_t tfi; /* must be signed */ |
| 114 | int rc; |
| 115 | |
| 116 | /* check for uplink data, so we copy our informations */ |
Holger Hans Peter Freyther | bb20b2c | 2013-08-24 21:22:16 +0200 | [diff] [blame^] | 117 | #warning "Do the same look up for IMSI, TLLI and OLD_TLLI" |
| 118 | #warning "Refactor the below lines... into a new method" |
Holger Hans Peter Freyther | 443c822 | 2013-08-24 20:59:46 +0200 | [diff] [blame] | 119 | tbf = tbf_by_tlli(tlli, GPRS_RLCMAC_UL_TBF); |
| 120 | if (tbf && tbf->dir.ul.contention_resolution_done |
| 121 | && !tbf->dir.ul.final_ack_sent) { |
| 122 | use_trx = tbf->trx; |
| 123 | ta = tbf->ta; |
| 124 | ss = 0; |
| 125 | old_tbf = tbf; |
| 126 | } else { |
| 127 | use_trx = -1; |
| 128 | /* we already have an uplink TBF, so we use that TA */ |
| 129 | if (tbf) |
| 130 | ta = tbf->ta; |
| 131 | else { |
| 132 | /* recall TA */ |
| 133 | rc = recall_timing_advance(tlli); |
| 134 | if (rc < 0) { |
| 135 | LOGP(DRLCMAC, LOGL_NOTICE, "TA unknown" |
| 136 | ", assuming 0\n"); |
| 137 | ta = 0; |
| 138 | } else |
| 139 | ta = rc; |
| 140 | } |
| 141 | ss = 1; /* PCH assignment only allows one timeslot */ |
| 142 | old_tbf = NULL; |
| 143 | } |
| 144 | |
| 145 | // Create new TBF (any TRX) |
Holger Hans Peter Freyther | bcafdf8 | 2013-08-24 21:13:31 +0200 | [diff] [blame] | 146 | #warning "Copy and paste with alloc_ul_tbf" |
Holger Hans Peter Freyther | 443c822 | 2013-08-24 20:59:46 +0200 | [diff] [blame] | 147 | tfi = tfi_find_free(bts, GPRS_RLCMAC_DL_TBF, &trx, use_trx); |
| 148 | if (tfi < 0) { |
| 149 | LOGP(DRLCMAC, LOGL_NOTICE, "No PDCH resource\n"); |
| 150 | /* FIXME: send reject */ |
| 151 | return -EBUSY; |
| 152 | } |
| 153 | /* set number of downlink slots according to multislot class */ |
| 154 | tbf = tbf_alloc(bts, tbf, GPRS_RLCMAC_DL_TBF, tfi, trx, ms_class, ss); |
| 155 | if (!tbf) { |
| 156 | LOGP(DRLCMAC, LOGL_NOTICE, "No PDCH ressource\n"); |
| 157 | /* FIXME: send reject */ |
| 158 | return -EBUSY; |
| 159 | } |
| 160 | tbf->tlli = tlli; |
| 161 | tbf->tlli_valid = 1; |
| 162 | tbf->ta = ta; |
| 163 | |
| 164 | LOGP(DRLCMAC, LOGL_DEBUG, |
| 165 | "TBF: [DOWNLINK] START TFI: %d TLLI: 0x%08x \n", |
| 166 | tbf->tfi, tbf->tlli); |
| 167 | |
| 168 | /* new TBF, so put first frame */ |
| 169 | memcpy(tbf->llc_frame, data, len); |
| 170 | tbf->llc_length = len; |
| 171 | |
| 172 | /* trigger downlink assignment and set state to ASSIGN. |
| 173 | * we don't use old_downlink, so the possible uplink is used |
| 174 | * to trigger downlink assignment. if there is no uplink, |
| 175 | * AGCH is used. */ |
| 176 | gprs_rlcmac_trigger_downlink_assignment(tbf, old_tbf, imsi); |
| 177 | |
| 178 | /* store IMSI for debugging purpose. TODO: it is more than debugging */ |
| 179 | tbf_assign_imsi(tbf, imsi); |
| 180 | return 0; |
| 181 | } |
| 182 | |
Holger Hans Peter Freyther | 17c31ce | 2013-08-24 18:31:27 +0200 | [diff] [blame] | 183 | /** |
| 184 | * TODO: split into unit test-able parts... |
| 185 | */ |
| 186 | int tbf_handle(struct gprs_rlcmac_bts *bts, |
| 187 | const uint32_t tlli, const char *imsi, |
Holger Hans Peter Freyther | 31d0df9 | 2013-08-24 20:42:45 +0200 | [diff] [blame] | 188 | const uint8_t ms_class, const uint16_t delay_csec, |
Holger Hans Peter Freyther | 17c31ce | 2013-08-24 18:31:27 +0200 | [diff] [blame] | 189 | const uint8_t *data, const uint16_t len) |
| 190 | { |
| 191 | struct gprs_rlcmac_tbf *tbf; |
Holger Hans Peter Freyther | 17c31ce | 2013-08-24 18:31:27 +0200 | [diff] [blame] | 192 | |
| 193 | /* check for existing TBF */ |
Holger Hans Peter Freyther | 31d0df9 | 2013-08-24 20:42:45 +0200 | [diff] [blame] | 194 | tbf = tbf_lookup_dl(tlli, imsi); |
| 195 | if (tbf) { |
| 196 | int rc = tbf_append_data(tbf, bts, ms_class, |
| 197 | delay_csec, data, len); |
Holger Hans Peter Freyther | 443c822 | 2013-08-24 20:59:46 +0200 | [diff] [blame] | 198 | if (rc >= 0) |
| 199 | tbf_assign_imsi(tbf, imsi); |
| 200 | return rc; |
| 201 | } |
Holger Hans Peter Freyther | 17c31ce | 2013-08-24 18:31:27 +0200 | [diff] [blame] | 202 | |
Holger Hans Peter Freyther | 443c822 | 2013-08-24 20:59:46 +0200 | [diff] [blame] | 203 | return tbf_new_dl_assignment(bts, imsi, tlli, ms_class, data, len); |
Holger Hans Peter Freyther | 17c31ce | 2013-08-24 18:31:27 +0200 | [diff] [blame] | 204 | } |