rename log_info to osmo_log_info to avoid namespace clash with app
[osmocom-bb.git] / src / gsm48_ie.c
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 */
3
4 /* (C) 2008 by Harald Welte <laforge@gnumonks.org>
5  * (C) 2009-2010 by Andreas Eversberg
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 <stdint.h>
27 #include <string.h>
28 #include <errno.h>
29
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>
35
36 static const char bcd_num_digits[] = {
37         '0', '1', '2', '3', '4', '5', '6', '7',
38         '8', '9', '*', '#', 'a', 'b', 'c', '\0'
39 };
40
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)
44 {
45         uint8_t in_len = bcd_lv[0];
46         int i;
47
48         for (i = 1 + h_len; i <= in_len; i++) {
49                 /* lower nibble */
50                 output_len--;
51                 if (output_len <= 1)
52                         break;
53                 *output++ = bcd_num_digits[bcd_lv[i] & 0xf];
54
55                 /* higher nibble */
56                 output_len--;
57                 if (output_len <= 1)
58                         break;
59                 *output++ = bcd_num_digits[bcd_lv[i] >> 4];
60         }
61         if (output_len >= 1)
62                 *output++ = '\0';
63
64         return 0;
65 }
66
67 /* convert a single ASCII character to call-control BCD */
68 static int asc_to_bcd(const char asc)
69 {
70         int i;
71
72         for (i = 0; i < ARRAY_SIZE(bcd_num_digits); i++) {
73                 if (bcd_num_digits[i] == asc)
74                         return i;
75         }
76         return -EINVAL;
77 }
78
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)
82 {
83         int in_len = strlen(input);
84         int i;
85         uint8_t *bcd_cur = bcd_lv + 1 + h_len;
86
87         /* two digits per byte, plus type byte */
88         bcd_lv[0] = in_len/2 + h_len;
89         if (in_len % 2)
90                 bcd_lv[0]++;
91
92         if (bcd_lv[0] > max_len)
93                 return -EIO;
94
95         for (i = 0; i < in_len; i++) {
96                 int rc = asc_to_bcd(input[i]);
97                 if (rc < 0)
98                         return rc;
99                 if (i % 2 == 0)
100                         *bcd_cur = rc;
101                 else
102                         *bcd_cur++ |= (rc << 4);
103         }
104         /* append padding nibble in case of odd length */
105         if (i % 2)
106                 *bcd_cur++ |= 0xf0;
107
108         /* return how many bytes we used */
109         return (bcd_cur - bcd_lv);
110 }
111
112 /* decode 'bearer capability' */
113 int gsm48_decode_bearer_cap(struct gsm_mncc_bearer_cap *bcap,
114                              const uint8_t *lv)
115 {
116         uint8_t in_len = lv[0];
117         int i, s;
118
119         if (in_len < 1)
120                 return -EINVAL;
121
122         bcap->speech_ver[0] = -1; /* end of list, of maximum 7 values */
123
124         /* octet 3 */
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;
129
130         if (bcap->transfer == GSM_MNCC_BCAP_SPEECH) {
131                 i = 1;
132                 s = 0;
133                 while(!(lv[i] & 0x80)) {
134                         i++; /* octet 3a etc */
135                         if (in_len < i)
136                                 return 0;
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 */
142                                 return 0;
143                 }
144         } else {
145                 i = 1;
146                 while (!(lv[i] & 0x80)) {
147                         i++; /* octet 3a etc */
148                         if (in_len < i)
149                                 return 0;
150                         /* ignore them */
151                 }
152                 /* FIXME: implement OCTET 4+ parsing */
153         }
154
155         return 0;
156 }
157
158 /* encode 'bearer capability' */
159 int gsm48_encode_bearer_cap(struct msgb *msg, int lv_only,
160                              const struct gsm_mncc_bearer_cap *bcap)
161 {
162         uint8_t lv[32 + 1];
163         int i = 1, s;
164
165         lv[1] = bcap->transfer;
166         lv[1] |= bcap->mode << 3;
167         lv[1] |= bcap->coding << 4;
168         lv[1] |= bcap->radio << 5;
169
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;
176                 }
177                 lv[i] |= 0x80; /* last IE of octet 3 etc */
178         } else {
179                 /* FIXME: implement OCTET 4+ encoding */
180         }
181
182         lv[0] = i;
183         if (lv_only)
184                 msgb_lv_put(msg, lv[0], lv+1);
185         else
186                 msgb_tlv_put(msg, GSM48_IE_BEARER_CAP, lv[0], lv+1);
187
188         return 0;
189 }
190
191 /* decode 'call control cap' */
192 int gsm48_decode_cccap(struct gsm_mncc_cccap *ccap, const uint8_t *lv)
193 {
194         uint8_t in_len = lv[0];
195
196         if (in_len < 1)
197                 return -EINVAL;
198
199         /* octet 3 */
200         ccap->dtmf = lv[1] & 0x01;
201         ccap->pcp = (lv[1] & 0x02) >> 1;
202
203         return 0;
204 }
205
206 /* encode 'call control cap' */
207 int gsm48_encode_cccap(struct msgb *msg,
208                         const struct gsm_mncc_cccap *ccap)
209 {
210         uint8_t lv[2];
211
212         lv[0] = 1;
213         lv[1] = 0;
214         if (ccap->dtmf)
215                 lv [1] |= 0x01;
216         if (ccap->pcp)
217                 lv [1] |= 0x02;
218
219         msgb_tlv_put(msg, GSM48_IE_CC_CAP, lv[0], lv+1);
220
221         return 0;
222 }
223
224 /* decode 'called party BCD number' */
225 int gsm48_decode_called(struct gsm_mncc_number *called,
226                          const uint8_t *lv)
227 {
228         uint8_t in_len = lv[0];
229
230         if (in_len < 1)
231                 return -EINVAL;
232
233         /* octet 3 */
234         called->plan = lv[1] & 0x0f;
235         called->type = (lv[1] & 0x70) >> 4;
236
237         /* octet 4..N */
238         gsm48_decode_bcd_number(called->number, sizeof(called->number), lv, 1);
239
240         return 0;
241 }
242
243 /* encode 'called party BCD number' */
244 int gsm48_encode_called(struct msgb *msg,
245                          const struct gsm_mncc_number *called)
246 {
247         uint8_t lv[18];
248         int ret;
249
250         /* octet 3 */
251         lv[1] = called->plan;
252         lv[1] |= called->type << 4;
253
254         /* octet 4..N, octet 2 */
255         ret = gsm48_encode_bcd_number(lv, sizeof(lv), 1, called->number);
256         if (ret < 0)
257                 return ret;
258
259         msgb_tlv_put(msg, GSM48_IE_CALLED_BCD, lv[0], lv+1);
260
261         return 0;
262 }
263
264 /* decode callerid of various IEs */
265 int gsm48_decode_callerid(struct gsm_mncc_number *callerid,
266                          const uint8_t *lv)
267 {
268         uint8_t in_len = lv[0];
269         int i = 1;
270
271         if (in_len < 1)
272                 return -EINVAL;
273
274         /* octet 3 */
275         callerid->plan = lv[1] & 0x0f;
276         callerid->type = (lv[1] & 0x70) >> 4;
277
278         /* octet 3a */
279         if (!(lv[1] & 0x80)) {
280                 callerid->screen = lv[2] & 0x03;
281                 callerid->present = (lv[2] & 0x60) >> 5;
282                 i = 2;
283         }
284
285         /* octet 4..N */
286         gsm48_decode_bcd_number(callerid->number, sizeof(callerid->number), lv, i);
287
288         return 0;
289 }
290
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)
294 {
295         uint8_t lv[max_len - 1];
296         int h_len = 1;
297         int ret;
298
299         /* octet 3 */
300         lv[1] = callerid->plan;
301         lv[1] |= callerid->type << 4;
302
303         if (callerid->present || callerid->screen) {
304                 /* octet 3a */
305                 lv[2] = callerid->screen;
306                 lv[2] |= callerid->present << 5;
307                 lv[2] |= 0x80;
308                 h_len++;
309         } else
310                 lv[1] |= 0x80;
311
312         /* octet 4..N, octet 2 */
313         ret = gsm48_encode_bcd_number(lv, sizeof(lv), h_len, callerid->number);
314         if (ret < 0)
315                 return ret;
316
317         msgb_tlv_put(msg, ie, lv[0], lv+1);
318
319         return 0;
320 }
321
322 /* decode 'cause' */
323 int gsm48_decode_cause(struct gsm_mncc_cause *cause,
324                         const uint8_t *lv)
325 {
326         uint8_t in_len = lv[0];
327         int i;
328
329         if (in_len < 2)
330                 return -EINVAL;
331
332         cause->diag_len = 0;
333
334         /* octet 3 */
335         cause->location = lv[1] & 0x0f;
336         cause->coding = (lv[1] & 0x60) >> 5;
337
338         i = 1;
339         if (!(lv[i] & 0x80)) {
340                 i++; /* octet 3a */
341                 if (in_len < i+1)
342                         return 0;
343                 cause->rec = 1;
344                 cause->rec_val = lv[i] & 0x7f;
345         }
346         i++;
347
348         /* octet 4 */
349         cause->value = lv[i] & 0x7f;
350         i++;
351
352         if (in_len < i) /* no diag */
353                 return 0;
354
355         if (in_len - (i-1) > 32) /* maximum 32 octets */
356                 return 0;
357
358         /* octet 5-N */
359         memcpy(cause->diag, lv + i, in_len - (i-1));
360         cause->diag_len = in_len - (i-1);
361
362         return 0;
363 }
364
365 /* encode 'cause' */
366 int gsm48_encode_cause(struct msgb *msg, int lv_only,
367                         const struct gsm_mncc_cause *cause)
368 {
369         uint8_t lv[32+4];
370         int i;
371
372         if (cause->diag_len > 32)
373                 return -EINVAL;
374
375         /* octet 3 */
376         lv[1] = cause->location;
377         lv[1] |= cause->coding << 5;
378
379         i = 1;
380         if (cause->rec) {
381                 i++; /* octet 3a */
382                 lv[i] = cause->rec_val;
383         }
384         lv[i] |= 0x80; /* end of octet 3 */
385
386         /* octet 4 */
387         i++;
388         lv[i] = 0x80 | cause->value;
389
390         /* octet 5-N */
391         if (cause->diag_len) {
392                 memcpy(lv + i, cause->diag, cause->diag_len);
393                 i += cause->diag_len;
394         }
395
396         lv[0] = i;
397         if (lv_only)
398                 msgb_lv_put(msg, lv[0], lv+1);
399         else
400                 msgb_tlv_put(msg, GSM48_IE_CAUSE, lv[0], lv+1);
401
402         return 0;
403 }
404
405 /* decode 'calling number' */
406 int gsm48_decode_calling(struct gsm_mncc_number *calling,
407                          const uint8_t *lv)
408 {
409         return gsm48_decode_callerid(calling, lv);
410 }
411
412 /* encode 'calling number' */
413 int gsm48_encode_calling(struct msgb *msg, 
414                           const struct gsm_mncc_number *calling)
415 {
416         return gsm48_encode_callerid(msg, GSM48_IE_CALLING_BCD, 14, calling);
417 }
418
419 /* decode 'connected number' */
420 int gsm48_decode_connected(struct gsm_mncc_number *connected,
421                          const uint8_t *lv)
422 {
423         return gsm48_decode_callerid(connected, lv);
424 }
425
426 /* encode 'connected number' */
427 int gsm48_encode_connected(struct msgb *msg,
428                             const struct gsm_mncc_number *connected)
429 {
430         return gsm48_encode_callerid(msg, GSM48_IE_CONN_BCD, 14, connected);
431 }
432
433 /* decode 'redirecting number' */
434 int gsm48_decode_redirecting(struct gsm_mncc_number *redirecting,
435                          const uint8_t *lv)
436 {
437         return gsm48_decode_callerid(redirecting, lv);
438 }
439
440 /* encode 'redirecting number' */
441 int gsm48_encode_redirecting(struct msgb *msg,
442                               const struct gsm_mncc_number *redirecting)
443 {
444         return gsm48_encode_callerid(msg, GSM48_IE_REDIR_BCD, 19, redirecting);
445 }
446
447 /* decode 'facility' */
448 int gsm48_decode_facility(struct gsm_mncc_facility *facility,
449                            const uint8_t *lv)
450 {
451         uint8_t in_len = lv[0];
452
453         if (in_len < 1)
454                 return -EINVAL;
455
456         if (in_len > sizeof(facility->info))
457                 return -EINVAL;
458
459         memcpy(facility->info, lv+1, in_len);
460         facility->len = in_len;
461
462         return 0;
463 }
464
465 /* encode 'facility' */
466 int gsm48_encode_facility(struct msgb *msg, int lv_only,
467                            const struct gsm_mncc_facility *facility)
468 {
469         uint8_t lv[GSM_MAX_FACILITY + 1];
470
471         if (facility->len < 1 || facility->len > GSM_MAX_FACILITY)
472                 return -EINVAL;
473
474         memcpy(lv+1, facility->info, facility->len);
475         lv[0] = facility->len;
476         if (lv_only)
477                 msgb_lv_put(msg, lv[0], lv+1);
478         else
479                 msgb_tlv_put(msg, GSM48_IE_FACILITY, lv[0], lv+1);
480
481         return 0;
482 }
483
484 /* decode 'notify' */
485 int gsm48_decode_notify(int *notify, const uint8_t *v)
486 {
487         *notify = v[0] & 0x7f;
488
489         return 0;
490 }
491
492 /* encode 'notify' */
493 int gsm48_encode_notify(struct msgb *msg, int notify)
494 {
495         msgb_v_put(msg, notify | 0x80);
496
497         return 0;
498 }
499
500 /* decode 'signal' */
501 int gsm48_decode_signal(int *signal, const uint8_t *v)
502 {
503         *signal = v[0];
504
505         return 0;
506 }
507
508 /* encode 'signal' */
509 int gsm48_encode_signal(struct msgb *msg, int signal)
510 {
511         msgb_tv_put(msg, GSM48_IE_SIGNAL, signal);
512
513         return 0;
514 }
515
516 /* decode 'keypad' */
517 int gsm48_decode_keypad(int *keypad, const uint8_t *lv)
518 {
519         uint8_t in_len = lv[0];
520
521         if (in_len < 1)
522                 return -EINVAL;
523
524         *keypad = lv[1] & 0x7f;
525
526         return 0;
527 }
528
529 /* encode 'keypad' */
530 int gsm48_encode_keypad(struct msgb *msg, int keypad)
531 {
532         msgb_tv_put(msg, GSM48_IE_KPD_FACILITY, keypad);
533
534         return 0;
535 }
536
537 /* decode 'progress' */
538 int gsm48_decode_progress(struct gsm_mncc_progress *progress,
539                            const uint8_t *lv)
540 {
541         uint8_t in_len = lv[0];
542
543         if (in_len < 2)
544                 return -EINVAL;
545
546         progress->coding = (lv[1] & 0x60) >> 5;
547         progress->location = lv[1] & 0x0f;
548         progress->descr = lv[2] & 0x7f;
549
550         return 0;
551 }
552
553 /* encode 'progress' */
554 int gsm48_encode_progress(struct msgb *msg, int lv_only,
555                            const struct gsm_mncc_progress *p)
556 {
557         uint8_t lv[3];
558
559         lv[0] = 2;
560         lv[1] = 0x80 | ((p->coding & 0x3) << 5) | (p->location & 0xf);
561         lv[2] = 0x80 | (p->descr & 0x7f);
562         if (lv_only)
563                 msgb_lv_put(msg, lv[0], lv+1);
564         else
565                 msgb_tlv_put(msg, GSM48_IE_PROGR_IND, lv[0], lv+1);
566
567         return 0;
568 }
569
570 /* decode 'user-user' */
571 int gsm48_decode_useruser(struct gsm_mncc_useruser *uu,
572                            const uint8_t *lv)
573 {
574         uint8_t in_len = lv[0];
575         char *info = uu->info;
576         int info_len = sizeof(uu->info);
577         int i;
578
579         if (in_len < 1)
580                 return -EINVAL;
581
582         uu->proto = lv[1];
583
584         for (i = 2; i <= in_len; i++) {
585                 info_len--;
586                 if (info_len <= 1)
587                         break;
588                 *info++ = lv[i];
589         }
590         if (info_len >= 1)
591                 *info++ = '\0';
592
593         return 0;
594 }
595
596 /* encode 'useruser' */
597 int gsm48_encode_useruser(struct msgb *msg, int lv_only,
598                            const struct gsm_mncc_useruser *uu)
599 {
600         uint8_t lv[GSM_MAX_USERUSER + 2];
601
602         if (strlen(uu->info) > GSM_MAX_USERUSER)
603                 return -EINVAL;
604
605         lv[0] = 1 + strlen(uu->info);
606         lv[1] = uu->proto;
607         memcpy(lv + 2, uu->info, strlen(uu->info));
608         if (lv_only)
609                 msgb_lv_put(msg, lv[0], lv+1);
610         else
611                 msgb_tlv_put(msg, GSM48_IE_USER_USER, lv[0], lv+1);
612
613         return 0;
614 }
615
616 /* decode 'ss version' */
617 int gsm48_decode_ssversion(struct gsm_mncc_ssversion *ssv,
618                             const uint8_t *lv)
619 {
620         uint8_t in_len = lv[0];
621
622         if (in_len < 1 || in_len < sizeof(ssv->info))
623                 return -EINVAL;
624
625         memcpy(ssv->info, lv + 1, in_len);
626         ssv->len = in_len;
627
628         return 0;
629 }
630
631 /* encode 'ss version' */
632 int gsm48_encode_ssversion(struct msgb *msg,
633                            const struct gsm_mncc_ssversion *ssv)
634 {
635         uint8_t lv[GSM_MAX_SSVERSION + 1];
636
637         if (ssv->len > GSM_MAX_SSVERSION)
638                 return -EINVAL;
639
640         lv[0] = ssv->len;
641         memcpy(lv + 1, ssv->info, ssv->len);
642         msgb_tlv_put(msg, GSM48_IE_SS_VERS, lv[0], lv+1);
643
644         return 0;
645 }
646
647 /* decode 'more data' does not require a function, because it has no value */
648
649 /* encode 'more data' */
650 int gsm48_encode_more(struct msgb *msg)
651 {
652         uint8_t *ie;
653
654         ie = msgb_put(msg, 1);
655         ie[0] = GSM48_IE_MORE_DATA;
656
657         return 0;
658 }
659