blob: 4c1a12a74c85d7ed82b847e7fb2d54d5b664701a [file] [log] [blame]
Holger Hans Peter Freyther55aea502010-09-30 18:30:41 +08001/* Format functions for GSM 04.80 */
2
3/*
4 * (C) 2010 by Holger Hans Peter Freyther <zecke@selfish.org>
5 * (C) 2009 by Mike Haben <michael.haben@btinternet.com>
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#include <osmocore/gsm0480.h>
26#include <osmocore/gsm_utils.h>
27
Holger Hans Peter Freyther00cb5702010-10-09 01:47:15 +080028#include <osmocore/logging.h>
29
30#include <osmocore/protocol/gsm_04_08.h>
Holger Hans Peter Freyther55aea502010-09-30 18:30:41 +080031#include <osmocore/protocol/gsm_04_80.h>
32
33#include <string.h>
34
35static inline unsigned char *msgb_wrap_with_TL(struct msgb *msgb, uint8_t tag)
36{
37 uint8_t *data = msgb_push(msgb, 2);
38
39 data[0] = tag;
40 data[1] = msgb->len - 2;
41 return data;
42}
43
44static inline unsigned char *msgb_push_TLV1(struct msgb *msgb, uint8_t tag,
45 uint8_t value)
46{
47 uint8_t *data = msgb_push(msgb, 3);
48
49 data[0] = tag;
50 data[1] = 1;
51 data[2] = value;
52 return data;
53}
54
55/* wrap an invoke around it... the other way around
56 *
57 * 1.) Invoke Component tag
58 * 2.) Invoke ID Tag
59 * 3.) Operation
60 * 4.) Data
61 */
62int gsm0480_wrap_invoke(struct msgb *msg, int op, int link_id)
63{
64 /* 3. operation */
65 msgb_push_TLV1(msg, GSM0480_OPERATION_CODE, op);
66
67 /* 2. invoke id tag */
68 msgb_push_TLV1(msg, GSM0480_COMPIDTAG_INVOKE_ID, link_id);
69
70 /* 1. component tag */
71 msgb_wrap_with_TL(msg, GSM0480_CTYPE_INVOKE);
72
73 return 0;
74}
75
76/* wrap the GSM 04.08 Facility IE around it */
77int gsm0480_wrap_facility(struct msgb *msg)
78{
79 msgb_wrap_with_TL(msg, GSM0480_IE_FACILITY);
80
81 return 0;
82}
83
84struct msgb *gsm0480_create_unstructuredSS_Notify(int alertPattern, const char *text)
85{
86 struct msgb *msg;
87 uint8_t *seq_len_ptr, *ussd_len_ptr, *data;
88 int len;
89
90 msg = msgb_alloc_headroom(1024, 128, "GSM 04.80");
91 if (!msg)
92 return NULL;
93
94 /* SEQUENCE { */
95 msgb_put_u8(msg, GSM_0480_SEQUENCE_TAG);
96 seq_len_ptr = msgb_put(msg, 1);
97
98 /* DCS { */
99 msgb_put_u8(msg, ASN1_OCTET_STRING_TAG);
100 msgb_put_u8(msg, 1);
101 msgb_put_u8(msg, 0x0F);
102 /* } DCS */
103
104 /* USSD-String { */
105 msgb_put_u8(msg, ASN1_OCTET_STRING_TAG);
106 ussd_len_ptr = msgb_put(msg, 1);
107 data = msgb_put(msg, 0);
108 len = gsm_7bit_encode(data, text);
109 msgb_put(msg, len);
110 ussd_len_ptr[0] = len;
111 /* USSD-String } */
112
113 /* alertingPattern { */
114 msgb_put_u8(msg, ASN1_OCTET_STRING_TAG);
115 msgb_put_u8(msg, 1);
116 msgb_put_u8(msg, alertPattern);
117 /* } alertingPattern */
118
119 seq_len_ptr[0] = 3 + 2 + ussd_len_ptr[0] + 3;
120 /* } SEQUENCE */
121
122 return msg;
123}
124
125struct msgb *gsm0480_create_notifySS(const char *text)
126{
127 struct msgb *msg;
128 uint8_t *data, *tmp_len;
129 uint8_t *seq_len_ptr, *cal_len_ptr, *opt_len_ptr, *nam_len_ptr;
130 int len;
131
132 len = strlen(text);
133 if (len < 1 || len > 160)
134 return NULL;
135
136 msg = msgb_alloc_headroom(1024, 128, "GSM 04.80");
137 if (!msg)
138 return NULL;
139
140 msgb_put_u8(msg, GSM_0480_SEQUENCE_TAG);
141 seq_len_ptr = msgb_put(msg, 1);
142
143 /* ss_code for CNAP { */
144 msgb_put_u8(msg, 0x81);
145 msgb_put_u8(msg, 1);
146 msgb_put_u8(msg, 0x19);
147 /* } ss_code */
148
149
150 /* nameIndicator { */
151 msgb_put_u8(msg, 0xB4);
152 nam_len_ptr = msgb_put(msg, 1);
153
154 /* callingName { */
155 msgb_put_u8(msg, 0xA0);
156 opt_len_ptr = msgb_put(msg, 1);
157 msgb_put_u8(msg, 0xA0);
158 cal_len_ptr = msgb_put(msg, 1);
159
160 /* namePresentationAllowed { */
161 /* add the DCS value */
162 msgb_put_u8(msg, 0x80);
163 msgb_put_u8(msg, 1);
164 msgb_put_u8(msg, 0x0F);
165
166 /* add the lengthInCharacters */
167 msgb_put_u8(msg, 0x81);
168 msgb_put_u8(msg, 1);
169 msgb_put_u8(msg, strlen(text));
170
171 /* add the actual string */
172 msgb_put_u8(msg, 0x82);
173 tmp_len = msgb_put(msg, 1);
174 data = msgb_put(msg, 0);
175 len = gsm_7bit_encode(data, text);
176 tmp_len[0] = len;
177 msgb_put(msg, len);
178
179 /* }; namePresentationAllowed */
180
181 cal_len_ptr[0] = 3 + 3 + 2 + len;
182 opt_len_ptr[0] = cal_len_ptr[0] + 2;
183 /* }; callingName */
184
185 nam_len_ptr[0] = opt_len_ptr[0] + 2;
186 /* ); nameIndicator */
187
188 /* write the lengths... */
189 seq_len_ptr[0] = 3 + nam_len_ptr[0] + 2;
190
191 return msg;
192}
193
Holger Hans Peter Freyther00cb5702010-10-09 01:47:15 +0800194/* Forward declarations */
Holger Hans Peter Freytherdaa653f2010-10-11 07:56:06 +0200195static int parse_ussd(const struct gsm48_hdr *hdr,
196 uint16_t len, struct ussd_request *req);
197static int parse_ussd_info_elements(const uint8_t *ussd_ie, uint16_t len,
Holger Hans Peter Freyther00cb5702010-10-09 01:47:15 +0800198 struct ussd_request *req);
Holger Hans Peter Freytherdaa653f2010-10-11 07:56:06 +0200199static int parse_facility_ie(const uint8_t *facility_ie, uint8_t length,
Holger Hans Peter Freyther00cb5702010-10-09 01:47:15 +0800200 struct ussd_request *req);
Holger Hans Peter Freytherdaa653f2010-10-11 07:56:06 +0200201static int parse_ss_invoke(const uint8_t *invoke_data, uint8_t length,
Holger Hans Peter Freyther00cb5702010-10-09 01:47:15 +0800202 struct ussd_request *req);
Holger Hans Peter Freytherdaa653f2010-10-11 07:56:06 +0200203static int parse_process_uss_req(const uint8_t *uss_req_data, uint8_t length,
Holger Hans Peter Freyther00cb5702010-10-09 01:47:15 +0800204 struct ussd_request *req);
205
206/* Decode a mobile-originated USSD-request message */
Holger Hans Peter Freytherdaa653f2010-10-11 07:56:06 +0200207int gsm0480_decode_ussd_request(const struct gsm48_hdr *hdr, uint16_t len,
208 struct ussd_request *req)
Holger Hans Peter Freyther00cb5702010-10-09 01:47:15 +0800209{
210 int rc = 0;
Holger Hans Peter Freyther00cb5702010-10-09 01:47:15 +0800211
Holger Hans Peter Freytherdaa653f2010-10-11 07:56:06 +0200212 if ((hdr->proto_discr & 0x0f) == GSM48_PDISC_NC_SS) {
213 req->transaction_id = hdr->proto_discr & 0x70;
214 rc = parse_ussd(hdr, len, req);
Holger Hans Peter Freyther00cb5702010-10-09 01:47:15 +0800215 }
216
217 if (!rc)
218 LOGP(0, LOGL_DEBUG, "Error occurred while parsing received USSD!\n");
219
220 return rc;
221}
222
Holger Hans Peter Freytherdaa653f2010-10-11 07:56:06 +0200223static int parse_ussd(const struct gsm48_hdr *hdr, uint16_t len, struct ussd_request *req)
Holger Hans Peter Freyther00cb5702010-10-09 01:47:15 +0800224{
225 int rc = 1;
Holger Hans Peter Freytherdaa653f2010-10-11 07:56:06 +0200226 uint8_t msg_type = hdr->msg_type & 0xBF; /* message-type - section 3.4 */
Holger Hans Peter Freyther00cb5702010-10-09 01:47:15 +0800227
228 switch (msg_type) {
229 case GSM0480_MTYPE_RELEASE_COMPLETE:
230 LOGP(0, LOGL_DEBUG, "USS Release Complete\n");
231 /* could also parse out the optional Cause/Facility data */
232 req->text[0] = 0xFF;
233 break;
234 case GSM0480_MTYPE_REGISTER:
235 case GSM0480_MTYPE_FACILITY:
Holger Hans Peter Freytherdaa653f2010-10-11 07:56:06 +0200236 rc &= parse_ussd_info_elements(&hdr->data[0], len - sizeof(*hdr), req);
Holger Hans Peter Freyther00cb5702010-10-09 01:47:15 +0800237 break;
238 default:
239 LOGP(0, LOGL_DEBUG, "Unknown GSM 04.80 message-type field 0x%02x\n",
Holger Hans Peter Freytherdaa653f2010-10-11 07:56:06 +0200240 hdr->msg_type);
Holger Hans Peter Freyther00cb5702010-10-09 01:47:15 +0800241 rc = 0;
242 break;
243 }
244
245 return rc;
246}
247
Holger Hans Peter Freytherdaa653f2010-10-11 07:56:06 +0200248static int parse_ussd_info_elements(const uint8_t *ussd_ie, uint16_t len,
249 struct ussd_request *req)
Holger Hans Peter Freyther00cb5702010-10-09 01:47:15 +0800250{
251 int rc = -1;
252 /* Information Element Identifier - table 3.2 & GSM 04.08 section 10.5 */
Holger Hans Peter Freytherdaa653f2010-10-11 07:56:06 +0200253 uint8_t iei;
254 uint8_t iei_length;
255
256 iei = ussd_ie[0];
257 iei_length = ussd_ie[1];
Holger Hans Peter Freyther00cb5702010-10-09 01:47:15 +0800258
259 switch (iei) {
260 case GSM48_IE_CAUSE:
261 break;
262 case GSM0480_IE_FACILITY:
263 rc = parse_facility_ie(ussd_ie+2, iei_length, req);
264 break;
265 case GSM0480_IE_SS_VERSION:
266 break;
267 default:
268 LOGP(0, LOGL_DEBUG, "Unhandled GSM 04.08 or 04.80 IEI 0x%02x\n",
269 iei);
270 rc = 0;
271 break;
272 }
273
274 return rc;
275}
276
Holger Hans Peter Freytherdaa653f2010-10-11 07:56:06 +0200277static int parse_facility_ie(const uint8_t *facility_ie, uint8_t length,
Holger Hans Peter Freyther00cb5702010-10-09 01:47:15 +0800278 struct ussd_request *req)
279{
280 int rc = 1;
281 uint8_t offset = 0;
282
283 do {
284 /* Component Type tag - table 3.7 */
285 uint8_t component_type = facility_ie[offset];
286 uint8_t component_length = facility_ie[offset+1];
287
288 switch (component_type) {
289 case GSM0480_CTYPE_INVOKE:
290 rc &= parse_ss_invoke(facility_ie+2,
291 component_length,
292 req);
293 break;
294 case GSM0480_CTYPE_RETURN_RESULT:
295 break;
296 case GSM0480_CTYPE_RETURN_ERROR:
297 break;
298 case GSM0480_CTYPE_REJECT:
299 break;
300 default:
301 LOGP(0, LOGL_DEBUG, "Unknown GSM 04.80 Facility "
302 "Component Type 0x%02x\n", component_type);
303 rc = 0;
304 break;
305 }
306 offset += (component_length+2);
307 } while (offset < length);
308
309 return rc;
310}
311
312/* Parse an Invoke component - see table 3.3 */
Holger Hans Peter Freytherdaa653f2010-10-11 07:56:06 +0200313static int parse_ss_invoke(const uint8_t *invoke_data, uint8_t length,
Holger Hans Peter Freyther00cb5702010-10-09 01:47:15 +0800314 struct ussd_request *req)
315{
316 int rc = 1;
317 uint8_t offset;
318
319 /* mandatory part */
320 if (invoke_data[0] != GSM0480_COMPIDTAG_INVOKE_ID) {
321 LOGP(0, LOGL_DEBUG, "Unexpected GSM 04.80 Component-ID tag "
322 "0x%02x (expecting Invoke ID tag)\n", invoke_data[0]);
323 }
324
325 offset = invoke_data[1] + 2;
326 req->invoke_id = invoke_data[2];
327
328 /* optional part */
329 if (invoke_data[offset] == GSM0480_COMPIDTAG_LINKED_ID)
330 offset += invoke_data[offset+1] + 2; /* skip over it */
331
332 /* mandatory part */
333 if (invoke_data[offset] == GSM0480_OPERATION_CODE) {
334 uint8_t operation_code = invoke_data[offset+2];
335 switch (operation_code) {
336 case GSM0480_OP_CODE_PROCESS_USS_REQ:
337 rc = parse_process_uss_req(invoke_data + offset + 3,
338 length - offset - 3,
339 req);
340 break;
341 default:
342 LOGP(0, LOGL_DEBUG, "GSM 04.80 operation code 0x%02x "
343 "is not yet handled\n", operation_code);
344 rc = 0;
345 break;
346 }
347 } else {
348 LOGP(0, LOGL_DEBUG, "Unexpected GSM 04.80 Component-ID tag 0x%02x "
349 "(expecting Operation Code tag)\n",
350 invoke_data[0]);
351 rc = 0;
352 }
353
354 return rc;
355}
356
357/* Parse the parameters of a Process UnstructuredSS Request */
Holger Hans Peter Freytherdaa653f2010-10-11 07:56:06 +0200358static int parse_process_uss_req(const uint8_t *uss_req_data, uint8_t length,
Holger Hans Peter Freyther00cb5702010-10-09 01:47:15 +0800359 struct ussd_request *req)
360{
361 int rc = 0;
362 int num_chars;
363 uint8_t dcs;
364
365 if (uss_req_data[0] == GSM_0480_SEQUENCE_TAG) {
366 if (uss_req_data[2] == ASN1_OCTET_STRING_TAG) {
367 dcs = uss_req_data[4];
368 if ((dcs == 0x0F) &&
369 (uss_req_data[5] == ASN1_OCTET_STRING_TAG)) {
370 num_chars = (uss_req_data[6] * 8) / 7;
371 /* Prevent a mobile-originated buffer-overrun! */
372 if (num_chars > MAX_LEN_USSD_STRING)
373 num_chars = MAX_LEN_USSD_STRING;
374 gsm_7bit_decode(req->text,
375 &(uss_req_data[7]), num_chars);
376 /* append null-terminator */
377 req->text[num_chars+1] = 0;
378 rc = 1;
379 }
380 }
381 }
382 return rc;
383}