Harald Welte | 59b0468 | 2009-06-10 05:40:52 +0800 | [diff] [blame] | 1 | /* Point-to-Point (PP) Short Message Service (SMS) |
| 2 | * Support on Mobile Radio Interface |
| 3 | * 3GPP TS 04.11 version 7.1.0 Release 1998 / ETSI TS 100 942 V7.1.0 */ |
| 4 | |
| 5 | /* (C) 2008 by Daniel Willmann <daniel@totalueberwachung.de> |
| 6 | * (C) 2009 by Harald Welte <laforge@gnumonks.org> |
| 7 | * |
| 8 | * All Rights Reserved |
| 9 | * |
| 10 | * This program is free software; you can redistribute it and/or modify |
| 11 | * it under the terms of the GNU General Public License as published by |
| 12 | * the Free Software Foundation; either version 2 of the License, or |
| 13 | * (at your option) any later version. |
| 14 | * |
| 15 | * This program is distributed in the hope that it will be useful, |
| 16 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 17 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 18 | * GNU General Public License for more details. |
| 19 | * |
| 20 | * You should have received a copy of the GNU General Public License along |
| 21 | * with this program; if not, write to the Free Software Foundation, Inc., |
| 22 | * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. |
| 23 | * |
| 24 | */ |
| 25 | |
| 26 | |
| 27 | #include <stdio.h> |
| 28 | #include <stdlib.h> |
| 29 | #include <string.h> |
| 30 | #include <errno.h> |
Harald Welte | b78996d | 2009-07-27 20:11:35 +0200 | [diff] [blame] | 31 | #include <time.h> |
Harald Welte | 59b0468 | 2009-06-10 05:40:52 +0800 | [diff] [blame] | 32 | #include <netinet/in.h> |
| 33 | |
| 34 | #include <openbsc/msgb.h> |
| 35 | #include <openbsc/tlv.h> |
| 36 | #include <openbsc/debug.h> |
| 37 | #include <openbsc/gsm_data.h> |
| 38 | #include <openbsc/gsm_subscriber.h> |
| 39 | #include <openbsc/gsm_04_11.h> |
| 40 | #include <openbsc/gsm_04_08.h> |
| 41 | #include <openbsc/gsm_utils.h> |
| 42 | #include <openbsc/abis_rsl.h> |
| 43 | #include <openbsc/signal.h> |
| 44 | #include <openbsc/db.h> |
Harald Welte | a837977 | 2009-06-20 22:36:41 +0200 | [diff] [blame] | 45 | #include <openbsc/talloc.h> |
Harald Welte | b78996d | 2009-07-27 20:11:35 +0200 | [diff] [blame] | 46 | #include <openbsc/transaction.h> |
Harald Welte | 68b7df2 | 2009-08-08 16:03:15 +0200 | [diff] [blame] | 47 | #include <openbsc/paging.h> |
Harald Welte | 09421d3 | 2009-08-09 14:59:02 +0200 | [diff] [blame] | 48 | #include <openbsc/bsc_rll.h> |
Holger Hans Peter Freyther | 735cd9e | 2009-08-10 07:54:02 +0200 | [diff] [blame] | 49 | #include <openbsc/chan_alloc.h> |
Harald Welte | 59b0468 | 2009-06-10 05:40:52 +0800 | [diff] [blame] | 50 | |
| 51 | #define GSM411_ALLOC_SIZE 1024 |
| 52 | #define GSM411_ALLOC_HEADROOM 128 |
| 53 | |
Harald Welte | 09421d3 | 2009-08-09 14:59:02 +0200 | [diff] [blame] | 54 | #define UM_SAPI_SMS 3 /* See GSM 04.05/04.06 */ |
| 55 | |
Harald Welte | a837977 | 2009-06-20 22:36:41 +0200 | [diff] [blame] | 56 | static void *tall_gsms_ctx; |
| 57 | |
Harald Welte | b78996d | 2009-07-27 20:11:35 +0200 | [diff] [blame] | 58 | static u_int32_t new_callref = 0x40000001; |
| 59 | |
Harald Welte | 68b7df2 | 2009-08-08 16:03:15 +0200 | [diff] [blame] | 60 | struct gsm_sms *sms_alloc(void) |
| 61 | { |
| 62 | return talloc_zero(tall_gsms_ctx, struct gsm_sms); |
| 63 | } |
| 64 | |
| 65 | void sms_free(struct gsm_sms *sms) |
| 66 | { |
| 67 | /* drop references to subscriber structure */ |
| 68 | if (sms->sender) |
| 69 | subscr_put(sms->sender); |
| 70 | if (sms->receiver) |
| 71 | subscr_put(sms->receiver); |
| 72 | |
| 73 | talloc_free(sms); |
| 74 | } |
| 75 | |
Harald Welte | 59b0468 | 2009-06-10 05:40:52 +0800 | [diff] [blame] | 76 | struct msgb *gsm411_msgb_alloc(void) |
| 77 | { |
Harald Welte | 9cfc935 | 2009-06-26 19:39:35 +0200 | [diff] [blame] | 78 | return msgb_alloc_headroom(GSM411_ALLOC_SIZE, GSM411_ALLOC_HEADROOM, |
| 79 | "GSM 04.11"); |
Harald Welte | 59b0468 | 2009-06-10 05:40:52 +0800 | [diff] [blame] | 80 | } |
| 81 | |
Harald Welte | 7e2f57d | 2009-07-04 17:39:00 +0200 | [diff] [blame] | 82 | static int gsm411_sendmsg(struct msgb *msg) |
Harald Welte | 59b0468 | 2009-06-10 05:40:52 +0800 | [diff] [blame] | 83 | { |
| 84 | if (msg->lchan) |
| 85 | msg->trx = msg->lchan->ts->trx; |
| 86 | |
| 87 | msg->l3h = msg->data; |
| 88 | |
Harald Welte | 68b7df2 | 2009-08-08 16:03:15 +0200 | [diff] [blame] | 89 | DEBUGP(DSMS, "GSM4.11 TX %s\n", hexdump(msg->data, msg->len)); |
| 90 | |
Harald Welte | 09421d3 | 2009-08-09 14:59:02 +0200 | [diff] [blame] | 91 | return rsl_data_request(msg, UM_SAPI_SMS); |
Harald Welte | 59b0468 | 2009-06-10 05:40:52 +0800 | [diff] [blame] | 92 | } |
| 93 | |
Harald Welte | 5fa17a2 | 2009-08-10 00:24:32 +0200 | [diff] [blame] | 94 | /* SMC TC1* is expired */ |
| 95 | static void cp_timer_expired(void *data) |
| 96 | { |
| 97 | struct gsm_trans *trans = data; |
| 98 | |
| 99 | DEBUGP(DSMS, "SMC Timer TC1* is expired, calling trans_free()\n"); |
| 100 | /* FIXME: we need to re-transmit the last CP-DATA 1..3 times */ |
| 101 | trans_free(trans); |
| 102 | } |
| 103 | |
Harald Welte | 7e2f57d | 2009-07-04 17:39:00 +0200 | [diff] [blame] | 104 | /* Prefix msg with a 04.08/04.11 CP header */ |
Harald Welte | b78996d | 2009-07-27 20:11:35 +0200 | [diff] [blame] | 105 | static int gsm411_cp_sendmsg(struct msgb *msg, struct gsm_trans *trans, |
| 106 | u_int8_t msg_type) |
Harald Welte | 7e2f57d | 2009-07-04 17:39:00 +0200 | [diff] [blame] | 107 | { |
| 108 | struct gsm48_hdr *gh; |
| 109 | |
| 110 | gh = (struct gsm48_hdr *) msgb_push(msg, sizeof(*gh)); |
| 111 | /* Outgoing needs the highest bit set */ |
Harald Welte | b78996d | 2009-07-27 20:11:35 +0200 | [diff] [blame] | 112 | gh->proto_discr = trans->protocol | (trans->transaction_id<<4); |
Harald Welte | 7e2f57d | 2009-07-04 17:39:00 +0200 | [diff] [blame] | 113 | gh->msg_type = msg_type; |
| 114 | |
Harald Welte | b78996d | 2009-07-27 20:11:35 +0200 | [diff] [blame] | 115 | /* assign the outgoing lchan */ |
| 116 | msg->lchan = trans->lchan; |
| 117 | |
| 118 | /* mobile originating */ |
| 119 | switch (gh->msg_type) { |
| 120 | case GSM411_MT_CP_DATA: |
| 121 | /* 5.2.3.1.2: enter MO-wait for CP-ack */ |
Harald Welte | 09421d3 | 2009-08-09 14:59:02 +0200 | [diff] [blame] | 122 | /* 5.2.3.2.3: enter MT-wait for CP-ACK */ |
Harald Welte | b78996d | 2009-07-27 20:11:35 +0200 | [diff] [blame] | 123 | trans->sms.cp_state = GSM411_CPS_WAIT_CP_ACK; |
Harald Welte | 5fa17a2 | 2009-08-10 00:24:32 +0200 | [diff] [blame] | 124 | trans->sms.cp_timer.data = trans; |
| 125 | trans->sms.cp_timer.cb = cp_timer_expired; |
| 126 | /* 5.3.2.1: Set Timer TC1A */ |
| 127 | bsc_schedule_timer(&trans->sms.cp_timer, GSM411_TMR_TC1A); |
Harald Welte | b78996d | 2009-07-27 20:11:35 +0200 | [diff] [blame] | 128 | break; |
| 129 | } |
| 130 | |
Harald Welte | 7e2f57d | 2009-07-04 17:39:00 +0200 | [diff] [blame] | 131 | return gsm411_sendmsg(msg); |
| 132 | } |
| 133 | |
| 134 | /* Prefix msg with a RP-DATA header and send as CP-DATA */ |
Harald Welte | b78996d | 2009-07-27 20:11:35 +0200 | [diff] [blame] | 135 | static int gsm411_rp_sendmsg(struct msgb *msg, struct gsm_trans *trans, |
| 136 | u_int8_t rp_msg_type, u_int8_t rp_msg_ref) |
Harald Welte | 7e2f57d | 2009-07-04 17:39:00 +0200 | [diff] [blame] | 137 | { |
| 138 | struct gsm411_rp_hdr *rp; |
Harald Welte | f2e680b | 2009-08-10 00:22:19 +0200 | [diff] [blame] | 139 | u_int8_t len = msg->len; |
Harald Welte | 7e2f57d | 2009-07-04 17:39:00 +0200 | [diff] [blame] | 140 | |
| 141 | /* GSM 04.11 RP-DATA header */ |
| 142 | rp = (struct gsm411_rp_hdr *)msgb_push(msg, sizeof(*rp)); |
Harald Welte | f2e680b | 2009-08-10 00:22:19 +0200 | [diff] [blame] | 143 | rp->len = len + 2; |
Harald Welte | 7e2f57d | 2009-07-04 17:39:00 +0200 | [diff] [blame] | 144 | rp->msg_type = rp_msg_type; |
| 145 | rp->msg_ref = rp_msg_ref; /* FIXME: Choose randomly */ |
| 146 | |
Harald Welte | b78996d | 2009-07-27 20:11:35 +0200 | [diff] [blame] | 147 | return gsm411_cp_sendmsg(msg, trans, GSM411_MT_CP_DATA); |
Harald Welte | 7e2f57d | 2009-07-04 17:39:00 +0200 | [diff] [blame] | 148 | } |
| 149 | |
Harald Welte | 68b7df2 | 2009-08-08 16:03:15 +0200 | [diff] [blame] | 150 | static unsigned long gsm340_validity_period(u_int8_t sms_vpf, u_int8_t *sms_vp) |
Harald Welte | 59b0468 | 2009-06-10 05:40:52 +0800 | [diff] [blame] | 151 | { |
| 152 | u_int8_t vp; |
| 153 | unsigned long minutes; |
| 154 | |
Harald Welte | 68b7df2 | 2009-08-08 16:03:15 +0200 | [diff] [blame] | 155 | switch (sms_vpf) { |
Harald Welte | 59b0468 | 2009-06-10 05:40:52 +0800 | [diff] [blame] | 156 | case GSM340_TP_VPF_RELATIVE: |
| 157 | /* Chapter 9.2.3.12.1 */ |
Harald Welte | 68b7df2 | 2009-08-08 16:03:15 +0200 | [diff] [blame] | 158 | vp = *(sms_vp); |
Harald Welte | 59b0468 | 2009-06-10 05:40:52 +0800 | [diff] [blame] | 159 | if (vp <= 143) |
| 160 | minutes = vp + 1 * 5; |
| 161 | else if (vp <= 167) |
| 162 | minutes = 12*60 + (vp-143) * 30; |
| 163 | else if (vp <= 196) |
| 164 | minutes = vp-166 * 60 * 24; |
| 165 | else |
| 166 | minutes = vp-192 * 60 * 24 * 7; |
| 167 | break; |
| 168 | case GSM340_TP_VPF_ABSOLUTE: |
| 169 | /* Chapter 9.2.3.12.2 */ |
| 170 | /* FIXME: like service center time stamp */ |
| 171 | DEBUGP(DSMS, "VPI absolute not implemented yet\n"); |
| 172 | break; |
| 173 | case GSM340_TP_VPF_ENHANCED: |
| 174 | /* Chapter 9.2.3.12.3 */ |
| 175 | /* FIXME: implementation */ |
| 176 | DEBUGP(DSMS, "VPI enhanced not implemented yet\n"); |
| 177 | break; |
| 178 | } |
| 179 | return minutes; |
| 180 | } |
| 181 | |
| 182 | /* determine coding alphabet dependent on GSM 03.38 Section 4 DCS */ |
| 183 | enum sms_alphabet gsm338_get_sms_alphabet(u_int8_t dcs) |
| 184 | { |
| 185 | u_int8_t cgbits = dcs >> 4; |
| 186 | enum sms_alphabet alpha = DCS_NONE; |
| 187 | |
| 188 | if ((cgbits & 0xc) == 0) { |
| 189 | if (cgbits & 2) |
| 190 | DEBUGP(DSMS, "Compressed SMS not supported yet\n"); |
| 191 | |
| 192 | switch (dcs & 3) { |
| 193 | case 0: |
| 194 | alpha = DCS_7BIT_DEFAULT; |
| 195 | break; |
| 196 | case 1: |
| 197 | alpha = DCS_8BIT_DATA; |
| 198 | break; |
| 199 | case 2: |
| 200 | alpha = DCS_UCS2; |
| 201 | break; |
| 202 | } |
| 203 | } else if (cgbits == 0xc || cgbits == 0xd) |
| 204 | alpha = DCS_7BIT_DEFAULT; |
| 205 | else if (cgbits == 0xe) |
| 206 | alpha = DCS_UCS2; |
| 207 | else if (cgbits == 0xf) { |
| 208 | if (dcs & 4) |
| 209 | alpha = DCS_8BIT_DATA; |
| 210 | else |
| 211 | alpha = DCS_7BIT_DEFAULT; |
| 212 | } |
| 213 | |
| 214 | return alpha; |
| 215 | } |
| 216 | |
Harald Welte | 68b7df2 | 2009-08-08 16:03:15 +0200 | [diff] [blame] | 217 | static int gsm340_rx_sms_submit(struct msgb *msg, struct gsm_sms *gsms) |
Harald Welte | 59b0468 | 2009-06-10 05:40:52 +0800 | [diff] [blame] | 218 | { |
| 219 | if (db_sms_store(gsms) != 0) { |
| 220 | DEBUGP(DSMS, "Failed to store SMS in Database\n"); |
Harald Welte | 156c5e6 | 2009-07-05 14:02:46 +0200 | [diff] [blame] | 221 | return GSM411_RP_CAUSE_MO_NET_OUT_OF_ORDER; |
Harald Welte | 59b0468 | 2009-06-10 05:40:52 +0800 | [diff] [blame] | 222 | } |
Harald Welte | 68b7df2 | 2009-08-08 16:03:15 +0200 | [diff] [blame] | 223 | /* dispatch a signal to tell higher level about it */ |
| 224 | dispatch_signal(SS_SMS, S_SMS_SUBMITTED, gsms); |
Harald Welte | 59b0468 | 2009-06-10 05:40:52 +0800 | [diff] [blame] | 225 | return 0; |
| 226 | } |
| 227 | |
Harald Welte | 68b7df2 | 2009-08-08 16:03:15 +0200 | [diff] [blame] | 228 | /* generate a TPDU address field compliant with 03.40 sec. 9.1.2.5 */ |
| 229 | static int gsm340_gen_oa(u_int8_t *oa, unsigned int oa_len, |
| 230 | struct gsm_subscriber *subscr) |
Harald Welte | b78996d | 2009-07-27 20:11:35 +0200 | [diff] [blame] | 231 | { |
Harald Welte | 68b7df2 | 2009-08-08 16:03:15 +0200 | [diff] [blame] | 232 | int len_in_bytes; |
Harald Welte | b78996d | 2009-07-27 20:11:35 +0200 | [diff] [blame] | 233 | |
Harald Welte | 68b7df2 | 2009-08-08 16:03:15 +0200 | [diff] [blame] | 234 | oa[1] = 0xb9; /* networks-specific number, private numbering plan */ |
| 235 | |
| 236 | len_in_bytes = encode_bcd_number(oa, oa_len, 1, subscr->extension); |
| 237 | |
| 238 | /* GSM 03.40 tells us the length is in 'useful semi-octets' */ |
| 239 | oa[0] = strlen(subscr->extension) & 0xff; |
| 240 | |
| 241 | return len_in_bytes; |
Harald Welte | b78996d | 2009-07-27 20:11:35 +0200 | [diff] [blame] | 242 | } |
| 243 | |
| 244 | static u_int8_t bcdify(u_int8_t value) |
| 245 | { |
| 246 | u_int8_t ret; |
| 247 | |
Harald Welte | 68b7df2 | 2009-08-08 16:03:15 +0200 | [diff] [blame] | 248 | ret = value / 10; |
| 249 | ret |= (value % 10) << 4; |
Harald Welte | b78996d | 2009-07-27 20:11:35 +0200 | [diff] [blame] | 250 | |
| 251 | return ret; |
| 252 | } |
| 253 | |
| 254 | /* Generate 03.40 TP-SCTS */ |
| 255 | static void gsm340_gen_scts(u_int8_t *scts, time_t time) |
| 256 | { |
| 257 | struct tm *tm = localtime(&time); |
Harald Welte | b78996d | 2009-07-27 20:11:35 +0200 | [diff] [blame] | 258 | |
| 259 | *scts++ = bcdify(tm->tm_year % 100); |
| 260 | *scts++ = bcdify(tm->tm_mon); |
| 261 | *scts++ = bcdify(tm->tm_mday); |
| 262 | *scts++ = bcdify(tm->tm_hour); |
| 263 | *scts++ = bcdify(tm->tm_min); |
| 264 | *scts++ = bcdify(tm->tm_sec); |
| 265 | *scts++ = 0; /* FIXME: timezone */ |
| 266 | } |
| 267 | |
Harald Welte | 68b7df2 | 2009-08-08 16:03:15 +0200 | [diff] [blame] | 268 | /* generate a msgb containing a TPDU derived from struct gsm_sms, |
| 269 | * returns total size of TPDU */ |
| 270 | static int gsm340_gen_tpdu(struct msgb *msg, struct gsm_sms *sms) |
Harald Welte | b78996d | 2009-07-27 20:11:35 +0200 | [diff] [blame] | 271 | { |
Harald Welte | b78996d | 2009-07-27 20:11:35 +0200 | [diff] [blame] | 272 | u_int8_t *smsp; |
| 273 | u_int8_t oa[12]; /* max len per 03.40 */ |
| 274 | u_int8_t oa_len = 0; |
Harald Welte | 68b7df2 | 2009-08-08 16:03:15 +0200 | [diff] [blame] | 275 | unsigned int old_msg_len = msg->len; |
Harald Welte | b78996d | 2009-07-27 20:11:35 +0200 | [diff] [blame] | 276 | |
| 277 | /* generate first octet with masked bits */ |
| 278 | smsp = msgb_put(msg, 1); |
Harald Welte | 68b7df2 | 2009-08-08 16:03:15 +0200 | [diff] [blame] | 279 | /* TP-MTI (message type indicator) */ |
Harald Welte | b78996d | 2009-07-27 20:11:35 +0200 | [diff] [blame] | 280 | *smsp = GSM340_SMS_DELIVER_SC2MS; |
Harald Welte | 68b7df2 | 2009-08-08 16:03:15 +0200 | [diff] [blame] | 281 | /* TP-MMS (more messages to send) */ |
| 282 | if (0 /* FIXME */) |
Harald Welte | b78996d | 2009-07-27 20:11:35 +0200 | [diff] [blame] | 283 | *smsp |= 0x04; |
Harald Welte | 68b7df2 | 2009-08-08 16:03:15 +0200 | [diff] [blame] | 284 | /* TP-SRI(deliver)/SRR(submit) */ |
Harald Welte | b78996d | 2009-07-27 20:11:35 +0200 | [diff] [blame] | 285 | if (sms->status_rep_req) |
| 286 | *smsp |= 0x20; |
Harald Welte | 68b7df2 | 2009-08-08 16:03:15 +0200 | [diff] [blame] | 287 | /* TP-UDHI (indicating TP-UD contains a header) */ |
| 288 | if (sms->ud_hdr_ind) |
Harald Welte | b78996d | 2009-07-27 20:11:35 +0200 | [diff] [blame] | 289 | *smsp |= 0x40; |
Harald Welte | 68b7df2 | 2009-08-08 16:03:15 +0200 | [diff] [blame] | 290 | #if 0 |
| 291 | /* TP-RP (indicating that a reply path exists) */ |
Harald Welte | b78996d | 2009-07-27 20:11:35 +0200 | [diff] [blame] | 292 | if (sms-> |
| 293 | *smsp |= 0x80; |
| 294 | #endif |
| 295 | |
| 296 | /* generate originator address */ |
Harald Welte | 68b7df2 | 2009-08-08 16:03:15 +0200 | [diff] [blame] | 297 | oa_len = gsm340_gen_oa(oa, sizeof(oa), sms->sender); |
Harald Welte | b78996d | 2009-07-27 20:11:35 +0200 | [diff] [blame] | 298 | smsp = msgb_put(msg, oa_len); |
Harald Welte | b78996d | 2009-07-27 20:11:35 +0200 | [diff] [blame] | 299 | memcpy(smsp, oa, oa_len); |
| 300 | |
| 301 | /* generate TP-PID */ |
| 302 | smsp = msgb_put(msg, 1); |
| 303 | *smsp = sms->protocol_id; |
| 304 | |
| 305 | /* generate TP-DCS */ |
| 306 | smsp = msgb_put(msg, 1); |
| 307 | *smsp = sms->data_coding_scheme; |
| 308 | |
| 309 | /* generate TP-SCTS */ |
| 310 | smsp = msgb_put(msg, 7); |
| 311 | gsm340_gen_scts(smsp, time(NULL)); |
Harald Welte | 68b7df2 | 2009-08-08 16:03:15 +0200 | [diff] [blame] | 312 | |
Harald Welte | b78996d | 2009-07-27 20:11:35 +0200 | [diff] [blame] | 313 | /* generate TP-UDL */ |
| 314 | smsp = msgb_put(msg, 1); |
Harald Welte | 68b7df2 | 2009-08-08 16:03:15 +0200 | [diff] [blame] | 315 | *smsp = sms->user_data_len; |
Harald Welte | b78996d | 2009-07-27 20:11:35 +0200 | [diff] [blame] | 316 | |
| 317 | /* generate TP-UD */ |
Harald Welte | 68b7df2 | 2009-08-08 16:03:15 +0200 | [diff] [blame] | 318 | smsp = msgb_put(msg, sms->user_data_len); |
| 319 | memcpy(smsp, sms->user_data, sms->user_data_len); |
Harald Welte | b78996d | 2009-07-27 20:11:35 +0200 | [diff] [blame] | 320 | |
Harald Welte | 68b7df2 | 2009-08-08 16:03:15 +0200 | [diff] [blame] | 321 | return msg->len - old_msg_len; |
Harald Welte | b78996d | 2009-07-27 20:11:35 +0200 | [diff] [blame] | 322 | } |
| 323 | |
Harald Welte | 156c5e6 | 2009-07-05 14:02:46 +0200 | [diff] [blame] | 324 | /* process an incoming TPDU (called from RP-DATA) |
| 325 | * return value > 0: RP CAUSE for ERROR; < 0: silent error; 0 = success */ |
Harald Welte | 59b0468 | 2009-06-10 05:40:52 +0800 | [diff] [blame] | 326 | static int gsm340_rx_tpdu(struct msgb *msg) |
| 327 | { |
Harald Welte | 7535041 | 2009-07-23 18:46:00 +0200 | [diff] [blame] | 328 | struct gsm_bts *bts = msg->lchan->ts->trx->bts; |
Harald Welte | 59b0468 | 2009-06-10 05:40:52 +0800 | [diff] [blame] | 329 | u_int8_t *smsp = msgb_sms(msg); |
Harald Welte | 59b0468 | 2009-06-10 05:40:52 +0800 | [diff] [blame] | 330 | struct gsm_sms *gsms; |
Harald Welte | 68b7df2 | 2009-08-08 16:03:15 +0200 | [diff] [blame] | 331 | u_int8_t sms_mti, sms_mms, sms_vpf, sms_alphabet, sms_rp; |
| 332 | u_int8_t *sms_vp; |
Harald Welte | 59b0468 | 2009-06-10 05:40:52 +0800 | [diff] [blame] | 333 | u_int8_t da_len_bytes; |
| 334 | u_int8_t address_lv[12]; /* according to 03.40 / 9.1.2.5 */ |
| 335 | int rc = 0; |
| 336 | |
Harald Welte | 68b7df2 | 2009-08-08 16:03:15 +0200 | [diff] [blame] | 337 | gsms = sms_alloc(); |
| 338 | if (!gsms) |
Harald Welte | 156c5e6 | 2009-07-05 14:02:46 +0200 | [diff] [blame] | 339 | return GSM411_RP_CAUSE_MO_NET_OUT_OF_ORDER; |
Harald Welte | 59b0468 | 2009-06-10 05:40:52 +0800 | [diff] [blame] | 340 | |
| 341 | /* invert those fields where 0 means active/present */ |
Harald Welte | 68b7df2 | 2009-08-08 16:03:15 +0200 | [diff] [blame] | 342 | sms_mti = *smsp & 0x03; |
| 343 | sms_mms = !!(*smsp & 0x04); |
| 344 | sms_vpf = (*smsp & 0x18) >> 3; |
| 345 | gsms->status_rep_req = (*smsp & 0x20); |
| 346 | gsms->ud_hdr_ind = (*smsp & 0x40); |
| 347 | sms_rp = (*smsp & 0x80); |
Harald Welte | 59b0468 | 2009-06-10 05:40:52 +0800 | [diff] [blame] | 348 | |
| 349 | smsp++; |
Harald Welte | 68b7df2 | 2009-08-08 16:03:15 +0200 | [diff] [blame] | 350 | gsms->msg_ref = *smsp++; |
Harald Welte | 59b0468 | 2009-06-10 05:40:52 +0800 | [diff] [blame] | 351 | |
| 352 | /* length in bytes of the destination address */ |
| 353 | da_len_bytes = 2 + *smsp/2 + *smsp%2; |
| 354 | if (da_len_bytes > 12) { |
| 355 | DEBUGP(DSMS, "Destination Address > 12 bytes ?!?\n"); |
Harald Welte | 156c5e6 | 2009-07-05 14:02:46 +0200 | [diff] [blame] | 356 | rc = GSM411_RP_CAUSE_SEMANT_INC_MSG; |
Harald Welte | 59b0468 | 2009-06-10 05:40:52 +0800 | [diff] [blame] | 357 | goto out; |
| 358 | } |
Harald Welte | 3794e15 | 2009-06-12 02:42:11 +0800 | [diff] [blame] | 359 | memset(address_lv, 0, sizeof(address_lv)); |
Harald Welte | 59b0468 | 2009-06-10 05:40:52 +0800 | [diff] [blame] | 360 | memcpy(address_lv, smsp, da_len_bytes); |
| 361 | /* mangle first byte to reflect length in bytes, not digits */ |
Harald Welte | 3794e15 | 2009-06-12 02:42:11 +0800 | [diff] [blame] | 362 | address_lv[0] = da_len_bytes - 1; |
Harald Welte | 59b0468 | 2009-06-10 05:40:52 +0800 | [diff] [blame] | 363 | /* convert to real number */ |
Harald Welte | 68b7df2 | 2009-08-08 16:03:15 +0200 | [diff] [blame] | 364 | decode_bcd_number(gsms->dest_addr, sizeof(gsms->dest_addr), address_lv, 1); |
Harald Welte | 59b0468 | 2009-06-10 05:40:52 +0800 | [diff] [blame] | 365 | smsp += da_len_bytes; |
| 366 | |
Harald Welte | 68b7df2 | 2009-08-08 16:03:15 +0200 | [diff] [blame] | 367 | gsms->protocol_id = *smsp++; |
| 368 | gsms->data_coding_scheme = *smsp++; |
Harald Welte | 59b0468 | 2009-06-10 05:40:52 +0800 | [diff] [blame] | 369 | |
Harald Welte | 68b7df2 | 2009-08-08 16:03:15 +0200 | [diff] [blame] | 370 | sms_alphabet = gsm338_get_sms_alphabet(gsms->data_coding_scheme); |
Harald Welte | 59b0468 | 2009-06-10 05:40:52 +0800 | [diff] [blame] | 371 | |
Harald Welte | 68b7df2 | 2009-08-08 16:03:15 +0200 | [diff] [blame] | 372 | switch (sms_vpf) { |
Harald Welte | 59b0468 | 2009-06-10 05:40:52 +0800 | [diff] [blame] | 373 | case GSM340_TP_VPF_RELATIVE: |
Harald Welte | 68b7df2 | 2009-08-08 16:03:15 +0200 | [diff] [blame] | 374 | sms_vp = smsp++; |
Harald Welte | 59b0468 | 2009-06-10 05:40:52 +0800 | [diff] [blame] | 375 | break; |
| 376 | case GSM340_TP_VPF_ABSOLUTE: |
| 377 | case GSM340_TP_VPF_ENHANCED: |
Harald Welte | 68b7df2 | 2009-08-08 16:03:15 +0200 | [diff] [blame] | 378 | sms_vp = smsp; |
Harald Welte | 59b0468 | 2009-06-10 05:40:52 +0800 | [diff] [blame] | 379 | smsp += 7; |
| 380 | break; |
| 381 | default: |
| 382 | DEBUGP(DSMS, "SMS Validity period not implemented: 0x%02x\n", |
Harald Welte | 68b7df2 | 2009-08-08 16:03:15 +0200 | [diff] [blame] | 383 | sms_vpf); |
| 384 | return GSM411_RP_CAUSE_MO_NET_OUT_OF_ORDER; |
Harald Welte | 59b0468 | 2009-06-10 05:40:52 +0800 | [diff] [blame] | 385 | } |
Harald Welte | 68b7df2 | 2009-08-08 16:03:15 +0200 | [diff] [blame] | 386 | gsms->user_data_len = *smsp++; |
| 387 | if (gsms->user_data_len) { |
| 388 | memcpy(gsms->user_data, smsp, gsms->user_data_len); |
Harald Welte | 59b0468 | 2009-06-10 05:40:52 +0800 | [diff] [blame] | 389 | |
Harald Welte | 68b7df2 | 2009-08-08 16:03:15 +0200 | [diff] [blame] | 390 | switch (sms_alphabet) { |
Harald Welte | 59b0468 | 2009-06-10 05:40:52 +0800 | [diff] [blame] | 391 | case DCS_7BIT_DEFAULT: |
Harald Welte | 68b7df2 | 2009-08-08 16:03:15 +0200 | [diff] [blame] | 392 | gsm_7bit_decode(gsms->text, smsp, gsms->user_data_len); |
Harald Welte | 59b0468 | 2009-06-10 05:40:52 +0800 | [diff] [blame] | 393 | break; |
| 394 | case DCS_8BIT_DATA: |
| 395 | case DCS_UCS2: |
| 396 | case DCS_NONE: |
Harald Welte | 59b0468 | 2009-06-10 05:40:52 +0800 | [diff] [blame] | 397 | break; |
| 398 | } |
| 399 | } |
| 400 | |
| 401 | DEBUGP(DSMS, "SMS:\nMTI: 0x%02x, VPF: 0x%02x, MR: 0x%02x " |
| 402 | "PID: 0x%02x, DCS: 0x%02x, DA: %s, UserDataLength: 0x%02x " |
Harald Welte | 68b7df2 | 2009-08-08 16:03:15 +0200 | [diff] [blame] | 403 | "UserData: \"%s\"\n", sms_mti, sms_vpf, gsms->msg_ref, |
| 404 | gsms->protocol_id, gsms->data_coding_scheme, |
| 405 | gsms->dest_addr, gsms->user_data_len, |
| 406 | sms_alphabet == DCS_7BIT_DEFAULT ? gsms->text : |
| 407 | hexdump(gsms->user_data, gsms->user_data_len)); |
Harald Welte | 59b0468 | 2009-06-10 05:40:52 +0800 | [diff] [blame] | 408 | |
Harald Welte | 68b7df2 | 2009-08-08 16:03:15 +0200 | [diff] [blame] | 409 | gsms->sender = subscr_get(msg->lchan->subscr); |
Harald Welte | 59b0468 | 2009-06-10 05:40:52 +0800 | [diff] [blame] | 410 | |
Harald Welte | 68b7df2 | 2009-08-08 16:03:15 +0200 | [diff] [blame] | 411 | gsms->validity_minutes = gsm340_validity_period(sms_vpf, sms_vp); |
Harald Welte | 156c5e6 | 2009-07-05 14:02:46 +0200 | [diff] [blame] | 412 | |
Harald Welte | 68b7df2 | 2009-08-08 16:03:15 +0200 | [diff] [blame] | 413 | dispatch_signal(SS_SMS, 0, gsms); |
Harald Welte | 156c5e6 | 2009-07-05 14:02:46 +0200 | [diff] [blame] | 414 | |
Harald Welte | 59b0468 | 2009-06-10 05:40:52 +0800 | [diff] [blame] | 415 | /* determine gsms->receiver based on dialled number */ |
Harald Welte | 68b7df2 | 2009-08-08 16:03:15 +0200 | [diff] [blame] | 416 | gsms->receiver = subscr_get_by_extension(bts->network, gsms->dest_addr); |
Harald Welte | 59b0468 | 2009-06-10 05:40:52 +0800 | [diff] [blame] | 417 | if (!gsms->receiver) { |
| 418 | rc = 1; /* cause 1: unknown subscriber */ |
| 419 | goto out; |
| 420 | } |
| 421 | |
Harald Welte | 68b7df2 | 2009-08-08 16:03:15 +0200 | [diff] [blame] | 422 | switch (sms_mti) { |
Harald Welte | 59b0468 | 2009-06-10 05:40:52 +0800 | [diff] [blame] | 423 | case GSM340_SMS_SUBMIT_MS2SC: |
| 424 | /* MS is submitting a SMS */ |
Harald Welte | 68b7df2 | 2009-08-08 16:03:15 +0200 | [diff] [blame] | 425 | rc = gsm340_rx_sms_submit(msg, gsms); |
Harald Welte | 59b0468 | 2009-06-10 05:40:52 +0800 | [diff] [blame] | 426 | break; |
| 427 | case GSM340_SMS_COMMAND_MS2SC: |
| 428 | case GSM340_SMS_DELIVER_REP_MS2SC: |
Harald Welte | 68b7df2 | 2009-08-08 16:03:15 +0200 | [diff] [blame] | 429 | DEBUGP(DSMS, "Unimplemented MTI 0x%02x\n", sms_mti); |
Harald Welte | 156c5e6 | 2009-07-05 14:02:46 +0200 | [diff] [blame] | 430 | rc = GSM411_RP_CAUSE_IE_NOTEXIST; |
Harald Welte | 59b0468 | 2009-06-10 05:40:52 +0800 | [diff] [blame] | 431 | break; |
| 432 | default: |
Harald Welte | 68b7df2 | 2009-08-08 16:03:15 +0200 | [diff] [blame] | 433 | DEBUGP(DSMS, "Undefined MTI 0x%02x\n", sms_mti); |
Harald Welte | 156c5e6 | 2009-07-05 14:02:46 +0200 | [diff] [blame] | 434 | rc = GSM411_RP_CAUSE_IE_NOTEXIST; |
Harald Welte | 59b0468 | 2009-06-10 05:40:52 +0800 | [diff] [blame] | 435 | break; |
| 436 | } |
| 437 | |
Harald Welte | 156c5e6 | 2009-07-05 14:02:46 +0200 | [diff] [blame] | 438 | if (!rc && !gsms->receiver) |
| 439 | rc = GSM411_RP_CAUSE_MO_NUM_UNASSIGNED; |
| 440 | |
Harald Welte | 59b0468 | 2009-06-10 05:40:52 +0800 | [diff] [blame] | 441 | out: |
Harald Welte | 68b7df2 | 2009-08-08 16:03:15 +0200 | [diff] [blame] | 442 | sms_free(gsms); |
Harald Welte | 59b0468 | 2009-06-10 05:40:52 +0800 | [diff] [blame] | 443 | |
| 444 | return rc; |
| 445 | } |
| 446 | |
Harald Welte | b78996d | 2009-07-27 20:11:35 +0200 | [diff] [blame] | 447 | static int gsm411_send_rp_ack(struct gsm_trans *trans, u_int8_t msg_ref) |
Harald Welte | 59b0468 | 2009-06-10 05:40:52 +0800 | [diff] [blame] | 448 | { |
| 449 | struct msgb *msg = gsm411_msgb_alloc(); |
Harald Welte | 59b0468 | 2009-06-10 05:40:52 +0800 | [diff] [blame] | 450 | |
Harald Welte | 59b0468 | 2009-06-10 05:40:52 +0800 | [diff] [blame] | 451 | DEBUGP(DSMS, "TX: SMS RP ACK\n"); |
| 452 | |
Harald Welte | b78996d | 2009-07-27 20:11:35 +0200 | [diff] [blame] | 453 | return gsm411_rp_sendmsg(msg, trans, GSM411_MT_RP_ACK_MT, msg_ref); |
Harald Welte | 59b0468 | 2009-06-10 05:40:52 +0800 | [diff] [blame] | 454 | } |
| 455 | |
Harald Welte | b78996d | 2009-07-27 20:11:35 +0200 | [diff] [blame] | 456 | static int gsm411_send_rp_error(struct gsm_trans *trans, |
| 457 | u_int8_t msg_ref, u_int8_t cause) |
Harald Welte | 59b0468 | 2009-06-10 05:40:52 +0800 | [diff] [blame] | 458 | { |
| 459 | struct msgb *msg = gsm411_msgb_alloc(); |
Harald Welte | 59b0468 | 2009-06-10 05:40:52 +0800 | [diff] [blame] | 460 | |
Harald Welte | 59b0468 | 2009-06-10 05:40:52 +0800 | [diff] [blame] | 461 | msgb_tv_put(msg, 1, cause); |
| 462 | |
| 463 | DEBUGP(DSMS, "TX: SMS RP ERROR (cause %02d)\n", cause); |
| 464 | |
Harald Welte | b78996d | 2009-07-27 20:11:35 +0200 | [diff] [blame] | 465 | return gsm411_rp_sendmsg(msg, trans, GSM411_MT_RP_ERROR_MT, msg_ref); |
Harald Welte | 59b0468 | 2009-06-10 05:40:52 +0800 | [diff] [blame] | 466 | } |
| 467 | |
| 468 | /* Receive a 04.11 TPDU inside RP-DATA / user data */ |
Harald Welte | b78996d | 2009-07-27 20:11:35 +0200 | [diff] [blame] | 469 | static int gsm411_rx_rp_ud(struct msgb *msg, struct gsm_trans *trans, |
| 470 | struct gsm411_rp_hdr *rph, |
Harald Welte | 59b0468 | 2009-06-10 05:40:52 +0800 | [diff] [blame] | 471 | u_int8_t src_len, u_int8_t *src, |
| 472 | u_int8_t dst_len, u_int8_t *dst, |
| 473 | u_int8_t tpdu_len, u_int8_t *tpdu) |
| 474 | { |
Harald Welte | 59b0468 | 2009-06-10 05:40:52 +0800 | [diff] [blame] | 475 | int rc = 0; |
| 476 | |
| 477 | if (src_len && src) |
| 478 | DEBUGP(DSMS, "RP-DATA (MO) with SRC ?!?\n"); |
| 479 | |
| 480 | if (!dst_len || !dst || !tpdu_len || !tpdu) { |
| 481 | DEBUGP(DSMS, "RP-DATA (MO) without DST or TPDU ?!?\n"); |
Harald Welte | b78996d | 2009-07-27 20:11:35 +0200 | [diff] [blame] | 482 | gsm411_send_rp_error(trans, rph->msg_ref, |
Harald Welte | 156c5e6 | 2009-07-05 14:02:46 +0200 | [diff] [blame] | 483 | GSM411_RP_CAUSE_INV_MAND_INF); |
Harald Welte | 59b0468 | 2009-06-10 05:40:52 +0800 | [diff] [blame] | 484 | return -EIO; |
| 485 | } |
| 486 | msg->smsh = tpdu; |
| 487 | |
| 488 | DEBUGP(DSMS, "DST(%u,%s)\n", dst_len, hexdump(dst, dst_len)); |
Harald Welte | 59b0468 | 2009-06-10 05:40:52 +0800 | [diff] [blame] | 489 | |
| 490 | rc = gsm340_rx_tpdu(msg); |
| 491 | if (rc == 0) |
Harald Welte | b78996d | 2009-07-27 20:11:35 +0200 | [diff] [blame] | 492 | return gsm411_send_rp_ack(trans, rph->msg_ref); |
Harald Welte | 59b0468 | 2009-06-10 05:40:52 +0800 | [diff] [blame] | 493 | else if (rc > 0) |
Harald Welte | b78996d | 2009-07-27 20:11:35 +0200 | [diff] [blame] | 494 | return gsm411_send_rp_error(trans, rph->msg_ref, rc); |
Harald Welte | 59b0468 | 2009-06-10 05:40:52 +0800 | [diff] [blame] | 495 | else |
| 496 | return rc; |
| 497 | } |
| 498 | |
| 499 | /* Receive a 04.11 RP-DATA message in accordance with Section 7.3.1.2 */ |
Harald Welte | b78996d | 2009-07-27 20:11:35 +0200 | [diff] [blame] | 500 | static int gsm411_rx_rp_data(struct msgb *msg, struct gsm_trans *trans, |
| 501 | struct gsm411_rp_hdr *rph) |
Harald Welte | 59b0468 | 2009-06-10 05:40:52 +0800 | [diff] [blame] | 502 | { |
| 503 | u_int8_t src_len, dst_len, rpud_len; |
| 504 | u_int8_t *src = NULL, *dst = NULL , *rp_ud = NULL; |
| 505 | |
| 506 | /* in the MO case, this should always be zero length */ |
| 507 | src_len = rph->data[0]; |
| 508 | if (src_len) |
| 509 | src = &rph->data[1]; |
| 510 | |
| 511 | dst_len = rph->data[1+src_len]; |
| 512 | if (dst_len) |
| 513 | dst = &rph->data[1+src_len+1]; |
| 514 | |
| 515 | rpud_len = rph->data[1+src_len+1+dst_len]; |
| 516 | if (rpud_len) |
| 517 | rp_ud = &rph->data[1+src_len+1+dst_len+1]; |
| 518 | |
Harald Welte | 156c5e6 | 2009-07-05 14:02:46 +0200 | [diff] [blame] | 519 | DEBUGP(DSMS, "RX_RP-DATA: src_len=%u, dst_len=%u ud_len=%u\n", |
| 520 | src_len, dst_len, rpud_len); |
Harald Welte | b78996d | 2009-07-27 20:11:35 +0200 | [diff] [blame] | 521 | return gsm411_rx_rp_ud(msg, trans, rph, src_len, src, dst_len, dst, |
Harald Welte | 59b0468 | 2009-06-10 05:40:52 +0800 | [diff] [blame] | 522 | rpud_len, rp_ud); |
| 523 | } |
| 524 | |
Harald Welte | 09421d3 | 2009-08-09 14:59:02 +0200 | [diff] [blame] | 525 | /* Receive a 04.11 RP-ACK message (response to RP-DATA from us) */ |
Harald Welte | b78996d | 2009-07-27 20:11:35 +0200 | [diff] [blame] | 526 | static int gsm411_rx_rp_ack(struct msgb *msg, struct gsm_trans *trans, |
| 527 | struct gsm411_rp_hdr *rph) |
Harald Welte | 156c5e6 | 2009-07-05 14:02:46 +0200 | [diff] [blame] | 528 | { |
Harald Welte | 68b7df2 | 2009-08-08 16:03:15 +0200 | [diff] [blame] | 529 | struct gsm_sms *sms = trans->sms.sms; |
| 530 | |
Harald Welte | 156c5e6 | 2009-07-05 14:02:46 +0200 | [diff] [blame] | 531 | /* Acnkowledgement to MT RP_DATA, i.e. the MS confirms it |
| 532 | * successfully received a SMS. We can now safely mark it as |
| 533 | * transmitted */ |
| 534 | |
Harald Welte | 09421d3 | 2009-08-09 14:59:02 +0200 | [diff] [blame] | 535 | if (!trans->sms.is_mt) { |
| 536 | DEBUGP(DSMS, "RX RP-ACK on a MO transfer ?\n"); |
| 537 | return gsm411_send_rp_error(trans, rph->msg_ref, |
| 538 | GSM411_RP_CAUSE_MSG_INCOMP_STATE); |
| 539 | } |
Harald Welte | dd62b16 | 2009-07-09 23:52:59 +0200 | [diff] [blame] | 540 | |
Harald Welte | 68b7df2 | 2009-08-08 16:03:15 +0200 | [diff] [blame] | 541 | if (!sms) { |
Harald Welte | 09421d3 | 2009-08-09 14:59:02 +0200 | [diff] [blame] | 542 | DEBUGP(DSMS, "RX RP-ACK but no sms in transaction?!?\n"); |
| 543 | return gsm411_send_rp_error(trans, rph->msg_ref, |
| 544 | GSM411_RP_CAUSE_PROTOCOL_ERR); |
Harald Welte | 68b7df2 | 2009-08-08 16:03:15 +0200 | [diff] [blame] | 545 | } |
| 546 | |
| 547 | /* mark this SMS as sent in database */ |
| 548 | db_sms_mark_sent(sms); |
| 549 | |
| 550 | dispatch_signal(SS_SMS, S_SMS_DELIVERED, sms); |
| 551 | |
| 552 | sms_free(sms); |
| 553 | trans->sms.sms = NULL; |
| 554 | |
Harald Welte | fc01b24 | 2009-08-09 19:07:41 +0200 | [diff] [blame] | 555 | /* do not free the transaction here, this is done by sending CP-ACK */ |
| 556 | |
| 557 | /* check for more messages for this subscriber */ |
| 558 | sms = db_sms_get_unsent_for_subscr(msg->lchan->subscr); |
| 559 | if (sms) |
| 560 | gsm411_send_sms_lchan(msg->lchan, sms); |
| 561 | else |
| 562 | rsl_release_request(msg->lchan, UM_SAPI_SMS); |
Harald Welte | 68b7df2 | 2009-08-08 16:03:15 +0200 | [diff] [blame] | 563 | |
| 564 | return 0; |
Harald Welte | 156c5e6 | 2009-07-05 14:02:46 +0200 | [diff] [blame] | 565 | } |
| 566 | |
Harald Welte | b78996d | 2009-07-27 20:11:35 +0200 | [diff] [blame] | 567 | static int gsm411_rx_rp_error(struct msgb *msg, struct gsm_trans *trans, |
| 568 | struct gsm411_rp_hdr *rph) |
Harald Welte | 156c5e6 | 2009-07-05 14:02:46 +0200 | [diff] [blame] | 569 | { |
Harald Welte | 68b7df2 | 2009-08-08 16:03:15 +0200 | [diff] [blame] | 570 | struct gsm_sms *sms = trans->sms.sms; |
Harald Welte | b78996d | 2009-07-27 20:11:35 +0200 | [diff] [blame] | 571 | u_int8_t cause_len = rph->data[0]; |
| 572 | u_int8_t cause = rph->data[1]; |
| 573 | |
Harald Welte | 156c5e6 | 2009-07-05 14:02:46 +0200 | [diff] [blame] | 574 | /* Error in response to MT RP_DATA, i.e. the MS did not |
| 575 | * successfully receive the SMS. We need to investigate |
| 576 | * the cause and take action depending on it */ |
| 577 | |
Harald Welte | b78996d | 2009-07-27 20:11:35 +0200 | [diff] [blame] | 578 | DEBUGP(DSMS, "RX SMS RP-ERROR Cause=0x%02x\n", cause); |
| 579 | |
Harald Welte | 09421d3 | 2009-08-09 14:59:02 +0200 | [diff] [blame] | 580 | if (!trans->sms.is_mt) { |
| 581 | DEBUGP(DSMS, "RX RP-ERR on a MO transfer ?\n"); |
| 582 | return gsm411_send_rp_error(trans, rph->msg_ref, |
| 583 | GSM411_RP_CAUSE_MSG_INCOMP_STATE); |
| 584 | } |
Harald Welte | dd62b16 | 2009-07-09 23:52:59 +0200 | [diff] [blame] | 585 | |
Harald Welte | 68b7df2 | 2009-08-08 16:03:15 +0200 | [diff] [blame] | 586 | if (!sms) { |
Harald Welte | 09421d3 | 2009-08-09 14:59:02 +0200 | [diff] [blame] | 587 | DEBUGP(DSMS, "RX RP-ERR, but no sms in transaction?!?\n"); |
| 588 | return gsm411_send_rp_error(trans, rph->msg_ref, |
| 589 | GSM411_RP_CAUSE_PROTOCOL_ERR); |
| 590 | } |
| 591 | |
| 592 | if (cause == GSM411_RP_CAUSE_MT_MEM_EXCEEDED) { |
| 593 | /* MS has not enough memory to store the message. We need |
| 594 | * to store this in our database and wati for a SMMA message */ |
| 595 | /* FIXME */ |
| 596 | dispatch_signal(SS_SMS, S_SMS_MEM_EXCEEDED, trans->subscr); |
Harald Welte | 68b7df2 | 2009-08-08 16:03:15 +0200 | [diff] [blame] | 597 | } |
| 598 | |
| 599 | sms_free(sms); |
| 600 | trans->sms.sms = NULL; |
| 601 | |
Harald Welte | 09421d3 | 2009-08-09 14:59:02 +0200 | [diff] [blame] | 602 | trans_free(trans); |
Harald Welte | 68b7df2 | 2009-08-08 16:03:15 +0200 | [diff] [blame] | 603 | |
Harald Welte | b78996d | 2009-07-27 20:11:35 +0200 | [diff] [blame] | 604 | return 0; |
Harald Welte | 156c5e6 | 2009-07-05 14:02:46 +0200 | [diff] [blame] | 605 | } |
| 606 | |
Harald Welte | b78996d | 2009-07-27 20:11:35 +0200 | [diff] [blame] | 607 | static int gsm411_rx_rp_smma(struct msgb *msg, struct gsm_trans *trans, |
| 608 | struct gsm411_rp_hdr *rph) |
Harald Welte | 156c5e6 | 2009-07-05 14:02:46 +0200 | [diff] [blame] | 609 | { |
Harald Welte | fc01b24 | 2009-08-09 19:07:41 +0200 | [diff] [blame] | 610 | struct gsm_sms *sms; |
Harald Welte | b78996d | 2009-07-27 20:11:35 +0200 | [diff] [blame] | 611 | int rc; |
| 612 | |
Harald Welte | fc01b24 | 2009-08-09 19:07:41 +0200 | [diff] [blame] | 613 | rc = gsm411_send_rp_ack(trans, rph->msg_ref); |
| 614 | trans->sms.rp_state = GSM411_RPS_IDLE; |
| 615 | |
Harald Welte | 156c5e6 | 2009-07-05 14:02:46 +0200 | [diff] [blame] | 616 | /* MS tells us that it has memory for more SMS, we need |
| 617 | * to check if we have any pending messages for it and then |
| 618 | * transfer those */ |
Harald Welte | 68b7df2 | 2009-08-08 16:03:15 +0200 | [diff] [blame] | 619 | dispatch_signal(SS_SMS, S_SMS_SMMA, trans->subscr); |
Harald Welte | b78996d | 2009-07-27 20:11:35 +0200 | [diff] [blame] | 620 | |
Harald Welte | fc01b24 | 2009-08-09 19:07:41 +0200 | [diff] [blame] | 621 | /* check for more messages for this subscriber */ |
| 622 | sms = db_sms_get_unsent_for_subscr(msg->lchan->subscr); |
| 623 | if (sms) |
| 624 | gsm411_send_sms_lchan(msg->lchan, sms); |
| 625 | else |
| 626 | rsl_release_request(msg->lchan, UM_SAPI_SMS); |
Harald Welte | b78996d | 2009-07-27 20:11:35 +0200 | [diff] [blame] | 627 | |
| 628 | return rc; |
Harald Welte | 156c5e6 | 2009-07-05 14:02:46 +0200 | [diff] [blame] | 629 | } |
| 630 | |
Harald Welte | b78996d | 2009-07-27 20:11:35 +0200 | [diff] [blame] | 631 | static int gsm411_rx_cp_data(struct msgb *msg, struct gsm48_hdr *gh, |
| 632 | struct gsm_trans *trans) |
Harald Welte | 59b0468 | 2009-06-10 05:40:52 +0800 | [diff] [blame] | 633 | { |
| 634 | struct gsm411_rp_hdr *rp_data = (struct gsm411_rp_hdr*)&gh->data; |
| 635 | u_int8_t msg_type = rp_data->msg_type & 0x07; |
| 636 | int rc = 0; |
| 637 | |
| 638 | switch (msg_type) { |
| 639 | case GSM411_MT_RP_DATA_MO: |
Harald Welte | b78996d | 2009-07-27 20:11:35 +0200 | [diff] [blame] | 640 | DEBUGP(DSMS, "RX SMS RP-DATA (MO)\n"); |
| 641 | /* start TR2N and enter 'wait to send RP-ACK state' */ |
| 642 | trans->sms.rp_state = GSM411_RPS_WAIT_TO_TX_RP_ACK; |
| 643 | rc = gsm411_rx_rp_data(msg, trans, rp_data); |
Harald Welte | 59b0468 | 2009-06-10 05:40:52 +0800 | [diff] [blame] | 644 | break; |
| 645 | case GSM411_MT_RP_ACK_MO: |
Harald Welte | b78996d | 2009-07-27 20:11:35 +0200 | [diff] [blame] | 646 | DEBUGP(DSMS,"RX SMS RP-ACK (MO)\n"); |
| 647 | rc = gsm411_rx_rp_ack(msg, trans, rp_data); |
Harald Welte | 156c5e6 | 2009-07-05 14:02:46 +0200 | [diff] [blame] | 648 | break; |
Harald Welte | 59b0468 | 2009-06-10 05:40:52 +0800 | [diff] [blame] | 649 | case GSM411_MT_RP_SMMA_MO: |
Harald Welte | b78996d | 2009-07-27 20:11:35 +0200 | [diff] [blame] | 650 | DEBUGP(DSMS, "RX SMS RP-SMMA\n"); |
| 651 | /* start TR2N and enter 'wait to send RP-ACK state' */ |
| 652 | trans->sms.rp_state = GSM411_RPS_WAIT_TO_TX_RP_ACK; |
| 653 | rc = gsm411_rx_rp_smma(msg, trans, rp_data); |
| 654 | break; |
| 655 | case GSM411_MT_RP_ERROR_MO: |
| 656 | rc = gsm411_rx_rp_error(msg, trans, rp_data); |
Harald Welte | 59b0468 | 2009-06-10 05:40:52 +0800 | [diff] [blame] | 657 | break; |
| 658 | default: |
| 659 | DEBUGP(DSMS, "Invalid RP type 0x%02x\n", msg_type); |
Harald Welte | b78996d | 2009-07-27 20:11:35 +0200 | [diff] [blame] | 660 | rc = gsm411_send_rp_error(trans, rp_data->msg_ref, |
| 661 | GSM411_RP_CAUSE_MSGTYPE_NOTEXIST); |
Harald Welte | 59b0468 | 2009-06-10 05:40:52 +0800 | [diff] [blame] | 662 | break; |
| 663 | } |
| 664 | |
| 665 | return rc; |
| 666 | } |
| 667 | |
Harald Welte | b78996d | 2009-07-27 20:11:35 +0200 | [diff] [blame] | 668 | /* send CP-ACK to given transaction */ |
| 669 | static int gsm411_tx_cp_ack(struct gsm_trans *trans) |
| 670 | { |
| 671 | struct msgb *msg = gsm411_msgb_alloc(); |
Harald Welte | fc01b24 | 2009-08-09 19:07:41 +0200 | [diff] [blame] | 672 | int rc; |
Harald Welte | b78996d | 2009-07-27 20:11:35 +0200 | [diff] [blame] | 673 | |
Harald Welte | fc01b24 | 2009-08-09 19:07:41 +0200 | [diff] [blame] | 674 | rc = gsm411_cp_sendmsg(msg, trans, GSM411_MT_CP_ACK); |
| 675 | |
| 676 | if (trans->sms.is_mt) { |
| 677 | /* If this is a MT SMS DELIVER, we can clear transaction here */ |
| 678 | trans->sms.cp_state = GSM411_CPS_IDLE; |
| 679 | trans_free(trans); |
| 680 | } |
Holger Hans Peter Freyther | 182c745 | 2009-08-10 07:59:27 +0200 | [diff] [blame] | 681 | |
| 682 | return rc; |
Harald Welte | b78996d | 2009-07-27 20:11:35 +0200 | [diff] [blame] | 683 | } |
| 684 | |
| 685 | static int gsm411_tx_cp_error(struct gsm_trans *trans, u_int8_t cause) |
| 686 | { |
| 687 | struct msgb *msg = gsm411_msgb_alloc(); |
| 688 | u_int8_t *causep; |
| 689 | |
Harald Welte | 68b7df2 | 2009-08-08 16:03:15 +0200 | [diff] [blame] | 690 | causep = msgb_put(msg, 1); |
Harald Welte | b78996d | 2009-07-27 20:11:35 +0200 | [diff] [blame] | 691 | *causep = cause; |
| 692 | |
| 693 | return gsm411_cp_sendmsg(msg, trans, GSM411_MT_CP_ERROR); |
| 694 | } |
| 695 | |
| 696 | /* Entry point for incoming GSM48_PDISC_SMS from abis_rsl.c */ |
Harald Welte | 59b0468 | 2009-06-10 05:40:52 +0800 | [diff] [blame] | 697 | int gsm0411_rcv_sms(struct msgb *msg) |
| 698 | { |
| 699 | struct gsm48_hdr *gh = msgb_l3(msg); |
| 700 | u_int8_t msg_type = gh->msg_type; |
Harald Welte | b78996d | 2009-07-27 20:11:35 +0200 | [diff] [blame] | 701 | u_int8_t transaction_id = ((gh->proto_discr >> 4) ^ 0x8); /* flip */ |
| 702 | struct gsm_lchan *lchan = msg->lchan; |
| 703 | struct gsm_trans *trans; |
Harald Welte | 59b0468 | 2009-06-10 05:40:52 +0800 | [diff] [blame] | 704 | int rc = 0; |
| 705 | |
Harald Welte | b78996d | 2009-07-27 20:11:35 +0200 | [diff] [blame] | 706 | if (!lchan->subscr) |
| 707 | return -EIO; |
| 708 | /* FIXME: send some error message */ |
| 709 | |
| 710 | trans = trans_find_by_id(lchan->subscr, GSM48_PDISC_SMS, |
| 711 | transaction_id); |
| 712 | if (!trans) { |
| 713 | DEBUGP(DSMS, "Unknown transaction ID %x, " |
| 714 | "creating new trans\n", transaction_id); |
| 715 | trans = trans_alloc(lchan->subscr, GSM48_PDISC_SMS, |
| 716 | transaction_id, new_callref++); |
| 717 | if (!trans) { |
| 718 | DEBUGP(DSMS, "No memory for trans\n"); |
| 719 | /* FIXME: send some error message */ |
| 720 | return -ENOMEM; |
| 721 | } |
| 722 | trans->sms.cp_state = GSM411_CPS_IDLE; |
| 723 | trans->sms.rp_state = GSM411_RPS_IDLE; |
| 724 | trans->sms.is_mt = 0; |
| 725 | |
| 726 | trans->lchan = lchan; |
| 727 | use_lchan(lchan); |
| 728 | } |
| 729 | |
Harald Welte | 59b0468 | 2009-06-10 05:40:52 +0800 | [diff] [blame] | 730 | switch(msg_type) { |
| 731 | case GSM411_MT_CP_DATA: |
Harald Welte | b78996d | 2009-07-27 20:11:35 +0200 | [diff] [blame] | 732 | DEBUGP(DSMS, "RX SMS CP-DATA\n"); |
Harald Welte | 09421d3 | 2009-08-09 14:59:02 +0200 | [diff] [blame] | 733 | /* 5.2.3.1.3: MO state exists when SMC has received |
| 734 | * CP-DATA, including sending of the assoc. CP-ACK */ |
| 735 | /* 5.2.3.2.4: MT state exists when SMC has received |
| 736 | * CP-DATA, including sending of the assoc. CP-ACK */ |
| 737 | trans->sms.cp_state = GSM411_CPS_MM_ESTABLISHED; |
Harald Welte | b78996d | 2009-07-27 20:11:35 +0200 | [diff] [blame] | 738 | |
| 739 | rc = gsm411_rx_cp_data(msg, gh, trans); |
| 740 | /* Send CP-ACK or CP-ERORR in response */ |
| 741 | if (rc < 0) { |
| 742 | rc = gsm411_tx_cp_error(trans, GSM411_CP_CAUSE_NET_FAIL); |
| 743 | } else |
| 744 | rc = gsm411_tx_cp_ack(trans); |
Harald Welte | 59b0468 | 2009-06-10 05:40:52 +0800 | [diff] [blame] | 745 | break; |
| 746 | case GSM411_MT_CP_ACK: |
Harald Welte | b78996d | 2009-07-27 20:11:35 +0200 | [diff] [blame] | 747 | /* previous CP-DATA in this transaction was confirmed */ |
| 748 | DEBUGP(DSMS, "RX SMS CP-ACK\n"); |
Harald Welte | 09421d3 | 2009-08-09 14:59:02 +0200 | [diff] [blame] | 749 | /* 5.2.3.1.3: MO state exists when SMC has received CP-ACK */ |
| 750 | /* 5.2.3.2.4: MT state exists when SMC has received CP-ACK */ |
| 751 | trans->sms.cp_state = GSM411_CPS_MM_ESTABLISHED; |
Harald Welte | 5fa17a2 | 2009-08-10 00:24:32 +0200 | [diff] [blame] | 752 | /* Stop TC1* after CP-ACK has been received */ |
| 753 | bsc_del_timer(&trans->sms.cp_timer); |
Harald Welte | 09421d3 | 2009-08-09 14:59:02 +0200 | [diff] [blame] | 754 | |
Harald Welte | b78996d | 2009-07-27 20:11:35 +0200 | [diff] [blame] | 755 | if (!trans->sms.is_mt) { |
Harald Welte | b78996d | 2009-07-27 20:11:35 +0200 | [diff] [blame] | 756 | /* FIXME: we have sont one CP-DATA, which was now |
| 757 | * acknowledged. Check if we want to transfer more, |
| 758 | * i.e. multi-part message */ |
| 759 | trans->sms.cp_state = GSM411_CPS_IDLE; |
| 760 | trans_free(trans); |
| 761 | } |
Harald Welte | 59b0468 | 2009-06-10 05:40:52 +0800 | [diff] [blame] | 762 | break; |
| 763 | case GSM411_MT_CP_ERROR: |
Harald Welte | b78996d | 2009-07-27 20:11:35 +0200 | [diff] [blame] | 764 | DEBUGP(DSMS, "RX SMS CP-ERROR, cause 0x%02x\n", gh->data[0]); |
Harald Welte | 5fa17a2 | 2009-08-10 00:24:32 +0200 | [diff] [blame] | 765 | bsc_del_timer(&trans->sms.cp_timer); |
Harald Welte | b78996d | 2009-07-27 20:11:35 +0200 | [diff] [blame] | 766 | trans->sms.cp_state = GSM411_CPS_IDLE; |
| 767 | trans_free(trans); |
Harald Welte | 59b0468 | 2009-06-10 05:40:52 +0800 | [diff] [blame] | 768 | break; |
| 769 | default: |
Harald Welte | b78996d | 2009-07-27 20:11:35 +0200 | [diff] [blame] | 770 | DEBUGP(DSMS, "RX Unimplemented CP msg_type: 0x%02x\n", msg_type); |
| 771 | rc = gsm411_tx_cp_error(trans, GSM411_CP_CAUSE_MSGTYPE_NOTEXIST); |
Harald Welte | 09421d3 | 2009-08-09 14:59:02 +0200 | [diff] [blame] | 772 | trans->sms.cp_state = GSM411_CPS_IDLE; |
Harald Welte | b78996d | 2009-07-27 20:11:35 +0200 | [diff] [blame] | 773 | trans_free(trans); |
Harald Welte | 59b0468 | 2009-06-10 05:40:52 +0800 | [diff] [blame] | 774 | break; |
| 775 | } |
| 776 | |
Harald Welte | 59b0468 | 2009-06-10 05:40:52 +0800 | [diff] [blame] | 777 | return rc; |
| 778 | } |
| 779 | |
Harald Welte | 59b0468 | 2009-06-10 05:40:52 +0800 | [diff] [blame] | 780 | #if 0 |
Harald Welte | 59b0468 | 2009-06-10 05:40:52 +0800 | [diff] [blame] | 781 | /* Test TPDU - ALL YOUR */ |
| 782 | static u_int8_t tpdu_test[] = { |
| 783 | 0x04, 0x04, 0x81, 0x32, 0x24, 0x00, 0x00, 0x80, 0x21, 0x03, 0x41, 0x24, |
| 784 | 0x32, 0x40, 0x1F, 0x41, 0x26, 0x13, 0x94, 0x7D, 0x56, 0xA5, 0x20, 0x28, |
| 785 | 0xF2, 0xE9, 0x2C, 0x82, 0x82, 0xD2, 0x22, 0x48, 0x58, 0x64, 0x3E, 0x9D, |
| 786 | 0x47, 0x10, 0xF5, 0x09, 0xAA, 0x4E, 0x01 |
| 787 | }; |
| 788 | #endif |
| 789 | |
Harald Welte | fc01b24 | 2009-08-09 19:07:41 +0200 | [diff] [blame] | 790 | /* Take a SMS in gsm_sms structure and send it through an already |
| 791 | * existing lchan. We also assume that the caller ensured this lchan already |
| 792 | * has a SAPI3 RLL connection! */ |
Harald Welte | 68b7df2 | 2009-08-08 16:03:15 +0200 | [diff] [blame] | 793 | int gsm411_send_sms_lchan(struct gsm_lchan *lchan, struct gsm_sms *sms) |
Harald Welte | 59b0468 | 2009-06-10 05:40:52 +0800 | [diff] [blame] | 794 | { |
| 795 | struct msgb *msg = gsm411_msgb_alloc(); |
Harald Welte | b78996d | 2009-07-27 20:11:35 +0200 | [diff] [blame] | 796 | struct gsm_trans *trans; |
Harald Welte | 68b7df2 | 2009-08-08 16:03:15 +0200 | [diff] [blame] | 797 | u_int8_t *data, *rp_ud_len; |
Harald Welte | 7e2f57d | 2009-07-04 17:39:00 +0200 | [diff] [blame] | 798 | u_int8_t msg_ref = 42; |
Harald Welte | fc01b24 | 2009-08-09 19:07:41 +0200 | [diff] [blame] | 799 | u_int8_t transaction_id; |
Harald Welte | 68b7df2 | 2009-08-08 16:03:15 +0200 | [diff] [blame] | 800 | int rc; |
Harald Welte | 59b0468 | 2009-06-10 05:40:52 +0800 | [diff] [blame] | 801 | |
Harald Welte | fc01b24 | 2009-08-09 19:07:41 +0200 | [diff] [blame] | 802 | transaction_id = 4; /* FIXME: we always use 4 for now */ |
| 803 | |
Harald Welte | 59b0468 | 2009-06-10 05:40:52 +0800 | [diff] [blame] | 804 | msg->lchan = lchan; |
| 805 | |
Harald Welte | 68b7df2 | 2009-08-08 16:03:15 +0200 | [diff] [blame] | 806 | DEBUGP(DSMS, "send_sms_lchan()\n"); |
Harald Welte | b78996d | 2009-07-27 20:11:35 +0200 | [diff] [blame] | 807 | |
Harald Welte | 68b7df2 | 2009-08-08 16:03:15 +0200 | [diff] [blame] | 808 | /* FIXME: allocate transaction with message reference */ |
| 809 | trans = trans_alloc(lchan->subscr, GSM48_PDISC_SMS, |
| 810 | transaction_id, new_callref++); |
| 811 | if (!trans) { |
| 812 | DEBUGP(DSMS, "No memory for trans\n"); |
| 813 | /* FIXME: send some error message */ |
| 814 | return -ENOMEM; |
| 815 | } |
| 816 | trans->sms.cp_state = GSM411_CPS_IDLE; |
| 817 | trans->sms.rp_state = GSM411_RPS_IDLE; |
| 818 | trans->sms.is_mt = 1; |
| 819 | trans->sms.sms = sms; |
| 820 | |
| 821 | trans->lchan = lchan; |
| 822 | use_lchan(lchan); |
| 823 | |
| 824 | /* Hardcode SMSC Originating Address for now */ |
Harald Welte | 59b0468 | 2009-06-10 05:40:52 +0800 | [diff] [blame] | 825 | data = (u_int8_t *)msgb_put(msg, 8); |
Harald Welte | 7e2f57d | 2009-07-04 17:39:00 +0200 | [diff] [blame] | 826 | data[0] = 0x07; /* originator length == 7 */ |
Harald Welte | 156c5e6 | 2009-07-05 14:02:46 +0200 | [diff] [blame] | 827 | data[1] = 0x91; /* type of number: international, ISDN */ |
| 828 | data[2] = 0x44; /* 447785016005 */ |
Harald Welte | 59b0468 | 2009-06-10 05:40:52 +0800 | [diff] [blame] | 829 | data[3] = 0x77; |
| 830 | data[4] = 0x58; |
| 831 | data[5] = 0x10; |
| 832 | data[6] = 0x06; |
| 833 | data[7] = 0x50; |
Harald Welte | 7e2f57d | 2009-07-04 17:39:00 +0200 | [diff] [blame] | 834 | |
| 835 | /* Hardcoded Destination Address */ |
Harald Welte | 59b0468 | 2009-06-10 05:40:52 +0800 | [diff] [blame] | 836 | data = (u_int8_t *)msgb_put(msg, 1); |
Harald Welte | 7e2f57d | 2009-07-04 17:39:00 +0200 | [diff] [blame] | 837 | data[0] = 0; /* destination length == 0 */ |
Harald Welte | 59b0468 | 2009-06-10 05:40:52 +0800 | [diff] [blame] | 838 | |
Harald Welte | 68b7df2 | 2009-08-08 16:03:15 +0200 | [diff] [blame] | 839 | /* obtain a pointer for the rp_ud_len, so we can fill it later */ |
| 840 | rp_ud_len = (u_int8_t *)msgb_put(msg, 1); |
Harald Welte | 59b0468 | 2009-06-10 05:40:52 +0800 | [diff] [blame] | 841 | |
Harald Welte | 68b7df2 | 2009-08-08 16:03:15 +0200 | [diff] [blame] | 842 | #if 1 |
| 843 | /* generate the 03.40 TPDU */ |
| 844 | rc = gsm340_gen_tpdu(msg, sms); |
| 845 | if (rc < 0) { |
| 846 | msgb_free(msg); |
| 847 | return rc; |
| 848 | } |
Harald Welte | 59b0468 | 2009-06-10 05:40:52 +0800 | [diff] [blame] | 849 | |
Harald Welte | 68b7df2 | 2009-08-08 16:03:15 +0200 | [diff] [blame] | 850 | *rp_ud_len = rc; |
| 851 | #else |
| 852 | data = msgb_put(msg, sizeof(tpdu_test)); |
Harald Welte | 59b0468 | 2009-06-10 05:40:52 +0800 | [diff] [blame] | 853 | memcpy(data, tpdu_test, sizeof(tpdu_test)); |
Harald Welte | 68b7df2 | 2009-08-08 16:03:15 +0200 | [diff] [blame] | 854 | *rp_ud_len = sizeof(tpdu_test); |
| 855 | #endif |
Harald Welte | 59b0468 | 2009-06-10 05:40:52 +0800 | [diff] [blame] | 856 | |
Harald Welte | 68b7df2 | 2009-08-08 16:03:15 +0200 | [diff] [blame] | 857 | DEBUGP(DSMS, "TX: SMS DELIVER\n"); |
Harald Welte | 59b0468 | 2009-06-10 05:40:52 +0800 | [diff] [blame] | 858 | |
Harald Welte | b78996d | 2009-07-27 20:11:35 +0200 | [diff] [blame] | 859 | return gsm411_rp_sendmsg(msg, trans, GSM411_MT_RP_DATA_MT, msg_ref); |
| 860 | /* FIXME: enter 'wait for RP-ACK' state, start TR1N */ |
Harald Welte | 59b0468 | 2009-06-10 05:40:52 +0800 | [diff] [blame] | 861 | } |
Harald Welte | b78996d | 2009-07-27 20:11:35 +0200 | [diff] [blame] | 862 | |
Harald Welte | fc01b24 | 2009-08-09 19:07:41 +0200 | [diff] [blame] | 863 | /* RLL SAPI3 establish callback. Now we have a RLL connection and |
| 864 | * can deliver the actual message */ |
Harald Welte | 09421d3 | 2009-08-09 14:59:02 +0200 | [diff] [blame] | 865 | static void rll_ind_cb(struct gsm_lchan *lchan, u_int8_t link_id, |
| 866 | void *_sms, enum bsc_rllr_ind type) |
| 867 | { |
| 868 | struct gsm_sms *sms = _sms; |
| 869 | |
| 870 | DEBUGP(DSMS, "rll_ind_cb(lchan=%p, link_id=%u, sms=%p, type=%u\n", |
| 871 | lchan, link_id, sms, type); |
| 872 | |
| 873 | switch (type) { |
| 874 | case BSC_RLLR_IND_EST_CONF: |
| 875 | gsm411_send_sms_lchan(lchan, sms); |
| 876 | break; |
| 877 | case BSC_RLLR_IND_REL_IND: |
| 878 | case BSC_RLLR_IND_ERR_IND: |
| 879 | case BSC_RLLR_IND_TIMEOUT: |
| 880 | sms_free(sms); |
| 881 | break; |
| 882 | } |
| 883 | } |
| 884 | |
Harald Welte | fc01b24 | 2009-08-09 19:07:41 +0200 | [diff] [blame] | 885 | /* paging callback. Here we get called if paging a subscriber has |
| 886 | * succeeded or failed. */ |
Harald Welte | 68b7df2 | 2009-08-08 16:03:15 +0200 | [diff] [blame] | 887 | static int paging_cb_send_sms(unsigned int hooknum, unsigned int event, |
| 888 | struct msgb *msg, void *_lchan, void *_sms) |
Harald Welte | b78996d | 2009-07-27 20:11:35 +0200 | [diff] [blame] | 889 | { |
Harald Welte | 68b7df2 | 2009-08-08 16:03:15 +0200 | [diff] [blame] | 890 | struct gsm_lchan *lchan = _lchan; |
| 891 | struct gsm_sms *sms = _sms; |
| 892 | int rc; |
Harald Welte | b78996d | 2009-07-27 20:11:35 +0200 | [diff] [blame] | 893 | |
Harald Welte | 68b7df2 | 2009-08-08 16:03:15 +0200 | [diff] [blame] | 894 | DEBUGP(DSMS, "paging_cb_send_sms(hooknum=%u, event=%u, msg=%p," |
| 895 | "lchan=%p, sms=%p)\n", hooknum, event, msg, lchan, sms); |
| 896 | |
| 897 | if (hooknum != GSM_HOOK_RR_PAGING) |
| 898 | return -EINVAL; |
| 899 | |
| 900 | switch (event) { |
| 901 | case GSM_PAGING_SUCCEEDED: |
| 902 | /* Paging aborted without lchan ?!? */ |
| 903 | if (!lchan) { |
| 904 | sms_free(sms); |
| 905 | rc = -EIO; |
| 906 | break; |
| 907 | } |
Harald Welte | fc01b24 | 2009-08-09 19:07:41 +0200 | [diff] [blame] | 908 | /* Establish a SAPI3 RLL connection for SMS */ |
Harald Welte | 09421d3 | 2009-08-09 14:59:02 +0200 | [diff] [blame] | 909 | rc = rll_establish(lchan, UM_SAPI_SMS, rll_ind_cb, sms); |
Harald Welte | 68b7df2 | 2009-08-08 16:03:15 +0200 | [diff] [blame] | 910 | break; |
| 911 | case GSM_PAGING_EXPIRED: |
| 912 | sms_free(sms); |
| 913 | rc = -ETIMEDOUT; |
| 914 | break; |
| 915 | default: |
| 916 | rc = -EINVAL; |
| 917 | break; |
| 918 | } |
| 919 | |
| 920 | return rc; |
| 921 | } |
| 922 | |
Harald Welte | fc01b24 | 2009-08-09 19:07:41 +0200 | [diff] [blame] | 923 | /* high-level function to send a SMS to a given subscriber. The function |
| 924 | * will take care of paging the subscriber, establishing the RLL SAPI3 |
| 925 | * connection, etc. */ |
Harald Welte | 68b7df2 | 2009-08-08 16:03:15 +0200 | [diff] [blame] | 926 | int gsm411_send_sms_subscr(struct gsm_subscriber *subscr, |
| 927 | struct gsm_sms *sms) |
| 928 | { |
Harald Welte | fc01b24 | 2009-08-09 19:07:41 +0200 | [diff] [blame] | 929 | struct gsm_lchan *lchan; |
| 930 | |
Harald Welte | 68b7df2 | 2009-08-08 16:03:15 +0200 | [diff] [blame] | 931 | /* check if we already have an open lchan to the subscriber. |
| 932 | * if yes, send the SMS this way */ |
Harald Welte | fc01b24 | 2009-08-09 19:07:41 +0200 | [diff] [blame] | 933 | lchan = lchan_for_subscr(subscr); |
| 934 | if (lchan) |
| 935 | return rll_establish(lchan, UM_SAPI_SMS, |
| 936 | rll_ind_cb, sms); |
Harald Welte | 68b7df2 | 2009-08-08 16:03:15 +0200 | [diff] [blame] | 937 | |
| 938 | /* if not, we have to start paging */ |
| 939 | paging_request(subscr->net, subscr, RSL_CHANNEED_SDCCH, |
| 940 | paging_cb_send_sms, sms); |
| 941 | |
| 942 | return 0; |
| 943 | } |
Harald Welte | 5b359d8 | 2009-07-28 00:44:49 +0200 | [diff] [blame] | 944 | |
Harald Welte | fc01b24 | 2009-08-09 19:07:41 +0200 | [diff] [blame] | 945 | static int subscr_sig_cb(unsigned int subsys, unsigned int signal, |
| 946 | void *handler_data, void *signal_data) |
| 947 | { |
| 948 | struct gsm_subscriber *subscr; |
| 949 | struct gsm_lchan *lchan; |
| 950 | struct gsm_sms *sms; |
| 951 | |
| 952 | switch (signal) { |
| 953 | case S_SUBSCR_ATTACHED: |
| 954 | /* A subscriber has attached. Check if there are |
| 955 | * any pending SMS for him to be delivered */ |
| 956 | subscr = signal_data; |
| 957 | lchan = lchan_for_subscr(subscr); |
| 958 | if (!lchan) |
| 959 | break; |
| 960 | sms = db_sms_get_unsent_for_subscr(subscr); |
| 961 | if (!sms) |
| 962 | break; |
| 963 | /* Establish a SAPI3 RLL connection for SMS */ |
| 964 | rll_establish(lchan, UM_SAPI_SMS, rll_ind_cb, sms); |
| 965 | break; |
| 966 | default: |
| 967 | break; |
| 968 | } |
| 969 | return 0; |
| 970 | } |
| 971 | |
Harald Welte | 932e20d | 2009-07-28 00:41:45 +0200 | [diff] [blame] | 972 | static __attribute__((constructor)) void on_dso_load_sms(void) |
| 973 | { |
Harald Welte | 932e20d | 2009-07-28 00:41:45 +0200 | [diff] [blame] | 974 | tall_gsms_ctx = talloc_named_const(tall_bsc_ctx, 1, "sms"); |
Harald Welte | fc01b24 | 2009-08-09 19:07:41 +0200 | [diff] [blame] | 975 | |
| 976 | register_signal_handler(SS_SUBSCR, subscr_sig_cb, NULL); |
Harald Welte | 932e20d | 2009-07-28 00:41:45 +0200 | [diff] [blame] | 977 | } |