blob: 8a0530c7e13f3a7db3218fbb677a668e5d8e6d9f [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 Willmann471712b2008-12-29 01:54:02 +0000120static int gsm411_send_rp_ack(struct gsm_lchan *lchan, u_int8_t msg_ref)
121{
122 struct msgb *msg = gsm411_msgb_alloc();
123 struct gsm48_hdr *gh;
124 struct gsm411_rp_hdr *rp;
125
126 msg->lchan = lchan;
127
128 gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
129 gh->proto_discr = GSM48_PDISC_SMS;
130 gh->msg_type = GSM411_MT_CP_ACK;
131
132 rp = (struct gsm411_rp_hdr *)msgb_put(msg, sizeof(*rp));
133 rp->msg_type = GSM411_MT_RP_ACK_MT;
134 rp->msg_ref = msg_ref;
135
136 DEBUGP(DSMS, "TX: SMS RP ACK\n");
137
138 return gsm0411_sendmsg(msg);
139}
140
141static int gsm411_send_rp_error(struct gsm_lchan *lchan, u_int8_t msg_ref)
142{
143 struct msgb *msg = gsm411_msgb_alloc();
144 struct gsm48_hdr *gh;
145 struct gsm411_rp_hdr *rp;
146
147 msg->lchan = lchan;
148
149 gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
150 gh->proto_discr = GSM48_PDISC_SMS;
151 gh->msg_type = GSM411_MT_CP_ERROR;
152
153 rp = (struct gsm411_rp_hdr *)msgb_put(msg, sizeof(*rp));
154 rp->msg_type = GSM411_MT_RP_ERROR_MT;
155 rp->msg_ref = msg_ref;
156
157 DEBUGP(DSMS, "TX: SMS RP ERROR\n");
158
159 return gsm0411_sendmsg(msg);
160}
161
Daniel Willmann8b3390e2008-12-28 00:31:09 +0000162static int gsm411_cp_data(struct msgb *msg)
163{
164 struct gsm48_hdr *gh = msgb_l3(msg);
165 int rc = 0;
166
Daniel Willmann471712b2008-12-29 01:54:02 +0000167 struct gsm411_rp_hdr *rp_data = (struct gsm411_rp_hdr*)&gh->data;
Daniel Willmann8b3390e2008-12-28 00:31:09 +0000168 u_int8_t msg_type = rp_data->msg_type & 0x07;
169
170 switch (msg_type) {
171 case GSM411_MT_RP_DATA_MO:
Daniel Willmanne0fbec82008-12-29 00:44:41 +0000172 DEBUGP(DSMS, "SMS RP-DATA (MO)\n");
173 /* Skip SMSC no and RP-UD length */
174 msg->smsh = &rp_data->data[1] + rp_data->data[1] + 2;
175 gsm411_sms_submit_from_msgb(msg);
Daniel Willmann471712b2008-12-29 01:54:02 +0000176 gsm411_send_rp_ack(msg->lchan, rp_data->msg_ref);
Daniel Willmann8b3390e2008-12-28 00:31:09 +0000177 break;
178 default:
179 DEBUGP(DSMS, "Unimplemented RP type 0x%02x\n", msg_type);
180 break;
181 }
182
183 return rc;
184}
185
186int gsm0411_rcv_sms(struct msgb *msg)
187{
188 struct gsm48_hdr *gh = msgb_l3(msg);
189 u_int8_t msg_type = gh->msg_type;
190 int rc = 0;
191
192 DEBUGP(DSMS, "SMS Message\n");
193
194 switch(msg_type) {
195 case GSM411_MT_CP_DATA:
196 DEBUGP(DSMS, "SMS CP-DATA\n");
197 rc = gsm411_cp_data(msg);
198 break;
199 case GSM411_MT_CP_ACK:
200 case GSM411_MT_CP_ERROR:
201 default:
202 DEBUGP(DSMS, "Unimplemented CP msg_type: 0x%02x\n", msg_type);
203 break;
204 }
205
206
207 return rc;
208}
209