blob: 7d607a95890c4ba65904b449ddce51b9935f0564 [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 */
195static int parse_ussd(uint8_t *ussd, struct ussd_request *req);
196static int parse_ussd_info_elements(uint8_t *ussd_ie,
197 struct ussd_request *req);
198static int parse_facility_ie(uint8_t *facility_ie, uint8_t length,
199 struct ussd_request *req);
200static int parse_ss_invoke(uint8_t *invoke_data, uint8_t length,
201 struct ussd_request *req);
202static int parse_process_uss_req(uint8_t *uss_req_data, uint8_t length,
203 struct ussd_request *req);
204
205/* Decode a mobile-originated USSD-request message */
206int gsm0480_decode_ussd_request(const struct msgb *msg, struct ussd_request *req)
207{
208 int rc = 0;
209 uint8_t *parse_ptr = msgb_l3(msg);
210
211 if ((*parse_ptr & 0x0F) == GSM48_PDISC_NC_SS) {
212 req->transaction_id = *parse_ptr & 0x70;
213 rc = parse_ussd(parse_ptr+1, req);
214 }
215
216 if (!rc)
217 LOGP(0, LOGL_DEBUG, "Error occurred while parsing received USSD!\n");
218
219 return rc;
220}
221
222static int parse_ussd(uint8_t *ussd, struct ussd_request *req)
223{
224 int rc = 1;
225 uint8_t msg_type = ussd[0] & 0xBF; /* message-type - section 3.4 */
226
227 switch (msg_type) {
228 case GSM0480_MTYPE_RELEASE_COMPLETE:
229 LOGP(0, LOGL_DEBUG, "USS Release Complete\n");
230 /* could also parse out the optional Cause/Facility data */
231 req->text[0] = 0xFF;
232 break;
233 case GSM0480_MTYPE_REGISTER:
234 case GSM0480_MTYPE_FACILITY:
235 rc &= parse_ussd_info_elements(ussd+1, req);
236 break;
237 default:
238 LOGP(0, LOGL_DEBUG, "Unknown GSM 04.80 message-type field 0x%02x\n",
239 ussd[0]);
240 rc = 0;
241 break;
242 }
243
244 return rc;
245}
246
247static int parse_ussd_info_elements(uint8_t *ussd_ie, struct ussd_request *req)
248{
249 int rc = -1;
250 /* Information Element Identifier - table 3.2 & GSM 04.08 section 10.5 */
251 uint8_t iei = ussd_ie[0];
252 uint8_t iei_length = ussd_ie[1];
253
254 switch (iei) {
255 case GSM48_IE_CAUSE:
256 break;
257 case GSM0480_IE_FACILITY:
258 rc = parse_facility_ie(ussd_ie+2, iei_length, req);
259 break;
260 case GSM0480_IE_SS_VERSION:
261 break;
262 default:
263 LOGP(0, LOGL_DEBUG, "Unhandled GSM 04.08 or 04.80 IEI 0x%02x\n",
264 iei);
265 rc = 0;
266 break;
267 }
268
269 return rc;
270}
271
272static int parse_facility_ie(uint8_t *facility_ie, uint8_t length,
273 struct ussd_request *req)
274{
275 int rc = 1;
276 uint8_t offset = 0;
277
278 do {
279 /* Component Type tag - table 3.7 */
280 uint8_t component_type = facility_ie[offset];
281 uint8_t component_length = facility_ie[offset+1];
282
283 switch (component_type) {
284 case GSM0480_CTYPE_INVOKE:
285 rc &= parse_ss_invoke(facility_ie+2,
286 component_length,
287 req);
288 break;
289 case GSM0480_CTYPE_RETURN_RESULT:
290 break;
291 case GSM0480_CTYPE_RETURN_ERROR:
292 break;
293 case GSM0480_CTYPE_REJECT:
294 break;
295 default:
296 LOGP(0, LOGL_DEBUG, "Unknown GSM 04.80 Facility "
297 "Component Type 0x%02x\n", component_type);
298 rc = 0;
299 break;
300 }
301 offset += (component_length+2);
302 } while (offset < length);
303
304 return rc;
305}
306
307/* Parse an Invoke component - see table 3.3 */
308static int parse_ss_invoke(uint8_t *invoke_data, uint8_t length,
309 struct ussd_request *req)
310{
311 int rc = 1;
312 uint8_t offset;
313
314 /* mandatory part */
315 if (invoke_data[0] != GSM0480_COMPIDTAG_INVOKE_ID) {
316 LOGP(0, LOGL_DEBUG, "Unexpected GSM 04.80 Component-ID tag "
317 "0x%02x (expecting Invoke ID tag)\n", invoke_data[0]);
318 }
319
320 offset = invoke_data[1] + 2;
321 req->invoke_id = invoke_data[2];
322
323 /* optional part */
324 if (invoke_data[offset] == GSM0480_COMPIDTAG_LINKED_ID)
325 offset += invoke_data[offset+1] + 2; /* skip over it */
326
327 /* mandatory part */
328 if (invoke_data[offset] == GSM0480_OPERATION_CODE) {
329 uint8_t operation_code = invoke_data[offset+2];
330 switch (operation_code) {
331 case GSM0480_OP_CODE_PROCESS_USS_REQ:
332 rc = parse_process_uss_req(invoke_data + offset + 3,
333 length - offset - 3,
334 req);
335 break;
336 default:
337 LOGP(0, LOGL_DEBUG, "GSM 04.80 operation code 0x%02x "
338 "is not yet handled\n", operation_code);
339 rc = 0;
340 break;
341 }
342 } else {
343 LOGP(0, LOGL_DEBUG, "Unexpected GSM 04.80 Component-ID tag 0x%02x "
344 "(expecting Operation Code tag)\n",
345 invoke_data[0]);
346 rc = 0;
347 }
348
349 return rc;
350}
351
352/* Parse the parameters of a Process UnstructuredSS Request */
353static int parse_process_uss_req(uint8_t *uss_req_data, uint8_t length,
354 struct ussd_request *req)
355{
356 int rc = 0;
357 int num_chars;
358 uint8_t dcs;
359
360 if (uss_req_data[0] == GSM_0480_SEQUENCE_TAG) {
361 if (uss_req_data[2] == ASN1_OCTET_STRING_TAG) {
362 dcs = uss_req_data[4];
363 if ((dcs == 0x0F) &&
364 (uss_req_data[5] == ASN1_OCTET_STRING_TAG)) {
365 num_chars = (uss_req_data[6] * 8) / 7;
366 /* Prevent a mobile-originated buffer-overrun! */
367 if (num_chars > MAX_LEN_USSD_STRING)
368 num_chars = MAX_LEN_USSD_STRING;
369 gsm_7bit_decode(req->text,
370 &(uss_req_data[7]), num_chars);
371 /* append null-terminator */
372 req->text[num_chars+1] = 0;
373 rc = 1;
374 }
375 }
376 }
377 return rc;
378}