blob: eb8aec0d2d0c98c0d16d1aa3f321efd9139a8e99 [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;
Andreas Eversbergc1a91a82011-10-28 11:05:37 +0200290
291 /* check direction */
292 if (inst->network == (msg_type & 1)) {
Holger Hans Peter Freytherbcf125c2012-11-14 06:07:47 +0100293 LOGP(DLSMS, LOGL_NOTICE,
294 SMR_LOG_STR "invalid RP type 0x%02x\n",
295 inst->id, msg_type);
Andreas Eversbergc1a91a82011-10-28 11:05:37 +0200296 gsm411_send_rp_error(inst, rp_data->msg_ref,
297 GSM411_RP_CAUSE_MSG_INCOMP_STATE);
298 new_rp_state(inst, GSM411_RPS_IDLE);
299 gsm411_send_release(inst);
300 return -EINVAL;
301 }
302
303 switch (msg_type) {
304 case GSM411_MT_RP_ACK_MO:
305 case GSM411_MT_RP_ACK_MT:
Holger Hans Peter Freytherbcf125c2012-11-14 06:07:47 +0100306 LOGP(DLSMS, LOGL_DEBUG,
307 SMR_LOG_STR "RX SMS RP-ACK\n", inst->id);
Andreas Eversbergc1a91a82011-10-28 11:05:37 +0200308 new_rp_state(inst, GSM411_RPS_IDLE);
309 inst->rl_recv(inst, GSM411_SM_RL_REPORT_IND, msg);
310 gsm411_send_release(inst);
311 return 0;
312 case GSM411_MT_RP_ERROR_MO:
313 case GSM411_MT_RP_ERROR_MT:
Holger Hans Peter Freytherbcf125c2012-11-14 06:07:47 +0100314 LOGP(DLSMS, LOGL_DEBUG,
315 SMR_LOG_STR "RX SMS RP-ERROR\n", inst->id);
Andreas Eversbergc1a91a82011-10-28 11:05:37 +0200316 new_rp_state(inst, GSM411_RPS_IDLE);
317 inst->rl_recv(inst, GSM411_SM_RL_REPORT_IND, msg);
318 gsm411_send_release(inst);
319 return 0;
320 default:
Holger Hans Peter Freytherbcf125c2012-11-14 06:07:47 +0100321 LOGP(DLSMS, LOGL_NOTICE,
322 SMR_LOG_STR "Invalid RP type 0x%02x\n",
323 inst->id, msg_type);
Andreas Eversbergc1a91a82011-10-28 11:05:37 +0200324 gsm411_send_rp_error(inst, rp_data->msg_ref,
325 GSM411_RP_CAUSE_MSGTYPE_NOTEXIST);
326 new_rp_state(inst, GSM411_RPS_IDLE);
327 gsm411_send_release(inst);
328 return -EINVAL;
329 }
330
Vadim Yanitskiy4c3e4ea2017-05-15 21:32:43 +0300331 return 0;
Andreas Eversbergc1a91a82011-10-28 11:05:37 +0200332}
333
334static int gsm411_mnsms_error_ind_tx(struct gsm411_smr_inst *inst,
335 struct msgb *msg)
336{
Holger Hans Peter Freytherbcf125c2012-11-14 06:07:47 +0100337 LOGP(DLSMS, LOGL_DEBUG,
Holger Hans Peter Freyther1c4c3732012-11-22 00:33:52 +0100338 SMR_LOG_STR "TX SMS MNSMS-ERROR-IND\n", inst->id);
Andreas Eversbergc1a91a82011-10-28 11:05:37 +0200339 new_rp_state(inst, GSM411_RPS_IDLE);
340 inst->rl_recv(inst, GSM411_SM_RL_REPORT_IND, msg);
341 gsm411_send_release(inst);
342 return 0;
343}
344
345static int gsm411_mnsms_error_ind_rx(struct gsm411_smr_inst *inst,
346 struct msgb *msg)
347{
Holger Hans Peter Freytherbcf125c2012-11-14 06:07:47 +0100348 LOGP(DLSMS, LOGL_DEBUG,
349 SMR_LOG_STR "RX SMS MNSMS-ERROR-IND\n", inst->id);
Andreas Eversbergc1a91a82011-10-28 11:05:37 +0200350 new_rp_state(inst, GSM411_RPS_IDLE);
351 return inst->rl_recv(inst, GSM411_SM_RL_REPORT_IND, msg);
352}
353
354/* SMR TR1* is expired */
355static void rp_timer_expired(void *data)
356{
357 struct gsm411_smr_inst *inst = data;
Holger Hans Peter Freytherbcf125c2012-11-14 06:07:47 +0100358 const char *str;
Andreas Eversbergc1a91a82011-10-28 11:05:37 +0200359
Holger Hans Peter Freytherbcf125c2012-11-14 06:07:47 +0100360 str = inst->rp_state == GSM411_RPS_WAIT_TO_TX_RP_ACK
361 ? "TR2N" : "TR1N";
362
363 LOGP(DLSMS, LOGL_DEBUG,
364 SMR_LOG_STR "%s expired\n", inst->id, str);
Andreas Eversbergc1a91a82011-10-28 11:05:37 +0200365 gsm411_send_report(inst);
366 gsm411_send_abort(inst);
367}
368
369/* statefull handling for SM-RL SAP messages */
Holger Hans Peter Freyther9473c5d2012-11-22 10:50:52 +0100370static const struct smrdownstate {
Andreas Eversbergc1a91a82011-10-28 11:05:37 +0200371 uint32_t states;
372 int type;
373 const char *name;
374 int (*rout) (struct gsm411_smr_inst *inst,
375 struct msgb *msg);
376} smrdownstatelist[] = {
377 /* data request */
378 {SBIT(GSM411_RPS_IDLE),
379 GSM411_SM_RL_DATA_REQ,
380 "SM-RL-DATA_REQ", gsm411_rl_data_req},
381
382 /* report request */
383 {SBIT(GSM411_RPS_WAIT_TO_TX_RP_ACK),
384 GSM411_SM_RL_REPORT_REQ,
385 "SM-RL-REPORT_REQ", gsm411_rl_report_req},
386};
387
388#define SMRDOWNSLLEN \
389 (sizeof(smrdownstatelist) / sizeof(struct smrdownstate))
390
391/* message from upper layer */
392int gsm411_smr_send(struct gsm411_smr_inst *inst, int msg_type,
393 struct msgb *msg)
394{
395 int i, rc;
396
397 /* find function for current state and message */
398 for (i = 0; i < SMRDOWNSLLEN; i++) {
399 if ((msg_type == smrdownstatelist[i].type)
400 && (SBIT(inst->rp_state) & smrdownstatelist[i].states))
401 break;
402 }
403 if (i == SMRDOWNSLLEN) {
Holger Hans Peter Freytherbcf125c2012-11-14 06:07:47 +0100404 LOGP(DLSMS, LOGL_NOTICE,
405 SMR_LOG_STR "message %u unhandled at this state "
406 "%s.\n", inst->id, msg_type,
407 smr_state_names[inst->rp_state]);
Andreas Eversbergc1a91a82011-10-28 11:05:37 +0200408 msgb_free(msg);
409 return 0;
410 }
411
Holger Hans Peter Freytherbcf125c2012-11-14 06:07:47 +0100412 LOGP(DLSMS, LOGL_INFO,
413 SMR_LOG_STR "message %s received in state %s\n", inst->id,
Andreas Eversbergc1a91a82011-10-28 11:05:37 +0200414 smrdownstatelist[i].name, smr_state_names[inst->rp_state]);
415
416 rc = smrdownstatelist[i].rout(inst, msg);
417
418 return rc;
419}
420
421/* statefull handling for MMSMS SAP messages */
Holger Hans Peter Freyther9473c5d2012-11-22 10:50:52 +0100422static const struct smrdatastate {
Andreas Eversbergc1a91a82011-10-28 11:05:37 +0200423 uint32_t states;
424 int type;
425 const char *name;
426 int (*rout) (struct gsm411_smr_inst *inst,
427 struct msgb *msg);
428} smrdatastatelist[] = {
429 /* establish indication */
430 {SBIT(GSM411_RPS_IDLE),
431 GSM411_MNSMS_EST_IND,
432 "MNSMS-EST-IND", gsm411_mnsms_est_ind},
433
434 /* data indication */
435 {SBIT(GSM411_RPS_WAIT_FOR_RP_ACK),
436 GSM411_MNSMS_DATA_IND,
437 "MNSMS-DATA-IND", gsm411_mnsms_data_ind_tx},
438
439 /* error indication */
440 {SBIT(GSM411_RPS_WAIT_FOR_RP_ACK),
441 GSM411_MNSMS_ERROR_IND,
442 "MNSMS-ERROR-IND", gsm411_mnsms_error_ind_tx},
443
444 /* error indication */
445 {SBIT(GSM411_RPS_WAIT_TO_TX_RP_ACK),
446 GSM411_MNSMS_ERROR_IND,
447 "MNSMS-ERROR-IND", gsm411_mnsms_error_ind_rx},
448
449};
450
451#define SMRDATASLLEN \
452 (sizeof(smrdatastatelist) / sizeof(struct smrdatastate))
453
454/* message from lower layer
455 * WARNING: We must not free msg, since it will be performed by the
456 * lower layer. */
457int gsm411_smr_recv(struct gsm411_smr_inst *inst, int msg_type,
458 struct msgb *msg)
459{
460 int i, rc;
461
462 /* find function for current state and message */
463 for (i = 0; i < SMRDATASLLEN; i++) {
Holger Hans Peter Freyther1c4c3732012-11-22 00:33:52 +0100464 /* state must match, MM message must match
Andreas Eversbergc1a91a82011-10-28 11:05:37 +0200465 * CP msg must match only in case of MMSMS_DATA_IND
466 */
467 if ((msg_type == smrdatastatelist[i].type)
468 && (SBIT(inst->rp_state) & smrdatastatelist[i].states))
469 break;
470 }
471 if (i == SMRDATASLLEN) {
Holger Hans Peter Freytherbcf125c2012-11-14 06:07:47 +0100472 LOGP(DLSMS, LOGL_NOTICE,
473 SMR_LOG_STR "message %u unhandled at this state "
474 "%s.\n", inst->id, msg_type,
475 smr_state_names[inst->rp_state]);
Andreas Eversbergc1a91a82011-10-28 11:05:37 +0200476 return 0;
477 }
478
Holger Hans Peter Freytherbcf125c2012-11-14 06:07:47 +0100479 LOGP(DLSMS, LOGL_INFO,
480 SMR_LOG_STR "message %s received in state %s\n", inst->id,
Andreas Eversbergc1a91a82011-10-28 11:05:37 +0200481 smrdatastatelist[i].name, smr_state_names[inst->rp_state]);
482
483 rc = smrdatastatelist[i].rout(inst, msg);
484
485 return rc;
486}