1 /* Format functions for GSM 04.80 */
4 * (C) 2010 by Holger Hans Peter Freyther <zecke@selfish.org>
5 * (C) 2009 by Mike Haben <michael.haben@btinternet.com>
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.
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.
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.
25 #include <osmocore/gsm0480.h>
26 #include <osmocore/gsm_utils.h>
28 #include <osmocore/logging.h>
30 #include <osmocore/protocol/gsm_04_08.h>
31 #include <osmocore/protocol/gsm_04_80.h>
35 static inline unsigned char *msgb_wrap_with_TL(struct msgb *msgb, uint8_t tag)
37 uint8_t *data = msgb_push(msgb, 2);
40 data[1] = msgb->len - 2;
44 static inline unsigned char *msgb_push_TLV1(struct msgb *msgb, uint8_t tag,
47 uint8_t *data = msgb_push(msgb, 3);
55 /* wrap an invoke around it... the other way around
57 * 1.) Invoke Component tag
62 int gsm0480_wrap_invoke(struct msgb *msg, int op, int link_id)
65 msgb_push_TLV1(msg, GSM0480_OPERATION_CODE, op);
67 /* 2. invoke id tag */
68 msgb_push_TLV1(msg, GSM0480_COMPIDTAG_INVOKE_ID, link_id);
70 /* 1. component tag */
71 msgb_wrap_with_TL(msg, GSM0480_CTYPE_INVOKE);
76 /* wrap the GSM 04.08 Facility IE around it */
77 int gsm0480_wrap_facility(struct msgb *msg)
79 msgb_wrap_with_TL(msg, GSM0480_IE_FACILITY);
84 struct msgb *gsm0480_create_unstructuredSS_Notify(int alertPattern, const char *text)
87 uint8_t *seq_len_ptr, *ussd_len_ptr, *data;
90 msg = msgb_alloc_headroom(1024, 128, "GSM 04.80");
95 msgb_put_u8(msg, GSM_0480_SEQUENCE_TAG);
96 seq_len_ptr = msgb_put(msg, 1);
99 msgb_put_u8(msg, ASN1_OCTET_STRING_TAG);
101 msgb_put_u8(msg, 0x0F);
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);
110 ussd_len_ptr[0] = len;
113 /* alertingPattern { */
114 msgb_put_u8(msg, ASN1_OCTET_STRING_TAG);
116 msgb_put_u8(msg, alertPattern);
117 /* } alertingPattern */
119 seq_len_ptr[0] = 3 + 2 + ussd_len_ptr[0] + 3;
125 struct msgb *gsm0480_create_notifySS(const char *text)
128 uint8_t *data, *tmp_len;
129 uint8_t *seq_len_ptr, *cal_len_ptr, *opt_len_ptr, *nam_len_ptr;
133 if (len < 1 || len > 160)
136 msg = msgb_alloc_headroom(1024, 128, "GSM 04.80");
140 msgb_put_u8(msg, GSM_0480_SEQUENCE_TAG);
141 seq_len_ptr = msgb_put(msg, 1);
143 /* ss_code for CNAP { */
144 msgb_put_u8(msg, 0x81);
146 msgb_put_u8(msg, 0x19);
150 /* nameIndicator { */
151 msgb_put_u8(msg, 0xB4);
152 nam_len_ptr = msgb_put(msg, 1);
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);
160 /* namePresentationAllowed { */
161 /* add the DCS value */
162 msgb_put_u8(msg, 0x80);
164 msgb_put_u8(msg, 0x0F);
166 /* add the lengthInCharacters */
167 msgb_put_u8(msg, 0x81);
169 msgb_put_u8(msg, strlen(text));
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);
179 /* }; namePresentationAllowed */
181 cal_len_ptr[0] = 3 + 3 + 2 + len;
182 opt_len_ptr[0] = cal_len_ptr[0] + 2;
185 nam_len_ptr[0] = opt_len_ptr[0] + 2;
186 /* ); nameIndicator */
188 /* write the lengths... */
189 seq_len_ptr[0] = 3 + nam_len_ptr[0] + 2;
194 /* Forward declarations */
195 static int parse_ussd(const struct gsm48_hdr *hdr,
196 uint16_t len, struct ussd_request *req);
197 static int parse_ussd_info_elements(const uint8_t *ussd_ie, uint16_t len,
198 struct ussd_request *req);
199 static int parse_facility_ie(const uint8_t *facility_ie, uint16_t length,
200 struct ussd_request *req);
201 static int parse_ss_invoke(const uint8_t *invoke_data, uint16_t length,
202 struct ussd_request *req);
203 static int parse_process_uss_req(const uint8_t *uss_req_data, uint16_t length,
204 struct ussd_request *req);
206 /* Decode a mobile-originated USSD-request message */
207 int gsm0480_decode_ussd_request(const struct gsm48_hdr *hdr, uint16_t len,
208 struct ussd_request *req)
212 if (len < sizeof(*hdr) + 2) {
213 LOGP(0, LOGL_DEBUG, "USSD Request is too short.\n");
217 if ((hdr->proto_discr & 0x0f) == GSM48_PDISC_NC_SS) {
218 req->transaction_id = hdr->proto_discr & 0x70;
219 rc = parse_ussd(hdr, len, req);
223 LOGP(0, LOGL_DEBUG, "Error occurred while parsing received USSD!\n");
228 static int parse_ussd(const struct gsm48_hdr *hdr, uint16_t len, struct ussd_request *req)
231 uint8_t msg_type = hdr->msg_type & 0xBF; /* message-type - section 3.4 */
234 case GSM0480_MTYPE_RELEASE_COMPLETE:
235 LOGP(0, LOGL_DEBUG, "USS Release Complete\n");
236 /* could also parse out the optional Cause/Facility data */
239 case GSM0480_MTYPE_REGISTER:
240 case GSM0480_MTYPE_FACILITY:
241 rc &= parse_ussd_info_elements(&hdr->data[0], len - sizeof(*hdr), req);
244 LOGP(0, LOGL_DEBUG, "Unknown GSM 04.80 message-type field 0x%02x\n",
253 static int parse_ussd_info_elements(const uint8_t *ussd_ie, uint16_t len,
254 struct ussd_request *req)
257 /* Information Element Identifier - table 3.2 & GSM 04.08 section 10.5 */
262 iei_length = ussd_ie[1];
264 /* If the data does not fit, report an error */
265 if (len - 2 < iei_length)
271 case GSM0480_IE_FACILITY:
272 rc = parse_facility_ie(ussd_ie+2, iei_length, req);
274 case GSM0480_IE_SS_VERSION:
277 LOGP(0, LOGL_DEBUG, "Unhandled GSM 04.08 or 04.80 IEI 0x%02x\n",
286 static int parse_facility_ie(const uint8_t *facility_ie, uint16_t length,
287 struct ussd_request *req)
292 while (offset + 2 <= length) {
293 /* Component Type tag - table 3.7 */
294 uint8_t component_type = facility_ie[offset];
295 uint8_t component_length = facility_ie[offset+1];
298 if (offset + 2 + component_length > length) {
299 LOGP(0, LOGL_ERROR, "Component does not fit.\n");
303 switch (component_type) {
304 case GSM0480_CTYPE_INVOKE:
305 rc &= parse_ss_invoke(facility_ie+2,
309 case GSM0480_CTYPE_RETURN_RESULT:
311 case GSM0480_CTYPE_RETURN_ERROR:
313 case GSM0480_CTYPE_REJECT:
316 LOGP(0, LOGL_DEBUG, "Unknown GSM 04.80 Facility "
317 "Component Type 0x%02x\n", component_type);
321 offset += (component_length+2);
327 /* Parse an Invoke component - see table 3.3 */
328 static int parse_ss_invoke(const uint8_t *invoke_data, uint16_t length,
329 struct ussd_request *req)
338 if (invoke_data[0] != GSM0480_COMPIDTAG_INVOKE_ID) {
339 LOGP(0, LOGL_DEBUG, "Unexpected GSM 04.80 Component-ID tag "
340 "0x%02x (expecting Invoke ID tag)\n", invoke_data[0]);
343 offset = invoke_data[1] + 2;
344 req->invoke_id = invoke_data[2];
346 /* look ahead once */
347 if (offset + 1 > length)
351 if (invoke_data[offset] == GSM0480_COMPIDTAG_LINKED_ID)
352 offset += invoke_data[offset+1] + 2; /* skip over it */
355 if (invoke_data[offset] == GSM0480_OPERATION_CODE) {
356 if (offset + 2 > length)
358 uint8_t operation_code = invoke_data[offset+2];
359 switch (operation_code) {
360 case GSM0480_OP_CODE_PROCESS_USS_REQ:
361 rc = parse_process_uss_req(invoke_data + offset + 3,
366 LOGP(0, LOGL_DEBUG, "GSM 04.80 operation code 0x%02x "
367 "is not yet handled\n", operation_code);
372 LOGP(0, LOGL_DEBUG, "Unexpected GSM 04.80 Component-ID tag 0x%02x "
373 "(expecting Operation Code tag)\n",
381 /* Parse the parameters of a Process UnstructuredSS Request */
382 static int parse_process_uss_req(const uint8_t *uss_req_data, uint16_t length,
383 struct ussd_request *req)
390 /* we need at least that much */
395 if (uss_req_data[0] == GSM_0480_SEQUENCE_TAG) {
396 if (uss_req_data[2] == ASN1_OCTET_STRING_TAG) {
397 dcs = uss_req_data[4];
399 (uss_req_data[5] == ASN1_OCTET_STRING_TAG)) {
400 num_chars = (uss_req_data[6] * 8) / 7;
401 /* Prevent a mobile-originated buffer-overrun! */
402 if (num_chars > MAX_LEN_USSD_STRING)
403 num_chars = MAX_LEN_USSD_STRING;
404 gsm_7bit_decode(req->text,
405 &(uss_req_data[7]), num_chars);
406 /* append null-terminator */
407 req->text[num_chars+1] = 0;