blob: 6d7fb8b55b276a1057f97b2e1cd389e11eba9a3d [file] [log] [blame]
Andreas Eversbergc1a91a82011-10-28 11:05:37 +02001/* 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 * (C) 2010 by Holger Hans Peter Freyther <zecke@selfish.org>
8 * (C) 2010 by On-Waves
9 * (C) 2011 by Andreas Eversberg <jolly@eversberg.eu>
10 *
11 * All Rights Reserved
12 *
13 * This program is free software; you can redistribute it and/or modify
Harald Welte388fb032014-10-26 20:42:49 +010014 * it under the terms of the GNU General Public License as published by
15 * the Free Software Foundation; either version 2 of the License, or
Andreas Eversbergc1a91a82011-10-28 11:05:37 +020016 * (at your option) any later version.
17 *
18 * This program is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
Harald Welte388fb032014-10-26 20:42:49 +010021 * GNU General Public License for more details.
Andreas Eversbergc1a91a82011-10-28 11:05:37 +020022 *
Harald Welte388fb032014-10-26 20:42:49 +010023 * You should have received a copy of the GNU General Public License
Andreas Eversbergc1a91a82011-10-28 11:05:37 +020024 * along with this program. If not, see <http://www.gnu.org/licenses/>.
25 *
26 */
27
28/* Notes on msg:
29 *
30 * Messages from lower layer are freed by lower layer.
31 *
32 * Messages to upper layer are freed after upper layer call returns, so upper
33 * layer cannot use data after returning. Upper layer must not free the msg.
34 *
35 * This implies: Lower layer messages can be forwarded to upper layer.
36 *
37 * Upper layer messages are freed by lower layer, so they must not be freed
38 * after calling lower layer.
39 *
40 *
41 * Notes on release:
42 *
43 * Sending Abort/Release (MNSMS-ABORT-REQ or MNSMS-REL-REQ) may cause the
44 * lower layer to become IDLE. Then it is allowed to destroy this instance,
Holger Hans Peter Freyther1c4c3732012-11-22 00:33:52 +010045 * so sending this MUST be the last thing that is done.
Andreas Eversbergc1a91a82011-10-28 11:05:37 +020046 *
47 */
48
49
50#include <string.h>
51#include <errno.h>
Holger Hans Peter Freytherbcf125c2012-11-14 06:07:47 +010052#include <inttypes.h>
Andreas Eversbergc1a91a82011-10-28 11:05:37 +020053#include <osmocom/core/msgb.h>
54#include <osmocom/core/logging.h>
55#include <osmocom/core/timer.h>
56#include <osmocom/gsm/tlv.h>
57
58#include <osmocom/gsm/gsm0411_utils.h>
59#include <osmocom/gsm/gsm0411_smc.h>
60#include <osmocom/gsm/gsm0411_smr.h>
61#include <osmocom/gsm/protocol/gsm_04_08.h>
62
Holger Hans Peter Freytherbcf125c2012-11-14 06:07:47 +010063#define SMR_LOG_STR "SMR(%" PRIu64 ") "
64
Andreas Eversbergc1a91a82011-10-28 11:05:37 +020065static void rp_timer_expired(void *data);
66
67/* init a new instance */
Holger Hans Peter Freytherbcf125c2012-11-14 06:07:47 +010068void gsm411_smr_init(struct gsm411_smr_inst *inst, uint64_t id, int network,
Andreas Eversbergc1a91a82011-10-28 11:05:37 +020069 int (*rl_recv) (struct gsm411_smr_inst *inst, int msg_type,
70 struct msgb *msg),
71 int (*mn_send) (struct gsm411_smr_inst *inst, int msg_type,
72 struct msgb *msg))
73{
74 memset(inst, 0, sizeof(*inst));
Holger Hans Peter Freytherbcf125c2012-11-14 06:07:47 +010075 inst->id = id;
Andreas Eversbergc1a91a82011-10-28 11:05:37 +020076 inst->network = network;
77 inst->rp_state = GSM411_RPS_IDLE;
78 inst->rl_recv = rl_recv;
79 inst->mn_send = mn_send;
Pablo Neira Ayuso44f423f2017-05-08 18:00:28 +020080 osmo_timer_setup(&inst->rp_timer, rp_timer_expired, inst);
Andreas Eversbergc1a91a82011-10-28 11:05:37 +020081
Holger Hans Peter Freytherbcf125c2012-11-14 06:07:47 +010082 LOGP(DLSMS, LOGL_INFO,
Holger Hans Peter Freyther68f94472012-12-01 12:51:34 +010083 SMR_LOG_STR "instance created for %s.\n",
84 inst->id, inst->network ? "network" : "mobile");
Andreas Eversbergc1a91a82011-10-28 11:05:37 +020085}
86
87/* clear instance */
88void gsm411_smr_clear(struct gsm411_smr_inst *inst)
89{
Holger Hans Peter Freytherbcf125c2012-11-14 06:07:47 +010090 LOGP(DLSMS, LOGL_INFO,
91 SMR_LOG_STR "clearing SMR instance\n", inst->id);
Andreas Eversbergc1a91a82011-10-28 11:05:37 +020092
93 osmo_timer_del(&inst->rp_timer);
94}
95
Holger Hans Peter Freyther61d33922014-05-23 08:49:34 +020096static const char *smr_state_names[] = {
Andreas Eversbergc1a91a82011-10-28 11:05:37 +020097 "IDLE",
98 "WAIT_FOR_RP_ACK",
Holger Hans Peter Freytherda73aa62014-05-23 08:51:22 +020099 "illegal state 2",
Andreas Eversbergc1a91a82011-10-28 11:05:37 +0200100 "WAIT_TO_TX_RP_ACK",
101 "WAIT_FOR_RETRANS_T",
102};
103
104const struct value_string gsm411_rp_cause_strs[] = {
105 { GSM411_RP_CAUSE_MO_NUM_UNASSIGNED, "(MO) Number not assigned" },
106 { GSM411_RP_CAUSE_MO_OP_DET_BARR, "(MO) Operator determined barring" },
107 { GSM411_RP_CAUSE_MO_CALL_BARRED, "(MO) Call barred" },
108 { GSM411_RP_CAUSE_MO_SMS_REJECTED, "(MO) SMS rejected" },
109 { GSM411_RP_CAUSE_MO_DEST_OUT_OF_ORDER, "(MO) Destination out of order" },
110 { GSM411_RP_CAUSE_MO_UNIDENTIFIED_SUBSCR, "(MO) Unidentified subscriber" },
111 { GSM411_RP_CAUSE_MO_FACILITY_REJ, "(MO) Facility reject" },
112 { GSM411_RP_CAUSE_MO_UNKNOWN_SUBSCR, "(MO) Unknown subscriber" },
113 { GSM411_RP_CAUSE_MO_NET_OUT_OF_ORDER, "(MO) Network out of order" },
114 { GSM411_RP_CAUSE_MO_TEMP_FAIL, "(MO) Temporary failure" },
115 { GSM411_RP_CAUSE_MO_CONGESTION, "(MO) Congestion" },
116 { GSM411_RP_CAUSE_MO_RES_UNAVAIL, "(MO) Resource unavailable" },
117 { GSM411_RP_CAUSE_MO_REQ_FAC_NOTSUBSCR, "(MO) Requested facility not subscribed" },
118 { GSM411_RP_CAUSE_MO_REQ_FAC_NOTIMPL, "(MO) Requested facility not implemented" },
119 { GSM411_RP_CAUSE_MO_INTERWORKING, "(MO) Interworking" },
120 /* valid only for MT */
121 { GSM411_RP_CAUSE_MT_MEM_EXCEEDED, "(MT) Memory Exceeded" },
122 /* valid for both directions */
123 { GSM411_RP_CAUSE_INV_TRANS_REF, "Invalid Transaction Reference" },
124 { GSM411_RP_CAUSE_SEMANT_INC_MSG, "Semantically Incorrect Message" },
125 { GSM411_RP_CAUSE_INV_MAND_INF, "Invalid Mandatory Information" },
126 { GSM411_RP_CAUSE_MSGTYPE_NOTEXIST, "Message Type non-existant" },
127 { GSM411_RP_CAUSE_MSG_INCOMP_STATE, "Message incompatible with protocol state" },
128 { GSM411_RP_CAUSE_IE_NOTEXIST, "Information Element not existing" },
129 { GSM411_RP_CAUSE_PROTOCOL_ERR, "Protocol Error" },
130 { 0, NULL }
131};
132
133static void new_rp_state(struct gsm411_smr_inst *inst,
134 enum gsm411_rp_state state)
135{
Holger Hans Peter Freytherbcf125c2012-11-14 06:07:47 +0100136 LOGP(DLSMS, LOGL_INFO,
137 SMR_LOG_STR "new RP state %s -> %s\n", inst->id,
Andreas Eversbergc1a91a82011-10-28 11:05:37 +0200138 smr_state_names[inst->rp_state], smr_state_names[state]);
139 inst->rp_state = state;
140
141 /* stop timer when going idle */
142 if (state == GSM411_RPS_IDLE)
143 osmo_timer_del(&inst->rp_timer);
144}
145
146/* Prefix msg with a RP-DATA header and send as CP-DATA */
147static int gsm411_rp_sendmsg(struct gsm411_smr_inst *inst, struct msgb *msg,
148 uint8_t rp_msg_type, uint8_t rp_msg_ref,
149 int mnsms_msg_type)
150{
151 struct gsm411_rp_hdr *rp;
152 uint8_t len = msg->len;
153
154 /* GSM 04.11 RP-DATA header */
155 rp = (struct gsm411_rp_hdr *)msgb_push(msg, sizeof(*rp));
156 rp->len = len + 2;
157 rp->msg_type = rp_msg_type;
Holger Hans Peter Freytherf4f5a842014-02-08 15:14:53 +0100158 rp->msg_ref = rp_msg_ref;
Andreas Eversbergc1a91a82011-10-28 11:05:37 +0200159
160 return inst->mn_send(inst, mnsms_msg_type, msg);
161}
162
163static int gsm411_send_rp_error(struct gsm411_smr_inst *inst,
164 uint8_t msg_ref, uint8_t cause)
165{
166 struct msgb *msg = gsm411_msgb_alloc();
167
168 msgb_tv_put(msg, 1, cause);
169
Holger Hans Peter Freytherbcf125c2012-11-14 06:07:47 +0100170 LOGP(DLSMS, LOGL_NOTICE,
171 SMR_LOG_STR "TX: SMS RP ERROR, cause %d (%s)\n", inst->id,
172 cause, get_value_string(gsm411_rp_cause_strs, cause));
Andreas Eversbergc1a91a82011-10-28 11:05:37 +0200173
174 return gsm411_rp_sendmsg(inst, msg,
175 (inst->network) ? GSM411_MT_RP_ERROR_MT : GSM411_MT_RP_ERROR_MO,
176 msg_ref, GSM411_MNSMS_DATA_REQ);
177}
178
179static int gsm411_send_release(struct gsm411_smr_inst *inst)
180{
181 struct msgb *msg = gsm411_msgb_alloc();
182
Holger Hans Peter Freytherbcf125c2012-11-14 06:07:47 +0100183 LOGP(DLSMS, LOGL_DEBUG,
184 SMR_LOG_STR "TX: MNSMS-REL-REQ\n", inst->id);
Andreas Eversbergc1a91a82011-10-28 11:05:37 +0200185
186 return inst->mn_send(inst, GSM411_MNSMS_REL_REQ, msg);
187}
188
189static int gsm411_send_abort(struct gsm411_smr_inst *inst)
190{
191 struct msgb *msg = gsm411_msgb_alloc();
192
Holger Hans Peter Freytherbcf125c2012-11-14 06:07:47 +0100193 LOGP(DLSMS, LOGL_DEBUG,
194 SMR_LOG_STR "TX: MNSMS-ABORT-REQ\n", inst->id);
195
Andreas Eversbergc1a91a82011-10-28 11:05:37 +0200196 msgb_tv_put(msg, 1, 111); //FIXME: better idea ? */
Andreas Eversbergc1a91a82011-10-28 11:05:37 +0200197
198 return inst->mn_send(inst, GSM411_MNSMS_ABORT_REQ, msg);
199}
200
201static int gsm411_send_report(struct gsm411_smr_inst *inst)
202{
203 struct msgb *msg = gsm411_msgb_alloc();
204
Holger Hans Peter Freytherbcf125c2012-11-14 06:07:47 +0100205 LOGP(DLSMS, LOGL_DEBUG,
206 SMR_LOG_STR "Sending empty SM_RL_REPORT_IND\n", inst->id);
Andreas Eversbergc1a91a82011-10-28 11:05:37 +0200207
208 return inst->rl_recv(inst, GSM411_SM_RL_REPORT_IND, msg);
209}
210
211static int gsm411_rl_data_req(struct gsm411_smr_inst *inst, struct msgb *msg)
212{
Holger Hans Peter Freytherbcf125c2012-11-14 06:07:47 +0100213 LOGP(DLSMS, LOGL_DEBUG,
214 SMR_LOG_STR "TX SMS RP-DATA\n", inst->id);
Andreas Eversbergc1a91a82011-10-28 11:05:37 +0200215 /* start TR1N and enter 'wait for RP-ACK state' */
216 osmo_timer_schedule(&inst->rp_timer, GSM411_TMR_TR1M);
217 new_rp_state(inst, GSM411_RPS_WAIT_FOR_RP_ACK);
218
219 return inst->mn_send(inst, GSM411_MNSMS_EST_REQ, msg);
220}
221
222static int gsm411_rl_report_req(struct gsm411_smr_inst *inst, struct msgb *msg)
223{
Holger Hans Peter Freytherbcf125c2012-11-14 06:07:47 +0100224 LOGP(DLSMS, LOGL_DEBUG,
225 SMR_LOG_STR "TX SMS REPORT\n", inst->id);
Andreas Eversbergc1a91a82011-10-28 11:05:37 +0200226 new_rp_state(inst, GSM411_RPS_IDLE);
227
228 inst->mn_send(inst, GSM411_MNSMS_DATA_REQ, msg);
229 gsm411_send_release(inst);
230 return 0;
231}
232
233static int gsm411_mnsms_est_ind(struct gsm411_smr_inst *inst, struct msgb *msg)
234{
235 struct gsm48_hdr *gh = (struct gsm48_hdr*)msg->l3h;
236 struct gsm411_rp_hdr *rp_data = (struct gsm411_rp_hdr*)&gh->data;
237 uint8_t msg_type = rp_data->msg_type & 0x07;
238 int rc;
239
240 /* check direction */
241 if (inst->network == (msg_type & 1)) {
Holger Hans Peter Freytherbcf125c2012-11-14 06:07:47 +0100242 LOGP(DLSMS, LOGL_NOTICE,
243 SMR_LOG_STR "Invalid RP type 0x%02x\n",
244 inst->id, msg_type);
Andreas Eversbergc1a91a82011-10-28 11:05:37 +0200245 gsm411_send_rp_error(inst, rp_data->msg_ref,
246 GSM411_RP_CAUSE_MSG_INCOMP_STATE);
247 new_rp_state(inst, GSM411_RPS_IDLE);
248 gsm411_send_release(inst);
249 return -EINVAL;
250 }
251
252 switch (msg_type) {
253 case GSM411_MT_RP_DATA_MT:
254 case GSM411_MT_RP_DATA_MO:
Holger Hans Peter Freytherbcf125c2012-11-14 06:07:47 +0100255 LOGP(DLSMS, LOGL_DEBUG,
256 SMR_LOG_STR "RX SMS RP-DATA\n", inst->id);
Andreas Eversbergc1a91a82011-10-28 11:05:37 +0200257 /* start TR2N and enter 'wait to send RP-ACK state' */
258 osmo_timer_schedule(&inst->rp_timer, GSM411_TMR_TR2M);
259 new_rp_state(inst, GSM411_RPS_WAIT_TO_TX_RP_ACK);
260 rc = inst->rl_recv(inst, GSM411_SM_RL_DATA_IND, msg);
261 break;
262 case GSM411_MT_RP_SMMA_MO:
Holger Hans Peter Freytherbcf125c2012-11-14 06:07:47 +0100263 LOGP(DLSMS, LOGL_DEBUG,
264 SMR_LOG_STR "RX SMS RP-SMMA\n", inst->id);
Andreas Eversbergc1a91a82011-10-28 11:05:37 +0200265 /* start TR2N and enter 'wait to send RP-ACK state' */
266 osmo_timer_schedule(&inst->rp_timer, GSM411_TMR_TR2M);
267 new_rp_state(inst, GSM411_RPS_WAIT_TO_TX_RP_ACK);
268 rc = inst->rl_recv(inst, GSM411_SM_RL_DATA_IND, msg);
269 break;
270 default:
Holger Hans Peter Freytherbcf125c2012-11-14 06:07:47 +0100271 LOGP(DLSMS, LOGL_NOTICE,
272 SMR_LOG_STR "invalid RP type 0x%02x\n",
273 inst->id, msg_type);
Andreas Eversbergc1a91a82011-10-28 11:05:37 +0200274 gsm411_send_rp_error(inst, rp_data->msg_ref,
275 GSM411_RP_CAUSE_MSGTYPE_NOTEXIST);
276 new_rp_state(inst, GSM411_RPS_IDLE);
277 rc = -EINVAL;
278 break;
279 }
280
281 return rc;
282}
283
284static int gsm411_mnsms_data_ind_tx(struct gsm411_smr_inst *inst,
285 struct msgb *msg)
286{
287 struct gsm48_hdr *gh = (struct gsm48_hdr*)msg->l3h;
288 struct gsm411_rp_hdr *rp_data = (struct gsm411_rp_hdr*)&gh->data;
289 uint8_t msg_type = rp_data->msg_type & 0x07;
290 int rc;
291
292 /* check direction */
293 if (inst->network == (msg_type & 1)) {
Holger Hans Peter Freytherbcf125c2012-11-14 06:07:47 +0100294 LOGP(DLSMS, LOGL_NOTICE,
295 SMR_LOG_STR "invalid RP type 0x%02x\n",
296 inst->id, msg_type);
Andreas Eversbergc1a91a82011-10-28 11:05:37 +0200297 gsm411_send_rp_error(inst, rp_data->msg_ref,
298 GSM411_RP_CAUSE_MSG_INCOMP_STATE);
299 new_rp_state(inst, GSM411_RPS_IDLE);
300 gsm411_send_release(inst);
301 return -EINVAL;
302 }
303
304 switch (msg_type) {
305 case GSM411_MT_RP_ACK_MO:
306 case GSM411_MT_RP_ACK_MT:
Holger Hans Peter Freytherbcf125c2012-11-14 06:07:47 +0100307 LOGP(DLSMS, LOGL_DEBUG,
308 SMR_LOG_STR "RX SMS RP-ACK\n", inst->id);
Andreas Eversbergc1a91a82011-10-28 11:05:37 +0200309 new_rp_state(inst, GSM411_RPS_IDLE);
310 inst->rl_recv(inst, GSM411_SM_RL_REPORT_IND, msg);
311 gsm411_send_release(inst);
312 return 0;
313 case GSM411_MT_RP_ERROR_MO:
314 case GSM411_MT_RP_ERROR_MT:
Holger Hans Peter Freytherbcf125c2012-11-14 06:07:47 +0100315 LOGP(DLSMS, LOGL_DEBUG,
316 SMR_LOG_STR "RX SMS RP-ERROR\n", inst->id);
Andreas Eversbergc1a91a82011-10-28 11:05:37 +0200317 new_rp_state(inst, GSM411_RPS_IDLE);
318 inst->rl_recv(inst, GSM411_SM_RL_REPORT_IND, msg);
319 gsm411_send_release(inst);
320 return 0;
321 default:
Holger Hans Peter Freytherbcf125c2012-11-14 06:07:47 +0100322 LOGP(DLSMS, LOGL_NOTICE,
323 SMR_LOG_STR "Invalid RP type 0x%02x\n",
324 inst->id, msg_type);
Andreas Eversbergc1a91a82011-10-28 11:05:37 +0200325 gsm411_send_rp_error(inst, rp_data->msg_ref,
326 GSM411_RP_CAUSE_MSGTYPE_NOTEXIST);
327 new_rp_state(inst, GSM411_RPS_IDLE);
328 gsm411_send_release(inst);
329 return -EINVAL;
330 }
331
332 return rc;
333}
334
335static int gsm411_mnsms_error_ind_tx(struct gsm411_smr_inst *inst,
336 struct msgb *msg)
337{
Holger Hans Peter Freytherbcf125c2012-11-14 06:07:47 +0100338 LOGP(DLSMS, LOGL_DEBUG,
Holger Hans Peter Freyther1c4c3732012-11-22 00:33:52 +0100339 SMR_LOG_STR "TX SMS MNSMS-ERROR-IND\n", inst->id);
Andreas Eversbergc1a91a82011-10-28 11:05:37 +0200340 new_rp_state(inst, GSM411_RPS_IDLE);
341 inst->rl_recv(inst, GSM411_SM_RL_REPORT_IND, msg);
342 gsm411_send_release(inst);
343 return 0;
344}
345
346static int gsm411_mnsms_error_ind_rx(struct gsm411_smr_inst *inst,
347 struct msgb *msg)
348{
Holger Hans Peter Freytherbcf125c2012-11-14 06:07:47 +0100349 LOGP(DLSMS, LOGL_DEBUG,
350 SMR_LOG_STR "RX SMS MNSMS-ERROR-IND\n", inst->id);
Andreas Eversbergc1a91a82011-10-28 11:05:37 +0200351 new_rp_state(inst, GSM411_RPS_IDLE);
352 return inst->rl_recv(inst, GSM411_SM_RL_REPORT_IND, msg);
353}
354
355/* SMR TR1* is expired */
356static void rp_timer_expired(void *data)
357{
358 struct gsm411_smr_inst *inst = data;
Holger Hans Peter Freytherbcf125c2012-11-14 06:07:47 +0100359 const char *str;
Andreas Eversbergc1a91a82011-10-28 11:05:37 +0200360
Holger Hans Peter Freytherbcf125c2012-11-14 06:07:47 +0100361 str = inst->rp_state == GSM411_RPS_WAIT_TO_TX_RP_ACK
362 ? "TR2N" : "TR1N";
363
364 LOGP(DLSMS, LOGL_DEBUG,
365 SMR_LOG_STR "%s expired\n", inst->id, str);
Andreas Eversbergc1a91a82011-10-28 11:05:37 +0200366 gsm411_send_report(inst);
367 gsm411_send_abort(inst);
368}
369
370/* statefull handling for SM-RL SAP messages */
Holger Hans Peter Freyther9473c5d2012-11-22 10:50:52 +0100371static const struct smrdownstate {
Andreas Eversbergc1a91a82011-10-28 11:05:37 +0200372 uint32_t states;
373 int type;
374 const char *name;
375 int (*rout) (struct gsm411_smr_inst *inst,
376 struct msgb *msg);
377} smrdownstatelist[] = {
378 /* data request */
379 {SBIT(GSM411_RPS_IDLE),
380 GSM411_SM_RL_DATA_REQ,
381 "SM-RL-DATA_REQ", gsm411_rl_data_req},
382
383 /* report request */
384 {SBIT(GSM411_RPS_WAIT_TO_TX_RP_ACK),
385 GSM411_SM_RL_REPORT_REQ,
386 "SM-RL-REPORT_REQ", gsm411_rl_report_req},
387};
388
389#define SMRDOWNSLLEN \
390 (sizeof(smrdownstatelist) / sizeof(struct smrdownstate))
391
392/* message from upper layer */
393int gsm411_smr_send(struct gsm411_smr_inst *inst, int msg_type,
394 struct msgb *msg)
395{
396 int i, rc;
397
398 /* find function for current state and message */
399 for (i = 0; i < SMRDOWNSLLEN; i++) {
400 if ((msg_type == smrdownstatelist[i].type)
401 && (SBIT(inst->rp_state) & smrdownstatelist[i].states))
402 break;
403 }
404 if (i == SMRDOWNSLLEN) {
Holger Hans Peter Freytherbcf125c2012-11-14 06:07:47 +0100405 LOGP(DLSMS, LOGL_NOTICE,
406 SMR_LOG_STR "message %u unhandled at this state "
407 "%s.\n", inst->id, msg_type,
408 smr_state_names[inst->rp_state]);
Andreas Eversbergc1a91a82011-10-28 11:05:37 +0200409 msgb_free(msg);
410 return 0;
411 }
412
Holger Hans Peter Freytherbcf125c2012-11-14 06:07:47 +0100413 LOGP(DLSMS, LOGL_INFO,
414 SMR_LOG_STR "message %s received in state %s\n", inst->id,
Andreas Eversbergc1a91a82011-10-28 11:05:37 +0200415 smrdownstatelist[i].name, smr_state_names[inst->rp_state]);
416
417 rc = smrdownstatelist[i].rout(inst, msg);
418
419 return rc;
420}
421
422/* statefull handling for MMSMS SAP messages */
Holger Hans Peter Freyther9473c5d2012-11-22 10:50:52 +0100423static const struct smrdatastate {
Andreas Eversbergc1a91a82011-10-28 11:05:37 +0200424 uint32_t states;
425 int type;
426 const char *name;
427 int (*rout) (struct gsm411_smr_inst *inst,
428 struct msgb *msg);
429} smrdatastatelist[] = {
430 /* establish indication */
431 {SBIT(GSM411_RPS_IDLE),
432 GSM411_MNSMS_EST_IND,
433 "MNSMS-EST-IND", gsm411_mnsms_est_ind},
434
435 /* data indication */
436 {SBIT(GSM411_RPS_WAIT_FOR_RP_ACK),
437 GSM411_MNSMS_DATA_IND,
438 "MNSMS-DATA-IND", gsm411_mnsms_data_ind_tx},
439
440 /* error indication */
441 {SBIT(GSM411_RPS_WAIT_FOR_RP_ACK),
442 GSM411_MNSMS_ERROR_IND,
443 "MNSMS-ERROR-IND", gsm411_mnsms_error_ind_tx},
444
445 /* error indication */
446 {SBIT(GSM411_RPS_WAIT_TO_TX_RP_ACK),
447 GSM411_MNSMS_ERROR_IND,
448 "MNSMS-ERROR-IND", gsm411_mnsms_error_ind_rx},
449
450};
451
452#define SMRDATASLLEN \
453 (sizeof(smrdatastatelist) / sizeof(struct smrdatastate))
454
455/* message from lower layer
456 * WARNING: We must not free msg, since it will be performed by the
457 * lower layer. */
458int gsm411_smr_recv(struct gsm411_smr_inst *inst, int msg_type,
459 struct msgb *msg)
460{
461 int i, rc;
462
463 /* find function for current state and message */
464 for (i = 0; i < SMRDATASLLEN; i++) {
Holger Hans Peter Freyther1c4c3732012-11-22 00:33:52 +0100465 /* state must match, MM message must match
Andreas Eversbergc1a91a82011-10-28 11:05:37 +0200466 * CP msg must match only in case of MMSMS_DATA_IND
467 */
468 if ((msg_type == smrdatastatelist[i].type)
469 && (SBIT(inst->rp_state) & smrdatastatelist[i].states))
470 break;
471 }
472 if (i == SMRDATASLLEN) {
Holger Hans Peter Freytherbcf125c2012-11-14 06:07:47 +0100473 LOGP(DLSMS, LOGL_NOTICE,
474 SMR_LOG_STR "message %u unhandled at this state "
475 "%s.\n", inst->id, msg_type,
476 smr_state_names[inst->rp_state]);
Andreas Eversbergc1a91a82011-10-28 11:05:37 +0200477 return 0;
478 }
479
Holger Hans Peter Freytherbcf125c2012-11-14 06:07:47 +0100480 LOGP(DLSMS, LOGL_INFO,
481 SMR_LOG_STR "message %s received in state %s\n", inst->id,
Andreas Eversbergc1a91a82011-10-28 11:05:37 +0200482 smrdatastatelist[i].name, smr_state_names[inst->rp_state]);
483
484 rc = smrdatastatelist[i].rout(inst, msg);
485
486 return rc;
487}