blob: a409a59dbd1f9b143eac465fda1720b65e41b1d3 [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 Willmann9dfbf252008-12-29 03:24:29 +0000130 gh->proto_discr = GSM48_PDISC_SMS | trans_id<<4;
Daniel Willmann471712b2008-12-29 01:54:02 +0000131 gh->msg_type = GSM411_MT_CP_ACK;
132
133 rp = (struct gsm411_rp_hdr *)msgb_put(msg, sizeof(*rp));
134 rp->msg_type = GSM411_MT_RP_ACK_MT;
135 rp->msg_ref = msg_ref;
136
137 DEBUGP(DSMS, "TX: SMS RP ACK\n");
138
139 return gsm0411_sendmsg(msg);
140}
141
Daniel Willmann9dfbf252008-12-29 03:24:29 +0000142static int gsm411_send_rp_error(struct gsm_lchan *lchan, u_int8_t trans_id,
143 u_int8_t msg_ref)
Daniel Willmann471712b2008-12-29 01:54:02 +0000144{
145 struct msgb *msg = gsm411_msgb_alloc();
146 struct gsm48_hdr *gh;
147 struct gsm411_rp_hdr *rp;
148
149 msg->lchan = lchan;
150
151 gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
Daniel Willmann9dfbf252008-12-29 03:24:29 +0000152 gh->proto_discr = GSM48_PDISC_SMS | trans_id<<4;
Daniel Willmann471712b2008-12-29 01:54:02 +0000153 gh->msg_type = GSM411_MT_CP_ERROR;
154
155 rp = (struct gsm411_rp_hdr *)msgb_put(msg, sizeof(*rp));
156 rp->msg_type = GSM411_MT_RP_ERROR_MT;
157 rp->msg_ref = msg_ref;
158
159 DEBUGP(DSMS, "TX: SMS RP ERROR\n");
160
161 return gsm0411_sendmsg(msg);
162}
163
Daniel Willmann8b3390e2008-12-28 00:31:09 +0000164static int gsm411_cp_data(struct msgb *msg)
165{
166 struct gsm48_hdr *gh = msgb_l3(msg);
167 int rc = 0;
168
Daniel Willmann471712b2008-12-29 01:54:02 +0000169 struct gsm411_rp_hdr *rp_data = (struct gsm411_rp_hdr*)&gh->data;
Daniel Willmann8b3390e2008-12-28 00:31:09 +0000170 u_int8_t msg_type = rp_data->msg_type & 0x07;
171
172 switch (msg_type) {
173 case GSM411_MT_RP_DATA_MO:
Daniel Willmanne0fbec82008-12-29 00:44:41 +0000174 DEBUGP(DSMS, "SMS RP-DATA (MO)\n");
175 /* Skip SMSC no and RP-UD length */
176 msg->smsh = &rp_data->data[1] + rp_data->data[1] + 2;
177 gsm411_sms_submit_from_msgb(msg);
Daniel Willmann9dfbf252008-12-29 03:24:29 +0000178 gsm411_send_rp_ack(msg->lchan, (gh->proto_discr & 0xf0)>>4, rp_data->msg_ref);
Daniel Willmann8b3390e2008-12-28 00:31:09 +0000179 break;
180 default:
181 DEBUGP(DSMS, "Unimplemented RP type 0x%02x\n", msg_type);
182 break;
183 }
184
185 return rc;
186}
187
188int gsm0411_rcv_sms(struct msgb *msg)
189{
190 struct gsm48_hdr *gh = msgb_l3(msg);
191 u_int8_t msg_type = gh->msg_type;
192 int rc = 0;
193
194 DEBUGP(DSMS, "SMS Message\n");
195
196 switch(msg_type) {
197 case GSM411_MT_CP_DATA:
198 DEBUGP(DSMS, "SMS CP-DATA\n");
199 rc = gsm411_cp_data(msg);
200 break;
201 case GSM411_MT_CP_ACK:
202 case GSM411_MT_CP_ERROR:
203 default:
204 DEBUGP(DSMS, "Unimplemented CP msg_type: 0x%02x\n", msg_type);
205 break;
206 }
207
208
209 return rc;
210}
211