blob: fb86929326dd3e155f970588ab6bb6cd05ad60c0 [file] [log] [blame]
Daniel Willmann8b3390e2008-12-28 00:31:09 +00001/* 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 *
7 * All Rights Reserved
8 *
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
13 *
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
18 *
19 * You should have received a copy of the GNU General Public License along
20 * with this program; if not, write to the Free Software Foundation, Inc.,
21 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
22 *
23 */
24
25
26#include <stdio.h>
27#include <stdlib.h>
28#include <string.h>
29#include <errno.h>
30#include <netinet/in.h>
31
32#include <openbsc/msgb.h>
33#include <openbsc/debug.h>
Daniel Willmann471712b2008-12-29 01:54:02 +000034#include <openbsc/gsm_data.h>
35#include <openbsc/gsm_subscriber.h>
Daniel Willmann8b3390e2008-12-28 00:31:09 +000036#include <openbsc/gsm_04_11.h>
37#include <openbsc/gsm_04_08.h>
38#include <openbsc/abis_rsl.h>
39
Daniel Willmann471712b2008-12-29 01:54:02 +000040#define GSM411_ALLOC_SIZE 1024
41#define GSM411_ALLOC_HEADROOM 128
42
43static struct msgb *gsm411_msgb_alloc(void)
44{
45 return msgb_alloc_headroom(GSM411_ALLOC_SIZE, GSM411_ALLOC_HEADROOM);
46}
47
48static int gsm0411_sendmsg(struct msgb *msg)
49{
50 if (msg->lchan)
51 msg->trx = msg->lchan->ts->trx;
52
53 msg->l3h = msg->data;
54
55 return rsl_data_request(msg, 0);
56}
57
Daniel Willmanne0fbec82008-12-29 00:44:41 +000058static char *gsm411_7bit_decode(u_int8_t *user_data, u_int8_t length)
59{
60 u_int8_t d_off = 0, b_off = 0;
61 u_int8_t i;
62 char *text = malloc(length+1);
63
64 for (i=0;i<length;i++) {
65 text[i] = ((user_data[d_off] + (user_data[d_off+1]<<8)) & (0x7f<<b_off))>>b_off;
66 b_off += 7;
67 if (b_off >= 8) {
68 d_off += 1;
69 b_off -= 8;
70 }
71 }
72 text[i] = 0;
73 return text;
74}
75
76static int gsm411_sms_submit_from_msgb(struct msgb *msg)
77{
78 u_int8_t *smsp = msgb_sms(msg);
79 struct sms_submit *sms;
80
81 sms = malloc(sizeof(*sms));
82 sms->mti = *smsp & 0x03;
83 sms->mms = !!(*smsp & 0x04);
84 sms->vpf = (*smsp & 0x18) >> 3;
85 sms->sri = !!(*smsp & 0x20);
86 sms->udhi= !!(*smsp & 0x40);
87 sms->rp = !!(*smsp & 0x80);
88
89 smsp++;
90 sms->msg_ref = *smsp++;
91
92 /* Skip destination address for now */
93 smsp += 2 + *smsp/2 + *smsp%2;
94
95 sms->pid = *smsp++;
96 sms->dcs = *smsp++;
97 switch (sms->vpf)
98 {
99 case 2: /* relative */
100 sms->vp = *smsp++;
101 break;
102 default:
103 DEBUGP(DSMS, "SMS Validity period not implemented: 0x%02x\n",
104 sms->vpf);
105 }
106 sms->ud_len = *smsp++;
107
108 sms->user_data = gsm411_7bit_decode(smsp, sms->ud_len);
109
110 DEBUGP(DSMS, "SMS:\nMTI: 0x%02x, VPF: 0x%02x, MR: 0x%02x\n"
111 "PID: 0x%02x, DCS: 0x%02x, UserDataLength: 0x%02x\n"
112 "UserData: \"%s\"\n", sms->mti, sms->vpf, sms->msg_ref,
113 sms->pid, sms->dcs, sms->ud_len, sms->user_data);
114
115 free(sms);
116
117 return 0;
118}
119
Daniel Willmann9dfbf252008-12-29 03:24:29 +0000120static int gsm411_send_rp_ack(struct gsm_lchan *lchan, u_int8_t trans_id,
121 u_int8_t msg_ref)
Daniel Willmann471712b2008-12-29 01:54:02 +0000122{
123 struct msgb *msg = gsm411_msgb_alloc();
124 struct gsm48_hdr *gh;
125 struct gsm411_rp_hdr *rp;
126
127 msg->lchan = lchan;
128
129 gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
Daniel Willmannaecbbed2008-12-29 03:45:50 +0000130 // Outgoing needs the highest bit set
131 gh->proto_discr = GSM48_PDISC_SMS | trans_id<<4 | 0x80;
132 gh->msg_type = GSM411_MT_CP_DATA;
Daniel Willmann471712b2008-12-29 01:54:02 +0000133
134 rp = (struct gsm411_rp_hdr *)msgb_put(msg, sizeof(*rp));
Daniel Willmannaecbbed2008-12-29 03:45:50 +0000135 rp->len = 2;
Daniel Willmann471712b2008-12-29 01:54:02 +0000136 rp->msg_type = GSM411_MT_RP_ACK_MT;
137 rp->msg_ref = msg_ref;
138
139 DEBUGP(DSMS, "TX: SMS RP ACK\n");
140
141 return gsm0411_sendmsg(msg);
142}
143
Daniel Willmann9dfbf252008-12-29 03:24:29 +0000144static int gsm411_send_rp_error(struct gsm_lchan *lchan, u_int8_t trans_id,
145 u_int8_t msg_ref)
Daniel Willmann471712b2008-12-29 01:54:02 +0000146{
147 struct msgb *msg = gsm411_msgb_alloc();
148 struct gsm48_hdr *gh;
149 struct gsm411_rp_hdr *rp;
150
151 msg->lchan = lchan;
152
153 gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
Daniel Willmannaecbbed2008-12-29 03:45:50 +0000154 // Outgoing needs the highest bit set
155 gh->proto_discr = GSM48_PDISC_SMS | trans_id<<4 | 0x80;
156 gh->msg_type = GSM411_MT_CP_DATA;
Daniel Willmann471712b2008-12-29 01:54:02 +0000157
158 rp = (struct gsm411_rp_hdr *)msgb_put(msg, sizeof(*rp));
159 rp->msg_type = GSM411_MT_RP_ERROR_MT;
160 rp->msg_ref = msg_ref;
161
162 DEBUGP(DSMS, "TX: SMS RP ERROR\n");
163
164 return gsm0411_sendmsg(msg);
165}
166
Daniel Willmann8b3390e2008-12-28 00:31:09 +0000167static int gsm411_cp_data(struct msgb *msg)
168{
169 struct gsm48_hdr *gh = msgb_l3(msg);
170 int rc = 0;
171
Daniel Willmann471712b2008-12-29 01:54:02 +0000172 struct gsm411_rp_hdr *rp_data = (struct gsm411_rp_hdr*)&gh->data;
Daniel Willmann8b3390e2008-12-28 00:31:09 +0000173 u_int8_t msg_type = rp_data->msg_type & 0x07;
174
175 switch (msg_type) {
176 case GSM411_MT_RP_DATA_MO:
Daniel Willmanne0fbec82008-12-29 00:44:41 +0000177 DEBUGP(DSMS, "SMS RP-DATA (MO)\n");
178 /* Skip SMSC no and RP-UD length */
179 msg->smsh = &rp_data->data[1] + rp_data->data[1] + 2;
180 gsm411_sms_submit_from_msgb(msg);
Daniel Willmann9dfbf252008-12-29 03:24:29 +0000181 gsm411_send_rp_ack(msg->lchan, (gh->proto_discr & 0xf0)>>4, rp_data->msg_ref);
Daniel Willmann8b3390e2008-12-28 00:31:09 +0000182 break;
183 default:
184 DEBUGP(DSMS, "Unimplemented RP type 0x%02x\n", msg_type);
185 break;
186 }
187
188 return rc;
189}
190
191int gsm0411_rcv_sms(struct msgb *msg)
192{
193 struct gsm48_hdr *gh = msgb_l3(msg);
194 u_int8_t msg_type = gh->msg_type;
195 int rc = 0;
196
197 DEBUGP(DSMS, "SMS Message\n");
198
199 switch(msg_type) {
200 case GSM411_MT_CP_DATA:
201 DEBUGP(DSMS, "SMS CP-DATA\n");
202 rc = gsm411_cp_data(msg);
203 break;
204 case GSM411_MT_CP_ACK:
Daniel Willmannbb16e8e2008-12-29 03:53:50 +0000205 DEBUGP(DSMS, "SMS CP-ACK\n");
206 break;
Daniel Willmann8b3390e2008-12-28 00:31:09 +0000207 case GSM411_MT_CP_ERROR:
Daniel Willmannbb16e8e2008-12-29 03:53:50 +0000208 DEBUGP(DSMS, "SMS CP-ERROR, cause 0x%02x\n", gh->data[0]);
209 break;
Daniel Willmann8b3390e2008-12-28 00:31:09 +0000210 default:
211 DEBUGP(DSMS, "Unimplemented CP msg_type: 0x%02x\n", msg_type);
212 break;
213 }
214
215
216 return rc;
217}
218