kal_public_api.h 151 KB
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
/*****************************************************************************
*  Copyright Statement:
*  --------------------
*  This software is protected by Copyright and the information contained
*  herein is confidential. The software may not be copied and the information
*  contained herein may not be used or disclosed except with the written
*  permission of MediaTek Inc. (C) 2005
*
*  BY OPENING THIS FILE, BUYER HEREBY UNEQUIVOCALLY ACKNOWLEDGES AND AGREES
*  THAT THE SOFTWARE/FIRMWARE AND ITS DOCUMENTATIONS ("MEDIATEK SOFTWARE")
*  RECEIVED FROM MEDIATEK AND/OR ITS REPRESENTATIVES ARE PROVIDED TO BUYER ON
*  AN "AS-IS" BASIS ONLY. MEDIATEK EXPRESSLY DISCLAIMS ANY AND ALL WARRANTIES,
*  EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE IMPLIED WARRANTIES OF
*  MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE OR NONINFRINGEMENT.
*  NEITHER DOES MEDIATEK PROVIDE ANY WARRANTY WHATSOEVER WITH RESPECT TO THE
*  SOFTWARE OF ANY THIRD PARTY WHICH MAY BE USED BY, INCORPORATED IN, OR
*  SUPPLIED WITH THE MEDIATEK SOFTWARE, AND BUYER AGREES TO LOOK ONLY TO SUCH
*  THIRD PARTY FOR ANY WARRANTY CLAIM RELATING THERETO. MEDIATEK SHALL ALSO
*  NOT BE RESPONSIBLE FOR ANY MEDIATEK SOFTWARE RELEASES MADE TO BUYER'S
*  SPECIFICATION OR TO CONFORM TO A PARTICULAR STANDARD OR OPEN FORUM.
*
*  BUYER'S SOLE AND EXCLUSIVE REMEDY AND MEDIATEK'S ENTIRE AND CUMULATIVE
*  LIABILITY WITH RESPECT TO THE MEDIATEK SOFTWARE RELEASED HEREUNDER WILL BE,
*  AT MEDIATEK'S OPTION, TO REVISE OR REPLACE THE MEDIATEK SOFTWARE AT ISSUE,
*  OR REFUND ANY SOFTWARE LICENSE FEES OR SERVICE CHARGE PAID BY BUYER TO
*  MEDIATEK FOR SUCH MEDIATEK SOFTWARE AT ISSUE. 
*
*  THE TRANSACTION CONTEMPLATED HEREUNDER SHALL BE CONSTRUED IN ACCORDANCE
*  WITH THE LAWS OF THE STATE OF CALIFORNIA, USA, EXCLUDING ITS CONFLICT OF
*  LAWS PRINCIPLES.  ANY DISPUTES, CONTROVERSIES OR CLAIMS ARISING THEREOF AND
*  RELATED THERETO SHALL BE SETTLED BY ARBITRATION IN SAN FRANCISCO, CA, UNDER
*  THE RULES OF THE INTERNATIONAL CHAMBER OF COMMERCE (ICC).
*
*****************************************************************************/

/*****************************************************************************
 *
 * Filename:
 * ---------
 *   kal_public_api.h
 *
 * Project:
 * --------
 *   Maui_Software
 *
 * Description:
 * ------------
 *   This file provides KAL public API prototypes
 *
 * Author:
 * -------
 * -------
 *
 *============================================================================
 *             HISTORY
 * Below this line, this part is controlled by PVCS VM. DO NOT MODIFY!!
 *------------------------------------------------------------------------------
 * removed!
 * removed!
 * removed!
 *
 * removed!
 * removed!
 * removed!
 *
 * removed!
 * removed!
 * removed!
 *
 * removed!
 * removed!
 * removed!
 *
 * removed!
 * removed!
 * removed!
 *
 * removed!
 * removed!
 * removed!
 * removed!
 * removed!
 * removed!
 * removed!
 *
 * removed!
 * removed!
 * removed!
 *
 * removed!
 * removed!
 * removed!
 *
 * removed!
 * removed!
 * removed!
 *
 * removed!
 * removed!
 * removed!
 *
 * removed!
 * removed!
 * removed!
 *
 * removed!
 * removed!
 * removed!
 *
 * removed!
 * removed!
 * removed!
 *
 * removed!
 * removed!
 * removed!
 *
 * removed!
 * removed!
 * removed!
 *
 * removed!
 * removed!
 * removed!
 *
 * removed!
 * removed!
 * removed!
 *
 * removed!
 * removed!
 * removed!
 *
 * removed!
 * removed!
 * removed!
 *
 * removed!
 * removed!
 * removed!
 *
 * removed!
 * removed!
 * removed!
 *
 * removed!
 * removed!
 * removed!
 *
 * removed!
 * removed!
 * removed!
 *
 * removed!
 * removed!
 * removed!
 *
 * removed!
 * removed!
 * removed!
 *
 * removed!
 * removed!
 * removed!
 *
 * removed!
 * removed!
 * removed!
 *
 * removed!
 * removed!
 * removed!
 *
 * removed!
 * removed!
 * removed!
 *
 * removed!
 * removed!
 * removed!
 *
 * removed!
 * removed!
 * removed!
 *
 * removed!
 * removed!
 * removed!
 *
 * removed!
 * removed!
 * removed!
 *
 ****************************************************************************/

#ifndef _KAL_PUBLIC_API_H
#define _KAL_PUBLIC_API_H

/*******************************************************************************
 * Common Header File Include
 *******************************************************************************/
#ifndef GEN_FOR_PC
#include <stdio.h>
#include <string.h>
#endif

#include "kal_general_types.h"
#include "kal_public_defs.h"

#ifdef __cplusplus 
extern "C" {
#endif

/* DOM-NOT_FOR_SDK-BEGIN */
/*******************************************************************************
 * Category 1 : StdLib-like Function
 *******************************************************************************/
static __inline void * kal_mem_cpy(void* dest, const void* src, kal_uint32 size)
{
    return memcpy ( dest, src, size );
}

static __inline void * kal_mem_set(void* dest, kal_int32 value, kal_uint32 size)
{
    return memset ( dest, value, size );
}

static __inline kal_int32 kal_mem_cmp(const void* src1, const void* src2, kal_uint32 size)
{
    return memcmp ( src1, src2, size );
}

static __inline void * kal_mem_bwcpy(void* dest, const void* src, kal_uint32 size)
{    
    char *destaddr = (char*)dest+(size-1); 
    char const *srcaddr = (char*)src+(size-1); 	
    while (size-- > 0) *destaddr-- = *srcaddr--; 	
    return destaddr;   
}

#if defined(KAL_ON_OSCAR)
#define kal_snprintf _snprintf
#else
#define kal_snprintf snprintf
#endif

#define kal_sprintf sprintf

extern kal_char *kal_strtok_r(kal_char *string, const kal_char *seperators, kal_char **ppLast);

#define kal_toWCHAR(asciiString) L##asciiString

/* 
 * 16bits string operations
 */
extern int kal_wstrlen(const WCHAR *wstr);
extern WCHAR *kal_wstrcpy(WCHAR *to, const WCHAR *from);
extern WCHAR *kal_wstrncpy(WCHAR *to, const WCHAR *from, int n);
extern int kal_wstrcmp(const WCHAR *s1, const WCHAR *s2);
extern int kal_wstrncmp(const WCHAR *s1, const WCHAR *s2, int n);
extern WCHAR *kal_wstrcat(WCHAR *s1, const WCHAR *s2);
extern WCHAR *kal_wstrncat(WCHAR *s1, const WCHAR *s2, int n);
extern WCHAR *kal_wstrchr(const WCHAR *s, int c);
extern WCHAR *kal_wstrrchr(const WCHAR *str, int ch);

/* 
 * dual 8bits string operations
 */
extern int kal_dchar_strlen(const char *wstr);
extern char *kal_dchar_strcpy(char *to, const char *from);
extern char *kal_dchar_strncpy(char *to, const char *from, int n);
extern int kal_dchar_strcmp(const char *s1, const char *s2);
extern int kal_dchar_strncmp(const char *s1, const char *s2, int n);
extern char *kal_dchar_strcat(char *s1, const char *s2);
extern char *kal_dchar_strncat(char *s1, const char *s2, int n);
extern char *kal_dchar_strchr(const char *s, int c);
extern char *kal_dchar_strrchr(const char *str, int ch);
extern void kal_dchar2char(WCHAR *outstr, char* tostr);
extern void kal_wsprintf(WCHAR *outstr, char *fmt,...);


/*******************************************************************************
 * Category 2 : Query Functions for Initialize and States
 *******************************************************************************/
void kal_initialize(void);
void kal_initialize_mem(void);

 /*******************************************************************************
 * <GROUP Functions>
 * 
 * FUNCTION
 *  kal_query_systemInit
 * DESCRIPTION
 *  query whether the system is in initialization phase.
 * PARAMETERS
 *  N/A
 * RETURN VALUES
 *  KAL_TRUE: system is in initialization phase.
 *  KAL_FALSE: system is not in initialization phase.
 ******************************************************************************/
kal_bool kal_query_systemInit(void);

#if  !defined(__FUE__)
static __inline boot_mode_type stack_query_boot_mode(void)
{
    extern boot_mode_type system_boot_mode;
    return system_boot_mode;
} 
#else  /* __FUE__ */
static __inline boot_mode_type stack_query_boot_mode(void)
{
    return FUE_BOOT;
} 
#endif /* __FUE__ */

 /*******************************************************************************
 * <GROUP Functions>
 * 
 * FUNCTION
 *  kal_query_boot_mode
 * DESCRIPTION
 *  query system boot mode.
 * PARAMETERS
 *  N/A
 * SEE ALSO
 *  boot_mode_type
 * NOTE
 *  This API replaces stack_query_boot_mode(). And stack_query_boot_mode() is deprecated,
 *  it will be removed from next release.
 ******************************************************************************/
static __inline boot_mode_type kal_query_boot_mode(void)
{
    return stack_query_boot_mode();
}

/* DOM-NOT_FOR_SDK-END */

/*******************************************************************************
 * Category 3 : LISR, HISR and TASK functions
 *******************************************************************************/

 /*******************************************************************************
 * <GROUP Functions>
 * 
 * FUNCTION
 *  kal_get_current_thread_ID
 * DESCRIPTION
 *  get current thread identity, which is a unique pointer for each thread.
 * PARAMETERS
 *  N/A
 * RETURNS
 *  current thread identity.
 * NOTE
 *  thread is defined as task or HISR.
 ******************************************************************************/
void *kal_get_current_thread_ID(void);
 /*******************************************************************************
 * <GROUP Functions>
 * 
 * FUNCTION
 *  kal_sleep_task
 * DESCRIPTION
 *  put the task into sleep for a specified duration which is expressed in terms
 *  of system tick.
 * PARAMETERS
 *  time_in_ticks: [IN] how many system ticks to sleep.
 * RETURNS
 *  N/A
 * NOTE
 *  N/A
 * SEE ALSO
 *  KAL_MILLISECS_PER_TICK
 ******************************************************************************/
void kal_sleep_task(kal_uint32 time_in_ticks);
 /*******************************************************************************
 * <GROUP Functions>
 * 
 * FUNCTION
 *  kal_get_current_task
 * DESCRIPTION
 *  get current task identity.
 * PARAMETERS
 *  N/A
 * RETURNS
 *  current task identity.
 * NOTE
 *  This API replaces kal_get_task_self_id(). And kal_get_task_self_id() is deprecated, 
 *  it will be removed from next release.
 * SEE ALSO
 *  kal_get_current_task_index
 ******************************************************************************/
kal_taskid kal_get_current_task(void);
 /*******************************************************************************
 * <GROUP Functions>
 * 
 * FUNCTION
 *  kal_get_current_task_index
 * DESCRIPTION
 *  get current task index.
 * PARAMETERS
 *  N/A
 * RETURNS
 *  current task index.
 * NOTE
 *  This API replaces kal_get_my_task_index(). And kal_get_my_task_index() is deprecated,
 *  it will be removed from next release.
 * SEE ALSO
 *  kal_get_current_task
 ******************************************************************************/
task_indx_type kal_get_current_task_index(void);
 /*******************************************************************************
 * <GROUP Functions>
 * 
 * FUNCTION
 *  kal_get_current_task_priority
 * DESCRIPTION
 *  get current task priority.
 * PARAMETERS
 *  N/A
 * RETURNS
 *  current task priority.
 * NOTE
 *  This API replaces kal_get_mytask_priority(). And kal_get_mytask_priority() is deprecated,
 *  it will be removed from next release.
 ******************************************************************************/
kal_uint32 kal_get_current_task_priority(void);

static __inline kal_uint32 kal_get_mytask_priority(void)
{
    return kal_get_current_task_priority();
}

 /*******************************************************************************
 * <GROUP Functions>
 * 
 * FUNCTION
 *  kal_set_active_module_id
 * DESCRIPTION
 *  set active module in current task.
 * PARAMETERS
 *  module_id: [IN] active module in current task.
 * RETURNS
 *  N/A
 * NOTE
 *  (1)This API replaces stack_set_active_module_id(). And stack_set_active_module_id() is deprecated,
 *     it will be removed from next release.
 *  (2)This API must be called before kal_get_active_module_id(). So, the recommended place to call it
 *     is in task main function, immediately after get a ilm message.
 * SEE ALSO
 *  kal_get_active_module_id
 ******************************************************************************/
extern void kal_set_active_module_id(module_type module_id);
 /*******************************************************************************
 * <GROUP Functions>
 * 
 * FUNCTION
 *  kal_get_active_module_id
 * DESCRIPTION
 *  get active module in current task.
 * PARAMETERS
 *  N/A
 * RETURNS
 *  active module in current task.
 * NOTE
 *  (1)This API replaces stack_get_active_module_id(). And stack_get_active_module_id() is deprecated,
 *     it will be removed from next release.
 *  (2)This API return the last value set by kal_set_active_module_id().
 * SEE ALSO
 *  kal_set_active_module_id
 ******************************************************************************/
extern module_type kal_get_active_module_id(void);
 /*******************************************************************************
 * <GROUP Functions>
 * 
 * FUNCTION
 *  kal_get_task_by_index
 * DESCRIPTION
 *  get taskid from task index.
 * PARAMETERS
 *  index: [IN] target task index.
 * RETURNS
 *  Return corresponding taskid. If the target task doesn't exist, return NULL.
 * SEE ALSO
 *  kal_get_task_index
 ******************************************************************************/
extern kal_taskid kal_get_task_by_index(task_indx_type index);
 /*******************************************************************************
 * <GROUP Functions>
 * 
 * FUNCTION
 *  kal_get_task_index
 * DESCRIPTION
 *  query task index from taskid.
 * PARAMETERS
 *  taskid: [IN] target taskid.
 * RETURNS
 *  Return corresponding task index. 
 * SEE ALSO
 *  kal_get_task_by_index
 ******************************************************************************/
extern task_indx_type kal_get_task_index(kal_taskid taskid);
/* DOM-NOT_FOR_SDK-BEGIN */
/*******************************************************************************
 * <GROUP Functions>
 * 
 * FUNCTION
 *  kal_get_task_total_number
 * DESCRIPTION
 *  query total task index number.
 * PARAMETERS
 *  NA.
 * RETURNS
 *  Return total task index number. 
 ******************************************************************************/
extern kal_uint32 kal_get_task_total_number(void);
 
  /*******************************************************************************
 * <GROUP Functions>
 * 
 * FUNCTION
 *  kal_get_send_msg_module_id_total_number
 * DESCRIPTION
 *  query total module id number that can send message.
 * PARAMETERS
 *  NA.
 * RETURNS
 *  Return total module id number that can send message.
 ******************************************************************************/
 extern kal_uint32 kal_get_send_msg_module_id_total_number(void);
/* DOM-NOT_FOR_SDK-END */
 /*******************************************************************************
 * <GROUP Functions>
 * 
 * FUNCTION
 *  kal_map_module_to_task_index
 * DESCRIPTION
 *  Convert module to its owner task's index.
 * PARAMETERS
 *  mod: [IN] module to query.
 * RETURNS
 *  Return corresponding task index. 
 ******************************************************************************/
extern task_indx_type kal_map_module_to_task_index(module_type mod);
 /*******************************************************************************
 * <GROUP Functions>
 * 
 * FUNCTION
 *  kal_get_task_priority
 * DESCRIPTION
 *  get specified task's effective priority by taskid.
 * PARAMETERS
 *  taskid: [IN] specified task's taskid.
 * RETURNS
 *  Return corresponding task effective priority. 
 * SEE ALSO
 *  kal_get_task_priority_by_index
 ******************************************************************************/
extern kal_uint32 kal_get_task_priority(kal_taskid taskid);
 /*******************************************************************************
 * <GROUP Functions>
 * 
 * FUNCTION
 *  kal_get_task_priority_by_index
 * DESCRIPTION
 *  get specified task's effective priority by task index.
 * PARAMETERS
 *  index: [IN] specified task's task index.
 * RETURNS
 *  Return corresponding task effective priority. 
 * SEE ALSO
 *  kal_get_task_priority
 ******************************************************************************/
extern kal_uint32 kal_get_task_priority_by_index(task_indx_type index);

/* DOM-NOT_FOR_SDK-BEGIN */
 /*******************************************************************************
 * <GROUP Functions>
 * 
 * FUNCTION
 *  kal_get_task_default_priority_by_index
 * DESCRIPTION
 *  get specified task's default priority by task index.
 * PARAMETERS
 *  taskid: [IN] specified task's task index.
 * RETURNS
 *  Return corresponding task default priority. 
 ******************************************************************************/
extern kal_uint32 kal_get_task_default_priority_by_index(task_indx_type index);
 /*******************************************************************************
 * <GROUP Functions>
 * 
 * FUNCTION
 *  kal_change_priority
 * DESCRIPTION
 *  change specified task's effective priority by taskid.
 * PARAMETERS
 *  taskid: [IN] specified task's taskid.
 *  new_priority: [IN] new effective priority.
 * RETURNS
 *  Return specified task's old effective priority. 
 ******************************************************************************/
extern kal_uint32 kal_change_priority(kal_taskid taskid, kal_uint32 new_priority);
 /*******************************************************************************
 * <GROUP Functions>
 * 
 * FUNCTION
 *  kal_change_priority_by_index
 * DESCRIPTION
 *  change specified task's effective priority by task index.
 * PARAMETERS
 *  index: [IN] specified task's task index.
 *  new_priority: [IN] new effective priority.
 * RETURNS
 *  Return specified task's old effective priority. 
 ******************************************************************************/
extern kal_uint32 kal_change_priority_by_index(task_indx_type index, kal_uint32 new_priority);
 /*******************************************************************************
 * <GROUP Functions>
 * 
 * FUNCTION
 *  kal_change_default_priority_by_index
 * DESCRIPTION
 *  change specified task's default priority by task index.
 * PARAMETERS
 *  index: [IN] specified task's task index.
 *  new_priority: [IN] new default priority.
 * RETURNS
 *  Return specified task's old default priority. 
 ******************************************************************************/
extern kal_uint32 kal_change_default_priority_by_index(task_indx_type index, kal_uint32 new_priority);
 /*******************************************************************************
 * <GROUP Functions>
 * 
 * FUNCTION
 *  kal_get_current_hisr_index
 * DESCRIPTION
 *  get current HISR index.
 * PARAMETERS
 *  N/A
 * RETURNS
 *  current HISR index.
 * NOTE
 *  (1)This API replaces kal_get_my_hisr_index(). And kal_get_my_hisr_index() is deprecated,
 *     it will be removed from next release.
 *  (2)This API must be called from HISR.
 ******************************************************************************/
extern kal_uint32 kal_get_current_hisr_index(void);

static __inline kal_taskid kal_get_task_self_id(void)
{
    return kal_get_current_task();
}

static __inline void kal_get_my_task_index(kal_uint32 *index)
{
    *index = (kal_uint32)kal_get_current_task_index();
}

static __inline void kal_get_my_hisr_index(kal_uint32 *index)
{
    *index = kal_get_current_hisr_index();
}

kal_uint32 stack_change_priority_by_module_ID(module_type mod_ID, kal_uint32 new_priority);
kal_uint32 stack_change_default_priority_by_module_ID(module_type mod_ID, kal_uint32 new_priority);
void *kal_get_task_stack_ptr(kal_taskid taskid);
kal_uint32 kal_get_task_remain_stack_size(kal_taskid task);

extern module_type stack_get_active_module_id(void);
extern void stack_set_active_module_id(kal_uint32 task_index, module_type module_id);

extern kal_uint32 kal_lock_current_stack(void);
extern kal_uint32 kal_unlock_current_stack(void);
/*************************************************************************
* <GROUP Functions>
* 
* FUNCTION
*  kal_check_stack
*
* DESCRIPTION
*   This routine is to check whether the start of current thread stack is corrupted.
*   If so, the system enter fatal error handling.
*
* PARAMETERS
*  N/A
* RETURNS
*
* GLOBALS AFFECTED
*
*************************************************************************/
void kal_check_stack(void);

 /*******************************************************************************
 * <GROUP Functions>
 * 
 * FUNCTION
 *  kal_activate_hisr
 * DESCRIPTION
 *  activate specified HISR.
 * PARAMETERS
 *  ext_hisr_id: [IN] HISR pointer.
 * RETURNS
 *  N/A
 ******************************************************************************/
void kal_activate_hisr(kal_hisrid ext_hisr_id);
 /*******************************************************************************
 * <GROUP Functions>
 * 
 * FUNCTION
 *  kal_if_hisr
 * DESCRIPTION
 *  Identify if the current execution unit is a HISR.
 * PARAMETERS
 *  N/A
 * RETURNS
 *  KAL_FALSE if the current execution unit is a task, and KAL_TRUE if the current execution unit is a HISR.
 * SEE ALSO
 *  kal_if_lisr
 ******************************************************************************/
kal_bool kal_if_hisr(void);
 /*******************************************************************************
 * <GROUP Functions>
 * 
 * FUNCTION
 *  kal_if_lisr
 * DESCRIPTION
 *  Identify if the current execution unit is a LISR.
 * PARAMETERS
 *  N/A
 * RETURNS
 *  KAL_FALSE if the current execution unit is a task or HISR, and KAL_TRUE if the current execution unit is a LISR.
 * SEE ALSO
 *  kal_if_hisr
 ******************************************************************************/
kal_bool kal_if_lisr(void);

/* DOM_NOT_FOR_SDK-END */

/*******************************************************************************
 * Category 4 : Message Passing and Queue
 *******************************************************************************/
/* DOM-NOT_FOR_SDK-BEGIN */
kal_msgqid kal_create_msg_q(kal_char* queue_name_ptr, kal_uint16 msg_size, kal_uint16 max_msgs);
kal_status kal_enque_msg(kal_msgqid ext_msg_q_id, void *buffer_ptr, kal_uint16 msg_size, 
                         kal_wait_mode wait_mode, kal_bool message_to_head);
kal_status kal_deque_msg(kal_msgqid ext_msg_q_id, void* buffer_ptr, kal_uint16 *msg_size,
                         kal_wait_mode wait_mode);
kal_status kal_deque_msg_for_stack(kal_msgqid ext_msg_q_id, void* buffer_ptr, kal_uint16 *msg_size,
                         kal_wait_mode wait_mode);
kal_bool kal_get_queue_info(kal_msgqid task_ext_qid, kal_msgq_info *msgq_info);

extern void* construct_int_local_para(kal_uint16 local_para_size, kal_uint32 auto_reset,
                                      kal_char* file_ptr, kal_uint32 line);
extern void free_int_local_para(local_para_struct *local_para_ptr, kal_char* file, kal_uint32 line);
extern void free_int_local_para_r(local_para_struct *local_para_ptr, kal_char* file, kal_uint32 line);
extern void* construct_int_peer_buff(kal_uint16 pdu_len, kal_uint16 header_len, kal_uint16 tail_len, 
                                     kal_char* file_name_ptr, kal_uint32 line);
extern void free_int_peer_buff(peer_buff_struct *pdu_ptr, kal_char* file, kal_uint32 line);
extern void free_int_peer_buff_r(peer_buff_struct *pdu_ptr, kal_char* file, kal_uint32 line);
extern void destroy_int_ilm(ilm_struct *ilm_ptr, kal_char* file_name, kal_uint32 line);

#if defined(DEBUG_KAL) && defined(DEBUG_BUF2)
#define __construct_local_para(local_para_size, direction) \
            construct_int_local_para(local_para_size, ((kal_uint32)(direction) & (kal_uint32)TD_RESET)? 1 : 0, __FILE__, __LINE__)
#define __free_local_para(local_para) free_int_local_para(local_para, __FILE__, __LINE__)
#define __free_local_para_r(local_para) free_int_local_para_r(local_para, __FILE__, __LINE__)
#define __construct_peer_buff(pdu_len, header_len, tail_len, direction) \
            construct_int_peer_buff(pdu_len, header_len, tail_len, __FILE__, __LINE__)
#define __free_peer_buff(peer_buff) free_int_peer_buff(peer_buff, __FILE__, __LINE__)
#define __free_peer_buff_r(peer_buff) free_int_peer_buff_r(peer_buff, __FILE__, __LINE__)
#else /* !DEBUG_KAL || !DEBUG_BUF2 */
#define __construct_local_para(local_para_size, direction) \
            construct_int_local_para(local_para_size, ((kal_uint32)(direction) & (kal_uint32)TD_RESET)? 1 : 0, NULL, 0)
#define __free_local_para(local_para) free_int_local_para(local_para, NULL, 0)
#define __free_local_para_r(local_para) free_int_local_para_r(local_para, NULL, 0)
#define __construct_peer_buff(pdu_len, header_len, tail_len, direction) \
            construct_int_peer_buff(pdu_len, header_len, tail_len, NULL, 0)
#define __free_peer_buff(peer_buff) free_int_peer_buff(peer_buff, NULL, 0)
#define __free_peer_buff_r(peer_buff) free_int_peer_buff_r(peer_buff, NULL, 0)
#endif
/* DOM-NOT_FOR_SDK-END */
/* DOM-NOT_FOR_SDK-BEGIN */
 /*******************************************************************************
 * <GROUP Functions>
 * 
 * FUNCTION
 *  construct_local_para
 * DESCRIPTION
 *  allocate a local_para structure which has local_para_size bytes including LOCAL_PARA_HDR.
 * PARAMETERS
 *  local_para_size: [IN] local_para structure size, this size includes LOCAL_PARA_HDR header,
 *                   so the minimum size is sizeof(LOCAL_PARA_HDR) which imply the pdu part is 0 byte.
 *  direction: [IN] a bitmask, each bit is defined as following values:
 *             0 - nothing. 
 *             TD_RESET - to zero-initialize the pdu in this local_para structure.
 * RETURNS
 *  a local_para structure pointer. System enter fatal error handling if no enough memory.
 * NOTE
 *  (1) the prototype of this API is:
 *       void* construct_local_para(kal_uint16 local_para_size, transfer_direction direction);
 *  (2) local_para structure is a user-defined structure has a predefined header LOCAL_PARA_HDR 
 *      embedded in front of it:
 *          struct bt_connected_info {
 *              LOCAL_PARA_HDR; // this field must be the first elements in structure
 *              // user-defined fields follows, pdu of this local_para structure
 *          };
 *      LOCAL_PARA_HDR is defined as:
 *          #define LOCAL_PARA_HDR       kal_uint8  ref_count; kal_uint8 lp_reserved; kal_uint16  msg_len;
 *      where msg_len is used to record the length of total structre including LOCAL_PARA_HDR,
 *      ref_count is used to record how many pointers reference to this structure, you must 
 *      hold it on saving a private copy and free it after clear the private copy. 
 *  (3) The initial reference count in local_para structure is 1. It is a common error to 
 *      memset the whole structure to 0, if you want to zero-initialize pdu, pass TD_RESET 
 *      in direction to do that.
 * SEE ALSO
 *  free_local_para, hold_local_para, destroy_ilm, get_local_para_ptr
 ******************************************************************************/
#define construct_local_para(local_para_size, direction) __construct_local_para(local_para_size, direction)
/*******************************************************************************
 * <GROUP Functions>
 * 
 * FUNCTION
 *  free_local_para
 * DESCRIPTION
 *  decrease the local_para strucutre reference count, free the local_para if it reach 0.
 * PARAMETERS
 *  local_para        : [IN] local_para structure pointer.
 * RETURN VALUES
 *  N/A
 * NOTE
 *  (1)The prototype of this API is:
 *      void free_local_para(local_para_struct *local_para_ptr);
 *  (2)To pass user-defined local_para structure pointer to it, you need an explicit type-cast:
 *      free_local_para((local_para_struct *)ptr_to_bt_connected_info);
 * SEE ALSO
 *  construct_local_para, hold_local_para, free_local_para_r, destroy_ilm
 ******************************************************************************/
#define free_local_para(local_para) __free_local_para(local_para)
/*******************************************************************************
 * <GROUP Functions>
 * 
 * FUNCTION
 *  free_local_para_r
 * DESCRIPTION
 *  decrease the local_para strucutre reference count, free the local_para if it reach 0.
 *  it's almost identical to free_local_para, except that free_local_para_r will disable irq during
 *  decrease reference count to avoid race condition.
 * PARAMETERS
 *  local_para        : [IN] local_para structure pointer.
 * RETURN VALUES
 *  N/A
 * NOTE
 *  (1)The prototype of this API is:
 *      void free_local_para_r(local_para_struct *local_para_ptr);
 *  (2)To pass user-defined local_para structure pointer to it, you need an explicit type-cast:
 *      free_local_para_r((local_para_struct *)ptr_to_bt_connected_info);
 * SEE ALSO
 *  construct_local_para, hold_local_para, free_local_para, destroy_ilm
 ******************************************************************************/
#define free_local_para_r(local_para) __free_local_para_r(local_para)
 /*******************************************************************************
 * <GROUP Functions>
 * 
 * FUNCTION
 *  construct_peer_buff
 * DESCRIPTION
 *  allocate a peer_buff structure which has specified size in each part.
 * PARAMETERS
 *  pdu_len: [IN] pdu size.
 *  header_len: [IN] reserve size in header.
 *  tail_len: [IN] reserve size in tail.
 *  direction: [IN] ignored, use 0.
 * RETURNS
 *  a peer_buff structure pointer. System enter fatal error handling if no enough memory.
 * NOTE
 *  (1) the prototype of this API is:
 *       void* construct_peer_buff(kal_uint16 pdu_len, kal_uint16 header_len, kal_uint16 tail_len, 
 *                                  transfer_direction direction);
 *  (2) peer_buff is a opaque type which has reference count. Please use related API to manipulate it.
 *  (3) The initial reference count in peer_buff structure is 1. 
 * SEE ALSO
 *  free_peer_buff, hold_peer_buff, destroy_ilm, get_peer_buff_pdu, append_to_peer_buff, 
 *  prepend_to_peer_buff, remove_head_of_peer_buff, remove_tail_of_peer_buff, update_peer_buff_header
 ******************************************************************************/
#define construct_peer_buff(pdu_len, header_len, tail_len, direction) \
            __construct_peer_buff(pdu_len, header_len, tail_len, direction)
/*******************************************************************************
 * <GROUP Functions>
 * 
 * FUNCTION
 *  free_peer_buff
 * DESCRIPTION
 *  decrease the peer_buff strucutre reference count, free the peer_buff if it reach 0.
 * PARAMETERS
 *  peer_buff        : [IN] peer_buff structure pointer.
 * RETURN VALUES
 *  N/A
 * NOTE
 *  (1)The prototype of this API is:
 *      void free_peer_buff(peer_buff_struct *peer_buff_ptr);
 * SEE ALSO
 *  construct_peer_buff, free_peer_buff_r, hold_peer_buff, destroy_ilm, get_peer_buff_pdu, append_to_peer_buff, 
 *  prepend_to_peer_buff, remove_head_of_peer_buff, remove_tail_of_peer_buff, update_peer_buff_header
 ******************************************************************************/
#define free_peer_buff(peer_buff) __free_peer_buff(peer_buff)
/*******************************************************************************
 * <GROUP Functions>
 * 
 * FUNCTION
 *  free_peer_buff_r
 * DESCRIPTION
 *  decrease the peer_buff strucutre reference count, free the peer_buff if it reach 0.
 *  it's almost identical to free_peer_buff, except that free_peer_buff_r will disable irq during
 *  decrease reference count to avoid race condition.
 * PARAMETERS
 *  peer_buff        : [IN] peer_buff structure pointer.
 * RETURN VALUES
 *  N/A
 * NOTE
 *  (1)The prototype of this API is:
 *      void free_peer_buff_r(peer_buff_struct *peer_buff_ptr);
 * SEE ALSO
 *  construct_peer_buff, free_peer_buff, hold_peer_buff, destroy_ilm, get_peer_buff_pdu, append_to_peer_buff, 
 *  prepend_to_peer_buff, remove_head_of_peer_buff, remove_tail_of_peer_buff, update_peer_buff_header
 ******************************************************************************/
#define free_peer_buff_r(peer_buff) __free_peer_buff_r(peer_buff)

/*******************************************************************************
 * <GROUP Functions>
 * 
 * FUNCTION
 *  hold_local_para
 * DESCRIPTION
 *  If local_para_ptr is not NULL, increment its reference counter by 1.
 * PARAMETERS
 *  local_para_ptr        : [IN] local_para structure pointer.
 * RETURN VALUES
 *  KAL_TRUE if local_para_ptr is not NULL pointer, otherwise, KAL_FALSE.
 * NOTE
 *  (1) To pass user-defined local_para structure pointer to it, you need an explicit type-cast:
 *      hold_local_para((local_para_struct *)ptr_to_bt_connected_info);
 * SEE ALSO
 *  construct_local_para, hold_local_para_r, free_local_para, destroy_ilm
 ******************************************************************************/
extern kal_bool hold_local_para( local_para_struct *local_para_ptr );
/*******************************************************************************
 * <GROUP Functions>
 * 
 * FUNCTION
 *  hold_local_para_r
 * DESCRIPTION
 *  If local_para_ptr is not NULL, increment its reference counter by 1.
 *  it's almost identical to hold_local_para, except that hold_local_para_r will disable irq during
 *  increment reference count to avoid race condition.
 * PARAMETERS
 *  local_para_ptr        : [IN] local_para structure pointer.
 * RETURN VALUES
 *  KAL_TRUE if local_para_ptr is not NULL pointer, otherwise, KAL_FALSE.
 * NOTE
 *  (1) To pass user-defined local_para structure pointer to it, you need an explicit type-cast:
 *      hold_local_para_r((local_para_struct *)ptr_to_bt_connected_info);
 * SEE ALSO
 *  construct_local_para, hold_local_para, free_local_para, free_local_para_r, destroy_ilm
 ******************************************************************************/
extern kal_bool hold_local_para_r(local_para_struct *local_para_ptr);
/*******************************************************************************
 * <GROUP Functions>
 * 
 * FUNCTION
 *  get_local_para_ptr
 * DESCRIPTION
 *  retrieving start address & length of user-defined local_para structure.
 * PARAMETERS
 *  local_para_ptr        : [IN] local_para structure pointer.
 *  local_para_len_ptr    : [OUT] to store length. If it is NULL, the length is not stored.
 * RETURN VALUES
 *  start address of user-defined local_para structure, It's equal to local_para_ptr.
 * NOTE
 *  (1)To pass user-defined local_para structure pointer to it, you need an explicit type-cast:
 *      get_local_para_ptr((local_para_struct *)ptr_to_bt_connected_info, &len);
 * SEE ALSO
 *  construct_local_para
 ******************************************************************************/
void* get_local_para_ptr(local_para_struct *local_para_ptr, kal_uint16 *local_para_len_ptr);

/*******************************************************************************
 * <GROUP Functions>
 * 
 * FUNCTION
 *  hold_peer_buff
 * DESCRIPTION
 *  If peer_buff_ptr is not NULL, increment its reference counter by 1.
 * PARAMETERS
 *  peer_buff_ptr        : [IN] peer_buff structure pointer.
 * RETURN VALUES
 *  KAL_TRUE if peer_buff_ptr is not NULL pointer, otherwise, KAL_FALSE.
 * NOTE
 *  N/A
 * SEE ALSO
 *  construct_peer_buff, hold_peer_buff_r, free_peer_buff, destroy_ilm
 ******************************************************************************/
extern kal_bool hold_peer_buff( peer_buff_struct *peer_buff_ptr);
/*******************************************************************************
 * <GROUP Functions>
 * 
 * FUNCTION
 *  hold_peer_buff_r
 * DESCRIPTION
 *  If peer_buff_ptr is not NULL, increment its reference counter by 1.
 *  it's almost identical to hold_peer_buff, except that hold_peer_buff_r will disable irq during
 *  increment reference count to avoid race condition.
 * PARAMETERS
 *  peer_buff_ptr        : [IN] peer_buff structure pointer.
 * RETURN VALUES
 *  KAL_TRUE if peer_buff_ptr is not NULL pointer, otherwise, KAL_FALSE.
 * NOTE
 *  N/A
 * SEE ALSO
 *  construct_peer_buff, hold_peer_buff, free_peer_buff, free_peer_buff_r, destroy_ilm
 ******************************************************************************/
extern kal_bool hold_peer_buff_r(peer_buff_struct *peer_buff_ptr);
/*******************************************************************************
 * <GROUP Functions>
 * 
 * FUNCTION
 *  get_peer_buff_pdu
 * DESCRIPTION
 *  retrieving start address & length of pdu in peer buffer.
 * PARAMETERS
 *  peer_buff_ptr        : [IN] peer_buff structure pointer.
 *  length_ptr           : [OUT] to store pdu's length. If it is NULL, the length is not stored.
 * RETURN VALUES
 *  start address of pdu in peer buffer.
 * NOTE
  *  (1)This API replaces get_pdu_ptr(). And get_pdu_ptr() is deprecated,
 *     it will be removed from next release.
 * SEE ALSO
 *  construct_peer_buff, free_peer_buff, hold_peer_buff, destroy_ilm, append_to_peer_buff, 
 *  prepend_to_peer_buff, remove_head_of_peer_buff, remove_tail_of_peer_buff, update_peer_buff_header
 ******************************************************************************/
void* get_peer_buff_pdu(peer_buff_struct *peer_buff_ptr, kal_uint16 *length_ptr);
/*******************************************************************************
 * <GROUP Functions>
 * 
 * FUNCTION
 *  prepend_to_peer_buff
 * DESCRIPTION
 *  prepend data to head of peer_buff. On success, pdu length increases header_len bytes,
 *  and free space at head decreases header_len bytes. 
 * PARAMETERS
 *  peer_buff_ptr        : [IN] peer_buff structure pointer.
 *  header_data_ptr      : [IN] data appended.
 *  header_len           : [IN] length of data appended.
 * RETURN VALUES
 *  N/A
 * NOTE
 *  If the free head room is smaller than header_len, system enter fatal error handling.
 * SEE ALSO
 *  construct_peer_buff, free_peer_buff, hold_peer_buff, destroy_ilm, get_peer_buff_pdu, 
 *  append_to_peer_buff, remove_head_of_peer_buff, remove_tail_of_peer_buff, update_peer_buff_header
 ******************************************************************************/
extern void prepend_to_peer_buff( peer_buff_struct *peer_buff_ptr,
                                  void *header_data_ptr,
                                  kal_uint16 header_len);
/*******************************************************************************
 * <GROUP Functions>
 * 
 * FUNCTION
 *  append_to_peer_buff
 * DESCRIPTION
 *  append data to tail of peer_buff, On success, pdu length increases tail_len bytes,
 *  and free space at tail decreases tail_len bytes.
 * PARAMETERS
 *  peer_buff_ptr        : [IN] peer_buff structure pointer.
 *  tail_data_ptr        : [IN] data appended.
 *  tail_len             : [IN] length of data appended.
 * RETURN VALUES
 *  N/A
 * NOTE
 *  If the free tail room is smaller than tail_len, system enter fatal error handling.
 * SEE ALSO
 *  construct_peer_buff, free_peer_buff, hold_peer_buff, destroy_ilm, get_peer_buff_pdu, 
 *  prepend_to_peer_buff, remove_head_of_peer_buff, remove_tail_of_peer_buff, update_peer_buff_header
 ******************************************************************************/
extern void append_to_peer_buff( peer_buff_struct *peer_buff_ptr,
                                 void *tail_data_ptr,
                                 kal_uint16 tail_len);
/*******************************************************************************
 * <GROUP Functions>
 * 
 * FUNCTION
 *  remove_head_of_peer_buff
 * DESCRIPTION
 *  remove data at head of peer_buff. On success, pdu length decreases head_len bytes,
 *  and free space at head increases head_len bytes. 
 * PARAMETERS
 *  peer_buff_ptr        : [IN] peer_buff structure pointer.
 *  head_len             : [IN] length of data to remove.
 * RETURN VALUES
 *  N/A
 * NOTE
 *  (1)If the pdu size is smaller than head_len, system enter fatal error handling.
 *  (2)This API replaces remove_hdr_of_peer_buff(). And remove_hdr_of_peer_buff() is deprecated,
 *     it will be removed from next release.
 * SEE ALSO
 *  construct_peer_buff, free_peer_buff, hold_peer_buff, destroy_ilm, get_peer_buff_pdu, 
 *  prepend_to_peer_buff, append_to_peer_buff, remove_tail_of_peer_buff, update_peer_buff_header
 ******************************************************************************/
extern void remove_head_of_peer_buff(peer_buff_struct *peer_buff_ptr, kal_uint16 head_len);
/*******************************************************************************
 * <GROUP Functions>
 * 
 * FUNCTION
 *  remove_tail_of_peer_buff
 * DESCRIPTION
 *  remove data at tail of peer_buff. On success, pdu length decreases tail_len bytes,
 *  and free space at tail increases tail_len bytes. 
 * PARAMETERS
 *  peer_buff_ptr        : [IN] peer_buff structure pointer.
 *  tail_len             : [IN] length of data to remove.
 * RETURN VALUES
 *  N/A
 * NOTE
 *  If the pdu size is smaller than tail_len, system enter fatal error handling.
 * SEE ALSO
 *  construct_peer_buff, free_peer_buff, hold_peer_buff, destroy_ilm, get_peer_buff_pdu, 
 *  prepend_to_peer_buff, append_to_peer_buff, remove_head_of_peer_buff, update_peer_buff_header
 ******************************************************************************/
extern void remove_tail_of_peer_buff( peer_buff_struct *peer_buff_ptr,
                                      kal_uint16 tail_len);
/*******************************************************************************
 * <GROUP Functions>
 * 
 * FUNCTION
 *  update_peer_buff_header
 * DESCRIPTION
 *  Reinitialize peer buffer, reset size of free header space, pdu, free tail space to new values.
 * PARAMETERS
 *  peer_buff_ptr        : [IN] peer_buff structure pointer.
 *  new_head_len         : [IN] new head size after update.
 *  new_pdu_len          : [IN] new pdu size after update.
 *  new_tail_len         : [IN] new tail size after update.
 * RETURN VALUES
 *  N/A
 * NOTE
 *  (1)If total size is bigger than original size, system enter fatal error handling.
 *  (2)This API replaces update_peer_buff_hdr(). And update_peer_buff_hdr() is deprecated,
 *     it will be removed from next release.
 * SEE ALSO
 *  construct_peer_buff, free_peer_buff, hold_peer_buff, destroy_ilm, get_peer_buff_pdu, 
 *  prepend_to_peer_buff, append_to_peer_buff, remove_head_of_peer_buff, remove_tail_of_peer_buff
 ******************************************************************************/
extern void update_peer_buff_header(peer_buff_struct *peer_buff_ptr, kal_uint16 new_head_len,
                                    kal_uint16 new_pdu_len, kal_uint16 new_tail_len);

/*******************************************************************************
 * <GROUP Functions>
 * 
 * FUNCTION
 *  msg_send
 * DESCRIPTION
 *  send a ILM message to tail of destination task's queue. 
 *  Refer to NOTE about which queue (extq or intq) the message sent to.
 * PARAMETERS
 *  ilm_ptr             : [IN] the ILM message to be sent.
 * RETURN VALUES
 *  N/A
 * NOTE
 *  (1) This API select queue based on following criteria:
 *        a. If src_mod_id and dest_mod_id in ilm_ptr mapped to same task, and the 
 *           internal queue of the task is not NULL, the message is sent to internal queue.
 *        b. Otherwise, the message is sent to external queue.
 *  (2) The ILM messages is appended to tail of the queue.
 *  (3) If the destination queue is full, system enter fatal error handling.
 *  (4) This API replaces msg_send_ext_queue/msg_send_int_queue(). 
 *      They will be removed from next release.
 *  (5) The use of allocate_ilm()/cancel_ilm() is deprecated, 
 *      They will be removed from next release.
 * SEE ALSO
 *  msg_send_to_head, msg_send6, msg_send_to_head6
 ******************************************************************************/
kal_bool msg_send(ilm_struct *ilm_ptr);
/*******************************************************************************
 * <GROUP Functions>
 * 
 * FUNCTION
 *  msg_send_to_head
 * DESCRIPTION
 *  send a ILM message to head of destination task's queue. 
 * PARAMETERS
 *  ilm_ptr             : [IN] the ILM message to be sent.
 * RETURN VALUES
 *  N/A
 * NOTE
 *  (1) If the ILM is sent to internal queue, system enter fatal error handling.
 *      It uses same logic as msg_send to select queue based on following criteria:
 *        a. If src_mod_id and dest_mod_id in ilm_ptr mapped to same task, and the 
 *           internal queue of the task is not NULL, the message is sent to internal queue.
 *        b. Otherwise, the message is sent to external queue.
 *  (2) If the destination queue is full, system enter fatal error handling.
 *  (3) This API replaces msg_send_ext_queue_to_head(). 
 *      They will be removed from next release.
 *  (4) The use of allocate_ilm()/cancel_ilm() is deprecated, 
 *      They will be removed from next release.
 * SEE ALSO
 *  msg_send, msg_send6, msg_send_to_head6
 ******************************************************************************/
kal_bool msg_send_to_head(ilm_struct *ilm_ptr);
/*******************************************************************************
 * <GROUP Functions>
 * 
 * FUNCTION
 *  msg_send6
 * DESCRIPTION
 *  send a ILM message to tail of destination task's queue. 
 *  Refer to NOTE about which queue (extq or intq) the message sent to.
 * PARAMETERS
 *  src_mod_id           : [IN] source module id.
 *  dest_mod_id          : [IN] destination module id.
 *  sap_id               : [IN] service access point id.
 *  msg_id               : [IN] message id.
 *  local_para_ptr       : [IN] local_para structure pointer. 
 *  peer_buff_ptr        : [IN] peer_buff structure pointer.
 * RETURN VALUES
 *  N/A
 * NOTE
 *  (1) This API select queue based on following criteria:
 *        a. If src_mod_id and dest_mod_id in ilm_ptr mapped to same task, and the 
 *           internal queue of the task is not NULL, the message is sent to internal queue.
 *        b. Otherwise, the message is sent to external queue.
 *  (2) The ILM messages is appended to tail of the queue.
 *  (3) If the destination queue is full, system enter fatal error handling.
 *  (4) This API replaces msg_send_ext_queue/msg_send_int_queue(). 
 *      They will be removed from next release.
 *  (5) The use of allocate_ilm()/cancel_ilm() is deprecated, 
 *      They will be removed from next release.
 * SEE ALSO
 *  msg_send, msg_send4, msg_send5, msg_send_to_head, msg_send_to_head6
 ******************************************************************************/
kal_bool msg_send6(module_type _src_mod_id, module_type _dest_mod_id, sap_type _sap_id, msg_type _msg_id, 
                   local_para_struct *_local_para_ptr, peer_buff_struct *_peer_buff_ptr);
/*******************************************************************************
 * <GROUP Functions>
 * 
 * FUNCTION
 *  msg_send5
 * DESCRIPTION
 *  same with msg_send6 with _peer_buff_ptr = NULL.
 * NOTE
 * SEE ALSO
 *  msg_send, msg_send4, msg_send6
 ******************************************************************************/
kal_bool msg_send5(module_type _src_mod_id, module_type _dest_mod_id, sap_type _sap_id, msg_type _msg_id, 
                   local_para_struct *_local_para_ptr);
/*******************************************************************************
 * <GROUP Functions>
 * 
 * FUNCTION
 *  msg_send4
 * DESCRIPTION
 *  same with msg_send6 with _local_para_ptr = NULL & _peer_buff_ptr = NULL.
 * SEE ALSO
 *  msg_send, msg_send5, msg_send6
 ******************************************************************************/
kal_bool msg_send4(module_type _src_mod_id, module_type _dest_mod_id, sap_type _sap_id, msg_type _msg_id);
/*******************************************************************************
 * <GROUP Functions>
 * 
 * FUNCTION
 *  msg_send_to_head
 * DESCRIPTION
 *  send a ILM message to head of destination task's queue. 
 * PARAMETERS
 *  src_mod_id           : [IN] source module id.
 *  dest_mod_id          : [IN] destination module id.
 *  sap_id               : [IN] service access point id.
 *  msg_id               : [IN] message id.
 *  local_para_ptr       : [IN] local_para structure pointer. 
 *  peer_buff_ptr        : [IN] peer_buff structure pointer.
 * RETURN VALUES
 *  N/A
 * NOTE
 *  (1) If the ILM is sent to internal queue, system enter fatal error handling.
 *      It uses same logic as msg_send to select queue based on following criteria:
 *        a. If src_mod_id and dest_mod_id in ilm_ptr mapped to same task, and the 
 *           internal queue of the task is not NULL, the message is sent to internal queue.
 *        b. Otherwise, the message is sent to external queue.
 *  (2) If the destination queue is full, system enter fatal error handling.
 *  (3) This API replaces msg_send_ext_queue_to_head(). 
 *      They will be removed from next release.
 *  (4) The use of allocate_ilm()/cancel_ilm() is deprecated, 
 *      They will be removed from next release.
 * SEE ALSO
 *  msg_send, msg_send6, msg_send_to_head
 ******************************************************************************/
kal_bool msg_send_to_head6(module_type _src_mod_id, module_type _dest_mod_id, sap_type _sap_id, msg_type _msg_id, 
                           local_para_struct *_local_para_ptr, peer_buff_struct *_peer_buff_ptr);
/*******************************************************************************
 * <GROUP Functions>
 * 
 * FUNCTION
 *  msg_send_to_head5
 * DESCRIPTION
 *  same with msg_send_to_head6 with _peer_buff_ptr = NULL.
 * NOTE
 * SEE ALSO
 *  msg_send_to_head, msg_send_to_head4, msg_send_to_head6
 ******************************************************************************/
kal_bool msg_send_to_head5(module_type _src_mod_id, module_type _dest_mod_id, sap_type _sap_id, msg_type _msg_id, 
                           local_para_struct *_local_para_ptr);
/*******************************************************************************
 * <GROUP Functions>
 * 
 * FUNCTION
 *  msg_send_to_head4
 * DESCRIPTION
 *  same with msg_send_to_head6 with _local_para_ptr = NULL & _peer_buff_ptr = NULL.
 * NOTE
 * SEE ALSO
 *  msg_send_to_head, msg_send_to_head5, msg_send_to_head6
 ******************************************************************************/
kal_bool msg_send_to_head4(module_type _src_mod_id, module_type _dest_mod_id, sap_type _sap_id, msg_type _msg_id);
/*******************************************************************************
 * <GROUP Functions>
 * 
 * FUNCTION
 *  msg_send_inline5
 * DESCRIPTION
 *  send a inline-ILM message to tail of destination task's queue. 
 *  Refer to NOTE about which queue (extq or intq) the message sent to.
 * PARAMETERS
 *  src_mod_id           : [IN] source module id.
 *  dest_mod_id          : [IN] destination module id.
 *  msg_id               : [IN] message id.
 *  data                 : [IN] data to be copy in inline-ILM. 
 *  datalen              : [IN] data length, it's maximum value is MSG_INLINE_ILM_MAX_PAYLOAD.
 * RETURN VALUES
 *  N/A
 * NOTE
 *  (1) This API select queue based on following criteria:
 *        a. If src_mod_id and dest_mod_id in ilm_ptr mapped to same task, and the 
 *           internal queue of the task is not NULL, the message is sent to internal queue.
 *        b. Otherwise, the message is sent to external queue.
 *  (2) The ILM messages is appended to tail of the queue.
 *  (3) If the destination queue is full, system enter fatal error handling.
 *  (4) The inline data length specified by datalen is not recorded in destination ILM messages.
 *      Its user's responsibility to know what the inline data is.
 *  (5) The use of allocate_ilm()/cancel_ilm() is deprecated, 
 *      They will be removed from next release.
 * SEE ALSO
 *  msg_send, msg_send_to_head, msg_send_to_head6, msg_send_inline_to_head5, msg_is_inline, msg_get_inline_data
 ******************************************************************************/
kal_bool msg_send_inline5(module_type src_mod, module_type dest_mod, msg_type msg_id, void *data, kal_uint32 datalen);
/*******************************************************************************
 * <GROUP Functions>
 * 
 * FUNCTION
 *  msg_send_inline_to_head5
 * DESCRIPTION
 *  send a ILM message to head of destination task's queue. 
 * PARAMETERS
 *  src_mod_id           : [IN] source module id.
 *  dest_mod_id          : [IN] destination module id.
 *  msg_id               : [IN] message id.
 *  data                 : [IN] data to be copy in inline-ILM. 
 *  datalen              : [IN] data length, it's maximum value is MSG_INLINE_ILM_MAX_PAYLOAD.
 * RETURN VALUES
 *  N/A
 * NOTE
 *  (1) If the ILM is sent to internal queue, system enter fatal error handling.
 *      It uses same logic as msg_send to select queue based on following criteria:
 *        a. If src_mod_id and dest_mod_id in ilm_ptr mapped to same task, and the 
 *           internal queue of the task is not NULL, the message is sent to internal queue.
 *        b. Otherwise, the message is sent to external queue.
 *  (2) If the destination queue is full, system enter fatal error handling.
 *  (3) The inline data length specified by datalen is not recorded in destination ILM messages.
 *      Its user's responsibility to know what the inline data is.
 *  (4) The use of allocate_ilm()/cancel_ilm() is deprecated, 
 *      They will be removed from next release.
 * SEE ALSO
 *  msg_send, msg_send6, msg_send_to_head6, msg_send_inline5, msg_is_inline, msg_get_inline_data
 ******************************************************************************/
kal_bool msg_send_inline_to_head5(module_type src_mod, module_type dest_mod, msg_type msg_id, void *data, kal_uint32 datalen);

/*******************************************************************************
 * <GROUP Functions>
 * 
 * FUNCTION
 *  msg_is_inline
 * DESCRIPTION
 *  judge whether is a inline-ILM or not. 
 * PARAMETERS
 *  ilm_ptr              : [IN] ILM message pointer.
 * RETURN VALUES
 *  KAL_TRUE : this is a inline-ILM.
 *  KAL_FALSE : this isn't a inline-ILM.
 * NOTE
 *  (1) The prototype of this API is:
 *       kal_bool msg_is_inline(ilm_struct *ilm_ptr);
 *  (2) inline-ILM is identified by sap_id, always use this API to do this, never use
 *      sap_id comparison directly since this will change in future.
 * SEE ALSO
 *  msg_send, msg_send6, msg_send_to_head6, msg_send_inline5, msg_send_inline_to_head5, msg_get_inline_data
 ******************************************************************************/
#define msg_is_inline(ilm_ptr) ((ilm_ptr)->sap_id == INLINE_ILM_SAP ? KAL_TRUE : KAL_FALSE)

/*******************************************************************************
 * <GROUP Functions>
 * 
 * FUNCTION
 *  msg_get_inline_data
 * DESCRIPTION
 *  Get the inline data pointer in ILM message. 
 * PARAMETERS
 *  ilm_ptr              : [IN] ILM message pointer.
 * RETURN VALUES
 *  inline data pointer. If it's not an inline-ILM, system enter fatal error handling.
 * NOTE
 *  (1) Users must pass a inline-ILM to this function.
 *  (2) The inline data length is not recorded in ILM messages.
 *      Its user's responsibility to know what the inline data is.
 *
 * SEE ALSO
 *  msg_send, msg_send6, msg_send_to_head6, msg_send_inline5, msg_send_inline_to_head5, msg_get_inline
 ******************************************************************************/
void *msg_get_inline_data(ilm_struct *ilm_ptr);

#if defined(DEBUG_KAL) && defined(DEBUG_ITC)
#define __destroy_ilm(ilm_ptr) \
      destroy_int_ilm((ilm_ptr), (kal_char*)__FILE__, __LINE__ );
#else 
#define __destroy_ilm(ilm_ptr) \
      destroy_int_ilm((ilm_ptr), NULL, 0);
#endif 
/*******************************************************************************
 * <GROUP Functions>
 * 
 * FUNCTION
 *  destroy_ilm
 * DESCRIPTION
 *  destroy an ILM. It will free the local_para structure and peer_buff structure 
 *  associated with this ILM. 
 * PARAMETERS
 *  ilm_ptr        : [IN] ILM message pointer.
 * RETURN VALUES
 *  N/A
 * NOTE
 *  (1) The content pointed by ilm_ptr is not free. 
 *  (2) This API replaces free_ilm(). And free_ilm() is deprecated,
 *     it will be removed from next release.
 * SEE ALSO
 *  msg_send, msg_receive_extq, msg_receive_intq
 ******************************************************************************/
#define destroy_ilm(ilm_ptr) __destroy_ilm(ilm_ptr)

/*******************************************************************************
 * <GROUP Functions>
 * 
 * FUNCTION
 *  msg_receive_extq
 * DESCRIPTION
 *  receive a ILM message from current task's external queue.
 * PARAMETERS
 *  ilm_ptr        : [OUT] ILM message pointer.
 * RETURN VALUES
 *  KAL_TRUE: to indicate this operation finish successfully.
 * NOTE
 *  (1) If the external queue is not empty, this API return immediately. 
 *      Otherwise, it will suspend to wait for the arrival of ILM message.
 *  (2) This API replaces receive_msg_ext_q(). And receive_msg_ext_q() is deprecated,
 *     it will be removed from next release.
 * SEE ALSO
 *  msg_send, msg_receive_intq, destroy_ilm
 ******************************************************************************/
kal_bool msg_receive_extq(ilm_struct *ilm_ptr);
/*******************************************************************************
 * <GROUP Functions>
 * 
 * FUNCTION
 *  msg_receive_extq_for_stack
 * DESCRIPTION
 *  receive a ILM message from current task's external queue used for stack sharing.
 * PARAMETERS
 *  ilm_ptr        : [OUT] ILM message pointer.
 * RETURN VALUES
 *  KAL_TRUE: to indicate this operation finish successfully.
 * NOTE
 *  (1) If the external queue is not empty, this API return immediately. 
 *      Otherwise, it will suspend to wait for the arrival of ILM message.
 *  (2) This API replaces receive_msg_ext_q(). And receive_msg_ext_q() is deprecated,
 *     it will be removed from next release.
 * SEE ALSO
 *  msg_send, msg_receive_intq, destroy_ilm
 ******************************************************************************/
kal_bool msg_receive_extq_for_stack(ilm_struct *ilm_ptr);
/*******************************************************************************
 * <GROUP Functions>
 * 
 * FUNCTION
 *  msg_receive_intq
 * DESCRIPTION
 *  receive a ILM message from current task's internal queue.
 * PARAMETERS
 *  ilm_ptr        : [OUT] ILM message pointer.
 * RETURN VALUES
 *  KAL_TRUE: ILM fetched.
 *  KAL_FALSE: internal queue is empty and no ILM is fetched.
 * NOTE
 *  This API replaces receive_msg_int_q(). And receive_msg_int_q() is deprecated,
 *     it will be removed from next release.
 * SEE ALSO
 *  msg_send, msg_receive_extq, destroy_ilm
 ******************************************************************************/
kal_bool msg_receive_intq(ilm_struct *ilm_ptr);
/*******************************************************************************
 * <GROUP Functions>
 * 
 * FUNCTION
 *  msg_get_task_extq_messages
 * DESCRIPTION
 *  get messages count in external queue of specified task by task index.
 * PARAMETERS
 *  tindex        : [IN] task index.
 * RETURN VALUES
 *  messages count, -1 if error happens.
 * NOTE
 *  (1)This API replaces msg_get_ext_queue_info(). And msg_get_ext_queue_info() is deprecated,
 *     it will be removed from next release.
 * SEE ALSO
 *  msg_get_extq_messages, msg_get_task_extq_capacity, msg_get_extq_capacity
 ******************************************************************************/
kal_int32 msg_get_task_extq_messages(task_indx_type tindex);
/*******************************************************************************
 * <GROUP Functions>
 * 
 * FUNCTION
 *  msg_get_extq_messages
 * DESCRIPTION
 *  get messages count in external queue of current task.
 * PARAMETERS
 *  N/A
 * RETURN VALUES
 *  messages count, -1 if error happens.
 * SEE ALSO
 *  msg_get_task_extq_messages, msg_get_task_extq_capacity, msg_get_extq_capacity
 ******************************************************************************/
kal_int32 msg_get_extq_messages(void);
/*******************************************************************************
 * <GROUP Functions>
 * 
 * FUNCTION
 *  msg_get_task_extq_capacity
 * DESCRIPTION
 *  get capacity of external queue of specified task by task index.
 * PARAMETERS
 *  tindex        : [IN] task index.
 * RETURN VALUES
 *  capacity of target external queue, -1 if error happens.
 * NOTE
 *  (1)This API replaces msg_get_ext_queue_length(). And msg_get_ext_queue_length() is deprecated,
 *     it will be removed from next release.
 * SEE ALSO
 *  msg_get_task_extq_messages, msg_get_extq_messages, msg_get_extq_capacity
 ******************************************************************************/
kal_int32 msg_get_task_extq_capacity(task_indx_type tindex);
/*******************************************************************************
 * <GROUP Functions>
 * 
 * FUNCTION
 *  msg_get_extq_capacity
 * DESCRIPTION
 *  get capacity of external queue of current task.
 * PARAMETERS
 *  N/A
 * RETURN VALUES
 *  capacity of current task external queue, -1 if error happens.
 * SEE ALSO
 *  msg_get_task_extq_messages, msg_get_extq_messages, msg_get_task_extq_capacity
 ******************************************************************************/
kal_int32 msg_get_extq_capacity(void);                                            
/* DOM-NOT_FOR_SDK-END */

/*******************************************************************************
 * Category 5 : Synchronous Mechanism : Mutex , Enhance Mutex , Semaphore and Event Group
 *******************************************************************************/
 
 
/*******************************************************************************
 * <GROUP Synchronous>
 * 
 * FUNCTION
 *  kal_create_mutex
 * DESCRIPTION
 *  create a mutex.
 * PARAMETERS
 *  mutex_name  : [IN] mutex name. it's used for debug only, and only the 
 *                first 8 characters retain.
 * RETURNS
 *  Success:        mutex pointer.
 *  Error:          system enter fatal error handling.
 * NOTE
 *  It is strongly suggested to call this function only in system initialization
 *  time, and the related data allocated for the mutex could not be freed 
 *  once it¡¯s created.
 * SEE ALSO
 *  kal_take_mutex kal_give_mutex
 ******************************************************************************/
kal_mutexid kal_create_mutex(kal_char* mutex_name);
/*******************************************************************************
 * <GROUP Synchronous>
 * 
 * FUNCTION
 *  kal_take_mutex
 * DESCRIPTION
 *  Obtains an instance of the specified mutex. If the mutex is taken already
 *  before this call, the function cannot be immediately satisfied, and caller 
 *  task will be suspended until other task give the ownership to caller task.
 *  After the function returns, caller task is the only task held this mutex.
 * PARAMETERS
 *  mutex_ptr        : [IN] mutex pointer.
 * RETURNS
 *  N/A
 * NOTE
 *  (1)Mutex is not a recursive lock. If caller task already held this mutex, 
 *     then caller task will suspend forever because any task won^t and can^t 
 *     give up mutex^s ownership.
 *  (2)Mutex is a FIFO queue. If multiple tasks are waiting on a mutex, the 
 *     first waiting task is selected to be the next owner.
 * SEE ALSO
 *  kal_create_mutex kal_give_mutex
 ******************************************************************************/
void kal_take_mutex(kal_mutexid mutex_ptr);
/*******************************************************************************
 * <GROUP Synchronous>
 * 
 * FUNCTION
 *  kal_give_mutex
 * DESCRIPTION
 *  Give up mutex ownership. If any task is waiting on this mutex, the first 
 *  waiting task is selected to be the next owner.
 * PARAMETERS
 *  mutex_ptr        : [IN] mutex pointer.
 * RETURNS
 *  N/A
 * NOTE
 *  Mutex ownership is private to task. That means, give can be done at the same
 *  task of take, otherwise fatal error handling takes place. 
 * SEE ALSO
 *  kal_create_mutex kal_take_mutex 
 ******************************************************************************/
void kal_give_mutex(kal_mutexid mutex_ptr);
/*******************************************************************************
 * <GROUP Synchronous>
 * 
 * FUNCTION
 *  kal_create_enh_mutex
 * DESCRIPTION
 *  create an enhmutex which implements Priority Inheritance Protocol(PIP) to 
 *  avoid priority inversion.
 * PARAMETERS
 *  mutex_name  : [IN] enhmutex name. it's used for debug only, and only the 
 *                    first 8 characters retain.
 * RETURNS
 *  Success:        enhmutex pointer.
 *  Error:          system enter fatal error handling.
 * NOTE
 *  It is strongly suggested to call this function only in system initialization
 *  time, and the related data allocated for the enhmutex could not be freed 
 *  once it¡¯s created.
 * SEE ALSO
 *  kal_take_enh_mutex kal_give_enh_mutex
 ******************************************************************************/
kal_enhmutexid kal_create_enh_mutex(kal_char *mutex_name);
/*******************************************************************************
 * <GROUP Synchronous>
 * 
 * FUNCTION
 *  kal_take_enh_mutex
 * DESCRIPTION
 *  Obtain an instance of the specified enhmutex. If the enhmutex is taken 
 *  already before this call, the function cannot be immediately satisfied,
 *  caller task propogates its priority to the owner task if caller task has 
 *  higher priority than owner task, and caller task will be suspended until 
 *  other task give the ownership to caller task. After the function returns, 
 *  caller task is the only task held this enhmutex.
 * PARAMETERS
 *  mutex_ptr        : [IN] enhmutex pointer.
 * RETURNS
 *  N/A
 * NOTE
 *  (1)Enhmutex is not a recursive lock. If caller task already held this 
 *     enhmutex, then caller task will suspend forever because any task will not
 *     give up ehnmutex's ownership.
 *  (2)No promise of enhmutex's queue type. If multiple tasks is waiting on a 
 *     mutex, no promise made which task will selected to run first.
 * SEE ALSO
 *  kal_create_enh_mutex kal_give_enh_mutex
 ******************************************************************************/
void kal_take_enh_mutex(kal_enhmutexid mutex_ptr);
/*******************************************************************************
 * <GROUP Synchronous>
 * 
 * FUNCTION
 *  kal_give_mutex
 * DESCRIPTION
 *  Give up enhmutex ownership. If any task is waiting on this mutex, give the
 *  ownership to any task based on its judge.
 * PARAMETERS
 *  mutex_ptr        : [IN] mutex pointer.
 * RETURNS
 *  N/A
 * NOTE
 *  Enhmutex ownership is private to task. That means, give can be done at the 
 *  same task of take, otherwise fatal error handling takes place. 
 * SEE ALSO
 *  kal_create_enh_mutex kal_take_enh_mutex 
 ******************************************************************************/
void kal_give_enh_mutex(kal_enhmutexid mutex_ptr);
/*******************************************************************************
 * <GROUP Synchronous>
 * 
 * FUNCTION
 *  kal_create_sem
 * DESCRIPTION
 *  create a couting semaphore.
 * PARAMETERS
 *  sem_name  : [IN] semaphore name. it's used for debug only, and only the 
 *                    first 8 characters retain.
 *  initial_count : [IN] semaphore initial count.
 * RETURNS
 *  Success:        semaphore pointer.
 *  Error:          system enter fatal error handling.
 * NOTE
 *  (1)It is strongly suggested to call this function only in system initialization
 *     time, and the related data allocated for the semaphore could not be freed 
 *     once it¡¯s created.
 *  (2)Semaphore values can range from 0 through 4,294,967,294 (2^32 - 2). 
 * SEE ALSO
 *  kal_take_sem kal_give_sem
 ******************************************************************************/
kal_semid kal_create_sem(kal_char *sem_name, kal_uint32 initial_count);
/*******************************************************************************
 * <GROUP Synchronous>
 * 
 * FUNCTION
 *  kal_take_sem
 * DESCRIPTION
 *  Obtains an instance of the specified semaphore. 
 * PARAMETERS
 *  sem_ptr        : [IN] semaphore pointer.
 *  wait_mode      : [IN] wait mode, specify the behavior when the semaphore is
 *                   not ready immediately, it can be one the the following values:
 *                   KAL_NO_WAIT - don't wait for other task gives ownership to it
 *                   KAL_INFINITE_WAIT - wait until ownership can be satisfied
 * RETURN VALUES
 *  KAL_SUCCESS    : the operation is done successfully
 *  KAL_SEM_NOT_AVAILABLE : the semaphore is unavailable immediately
 * NOTE
 *  (1)Semaphore is a FIFO queue. If multiple tasks is waiting on a semaphore, 
 *     the first waiting task is selected to be the next owner.
 * SEE ALSO
 *  kal_create_sem kal_give_sem
 ******************************************************************************/
kal_status kal_take_sem(kal_semid sem_ptr, kal_wait_mode wait_mode);
/*******************************************************************************
 * <GROUP Synchronous>
 * 
 * FUNCTION
 *  kal_give_sem
 * DESCRIPTION
 *  Give up 1 semaphore ownership. If any task is waiting on this semaphore, 
 *  give the ownership to first waiting task.
 * PARAMETERS
 *  sem_ptr        : [IN] semaphore pointer.
 * RETURNS
 *  N/A
 * NOTE
 *  N/A
 * SEE ALSO
 *  kal_create_sem kal_take_sem
 ******************************************************************************/
void kal_give_sem(kal_semid sem_ptr);
/*******************************************************************************
 * <GROUP Communication>
 * 
 * FUNCTION
 *  kal_create_event_group
 * DESCRIPTION
 *  create an event group.
 * PARAMETERS
 *  eventgrp_name  : [IN] event group name. it's used for debug only, and only 
 *                    the first 8 characters retain.
 * RETURNS
 *  Success:        event group pointer.
 *  Error:          system enter fatal error handling.
 * NOTE
 *  (1)It is strongly suggested to call this function only in system initialization
 *     time, and the related data allocated for the event group could not be freed
 *     once it¡¯s created.
 *  (2)Each event group contains 32 event flags, all event flags are initially 
 *     set to 0.
 * SEE ALSO
 *  kal_set_eg_events kal_retrieve_eg_events kal_retrieve_eg_events_timeout
 ******************************************************************************/
kal_eventgrpid kal_create_event_group(kal_char *eventgrp_name);
void kal_delete_eventgrp(kal_eventgrpid eg_id);
/*******************************************************************************
 * <GROUP Communication>
 * 
 * FUNCTION
 *  kal_set_eg_events
 * DESCRIPTION
 *  sets the specified event flags in the specified event group. Any task waiting 
 *  on the event group whose event flag request is satisfied by this service is resumed.
 * PARAMETERS
 *  eg_ptr  : [IN] event group pointer.
 *  events  : [IN] event flags to be set.
 *  operation : [IN] specify the operation, which can be one of following values:
 *              KAL_AND - events in event group is AND with events argument, 
 *                     that is, mask out bits not set in events argument
 *              KAL_OR - events in event group is OR with events argument
 * RETURNS
 *  Success:        return KAL_SUCCESS 
 *  Error:          system enter fatal error handling.
 * NOTE
 *  N/A
 * SEE ALSO
 *  kal_create_event_group kal_retrieve_eg_events kal_retrieve_eg_events_timeout
 ******************************************************************************/
kal_status kal_set_eg_events(kal_eventgrpid eg_ptr, kal_uint32 events, kal_uint8 operation);
/*******************************************************************************
 * <GROUP Communication>
 * 
 * FUNCTION
 *  kal_retrieve_eg_events
 * DESCRIPTION
 *  retrieves the specified event-flag combination from the specified event-flag group.
 * PARAMETERS
 *  eg_ptr  : [IN] event group pointer.
 *  requested_events  : [IN] requested event flags
 *  operation : [IN] specify the operation, which can be one of following values:
 *              KAL_AND - all of the requested event flags are required
 *              KAL_AND_CONSUME - all of requested event flags are required, and
 *                                automatically clears the event flags present on a successful request
 *              KAL_OR - one or more of the requested event flags is sufficient
 *              KAL_OR_CONSUME - one or more of the requested event flags is sufficient, and
 *                               automatically clears the event flags present on a successful request
 *  retrieved_events: [OUT] actual events present in event group.
 *  suspend:  [IN] whether to wait for the event flags set when they can be satisfied immediately,
 *                 it can be one of following values:
 *                 0: don't wait and return KAL_NOT_PRESENT
 *                 KAL_SUSPEND: wait until they can be satisfied
 * RETURN VALUES
 *  KAL_SUCCESS:     the operation is done successfully
 *  KAL_NOT_PRESENT: the request event flags are not present immediately
 *  Error:          system enter fatal error handling.
 * NOTE
 *  N/A
 * SEE ALSO
 *  kal_create_event_group kal_set_eg_events kal_retrieve_eg_events_timeout
 ******************************************************************************/
kal_status kal_retrieve_eg_events(kal_eventgrpid eg_ptr, kal_uint32 requested_events, kal_uint8 operation, 
                                  kal_uint32 *retrieved_events, kal_uint32 suspend);
/*******************************************************************************
 * <GROUP Communication>
 * 
 * FUNCTION
 *  kal_retrieve_eg_events_timeout
 * DESCRIPTION
 *  retrieves the specified event-flag combination from the specified event-flag group.
 * PARAMETERS
 *  eg_ptr  : [IN] event group pointer.
 *  requested_events  : [IN] requested event flags
 *  operation : [IN] specify the operation, which can be one of following values:
 *              KAL_AND - all of the requested event flags are required
 *              KAL_AND_CONSUME - all of requested event flags are required, and
 *                                automatically clears the event flags present on a successful request
 *              KAL_OR - one or more of the requested event flags is sufficient
 *              KAL_OR_CONSUME - one or more of the requested event flags is sufficient, and
 *                               automatically clears the event flags present on a successful request
 *  retrieved_events: [OUT] actual events present in event group.
 *  timeout:  [IN] how long to wait for the event flags set when they can be satisfied immediately,
 *                 it can be one of following values:
 *                 0 - don't wait and return KAL_NOT_PRESENT
 *                 KAL_SUSPEND - wait until they can be satisfied
 *                 other values - wait at most timeout ticks, return KAL_NOT_PRESENT if timeout happens
 * RETURN VALUES
 *  KAL_SUCCESS:     the operation is done successfully
 *  KAL_NOT_PRESENT: the request event flags are not present immediately
 *  Error:          system enter fatal error handling.
 * NOTE
 *  This API's availability is dependent on a macro HAS_KAL_RETRIEVE_EG_EVENTS_TIMEOUT.
 *  If it's not defined, then this API is not available.
 * SEE ALSO
 *  kal_create_event_group kal_set_eg_events kal_retrieve_eg_events
 ******************************************************************************/
kal_status kal_retrieve_eg_events_timeout(kal_eventgrpid eg_ptr, kal_uint32 requested_events, kal_uint8 operation, 
                                          kal_uint32 *retrieved_events, kal_uint32 timeout);
#define HAS_KAL_RETRIEVE_EG_EVENTS_TIMEOUT 1


/*******************************************************************************
 * Category 6 : Application Memory Management  ADM , AFM
 *******************************************************************************/
 
#if !defined(__FUE__)
extern KAL_ADM_ID (kal_adm_create)(void *mem_addr, kal_uint32 size, kal_uint32 *subpool_size, kal_bool islogging);

/*******************************************************************************
 * <GROUP Functions>
 * 
 * FUNCTION
 *  kal_adm_create2
 * DESCRIPTION
 *  Create a ADM pool.
 * PARAMETERS
 *  mem_addr: [IN] starting address of the memory pool. It should be 4-byte alignment, or it will be aligned to next 4-aligned address.
 *  size: [IN] total size in byte of the memory pool.
 *  subpool_size: [IN] is an array, which describe size of each sub-pool.
 *  flags: [IN] new ADM pool extra flags, which is a combination of following values
 *         KAL_ADM_GUARDIAN_FLAG - RECOMMENDED! put a guard word before and after each user buffer to detect memory corruption
 *                                 it also enable some other error detections on this ADM pool.
 *         KAL_ADM_EXTHEADER_FLAG - has an extensible header immediate before each user buffer, 
 *                                  whose size is specified by extheader_size_in_word
 *         KAL_ADM_CLEANDELETE_FLAG - on kal_adm_delete(), if any user buffer is not free yet, 
 *                                    system enter fatal error handling
 *         KAL_ADM_REALLOCFAIL_FATAL_FLAG - if kal_adm_realloc() failed, system enter fatal error handling
 *         KAL_ADM_REALLOCFAIL_FREE_FLAG - if kal_adm_realloc() failed, free old user buffer before return NULL
 *  extheader_size_in_word: [IN] specified the extensible header size, only takes effect when KAL_ADM_EXTHEADER_FLAG is specified.
 *                          this argument specified in unit of word, you can use KAL_ADM_EXTHEADER_WORDS(sizeof(struct A)) if you want 
 *                          to put a struct A in extensible header.
 * RETURNS
 *  ADM id. If memory is too small to create a ADM, return NULL.
 * NOTE
 *  (1)This API replaces kal_adm_create(). And kal_adm_create() is deprecated,
 *     it will be removed from next release.
 *  (2)subpool_size must be monotonic increasing and ends with {0xffffffff, 0}. 
 *     If it's NULL, the default subpool size setting is used: { 16, 32, 64, 128, 256, 512, 0xffffffff, 0 }.
 *     Default subpool size has 7 subpools.
 *  (3)The ADM overhead for control block is divided into 2 parts:
 *     Static - that is (16*3+16*subpool_count}. subpool_count is element count in subpool_size except last element 0.
 *     Dynamic - Each user block has an extra: (8+extheader_size_in_word*4)
 * SEE ALSO
 *  kal_adm_delete KAL_ADM_EXTHEADER_WORDS
 ******************************************************************************/
extern KAL_ADM_ID kal_adm_create2(void *mem_addr, kal_uint32 size, kal_uint32 *subpool_size, kal_uint32 flags, kal_int8 extheader_size_in_word);
/*******************************************************************************
 * <GROUP Functions>
 * 
 * FUNCTION
 *  kal_adm_delete
 * DESCRIPTION
 *  delete a ADM pool.
 * PARAMETERS
 *  adm_id: [IN] ADM pool identity.
 * RETURN VALUES
 *  KAL_SUCCESS: the operation succeeded.
 *  KAL_MEMORY_NOT_RELEASE: some user buffer is not free yet.
 * NOTE
 *  if KAL_ADM_CLEANDELETE_FLAG is specified on creation and any user buffer is not free yet, 
 *  system enter fatal error handling
 * SEE ALSO
 *  kal_adm_create2
 ******************************************************************************/
extern kal_status kal_adm_delete(KAL_ADM_ID adm_id);

extern void *__kal_adm_alloc(KAL_ADM_ID adm_id, kal_uint32 size, kal_uint32 *actual_size, kal_uint32 opt, char *filename, kal_uint32 line);
extern void *__kal_adm_alloc_nc(KAL_ADM_ID adm_id, kal_uint32 size, kal_uint32 *actual_size, kal_uint32 option, char *filename, kal_uint32 line);
extern void *__kal_adm_alloc_c(KAL_ADM_ID adm_id, kal_uint32 size, kal_uint32 *actual_size, kal_uint32 option, char *filename, kal_uint32 line);

extern void *__kal_adm_realloc(KAL_ADM_ID adm_id, void *ptr, kal_uint32 size, kal_uint32 *actual_size, kal_uint32 opt, char *filename, kal_uint32 line);
extern void *__kal_adm_realloc_nc(KAL_ADM_ID adm_id, void *ptr, kal_uint32 size, kal_uint32 *actual_size, kal_uint32 opt, char *filename, kal_uint32 line);
extern void *__kal_adm_realloc_c(KAL_ADM_ID adm_id, void *ptr, kal_uint32 size, kal_uint32 *actual_size, kal_uint32 opt, char *filename, kal_uint32 line);


/*******************************************************************************
 * <GROUP Functions>
 * 
 * FUNCTION
 *  kal_adm_alloc
 * DESCRIPTION
 *  allocate a memory buffer from ADM.
 * PARAMETERS
 *  adm_id: [IN] ADM pool identity.
 *  size: [IN] request memory size.
 * RETURNS
 *  allocated memory buffer pointer. return NULL if no memory available.
 * NOTE 
 *  the prototype of this API is:
 *   void *kal_adm_alloc(KAL_ADM_ID adm_id, kal_uint32 size);
 * SEE ALSO
 *  kal_adm_create2 kal_adm_alloc_with_info kal_adm_free
 ******************************************************************************/
#define kal_adm_alloc(adm_id, size) __kal_adm_alloc((adm_id), (size), NULL, 0, __FILE__, __LINE__)
#define kal_adm_alloc_dbg(adm_id, size, f, l) __kal_adm_alloc((adm_id), (size), NULL, 0, (f), (l))
/*******************************************************************************
 * <GROUP Functions>
 * 
 * FUNCTION
 *  kal_adm_alloc_with_info
 * DESCRIPTION
 *  allocate a memory buffer from ADM, return the actually size (including any ADM overhead) of this memory block also.
 * PARAMETERS
 *  adm_id: [IN] ADM pool identity.
 *  size: [IN] request memory size.
 *  actual_size: [OUT] return the actually size in it.
 * RETURNS
 *  allocated memory buffer pointer. return NULL if no memory available.
 * NOTE 
 *  the prototype of this API is:
 *   void *kal_adm_alloc_with_info(KAL_ADM_ID adm_id, kal_uint32 size, kal_uint32 *actual_size);
 * SEE ALSO
 *  kal_adm_create2 kal_adm_alloc kal_adm_free
 ******************************************************************************/
#define kal_adm_alloc_with_info(adm_id, size, actual_size) __kal_adm_alloc((adm_id), (size), (actual_size), 0x0, __FILE__, __LINE__)
/*******************************************************************************
 * <GROUP Functions>
 * 
 * FUNCTION
 *  kal_adm_alloc_align
 * DESCRIPTION
 *  allocate a memory buffer which satisfy the alignment requirement from ADM.
 * PARAMETERS
 *  adm_id: [IN] ADM pool identity.
 *  size: [IN] request memory size.
 *  align: [IN] alignment requirement, which must be a multiple of 4.
 * RETURNS
 *  allocated memory buffer pointer. return NULL if no memory available.
 * NOTE 
 *  the prototype of this API is:
 *   void *kal_adm_alloc_align(KAL_ADM_ID adm_id, kal_uint32 size, kal_uint32 alignment);
 * SEE ALSO
 *  kal_adm_create2 kal_adm_free
 ******************************************************************************/
#define kal_adm_alloc_align(adm_id, size, alignment) __kal_adm_alloc((adm_id), (size), NULL, (alignment) & ~ADM_OPT_MASK, __FILE__, __LINE__)
#define kal_adm_alloc_align_dbg(adm_id, size, alignment, f, l) __kal_adm_alloc((adm_id), (size), NULL, (alignment) & ~ADM_OPT_MASK, (f), (l))


#if defined(__MTK_TARGET__) && (defined(__ARM9_MMU__) || defined(__DYNAMIC_SWITCH_CACHEABILITY__))
#define __kal_adm_alloc_cacheable(adm_id, size, option) __kal_adm_alloc_c((adm_id), (size), NULL, 0, __FILE__, __LINE__)
#define __kal_adm_alloc_cacheable_with_info(adm_id, size, option, actual_size) __kal_adm_alloc_c((adm_id), (size), (actual_size), 0x0, __FILE__, __LINE__)
#define __kal_adm_alloc_cacheable_dbg(adm_id, size, option, f, l) __kal_adm_alloc_c((adm_id), (size), NULL, 0, (f), (l))
#else 
#define __kal_adm_alloc_cacheable(adm_id, size, option) __kal_adm_alloc((adm_id), (size), NULL, 0, __FILE__, __LINE__)
#define __kal_adm_alloc_cacheable_with_info(adm_id, size, option, actual_size) __kal_adm_alloc((adm_id), (size), (actual_size), 0x0, __FILE__, __LINE__)
#define __kal_adm_alloc_cacheable_dbg(adm_id, size, option, f, l) __kal_adm_alloc((adm_id), (size), NULL, 0, (f), (l))
#endif 
/*******************************************************************************
 * <GROUP Functions>
 * 
 * FUNCTION
 *  kal_adm_alloc_cacheable
 * DESCRIPTION
 *  allocate a memory buffer which is cacheable from ADM.
 * PARAMETERS
 *  adm_id: [IN] ADM pool identity.
 *  size: [IN] request memory size.
 *  option_notused: [IN] useless, ignored.
 * RETURNS
 *  allocated memory buffer pointer. return NULL if no memory available.
 * NOTE 
 *  (1)the prototype of this API is:
 *   void *kal_adm_alloc_cacheable(KAL_ADM_ID adm_id, kal_uint32 size, kal_uint32 option_notused);
 *  (2)The pointer is cacheable only on platform that has ARM MMU (such as ARM9/ARM11 platform).
 *     If not, the pointer is still noncacheable.
 * SEE ALSO
 *  kal_adm_create2 kal_adm_alloc_cacheable_with_info kal_adm_alloc_noncacheable kal_adm_free
 ******************************************************************************/
#define kal_adm_alloc_cacheable(adm_id, size, option_notused) __kal_adm_alloc_cacheable(adm_id, size, option_notused)
/*******************************************************************************
 * <GROUP Functions>
 * 
 * FUNCTION
 *  kal_adm_alloc_cacheable_with_info
 * DESCRIPTION
 *  allocate a memory buffer which is cacheable from ADM, return the actually size (including any ADM overhead) of this memory block also.
 * PARAMETERS
 *  adm_id: [IN] ADM pool identity.
 *  size: [IN] request memory size.
 *  option_notused: [IN] useless, ignored.
 *  actual_size: [OUT] return the actually size in it.
 * RETURNS
 *  allocated memory buffer pointer. return NULL if no memory available.
 * NOTE 
 *  (1)the prototype of this API is:
 *   void *kal_adm_alloc_cacheable_with_info(KAL_ADM_ID adm_id, kal_uint32 size, kal_uint32 option_notused, kal_uint32 *actual_size);
 *  (2)The pointer is cacheable only on platform that has ARM MMU (such as ARM9/ARM11 platform).
 *     If not, the pointer is still noncacheable.
 * SEE ALSO
 *  kal_adm_create2 kal_adm_alloc_cacheable kal_adm_alloc_noncacheable kal_adm_free
 ******************************************************************************/
#define kal_adm_alloc_cacheable_with_info(adm_id, size, option_notused, actual_size) __kal_adm_alloc_cacheable_with_info(adm_id, size, option_notused, actual_size)
#define kal_adm_alloc_cacheable_dbg(adm_id, size, option_notused, f, l) __kal_adm_alloc_cacheable_dbg(adm_id, size, option_notused, f, l)

#if defined(__MTK_TARGET__) && (defined(__ARM9_MMU__) || defined(__DYNAMIC_SWITCH_CACHEABILITY__))
#define __kal_adm_alloc_noncacheable(adm_id, size, option) __kal_adm_alloc_nc((adm_id), (size), NULL, 0, __FILE__, __LINE__)
#define __kal_adm_alloc_noncacheable_with_info(adm_id, size, option, actual_size) __kal_adm_alloc_nc((adm_id), (size), (actual_size), 0x0, __FILE__, __LINE__)
#define __kal_adm_alloc_noncacheable_dbg(adm_id, size, option, f, l) __kal_adm_alloc_nc((adm_id), (size), NULL, 0, (f), (l))
#else 
#define __kal_adm_alloc_noncacheable(adm_id, size, option) __kal_adm_alloc((adm_id), (size), NULL, 0, __FILE__, __LINE__)
#define __kal_adm_alloc_noncacheable_with_info(adm_id, size, option, actual_size) __kal_adm_alloc((adm_id), (size), (actual_size), 0x0, __FILE__, __LINE__)
#define __kal_adm_alloc_noncacheable_dbg(adm_id, size, option, f, l) __kal_adm_alloc((adm_id), (size), NULL, 0, (f), (l))
#endif 
/*******************************************************************************
 * <GROUP Functions>
 * 
 * FUNCTION
 *  kal_adm_alloc_noncacheable
 * DESCRIPTION
 *  allocate a memory buffer which is noncacheable from ADM.
 * PARAMETERS
 *  adm_id: [IN] ADM pool identity.
 *  size: [IN] request memory size.
 *  option_notused: [IN] useless, ignored.
 * RETURNS
 *  allocated memory buffer pointer. return NULL if no memory available.
 * NOTE 
 *  (1)the prototype of this API is:
 *   void *kal_adm_alloc_noncacheable(KAL_ADM_ID adm_id, kal_uint32 size, kal_uint32 option_notused);
 * SEE ALSO
 *  kal_adm_create2 kal_adm_alloc_noncacheable_with_info kal_adm_alloc_cacheable kal_adm_free
 ******************************************************************************/
#define kal_adm_alloc_noncacheable(adm_id, size, option_notused) __kal_adm_alloc_noncacheable(adm_id, size, option_notused)
/*******************************************************************************
 * <GROUP Functions>
 * 
 * FUNCTION
 *  kal_adm_alloc_noncacheable_with_info
 * DESCRIPTION
 *  allocate a memory buffer which is noncacheable from ADM, return the actually size (including any ADM overhead) of this memory block also.
 * PARAMETERS
 *  adm_id: [IN] ADM pool identity.
 *  size: [IN] request memory size.
 *  option_notused: [IN] useless, ignored.
 *  actual_size: [OUT] return the actually size in it.
 * RETURNS
 *  allocated memory buffer pointer. return NULL if no memory available.
 * NOTE 
 *  (1)the prototype of this API is:
 *   void *kal_adm_alloc_noncacheable_with_info(KAL_ADM_ID adm_id, kal_uint32 size, kal_uint32 option_notused, kal_uint32 *actual_size);
 * SEE ALSO
 *  kal_adm_create2 kal_adm_alloc_noncacheable kal_adm_alloc_cacheable kal_adm_free
 ******************************************************************************/
#define kal_adm_alloc_noncacheable_with_info(adm_id, size, option_notused, actual_size) __kal_adm_alloc_noncacheable_with_info(adm_id, size, option_notused, actual_size)
#define kal_adm_alloc_noncacheable_dbg(adm_id, size, option_notused, f, l) __kal_adm_alloc_noncacheable_dbg(adm_id, size, option_notused, f, l)


/*******************************************************************************
 * <GROUP Functions>
 * 
 * FUNCTION
 *  kal_adm_realloc
 * DESCRIPTION
 *  re-allocate a memory buffer from ADM.
 * PARAMETERS
 *  adm_id: [IN] ADM pool identity.
 *  ptr: [IN] old memory buffer pointer.
 *  size: [IN] request memory size.
 * RETURNS
 *  re-allocated memory buffer pointer. return NULL if no memory available.
 * SEE ALSO
 *  kal_adm_create2 kal_adm_free kal_adm_alloc KAL_ADM_REALLOCFAIL_FATAL_FLAG KAL_ADM_REALLOCFAIL_FREE_FLAG
 ******************************************************************************/
#define kal_adm_realloc(adm_id, ptr, size) __kal_adm_realloc((adm_id), (ptr), (size), NULL, 0, __FILE__, __LINE__)
#define kal_adm_realloc_dbg(adm_id, ptr, size, f, l) __kal_adm_realloc((adm_id), (ptr), (size), NULL, 0, (f), (l))
#if defined(__MTK_TARGET__) && defined(__DYNAMIC_SWITCH_CACHEABILITY__)
#define __kal_adm_realloc_noncacheable(adm_id, ptr, size) __kal_adm_realloc_nc((adm_id), (ptr), (size), NULL, 0, __FILE__, __LINE__) 
#define __kal_adm_realloc_cacheable(adm_id, ptr, size) __kal_adm_realloc_c((adm_id), (ptr), (size), NULL, 0, __FILE__, __LINE__) 
#else
#define __kal_adm_realloc_noncacheable(adm_id, ptr, size) kal_adm_realloc(adm_id, ptr, size)
#define __kal_adm_realloc_cacheable(adm_id, ptr, size) kal_adm_realloc(adm_id, ptr, size)
#endif

#define kal_adm_realloc_cacheable(adm_id, ptr, size) __kal_adm_realloc_cacheable(adm_id, ptr, size)
#define kal_adm_realloc_noncacheable(adm_id, ptr, size) __kal_adm_realloc_noncacheable(adm_id, ptr, size)

/*******************************************************************************
 * <GROUP Functions>
 * 
 * FUNCTION
 *  kal_adm_free
 * DESCRIPTION
 *  free a memory buffer to dedicated ADM.
 * PARAMETERS
 *  adm_id: [IN] ADM pool identity.
 *  ptr: [IN] memory buffer pointer to be free. This pointer must be allocated from adm_id.
 * RETURNS
 *  N/A
 * SEE ALSO
 *  kal_adm_create2 kal_adm_alloc kal_adm_realloc
 ******************************************************************************/
extern void kal_adm_free(KAL_ADM_ID adm_id, void *ptr);
/*******************************************************************************
 * <GROUP Functions>
 * 
 * FUNCTION
 *  kal_adm_get_extheader
 * DESCRIPTION
 *  get the extensible header associated with ptr, which is immediate before ptr. 
 * PARAMETERS
 *  adm_id: [IN] ADM pool identity.
 *  ptr: [IN] memory buffer pointer to query. This pointer must be returned from kal_adm_alloc*() functions.
 * RETURNS
 *  associated extensible header pointer.
 * NOTE
 *  (1)This ADM pool must has KAL_ADM_EXTHEADER_FLAG on creation.
 *  (2)The size of this extensible header is (extheader_size_in_word*4)
 * SEE ALSO
 *  kal_adm_create2 KAL_ADM_EXTHEADER_FLAG
 ******************************************************************************/
extern void *kal_adm_get_extheader(KAL_ADM_ID adm_id, void *ptr);
/*******************************************************************************
 * <GROUP Functions>
 * 
 * FUNCTION
 *  kal_adm_get_max_alloc_size
 * DESCRIPTION
 *  get maximal available memory size to be allocated in a single kal_adm_alloc*(). 
 * PARAMETERS
 *  adm_id: [IN] ADM pool identity.
 * RETURNS
 *  maximal available memory size to be allocated.
 * NOTE
 *  (1)This size is guaranteed to allocated without considering any other requirement.
 *     For example, kal_adm_alloc(adm_id, kal_adm_get_max_alloc_size(adm_id)) can succeed.
 *     But kal_adm_alloc_align() is not guaranteed to succeed.
 * SEE ALSO
 *  kal_adm_get_total_left_size
 ******************************************************************************/
extern kal_uint32 kal_adm_get_max_alloc_size(KAL_ADM_ID adm_id);
/*******************************************************************************
 * <GROUP Functions>
 * 
 * FUNCTION
 *  kal_adm_get_total_left_size
 * DESCRIPTION
 *  get total un-allocated memory size. 
 * PARAMETERS
 *  adm_id: [IN] ADM pool identity.
 * RETURNS
 *  get total un-allocated memory size. 
 * NOTE
 *  (1)Total un-allocated memory size may be splitted in many blocks(fragmentation).
 * SEE ALSO
 *  kal_adm_get_max_alloc_size
 ******************************************************************************/
extern kal_uint32 kal_adm_get_total_left_size(KAL_ADM_ID adm_id);
/*******************************************************************************
 * <GROUP Functions>
 * 
 * FUNCTION
 *  kal_adm_check_integrity
 * DESCRIPTION
 *  Check the healthy of ADM pool. 
 * PARAMETERS
 *  adm_id: [IN] ADM pool identity.
 * RETURNS
 *  return bad memory block first found. If the ADM pool is healthy, return NULL to indicate
 *  nothing wrong.
 ******************************************************************************/
extern void *kal_adm_check_integrity(KAL_ADM_ID adm_id);

/* DOM-NOT_FOR_SDK-BEGIN */
extern void kal_adm_print_log(KAL_ADM_ID adm_id);
extern kal_uint32 kal_adm_calc_worst_alloc_size(KAL_ADM_ID adm_id, kal_uint32 size, kal_uint32 option);
extern void *kal_adm_get_prev_block_ptr(KAL_ADM_ID adm_id,void *mem_addr);
extern void *kal_adm_get_next_block_ptr(KAL_ADM_ID adm_id,void *mem_addr);
extern void *kal_adm_get_trans_ptr(KAL_ADM_ID adm_id,void *mem_addr);


/*
 * Function prototype: void *kal_adm_alloc_mtkL1_cacheable(KAL_ADM_ID adm_id, kal_uint32 size, kal_uint32 option)
 * Note: Allocate a MTKL1-cacheable memory block.
 */
#if defined(__MTK_TARGET__) && defined(__MTK_L1CACHEABLE__)
#define kal_adm_alloc_mtkL1_cacheable(adm_id, size, option) __kal_adm_alloc_c((adm_id), (size), NULL, (option), __FILE__, __LINE__)
#else /* __MTK_TARGET__ && __MTK_L1CACHEABLE__ */
#define kal_adm_alloc_mtkL1_cacheable(adm_id, size, option) __kal_adm_alloc((adm_id), (size), NULL, 0, __FILE__, __LINE__)
#endif /* __MTK_TARGET__ && __MTK_L1CACHEABLE__ */

/*
 * Function prototype: void *kal_adm_alloc_mtkL1_noncacheable(KAL_ADM_ID adm_id, kal_uint32 option)
 * Note: Allocate a memory block. This is for RFU only.
 */
#define kal_adm_alloc_mtkL1_noncacheable(adm_id, size, option) __kal_adm_alloc((adm_id), (size), NULL, 0, __FILE__, __LINE__)

/*
 * Function prototype: void *kal_adm_alloc_nc_align(KAL_ADM_ID adm_id, kal_uint32 align_size)
 * Note: Allocate a non-cacheable and aligned memory block.
 */
#if defined(__MTK_TARGET__) && (defined(__ARM9_MMU__) || defined(__DYNAMIC_SWITCH_CACHEABILITY__))
#define kal_adm_alloc_nc_align(adm_id, size, a) __kal_adm_alloc_nc((adm_id), (size), NULL, (a) & ~ADM_OPT_MASK, __FILE__, __LINE__)
#define kal_adm_alloc_nc_align_dbg(adm_id, size, a, f, l) __kal_adm_alloc_nc((adm_id), (size), NULL, (a) & ~ADM_OPT_MASK, (f), (l))
#else /* __MTK_TARGET__ && (__ARM9_MMU__ || __DYNAMIC_SWITCH_CACHEABILITY__) */
#define kal_adm_alloc_nc_align(adm_id, size, a) __kal_adm_alloc((adm_id), (size), NULL, (a) & ~ADM_OPT_MASK, __FILE__, __LINE__)
#define kal_adm_alloc_nc_align_dbg(adm_id, size, a, f, l) __kal_adm_alloc((adm_id), (size), NULL, (a) & ~ADM_OPT_MASK, (f), (l))
#endif /* __MTK_TARGET__ && (__ARM9_MMU__ || __DYNAMIC_SWITCH_CACHEABILITY__) */

/*
 * Function prototype: void *kal_adm_alloc_topmost(KAL_ADM_ID adm_id, kal_uint32 size, kal_uint32 align_size)
 *                     void *kal_adm_alloc_topmost_dbg(KAL_ADM_ID adm_id, kal_uint32 size, kal_uint32 align_size, kal_char *filename, kal_uint32 linenumber)
 * Note: Allocate a memory block from the topmost address of the pool.
 *       The start address of the allocated memory will be aligned to the input parameter align_size.
 *       kal_adm_alloc_topmost_dbg() is the debug verion. 
 *       The input parameters filename and line will be used for logging if logging is enabled.
 */
#define kal_adm_alloc_topmost(adm_id, size, a) __kal_adm_alloc((adm_id), (size), NULL, ((a) & ~ADM_OPT_MASK) | ADM_OPT_TOPMOST_ALLOC, __FILE__, __LINE__)
#define kal_adm_alloc_topmost_dbg(adm_id, size, a, f, l) __kal_adm_alloc((adm_id), (size), NULL, ((a) & ~ADM_OPT_MASK) | ADM_OPT_TOPMOST_ALLOC, (f), (l))

/*
 * Function prototype: void *kal_adm_alloc_c_topmost(KAL_ADM_ID adm_id, kal_uint32 size, kal_uint32 align_size)
 *                     void *kal_adm_alloc_c_topmost_dbg(KAL_ADM_ID adm_id, kal_uint32 size, kal_uint32 align_size, kal_char *filename, kal_uint32 line)
 * Note: Allocate a cacheable memory block from the topmost address of the pool.
 *       The start address of the allocated memory will be aligned to the input parameter align_size.
 */
#if defined(__MTK_TARGET__) && (defined(__ARM9_MMU__) || defined(__DYNAMIC_SWITCH_CACHEABILITY__))
#define kal_adm_alloc_c_topmost(adm_id, size, a) __kal_adm_alloc_c((adm_id), (size), NULL, ((a) & ~ADM_OPT_MASK) | ADM_OPT_TOPMOST_ALLOC, __FILE__, __LINE__)
#define kal_adm_alloc_c_topmost_dbg(adm_id, size, a, f, l) __kal_adm_alloc_c((adm_id), (size), NULL, ((a) & ~ADM_OPT_MASK) | ADM_OPT_TOPMOST_ALLOC, (f), (l))
#else /* __MTK_TARGET__ && (__ARM9_MMU__ || __DYNAMIC_SWITCH_CACHEABILITY__)*/
#define kal_adm_alloc_c_topmost(adm_id, size, a) __kal_adm_alloc((adm_id), (size), NULL, ((a) & ~ADM_OPT_MASK) | ADM_OPT_TOPMOST_ALLOC, __FILE__, __LINE__)
#define kal_adm_alloc_c_topmost_dbg(adm_id, size, a, f, l) __kal_adm_alloc((adm_id), (size), NULL, ((a) & ~ADM_OPT_MASK) | ADM_OPT_TOPMOST_ALLOC, (f), (l))
#endif /* __MTK_TARGET__ && (__ARM9_MMU__ || __DYNAMIC_SWITCH_CACHEABILITY__)*/

/*
 * Function prototype: void *kal_adm_alloc_nc_topmost(KAL_ADM_ID adm_id, kal_uint32 size, kal_uint32 align_size)
 *                     void *kal_adm_alloc_nc_topmost_dbg(KAL_ADM_ID adm_id, kal_uint32 size, kal_uint32 align_size, kal_char *filename, kal_uint32 line)
 * Note: Allocate a non-cacheable memory block from the topmost address of the pool.
 *       The start address of the allocated memory will be aligned to the input parameter align_size.
 *       kal_adm_alloc_nc_topmost_dbg() is the debug version.
 */
#if defined(__MTK_TARGET__) && (defined(__ARM9_MMU__) || defined(__DYNAMIC_SWITCH_CACHEABILITY__))
#define kal_adm_alloc_nc_topmost(adm_id, size, a) __kal_adm_alloc_nc((adm_id), (size), NULL, ((a) & ~ADM_OPT_MASK) | ADM_OPT_TOPMOST_ALLOC, __FILE__, __LINE__)
#define kal_adm_alloc_nc_topmost_dbg(adm_id, size, a, f, l) __kal_adm_alloc_nc((adm_id), (size), NULL, ((a) & ~ADM_OPT_MASK) | ADM_OPT_TOPMOST_ALLOC, (f), (l))
#else /* __MTK_TARGET__ && (__ARM9_MMU__ || __DYNAMIC_SWITCH_CACHEABILITY__)*/
#define kal_adm_alloc_nc_topmost(adm_id, size, a) __kal_adm_alloc((adm_id), (size), NULL, ((a) & ~ADM_OPT_MASK) | ADM_OPT_TOPMOST_ALLOC, __FILE__, __LINE__)
#define kal_adm_alloc_nc_topmost_dbg(adm_id, size, a, f, l) __kal_adm_alloc((adm_id), (size), NULL, ((a) & ~ADM_OPT_MASK) | ADM_OPT_TOPMOST_ALLOC, (f), (l))
#endif /* __MTK_TARGET__ && (__ARM9_MMU__ || __DYNAMIC_SWITCH_CACHEABILITY__)*/
/* DOM-NOT_FOR_SDK-END */

#endif /* !__FUE__ */


/*
 * AFM Section
 */

/*******************************************************************************
 * <GROUP Functions>
 * 
 * FUNCTION
 *  kal_afm_create
 * DESCRIPTION
 *  Create a AFM pool.
 * PARAMETERS
 *  mem_addr: [IN] starting address of the memory pool. It should be 4-byte alignment, or it will be aligned to next 4-aligned address.
 *  size: [IN] total size in byte of the memory pool.
 *  subpool_size: [IN] is an array, which describe size of each sub-pool.
 *  subpool_nr: [IN] is an array, which describe element count of each sub-pool.
 *  flags: [IN] new AFM pool extra flags, which is a combination of following values
 *         AFM_OPT_ALIGN_ALLOC - user buffers in this AFM is aligned to CPU cache line (32 bytes)
 *                               only available on platform which has a ARM MMU (ARM9+ chips).
 *         AFM_OPT_NOFALLBACK_ALLOC - when search avaiable user buffer, don't try to search subpools bigger than fittest subpool.
 *  left_mem: [OUT] get how many memory is not used by this AFM pool and can be used by app freely.
 * RETURNS
 *  AFM id. If memory is too small to create a AFM, system enters fatal error handling.
 * NOTE
 *  subpool_size must be monotonic increasing and ends with {0xffffffff, 0}. 
 *   subpool_nr must has same elements as subpool_size.
 * SEE ALSO
 *  kal_afm_delete
 ******************************************************************************/
extern KAL_AFM_ID kal_afm_create(void *mem_addr, kal_uint32 size, kal_uint32 *subpool_size,kal_uint32 *subpool_nr, kal_uint32 flags, kal_uint32 *left_mem);
/*******************************************************************************
 * <GROUP Functions>
 * 
 * FUNCTION
 *  kal_afm_delete
 * DESCRIPTION
 *  delete a AFM pool.
 * PARAMETERS
 *  afm_id: [IN] AFM pool identity.
 * RETURN VALUES
 *  KAL_SUCCESS: the operation succeeded.
 *  KAL_MEMORY_NOT_RELEASE: some user buffer is not freed yet.
 * SEE ALSO
 *  kal_afm_create
 ******************************************************************************/
extern kal_status kal_afm_delete(KAL_AFM_ID afm_id);

extern void *kal_afm_internal_alloc(KAL_AFM_ID afm_id, kal_uint32 size, kal_uint32 *actual_size, char *filename, kal_uint32 line);
/*******************************************************************************
 * <GROUP Functions>
 * 
 * FUNCTION
 *  kal_aFm_alloc
 * DESCRIPTION
 *  allocate a memory buffer from AFM.
 * PARAMETERS
 *  afm_id: [IN] AFM pool identity.
 *  size: [IN] request memory size.
 * RETURNS
 *  allocated memory buffer pointer. return NULL if no memory available.
 * NOTE 
 *  the prototype of this API is:
 *   void *kal_afm_alloc(KAL_AFM_ID afm_id, kal_uint32 size);
 * SEE ALSO
 *  kal_afm_create kal_afm_free
 ******************************************************************************/
#define kal_afm_alloc(afm_id, size) kal_afm_internal_alloc(afm_id, size, NULL, __FILE__, __LINE__)
#define kal_afm_alloc_dbg(afm_id, size, actual_size, filename, line) kal_afm_internal_alloc(afm_id, size, actual_size, filename, line)
/*******************************************************************************
 * <GROUP Functions>
 * 
 * FUNCTION
 *  kal_afm_free
 * DESCRIPTION
 *  free a memory buffer to dedicated AFM.
 * PARAMETERS
 *  afm_id: [IN] AFM pool identity.
 *  ptr: [IN] memory buffer pointer to be freed. This pointer must be allocated from afm_id.
 * RETURNS
 *  N/A
 * SEE ALSO
 *  kal_afm_create kal_afm_alloc
 ******************************************************************************/
extern void kal_afm_free(KAL_AFM_ID afm_id, void *ptr);
/*******************************************************************************
 * <GROUP Functions>
 * 
 * FUNCTION
 *  kal_afm_check_integrity
 * DESCRIPTION
 *  Check the healthy of AFM pool. 
 * PARAMETERS
 *  afm_id: [IN] AFM pool identity.
 * RETURNS
 *  return bad memory block first found. If the AFM pool is healthy, return NULL to indicate
 *  nothing wrong found.
 ******************************************************************************/
extern kal_int32 kal_afm_check_integrity(KAL_AFM_ID afm_id);

extern void *kal_afm_internal_alloc_cacheable(KAL_AFM_ID adm_id, kal_uint32 size, kal_uint32 *actual_size, char *filename, kal_uint32 line);
#if defined(__MTK_TARGET__) && (defined(__ARM9_MMU__) || defined(__DYNAMIC_SWITCH_CACHEABILITY__))
#define __kal_afm_alloc_cacheable(afm_id, size, option_notused) kal_afm_internal_alloc_cacheable(afm_id, size, NULL, __FILE__, __LINE__)
#define __kal_afm_alloc_cacheable_dbg(afm_id, size, option_notused, actual_size, filename, line) kal_afm_internal_alloc_cacheable(afm_id, size, actual_size, filename, line)
#else 
#define __kal_afm_alloc_cacheable(afm_id, size, option_notused) kal_afm_internal_alloc(afm_id, size, NULL, __FILE__, __LINE__)
#define __kal_afm_alloc_cacheable_dbg(afm_id, size, option_notused, actual_size, filename, line) kal_afm_internal_alloc(afm_id, size, actual_size, filename, line)
#endif 
/*******************************************************************************
 * <GROUP Functions>
 * 
 * FUNCTION
 *  kal_afm_alloc_cacheable
 * DESCRIPTION
 *  allocate a memory buffer which is cacheable from AFM.
 * PARAMETERS
 *  afm_id: [IN] AFM pool identity.
 *  size: [IN] request memory size.
 *  option_notused: [IN] useless, ignored.
 * RETURNS
 *  allocated memory buffer pointer. return NULL if no memory available.
 * NOTE 
 *  (1)the prototype of this API is:
 *   void *kal_afm_alloc_cacheable(KAL_ADM_ID adm_id, kal_uint32 size, kal_uint32 option_notused);
 *  (2)This AFM must be created using AFM_OPT_ALIGN_ALLOC in kal_afm_create.
 *  (3)The pointer is cacheable only on platform that has ARM MMU (such as ARM9/ARM11 platform).
 *     If not, the pointer is still noncacheable.
 * SEE ALSO
 *  kal_afm_create kal_afm_alloc_noncacheable kal_afm_free
 ******************************************************************************/
#define kal_afm_alloc_cacheable(afm_id, size, option_notused) __kal_afm_alloc_cacheable(afm_id, size, option_notused)
#define kal_afm_alloc_cacheable_dbg(afm_id, size, option_notused, actual_size, filename, line) __kal_afm_alloc_cacheable_dbg(afm_id, size, option_notused, actual_size, filename, line)

extern void *kal_afm_internal_alloc_noncacheable(KAL_AFM_ID afm_id, kal_uint32 size, kal_uint32 *actual_size, char *filename, kal_uint32 line);
#if defined(__MTK_TARGET__) && (defined(__ARM9_MMU__) || defined(__DYNAMIC_SWITCH_CACHEABILITY__))
#define __kal_afm_alloc_noncacheable(afm_id, size, option_notused) kal_afm_internal_alloc_noncacheable(afm_id, size, NULL, __FILE__, __LINE__)
#define __kal_afm_alloc_noncacheable_dbg(afm_id, size, option_notused, actual_size, filename, line) kal_afm_internal_alloc_noncacheable(afm_id, size, actual_size, filename, line)
#else 
#define __kal_afm_alloc_noncacheable(afm_id, size, option_notused) kal_afm_internal_alloc(afm_id, size, NULL, __FILE__, __LINE__)
#define __kal_afm_alloc_noncacheable_dbg(afm_id, size, option_notused, actual_size, filename, line) kal_afm_internal_alloc(afm_id, size, actual_size, filename, line)
#endif 
/*******************************************************************************
 * <GROUP Functions>
 * 
 * FUNCTION
 *  kal_adm_alloc_noncacheable
 * DESCRIPTION
 *  allocate a memory buffer which is noncacheable from AFM.
 * PARAMETERS
 *  adm_id: [IN] ADM pool identity.
 *  size: [IN] request memory size.
 *  option_notused: [IN] useless, ignored.
 * RETURNS
 *  allocated memory buffer pointer. return NULL if no memory available.
 * NOTE 
 *  (1)the prototype of this API is:
 *   void *kal_afm_alloc_noncacheable(KAL_ADM_ID adm_id, kal_uint32 size, kal_uint32 option_notused);
 *  (2)This AFM must be created using AFM_OPT_ALIGN_ALLOC in kal_afm_create.
 * SEE ALSO
 *  kal_afm_create kal_afm_alloc_cacheable kal_afm_free
 ******************************************************************************/
#define kal_afm_alloc_noncacheable(afm_id, size, option_notused) __kal_afm_alloc_noncacheable(afm_id, size, option_notused)
#define kal_afm_alloc_noncacheable_dbg(afm_id, size, option_notused, actual_size, filename, line) __kal_afm_alloc_noncacheable_dbg(afm_id, size, option_notused, actual_size, filename, line)

/* DOM-NOT_FOR_SDK-BEGIN */
extern kal_int32 kal_afm_print_log(KAL_AFM_ID afm_id);
extern kal_int32 kal_afm_get_subpool_num(KAL_AFM_ID afm_id);
extern kal_int32 kal_afm_info_get(KAL_AFM_ID afm_id, kal_uint32 *option, kal_uint32 *buff_size, kal_uint32 *buff_nr, kal_uint32 *left_buf_nr, kal_uint32 *total_left_mem_sz);
extern kal_int32 kal_afm_custom_performance_info_get(KAL_AFM_ID afm_id, kal_uint32 *option, kal_uint32 *buff_size, kal_uint32 *buff_nr, kal_uint32 *alloc_cnt, kal_uint32 *min_left_buf_nr);
extern kal_int32 kal_afm_set_cuslogging(KAL_AFM_ID afm_id, kal_uint32 cus_log_opt);
extern kal_int32 kal_afm_get_cuslogging(KAL_AFM_ID afm_id, kal_uint32 *cus_log_opt);
extern kal_int32 kal_afm_get_left_subpool_num(KAL_AFM_ID afm_id, kal_uint32 size);
/* DOM-NOT_FOR_SDK-END */

/* DOM-NOT_FOR_SDK-BEGIN */
KAL_SLMM_ID kal_create_slmm(KAL_FLMM_ID flmm_id, kal_uint32 size, kal_uint32 flags);
void *__kal_slmm_alloc(KAL_SLMM_ID slmm_id, kal_uint32 size, kal_uint32 option, kal_int32 *errcode, kal_char *filename, kal_int32 lineno);
#define kal_slmm_alloc(slmm_id, size, option, errcode) __kal_slmm_alloc((slmm_id), (size), (option), (errcode), __FILE__, __LINE__)
#define kal_slmm_alloc_dbg(slmm_id, size, option, errcode, filename, lineno) __kal_slmm_alloc((slmm_id), (size), (option), (errcode), (filename), (lineno))
void kal_slmm_free(KAL_SLMM_ID slmm_id, void *ptr);
kal_status kal_slmm_delete(KAL_SLMM_ID slmm_id);
kal_uint32 kal_slmm_vm_start(KAL_SLMM_ID slmm_id);
kal_uint32 kal_slmm_vm_end(KAL_SLMM_ID slmm_id);
/* DOM-NOT_FOR_SDK-END */

/* DOM-NOT_FOR_SDK-BEGIN */
/*******************************************************************************
 * Category 7 : System and Modem Memory Management 
 *******************************************************************************/
extern void *get_ctrl_buffer_ext(kal_uint32 size, kal_char *file_name, kal_uint32 line);
extern void free_ctrl_buffer_ext(void *buff_ptr, kal_char *file_name, kal_uint32 line);

#if defined(DEBUG_KAL) && defined(DEBUG_BUF2)
#define __get_ctrl_buffer(size) get_ctrl_buffer_ext(size, __FILE__, __LINE__)
#define __free_ctrl_buffer(ptr) free_ctrl_buffer_ext(ptr, __FILE__, __LINE__)
#else   /* DEBUG_KAL && DEBUG_BUF2 */
#define __get_ctrl_buffer(size) get_ctrl_buffer_ext(size, NULL, 0)
#define __free_ctrl_buffer(ptr) free_ctrl_buffer_ext(ptr, NULL, 0)
#endif   /* DEBUG_KAL && DEBUG_BUF2 */

/*******************************************************************************
 * <GROUP Functions>
 * 
 * FUNCTION
 *  get_ctrl_buffer
 * DESCRIPTION
 *  get a memory block at least size bytes from ctrl buffer.
 * PARAMETERS
 *  size: [IN] request memory block.
 * RETURNS
 *  memory block pointer. If all memory blocks used in corresponding ctrl buffer 
 *  used up, system enter fatal error handling.
 * NOTE
 *  (1)This API should be used in protocol stack only.
 * SEE ALSO
 *  free_ctrl_buffer
 ******************************************************************************/
#define get_ctrl_buffer(size) __get_ctrl_buffer(size)
/*******************************************************************************
 * <GROUP Functions>
 * 
 * FUNCTION
 *  free_ctrl_buffer
 * DESCRIPTION
 *  free a memory block allocated from ctrl buffer.
 * PARAMETERS
 *  ptr: [IN] memory block pointer.
 * RETURNS
 *  N/A
 * NOTE
 *  (1)This API should be used in protocol stack only.
 * SEE ALSO
 *  get_ctrl_buffer
 ******************************************************************************/
#define free_ctrl_buffer(ptr) __free_ctrl_buffer(ptr)

/* DOM-NOT_FOR_SDK-END */
/*******************************************************************************
 * Category 8 : Timer Service
 *******************************************************************************/

/*******************************************************************************
 * <GROUP Functions>
 * 
 * FUNCTION
 *  kal_get_systicks
 * DESCRIPTION
 *  return elapsed system ticks from boot up
 * PARAMETERS
 *  N/A
 * RETURNS
 *  elapsed system ticks from boot up
 * NOTE
 *  (1)Each tick is KAL_MILLISECS_PER_TICK miliseconds.
 *  (2)This API replaces kal_get_time(). And kal_get_time() is deprecated,
 *     it will be removed from next release.
 * SEE ALSO
 *  KAL_MILLISECS_PER_TICK
 ******************************************************************************/
kal_uint32 kal_get_systicks(void);
/*******************************************************************************
 * <GROUP Functions>
 * 
 * FUNCTION
 *  kal_secs_to_ticks
 * DESCRIPTION
 *  convert seconds to system ticks
 * PARAMETERS
 *  secs : [IN] seconds
 * RETURNS
 *  corresponding system ticks
 * NOTE
 *  This functions is equvilent to (secs * 1000 / KAL_MILLISECS_PER_TICK).
 *  It uses table lookup to avoid floating division to speed up.
 * SEE ALSO
 *  kal_milli_secs_to_ticks KAL_MILLISECS_PER_TICK
 ******************************************************************************/
kal_uint32 kal_secs_to_ticks(kal_uint32 secs);
/*******************************************************************************
 * <GROUP Functions>
 * 
 * FUNCTION
 *  kal_milli_secs_to_ticks
 * DESCRIPTION
 *  convert milliseconds to system ticks
 * PARAMETERS
 *  milli_secs : [IN] milliseconds
 * RETURNS
 *  corresponding system ticks
 * NOTE
 *  This functions is equvilent to (milli_secs / KAL_MILLISECS_PER_TICK).
 *  It uses table lookup to avoid floating division to speed up.
 * SEE ALSO
 *  kal_milli_secs_to_ticks KAL_MILLISECS_PER_TICK
 ******************************************************************************/
kal_uint32 kal_milli_secs_to_ticks(kal_uint32 milli_secs);
/*******************************************************************************
 * <GROUP Functions>
 * 
 * FUNCTION
 *  kal_ticks_to_secs
 * DESCRIPTION
 *  convert system ticks to seconds
 * PARAMETERS
 *  ticks : [IN] ticks count
 * RETURNS
 *  corresponding seconds
 * NOTE
 *  This functions is equvilent to (ticks * KAL_MILLISECS_PER_TICK / 1000).
 *  It uses table lookup to avoid floating multiplication to speed up.
 * SEE ALSO
 *  kal_ticks_to_milli_secs KAL_MILLISECS_PER_TICK
 ******************************************************************************/
kal_uint32 kal_ticks_to_secs(kal_uint32 ticks);
/*******************************************************************************
 * <GROUP Functions>
 * 
 * FUNCTION
 *  kal_ticks_to_milli_secs
 * DESCRIPTION
 *  convert system ticks to milliseconds
 * PARAMETERS
 *  ticks : [IN] ticks count
 * RETURNS
 *  corresponding milliseconds
 * NOTE
 *  This functions is equvilent to (ticks * KAL_MILLISECS_PER_TICK).
 *  It uses table lookup to avoid floating multiplication to speed up.
 * SEE ALSO
 *  kal_ticks_to_secs KAL_MILLISECS_PER_TICK
 ******************************************************************************/
kal_uint32 kal_ticks_to_milli_secs(kal_uint32 ticks);

static __inline void kal_get_time(kal_uint32* ticks_ptr)
{
    *ticks_ptr = kal_get_systicks();
}

/*******************************************************************************
 * Category 8 : Timer Service
 *******************************************************************************/
/*************************************************************************
 * Exported Function Prototypes for KAL timer
 *************************************************************************/
 
/* DOM-NOT_FOR_SDK-BEGIN */
 /*******************************************************************************
 * <GROUP Functions>
 * 
 * FUNCTION
 *  kal_create_timer
 * DESCRIPTION
 *  create a kal timer.
 * PARAMETERS
 *  timer_name : [IN] timer name. It's used for debug only, and only the 
 *                first 8 characters retain.
 * RETURNS
 *  Success:        timer pointer.
 *  Error:          system enter fatal error handling.
 * NOTE
 *  (1)It is strongly suggested to call this function only at system initialization stage, 
 *     and the related data allocated for the timer could not be freed once it¡¯s created.
 *  (2)kal timer API is not thread safe and it's strongly suggested to use it
 *     in single task/HISR only.
 * SEE ALSO
 *  kal_set_timer kal_cancel_timer kal_get_remaining_time
 ******************************************************************************/
kal_timerid kal_create_timer(kal_char* timer_name);
/*******************************************************************************
 * <GROUP Functions>
 * 
 * FUNCTION
 *  kal_set_timer
 * DESCRIPTION
 *  start a kal timer.
 * PARAMETERS
 *  timer_ptr : [IN] timer pointer.
 *  handler_func_ptr: [IN] pointer of timeout handler, it will be called after 
 *                    timeout happens
 *  handler_param_ptr: [IN] user supply argument for handler_func_ptr
 *  delay: [IN] timeout duration in terms of time ticks, the maximum value is 0xFFFFFFFF
 *  reschedule_time: [IN] reset delay to reschedule_time after timeout happens.
 *                   If it's zero, no reschedule happens and the timer is one-shot.
 *                   Otherwise, it's a periodic timer.
 * RETURNS
 *  N/A
 * NOTE
 *  kal_set_timer will cancel previous set timeout event on this timer, and 
 *  start a new one.
 * SEE ALSO
 *  kal_create_timer kal_cancel_timer kal_get_remaining_time
 ******************************************************************************/
void kal_set_timer(kal_timerid timer_ptr, 
                   kal_timer_func_ptr handler_func_ptr, void* handler_param_ptr,
                   kal_uint32 delay, kal_uint32 reschedule_time);
/*******************************************************************************
 * <GROUP Functions>
 * 
 * FUNCTION
 *  kal_cancel_timer
 * DESCRIPTION
 *  cancel a kal timer.
 * PARAMETERS
 *  timer_ptr : [IN] timer pointer.
 * RETURNS
 *  N/A
 * NOTE
 *  Please note that canceling timer inside HISR may cancel a timer which is 
 *  in expired stage but the callback function not yet been serviced.
 * SEE ALSO
 *  kal_create_timer kal_set_timer kal_get_remaining_time
 ******************************************************************************/
void kal_cancel_timer(kal_timerid timer_ptr);
/*******************************************************************************
 * <GROUP Functions>
 * 
 * FUNCTION
 *  kal_get_remaining_time
 * DESCRIPTION
 *  retrieve the remaining timeout period in units of time ticks
 * PARAMETERS
 *  timer_ptr : [IN] timer pointer.
 * RETURNS
 *  N/A
 * NOTE
 *  This API replaces kal_get_time_remaining(). And kal_get_time_remaining() is deprecated,
 *  it will be removed from next release. 
 * SEE ALSO
 *  kal_create_timer kal_set_timer kal_cancel_timer 
 ******************************************************************************/
kal_uint32 kal_get_remaining_time(kal_timerid timer_ptr);

static __inline kal_uint32 kal_get_time_remaining(kal_timerid timer_ptr)
{
    return kal_get_remaining_time(timer_ptr);
}

/* DOM-NOT_FOR_SDK-END */

/* DOM-NOT_FOR_SDK-BEGIN */
/*************************************************************************
 * Exported Function Prototypes for stack timer
 *************************************************************************/
 
/*******************************************************************************
 * <GROUP Functions>
 * 
 * FUNCTION
 *  stack_init_timer
 * DESCRIPTION
 *  initialize a stack timer, user is response for provide stack timer control 
 *  block memory.
 * PARAMETERS
 *  stack_timer: [OUT] stack timer control block provided by the users
 *  timer_name: [IN] stack timer name. it's used for debug only, and only the 
 *              first 8 characters retain.
 *  module_id: [IN] module ID of the owner, to which an expiry ILM will be delivered.
 * RETURNS
 *  If stack_init_timer fails, system enter fatal error handling.
 * NOTE
 *  (1)It is strongly suggested to call this function only at system initialization stage, 
 *     and the related data allocated for the stack timer could not be freed once it¡¯s created.
 *  (2)This API is deprecated and will be removed from next release. Please use stack_create_timer instead.
 * SEE ALSO
 *  stack_create_timer stack_start_timer stack_stop_timer stack_is_time_out_valid stack_process_time_out
 ******************************************************************************/
extern void stack_init_timer(stack_timer_struct *stack_timer, 
                            kal_char *timer_name, 
                            module_type module_id);
/*******************************************************************************
 * <GROUP Functions>
 * 
 * FUNCTION
 *  stack_create_timer
 * DESCRIPTION
 *  create a stack timer.
 * PARAMETERS
 *  timer_name: [IN] stack timer name. it's used for debug only, and only the 
 *              first 8 characters retain.
 *  module_id: [IN] module ID of the owner, to which an expiry ILM will be delivered.
 * RETURNS
 *  If stack_create_timer fails, system enter fatal error handling.
 * NOTE
 *  (1)It is strongly suggested to call this function only at system initialization stage, 
 *     and the related data allocated for the stack timer could not be freed once it¡¯s created.
 *  (2)This API replaces stack_init_timer(). And stack_init_timer() is deprecated,
 *     it will be removed from next release.
 *  (3)The inaccuracy of stack timer comes from
 *        schedule delay - the time from STACK_TIMER_EXPIRED sent to the task runs, more high priority
 *                         tasks preempt target task, more longer.
 *        Task handling message delay - the time until the STACK_TIMER_EXPIRED ILM handled, more ILM messages before it, more longer.
 * SEE ALSO
 *  stack_init_timer stack_start_timer stack_stop_timer stack_is_time_out_valid stack_process_time_out
 ******************************************************************************/
extern stack_timer_struct *stack_create_timer(kal_char *timer_name, module_type module_id);
#define HAS_STACK_CREATE_TIMER 1
/*******************************************************************************
 * <GROUP Functions>
 * 
 * FUNCTION
 *  stack_start_timer
 * DESCRIPTION
 *  start a stack timer. After the timer is expired, system send a STACK_TIMER_EXPIRED ilm message
 *  to task of module specified by module_id in stack_create_timer.
 * PARAMETERS
 *  stack_timer : [IN] stack timer pointer.
 *  timer_indx: [IN] timer index, this index can be retrieved using stack_timer_get_index
 *  init_time: [IN] timeout duration in terms of time ticks, the maximum value is 0xFFFFFFFF
 * RETURNS
 *  N/A
 * NOTE
 *  (1)stack_start_timer will cancel previous set timeout event on this timer, and 
 *     start a new one.
 *  (2)stack timer is a one-shot event, it will automatically stop after first expire.
 *  (3)It's highly recommanded to ensure the stack timer is stopped before start a new one.
 *     Please call stack_stop_timer() to do that. 
 * SEE ALSO
 *  stack_create_timer stack_stop_timer stack_is_time_out_valid stack_process_time_out
 ******************************************************************************/
extern void stack_start_timer(stack_timer_struct *stack_timer, 
                              kal_uint16 timer_indx, 
                              kal_uint32 init_time);
/*******************************************************************************
 * <GROUP Functions>
 * 
 * FUNCTION
 *  stack_stop_timer
 * DESCRIPTION
 *  stop a stack timer.
 * PARAMETERS
 *  stack_timer : [IN] stack timer pointer.
 * RETURNS
 *  stack timer status before it's stopped.
 * NOTE
 *  Please note that canceling timer inside HISR may cancel a timer which is 
 *  in expired stage but the callback function not yet been serviced.
 * SEE ALSO
 *  stack_create_timer stack_start_timer stack_is_time_out_valid stack_process_time_out
 ******************************************************************************/
extern stack_timer_status_type stack_stop_timer(stack_timer_struct *stack_timer);
/*******************************************************************************
 * <GROUP Functions>
 * 
 * FUNCTION
 *  stack_timer_status
 * DESCRIPTION
 *  query current status of the stack timer. And if timer_remaing is not NULL, 
 *  return remaining time before this stack timer in it. 
 * PARAMETERS
 *  stack_timer : [IN] stack timer pointer.
 *  time_remaining: [OUT] return return remaining time before this stack timer in it.
 * RETURNS
 *  Current status of the stack timer.
 * SEE ALSO
 *  stack_create_timer stack_start_timer stack_stop_timer
 ******************************************************************************/
extern stack_timer_status_type stack_timer_status(stack_timer_struct *stack_timer, 
                                                    kal_uint32 *time_remaining);
 /*******************************************************************************
 * <GROUP Functions>
 * 
 * FUNCTION
 *  stack_timer_get_remaining_time
 * DESCRIPTION
 *  retrieve the remaining timeout period in units of time ticks
 * PARAMETERS
 *  stack_timer : [IN] stack timer pointer.
 * RETURNS
 *  remaining timeout period in units of time ticks
 * SEE ALSO
 *  stack_create_timer stack_start_timer stack_stop_timer
 ******************************************************************************/
static __inline kal_uint32 stack_timer_get_remaining_time(stack_timer_struct *stack_timer)
{
    kal_uint32 remaining = 0;
    
    stack_timer_status(stack_timer, &remaining);
    
    return remaining;
}
/*******************************************************************************
 * <GROUP Functions>
 * 
 * FUNCTION
 *  stack_is_time_out_valid
 * DESCRIPTION
 *  query whether current STACK_TIMER_EXPIRED ilm message is cancelled(valid). A cancelled
 *  STACK_TIMER_EXPIRED ilm message should not be processed.
 * PARAMETERS
 *  stack_timer : [IN] stack timer pointer.
 * RETURN VALUES:
 *  KAL_TRUE: this STACK_TIMER_EXPIRED ilm message is valid and not cancelled yet, it should be handled.
 *  KAL_FALSE: this STACK_TIMER_EXPIRED ilm message is cancelled, it should not be handled.
 * NOTE
 *  (1)Before every STACK_TIMER_EXPIRED ilm message can be handled, this API must be called
 *     to identify whether it's cancelled.
 * SEE ALSO
 *  stack_create_timer stack_start_timer stack_stop_timer stack_process_time_out
 ******************************************************************************/
extern kal_bool stack_is_time_out_valid(stack_timer_struct *stack_timer);
/*******************************************************************************
 * <GROUP Functions>
 * 
 * FUNCTION
 *  stack_process_time_out
 * DESCRIPTION
 *  Claim a STACK_TIMER_EXPIRED ILM message is processed. 
 * PARAMETERS
 *  stack_timer : [IN] stack timer pointer.
 * RETURNS
 *  N/A
 * NOTE
 *  (1)After every valid STACK_TIMER_EXPIRED ILM message is handled, this API must 
 *     be called to correctly maintain the control element in stack timer.
 * SEE ALSO
 *  stack_create_timer stack_start_timer stack_stop_timer stack_is_time_out_valid
 ******************************************************************************/
extern void stack_process_time_out(stack_timer_struct *stack_timer);

extern kal_uint8 stack_time_out_messages_in_queue(stack_timer_struct *stack_timer);

extern stack_timer_user_view_status_type 
stack_timer_user_view_status(stack_timer_struct *stack_timer, 
                              kal_uint32 *time_remaining);
/* DOM-NOT_FOR_SDK-END */

/*************************************************************************
 * Exported Function Prototypes for event scheduler
 *************************************************************************/
 
/*******************************************************************************
 * <GROUP CallBackFunctions>
 *  
 * FUNCTION
 *  malloc_fp_t
 * DESCRIPTION
 *  pointer to a function that can allocation memory like malloc.
 * PARAMETERS
 *  size: [IN] request memory size in byte.
 * RETURNS
 *  Successful: memory pointer.
 *  Fail:       NULL.
 ******************************************************************************/
typedef void * (*malloc_fp_t)(unsigned int size);
/*******************************************************************************
 * <GROUP CallBackFunctions>
 *  
 * FUNCTION
 *  realloc_fp_t
 * DESCRIPTION
 *  pointer to a function that can re-allocation memory like realloc.
 * PARAMETERS
 *  ptr: [IN] old pointer.
 *  new_size: [IN] new request memory size in byte.
 * RETURNS
 *  Successful: memory pointer.
 *  Fail:       NULL.
 ******************************************************************************/
typedef void * (*realloc_fp_t)(void *ptr, unsigned int new_size);
/*******************************************************************************
 * <GROUP CallBackFunctions>
 *  
 * FUNCTION
 *  free_fp_t
 * DESCRIPTION
 *  pointer to a function that can free memory like free.
 * PARAMETERS
 *  ptr: [IN] memory pointer to be free.
 * RETURNS
 *  N/A
 ******************************************************************************/
typedef void (*free_fp_t)(void *ptr);

/*extern event_scheduler *new_evshed(void *timer_id,
                                   void (*start_timer)(void *, unsigned int),
                                   void (*stop_timer)(void *),
                                   kal_uint32 fuzz,
                                   malloc_fp_t alloc_fn_p,
                                   free_fp_t free_fn_p,
                                   kal_uint8 max_delay_ticks);
 */                                  
/*******************************************************************************
 * <GROUP Functions>
 *  
 * FUNCTION
 *  evshed_create
 * DESCRIPTION
 *  create a new event scheduler.
 * PARAMETERS
 *  evshed_name: [IN] event scheduler name. it's used for debug only, and only the
 *                    first 8 characters retain.
 *  module_id: [IN] the module this event scheduler belongs to. When event scheduler
 *              timeout happens, system sent timeout message to task of this module.
 *  allowed_time_shift: [IN] 
 *      Timer inaccuracy in ticks that the event scheduler allow, regardless of sleep mode. 
 *      If set, the event may delay or prematurely timeout for maximum allowed_time_shift ticks.
 *      If don't like it, just set it to 0.
 *
 *      Advantage -
 *      Set this argument will save you a lot of CPU resource if the events are frequent and close to each other. 
 *   
 *  allowed_time_shift_sleep_mode: [IN]
 *      Behaviour -
 *      Time inaccuracy in ticks the event scheduler allow, due to target enters sleep mode. 
 *      The event may delay for max allowed_time_shift_sleep_mode tick if the target enters sleep mode, 
 *      so that the target can sleep longer. The event will suffer no delay if the target doesn't enter sleep mode.
 *      
 *      Advantage -
 *      Set this argument will save the target a lot of power. You are encouraged to set this value for the sake of power.
 *
 *      Note -
 *      0<=allowed_time_error_shift_mode<=255
 *
 * RETURNS
 *  Successful: created event scheduler pointer.
 *  Fail: system enter fatal error handling.
 * NOTE
 *  (1)This API replaces new_evshed(). And new_evshed() is deprecated,
 *     it will be removed from next release. 
 *  (2)It is strongly suggested to call this function only at system initialization stage.
 *     and This is no way to free the event schedule data structure once allocated.
 *  (3)Event scheduler is not thread safe, if you want to share it in multi-task/hisr,
 *     please use lock to protect it.
 *  (4)To reduce power consumption, it's strongly suggested to set allowed_time_error_sleep_mode.
 *  (5)The inaccuracy of event scheduler comes from 
 *       schedule delay -  Time lisr, Time HISR, the owner task may be preempted.
 *       Task handling message delay - There may be many ILMs to handle before the event expired ILM gets handled
 *       allowed_time_shift
 *       allowed_time_shift_sleep_mode
 * SEE ALSO
 *  evshed_set_event evshed_cancel_event evshed_timer_handler
 ******************************************************************************/
extern event_scheduler *evshed_create(kal_char *evshed_name, module_type module_id,
                                      kal_uint32 allowed_time_shift, kal_uint16 allowed_time_shift_sleep_mode);
#define HAS_EVSHED_CREATE 1

 /*******************************************************************************
 * <GROUP Functions>
 *  
 * FUNCTION
 *      evshed_set_index
 * DESCRIPTION
 *      Set timer_index to distinguish many event schedulers.
 *      The default timer index is 0.
 * PARAMETERS
 *      es_usr: [IN] event scheduler id returned by evshed_create
 *      timer_index: [IN] timer index given by user
 * RETURNS
 *      None
 ******************************************************************************/
extern void evshed_set_index(event_scheduler *es_usr, kal_uint16 timer_index);
#define HAS_EVSHED_SET_INDEX 1

/*******************************************************************************
 * <GROUP Functions>
 *  
 * FUNCTION
 *      evshed_get_index
 * DESCRIPTION
 *      Get the index set by evshed_set_index
 * PARAMETERS
 *      expired_ilm: [IN] The ilm sent by expired event and got from task queue 
 * RETURNS
 *      Sucessful: The timer index of kal_uint32
 *      Fail: Fatal error if wrong ilm input
 ******************************************************************************/
extern kal_uint16 evshed_get_index(ilm_struct *expired_ilm);
#define HAS_EVSHED_GET_INDEX 1
/*******************************************************************************
 * <GROUP Functions>
 *  
 * FUNCTION
 *  evshed_set_event
 * DESCRIPTION
 *  set an event on event scheduler.
 * PARAMETERS
 *  es: [IN] event scheduler pointer.
 *  event_hf: [IN] the callback function when event timeout.
 *  event_hf_param: [IN] the user supplied argument for event_hf.
 *  elapse_time: [IN] how many system ticks elapses before this event timeout.
 * RETURNS
 *  Successful: return set eventid.
 *  Fail: return NULL.
 * NOTE
 *  System will allocate memory for event id, it will be freed automatically when
 *      a. event is cancelled
 *      b. event expired
 *   It's not allowed to use the event id after above cases.
 *   If the event id is saved, please be careful to reset it.
 * SEE ALSO
 *  evshed_create evshed_cancel_event evshed_timer_handler
 ******************************************************************************/
extern eventid evshed_set_event(event_scheduler *es,
                                kal_timer_func_ptr event_hf,
                                void *event_hf_param,
                                kal_uint32 elapse_time);
/*******************************************************************************
 * <GROUP Functions>
 *  
 * FUNCTION
 *  evshed_set_event_userid
 * DESCRIPTION
 *  set an event on event scheduler, and give this event an user id
 * PARAMETERS
 *  es: [IN] event scheduler pointer.
 *  event_hf: [IN] the callback function when event timeout.
 *  event_hf_param: [IN] the user supplied argument for event_hf.
 *  elapse_time: [IN] how many system ticks elapses before this event timeout.
 *  event_user_id: [IN] User can input an id to distinct the events.
 * RETURNS
 *  Successful: return set eventid.
 *  Fail: return NULL.
 * NOTE
 *  System will allocate memory for event id, it will be freed automatically when
 *      a. event is cancelled
 *      b. event expired
 *   It's not allowed to use the event id after above cases.
 *   If the event id is saved, please be careful to reset it.
 * SEE ALSO
 *  evshed_create evshed_cancel_event evshed_timer_handler
 ******************************************************************************/
extern eventid evshed_set_event_userid(event_scheduler *es,
                                kal_timer_func_ptr event_hf,
                                void *event_hf_param,
                                kal_uint32 elapse_time,
                                kal_uint32 event_user_id);
#define HAS_EVSHED_SET_EVENT_USERID 1 

/*******************************************************************************
 * <GROUP Functions>
 *  
 * FUNCTION
 *  evshed_get_event_userid
 * DESCRIPTION
 * Get the user id, inputed in evshed_set_event_userid
 * PARAMETERS
 *  user_eventid: [IN] eventid returned by evshed_set_event_userid
 * RETURNS
 *  Successful: returns kal_uint32 
 *  Fail: If user_eventid is NULL, fatal error
 * SEE ALSO
 *  evshed_create evshed_cancel_event evshed_timer_handler
 ******************************************************************************/
extern kal_uint32 evshed_get_event_userid(eventid user_eventid);
#define HAS_EVSHED_GET_EVENT_USERID 1 
/*******************************************************************************
 * <GROUP Functions>
 *  
 * FUNCTION
 *  evshed_cancel_event
 * DESCRIPTION
 *  cancel a event on event scheduler.
 * PARAMETERS
 *  es: [IN] event scheduler pointer.
 *  eid: [IN,OUT] pointer to event id.
 * RETURNS
 *  Successful: remain ticks before this event expires.
 *  Fail: -1.
 * NOTE
 *  (1)system would reset the event ID (eid) to NULL before returning; however, 
 *     caller should pay attention to its privately saved event id
 * SEE ALSO
 *  evshed_create evshed_set_event evshed_timer_handler evshed_delete_all_events
 ******************************************************************************/
extern kal_int32 evshed_cancel_event(event_scheduler *es, eventid *eid);
/*******************************************************************************
 * <GROUP Functions>
 *  
 * FUNCTION
 *  evshed_get_remaining_time
 * DESCRIPTION
 *  get remain ticks before this event expires.
 * PARAMETERS
 *  es: [IN] event scheduler pointer.
 *  eid: [IN] event id.
 * RETURNS
 *  Successful: remain ticks before this event expires.
 ******************************************************************************/
extern kal_uint32 evshed_get_remaining_time(event_scheduler *es, eventid eid);

static __inline kal_uint32 evshed_get_rem_time(event_scheduler *es, eventid eid)
{
    return evshed_get_remaining_time(es, eid);
}

/*******************************************************************************
 * <GROUP Functions>
 *  
 * FUNCTION
 *  evshed_delete_all_events
 * DESCRIPTION
 *  delete and cancel all events on event scheduler.
 * PARAMETERS
 *  es: [IN] event scheduler pointer.
 * RETURNS
 *  N/A
 * SEE ALSO
 *  evshed_create evshed_set_event evshed_timer_handler evshed_delete_all_events
 *****************************************************************************/
extern void evshed_delete_all_events(event_scheduler *es);
/*******************************************************************************
 * <GROUP Functions>
 *  
 * FUNCTION
 *  evshed_timer_handler
 * DESCRIPTION
 *  event scheduler main function. It will check all set events status, and call
 *  the callback function if they are expired.
 *
 * PARAMETERS
 *  es: [IN] event scheduler pointer.
 * RETURNS
 *  N/A
 * NOTE
 *  (1)it's not thread-safe. Don't use in parallel.
 *  (2)If an event is expired, the eventid is freed automatically and don't use it any more.
 *     In evshed_timer_handler(), system would execute event registered timeout callback function.
 *     Caller should reset saved event id in registered timeout callback function,
 *     or cause potential bug to cancel wrong timer event.
 * SEE ALSO
 *  evshed_create evshed_set_event evshed_cancel_event evshed_delete_all_events
 *****************************************************************************/
extern void evshed_timer_handler(event_scheduler *es);
/*******************************************************************************
 * <GROUP Functions>
 *  
 * FUNCTION
 *  evshed_has_pending_events
 * DESCRIPTION
 *  query there is any event pending (expired but not handled yet).
 * PARAMETERS
 *  es: [IN] event scheduler pointer.
 * RETURNS
 *  return KAL_TRUE or KAL_FALSE.
 * NOTE
 *  This API replaces evshed_events_pending(). And evshed_events_pending() is deprecated,
 *  it will be removed from next release.
 *****************************************************************************/
extern kal_bool evshed_has_pending_events(event_scheduler *es);

static __inline kal_bool evshed_events_pending(event_scheduler *es)
{
    return evshed_has_pending_events(es);
}

/*******************************************************************************
 * <GROUP Functions>
 *  
 * FUNCTION
 *  evshed_suspend_all_events
 * DESCRIPTION
 *  suspend all of the events set on an event scheduler.
 * PARAMETERS
 *  es: [IN] event scheduler pointer.
 * RETURNS
 *  N/A
 * SEE ALSO
 *  evshed_resume_all_events
 *****************************************************************************/
extern void evshed_suspend_all_events(event_scheduler *es);
/*******************************************************************************
 * <GROUP Functions>
 *  
 * FUNCTION
 *  evshed_resume_all_events
 * DESCRIPTION
 *  resume all of the events set on an event scheduler.
 * PARAMETERS
 *  es: [IN] event scheduler pointer.
 * RETURNS
 *  N/A
 * SEE ALSO
 *  evshed_suspend_all_events
 *****************************************************************************/
extern void evshed_resume_all_events(event_scheduler *es);


/* DOM-NOT_FOR_SDK-BEGIN */
/*******************************************************************************
 * Category 9 : Error Handling
 *******************************************************************************/
 

/*
 * These functions has been duplicated to several files - .\bootloader\src\bl_portingfunc.c
 *                                                        .\fota\src\fue_env_api.c
 *                                                        .\plutommi\VendorApp\APPSDK\APPSDKInc\APPSDKSystemServiceGProt.h
 * If there's any modification to these functions, please remember to update above file.
 * If you need to duplicate these functions, please update above file list.
 */
#if defined(__UBL__)

#if defined(__KAL_ASSERT_LEVEL0__)
extern void kal_assert_fail();
#else /* __KAL_ASSERT_LEVEL0__ */
extern void kal_assert_fail(char* expr, char* file, kal_uint32 line, kal_bool isext, kal_uint32 ex1, kal_uint32 ex2, kal_uint32 ex3, ASSERT_DUMP_PARAM_T *dump_param);
#endif /* __KAL_ASSERT_LEVEL0__ */

#else /* __UBL__ */

#if defined(__MTK_TARGET__)

#if defined(__KAL_ASSERT_LEVEL0__)
extern void kal_assert_fail(void);
#else /* __KAL_ASSERT_LEVEL0__ */

#if defined(__KAL_ASSERT_LEVEL4__)
extern void INT_ExceptionSaveCpuReg(void);
#endif /* __KAL_ASSERT_LEVEL4__ */

extern void kal_assert_fail(char* expr, char* file, kal_uint32 line, kal_bool isext, kal_uint32 ex1, kal_uint32 ex2, kal_uint32 ex3, ASSERT_DUMP_PARAM_T *dump_param);
extern void kal_assert_fail_internal(kal_char* expr, kal_char *file, kal_uint32 line, kal_uint32 caller_addr, kal_bool isext, kal_uint32 ex1, kal_uint32 ex2, kal_uint32 ex3, ASSERT_DUMP_PARAM_T *dump_param);

#if defined(__KAL_ASSERT_SHRINKING__)
extern void kal_assert_fail_line(kal_uint32 line);
extern void kal_assert_fail_ext(kal_uint32 line, kal_uint32 ex1, kal_uint32 ex2, kal_uint32 ex3);
extern void kal_assert_fail_dump(kal_uint32 line, kal_uint32 ex1, kal_uint32 ex2, kal_uint32 ex3, ASSERT_DUMP_PARAM_T *dump_param);
extern void kal_assert_fail_specific(kal_uint32 line, assert_specific_key key, ...);
extern void kal_assert_fail_specific_ext(kal_uint32 line, kal_uint32 ex1, kal_uint32 ex2, kal_uint32 ex3, assert_specific_key key, ...);
#else
extern void kal_assert_fail_line(char* expr, char* file, kal_uint32 line);
extern void kal_assert_fail_ext(char* expr, char* file, kal_uint32 line, kal_uint32 ex1, kal_uint32 ex2, kal_uint32 ex3);
extern void kal_assert_fail_dump(char* expr, char* file, kal_uint32 line, kal_uint32 ex1, kal_uint32 ex2, kal_uint32 ex3, ASSERT_DUMP_PARAM_T *dump_param);
extern void kal_assert_fail_specific(char* expr, char* file, kal_uint32 line, assert_specific_key key, ...);
extern void kal_assert_fail_specific_ext(char* expr, char* file, kal_uint32 line, kal_uint32 ex1, kal_uint32 ex2, kal_uint32 ex3, assert_specific_key key, ...);
#endif /* __KAL_ASSERT_SHRINKING__ */

#endif /* __KAL_ASSERT_LEVEL0__ */

#else /* __MTK_TARGET__ */

extern void kal_assert_fail(char* expr, char* file, kal_uint32 line, kal_bool isext, kal_uint32 ex1, kal_uint32 ex2, kal_uint32 ex3, ASSERT_DUMP_PARAM_T *dump_param);
extern void kal_assert_fail_internal(kal_char* expr, kal_char *file, kal_uint32 line, kal_uint32 caller_addr, kal_bool isext, kal_uint32 ex1, kal_uint32 ex2, kal_uint32 ex3, ASSERT_DUMP_PARAM_T *dump_param);

#endif /* __MTK_TARGET__  */

#endif /* __UBL__ */

/*
 * Error Handling
 */
#if defined(__MTK_TARGET__) && !defined(__UBL__)

#if defined(__KAL_ASSERT_LEVEL4__)
#define DEBUG_ASSERT(expr) do {if(!(expr)) {} } while (0)
#define ASSERT(expr) do {if(!(expr)) { \
                             INT_ExceptionSaveCpuReg(); \
                             kal_assert_fail_line((kal_char *)#expr, (kal_char *)__FILE__, __LINE__); \
                         } \
                     } while (0)
#define EXT_ASSERT(expr, e1, e2, e3) do {if(!(expr)) { \
                                             INT_ExceptionSaveCpuReg(); \
                                             kal_assert_fail_ext((kal_char *)#expr, (kal_char *)__FILE__, __LINE__, (e1), (e2), (e3)); \
                                         } \
                                     } while (0)
#define EXT_ASSERT_DUMP(expr, e1, e2, e3, param) do {if(!(expr)) { \
                                                         INT_ExceptionSaveCpuReg(); \
                                                         kal_assert_fail_dump((kal_char *)#expr, (kal_char *)__FILE__, __LINE__, (e1), (e2), (e3), (param)); \
                                                     } \
                                                 } while (0)
#define ASSERT_SPECIFIC(expr, key, ...) do {if(!(expr)) { \
                                                INT_ExceptionSaveCpuReg(); \
                                                kal_assert_fail_specific((kal_char *)#expr, (kal_char *)__FILE__, __LINE__, key, __VA_ARGS__); \
                                            } \
                                        } while (0)
#define EXT_ASSERT_SPECIFIC(expr, e1, e2, e3, key, ...) do {if(!(expr)) { \
                                                                INT_ExceptionSaveCpuReg(); \
                                                                kal_assert_fail_specific_ext((kal_char *)#expr, (kal_char *)__FILE__, __LINE__, e1, e2, e3, key, __VA_ARGS__); \
                                                            } \
                                                        } while (0)
#elif defined(__KAL_ASSERT_LEVEL3__)
#define DEBUG_ASSERT(expr) do {if(!(expr)) {} } while (0)
#define ASSERT(expr) do {if(!(expr)) kal_assert_fail_line((kal_char *)#expr, (kal_char *)__FILE__, __LINE__); } while (0)
#define EXT_ASSERT(expr, e1, e2, e3) do {if(!(expr)) kal_assert_fail_ext((kal_char *)#expr, (kal_char *)__FILE__, __LINE__, (e1), (e2), (e3)); } while (0)
#define EXT_ASSERT_DUMP(expr, e1, e2, e3, param) do {if(!(expr)) kal_assert_fail_dump((kal_char *)#expr, (kal_char *)__FILE__, __LINE__, (e1), (e2), (e3), (param)); } while (0)
#define ASSERT_SPECIFIC(expr, key, ...) do {if(!(expr)) kal_assert_fail_specific((kal_char *)#expr, (kal_char *)__FILE__, __LINE__, key, __VA_ARGS__); } while (0)
#define EXT_ASSERT_SPECIFIC(expr, e1, e2, e3, key, ...) do {if(!(expr)) kal_assert_fail_specific_ext((kal_char *)#expr, (kal_char *)__FILE__, __LINE__, e1, e2, e3, key, __VA_ARGS__); } while (0)
#elif defined(__KAL_ASSERT_LEVEL2__) && defined(__KAL_ASSERT_SHRINKING__)
#define DEBUG_ASSERT(expr) do {if(!(expr)) {} } while (0)
#define ASSERT(expr) do {if(!(expr)) kal_assert_fail_line(__LINE__); } while (0)
#define EXT_ASSERT(expr, e1, e2, e3) do {if(!(expr)) kal_assert_fail_ext(__LINE__, (e1), (e2), (e3)); } while (0)
#define EXT_ASSERT_DUMP(expr, e1, e2, e3, param) do {if(!(expr)) kal_assert_fail_dump(__LINE__, (e1), (e2), (e3), (param)); } while (0)
#define ASSERT_SPECIFIC(expr, key, ...) do {if(!(expr)) kal_assert_fail_specific(__LINE__, key, __VA_ARGS__); } while (0)
#define EXT_ASSERT_SPECIFIC(expr, e1, e2, e3, key, ...) do {if(!(expr)) kal_assert_fail_specific_ext(__LINE__, e1, e2, e3, key, __VA_ARGS__); } while (0)
#elif defined(__KAL_ASSERT_LEVEL1__) && defined(__KAL_ASSERT_SHRINKING__)
#define DEBUG_ASSERT(expr) do {if(!(expr)) {} } while (0)
#define ASSERT(expr) do {if(!(expr)) kal_assert_fail_line(__LINE__); } while (0)
#define EXT_ASSERT(expr, e1, e2, e3) do {if(!(expr)) kal_assert_fail_line(__LINE__); } while (0)
#define EXT_ASSERT_DUMP(expr, e1, e2, e3, param) do {if(!(expr)) kal_assert_fail_line(__LINE__); } while (0)
#define ASSERT_SPECIFIC(expr, key, ...) do {if(!(expr)) kal_assert_fail_line(__LINE__); } while (0)
#define EXT_ASSERT_SPECIFIC(expr, e1, e2, e3, key, ...) do {if(!(expr)) kal_assert_fail_line(__LINE__); } while (0)
#else
#define DEBUG_ASSERT(expr) do {if(!(expr)) {} } while (0)
#define ASSERT(expr) do {if(!(expr)) {} } while (0)
#define EXT_ASSERT(expr, e1, e2, e3) do {if(!(expr)) kal_assert_fail(); } while (0)
#define EXT_ASSERT_DUMP(expr, e1, e2, e3, param) do {if(!(expr)) kal_assert_fail(); } while (0)
#define ASSERT_SPECIFIC(expr, key, ...) do {if(!(expr)) kal_assert_fail(); } while (0)
#define EXT_ASSERT_SPECIFIC(expr, e1, e2, e3, key, ...) do {if(!(expr)) kal_assert_fail(); } while (0)
#endif

#else  /* __MTK_TARGET__ && !__UBL__ */

/* align to the highest assert level in MoDIS, no shrinking requirement */
#define DEBUG_ASSERT(expr) do {if(!(expr)) {} } while (0)
#define ASSERT(expr) do {if(!(expr)) kal_assert_fail((kal_char *)#expr, (kal_char *)__FILE__, __LINE__, KAL_FALSE, 0, 0, 0, NULL); } while (0)
#define EXT_ASSERT(expr, e1, e2, e3) do {if(!(expr)) kal_assert_fail((kal_char *)#expr, (kal_char *)__FILE__, __LINE__, KAL_TRUE, (e1), (e2), (e3), NULL); } while (0)
#define EXT_ASSERT_DUMP(expr, e1, e2, e3, param) do {if(!(expr)) kal_assert_fail((kal_char *)#expr, (kal_char *)__FILE__, __LINE__, KAL_TRUE, (e1), (e2), (e3), (param)); } while (0)
#if defined(_MSC_VER) && (_MSC_VER >= 1400)
/* VC6=1200, VS2003=1300, VS2005=1400, VS2008/2010=1500 */
/* variable number of arguments is supported since VS2005  */
#define ASSERT_SPECIFIC(expr, key, ...) do {if(!(expr)) kal_assert_fail((kal_char *)#expr, (kal_char *)__FILE__, __LINE__, KAL_FALSE, 0, 0, 0, NULL); } while (0)
#define EXT_ASSERT_SPECIFIC(expr, e1, e2, e3, key, ...) do {if(!(expr)) do {if(!(expr)) kal_assert_fail((kal_char *)#expr, (kal_char *)__FILE__, __LINE__, KAL_TRUE, (e1), (e2), (e3), NULL); } while (0)
#endif /* _MSC_VER >= 1400 */

#endif /* __MTK_TARGET__ && !__UBL__ */

/* DOM-NOT_FOR_SDK-END */

#ifdef __BUILD_DOM__
/*******************************************************************************
 * <GROUP Functions>
 *  
 * FUNCTION
 *  ASSERT
 * DESCRIPTION
 *  assert the condition is true, otherwise enter system exception handling.
 * PARAMETERS
 *  expr: [IN] the condition to be evaluated
 * RETURNS
 *  N/A
 * NOTE
 *  (1)The assertion only takes effect when DEBUG_KAL is defined.
 *  (2)No matter DEBUG_KAL is defined or not, the expr will be evaluated.
 * SEE ALSO
 *   EXT_ASSERT EXT_ASSERT_DUMP
 ******************************************************************************/
#define ASSERT(expr) __ASSERT(expr)
/*******************************************************************************
 * <GROUP Functions>
 *  
 * FUNCTION
 *  EXT_ASSERT
 * DESCRIPTION
 *  external assert the condition is true, otherwise enter system exception handling.
 * PARAMETERS
 *  expr: [IN] the condition to be evaluated.
 *  e1: [IN] first extra expression recorded.
 *  e2: [IN] second extra expression recorded.
 *  e3: [IN] third extra expression recorded.
 * RETURNS
 *  N/A
 * NOTE
 *  (1)The assertion takes effect all the time, both DEBUG_KAL and RELEASE_KAL.
 * SEE ALSO
 *   ASSERT EXT_ASSERT_DUMP
 ******************************************************************************/
#define EXT_ASSERT(expr, e1, e2, e3) __EXT_ASSERT(expr, e1, e2, e3)
#endif /*__BUILD_DOM__*/

/* DOM-NOT_FOR_SDK-BEGIN */

/* deprecated interface. use RAND_MAX & rand() in standard C libarary */
#define STACK_RAND_MAX      0x7fffffff
extern kal_uint32 stack_rand(void);

/*******************************************************************************
 * Category 10 --- Profiling
 *******************************************************************************/

/*******************************************************************************
 * <GROUP Functions>
 *  
 * FUNCTION
 *  kal_profile_enable
 * DESCRIPTION
 *  enable cpu usage profile.
 * PARAMETERS
 *  N/A
 * RETURNS
 *  N/A
 * SEE ALSO
 *   kal_profile_get_cpu_usage
 ******************************************************************************/
void kal_profile_enable(void);
/*******************************************************************************
 * <GROUP Functions>
 *  
 * FUNCTION
 *  kal_profile_get_cpu_usage
 * DESCRIPTION
 *  get cpu usage percentage, which is a float number at [0.0, 1.0].
 * PARAMETERS
 *  N/A
 * RETURNS
 *  cpu usage percentage.
 * SEE ALSO
 *   kal_profile_enable
 ******************************************************************************/
float kal_profile_get_cpu_usage(void); 

/*******************************************************************************
 * Category Last --- 
 *******************************************************************************/

/* DOM-NOT_FOR_SDK-END */

#ifdef __cplusplus 
}
#endif

#endif  /* _KAL_PUBLIC_API_H */