blob: d120f2df4542642bbfe940b2645f0f1ccdd76d4d [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
Harald Welte96e2a002017-06-12 21:44:18 +020065/*! \addtogroup sms
66 * @{
67 */
68
Andreas Eversbergc1a91a82011-10-28 11:05:37 +020069static void rp_timer_expired(void *data);
70
71/* init a new instance */
Holger Hans Peter Freytherbcf125c2012-11-14 06:07:47 +010072void gsm411_smr_init(struct gsm411_smr_inst *inst, uint64_t id, int network,
Andreas Eversbergc1a91a82011-10-28 11:05:37 +020073 int (*rl_recv) (struct gsm411_smr_inst *inst, int msg_type,
74 struct msgb *msg),
75 int (*mn_send) (struct gsm411_smr_inst *inst, int msg_type,
76 struct msgb *msg))
77{
78 memset(inst, 0, sizeof(*inst));
Holger Hans Peter Freytherbcf125c2012-11-14 06:07:47 +010079 inst->id = id;
Andreas Eversbergc1a91a82011-10-28 11:05:37 +020080 inst->network = network;
81 inst->rp_state = GSM411_RPS_IDLE;
82 inst->rl_recv = rl_recv;
83 inst->mn_send = mn_send;
Pablo Neira Ayuso44f423f2017-05-08 18:00:28 +020084 osmo_timer_setup(&inst->rp_timer, rp_timer_expired, inst);
Andreas Eversbergc1a91a82011-10-28 11:05:37 +020085
Holger Hans Peter Freytherbcf125c2012-11-14 06:07:47 +010086 LOGP(DLSMS, LOGL_INFO,
Holger Hans Peter Freyther68f94472012-12-01 12:51:34 +010087 SMR_LOG_STR "instance created for %s.\n",
88 inst->id, inst->network ? "network" : "mobile");
Andreas Eversbergc1a91a82011-10-28 11:05:37 +020089}
90
91/* clear instance */
92void gsm411_smr_clear(struct gsm411_smr_inst *inst)
93{
Holger Hans Peter Freytherbcf125c2012-11-14 06:07:47 +010094 LOGP(DLSMS, LOGL_INFO,
95 SMR_LOG_STR "clearing SMR instance\n", inst->id);
Andreas Eversbergc1a91a82011-10-28 11:05:37 +020096
97 osmo_timer_del(&inst->rp_timer);
98}
99
Holger Hans Peter Freyther61d33922014-05-23 08:49:34 +0200100static const char *smr_state_names[] = {
Andreas Eversbergc1a91a82011-10-28 11:05:37 +0200101 "IDLE",
102 "WAIT_FOR_RP_ACK",
Holger Hans Peter Freytherda73aa62014-05-23 08:51:22 +0200103 "illegal state 2",
Andreas Eversbergc1a91a82011-10-28 11:05:37 +0200104 "WAIT_TO_TX_RP_ACK",
105 "WAIT_FOR_RETRANS_T",
106};
107
108const struct value_string gsm411_rp_cause_strs[] = {
109 { GSM411_RP_CAUSE_MO_NUM_UNASSIGNED, "(MO) Number not assigned" },
110 { GSM411_RP_CAUSE_MO_OP_DET_BARR, "(MO) Operator determined barring" },
111 { GSM411_RP_CAUSE_MO_CALL_BARRED, "(MO) Call barred" },
112 { GSM411_RP_CAUSE_MO_SMS_REJECTED, "(MO) SMS rejected" },
113 { GSM411_RP_CAUSE_MO_DEST_OUT_OF_ORDER, "(MO) Destination out of order" },
114 { GSM411_RP_CAUSE_MO_UNIDENTIFIED_SUBSCR, "(MO) Unidentified subscriber" },
115 { GSM411_RP_CAUSE_MO_FACILITY_REJ, "(MO) Facility reject" },
116 { GSM411_RP_CAUSE_MO_UNKNOWN_SUBSCR, "(MO) Unknown subscriber" },
117 { GSM411_RP_CAUSE_MO_NET_OUT_OF_ORDER, "(MO) Network out of order" },
118 { GSM411_RP_CAUSE_MO_TEMP_FAIL, "(MO) Temporary failure" },
119 { GSM411_RP_CAUSE_MO_CONGESTION, "(MO) Congestion" },
120 { GSM411_RP_CAUSE_MO_RES_UNAVAIL, "(MO) Resource unavailable" },
121 { GSM411_RP_CAUSE_MO_REQ_FAC_NOTSUBSCR, "(MO) Requested facility not subscribed" },
122 { GSM411_RP_CAUSE_MO_REQ_FAC_NOTIMPL, "(MO) Requested facility not implemented" },
123 { GSM411_RP_CAUSE_MO_INTERWORKING, "(MO) Interworking" },
124 /* valid only for MT */
125 { GSM411_RP_CAUSE_MT_MEM_EXCEEDED, "(MT) Memory Exceeded" },
126 /* valid for both directions */
127 { GSM411_RP_CAUSE_INV_TRANS_REF, "Invalid Transaction Reference" },
128 { GSM411_RP_CAUSE_SEMANT_INC_MSG, "Semantically Incorrect Message" },
129 { GSM411_RP_CAUSE_INV_MAND_INF, "Invalid Mandatory Information" },
130 { GSM411_RP_CAUSE_MSGTYPE_NOTEXIST, "Message Type non-existant" },
131 { GSM411_RP_CAUSE_MSG_INCOMP_STATE, "Message incompatible with protocol state" },
132 { GSM411_RP_CAUSE_IE_NOTEXIST, "Information Element not existing" },
133 { GSM411_RP_CAUSE_PROTOCOL_ERR, "Protocol Error" },
134 { 0, NULL }
135};
136
137static void new_rp_state(struct gsm411_smr_inst *inst,
138 enum gsm411_rp_state state)
139{
Holger Hans Peter Freytherbcf125c2012-11-14 06:07:47 +0100140 LOGP(DLSMS, LOGL_INFO,
141 SMR_LOG_STR "new RP state %s -> %s\n", inst->id,
Andreas Eversbergc1a91a82011-10-28 11:05:37 +0200142 smr_state_names[inst->rp_state], smr_state_names[state]);
143 inst->rp_state = state;
144
145 /* stop timer when going idle */
146 if (state == GSM411_RPS_IDLE)
147 osmo_timer_del(&inst->rp_timer);
148}
149
150/* Prefix msg with a RP-DATA header and send as CP-DATA */
151static int gsm411_rp_sendmsg(struct gsm411_smr_inst *inst, struct msgb *msg,
152 uint8_t rp_msg_type, uint8_t rp_msg_ref,
153 int mnsms_msg_type)
154{
155 struct gsm411_rp_hdr *rp;
156 uint8_t len = msg->len;
157
158 /* GSM 04.11 RP-DATA header */
159 rp = (struct gsm411_rp_hdr *)msgb_push(msg, sizeof(*rp));
160 rp->len = len + 2;
161 rp->msg_type = rp_msg_type;
Holger Hans Peter Freytherf4f5a842014-02-08 15:14:53 +0100162 rp->msg_ref = rp_msg_ref;
Andreas Eversbergc1a91a82011-10-28 11:05:37 +0200163
164 return inst->mn_send(inst, mnsms_msg_type, msg);
165}
166
167static int gsm411_send_rp_error(struct gsm411_smr_inst *inst,
168 uint8_t msg_ref, uint8_t cause)
169{
170 struct msgb *msg = gsm411_msgb_alloc();
171
172 msgb_tv_put(msg, 1, cause);
173
Holger Hans Peter Freytherbcf125c2012-11-14 06:07:47 +0100174 LOGP(DLSMS, LOGL_NOTICE,
175 SMR_LOG_STR "TX: SMS RP ERROR, cause %d (%s)\n", inst->id,
176 cause, get_value_string(gsm411_rp_cause_strs, cause));
Andreas Eversbergc1a91a82011-10-28 11:05:37 +0200177
178 return gsm411_rp_sendmsg(inst, msg,
179 (inst->network) ? GSM411_MT_RP_ERROR_MT : GSM411_MT_RP_ERROR_MO,
180 msg_ref, GSM411_MNSMS_DATA_REQ);
181}
182
183static int gsm411_send_release(struct gsm411_smr_inst *inst)
184{
185 struct msgb *msg = gsm411_msgb_alloc();
186
Holger Hans Peter Freytherbcf125c2012-11-14 06:07:47 +0100187 LOGP(DLSMS, LOGL_DEBUG,
188 SMR_LOG_STR "TX: MNSMS-REL-REQ\n", inst->id);
Andreas Eversbergc1a91a82011-10-28 11:05:37 +0200189
190 return inst->mn_send(inst, GSM411_MNSMS_REL_REQ, msg);
191}
192
193static int gsm411_send_abort(struct gsm411_smr_inst *inst)
194{
195 struct msgb *msg = gsm411_msgb_alloc();
196
Holger Hans Peter Freytherbcf125c2012-11-14 06:07:47 +0100197 LOGP(DLSMS, LOGL_DEBUG,
198 SMR_LOG_STR "TX: MNSMS-ABORT-REQ\n", inst->id);
199
Andreas Eversbergc1a91a82011-10-28 11:05:37 +0200200 msgb_tv_put(msg, 1, 111); //FIXME: better idea ? */
Andreas Eversbergc1a91a82011-10-28 11:05:37 +0200201
202 return inst->mn_send(inst, GSM411_MNSMS_ABORT_REQ, msg);
203}
204
205static int gsm411_send_report(struct gsm411_smr_inst *inst)
206{
207 struct msgb *msg = gsm411_msgb_alloc();
208
Holger Hans Peter Freytherbcf125c2012-11-14 06:07:47 +0100209 LOGP(DLSMS, LOGL_DEBUG,
210 SMR_LOG_STR "Sending empty SM_RL_REPORT_IND\n", inst->id);
Andreas Eversbergc1a91a82011-10-28 11:05:37 +0200211
212 return inst->rl_recv(inst, GSM411_SM_RL_REPORT_IND, msg);
213}
214
215static int gsm411_rl_data_req(struct gsm411_smr_inst *inst, struct msgb *msg)
216{
Holger Hans Peter Freytherbcf125c2012-11-14 06:07:47 +0100217 LOGP(DLSMS, LOGL_DEBUG,
218 SMR_LOG_STR "TX SMS RP-DATA\n", inst->id);
Andreas Eversbergc1a91a82011-10-28 11:05:37 +0200219 /* start TR1N and enter 'wait for RP-ACK state' */
220 osmo_timer_schedule(&inst->rp_timer, GSM411_TMR_TR1M);
221 new_rp_state(inst, GSM411_RPS_WAIT_FOR_RP_ACK);
222
223 return inst->mn_send(inst, GSM411_MNSMS_EST_REQ, msg);
224}
225
226static int gsm411_rl_report_req(struct gsm411_smr_inst *inst, struct msgb *msg)
227{
Holger Hans Peter Freytherbcf125c2012-11-14 06:07:47 +0100228 LOGP(DLSMS, LOGL_DEBUG,
229 SMR_LOG_STR "TX SMS REPORT\n", inst->id);
Andreas Eversbergc1a91a82011-10-28 11:05:37 +0200230 new_rp_state(inst, GSM411_RPS_IDLE);
231
232 inst->mn_send(inst, GSM411_MNSMS_DATA_REQ, msg);
233 gsm411_send_release(inst);
234 return 0;
235}
236
237static int gsm411_mnsms_est_ind(struct gsm411_smr_inst *inst, struct msgb *msg)
238{
239 struct gsm48_hdr *gh = (struct gsm48_hdr*)msg->l3h;
240 struct gsm411_rp_hdr *rp_data = (struct gsm411_rp_hdr*)&gh->data;
241 uint8_t msg_type = rp_data->msg_type & 0x07;
242 int rc;
243
244 /* check direction */
245 if (inst->network == (msg_type & 1)) {
Holger Hans Peter Freytherbcf125c2012-11-14 06:07:47 +0100246 LOGP(DLSMS, LOGL_NOTICE,
247 SMR_LOG_STR "Invalid RP type 0x%02x\n",
248 inst->id, msg_type);
Andreas Eversbergc1a91a82011-10-28 11:05:37 +0200249 gsm411_send_rp_error(inst, rp_data->msg_ref,
250 GSM411_RP_CAUSE_MSG_INCOMP_STATE);
251 new_rp_state(inst, GSM411_RPS_IDLE);
252 gsm411_send_release(inst);
253 return -EINVAL;
254 }
255
256 switch (msg_type) {
257 case GSM411_MT_RP_DATA_MT:
258 case GSM411_MT_RP_DATA_MO:
Holger Hans Peter Freytherbcf125c2012-11-14 06:07:47 +0100259 LOGP(DLSMS, LOGL_DEBUG,
260 SMR_LOG_STR "RX SMS RP-DATA\n", inst->id);
Andreas Eversbergc1a91a82011-10-28 11:05:37 +0200261 /* start TR2N and enter 'wait to send RP-ACK state' */
262 osmo_timer_schedule(&inst->rp_timer, GSM411_TMR_TR2M);
263 new_rp_state(inst, GSM411_RPS_WAIT_TO_TX_RP_ACK);
264 rc = inst->rl_recv(inst, GSM411_SM_RL_DATA_IND, msg);
265 break;
266 case GSM411_MT_RP_SMMA_MO:
Holger Hans Peter Freytherbcf125c2012-11-14 06:07:47 +0100267 LOGP(DLSMS, LOGL_DEBUG,
268 SMR_LOG_STR "RX SMS RP-SMMA\n", inst->id);
Andreas Eversbergc1a91a82011-10-28 11:05:37 +0200269 /* start TR2N and enter 'wait to send RP-ACK state' */
270 osmo_timer_schedule(&inst->rp_timer, GSM411_TMR_TR2M);
271 new_rp_state(inst, GSM411_RPS_WAIT_TO_TX_RP_ACK);
272 rc = inst->rl_recv(inst, GSM411_SM_RL_DATA_IND, msg);
273 break;
274 default:
Holger Hans Peter Freytherbcf125c2012-11-14 06:07:47 +0100275 LOGP(DLSMS, LOGL_NOTICE,
276 SMR_LOG_STR "invalid RP type 0x%02x\n",
277 inst->id, msg_type);
Andreas Eversbergc1a91a82011-10-28 11:05:37 +0200278 gsm411_send_rp_error(inst, rp_data->msg_ref,
279 GSM411_RP_CAUSE_MSGTYPE_NOTEXIST);
280 new_rp_state(inst, GSM411_RPS_IDLE);
281 rc = -EINVAL;
282 break;
283 }
284
285 return rc;
286}
287
288static int gsm411_mnsms_data_ind_tx(struct gsm411_smr_inst *inst,
289 struct msgb *msg)
290{
291 struct gsm48_hdr *gh = (struct gsm48_hdr*)msg->l3h;
292 struct gsm411_rp_hdr *rp_data = (struct gsm411_rp_hdr*)&gh->data;
293 uint8_t msg_type = rp_data->msg_type & 0x07;
Andreas Eversbergc1a91a82011-10-28 11:05:37 +0200294
295 /* check direction */
296 if (inst->network == (msg_type & 1)) {
Holger Hans Peter Freytherbcf125c2012-11-14 06:07:47 +0100297 LOGP(DLSMS, LOGL_NOTICE,
298 SMR_LOG_STR "invalid RP type 0x%02x\n",
299 inst->id, msg_type);
Andreas Eversbergc1a91a82011-10-28 11:05:37 +0200300 gsm411_send_rp_error(inst, rp_data->msg_ref,
301 GSM411_RP_CAUSE_MSG_INCOMP_STATE);
302 new_rp_state(inst, GSM411_RPS_IDLE);
303 gsm411_send_release(inst);
304 return -EINVAL;
305 }
306
307 switch (msg_type) {
308 case GSM411_MT_RP_ACK_MO:
309 case GSM411_MT_RP_ACK_MT:
Holger Hans Peter Freytherbcf125c2012-11-14 06:07:47 +0100310 LOGP(DLSMS, LOGL_DEBUG,
311 SMR_LOG_STR "RX SMS RP-ACK\n", inst->id);
Andreas Eversbergc1a91a82011-10-28 11:05:37 +0200312 new_rp_state(inst, GSM411_RPS_IDLE);
313 inst->rl_recv(inst, GSM411_SM_RL_REPORT_IND, msg);
314 gsm411_send_release(inst);
315 return 0;
316 case GSM411_MT_RP_ERROR_MO:
317 case GSM411_MT_RP_ERROR_MT:
Holger Hans Peter Freytherbcf125c2012-11-14 06:07:47 +0100318 LOGP(DLSMS, LOGL_DEBUG,
319 SMR_LOG_STR "RX SMS RP-ERROR\n", inst->id);
Andreas Eversbergc1a91a82011-10-28 11:05:37 +0200320 new_rp_state(inst, GSM411_RPS_IDLE);
321 inst->rl_recv(inst, GSM411_SM_RL_REPORT_IND, msg);
322 gsm411_send_release(inst);
323 return 0;
324 default:
Holger Hans Peter Freytherbcf125c2012-11-14 06:07:47 +0100325 LOGP(DLSMS, LOGL_NOTICE,
326 SMR_LOG_STR "Invalid RP type 0x%02x\n",
327 inst->id, msg_type);
Andreas Eversbergc1a91a82011-10-28 11:05:37 +0200328 gsm411_send_rp_error(inst, rp_data->msg_ref,
329 GSM411_RP_CAUSE_MSGTYPE_NOTEXIST);
330 new_rp_state(inst, GSM411_RPS_IDLE);
331 gsm411_send_release(inst);
332 return -EINVAL;
333 }
334
Vadim Yanitskiy4c3e4ea2017-05-15 21:32:43 +0300335 return 0;
Andreas Eversbergc1a91a82011-10-28 11:05:37 +0200336}
337
338static int gsm411_mnsms_error_ind_tx(struct gsm411_smr_inst *inst,
339 struct msgb *msg)
340{
Holger Hans Peter Freytherbcf125c2012-11-14 06:07:47 +0100341 LOGP(DLSMS, LOGL_DEBUG,
Holger Hans Peter Freyther1c4c3732012-11-22 00:33:52 +0100342 SMR_LOG_STR "TX SMS MNSMS-ERROR-IND\n", inst->id);
Andreas Eversbergc1a91a82011-10-28 11:05:37 +0200343 new_rp_state(inst, GSM411_RPS_IDLE);
344 inst->rl_recv(inst, GSM411_SM_RL_REPORT_IND, msg);
345 gsm411_send_release(inst);
346 return 0;
347}
348
349static int gsm411_mnsms_error_ind_rx(struct gsm411_smr_inst *inst,
350 struct msgb *msg)
351{
Holger Hans Peter Freytherbcf125c2012-11-14 06:07:47 +0100352 LOGP(DLSMS, LOGL_DEBUG,
353 SMR_LOG_STR "RX SMS MNSMS-ERROR-IND\n", inst->id);
Andreas Eversbergc1a91a82011-10-28 11:05:37 +0200354 new_rp_state(inst, GSM411_RPS_IDLE);
355 return inst->rl_recv(inst, GSM411_SM_RL_REPORT_IND, msg);
356}
357
358/* SMR TR1* is expired */
359static void rp_timer_expired(void *data)
360{
361 struct gsm411_smr_inst *inst = data;
Holger Hans Peter Freytherbcf125c2012-11-14 06:07:47 +0100362 const char *str;
Andreas Eversbergc1a91a82011-10-28 11:05:37 +0200363
Holger Hans Peter Freytherbcf125c2012-11-14 06:07:47 +0100364 str = inst->rp_state == GSM411_RPS_WAIT_TO_TX_RP_ACK
365 ? "TR2N" : "TR1N";
366
367 LOGP(DLSMS, LOGL_DEBUG,
368 SMR_LOG_STR "%s expired\n", inst->id, str);
Andreas Eversbergc1a91a82011-10-28 11:05:37 +0200369 gsm411_send_report(inst);
370 gsm411_send_abort(inst);
371}
372
373/* statefull handling for SM-RL SAP messages */
Holger Hans Peter Freyther9473c5d2012-11-22 10:50:52 +0100374static const struct smrdownstate {
Andreas Eversbergc1a91a82011-10-28 11:05:37 +0200375 uint32_t states;
376 int type;
377 const char *name;
378 int (*rout) (struct gsm411_smr_inst *inst,
379 struct msgb *msg);
380} smrdownstatelist[] = {
381 /* data request */
382 {SBIT(GSM411_RPS_IDLE),
383 GSM411_SM_RL_DATA_REQ,
384 "SM-RL-DATA_REQ", gsm411_rl_data_req},
385
386 /* report request */
387 {SBIT(GSM411_RPS_WAIT_TO_TX_RP_ACK),
388 GSM411_SM_RL_REPORT_REQ,
389 "SM-RL-REPORT_REQ", gsm411_rl_report_req},
390};
391
392#define SMRDOWNSLLEN \
393 (sizeof(smrdownstatelist) / sizeof(struct smrdownstate))
394
395/* message from upper layer */
396int gsm411_smr_send(struct gsm411_smr_inst *inst, int msg_type,
397 struct msgb *msg)
398{
399 int i, rc;
400
401 /* find function for current state and message */
402 for (i = 0; i < SMRDOWNSLLEN; i++) {
403 if ((msg_type == smrdownstatelist[i].type)
404 && (SBIT(inst->rp_state) & smrdownstatelist[i].states))
405 break;
406 }
407 if (i == SMRDOWNSLLEN) {
Holger Hans Peter Freytherbcf125c2012-11-14 06:07:47 +0100408 LOGP(DLSMS, LOGL_NOTICE,
409 SMR_LOG_STR "message %u unhandled at this state "
410 "%s.\n", inst->id, msg_type,
411 smr_state_names[inst->rp_state]);
Andreas Eversbergc1a91a82011-10-28 11:05:37 +0200412 msgb_free(msg);
413 return 0;
414 }
415
Holger Hans Peter Freytherbcf125c2012-11-14 06:07:47 +0100416 LOGP(DLSMS, LOGL_INFO,
417 SMR_LOG_STR "message %s received in state %s\n", inst->id,
Andreas Eversbergc1a91a82011-10-28 11:05:37 +0200418 smrdownstatelist[i].name, smr_state_names[inst->rp_state]);
419
420 rc = smrdownstatelist[i].rout(inst, msg);
421
422 return rc;
423}
424
425/* statefull handling for MMSMS SAP messages */
Holger Hans Peter Freyther9473c5d2012-11-22 10:50:52 +0100426static const struct smrdatastate {
Andreas Eversbergc1a91a82011-10-28 11:05:37 +0200427 uint32_t states;
428 int type;
429 const char *name;
430 int (*rout) (struct gsm411_smr_inst *inst,
431 struct msgb *msg);
432} smrdatastatelist[] = {
433 /* establish indication */
434 {SBIT(GSM411_RPS_IDLE),
435 GSM411_MNSMS_EST_IND,
436 "MNSMS-EST-IND", gsm411_mnsms_est_ind},
437
438 /* data indication */
439 {SBIT(GSM411_RPS_WAIT_FOR_RP_ACK),
440 GSM411_MNSMS_DATA_IND,
441 "MNSMS-DATA-IND", gsm411_mnsms_data_ind_tx},
442
443 /* error indication */
444 {SBIT(GSM411_RPS_WAIT_FOR_RP_ACK),
445 GSM411_MNSMS_ERROR_IND,
446 "MNSMS-ERROR-IND", gsm411_mnsms_error_ind_tx},
447
448 /* error indication */
449 {SBIT(GSM411_RPS_WAIT_TO_TX_RP_ACK),
450 GSM411_MNSMS_ERROR_IND,
451 "MNSMS-ERROR-IND", gsm411_mnsms_error_ind_rx},
452
453};
454
455#define SMRDATASLLEN \
456 (sizeof(smrdatastatelist) / sizeof(struct smrdatastate))
457
458/* message from lower layer
459 * WARNING: We must not free msg, since it will be performed by the
460 * lower layer. */
461int gsm411_smr_recv(struct gsm411_smr_inst *inst, int msg_type,
462 struct msgb *msg)
463{
464 int i, rc;
465
466 /* find function for current state and message */
467 for (i = 0; i < SMRDATASLLEN; i++) {
Holger Hans Peter Freyther1c4c3732012-11-22 00:33:52 +0100468 /* state must match, MM message must match
Andreas Eversbergc1a91a82011-10-28 11:05:37 +0200469 * CP msg must match only in case of MMSMS_DATA_IND
470 */
471 if ((msg_type == smrdatastatelist[i].type)
472 && (SBIT(inst->rp_state) & smrdatastatelist[i].states))
473 break;
474 }
475 if (i == SMRDATASLLEN) {
Holger Hans Peter Freytherbcf125c2012-11-14 06:07:47 +0100476 LOGP(DLSMS, LOGL_NOTICE,
477 SMR_LOG_STR "message %u unhandled at this state "
478 "%s.\n", inst->id, msg_type,
479 smr_state_names[inst->rp_state]);
Andreas Eversbergc1a91a82011-10-28 11:05:37 +0200480 return 0;
481 }
482
Holger Hans Peter Freytherbcf125c2012-11-14 06:07:47 +0100483 LOGP(DLSMS, LOGL_INFO,
484 SMR_LOG_STR "message %s received in state %s\n", inst->id,
Andreas Eversbergc1a91a82011-10-28 11:05:37 +0200485 smrdatastatelist[i].name, smr_state_names[inst->rp_state]);
486
487 rc = smrdatastatelist[i].rout(inst, msg);
488
489 return rc;
490}
Harald Welte96e2a002017-06-12 21:44:18 +0200491
492/*! @} */