Holger Hans Peter Freyther | 575d2e1 | 2010-12-24 13:48:27 +0100 | [diff] [blame] | 1 | /* SMS queue to continously attempt to deliver SMS */ |
| 2 | /* |
| 3 | * (C) 2010 by Holger Hans Peter Freyther <zecke@selfish.org> |
| 4 | * All Rights Reserved |
| 5 | * |
| 6 | * This program is free software; you can redistribute it and/or modify |
Harald Welte | 0e3e88e | 2011-01-01 15:25:50 +0100 | [diff] [blame] | 7 | * it under the terms of the GNU Affero General Public License as published by |
| 8 | * the Free Software Foundation; either version 3 of the License, or |
Holger Hans Peter Freyther | 575d2e1 | 2010-12-24 13:48:27 +0100 | [diff] [blame] | 9 | * (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 |
Harald Welte | 0e3e88e | 2011-01-01 15:25:50 +0100 | [diff] [blame] | 14 | * GNU Affero General Public License for more details. |
Holger Hans Peter Freyther | 575d2e1 | 2010-12-24 13:48:27 +0100 | [diff] [blame] | 15 | * |
Harald Welte | 0e3e88e | 2011-01-01 15:25:50 +0100 | [diff] [blame] | 16 | * You should have received a copy of the GNU Affero General Public License |
| 17 | * along with this program. If not, see <http://www.gnu.org/licenses/>. |
Holger Hans Peter Freyther | 575d2e1 | 2010-12-24 13:48:27 +0100 | [diff] [blame] | 18 | * |
| 19 | */ |
| 20 | |
| 21 | /** |
| 22 | * The difficulty of such a queue is to send a lot of SMS without |
| 23 | * overloading the paging subsystem and the database and other users |
| 24 | * of the MSC. To make the best use we would need to know the number |
| 25 | * of pending paging requests, then throttle the number of SMS we |
| 26 | * want to send and such. |
| 27 | * We will start with a very simple SMS Queue and then try to speed |
| 28 | * things up by collecting data from other parts of the system. |
| 29 | */ |
| 30 | |
| 31 | #include <openbsc/sms_queue.h> |
| 32 | #include <openbsc/chan_alloc.h> |
| 33 | #include <openbsc/db.h> |
| 34 | #include <openbsc/debug.h> |
| 35 | #include <openbsc/gsm_data.h> |
| 36 | #include <openbsc/gsm_04_11.h> |
Holger Hans Peter Freyther | 6131833 | 2010-12-24 21:39:55 +0100 | [diff] [blame] | 37 | #include <openbsc/gsm_subscriber.h> |
Holger Hans Peter Freyther | 575d2e1 | 2010-12-24 13:48:27 +0100 | [diff] [blame] | 38 | #include <openbsc/signal.h> |
| 39 | |
Pablo Neira Ayuso | dd5fff4 | 2011-03-22 16:47:59 +0100 | [diff] [blame] | 40 | #include <osmocom/core/talloc.h> |
Holger Hans Peter Freyther | 575d2e1 | 2010-12-24 13:48:27 +0100 | [diff] [blame] | 41 | |
Holger Hans Peter Freyther | 6ec8098 | 2010-12-25 14:08:00 +0100 | [diff] [blame] | 42 | #include <osmocom/vty/vty.h> |
| 43 | |
Holger Hans Peter Freyther | 6131833 | 2010-12-24 21:39:55 +0100 | [diff] [blame] | 44 | /* |
| 45 | * One pending SMS that we wait for. |
| 46 | */ |
| 47 | struct gsm_sms_pending { |
| 48 | struct llist_head entry; |
| 49 | |
| 50 | struct gsm_subscriber *subscr; |
| 51 | unsigned long long sms_id; |
| 52 | int failed_attempts; |
| 53 | int resend; |
| 54 | }; |
Holger Hans Peter Freyther | 575d2e1 | 2010-12-24 13:48:27 +0100 | [diff] [blame] | 55 | |
| 56 | struct gsm_sms_queue { |
Pablo Neira Ayuso | 840ccf6 | 2011-05-06 12:11:06 +0200 | [diff] [blame] | 57 | struct osmo_timer_list resend_pending; |
| 58 | struct osmo_timer_list push_queue; |
Holger Hans Peter Freyther | 575d2e1 | 2010-12-24 13:48:27 +0100 | [diff] [blame] | 59 | struct gsm_network *network; |
Holger Hans Peter Freyther | 4334e4e | 2010-12-25 14:46:54 +0100 | [diff] [blame] | 60 | int max_fail; |
Holger Hans Peter Freyther | 575d2e1 | 2010-12-24 13:48:27 +0100 | [diff] [blame] | 61 | int max_pending; |
| 62 | int pending; |
Holger Hans Peter Freyther | 6131833 | 2010-12-24 21:39:55 +0100 | [diff] [blame] | 63 | |
| 64 | struct llist_head pending_sms; |
| 65 | unsigned long long last_subscr_id; |
Holger Hans Peter Freyther | 575d2e1 | 2010-12-24 13:48:27 +0100 | [diff] [blame] | 66 | }; |
| 67 | |
Holger Hans Peter Freyther | 6131833 | 2010-12-24 21:39:55 +0100 | [diff] [blame] | 68 | static int sms_subscr_cb(unsigned int, unsigned int, void *, void *); |
| 69 | static int sms_sms_cb(unsigned int, unsigned int, void *, void *); |
| 70 | |
| 71 | static struct gsm_sms_pending *sms_find_pending(struct gsm_sms_queue *smsq, |
| 72 | struct gsm_sms *sms) |
| 73 | { |
| 74 | struct gsm_sms_pending *pending; |
| 75 | |
| 76 | llist_for_each_entry(pending, &smsq->pending_sms, entry) { |
| 77 | if (pending->sms_id == sms->id) |
| 78 | return pending; |
| 79 | } |
| 80 | |
| 81 | return NULL; |
| 82 | } |
| 83 | |
| 84 | static int sms_is_in_pending(struct gsm_sms_queue *smsq, struct gsm_sms *sms) |
| 85 | { |
| 86 | return sms_find_pending(smsq, sms) != NULL; |
| 87 | } |
| 88 | |
Holger Hans Peter Freyther | 735a280 | 2011-07-25 00:13:06 +0200 | [diff] [blame] | 89 | static struct gsm_sms_pending *sms_subscriber_find_pending( |
| 90 | struct gsm_sms_queue *smsq, |
| 91 | struct gsm_subscriber *subscr) |
Holger Hans Peter Freyther | 6131833 | 2010-12-24 21:39:55 +0100 | [diff] [blame] | 92 | { |
| 93 | struct gsm_sms_pending *pending; |
| 94 | |
| 95 | llist_for_each_entry(pending, &smsq->pending_sms, entry) { |
| 96 | if (pending->subscr == subscr) |
Holger Hans Peter Freyther | 735a280 | 2011-07-25 00:13:06 +0200 | [diff] [blame] | 97 | return pending; |
Holger Hans Peter Freyther | 6131833 | 2010-12-24 21:39:55 +0100 | [diff] [blame] | 98 | } |
| 99 | |
Holger Hans Peter Freyther | 735a280 | 2011-07-25 00:13:06 +0200 | [diff] [blame] | 100 | return NULL; |
| 101 | } |
| 102 | |
| 103 | static int sms_subscriber_is_pending(struct gsm_sms_queue *smsq, |
| 104 | struct gsm_subscriber *subscr) |
| 105 | { |
| 106 | return sms_subscriber_find_pending(smsq, subscr) != NULL; |
Holger Hans Peter Freyther | 6131833 | 2010-12-24 21:39:55 +0100 | [diff] [blame] | 107 | } |
| 108 | |
| 109 | static struct gsm_sms_pending *sms_pending_from(struct gsm_sms_queue *smsq, |
| 110 | struct gsm_sms *sms) |
| 111 | { |
| 112 | struct gsm_sms_pending *pending; |
| 113 | |
| 114 | pending = talloc_zero(smsq, struct gsm_sms_pending); |
| 115 | if (!pending) |
| 116 | return NULL; |
| 117 | |
| 118 | pending->subscr = subscr_get(sms->receiver); |
| 119 | pending->sms_id = sms->id; |
| 120 | return pending; |
| 121 | } |
| 122 | |
| 123 | static void sms_pending_free(struct gsm_sms_pending *pending) |
| 124 | { |
| 125 | subscr_put(pending->subscr); |
| 126 | llist_del(&pending->entry); |
| 127 | talloc_free(pending); |
| 128 | } |
| 129 | |
| 130 | static void sms_pending_resend(struct gsm_sms_pending *pending) |
| 131 | { |
| 132 | struct gsm_sms_queue *smsq; |
Holger Hans Peter Freyther | d0a34ed | 2012-11-10 19:46:58 +0100 | [diff] [blame] | 133 | LOGP(DLSMS, LOGL_DEBUG, |
Holger Hans Peter Freyther | 6131833 | 2010-12-24 21:39:55 +0100 | [diff] [blame] | 134 | "Scheduling resend of SMS %llu.\n", pending->sms_id); |
| 135 | |
| 136 | pending->resend = 1; |
| 137 | |
| 138 | smsq = pending->subscr->net->sms_queue; |
Pablo Neira Ayuso | 840ccf6 | 2011-05-06 12:11:06 +0200 | [diff] [blame] | 139 | if (osmo_timer_pending(&smsq->resend_pending)) |
Holger Hans Peter Freyther | 6131833 | 2010-12-24 21:39:55 +0100 | [diff] [blame] | 140 | return; |
| 141 | |
Pablo Neira Ayuso | 840ccf6 | 2011-05-06 12:11:06 +0200 | [diff] [blame] | 142 | osmo_timer_schedule(&smsq->resend_pending, 1, 0); |
Holger Hans Peter Freyther | 6131833 | 2010-12-24 21:39:55 +0100 | [diff] [blame] | 143 | } |
| 144 | |
| 145 | static void sms_pending_failed(struct gsm_sms_pending *pending, int paging_error) |
| 146 | { |
| 147 | struct gsm_sms_queue *smsq; |
| 148 | |
Holger Hans Peter Freyther | d0a34ed | 2012-11-10 19:46:58 +0100 | [diff] [blame] | 149 | LOGP(DLSMS, LOGL_NOTICE, "Sending SMS %llu failed %d times.\n", |
Holger Hans Peter Freyther | 6131833 | 2010-12-24 21:39:55 +0100 | [diff] [blame] | 150 | pending->sms_id, pending->failed_attempts); |
| 151 | |
| 152 | smsq = pending->subscr->net->sms_queue; |
Holger Hans Peter Freyther | 4334e4e | 2010-12-25 14:46:54 +0100 | [diff] [blame] | 153 | if (++pending->failed_attempts < smsq->max_fail) |
Holger Hans Peter Freyther | 6131833 | 2010-12-24 21:39:55 +0100 | [diff] [blame] | 154 | return sms_pending_resend(pending); |
| 155 | |
Holger Hans Peter Freyther | 6131833 | 2010-12-24 21:39:55 +0100 | [diff] [blame] | 156 | sms_pending_free(pending); |
| 157 | smsq->pending -= 1; |
| 158 | sms_queue_trigger(smsq); |
| 159 | } |
| 160 | |
| 161 | /* |
| 162 | * Resend all SMS that are scheduled for a resend. This is done to |
| 163 | * avoid an immediate failure. |
| 164 | */ |
| 165 | static void sms_resend_pending(void *_data) |
| 166 | { |
| 167 | struct gsm_sms_pending *pending, *tmp; |
| 168 | struct gsm_sms_queue *smsq = _data; |
| 169 | |
| 170 | llist_for_each_entry_safe(pending, tmp, &smsq->pending_sms, entry) { |
| 171 | struct gsm_sms *sms; |
| 172 | if (!pending->resend) |
| 173 | continue; |
| 174 | |
| 175 | sms = db_sms_get(smsq->network, pending->sms_id); |
| 176 | |
| 177 | /* the sms is gone? Move to the next */ |
| 178 | if (!sms) { |
| 179 | sms_pending_free(pending); |
| 180 | smsq->pending -= 1; |
| 181 | sms_queue_trigger(smsq); |
| 182 | } else { |
| 183 | pending->resend = 0; |
| 184 | gsm411_send_sms_subscr(sms->receiver, sms); |
| 185 | } |
| 186 | } |
| 187 | } |
| 188 | |
Holger Hans Peter Freyther | e41db2a | 2010-12-25 17:45:23 +0100 | [diff] [blame] | 189 | static struct gsm_sms *take_next_sms(struct gsm_sms_queue *smsq) |
| 190 | { |
| 191 | struct gsm_sms *sms; |
| 192 | |
| 193 | sms = db_sms_get_unsent_by_subscr(smsq->network, smsq->last_subscr_id, 10); |
| 194 | if (sms) { |
| 195 | smsq->last_subscr_id = sms->receiver->id + 1; |
| 196 | return sms; |
| 197 | } |
| 198 | |
| 199 | /* need to wrap around */ |
| 200 | smsq->last_subscr_id = 0; |
| 201 | sms = db_sms_get_unsent_by_subscr(smsq->network, |
| 202 | smsq->last_subscr_id, 10); |
| 203 | if (sms) |
| 204 | smsq->last_subscr_id = sms->receiver->id + 1; |
| 205 | return sms; |
| 206 | } |
| 207 | |
Holger Hans Peter Freyther | 6131833 | 2010-12-24 21:39:55 +0100 | [diff] [blame] | 208 | /** |
| 209 | * I will submit up to max_pending - pending SMS to the |
| 210 | * subsystem. |
| 211 | */ |
| 212 | static void sms_submit_pending(void *_data) |
| 213 | { |
| 214 | struct gsm_sms_queue *smsq = _data; |
| 215 | int attempts = smsq->max_pending - smsq->pending; |
Holger Hans Peter Freyther | 3e479cc | 2010-12-25 19:28:44 +0100 | [diff] [blame] | 216 | int initialized = 0; |
| 217 | unsigned long long first_sub = 0; |
Holger Hans Peter Freyther | 8ba6f85 | 2010-12-25 19:32:12 +0100 | [diff] [blame] | 218 | int attempted = 0, rounds = 0; |
Holger Hans Peter Freyther | 6131833 | 2010-12-24 21:39:55 +0100 | [diff] [blame] | 219 | |
Holger Hans Peter Freyther | d0a34ed | 2012-11-10 19:46:58 +0100 | [diff] [blame] | 220 | LOGP(DLSMS, LOGL_NOTICE, "Attempting to send %d SMS\n", attempts); |
Holger Hans Peter Freyther | 6131833 | 2010-12-24 21:39:55 +0100 | [diff] [blame] | 221 | |
Holger Hans Peter Freyther | 3e479cc | 2010-12-25 19:28:44 +0100 | [diff] [blame] | 222 | do { |
Holger Hans Peter Freyther | 6131833 | 2010-12-24 21:39:55 +0100 | [diff] [blame] | 223 | struct gsm_sms_pending *pending; |
| 224 | struct gsm_sms *sms; |
| 225 | |
Holger Hans Peter Freyther | 6131833 | 2010-12-24 21:39:55 +0100 | [diff] [blame] | 226 | |
Holger Hans Peter Freyther | e41db2a | 2010-12-25 17:45:23 +0100 | [diff] [blame] | 227 | sms = take_next_sms(smsq); |
Holger Hans Peter Freyther | 6131833 | 2010-12-24 21:39:55 +0100 | [diff] [blame] | 228 | if (!sms) |
| 229 | break; |
| 230 | |
Holger Hans Peter Freyther | 8ba6f85 | 2010-12-25 19:32:12 +0100 | [diff] [blame] | 231 | rounds += 1; |
| 232 | |
Holger Hans Peter Freyther | 3e479cc | 2010-12-25 19:28:44 +0100 | [diff] [blame] | 233 | /* |
| 234 | * This code needs to detect a loop. It assumes that no SMS |
| 235 | * will vanish during the time this is executed. We will remember |
| 236 | * the id of the first GSM subscriber we see and then will |
| 237 | * compare this. The Database code should make sure that we will |
| 238 | * see all other subscribers first before seeing this one again. |
| 239 | * |
| 240 | * It is always scary to have an infinite loop like this. |
| 241 | */ |
| 242 | if (!initialized) { |
| 243 | first_sub = sms->receiver->id; |
| 244 | initialized = 1; |
| 245 | } else if (first_sub == sms->receiver->id) { |
| 246 | sms_free(sms); |
| 247 | break; |
| 248 | } |
| 249 | |
Holger Hans Peter Freyther | 6131833 | 2010-12-24 21:39:55 +0100 | [diff] [blame] | 250 | /* no need to send a pending sms */ |
| 251 | if (sms_is_in_pending(smsq, sms)) { |
Holger Hans Peter Freyther | d0a34ed | 2012-11-10 19:46:58 +0100 | [diff] [blame] | 252 | LOGP(DLSMS, LOGL_DEBUG, |
Holger Hans Peter Freyther | 487b9aa | 2010-12-27 20:12:25 +0100 | [diff] [blame] | 253 | "SMSqueue with pending sms: %llu. Skipping\n", sms->id); |
Holger Hans Peter Freyther | 6131833 | 2010-12-24 21:39:55 +0100 | [diff] [blame] | 254 | sms_free(sms); |
| 255 | continue; |
| 256 | } |
| 257 | |
| 258 | /* no need to send a SMS with the same receiver */ |
| 259 | if (sms_subscriber_is_pending(smsq, sms->receiver)) { |
Holger Hans Peter Freyther | d0a34ed | 2012-11-10 19:46:58 +0100 | [diff] [blame] | 260 | LOGP(DLSMS, LOGL_DEBUG, |
Holger Hans Peter Freyther | 6131833 | 2010-12-24 21:39:55 +0100 | [diff] [blame] | 261 | "SMSqueue with pending sub: %llu. Skipping\n", sms->receiver->id); |
| 262 | sms_free(sms); |
| 263 | continue; |
| 264 | } |
| 265 | |
| 266 | pending = sms_pending_from(smsq, sms); |
| 267 | if (!pending) { |
Holger Hans Peter Freyther | d0a34ed | 2012-11-10 19:46:58 +0100 | [diff] [blame] | 268 | LOGP(DLSMS, LOGL_ERROR, |
Holger Hans Peter Freyther | 6131833 | 2010-12-24 21:39:55 +0100 | [diff] [blame] | 269 | "Failed to create pending SMS entry.\n"); |
| 270 | sms_free(sms); |
| 271 | continue; |
| 272 | } |
| 273 | |
Holger Hans Peter Freyther | 3e479cc | 2010-12-25 19:28:44 +0100 | [diff] [blame] | 274 | attempted += 1; |
Holger Hans Peter Freyther | 6131833 | 2010-12-24 21:39:55 +0100 | [diff] [blame] | 275 | smsq->pending += 1; |
Holger Hans Peter Freyther | b01ca46 | 2010-12-28 14:25:25 +0100 | [diff] [blame] | 276 | llist_add_tail(&pending->entry, &smsq->pending_sms); |
Holger Hans Peter Freyther | 6131833 | 2010-12-24 21:39:55 +0100 | [diff] [blame] | 277 | gsm411_send_sms_subscr(sms->receiver, sms); |
Holger Hans Peter Freyther | 8ba6f85 | 2010-12-25 19:32:12 +0100 | [diff] [blame] | 278 | } while (attempted < attempts && rounds < 1000); |
Holger Hans Peter Freyther | 3e479cc | 2010-12-25 19:28:44 +0100 | [diff] [blame] | 279 | |
Holger Hans Peter Freyther | d0a34ed | 2012-11-10 19:46:58 +0100 | [diff] [blame] | 280 | LOGP(DLSMS, LOGL_DEBUG, "SMSqueue added %d messages in %d rounds\n", attempted, rounds); |
Holger Hans Peter Freyther | 6131833 | 2010-12-24 21:39:55 +0100 | [diff] [blame] | 281 | } |
| 282 | |
| 283 | /* |
| 284 | * Kick off the queue again. |
| 285 | */ |
| 286 | int sms_queue_trigger(struct gsm_sms_queue *smsq) |
| 287 | { |
Pablo Neira Ayuso | 840ccf6 | 2011-05-06 12:11:06 +0200 | [diff] [blame] | 288 | if (osmo_timer_pending(&smsq->push_queue)) |
Holger Hans Peter Freyther | 6131833 | 2010-12-24 21:39:55 +0100 | [diff] [blame] | 289 | return 0; |
| 290 | |
Pablo Neira Ayuso | 840ccf6 | 2011-05-06 12:11:06 +0200 | [diff] [blame] | 291 | osmo_timer_schedule(&smsq->push_queue, 1, 0); |
Holger Hans Peter Freyther | 6131833 | 2010-12-24 21:39:55 +0100 | [diff] [blame] | 292 | return 0; |
| 293 | } |
Holger Hans Peter Freyther | 575d2e1 | 2010-12-24 13:48:27 +0100 | [diff] [blame] | 294 | |
| 295 | int sms_queue_start(struct gsm_network *network, int max_pending) |
| 296 | { |
| 297 | struct gsm_sms_queue *sms = talloc_zero(network, struct gsm_sms_queue); |
| 298 | if (!sms) { |
| 299 | LOGP(DMSC, LOGL_ERROR, "Failed to create the SMS queue.\n"); |
| 300 | return -1; |
| 301 | } |
| 302 | |
Pablo Neira Ayuso | ef717c6 | 2011-05-06 12:12:31 +0200 | [diff] [blame] | 303 | osmo_signal_register_handler(SS_SUBSCR, sms_subscr_cb, network); |
| 304 | osmo_signal_register_handler(SS_SMS, sms_sms_cb, network); |
Holger Hans Peter Freyther | 6131833 | 2010-12-24 21:39:55 +0100 | [diff] [blame] | 305 | |
Holger Hans Peter Freyther | 575d2e1 | 2010-12-24 13:48:27 +0100 | [diff] [blame] | 306 | network->sms_queue = sms; |
Holger Hans Peter Freyther | 6131833 | 2010-12-24 21:39:55 +0100 | [diff] [blame] | 307 | INIT_LLIST_HEAD(&sms->pending_sms); |
Holger Hans Peter Freyther | 4871046 | 2010-12-25 17:43:03 +0100 | [diff] [blame] | 308 | sms->max_fail = 1; |
Holger Hans Peter Freyther | 575d2e1 | 2010-12-24 13:48:27 +0100 | [diff] [blame] | 309 | sms->network = network; |
| 310 | sms->max_pending = max_pending; |
Holger Hans Peter Freyther | 6131833 | 2010-12-24 21:39:55 +0100 | [diff] [blame] | 311 | sms->push_queue.data = sms; |
| 312 | sms->push_queue.cb = sms_submit_pending; |
| 313 | sms->resend_pending.data = sms; |
| 314 | sms->resend_pending.cb = sms_resend_pending; |
| 315 | |
| 316 | sms_submit_pending(sms); |
Holger Hans Peter Freyther | 575d2e1 | 2010-12-24 13:48:27 +0100 | [diff] [blame] | 317 | |
| 318 | return 0; |
| 319 | } |
| 320 | |
Holger Hans Peter Freyther | 735a280 | 2011-07-25 00:13:06 +0200 | [diff] [blame] | 321 | static int sub_ready_for_sm(struct gsm_network *net, struct gsm_subscriber *subscr) |
Holger Hans Peter Freyther | 575d2e1 | 2010-12-24 13:48:27 +0100 | [diff] [blame] | 322 | { |
Holger Hans Peter Freyther | 575d2e1 | 2010-12-24 13:48:27 +0100 | [diff] [blame] | 323 | struct gsm_sms *sms; |
Holger Hans Peter Freyther | 735a280 | 2011-07-25 00:13:06 +0200 | [diff] [blame] | 324 | struct gsm_sms_pending *pending; |
| 325 | struct gsm_subscriber_connection *conn; |
Holger Hans Peter Freyther | 575d2e1 | 2010-12-24 13:48:27 +0100 | [diff] [blame] | 326 | |
Holger Hans Peter Freyther | 735a280 | 2011-07-25 00:13:06 +0200 | [diff] [blame] | 327 | /* |
| 328 | * The code used to be very clever and tried to submit |
| 329 | * a SMS during the Location Updating Request. This has |
| 330 | * two issues: |
| 331 | * 1.) The Phone might not be ready yet, e.g. the C155 |
| 332 | * will not respond to the Submit when it is booting. |
| 333 | * 2.) The queue is already trying to submit SMS to the |
| 334 | * user and by not responding to the paging request |
| 335 | * we will set the LAC back to 0. We would have to |
| 336 | * stop the paging and move things over. |
| 337 | * |
| 338 | * We need to be careful in what we try here. |
| 339 | */ |
| 340 | |
| 341 | /* check if we have pending requests */ |
| 342 | pending = sms_subscriber_find_pending(net->sms_queue, subscr); |
| 343 | if (pending) { |
| 344 | LOGP(DMSC, LOGL_NOTICE, |
| 345 | "Pending paging while subscriber %llu attached.\n", |
| 346 | subscr->id); |
Holger Hans Peter Freyther | 735a280 | 2011-07-25 00:13:06 +0200 | [diff] [blame] | 347 | return 0; |
| 348 | } |
| 349 | |
Holger Hans Peter Freyther | 575d2e1 | 2010-12-24 13:48:27 +0100 | [diff] [blame] | 350 | conn = connection_for_subscr(subscr); |
| 351 | if (!conn) |
| 352 | return -1; |
Holger Hans Peter Freyther | 735a280 | 2011-07-25 00:13:06 +0200 | [diff] [blame] | 353 | |
| 354 | /* Now try to deliver any pending SMS to this sub */ |
Holger Hans Peter Freyther | 575d2e1 | 2010-12-24 13:48:27 +0100 | [diff] [blame] | 355 | sms = db_sms_get_unsent_for_subscr(subscr); |
| 356 | if (!sms) |
| 357 | return -1; |
| 358 | gsm411_send_sms(conn, sms); |
| 359 | return 0; |
| 360 | } |
| 361 | |
| 362 | static int sms_subscr_cb(unsigned int subsys, unsigned int signal, |
| 363 | void *handler_data, void *signal_data) |
| 364 | { |
| 365 | struct gsm_subscriber *subscr = signal_data; |
| 366 | |
| 367 | if (signal != S_SUBSCR_ATTACHED) |
| 368 | return 0; |
| 369 | |
| 370 | /* this is readyForSM */ |
Holger Hans Peter Freyther | 735a280 | 2011-07-25 00:13:06 +0200 | [diff] [blame] | 371 | return sub_ready_for_sm(handler_data, subscr); |
Holger Hans Peter Freyther | 575d2e1 | 2010-12-24 13:48:27 +0100 | [diff] [blame] | 372 | } |
| 373 | |
Holger Hans Peter Freyther | 6131833 | 2010-12-24 21:39:55 +0100 | [diff] [blame] | 374 | static int sms_sms_cb(unsigned int subsys, unsigned int signal, |
| 375 | void *handler_data, void *signal_data) |
Holger Hans Peter Freyther | 575d2e1 | 2010-12-24 13:48:27 +0100 | [diff] [blame] | 376 | { |
Holger Hans Peter Freyther | 6131833 | 2010-12-24 21:39:55 +0100 | [diff] [blame] | 377 | struct gsm_network *network = handler_data; |
| 378 | struct sms_signal_data *sig_sms = signal_data; |
| 379 | struct gsm_sms_pending *pending; |
| 380 | |
| 381 | /* We got a new SMS and maybe should launch the queue again. */ |
| 382 | if (signal == S_SMS_SUBMITTED || signal == S_SMS_SMMA) { |
| 383 | sms_queue_trigger(network->sms_queue); |
| 384 | return 0; |
| 385 | } |
| 386 | |
| 387 | if (!sig_sms->sms) |
| 388 | return -1; |
| 389 | |
| 390 | |
| 391 | /* |
| 392 | * Find the entry of our queue. The SMS subsystem will submit |
| 393 | * sms that are not in our control as we just have a channel |
| 394 | * open anyway. |
| 395 | */ |
| 396 | pending = sms_find_pending(network->sms_queue, sig_sms->sms); |
| 397 | if (!pending) |
| 398 | return 0; |
| 399 | |
| 400 | switch (signal) { |
| 401 | case S_SMS_DELIVERED: |
| 402 | /* |
| 403 | * Create place for a new SMS but keep the pending data |
| 404 | * so we will not attempt to send the SMS for this subscriber |
| 405 | * as we still have an open channel and will attempt to submit |
| 406 | * SMS to it anyway. |
| 407 | */ |
| 408 | network->sms_queue->pending -= 1; |
| 409 | sms_submit_pending(network->sms_queue); |
| 410 | sms_pending_free(pending); |
| 411 | break; |
| 412 | case S_SMS_MEM_EXCEEDED: |
| 413 | network->sms_queue->pending -= 1; |
| 414 | sms_pending_free(pending); |
| 415 | sms_queue_trigger(network->sms_queue); |
| 416 | break; |
| 417 | case S_SMS_UNKNOWN_ERROR: |
| 418 | /* |
| 419 | * There can be many reasons for this failure. E.g. the paging |
| 420 | * timed out, the subscriber was not paged at all, or there was |
| 421 | * a protocol error. The current strategy is to try sending the |
| 422 | * next SMS for busy/oom and to retransmit when we have paged. |
| 423 | * |
| 424 | * When the paging expires three times we will disable the |
| 425 | * subscriber. If we have some kind of other transmit error we |
| 426 | * should flag the SMS as bad. |
| 427 | */ |
| 428 | switch (sig_sms->paging_result) { |
| 429 | case 0: |
| 430 | /* BAD SMS? */ |
| 431 | db_sms_inc_deliver_attempts(sig_sms->sms); |
| 432 | sms_pending_failed(pending, 0); |
| 433 | break; |
| 434 | case GSM_PAGING_EXPIRED: |
| 435 | sms_pending_failed(pending, 1); |
| 436 | break; |
| 437 | |
| 438 | case GSM_PAGING_OOM: |
| 439 | case GSM_PAGING_BUSY: |
| 440 | network->sms_queue->pending -= 1; |
| 441 | sms_pending_free(pending); |
| 442 | sms_queue_trigger(network->sms_queue); |
| 443 | break; |
| 444 | default: |
Holger Hans Peter Freyther | d0a34ed | 2012-11-10 19:46:58 +0100 | [diff] [blame] | 445 | LOGP(DLSMS, LOGL_ERROR, "Unhandled result: %d\n", |
Holger Hans Peter Freyther | 6131833 | 2010-12-24 21:39:55 +0100 | [diff] [blame] | 446 | sig_sms->paging_result); |
| 447 | } |
| 448 | break; |
| 449 | default: |
Holger Hans Peter Freyther | d0a34ed | 2012-11-10 19:46:58 +0100 | [diff] [blame] | 450 | LOGP(DLSMS, LOGL_ERROR, "Unhandled result: %d\n", |
Holger Hans Peter Freyther | 6131833 | 2010-12-24 21:39:55 +0100 | [diff] [blame] | 451 | sig_sms->paging_result); |
| 452 | } |
| 453 | |
| 454 | return 0; |
Holger Hans Peter Freyther | 575d2e1 | 2010-12-24 13:48:27 +0100 | [diff] [blame] | 455 | } |
Holger Hans Peter Freyther | 6ec8098 | 2010-12-25 14:08:00 +0100 | [diff] [blame] | 456 | |
| 457 | /* VTY helper functions */ |
| 458 | int sms_queue_stats(struct gsm_sms_queue *smsq, struct vty *vty) |
| 459 | { |
| 460 | struct gsm_sms_pending *pending; |
| 461 | |
| 462 | vty_out(vty, "SMSqueue with max_pending: %d pending: %d%s", |
| 463 | smsq->max_pending, smsq->pending, VTY_NEWLINE); |
| 464 | |
| 465 | llist_for_each_entry(pending, &smsq->pending_sms, entry) |
Holger Hans Peter Freyther | eed400a | 2010-12-27 20:19:48 +0100 | [diff] [blame] | 466 | vty_out(vty, " SMS Pending for Subscriber: %llu SMS: %llu Failed: %d.%s", |
| 467 | pending->subscr->id, pending->sms_id, |
| 468 | pending->failed_attempts, VTY_NEWLINE); |
Holger Hans Peter Freyther | 6ec8098 | 2010-12-25 14:08:00 +0100 | [diff] [blame] | 469 | return 0; |
| 470 | } |
Holger Hans Peter Freyther | 96b4d35 | 2010-12-25 14:25:12 +0100 | [diff] [blame] | 471 | |
| 472 | int sms_queue_set_max_pending(struct gsm_sms_queue *smsq, int max_pending) |
| 473 | { |
Holger Hans Peter Freyther | d0a34ed | 2012-11-10 19:46:58 +0100 | [diff] [blame] | 474 | LOGP(DLSMS, LOGL_NOTICE, "SMSqueue old max: %d new: %d\n", |
Holger Hans Peter Freyther | 96b4d35 | 2010-12-25 14:25:12 +0100 | [diff] [blame] | 475 | smsq->max_pending, max_pending); |
| 476 | smsq->max_pending = max_pending; |
| 477 | return 0; |
| 478 | } |
Holger Hans Peter Freyther | 416c7de | 2010-12-25 14:38:30 +0100 | [diff] [blame] | 479 | |
Holger Hans Peter Freyther | 70428d9 | 2010-12-25 14:50:50 +0100 | [diff] [blame] | 480 | int sms_queue_set_max_failure(struct gsm_sms_queue *smsq, int max_fail) |
| 481 | { |
Holger Hans Peter Freyther | d0a34ed | 2012-11-10 19:46:58 +0100 | [diff] [blame] | 482 | LOGP(DLSMS, LOGL_NOTICE, "SMSqueue max failure old: %d new: %d\n", |
Holger Hans Peter Freyther | 70428d9 | 2010-12-25 14:50:50 +0100 | [diff] [blame] | 483 | smsq->max_fail, max_fail); |
| 484 | smsq->max_fail = max_fail; |
| 485 | return 0; |
| 486 | } |
| 487 | |
Holger Hans Peter Freyther | 416c7de | 2010-12-25 14:38:30 +0100 | [diff] [blame] | 488 | int sms_queue_clear(struct gsm_sms_queue *smsq) |
| 489 | { |
| 490 | struct gsm_sms_pending *pending, *tmp; |
| 491 | |
| 492 | llist_for_each_entry_safe(pending, tmp, &smsq->pending_sms, entry) { |
Holger Hans Peter Freyther | d0a34ed | 2012-11-10 19:46:58 +0100 | [diff] [blame] | 493 | LOGP(DLSMS, LOGL_NOTICE, |
Holger Hans Peter Freyther | 416c7de | 2010-12-25 14:38:30 +0100 | [diff] [blame] | 494 | "SMSqueue clearing for sub %llu\n", pending->subscr->id); |
| 495 | sms_pending_free(pending); |
| 496 | } |
| 497 | |
Holger Hans Peter Freyther | 7142b28 | 2010-12-28 14:09:07 +0100 | [diff] [blame] | 498 | smsq->pending = 0; |
Holger Hans Peter Freyther | 416c7de | 2010-12-25 14:38:30 +0100 | [diff] [blame] | 499 | return 0; |
| 500 | } |