1 /* GSM Mobile Radio Interface Layer 3 messages
2 * 3GPP TS 04.08 version 7.21.0 Release 1998 / ETSI TS 100 940 V7.21.0 */
4 /* (C) 2008 by Harald Welte <laforge@gnumonks.org>
5 * (C) 2009-2010 by Andreas Eversberg
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.
30 #include <osmocore/utils.h>
31 #include <osmocore/msgb.h>
32 #include <osmocore/tlv.h>
33 #include <osmocore/mncc.h>
34 #include <osmocore/protocol/gsm_04_08.h>
36 static const char bcd_num_digits[] = {
37 '0', '1', '2', '3', '4', '5', '6', '7',
38 '8', '9', '*', '#', 'a', 'b', 'c', '\0'
41 /* decode a 'called/calling/connect party BCD number' as in 10.5.4.7 */
42 int gsm48_decode_bcd_number(char *output, int output_len,
43 const uint8_t *bcd_lv, int h_len)
45 uint8_t in_len = bcd_lv[0];
48 for (i = 1 + h_len; i <= in_len; i++) {
53 *output++ = bcd_num_digits[bcd_lv[i] & 0xf];
59 *output++ = bcd_num_digits[bcd_lv[i] >> 4];
67 /* convert a single ASCII character to call-control BCD */
68 static int asc_to_bcd(const char asc)
72 for (i = 0; i < ARRAY_SIZE(bcd_num_digits); i++) {
73 if (bcd_num_digits[i] == asc)
79 /* convert a ASCII phone number to 'called/calling/connect party BCD number' */
80 int gsm48_encode_bcd_number(uint8_t *bcd_lv, uint8_t max_len,
81 int h_len, const char *input)
83 int in_len = strlen(input);
85 uint8_t *bcd_cur = bcd_lv + 1 + h_len;
87 /* two digits per byte, plus type byte */
88 bcd_lv[0] = in_len/2 + h_len;
92 if (bcd_lv[0] > max_len)
95 for (i = 0; i < in_len; i++) {
96 int rc = asc_to_bcd(input[i]);
102 *bcd_cur++ |= (rc << 4);
104 /* append padding nibble in case of odd length */
108 /* return how many bytes we used */
109 return (bcd_cur - bcd_lv);
112 /* decode 'bearer capability' */
113 int gsm48_decode_bearer_cap(struct gsm_mncc_bearer_cap *bcap,
116 uint8_t in_len = lv[0];
122 bcap->speech_ver[0] = -1; /* end of list, of maximum 7 values */
125 bcap->transfer = lv[1] & 0x07;
126 bcap->mode = (lv[1] & 0x08) >> 3;
127 bcap->coding = (lv[1] & 0x10) >> 4;
128 bcap->radio = (lv[1] & 0x60) >> 5;
130 if (bcap->transfer == GSM_MNCC_BCAP_SPEECH) {
133 while(!(lv[i] & 0x80)) {
134 i++; /* octet 3a etc */
137 bcap->speech_ver[s++] = lv[i] & 0x0f;
138 bcap->speech_ver[s] = -1; /* end of list */
139 if (i == 2) /* octet 3a */
140 bcap->speech_ctm = (lv[i] & 0x20) >> 5;
141 if (s == 7) /* maximum speech versions + end of list */
146 while (!(lv[i] & 0x80)) {
147 i++; /* octet 3a etc */
152 /* FIXME: implement OCTET 4+ parsing */
158 /* encode 'bearer capability' */
159 int gsm48_encode_bearer_cap(struct msgb *msg, int lv_only,
160 const struct gsm_mncc_bearer_cap *bcap)
165 lv[1] = bcap->transfer;
166 lv[1] |= bcap->mode << 3;
167 lv[1] |= bcap->coding << 4;
168 lv[1] |= bcap->radio << 5;
170 if (bcap->transfer == GSM_MNCC_BCAP_SPEECH) {
171 for (s = 0; bcap->speech_ver[s] >= 0; s++) {
172 i++; /* octet 3a etc */
173 lv[i] = bcap->speech_ver[s];
174 if (i == 2) /* octet 3a */
175 lv[i] |= bcap->speech_ctm << 5;
177 lv[i] |= 0x80; /* last IE of octet 3 etc */
179 /* FIXME: implement OCTET 4+ encoding */
184 msgb_lv_put(msg, lv[0], lv+1);
186 msgb_tlv_put(msg, GSM48_IE_BEARER_CAP, lv[0], lv+1);
191 /* decode 'call control cap' */
192 int gsm48_decode_cccap(struct gsm_mncc_cccap *ccap, const uint8_t *lv)
194 uint8_t in_len = lv[0];
200 ccap->dtmf = lv[1] & 0x01;
201 ccap->pcp = (lv[1] & 0x02) >> 1;
206 /* encode 'call control cap' */
207 int gsm48_encode_cccap(struct msgb *msg,
208 const struct gsm_mncc_cccap *ccap)
219 msgb_tlv_put(msg, GSM48_IE_CC_CAP, lv[0], lv+1);
224 /* decode 'called party BCD number' */
225 int gsm48_decode_called(struct gsm_mncc_number *called,
228 uint8_t in_len = lv[0];
234 called->plan = lv[1] & 0x0f;
235 called->type = (lv[1] & 0x70) >> 4;
238 gsm48_decode_bcd_number(called->number, sizeof(called->number), lv, 1);
243 /* encode 'called party BCD number' */
244 int gsm48_encode_called(struct msgb *msg,
245 const struct gsm_mncc_number *called)
251 lv[1] = called->plan;
252 lv[1] |= called->type << 4;
254 /* octet 4..N, octet 2 */
255 ret = gsm48_encode_bcd_number(lv, sizeof(lv), 1, called->number);
259 msgb_tlv_put(msg, GSM48_IE_CALLED_BCD, lv[0], lv+1);
264 /* decode callerid of various IEs */
265 int gsm48_decode_callerid(struct gsm_mncc_number *callerid,
268 uint8_t in_len = lv[0];
275 callerid->plan = lv[1] & 0x0f;
276 callerid->type = (lv[1] & 0x70) >> 4;
279 if (!(lv[1] & 0x80)) {
280 callerid->screen = lv[2] & 0x03;
281 callerid->present = (lv[2] & 0x60) >> 5;
286 gsm48_decode_bcd_number(callerid->number, sizeof(callerid->number), lv, i);
291 /* encode callerid of various IEs */
292 int gsm48_encode_callerid(struct msgb *msg, int ie, int max_len,
293 const struct gsm_mncc_number *callerid)
295 uint8_t lv[max_len - 1];
300 lv[1] = callerid->plan;
301 lv[1] |= callerid->type << 4;
303 if (callerid->present || callerid->screen) {
305 lv[2] = callerid->screen;
306 lv[2] |= callerid->present << 5;
312 /* octet 4..N, octet 2 */
313 ret = gsm48_encode_bcd_number(lv, sizeof(lv), h_len, callerid->number);
317 msgb_tlv_put(msg, ie, lv[0], lv+1);
323 int gsm48_decode_cause(struct gsm_mncc_cause *cause,
326 uint8_t in_len = lv[0];
335 cause->location = lv[1] & 0x0f;
336 cause->coding = (lv[1] & 0x60) >> 5;
339 if (!(lv[i] & 0x80)) {
344 cause->rec_val = lv[i] & 0x7f;
349 cause->value = lv[i] & 0x7f;
352 if (in_len < i) /* no diag */
355 if (in_len - (i-1) > 32) /* maximum 32 octets */
359 memcpy(cause->diag, lv + i, in_len - (i-1));
360 cause->diag_len = in_len - (i-1);
366 int gsm48_encode_cause(struct msgb *msg, int lv_only,
367 const struct gsm_mncc_cause *cause)
372 if (cause->diag_len > 32)
376 lv[1] = cause->location;
377 lv[1] |= cause->coding << 5;
382 lv[i] = cause->rec_val;
384 lv[i] |= 0x80; /* end of octet 3 */
388 lv[i] = 0x80 | cause->value;
391 if (cause->diag_len) {
392 memcpy(lv + i, cause->diag, cause->diag_len);
393 i += cause->diag_len;
398 msgb_lv_put(msg, lv[0], lv+1);
400 msgb_tlv_put(msg, GSM48_IE_CAUSE, lv[0], lv+1);
405 /* decode 'calling number' */
406 int gsm48_decode_calling(struct gsm_mncc_number *calling,
409 return gsm48_decode_callerid(calling, lv);
412 /* encode 'calling number' */
413 int gsm48_encode_calling(struct msgb *msg,
414 const struct gsm_mncc_number *calling)
416 return gsm48_encode_callerid(msg, GSM48_IE_CALLING_BCD, 14, calling);
419 /* decode 'connected number' */
420 int gsm48_decode_connected(struct gsm_mncc_number *connected,
423 return gsm48_decode_callerid(connected, lv);
426 /* encode 'connected number' */
427 int gsm48_encode_connected(struct msgb *msg,
428 const struct gsm_mncc_number *connected)
430 return gsm48_encode_callerid(msg, GSM48_IE_CONN_BCD, 14, connected);
433 /* decode 'redirecting number' */
434 int gsm48_decode_redirecting(struct gsm_mncc_number *redirecting,
437 return gsm48_decode_callerid(redirecting, lv);
440 /* encode 'redirecting number' */
441 int gsm48_encode_redirecting(struct msgb *msg,
442 const struct gsm_mncc_number *redirecting)
444 return gsm48_encode_callerid(msg, GSM48_IE_REDIR_BCD, 19, redirecting);
447 /* decode 'facility' */
448 int gsm48_decode_facility(struct gsm_mncc_facility *facility,
451 uint8_t in_len = lv[0];
456 if (in_len > sizeof(facility->info))
459 memcpy(facility->info, lv+1, in_len);
460 facility->len = in_len;
465 /* encode 'facility' */
466 int gsm48_encode_facility(struct msgb *msg, int lv_only,
467 const struct gsm_mncc_facility *facility)
469 uint8_t lv[GSM_MAX_FACILITY + 1];
471 if (facility->len < 1 || facility->len > GSM_MAX_FACILITY)
474 memcpy(lv+1, facility->info, facility->len);
475 lv[0] = facility->len;
477 msgb_lv_put(msg, lv[0], lv+1);
479 msgb_tlv_put(msg, GSM48_IE_FACILITY, lv[0], lv+1);
484 /* decode 'notify' */
485 int gsm48_decode_notify(int *notify, const uint8_t *v)
487 *notify = v[0] & 0x7f;
492 /* encode 'notify' */
493 int gsm48_encode_notify(struct msgb *msg, int notify)
495 msgb_v_put(msg, notify | 0x80);
500 /* decode 'signal' */
501 int gsm48_decode_signal(int *signal, const uint8_t *v)
508 /* encode 'signal' */
509 int gsm48_encode_signal(struct msgb *msg, int signal)
511 msgb_tv_put(msg, GSM48_IE_SIGNAL, signal);
516 /* decode 'keypad' */
517 int gsm48_decode_keypad(int *keypad, const uint8_t *lv)
519 uint8_t in_len = lv[0];
524 *keypad = lv[1] & 0x7f;
529 /* encode 'keypad' */
530 int gsm48_encode_keypad(struct msgb *msg, int keypad)
532 msgb_tv_put(msg, GSM48_IE_KPD_FACILITY, keypad);
537 /* decode 'progress' */
538 int gsm48_decode_progress(struct gsm_mncc_progress *progress,
541 uint8_t in_len = lv[0];
546 progress->coding = (lv[1] & 0x60) >> 5;
547 progress->location = lv[1] & 0x0f;
548 progress->descr = lv[2] & 0x7f;
553 /* encode 'progress' */
554 int gsm48_encode_progress(struct msgb *msg, int lv_only,
555 const struct gsm_mncc_progress *p)
560 lv[1] = 0x80 | ((p->coding & 0x3) << 5) | (p->location & 0xf);
561 lv[2] = 0x80 | (p->descr & 0x7f);
563 msgb_lv_put(msg, lv[0], lv+1);
565 msgb_tlv_put(msg, GSM48_IE_PROGR_IND, lv[0], lv+1);
570 /* decode 'user-user' */
571 int gsm48_decode_useruser(struct gsm_mncc_useruser *uu,
574 uint8_t in_len = lv[0];
575 char *info = uu->info;
576 int info_len = sizeof(uu->info);
584 for (i = 2; i <= in_len; i++) {
596 /* encode 'useruser' */
597 int gsm48_encode_useruser(struct msgb *msg, int lv_only,
598 const struct gsm_mncc_useruser *uu)
600 uint8_t lv[GSM_MAX_USERUSER + 2];
602 if (strlen(uu->info) > GSM_MAX_USERUSER)
605 lv[0] = 1 + strlen(uu->info);
607 memcpy(lv + 2, uu->info, strlen(uu->info));
609 msgb_lv_put(msg, lv[0], lv+1);
611 msgb_tlv_put(msg, GSM48_IE_USER_USER, lv[0], lv+1);
616 /* decode 'ss version' */
617 int gsm48_decode_ssversion(struct gsm_mncc_ssversion *ssv,
620 uint8_t in_len = lv[0];
622 if (in_len < 1 || in_len < sizeof(ssv->info))
625 memcpy(ssv->info, lv + 1, in_len);
631 /* encode 'ss version' */
632 int gsm48_encode_ssversion(struct msgb *msg,
633 const struct gsm_mncc_ssversion *ssv)
635 uint8_t lv[GSM_MAX_SSVERSION + 1];
637 if (ssv->len > GSM_MAX_SSVERSION)
641 memcpy(lv + 1, ssv->info, ssv->len);
642 msgb_tlv_put(msg, GSM48_IE_SS_VERS, lv[0], lv+1);
647 /* decode 'more data' does not require a function, because it has no value */
649 /* encode 'more data' */
650 int gsm48_encode_more(struct msgb *msg)
654 ie = msgb_put(msg, 1);
655 ie[0] = GSM48_IE_MORE_DATA;