blob: c4ac286a2af601c31f4f62e12beaf7f08ac41231 [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>
34#include <openbsc/gsm_04_11.h>
35#include <openbsc/gsm_04_08.h>
36#include <openbsc/abis_rsl.h>
37
Daniel Willmanne0fbec82008-12-29 00:44:41 +000038static char *gsm411_7bit_decode(u_int8_t *user_data, u_int8_t length)
39{
40 u_int8_t d_off = 0, b_off = 0;
41 u_int8_t i;
42 char *text = malloc(length+1);
43
44 for (i=0;i<length;i++) {
45 text[i] = ((user_data[d_off] + (user_data[d_off+1]<<8)) & (0x7f<<b_off))>>b_off;
46 b_off += 7;
47 if (b_off >= 8) {
48 d_off += 1;
49 b_off -= 8;
50 }
51 }
52 text[i] = 0;
53 return text;
54}
55
56static int gsm411_sms_submit_from_msgb(struct msgb *msg)
57{
58 u_int8_t *smsp = msgb_sms(msg);
59 struct sms_submit *sms;
60
61 sms = malloc(sizeof(*sms));
62 sms->mti = *smsp & 0x03;
63 sms->mms = !!(*smsp & 0x04);
64 sms->vpf = (*smsp & 0x18) >> 3;
65 sms->sri = !!(*smsp & 0x20);
66 sms->udhi= !!(*smsp & 0x40);
67 sms->rp = !!(*smsp & 0x80);
68
69 smsp++;
70 sms->msg_ref = *smsp++;
71
72 /* Skip destination address for now */
73 smsp += 2 + *smsp/2 + *smsp%2;
74
75 sms->pid = *smsp++;
76 sms->dcs = *smsp++;
77 switch (sms->vpf)
78 {
79 case 2: /* relative */
80 sms->vp = *smsp++;
81 break;
82 default:
83 DEBUGP(DSMS, "SMS Validity period not implemented: 0x%02x\n",
84 sms->vpf);
85 }
86 sms->ud_len = *smsp++;
87
88 sms->user_data = gsm411_7bit_decode(smsp, sms->ud_len);
89
90 DEBUGP(DSMS, "SMS:\nMTI: 0x%02x, VPF: 0x%02x, MR: 0x%02x\n"
91 "PID: 0x%02x, DCS: 0x%02x, UserDataLength: 0x%02x\n"
92 "UserData: \"%s\"\n", sms->mti, sms->vpf, sms->msg_ref,
93 sms->pid, sms->dcs, sms->ud_len, sms->user_data);
94
95 free(sms);
96
97 return 0;
98}
99
Daniel Willmann8b3390e2008-12-28 00:31:09 +0000100static int gsm411_cp_data(struct msgb *msg)
101{
102 struct gsm48_hdr *gh = msgb_l3(msg);
103 int rc = 0;
104
Daniel Willmannfdd0a6c2008-12-28 01:51:14 +0000105 struct gsm411_rp_data_hdr *rp_data = (struct gsm411_rp_data_hdr*)&gh->data;
Daniel Willmann8b3390e2008-12-28 00:31:09 +0000106 u_int8_t msg_type = rp_data->msg_type & 0x07;
107
108 switch (msg_type) {
109 case GSM411_MT_RP_DATA_MO:
Daniel Willmanne0fbec82008-12-29 00:44:41 +0000110 DEBUGP(DSMS, "SMS RP-DATA (MO)\n");
111 /* Skip SMSC no and RP-UD length */
112 msg->smsh = &rp_data->data[1] + rp_data->data[1] + 2;
113 gsm411_sms_submit_from_msgb(msg);
Daniel Willmann8b3390e2008-12-28 00:31:09 +0000114 break;
115 default:
116 DEBUGP(DSMS, "Unimplemented RP type 0x%02x\n", msg_type);
117 break;
118 }
119
120 return rc;
121}
122
123int gsm0411_rcv_sms(struct msgb *msg)
124{
125 struct gsm48_hdr *gh = msgb_l3(msg);
126 u_int8_t msg_type = gh->msg_type;
127 int rc = 0;
128
129 DEBUGP(DSMS, "SMS Message\n");
130
131 switch(msg_type) {
132 case GSM411_MT_CP_DATA:
133 DEBUGP(DSMS, "SMS CP-DATA\n");
134 rc = gsm411_cp_data(msg);
135 break;
136 case GSM411_MT_CP_ACK:
137 case GSM411_MT_CP_ERROR:
138 default:
139 DEBUGP(DSMS, "Unimplemented CP msg_type: 0x%02x\n", msg_type);
140 break;
141 }
142
143
144 return rc;
145}
146