aboutsummaryrefslogtreecommitdiffstats
path: root/extra/libpri/libpri-1.4.9-i14292.patch
blob: 27ed1b8d432298194c14c0d288fe89c96c4f2b0e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
2308
2309
2310
2311
2312
2313
2314
2315
2316
2317
2318
2319
2320
2321
2322
2323
2324
2325
2326
2327
2328
2329
2330
2331
2332
2333
2334
2335
2336
2337
2338
2339
2340
2341
2342
2343
2344
2345
2346
2347
2348
2349
2350
2351
2352
2353
2354
2355
2356
2357
2358
2359
2360
2361
2362
2363
2364
2365
2366
2367
2368
2369
2370
2371
2372
2373
2374
2375
2376
2377
2378
2379
2380
2381
2382
2383
2384
2385
2386
2387
2388
2389
2390
2391
2392
2393
2394
2395
2396
2397
2398
2399
2400
2401
2402
2403
2404
2405
2406
2407
2408
2409
2410
2411
2412
2413
2414
2415
2416
2417
2418
2419
2420
2421
2422
2423
2424
2425
2426
2427
2428
2429
2430
2431
2432
2433
2434
2435
2436
2437
2438
2439
2440
2441
2442
2443
2444
2445
2446
2447
2448
2449
2450
2451
2452
2453
2454
2455
2456
2457
2458
2459
2460
2461
2462
2463
2464
2465
2466
2467
2468
2469
2470
2471
2472
2473
2474
2475
2476
2477
2478
2479
2480
2481
2482
2483
2484
2485
2486
2487
2488
2489
2490
2491
2492
2493
2494
2495
2496
2497
2498
2499
2500
2501
2502
2503
2504
2505
2506
2507
2508
2509
2510
2511
2512
2513
2514
2515
2516
2517
2518
2519
2520
2521
2522
2523
2524
2525
2526
2527
2528
2529
2530
2531
2532
2533
2534
2535
2536
2537
2538
2539
2540
2541
2542
2543
2544
2545
2546
2547
2548
2549
2550
2551
2552
2553
2554
2555
2556
2557
2558
2559
2560
2561
2562
2563
2564
2565
2566
2567
2568
2569
2570
2571
2572
2573
2574
2575
2576
2577
2578
2579
2580
2581
2582
2583
2584
2585
2586
2587
2588
2589
2590
2591
2592
2593
2594
2595
2596
2597
2598
2599
2600
2601
2602
2603
2604
2605
2606
2607
2608
2609
2610
2611
2612
2613
2614
2615
2616
2617
2618
2619
2620
2621
2622
2623
2624
2625
2626
2627
2628
2629
2630
2631
2632
2633
2634
2635
2636
2637
2638
2639
2640
2641
2642
2643
2644
2645
2646
2647
2648
2649
2650
2651
2652
2653
2654
2655
2656
2657
2658
2659
2660
2661
2662
2663
2664
2665
2666
2667
2668
2669
2670
2671
2672
2673
2674
2675
2676
2677
2678
2679
2680
2681
2682
2683
2684
2685
2686
2687
2688
2689
2690
2691
2692
2693
2694
2695
2696
2697
2698
2699
2700
2701
2702
2703
2704
2705
2706
2707
2708
2709
2710
2711
2712
2713
2714
2715
2716
2717
2718
2719
2720
2721
2722
2723
2724
2725
2726
2727
2728
2729
2730
2731
2732
2733
2734
2735
2736
2737
2738
2739
2740
2741
2742
2743
2744
2745
2746
2747
2748
2749
2750
2751
2752
2753
2754
2755
2756
2757
2758
2759
2760
2761
2762
2763
2764
2765
2766
2767
2768
2769
2770
2771
2772
2773
2774
2775
2776
2777
2778
2779
2780
2781
2782
2783
2784
2785
2786
2787
2788
2789
2790
2791
2792
2793
2794
2795
2796
2797
2798
2799
2800
2801
2802
2803
2804
2805
2806
2807
2808
2809
2810
2811
2812
2813
2814
2815
2816
2817
2818
2819
2820
2821
2822
2823
2824
2825
2826
2827
2828
2829
2830
2831
2832
2833
2834
2835
2836
2837
2838
2839
2840
2841
2842
2843
2844
2845
2846
2847
2848
2849
2850
2851
2852
2853
2854
2855
2856
2857
2858
2859
2860
2861
2862
2863
2864
2865
2866
2867
2868
2869
2870
2871
2872
2873
2874
2875
2876
2877
2878
2879
2880
2881
2882
2883
2884
2885
2886
2887
2888
2889
2890
2891
2892
2893
2894
2895
2896
2897
2898
2899
2900
2901
2902
2903
2904
2905
2906
2907
2908
2909
2910
2911
2912
2913
2914
2915
2916
2917
2918
2919
2920
2921
2922
2923
2924
2925
2926
2927
2928
2929
2930
2931
2932
2933
2934
2935
2936
2937
2938
2939
2940
2941
2942
2943
2944
2945
2946
2947
2948
2949
2950
2951
2952
2953
2954
2955
2956
2957
2958
2959
2960
2961
2962
2963
2964
2965
2966
2967
2968
2969
2970
2971
2972
2973
2974
2975
2976
2977
2978
2979
2980
2981
2982
2983
2984
2985
2986
2987
2988
2989
2990
2991
2992
2993
2994
2995
2996
2997
2998
2999
3000
3001
3002
3003
3004
3005
3006
3007
3008
3009
3010
3011
3012
3013
3014
3015
3016
3017
3018
3019
3020
3021
3022
3023
3024
3025
3026
3027
3028
3029
3030
3031
3032
3033
3034
3035
3036
3037
3038
3039
3040
3041
3042
3043
3044
3045
3046
3047
3048
3049
3050
3051
3052
3053
3054
3055
3056
3057
3058
3059
3060
3061
3062
3063
3064
3065
3066
3067
3068
3069
3070
3071
3072
3073
3074
3075
3076
3077
3078
3079
3080
3081
3082
3083
3084
3085
3086
3087
3088
3089
3090
3091
3092
3093
3094
3095
3096
3097
3098
3099
3100
3101
3102
3103
3104
3105
3106
3107
3108
3109
3110
3111
3112
3113
3114
3115
3116
3117
3118
3119
3120
3121
3122
3123
3124
3125
3126
3127
3128
3129
3130
3131
3132
3133
3134
3135
3136
3137
3138
3139
3140
3141
3142
3143
3144
3145
3146
3147
3148
3149
3150
3151
3152
3153
3154
3155
3156
3157
3158
3159
3160
3161
3162
3163
3164
3165
3166
3167
3168
3169
3170
3171
3172
3173
3174
3175
3176
3177
3178
3179
3180
3181
3182
3183
3184
3185
3186
3187
3188
3189
3190
3191
3192
3193
3194
3195
3196
3197
3198
3199
3200
3201
3202
3203
3204
3205
3206
3207
3208
3209
3210
3211
3212
3213
3214
3215
3216
3217
3218
3219
3220
3221
3222
3223
3224
3225
3226
3227
3228
3229
3230
3231
3232
3233
3234
3235
3236
3237
3238
3239
3240
3241
3242
3243
3244
3245
3246
3247
3248
3249
3250
3251
3252
3253
3254
3255
3256
3257
3258
3259
3260
3261
3262
3263
3264
3265
3266
3267
3268
3269
3270
3271
3272
3273
3274
3275
3276
3277
3278
3279
3280
3281
3282
3283
3284
3285
3286
3287
3288
3289
3290
3291
3292
3293
3294
3295
3296
3297
3298
3299
3300
3301
3302
3303
3304
3305
3306
3307
3308
3309
3310
3311
3312
3313
3314
3315
3316
3317
3318
3319
3320
3321
3322
3323
3324
3325
3326
3327
3328
3329
3330
3331
3332
3333
3334
3335
3336
3337
3338
3339
3340
3341
3342
3343
3344
3345
3346
3347
3348
3349
3350
3351
3352
3353
3354
3355
3356
3357
3358
3359
3360
3361
3362
3363
3364
3365
3366
3367
3368
3369
3370
3371
3372
3373
3374
3375
3376
3377
3378
3379
3380
3381
3382
3383
3384
3385
3386
3387
3388
3389
3390
3391
3392
3393
3394
3395
3396
3397
3398
3399
3400
3401
3402
3403
3404
3405
3406
3407
3408
3409
3410
3411
3412
3413
3414
3415
3416
3417
3418
3419
3420
3421
3422
3423
3424
3425
3426
3427
3428
3429
3430
3431
3432
3433
3434
3435
3436
3437
3438
3439
3440
3441
3442
3443
3444
3445
3446
3447
3448
3449
3450
3451
3452
3453
3454
3455
3456
3457
3458
3459
3460
3461
3462
3463
3464
3465
3466
3467
3468
3469
3470
3471
3472
3473
3474
3475
3476
3477
3478
3479
3480
3481
3482
3483
3484
3485
3486
3487
3488
3489
3490
3491
3492
3493
3494
3495
3496
3497
3498
3499
3500
3501
3502
3503
3504
3505
3506
3507
3508
3509
3510
3511
3512
3513
3514
3515
3516
3517
3518
3519
3520
3521
3522
3523
3524
3525
3526
3527
3528
3529
3530
3531
3532
3533
3534
3535
3536
3537
3538
3539
3540
3541
3542
3543
3544
3545
3546
3547
3548
3549
3550
3551
3552
3553
3554
3555
3556
3557
3558
3559
3560
3561
3562
3563
3564
3565
3566
3567
3568
3569
3570
3571
3572
3573
3574
3575
3576
3577
3578
3579
3580
3581
3582
3583
3584
3585
3586
3587
3588
3589
3590
3591
3592
3593
3594
3595
3596
3597
3598
3599
3600
3601
3602
3603
3604
3605
3606
3607
3608
3609
3610
3611
3612
3613
3614
3615
3616
3617
3618
3619
3620
3621
3622
3623
3624
3625
3626
3627
3628
3629
3630
3631
3632
3633
3634
3635
3636
3637
3638
3639
3640
3641
3642
3643
3644
3645
3646
3647
3648
3649
3650
3651
3652
3653
3654
3655
3656
3657
3658
3659
3660
3661
3662
3663
3664
3665
3666
3667
3668
3669
3670
3671
3672
3673
3674
3675
3676
3677
3678
3679
3680
3681
3682
3683
3684
3685
3686
3687
3688
3689
3690
3691
3692
3693
3694
3695
3696
3697
3698
3699
3700
3701
3702
3703
3704
3705
3706
3707
3708
3709
3710
3711
3712
3713
3714
3715
3716
3717
3718
3719
3720
3721
3722
3723
3724
3725
3726
3727
3728
3729
3730
3731
3732
3733
3734
3735
3736
3737
3738
3739
3740
3741
3742
3743
3744
3745
3746
3747
3748
3749
3750
3751
3752
3753
3754
3755
3756
3757
3758
3759
3760
3761
3762
3763
3764
3765
3766
3767
3768
3769
3770
3771
3772
3773
3774
3775
3776
3777
3778
3779
3780
3781
3782
3783
3784
3785
3786
3787
3788
3789
3790
3791
3792
3793
3794
3795
3796
3797
3798
3799
3800
3801
3802
3803
3804
3805
3806
3807
3808
3809
3810
3811
3812
3813
3814
3815
3816
3817
3818
3819
3820
3821
3822
3823
3824
3825
3826
3827
3828
3829
3830
3831
3832
3833
3834
3835
3836
3837
3838
3839
3840
3841
3842
3843
3844
3845
3846
3847
3848
3849
3850
3851
3852
3853
3854
3855
3856
3857
3858
3859
3860
3861
3862
3863
3864
3865
3866
3867
3868
3869
3870
3871
3872
3873
3874
3875
3876
3877
3878
3879
3880
3881
3882
3883
3884
3885
3886
3887
3888
3889
3890
3891
3892
3893
3894
3895
3896
3897
3898
3899
3900
3901
3902
3903
3904
3905
3906
3907
3908
3909
3910
3911
3912
3913
3914
3915
3916
3917
3918
3919
3920
3921
3922
3923
3924
3925
3926
3927
3928
3929
3930
3931
3932
3933
3934
3935
3936
3937
3938
3939
3940
3941
3942
3943
3944
3945
3946
3947
3948
3949
3950
3951
3952
3953
3954
3955
3956
3957
3958
3959
3960
3961
3962
3963
3964
3965
3966
3967
3968
3969
3970
3971
3972
3973
3974
3975
3976
3977
3978
3979
3980
3981
3982
3983
3984
3985
3986
3987
3988
3989
3990
3991
3992
3993
3994
3995
3996
3997
3998
3999
4000
4001
4002
4003
4004
4005
4006
4007
4008
4009
4010
4011
4012
4013
4014
4015
4016
4017
4018
4019
4020
4021
4022
4023
4024
4025
4026
4027
4028
4029
4030
4031
4032
4033
4034
4035
4036
4037
4038
4039
4040
4041
4042
4043
4044
4045
4046
4047
4048
4049
4050
4051
4052
4053
4054
4055
4056
4057
4058
4059
4060
4061
4062
4063
4064
4065
4066
4067
4068
4069
4070
4071
4072
4073
4074
4075
4076
4077
4078
4079
4080
4081
4082
4083
4084
4085
4086
4087
4088
4089
4090
4091
4092
4093
4094
4095
4096
4097
4098
4099
4100
4101
4102
4103
4104
4105
4106
4107
4108
4109
4110
4111
4112
4113
4114
4115
4116
4117
4118
4119
4120
4121
4122
4123
4124
4125
4126
4127
4128
4129
4130
4131
4132
4133
4134
4135
4136
4137
4138
4139
4140
4141
4142
4143
4144
4145
4146
4147
4148
4149
4150
4151
4152
4153
4154
4155
4156
4157
4158
4159
4160
4161
4162
4163
4164
4165
4166
4167
4168
4169
4170
4171
4172
4173
4174
4175
4176
4177
4178
4179
4180
4181
4182
4183
4184
4185
4186
4187
4188
4189
4190
4191
4192
4193
4194
4195
4196
4197
4198
4199
4200
4201
4202
4203
4204
4205
4206
4207
4208
4209
4210
4211
4212
4213
4214
4215
4216
4217
4218
4219
4220
4221
4222
4223
4224
4225
4226
4227
4228
4229
4230
4231
4232
4233
4234
4235
4236
4237
4238
4239
4240
4241
4242
4243
4244
4245
4246
4247
4248
4249
4250
4251
4252
4253
4254
4255
4256
4257
4258
4259
4260
4261
4262
4263
4264
4265
4266
4267
4268
4269
4270
4271
4272
4273
4274
4275
4276
4277
4278
4279
4280
4281
4282
4283
4284
4285
4286
4287
4288
4289
4290
4291
4292
4293
4294
4295
4296
4297
4298
4299
4300
4301
4302
4303
4304
4305
4306
4307
4308
4309
4310
4311
4312
4313
4314
4315
4316
4317
4318
4319
4320
4321
4322
4323
4324
4325
4326
4327
4328
4329
4330
4331
4332
4333
4334
4335
4336
4337
4338
4339
4340
4341
4342
4343
4344
4345
4346
4347
4348
4349
4350
4351
4352
4353
4354
4355
4356
4357
4358
4359
4360
4361
4362
4363
4364
4365
4366
4367
4368
4369
4370
4371
4372
4373
4374
4375
4376
4377
4378
4379
4380
4381
4382
4383
4384
4385
4386
4387
4388
4389
4390
4391
4392
4393
4394
4395
4396
4397
4398
4399
4400
4401
4402
4403
4404
4405
4406
4407
4408
4409
4410
4411
4412
4413
4414
4415
4416
4417
4418
4419
4420
4421
4422
4423
4424
4425
4426
4427
4428
4429
4430
4431
4432
4433
4434
4435
4436
4437
4438
4439
4440
4441
4442
4443
4444
4445
4446
4447
4448
4449
4450
4451
4452
4453
4454
4455
4456
4457
4458
4459
4460
4461
4462
4463
4464
4465
4466
4467
4468
4469
4470
4471
4472
4473
4474
4475
4476
4477
4478
4479
4480
4481
4482
4483
4484
4485
4486
4487
4488
4489
4490
4491
4492
4493
4494
4495
4496
4497
4498
4499
4500
4501
4502
4503
4504
4505
4506
4507
4508
4509
4510
4511
4512
4513
4514
4515
4516
4517
4518
4519
4520
4521
4522
4523
4524
4525
4526
4527
4528
4529
4530
4531
4532
4533
4534
4535
4536
4537
4538
4539
4540
4541
4542
4543
4544
4545
4546
4547
4548
4549
4550
4551
4552
4553
4554
4555
4556
4557
4558
4559
4560
4561
4562
4563
4564
4565
4566
4567
4568
4569
4570
4571
4572
4573
4574
4575
4576
4577
4578
4579
4580
4581
4582
4583
4584
4585
4586
4587
4588
4589
4590
4591
4592
4593
4594
4595
4596
4597
4598
4599
4600
4601
4602
4603
4604
4605
4606
4607
4608
4609
4610
4611
4612
4613
4614
4615
4616
4617
4618
4619
4620
4621
4622
4623
4624
4625
4626
4627
4628
4629
4630
4631
4632
4633
4634
4635
4636
4637
4638
4639
4640
4641
4642
4643
4644
4645
4646
4647
4648
4649
4650
4651
4652
4653
4654
4655
4656
4657
4658
4659
4660
4661
4662
4663
4664
4665
4666
4667
4668
4669
4670
4671
4672
4673
4674
4675
4676
4677
4678
4679
4680
4681
4682
4683
4684
4685
4686
4687
4688
4689
4690
4691
4692
4693
4694
4695
4696
4697
4698
4699
4700
4701
4702
4703
4704
4705
4706
4707
4708
4709
4710
4711
4712
4713
4714
4715
4716
4717
4718
4719
4720
4721
4722
4723
4724
4725
4726
4727
4728
4729
4730
4731
4732
4733
4734
4735
4736
4737
4738
4739
4740
4741
4742
4743
4744
4745
4746
4747
4748
4749
4750
4751
4752
4753
4754
4755
4756
4757
4758
4759
4760
4761
4762
4763
4764
4765
4766
4767
4768
4769
4770
4771
4772
4773
4774
4775
4776
4777
4778
4779
4780
4781
4782
4783
4784
4785
4786
4787
4788
4789
4790
4791
4792
4793
4794
4795
4796
4797
4798
4799
4800
4801
4802
4803
4804
4805
4806
4807
4808
4809
4810
4811
4812
4813
4814
4815
4816
4817
4818
4819
4820
4821
4822
4823
4824
4825
4826
4827
4828
4829
4830
4831
4832
4833
4834
4835
4836
4837
4838
4839
4840
4841
4842
4843
4844
4845
4846
4847
4848
4849
4850
4851
4852
4853
4854
4855
4856
4857
4858
4859
4860
4861
4862
4863
4864
4865
4866
4867
4868
4869
4870
4871
4872
4873
4874
4875
4876
4877
4878
4879
4880
4881
4882
4883
4884
4885
4886
4887
4888
4889
4890
4891
4892
4893
4894
4895
4896
4897
4898
4899
4900
4901
4902
4903
4904
4905
4906
4907
4908
4909
4910
4911
4912
4913
4914
4915
4916
4917
4918
4919
4920
4921
4922
4923
4924
4925
4926
4927
4928
4929
4930
4931
4932
4933
4934
4935
4936
4937
4938
4939
4940
4941
4942
4943
4944
4945
4946
4947
4948
4949
4950
4951
4952
4953
4954
4955
4956
4957
4958
4959
4960
4961
4962
4963
4964
4965
4966
4967
4968
4969
4970
4971
4972
4973
4974
4975
4976
4977
4978
4979
4980
4981
4982
4983
4984
4985
4986
4987
4988
4989
4990
4991
4992
4993
4994
4995
4996
4997
4998
4999
5000
5001
5002
5003
5004
5005
5006
5007
5008
5009
5010
5011
5012
5013
5014
5015
5016
5017
5018
5019
5020
5021
5022
5023
5024
5025
5026
5027
5028
5029
5030
5031
5032
5033
5034
5035
5036
5037
5038
5039
5040
5041
5042
5043
5044
5045
5046
5047
5048
5049
5050
5051
5052
5053
5054
5055
5056
5057
5058
5059
5060
5061
5062
5063
5064
5065
5066
5067
5068
5069
5070
5071
5072
5073
5074
5075
5076
5077
5078
5079
5080
5081
5082
5083
5084
5085
5086
5087
5088
5089
5090
5091
5092
5093
5094
5095
5096
5097
5098
5099
5100
5101
5102
5103
5104
5105
5106
5107
5108
5109
5110
5111
5112
5113
5114
5115
5116
5117
5118
5119
5120
5121
5122
5123
5124
5125
5126
5127
5128
5129
5130
5131
5132
5133
5134
5135
5136
5137
5138
5139
5140
5141
5142
5143
5144
5145
5146
5147
5148
5149
5150
5151
5152
5153
5154
5155
5156
5157
5158
5159
5160
5161
5162
5163
5164
5165
5166
5167
5168
5169
5170
5171
5172
5173
5174
5175
5176
5177
5178
5179
5180
5181
5182
5183
5184
5185
5186
5187
5188
5189
5190
5191
5192
5193
5194
5195
5196
5197
5198
5199
5200
5201
5202
5203
5204
5205
5206
5207
5208
5209
5210
5211
5212
5213
5214
5215
5216
5217
5218
5219
5220
5221
5222
5223
5224
5225
5226
5227
5228
5229
5230
5231
5232
5233
5234
5235
5236
5237
5238
5239
5240
5241
5242
5243
5244
5245
5246
5247
5248
5249
5250
5251
5252
5253
5254
5255
5256
5257
5258
5259
5260
5261
5262
5263
5264
5265
5266
5267
5268
5269
5270
5271
5272
5273
5274
5275
5276
5277
5278
5279
5280
5281
5282
5283
5284
5285
5286
5287
5288
5289
5290
5291
5292
5293
5294
5295
5296
5297
5298
5299
5300
5301
5302
5303
5304
5305
5306
5307
5308
5309
5310
5311
5312
5313
5314
5315
5316
5317
5318
5319
5320
5321
5322
5323
5324
5325
5326
5327
5328
5329
5330
5331
5332
5333
5334
5335
5336
5337
5338
5339
5340
5341
5342
5343
5344
5345
5346
5347
5348
5349
5350
5351
5352
5353
5354
5355
5356
5357
5358
5359
5360
5361
5362
5363
5364
5365
5366
5367
5368
5369
5370
5371
5372
5373
5374
5375
5376
5377
5378
5379
5380
5381
5382
5383
5384
5385
5386
5387
5388
5389
5390
5391
5392
5393
5394
5395
5396
5397
5398
5399
5400
5401
5402
5403
5404
5405
5406
5407
5408
5409
5410
5411
5412
5413
5414
5415
5416
5417
5418
5419
5420
5421
5422
5423
5424
5425
5426
5427
5428
5429
5430
5431
5432
5433
5434
5435
5436
5437
5438
5439
5440
5441
5442
5443
5444
5445
5446
5447
5448
5449
5450
5451
5452
5453
5454
5455
5456
5457
5458
5459
5460
5461
5462
5463
5464
5465
5466
5467
5468
5469
5470
5471
5472
5473
5474
5475
5476
5477
5478
5479
5480
5481
5482
5483
5484
5485
5486
5487
5488
5489
5490
5491
5492
5493
5494
5495
5496
5497
5498
5499
5500
5501
5502
5503
5504
5505
5506
5507
5508
5509
5510
5511
5512
5513
5514
5515
5516
5517
5518
5519
5520
5521
5522
5523
5524
5525
5526
5527
5528
5529
5530
5531
5532
5533
5534
5535
5536
5537
5538
5539
5540
5541
5542
5543
5544
5545
5546
5547
5548
5549
5550
5551
5552
5553
5554
5555
5556
5557
5558
5559
5560
5561
5562
5563
5564
5565
5566
5567
5568
5569
5570
5571
5572
5573
5574
5575
5576
5577
5578
5579
5580
5581
5582
5583
5584
5585
5586
5587
5588
5589
5590
5591
5592
5593
5594
5595
5596
5597
5598
5599
5600
5601
5602
5603
5604
5605
5606
5607
5608
5609
5610
5611
5612
5613
5614
5615
5616
5617
5618
5619
5620
5621
5622
5623
5624
5625
5626
5627
5628
5629
5630
5631
5632
5633
5634
5635
5636
5637
5638
5639
5640
5641
5642
5643
5644
5645
5646
5647
5648
5649
5650
5651
5652
5653
5654
5655
5656
5657
5658
5659
5660
5661
5662
5663
5664
5665
5666
5667
5668
Index: .version
===================================================================
--- a/.version	(.../tags/1.4.9)	(revision 700)
+++ b/.version	(.../team/group/issue14292)	(revision 700)
@@ -1 +1 @@
-1.4.9
+1.4.9-issue14292
Index: pri_facility.c
===================================================================
--- a/pri_facility.c	(.../tags/1.4.9)	(revision 700)
+++ b/pri_facility.c	(.../team/group/issue14292)	(revision 700)
@@ -171,7 +171,7 @@
 
 struct addressingdataelements_presentednumberunscreened {
 	char partyaddress[21];
-	char partysubaddress[21];
+	char notused[21];
 	int  npi;       /* Numbering Plan Indicator */
 	int  ton;       /* Type Of Number */
 	int  pres;      /* Presentation */
@@ -179,6 +179,15 @@
 
 struct addressingdataelements_presentednumberscreened {
 	char partyaddress[21];
+	char notused[21];
+	int  npi;       /* Numbering Plan Indicator */
+	int  ton;       /* Type Of Number */
+	int  pres;      /* Presentation */
+	int  scrind;    /* Screening Indicator */
+};
+
+struct addressingdataelements_presentedaddressscreened {
+	char partyaddress[21];
 	char partysubaddress[21];
 	int  npi;       /* Numbering Plan Indicator */
 	int  ton;       /* Type Of Number */
@@ -186,6 +195,35 @@
 	int  scrind;    /* Screening Indicator */
 };
 
+struct addressingdataelements_addressscreened {
+	char partyaddress[21];
+	char partysubaddress[21];
+	int  npi;       /* Numbering Plan Indicator */
+	int  ton;       /* Type Of Number */
+	int  notused;
+	int  scrind;    /* Screening Indicator */
+};
+
+struct addressingdataelements_partysubaddress {
+	char notused[21];
+	char partysubaddress[21];
+};
+
+struct nameelements_name {
+	char name[51];
+	int  characterset;
+	int  namepres;
+};
+
+struct nameelements_nameset {
+	char name[51];
+	int  characterset;
+};
+
+struct nameelements_namedata {
+	char name[51];
+};
+
 #define PRI_CHECKOVERFLOW(size) \
 		if (msgptr - message + (size) >= sizeof(message)) { \
 			*msgptr = '\0'; \
@@ -225,6 +263,92 @@
 }
 #undef PRI_CHECKOVERFLOW
 
+static const char *namepres_to_str(int namepres)
+{
+	return (namepres == 0) ? "Restricted" : "Allowed";
+}
+
+static const char *characterset_to_str(int characterset)
+{
+	switch (characterset) {
+	case CHARACTER_SET_UNKNOWN:
+		return "Unknown";
+	case CHARACTER_SET_ISO8859_1:
+		return "ISO8859-1";
+	case CHARACTER_SET_ISO8859_2:
+		return "ISO8859-2";
+	case CHARACTER_SET_ISO8859_3:
+		return "ISO8859-3";
+	case CHARACTER_SET_ISO8859_4:
+		return "ISO8859-4";
+	case CHARACTER_SET_ISO8859_5:
+		return "ISO8859-5";
+	case CHARACTER_SET_ISO8859_7:
+		return "ISO8859-7";
+	default:
+		return "illegal value";
+	}
+}
+
+static const char *diversionreason_to_str(struct pri *pri, int diversionreason)
+{
+	if (pri->switchtype == PRI_SWITCH_QSIG) {
+		switch (diversionreason) {
+		case QSIG_DIVERT_REASON_UNKNOWN:
+			return "Unknown";
+		case QSIG_DIVERT_REASON_CFU:
+			return "Call Forwarding Unconditional";
+		case QSIG_DIVERT_REASON_CFB:
+			return "Call Forwarding Busy";
+		case QSIG_DIVERT_REASON_CFNR:
+			return "Call Forwarding No Reply";
+		default:
+			return "invalid value";
+		}
+	} else {
+		switch(diversionreason) {
+		case Q952_DIVERT_REASON_UNKNOWN:
+			return "Unknown";
+		case Q952_DIVERT_REASON_CFU:
+			return "Call Forwarding Unconditional";
+		case Q952_DIVERT_REASON_CFB:
+			return "Call Forwarding Busy";
+		case Q952_DIVERT_REASON_CFNR:
+			return "Call Forwarding No Reply";
+		case Q952_DIVERT_REASON_CD:
+			return "Call Deflection";
+		case Q952_DIVERT_REASON_IMMEDIATE:
+			return "Call Deflection Immediate";
+		default:
+			return "invalid value";
+		}
+	}
+}
+
+static const char *callstatus_to_str(int callstatus)
+{
+	switch (callstatus) {
+	case 0:
+		return "answered";
+	case 1:
+		return "alerting";
+	default:
+		return "illegal value";
+	}
+}
+
+static const char *enddesignation_to_str(int enddesignation)
+{
+	switch (enddesignation) {
+	case 0:
+		return "primaryEnd";
+	case 1:
+		return "secondaryEnd";
+	default:
+		return "illegal value";
+	}
+}
+
 int redirectingreason_from_q931(struct pri *pri, int redirectingreason)
 {
 	switch(pri->switchtype) {
@@ -350,8 +474,25 @@
 	}
 }
 
-int asn1_name_decode(void * data, int len, char *namebuf, int buflen)
+static int presentation_to_subscription(struct pri *pri, int presentation)
 {
+	/* derive subscription value from presentation value */
+
+	switch (presentation & PRES_RESTRICTION) {
+	case PRES_ALLOWED:
+		return QSIG_NOTIFICATION_WITH_DIVERTED_TO_NR;
+	case PRES_RESTRICTED:
+		return QSIG_NOTIFICATION_WITHOUT_DIVERTED_TO_NR;
+	case PRES_UNAVAILABLE:			/* Number not available due to interworking */
+		return QSIG_NOTIFICATION_WITHOUT_DIVERTED_TO_NR;	/* ?? QSIG_NO_NOTIFICATION */
+	default:
+		pri_message(pri, "!! Unknown Q.SIG presentationIndicator 0x%02x\n", presentation);
+		return QSIG_NOTIFICATION_WITHOUT_DIVERTED_TO_NR;
+	}
+}
+
+int asn1_name_decode(void *data, int len, char *namebuf, int buflen)
+{
 	struct rose_component *comp = (struct rose_component*)data;
 	int datalen = 0, res = 0;
 
@@ -361,11 +502,13 @@
 	} else
 		datalen = res = comp->len;
 
-	if (datalen > buflen) {
+	if (datalen > buflen - 1) {
 		/* Truncate */
 		datalen = buflen;
 	}
 	memcpy(namebuf, comp->data, datalen);
+	namebuf[datalen] = '\0';
+
 	return res + 2;
 }
 
@@ -407,42 +550,230 @@
 	return res;
 }
 
-static int rose_number_digits_decode(struct pri *pri, q931_call *call, unsigned char *data, int len, struct addressingdataelements_presentednumberunscreened *value)
+static int rose_namedata_decode(struct pri *pri, unsigned char *data, int len, int implicit, struct nameelements_namedata *value)
 {
 	int i = 0;
 	struct rose_component *comp = NULL;
 	unsigned char *vdata = data;
-	int datalen = 0;
-	int res = 0;
+	int res;
 
 	do {
+		/* NameData */
+		if (!implicit) {
+			GET_COMPONENT(comp, i, vdata, len);
+			CHECK_COMPONENT(comp, ASN1_OCTETSTRING, "Don't know what to do if NameData is of type 0x%x\n");
+
+			data = comp->data;
+			if (comp->len == ASN1_LEN_INDEF) {
+				len = strlen((char *)comp->data);
+				res = len + 2 + 2;
+			} else {
+				len = comp->len;
+				res = len + 2;
+			}
+		} else
+			res = len;
+
+		if (len > sizeof(value->name)-1) {
+			pri_message(pri, "!! Oversized NameData component (%d)\n", len);
+			return -1;
+		}
+
+		memcpy(value->name, data, len);
+		value->name[len] = '\0';
+
+		return res;
+	}
+	while(0);
+
+	return -1;
+}
+
+static int rose_namedata_encode(struct pri *pri, unsigned char *dst, int implicit, char *name)
+{
+	int size = 0;
+	struct rose_component *comp;
+	int namesize;
+
+	namesize = strlen(name);
+	if (namesize > 50 ) {
+		pri_message(pri, "!! Encoding of oversized NameData component failed (%d)\n", namesize);
+		return -1;
+	} else if (namesize == 0){
+		pri_message(pri, "!! Encoding of undersized NameData component failed (%d)\n", namesize);
+		return -1;
+	}
+
+	if (!implicit) {
+		/* constructor component  (0x04,len) */
+		comp = (struct rose_component *)dst;
+		comp->type = ASN1_OCTETSTRING;
+		comp->len = 2 + namesize;
+		size += 2;
+		dst += 2;
+	}
+
+	memcpy(dst, name, namesize);
+	size += namesize;
+
+	return size;
+}
+
+static int rose_nameset_decode(struct pri *pri, unsigned char *data, int len, int implicit, struct nameelements_nameset *value)
+{
+	int size;
+	int i = 0;
+	struct rose_component *comp = NULL;
+	unsigned char *vdata = data;
+ 	int characterset;
+
+	value->characterset = CHARACTER_SET_ISO8859_1;
+
+	do {
+		/* NameSet */
+		if (!implicit) {
+			GET_COMPONENT(comp, i, vdata, len);
+			CHECK_COMPONENT(comp, ASN1_SEQUENCE, "Don't know what to do if NameSet is of type 0x%x\n");
+			SUB_COMPONENT(comp, i);
+		}
+
+		/* nameData NameData */
 		GET_COMPONENT(comp, i, vdata, len);
-		CHECK_COMPONENT(comp, ASN1_NUMERICSTRING, "Don't know what to do with PublicPartyNumber ROSE component type 0x%x\n");
-		if(comp->len > 20 && comp->len != ASN1_LEN_INDEF) {
-			pri_message(pri, "!! Oversized NumberDigits component (%d)\n", comp->len);
+		size = rose_namedata_decode(pri, (u_int8_t *)comp, len, 0, (struct nameelements_namedata *)value);
+		if (size < 0)
 			return -1;
+		i += size;
+
+		if (i < len) {
+			/* characterSet CharacterSet OPTIONAL */
+			GET_COMPONENT(comp, i, vdata, len);
+			CHECK_COMPONENT(comp, ASN1_INTEGER, "Don't know what to do if CharacterSet is of type 0x%x\n");
+			ASN1_GET_INTEGER(comp, characterset);
+			NEXT_COMPONENT(comp, i);
+			if (pri->debug & PRI_DEBUG_APDU)
+				pri_message(pri, "     NameSet: Received characterSet=%s(%d)\n", characterset_to_str(characterset), characterset);
+			value->characterset = characterset;
 		}
-		if (comp->len == ASN1_LEN_INDEF) {
-			datalen = strlen((char *)comp->data);
-			res = datalen + 2;
-		} else
-			res = datalen = comp->len;
-			
-		memcpy(value->partyaddress, comp->data, datalen);
-		value->partyaddress[datalen] = '\0';
 
-		return res + 2;
+		if (pri->debug & PRI_DEBUG_APDU)
+			pri_message(pri, "     NameSet: '%s', characterSet=%s(%d) i=%d len=%d\n", value->name, characterset_to_str(value->characterset), value->characterset, i, len);
+
+		return i;
 	}
 	while(0);
 	
 	return -1;
 }
 
-static int rose_public_party_number_decode(struct pri *pri, q931_call *call, unsigned char *data, int len, struct addressingdataelements_presentednumberunscreened *value)
+static int rose_name_decode(struct pri *pri, unsigned char *data, int len, struct nameelements_name *value)
 {
 	int i = 0;
+	int size = 0;
 	struct rose_component *comp = NULL;
 	unsigned char *vdata = data;
+
+	value->name[0] = '\0';
+	value->characterset = CHARACTER_SET_UNKNOWN;
+	value->namepres = -1;
+
+	do {
+		GET_COMPONENT(comp, i, vdata, len);
+
+		switch(comp->type) {
+		case (ASN1_CONTEXT_SPECIFIC | ASN1_TAG_0):			/* [0] namePresentationAllowedSimple */
+			size = rose_namedata_decode(pri, comp->data, comp->len, 1, (struct nameelements_namedata *)value);
+			if (size < 0)
+				return -1;
+			i += (size + 2);
+			value->characterset = CHARACTER_SET_ISO8859_1;
+			value->namepres = 1;
+			break;
+		case (ASN1_CONTEXT_SPECIFIC | ASN1_CONSTRUCTOR | ASN1_TAG_1):	/* [1] namePresentationAllowedExtended */
+			size = rose_nameset_decode(pri, comp->data, comp->len, 1, (struct nameelements_nameset *)value);
+			if (size < 0)
+				return -1;
+			i += (size + 2);
+			value->namepres = 1;
+			break;
+		case (ASN1_CONTEXT_SPECIFIC | ASN1_TAG_2):			/* [2] namePresentationRestrictedSimple */
+			size = rose_namedata_decode(pri, comp->data, comp->len, 1, (struct nameelements_namedata *)value);
+			if (size < 0)
+				return -1;
+			i += (size + 2);
+			value->characterset = CHARACTER_SET_ISO8859_1;
+			value->namepres = 0;
+			break;
+		case (ASN1_CONTEXT_SPECIFIC | ASN1_CONSTRUCTOR | ASN1_TAG_3):	/* [3] namePresentationRestrictedExtended */
+			size = rose_nameset_decode(pri, comp->data, comp->len, 1, (struct nameelements_nameset *)value);
+			if (size < 0)
+				return -1;
+			i += (size + 2);
+			value->namepres = 0;
+			break;
+		case (ASN1_CONTEXT_SPECIFIC | ASN1_TAG_4):			/* [4] nameNotAvailable */
+		case (ASN1_CONTEXT_SPECIFIC | ASN1_TAG_7):			/* [7] namePresentationRestrictedNull */
+			i += (comp->len + 2);
+			value->name[0] = '\0';
+			value->characterset = CHARACTER_SET_UNKNOWN;
+			value->namepres = 0;
+			break;
+		default:
+			pri_message(pri, "!! Unknown Name component received 0x%x\n", comp->type);
+			return -1;
+		}
+
+		if (pri->debug & PRI_DEBUG_APDU)
+			pri_message(pri, "     Name: '%s' i=%d len=%d\n", value->name, i, len);
+		return i;
+	}
+	while (0);
+
+	return -1;
+}
+
+static int rose_number_digits_decode(struct pri *pri, unsigned char *data, int len, int implicit, struct addressingdataelements_presentednumberunscreened *value)
+{
+	int i = 0;
+	struct rose_component *comp = NULL;
+	unsigned char *vdata = data;
+	int res = 0;
+
+	do {
+		if (!implicit) {
+			GET_COMPONENT(comp, i, vdata, len);
+			CHECK_COMPONENT(comp, ASN1_NUMERICSTRING, "Don't know what to do with NumberDigits ROSE component type 0x%x\n");
+
+			data = comp->data;
+			if (comp->len == ASN1_LEN_INDEF) {
+				len = strlen((char *)comp->data);
+				res = len + 2 + 2;
+			} else {
+				len = comp->len;
+				res = len + 2;
+			}
+		} else
+			res = len;
+
+		if (len > sizeof(value->partyaddress)-1) {
+			pri_message(pri, "!! Oversized NumberDigits component (%d)\n", len);
+			return -1;
+		}
+
+		memcpy(value->partyaddress, data, len);
+		value->partyaddress[len] = '\0';
+
+		return res;
+	}
+	while(0);
+
+	return -1;
+}
+
+static int rose_public_party_number_decode(struct pri *pri, unsigned char *data, int len, int implicit, struct addressingdataelements_presentednumberunscreened *value)
+{
+	int i = 0;
+	struct rose_component *comp = NULL;
+	unsigned char *vdata = data;
 	int ton;
 	int res = 0;
 
@@ -450,13 +781,19 @@
 		return -1;
 
 	do {
+		if (!implicit) {
+			GET_COMPONENT(comp, i, vdata, len);
+			CHECK_COMPONENT(comp, ASN1_SEQUENCE, "Don't know what to do if PublicPartyNumber is of type 0x%x\n");
+			SUB_COMPONENT(comp, i);
+		}
+
 		GET_COMPONENT(comp, i, vdata, len);
 		CHECK_COMPONENT(comp, ASN1_ENUMERATED, "Don't know what to do with PublicPartyNumber ROSE component type 0x%x\n");
 		ASN1_GET_INTEGER(comp, ton);
 		NEXT_COMPONENT(comp, i);
 		ton = typeofnumber_for_q931(pri, ton);
 
-		res = rose_number_digits_decode(pri, call, &vdata[i], len-i, value);
+		res = rose_number_digits_decode(pri, &vdata[i], len-i, 0, value);
 		if (res < 0)
 			return -1;
 		value->ton = ton;
@@ -467,8 +804,48 @@
 	return -1;
 }
 
-static int rose_private_party_number_decode(struct pri *pri, q931_call *call, unsigned char *data, int len, struct addressingdataelements_presentednumberunscreened *value)
+static int rose_public_party_number_encode(struct pri *pri, unsigned char *dst, int implicit, unsigned char ton, char *num)
 {
+	int i = 0, compsp = 0;
+	struct rose_component *comp, *compstk[10];
+	int numsize;
+
+	numsize = strlen(num);
+	if (numsize > 20 ) {
+		pri_message(pri, "!! Encoding of oversized PublicPartyNumber component failed (%d)\n", numsize);
+		return -1;
+	}
+
+	if (!implicit) {
+		/* constructor component  (0x30,len) */
+		ASN1_ADD_SIMPLE(comp, (ASN1_CONSTRUCTOR | ASN1_SEQUENCE), dst, i);
+		ASN1_PUSH(compstk, compsp, comp);
+	} else
+		comp = (struct rose_component *)dst;
+
+	/* publicTypeOfNumber   (0x0a,0x01,ton)*/
+	ASN1_ADD_BYTECOMP(comp, ASN1_ENUMERATED, dst, i, ton);
+
+	/* publicNumberDigits */
+
+	/* tag component NumericString  (0x12,len) */
+	ASN1_ADD_SIMPLE(comp, ASN1_NUMERICSTRING, dst, i);
+	ASN1_PUSH(compstk, compsp, comp);
+
+	/* NumericString */
+	memcpy(comp->data, num, numsize);
+	i += numsize;
+
+	ASN1_FIXUP(compstk, compsp, dst, i);
+
+	if (!implicit)
+		ASN1_FIXUP(compstk, compsp, dst, i);
+
+	return i;
+}
+
+static int rose_private_party_number_decode(struct pri *pri, unsigned char *data, int len, int implicit, struct addressingdataelements_presentednumberunscreened *value)
+{
 	int i = 0;
 	struct rose_component *comp = NULL;
 	unsigned char *vdata = data;
@@ -476,18 +853,24 @@
 	int res = 0;
 
 	if (len < 2)
-	return -1;
+		return -1;
 
 	do {
+		if (!implicit) {
+			GET_COMPONENT(comp, i, vdata, len);
+			CHECK_COMPONENT(comp, ASN1_SEQUENCE, "Don't know what to do if PrivatePartyNumber is of type 0x%x\n");
+			SUB_COMPONENT(comp, i);
+		}
+
 		GET_COMPONENT(comp, i, vdata, len);
 		CHECK_COMPONENT(comp, ASN1_ENUMERATED, "Don't know what to do with PrivatePartyNumber ROSE component type 0x%x\n");
 		ASN1_GET_INTEGER(comp, ton);
 		NEXT_COMPONENT(comp, i);
 		ton = typeofnumber_for_q931(pri, ton);
 
-		res = rose_number_digits_decode(pri, call, &vdata[i], len-i, value);
+		res = rose_number_digits_decode(pri, &vdata[i], len-i, 0, value);
 		if (res < 0)
-		  return -1;
+			return -1;
 		value->ton = ton;
 
 		return res + 3;
@@ -496,7 +879,7 @@
 	return -1;
 }
 
-static int rose_address_decode(struct pri *pri, q931_call *call, unsigned char *data, int len, struct addressingdataelements_presentednumberunscreened *value)
+static int rose_address_decode(struct pri *pri, unsigned char *data, int len, struct addressingdataelements_presentednumberunscreened *value)
 {
 	int i = 0;
 	struct rose_component *comp = NULL;
@@ -506,65 +889,59 @@
 	do {
 		GET_COMPONENT(comp, i, vdata, len);
 
+		/* PartyNumber */
 		switch(comp->type) {
-		case (ASN1_CONTEXT_SPECIFIC | ASN1_CONSTRUCTOR | ASN1_TAG_0):	/* [0] unknownPartyNumber */
-			res = rose_number_digits_decode(pri, call, comp->data, comp->len, value);
+		case (ASN1_CONTEXT_SPECIFIC | ASN1_TAG_0):	/* [0] unknownPartyNumber, IMPLICIT NumberDigits */
+			res = rose_number_digits_decode(pri, comp->data, comp->len, 1, value);
 			if (res < 0)
 				return -1;
 			value->npi = PRI_NPI_UNKNOWN;
 			value->ton = PRI_TON_UNKNOWN;
 			break;
-		case (ASN1_CONTEXT_SPECIFIC | ASN1_TAG_0):	/* [0] unknownPartyNumber */
-			res = asn1_copy_string(value->partyaddress, sizeof(value->partyaddress), comp);
+		case (ASN1_CONTEXT_SPECIFIC | ASN1_CONSTRUCTOR | ASN1_TAG_1):	/* [1] publicPartyNumber, IMPLICIT PublicPartyNumber */
+			res = rose_public_party_number_decode(pri, comp->data, comp->len, 1, value);
 			if (res < 0)
 				return -1;
-			value->npi = PRI_NPI_UNKNOWN;
-			value->ton = PRI_TON_UNKNOWN;
+			value->npi = PRI_NPI_E163_E164;
 			break;
-		case (ASN1_CONTEXT_SPECIFIC | ASN1_CONSTRUCTOR | ASN1_TAG_1):	/* [1] publicPartyNumber */
-			res = rose_public_party_number_decode(pri, call, comp->data, comp->len, value);
+		case (ASN1_CONTEXT_SPECIFIC | ASN1_TAG_3):	/* [3] dataPartyNumber, IMPLICIT NumberDigits */
+			res = rose_number_digits_decode(pri, comp->data, comp->len, 1, value);
 			if (res < 0)
 				return -1;
-			value->npi = PRI_NPI_E163_E164;
-			break;
-		case (ASN1_CONTEXT_SPECIFIC | ASN1_CONSTRUCTOR | ASN1_TAG_2):	/* [2] nsapEncodedNumber */
-			pri_message(pri, "!! NsapEncodedNumber isn't handled\n");
-			return -1;
-		case (ASN1_CONTEXT_SPECIFIC | ASN1_CONSTRUCTOR | ASN1_TAG_3):	/* [3] dataPartyNumber */
-			if(rose_number_digits_decode(pri, call, comp->data, comp->len, value))
-				return -1;
 			value->npi = PRI_NPI_X121 /* ??? */;
 			value->ton = PRI_TON_UNKNOWN /* ??? */;
 			pri_message(pri, "!! dataPartyNumber isn't handled\n");
 			return -1;
-		case (ASN1_CONTEXT_SPECIFIC | ASN1_CONSTRUCTOR | ASN1_TAG_4):	/* [4] telexPartyNumber */
-			res = rose_number_digits_decode(pri, call, comp->data, comp->len, value);
+		case (ASN1_CONTEXT_SPECIFIC | ASN1_TAG_4):	/* [4] telexPartyNumber, IMPLICIT NumberDigits */
+			res = rose_number_digits_decode(pri, comp->data, comp->len, 1, value);
 			if (res < 0)
 				return -1;
 			value->npi = PRI_NPI_F69 /* ??? */;
 			value->ton = PRI_TON_UNKNOWN /* ??? */;
 			pri_message(pri, "!! telexPartyNumber isn't handled\n");
 			return -1;
-		case (ASN1_CONTEXT_SPECIFIC | ASN1_CONSTRUCTOR | ASN1_TAG_5):	/* [5] priavePartyNumber */
-			res = rose_private_party_number_decode(pri, call, comp->data, comp->len, value);
+		case (ASN1_CONTEXT_SPECIFIC | ASN1_CONSTRUCTOR | ASN1_TAG_5):	/* [5] privatePartyNumber, IMPLICIT PrivatePartyNumber */
+			res = rose_private_party_number_decode(pri, comp->data, comp->len, 1, value);
 			if (res < 0)
-			return -1;
+				return -1;
 			value->npi = PRI_NPI_PRIVATE;
 			break;
-		case (ASN1_CONTEXT_SPECIFIC | ASN1_CONSTRUCTOR | ASN1_TAG_8):	/* [8] nationalStandardPartyNumber */
-			res = rose_number_digits_decode(pri, call, comp->data, comp->len, value);
+		case (ASN1_CONTEXT_SPECIFIC | ASN1_TAG_8):	/* [8] nationalStandardPartyNumber, IMPLICIT NumberDigits */
+			res = rose_number_digits_decode(pri, comp->data, comp->len, 1, value);
 			if (res < 0)
 				return -1;
 			value->npi = PRI_NPI_NATIONAL;
 			value->ton = PRI_TON_NATIONAL;
 			break;
 		default:
-			pri_message(pri, "!! Unknown Party number component received 0x%X\n", comp->type);
+			pri_message(pri, "!! Unknown PartyNumber component received 0x%X\n", comp->type);
 			return -1;
 		}
 		ASN1_FIXUP_LEN(comp, res);
 		NEXT_COMPONENT(comp, i);
-		if(i < len)
+
+		/* PartySubaddress OPTIONAL */
+		if (i < len)
 			pri_message(pri, "!! not all information is handled from Address component\n");
 		return res + 2;
 	}
@@ -573,14 +950,176 @@
 	return -1;
 }
 
-static int rose_presented_number_unscreened_decode(struct pri *pri, q931_call *call, unsigned char *data, int len, struct addressingdataelements_presentednumberunscreened *value)
+static int rose_party_number_encode(struct pri *pri, unsigned char *dst, unsigned char ton, char *num)
 {
+	int i = 0, compsp = 0;
+	struct rose_component *comp, *compstk[10];
+	int numsize, size;
+
+	numsize = strlen(num);
+	if (numsize > 20 ) {
+		pri_message(pri, "!! Encoding of oversized PartyNumber component failed (%d)\n", numsize);
+		return -1;
+	}
+
+#if 0
+	/* tag component unknownPartyNumber  (0x80,len) */
+	ASN1_ADD_SIMPLE(comp, (ASN1_CONTEXT_SPECIFIC | ASN1_TAG_0), dst, i);
+	ASN1_PUSH(compstk, compsp, comp);
+
+	/* unknownPartyNumber, implicid NumberDigits */
+	memcpy(comp->data, num, numsize);
+	i += numsize;
+
+	ASN1_FIXUP(compstk, compsp, dst, i);
+#endif
+
+	/* tag component publicPartyNumber  (0xa1,len) */
+	ASN1_ADD_SIMPLE(comp, (ASN1_CONTEXT_SPECIFIC | ASN1_CONSTRUCTOR | ASN1_TAG_1), dst, i);
+	ASN1_PUSH(compstk, compsp, comp);
+
+	/* publicPartyNumber, implicid PublicPartyNumber */
+	size = rose_public_party_number_encode(pri, comp->data, 1, ton, num);
+	if (size < 0)
+		return -1;
+	i += size;
+
+	ASN1_FIXUP(compstk, compsp, dst, i);
+
+	return i;
+}
+
+static int rose_party_number_decode(struct pri *pri, unsigned char *data, int len, struct addressingdataelements_presentednumberunscreened *value)
+{
 	int i = 0;
 	int size = 0;
 	struct rose_component *comp = NULL;
 	unsigned char *vdata = data;
 
+	do {
+		GET_COMPONENT(comp, i, vdata, len);
+
+		switch(comp->type) {
+			case (ASN1_CONTEXT_SPECIFIC | ASN1_TAG_0):   /* [0] IMPLICIT NumberDigits -- default: unknownPartyNumber */
+				if (pri->debug & PRI_DEBUG_APDU)
+					pri_message(pri, "     PartyNumber: UnknownPartyNumber len=%d\n", len);
+				size = rose_number_digits_decode(pri, comp->data, comp->len, 1, value);
+				if (size < 0)
+					return -1;
+				value->npi = PRI_NPI_UNKNOWN;
+				value->ton = PRI_TON_UNKNOWN;
+				break;
+
+			case (ASN1_CONTEXT_SPECIFIC | ASN1_CONSTRUCTOR | ASN1_TAG_1):   /* [1] IMPLICIT PublicPartyNumber */
+				if (pri->debug & PRI_DEBUG_APDU)
+					pri_message(pri, "     PartyNumber: PublicPartyNumber len=%d\n", len);
+				size = rose_public_party_number_decode(pri, comp->data, comp->len, 1, value);
+				if (size < 0)
+					return -1;
+				value->npi = PRI_NPI_E163_E164;
+				break;
+
+			case (ASN1_CONTEXT_SPECIFIC | ASN1_TAG_3):   /* [3] IMPLICIT NumberDigits -- not used: dataPartyNumber */
+				pri_message(pri, "!! PartyNumber: dataPartyNumber is reserved!\n");
+				size = rose_number_digits_decode(pri, comp->data, comp->len, 1, value);
+				if (size < 0)
+					return -1;
+				value->npi = PRI_NPI_X121 /* ??? */;
+				value->ton = PRI_TON_UNKNOWN /* ??? */;
+				break;
+
+			case (ASN1_CONTEXT_SPECIFIC | ASN1_TAG_4):   /* [4] IMPLICIT NumberDigits -- not used: telexPartyNumber */
+				pri_message(pri, "!! PartyNumber: telexPartyNumber is reserved!\n");
+				size = rose_number_digits_decode(pri, comp->data, comp->len, 1, value);
+				if (size < 0)
+					return -1;
+				value->npi = PRI_NPI_F69 /* ??? */;
+				value->ton = PRI_TON_UNKNOWN /* ??? */;
+				break;
+
+			case (ASN1_CONTEXT_SPECIFIC | ASN1_CONSTRUCTOR | ASN1_TAG_5):   /* [5] IMPLICIT PrivatePartyNumber */
+				if (pri->debug & PRI_DEBUG_APDU)
+					pri_message(pri, "     PartyNumber: PrivatePartyNumber len=%d\n", len);
+				size = rose_private_party_number_decode(pri, comp->data, comp->len, 1, value);
+				if (size < 0)
+					return -1;
+ 				value->npi = PRI_NPI_PRIVATE;
+				break;
+
+			case (ASN1_CONTEXT_SPECIFIC | ASN1_TAG_8):   /* [8] IMPLICIT NumberDigits -- not used: nationalStandatdPartyNumber */
+				pri_message(pri, "!! PartyNumber: nationalStandardPartyNumber is reserved!\n");
+				size = rose_number_digits_decode(pri, comp->data, comp->len, 1, value);
+				if (size < 0)
+					return -1;
+				value->npi = PRI_NPI_NATIONAL;
+				value->ton = PRI_TON_NATIONAL;
+				break;
+
+			default:
+				pri_message(pri, "Invalid PartyNumber component 0x%X\n", comp->type);
+				return -1;
+		}
+		ASN1_FIXUP_LEN(comp, size);
+		if (pri->debug & PRI_DEBUG_APDU)
+			pri_message(pri, "     PartyNumber: '%s' size=%d len=%d\n", value->partyaddress, size, len);
+		return size;
+	}
+	while (0);
+
+	return -1;
+}
+
+static int rose_presented_number_unscreened_encode(struct pri *pri, unsigned char *dst, unsigned char presentation, unsigned char ton, char *num)
+{
+	int i = 0, compsp = 0;
+	struct rose_component *comp, *compstk[10];
+	int numsize, size;
+
+	numsize = strlen(num);
+	if (numsize > 20 ) {
+		pri_message(pri, "!! Encoding of oversized PresentedNumberUnscreened component failed (%d)\n", numsize);
+		return -1;
+	}
+
+	switch (presentation & PRES_RESTRICTION) {
+		case PRES_ALLOWED:
+			/* tag component [0] presentationAllowedAddress (0xa0,len) */
+			ASN1_ADD_SIMPLE(comp, (ASN1_CONTEXT_SPECIFIC | ASN1_CONSTRUCTOR | ASN1_TAG_0), dst, i);
+			ASN1_PUSH(compstk, compsp, comp);
+
+			/* PartyNumber */
+			size = rose_party_number_encode(pri, comp->data, ton, num);
+			if (size < 0)
+				return -1;
+			i += size;
+			ASN1_FIXUP(compstk, compsp, dst, i);
+			break;
+		case PRES_RESTRICTED:
+			/* tag component [1] presentationRestricted (0x81,len) */
+			ASN1_ADD_SIMPLE(comp, (ASN1_CONTEXT_SPECIFIC | ASN1_TAG_1), dst, i);
+			break;
+		case PRES_UNAVAILABLE:
+			/* tag component [2] numberNotAvailableDueToInterworking (0x82,len) */
+			ASN1_ADD_SIMPLE(comp, (ASN1_CONTEXT_SPECIFIC | ASN1_TAG_2), dst, i);
+			/* ASN1_FIXUP(compstk, compsp, dst, i); */
+			break;
+		default:
+			pri_message(pri, "!! Undefined presentation value for PresentedNumberUnscreened: 0x%x\n", presentation);
+			return -1;
+	}
+
+	return i;
+}
+
+static int rose_presented_number_unscreened_decode(struct pri *pri, unsigned char *data, int len, struct addressingdataelements_presentednumberunscreened *value)
+{
+	int i = 0;
+	int size = 0;
+	struct rose_component *comp = NULL;
+	unsigned char *vdata = data;
+
 	/* Fill in default values */
+	value->partyaddress[0] = '\0';
 	value->ton = PRI_TON_UNKNOWN;
 	value->npi = PRI_NPI_E163_E164;
 	value->pres = -1;	/* Data is not available */
@@ -591,7 +1130,7 @@
 		switch(comp->type) {
 		case (ASN1_CONTEXT_SPECIFIC | ASN1_CONSTRUCTOR | ASN1_TAG_0):		/* [0] presentationAllowedNumber */
 			value->pres = PRES_ALLOWED_USER_NUMBER_NOT_SCREENED;
-			size = rose_address_decode(pri, call, comp->data, comp->len, value);
+			size = rose_address_decode(pri, comp->data, comp->len, value);
 			ASN1_FIXUP_LEN(comp, size);
 			return size + 2;
 		case (ASN1_CONTEXT_SPECIFIC | ASN1_TAG_1):		/* [1] IMPLICIT presentationRestricted */
@@ -610,7 +1149,7 @@
 			return 2;
 		case (ASN1_CONTEXT_SPECIFIC | ASN1_CONSTRUCTOR | ASN1_TAG_3):		/* [3] presentationRestrictedNumber */
 			value->pres = PRES_PROHIB_USER_NUMBER_NOT_SCREENED;
-			size = rose_address_decode(pri, call, comp->data, comp->len, value) + 2;
+			size = rose_address_decode(pri, comp->data, comp->len, value) + 2;
 			ASN1_FIXUP_LEN(comp, size);
 			return size + 2;
 		default:
@@ -623,17 +1162,482 @@
 	return -1;
 }
 
+static int rose_number_screened_encode(struct pri *pri, unsigned char *dst, int implicit, unsigned char ton, unsigned char screenind, char *num)
+{
+	int i = 0, compsp = 0;
+	struct rose_component *comp, *compstk[10];
+	int numsize, size;
+
+	numsize = strlen(num);
+	if (numsize > 20 ) {
+		pri_message(pri, "!! Encoding of oversized NumberScreened component failed (%d)\n", numsize);
+		return -1;
+	}
+
+	if (!implicit) {
+		/* constructor component  (0x30,len) */
+		ASN1_ADD_SIMPLE(comp, (ASN1_CONSTRUCTOR | ASN1_SEQUENCE), dst, i);
+		ASN1_PUSH(compstk, compsp, comp);
+	} else
+		comp = (struct rose_component *)dst;
+
+	/* PartyNumber */
+	size = rose_party_number_encode(pri, (u_int8_t *)comp, ton, num);
+	if (size < 0)
+		return -1;
+	i += size;
+
+	/* ScreeningIndicator  (0x0a,0x01,screenind) */
+	ASN1_ADD_BYTECOMP(comp, ASN1_ENUMERATED, dst, i, screenind);
+
+	if (!implicit)
+		ASN1_FIXUP(compstk, compsp, dst, i);
+
+	return i;
+}
+
+static int rose_number_screened_decode(struct pri *pri, unsigned char *data, int len, struct addressingdataelements_presentednumberscreened *value)
+{
+	int i = 0;
+	int size = 0;
+	struct rose_component *comp = NULL;
+	unsigned char *vdata = data;
+
+	int scrind = -1;
+
+	do {
+		/* Party Number */
+		GET_COMPONENT(comp, i, vdata, len);
+		size = rose_party_number_decode(pri, (u_int8_t *)comp, comp->len + 2, (struct addressingdataelements_presentednumberunscreened*) value);
+		if (size < 0)
+			return -1;
+		comp->len = size;
+		NEXT_COMPONENT(comp, i);
+
+		/* Screening Indicator */
+		GET_COMPONENT(comp, i, vdata, len);
+		CHECK_COMPONENT(comp, ASN1_ENUMERATED, "Don't know what to do with NumberScreened ROSE component type 0x%x\n");
+		ASN1_GET_INTEGER(comp, scrind);
+		// Todo: scrind = screeningindicator_for_q931(pri, scrind);
+		NEXT_COMPONENT(comp, i);
+
+		value->scrind = scrind;
+
+		if (pri->debug & PRI_DEBUG_APDU)
+			pri_message(pri, "     NumberScreened: '%s' ScreeningIndicator=%d  i=%d  len=%d\n", value->partyaddress, scrind, i, len);
+
+		return i-2;  // We do not have a sequence header here.
+	}
+	while (0);
+
+	return -1;
+}
+
+static int rose_presented_number_screened_decode(struct pri *pri, unsigned char *data, int len, struct addressingdataelements_presentednumberscreened *value)
+{
+	int i = 0;
+	int size = 0;
+	struct rose_component *comp = NULL;
+	unsigned char *vdata = data;
+
+	/* Fill in default values */
+	value->partyaddress[0] = '\0';
+	value->ton = PRI_TON_UNKNOWN;
+	value->npi = PRI_NPI_UNKNOWN;
+	value->pres = -1; /* Data is not available */
+	value->scrind = 0;
+
+	do {
+		GET_COMPONENT(comp, i, vdata, len);
+
+		switch(comp->type) {
+			case (ASN1_CONTEXT_SPECIFIC | ASN1_CONSTRUCTOR | ASN1_TAG_0):   /* [0] IMPLICIT presentationAllowedNumber */
+				if (pri->debug & PRI_DEBUG_APDU)
+					pri_message(pri, "     PresentedNumberScreened: presentationAllowedNumber comp->len=%d\n", comp->len);
+				value->pres = PRES_ALLOWED_USER_NUMBER_PASSED_SCREEN;
+				size = rose_number_screened_decode(pri, comp->data, comp->len, value);
+				if (size < 0)
+					return -1;
+				ASN1_FIXUP_LEN(comp, size);
+				return size + 2;
+
+			case (ASN1_CONTEXT_SPECIFIC | ASN1_TAG_1):    /* [1] IMPLICIT presentationRestricted */
+				if (pri->debug & PRI_DEBUG_APDU)
+					pri_message(pri, "     PresentedNumberScreened: presentationRestricted comp->len=%d\n", comp->len);
+				if (comp->len != 0) { /* must be NULL */
+					pri_error(pri, "!! Invalid PresentationRestricted component received (len != 0)\n");
+					return -1;
+				}
+				value->pres = PRES_PROHIB_USER_NUMBER_PASSED_SCREEN;
+				return 2;
+
+			case (ASN1_CONTEXT_SPECIFIC | ASN1_TAG_2):    /* [2] IMPLICIT numberNotAvailableDueToInterworking */
+				if (pri->debug & PRI_DEBUG_APDU)
+					pri_message(pri, "     PresentedNumberScreened: NumberNotAvailableDueToInterworking comp->len=%d\n", comp->len);
+				if (comp->len != 0) { /* must be NULL */
+					pri_error(pri, "!! Invalid NumberNotAvailableDueToInterworking component received (len != 0)\n");
+					return -1;
+				}
+				value->pres = PRES_NUMBER_NOT_AVAILABLE;
+				if (pri->debug & PRI_DEBUG_APDU)
+					pri_message(pri, "     PresentedNumberScreened: numberNotAvailableDueToInterworking Type=0x%X  i=%d len=%d size=%d\n", comp->type, i, len);
+				return 2;
+
+			case (ASN1_CONTEXT_SPECIFIC | ASN1_CONSTRUCTOR | ASN1_TAG_3):    /* [3] IMPLICIT presentationRestrictedNumber */
+				if (pri->debug & PRI_DEBUG_APDU)
+					pri_message(pri, "     PresentedNumberScreened: presentationRestrictedNumber comp->len=%d\n", comp->len);
+				value->pres = PRES_PROHIB_USER_NUMBER_PASSED_SCREEN;
+				size = rose_number_screened_decode(pri, comp->data, comp->len, value);
+				if (size < 0)
+					return -1;
+				ASN1_FIXUP_LEN(comp, size);
+				return size + 2;
+
+			default:
+				pri_message(pri, "Invalid PresentedNumberScreened component 0x%X\n", comp->type);
+		}
+		return -1;
+	}
+	while (0);
+
+	return -1;
+}
+
+static int rose_partysubaddress_decode(struct pri *pri, unsigned char *data, int len, struct addressingdataelements_partysubaddress *value)
+{
+	int i = 0;
+	int size = 0;
+	struct rose_component *comp = NULL;
+	unsigned char *vdata = data;
+
+	int odd_count_indicator = -1;
+	value->partysubaddress[0] = '\0';
+
+	do {
+		GET_COMPONENT(comp, i, vdata, len);
+
+		switch(comp->type) {
+		case (ASN1_CONSTRUCTOR | ASN1_SEQUENCE):    /* UserSpecifiedSubaddress */
+			/* SubaddressInformation */
+			SUB_COMPONENT(comp, i);
+			GET_COMPONENT(comp, i, vdata, len);
+			CHECK_COMPONENT(comp, ASN1_OCTETSTRING, "Don't know what to do if SubaddressInformation is of type 0x%x\n");
+			size = asn1_name_decode(comp->data, comp->len, value->partysubaddress, sizeof(value->partysubaddress));
+			if (size < 0)
+				return -1;
+			i += size;
+
+			/* oddCountIndicator BOOLEAN OPTIONAL */
+			if (i < len) {
+				GET_COMPONENT(comp, i, vdata, len);
+				CHECK_COMPONENT(comp, ASN1_BOOLEAN, "Don't know what to do if SubaddressInformation is of type 0x%x\n");
+
+				ASN1_GET_INTEGER(comp, odd_count_indicator);
+				NEXT_COMPONENT(comp, i);
+			}
+		case (ASN1_OCTETSTRING):    /* NSAPSubaddress */
+			size = asn1_name_decode((u_int8_t *)comp, comp->len + 2, value->partysubaddress, sizeof(value->partysubaddress));
+			if (size < 0)
+				return -1;
+			i += size;
+			break;
+		default:
+			pri_message(pri, "Invalid PartySubaddress component 0x%X\n", comp->type);
+			return -1;
+		}
+
+		if (pri->debug & PRI_DEBUG_APDU)
+			pri_message(pri, "     PartySubaddress: '%s', oddCountIndicator=%d, i=%d len=%d\n", value->partysubaddress, odd_count_indicator, i, len);
+
+		return i;
+	}
+	while (0);
+
+	return -1;
+}
+
+static int rose_address_screened_decode(struct pri *pri, unsigned char *data, int len, struct addressingdataelements_addressscreened *value)
+{
+	int i = 0;
+	int size = 0;
+	struct rose_component *comp = NULL;
+	unsigned char *vdata = data;
+
+	int scrind;
+	value->partysubaddress[0] = '\0';
+
+	/* SEQUENCE AddressScreened */
+	do {
+		/* PartyNumber */
+		GET_COMPONENT(comp, i, vdata, len);
+		size = rose_party_number_decode(pri, (u_int8_t *)comp, comp->len + 2, (struct addressingdataelements_presentednumberunscreened *)value);
+		if (size < 0)
+			return -1;
+		comp->len = size;
+		NEXT_COMPONENT(comp, i);
+
+		/* ScreeningIndicator */
+		GET_COMPONENT(comp, i, vdata, len);
+		CHECK_COMPONENT(comp, ASN1_ENUMERATED, "Don't know what to do with AddressScreened ROSE component type 0x%x\n");
+		ASN1_GET_INTEGER(comp, scrind);
+		NEXT_COMPONENT(comp, i);
+
+		if (i < len) {
+			/* PartySubaddress OPTIONAL */
+			GET_COMPONENT(comp, i, vdata, len);
+			size = rose_partysubaddress_decode(pri, (u_int8_t *)comp, comp->len + 2, (struct addressingdataelements_partysubaddress *)value);
+			if (size < 0)
+				return -1;
+			i += size;
+		}
+
+		value->scrind = scrind;
+
+		if (pri->debug & PRI_DEBUG_APDU)
+			pri_message(pri, "     AddressScreened: '%s' ScreeningIndicator=%d  i=%d  len=%d\n", value->partyaddress, scrind, i, len);
+
+		return i-2;
+	}
+	while (0);
+
+	return -1;
+}
+
+static int rose_presented_address_screened_decode(struct pri *pri, unsigned char *data, int len, struct addressingdataelements_presentedaddressscreened *value)
+{
+	int i = 0;
+	int size = 0;
+	struct rose_component *comp = NULL;
+	unsigned char *vdata = data;
+
+	/* Fill in default values */
+	value->partyaddress[0] = '\0';
+	value->partysubaddress[0] = '\0';
+	value->npi = PRI_NPI_UNKNOWN;
+	value->ton = PRI_TON_UNKNOWN;
+	value->pres = -1; /* Data is not available */
+	value->scrind = 0;
+
+	do {
+		GET_COMPONENT(comp, i, vdata, len);
+
+		switch(comp->type) {
+			case (ASN1_CONTEXT_SPECIFIC | ASN1_CONSTRUCTOR | ASN1_TAG_0):   /* [0] IMPLICIT presentationAllowedAddress */
+				if (pri->debug & PRI_DEBUG_APDU)
+					pri_message(pri, "     PresentedAddressScreened: presentationAllowedAddress comp->len=%d\n", comp->len);
+				value->pres = PRES_ALLOWED_USER_NUMBER_PASSED_SCREEN;
+				size = rose_address_screened_decode(pri, comp->data, comp->len, (struct addressingdataelements_addressscreened *)value);
+				if (size < 0)
+					return -1;
+				ASN1_FIXUP_LEN(comp, size);
+				return size + 2;
+
+			case (ASN1_CONTEXT_SPECIFIC | ASN1_TAG_1):    /* [1] IMPLICIT presentationRestricted */
+				if (pri->debug & PRI_DEBUG_APDU)
+					pri_message(pri, "     PresentedAddressScreened: presentationRestricted comp->len=%d\n", comp->len);
+				if (comp->len != 0) { /* must be NULL */
+					pri_error(pri, "!! Invalid PresentationRestricted component received (len != 0)\n");
+					return -1;
+				}
+				value->pres = PRES_PROHIB_USER_NUMBER_PASSED_SCREEN;
+				return 2;
+
+			case (ASN1_CONTEXT_SPECIFIC | ASN1_TAG_2):    /* [2] IMPLICIT numberNotAvailableDueToInterworking */
+				if (pri->debug & PRI_DEBUG_APDU)
+					pri_message(pri, "     PresentedAddressScreened: NumberNotAvailableDueToInterworking comp->len=%d\n", comp->len);
+				if (comp->len != 0) { /* must be NULL */
+					pri_error(pri, "!! Invalid NumberNotAvailableDueToInterworking component received (len != 0)\n");
+					return -1;
+				}
+				value->pres = PRES_NUMBER_NOT_AVAILABLE;
+				if (pri->debug & PRI_DEBUG_APDU)
+					pri_message(pri, "     PresentedAddressScreened: numberNotAvailableDueToInterworking Type=0x%X  i=%d len=%d size=%d\n", comp->type, i, len);
+				return 2;
+
+			case (ASN1_CONTEXT_SPECIFIC | ASN1_CONSTRUCTOR | ASN1_TAG_3):    /* [3] IMPLICIT presentationRestrictedAddress */
+				if (pri->debug & PRI_DEBUG_APDU)
+					pri_message(pri, "     PresentedAddressScreened: presentationRestrictedAddress comp->len=%d\n", comp->len);
+				value->pres = PRES_PROHIB_USER_NUMBER_PASSED_SCREEN;
+				size = rose_address_screened_decode(pri, comp->data, comp->len, (struct addressingdataelements_addressscreened *)value);
+				if (size < 0)
+					return -1;
+				ASN1_FIXUP_LEN(comp, size);
+				return size + 2;
+
+			default:
+				pri_message(pri, "Invalid PresentedAddressScreened component 0x%X\n", comp->type);
+		}
+		return -1;
+	}
+	while (0);
+
+	return -1;
+}
+
+static int rose_diverting_leg_information1_decode(struct pri *pri, q931_call *call, struct rose_component *sequence, int len)
+{
+	int i = 0;
+	struct addressingdataelements_presentednumberunscreened nominatednr;
+	int diversion_reason;
+	int subscription_option;
+	struct rose_component *comp = NULL;
+	unsigned char *vdata = sequence->data;
+	int size = 0;
+	memset(&nominatednr, 0, sizeof(nominatednr));
+
+	/* Data checks */
+	if (sequence->type != (ASN1_CONSTRUCTOR | ASN1_SEQUENCE)) { /* Constructed Sequence */
+		pri_message(pri, "Invalid DivertingLegInformation1Type argument\n");
+		return -1;
+	}
+
+	if (sequence->len == ASN1_LEN_INDEF) {
+		len -= 4;   /* For the 2 extra characters at the end
+		               and two characters of header */
+	} else
+		len -= 2;
+
+	do {
+		/* diversionReason DiversionReason */
+		GET_COMPONENT(comp, i, vdata, len);
+		CHECK_COMPONENT(comp, ASN1_ENUMERATED, "Invalid diversionReason type 0x%X of ROSE divertingLegInformation1 component received\n");
+		ASN1_GET_INTEGER(comp, diversion_reason);
+		NEXT_COMPONENT(comp, i);
+
+		if (pri->debug & PRI_DEBUG_APDU)
+			pri_message(pri, "    Received diversionReason: %s(%d)\n", diversionreason_to_str(pri, diversion_reason), diversion_reason);
+
+		diversion_reason = redirectingreason_for_q931(pri, diversion_reason);
+
+		/* subscriptionOption SubscriptionOption */
+		GET_COMPONENT(comp, i, vdata, len);
+		CHECK_COMPONENT(comp, ASN1_ENUMERATED, "Invalid subscriptionOption type 0x%X of ROSE divertingLegInformation1 component received\n");
+		ASN1_GET_INTEGER(comp, subscription_option);
+		NEXT_COMPONENT(comp, i);
+
+		if (pri->debug & PRI_DEBUG_APDU)
+			pri_message(pri, "    Received subscriptionOption: %d\n", subscription_option);
+
+		/* nominatedNr PartyNumber */
+		GET_COMPONENT(comp, i, vdata, len);
+		size = rose_party_number_decode(pri, (u_int8_t *)comp, comp->len + 2, &nominatednr);
+		if (size < 0)
+			return -1;
+
+		if (pri->debug & PRI_DEBUG_APDU) {
+			pri_message(pri, "    Received nominatedNr '%s'\n", nominatednr.partyaddress);
+			pri_message(pri, "      ton = %d, npi = %d\n\n", nominatednr.ton, nominatednr.npi);
+		}
+
+		call->divleginfo1activeflag = 1;
+		if (subscription_option == QSIG_NOTIFICATION_WITH_DIVERTED_TO_NR) {
+			libpri_copy_string(call->divertedtonum, nominatednr.partyaddress, sizeof(call->divertedtonum));
+		} else {
+			call->divertedtonum[0] = '\0';
+		}
+		call->divertedtopres = (subscription_option == QSIG_NOTIFICATION_WITH_DIVERTED_TO_NR) ? PRES_ALLOWED_USER_NUMBER_NOT_SCREENED : PRES_PROHIB_USER_NUMBER_NOT_SCREENED;
+		call->divertedtoplan = ((nominatednr.ton & 0x07) << 4) | (nominatednr.npi & 0x0f);
+		call->divertedtoreason = diversion_reason;
+		call->divertedtocount++;
+
+		return 0;
+	}
+	while (0);
+
+	return -1;
+}
+
+int rose_diverting_leg_information1_encode(struct pri *pri, q931_call *call)
+{
+	int i = 0, compsp = 0;
+	struct rose_component *comp, *compstk[10];
+	unsigned char buffer[256];
+	int size;
+
+	if (pri->debug & PRI_DEBUG_APDU)
+		pri_message(pri, "    Encode divertingLegInformation1\n");
+
+	/* Protocol Profile = 0x1f (Networking Extensions)  (0x9f) */
+	buffer[i++] = (ASN1_CONTEXT_SPECIFIC | Q932_PROTOCOL_EXTENSIONS);
+
+	/* Network Facility Extension */
+	if (pri->switchtype == PRI_SWITCH_QSIG) {
+		/* tag component NetworkFacilityExtension (0xaa, len ) */
+		ASN1_ADD_SIMPLE(comp, COMP_TYPE_NFE, buffer, i);
+		ASN1_PUSH(compstk, compsp, comp);
+
+		/* sourceEntity  (0x80,0x01,0x00) */
+		ASN1_ADD_BYTECOMP(comp, (ASN1_CONTEXT_SPECIFIC | ASN1_TAG_0), buffer, i, 0);	/* endPINX(0) */
+
+		/* destinationEntity  (0x82,0x01,0x00) */
+		ASN1_ADD_BYTECOMP(comp, (ASN1_CONTEXT_SPECIFIC | ASN1_TAG_2), buffer, i, 0);	/* endPINX(0) */
+		ASN1_FIXUP(compstk, compsp, buffer, i);
+	}
+
+	/* Network Protocol Profile */
+	/*  - not included - */
+
+	/* Interpretation APDU  (0x8b,0x01,0x00) */
+	ASN1_ADD_BYTECOMP(comp, COMP_TYPE_INTERPRETATION, buffer, i, 0);	/* discardAnyUnrecognisedInvokePdu(0) */
+
+	/* Service APDU(s): */
+
+	/* ROSE InvokePDU  (0xa1,len) */
+	ASN1_ADD_SIMPLE(comp, COMP_TYPE_INVOKE, buffer, i);
+	ASN1_PUSH(compstk, compsp, comp);
+
+	/* ROSE InvokeID  (0x02,0x01,invokeid) */
+	ASN1_ADD_BYTECOMP(comp, ASN1_INTEGER, buffer, i, get_invokeid(pri));
+
+	/* ROSE operationId  (0x02,0x01,0x14)*/
+	ASN1_ADD_BYTECOMP(comp, ASN1_INTEGER, buffer, i, ROSE_DIVERTING_LEG_INFORMATION1);
+
+	/* constructor component  (0x30,len) */
+	ASN1_ADD_SIMPLE(comp, (ASN1_CONSTRUCTOR | ASN1_SEQUENCE), buffer, i);
+	ASN1_PUSH(compstk, compsp, comp);
+
+	/* diversionReason  (0x0a,0x01,diversionreason) */
+	ASN1_ADD_BYTECOMP(comp, ASN1_ENUMERATED, buffer, i, redirectingreason_from_q931(pri, call->divertedtoreason));
+
+	/* subscriptionOption  (0x0a,0x01,subscriptionoption) */
+	ASN1_ADD_BYTECOMP(comp, ASN1_ENUMERATED, buffer, i, presentation_to_subscription(pri, call->divertedtopres));
+
+	/* nominatedNr */
+
+	/* tag component publicPartyNumber  (0xa1,len) */
+	ASN1_ADD_SIMPLE(comp, (ASN1_CONTEXT_SPECIFIC | ASN1_CONSTRUCTOR | ASN1_TAG_1), buffer, i);
+	ASN1_PUSH(compstk, compsp, comp);
+
+	/* publicPartyNumber, implicid PublicPartyNumber */
+	size = rose_public_party_number_encode(pri, &buffer[i], 1, (call->divertedtoplan & 0x70) >> 4, call->divertedtonum);
+	if (size < 0)
+		return -1;
+	i += size;
+
+	ASN1_FIXUP(compstk, compsp, buffer, i);
+	ASN1_FIXUP(compstk, compsp, buffer, i);
+	ASN1_FIXUP(compstk, compsp, buffer, i);
+
+	if (pri_call_apdu_queue(call, Q931_FACILITY, buffer, i, NULL, NULL))
+		return -1;
+
+	return 0;
+}
+
 static int rose_diverting_leg_information2_decode(struct pri *pri, q931_call *call, struct rose_component *sequence, int len)
 {
 	int i = 0;
+	struct rose_component *comp = NULL;
+	unsigned char *vdata = sequence->data;
+	int size = 0;
+
 	int diversion_counter;
 	int diversion_reason;
-	char origcalledname[50] = "", redirectingname[50] = "";
+	int original_diversion_reason = QSIG_DIVERT_REASON_UNKNOWN;
+	struct nameelements_name redirectingname = { "", CHARACTER_SET_UNKNOWN, 0 };
+	struct nameelements_name origcalledname = { "", CHARACTER_SET_UNKNOWN, 0 };;
 	struct addressingdataelements_presentednumberunscreened divertingnr;
- 	struct addressingdataelements_presentednumberunscreened originalcallednr;
-	struct rose_component *comp = NULL;
-	unsigned char *vdata = sequence->data;
-	int res = 0;
+	struct addressingdataelements_presentednumberunscreened originalcallednr;
 	memset(&divertingnr, 0, sizeof(divertingnr));
 	memset(&originalcallednr, 0, sizeof(originalcallednr));
 
@@ -644,249 +1648,429 @@
 	}
 
 	if (sequence->len == ASN1_LEN_INDEF) {
-		len -= 4; /* For the 2 extra characters at the end
-                           * and two characters of header */
+		len -= 4;   /* For the 2 extra characters at the end
+		               and two characters of header */
 	} else
 		len -= 2;
 
 	do {
-		/* diversionCounter stuff */
+		/* diversionCounter */
 		GET_COMPONENT(comp, i, vdata, len);
-		CHECK_COMPONENT(comp, ASN1_INTEGER, "Don't know what to do it diversionCounter is of type 0x%x\n");
+		CHECK_COMPONENT(comp, ASN1_INTEGER, "Don't know what to do if diversionCounter is of type 0x%x\n");
 		ASN1_GET_INTEGER(comp, diversion_counter);
 		NEXT_COMPONENT(comp, i);
 
-		/* diversionReason stuff */
+		if (pri->debug & PRI_DEBUG_APDU)
+			pri_message(pri, "    Received diversionCounter: %d\n",  diversion_counter);
+
+		/* diversionReason DiversionReason */
 		GET_COMPONENT(comp, i, vdata, len);
 		CHECK_COMPONENT(comp, ASN1_ENUMERATED, "Invalid diversionReason type 0x%X of ROSE divertingLegInformation2 component received\n");
 		ASN1_GET_INTEGER(comp, diversion_reason);
 		NEXT_COMPONENT(comp, i);
 
+		if (pri->debug & PRI_DEBUG_APDU)
+			pri_message(pri, "    Received diversionReason: %s(%d)\n", diversionreason_to_str(pri, diversion_reason), diversion_reason);
+
 		diversion_reason = redirectingreason_for_q931(pri, diversion_reason);
-	
-		if(pri->debug & PRI_DEBUG_APDU)
-			pri_message(pri, "    Redirection reason: %d, total diversions: %d\n", diversion_reason, diversion_counter);
-		pri_message(NULL, "Length of message is %d\n", len);
 
-		for(; i < len; NEXT_COMPONENT(comp, i)) {
+		/* Type SEQUENCE specifies an ordered list of component types.           *
+		 * We decode all components but for simplicity we don't check the order. */
+		while (i < len) {
 			GET_COMPONENT(comp, i, vdata, len);
+
 			switch(comp->type) {
 			case (ASN1_CONTEXT_SPECIFIC | ASN1_TAG_0):
-				call->origredirectingreason = redirectingreason_for_q931(pri, comp->data[0]);
+				/* originalDiversionReason */
+				ASN1_GET_INTEGER(comp, original_diversion_reason);
+				NEXT_COMPONENT(comp,i);
 				if (pri->debug & PRI_DEBUG_APDU)
-					pri_message(pri, "    Received reason for original redirection %d\n", call->origredirectingreason);
+					pri_message(pri, "    Received originalDiversionReason: %s(%d)\n", diversionreason_to_str(pri, original_diversion_reason), original_diversion_reason);
+				original_diversion_reason = redirectingreason_for_q931(pri, original_diversion_reason);
 				break;
 			case (ASN1_CONTEXT_SPECIFIC | ASN1_CONSTRUCTOR | ASN1_TAG_1):
-				res = rose_presented_number_unscreened_decode(pri, call, comp->data, comp->len, &divertingnr);
-				/* TODO: Fix indefinite length form hacks */
-				ASN1_FIXUP_LEN(comp, res);
-				comp->len = res;
-				if (res < 0)
+				/* divertingNr */
+				size = rose_presented_number_unscreened_decode(pri, comp->data, comp->len, &divertingnr);
+				if (size < 0)
 					return -1;
+				ASN1_FIXUP_LEN(comp, size);
+				comp->len = size;
+				NEXT_COMPONENT(comp,i);
 				if (pri->debug & PRI_DEBUG_APDU) {
 					pri_message(pri, "    Received divertingNr '%s'\n", divertingnr.partyaddress);
 					pri_message(pri, "      ton = %d, pres = %d, npi = %d\n", divertingnr.ton, divertingnr.pres, divertingnr.npi);
 				}
 				break;
 			case (ASN1_CONTEXT_SPECIFIC | ASN1_CONSTRUCTOR | ASN1_TAG_2):
-				res = rose_presented_number_unscreened_decode(pri, call, comp->data, comp->len, &originalcallednr);
-				if (res < 0)
+				/* originalCalledNr */
+				size = rose_presented_number_unscreened_decode(pri, comp->data, comp->len, &originalcallednr);
+				if (size < 0)
 					return -1;
-				ASN1_FIXUP_LEN(comp, res);
-				comp->len = res;
+				ASN1_FIXUP_LEN(comp, size);
+				comp->len = size;
+				NEXT_COMPONENT(comp,i);
 				if (pri->debug & PRI_DEBUG_APDU) {
-					pri_message(pri, "    Received originalcallednr '%s'\n", originalcallednr.partyaddress);
+					pri_message(pri, "    Received originalCalledNr '%s'\n", originalcallednr.partyaddress);
 					pri_message(pri, "      ton = %d, pres = %d, npi = %d\n", originalcallednr.ton, originalcallednr.pres, originalcallednr.npi);
 				}
 				break;
 			case (ASN1_CONTEXT_SPECIFIC | ASN1_CONSTRUCTOR | ASN1_TAG_3):
-				res = asn1_name_decode(comp->data, comp->len, redirectingname, sizeof(redirectingname));
-				if (res < 0)
+				/* redirectingName */
+				size = rose_name_decode(pri, comp->data, comp->len, &redirectingname);
+				if (size < 0)
 					return -1;
-				ASN1_FIXUP_LEN(comp, res);
-				comp->len = res;
+				i += (size + 2);
 				if (pri->debug & PRI_DEBUG_APDU)
-					pri_message(pri, "    Received RedirectingName '%s'\n", redirectingname);
+					pri_message(pri, "     Received RedirectingName '%s', namepres %s(%d), characterset %s(%d)\n",
+						        redirectingname.name, namepres_to_str(redirectingname.namepres), redirectingname.namepres,
+						        characterset_to_str(redirectingname.characterset), redirectingname.characterset);
 				break;
 			case (ASN1_CONTEXT_SPECIFIC | ASN1_CONSTRUCTOR | ASN1_TAG_4):
-				res = asn1_name_decode(comp->data, comp->len, origcalledname, sizeof(origcalledname));
-				if (res < 0)
+				/* originalCalledName */
+				size = rose_name_decode(pri, comp->data, comp->len, &origcalledname);
+				if (size < 0)
 					return -1;
-				ASN1_FIXUP_LEN(comp, res);
-				comp->len = res;
+				i += (size + 2);
 				if (pri->debug & PRI_DEBUG_APDU)
-					pri_message(pri, "    Received Originally Called Name '%s'\n", origcalledname);
+					pri_message(pri, "     Received OriginalCalledName '%s', namepres %s(%d), characterset %s(%d)\n",
+						        origcalledname.name, namepres_to_str(origcalledname.namepres), origcalledname.namepres,
+						        characterset_to_str(origcalledname.characterset), origcalledname.characterset);
 				break;
-			case (ASN1_CONTEXT_SPECIFIC | ASN1_CONSTRUCTOR | ASN1_TAG_5):
-				pri_message(pri, "!! Ignoring DivertingLegInformation2 component 0x%X\n", comp->type);
+			case (ASN1_CONTEXT_SPECIFIC | ASN1_CONSTRUCTOR | ASN1_TAG_5):	/* [5] IMPLICIT Extension */
+			case (ASN1_CONTEXT_SPECIFIC | ASN1_CONSTRUCTOR | ASN1_TAG_6):	/* [6] IMPLICIT SEQUENCE OF Extension */
+				if (pri->debug & PRI_DEBUG_APDU)
+					pri_message(pri, "!!     Ignoring DivertingLegInformation2 component 0x%X\n", comp->type);
+				NEXT_COMPONENT(comp, i);
 				break;
 			default:
-				if (comp->type == 0 && comp->len == 0) {
-					break; /* Found termination characters */
-				}
-				pri_message(pri, "!! Invalid DivertingLegInformation2 component received 0x%X\n", comp->type);
+				pri_message(pri, "!!     Invalid DivertingLegInformation2 component received 0x%X\n", comp->type);
 				return -1;
 			}
 		}
 
 		if (divertingnr.pres >= 0) {
-			call->redirectingplan = divertingnr.npi;
+			call->redirectingplan = ((divertingnr.ton & 0x07) << 4) | (divertingnr.npi & 0x0f);
 			call->redirectingpres = divertingnr.pres;
 			call->redirectingreason = diversion_reason;
 			libpri_copy_string(call->redirectingnum, divertingnr.partyaddress, sizeof(call->redirectingnum));
-			pri_message(pri, "    Received redirectingnum '%s' (%d)\n", call->redirectingnum, (int)call->redirectingnum[0]);
 		}
 		if (originalcallednr.pres >= 0) {
-			call->origcalledplan = originalcallednr.npi;
+			call->origcalledplan = ((originalcallednr.ton & 0x07) << 4) | (originalcallednr.npi & 0x0f);
 			call->origcalledpres = originalcallednr.pres;
 			libpri_copy_string(call->origcallednum, originalcallednr.partyaddress, sizeof(call->origcallednum));
-			pri_message(pri, "    Received origcallednum '%s' (%d)\n", call->origcallednum, (int)call->origcallednum[0]);
 		}
-		libpri_copy_string(call->redirectingname, redirectingname, sizeof(call->redirectingname));
-		libpri_copy_string(call->origcalledname, origcalledname, sizeof(call->origcalledname));
+
+		if (redirectingname.namepres != 0) {
+			libpri_copy_string(call->redirectingname, redirectingname.name, sizeof(call->redirectingname));
+		} else {
+			call->redirectingname[0] = '\0';
+		}
+
+		if (origcalledname.namepres != 0) {
+			libpri_copy_string(call->origcalledname, origcalledname.name, sizeof(call->origcalledname));
+		} else {
+			call->origcalledname[0] = '\0';
+		}
+
+		call->origredirectingreason = original_diversion_reason;
+		call->redirectingcount = diversion_counter;
+
 		return 0;
 	}
 	while (0);
 
 	return -1;
 }
-				
+
 static int rose_diverting_leg_information2_encode(struct pri *pri, q931_call *call)
 {
-	int i = 0, j, compsp = 0;
+	int i = 0, compsp = 0;
 	struct rose_component *comp, *compstk[10];
 	unsigned char buffer[256];
-	int len = 253;
-	
-#if 0	/* This is not required by specifications */
-	if (!strlen(call->callername)) {
-		return -1;
+	int size;
+
+	if (pri->debug & PRI_DEBUG_APDU)
+		pri_message(pri, "    Encode divertingLegInformation2\n");
+
+	/* Protocol Profile = 0x1f (Networking Extensions)  (0x9f) */
+	buffer[i++] = (ASN1_CONTEXT_SPECIFIC | Q932_PROTOCOL_EXTENSIONS);
+
+	/* Network Facility Extension */
+	if (pri->switchtype == PRI_SWITCH_QSIG) {
+		/* tag component NetworkFacilityExtension (0xaa, len ) */
+		ASN1_ADD_SIMPLE(comp, COMP_TYPE_NFE, buffer, i);
+		ASN1_PUSH(compstk, compsp, comp);
+
+		/* sourceEntity  (0x80,0x01,0x00) */
+		ASN1_ADD_BYTECOMP(comp, (ASN1_CONTEXT_SPECIFIC | ASN1_TAG_0), buffer, i, 0);	/* endPINX(0) */
+
+		/* destinationEntity  (0x82,0x01,0x00) */
+		ASN1_ADD_BYTECOMP(comp, (ASN1_CONTEXT_SPECIFIC | ASN1_TAG_2), buffer, i, 0);	/* endPINX(0) */
+		ASN1_FIXUP(compstk, compsp, buffer, i);
 	}
-#endif
 
-	buffer[i] = (ASN1_CONTEXT_SPECIFIC | Q932_PROTOCOL_EXTENSIONS);
-	i++;
-	/* Interpretation component */
-	ASN1_ADD_BYTECOMP(comp, COMP_TYPE_INTERPRETATION, buffer, i, 0x00 /* Discard unrecognized invokes */);
-	
+	/* Network Protocol Profile */
+	/*  - not included - */
+
+	/* Interpretation APDU  (0x8b,0x01,0x00) */
+	ASN1_ADD_BYTECOMP(comp, COMP_TYPE_INTERPRETATION, buffer, i, 0);	/* discardAnyUnrecognisedInvokePdu(0) */
+
+	/* Service APDU(s): */
+
+	/* ROSE InvokePDU  (0xa1,len) */
 	ASN1_ADD_SIMPLE(comp, COMP_TYPE_INVOKE, buffer, i);
-	
 	ASN1_PUSH(compstk, compsp, comp);
-	/* Invoke component contents */
-	/*	Invoke ID */
+
+	/* ROSE InvokeID  (0x02,0x01,invokeid) */
 	ASN1_ADD_BYTECOMP(comp, ASN1_INTEGER, buffer, i, get_invokeid(pri));
-	/*	Operation Tag */
-	
-	/* ROSE operationId component */
+
+	/* ROSE operationId  (0x02,0x01,0x15)*/
 	ASN1_ADD_BYTECOMP(comp, ASN1_INTEGER, buffer, i, ROSE_DIVERTING_LEG_INFORMATION2);
 
-	/* ROSE ARGUMENT component */
+	/* constructor component  (0x30,len) */
 	ASN1_ADD_SIMPLE(comp, (ASN1_CONSTRUCTOR | ASN1_SEQUENCE), buffer, i);
 	ASN1_PUSH(compstk, compsp, comp);
-	/* ROSE DivertingLegInformation2.diversionCounter component */
-	/* Always is 1 because other isn't available in the current design */
+
+	/* diversionCounter always is 1 because other isn't available in the current design */
+	/* diversionCounter  (0x02,0x01,0x01) */
 	ASN1_ADD_BYTECOMP(comp, ASN1_INTEGER, buffer, i, 1);
-	
-	/* ROSE DivertingLegInformation2.diversionReason component */
+
+	/* diversionReason  (0x0a,0x01,redirectingreason) */
 	ASN1_ADD_BYTECOMP(comp, ASN1_ENUMERATED, buffer, i, redirectingreason_from_q931(pri, call->redirectingreason));
-		
-	/* ROSE DivertingLegInformation2.divertingNr component */
+
+	/* originalDiversionReason */
+	/*  - not included - */
+
+	/* divertingNr */
+
+	/* tag component divertingNr  (0xa1,len) */
 	ASN1_ADD_SIMPLE(comp, (ASN1_CONTEXT_SPECIFIC | ASN1_CONSTRUCTOR | ASN1_TAG_1), buffer, i);
-	
 	ASN1_PUSH(compstk, compsp, comp);
-		/* Redirecting information always not screened */
-	
-	switch(call->redirectingpres) {
-		case PRES_ALLOWED_USER_NUMBER_NOT_SCREENED:
-		case PRES_ALLOWED_USER_NUMBER_PASSED_SCREEN:
-			if (call->redirectingnum && strlen(call->redirectingnum)) {
-				ASN1_ADD_SIMPLE(comp, (ASN1_CONTEXT_SPECIFIC | ASN1_CONSTRUCTOR | ASN1_TAG_0), buffer, i);
-				ASN1_PUSH(compstk, compsp, comp);
-					/* NPI of redirected number is not supported in the current design */
-				ASN1_ADD_SIMPLE(comp, (ASN1_CONTEXT_SPECIFIC | ASN1_CONSTRUCTOR | ASN1_TAG_1), buffer, i);
-				ASN1_PUSH(compstk, compsp, comp);
-					ASN1_ADD_BYTECOMP(comp, ASN1_ENUMERATED, buffer, i, typeofnumber_from_q931(pri, call->redirectingplan >> 4));
-					j = asn1_string_encode(ASN1_NUMERICSTRING, &buffer[i], len - i, 20, call->redirectingnum, strlen(call->redirectingnum));
-				if (j < 0)
-					return -1;
-					
-				i += j;
-				ASN1_FIXUP(compstk, compsp, buffer, i);
-				ASN1_FIXUP(compstk, compsp, buffer, i);
-				break;
-			}
-			/* fall through */
-		case PRES_PROHIB_USER_NUMBER_PASSED_SCREEN:
-		case PRES_PROHIB_USER_NUMBER_NOT_SCREENED:
-			ASN1_ADD_SIMPLE(comp, (ASN1_CONTEXT_SPECIFIC | ASN1_TAG_1), buffer, i);
-			break;
-		/* Don't know how to handle this */
-		case PRES_ALLOWED_NETWORK_NUMBER:
-		case PRES_PROHIB_NETWORK_NUMBER:
-		case PRES_ALLOWED_USER_NUMBER_FAILED_SCREEN:
-		case PRES_PROHIB_USER_NUMBER_FAILED_SCREEN:
-			ASN1_ADD_SIMPLE(comp, (ASN1_CONTEXT_SPECIFIC | ASN1_TAG_1), buffer, i);
-			break;
-		default:
-			pri_message(pri, "!! Undefined presentation value for redirecting number: %d\n", call->redirectingpres);
-		case PRES_NUMBER_NOT_AVAILABLE:
-			ASN1_ADD_SIMPLE(comp, (ASN1_CONTEXT_SPECIFIC | ASN1_TAG_2), buffer, i);
-			break;
-	}
+
+	size = rose_presented_number_unscreened_encode(pri, &buffer[i], call->redirectingpres, typeofnumber_from_q931(pri, (call->redirectingplan & 0x70) >> 4), call->redirectingnum);
+	if (size < 0)
+		return -1;
+	i += size;
 	ASN1_FIXUP(compstk, compsp, buffer, i);
 
-	/* ROSE DivertingLegInformation2.originalCalledNr component */
-	/* This information isn't supported by current design - duplicate divertingNr */
+	/* originalCalledNr */
+	/*  - not included - */
+
+#if 0
+	/* The originalCalledNr is unknown here. Its the same as divertingNr if the call *
+	 * is diverted only once but we don't know if its diverted one ore more times.   */
+
+	/* originalCalledNr */
+
+	/* tag component originalCalledNr  (0xa2,len) */
 	ASN1_ADD_SIMPLE(comp, (ASN1_CONTEXT_SPECIFIC | ASN1_CONSTRUCTOR | ASN1_TAG_2), buffer, i);
 	ASN1_PUSH(compstk, compsp, comp);
-		/* Redirecting information always not screened */
-	switch(call->redirectingpres) {
-		case PRES_ALLOWED_USER_NUMBER_NOT_SCREENED:
-		case PRES_ALLOWED_USER_NUMBER_PASSED_SCREEN:
-			if (call->redirectingnum && strlen(call->redirectingnum)) {
-				ASN1_ADD_SIMPLE(comp, (ASN1_CONTEXT_SPECIFIC | ASN1_CONSTRUCTOR | ASN1_TAG_0), buffer, i);
-				ASN1_PUSH(compstk, compsp, comp);
-				ASN1_ADD_SIMPLE(comp, (ASN1_CONTEXT_SPECIFIC | ASN1_CONSTRUCTOR | ASN1_TAG_1), buffer, i);
-				ASN1_PUSH(compstk, compsp, comp);
-				ASN1_ADD_BYTECOMP(comp, ASN1_ENUMERATED, buffer, i, typeofnumber_from_q931(pri, call->redirectingplan >> 4));
-	
-				j = asn1_string_encode(ASN1_NUMERICSTRING, &buffer[i], len - i, 20, call->redirectingnum, strlen(call->redirectingnum));
-				if (j < 0)
+
+	size = rose_presented_number_unscreened_encode(pri, &buffer[i], call->redirectingpres, typeofnumber_from_q931(pri, (call->redirectingplan & 0x70) >> 4), call->redirectingnum);
+	if (size < 0)
+		return -1;
+	i += size;
+	ASN1_FIXUP(compstk, compsp, buffer, i);
+#endif
+
+	/* redirectingName */
+	if (call->redirectingname[0]) {
+		/* tag component redirectingName  (0xa3,len) */
+		ASN1_ADD_SIMPLE(comp, (ASN1_CONTEXT_SPECIFIC | ASN1_CONSTRUCTOR | ASN1_TAG_3), buffer, i);
+		ASN1_PUSH(compstk, compsp, comp);
+
+		/* tag component namePresentationAllowedSimple  (0x80,len) */
+		ASN1_ADD_SIMPLE(comp, (ASN1_CONTEXT_SPECIFIC | ASN1_TAG_0), buffer, i);
+		ASN1_PUSH(compstk, compsp, comp);
+
+		/* namePresentationAllowedSimple, implicid NameData */
+		size = rose_namedata_encode(pri, &buffer[i], 1, call->redirectingname);
+		if (size < 0)
+			return -1;
+		i += size;
+
+		ASN1_FIXUP(compstk, compsp, buffer, i);
+		ASN1_FIXUP(compstk, compsp, buffer, i);
+	}
+
+	/* originalCalledName */
+	/*  - not included - */
+
+	ASN1_FIXUP(compstk, compsp, buffer, i);
+	ASN1_FIXUP(compstk, compsp, buffer, i);
+
+	if (pri_call_apdu_queue(call, Q931_SETUP, buffer, i, NULL, NULL))
+		return -1;
+
+	return 0;
+}
+
+static int rose_diverting_leg_information3_decode(struct pri *pri, q931_call *call, struct rose_component *sequence, int len)
+{
+	int i = 0;
+	struct nameelements_name redirectionname = { "", CHARACTER_SET_UNKNOWN, 0 };
+	int presentation_allowed_indicator;
+	struct rose_component *comp = NULL;
+	unsigned char *vdata = sequence->data;
+	int size = 0;
+
+	/* Data checks */
+	if (sequence->type != (ASN1_CONSTRUCTOR | ASN1_SEQUENCE)) { /* Constructed Sequence */
+		pri_message(pri, "Invalid DivertingLegInformation3Type argument\n");
+		return -1;
+	}
+
+	if (sequence->len == ASN1_LEN_INDEF) {
+		len -= 4;   /* For the 2 extra characters at the end
+		               and two characters of header */
+	} else
+		len -= 2;
+
+	do {
+		/* presentationAllowedIndicator */
+		GET_COMPONENT(comp, i, vdata, len);
+		CHECK_COMPONENT(comp, ASN1_BOOLEAN, "Don't know what to do if presentationAllowedIndicator is of type 0x%x\n");
+		ASN1_GET_INTEGER(comp, presentation_allowed_indicator);
+		NEXT_COMPONENT(comp, i);
+
+		if (pri->debug & PRI_DEBUG_APDU)
+			pri_message(pri, "    Received presentationAllowedIndicator: %d\n", presentation_allowed_indicator);
+
+		/* Type SEQUENCE specifies an ordered list of component types.           *
+		 * We decode all components but for simplicity we don't check the order. */
+		while (i < len) {
+			GET_COMPONENT(comp, i, vdata, len);
+
+			switch(comp->type) {
+			case (ASN1_CONTEXT_SPECIFIC | ASN1_CONSTRUCTOR | ASN1_TAG_0):
+				/* redirectionName */
+				size = rose_name_decode(pri, comp->data, comp->len, &redirectionname);
+				if (size < 0)
 					return -1;
-				
-				i += j;
-				ASN1_FIXUP(compstk, compsp, buffer, i);
-				ASN1_FIXUP(compstk, compsp, buffer, i);
+				i += (size + 2);
+				if (pri->debug & PRI_DEBUG_APDU)
+					pri_message(pri, "     Received RedirectionName '%s', namepres %s(%d), characterset %s(%d)\n",
+					            redirectionname.name, namepres_to_str(redirectionname.namepres), redirectionname.namepres,
+					            characterset_to_str(redirectionname.characterset), redirectionname.characterset);
 				break;
+			case (ASN1_CONTEXT_SPECIFIC | ASN1_CONSTRUCTOR | ASN1_TAG_1):	/* [1] IMPLICIT Extension */
+			case (ASN1_CONTEXT_SPECIFIC | ASN1_CONSTRUCTOR | ASN1_TAG_2):	/* [2] IMPLICIT SEQUENCE OF Extension */
+				if (pri->debug & PRI_DEBUG_APDU)
+					pri_message(pri, "!!     Ignoring DivertingLegInformation3 component 0x%X\n", comp->type);
+				NEXT_COMPONENT(comp, i);
+				break;
+			default:
+				pri_message(pri, "!! Invalid DivertingLegInformation3 component received 0x%X\n", comp->type);
+				return -1;
 			}
-				/* fall through */
-		case PRES_PROHIB_USER_NUMBER_PASSED_SCREEN:
-		case PRES_PROHIB_USER_NUMBER_NOT_SCREENED:
-			ASN1_ADD_SIMPLE(comp, (ASN1_CONTEXT_SPECIFIC | ASN1_TAG_1), buffer, i);
-			break;
-		/* Don't know how to handle this */
-		case PRES_ALLOWED_NETWORK_NUMBER:
-		case PRES_PROHIB_NETWORK_NUMBER:
-		case PRES_ALLOWED_USER_NUMBER_FAILED_SCREEN:
-		case PRES_PROHIB_USER_NUMBER_FAILED_SCREEN:
-			ASN1_ADD_SIMPLE(comp, (ASN1_CONTEXT_SPECIFIC | ASN1_TAG_1), buffer, i);
-			break;
-		default:
-			pri_message(pri, "!! Undefined presentation value for redirecting number: %d\n", call->redirectingpres);
-		case PRES_NUMBER_NOT_AVAILABLE:
-			ASN1_ADD_SIMPLE(comp, (ASN1_CONTEXT_SPECIFIC | ASN1_TAG_2), buffer, i);
-			break;
+		}
+
+		call->divleginfo3activeflag = 1;
+		if ((redirectionname.namepres != 0) && (presentation_allowed_indicator != 0)) {
+			libpri_copy_string(call->divertedtoname, redirectionname.name, sizeof(call->divertedtoname));
+		} else {
+			call->divertedtoname[0] = '\0';
+		}
+
+		return 0;
 	}
-	ASN1_FIXUP(compstk, compsp, buffer, i);
-		
-	/* Fix length of stacked components */
-	while(compsp > 0) {
+	while (0);
+
+	return -1;
+}
+
+int rose_diverting_leg_information3_encode(struct pri *pri, q931_call *call, int messagetype)
+{
+	int i = 0, compsp = 0;
+	struct rose_component *comp, *compstk[10];
+	unsigned char buffer[256];
+	int size;
+
+	if (pri->debug & PRI_DEBUG_APDU)
+		pri_message(pri, "    Encode divertingLegInformation3\n");
+
+	/* Protocol Profile = 0x1f (Networking Extensions)  (0x9f) */
+	buffer[i++] = (ASN1_CONTEXT_SPECIFIC | Q932_PROTOCOL_EXTENSIONS);
+
+	/* Network Facility Extension */
+	if (pri->switchtype == PRI_SWITCH_QSIG) {
+		/* tag component NetworkFacilityExtension (0xaa, len ) */
+		ASN1_ADD_SIMPLE(comp, COMP_TYPE_NFE, buffer, i);
+		ASN1_PUSH(compstk, compsp, comp);
+
+		/* sourceEntity  (0x80,0x01,0x00) */
+		ASN1_ADD_BYTECOMP(comp, (ASN1_CONTEXT_SPECIFIC | ASN1_TAG_0), buffer, i, 0);	/* endPINX(0) */
+
+		/* destinationEntity  (0x82,0x01,0x00) */
+		ASN1_ADD_BYTECOMP(comp, (ASN1_CONTEXT_SPECIFIC | ASN1_TAG_2), buffer, i, 0);	/* endPINX(0) */
 		ASN1_FIXUP(compstk, compsp, buffer, i);
 	}
-	
-	if (pri_call_apdu_queue(call, Q931_SETUP, buffer, i, NULL, NULL))
+
+	/* Network Protocol Profile */
+	/*  - not included - */
+
+	/* Interpretation APDU  (0x8b,0x01,0x00) */
+	ASN1_ADD_BYTECOMP(comp, COMP_TYPE_INTERPRETATION, buffer, i, 0);	/* discardAnyUnrecognisedInvokePdu(0) */
+
+	/* Service APDU(s): */
+
+	/* ROSE InvokePDU  (0xa1,len) */
+	ASN1_ADD_SIMPLE(comp, COMP_TYPE_INVOKE, buffer, i);
+	ASN1_PUSH(compstk, compsp, comp);
+
+	/* ROSE InvokeID  (0x02,0x01,invokeid) */
+	ASN1_ADD_BYTECOMP(comp, ASN1_INTEGER, buffer, i, get_invokeid(pri));
+
+	/* ROSE operationId  (0x02,0x01,0x16)*/
+	ASN1_ADD_BYTECOMP(comp, ASN1_INTEGER, buffer, i, ROSE_DIVERTING_LEG_INFORMATION3);
+
+	/* constructor component  (0x30,len) */
+	ASN1_ADD_SIMPLE(comp, (ASN1_CONSTRUCTOR | ASN1_SEQUENCE), buffer, i);
+	ASN1_PUSH(compstk, compsp, comp);
+
+	/* 'connectedpres' also indicates if name presentation is allowed */
+	if (((call->divertedtopres & 0x60) >> 5) == 0) {
+		/* presentation allowed */
+
+		/* presentationAllowedIndicator  (0x01,0x01,0xff) */
+		ASN1_ADD_BYTECOMP(comp, ASN1_BOOLEAN, buffer, i, 0xff);			/* true(255) */
+
+		/* redirectionName */
+
+		/* tag component redirectionName  (0xa0,len) */
+		ASN1_ADD_SIMPLE(comp, (ASN1_CONTEXT_SPECIFIC | ASN1_CONSTRUCTOR | ASN1_TAG_0), buffer, i);
+		ASN1_PUSH(compstk, compsp, comp);
+
+		if (call->divertedtoname[0]) {
+			/* tag component namePresentationAllowedSimple  (0x80,len) */
+			ASN1_ADD_SIMPLE(comp, (ASN1_CONTEXT_SPECIFIC | ASN1_TAG_0), buffer, i);
+			ASN1_PUSH(compstk, compsp, comp);
+
+			/* namePresentationAllowedSimple, implicid NameData */
+			size = rose_namedata_encode(pri, &buffer[i], 1, call->divertedtoname);
+			if (size < 0)
+				return -1;
+			i += size;
+
+			ASN1_FIXUP(compstk, compsp, buffer, i);
+		}
+
+		ASN1_FIXUP(compstk, compsp, buffer, i);
+	} else {
+		/* presentation restricted */
+
+		/* presentationAllowedIndicator  (0x01,0x01,0x00) */
+		ASN1_ADD_BYTECOMP(comp, ASN1_BOOLEAN, buffer, i, 0);			/* false(0) */
+
+		/* - don't include redirectionName, component is optional - */
+	}
+
+	ASN1_FIXUP(compstk, compsp, buffer, i);
+	ASN1_FIXUP(compstk, compsp, buffer, i);
+
+	if (pri_call_apdu_queue(call, messagetype, buffer, i, NULL, NULL))
 		return -1;
-		
+
 	return 0;
 }
 
@@ -1644,7 +2828,7 @@
 		switch (comp1->type) {
 			/* TODO: charged number is untested - please report! */
 			case (ASN1_CONTEXT_SPECIFIC | ASN1_CONSTRUCTOR | ASN1_TAG_0): /* chargedNumber (0xA0) */
-				if(rose_presented_number_unscreened_decode(pri, call, comp1->data, comp1->len, &chargednr) != 0)
+				if(rose_presented_number_unscreened_decode(pri, comp1->data, comp1->len, &chargednr) != 0)
 					return -1;
 				pri_message(pri, "!! not handled: Received ChargedNr '%s' \n", chargednr.partyaddress);
 				pri_message(pri, "  ton = %d, pres = %d, npi = %d\n", chargednr.ton, chargednr.pres, chargednr.npi);
@@ -1740,302 +2924,407 @@
 }
 /* End AOC */
 
-static int rose_calling_name_decode(struct pri *pri, q931_call *call, struct rose_component *choice, int len)
+/* ===== Call Transfer Supplementary Service (ECMA-178) ===== */
+
+static int rose_call_transfer_complete_decode(struct pri *pri, q931_call *call, struct rose_component *sequence, int len)
 {
 	int i = 0;
 	struct rose_component *comp = NULL;
-	unsigned char *vdata = choice->data;
-	int characterSet = 1;
-	switch (choice->type) {
-		case ROSE_NAME_PRESENTATION_ALLOWED_SIMPLE:
-			memcpy(call->callername, choice->data, choice->len);
-			call->callername[choice->len] = 0;
-			if (pri->debug & PRI_DEBUG_APDU)
-				pri_message(pri, "    Received simple calling name '%s'\n", call->callername);
-			return 0;
+	unsigned char *vdata = sequence->data;
+	int size = 0;
 
-		case ROSE_NAME_PRESENTATION_ALLOWED_EXTENDED:
-			do {
-				GET_COMPONENT(comp, i, vdata, len);
-				CHECK_COMPONENT(comp, ASN1_OCTETSTRING, "Don't know what to do if nameData is of type 0x%x\n");
-				memcpy(call->callername, comp->data, comp->len);
-				call->callername[comp->len] = 0;
-				NEXT_COMPONENT(comp, i);
+	struct addressingdataelements_presentednumberscreened redirection_number;
+	struct nameelements_name redirectionname = { "", CHARACTER_SET_UNKNOWN, 0 };
+	char basiccallinfoelements[257] = "";
+	int call_status = 0;	/* answered(0) */
+	int end_designation;
 
-				GET_COMPONENT(comp, i, vdata, len);
-				CHECK_COMPONENT(comp, ASN1_INTEGER, "Don't know what to do if CharacterSet is of type 0x%x\n");
-				ASN1_GET_INTEGER(comp, characterSet);
-			}
-			while (0);
-
-			if (pri->debug & PRI_DEBUG_APDU)
-				pri_message(pri, "    Received extended calling name '%s', characterset %d\n", call->callername, characterSet);
-			return 0;
-		case ROSE_NAME_PRESENTATION_RESTRICTED_SIMPLE:
-		case ROSE_NAME_PRESENTATION_RESTRICTED_EXTENDED:
-		case ROSE_NAME_PRESENTATION_RESTRICTED_NULL:
-		case ROSE_NAME_NOT_AVAIL:
-		default:
-			if (pri->debug & PRI_DEBUG_APDU)
-				pri_message(pri, "Do not handle argument of type 0x%X\n", choice->type);
-			return -1;
+	/* Data checks */
+	if (sequence->type != (ASN1_CONSTRUCTOR | ASN1_SEQUENCE)) { /* Constructed Sequence */
+		pri_message(pri, "Invalid callTransferComplete argument. (Not a sequence)\n");
+		return -1;
 	}
-}
-/* ===== Call Transfer Supplementary Service (ECMA-178) ===== */
 
-static int rose_party_number_decode(struct pri *pri, q931_call *call, unsigned char *data, int len, struct addressingdataelements_presentednumberunscreened *value)
-{
-	int i = 0;
-	int size = 0;
-	struct rose_component *comp = NULL;
-	unsigned char *vdata = data;
+	if (sequence->len == ASN1_LEN_INDEF) {
+		len -= 4;   /* For the 2 extra characters at the end
+		               and two characters of header */
+	} else
+		len -= 2;
 
+	if (pri->debug & PRI_DEBUG_APDU)
+		pri_message(pri, "     CT-Complete: len=%d\n", len);
 
+	/* CTCompleteArg SEQUENCE */
 	do {
+		/* endDesignation EndDesignation */
 		GET_COMPONENT(comp, i, vdata, len);
+		CHECK_COMPONENT(comp, ASN1_ENUMERATED, "Invalid endDesignation type 0x%X of ROSE callTransferComplete component received\n");
+		ASN1_GET_INTEGER(comp, end_designation);
+		NEXT_COMPONENT(comp, i);
+		if (pri->debug & PRI_DEBUG_APDU)
+			pri_message(pri, "     CT-Complete: Received endDesignation=%s(%d)\n", enddesignation_to_str(end_designation), end_designation);
 
-		switch(comp->type) {
-			case (ASN1_CONTEXT_SPECIFIC | ASN1_CONSTRUCTOR | ASN1_TAG_0):   /* [0] IMPLICIT NumberDigits -- default: unknownPartyNumber */
-				if (pri->debug & PRI_DEBUG_APDU)
-					pri_message(pri, "     PartyNumber: UnknownPartyNumber len=%d\n", len);
-				size = rose_number_digits_decode(pri, call, comp->data, comp->len, value);
-				if (size < 0)
-					return -1;
-				value->npi = PRI_NPI_UNKNOWN;
-				value->ton = PRI_TON_UNKNOWN;
-				break;
+		/* redirectionNumber PresentedNumberScreened */
+		GET_COMPONENT(comp, i, vdata, len);
+		size = rose_presented_number_screened_decode(pri, (u_int8_t *)comp, comp->len + 2, &redirection_number);
+		if (size < 0)
+			return -1;
+		comp->len = size;
+		NEXT_COMPONENT(comp, i);
+		if (pri->debug & PRI_DEBUG_APDU)
+			pri_message(pri, "     CT-Complete: Received redirectionNumber=%s\n", redirection_number.partyaddress);
 
-			case (ASN1_CONTEXT_SPECIFIC | ASN1_CONSTRUCTOR | ASN1_TAG_1):   /* [1] IMPLICIT PublicPartyNumber */
-				if (pri->debug & PRI_DEBUG_APDU)
-					pri_message(pri, "     PartyNumber: PublicPartyNumber len=%d\n", len);
-				size = rose_public_party_number_decode(pri, call, comp->data, comp->len, value);
-				if (size < 0)
-					return -1;
-				value->npi = PRI_NPI_E163_E164;
-				break;
+		/* Type SEQUENCE specifies an ordered list of component types.           *
+		 * We decode all components but for simplicity we don't check the order. */
+		while (i < len) {
+			GET_COMPONENT(comp, i, vdata, len);
 
-			case (ASN1_CONTEXT_SPECIFIC | ASN1_CONSTRUCTOR | ASN1_TAG_3):   /* [3] IMPLICIT NumberDigits -- not used: dataPartyNumber */
-				pri_message(pri, "!! PartyNumber: dataPartyNumber is reserved!\n");
-				size = rose_number_digits_decode(pri, call, comp->data, comp->len, value);
+			switch(comp->type) {
+ 			case (ASN1_APPLICATION):
+				/* basicCallInfoElements PSS1InformationElement OPTIONAL */
+				size = asn1_name_decode((u_int8_t *)comp, comp->len + 2, basiccallinfoelements, sizeof(basiccallinfoelements));
 				if (size < 0)
 					return -1;
-				value->npi = PRI_NPI_X121 /* ??? */;
-				value->ton = PRI_TON_UNKNOWN /* ??? */;
+				i += size;
+				if (pri->debug & PRI_DEBUG_APDU) {
+					int j;
+					pri_message(pri, "     CT-Complete: Received basicCallInfoElements\n");
+					pri_message(pri, "                  ");
+					for (j = 0; basiccallinfoelements[j] != '\0'; j++)
+						pri_message(pri, "%02x ", (u_int8_t)basiccallinfoelements[j]);
+					pri_message(pri, "\n");
+				}
 				break;
-
-			case (ASN1_CONTEXT_SPECIFIC | ASN1_CONSTRUCTOR | ASN1_TAG_4):   /* [4] IMPLICIT NumberDigits -- not used: telexPartyNumber */
-				pri_message(pri, "!! PartyNumber: telexPartyNumber is reserved!\n");
-				size = rose_number_digits_decode(pri, call, comp->data, comp->len, value);
+			case (ASN1_CONTEXT_SPECIFIC | ASN1_TAG_0):                      /* [0] namePresentationAllowedSimple */
+			case (ASN1_CONTEXT_SPECIFIC | ASN1_CONSTRUCTOR | ASN1_TAG_1):   /* [1] namePresentationAllowedExtended */
+			case (ASN1_CONTEXT_SPECIFIC | ASN1_TAG_2):                      /* [2] namePresentationRestrictedSimple */
+			case (ASN1_CONTEXT_SPECIFIC | ASN1_CONSTRUCTOR | ASN1_TAG_3):   /* [3] namePresentationRestrictedExtended */
+			case (ASN1_CONTEXT_SPECIFIC | ASN1_TAG_4):                      /* [4] nameNotAvailable */
+			case (ASN1_CONTEXT_SPECIFIC | ASN1_TAG_7):                      /* [7] namePresentationRestrictedNull */
+				/* redirectionName Name OPTIONAL */
+				size = rose_name_decode(pri, (u_int8_t *)comp, comp->len + 2, &redirectionname);
 				if (size < 0)
 					return -1;
-				value->npi = PRI_NPI_F69 /* ??? */;
-				value->ton = PRI_TON_UNKNOWN /* ??? */;
+				i += size;
+				if (pri->debug & PRI_DEBUG_APDU)
+					pri_message(pri, "     CT-Complete: Received RedirectionName '%s', namepres %s(%d), characterset %s(%d)\n",
+						    redirectionname.name, namepres_to_str(redirectionname.namepres), redirectionname.namepres,
+						    characterset_to_str(redirectionname.characterset), redirectionname.characterset);
 				break;
-
-			case (ASN1_CONTEXT_SPECIFIC | ASN1_CONSTRUCTOR | ASN1_TAG_5):   /* [5] IMPLICIT PrivatePartyNumber */
+			case (ASN1_ENUMERATED):
+				/* callStatus CallStatus DEFAULT answered */
+				ASN1_GET_INTEGER(comp, call_status);
+				NEXT_COMPONENT(comp,i);
 				if (pri->debug & PRI_DEBUG_APDU)
-					pri_message(pri, "     PartyNumber: PrivatePartyNumber len=%d\n", len);
-				size = rose_private_party_number_decode(pri, call, comp->data, comp->len, value);
-				if (size < 0)
-					return -1;
- 				value->npi = PRI_NPI_PRIVATE;
+					pri_message(pri, "     CT-Complete: Received callStatus=%s(%d)\n", callstatus_to_str(call_status), call_status);
 				break;
-
-			case (ASN1_CONTEXT_SPECIFIC | ASN1_CONSTRUCTOR | ASN1_TAG_8):   /* [8] IMPLICIT NumberDigits -- not used: nationalStandatdPartyNumber */
-				pri_message(pri, "!! PartyNumber: nationalStandardPartyNumber is reserved!\n");
-				size = rose_number_digits_decode(pri, call, comp->data, comp->len, value);
-				if (size < 0)
-					return -1;
-				value->npi = PRI_NPI_NATIONAL;
-				value->ton = PRI_TON_NATIONAL;
+			case (ASN1_CONTEXT_SPECIFIC | ASN1_CONSTRUCTOR | ASN1_TAG_9):	/* [9] IMPLICIT Extension */
+			case (ASN1_CONTEXT_SPECIFIC | ASN1_CONSTRUCTOR | ASN1_TAG_10):	/* [10] IMPLICIT SEQUENCE OF Extension */
+				if (pri->debug & PRI_DEBUG_APDU)
+					pri_message(pri, "!!     CT-Complete: Ignoring CallTransferComplete component 0x%X\n", comp->type);
+				NEXT_COMPONENT(comp, i);
 				break;
-
 			default:
-				pri_message(pri, "Invalid PartyNumber component 0x%X\n", comp->type);
+				pri_message(pri, "!!     CT-Complete: Invalid CallTransferComplete component received 0x%X\n", comp->type);
 				return -1;
+			}
 		}
-		ASN1_FIXUP_LEN(comp, size);
+
 		if (pri->debug & PRI_DEBUG_APDU)
-			pri_message(pri, "     PartyNumber: '%s' size=%d len=%d\n", value->partyaddress, size, len);
-		return size;
+			pri_message(pri, "     CT-Complete: callStatus=%s(%d)\n", callstatus_to_str(call_status), call_status);
+
+		call->ctcompleteflag = 1;
+		if ((redirection_number.pres & PRES_RESTRICTION) == PRES_ALLOWED) {
+			libpri_copy_string(call->ctcompletenum, redirection_number.partyaddress, sizeof(call->ctcompletenum));
+		} else {
+			call->ctcompletenum[0] = '\0';
+		}
+		call->ctcompletepres = redirection_number.pres;
+		call->ctcompleteplan = ((redirection_number.ton & 0x07) << 4) | (redirection_number.npi & 0x0f);
+		call->ctcompletecallstatus = call_status;
+
+		if (redirectionname.namepres != 0) {
+			libpri_copy_string(call->ctcompletename, redirectionname.name, sizeof(call->ctcompletename));
+		} else {
+			call->ctcompletename[0] = '\0';
+		}
+
+		return 0;
 	}
 	while (0);
 
 	return -1;
 }
 
-
-static int rose_number_screened_decode(struct pri *pri, q931_call *call, unsigned char *data, int len, struct addressingdataelements_presentednumberscreened *value)
+static int rose_call_transfer_complete_encode(struct pri *pri, q931_call *call, int call_status)
 {
-	int i = 0;
-	int size = 0;
-	struct rose_component *comp = NULL;
-	unsigned char *vdata = data;
+	int i = 0, compsp = 0;
+	struct rose_component *comp, *compstk[10];
+	unsigned char buffer[256];
+	int size;
 
-	int scrind = -1;
+	if (pri->debug & PRI_DEBUG_APDU)
+		pri_message(pri, "    Encode CallTransferComplete\n");
 
-	do {
-		/* Party Number */
-		GET_COMPONENT(comp, i, vdata, len);
-		size = rose_party_number_decode(pri, call, (u_int8_t *)comp, comp->len + 2, (struct addressingdataelements_presentednumberunscreened*) value);
+	/* Protocol Profile = 0x1f (Networking Extensions)  (0x9f) */
+	buffer[i++] = (ASN1_CONTEXT_SPECIFIC | Q932_PROTOCOL_EXTENSIONS);
+
+	/* Network Facility Extension */
+	if (pri->switchtype == PRI_SWITCH_QSIG) {
+		/* tag component NetworkFacilityExtension (0xaa, len ) */
+		ASN1_ADD_SIMPLE(comp, COMP_TYPE_NFE, buffer, i);
+		ASN1_PUSH(compstk, compsp, comp);
+
+		/* sourceEntity  (0x80,0x01,0x00) */
+		ASN1_ADD_BYTECOMP(comp, (ASN1_CONTEXT_SPECIFIC | ASN1_TAG_0), buffer, i, 0);	/* endPINX(0) */
+
+		/* destinationEntity  (0x82,0x01,0x00) */
+		ASN1_ADD_BYTECOMP(comp, (ASN1_CONTEXT_SPECIFIC | ASN1_TAG_2), buffer, i, 0);	/* endPINX(0) */
+		ASN1_FIXUP(compstk, compsp, buffer, i);
+	}
+
+	/* Network Protocol Profile */
+	/*  - not included - */
+
+	/* Interpretation APDU  (0x8b,0x01,0x00) */
+	ASN1_ADD_BYTECOMP(comp, COMP_TYPE_INTERPRETATION, buffer, i, 0);	/* discardAnyUnrecognisedInvokePdu(0) */
+
+	/* Service APDU(s): */
+
+	/* ROSE InvokePDU  (0xa1,len) */
+	ASN1_ADD_SIMPLE(comp, COMP_TYPE_INVOKE, buffer, i);
+	ASN1_PUSH(compstk, compsp, comp);
+
+	/* ROSE InvokeID  (0x02,0x01,invokeid) */
+	ASN1_ADD_BYTECOMP(comp, ASN1_INTEGER, buffer, i, get_invokeid(pri));
+
+	/* ROSE operationId  (0x02,0x01,0x0c)*/
+	ASN1_ADD_BYTECOMP(comp, ASN1_INTEGER, buffer, i, ROSE_CALL_TRANSFER_COMPLETE);
+
+
+	/* CTCompleteArg */
+
+	/* constructor component  (0x30,len) */
+	ASN1_ADD_SIMPLE(comp, (ASN1_CONSTRUCTOR | ASN1_SEQUENCE), buffer, i);
+	ASN1_PUSH(compstk, compsp, comp);
+
+
+	/* endDesignation  (0x0a,0x01,0x00) */
+	ASN1_ADD_BYTECOMP(comp, ASN1_ENUMERATED, buffer, i, 0);		/* primaryEnd(0) */
+
+
+	/* redirectionNumber PresentedNumberScreened */
+
+	/* tag component presentationAllowedAddress  (0xa0,len) */
+	ASN1_ADD_SIMPLE(comp, (ASN1_CONTEXT_SPECIFIC | ASN1_CONSTRUCTOR | ASN1_TAG_0), buffer, i);
+	ASN1_PUSH(compstk, compsp, comp);
+
+	/* presentationAllowedAddress, implicit NumberScreened */
+	size = rose_number_screened_encode(pri, &buffer[i], 1, typeofnumber_from_q931(pri, (call->connectedplan & 0x70) >> 4), call->connectedpres & 0x03, call->connectednum);
+	if (size < 0)
+		return -1;
+	i += size;
+
+	ASN1_FIXUP(compstk, compsp, buffer, i);
+
+	/* basicCallInfoElements */
+	/*  - not included -  */
+
+#if 0
+	/* basicCallInfoElements  (0x40,0x00) */
+	ASN1_ADD_SIMPLE(comp, (ASN1_APPLICATION| ASN1_TAG_0), buffer, i);
+#endif
+
+	/* redirectionName */
+	if (call->connectedname[0]) {
+		/* tag component namePresentationAllowedSimple  (0x80,len) */
+		ASN1_ADD_SIMPLE(comp, (ASN1_CONTEXT_SPECIFIC | ASN1_TAG_0), buffer, i);
+		ASN1_PUSH(compstk, compsp, comp);
+
+		/* namePresentationAllowedSimple, implicid NameData */
+		size = rose_namedata_encode(pri, &buffer[i], 1, call->connectedname);
 		if (size < 0)
 			return -1;
-		comp->len = size;
-		NEXT_COMPONENT(comp, i);
+		i += size;
 
-		/* Screening Indicator */
-		GET_COMPONENT(comp, i, vdata, len);
-		CHECK_COMPONENT(comp, ASN1_ENUMERATED, "Don't know what to do with NumberScreened ROSE component type 0x%x\n");
-		ASN1_GET_INTEGER(comp, scrind);
-		// Todo: scrind = screeningindicator_for_q931(pri, scrind);
-		NEXT_COMPONENT(comp, i);
+		ASN1_FIXUP(compstk, compsp, buffer, i);
+	}
 
-		value->scrind = scrind;
+	if (call_status) {
+		/* callStatus  (0x0a,0x01,0x01) */
+		ASN1_ADD_BYTECOMP(comp, ASN1_ENUMERATED, buffer, i, 1);		/* alerting(1) */
+	} else {
+		/* callStatus */
+		/*  - not included, default: answered(0) -  */
+#if 0
+		/* callStatus  (0x0a,0x01,0x00) */
+		ASN1_ADD_BYTECOMP(comp, ASN1_ENUMERATED, buffer, i, 0);		/* answered(0) */
+#endif
+	}
 
-		if (pri->debug & PRI_DEBUG_APDU)
-			pri_message(pri, "     NumberScreened: '%s' ScreeningIndicator=%d  i=%d  len=%d\n", value->partyaddress, scrind, i, len);
+	ASN1_FIXUP(compstk, compsp, buffer, i);
+	ASN1_FIXUP(compstk, compsp, buffer, i);
 
-		return i-2;  // We do not have a sequence header here.
-	}
-	while (0);
+	if (pri_call_apdu_queue(call, Q931_FACILITY, buffer, i, NULL, NULL))
+		return -1;
 
-	return -1;
+	return 0;
 }
 
-
-static int rose_presented_number_screened_decode(struct pri *pri, q931_call *call, unsigned char *data, int len, struct addressingdataelements_presentednumberscreened *value)
+static int rose_call_transfer_active_decode(struct pri *pri, q931_call *call, struct rose_component *sequence, int len)
 {
 	int i = 0;
+	struct rose_component *comp = NULL;
+	unsigned char *vdata = sequence->data;
 	int size = 0;
-	struct rose_component *comp = NULL;
-	unsigned char *vdata = data;
 
-	/* Fill in default values */
-	value->ton = PRI_TON_UNKNOWN;
-	value->npi = PRI_NPI_UNKNOWN;
-	value->pres = -1; /* Data is not available */
+	struct addressingdataelements_presentedaddressscreened connectedaddress;
+	struct nameelements_name connectedname = { "", CHARACTER_SET_UNKNOWN, 0 };
+	char basiccallinfoelements[257] = "";
 
+	/* Data checks */
+	if (sequence->type != (ASN1_CONSTRUCTOR | ASN1_SEQUENCE)) { /* Constructed Sequence */
+		pri_message(pri, "Invalid callTransferActive argument. (Not a sequence)\n");
+		return -1;
+	}
+
+	if (sequence->len == ASN1_LEN_INDEF) {
+		len -= 4;   /* For the 2 extra characters at the end
+		               and two characters of header */
+	} else
+		len -= 2;
+
+	if (pri->debug & PRI_DEBUG_APDU)
+		pri_message(pri, "     CT-Active: len=%d\n", len);
+
+	/* CTActiveArg SEQUENCE */
 	do {
+		/* connectedAddress PresentedAddressScreened */
 		GET_COMPONENT(comp, i, vdata, len);
+		size = rose_presented_address_screened_decode(pri, (u_int8_t *)comp, comp->len + 2, &connectedaddress);
+		if (size < 0)
+			return -1;
+		comp->len = size;
+		NEXT_COMPONENT(comp, i);
+		if (pri->debug & PRI_DEBUG_APDU) {
+			pri_message(pri, "     CT-Active: Received connectedAddress=%s\n", connectedaddress.partyaddress);
+		}
 
-		switch(comp->type) {
-			case (ASN1_CONTEXT_SPECIFIC | ASN1_CONSTRUCTOR | ASN1_TAG_0):   /* [0] IMPLICIT presentationAllowedNumber */
-				if (pri->debug & PRI_DEBUG_APDU)
-					pri_message(pri, "     PresentedNumberScreened: presentationAllowedNumber comp->len=%d\n", comp->len);
-				value->pres = PRES_ALLOWED_USER_NUMBER_PASSED_SCREEN;
-				size = rose_number_screened_decode(pri, call, comp->data, comp->len, value);
+		/* Type SEQUENCE specifies an ordered list of component types.           *
+		 * We decode all components but for simplicity we don't check the order. */
+		while (i < len) {
+			GET_COMPONENT(comp, i, vdata, len);
+
+			switch(comp->type) {
+ 			case (ASN1_APPLICATION):
+				/* basiccallinfoelements PSS1InformationElement OPTIONAL */
+				size = asn1_name_decode((u_int8_t *)comp, comp->len + 2, basiccallinfoelements, sizeof(basiccallinfoelements));
 				if (size < 0)
 					return -1;
-				ASN1_FIXUP_LEN(comp, size);
-				return size + 2;
-
-			case (ASN1_CONTEXT_SPECIFIC | ASN1_TAG_1):    /* [1] IMPLICIT presentationRestricted */
-				if (pri->debug & PRI_DEBUG_APDU)
-					pri_message(pri, "     PresentedNumberScreened: presentationRestricted comp->len=%d\n", comp->len);
-				if (comp->len != 0) { /* must be NULL */
-					pri_error(pri, "!! Invalid PresentationRestricted component received (len != 0)\n");
-					return -1;
+				i += size;
+				if (pri->debug & PRI_DEBUG_APDU) {
+					int j;
+					pri_message(pri, "     CT-Active: Received basicCallInfoElements\n");
+					pri_message(pri, "                ");
+					for (j = 0; basiccallinfoelements[j] != '\0'; j++)
+						pri_message(pri, "%02x ", (u_int8_t)basiccallinfoelements[j]);
+					pri_message(pri, "\n");
 				}
-				value->pres = PRES_PROHIB_USER_NUMBER_PASSED_SCREEN;
-				return 2;
-
-			case (ASN1_CONTEXT_SPECIFIC | ASN1_TAG_2):    /* [2] IMPLICIT numberNotAvailableDueToInterworking */
-				if (pri->debug & PRI_DEBUG_APDU)
-					pri_message(pri, "     PresentedNumberScreened: NumberNotAvailableDueToInterworking comp->len=%d\n", comp->len);
-				if (comp->len != 0) { /* must be NULL */
-					pri_error(pri, "!! Invalid NumberNotAvailableDueToInterworking component received (len != 0)\n");
+				break;
+			case (ASN1_CONTEXT_SPECIFIC | ASN1_TAG_0):                      /* [0] namePresentationAllowedSimple */
+			case (ASN1_CONTEXT_SPECIFIC | ASN1_CONSTRUCTOR | ASN1_TAG_1):   /* [1] namePresentationAllowedExtended */
+			case (ASN1_CONTEXT_SPECIFIC | ASN1_TAG_2):                      /* [2] namePresentationRestrictedSimple */
+			case (ASN1_CONTEXT_SPECIFIC | ASN1_CONSTRUCTOR | ASN1_TAG_3):   /* [3] namePresentationRestrictedExtended */
+			case (ASN1_CONTEXT_SPECIFIC | ASN1_TAG_4):                      /* [4] nameNotAvailable */
+			case (ASN1_CONTEXT_SPECIFIC | ASN1_TAG_7):                      /* [7] namePresentationRestrictedNull */
+				/* connectedName Name OPTIONAL */
+				size = rose_name_decode(pri, (u_int8_t *)comp, comp->len + 2, &connectedname);
+				if (size < 0)
 					return -1;
-				}
-				value->pres = PRES_NUMBER_NOT_AVAILABLE;
+				i += size;
 				if (pri->debug & PRI_DEBUG_APDU)
-					pri_message(pri, "     PresentedNumberScreened: numberNotAvailableDueToInterworking Type=0x%X  i=%d len=%d size=%d\n", comp->type, i, len);
-				return 2;
-
-			case (ASN1_CONTEXT_SPECIFIC | ASN1_CONSTRUCTOR | ASN1_TAG_3):    /* [3] IMPLICIT presentationRestrictedNumber */
+					pri_message(pri, "     CT-Active: Received ConnectedName '%s', namepres %s(%d), characterset %s(%d)\n",
+						    connectedname.name, namepres_to_str(connectedname.namepres), connectedname.namepres,
+						    characterset_to_str(connectedname.characterset), connectedname.characterset);
+				break;
+			case (ASN1_CONTEXT_SPECIFIC | ASN1_CONSTRUCTOR | ASN1_TAG_9):   /* [9] IMPLICIT Extension */
+			case (ASN1_CONTEXT_SPECIFIC | ASN1_CONSTRUCTOR | ASN1_TAG_10):  /* [10] IMPLICIT SEQUENCE OF Extension */
 				if (pri->debug & PRI_DEBUG_APDU)
-					pri_message(pri, "     PresentedNumberScreened: presentationRestrictedNumber comp->len=%d\n", comp->len);
-				value->pres = PRES_PROHIB_USER_NUMBER_PASSED_SCREEN;
-				size = rose_number_screened_decode(pri, call, comp->data, comp->len, value);
-				if (size < 0)
-					return -1;
-				ASN1_FIXUP_LEN(comp, size);
-				return size + 2;
+					pri_message(pri, "!!     CT-Active: Ignoring CallTransferActive component 0x%X\n", comp->type);
+				NEXT_COMPONENT(comp, i);
+				break;
+ 			default:
+ 				pri_message(pri, "!!     CT-Active: Invalid CallTransferActive component received 0x%X\n", comp->type);
+ 				return -1;
+ 			}
+		}
 
-			default:
-				pri_message(pri, "Invalid PresentedNumberScreened component 0x%X\n", comp->type);
+		call->ctactiveflag = 1;
+		if ((connectedaddress.pres & PRES_RESTRICTION) == PRES_ALLOWED) {
+			libpri_copy_string(call->ctactivenum, connectedaddress.partyaddress, sizeof(call->ctactivenum));
+		} else {
+			call->ctactivenum[0] = '\0';
 		}
-		return -1;
+		call->ctactivepres = connectedaddress.pres;
+		call->ctactiveplan = ((connectedaddress.ton & 0x07) << 4) | (connectedaddress.npi & 0x0f);
+
+		if (connectedname.namepres != 0) {
+			libpri_copy_string(call->ctactivename, connectedname.name, sizeof(call->ctactivename));
+		} else {
+			call->ctactivename[0] = '\0';
+		}
+
+		return 0;
 	}
 	while (0);
 
 	return -1;
 }
 
-
-static int rose_call_transfer_complete_decode(struct pri *pri, q931_call *call, struct rose_component *sequence, int len)
+#if 0
+static int rose_call_transfer_update_decode(struct pri *pri, q931_call *call, struct rose_component *sequence, int len)
 {
 	int i = 0;
 	struct rose_component *comp = NULL;
 	unsigned char *vdata = sequence->data;
 	int res = 0;
 
-	int end_designation = 0;
 	struct addressingdataelements_presentednumberscreened redirection_number;
-	char redirection_name[50] = "";
-	int call_status = 0;
 	redirection_number.partyaddress[0] = 0;
-	redirection_number.partysubaddress[0] = 0;
+	char redirection_name[51] = "";
 	call->callername[0] = 0;
 	call->callernum[0] = 0;
 
-
 	/* Data checks */
 	if (sequence->type != (ASN1_CONSTRUCTOR | ASN1_SEQUENCE)) { /* Constructed Sequence */
-		pri_message(pri, "Invalid callTransferComplete argument. (Not a sequence)\n");
+		pri_message(pri, "Invalid callTransferUpdate argument. (Not a sequence)\n");
 		return -1;
 	}
 
 	if (sequence->len == ASN1_LEN_INDEF) {
-		len -= 4; /* For the 2 extra characters at the end
-					   * and two characters of header */
+		len -= 4;   /* For the 2 extra characters at the end
+		               and two characters of header */
 	} else
 		len -= 2;
 
 	if (pri->debug & PRI_DEBUG_APDU)
-		pri_message(pri, "     CT-Complete: len=%d\n", len);
+		pri_message(pri, "     CT-Update: len=%d\n", len);
 
 	do {
-		/* End Designation */
-		GET_COMPONENT(comp, i, vdata, len);
-		CHECK_COMPONENT(comp, ASN1_ENUMERATED, "Invalid endDesignation type 0x%X of ROSE callTransferComplete component received\n");
-		ASN1_GET_INTEGER(comp, end_designation);
-		NEXT_COMPONENT(comp, i);
-		if (pri->debug & PRI_DEBUG_APDU)
-			pri_message(pri, "     CT-Complete: Received endDesignation=%d\n", end_designation);
-
-
 		/* Redirection Number */
 		GET_COMPONENT(comp, i, vdata, len);
-		res = rose_presented_number_screened_decode(pri, call, (u_int8_t *)comp, comp->len + 2, &redirection_number);
+		res = rose_presented_number_screened_decode(pri, (u_int8_t *)comp, comp->len + 2, &redirection_number);
 		if (res < 0)
 			return -1;
 		comp->len = res;
 		if (res > 2) {
 			if (pri->debug & PRI_DEBUG_APDU)
-				pri_message(pri, "     CT-Complete: Received redirectionNumber=%s\n", redirection_number.partyaddress);
+				pri_message(pri, "     CT-Update: Received redirectionNumber=%s\n", redirection_number.partyaddress);
 			strncpy(call->callernum, redirection_number.partyaddress, 20);
 			call->callernum[20] = 0;
 		}
 		NEXT_COMPONENT(comp, i);
 
-
-#if 0 /* This one is optional. How do we check if it is there? */
-		/* Basic Call Info Elements */
-		GET_COMPONENT(comp, i, vdata, len);
-		NEXT_COMPONENT(comp, i);
-#endif
-
-
 		/* Redirection Name */
 		GET_COMPONENT(comp, i, vdata, len);
 		res = asn1_name_decode((u_int8_t *)comp, comp->len + 2, redirection_name, sizeof(redirection_name));
@@ -2047,16 +3336,14 @@
 		comp->len = res;
 		NEXT_COMPONENT(comp, i);
 		if (pri->debug & PRI_DEBUG_APDU)
-			pri_message(pri, "     CT-Complete: Received redirectionName '%s'\n", redirection_name);
+			pri_message(pri, "     CT-Update: Received redirectionName '%s'\n", redirection_name);
 
 
-		/* Call Status */
+#if 0 /* This one is optional. How do we check if it is there? */
+		/* Basic Call Info Elements */
 		GET_COMPONENT(comp, i, vdata, len);
-		CHECK_COMPONENT(comp, ASN1_ENUMERATED, "Invalid callStatus type 0x%X of ROSE callTransferComplete component received\n");
-		ASN1_GET_INTEGER(comp, call_status);
 		NEXT_COMPONENT(comp, i);
-		if (pri->debug & PRI_DEBUG_APDU)
-			pri_message(pri, "     CT-Complete: Received callStatus=%d\n", call_status);
+#endif
 
 
 		/* Argument Extension */
@@ -2078,8 +3365,8 @@
 				comp->len = res;
 
 			default:
-			pri_message(pri, "     CT-Complete: !! Unknown argumentExtension received 0x%X\n", comp->type);
-			return -1;
+				pri_message(pri, "     CT-Update: !! Unknown argumentExtension received 0x%X\n", comp->type);
+				return -1;
 		}
 #else
 		GET_COMPONENT(comp, i, vdata, len);
@@ -2088,7 +3375,7 @@
 #endif
 
 		if(i < len)
-			pri_message(pri, "     CT-Complete: !! not all information is handled !! i=%d / len=%d\n", i, len);
+			pri_message(pri, "     CT-Update: !! not all information is handled !! i=%d / len=%d\n", i, len);
 
 		return 0;
 	}
@@ -2096,26 +3383,422 @@
 
 	return -1;
 }
+#endif
 
+/* ===== End Call Transfer Supplementary Service (ECMA-178) ===== */
 
-static int rose_call_transfer_update_decode(struct pri *pri, q931_call *call, struct rose_component *sequence, int len)
+
+
+static int rose_calling_name_decode(struct pri *pri, q931_call *call, struct rose_component *sequence, int len)
 {
+	struct nameelements_name callingname;
+	int res;
+
+	if (pri->debug & PRI_DEBUG_APDU)
+		pri_message(pri, "    Handle callingName\n");
+
+	res = rose_name_decode(pri, (u_int8_t *)sequence, sequence->len + 2, &callingname);
+	if (res < 0)
+		return -1;
+
+	if (pri->debug & PRI_DEBUG_APDU)
+		pri_message(pri, "     Received CallingName '%s', namepres: %s(%d), characterset %s(%d)\n",
+		            callingname.name, namepres_to_str(callingname.namepres), callingname.namepres,
+		            characterset_to_str(callingname.characterset), callingname.characterset);
+
+	if (callingname.namepres >= 0) {
+		libpri_copy_string(call->callername, callingname.name, sizeof(call->callername));
+	}
+
+	return 0;
+}
+
+static int rose_called_name_decode(struct pri *pri, q931_call *call, struct rose_component *sequence, int len)
+{
+	struct nameelements_name calledname;
+	int res;
+
+	if (pri->debug & PRI_DEBUG_APDU)
+		pri_message(pri, "    Handle calledName\n");
+
+	res = rose_name_decode(pri, (u_int8_t *)sequence, sequence->len + 2, &calledname);
+	if (res < 0)
+		return -1;
+
+	if (pri->debug & PRI_DEBUG_APDU)
+		pri_message(pri, "     Received CalledName '%s', namepres %s(%d), characterset %s(%d)\n",
+		            calledname.name, namepres_to_str(calledname.namepres), calledname.namepres,
+		            characterset_to_str(calledname.characterset), calledname.characterset);
+
+	if (calledname.namepres != 0) {
+		libpri_copy_string(call->calledname, calledname.name, sizeof(call->calledname));
+	} else {
+		call->calledname[0] = '\0';
+	}
+
+	return 0;
+}
+
+int rose_called_name_encode(struct pri *pri, q931_call *call, int messagetype)
+{
+	int i = 0, compsp = 0;
+	struct rose_component *comp, *compstk[10];
+	unsigned char buffer[256];
+	int size;
+
+	if (pri->debug & PRI_DEBUG_APDU)
+		pri_message(pri, "    Encode calledName\n");
+
+	/* Protocol Profile = 0x1f (Networking Extensions)  (0x9f) */
+	buffer[i++] = (ASN1_CONTEXT_SPECIFIC | Q932_PROTOCOL_EXTENSIONS);
+
+	/* Network Facility Extension */
+	if (pri->switchtype == PRI_SWITCH_QSIG) {
+		/* tag component NetworkFacilityExtension (0xaa, len ) */
+		ASN1_ADD_SIMPLE(comp, COMP_TYPE_NFE, buffer, i);
+		ASN1_PUSH(compstk, compsp, comp);
+
+		/* sourceEntity  (0x80,0x01,0x00) */
+		ASN1_ADD_BYTECOMP(comp, (ASN1_CONTEXT_SPECIFIC | ASN1_TAG_0), buffer, i, 0);	/* endPINX(0) */
+
+		/* destinationEntity  (0x82,0x01,0x00) */
+		ASN1_ADD_BYTECOMP(comp, (ASN1_CONTEXT_SPECIFIC | ASN1_TAG_2), buffer, i, 0);	/* endPINX(0) */
+		ASN1_FIXUP(compstk, compsp, buffer, i);
+	}
+
+	/* Network Protocol Profile */
+	/*  - not included - */
+
+	/* Interpretation APDU  (0x8b,0x01,0x00) */
+	ASN1_ADD_BYTECOMP(comp, COMP_TYPE_INTERPRETATION, buffer, i, 0);	/* discardAnyUnrecognisedInvokePdu(0) */
+
+	/* Service APDU(s): */
+
+	/* ROSE InvokePDU  (0xa1,len) */
+	ASN1_ADD_SIMPLE(comp, COMP_TYPE_INVOKE, buffer, i);
+	ASN1_PUSH(compstk, compsp, comp);
+
+	/* ROSE InvokeID  (0x02,0x01,invokeid) */
+	ASN1_ADD_BYTECOMP(comp, ASN1_INTEGER, buffer, i, get_invokeid(pri));
+
+	/* ROSE operationId  (0x02,0x01,0x02)*/
+	ASN1_ADD_BYTECOMP(comp, ASN1_INTEGER, buffer, i, SS_CNOP_CALLEDNAME);
+
+	/* tag component namePresentationAllowedSimple  (0x80,len) */
+	ASN1_ADD_SIMPLE(comp, (ASN1_CONTEXT_SPECIFIC | ASN1_TAG_0), buffer, i);
+	ASN1_PUSH(compstk, compsp, comp);
+
+	/* namePresentationAllowedSimple, implicid NameData */
+	size = rose_namedata_encode(pri, &buffer[i], 1, call->connectedname);
+	if (size < 0)
+		return -1;
+	i += size;
+
+	ASN1_FIXUP(compstk, compsp, buffer, i);
+	ASN1_FIXUP(compstk, compsp, buffer, i);
+
+	if (pri_call_apdu_queue(call, messagetype, buffer, i, NULL, NULL))
+		return -1;
+
+	return 0;
+}
+
+static int rose_connected_name_decode(struct pri *pri, q931_call *call, struct rose_component *sequence, int len)
+{
+	struct nameelements_name connectedname;
+	int res;
+
+	if (pri->debug & PRI_DEBUG_APDU)
+		pri_message(pri, "    Handle connectedName\n");
+
+	res = rose_name_decode(pri, (u_int8_t *)sequence, sequence->len + 2, &connectedname);
+	if (res < 0)
+		return -1;
+
+	if (pri->debug & PRI_DEBUG_APDU)
+		pri_message(pri, "     Received ConnectedName '%s', namepres %s(%d), characterset %s(%d)\n",
+		            connectedname.name, namepres_to_str(connectedname.namepres), connectedname.namepres,
+		            characterset_to_str(connectedname.characterset), connectedname.characterset);
+
+	if (connectedname.namepres != 0) {
+		libpri_copy_string(call->connectedname, connectedname.name, sizeof(call->connectedname));
+	} else {
+		call->connectedname[0] = '\0';
+	}
+
+	return 0;
+}
+
+int rose_connected_name_encode(struct pri *pri, q931_call *call, int messagetype)
+{
+	int i = 0, compsp = 0;
+	struct rose_component *comp, *compstk[10];
+	unsigned char buffer[256];
+	int size;
+
+	if (pri->debug & PRI_DEBUG_APDU)
+		pri_message(pri, "    Encode connectedName\n");
+
+	/* Protocol Profile = 0x1f (Networking Extensions)  (0x9f) */
+	buffer[i++] = (ASN1_CONTEXT_SPECIFIC | Q932_PROTOCOL_EXTENSIONS);
+
+	/* Network Facility Extension */
+	if (pri->switchtype == PRI_SWITCH_QSIG) {
+		/* tag component NetworkFacilityExtension (0xaa, len ) */
+		ASN1_ADD_SIMPLE(comp, COMP_TYPE_NFE, buffer, i);
+		ASN1_PUSH(compstk, compsp, comp);
+
+		/* sourceEntity  (0x80,0x01,0x00) */
+		ASN1_ADD_BYTECOMP(comp, (ASN1_CONTEXT_SPECIFIC | ASN1_TAG_0), buffer, i, 0);	/* endPINX(0) */
+
+		/* destinationEntity  (0x82,0x01,0x00) */
+		ASN1_ADD_BYTECOMP(comp, (ASN1_CONTEXT_SPECIFIC | ASN1_TAG_2), buffer, i, 0);	/* endPINX(0) */
+		ASN1_FIXUP(compstk, compsp, buffer, i);
+	}
+
+	/* Network Protocol Profile */
+	/*  - not included - */
+
+	/* Interpretation APDU  (0x8b,0x01,0x00) */
+	ASN1_ADD_BYTECOMP(comp, COMP_TYPE_INTERPRETATION, buffer, i, 0);	/* discardAnyUnrecognisedInvokePdu(0) */
+
+	/* Service APDU(s): */
+
+	/* ROSE InvokePDU  (0xa1,len) */
+	ASN1_ADD_SIMPLE(comp, COMP_TYPE_INVOKE, buffer, i);
+	ASN1_PUSH(compstk, compsp, comp);
+
+	/* ROSE InvokeID  (0x02,0x01,invokeid) */
+	ASN1_ADD_BYTECOMP(comp, ASN1_INTEGER, buffer, i, get_invokeid(pri));
+
+	/* ROSE operationId  (0x02,0x01,0x02)*/
+	ASN1_ADD_BYTECOMP(comp, ASN1_INTEGER, buffer, i, SS_CNOP_CONNECTEDNAME);
+
+	/* tag component namePresentationAllowedSimple  (0x80,len) */
+	ASN1_ADD_SIMPLE(comp, (ASN1_CONTEXT_SPECIFIC | ASN1_TAG_0), buffer, i);
+	ASN1_PUSH(compstk, compsp, comp);
+
+	/* namePresentationAllowedSimple, implicid NameData */
+	size = rose_namedata_encode(pri, &buffer[i], 1, call->connectedname);
+	if (size < 0)
+		return -1;
+	i += size;
+
+	ASN1_FIXUP(compstk, compsp, buffer, i);
+	ASN1_FIXUP(compstk, compsp, buffer, i);
+
+	if (pri_call_apdu_queue(call, messagetype, buffer, i, NULL, NULL))
+		return -1;
+
+	return 0;
+}
+
+/* ===== Begin Call Completion Supplementary Service (ETS 300 366/ECMA 186) ===== */
+/* operationId e.g. QSIG_CCBSRINGOUT, QSIG_CC_CANCEL */
+int add_qsigCcInv_facility_ie (struct pri *pri, q931_call *c, int messagetype)
+{
 	int i = 0;
+	unsigned char buffer[256];
+	struct rose_component *comp = NULL, *compstk[10];
+	int compsp = 0;
+	u_int8_t operationId = c->ccoperation;
+
+	/* 1 Byte 	   0x80 | 0x1F = 9F	 Protocol Profile */
+	buffer[i++] = (ASN1_CONTEXT_SPECIFIC | Q932_PROTOCOL_EXTENSIONS);
+
+	/* Interpretation component */
+	ASN1_ADD_SIMPLE(comp, COMP_TYPE_NFE, buffer, i); /* 2. Byte NEtwork Facility Extension 0xAA = ASN1_CONTEXT_SPECIFIC(0x80) | (ASN1_CONSTRUCTOR 0x20)  0x0A (Tag laut Standard) */
+	ASN1_PUSH(compstk, compsp, comp);
+	ASN1_ADD_BYTECOMP(comp, (ASN1_CONTEXT_SPECIFIC | ASN1_TAG_0), buffer, i, 0);	/* (0x80, 0x01(len), 0x00) endPTNX */
+	ASN1_ADD_BYTECOMP(comp, (ASN1_CONTEXT_SPECIFIC | ASN1_TAG_2), buffer, i, 0);	/* (0x82, 0x01(len), 0x00) endPTNX */
+	ASN1_FIXUP(compstk, compsp, buffer, i);
+
+	ASN1_ADD_BYTECOMP(comp, COMP_TYPE_INTERPRETATION, buffer, i, 0);   /* 0x8B, 0x01(len), 0x00 discard */
+	ASN1_ADD_SIMPLE(comp, COMP_TYPE_INVOKE, buffer, i);			 /* 0xA1, 0xXX (len of Invoke Sequenz) invoke APDU */
+	ASN1_PUSH(compstk, compsp, comp);
+
+	/* Invoke ID */
+	ASN1_ADD_BYTECOMP(comp, ASN1_INTEGER, buffer, i, get_invokeid(pri));   /* 0x02 0x01 0xXX */
+
+	/* Operation ID: QSIG_CCBSRINGOUT, QSIG_CC_CANCEL */
+	ASN1_ADD_BYTECOMP(comp, ASN1_INTEGER, buffer, i, operationId); /* 0x02 0x01 0x1f/0x1c */
+
+	/* CcExtension */
+	ASN1_ADD_SIMPLE(comp, ASN1_NULL, buffer, i); /* 0x05 0x00 */
+
+	ASN1_FIXUP(compstk, compsp, buffer, i);
+
+	if (pri_call_apdu_queue(c, messagetype, buffer, i, NULL, NULL))
+		return -1;
+	
+	return 0;
+}
+
+static int rose_cc_ringout_inv_decode(struct pri *pri, struct qsig_cc_extension *cc_extension, struct rose_component *choice, int len) {
+	int i = 0;
+	cc_extension->cc_extension_tag = 0;
+
+	do {
+		switch(choice->type) {
+		case (ASN1_NULL):   /* none NULL */
+			cc_extension->cc_extension_tag = ASN1_NULL;
+			return 0;
+
+		case (ASN1_CONTEXT_SPECIFIC | ASN1_CONSTRUCTOR | ASN1_TAG_14):   /* single [14] IMPLICIT Extension */
+			cc_extension->cc_extension_tag = ASN1_TAG_14;
+			return 0;
+
+
+		case (ASN1_CONTEXT_SPECIFIC | ASN1_CONSTRUCTOR | ASN1_TAG_15):   /* multiple [15] IMPLICIT SEQUENCE OF Extension */
+			cc_extension->cc_extension_tag = ASN1_TAG_15;
+			return 0;
+
+		default:
+			if (choice->type == 0 && choice->len == 0) {
+				return 0;
+			}
+			pri_message(pri, "!! Invalid ss-cc-optional-Arg component received 0x%X\n", choice->type);
+			return -1;
+		}
+
+		if (i < len)
+			pri_message(pri, "     ss-cc-extension: !! not all information is handled !! i=%d / len=%d\n", i, len);
+
+		return 0;
+	}
+	while (0);
+
+	return -1;
+}
+
+static int rose_cc_optional_arg_decode(struct pri *pri, q931_call *call, struct qsig_cc_optional_arg *cc_optional_arg , struct rose_component *choice, int len) {
+	int i = 0;
+	int res = 0;
 	struct rose_component *comp = NULL;
+	unsigned char *vdata = choice->data;
+	struct addressingdataelements_presentednumberunscreened numberA;
+	struct addressingdataelements_presentednumberunscreened numberB;
+
+	cc_optional_arg->cc_extension.cc_extension_tag = 0;
+	cc_optional_arg->number_A[0] = '\0';
+	cc_optional_arg->number_B[0] = '\0';
+
+	do {
+		switch(choice->type) {
+		case (ASN1_CONTEXT_SPECIFIC | ASN1_CONSTRUCTOR | ASN1_TAG_0):   /* fullArg [0] IMPLICIT SEQUENCE */
+			if (pri->debug & PRI_DEBUG_APDU)
+				pri_message(pri, "     ss-cc-optional-Arg: len=%d\n", len);
+
+			numberA.partyaddress[0] = '\0';
+
+			/* numberA */
+			GET_COMPONENT(comp, i, vdata, len);
+			res += rose_party_number_decode(pri, (u_int8_t *)comp, comp->len + 2, &numberA);
+			if (res < 0)
+				return -1;
+			comp->len = res;
+			if (res > 2) {
+				if (pri->debug & PRI_DEBUG_APDU)
+					pri_message(pri, "     ss-cc-optional-Arg: Received numberA=%s\n", numberA.partyaddress);
+				strncpy(cc_optional_arg->number_A, numberA.partyaddress, 20);
+				cc_optional_arg->number_A[20] = '\0';
+			}
+			NEXT_COMPONENT(comp, i);
+
+			numberB.partyaddress[0] = '\0';
+
+			/* numberB */
+			GET_COMPONENT(comp, i, vdata, len);
+			res = rose_party_number_decode(pri, (u_int8_t *)comp, comp->len + 2, &numberB);
+			if (res < 0)
+				return -1;
+			comp->len = res;
+			if (res > 2) {
+				if (pri->debug & PRI_DEBUG_APDU)
+					pri_message(pri, "     ss-cc-optional-Arg: Received numberB=%s\n", numberB.partyaddress);
+				strncpy(cc_optional_arg->number_B, numberB.partyaddress, 20);
+				cc_optional_arg->number_B[20] = '\0';
+			}
+			NEXT_COMPONENT(comp, i);
+
+			/* service */ /* PSS1InformationElement */
+			GET_COMPONENT(comp, i, vdata, len);
+			NEXT_COMPONENT(comp, i);
+
+			/* optional */
+			for (; i < len; NEXT_COMPONENT(comp, i)) {
+				GET_COMPONENT(comp, i, vdata, len);
+				switch(comp->type) {
+				case (ASN1_NULL):   /*  */
+					cc_optional_arg->cc_extension.cc_extension_tag = ASN1_NULL;
+					NEXT_COMPONENT(comp, i);
+					break;
+				case (ASN1_CONTEXT_SPECIFIC | ASN1_CONSTRUCTOR | ASN1_TAG_14):   /*  */
+					NEXT_COMPONENT(comp, i);
+					break;
+				case (ASN1_CONTEXT_SPECIFIC | ASN1_CONSTRUCTOR | ASN1_TAG_15):   /*  */
+					NEXT_COMPONENT(comp, i);
+					break;
+				default:
+					if (comp->type == 0 && comp->len == 0) {
+						return 0;
+						break; /* Found termination characters */
+					}
+					pri_message(pri, "!! Invalid ss-cc-optional-Arg component received 0x%X\n", comp->type);
+					return -1;
+				}
+			}
+
+			if (i < len)
+				pri_message(pri, "     ss-cc-optional-Arg: !! not all information is handled !! i=%d / len=%d\n", i, len);
+
+			return 0;
+
+		/* extArg CcExtension */
+		case (ASN1_NULL):   /* none NULL */
+			cc_optional_arg->cc_extension.cc_extension_tag = ASN1_NULL;
+			return 0;
+
+		case (ASN1_CONTEXT_SPECIFIC | ASN1_CONSTRUCTOR | ASN1_TAG_14):   /* single [14] IMPLICIT Extension */
+			cc_optional_arg->cc_extension.cc_extension_tag = ASN1_TAG_14;
+			return 0;
+
+
+		case (ASN1_CONTEXT_SPECIFIC | ASN1_CONSTRUCTOR | ASN1_TAG_15):   /* multiple [15] IMPLICIT SEQUENCE OF Extension */
+			cc_optional_arg->cc_extension.cc_extension_tag = ASN1_TAG_15;
+			return 0;
+
+		default:
+			if (choice->type == 0 && choice->len == 0) {
+				return 0;
+			}
+			pri_message(pri, "!! Invalid ss-cc-optional-Arg component received 0x%X\n", choice->type);
+			return -1;
+		}
+
+		if (i < len)
+			pri_message(pri, "     ss-cc-optional-Arg: !! not all information is handled !! i=%d / len=%d\n", i, len);
+
+		return 0;
+	}
+	while (0);
+
+	return -1;
+}
+
+static int rose_cc_request_result_decode(struct pri *pri, struct qsig_cc_request_res *cc_request_res , struct rose_component *sequence, int len)
+{
+	int i = 0;
+	struct rose_component *comp = NULL;
 	unsigned char *vdata = sequence->data;
-	int res = 0;
 
-	struct addressingdataelements_presentednumberscreened redirection_number;
-	redirection_number.partyaddress[0] = 0;
-	redirection_number.partysubaddress[0] = 0;
-	char redirection_name[50] = "";
-	call->callername[0] = 0;
-	call->callernum[0] = 0;
+	cc_request_res->no_path_reservation           = 0;  /* Default FALSE */
+	cc_request_res->retain_service                = 0;  /* Default FALSE */
+	cc_request_res->cc_extension.cc_extension_tag = 0;
 
-
 	/* Data checks */
 	if (sequence->type != (ASN1_CONSTRUCTOR | ASN1_SEQUENCE)) { /* Constructed Sequence */
-		pri_message(pri, "Invalid callTransferComplete argument. (Not a sequence)\n");
+		pri_message(pri, "Invalid cc request result argument. (Not a sequence)\n");
 		return -1;
 	}
 
@@ -2126,75 +3809,55 @@
 		len -= 2;
 
 	if (pri->debug & PRI_DEBUG_APDU)
-		pri_message(pri, "     CT-Complete: len=%d\n", len);
+		pri_message(pri, "     CC-request-Return-Result: len=%d\n", len);
 
 	do {
-		/* Redirection Number */
-		GET_COMPONENT(comp, i, vdata, len);
-		res = rose_presented_number_screened_decode(pri, call, (u_int8_t *)comp, comp->len + 2, &redirection_number);
-		if (res < 0)
-			return -1;
-		comp->len = res;
-		if (res > 2) {
-			if (pri->debug & PRI_DEBUG_APDU)
-				pri_message(pri, "     CT-Complete: Received redirectionNumber=%s\n", redirection_number.partyaddress);
-			strncpy(call->callernum, redirection_number.partyaddress, 20);
-			call->callernum[20] = 0;
-		}
-		NEXT_COMPONENT(comp, i);
+		/* defaults and optional */
+		for (; i < len; NEXT_COMPONENT(comp, i)) {
+			GET_COMPONENT(comp, i, vdata, len);
+			switch(comp->type) {
+			case (ASN1_CONTEXT_SPECIFIC | ASN1_TAG_0):
+				/* no-path-reservation */
+				ASN1_GET_INTEGER(comp, cc_request_res->no_path_reservation);
+				NEXT_COMPONENT(comp, i);
+				if (pri->debug & PRI_DEBUG_APDU)
+					pri_message(pri, "     cc request result: Received noPathReservation=%d\n", cc_request_res->no_path_reservation);
+				break;
 
-		/* Redirection Name */
-		GET_COMPONENT(comp, i, vdata, len);
-		res = asn1_name_decode((u_int8_t *)comp, comp->len + 2, redirection_name, sizeof(redirection_name));
-		if (res < 0)
-			return -1;
-		memcpy(call->callername, comp->data, comp->len);
-		call->callername[comp->len] = 0;
-		ASN1_FIXUP_LEN(comp, res);
-		comp->len = res;
-		NEXT_COMPONENT(comp, i);
-		if (pri->debug & PRI_DEBUG_APDU)
-			pri_message(pri, "     CT-Complete: Received redirectionName '%s'\n", redirection_name);
+			case (ASN1_CONTEXT_SPECIFIC | ASN1_TAG_1):
+				/* retain_service */
+				ASN1_GET_INTEGER(comp, cc_request_res->retain_service);
+				NEXT_COMPONENT(comp, i);
+				if (pri->debug & PRI_DEBUG_APDU)
+					pri_message(pri, "     cc request result: Received retainService=%d\n", cc_request_res->retain_service);
+				break;
 
+			case (ASN1_NULL):   /*  */
+				cc_request_res->cc_extension.cc_extension_tag = ASN1_NULL;
+				NEXT_COMPONENT(comp, i);
+				break;
 
-#if 0 /* This one is optional. How do we check if it is there? */
-		/* Basic Call Info Elements */
-		GET_COMPONENT(comp, i, vdata, len);
-		NEXT_COMPONENT(comp, i);
-#endif
+			case (ASN1_CONTEXT_SPECIFIC | ASN1_CONSTRUCTOR | ASN1_TAG_14):
+				cc_request_res->cc_extension.cc_extension_tag = ASN1_TAG_14;
+				NEXT_COMPONENT(comp, i);
+				break;
 
+			case (ASN1_CONTEXT_SPECIFIC | ASN1_CONSTRUCTOR | ASN1_TAG_15):
+				cc_request_res->cc_extension.cc_extension_tag = ASN1_TAG_15;
+				NEXT_COMPONENT(comp, i);
+				break;
 
-		/* Argument Extension */
-#if 0 /* Not supported */
-		GET_COMPONENT(comp, i, vdata, len);
-		switch (comp->type) {
-			case (ASN1_CONTEXT_SPECIFIC | ASN1_TAG_9):   /* [9] IMPLICIT Extension */
-				res = rose_extension_decode(pri, call, comp->data, comp->len, &redirection_number);
-				if (res < 0)
-					return -1;
-				ASN1_FIXUP_LEN(comp, res);
-				comp->len = res;
-
-			case (ASN1_CONTEXT_SPECIFIC | ASN1_TAG_10):    /* [10] IMPLICIT SEQUENCE OF Extension */
-				res = rose_sequence_of_extension_decode(pri, call, comp->data, comp->len, &redirection_number);
-				if (res < 0)
-					return -1;
-				ASN1_FIXUP_LEN(comp, res);
-				comp->len = res;
-
 			default:
-				pri_message(pri, "     CT-Complete: !! Unknown argumentExtension received 0x%X\n", comp->type);
+				if (comp->type == 0 && comp->len == 0) {
+					break; /* Found termination characters */
+				}
+				pri_message(pri, "!! Invalid ss-cc-optional-Arg component received 0x%X\n", comp->type);
 				return -1;
+			}
 		}
-#else
-		GET_COMPONENT(comp, i, vdata, len);
-		ASN1_FIXUP_LEN(comp, res);
-		NEXT_COMPONENT(comp, i);
-#endif
 
-		if(i < len)
-			pri_message(pri, "     CT-Complete: !! not all information is handled !! i=%d / len=%d\n", i, len);
-
+		if (i < len)
+			pri_message(pri, "     ss-cc-optional-Arg: !! not all information is handled !! i=%d / len=%d\n", i, len);
 		return 0;
 	}
 	while (0);
@@ -2202,11 +3865,18 @@
 	return -1;
 }
 
+static int rose_ccbs_request_result_decode(struct pri *pri, struct qsig_cc_request_res *cc_request_res , struct rose_component *sequence, int len)
+{
+	return rose_cc_request_result_decode(pri, cc_request_res , sequence, len);
+}
 
-/* ===== End Call Transfer Supplementary Service (ECMA-178) ===== */
+static int rose_ccnr_request_result_decode(struct pri *pri, struct qsig_cc_request_res *cc_request_res , struct rose_component *sequence, int len)
+{
+	return rose_cc_request_result_decode(pri, cc_request_res , sequence, len);
+}
+/* ===== End Call Completion Supplementary Service (ETS 300 366/ECMA 186) ===== */
 
 
-
 int rose_reject_decode(struct pri *pri, q931_call *call, q931_ie *ie, unsigned char *data, int len)
 {
 	int i = 0;
@@ -2267,7 +3937,7 @@
 
 			return 0;
 		} else {
-			pri_message(pri, "Unable to handle return result on switchtype %d!\n", pri->switchtype);
+			pri_message(pri, "Unable to handle reject on switchtype %d!\n", pri->switchtype);
 			return -1;
 		}
 
@@ -2275,6 +3945,20 @@
 	
 	return -1;
 }
+
+
+static struct subcommand *get_ptr_subcommand(struct subcommands *sub)
+{
+	if (sub->counter_subcmd < MAX_SUBCOMMANDS) {
+		int count = sub->counter_subcmd;
+		sub->counter_subcmd++;
+		return &sub->subcmd[count];
+	}
+
+	return NULL;
+}
+
+
 int rose_return_error_decode(struct pri *pri, q931_call *call, q931_ie *ie, unsigned char *data, int len)
 {
 	int i = 0;
@@ -2283,6 +3967,7 @@
 	unsigned char *vdata = data;
 	struct rose_component *comp = NULL;
 	char *invokeidstr, *errorstr;
+	struct subcommand *c_subcmd;
 	
 	do {
 		/* Invoke ID stuff */
@@ -2329,9 +4014,39 @@
 			pri_error(pri, "\tERROR: %s\n", errorstr);
 
 			return 0;
+		} else if (pri->switchtype == PRI_SWITCH_QSIG) {
+			switch (errorvalue) {
+			case 1008:
+				errorstr = "Unspecified";
+				break;
+			case 1012:
+				errorstr = "Remote user busy again";
+				break;
+			case 1013:
+				errorstr = "Failure to match";
+				break;
+			default:
+				errorstr = "Unknown";
+			}
+
+			c_subcmd = get_ptr_subcommand(&call->subcmds);
+			if (!c_subcmd) {
+				if (pri->debug & PRI_DEBUG_APDU)
+					pri_message(pri, "ROSE RETURN ERROR %i - more than %d facilities !\n", errorvalue, MAX_SUBCOMMANDS);
+				dump_apdu (pri, (u_int8_t *)comp, comp->len + 2);
+				return -1;
+			}
+
+			if (pri->debug & PRI_DEBUG_APDU)
+			{
+				pri_message(pri, "ROSE RETURN RESULT %i:   %s\n", errorvalue, errorstr);
+				dump_apdu (pri, (u_int8_t *)comp, comp->len + 2);
+			}
+			c_subcmd->cmd = CMD_CC_ERROR;
+			c_subcmd->cc_error.error_value = errorvalue;
+			return 0;
 		} else {
-			pri_message(pri, "Unable to handle return result on switchtype %d!\n", pri->switchtype);
-			return -1;
+			pri_message(pri, "Unable to handle return error on switchtype %d!\n", pri->switchtype);
 		}
 
 	} while(0);
@@ -2346,6 +4061,8 @@
 	int invokeidvalue = -1;
 	unsigned char *vdata = data;
 	struct rose_component *comp = NULL;
+	int res;
+	struct subcommand *c_subcmd;
 	
 	do {
 		/* Invoke ID stuff */
@@ -2392,11 +4109,90 @@
 				return -1;
 			}
 		} else if (pri->switchtype == PRI_SWITCH_QSIG) {
-			switch (invokeidvalue) {
-			case 0x13:
-				if (pri->debug & PRI_DEBUG_APDU) pri_message(pri, "Successfully completed QSIG CF callRerouting!\n");
+			int operation_tag;
+
+			/* sequence is optional */
+			if (i >= len) 
 				return 0;
+
+			/* Data checks, sequence is optional */
+			GET_COMPONENT(comp, i, vdata, len);
+			if (comp->type != (ASN1_CONSTRUCTOR | ASN1_SEQUENCE)) { /* Constructed Sequence */
+				pri_message(pri, "No arguments on cc-return result\n");
+				return 0;
 			}
+
+			if (comp->len == ASN1_LEN_INDEF) {
+				len -= 2; /* For the 2 extra characters at the end*/
+			}
+
+			/* Traverse the contents of this sequence */
+			SUB_COMPONENT(comp, i);
+
+			/* Operation Tag */
+			GET_COMPONENT(comp, i, vdata, len);
+			CHECK_COMPONENT(comp, ASN1_INTEGER, "Don't know what to do if second ROSE component is of type 0x%x\n");
+			ASN1_GET_INTEGER(comp, operation_tag);
+			NEXT_COMPONENT(comp, i);
+
+			/* No argument - return with error */
+			if (i >= len) 
+				return -1;
+
+			/* Arguement Tag */
+			GET_COMPONENT(comp, i, vdata, len);
+			if (!comp->type)
+				return -1;
+
+			if (pri->debug & PRI_DEBUG_APDU)
+				pri_message(pri, "  [ Handling operation %d ]\n", operation_tag);
+			switch (operation_tag) {
+			case QSIG_CF_CALLREROUTING:
+				if (pri->debug & PRI_DEBUG_APDU)
+					pri_message(pri, "Successfully completed QSIG CF callRerouting!\n");
+				return 0;
+
+			case QSIG_CC_CCBSREQUEST:
+				c_subcmd = get_ptr_subcommand(&call->subcmds);
+				if (!c_subcmd) {
+					if (pri->debug & PRI_DEBUG_APDU)
+						pri_message(pri, "ROSE %i:  return_result CcCcbsRequest - more than %d facilities !\n", operation_tag, MAX_SUBCOMMANDS);
+					dump_apdu (pri, (u_int8_t *)comp, comp->len + 2);
+					return -1;
+				}
+				if (pri->debug & PRI_DEBUG_APDU)
+				{
+					pri_message(pri, "ROSE %i:   Handle CcCcbsRequest\n", operation_tag);
+					dump_apdu (pri, (u_int8_t *)comp, comp->len + 2);
+				}
+				c_subcmd->cmd = CMD_CC_CCBSREQUEST_RR;
+				res = rose_ccbs_request_result_decode(pri, &c_subcmd->cc_ccbs_rr.cc_request_res, comp, len-i);
+				return res;
+
+			case QSIG_CC_CCNRREQUEST:
+				c_subcmd = get_ptr_subcommand(&call->subcmds);
+				if (!c_subcmd) {
+					if (pri->debug & PRI_DEBUG_APDU)
+						pri_message(pri, "ROSE %i:  return_result CcCcnrRequest - more than %d facilities !\n", operation_tag, MAX_SUBCOMMANDS);
+					dump_apdu (pri, (u_int8_t *)comp, comp->len + 2);
+					return -1;
+				}
+				if (pri->debug & PRI_DEBUG_APDU)
+				{
+					pri_message(pri, "ROSE %i:   Handle CcCcnrRequest\n", operation_tag);
+					dump_apdu (pri, (u_int8_t *)comp, comp->len + 2);
+				}
+				c_subcmd->cmd = CMD_CC_CCNRREQUEST_RR;
+				res = rose_ccnr_request_result_decode(pri, &c_subcmd->cc_ccnr_rr.cc_request_res, comp, len-i);
+				return res;
+
+			default:
+				if (pri->debug & PRI_DEBUG_APDU) {
+					pri_message(pri, "!! Unable to handle ROSE operation %d", operation_tag);
+					dump_apdu (pri, (u_int8_t *)comp, comp->len + 2);
+				}
+				return -1;
+			}
 		} else {
 			pri_message(pri, "Unable to handle return result on switchtype %d!\n", pri->switchtype);
 			return -1;
@@ -2414,6 +4210,7 @@
 	int operation_tag;
 	unsigned char *vdata = data;
 	struct rose_component *comp = NULL, *invokeid = NULL, *operationid = NULL;
+	struct subcommand *c_subcmd;
 	
 	do {
 		/* Invoke ID stuff */
@@ -2444,126 +4241,294 @@
 
 		if (pri->debug & PRI_DEBUG_APDU)
 			pri_message(pri, "  [ Handling operation %d ]\n", operation_tag);
-		switch (operation_tag) {
-		case SS_CNID_CALLINGNAME:
-			if (pri->debug & PRI_DEBUG_APDU)
-				pri_message(pri, "  Handle Name display operation\n");
- 			return rose_calling_name_decode(pri, call, comp, len-i);
-		case ROSE_CALL_TRANSFER_IDENTIFY:
-			if (pri->debug & PRI_DEBUG_APDU)
-				pri_message(pri, "ROSE %i:   CallTransferIdentify - not handled!\n", operation_tag);
-			dump_apdu (pri, (u_int8_t *)comp, comp->len + 2);
-			return -1;
-		case ROSE_CALL_TRANSFER_ABANDON:
-			if (pri->debug & PRI_DEBUG_APDU)
-				pri_message(pri, "ROSE %i:   CallTransferAbandon - not handled!\n", operation_tag);
-			dump_apdu (pri, (u_int8_t *)comp, comp->len + 2);
-			return -1;
-		case ROSE_CALL_TRANSFER_INITIATE:
-			if (pri->debug & PRI_DEBUG_APDU)
-				pri_message(pri, "ROSE %i:   CallTransferInitiate - not handled!\n", operation_tag);
-			dump_apdu (pri, (u_int8_t *)comp, comp->len + 2);
-			return -1;
-		case ROSE_CALL_TRANSFER_SETUP:
-			if (pri->debug & PRI_DEBUG_APDU)
-				pri_message(pri, "ROSE %i:   CallTransferSetup - not handled!\n", operation_tag);
-			dump_apdu (pri, (u_int8_t *)comp, comp->len + 2);
-			return -1;
-		case ROSE_CALL_TRANSFER_ACTIVE:
-			if (pri->debug & PRI_DEBUG_APDU)
-				pri_message(pri, "ROSE %i:   CallTransferActive - not handled!\n", operation_tag);
-			dump_apdu (pri, (u_int8_t *)comp, comp->len + 2);
-			return -1;
-		case ROSE_CALL_TRANSFER_COMPLETE:
-			if (pri->debug & PRI_DEBUG_APDU)
-			{
-				pri_message(pri, "ROSE %i:   Handle CallTransferComplete\n", operation_tag);
+
+		if (pri->switchtype == PRI_SWITCH_QSIG) {
+
+			switch (operation_tag) {
+			case SS_CNID_CALLINGNAME:
+				if (pri->debug & PRI_DEBUG_APDU) {
+					pri_message(pri, "ROSE %i:   Handle CallingName\n", operation_tag);
+					dump_apdu (pri, (u_int8_t *)comp, comp->len + 2);
+				}
+				return rose_calling_name_decode(pri, call, comp, len-i);
+			case SS_CNOP_CALLEDNAME:
+				if (pri->debug & PRI_DEBUG_APDU) {
+					pri_message(pri, "ROSE %i:   Handle CalledName\n", operation_tag);
+					dump_apdu (pri, (u_int8_t *)comp, comp->len + 2);
+				}
+				return rose_called_name_decode(pri, call, comp, len-i);
+			case SS_CNOP_CONNECTEDNAME:
+				if (pri->debug & PRI_DEBUG_APDU) {
+					pri_message(pri, "ROSE %i:   Handle ConnectedName\n", operation_tag);
+					dump_apdu (pri, (u_int8_t *)comp, comp->len + 2);
+				}
+				return rose_connected_name_decode(pri, call, comp, len-i);
+			case ROSE_CALL_TRANSFER_IDENTIFY:
+				if (pri->debug & PRI_DEBUG_APDU)
+					pri_message(pri, "ROSE %i:   CallTransferIdentify - not handled!\n", operation_tag);
 				dump_apdu (pri, (u_int8_t *)comp, comp->len + 2);
-			}
-			return rose_call_transfer_complete_decode(pri, call, comp, len-i);
-		case ROSE_CALL_TRANSFER_UPDATE:
-			if (pri->debug & PRI_DEBUG_APDU)
-			{
-				pri_message(pri, "ROSE %i:    Handle CallTransferUpdate\n", operation_tag);
+				return -1;
+			case ROSE_CALL_TRANSFER_ABANDON:
+				if (pri->debug & PRI_DEBUG_APDU)
+					pri_message(pri, "ROSE %i:   CallTransferAbandon - not handled!\n", operation_tag);
 				dump_apdu (pri, (u_int8_t *)comp, comp->len + 2);
-			}
-			return rose_call_transfer_update_decode(pri, call, comp, len-i);
-		case ROSE_SUBADDRESS_TRANSFER:
-			if (pri->debug & PRI_DEBUG_APDU)
-				pri_message(pri, "ROSE %i:   SubaddressTransfer - not handled!\n", operation_tag);
-			dump_apdu (pri, (u_int8_t *)comp, comp->len + 2);
-			return -1;
-		case ROSE_DIVERTING_LEG_INFORMATION2:
-			if (pri->debug & PRI_DEBUG_APDU) {
- 				pri_message(pri, "ROSE %i:   Handle CallingName\n", operation_tag);
- 				dump_apdu (pri, (u_int8_t *)comp, comp->len + 2);
-  			}
-			return rose_diverting_leg_information2_decode(pri, call, comp, len-i);
-		case ROSE_AOC_NO_CHARGING_INFO_AVAILABLE:
-			if (pri->debug & PRI_DEBUG_APDU) {
-				pri_message(pri, "ROSE %i: AOC No Charging Info Available - not handled!", operation_tag);
-				dump_apdu (pri, comp->data, comp->len);
-			}
-			return -1;
-		case ROSE_AOC_CHARGING_REQUEST:
-			return aoc_aoce_charging_request_decode(pri, call, (u_int8_t *)comp, comp->len + 2);
-		case ROSE_AOC_AOCS_CURRENCY:
-			if (pri->debug & PRI_DEBUG_APDU) {
-				pri_message(pri, "ROSE %i: AOC-S Currency - not handled!", operation_tag);
+				return -1;
+			case ROSE_CALL_TRANSFER_INITIATE:
+				if (pri->debug & PRI_DEBUG_APDU)
+					pri_message(pri, "ROSE %i:   CallTransferInitiate - not handled!\n", operation_tag);
 				dump_apdu (pri, (u_int8_t *)comp, comp->len + 2);
-			}
-			return -1;
-		case ROSE_AOC_AOCS_SPECIAL_ARR:
-			if (pri->debug & PRI_DEBUG_APDU) {
-				pri_message(pri, "ROSE %i: AOC-S Special Array - not handled!", operation_tag);
+				return -1;
+			case ROSE_CALL_TRANSFER_SETUP:
+				if (pri->debug & PRI_DEBUG_APDU)
+					pri_message(pri, "ROSE %i:   CallTransferSetup - not handled!\n", operation_tag);
 				dump_apdu (pri, (u_int8_t *)comp, comp->len + 2);
-			}
-			return -1;
-		case ROSE_AOC_AOCD_CURRENCY:
-			if (pri->debug & PRI_DEBUG_APDU) {
-				pri_message(pri, "ROSE %i: AOC-D Currency - not handled!", operation_tag);
+				return -1;
+			case ROSE_CALL_TRANSFER_ACTIVE:
+				if (pri->debug & PRI_DEBUG_APDU)
+					pri_message(pri, "ROSE %i:   Handle CallTransferActive\n", operation_tag);
 				dump_apdu (pri, (u_int8_t *)comp, comp->len + 2);
-			}
-			return -1;
-		case ROSE_AOC_AOCD_CHARGING_UNIT:
-			if (pri->debug & PRI_DEBUG_APDU) {
-				pri_message(pri, "ROSE %i: AOC-D Charging Unit - not handled!", operation_tag);
+				return rose_call_transfer_active_decode(pri, call, comp, len-i);
+			case ROSE_CALL_TRANSFER_COMPLETE:
+				if (pri->debug & PRI_DEBUG_APDU)
+				{
+					pri_message(pri, "ROSE %i:   Handle CallTransferComplete\n", operation_tag);
+					dump_apdu (pri, (u_int8_t *)comp, comp->len + 2);
+				}
+				return rose_call_transfer_complete_decode(pri, call, comp, len-i);
+			case ROSE_CALL_TRANSFER_UPDATE:
+				if (pri->debug & PRI_DEBUG_APDU)
+					pri_message(pri, "ROSE %i:   CallTransferUpdate - not handled!\n", operation_tag);
 				dump_apdu (pri, (u_int8_t *)comp, comp->len + 2);
-			}
-			return -1;
-		case ROSE_AOC_AOCE_CURRENCY:
-			if (pri->debug & PRI_DEBUG_APDU) {
-				pri_message(pri, "ROSE %i: AOC-E Currency - not handled!", operation_tag);
+				return -1;
+			case ROSE_SUBADDRESS_TRANSFER:
+				if (pri->debug & PRI_DEBUG_APDU)
+					pri_message(pri, "ROSE %i:   SubaddressTransfer - not handled!\n", operation_tag);
 				dump_apdu (pri, (u_int8_t *)comp, comp->len + 2);
-			}
-			return -1;
-		case ROSE_AOC_AOCE_CHARGING_UNIT:
-			return aoc_aoce_charging_unit_decode(pri, call, (u_int8_t *)comp, comp->len + 2);
-			if (0) { /* the following function is currently not used - just to make the compiler happy */
-				aoc_aoce_charging_unit_encode(pri, call, call->aoc_units); /* use this function to forward the aoc-e on a bridged channel */ 
+				return -1;
+			case ROSE_DIVERTING_LEG_INFORMATION1:
+				if (pri->debug & PRI_DEBUG_APDU)
+					pri_message(pri, "  Handle DivertingLegInformation1\n");
+				return rose_diverting_leg_information1_decode(pri, call, comp, len-i);
+			case ROSE_DIVERTING_LEG_INFORMATION2:
+				if (pri->debug & PRI_DEBUG_APDU)
+					pri_message(pri, "  Handle DivertingLegInformation2\n");
+				return rose_diverting_leg_information2_decode(pri, call, comp, len-i);
+			case ROSE_DIVERTING_LEG_INFORMATION3:
+				if (pri->debug & PRI_DEBUG_APDU)
+					pri_message(pri, "  Handle DivertingLegInformation3\n");
+				return rose_diverting_leg_information3_decode(pri, call, comp, len-i);
+			case SS_ANFPR_PATHREPLACEMENT:
+				/* Clear Queue */
+				res = pri_call_apdu_queue_cleanup(call->bridged_call);
+				if (res) {
+					pri_message(pri, "Could not Clear queue ADPU\n");
+					return -1;
+				}
+				anfpr_pathreplacement_respond(pri, call, ie);
+				break;
+			case QSIG_CC_CCBSREQUEST:
+				if (pri->debug & PRI_DEBUG_APDU)
+					pri_message(pri, "ROSE %i:  invoke CcbsRequest - not handled!\n", operation_tag);
+				dump_apdu (pri, (u_int8_t *)comp, comp->len + 2);
+				return -1;
+			case QSIG_CC_CCNRREQUEST:
+				if (pri->debug & PRI_DEBUG_APDU)
+					pri_message(pri, "ROSE %i:  invoke CcnrRequest - not handled!\n", operation_tag);
+				dump_apdu (pri, (u_int8_t *)comp, comp->len + 2);
+				return -1;
+			case QSIG_CC_CANCEL:
+				c_subcmd = get_ptr_subcommand(&call->subcmds);
+				if (!c_subcmd) {
+					if (pri->debug & PRI_DEBUG_APDU)
+						pri_message(pri, "ROSE %i:  invoke CcCancel - more than %d facilities !\n", operation_tag, MAX_SUBCOMMANDS);
+					dump_apdu (pri, (u_int8_t *)comp, comp->len + 2);
+					return -1;
+				}
+				if (pri->debug & PRI_DEBUG_APDU)
+				{
+					pri_message(pri, "ROSE %i:   Handle CcCancel\n", operation_tag);
+					dump_apdu (pri, (u_int8_t *)comp, comp->len + 2);
+				}
+				c_subcmd->cmd = CMD_CC_CANCEL_INV;
+				res = rose_cc_optional_arg_decode(pri, call, &c_subcmd->cc_cancel_inv.cc_optional_arg, comp, len-i);
+				return res;
+			case QSIG_CC_EXECPOSIBLE:
+				c_subcmd = get_ptr_subcommand(&call->subcmds);
+				if (!c_subcmd) {
+					if (pri->debug & PRI_DEBUG_APDU)
+						pri_message(pri, "ROSE %i:  invoke CcExecposible - more than %d facilities !\n", operation_tag, MAX_SUBCOMMANDS);
+					dump_apdu (pri, (u_int8_t *)comp, comp->len + 2);
+					return -1;
+				}
+				if (pri->debug & PRI_DEBUG_APDU)
+				{
+					pri_message(pri, "ROSE %i:   Handle CcExecposible\n", operation_tag);
+					dump_apdu (pri, (u_int8_t *)comp, comp->len + 2);
+				}
+				c_subcmd->cmd = CMD_CC_EXECPOSIBLE_INV;
+				res = rose_cc_optional_arg_decode(pri, call, &c_subcmd->cc_execposible_inv.cc_optional_arg, comp, len-i);
+				return res;
+			case QSIG_CC_PATHRESERVE:
+				if (pri->debug & PRI_DEBUG_APDU)
+					pri_message(pri, "ROSE %i:  invoke CcPathreserve - not handled!\n", operation_tag);
+				dump_apdu (pri, (u_int8_t *)comp, comp->len + 2);
+				return -1;
+			case QSIG_CC_RINGOUT:
+				c_subcmd = get_ptr_subcommand(&call->subcmds);
+				if (!c_subcmd) {
+					if (pri->debug & PRI_DEBUG_APDU)
+						pri_message(pri, "ROSE %i:  invoke CcRingout - more than %d facilities !\n", operation_tag, MAX_SUBCOMMANDS);
+					dump_apdu (pri, (u_int8_t *)comp, comp->len + 2);
+					return -1;
+				}
+				if (pri->debug & PRI_DEBUG_APDU)
+				{
+					pri_message(pri, "ROSE %i:   Handle CcRingout\n", operation_tag);
+					dump_apdu (pri, (u_int8_t *)comp, comp->len + 2);
+				}
+				c_subcmd->cmd = CMD_CC_RINGOUT_INV;
+				res = rose_cc_ringout_inv_decode(pri, &c_subcmd->cc_ringout_inv.cc_extension, comp, len-i);
+				return res;
+			case QSIG_CC_SUSPEND:
+				if (pri->debug & PRI_DEBUG_APDU)
+				{
+					pri_message(pri, "ROSE %i:   Handle CcSuspend\n", operation_tag);
+					dump_apdu (pri, (u_int8_t *)comp, comp->len + 2);
+				}
 				return 0;
-			}
-		case ROSE_AOC_IDENTIFICATION_OF_CHARGE:
-			if (pri->debug & PRI_DEBUG_APDU) {
-				pri_message(pri, "ROSE %i: AOC Identification Of Charge - not handled!", operation_tag);
+			case QSIG_CC_RESUME:
+				if (pri->debug & PRI_DEBUG_APDU)
+					pri_message(pri, "ROSE %i:  invoke CcResume - not handled!\n", operation_tag);
 				dump_apdu (pri, (u_int8_t *)comp, comp->len + 2);
+				return -1;
+			default:
+				if (pri->debug & PRI_DEBUG_APDU) {
+					pri_message(pri, "!! Unable to handle ROSE operation %d", operation_tag);
+					dump_apdu (pri, (u_int8_t *)comp, comp->len + 2);
+				}
+				return -1;
 			}
-			return -1;
-                case SS_ANFPR_PATHREPLACEMENT:
-			/* Clear Queue */
-			res = pri_call_apdu_queue_cleanup(call->bridged_call);
-			if (res) {
-			        pri_message(pri, "Could not Clear queue ADPU\n");
-			        return -1;
-			}
-			anfpr_pathreplacement_respond(pri, call, ie);
-                        break;
-		default:
-			if (pri->debug & PRI_DEBUG_APDU) {
-				pri_message(pri, "!! Unable to handle ROSE operation %d", operation_tag);
+		} else { /* pri->switchtype == PRI_SWITCH_QSIG */
+
+			switch (operation_tag) {
+			case SS_CNID_CALLINGNAME:
+				if (pri->debug & PRI_DEBUG_APDU) {
+					pri_message(pri, "ROSE %i:   Handle CallingName\n", operation_tag);
+					dump_apdu (pri, (u_int8_t *)comp, comp->len + 2);
+				}
+				return rose_calling_name_decode(pri, call, comp, len-i);
+			case ROSE_CALL_TRANSFER_IDENTIFY:
+				if (pri->debug & PRI_DEBUG_APDU)
+					pri_message(pri, "ROSE %i:   CallTransferIdentify - not handled!\n", operation_tag);
 				dump_apdu (pri, (u_int8_t *)comp, comp->len + 2);
+				return -1;
+			case ROSE_CALL_TRANSFER_ABANDON:
+				if (pri->debug & PRI_DEBUG_APDU)
+					pri_message(pri, "ROSE %i:   CallTransferAbandon - not handled!\n", operation_tag);
+				dump_apdu (pri, (u_int8_t *)comp, comp->len + 2);
+				return -1;
+			case ROSE_CALL_TRANSFER_INITIATE:
+				if (pri->debug & PRI_DEBUG_APDU)
+					pri_message(pri, "ROSE %i:   CallTransferInitiate - not handled!\n", operation_tag);
+				dump_apdu (pri, (u_int8_t *)comp, comp->len + 2);
+				return -1;
+			case ROSE_CALL_TRANSFER_SETUP:
+				if (pri->debug & PRI_DEBUG_APDU)
+					pri_message(pri, "ROSE %i:   CallTransferSetup - not handled!\n", operation_tag);
+				dump_apdu (pri, (u_int8_t *)comp, comp->len + 2);
+				return -1;
+			case ROSE_CALL_TRANSFER_ACTIVE:
+				if (pri->debug & PRI_DEBUG_APDU)
+					pri_message(pri, "ROSE %i:   Handle CallTransferActive\n", operation_tag);
+				dump_apdu (pri, (u_int8_t *)comp, comp->len + 2);
+				return rose_call_transfer_active_decode(pri, call, comp, len-i);
+			case ROSE_CALL_TRANSFER_COMPLETE:
+				if (pri->debug & PRI_DEBUG_APDU)
+				{
+					pri_message(pri, "ROSE %i:   Handle CallTransferComplete\n", operation_tag);
+					dump_apdu (pri, (u_int8_t *)comp, comp->len + 2);
+				}
+				return rose_call_transfer_complete_decode(pri, call, comp, len-i);
+			case ROSE_CALL_TRANSFER_UPDATE:
+				if (pri->debug & PRI_DEBUG_APDU)
+					pri_message(pri, "ROSE %i:   CallTransferUpdate - not handled!\n", operation_tag);
+				dump_apdu (pri, (u_int8_t *)comp, comp->len + 2);
+				return -1;
+			case ROSE_SUBADDRESS_TRANSFER:
+				if (pri->debug & PRI_DEBUG_APDU)
+					pri_message(pri, "ROSE %i:   SubaddressTransfer - not handled!\n", operation_tag);
+				dump_apdu (pri, (u_int8_t *)comp, comp->len + 2);
+				return -1;
+			case ROSE_DIVERTING_LEG_INFORMATION2:
+				if (pri->debug & PRI_DEBUG_APDU)
+					pri_message(pri, "  Handle DivertingLegInformation2\n");
+				return rose_diverting_leg_information2_decode(pri, call, comp, len-i);
+			case ROSE_AOC_NO_CHARGING_INFO_AVAILABLE:
+				if (pri->debug & PRI_DEBUG_APDU) {
+					pri_message(pri, "ROSE %i: AOC No Charging Info Available - not handled!", operation_tag);
+					dump_apdu (pri, comp->data, comp->len);
+				}
+				return -1;
+			case ROSE_AOC_CHARGING_REQUEST:
+				return aoc_aoce_charging_request_decode(pri, call, (u_int8_t *)comp, comp->len + 2);
+			case ROSE_AOC_AOCS_CURRENCY:
+				if (pri->debug & PRI_DEBUG_APDU) {
+					pri_message(pri, "ROSE %i: AOC-S Currency - not handled!", operation_tag);
+					dump_apdu (pri, (u_int8_t *)comp, comp->len + 2);
+				}
+				return -1;
+			case ROSE_AOC_AOCS_SPECIAL_ARR:
+				if (pri->debug & PRI_DEBUG_APDU) {
+					pri_message(pri, "ROSE %i: AOC-S Special Array - not handled!", operation_tag);
+					dump_apdu (pri, (u_int8_t *)comp, comp->len + 2);
+				}
+				return -1;
+			case ROSE_AOC_AOCD_CURRENCY:
+				if (pri->debug & PRI_DEBUG_APDU) {
+					pri_message(pri, "ROSE %i: AOC-D Currency - not handled!", operation_tag);
+					dump_apdu (pri, (u_int8_t *)comp, comp->len + 2);
+				}
+				return -1;
+			case ROSE_AOC_AOCD_CHARGING_UNIT:
+				if (pri->debug & PRI_DEBUG_APDU) {
+					pri_message(pri, "ROSE %i: AOC-D Charging Unit - not handled!", operation_tag);
+					dump_apdu (pri, (u_int8_t *)comp, comp->len + 2);
+				}
+				return -1;
+			case ROSE_AOC_AOCE_CURRENCY:
+				if (pri->debug & PRI_DEBUG_APDU) {
+					pri_message(pri, "ROSE %i: AOC-E Currency - not handled!", operation_tag);
+					dump_apdu (pri, (u_int8_t *)comp, comp->len + 2);
+				}
+				return -1;
+			case ROSE_AOC_AOCE_CHARGING_UNIT:
+				return aoc_aoce_charging_unit_decode(pri, call, (u_int8_t *)comp, comp->len + 2);
+				if (0) { /* the following function is currently not used - just to make the compiler happy */
+					aoc_aoce_charging_unit_encode(pri, call, call->aoc_units); /* use this function to forward the aoc-e on a bridged channel */
+					return 0;
+				}
+			case ROSE_AOC_IDENTIFICATION_OF_CHARGE:
+				if (pri->debug & PRI_DEBUG_APDU) {
+					pri_message(pri, "ROSE %i: AOC Identification Of Charge - not handled!", operation_tag);
+					dump_apdu (pri, (u_int8_t *)comp, comp->len + 2);
+				}
+				return -1;
+			case SS_ANFPR_PATHREPLACEMENT:
+				/* Clear Queue */
+				res = pri_call_apdu_queue_cleanup(call->bridged_call);
+				if (res) {
+					pri_message(pri, "Could not Clear queue ADPU\n");
+					return -1;
+				}
+				anfpr_pathreplacement_respond(pri, call, ie);
+				break;
+			default:
+				if (pri->debug & PRI_DEBUG_APDU) {
+					pri_message(pri, "!! Unable to handle ROSE operation %d", operation_tag);
+					dump_apdu (pri, (u_int8_t *)comp, comp->len + 2);
+				}
+				return -1;
 			}
-			return -1;
 		}
 	} while(0);
 	
@@ -2619,15 +4584,119 @@
 	return 0;
 }
 
+/* ===== Begin Call Completion Supplementary Service (ETS 300 366/ECMA 186) ===== */
+/* operationId e.g. QSIG_CC_CCBS_REQUEST and QSIG_CC_CCNR_REQUEST */
+static int add_qsigCcRequestArg_facility_ie (struct pri *pri, q931_call *c)
+{
+	int size = 0;
+	int i = 0;
+	unsigned char buffer[256];
+	struct rose_component *comp = NULL, *compstk[10];
+	int compsp = 0;
+	u_int8_t operationId = c->ccoperation;
+	char *numberA = c->callernum;
+	char *numberB = c->callednum;
+ 
+	/* 1 Byte 	   0x80 | 0x1F = 9F	 Protocol Profile (0x93 wäre altes QSIG oder DDS1) */
+	buffer[i++] = (ASN1_CONTEXT_SPECIFIC | Q932_PROTOCOL_EXTENSIONS);
+
+	/* Interpretation component */
+	ASN1_ADD_SIMPLE(comp, COMP_TYPE_NFE, buffer, i); /* 2. Byte NEtwork Facility Extension 0xAA = ASN1_CONTEXT_SPECIFIC(0x80) | (ASN1_CONSTRUCTOR 0x20)  0x0A (Tag laut Standard) */
+	ASN1_PUSH(compstk, compsp, comp);
+
+	ASN1_ADD_BYTECOMP(comp, (ASN1_CONTEXT_SPECIFIC | ASN1_TAG_0), buffer, i, 0);	/* (0x80, 0x01(len), 0x00) endPTNX */
+	ASN1_ADD_BYTECOMP(comp, (ASN1_CONTEXT_SPECIFIC | ASN1_TAG_2), buffer, i, 0);	/* (0x82, 0x01(len), 0x00) endPTNX */
+
+	ASN1_FIXUP(compstk, compsp, buffer, i);
+
+#if 0
+	ASN1_ADD_BYTECOMP(comp, COMP_TYPE_INTERPRETATION, buffer, i, 0);   /* 0x8B, 0x01(len), 0x00 discard */
+#endif
+	ASN1_ADD_SIMPLE(comp, COMP_TYPE_INVOKE, buffer, i);			 /* 0xA1, 0xXX (len of Invoke Sequenz) invoke APDU */
+	ASN1_PUSH(compstk, compsp, comp);
+
+	/* Invoke ID */
+	ASN1_ADD_BYTECOMP(comp, ASN1_INTEGER, buffer, i, get_invokeid(pri));   /* InvokeID 0x02 0x01 0xXX */
+
+	/*CcbsRequest ::= 40 or CcnrRequest ::= 27 */
+	/* Operation ID: CCBS/CCNR */
+	ASN1_ADD_BYTECOMP(comp, ASN1_INTEGER, buffer, i, operationId); /* 0x02 0x01 0x28/0x1b */
+
+	/* ccbs/ccnr request argument */
+	/* PresentedNumberUnscreened */
+	ASN1_ADD_SIMPLE(comp, (ASN1_CONSTRUCTOR | ASN1_SEQUENCE), buffer, i); /*0x30 0xXX (len)*/
+	ASN1_PUSH(compstk, compsp, comp);
+	/* (0xA0, 0x01(len)) presentationAlloweAddress  [0] PartyNumber */
+	/* (0xA1, 0xXX (len) publicPartyNumber [1] IMPLICIT PublicPartyNumber */
+	/* (0x0A, 0x01, 0x00 ) type of public party number = subscriber number */
+	/* (0x12, 0xXX (len), 0xXX .. 0xXX) numeric string */
+	size = rose_presented_number_unscreened_encode(pri, &buffer[i], PRES_ALLOWED, Q932_TON_UNKNOWN, numberA);
+	if (size < 0)
+		return -1;
+	i += size;
+
+	/* (0xA1, 0xXX (len) [1] IMPLICIT PublicPartyNumber */
+	ASN1_ADD_SIMPLE(comp, (ASN1_CONTEXT_SPECIFIC | ASN1_CONSTRUCTOR | ASN1_TAG_1), buffer, i);
+	ASN1_PUSH(compstk, compsp, comp);
+	/* (0x0A, 0x01, 0x00 ) type of public party number = subscriber number */
+	/* (0x12, 0xXX (len), 0xXX .. 0xXX) numeric string */
+	size = rose_public_party_number_encode(pri, comp->data, 1, Q932_TON_UNKNOWN, numberB);
+	if (size < 0)
+		return -1;
+	i += size;
+	ASN1_FIXUP(compstk, compsp, buffer, i);
+
+	/* (0x40, 0xXX (len), 0xXX .. 0xXX) pSS1InfoElement */
+	ASN1_ADD_SIMPLE(comp, (ASN1_APPLICATION | ASN1_TAG_0 ), buffer, i);
+	ASN1_PUSH(compstk, compsp, comp);
+	buffer[i++] = (0x04);	/*  add Bearer Capability IE */
+	buffer[i++] = (0x03);	/* len*/
+	buffer[i++] = (0x80);	/* ETSI Standard, Speech */
+	buffer[i++] = (0x90);	/* circuit mode, 64kbit/s */
+	buffer[i++] = (0xa3);	/* level1 protocol, a-law */
+	ASN1_FIXUP(compstk, compsp, buffer, i);
+#if 0
+	/* can-retain-service [12] IMPLICIT BOOLEAN DEFAULT FALSE,*/
+	ASN1_ADD_BYTECOMP(comp, (ASN1_CONTEXT_SPECIFIC | ASN1_TAG_12), buffer, i, 0);   /* 0x1C, 0x01(len), 0x00 false */
+#endif
+	/* retain-sig-connection [13] IMPLICIT BOOLEAN OPTIONAL, --TRUE: sign. connection to be retained */	 
+	ASN1_ADD_BYTECOMP(comp, (ASN1_CONTEXT_SPECIFIC | ASN1_TAG_13), buffer, i, 1);   /* 0x1D, 0x01(len), 0x01 true */
+
+	ASN1_FIXUP(compstk, compsp, buffer, i);
+
+	ASN1_FIXUP(compstk, compsp, buffer, i);
+
+	if (pri_call_apdu_queue(c, Q931_SETUP, buffer, i, NULL, NULL))
+		return -1;
+	
+	return 0;
+}
+/* ===== End Call Completion Supplementary Service (ETS 300 366/ECMA 186) ===== */
+
 int pri_call_add_standard_apdus(struct pri *pri, q931_call *call)
 {
 	if (!pri->sendfacility)
 		return 0;
 
 	if (pri->switchtype == PRI_SWITCH_QSIG) { /* For Q.SIG it does network and cpe operations */
-		if (call->redirectingnum[0]) 
+		if (call->redirectingnum[0])
 			rose_diverting_leg_information2_encode(pri, call);
 		add_callername_facility_ies(pri, call, 1);
+		if (call->ccoperation) {
+			switch(call->ccoperation) {
+			case 0:
+				break;
+			case QSIG_CC_CCBSREQUEST:
+			case QSIG_CC_CCNRREQUEST:
+				add_qsigCcRequestArg_facility_ie(pri, call);
+				break;
+			case QSIG_CC_RINGOUT:
+				add_qsigCcInv_facility_ie(pri, call, Q931_SETUP);
+				break;
+			default:
+				break;
+			}
+		}
 		return 0;
 	}
 
@@ -2665,3 +4734,26 @@
 	return 0;
 }
 
+int qsig_initiate_diverting_leg_information1(struct pri *pri, q931_call *call)
+{
+	rose_diverting_leg_information1_encode(pri, call);
+
+	if (q931_facility(pri, call)) {
+		pri_message(pri, "Could not schedule facility message for divertingLegInfo1\n");
+		return -1;
+	}
+
+	return 0;
+}
+
+int qsig_initiate_call_transfer_complete(struct pri *pri, q931_call *call, int call_status)
+{
+	rose_call_transfer_complete_encode(pri, call, call_status);
+
+	if (q931_facility(pri, call)) {
+		pri_message(pri, "Could not schedule facility message for callTransferComplete\n");
+		return -1;
+	}
+
+	return 0;
+}
Index: pri_timers.h
===================================================================
--- a/pri_timers.h	(.../tags/1.4.9)	(revision 700)
+++ b/pri_timers.h	(.../team/group/issue14292)	(revision 700)
@@ -62,6 +62,7 @@
 				-1,		/* T322 */ \
 				2500,	/* TM20 - Q.921 Appendix IV */ \
 				3,		/* NM20 - Q.921 Appendix IV */ \
+				45*60*1000	/* CC-Timer2 45 min */\
 			}
 
 /* XXX Only our default timers are setup now XXX */
Index: pri_facility.h
===================================================================
--- a/pri_facility.h	(.../tags/1.4.9)	(revision 700)
+++ b/pri_facility.h	(.../team/group/issue14292)	(revision 700)
@@ -38,14 +38,6 @@
 #define Q932_PROTOCOL_GAT			0x16
 #define Q932_PROTOCOL_EXTENSIONS	0x1F
 
-/* Argument values */
-#define ROSE_NAME_PRESENTATION_ALLOWED_SIMPLE	0x80
-#define ROSE_NAME_PRESENTATION_RESTRICTED_NULL	0x87
-#define ROSE_NAME_PRESENTATION_ALLOWED_EXTENDED    0xA1
-#define ROSE_NAME_PRESENTATION_RESTRICTED_SIMPLE   0xA2
-#define ROSE_NAME_PRESENTATION_RESTRICTED_EXTENDED 0xA3
-#define ROSE_NAME_NOT_AVAIL			0x84
-
 /* Component types */
 #define COMP_TYPE_INTERPRETATION			0x8B
 #define COMP_TYPE_NETWORK_PROTOCOL_PROFILE	0x92
@@ -65,10 +57,11 @@
 #define ROSE_CALL_TRANSFER_COMPLETE			12
 #define ROSE_CALL_TRANSFER_UPDATE			13
 #define ROSE_SUBADDRESS_TRANSFER 			14
+#define QSIG_CF_CALLREROUTING 				19
 /* Q.952 ROSE operations (Diverting) */
-#define ROSE_DIVERTING_LEG_INFORMATION1		18
-#define ROSE_DIVERTING_LEG_INFORMATION2		0x15
-#define ROSE_DIVERTING_LEG_INFORMATION3		19
+#define ROSE_DIVERTING_LEG_INFORMATION1			20
+#define ROSE_DIVERTING_LEG_INFORMATION2			21
+#define ROSE_DIVERTING_LEG_INFORMATION3			22
 /* Q.956 ROSE operations (Advice Of Charge) */
 #define ROSE_AOC_NO_CHARGING_INFO_AVAILABLE	26
 #define ROSE_AOC_CHARGING_REQUEST			30
@@ -81,12 +74,27 @@
 #define ROSE_AOC_IDENTIFICATION_OF_CHARGE	37
 /* Q.SIG operations */
 #define SS_CNID_CALLINGNAME					0
+#define SS_CNOP_CALLEDNAME			 		1
+#define SS_CNOP_CONNECTEDNAME					2
 #define SS_ANFPR_PATHREPLACEMENT                                4
-#define SS_DIVERTING_LEG_INFORMATION2		21
 #define SS_MWI_ACTIVATE						80
 #define SS_MWI_DEACTIVATE					81
 #define SS_MWI_INTERROGATE					82
 
+/* ECMA 186 */
+#define QSIG_CC_CCNRREQUEST		PRI_CC_CCNRREQUEST /* 27 */
+#define QSIG_CC_CANCEL				PRI_CC_CANCEL      /* 28 */
+#define QSIG_CC_EXECPOSIBLE		29
+#define QSIG_CC_PATHRESERVE		30
+#define QSIG_CC_RINGOUT				31
+#define QSIG_CC_SUSPEND				32
+#define QSIG_CC_RESUME				33
+#define QSIG_CC_CCBSREQUEST		PRI_CC_CCBSREQUEST /* 40 */
+
+#define QSIG_CC_UNSPECIFIED				1008
+#define QSIG_CC_REMOTE_USER_BUSY_AGAIN	1012
+#define QSIG_CC_FAILURE_TO_MATCH			1013
+
 /* ROSE definitions and data structures */
 #define INVOKE_IDENTIFIER			0x02
 #define INVOKE_LINKED_IDENTIFIER	0x80
@@ -128,6 +136,12 @@
 #define ASN1_TAG_7				0x07
 #define ASN1_TAG_8				0x08
 #define ASN1_TAG_9				0x09
+#define ASN1_TAG_10				0x0a
+#define ASN1_TAG_11				0x0b
+#define ASN1_TAG_12				0x0c
+#define ASN1_TAG_13				0x0d
+#define ASN1_TAG_14				0x0e
+#define ASN1_TAG_15				0x0f
 
 /* ASN.1 Identifier Octet - Primitive/Constructor Bit */
 #define ASN1_PC_MASK			0x20
@@ -169,6 +183,20 @@
 #define Q932_TON_SUBSCRIBER				0x04
 #define Q932_TON_ABBREVIATED			0x06
 
+/* Q.SIG Character Sets. Listed in ISO/IEC 8859 */
+#define CHARACTER_SET_UNKNOWN			0x00
+#define CHARACTER_SET_ISO8859_1			0x01
+#define CHARACTER_SET_ISO8859_2			0x03
+#define CHARACTER_SET_ISO8859_3			0x04
+#define CHARACTER_SET_ISO8859_4			0x05
+#define CHARACTER_SET_ISO8859_5			0x06
+#define CHARACTER_SET_ISO8859_7			0x07
+
+/* Q.SIG Subscription Option. Listed in ECMA-174 */
+#define QSIG_NO_NOTIFICATION						0x00
+#define QSIG_NOTIFICATION_WITHOUT_DIVERTED_TO_NR	0x01
+#define QSIG_NOTIFICATION_WITH_DIVERTED_TO_NR		0x02
+
 /* RLT related Operations */
 #define RLT_SERVICE_ID		0x3e
 #define RLT_OPERATION_IND	0x01
@@ -314,6 +342,18 @@
 /* starts a QSIG Path Replacement */
 int anfpr_initiate_transfer(struct pri *pri, q931_call *c1, q931_call *c2);
 
+int qsig_initiate_diverting_leg_information1(struct pri *pri, q931_call *call);
+
+int qsig_initiate_call_transfer_complete(struct pri *pri, q931_call *call, int call_status);
+
+int rose_diverting_leg_information1_encode(struct pri *pri, q931_call *call);
+
+int rose_diverting_leg_information3_encode(struct pri *pri, q931_call *call, int messagetype);
+
+int rose_connected_name_encode(struct pri *pri, q931_call *call, int messagetype);
+
+int rose_called_name_encode(struct pri *pri, q931_call *call, int messagetype);
+
 /* Use this function to queue a facility-IE born APDU onto a call
  * call is the call to use, messagetype is any one of the Q931 messages,
  * apdu is the apdu data, apdu_len is the length of the apdu data  */
@@ -325,6 +365,8 @@
 /* Adds the "standard" APDUs to a call */
 int pri_call_add_standard_apdus(struct pri *pri, q931_call *call);
 
+int add_qsigCcInv_facility_ie (struct pri *pri, q931_call *c, int messagetype);
+
 int asn1_dump(struct pri *pri, void *comp, int len);
 
 #endif /* _PRI_FACILITY_H */
Index: libpri.h
===================================================================
--- a/libpri.h	(.../tags/1.4.9)	(revision 700)
+++ b/libpri.h	(.../team/group/issue14292)	(revision 700)
@@ -81,6 +81,7 @@
 #define PRI_EVENT_NOTIFY		16	/* Notification received */
 #define PRI_EVENT_PROGRESS		17	/* When we get CALL_PROCEEDING or PROGRESS */
 #define PRI_EVENT_KEYPAD_DIGIT		18	/* When we receive during ACTIVE state */
+#define PRI_EVENT_FACILITY		19	/* Facility received */
 
 /* Simple states */
 #define PRI_STATE_DOWN		0
@@ -135,16 +136,45 @@
 #define PRI_UNKNOWN					0x0
 
 /* Presentation */
-#define PRES_ALLOWED_USER_NUMBER_NOT_SCREENED	0x00
-#define PRES_ALLOWED_USER_NUMBER_PASSED_SCREEN	0x01
-#define PRES_ALLOWED_USER_NUMBER_FAILED_SCREEN	0x02
-#define PRES_ALLOWED_NETWORK_NUMBER				0x03
-#define PRES_PROHIB_USER_NUMBER_NOT_SCREENED	0x20
-#define PRES_PROHIB_USER_NUMBER_PASSED_SCREEN	0x21
-#define PRES_PROHIB_USER_NUMBER_FAILED_SCREEN	0x22
-#define PRES_PROHIB_NETWORK_NUMBER				0x23
-#define PRES_NUMBER_NOT_AVAILABLE				0x43
+#define PRES_NUMBER_TYPE					0x03
+#define PRES_USER_NUMBER_UNSCREENED			0x00
+#define PRES_USER_NUMBER_PASSED_SCREEN		0x01
+#define PRES_USER_NUMBER_FAILED_SCREEN		0x02
+#define PRES_NETWORK_NUMBER					0x03
 
+#define PRES_RESTRICTION					0x60
+#define PRES_ALLOWED						0x00
+#define PRES_RESTRICTED						0x20
+#define PRES_UNAVAILABLE					0x40
+#define PRES_RESERVED						0x60
+
+#define PRES_ALLOWED_USER_NUMBER_NOT_SCREENED \
+	(PRES_ALLOWED | PRES_USER_NUMBER_UNSCREENED)
+
+#define PRES_ALLOWED_USER_NUMBER_PASSED_SCREEN \
+	(PRES_ALLOWED | PRES_USER_NUMBER_PASSED_SCREEN)
+
+#define PRES_ALLOWED_USER_NUMBER_FAILED_SCREEN \
+	(PRES_ALLOWED | PRES_USER_NUMBER_FAILED_SCREEN)
+
+#define PRES_ALLOWED_NETWORK_NUMBER	\
+	(PRES_ALLOWED | PRES_NETWORK_NUMBER)
+
+#define PRES_PROHIB_USER_NUMBER_NOT_SCREENED \
+	(PRES_RESTRICTED | PRES_USER_NUMBER_UNSCREENED)
+
+#define PRES_PROHIB_USER_NUMBER_PASSED_SCREEN \
+	(PRES_RESTRICTED | PRES_USER_NUMBER_PASSED_SCREEN)
+
+#define PRES_PROHIB_USER_NUMBER_FAILED_SCREEN \
+	(PRES_RESTRICTED | PRES_USER_NUMBER_FAILED_SCREEN)
+
+#define PRES_PROHIB_NETWORK_NUMBER \
+	(PRES_RESTRICTED | PRES_NETWORK_NUMBER)
+
+#define PRES_NUMBER_NOT_AVAILABLE \
+	(PRES_UNAVAILABLE | PRES_NETWORK_NUMBER)
+
 /* Causes for disconnection */
 #define PRI_CAUSE_UNALLOCATED					1
 #define PRI_CAUSE_NO_ROUTE_TRANSIT_NET			2	/* !Q.SIG */
@@ -303,8 +333,141 @@
 #define PRI_NSF_ATT_MULTIQUEST         0xF0
 #define PRI_NSF_CALL_REDIRECTION_SERVICE       0xF7
 
+/* ECMA 186 */
+#define PRI_CC_CCNRREQUEST		27
+#define PRI_CC_CANCEL			28
+#define PRI_CC_CCBSREQUEST		40
+
 typedef struct q931_call q931_call;
 
+/* Connected line update source code */
+enum PRI_CONNECTED_LINE_UPDATE_SOURCE {
+	PRI_CONNECTED_LINE_UPDATE_SOURCE_UNKNOWN,   /* Update for unknown reason (May be interpreted to mean from answer) */
+	PRI_CONNECTED_LINE_UPDATE_SOURCE_ANSWER,    /* Update from normal call answering */
+	PRI_CONNECTED_LINE_UPDATE_SOURCE_TRANSFER,   /* Update from call transfer(active) (Party has already answered) */
+	PRI_CONNECTED_LINE_UPDATE_SOURCE_TRANSFER_ALERTING   /* Update from call transfer(alerting) (Party has not answered yet) */
+};
+
+/* Information needed to identify an endpoint in a call. */
+struct pri_party_id {
+	char number[256];			/* Subscriber phone number */
+	char name[256];				/* Subscriber name */
+	int number_type;			/* Q.931 encoded "type of number" and "numbering plan identification" */
+	int number_presentation;	/* Q.931 encoded "presentation indicator" and "screening indicator" */
+};
+
+/* Connected Line/Party information */
+struct pri_party_connected_line {
+	struct pri_party_id id;		/* Connected party ID */
+	int source;					/* Information about the source of an update .
+	 							 * enum PRI_CONNECTED_LINE_UPDATE_SOURCE values
+	 							 * for Normal-Answer, Call-transfer */
+};
+
+/* Redirecting Line information.
+ * RDNIS (Redirecting Directory Number Information Service)
+ * Where a call diversion or transfer was invoked. */
+struct pri_party_redirecting {
+	struct pri_party_id from;	/* Who is redirecting the call (Sent to the party the call is redirected toward) */
+	struct pri_party_id to;		/* Call is redirecting to a new party (Sent to the caller) */
+	int count;					/* Number of times the call was redirected */
+	int reason;					/* Redirection reasons */
+};
+
+/* Structures for qsig_cc_facilities */
+struct qsig_cc_extension {
+	int	cc_extension_tag;
+	char extension[256];
+};
+
+struct qsig_cc_optional_arg {
+	char number_A[256];
+	char number_B[256];
+	int	service;
+	struct qsig_cc_extension cc_extension;
+};
+
+struct qsig_cc_request_res {
+	int	no_path_reservation;
+	int	retain_service;
+	struct qsig_cc_extension cc_extension;
+};
+
+/* Command derived from Facility */
+#define CMD_REDIRECTING         1
+#define CMD_CONNECTEDLINE       2
+#define CMD_CC_CCBSREQUEST_RR   3
+#define CMD_CC_CCNRREQUEST_RR   4
+#define CMD_CC_CANCEL_INV       5
+#define CMD_CC_EXECPOSIBLE_INV  6
+#define CMD_CC_RINGOUT_INV      7
+#define CMD_CC_SUSPEND_INV      8
+#define CMD_CC_ERROR            9
+
+#define CCERROR_UNSPECIFIED		1008
+#define CCERROR_REMOTE_USER_BUSY_AGAIN	1012
+#define CCERROR_FAILURE_TO_MATCH	1013
+
+struct cmd_connectedline {
+	struct pri_party_connected_line connected;
+};
+
+struct cmd_redirecting {
+	struct pri_party_redirecting redirecting;
+};
+
+struct cmd_cc_ccbs_rr {
+	struct qsig_cc_request_res	cc_request_res;
+};
+
+struct cmd_cc_ccnr_rr {
+	struct qsig_cc_request_res	cc_request_res;
+};
+
+struct cmd_cc_cancel_inv {
+	struct qsig_cc_optional_arg	cc_optional_arg;
+};
+
+struct cmd_cc_execposible_inv {
+	struct qsig_cc_optional_arg	cc_optional_arg;
+};
+
+struct cmd_cc_suspend_inv {
+	struct qsig_cc_extension	cc_extension;
+};
+
+struct cmd_cc_ringout_inv {
+	struct qsig_cc_extension	cc_extension;
+};
+
+struct cmd_cc_error {
+	int	error_value;
+};
+
+struct subcommand {
+	int cmd;
+	union {
+		struct cmd_connectedline      connectedline;
+		struct cmd_redirecting        redirecting;
+		struct cmd_cc_ccbs_rr         cc_ccbs_rr;
+		struct cmd_cc_ccnr_rr         cc_ccnr_rr;
+		struct cmd_cc_cancel_inv      cc_cancel_inv;
+		struct cmd_cc_execposible_inv cc_execposible_inv;
+		struct cmd_cc_suspend_inv     cc_suspend_inv;
+		struct cmd_cc_ringout_inv     cc_ringout_inv;
+		struct cmd_cc_error           cc_error;
+	};
+};
+
+/* Max number of subcommands per event message */
+#define MAX_SUBCOMMANDS	4
+
+struct subcommands {
+	int counter_subcmd;
+	struct subcommand subcmd[MAX_SUBCOMMANDS];
+};
+
+
 typedef struct pri_event_generic {
 	/* Events with no additional information fall in this category */
 	int e;
@@ -328,6 +491,11 @@
 	int progressmask;
 	q931_call *call;
 	char useruserinfo[260];		/* User->User info */
+	char calledname[256];
+	char callednum[256];
+	int calledpres;
+	int calledplan;
+	struct subcommands subcmds;
 } pri_event_ringing;
 
 typedef struct pri_event_answer {
@@ -338,6 +506,12 @@
 	int progressmask;
 	q931_call *call;
 	char useruserinfo[260];		/* User->User info */
+	char connectednum[256];
+	char connectedname[256];
+	int connectedpres;
+	int connectedplan;
+	int source;
+	struct subcommands subcmds;
 } pri_event_answer;
 
 typedef struct pri_event_facname {
@@ -351,6 +525,14 @@
 	int callingplan;			/* Dialing plan of Calling entity */
 } pri_event_facname;
 
+struct pri_event_facility {
+	int e;
+	int channel;
+	int cref;
+	q931_call *call;
+	struct subcommands subcmds;
+};
+
 #define PRI_CALLINGPLANANI
 #define PRI_CALLINGPLANRDNIS
 typedef struct pri_event_ring {
@@ -383,6 +565,9 @@
 	char origcallednum[256];
 	int callingplanorigcalled;		/* Dialing plan of Originally Called Number */
 	int origredirectingreason;
+	int redirectingpres;
+	int redirectingcount;
+	struct subcommands subcmds;
 } pri_event_ring;
 
 typedef struct pri_event_hangup {
@@ -393,6 +578,7 @@
 	q931_call *call;			/* Opaque call pointer */
 	long aoc_units;				/* Advise of Charge number of charged units */
 	char useruserinfo[260];		/* User->User info */
+	struct subcommands subcmds;
 } pri_event_hangup;	
 
 typedef struct pri_event_restart_ack {
@@ -445,6 +631,7 @@
 	pri_event_setup_ack   setup_ack;	/* SETUP_ACKNOWLEDGE structure */
 	pri_event_notify notify;		/* Notification */
 	pri_event_keypad_digit digit;			/* Digits that come during a call */
+	struct pri_event_facility facility;
 } pri_event;
 
 struct pri;
@@ -531,6 +718,12 @@
    Set non-isdn to non-zero if you are not connecting to ISDN equipment */
 int pri_answer(struct pri *pri, q931_call *call, int channel, int nonisdn);
 
+/* Give connected line information to a call */
+int pri_connected_line_update(struct pri *pri, q931_call *call, struct pri_party_connected_line *connected);
+
+/* Give redirection information to a call */
+int pri_redirecting_update(struct pri *pri, q931_call *call, struct pri_party_redirecting *redirecting);
+
 /* Set CRV reference for GR-303 calls */
 
 
@@ -558,7 +751,12 @@
 
 /* Create a new call */
 q931_call *pri_new_call(struct pri *pri);
+q931_call *pri_new_nochannel_call(struct pri *pri, int *cr);
 
+q931_call *pri_find_call(struct pri *pri, int cr);
+
+void pri_call_set_cc_operation(q931_call *call, int cc_operation);
+
 /* Retrieve CRV reference for GR-303 calls.  Returns >0 on success. */
 int pri_get_crv(struct pri *pri, q931_call *call, int *callmode);
 
@@ -584,6 +782,11 @@
 int pri_sr_set_called(struct pri_sr *sr, char *called, int calledplan, int complete);
 int pri_sr_set_caller(struct pri_sr *sr, char *caller, char *callername, int callerplan, int callerpres);
 int pri_sr_set_redirecting(struct pri_sr *sr, char *num, int plan, int pres, int reason);
+void pri_sr_set_redirecting_name(struct pri_sr *sr, char *name);
+
+int pri_sr_set_ccringout(struct pri_sr *sr, int ccringout);
+int pri_sr_set_ccbsnr(struct pri_sr *sr, int ccbsnr);
+
 #define PRI_USER_USER_TX
 /* Set the user user field.  Warning!  don't send binary data accross this field */
 void pri_sr_set_useruser(struct pri_sr *sr, const char *userchars);
@@ -595,7 +798,10 @@
 /* Set a call has a call indpendent signalling connection (i.e. no bchan) */
 int pri_sr_set_connection_call_independent(struct pri_sr *req);
 
-/* Send an MWI indication to a remote location.  If activate is non zero, activates, if zero, decativates */
+/* Set a no channel call (i.e. QSIG-CCBS/CCNR) */
+int pri_sr_set_no_channel_call(struct pri_sr *req);
+
+/* Send an MWI indication to a remote location.  If activate is non zero, activates, if zero, deactivates */
 int pri_mwi_activate(struct pri *pri, q931_call *c, char *caller, int callerplan, char *callername, int callerpres, char *called, int calledplan);
 
 /* Send an MWI deactivate request to a remote location */
@@ -698,6 +904,7 @@
 #define PRI_TIMER_TM20	28	/* maximum time avaiting XID response */
 #define PRI_TIMER_NM20	29	/* number of XID retransmits */
 
+#define PRI_TIMER_CCBST2	30	/* maximum time on completion of CC Call */
 /* Get PRI version */
 const char *pri_get_version(void);
 
Index: pri.c
===================================================================
--- a/pri.c	(.../tags/1.4.9)	(revision 700)
+++ b/pri.c	(.../team/group/issue14292)	(revision 700)
@@ -166,6 +166,8 @@
 		return PRI_TIMER_T321;
 	else if (!strcasecmp(timer, "T322"))
 		return PRI_TIMER_T322;
+	else if (!strcasecmp(timer, "CCT2"))
+		return PRI_TIMER_CCBST2;
 	else
 		return -1;
 }
@@ -348,6 +350,8 @@
 		return "Restart ACK";
 	case PRI_EVENT_FACNAME:
 		return "FacName";
+	case PRI_EVENT_FACILITY:
+		return "Facility";
 	case PRI_EVENT_INFO_RECEIVED:
 		return "Info Received";
 	case PRI_EVENT_PROCEEDING:
@@ -551,6 +555,131 @@
 	return q931_connect(pri, call, channel, nonisdn);
 }
 
+int pri_connected_line_update(struct pri *pri, q931_call *call, struct pri_party_connected_line *connected)
+{
+	if (!pri || !call)
+		return -1;
+
+	libpri_copy_string(call->connectednum, connected->id.number, sizeof(call->connectednum));
+	libpri_copy_string(call->connectedname, connected->id.name, sizeof(call->connectedname));
+	call->connectedplan = connected->id.number_type;
+	call->connectedpres = connected->id.number_presentation;
+
+	if (pri->switchtype == PRI_SWITCH_QSIG) {
+		switch (call->ourcallstate) {
+		case Q931_CALL_STATE_ACTIVE:
+			/* immediately send callTransferComplete APDU, callStatus=answered(0) */
+			qsig_initiate_call_transfer_complete(pri, call, 0);
+			break;
+		case Q931_CALL_STATE_OVERLAP_RECEIVING:
+		case Q931_CALL_STATE_INCOMING_CALL_PROCEEDING:
+			/* queue updates for next ALERTING */
+			if (call->connectedname[0]) {
+				/* queue connectedName to be send with next Q931_ALERTING */
+				rose_called_name_encode(pri, call, Q931_ALERTING);
+			}
+
+			if (call->divertedstate != DIVERTEDSTATE_NONE) {
+				libpri_copy_string(call->divertedtonum, connected->id.number, sizeof(call->divertedtonum));
+				libpri_copy_string(call->divertedtoname, connected->id.name, sizeof(call->divertedtoname));
+				call->divertedtoplan = connected->id.number_type;
+				call->divertedtopres = connected->id.number_presentation;
+
+				if ((call->divertedstate == DIVERTEDSTATE_DIVERTED) && call->divertedtonum[0]) {
+					/* immediately send divertingLegInformation1 APDU  */
+					qsig_initiate_diverting_leg_information1(pri, call);
+					call->divertedstate = DIVERTEDSTATE_DIVLEGINFO1SEND;
+				}
+				if ((call->divertedstate == DIVERTEDSTATE_DIVLEGINFO1SEND) && call->divertedtoname[0]) {
+					/* queue divertingLegInformation3 to be send with next Q931_ALERTING */
+					rose_diverting_leg_information3_encode(pri, call, Q931_ALERTING);
+					call->divertedstate = DIVERTEDSTATE_DIVLEGINFO3SEND;
+				}
+			}
+			break;
+		case Q931_CALL_STATE_CALL_RECEIVED:
+			/* queue updates for next CONNECT */
+			if (call->connectedname[0] && ((call->divertedstate == DIVERTEDSTATE_NONE) || (call->divertedstate == DIVERTEDSTATE_DIVLEGINFO3SEND))) {
+				/* queue connectedName to be send with next Q931_CONNECT */
+				rose_connected_name_encode(pri, call, Q931_CONNECT);
+			}
+
+			if (call->divertedstate != DIVERTEDSTATE_NONE) {
+				libpri_copy_string(call->divertedtonum, connected->id.number, sizeof(call->divertedtonum));
+				libpri_copy_string(call->divertedtoname, connected->id.name, sizeof(call->divertedtoname));
+				call->divertedtoplan = connected->id.number_type;
+				call->divertedtopres = connected->id.number_presentation;
+
+				if ((call->divertedstate == DIVERTEDSTATE_DIVERTED) && call->divertedtonum[0]) {
+					/* queue divertingLegInformation1 to be send with next Q931_FACILITY */
+					rose_diverting_leg_information1_encode(pri, call);
+					call->divertedstate = DIVERTEDSTATE_DIVLEGINFO1SEND;
+
+					if (call->divertedtoname[0]) {
+						/* queue divertingLegInformation3 to be send with next Q931_FACILITY */
+						rose_diverting_leg_information3_encode(pri, call, Q931_FACILITY);
+						call->divertedstate = DIVERTEDSTATE_DIVLEGINFO3SEND;
+					}
+
+					/* immediately send Q931_FACILITY */
+					if (q931_facility(pri, call)) {
+						pri_message(pri, "Could not schedule facility message for divertingLegInfo1+3\n");
+					}
+				}
+				if ((call->divertedstate == DIVERTEDSTATE_DIVLEGINFO1SEND) && call->divertedtoname[0]) {
+					/* queue divertingLegInformation3 to be send with next Q931_CONNECT */
+					rose_diverting_leg_information3_encode(pri, call, Q931_CONNECT);
+					call->divertedstate = DIVERTEDSTATE_DIVLEGINFO3SEND;
+				}
+			}
+			break;
+		}
+	}
+
+	return 0;
+}
+
+int pri_redirecting_update(struct pri *pri, q931_call *call, struct pri_party_redirecting *redirecting)
+{
+	if (!pri || !call)
+		return -1;
+
+	libpri_copy_string(call->divertedtonum, redirecting->to.number, sizeof(call->divertedtonum));
+	libpri_copy_string(call->divertedtoname, redirecting->to.name, sizeof(call->divertedtoname));
+	call->divertedtoplan = redirecting->to.number_type;
+	call->divertedtopres = redirecting->to.number_presentation;
+	call->divertedtoreason = redirecting->reason;
+
+	if (pri->switchtype == PRI_SWITCH_QSIG) {
+		switch (call->ourcallstate) {
+		case Q931_CALL_STATE_ACTIVE:
+			/* immediately send callTransferComplete APDU, callStatus=alerting(1) */
+			qsig_initiate_call_transfer_complete(pri, call, 1);
+			break;
+		case Q931_CALL_STATE_OVERLAP_RECEIVING:
+		case Q931_CALL_STATE_INCOMING_CALL_PROCEEDING:
+			call->divertedstate = DIVERTEDSTATE_DIVERTED;
+
+			if (call->divertedtonum[0]) {
+				/* immediately send divertingLegInformation1 APDU */
+				qsig_initiate_diverting_leg_information1(pri, call);
+				call->divertedstate = DIVERTEDSTATE_DIVLEGINFO1SEND;
+			}
+			if ((call->divertedstate == DIVERTEDSTATE_DIVLEGINFO1SEND) && call->divertedtoname[0]) {
+				/* queue divertingLegInformation3 to be send with next Q931_ALERTING */
+				rose_diverting_leg_information3_encode(pri, call, Q931_ALERTING);
+				call->divertedstate = DIVERTEDSTATE_DIVLEGINFO3SEND;
+			}
+			break;
+		default:
+			pri_message(pri, "Redirecting update in state %d\n", call->ourcallstate);
+			break;
+		}
+	}
+
+	return 0;
+}
+
 #if 0
 /* deprecated routines, use pri_hangup */
 int pri_release(struct pri *pri, q931_call *call, int cause)
@@ -637,6 +766,17 @@
 	return q931_new_call(pri);
 }
 
+q931_call *pri_new_nochannel_call(struct pri *pri, int *cr)
+{
+	q931_call *call;
+	if (!pri)
+		return NULL;
+	call = q931_new_call(pri);
+	if (cr)
+		*cr = call->cr;
+	return call;
+}
+
 void pri_dump_event(struct pri *pri, pri_event *e)
 {
 	if (!pri || !e)
@@ -679,6 +819,15 @@
 	return 0;
 }
 
+int pri_sr_set_no_channel_call(struct pri_sr *req)
+{
+	if (!req)
+		return -1;
+
+	req->nochannelsignalling = 1;
+	return 0;
+}
+
 /* Don't call any other pri functions on this */
 int pri_mwi_activate(struct pri *pri, q931_call *c, char *caller, int callerplan, char *callername, int callerpres, char *called,
 					int calledplan)
@@ -829,6 +978,10 @@
 	struct q921_frame *f;
 	int q921outstanding = 0;
 #endif
+	q931_call *cur, *prev;
+	struct pri *master;
+	int counter = 0;
+
 	if (!pri)
 		return NULL;
 
@@ -862,10 +1015,57 @@
 	len += sprintf(buf + len, "T309 Timer: %d\n", pri->timers[PRI_TIMER_T309]);
 	len += sprintf(buf + len, "T313 Timer: %d\n", pri->timers[PRI_TIMER_T313]);
 	len += sprintf(buf + len, "N200 Counter: %d\n", pri->timers[PRI_TIMER_N200]);
+	len += sprintf(buf + len, "CCT2 Timer: %d\n", pri->timers[PRI_TIMER_CCBST2]);
+	/* Find the master  - He has the call pool */
+	if (pri->master)
+		master = pri->master;
+	else
+		master = pri;
 
+	cur = *master->callpool;
+	prev = NULL;
+	while(cur) {
+		if (cur->cctimer2) {
+			struct timeval tv;
+			int time_ms_to_go, time_to_go_min, time_to_go_sec;
+			gettimeofday(&tv, NULL);
+			time_ms_to_go = (pri->pri_sched[cur->cctimer2].when.tv_sec - tv.tv_sec)*1000;
+			time_to_go_min = time_ms_to_go/1000/60;
+			time_to_go_sec = (time_ms_to_go-(time_to_go_min*60*1000))/1000;
+
+			len += sprintf(buf + len, "%d. Active Q.931 Call: %p cr=%d: (%dmin %dsec)\n",
+				++counter, cur, cur->cr,
+				time_to_go_min, time_to_go_sec);
+		} else {
+			len += sprintf(buf + len, "%d. Active Q.931 Call: %p cr=%d\n", ++counter, cur, cur->cr);
+		}
+		cur = cur->next;
+	}
+
 	return strdup(buf);
 }
 
+q931_call *pri_find_call(struct pri *pri, int cr)
+{
+	q931_call *cur;
+	struct pri *master;
+
+	/* Find the master  - He has the call pool */
+	if (pri->master)
+		master = pri->master;
+	else
+		master = pri;
+	
+	cur = *master->callpool;
+	while(cur) {
+		if (cur->cr == cr)
+			return cur;
+		cur = cur->next;
+	}
+
+	return NULL;
+}
+
 int pri_get_crv(struct pri *pri, q931_call *call, int *callmode)
 {
 	return q931_call_getcrv(pri, call, callmode);
@@ -936,3 +1136,25 @@
 	sr->redirectingreason = reason;
 	return 0;
 }
+
+void pri_sr_set_redirecting_name(struct pri_sr *sr, char *name)
+{
+	sr->redirectingname = name;
+}
+
+int pri_sr_set_ccringout(struct pri_sr *sr, int ccringout)
+{
+	sr->ccringout = ccringout;
+	return 0;
+}
+
+int pri_sr_set_ccbsnr(struct pri_sr *sr, int ccbsnr)
+{
+	sr->ccbsnr = ccbsnr;
+	return 0;
+}
+
+void pri_call_set_cc_operation(q931_call *call, int cc_operation)
+{
+	call->ccoperation = cc_operation;
+}
Index: pri_internal.h
===================================================================
--- a/pri_internal.h	(.../tags/1.4.9)	(revision 700)
+++ b/pri_internal.h	(.../team/group/issue14292)	(revision 700)
@@ -36,6 +36,12 @@
 #define DBGHEAD __FILE__ ":%d %s: "
 #define DBGINFO __LINE__,__PRETTY_FUNCTION__
 
+/* divertedstate */
+#define DIVERTEDSTATE_NONE				0
+#define DIVERTEDSTATE_DIVERTED			1
+#define DIVERTEDSTATE_DIVLEGINFO1SEND	2
+#define DIVERTEDSTATE_DIVLEGINFO3SEND	3
+
 struct pri_sched {
 	struct timeval when;
 	void (*callback)(void *data);
@@ -47,7 +53,7 @@
 enum q931_mode;
 
 /* No more than 128 scheduled events */
-#define MAX_SCHED 128
+#define MAX_SCHED 128+256 /* 256 ccT2 timer events*/
 
 #define MAX_TIMERS 32
 
@@ -142,13 +148,17 @@
 	int calledplan;
 	int userl1;
 	int numcomplete;
+	char *redirectingname;
 	char *redirectingnum;
 	int redirectingplan;
 	int redirectingpres;
 	int redirectingreason;
 	int justsignalling;
+	int nochannelsignalling;
+	int ccbsnr;
 	const char *useruserinfo;
 	int transferable;
+	int ccringout;
 };
 
 /* Internal switch types */
@@ -202,6 +212,7 @@
 	
 	int sentchannel;
 	int justsignalling;		/* for a signalling-only connection */
+	int nochannelsignalling;
 
 	int progcode;			/* Progress coding */
 	int progloc;			/* Progress Location */	
@@ -225,12 +236,19 @@
 	char callernum[256];
 	char callername[256];
 
+	int ccoperation;		/* QSIG_CCBSREQUEST/QSIG_CCNRREQUEST */
+	int ccrequestresult;
+	int cctimer2;			/* Timer for QSIG-timer2 */
+	/* QSIG cc infos (receive) */
+	struct subcommands subcmds;
+
 	char keypad_digits[64];		/* Buffer for digits that come in KEYPAD_FACILITY */
 
 	int ani2;               /* ANI II */
 	
 	int calledplan;
 	int nonisdn;
+	char calledname[256];
 	char callednum[256];	/* Called Number */
 	int complete;			/* no more digits coming */
 	int newcall;			/* if the received message has a new call reference value */
@@ -241,6 +259,7 @@
 	int redirectingplan;
 	int redirectingpres;
 	int redirectingreason;	      
+	int redirectingcount;
 	char redirectingnum[256];	/* Number of redirecting party */
 	char redirectingname[256];	/* Name of redirecting party */
 
@@ -251,6 +270,39 @@
 	char origcalledname[256];	/* Original name of person being called */
 	char origcallednum[256];	/* Orignal number of person being called */
 
+	int connectedplan;
+	int connectedpres;
+	char connectednum[256];
+	char connectedname[256];
+
+	/* divertingLegInformation1 */
+	int divleginfo1activeflag;
+	int divertedtoplan;
+	int divertedtopres;
+	int divertedtoreason;
+	char divertedtonum[256];
+	int divertedtocount;
+
+	/* divertingLegInformation3 */
+	int divleginfo3activeflag;
+	char divertedtoname[256];
+	int divertedstate;
+
+	/* callTransferComplete */
+	int ctcompleteflag;
+	int ctcompletepres;
+	int ctcompleteplan;
+	char ctcompletenum[256];
+	char ctcompletename[256];
+	int ctcompletecallstatus;
+
+	/* callTransferActive */
+	int ctactiveflag;
+	int ctactivepres;
+	int ctactiveplan;
+	char ctactivenum[256];
+	char ctactivename[256];
+
 	int useruserprotocoldisc;
 	char useruserinfo[256];
 	char callingsubaddr[256];	/* Calling parties sub address */
Index: q921.c
===================================================================
--- a/q921.c	(.../tags/1.4.9)	(revision 700)
+++ b/q921.c	(.../team/group/issue14292)	(revision 700)
@@ -268,12 +268,31 @@
 	pri->t203_timer = pri_schedule_event(pri, pri->timers[PRI_TIMER_T203], t203_expire, pri);
 }
 
+static void q921_send_queued_iframes(struct pri *pri)
+{
+	struct q921_frame *f;
+
+	f = pri->txqueue;
+	while(f && (pri->windowlen < pri->window)) {
+		if (!f->transmitted) {
+			/* Send it now... */
+			if (pri->debug & PRI_DEBUG_Q921_DUMP)
+				pri_message(pri, "-- Finally transmitting %d, since window opened up (%d)\n", f->h.n_s, pri->windowlen);
+			f->transmitted++;
+			pri->windowlen++;
+			f->h.n_r = pri->v_r;
+			f->h.p_f = 0;
+			q921_transmit(pri, (q921_h *)(&f->h), f->len);
+		}
+		f = f->next;
+	}
+}
+
 static pri_event *q921_ack_rx(struct pri *pri, int ack, int send_untransmitted_frames)
 {
 	int x;
 	int cnt=0;
 	pri_event *ev;
-	struct q921_frame *f;
 	/* Make sure the ACK was within our window */
 	for (x=pri->v_a; (x != pri->v_s) && (x != ack); Q921_INC(x));
 	if (x != ack) {
@@ -309,20 +328,7 @@
 		if (!pri->busy && send_untransmitted_frames) {
 			pri->retrans = 0;
 			/* Search for something to send */
-			f = pri->txqueue;
-			while(f && (pri->windowlen < pri->window)) {
-				if (!f->transmitted) {
-					/* Send it now... */
-					if (pri->debug & PRI_DEBUG_Q921_DUMP)
-						pri_message(pri, "-- Finally transmitting %d, since window opened up (%d)\n", f->h.n_s, pri->windowlen);
-					f->transmitted++;
-					pri->windowlen++;
-					f->h.n_r = pri->v_r;
-					f->h.p_f = 0;
-					q921_transmit(pri, (q921_h *)(&f->h), f->len);
-				}
-				f = f->next;
-			}
+			q921_send_queued_iframes(pri);
 		}
 		if (pri->debug & PRI_DEBUG_Q921_DUMP)
 			pri_message(pri, "-- Waiting for acknowledge, restarting T200 counter\n");		
@@ -515,7 +521,7 @@
 			pri->txqueue = f;
 		/* Immediately transmit unless we're in a recovery state, or the window
 		   size is too big */
-		if (!pri->retrans && !pri->busy) {
+		if ((pri->q921_state == Q921_LINK_CONNECTION_ESTABLISHED) && (!pri->retrans && !pri->busy)) {
 			if (pri->windowlen < pri->window) {
 				pri->windowlen++;
 				q921_transmit(pri, (q921_h *)(&f->h), f->len);
@@ -532,9 +538,14 @@
 			pri_schedule_del(pri, pri->t203_timer);
 			pri->t203_timer = 0;
 		}
-		if (pri->debug & PRI_DEBUG_Q921_DUMP)
-			pri_message(pri, "Starting T_200 timer\n");
-		reschedule_t200(pri);		
+
+		/* Check this so that we don't try to send frames while multi frame mode is down */
+		if (pri->q921_state == Q921_LINK_CONNECTION_ESTABLISHED) {
+			if (pri->debug & PRI_DEBUG_Q921_DUMP)
+				pri_message(pri, "Starting T_200 timer\n");
+
+			reschedule_t200(pri);		
+		}
 	} else {
 		pri_error(pri, "!! Out of memory for Q.921 transmit\n");
 		return -1;
@@ -776,9 +787,6 @@
 
 static pri_event *q921_dchannel_up(struct pri *pri)
 {
-	/* Reset counters, etc */
-	q921_reset(pri);
-	
 	/* Stop any SABME retransmissions */
 	if (pri->sabme_timer) {
 		pri_schedule_del(pri, pri->sabme_timer);
@@ -799,6 +807,8 @@
 	/* Notify Layer 3 */
 	q931_dl_indication(pri, PRI_EVENT_DCHAN_UP);
 
+	q921_send_queued_iframes(pri);
+
 	/* Report event that D-Channel is now up */
 	pri->ev.gen.e = PRI_EVENT_DCHAN_UP;
 	return &pri->ev;
Index: q931.c
===================================================================
--- a/q931.c	(.../tags/1.4.9)	(revision 700)
+++ b/q931.c	(.../team/group/issue14292)	(revision 700)
@@ -362,6 +362,11 @@
 	/* We are ready to transmit single IE only */
 	if (order > 1)
 		return 0;
+
+	if (call->nochannelsignalling) {
+		ie->data[pos++] = 0xac;
+		return pos + 2;
+	}
 	
 	if (call->justsignalling) {
 		ie->data[pos++] = 0xac; /* Read the standards docs to figure this out
@@ -804,7 +809,13 @@
 		ie->data[1] = 0x90;
 		return 4;
 	}
-	
+
+	if (call->nochannelsignalling) {
+		ie->data[0] = 0xa8;
+		ie->data[1] = 0x80;
+		return 4;
+	}
+
 	if (call->justsignalling) {
 		ie->data[0] = 0xa8;
 		ie->data[1] = 0x80;
@@ -992,12 +1003,45 @@
 				prefix, ie->data[2] >> 7, redirection_reason2str(ie->data[2] & 0x7f), ie->data[2] & 0x7f);
 			break;
 		}
-	}
-	while(!(ie->data[i++]& 0x80));
+	} while(!(ie->data[i++]& 0x80));
 	q931_get_number(cnum, sizeof(cnum), ie->data + i, ie->len - i);
 	pri_message(pri, "  '%s' ]\n", cnum);
 }
 
+static FUNC_RECV(receive_connected_number)
+{
+	int i = 0;
+
+	/* To follow Q.931 (4.5.1), we must search for start of octet 4 by
+	   walking through all bytes until one with ext bit (8) set to 1 */
+	do {
+		switch(i) {
+		case 0:
+			call->connectedplan = ie->data[i] & 0x7f;
+			break;
+		case 1:
+			call->connectedpres = ie->data[i] & 0x7f;
+			break;
+		}
+	} while(!(ie->data[i++] & 0x80));
+	q931_get_number((unsigned char *) call->connectednum, sizeof(call->connectednum), ie->data + i, ie->len - i);
+
+	return 0;
+}
+
+static FUNC_SEND(transmit_connected_number)
+{
+	int datalen = strlen(call->connectednum);
+	if (datalen > 0) {
+		ie->data[0] = call->connectedplan;
+		ie->data[1] = 0x80 | call->connectedpres;
+		memcpy(ie->data + 2, call->connectednum, strlen(call->connectednum));
+		return datalen + 4;
+	}
+
+	return 0;
+}
+
 static FUNC_DUMP(dump_connected_number)
 {
 	unsigned char cnum[256];
@@ -1015,8 +1059,7 @@
 				prefix, ie->data[1] >> 7, pri_pres2str(ie->data[1] & 0x7f), ie->data[1] & 0x7f);
 			break;
 		}
-	}
-	while(!(ie->data[i++]& 0x80));
+	} while(!(ie->data[i++]& 0x80));
 	q931_get_number(cnum, sizeof(cnum), ie->data + i, ie->len - i);
 	pri_message(pri, "  '%s' ]\n", cnum);
 }
@@ -1040,8 +1083,7 @@
 			call->redirectingreason = ie->data[i] & 0x0f;
 			break;
 		}
-	}
-	while(!(ie->data[i++] & 0x80));
+	} while(!(ie->data[i++] & 0x80));
 	q931_get_number((unsigned char *) call->redirectingnum, sizeof(call->redirectingnum), ie->data + i, ie->len - i);
 	return 0;
 }
@@ -2183,7 +2225,7 @@
 	{ 1, Q931_IE_CALL_STATUS, "Call Status" },
 	{ 1, Q931_IE_CHANGE_STATUS, "Change Status" },
 	{ 1, Q931_IE_CONNECTED_ADDR, "Connected Number", dump_connected_number },
-	{ 1, Q931_IE_CONNECTED_NUM, "Connected Number", dump_connected_number },
+	{ 1, Q931_IE_CONNECTED_NUM, "Connected Number", dump_connected_number, receive_connected_number, transmit_connected_number },
 	{ 1, Q931_IE_ORIGINAL_CALLED_NUMBER, "Original Called Number", dump_redirecting_number, receive_redirecting_number, transmit_redirecting_number },
 	{ 1, Q931_IE_USER_USER_FACILITY, "User-User Facility" },
 	{ 1, Q931_IE_UPDATE, "Update" },
@@ -2478,8 +2520,7 @@
 						maxlen -= res;
 						iet = (q931_ie *)((char *)iet + res);
 					}
-				}
-				while (res > 0 && order < ies_count);
+				} while (res > 0 && order < ies_count);
 				if (have_shift && total_res) {
 					if (Q931_IE_CODESET(ies[x].ie))
 						*codeset = Q931_IE_CODESET(ies[x].ie);
@@ -2820,9 +2861,9 @@
 	return send_message(pri, c, Q931_CALL_PROCEEDING, call_proceeding_ies);
 }
 #ifndef ALERTING_NO_PROGRESS
-static int alerting_ies[] = { Q931_PROGRESS_INDICATOR, Q931_IE_USER_USER,  -1 };
+static int alerting_ies[] = { Q931_PROGRESS_INDICATOR, Q931_IE_USER_USER, Q931_IE_FACILITY, -1 };
 #else
-static int alerting_ies[] = { -1 };
+static int alerting_ies[] = { Q931_IE_FACILITY, -1 };
 #endif
 
 int q931_alerting(struct pri *pri, q931_call *c, int channel, int info)
@@ -2841,7 +2882,7 @@
 	return send_message(pri, c, Q931_ALERTING, alerting_ies);
 }
 
-static int connect_ies[] = {  Q931_CHANNEL_IDENT, Q931_PROGRESS_INDICATOR, -1 };
+static int connect_ies[] = {  Q931_CHANNEL_IDENT, Q931_PROGRESS_INDICATOR, Q931_IE_CONNECTED_NUM, Q931_IE_FACILITY, -1 };
  
 int q931_setup_ack(struct pri *pri, q931_call *c, int channel, int nonisdn)
 {
@@ -2923,6 +2964,18 @@
 	q931_release(pri, c, PRI_CAUSE_NORMAL_CLEARING);
 }
 
+static void pri_cctimer2_timeout(void *data)
+{
+	int cause = 16;
+	struct q931_call *c = data;
+	struct pri *pri = c->pri;
+	if (pri->debug & PRI_DEBUG_Q931_STATE)
+		pri_message(pri, "Timed out no-channel call\n");
+	c->ccoperation = QSIG_CC_CANCEL;
+	/* normal clear cause */
+	q931_hangup(pri, c, cause);
+}
+
 int q931_connect(struct pri *pri, q931_call *c, int channel, int nonisdn)
 {
 	if (channel) { 
@@ -2954,7 +3007,7 @@
 	return send_message(pri, c, Q931_CONNECT, connect_ies);
 }
 
-static int release_ies[] = { Q931_CAUSE, Q931_IE_USER_USER, -1 };
+static int release_ies[] = { Q931_CAUSE, Q931_IE_USER_USER, Q931_IE_FACILITY, -1 };
 
 int q931_release(struct pri *pri, q931_call *c, int cause)
 {
@@ -3002,7 +3055,7 @@
 	return send_message(pri, c, Q931_RESTART, restart_ies);
 }
 
-static int disconnect_ies[] = { Q931_CAUSE, Q931_IE_USER_USER, -1 };
+static int disconnect_ies[] = { Q931_CAUSE, Q931_IE_USER_USER, Q931_IE_FACILITY, -1 };
 
 int q931_disconnect(struct pri *pri, q931_call *c, int cause)
 {
@@ -3030,6 +3083,8 @@
 
 static int cis_setup_ies[] = { Q931_BEARER_CAPABILITY, Q931_CHANNEL_IDENT, Q931_IE_FACILITY, Q931_CALLED_PARTY_NUMBER, -1 };
 
+static int nochannel_setup_ies[] = { Q931_BEARER_CAPABILITY, Q931_CHANNEL_IDENT, Q931_IE_FACILITY, Q931_CALLING_PARTY_NUMBER, Q931_CALLED_PARTY_NUMBER, Q931_SENDING_COMPLETE, -1 };
+
 int q931_setup(struct pri *pri, q931_call *c, struct pri_sr *req)
 {
 	int res;
@@ -3055,6 +3110,7 @@
 	c->nonisdn = req->nonisdn;
 	c->newcall = 0;
 	c->justsignalling = req->justsignalling;		
+	c->nochannelsignalling = req->nochannelsignalling;		
 	c->complete = req->numcomplete; 
 	if (req->exclusive) 
 		c->chanflags = FLAG_EXCLUSIVE;
@@ -3082,6 +3138,10 @@
 	}
 	if (req->redirectingnum) {
 		libpri_copy_string(c->redirectingnum, req->redirectingnum, sizeof(c->redirectingnum));
+		if (req->redirectingname)
+			libpri_copy_string(c->redirectingname, req->redirectingname, sizeof(c->redirectingname));
+		else
+			c->redirectingname[0] = '\0';
 		c->redirectingplan = req->redirectingplan;
 		if ((pri->switchtype == PRI_SWITCH_DMS100) ||
 		    (pri->switchtype == PRI_SWITCH_ATT4ESS)) {
@@ -3092,6 +3152,7 @@
 		c->redirectingpres = req->redirectingpres;
 		c->redirectingreason = req->redirectingreason;
 	} else {
+		c->redirectingname[0] = '\0';
 		c->redirectingnum[0] = '\0';
 		c->redirectingplan = PRI_UNKNOWN;
 		c->redirectingpres = PRES_NUMBER_NOT_AVAILABLE;
@@ -3113,12 +3174,19 @@
 	else
 		c->progressmask = 0;
 
+	if (req->ccringout)
+		c->ccoperation = QSIG_CC_RINGOUT;
+	if (req->ccbsnr)
+		c->ccoperation = req->ccbsnr;
+
 	pri_call_add_standard_apdus(pri, c);
 
 	if (pri->subchannel && !pri->bri)
 		res = send_message(pri, c, Q931_SETUP, gr303_setup_ies);
 	else if (c->justsignalling)
 		res = send_message(pri, c, Q931_SETUP, cis_setup_ies);
+	else if (c->nochannelsignalling)
+		res = send_message(pri, c, Q931_SETUP, nochannel_setup_ies);
 	else
 		res = send_message(pri, c, Q931_SETUP, setup_ies);
 	if (!res) {
@@ -3132,7 +3200,7 @@
 	
 }
 
-static int release_complete_ies[] = { Q931_IE_USER_USER, -1 };
+static int release_complete_ies[] = { Q931_IE_USER_USER, Q931_IE_FACILITY, -1 };
 
 static int q931_release_complete(struct pri *pri, q931_call *c, int cause)
 {
@@ -3187,6 +3255,18 @@
 		/* We'll send RELEASE with these causes */
 		disconnect = 0;
 	}
+	if (c->nochannelsignalling) {
+		if (c->ccoperation == QSIG_CC_CANCEL) {
+			add_qsigCcInv_facility_ie(pri, c, Q931_RELEASE);
+		}
+		if (c->cctimer2) {
+			pri_schedule_del(pri, c->cctimer2);
+			c->cctimer2 = 0;
+			pri_message(pri, "NEW_HANGUP DEBUG: stop CC-Timer2\n");
+		}
+		disconnect = 0;
+	}
+
 	/* All other causes we send with DISCONNECT */
 	switch(c->ourcallstate) {
 	case Q931_CALL_STATE_NULL:
@@ -3227,6 +3307,10 @@
 		break;
 	case Q931_CALL_STATE_ACTIVE:
 		/* received CONNECT */
+		if (c->nochannelsignalling) {
+			q931_release(pri,c,cause);
+			break;
+		}
 		q931_disconnect(pri,c,cause);
 		break;
 	case Q931_CALL_STATE_DISCONNECT_REQUEST:
@@ -3260,6 +3344,132 @@
 	return 0;
 }
 
+static void clr_subcommands(struct subcommands *sub)
+{
+	sub->counter_subcmd = 0;
+}
+
+static struct subcommand *get_ptr_subcommand(struct subcommands *sub)
+{
+	if (sub->counter_subcmd < MAX_SUBCOMMANDS) {
+		int count = sub->counter_subcmd;
+		sub->counter_subcmd++;
+		return &sub->subcmd[count];
+	}
+
+	return NULL;
+}
+
+static struct subcommand *get_ptr_q931_subcommand_by_index(struct subcommands *sub, int index)
+{
+	if (index < MAX_SUBCOMMANDS) {
+		sub->counter_subcmd--;
+		return &sub->subcmd[index];
+	}
+
+	return NULL;
+}
+
+static int q931_facilities2eventfacilities(struct pri *pri, q931_call *c, struct subcommands *subcmds)
+{
+	int facilitypos;
+	int facility_number;
+	struct subcommand *c_subcmd;
+	struct subcommand *e_subcmd;
+
+	if (c->subcmds.counter_subcmd) {
+		facility_number = c->subcmds.counter_subcmd;
+
+		for (facilitypos = 0; facilitypos < facility_number; facilitypos++) {
+			c->subcmds.counter_subcmd--;
+			c_subcmd = get_ptr_q931_subcommand_by_index(&c->subcmds, facilitypos);
+			e_subcmd = get_ptr_subcommand(subcmds);
+			if (c_subcmd && e_subcmd) {
+				switch (c_subcmd->cmd) {
+				case CMD_CC_CCBSREQUEST_RR:
+					e_subcmd->cmd = c_subcmd->cmd;
+					memcpy(&c_subcmd->cc_ccbs_rr, &e_subcmd->cc_ccbs_rr, sizeof(c_subcmd->cc_ccbs_rr));
+					if (pri->debug & PRI_DEBUG_APDU)
+						pri_message(pri, "facility(QSIG_CC_CCBSREQUEST) (%d/%d)\n",
+										e_subcmd->cc_ccbs_rr.cc_request_res.no_path_reservation,
+										e_subcmd->cc_ccbs_rr.cc_request_res.retain_service);
+					if (pri->debug & PRI_DEBUG_APDU)
+						pri_message(pri, "counter_subcmd(%d)\n", subcmds->counter_subcmd);
+					c->ccrequestresult = 1;
+					break;
+				case CMD_CC_CCNRREQUEST_RR:
+					e_subcmd->cmd = c_subcmd->cmd;
+					memcpy(&c_subcmd->cc_ccnr_rr, &e_subcmd->cc_ccnr_rr, sizeof(c_subcmd->cc_ccnr_rr));
+					if (pri->debug & PRI_DEBUG_APDU)
+						pri_message(pri, "facility(QSIG_CC_CCNRREQUEST) (%d/%d)\n",
+										e_subcmd->cc_ccnr_rr.cc_request_res.no_path_reservation,
+										e_subcmd->cc_ccnr_rr.cc_request_res.retain_service);
+					if (pri->debug & PRI_DEBUG_APDU)
+						pri_message(pri, "counter_subcmd(%d)\n", subcmds->counter_subcmd);
+					c->ccrequestresult = 1;
+					break;
+				case CMD_CC_CANCEL_INV:
+					e_subcmd->cmd = c_subcmd->cmd;
+					memcpy(&c_subcmd->cc_cancel_inv, &e_subcmd->cc_cancel_inv, sizeof(c_subcmd->cc_cancel_inv));
+					if (pri->debug & PRI_DEBUG_APDU)
+						pri_message(pri, "facility(QSIG_CC_CANCEL) (%s/%s)\n",
+										e_subcmd->cc_cancel_inv.cc_optional_arg.number_A,
+										e_subcmd->cc_cancel_inv.cc_optional_arg.number_B);
+					if (pri->debug & PRI_DEBUG_APDU)
+						pri_message(pri, "counter_subcmd(%d)\n", subcmds->counter_subcmd);
+					break;
+				case CMD_CC_EXECPOSIBLE_INV:
+					e_subcmd->cmd = c_subcmd->cmd;
+					memcpy(&c_subcmd->cc_execposible_inv, &e_subcmd->cc_execposible_inv, sizeof(c_subcmd->cc_execposible_inv));
+					if (pri->debug & PRI_DEBUG_APDU)
+						pri_message(pri, "facility(QSIG_CC_EXECPOSIBLE) (%s/%s)\n",
+										e_subcmd->cc_execposible_inv.cc_optional_arg.number_A,
+										e_subcmd->cc_execposible_inv.cc_optional_arg.number_B);
+					if (pri->debug & PRI_DEBUG_APDU)
+						pri_message(pri, "counter_subcmd(%d)\n", subcmds->counter_subcmd);
+					break;
+				case CMD_CC_RINGOUT_INV:
+					e_subcmd->cmd = c_subcmd->cmd;
+					memcpy(&c_subcmd->cc_ringout_inv, &e_subcmd->cc_ringout_inv, sizeof(c_subcmd->cc_ringout_inv));
+					if (pri->debug & PRI_DEBUG_APDU)
+						pri_message(pri, "facility(QSIG_CC_RINGOUT) (%d)\n",
+										e_subcmd->cc_ringout_inv.cc_extension.cc_extension_tag);
+					if (pri->debug & PRI_DEBUG_APDU)
+						pri_message(pri, "counter_subcmd(%d)\n", subcmds->counter_subcmd);
+					break;
+				case CMD_CC_SUSPEND_INV:
+					e_subcmd->cmd = c_subcmd->cmd;
+					memcpy(&c_subcmd->cc_suspend_inv, &e_subcmd->cc_suspend_inv, sizeof(c_subcmd->cc_suspend_inv));
+					if (pri->debug & PRI_DEBUG_APDU)
+						pri_message(pri, "facility(QSIG_CC_SUSPEND) (%d)\n",
+										e_subcmd->cc_suspend_inv.cc_extension.cc_extension_tag);
+					if (pri->debug & PRI_DEBUG_APDU)
+						pri_message(pri, "counter_subcmd(%d)\n", subcmds->counter_subcmd);
+					break;
+				case CMD_CC_ERROR:
+					e_subcmd->cmd = c_subcmd->cmd;
+					memcpy(&c_subcmd->cc_error, &e_subcmd->cc_error, sizeof(c_subcmd->cc_error));
+					if (pri->debug & PRI_DEBUG_APDU)
+						pri_message(pri, "facility(QSIG_CC_ERROR) (%d)\n",
+										e_subcmd->cc_error.error_value);
+					if (pri->debug & PRI_DEBUG_APDU)
+						pri_message(pri, "counter_subcmd(%d)\n", subcmds->counter_subcmd);
+					break;
+				default:
+					pri_error(pri, "Don't know how to handle Facility subcmd %d\n", c_subcmd->cmd);
+					break;
+				}
+			}
+		}
+#if 0
+	} else {
+		pri_message(pri, "No facilities specified\n");
+#endif
+	}
+	return 0;
+}
+
+
 int q931_receive(struct pri *pri, q931_h *h, int len)
 {
 	q931_mh *mh;
@@ -3312,6 +3522,7 @@
 		break;
 	case Q931_FACILITY:
 		c->callername[0] = '\0';
+		c->subcmds.counter_subcmd = 0;
 		break;
 	case Q931_SETUP:
 		if (pri->debug & PRI_DEBUG_Q931_STATE)
@@ -3339,6 +3550,7 @@
 		c->redirectingplan = -1;
 		c->redirectingpres = -1;
 		c->redirectingreason = -1;
+		c->redirectingcount = 0;
 		c->origcalledplan = -1;
 		c->origcalledpres = -1;
 		c->origredirectingreason = -1;
@@ -3346,6 +3558,29 @@
 		c->origcallednum[0] = '\0';
 		c->redirectingname[0] = '\0';
 		c->origcalledname[0] = '\0';
+		c->connectedplan = -1;
+		c->connectedpres = -1;
+		c->connectednum[0] = '\0';
+		c->connectedname[0] = '\0';
+		c->divleginfo1activeflag = 0;
+		c->divertedtocount = 0;
+		c->divertedtoplan = -1;
+		c->divertedtopres = -1;
+		c->divertedtoreason = -1;
+		c->divertedtonum[0] = '\0';
+		c->divleginfo3activeflag = 0;
+		c->divertedtoname[0] = '\0';
+		c->divertedstate = DIVERTEDSTATE_NONE;
+		c->ctcompleteflag = 0;
+		c->ctcompleteplan = -1;
+		c->ctcompletepres = -1;
+		c->ctcompletenum[0] = '\0';
+		c->ctcompletename[0] = '\0';
+		c->ctactiveflag = 0;
+		c->ctactiveplan = -1;
+		c->ctactivepres = -1;
+		c->ctactivenum[0] = '\0';
+		c->ctactivename[0] = '\0';
 		c->useruserprotocoldisc = -1; 
 		c->useruserinfo[0] = '\0';
 		c->complete = 0;
@@ -3353,7 +3588,9 @@
 		c->aoc_units = -1;
 		/* Fall through */
 	case Q931_CONNECT:
+		c->ccrequestresult = 0;
 	case Q931_ALERTING:
+		c->subcmds.counter_subcmd = 0;
 	case Q931_PROGRESS:
 		c->useruserinfo[0] = '\0';
 		c->cause = -1;
@@ -3369,6 +3606,7 @@
 		break;
 	case Q931_RELEASE:
 	case Q931_DISCONNECT:
+		c->subcmds.counter_subcmd = 0;
 		c->cause = -1;
 		c->causecode = -1;
 		c->causeloc = -1;
@@ -3383,6 +3621,7 @@
 			pri_schedule_del(pri, c->retranstimer);
 		c->retranstimer = 0;
 		c->useruserinfo[0] = '\0';
+		c->subcmds.counter_subcmd = 0;
 		/* Fall through */
 	case Q931_STATUS:
 		c->cause = -1;
@@ -3539,6 +3778,9 @@
 		if (!c->newcall) {
 			break;
 		}
+
+		clr_subcommands(&pri->ev.ring.subcmds);
+
 		if (c->progressmask & PRI_PROG_CALLER_NOT_ISDN)
 			c->nonisdn = 1;
 		c->newcall = 0;
@@ -3570,7 +3812,9 @@
 		libpri_copy_string(pri->ev.ring.redirectingname, c->redirectingname, sizeof(pri->ev.ring.redirectingname));
 		libpri_copy_string(pri->ev.ring.useruserinfo, c->useruserinfo, sizeof(pri->ev.ring.useruserinfo));
 		c->useruserinfo[0] = '\0';
+		pri->ev.ring.redirectingpres = c->redirectingpres;
 		pri->ev.ring.redirectingreason = c->redirectingreason;
+		pri->ev.ring.redirectingcount = c->redirectingcount;
 		pri->ev.ring.origredirectingreason = c->origredirectingreason;
 		pri->ev.ring.flexible = ! (c->chanflags & FLAG_EXCLUSIVE);
 		pri->ev.ring.cref = c->cr;
@@ -3578,15 +3822,21 @@
 		pri->ev.ring.layer1 = c->userl1;
 		pri->ev.ring.complete = c->complete; 
 		pri->ev.ring.ctype = c->transcapability;
-		pri->ev.ring.redirectingreason = c->redirectingreason;
 		pri->ev.ring.progress = c->progress;
 		pri->ev.ring.progressmask = c->progressmask;
+		if (pri->debug & PRI_DEBUG_APDU) {
+			pri_message(pri, "Sending ring event (%d) nochannelsignalling (%d) facility_number (%d)\n",
+									pri->ev.ring.cref, c->nochannelsignalling, pri->ev.ring.subcmds.counter_subcmd);
+		}
+		q931_facilities2eventfacilities(pri, c, &pri->ev.ring.subcmds);
 		return Q931_RES_HAVEEVENT;
 	case Q931_ALERTING:
 		if (c->newcall) {
 			q931_release_complete(pri,c,PRI_CAUSE_INVALID_CALL_REFERENCE);
 			break;
 		}
+		clr_subcommands(&pri->ev.ringing.subcmds);
+
 		UPDATE_OURCALLSTATE(pri, c, Q931_CALL_STATE_CALL_DELIVERED);
 		c->peercallstate = Q931_CALL_STATE_CALL_RECEIVED;
 		pri->ev.e = PRI_EVENT_RINGING;
@@ -3595,8 +3845,26 @@
 		pri->ev.ringing.call = c;
 		pri->ev.ringing.progress = c->progress;
 		pri->ev.ringing.progressmask = c->progressmask;
+
+		if (c->divleginfo3activeflag) {
+			c->divleginfo3activeflag = 0;
+			libpri_copy_string(pri->ev.ringing.calledname, c->divertedtoname, sizeof(pri->ev.ringing.calledname));
+			libpri_copy_string(pri->ev.ringing.callednum, (c->divertedtonum[0]) ? c->divertedtonum : c->callednum, sizeof(pri->ev.ringing.callednum));
+			pri->ev.ringing.calledpres = c->divertedtopres;
+			pri->ev.ringing.calledplan = c->divertedtoplan;
+		} else {
+			libpri_copy_string(pri->ev.ringing.calledname, c->calledname, sizeof(pri->ev.ringing.calledname));
+			libpri_copy_string(pri->ev.ringing.callednum, (c->divertedtonum[0]) ? c->divertedtonum : c->callednum, sizeof(pri->ev.ringing.callednum));
+			pri->ev.ringing.calledpres = PRES_ALLOWED_USER_NUMBER_NOT_SCREENED;
+			pri->ev.ringing.calledplan = c->calledplan;
+		}
 		libpri_copy_string(pri->ev.ringing.useruserinfo, c->useruserinfo, sizeof(pri->ev.ringing.useruserinfo));
 		c->useruserinfo[0] = '\0';
+		if (pri->debug & PRI_DEBUG_APDU) {
+			pri_message(pri, "Sending ringing event (%d) nochannelsignalling (%d) facility_number (%d)\n",
+									pri->ev.ringing.cref, c->nochannelsignalling, pri->ev.ringing.subcmds.counter_subcmd);
+		}
+		q931_facilities2eventfacilities(pri, c, &pri->ev.ringing.subcmds);
 
 		cur = c->apdus;
 		while (cur) {
@@ -3617,6 +3885,8 @@
 			q931_status(pri, c, PRI_CAUSE_WRONG_MESSAGE);
 			break;
 		}
+		clr_subcommands(&pri->ev.answer.subcmds);
+
 		UPDATE_OURCALLSTATE(pri, c, Q931_CALL_STATE_ACTIVE);
 		c->peercallstate = Q931_CALL_STATE_CONNECT_REQUEST;
 		pri->ev.e = PRI_EVENT_ANSWER;
@@ -3625,31 +3895,166 @@
 		pri->ev.answer.call = c;
 		pri->ev.answer.progress = c->progress;
 		pri->ev.answer.progressmask = c->progressmask;
+		libpri_copy_string(pri->ev.answer.connectednum, c->connectednum, sizeof(pri->ev.answer.connectednum));
+		libpri_copy_string(pri->ev.answer.connectedname, c->connectedname, sizeof(pri->ev.answer.connectedname));
+		pri->ev.answer.connectedpres = c->connectedpres;
+		pri->ev.answer.connectedplan = c->connectedplan;
+		pri->ev.answer.source = PRI_CONNECTED_LINE_UPDATE_SOURCE_ANSWER;
 		libpri_copy_string(pri->ev.answer.useruserinfo, c->useruserinfo, sizeof(pri->ev.answer.useruserinfo));
 		c->useruserinfo[0] = '\0';
+		if (pri->debug & PRI_DEBUG_APDU) {
+			pri_message(pri, "Sending answer event (%d) nochannelsignalling (%d) facility_number (%d)\n",
+									pri->ev.answer.cref, c->nochannelsignalling, pri->ev.answer.subcmds.counter_subcmd);
+		}
+		q931_facilities2eventfacilities(pri, c, &pri->ev.answer.subcmds);
 		q931_connect_acknowledge(pri, c);
+		if (c->nochannelsignalling) {
+			if (c->ccrequestresult) {
+				pri_message(pri, "Q931_CONNECT: start CC-Timer2\n");
+				c->cctimer2 = pri_schedule_event(pri, pri->timers[PRI_TIMER_CCBST2], pri_cctimer2_timeout, c);
+			}
+			return Q931_RES_HAVEEVENT;
+		}
 		if (c->justsignalling) {  /* Make sure WE release when we initiatie a signalling only connection */
 			q931_release(pri, c, PRI_CAUSE_NORMAL_CLEARING);
 			break;
 		} else
 			return Q931_RES_HAVEEVENT;
 	case Q931_FACILITY:
-		if (c->newcall) {
-			q931_release_complete(pri,c,PRI_CAUSE_INVALID_CALL_REFERENCE);
-			break;
+		{
+			int haveevent = 0;
+			clr_subcommands(&pri->ev.facility.subcmds);
+
+			if (c->newcall) {
+				q931_release_complete(pri,c,PRI_CAUSE_INVALID_CALL_REFERENCE);
+				break;
+			}
+			if (c->subcmds.counter_subcmd) {
+				pri->ev.e = PRI_EVENT_FACILITY;
+				pri->ev.facility.channel = c->channelno | (c->ds1no << 8) | (c->ds1explicit << 16);
+				pri->ev.facility.cref = c->cr;
+				pri->ev.facility.call = c;
+
+				if (pri->debug & PRI_DEBUG_APDU) {
+					pri_message(pri, "Sending facility event (%d) nochannelsignalling (%d) facility_number (%d)\n",
+											pri->ev.facility.cref, c->nochannelsignalling, pri->ev.facility.subcmds.counter_subcmd);
+				}
+				q931_facilities2eventfacilities(pri, c, &pri->ev.facility.subcmds);
+				haveevent = 1;
+			}
+			if (c->ctcompleteflag) {
+				c->ctcompleteflag = 0;
+
+				if (c->ctcompletecallstatus == 0) {
+					/* answered(0) */
+					struct subcommand *subcmd;
+
+					pri_message(pri, "Got CT-Complete, callStatus = answered(0)\n");
+					pri->ev.e = PRI_EVENT_FACILITY;
+					pri->ev.facility.channel = c->channelno | (c->ds1no << 8) | (c->ds1explicit << 16);
+					pri->ev.facility.call = c;
+
+					subcmd = get_ptr_subcommand(&pri->ev.facility.subcmds);
+					if (subcmd) {
+						struct cmd_connectedline *cmdcl = &subcmd->connectedline;
+
+						subcmd->cmd = CMD_CONNECTEDLINE;
+						libpri_copy_string(cmdcl->connected.id.number, c->ctcompletenum, sizeof(cmdcl->connected.id.number));
+						libpri_copy_string(cmdcl->connected.id.name, c->ctcompletename, sizeof(cmdcl->connected.id.name));
+						cmdcl->connected.id.number_type = c->ctcompleteplan;
+						cmdcl->connected.id.number_presentation = c->ctcompletepres;
+						cmdcl->connected.source = PRI_CONNECTED_LINE_UPDATE_SOURCE_TRANSFER;
+						haveevent = 1;
+						pri_message(pri, "CT-Complete, sending facility/CMD_CONNECTEDLINE (%s/%s)\n", cmdcl->connected.id.name, cmdcl->connected.id.number);
+					}
+				} else if (c->ctcompletecallstatus == 1) {
+					/* alerting(1) */
+					struct subcommand *subcmd;
+
+					pri_message(pri, "Got CT-Complete, callStatus = alerting(1)\n");
+					pri->ev.e = PRI_EVENT_FACILITY;
+					pri->ev.facility.channel = c->channelno | (c->ds1no << 8) | (c->ds1explicit << 16);
+					pri->ev.facility.call = c;
+
+					subcmd = get_ptr_subcommand(&pri->ev.facility.subcmds);
+					if (subcmd) {
+						struct cmd_redirecting *cmdr = &subcmd->redirecting;
+
+						subcmd->cmd = CMD_REDIRECTING;
+						libpri_copy_string(cmdr->redirecting.from.number, c->connectednum, sizeof(cmdr->redirecting.from.number));
+						libpri_copy_string(cmdr->redirecting.from.name, c->connectedname, sizeof(cmdr->redirecting.from.name));
+						cmdr->redirecting.from.number_type = c->connectedplan;
+						cmdr->redirecting.from.number_presentation = c->connectedpres;
+						libpri_copy_string(cmdr->redirecting.to.number, c->ctcompletenum, sizeof(cmdr->redirecting.to.number));
+						libpri_copy_string(cmdr->redirecting.to.name, c->ctcompletename, sizeof(cmdr->redirecting.to.name));
+						cmdr->redirecting.to.number_type = c->ctcompleteplan;
+						cmdr->redirecting.to.number_presentation = c->ctcompletepres;
+						cmdr->redirecting.count = 0;
+						cmdr->redirecting.reason = PRI_REDIR_UNKNOWN;
+						haveevent = 1;
+						pri_message(pri, "CT-Complete, sending facility/CMD_REDIRECTING (%s/%s)\n", cmdr->redirecting.to.name, cmdr->redirecting.to.number);
+					}
+				} else {
+					pri_message(pri, "illegal value for callStatus=%d\n", c->ctcompletecallstatus);
+				}
+			} else if (c->ctactiveflag) {
+				struct subcommand *subcmd;
+
+				c->ctactiveflag = 0;
+
+				pri_message(pri, "Got CT-Active\n");
+				pri->ev.e = PRI_EVENT_FACILITY;
+				pri->ev.facility.channel = c->channelno | (c->ds1no << 8) | (c->ds1explicit << 16);
+				pri->ev.facility.call = c;
+
+				subcmd = get_ptr_subcommand(&pri->ev.facility.subcmds);
+				if (subcmd) {
+					struct cmd_connectedline *cmdcl = &subcmd->connectedline;
+
+					subcmd->cmd = CMD_CONNECTEDLINE;
+					libpri_copy_string(cmdcl->connected.id.number, c->ctcompletenum, sizeof(cmdcl->connected.id.number));
+					libpri_copy_string(cmdcl->connected.id.name, c->ctcompletename, sizeof(cmdcl->connected.id.name));
+					cmdcl->connected.id.number_type = c->ctcompleteplan;
+					cmdcl->connected.id.number_presentation = c->ctcompletepres;
+					cmdcl->connected.source = PRI_CONNECTED_LINE_UPDATE_SOURCE_TRANSFER;
+					haveevent = 1;
+					pri_message(pri, "CT-Active, sending facility CMD_CONNECTEDLINE (%s/%s)\n", cmdcl->connected.id.name, cmdcl->connected.id.number);
+				}
+			}
+			else if (c->divleginfo1activeflag) {
+				struct subcommand *subcmd;
+
+				c->divleginfo1activeflag = 0;
+
+				pri_message(pri, "Got DivertingLegInformation1\n");
+				pri->ev.e = PRI_EVENT_FACILITY;
+				pri->ev.facility.channel = c->channelno | (c->ds1no << 8) | (c->ds1explicit << 16);
+				pri->ev.facility.call = c;
+
+				subcmd = get_ptr_subcommand(&pri->ev.facility.subcmds);
+				if (subcmd) {
+					struct cmd_redirecting *cmdr = &subcmd->redirecting;
+
+					subcmd->cmd = CMD_REDIRECTING;
+					libpri_copy_string(cmdr->redirecting.from.number, c->callednum, sizeof(cmdr->redirecting.from.number));
+					cmdr->redirecting.from.name[0] = '\0';
+					cmdr->redirecting.from.number_type = c->calledplan;
+					cmdr->redirecting.from.number_presentation = PRES_ALLOWED_USER_NUMBER_NOT_SCREENED;
+					libpri_copy_string(cmdr->redirecting.to.number, c->divertedtonum, sizeof(cmdr->redirecting.to.number));
+					cmdr->redirecting.to.name[0] = '\0';
+					cmdr->redirecting.to.number_type = c->divertedtoplan;
+					cmdr->redirecting.to.number_presentation = c->divertedtopres;
+					cmdr->redirecting.count = c->divertedtocount;
+					cmdr->redirecting.reason = c->divertedtoreason;
+					haveevent = 1;
+					pri_message(pri, "DivertingLegInformation1, sending facility/CMD_REDIRECTING (%s/%s)\n", cmdr->redirecting.to.name, cmdr->redirecting.to.number);
+				}
+			}
+
+			if (haveevent)
+				return Q931_RES_HAVEEVENT;
 		}
-		pri->ev.e = PRI_EVENT_FACNAME;
-		libpri_copy_string(pri->ev.facname.callingname, c->callername, sizeof(pri->ev.facname.callingname));
-		libpri_copy_string(pri->ev.facname.callingnum, c->callernum, sizeof(pri->ev.facname.callingnum));
-		pri->ev.facname.channel = c->channelno | (c->ds1no << 8) | (c->ds1explicit << 16);
-		pri->ev.facname.callingpres = c->callerpres;
-		pri->ev.facname.callingplan = c->callerplan;
-		pri->ev.facname.cref = c->cr;
-		pri->ev.facname.call = c;
-#if 0
-		pri_message(pri, "Sending facility event (%s/%s)\n", pri->ev.facname.callingname, pri->ev.facname.callingnum);
-#endif
-		return Q931_RES_HAVEEVENT;
+		break;
 	case Q931_PROGRESS:
 		if (missingmand) {
 			q931_status(pri, c, PRI_CAUSE_MANDATORY_IE_MISSING);
@@ -3757,6 +4162,8 @@
 		}
 		break;
 	case Q931_RELEASE_COMPLETE:
+		clr_subcommands(&pri->ev.hangup.subcmds);
+
 		UPDATE_OURCALLSTATE(pri, c, Q931_CALL_STATE_NULL);
 		c->peercallstate = Q931_CALL_STATE_NULL;
 		pri->ev.hangup.channel = c->channelno | (c->ds1no << 8) | (c->ds1explicit << 16);
@@ -3766,6 +4173,11 @@
 		pri->ev.hangup.aoc_units = c->aoc_units;
 		libpri_copy_string(pri->ev.hangup.useruserinfo, c->useruserinfo, sizeof(pri->ev.hangup.useruserinfo));
 		c->useruserinfo[0] = '\0';
+		if (pri->debug & PRI_DEBUG_APDU) {
+			pri_message(pri, "Sending hangup event (%d) nochannelsignalling (%d) facility_number (%d)\n",
+									pri->ev.hangup.cref, c->nochannelsignalling, pri->ev.hangup.subcmds.counter_subcmd);
+		}
+		q931_facilities2eventfacilities(pri, c, &pri->ev.hangup.subcmds);
 		/* Free resources */
 		if (c->alive) {
 			pri->ev.e = PRI_EVENT_HANGUP;
@@ -3783,6 +4195,8 @@
 			q931_hangup(pri,c,c->cause);
 		break;
 	case Q931_RELEASE:
+		clr_subcommands(&pri->ev.hangup.subcmds);
+
 		if (missingmand) {
 			/* Force cause to be mandatory IE missing */
 			c->cause = PRI_CAUSE_MANDATORY_IE_MISSING;
@@ -3801,6 +4215,11 @@
 		pri->ev.hangup.aoc_units = c->aoc_units;
 		libpri_copy_string(pri->ev.hangup.useruserinfo, c->useruserinfo, sizeof(pri->ev.hangup.useruserinfo));
 		c->useruserinfo[0] = '\0';
+		if (pri->debug & PRI_DEBUG_APDU) {
+			pri_message(pri, "Sending hangup event (%d) nochannelsignalling (%d) facility_number (%d)\n",
+									pri->ev.hangup.cref, c->nochannelsignalling, pri->ev.hangup.subcmds.counter_subcmd);
+		}
+		q931_facilities2eventfacilities(pri, c, &pri->ev.hangup.subcmds);
 		/* Don't send release complete if they send us release 
 		   while we sent it, assume a NULL state */
 		if (c->newcall)
@@ -3809,6 +4228,8 @@
 			return Q931_RES_HAVEEVENT;
 		break;
 	case Q931_DISCONNECT:
+		clr_subcommands(&pri->ev.hangup.subcmds);
+
 		if (missingmand) {
 			/* Still let user call release */
 			c->cause = PRI_CAUSE_MANDATORY_IE_MISSING;
@@ -3835,6 +4256,11 @@
 		pri->ev.hangup.aoc_units = c->aoc_units;
 		libpri_copy_string(pri->ev.hangup.useruserinfo, c->useruserinfo, sizeof(pri->ev.hangup.useruserinfo));
 		c->useruserinfo[0] = '\0';
+		if (pri->debug & PRI_DEBUG_APDU) {
+			pri_message(pri, "Sending hangup event (%d) nochannelsignalling (%d) facility_number (%d)\n",
+									pri->ev.hangup.cref, c->nochannelsignalling, pri->ev.hangup.subcmds.counter_subcmd);
+		}
+		q931_facilities2eventfacilities(pri, c, &pri->ev.hangup.subcmds);
 		if (c->alive)
 			return Q931_RES_HAVEEVENT;
 		else

Property changes on: .
___________________________________________________________________
Added: automerge
   + *
Added: automerge-propname
   + issue14292-integrated
Added: svnmerge-integrated
   + /branches/1.4:1-688
Added: automerge-email
   + rmudgett@digium.com
Added: issue14292-integrated
   + /team/group/issue14068:1-699