SensorSrv.c 138 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 3553 3554 3555 3556 3557 3558 3559 3560 3561 3562 3563 3564 3565 3566 3567 3568 3569 3570 3571 3572 3573 3574 3575 3576 3577 3578 3579 3580 3581 3582 3583 3584 3585 3586 3587 3588 3589 3590 3591 3592 3593 3594 3595 3596 3597 3598 3599 3600 3601 3602 3603 3604 3605 3606 3607 3608 3609 3610 3611 3612 3613 3614 3615 3616 3617 3618 3619 3620 3621 3622 3623 3624 3625 3626 3627 3628 3629 3630 3631 3632 3633 3634 3635 3636 3637 3638 3639 3640 3641 3642 3643 3644 3645 3646 3647 3648 3649 3650 3651 3652 3653 3654 3655 3656 3657 3658 3659 3660 3661 3662 3663 3664 3665 3666 3667 3668 3669 3670 3671 3672 3673 3674 3675 3676 3677 3678 3679 3680 3681 3682 3683 3684 3685 3686 3687 3688 3689 3690 3691 3692 3693 3694 3695 3696 3697 3698 3699 3700 3701 3702 3703 3704 3705 3706 3707 3708 3709 3710 3711 3712 3713 3714 3715 3716 3717 3718 3719 3720 3721 3722 3723 3724 3725 3726 3727 3728 3729 3730 3731 3732 3733 3734 3735 3736 3737 3738 3739 3740 3741 3742 3743 3744 3745 3746 3747 3748 3749 3750 3751 3752 3753 3754 3755 3756 3757 3758 3759 3760 3761 3762 3763 3764 3765 3766 3767 3768 3769 3770 3771 3772 3773 3774 3775 3776 3777 3778 3779 3780 3781 3782 3783 3784 3785 3786 3787 3788 3789 3790 3791 3792 3793 3794 3795 3796 3797 3798 3799 3800 3801 3802 3803 3804 3805 3806 3807 3808 3809 3810 3811 3812 3813 3814 3815 3816 3817 3818 3819 3820 3821 3822 3823 3824 3825 3826 3827 3828 3829 3830 3831 3832 3833 3834 3835 3836 3837 3838 3839 3840 3841 3842 3843 3844 3845 3846 3847 3848 3849 3850 3851 3852 3853 3854 3855 3856 3857 3858 3859 3860 3861 3862 3863 3864 3865 3866 3867 3868 3869 3870 3871 3872 3873 3874 3875 3876 3877 3878 3879 3880 3881 3882 3883 3884 3885 3886 3887 3888 3889 3890 3891 3892 3893 3894 3895 3896 3897 3898 3899 3900 3901 3902 3903 3904 3905 3906 3907 3908 3909 3910 3911 3912 3913 3914 3915 3916 3917 3918 3919 3920 3921 3922 3923 3924 3925 3926 3927 3928 3929 3930 3931 3932 3933 3934 3935 3936 3937 3938 3939 3940 3941 3942 3943 3944 3945 3946 3947 3948 3949 3950 3951 3952 3953 3954 3955 3956 3957 3958 3959 3960 3961 3962 3963 3964 3965 3966 3967 3968 3969 3970 3971 3972 3973 3974 3975 3976 3977 3978 3979 3980 3981 3982 3983 3984 3985 3986 3987 3988 3989 3990 3991 3992 3993 3994 3995 3996 3997 3998 3999 4000 4001 4002 4003 4004 4005 4006 4007 4008 4009 4010 4011 4012 4013 4014 4015 4016 4017 4018 4019 4020 4021 4022 4023 4024 4025 4026 4027 4028 4029 4030 4031 4032 4033 4034 4035 4036 4037 4038 4039 4040 4041 4042 4043 4044 4045 4046 4047 4048 4049 4050 4051 4052 4053 4054 4055 4056 4057 4058 4059 4060 4061 4062 4063 4064 4065 4066 4067 4068 4069 4070 4071 4072 4073 4074 4075 4076 4077 4078 4079 4080 4081 4082 4083 4084 4085 4086 4087 4088 4089 4090 4091 4092 4093 4094 4095 4096 4097 4098 4099 4100 4101 4102 4103 4104 4105 4106 4107 4108 4109 4110 4111 4112 4113 4114 4115 4116 4117 4118 4119 4120 4121 4122 4123 4124 4125 4126 4127 4128 4129 4130 4131 4132 4133 4134 4135 4136 4137 4138 4139 4140 4141 4142 4143 4144 4145 4146 4147 4148 4149 4150 4151 4152 4153 4154 4155 4156 4157 4158 4159 4160 4161 4162 4163 4164 4165 4166 4167 4168 4169 4170 4171 4172 4173 4174 4175 4176 4177 4178 4179 4180 4181 4182 4183 4184 4185 4186 4187 4188 4189 4190 4191 4192 4193 4194 4195 4196 4197 4198 4199 4200 4201 4202 4203 4204 4205 4206 4207 4208 4209 4210 4211 4212 4213 4214 4215 4216 4217 4218 4219 4220 4221 4222 4223 4224 4225 4226 4227 4228 4229 4230 4231 4232 4233 4234 4235 4236 4237 4238 4239 4240 4241 4242 4243 4244 4245 4246 4247 4248 4249 4250 4251 4252 4253 4254 4255 4256 4257 4258 4259 4260 4261 4262 4263 4264 4265 4266 4267 4268 4269 4270 4271 4272 4273 4274 4275 4276 4277 4278 4279 4280 4281 4282 4283 4284 4285 4286 4287 4288 4289 4290 4291 4292 4293 4294 4295 4296 4297 4298 4299 4300 4301 4302 4303 4304 4305 4306 4307 4308 4309 4310 4311 4312 4313 4314 4315 4316 4317 4318 4319 4320 4321 4322 4323 4324 4325 4326 4327 4328 4329 4330 4331 4332 4333 4334 4335 4336 4337 4338 4339 4340 4341 4342 4343 4344 4345 4346 4347 4348 4349 4350 4351 4352 4353 4354 4355 4356 4357 4358 4359 4360 4361 4362 4363 4364 4365 4366 4367 4368 4369 4370 4371 4372 4373 4374 4375 4376 4377 4378 4379 4380 4381 4382 4383 4384 4385 4386 4387 4388 4389 4390 4391 4392 4393 4394 4395 4396 4397 4398 4399 4400 4401 4402 4403 4404 4405 4406 4407 4408 4409 4410 4411 4412 4413 4414 4415 4416 4417 4418 4419 4420 4421 4422 4423 4424 4425 4426 4427 4428 4429 4430 4431 4432 4433 4434 4435 4436 4437 4438 4439 4440 4441 4442 4443 4444 4445 4446 4447 4448 4449 4450 4451 4452 4453 4454 4455 4456 4457 4458 4459 4460 4461 4462 4463 4464 4465 4466 4467 4468 4469 4470 4471 4472 4473 4474 4475 4476 4477 4478 4479 4480 4481 4482 4483 4484 4485 4486 4487 4488 4489 4490 4491 4492 4493 4494 4495 4496 4497 4498 4499 4500 4501 4502 4503 4504 4505 4506 4507 4508 4509 4510 4511 4512 4513 4514 4515 4516 4517 4518 4519 4520 4521 4522 4523 4524 4525 4526 4527 4528 4529 4530 4531 4532 4533 4534
/*****************************************************************************
*  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:
* ---------
*  SensorSrv.c
*
* Project:
* --------
*  MAUI
*
* Description:
* ------------
*  This file is intends for sensor service
*
* 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!
 * 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!
*
*------------------------------------------------------------------------------
* Upper this line, this part is controlled by PVCS VM. DO NOT MODIFY!!
*============================================================================
****************************************************************************/

#include "MMI_features.h"

#include "MMIDataType.h"
#include "custom_events_notify.h"
#include "mmi_frm_gprot.h"
#include "gui.h"        				/*	 UI timer */
#include "math.h"      			  /* atan */

#include "mdi_datatype.h"       /* mdi datatype */
#include "mdi_resdef.h"
#include "SensorSrvGport.h"

#include "common_nvram_editor_data_item.h"
#include "nvram_common_defs.h"

#include "stack_config.h"    
#include "kal_general_types.h"   
#include "kal_public_defs.h"    
#include "app_ltlcom.h"   
#include "kal_public_api.h"    
#include "DebugInitDef_Int.h"   
#include "MMI_media_app_trc.h"   
#include "mmi_media_app_trc.h"   
#include "kal_trace.h"   
#include "stack_msgs.h"   
#include "mmi_frm_events_gprot.h"   
#include "string.h"   
#include "stdlib.h"   
#include "mmi_frm_queue_gprot.h"   
#include "mmi_rp_srv_mdi_def.h"

#include "mmi_cb_mgr_gprot.h"
#include "GpioSrvGprot.h"



#ifdef __PXS_ENABLE__
#include "Drv_comm.h"
#include "mmi_rp_srv_sensor_def.h"
#include "dcl.h" /* for proximity */
#endif /* __PXS_ENABLE__ */

#ifdef MOTION_SENSOR_SUPPORT

#include "mmi_rp_srv_sensor_def.h"
#include "mcube_library_if.h"
#include "mcube_storage.h"

#ifdef __MTK_TARGET__
/* 
* - For PC Simulator and MoDis, do not call media's webcam functions 
* - For Target, call media's cam functions.
*/
#define MDI_MOTION_DRIVER_AVAIALBE
#endif /* __MTK_TARGET__ */ 

#endif /* MOTION_SENSOR_SUPPORT */

/***************************************************************************** 
* Include
*****************************************************************************/


#ifdef MOTION_SENSOR_SUPPORT

#ifdef MDI_MOTION_DRIVER_AVAIALBE
#include "motion_sensor.h"      /* motion sensor driver */
#endif 

#endif /* MOTION_SENSOR_SUPPORT */

/***************************************************************************** 
* Define
*****************************************************************************/
#ifdef MOTION_SENSOR_SUPPORT

#define MOTION_SUPPORT_APP_NUM                  (5)   /*the number of app can open motion*/
#define MOTION_SAMPLE_PERIOD                    (5)        /* value*10 ms, if 5 means 50 ms */
#define MOTION_THRESHOLD_FOR_DRV                0//(50)
#define MOTION_SHAKE_DET_MUTE_DURATION          (800)
#define MOTION_SHAKE_HISTORY_DEPTH              (5)

#define MOTION_DIRECT_DEBOUNCE                  (400)    /*It can be tune by customer*/

/* when sensitive is higher, threshold value smaller, 
* whihc means smaller action will trigger event  */
#define MOTION_SHAKE_THRESHOLD_HIGH             (500)
#define MOTION_SHAKE_THRESHOLD_NORMAL           (1000)
#define MOTION_SHAKE_THRESHOLD_LOW              (1500)

#define MOTION_TILE_THRESHOLD_HIGH              (10)
#define MOTION_TILE_THRESHOLD_NORMAL            (50)
#define MOTION_TILE_THRESHOLD_LOW               (100)
#define MOTION_ANGLE_SENS_HIGH_THRESHOLD        (45)
#define MOTION_ANGLE_SENS_NORMAL_THRESHOLD      (30)
#define MOTION_ANGLE_SENS_LOW_THRESHOLD         (20)

#define MOTION_ACC_G                            (980.0)
#define MOTION_ACC_G_THRESHOLD                  (100)
#define MOTION_ACC_G_THRESHOLD_AXIS             (300)
#define MOTION_SQUARE_G                         (MOTION_ACC_G * MOTION_ACC_G)
#define MOTION_SQUARE_G_THRESHOLD               (100 * 100)

#define MOTION_ERR_CALC_ANGLE_FAILED            (-1)
#define MOTION_ANGLE_180                        (180)
#define MOTION_ANGLE_90                         (90)
#define H_PI    (3.1415926/2)
#define PI      (3.1415926)

#define MOTION_ACC_G_CALI_THRESHOLD             (100)
#define MOTION_ACC_G_CALI_MIN                   (MOTION_ACC_G - MOTION_ACC_G_CALI_THRESHOLD)
#define MOTION_ACC_G_CALI_MAX                   (MOTION_ACC_G + MOTION_ACC_G_CALI_THRESHOLD)
#define MOTION_ACC_G_OFFSET_MAX                 (1 * 100)
#define MOTION_ACC_G_SCALE_MAX                  (1.20 * 10000.0)
#define MOTION_ACC_G_SCALE_MIN                  (0.8 * 10000.0)

#define MOTION_ENTER_PROTECT                    kal_take_mutex(g_srv_sensor_motion_cntx.mutex)
#define MOTION_EXIT_PROTECT                     kal_give_mutex(g_srv_sensor_motion_cntx.mutex)
#define MOTION_RUN_MOD                          MOD_MMI



#define MOTION_SUPPORT_MAX_MODE                 (10)


#endif /* MOTION_SENSOR_SUPPORT */

#ifdef __PXS_ENABLE__
#define SENSOR_PXS_FIRST_POLLING_TIME           (500)   /* ms */
#define SENSOR_PXS_REPEAT_POLLING_TIME          (200)   /* ms */
#endif

/***************************************************************************** 
* Typedef 
*****************************************************************************/
#ifdef MOTION_SENSOR_SUPPORT

/* This struct is for motion sensor mode(always on or others)*/
typedef struct
{
	U8 flag ;
	U8 mode ;
	SRV_SENSOR_HANDLE hdlr;

}srv_sensor_motion_mode_struct;


typedef enum
{
	MOTION_DATA_EPMTY,
	MOTION_DATA_FULL,
	MOTION_DATA_EXIST
} srv_sensor_motion_data_state_enum;

typedef struct
{
	S32 acc_x; /*real value * 100*/
	S32 acc_y;
} srv_sensor_motion_acc_history_struct;


typedef struct
{
	srv_sensor_motion_tilt_struct          tilt;                /*tilt data*/       
	void                            	   *tilt_user_data;

	srv_sensor_motion_shake_struct          shake;
	void                            	   *shake_user_data;

	srv_sensor_motion_direct_struct        direct;             	/*direct data*/
	void                                   *direct_user_data;

	SRV_SENSOR_EVENT_HDLR                  tilt_callback;
	SRV_SENSOR_EVENT_HDLR                  shake_callback;
	SRV_SENSOR_EVENT_HDLR                  direct_callback;

	srv_sensor_type_enum                   type;

	srv_sensor_motion_direct_cfg_struct    direct_sen;
	srv_sensor_motion_shake_cfg_struct     shake_sen;
	srv_sensor_motion_tilt_cfg_struct      tilt_sen;

	module_type                     	    mod_id;
	MMI_BOOL    is_msg_send;

	MMI_BOOL    is_shake;
	MMI_BOOL    is_tilt;
	MMI_BOOL    is_direct;

	srv_sensor_motion_direct_enum          prev_direct;


	srv_sensor_motion_tap_struct           		tap;    		 	/* Tap data */
	void                            	   		*tap_user_data;
	srv_sensor_motion_double_tap_struct    		double_tap;			/* double tap data */
	void                                   		*double_tap_user_data;
	srv_sensor_motion_advanced_shake_struct  	advanced_shake;     /* advanced shake */
	void                                     	*advanced_shake_user_data;
	srv_sensor_motion_turnover_struct           turnover;			/* turn over */
	void                                        *turnover_user_data;
	srv_sensor_motion_drop_struct               drop;				/* drop */
	void                                        *drop_user_data;


	SRV_SENSOR_EVENT_HDLR				   		tap_callback;  
	SRV_SENSOR_EVENT_HDLR				   		double_tap_callback; 
	SRV_SENSOR_EVENT_HDLR						advanced_shake_callback;
	SRV_SENSOR_EVENT_HDLR						turnover_callback;
	SRV_SENSOR_EVENT_HDLR						drop_callback;

	//#ifdef __MOTION_SENSOR_ADVANCED_GESTURE__
	srv_sensor_motion_step_struct               step;				/* step */
	void                                        *step_user_data;
	SRV_SENSOR_EVENT_HDLR                       step_callback;
	MMI_BOOL	is_step;
	//#endif /*__MOTION_SENSOR_ADVANCED_GESTURE__*/

	MMI_BOOL    is_tap;
	MMI_BOOL    is_double_tap;
	MMI_BOOL    is_advanced_shake;
	MMI_BOOL    is_turnover;

	MMI_BOOL    is_drop;

} srv_sensor_motion_app_struct;

typedef struct
{
	LOCAL_PARA_HDR U16 data_state;
} srv_sensor_motion_msg_struct;


typedef struct
{
	LOCAL_PARA_HDR 
		srv_sensor_motion_cali_status_enum cali_status;
}srv_sensor_motion_cali_msg_struct;


/* This struct is for mcube sensor data */
typedef struct
{
	uint8_t status; 
	float mcube_acc_x;
	float mcube_acc_y;
	float mcube_acc_z;

	kal_uint64 timeStampMs; /*Time stamp*/

}sensor_srv_motion_mcube_sensor_data;


/*There will store the history action and direction*/
typedef struct
{
	srv_sensor_motion_action_enum   prev_action;   /*previous action*/
	srv_sensor_motion_direct_enum    prev_direct;   /*previous direction*/
	// mdi_motion_action_enum   prev2_action;  /*the previous action of previous*/
	// mdi_motion_angle_enum    prev2_direct;  /*the previous direction of previous*/
	// MMI_BOOL is_prev_action;                /*is has previous data*/
	// MMI_BOOL is_prev2_action;               /*is has previous data of previous*/
} srv_sensor_motion_direct_pre_action_struct;

/*Detail infomation of tilt detection*/
typedef struct 
{
	S32 angle_gx;   /*X angle related to acceleration of gravity, 0-180 and less then 0 means not support*/
	S32 angle_gy;   /*Y angle related to acceleration of gravity, 0-180 and less then 0 means not support*/
	S32 angle_gz;   /*Z angle 0-90(for hardware not support) and less then 0 means can't detect this time*/

	S16 acc_x; /*acc*10 at x axis*/
	S16 acc_y; /*acc*10 at y axis*/
	S16 acc_z; /*acc*10 at z axis*/
	MMI_BOOL is_support_acc_x;/*is sensor support acc at x axis*/
	MMI_BOOL is_support_acc_y;/*is sensor support acc at y axis*/
	MMI_BOOL is_support_acc_z;/*is sensor support acc at z axis*/

	sensor_srv_motion_mcube_sensor_data sensor_data;
} srv_sensor_motion_detail_struct;

typedef struct
{
	srv_sensor_motion_app_struct           app[MOTION_SUPPORT_APP_NUM];
	srv_sensor_motion_acc_history_struct   history[MOTION_SHAKE_HISTORY_DEPTH];
	srv_sensor_motion_detail_struct        detail_info;    
	kal_mutexid mutex;
	MMI_BOOL    is_shake_mute;
	MMI_BOOL    is_msg_send;
	MMI_BOOL    is_mute;	
} srv_sensor_motion_cntx_struct;


typedef struct
{
	SRV_SENSOR_HANDLE internal_handle;
	srv_sensor_type_enum app_type;
	SRV_SENSOR_HANDLE external_handle;			/*Interface handle for app*/ 

}srv_sensor_motion_handle_map;

#endif /* MOTION_SENSOR_SUPPORT */


#ifdef __PXS_ENABLE__

typedef void (* srv_sensor_pxs_set_val_func)(void *);


typedef struct
{
	srv_sensor_pxs_set_param_type_enum          pxs_set_type;
	srv_sensor_pxs_set_val_func      internal_set_handler;
}srv_sensor_pxs_set_map_tbl;

#endif /*__PXS_ENABLE__*/

/***************************************************************************** 
* const
*****************************************************************************/
#ifdef MOTION_SENSOR_SUPPORT

#define HANDLE_TAB_SIZE   sizeof(handle_tab)/sizeof(srv_sensor_motion_handle_map)


static const srv_sensor_motion_handle_map handle_tab[45] = 
{
	{0,SRV_SENSOR_MOTION_DIRECT,1},
	{0,SRV_SENSOR_MOTION_SHAKE,2},
	{0,SRV_SENSOR_MOTION_TILT,3},
	{0,SRV_SENSOR_MOTION_TAP,4},
	{0,SRV_SENSOR_MOTION_DOUBLE_TAP,5},
	{0,SRV_SENSOR_MOTION_DROP,6},
	{0,SRV_SENSOR_MOTION_STEP,7},
	{0,SRV_SENSOR_MOTION_ADVANCED_SHAKE,8},
	{0,SRV_SENSOR_MOTION_TURNOVER,9},

	{1,SRV_SENSOR_MOTION_DIRECT,10},
	{1,SRV_SENSOR_MOTION_SHAKE,11},
	{1,SRV_SENSOR_MOTION_TILT,12},
	{1,SRV_SENSOR_MOTION_TAP,13},
	{1,SRV_SENSOR_MOTION_DOUBLE_TAP,14},
	{1,SRV_SENSOR_MOTION_DROP,15},
	{1,SRV_SENSOR_MOTION_STEP,16},
	{1,SRV_SENSOR_MOTION_ADVANCED_SHAKE,17},
	{1,SRV_SENSOR_MOTION_TURNOVER,18},

	{2,SRV_SENSOR_MOTION_DIRECT,19},
	{2,SRV_SENSOR_MOTION_SHAKE,20},
	{2,SRV_SENSOR_MOTION_TILT,21},
	{2,SRV_SENSOR_MOTION_TAP,22},
	{2,SRV_SENSOR_MOTION_DOUBLE_TAP,23},
	{2,SRV_SENSOR_MOTION_DROP,24},
	{2,SRV_SENSOR_MOTION_STEP,25},
	{2,SRV_SENSOR_MOTION_ADVANCED_SHAKE,26},
	{2,SRV_SENSOR_MOTION_TURNOVER,27},

	{3,SRV_SENSOR_MOTION_DIRECT,28},
	{3,SRV_SENSOR_MOTION_SHAKE,29},
	{3,SRV_SENSOR_MOTION_TILT,30},
	{3,SRV_SENSOR_MOTION_TAP,31},
	{3,SRV_SENSOR_MOTION_DOUBLE_TAP,32},
	{3,SRV_SENSOR_MOTION_DROP,33},
	{3,SRV_SENSOR_MOTION_STEP,34},
	{3,SRV_SENSOR_MOTION_ADVANCED_SHAKE,35},
	{3,SRV_SENSOR_MOTION_TURNOVER,36},


	{4,SRV_SENSOR_MOTION_DIRECT,37},
	{4,SRV_SENSOR_MOTION_SHAKE,38},
	{4,SRV_SENSOR_MOTION_TILT,39}, 
	{4,SRV_SENSOR_MOTION_TAP,40},
	{4,SRV_SENSOR_MOTION_DOUBLE_TAP,41},
	{4,SRV_SENSOR_MOTION_DROP,42},
	{4,SRV_SENSOR_MOTION_STEP,43},
	{4,SRV_SENSOR_MOTION_ADVANCED_SHAKE,44},
	{4,SRV_SENSOR_MOTION_TURNOVER,45}
};

#endif /* MOTION_SENSOR_SUPPORT */

#ifdef __PXS_ENABLE__

static void srv_sensor_pxs_set_debounce(void *);  /*internal function, for set pxs debounce*/


srv_sensor_pxs_set_map_tbl g_sensor_srv_pxs_set_val_tab[SRV_SENSOR_PXS_PARAM_TYPE_TOTAL] = 
{
	{SRV_SENSOR_PXS_PARAM_TYPE_DEBOUNCE,    srv_sensor_pxs_set_debounce}
};

#endif /*__PXS_ENABLE__*/


/***************************************************************************** 
* Global Function
*****************************************************************************/
#ifdef MOTION_SENSOR_SUPPORT
srv_sensor_motion_cntx_struct g_srv_sensor_motion_cntx;
MMI_BOOL    g_motion_sensor_able;  /*If sensor is on enable mode,default is enable*/


MMI_BOOL g_is_reg_mcube = MMI_FALSE;    /* If MMI_TRUE, some APP have registered mCube */

MMI_BOOL g_is_power_on = MMI_FALSE;    

MMI_BOOL g_sub_init = MMI_TRUE;

srv_sensor_motion_mode_struct g_motion_sensor_tab[10] = {0};

srv_sensor_motion_flip_para_struct g_mcube_init ={MMI_TRUE,0.7,-0.3,0.4,1.2};

/* mcube init value 
g_mcube_init.flipEnable = MMI_TRUE;
g_mcube_init.flipFilter = 0.7;
g_mcube_init.flipThreshold = -0.3;
g_mcube_init.tFlipDebounce = 0.4;
g_mcube_init.tFlipInterval = 1.2;*/


S16 old_x,old_y,old_z;
S16 new_x = -10000, new_y = -10000, new_z = -10000;

/* Shake vs Direct */
srv_sensor_motion_direct_enum  g_direction = SRV_SENSOR_MOTION_ANGLE_OTHER;
srv_sensor_motion_direct_enum  g_old_direction = SRV_SENSOR_MOTION_ANGLE_OTHER;
srv_sensor_motion_direct_enum  g_real_direction = SRV_SENSOR_MOTION_ANGLE_OTHER;


#endif /* MOTION_SENSOR_SUPPORT */

/***************************************************************************** 
* Local Variable
*****************************************************************************/
#ifdef __PXS_ENABLE__

extern DCL_HANDLE g_pxs_handle = DCL_HANDLE_NONE;   /* for PXS */
/* If MSG_ID_SRV_SENSOR_PXS_DETECT_OBJECT_IND is already en-queued in MMI external queue, we don't need to send the primitive again */
static MMI_BOOL g_pxs_is_indication_in_queue = MMI_FALSE;

static U8 g_pxs_user_count = 0;
MMI_BOOL g_pxs_res_status = MMI_FALSE;


#endif /* __PXS_ENABLE__ */

/***************************************************************************** 
* Local Function
*****************************************************************************/
#ifdef MOTION_SENSOR_SUPPORT

#ifdef MDI_MOTION_DRIVER_AVAIALBE
static void srv_sensor_motion_data_hdlr(void *msg_ptr);
static void srv_sensor_motion_driver_data_state_hdlr(void *parameter, Motion_Sensor_BuffState_enum state);

#endif /* MDI_MOTION_DRIVER_AVAIALBE */ 

static SRV_SENSOR_RESULT srv_sensor_motion_add_to_slot(srv_sensor_type_enum type,
													   U8 sensitive,void(* callback)(),void * param,void * user_data);
static SRV_SENSOR_RESULT srv_sensor_motion_remove_from_slot(srv_sensor_type_enum type,SRV_SENSOR_HANDLE index);
static void srv_sensor_motion_reset_direct_data(srv_sensor_motion_direct_struct *tilt);
static void srv_sensor_motion_data_hdlr(void *msg_ptr);
static void srv_sensor_motion_update_hldr(void);
static void srv_sensor_motion_shake_add_to_history(srv_sensor_motion_acc_history_struct *history);
static void srv_sensor_motion_calc_angle_gxyz_by_xy(
	S32 int_x, S32 int_y, S32 int_z, 
	S32 *out_angle_gx, S32 *out_angle_gy, S32 *out_angle_gz);
static srv_sensor_motion_direct_enum
srv_sensor_motion_calc_axis_direct(U8 sensitive, S32 angle_gx, S32 angle_gy, S32 angle_gz, S32 acc_x, S32 acc_y);
static srv_sensor_motion_action_enum
srv_sensor_motion_calc_action(srv_sensor_motion_direct_enum old_direct, srv_sensor_motion_direct_enum new_direct);
static srv_sensor_motion_shake_action_enum srv_sensor_motion_calc_shake(srv_sensor_motion_acc_history_struct *history, 
																		srv_sensor_motion_shake_sensitive_enum sensitive);
static void srv_sensor_motion_push_direct2history(
	srv_sensor_motion_direct_enum motion_direct,
	srv_sensor_motion_app_struct *app_tilt);

//static MMI_BOOL srv_sensor_motion_is_around(S32 x, S32 point, S32 threshold);

static SRV_SENSOR_HANDLE srv_sensor_motion_get_external_handle(srv_sensor_type_enum type,SRV_SENSOR_HANDLE handle);

static void srv_sensor_motion_get_internal_handle(SRV_SENSOR_HANDLE handle, SRV_SENSOR_HANDLE *internal_handle, srv_sensor_type_enum *type);

//static MMI_BOOL srv_sensor_motion_is_normal_mode();

static void srv_sensor_motion_remove_mode(SRV_SENSOR_HANDLE handler);

static U16 srv_sensor_motion_check_mode();

static MMI_BOOL srv_sensor_no_have_mcube_reg();




#endif /* MOTION_SENSOR_SUPPORT */


/***************************************************************************** 
* External functions
*****************************************************************************/
#ifdef MOTION_SENSOR_SUPPORT

/*****************************************************************************
* FUNCTION
*  sensor_srv_motion_write_switch
* DESCRIPTION
*  Write switch of motion sensor to NVRAM
* PARAMETERS
*  is_enable
* RETURNS
*  MDI_RESULT
*****************************************************************************/
static sensor_srv_motion_write_switch(MMI_BOOL is_enable)
{
	nvram_ef_motion_sensor_switch_struct sensor_switch;
	S16 ErrorCode;

	sensor_switch.motion_sensor_switch = is_enable;

	WriteRecord(NVRAM_EF_MOTION_SENSOR_SWITCH_LID, 1, &sensor_switch, 
		NVRAM_EF_MOTION_SENSOR_SWITCH_SIZE, &ErrorCode);

}

/*****************************************************************************
* FUNCTION
*  sensor_srv_motion_read_switch
* DESCRIPTION
*  Read motion sensor switch from NVRAM
* PARAMETERS
*  void
* RETURNS
*  MMI_BOOL
*****************************************************************************/
static MMI_BOOL sensor_srv_motion_read_switch()
{
	nvram_ef_motion_sensor_switch_struct sensor_switch;
	S16 ErrorCode;

	ReadRecord(NVRAM_EF_MOTION_SENSOR_SWITCH_LID,1,&sensor_switch,NVRAM_EF_MOTION_SENSOR_SWITCH_SIZE,&ErrorCode);

	g_motion_sensor_able = sensor_switch.motion_sensor_switch;

//	kal_prompt_trace(MOD_MMI,"[mcube] %d",g_motion_sensor_able);

}


/*****************************************************************************
* FUNCTION
*  srv_sensor_motion_init
* DESCRIPTION
*  init MDI Camera
* PARAMETERS
*  void
* RETURNS
*  MDI_RESULT
*****************************************************************************/
mmi_ret srv_sensor_motion_init(mmi_event_struct *evt)
{
	/*----------------------------------------------------------------*/
	/* Local Variables                                                */
	/*----------------------------------------------------------------*/
	U32 i;

	/*----------------------------------------------------------------*/
	/* Code Body                                                      */
	/*----------------------------------------------------------------*/
	for (i = 0; i < MOTION_SUPPORT_APP_NUM; i++)
	{
		g_srv_sensor_motion_cntx.app[i].mod_id = MOD_NIL;
		g_srv_sensor_motion_cntx.app[i].is_msg_send = MMI_FALSE;
		g_srv_sensor_motion_cntx.app[i].is_direct = MMI_FALSE;
		g_srv_sensor_motion_cntx.app[i].is_shake = MMI_FALSE;
		g_srv_sensor_motion_cntx.app[i].is_tilt = MMI_FALSE;
		g_srv_sensor_motion_cntx.app[i].prev_direct = SRV_SENSOR_MOTION_ANGLE_GY180;
	}
	g_srv_sensor_motion_cntx.is_shake_mute = MMI_FALSE;
	g_srv_sensor_motion_cntx.is_msg_send = MMI_FALSE;
	g_srv_sensor_motion_cntx.mutex = kal_create_mutex((CHAR*)"srv_sensor_motion");

	sensor_srv_motion_read_switch();

#ifdef MDI_MOTION_DRIVER_AVAIALBE

	motion_sensor_read_cali_from_nvram(); /* Notify drv to read calibration information */

#endif /*MDI_MOTION_DRIVER_AVAIALBE*/


	return MMI_RET_OK;
}

mmi_ret srv_sensor_motion_sub_init()
{

	/*----------------------------------------------------------------*/
	/* Local Variables                                                */
	/*----------------------------------------------------------------*/
	U32 i;

	/*----------------------------------------------------------------*/
	/* Code Body                                                      */
	/*----------------------------------------------------------------*/
	for (i = 0; i < MOTION_SUPPORT_APP_NUM; i++)
	{
		g_srv_sensor_motion_cntx.app[i].mod_id = MOD_NIL;
		g_srv_sensor_motion_cntx.app[i].is_msg_send = MMI_FALSE;
		g_srv_sensor_motion_cntx.app[i].is_direct = MMI_FALSE;
		g_srv_sensor_motion_cntx.app[i].is_shake = MMI_FALSE;
		g_srv_sensor_motion_cntx.app[i].is_tilt = MMI_FALSE;
		g_srv_sensor_motion_cntx.app[i].prev_direct = SRV_SENSOR_MOTION_ANGLE_GY180;
	}
	g_srv_sensor_motion_cntx.is_shake_mute = MMI_FALSE;
	g_srv_sensor_motion_cntx.is_msg_send = MMI_FALSE;
	g_srv_sensor_motion_cntx.mutex = kal_create_mutex((CHAR*)"srv_sensor_motion");

	sensor_srv_motion_read_switch();

	return MMI_RET_OK;
}



static SRV_SENSOR_RESULT power_off_sensor()
{

	if (srv_sensor_motion_check_mode() != SRV_SENSOR_MOTION_ALWAYS_ON_MODE )
	{
#ifdef MDI_MOTION_DRIVER_AVAIALBE
		motion_sensor_power(MMI_FALSE);
		g_is_power_on = MMI_FALSE;

		motion_sensor_sample(MMI_FALSE); /*Add by XiaoHua*/

		if (srv_sensor_no_have_mcube_reg() != MMI_TRUE)
		{
			mCube_LibClose();
		}

#endif /* MDI_MOTION_DRIVER_AVAIALBE */ 


		return MDI_RES_MOTION_SUCCEED;
	}

}


static SRV_SENSOR_RESULT power_on_sensor()
{
	mCubeLibInit_t initData;

	initData.accelInit.accelOrientation = CHIP_TOP_RIGHT_DOWN;
	initData.accelInit.accelPolaBafrInit.enableUpdate = MMI_TRUE;
	initData.accelInit.accelPolaBafrInit.polaUpdRateHz = 10;

	initData.accelInit.accelPolaBafrInit.flipCntrl.flipEnable = g_mcube_init.flipEnable ;
	initData.accelInit.accelPolaBafrInit.flipCntrl.flipFilter = g_mcube_init.flipFilter;
	initData.accelInit.accelPolaBafrInit.flipCntrl.flipThreshold = g_mcube_init.flipThreshold;
	initData.accelInit.accelPolaBafrInit.flipCntrl.tFlipDebounce = g_mcube_init.tFlipDebounce ;
	initData.accelInit.accelPolaBafrInit.flipCntrl.tFlipInterval = g_mcube_init.tFlipInterval;


	if (srv_sensor_motion_check_mode() != SRV_SENSOR_MOTION_ALWAYS_ON_MODE )
	{

#ifdef MDI_MOTION_DRIVER_AVAIALBE

		motion_sensor_power(MMI_TRUE);
		g_is_power_on = MMI_TRUE;
		// motion_sensor_conf_sample_period(MOTION_SAMPLE_PERIOD);
		motion_sensor_flush_buff();
		motion_sensor_cb_registration(srv_sensor_motion_driver_data_state_hdlr, NULL);

		motion_sensor_conf_filter(MOTION_THRESHOLD_FOR_DRV);
		/* star sampling */
		motion_sensor_sample(MMI_TRUE);

		if (srv_sensor_no_have_mcube_reg() != MMI_TRUE)
		{
			mCube_LibOpen( &initData );
		}
#endif /* MDI_MOTION_DRIVER_AVAIALBE */ 
		return SRV_SENSOR_MOTION_SUCCEED;    
	}
}


/*****************************************************************************
* FUNCTION
*  srv_sensor_motion_power_on
* DESCRIPTION
*  power on
* PARAMETERS
*  void
* RETURNS
*  MDI_RESULT
*****************************************************************************/
static SRV_SENSOR_RESULT srv_sensor_motion_power_on(void)
{
	/*----------------------------------------------------------------*/
	/* Local Variables                                                */
	/*----------------------------------------------------------------*/

	/*----------------------------------------------------------------*/
	/* Code Body                                                      */
	/*----------------------------------------------------------------*/
#ifdef MDI_MOTION_DRIVER_AVAIALBE
	motion_sensor_power(MMI_TRUE);
	g_is_power_on = MMI_TRUE;
	motion_sensor_conf_sample_period(MOTION_SAMPLE_PERIOD);
	motion_sensor_flush_buff();


	mmi_frm_cb_reg_event (EVT_ID_GPIO_LCD_SLEEP_IN, 
		power_off_sensor, NULL);

	mmi_frm_cb_reg_event (EVT_ID_GPIO_LCD_SLEEP_OUT, 
		power_on_sensor, NULL);

#endif /* MDI_MOTION_DRIVER_AVAIALBE */ 
	return SRV_SENSOR_MOTION_SUCCEED;
}

/*****************************************************************************
* FUNCTION
*  srv_sensor_motion_shake_det_mute_timeout
* DESCRIPTION
*  shake timeout function
* PARAMETERS
*  void
* RETURNS
*  void
*****************************************************************************/
static void srv_sensor_motion_shake_det_mute_timeout(void)
{
	/*----------------------------------------------------------------*/
	/* Local Variables                                                */
	/*----------------------------------------------------------------*/

	/*----------------------------------------------------------------*/
	/* Code Body                                                      */
	/*----------------------------------------------------------------*/
	g_srv_sensor_motion_cntx.is_shake_mute = MMI_FALSE;
}



/* Transfer MTK drv data to mCube data */
void srv_sensor_motion_get_acc(S16 acc_x, S16 acc_y, S16 acc_z,
							   float *x, float *y, float *z)
{
	*x = -1.0 * (float)acc_x * 9.807 / 1000.0 ;             //milli-g to m/s2
	*y = -1.0 * (float)acc_y * 9.807 / 1000.0  ;            //milli-g to m/s2
	*z = -1.0 * (float)acc_z * 9.807 / 1000.0 ;             //milli-g to m/s2
}


/*****************************************************************************
* FUNCTION
*  srv_sensor_motion_power_off
* DESCRIPTION
*  power off
* PARAMETERS
*  void
* RETURNS
*  MDI_RESULT
*****************************************************************************/
static SRV_SENSOR_RESULT srv_sensor_motion_power_off(void)
{
	/*----------------------------------------------------------------*/
	/* Local Variables                                                */
	/*----------------------------------------------------------------*/

	/*----------------------------------------------------------------*/
	/* Code Body                                                      */
	/*----------------------------------------------------------------*/
#ifdef MDI_MOTION_DRIVER_AVAIALBE
	motion_sensor_power(MMI_FALSE);
	g_is_power_on = MMI_FALSE;

	mmi_frm_cb_dereg_event(EVT_ID_GPIO_LCD_SLEEP_IN,
		power_off_sensor,
		NULL);

	mmi_frm_cb_dereg_event(EVT_ID_GPIO_LCD_SLEEP_OUT,
		power_on_sensor,
		NULL);

#endif /* MDI_MOTION_DRIVER_AVAIALBE */ 
	return MDI_RES_MOTION_SUCCEED;
}



/*****************************************************************************
* FUNCTION
*  srv_sensor_motion_start_listen_shake
* DESCRIPTION
*  listen to shake event
* PARAMETERS
*  sensitive                   [IN]        sensitive
*  shake_event_callback        [IN]        Event callback
* RETURNS
*  MDI_RESULT
*****************************************************************************/
SRV_SENSOR_HANDLE srv_sensor_start_listen(srv_sensor_type_enum type, void *option, 
										  SRV_SENSOR_EVENT_HDLR event_hdlr, void *user_data)
{
	/*----------------------------------------------------------------*/
	/* Local Variables                                                */
	/*----------------------------------------------------------------*/
	SRV_SENSOR_HANDLE  return_value = SRV_SENSOR_MOTION_INVALID_PARAM;
	U8 sen;
  #ifdef __MTK_TARGET__
	mCubeLibResult_t mcube_open_result;
  #endif
	mCubeLibInit_t initData;

	U16 ms_gest_type;

	srv_sensor_motion_tilt_sensitive_enum tilt_mode = SRV_SENSOR_MOTION_RAW_TILT;

	srv_sensor_motion_step_sensitive_enum step_mode = SRV_SENSOR_MOTION_STEP_MEDIUM;

	/*----------------------------------------------------------------*/
	/* Code Body                                                      */
	/*----------------------------------------------------------------*/       
	if (g_sub_init == MMI_TRUE)
	{
		srv_sensor_motion_sub_init();
		g_sub_init = MMI_FALSE;
	}

	initData.accelInit.accelOrientation = CHIP_TOP_RIGHT_DOWN;
	initData.accelInit.accelPolaBafrInit.enableUpdate = MMI_TRUE;
	initData.accelInit.accelPolaBafrInit.polaUpdRateHz = 10;

	initData.accelInit.accelPolaBafrInit.flipCntrl.flipEnable = g_mcube_init.flipEnable ;
	initData.accelInit.accelPolaBafrInit.flipCntrl.flipFilter = g_mcube_init.flipFilter;
	initData.accelInit.accelPolaBafrInit.flipCntrl.flipThreshold = g_mcube_init.flipThreshold;
	initData.accelInit.accelPolaBafrInit.flipCntrl.tFlipDebounce = g_mcube_init.tFlipDebounce ;
	initData.accelInit.accelPolaBafrInit.flipCntrl.tFlipInterval = g_mcube_init.tFlipInterval;

    initData.stepInit.stepSensitivity = STEP_SENS_LOW;  /* Default step sens is low */
	
	//mcube_open_result = mCube_LibOpen(&initData);
	//mcube_open_result = mCube_LibOpen();

#ifdef MDI_MOTION_DRIVER_AVAIALBE

	if (type == SRV_SENSOR_MOTION_TAP || type == SRV_SENSOR_MOTION_DOUBLE_TAP 
		|| type == SRV_SENSOR_MOTION_STEP || type == SRV_SENSOR_MOTION_DROP)
	{

		if (type == SRV_SENSOR_MOTION_TAP || type == SRV_SENSOR_MOTION_DOUBLE_TAP)
		{
			ms_gest_type = MS_TAP;
		}

		//#ifdef __MOTION_SENSOR_ADVANCED_GESTURE__
		else if (type == SRV_SENSOR_MOTION_STEP)
		{
			ms_gest_type = MS_STEP;
		}
		//#endif

		else if (type == SRV_SENSOR_MOTION_DROP)
		{
			ms_gest_type = MS_DROP;
		}


		if (motion_sensor_query_gesture(ms_gest_type) == MMI_FALSE)
		{
			return SRV_SENSOR_MOTION_NOT_SUPPORT_GESTURE;
		}
	}

#endif /*MDI_MOTION_DRIVER_AVAIALBE*/

	switch (type)
	{
	case SRV_SENSOR_MOTION_SHAKE:
		{

			MMI_ASSERT(event_hdlr != NULL);
			sen = ((srv_sensor_motion_shake_cfg_struct *)option)->sensitive;
			return_value = srv_sensor_motion_add_to_slot(SRV_SENSOR_MOTION_SHAKE, 
				sen , event_hdlr, NULL,user_data);
			break;

		}
	case SRV_SENSOR_MOTION_DIRECT:
		{
			MMI_ASSERT(event_hdlr != NULL);
			sen = ((srv_sensor_motion_direct_cfg_struct *)option)->angle_threshold;
			return_value = srv_sensor_motion_add_to_slot(SRV_SENSOR_MOTION_DIRECT,
				sen, event_hdlr, NULL, user_data);
			break;
		}
	case SRV_SENSOR_MOTION_TILT:
		{
			MMI_ASSERT(event_hdlr != NULL);
			if (option != NULL)
			    tilt_mode = ((srv_sensor_motion_tilt_cfg_struct *)option)->sensitive;
			return_value = srv_sensor_motion_add_to_slot(SRV_SENSOR_MOTION_TILT,
				tilt_mode, event_hdlr, NULL, user_data);
			break;
		}


		/* For mCube library */


	case SRV_SENSOR_MOTION_TAP:

		MMI_ASSERT(event_hdlr != NULL);

#ifdef __MTK_TARGET__

		mcube_open_result = mCube_LibOpen(&initData);

#endif

		return_value = srv_sensor_motion_add_to_slot(SRV_SENSOR_MOTION_TAP,
			0,event_hdlr, NULL, user_data);

		break;
	case SRV_SENSOR_MOTION_DOUBLE_TAP:
		MMI_ASSERT(event_hdlr != NULL);

#ifdef __MTK_TARGET__
		mcube_open_result = mCube_LibOpen(&initData);
#endif

		return_value = srv_sensor_motion_add_to_slot(SRV_SENSOR_MOTION_DOUBLE_TAP,
			0,event_hdlr, NULL, user_data);
		break;

	case SRV_SENSOR_MOTION_DROP:
		MMI_ASSERT(event_hdlr != NULL);

#ifdef __MTK_TARGET__
		mcube_open_result = mCube_LibOpen(&initData);
#endif

		//kal_prompt_trace(MOD_MMI,"[mcube]Open %d",mcube_open_result);

		return_value = srv_sensor_motion_add_to_slot(SRV_SENSOR_MOTION_DROP,
			0,event_hdlr, NULL, user_data);
		break;

	case SRV_SENSOR_MOTION_ADVANCED_SHAKE:
		MMI_ASSERT(event_hdlr != NULL);

#ifdef __MTK_TARGET__
		mcube_open_result = mCube_LibOpen(&initData);
#endif
		//mcube_open_result = mCube_LibOpen();

		//kal_prompt_trace(MOD_MMI,"[mcube]Open %d",mcube_open_result);

		return_value = srv_sensor_motion_add_to_slot(SRV_SENSOR_MOTION_ADVANCED_SHAKE,
			0,event_hdlr, NULL, user_data);
		break;

	case SRV_SENSOR_MOTION_TURNOVER:
		MMI_ASSERT(event_hdlr != NULL);

#ifdef __MTK_TARGET__
		mcube_open_result = mCube_LibOpen(&initData);
#endif

		return_value = srv_sensor_motion_add_to_slot(SRV_SENSOR_MOTION_TURNOVER,
			0,event_hdlr, NULL, user_data);

#ifdef __MTK_TARGET__
		mcube_open_result = mCube_LibOpen(&initData);

#endif
		//mcube_open_result = mCube_LibOpen();
		//kal_prompt_trace(MOD_MMI,"[mcube]Open %d",mcube_open_result);

		break;

		//#ifdef __MOTION_SENSOR_ADVANCED_GESTURE__	
	case SRV_SENSOR_MOTION_STEP:
		MMI_ASSERT(event_hdlr != NULL);

#ifdef __MTK_TARGET__
		mcube_open_result = mCube_LibOpen(&initData);

#endif

		//kal_prompt_trace(MOD_MMI,"[mcube]Open %d",mcube_open_result);

        if (option != NULL)
        {
            step_mode = ((srv_sensor_motion_step_cfg_struct *)option)->sensitive;
        }
        
		return_value = srv_sensor_motion_add_to_slot(SRV_SENSOR_MOTION_STEP,
			step_mode,event_hdlr, NULL, user_data);

		kal_prompt_trace(MOD_MMI,"[mcube step]step_mode=%d",step_mode);
		
		break;
		//#endif

	default :
		break;
	}

	return return_value;
}


/*****************************************************************************
* FUNCTION
*  srv_sensor_motion_stop_listen_shake
* DESCRIPTION
*  stop listen shake operation
* PARAMETERS
*  *callback       [IN]    the callback of application
* RETURNS
*  MDI_RESULT
*****************************************************************************/
SRV_SENSOR_RESULT srv_sensor_stop_listen(SRV_SENSOR_HANDLE handle)
{
	/*----------------------------------------------------------------*/
	/* Local Variables                                                */
	/*----------------------------------------------------------------*/
	SRV_SENSOR_RESULT  return_value = SRV_SENSOR_MOTION_INVALID_PARAM;
	SRV_SENSOR_HANDLE   internal_handle = 0;
	srv_sensor_type_enum  type;
	// U8 i;

	/*----------------------------------------------------------------*/
	/* Code Body                                                      */
	/*----------------------------------------------------------------*/
	if (handle>45 || handle<1)
		return SRV_SENSOR_MOTION_INVALID_PARAM;

//	kal_prompt_trace(MOD_MMI,"[mcube]srv_sensor_stop_listen");

	srv_sensor_motion_get_internal_handle(handle, &internal_handle, &type);

	//if(handle < MOTION_SUPPORT_APP_NUM)

	kal_prompt_trace(MOD_MMI,"[mcube]type %d,internal handle %d",
		type, internal_handle);

	srv_sensor_motion_remove_mode(handle);


	return_value = srv_sensor_motion_remove_from_slot(type, internal_handle);
	return return_value;
}


/*****************************************************************************
* FUNCTION
*  srv_sensor_motion_get_external_handle
* DESCRIPTION
*  
* PARAMETERS
*
* RETURNS
*  MDI_RESULT
*****************************************************************************/
static SRV_SENSOR_HANDLE srv_sensor_motion_get_external_handle(srv_sensor_type_enum type,SRV_SENSOR_HANDLE handle)
{
	/*----------------------------------------------------------------*/
	/* Local Variables                                                */
	/*----------------------------------------------------------------*/

	U8 i;
	/*----------------------------------------------------------------*/
	/* Code Body                                                      */
	/*----------------------------------------------------------------*/
	for(i=0; i<HANDLE_TAB_SIZE; i++)
	{
		if (type == handle_tab[i].app_type && handle == handle_tab[i].internal_handle)
			return handle_tab[i].external_handle;
	}

	return SRV_SENSOR_MOTION_NO_SLOT;
}

/*****************************************************************************
* FUNCTION
*  srv_sensor_motion_get_external_handle
* DESCRIPTION
*  
* PARAMETERS
*
* RETURNS
*  MDI_RESULT
*****************************************************************************/
static void srv_sensor_motion_get_internal_handle(SRV_SENSOR_HANDLE handle, SRV_SENSOR_HANDLE *internal_handle, srv_sensor_type_enum *type)
{
	/*----------------------------------------------------------------*/
	/* Local Variables                                                */
	/*----------------------------------------------------------------*/
	U8 i;
	/*----------------------------------------------------------------*/
	/* Code Body                                                      */
	/*----------------------------------------------------------------*/
	for(i=0; i<HANDLE_TAB_SIZE; i++)
	{
		if (handle == handle_tab[i].external_handle)
		{
			*type =  handle_tab[i].app_type;
			*internal_handle = handle_tab[i].internal_handle;
			break;
		}
	}

}


static MMI_BOOL srv_sensor_no_have_mcube_reg()
{
	/*----------------------------------------------------------------*/
	/* Local Variables                                                */
	/*----------------------------------------------------------------*/
	U32 i = 0;

	/*----------------------------------------------------------------*/
	/* Code Body                                                      */
	/*----------------------------------------------------------------*/

	for (i = 0; i < MOTION_SUPPORT_APP_NUM; i++)
	{
		if (g_srv_sensor_motion_cntx.app[i].double_tap_callback != NULL ||
			g_srv_sensor_motion_cntx.app[i].advanced_shake_callback != NULL ||
			g_srv_sensor_motion_cntx.app[i].tap_callback != NULL ||
			g_srv_sensor_motion_cntx.app[i].turnover_callback != NULL ||
			g_srv_sensor_motion_cntx.app[i].drop_callback != NULL ||
			g_srv_sensor_motion_cntx.app[i].step_callback != NULL)
		{
			return MMI_FALSE;
		}
	}
	return MMI_TRUE;
}

static srv_sensor_motion_set_step_sens(U8 sens)
{
	/*----------------------------------------------------------------*/
	/* Local Variables                                                */
	/*----------------------------------------------------------------*/
	mCubeLibInit_t initData;
	initData.stepInit.stepSensitivity = STEP_SENS_MED;
    /* other */
	initData.accelInit.accelOrientation = CHIP_TOP_RIGHT_DOWN;
	initData.accelInit.accelPolaBafrInit.enableUpdate = MMI_TRUE;
	initData.accelInit.accelPolaBafrInit.polaUpdRateHz = 10;

	initData.accelInit.accelPolaBafrInit.flipCntrl.flipEnable = g_mcube_init.flipEnable ;
	initData.accelInit.accelPolaBafrInit.flipCntrl.flipFilter = g_mcube_init.flipFilter;
	initData.accelInit.accelPolaBafrInit.flipCntrl.flipThreshold = g_mcube_init.flipThreshold;
	initData.accelInit.accelPolaBafrInit.flipCntrl.tFlipDebounce = g_mcube_init.tFlipDebounce ;
	initData.accelInit.accelPolaBafrInit.flipCntrl.tFlipInterval = g_mcube_init.tFlipInterval;

	/*----------------------------------------------------------------*/
	/* Code Body                                                      */
	/*----------------------------------------------------------------*/

    switch ((srv_sensor_motion_step_sensitive_enum)sens)
    {
       case SRV_SENSOR_MOTION_STEP_HIGH:
	   	initData.stepInit.stepSensitivity = STEP_SENS_HIGH;
	   	break;
       case SRV_SENSOR_MOTION_STEP_MEDIUM:
	   	initData.stepInit.stepSensitivity = STEP_SENS_MED;
	   	break;
       case SRV_SENSOR_MOTION_STEP_LOW:
	   	initData.stepInit.stepSensitivity = STEP_SENS_LOW;
	   	break;
    }

	
	kal_prompt_trace(MOD_MMI,"[mcube step]initData.stepInit.stepSensitivity = %d",initData.stepInit.stepSensitivity);

	
   #ifdef __MTK_TARGET__
		mCube_LibClose();
		mCube_LibOpen(&initData);
   #endif
}

/*****************************************************************************
* FUNCTION
*  srv_sensor_motion_add_to_slot
* DESCRIPTION
*  add request to the slot
* PARAMETERS
*  type                    [IN]        request type
*  sensitive               [IN]        sensitive
*  *callback               [IN]        callback pointer to the app function
*  *param                  [IN]        param to the slot add function
* RETURNS
*  MDI_RESULT
*****************************************************************************/
static SRV_SENSOR_RESULT srv_sensor_motion_add_to_slot(srv_sensor_type_enum type,
													   U8 sensitive,void(* callback)(),void * param,void * user_data)
{
	/*----------------------------------------------------------------*/
	/* Local Variables                                                */
	/*----------------------------------------------------------------*/
	module_type module_id;
	//srv_sensor_motion_tilt_event_enum   *tilt_indcation = param;
	SRV_SENSOR_HANDLE  return_value = SRV_SENSOR_MOTION_NO_SLOT;
	SRV_SENSOR_HANDLE  res_handle;
	U32 i = 0;

	/*----------------------------------------------------------------*/
	/* Code Body                                                      */
	/*----------------------------------------------------------------*/
	MMI_ASSERT(callback != NULL);

	module_id = stack_get_active_module_id();
	MMI_TRACE(MMI_MEDIA_TRC_G10_MMI_PRINT, MMI_TRC_MOTION_ADD_TO_SLOT, module_id, type, callback);

	kal_prompt_trace(MOD_MMI,"Motion sensor: Type = %d",type);
	MOTION_ENTER_PROTECT;    
	/*check if this is the first request*/
	for (i = 0; i < MOTION_SUPPORT_APP_NUM; i++)
	{
		if (MOD_NIL != g_srv_sensor_motion_cntx.app[i].mod_id)
		{
			break;
		}
	}
	if (i == MOTION_SUPPORT_APP_NUM  && g_motion_sensor_able == KAL_TRUE)		/* Is the first request and setting enable sensor mode */
	{
		MMI_TRACE(MMI_MEDIA_TRC_G10_MMI_PRINT, MMI_TRC_MOTION_POWER_ON);
		srv_sensor_motion_power_on();
		g_srv_sensor_motion_cntx.is_shake_mute = MMI_TRUE;
		gui_start_timer(MOTION_SHAKE_DET_MUTE_DURATION, srv_sensor_motion_shake_det_mute_timeout);
		g_srv_sensor_motion_cntx.is_msg_send = MMI_FALSE;
#ifdef MDI_MOTION_DRIVER_AVAIALBE
		/* register callback to retrive data */
		motion_sensor_cb_registration(srv_sensor_motion_driver_data_state_hdlr, NULL);
		SetProtocolEventHandler(srv_sensor_motion_data_hdlr, MSG_ID_MDI_MOTION_DATA_STATE_RSP);
		motion_sensor_conf_filter(MOTION_THRESHOLD_FOR_DRV);
		/* star sampling */
		motion_sensor_sample(MMI_TRUE);

#endif /* MDI_MOTION_DRIVER_AVAIALBE */ 
	}

	/*check if another mode have open at same module*/
	for (i = 0; i < MOTION_SUPPORT_APP_NUM; i++)
	{
		if (type == SRV_SENSOR_MOTION_SHAKE &&
			module_id == g_srv_sensor_motion_cntx.app[i].mod_id &&
			NULL == g_srv_sensor_motion_cntx.app[i].shake_callback)
		{
			g_srv_sensor_motion_cntx.app[i].shake_sen.sensitive  = (srv_sensor_motion_shake_sensitive_enum)sensitive;
			g_srv_sensor_motion_cntx.app[i].shake_callback = (SRV_SENSOR_EVENT_HDLR)callback;
			g_srv_sensor_motion_cntx.app[i].shake_user_data= user_data;
			g_srv_sensor_motion_cntx.app[i].mod_id = module_id;
			g_srv_sensor_motion_cntx.app[i].is_shake = MMI_TRUE;
			MMI_TRACE(MMI_MEDIA_TRC_G10_MMI_PRINT, MMI_TRC_MOTION_SHAKE_FIND_TILT_ADD_OK, i, module_id);
			return_value = i;
			break;
		}
		else if(type == SRV_SENSOR_MOTION_DIRECT && 
			module_id == g_srv_sensor_motion_cntx.app[i].mod_id &&
			NULL == g_srv_sensor_motion_cntx.app[i].direct_callback)
		{
			MMI_TRACE(MMI_MEDIA_TRC_G10_MMI_PRINT, MMI_TRC_MOTION_TILT_FIND_SHAKE_ADD_OK, i, module_id);
			g_srv_sensor_motion_cntx.app[i].direct_sen.angle_threshold = sensitive;
			g_srv_sensor_motion_cntx.app[i].direct_callback = (SRV_SENSOR_EVENT_HDLR)callback;
			g_srv_sensor_motion_cntx.app[i].direct_user_data= user_data;
			g_srv_sensor_motion_cntx.app[i].mod_id = module_id;
			g_srv_sensor_motion_cntx.app[i].is_direct = MMI_TRUE;
			srv_sensor_motion_reset_direct_data(&g_srv_sensor_motion_cntx.app[i].direct);
			return_value = i;
			break;
		}
		else if(type == SRV_SENSOR_MOTION_TILT && 
			module_id == g_srv_sensor_motion_cntx.app[i].mod_id &&
			NULL == g_srv_sensor_motion_cntx.app[i].tilt_callback)
		{

			MMI_TRACE(MMI_MEDIA_TRC_G10_MMI_PRINT, MMI_TRC_MOTION_TILT_FIND_SHAKE_ADD_OK, i, module_id);
			g_srv_sensor_motion_cntx.app[i].tilt_sen.sensitive = (srv_sensor_motion_tilt_sensitive_enum)sensitive;
			g_srv_sensor_motion_cntx.app[i].tilt_callback = (SRV_SENSOR_EVENT_HDLR)callback;
			g_srv_sensor_motion_cntx.app[i].tilt_user_data = user_data;
			g_srv_sensor_motion_cntx.app[i].mod_id = module_id;
			g_srv_sensor_motion_cntx.app[i].is_tilt = MMI_TRUE;
			return_value = i;
			break;
		}       

		/* For porting mCude library */



		/* Tap */

		else if (type == SRV_SENSOR_MOTION_TAP && 
			module_id == g_srv_sensor_motion_cntx.app[i].mod_id &&
			NULL == g_srv_sensor_motion_cntx.app[i].tap_callback)
		{			
			kal_prompt_trace(MOD_MMI,"[mcube] Register tap(another mode)");
			g_srv_sensor_motion_cntx.app[i].tap_callback = (SRV_SENSOR_EVENT_HDLR)callback;
			g_srv_sensor_motion_cntx.app[i].tap_user_data = user_data;
			g_srv_sensor_motion_cntx.app[i].mod_id = module_id;
			g_srv_sensor_motion_cntx.app[i].is_tap = MMI_TRUE;
			return_value = i;
			g_is_reg_mcube = MMI_TRUE;
			break;
		}


		/*Double Tap*/
		else if (type == SRV_SENSOR_MOTION_DOUBLE_TAP && 
			module_id == g_srv_sensor_motion_cntx.app[i].mod_id &&
			NULL == g_srv_sensor_motion_cntx.app[i].double_tap_callback)
		{			
			kal_prompt_trace(MOD_MMI,"[mcube] Register double tap(another mode)");
			g_srv_sensor_motion_cntx.app[i].double_tap_callback = (SRV_SENSOR_EVENT_HDLR)callback;
			g_srv_sensor_motion_cntx.app[i].double_tap_user_data = user_data;
			g_srv_sensor_motion_cntx.app[i].mod_id = module_id;
			g_srv_sensor_motion_cntx.app[i].is_double_tap = MMI_TRUE;
			return_value = i;
			g_is_reg_mcube = MMI_TRUE;
			break;
		}

		/* Advanced shake */

		else if (type == SRV_SENSOR_MOTION_ADVANCED_SHAKE &&
			module_id == g_srv_sensor_motion_cntx.app[i].mod_id &&
			NULL == g_srv_sensor_motion_cntx.app[i].advanced_shake_callback)

		{			
			kal_prompt_trace(MOD_MMI,"[mcube] Register advanced shake(another mode)");
			g_srv_sensor_motion_cntx.app[i].advanced_shake_callback = (SRV_SENSOR_EVENT_HDLR)callback;
			g_srv_sensor_motion_cntx.app[i].advanced_shake_user_data = user_data;
			g_srv_sensor_motion_cntx.app[i].mod_id = module_id;
			g_srv_sensor_motion_cntx.app[i].is_advanced_shake = MMI_TRUE;
			return_value = i;
			g_is_reg_mcube = MMI_TRUE;
			break;
		}

		/*Turn over*/
		else if (type == SRV_SENSOR_MOTION_TURNOVER &&
			module_id == g_srv_sensor_motion_cntx.app[i].mod_id &&
			NULL == g_srv_sensor_motion_cntx.app[i].turnover_callback)
		{
			kal_prompt_trace(MOD_MMI,"[mcube] Register turn over(another mode)");
			g_srv_sensor_motion_cntx.app[i].turnover_callback = (SRV_SENSOR_EVENT_HDLR)callback;
			g_srv_sensor_motion_cntx.app[i].turnover_user_data = user_data;
			g_srv_sensor_motion_cntx.app[i].mod_id = module_id;
			g_srv_sensor_motion_cntx.app[i].is_turnover = MMI_TRUE;
			return_value = i;
			g_is_reg_mcube = MMI_TRUE;
			break;
		}

		//#ifdef __MOTION_SENSOR_ADVANCED_GESTURE__
		/* Step */
		else if (type == SRV_SENSOR_MOTION_STEP &&
			module_id == g_srv_sensor_motion_cntx.app[i].mod_id &&
			NULL == g_srv_sensor_motion_cntx.app[i].step_callback)
		{
			MMI_TRACE(MMI_MEDIA_TRC_G10_MMI_PRINT, MMI_TRC_MOTION_TILT_FIND_SHAKE_ADD_OK, i, module_id);
			g_srv_sensor_motion_cntx.app[i].step_callback = (SRV_SENSOR_EVENT_HDLR)callback;
			g_srv_sensor_motion_cntx.app[i].step_user_data = user_data;
			g_srv_sensor_motion_cntx.app[i].mod_id = module_id;
			g_srv_sensor_motion_cntx.app[i].is_step = MMI_TRUE;
			return_value = i;
			g_is_reg_mcube = MMI_TRUE;
			srv_sensor_motion_set_step_sens(sensitive);
			break;
		}
		//#endif /*__MOTION_SENSOR_ADVANCED_GESTURE__*/

		/* Drop */
		else if (type == SRV_SENSOR_MOTION_DROP &&
			module_id == g_srv_sensor_motion_cntx.app[i].mod_id &&
			NULL == g_srv_sensor_motion_cntx.app[i].drop_callback)
		{
			MMI_TRACE(MMI_MEDIA_TRC_G10_MMI_PRINT, MMI_TRC_MOTION_TILT_FIND_SHAKE_ADD_OK, i, module_id);
			g_srv_sensor_motion_cntx.app[i].drop_callback = (SRV_SENSOR_EVENT_HDLR)callback;
			g_srv_sensor_motion_cntx.app[i].drop_user_data = user_data;
			g_srv_sensor_motion_cntx.app[i].mod_id = module_id;
			g_srv_sensor_motion_cntx.app[i].is_drop = MMI_TRUE;
			return_value = i;
			g_is_reg_mcube = MMI_TRUE;
			break;
		}


	}
	/*find empty slot*/
	if (i == MOTION_SUPPORT_APP_NUM)
	{
		for (i = 0; i < MOTION_SUPPORT_APP_NUM; i++)
		{
			if (type == SRV_SENSOR_MOTION_SHAKE &&
				MOD_NIL == g_srv_sensor_motion_cntx.app[i].mod_id &&
				NULL == g_srv_sensor_motion_cntx.app[i].shake_callback)
			{
				MMI_TRACE(MMI_MEDIA_TRC_G10_MMI_PRINT, MMI_TRC_MOTION_SHAKE_FIND_EMPTY_SLOT, i, module_id);
				g_srv_sensor_motion_cntx.app[i].shake_callback = (SRV_SENSOR_EVENT_HDLR)callback;
				g_srv_sensor_motion_cntx.app[i].mod_id = module_id;
				g_srv_sensor_motion_cntx.app[i].shake_sen.sensitive = (srv_sensor_motion_shake_sensitive_enum)sensitive;
				g_srv_sensor_motion_cntx.app[i].shake_user_data= user_data;
				g_srv_sensor_motion_cntx.app[i].is_shake = MMI_TRUE;
				return_value = i;
				break;
			}
			else if(type == SRV_SENSOR_MOTION_DIRECT && 
				MOD_NIL == g_srv_sensor_motion_cntx.app[i].mod_id &&
				NULL == g_srv_sensor_motion_cntx.app[i].direct_callback)
			{
				MMI_TRACE(MMI_MEDIA_TRC_G10_MMI_PRINT, MMI_TRC_MOTION_TILT_FIND_EMPTY_SLOT, i, module_id);
				g_srv_sensor_motion_cntx.app[i].direct_callback = (SRV_SENSOR_EVENT_HDLR)callback;
				g_srv_sensor_motion_cntx.app[i].mod_id = module_id;
				g_srv_sensor_motion_cntx.app[i].direct_sen.angle_threshold = sensitive;
				g_srv_sensor_motion_cntx.app[i].direct_user_data= user_data;
				g_srv_sensor_motion_cntx.app[i].is_direct = MMI_TRUE;
				srv_sensor_motion_reset_direct_data(&g_srv_sensor_motion_cntx.app[i].direct);
				return_value = i;
				break;
			}
			else if(type == SRV_SENSOR_MOTION_TILT && 
				MOD_NIL == g_srv_sensor_motion_cntx.app[i].mod_id &&
				NULL == g_srv_sensor_motion_cntx.app[i].tilt_callback)
			{
				MMI_TRACE(MMI_MEDIA_TRC_G10_MMI_PRINT, MMI_TRC_MOTION_TILT_FIND_SHAKE_ADD_OK, i, module_id);
				g_srv_sensor_motion_cntx.app[i].tilt_sen.sensitive = (srv_sensor_motion_tilt_sensitive_enum)sensitive;
				g_srv_sensor_motion_cntx.app[i].tilt_callback = (SRV_SENSOR_EVENT_HDLR)callback;
				g_srv_sensor_motion_cntx.app[i].tilt_user_data= user_data;
				g_srv_sensor_motion_cntx.app[i].mod_id = module_id;
				g_srv_sensor_motion_cntx.app[i].is_tilt = MMI_TRUE;
				return_value = i;
				break;
			}


			/* For porting mCude library */



			/* Tap */

			else if (type == SRV_SENSOR_MOTION_TAP && 
				MOD_NIL == g_srv_sensor_motion_cntx.app[i].mod_id &&
				NULL == g_srv_sensor_motion_cntx.app[i].tap_callback)
			{			
				kal_prompt_trace(MOD_MMI,"[mcube] Register tap and add to slot");
				g_srv_sensor_motion_cntx.app[i].tap_callback = (SRV_SENSOR_EVENT_HDLR)callback;
				g_srv_sensor_motion_cntx.app[i].tap_user_data = user_data;
				g_srv_sensor_motion_cntx.app[i].mod_id = module_id;
				g_srv_sensor_motion_cntx.app[i].is_tap = MMI_TRUE;
				return_value = i;
				g_is_reg_mcube = MMI_TRUE;
				break;
			}

			/* Advanced shake */

			else if (type == SRV_SENSOR_MOTION_ADVANCED_SHAKE &&
				MOD_NIL == g_srv_sensor_motion_cntx.app[i].mod_id &&
				NULL == g_srv_sensor_motion_cntx.app[i].advanced_shake_callback)
			{			
				kal_prompt_trace(MOD_MMI,"[mcube] Register advanced shake and add to slot");
				g_srv_sensor_motion_cntx.app[i].advanced_shake_callback = (SRV_SENSOR_EVENT_HDLR)callback;
				g_srv_sensor_motion_cntx.app[i].advanced_shake_user_data = user_data;
				g_srv_sensor_motion_cntx.app[i].mod_id = module_id;
				g_srv_sensor_motion_cntx.app[i].is_advanced_shake = MMI_TRUE;
				return_value = i;
				g_is_reg_mcube = MMI_TRUE;
				break;
			}

			/* Double tap */

			else if (type == SRV_SENSOR_MOTION_DOUBLE_TAP && 
				MOD_NIL == g_srv_sensor_motion_cntx.app[i].mod_id &&
				NULL == g_srv_sensor_motion_cntx.app[i].double_tap_callback)
			{			
				kal_prompt_trace(MOD_MMI,"[mcube] Register double tap and add to slot");
				g_srv_sensor_motion_cntx.app[i].double_tap_callback = (SRV_SENSOR_EVENT_HDLR)callback;
				g_srv_sensor_motion_cntx.app[i].double_tap_user_data = user_data;
				g_srv_sensor_motion_cntx.app[i].mod_id = module_id;
				g_srv_sensor_motion_cntx.app[i].is_double_tap = MMI_TRUE;
				return_value = i;
				g_is_reg_mcube = MMI_TRUE;
				break;
			}

			/*Turn over*/

			else if (type == SRV_SENSOR_MOTION_TURNOVER &&
				MOD_NIL == g_srv_sensor_motion_cntx.app[i].mod_id &&
				NULL == g_srv_sensor_motion_cntx.app[i].turnover_callback)
			{
				kal_prompt_trace(MOD_MMI,"[mcube] Register turnover and add to slot");
				g_srv_sensor_motion_cntx.app[i].turnover_callback = (SRV_SENSOR_EVENT_HDLR)callback;
				g_srv_sensor_motion_cntx.app[i].turnover_user_data = user_data;
				g_srv_sensor_motion_cntx.app[i].mod_id = module_id;
				g_srv_sensor_motion_cntx.app[i].is_turnover = MMI_TRUE;
				return_value = i;
				g_is_reg_mcube = MMI_TRUE;
				break;
			}
			//#ifdef __MOTION_SENSOR_ADVANCED_GESTURE__

			/* Step */
			else if (type == SRV_SENSOR_MOTION_STEP &&
				MOD_NIL == g_srv_sensor_motion_cntx.app[i].mod_id &&
				NULL == g_srv_sensor_motion_cntx.app[i].step_callback)
			{
				kal_prompt_trace(MOD_MMI,"[mcube] Register step and add to slot");
				g_srv_sensor_motion_cntx.app[i].step_callback = (SRV_SENSOR_EVENT_HDLR)callback;
				g_srv_sensor_motion_cntx.app[i].step_user_data = user_data;
				g_srv_sensor_motion_cntx.app[i].mod_id = module_id;
				g_srv_sensor_motion_cntx.app[i].is_step = MMI_TRUE;
				return_value = i;
				g_is_reg_mcube = MMI_TRUE;
				srv_sensor_motion_set_step_sens(sensitive);
				break;
			}

			//#endif /*__MOTION_SENSOR_ADVANCED_GESTURE__*/

			/* Drop */
			else if (type == SRV_SENSOR_MOTION_DROP &&
				MOD_NIL == g_srv_sensor_motion_cntx.app[i].mod_id &&
				NULL == g_srv_sensor_motion_cntx.app[i].drop_callback)
			{
			//	kal_prompt_trace(MOD_MMI,"[mcube] Register drop and add to slot");
				g_srv_sensor_motion_cntx.app[i].drop_callback = (SRV_SENSOR_EVENT_HDLR)callback;
				g_srv_sensor_motion_cntx.app[i].drop_user_data = user_data;
				g_srv_sensor_motion_cntx.app[i].mod_id = module_id;
				g_srv_sensor_motion_cntx.app[i].is_drop = MMI_TRUE;
				return_value = i;
				g_is_reg_mcube = MMI_TRUE;
				break;
			}




		}
	}

	MMI_ASSERT(i>=0);
	res_handle = srv_sensor_motion_get_external_handle(type,return_value);
	if (i == MOTION_SUPPORT_APP_NUM)
	{
		res_handle = SRV_SENSOR_MOTION_NO_SLOT;
	}

	kal_prompt_trace(MOD_MMI,"[mcube]add external index %d",return_value);

	MOTION_EXIT_PROTECT;
	return res_handle;

}



static U8 srv_sensor_callback_handler_count(U8 index)
{
	U8 count = 0;


	if (g_srv_sensor_motion_cntx.app[index].advanced_shake_callback != NULL)
		count++;
	if (g_srv_sensor_motion_cntx.app[index].tap_callback != NULL)
		count++;
	if (g_srv_sensor_motion_cntx.app[index].double_tap_callback != NULL)
		count++;
	//#ifdef __MOTION_SENSOR_ADVANCED_GESTURE__
	if (g_srv_sensor_motion_cntx.app[index].step_callback!= NULL)
		count++;
	//#endif /*__MOTION_SENSOR_ADVANCED_GESTURE__*/
	if (g_srv_sensor_motion_cntx.app[index].drop_callback!= NULL)
		count++;
	if (g_srv_sensor_motion_cntx.app[index].turnover_callback != NULL)
		count++;


	if (g_srv_sensor_motion_cntx.app[index].tilt_callback != NULL)
		count++;
	if (g_srv_sensor_motion_cntx.app[index].direct_callback != NULL)
		count++;
	if (g_srv_sensor_motion_cntx.app[index].shake_callback != NULL)
		count++;

	return count;
}






/*****************************************************************************
* FUNCTION
*  srv_sensor_motion_remove_from_slot
* DESCRIPTION
*  remove  a request from the slot
* PARAMETERS
*  type                [IN]        the type want to remove
*  *callback           [IN]        the callback function pointer
* RETURNS
*  MDI_RESULT
*****************************************************************************/
static SRV_SENSOR_RESULT srv_sensor_motion_remove_from_slot(srv_sensor_type_enum type,SRV_SENSOR_HANDLE index)
{
	/*----------------------------------------------------------------*/
	/* Local Variables                                                */
	/*----------------------------------------------------------------*/
	module_type module_id;
	SRV_SENSOR_RESULT  return_value = SRV_SENSOR_MOTION_CB_NOT_FOUND;
	U32 i = 0;

	/*----------------------------------------------------------------*/
	/* Code Body                                                      */
	/*----------------------------------------------------------------*/

	module_id = stack_get_active_module_id();
	MMI_TRACE(MMI_MEDIA_TRC_G10_MMI_PRINT, MMI_TRC_MOTION_REMOVE_FROM_SLOT, module_id, type, index);
	MMI_ASSERT(index >= 0);
	MMI_ASSERT(index < MOTION_SUPPORT_APP_NUM);

	kal_prompt_trace(MOD_MMI,"[mcube]remove internal index %d",index);

	MMI_ASSERT(g_srv_sensor_motion_cntx.app[index].mod_id == module_id);
	MOTION_ENTER_PROTECT;    
	if (type == SRV_SENSOR_MOTION_SHAKE)
	{
		g_srv_sensor_motion_cntx.app[index].shake_callback = NULL;
		g_srv_sensor_motion_cntx.app[index].shake_user_data= NULL;
		/*
		if (NULL == g_srv_sensor_motion_cntx.app[index].tilt_callback &&
		NULL == g_srv_sensor_motion_cntx.app[index].direct_callback)
		*/    
		if (srv_sensor_callback_handler_count(index) == 0)
		{
			g_srv_sensor_motion_cntx.app[index].mod_id = MOD_NIL;
		}
		MMI_TRACE(MMI_MEDIA_TRC_G10_MMI_PRINT, MMI_TRC_MOTION_SHAKE_REMOVE_OK, i);
		return_value = SRV_SENSOR_MOTION_SUCCEED;
	}
	else if(type == SRV_SENSOR_MOTION_TILT)
	{
		g_srv_sensor_motion_cntx.app[index].tilt_callback = NULL;
		g_srv_sensor_motion_cntx.app[index].tilt_user_data= NULL;
		/*
		if (NULL == g_srv_sensor_motion_cntx.app[index].shake_callback &&
		NULL == g_srv_sensor_motion_cntx.app[index].direct_callback)
		*/
		if (srv_sensor_callback_handler_count(index) == 0)
		{
			g_srv_sensor_motion_cntx.app[index].mod_id = MOD_NIL;
		}
		MMI_TRACE(MMI_MEDIA_TRC_G10_MMI_PRINT, MMI_TRC_MOTION_TILT_REMOVE_OK, i);
		return_value = SRV_SENSOR_MOTION_SUCCEED;
	}
	else if(type == SRV_SENSOR_MOTION_DIRECT)
	{
		g_srv_sensor_motion_cntx.app[index].direct_callback = NULL;
		g_srv_sensor_motion_cntx.app[index].direct_user_data= NULL;

		/*
		if (NULL == g_srv_sensor_motion_cntx.app[index].shake_callback &&
		NULL == g_srv_sensor_motion_cntx.app[index].tilt_callback)
		*/
		if (srv_sensor_callback_handler_count(index) == 0)
		{
			g_srv_sensor_motion_cntx.app[index].mod_id = MOD_NIL;
		}
		MMI_TRACE(MMI_MEDIA_TRC_G10_MMI_PRINT, MMI_TRC_MOTION_TILT_REMOVE_OK, i);
		return_value = SRV_SENSOR_MOTION_SUCCEED;
	}


	else if(type == SRV_SENSOR_MOTION_TAP)
	{
		g_srv_sensor_motion_cntx.app[index].tap_callback = NULL;
		g_srv_sensor_motion_cntx.app[index].tap_user_data = NULL;

		/*
		if (NULL == g_srv_sensor_motion_cntx.app[index].shake_callback &&
		NULL == g_srv_sensor_motion_cntx.app[index].tilt_callback)
		*/
		if (srv_sensor_callback_handler_count(index) == 0)
		{
			g_srv_sensor_motion_cntx.app[index].mod_id = MOD_NIL;
		}
		MMI_TRACE(MMI_MEDIA_TRC_G10_MMI_PRINT, MMI_TRC_MOTION_TILT_REMOVE_OK, i);
	//	kal_prompt_trace(MOD_MMI,"[mcube]close tap!!");
		return_value = SRV_SENSOR_MOTION_SUCCEED;
	}
	else if(type == SRV_SENSOR_MOTION_DOUBLE_TAP)
	{
		g_srv_sensor_motion_cntx.app[index].double_tap_callback = NULL;
		g_srv_sensor_motion_cntx.app[index].double_tap_user_data= NULL;

		/*
		if (NULL == g_srv_sensor_motion_cntx.app[index].shake_callback &&
		NULL == g_srv_sensor_motion_cntx.app[index].tilt_callback)
		*/
		if (srv_sensor_callback_handler_count(index) == 0)
		{
			g_srv_sensor_motion_cntx.app[index].mod_id = MOD_NIL;
		}
		MMI_TRACE(MMI_MEDIA_TRC_G10_MMI_PRINT, MMI_TRC_MOTION_TILT_REMOVE_OK, i);
	//	kal_prompt_trace(MOD_MMI,"[mcube]close double tap!!");
		return_value = SRV_SENSOR_MOTION_SUCCEED;
	}
	else if(type == SRV_SENSOR_MOTION_TURNOVER)
	{
		g_srv_sensor_motion_cntx.app[index].turnover_callback = NULL;
		g_srv_sensor_motion_cntx.app[index].turnover_user_data= NULL;

		/*
		if (NULL == g_srv_sensor_motion_cntx.app[index].shake_callback &&
		NULL == g_srv_sensor_motion_cntx.app[index].tilt_callback)
		*/
		if (srv_sensor_callback_handler_count(index) == 0)
		{
			g_srv_sensor_motion_cntx.app[index].mod_id = MOD_NIL;
		}
		MMI_TRACE(MMI_MEDIA_TRC_G10_MMI_PRINT, MMI_TRC_MOTION_TILT_REMOVE_OK, i);
	//	kal_prompt_trace(MOD_MMI,"[mcube]close turnover!!");
		return_value = SRV_SENSOR_MOTION_SUCCEED;
	}
	else if(type == SRV_SENSOR_MOTION_ADVANCED_SHAKE)
	{
		g_srv_sensor_motion_cntx.app[index].advanced_shake_callback = NULL;
		g_srv_sensor_motion_cntx.app[index].advanced_shake_user_data= NULL;

		/*
		if (NULL == g_srv_sensor_motion_cntx.app[index].shake_callback &&
		NULL == g_srv_sensor_motion_cntx.app[index].tilt_callback)
		*/
		if (srv_sensor_callback_handler_count(index) == 0)
		{
			g_srv_sensor_motion_cntx.app[index].mod_id = MOD_NIL;
		}
		MMI_TRACE(MMI_MEDIA_TRC_G10_MMI_PRINT, MMI_TRC_MOTION_TILT_REMOVE_OK, i);
	//	kal_prompt_trace(MOD_MMI,"[mcube]close advanced shake!!");
		return_value = SRV_SENSOR_MOTION_SUCCEED;
	}
	//#ifdef __MOTION_SENSOR_ADVANCED_GESTURE__
	else if(type == SRV_SENSOR_MOTION_STEP)
	{
		g_srv_sensor_motion_cntx.app[index].step_callback = NULL;
		g_srv_sensor_motion_cntx.app[index].step_user_data= NULL;

		/*
		if (NULL == g_srv_sensor_motion_cntx.app[index].shake_callback &&
		NULL == g_srv_sensor_motion_cntx.app[index].tilt_callback)
		*/
		if (srv_sensor_callback_handler_count(index) == 0)
		{
			g_srv_sensor_motion_cntx.app[index].mod_id = MOD_NIL;
		}
		MMI_TRACE(MMI_MEDIA_TRC_G10_MMI_PRINT, MMI_TRC_MOTION_TILT_REMOVE_OK, i);
	//	kal_prompt_trace(MOD_MMI,"[mcube]close step!!");
		return_value = SRV_SENSOR_MOTION_SUCCEED;
	}
	//#endif
	else if(type == SRV_SENSOR_MOTION_DROP)
	{
		g_srv_sensor_motion_cntx.app[index].drop_callback = NULL;
		g_srv_sensor_motion_cntx.app[index].drop_user_data= NULL;

		/*
		if (NULL == g_srv_sensor_motion_cntx.app[index].shake_callback &&
		NULL == g_srv_sensor_motion_cntx.app[index].tilt_callback)
		*/
		if (srv_sensor_callback_handler_count(index) == 0)
		{
			g_srv_sensor_motion_cntx.app[index].mod_id = MOD_NIL;
		}
		MMI_TRACE(MMI_MEDIA_TRC_G10_MMI_PRINT, MMI_TRC_MOTION_TILT_REMOVE_OK, i);
	//	kal_prompt_trace(MOD_MMI,"[mcube]close drop!!");
		return_value = SRV_SENSOR_MOTION_SUCCEED;
	}




	if (srv_sensor_no_have_mcube_reg())
	{
#ifdef __MTK_TARGET__
		mCube_LibClose();
#endif

		g_is_reg_mcube = MMI_FALSE;
	//	kal_prompt_trace(MOD_MMI,"[mcube]close mcube lib");
	}



	for (i = 0; i < MOTION_SUPPORT_APP_NUM; i++)
	{
		if (MOD_NIL != g_srv_sensor_motion_cntx.app[i].mod_id)
		{
			MMI_TRACE(MMI_MEDIA_TRC_G10_MMI_PRINT, MMI_TRC_MOTION_FIND_OPEN_NOT_POWER_OFF, i, 
				g_srv_sensor_motion_cntx.app[i].mod_id,
				g_srv_sensor_motion_cntx.app[i].tilt_callback,
				g_srv_sensor_motion_cntx.app[i].shake_callback);
			break;
		}
	}
	if (i == MOTION_SUPPORT_APP_NUM)
	{
		MMI_TRACE(MMI_MEDIA_TRC_G10_MMI_PRINT, MMI_TRC_MOTION_POWER_OFF_MOTION);
		SetProtocolEventHandler(NULL, MSG_ID_MDI_MOTION_DATA_STATE_RSP);
		srv_sensor_motion_power_off();  //if all request are closed , power off motion
	}
	MOTION_EXIT_PROTECT;
	MMI_TRACE(MMI_MEDIA_TRC_G10_MMI_PRINT, MMI_TRC_MOTION_REMOVE_FROM_SLOT_RETURN,return_value);
	return return_value;  
}

/*****************************************************************************
* FUNCTION
*  srv_sensor_motion_reset_tilt_data
* DESCRIPTION
*  reset tilt detect context
* PARAMETERS
*  *tilt   [IN]    the pointer to the tilt context
* RETURNS
*  void
*****************************************************************************/
static void srv_sensor_motion_reset_direct_data(srv_sensor_motion_direct_struct *tilt)
{
	/*----------------------------------------------------------------*/
	/* Local Variables                                                */
	/*----------------------------------------------------------------*/

	/*----------------------------------------------------------------*/
	/* Code Body                                                      */
	/*----------------------------------------------------------------*/
	tilt->action = SRV_SENSOR_MOTION_ACTION_NULL;
	tilt->direct = SRV_SENSOR_MOTION_ANGLE_OTHER;  
}
#ifdef MDI_MOTION_DRIVER_AVAIALBE
#pragma arm section code = "SECONDARY_ROCODE", rodata = "SECONDARY_RODATA"
/*****************************************************************************
* FUNCTION
*  srv_sensor_motion_driver_data_state_hdlr
* DESCRIPTION
*  callback function to handler driver's data state change.
*  this function will be under driver's HISR. so only send message to MMI
* PARAMETERS
*  parameter       [IN]        Para
*  state           [IN]        Data state
* RETURNS
*  void
*****************************************************************************/
static void srv_sensor_motion_driver_data_state_hdlr(void *parameter, Motion_Sensor_BuffState_enum state)
{
	/*----------------------------------------------------------------*/
	/* Local Variables                                                */
	/*----------------------------------------------------------------*/
	MYQUEUE message;
	srv_sensor_motion_msg_struct *msg_data_ptr;

	/*----------------------------------------------------------------*/
	/* Code Body                                                      */
	/*----------------------------------------------------------------*/
	if (g_srv_sensor_motion_cntx.is_msg_send == MMI_TRUE)
	{
		return;
	}
	if (state == BUFF_EMPTY)
	{
		return;
	}
	msg_data_ptr = OslConstructDataPtr(sizeof(srv_sensor_motion_msg_struct));

	switch (state)
	{
	case BUFF_DATA_EXIST:
		msg_data_ptr->data_state = MOTION_DATA_EXIST;
		break;

	case BUFF_FULL:
		msg_data_ptr->data_state = MOTION_DATA_FULL;
		break;
	}
	message.oslMsgId = MSG_ID_MDI_MOTION_DATA_STATE_RSP;
	message.oslDataPtr = (oslParaType*) msg_data_ptr;
	message.oslPeerBuffPtr = NULL;
	// message.oslSrcId = MOD_DRV_HISR;
	message.oslSrcId = stack_get_active_module_id();
	message.oslDestId = MOTION_RUN_MOD;
	OslMsgSendExtQueue(&message);
	g_srv_sensor_motion_cntx.is_msg_send = MMI_TRUE;
}
#pragma arm section code, rodata


/*****************************************************************************
* FUNCTION
*  srv_sensor_motion_data_hdlr
* DESCRIPTION
*  tilt event data handler
* PARAMETERS
*  msg_ptr     [IN]        Msg data pointer
* RETURNS
*  void
*****************************************************************************/
static void srv_sensor_motion_data_hdlr(void *msg_ptr)
{
	/*----------------------------------------------------------------*/
	/* Local Variables                                                */
	/*----------------------------------------------------------------*/
	MotionSensorDataStruct            		   ms_data[2];
	srv_sensor_motion_msg_struct               *msg_data_ptr;
	srv_sensor_motion_acc_history_struct        history;
	MYQUEUE message;
	srv_sensor_motion_action_enum         motion_action;
	srv_sensor_motion_direct_enum  		  motion_direct;
	srv_sensor_motion_shake_action_enum       	  shake;
	//module_type                         	  module_id;

	char x[20]=0;
	char y[20]=0;
	char z[20]=0;

	//kal_uint32 time_ms;
#ifdef __MTK_TARGET__
	mCubeLibResult_t mcube_result;
#endif

	//static U8 universal_filter_level = 0;
	U8 need_push_data_to_mcube = 0;       



	U8  ms_index = 0, i;
	MMI_BOOL    need_mute = MMI_FALSE, is_has_data = MMI_TRUE;

#ifdef MDI_MOTION_DRIVER_AVAIALBE

    MotionSensorQueryStruct unit;

#endif /*MDI_MOTION_DRIVER_AVAIALBE*/

	U32 threshold = 0;

	/*----------------------------------------------------------------*/
	/* Code Body                                                      */
	/*----------------------------------------------------------------*/
	msg_data_ptr = (srv_sensor_motion_msg_struct*) msg_ptr;
	if (msg_data_ptr->data_state == MOTION_DATA_EPMTY)
	{
		MMI_TRACE(MMI_MEDIA_TRC_G10_MMI_PRINT, MMI_TRC_MOTION_DATA_EMPTY);
		return;
	}
	//module_id = stack_get_active_module_id();
	/* if has buffer, get data and text if pass threshold or not */
	if ((msg_data_ptr->data_state == MOTION_DATA_EXIST) || (msg_data_ptr->data_state == MOTION_DATA_FULL))
	{
		is_has_data = motion_sensor_get_data(&ms_data[1 - ms_index]);
		MMI_TRACE(MMI_MEDIA_TRC_G10_MMI_PRINT, MMI_TRC_MOTION_TILT_GET_DATA);
		if (is_has_data == MMI_TRUE)
		{
			/*read out all the data and callback app with last one*/
			for (i = 0;i < 50; i++)
			{
				is_has_data = motion_sensor_get_data(&ms_data[ms_index]);
				MMI_TRACE(MMI_MEDIA_TRC_G10_MMI_PRINT, MMI_TRC_MOTION_TILT_GET_DATA);
				ms_index = 1 - ms_index;
				if (is_has_data != MMI_TRUE)
				{
					break;
				}
			}
			motion_sensor_flush_buff();
			/*fill acceleration data to xyz*/
			MMI_TRACE(MMI_MEDIA_TRC_G10_MMI_PRINT, MMI_TRC_MOTION_PRT_DATA_FROM_DRV,
				ms_data[ms_index].x_acc,
				ms_data[ms_index].y_acc,
				ms_data[ms_index].z_acc);
			{
				MMI_TRACE(MMI_MEDIA_TRC_G10_MMI_PRINT, MMI_TRC_MOTION_CALI_NOT_USE);
				/*driver's x y z axis is not same to MDI's*/
				g_srv_sensor_motion_cntx.detail_info.acc_x = ms_data[ms_index].x_acc;
				g_srv_sensor_motion_cntx.detail_info.acc_y = (-1) * ms_data[ms_index].y_acc;
				g_srv_sensor_motion_cntx.detail_info.acc_z = ms_data[ms_index].z_acc;
				g_srv_sensor_motion_cntx.detail_info.sensor_data.status = ms_data[ms_index].motion_info;
				g_srv_sensor_motion_cntx.detail_info.sensor_data.timeStampMs = (kal_uint64)ms_data[ms_index].timeStampMs;
			}

		//	kal_prompt_trace(MOD_MMI,"[mcube]acc_x:%d,acc_y:%d,acc_z:%d",g_srv_sensor_motion_cntx.detail_info.acc_x,
		//		g_srv_sensor_motion_cntx.detail_info.acc_y,g_srv_sensor_motion_cntx.detail_info.acc_z);

			/*push data to history for shake check*/
			history.acc_x = g_srv_sensor_motion_cntx.detail_info.acc_x;
			history.acc_y = g_srv_sensor_motion_cntx.detail_info.acc_y;
			srv_sensor_motion_shake_add_to_history(&history);       /* For shake */

			/*calc all infomation at MMI task*/
			/*calc tilt for all task here*/
			for (i = 0; i < MOTION_SUPPORT_APP_NUM; i++)
			{
				if (g_srv_sensor_motion_cntx.app[i].mod_id == MOD_NIL)
				{
					MMI_TRACE(MMI_MEDIA_TRC_G10_MMI_PRINT, MMI_TRC_MOTION_FIND_NIL_MOD_BREAK, i);
					break;
				}
				MMI_TRACE(MMI_MEDIA_TRC_G10_MMI_PRINT, MMI_TRC_MOTION_MOD_ID,g_srv_sensor_motion_cntx.app[i].mod_id);
				if (g_srv_sensor_motion_cntx.app[i].direct_callback != NULL ||
					g_srv_sensor_motion_cntx.app[i].tilt_callback != NULL)
				{
					MMI_TRACE(MMI_MEDIA_TRC_G10_MMI_PRINT, MMI_TRC_MOTION_TILT_CALC_ACTION,i);
					/*calc angle of gx gy gz*/
					MOTION_ENTER_PROTECT;
					srv_sensor_motion_calc_angle_gxyz_by_xy(
						g_srv_sensor_motion_cntx.detail_info.acc_x,
						g_srv_sensor_motion_cntx.detail_info.acc_y,
						g_srv_sensor_motion_cntx.detail_info.acc_z,
						&g_srv_sensor_motion_cntx.detail_info.angle_gx,
						&g_srv_sensor_motion_cntx.detail_info.angle_gy,
						&g_srv_sensor_motion_cntx.detail_info.angle_gz);

					/*calc direction of the motion*/
					motion_direct = srv_sensor_motion_calc_axis_direct(
						g_srv_sensor_motion_cntx.app[i].direct_sen.angle_threshold,
						g_srv_sensor_motion_cntx.detail_info.angle_gx,
						g_srv_sensor_motion_cntx.detail_info.angle_gy,
						g_srv_sensor_motion_cntx.detail_info.angle_gz,
						g_srv_sensor_motion_cntx.detail_info.acc_x,
						g_srv_sensor_motion_cntx.detail_info.acc_y);


					kal_prompt_trace(MOD_MMI,"Sensor service gx=%d,gy=%d,gz=%d",
						g_srv_sensor_motion_cntx.detail_info.angle_gx,
						g_srv_sensor_motion_cntx.detail_info.angle_gy,
						g_srv_sensor_motion_cntx.detail_info.angle_gz);

					/*calc action of the motion*/
					motion_action = srv_sensor_motion_calc_action(g_srv_sensor_motion_cntx.app[i].prev_direct,
						motion_direct);
					/* get direct tilt data for each app */
					g_srv_sensor_motion_cntx.app[i].direct.direct = motion_direct;
					g_srv_sensor_motion_cntx.app[i].direct.action = motion_action;
					g_srv_sensor_motion_cntx.app[i].tilt.angle.gx = g_srv_sensor_motion_cntx.detail_info.angle_gx;
					g_srv_sensor_motion_cntx.app[i].tilt.angle.gy = g_srv_sensor_motion_cntx.detail_info.angle_gy;
					g_srv_sensor_motion_cntx.app[i].tilt.angle.gz = g_srv_sensor_motion_cntx.detail_info.angle_gz;
					g_srv_sensor_motion_cntx.app[i].tilt.acc.x = g_srv_sensor_motion_cntx.detail_info.acc_x;
					g_srv_sensor_motion_cntx.app[i].tilt.acc.y = g_srv_sensor_motion_cntx.detail_info.acc_y;
					g_srv_sensor_motion_cntx.app[i].tilt.acc.z = g_srv_sensor_motion_cntx.detail_info.acc_z;

		

					/* If user set normal tilt (or default), it will filter noise
					this part will calculate filter noise acc_xyz*/

					if (g_srv_sensor_motion_cntx.app[i].tilt_sen.sensitive == SRV_SENSOR_MOTION_NORMAL_TILT)
					{
						#ifdef MDI_MOTION_DRIVER_AVAIALBE

						motion_sensor_get_sensor_params(MS_ACC_DATA_RESOLUTION,&unit);	/* call driver api*/
					    threshold = unit.curr_val * 3;

						#endif /*MDI_MOTION_DRIVER_AVAIALBE*/

						kal_prompt_trace(MOD_MMI,"Filter nosie: threshold = %d",threshold);

						new_x = g_srv_sensor_motion_cntx.detail_info.acc_x;
					    new_y = g_srv_sensor_motion_cntx.detail_info.acc_y;
					    new_z = g_srv_sensor_motion_cntx.detail_info.acc_z;
					
					    if (abs(old_x - new_x) <= threshold)
						    g_srv_sensor_motion_cntx.app[i].tilt.acc.x = old_x;
					    else
					    {
						    g_srv_sensor_motion_cntx.app[i].tilt.acc.x = new_x;
						    old_x = new_x;
					    }
					    if (abs(old_y - new_y) <= threshold)
						    g_srv_sensor_motion_cntx.app[i].tilt.acc.y = old_y;
					    else
					    {
						    g_srv_sensor_motion_cntx.app[i].tilt.acc.y = new_y;
					        old_y = new_y;
					    }
					    if (abs(old_z - new_z) <= threshold)
						    g_srv_sensor_motion_cntx.app[i].tilt.acc.z = old_z;
					    else
					    {
						    g_srv_sensor_motion_cntx.app[i].tilt.acc.z = new_z;
						    old_z = new_z;
					    }


					    /* Caculate angle through filter noise acc*/
					    srv_sensor_motion_calc_angle_gxyz_by_xy(
						    g_srv_sensor_motion_cntx.app[i].tilt.acc.x,
						    g_srv_sensor_motion_cntx.app[i].tilt.acc.y,
						    g_srv_sensor_motion_cntx.app[i].tilt.acc.z,
						    &g_srv_sensor_motion_cntx.app[i].tilt.angle.gx,
						    &g_srv_sensor_motion_cntx.app[i].tilt.angle.gy,
						    &g_srv_sensor_motion_cntx.app[i].tilt.angle.gz);				
					 }

					MOTION_EXIT_PROTECT;
				}
				//  MMI_TRACE(MMI_MEDIA_TRC_G2_APP, MMI_TRC_MOTION_SHAKE_STATE,
				//       g_srv_sensor_motion_cntx.app[i].shake_callback, i, g_srv_sensor_motion_cntx.is_shake_mute);

				if (g_srv_sensor_motion_cntx.app[i].shake_callback != NULL &&
					g_srv_sensor_motion_cntx.is_shake_mute == MMI_FALSE)
				{
					/*check shake here*/
					shake = srv_sensor_motion_calc_shake(g_srv_sensor_motion_cntx.history, g_srv_sensor_motion_cntx.app[i].shake_sen.sensitive);
					g_srv_sensor_motion_cntx.app[i].shake.action = shake;

				//	kal_prompt_trace(MOD_MMI,"Motion sensor: Shake action = %d",shake);

					if (shake != SRV_SENSOR_MOTION_SHAKE_NULL)
					{
						need_mute = MMI_TRUE;
					}
				}

				/* For porting mCube library */


				if (g_srv_sensor_motion_cntx.app[i].tap_callback != NULL ||
					g_srv_sensor_motion_cntx.app[i].double_tap_callback != NULL
					|| g_srv_sensor_motion_cntx.app[i].advanced_shake_callback != NULL
					|| g_srv_sensor_motion_cntx.app[i].step_callback != NULL
					|| g_srv_sensor_motion_cntx.app[i].drop_callback != NULL
					|| g_srv_sensor_motion_cntx.app[i].turnover_callback != NULL)
				{
					/*There is APP register mCube,need push data*/
					need_push_data_to_mcube = 1;

				}
				else
					need_push_data_to_mcube = 0;

			} /*end for*/

			if (need_push_data_to_mcube == 1)
			{
				/* translate acc_xyz to mcube acc_xyz (milli-g to m/s2))*/
				srv_sensor_motion_get_acc(g_srv_sensor_motion_cntx.detail_info.acc_x,
					g_srv_sensor_motion_cntx.detail_info.acc_y,
					g_srv_sensor_motion_cntx.detail_info.acc_z,
					&g_srv_sensor_motion_cntx.detail_info.sensor_data.mcube_acc_x,
					&g_srv_sensor_motion_cntx.detail_info.sensor_data.mcube_acc_y,
					&g_srv_sensor_motion_cntx.detail_info.sensor_data.mcube_acc_z
					);

#ifdef __MTK_TARGET__
				/* mCube library process data */
				mcube_result = mCube_LibProcessData(MCUBE_LIB_ACCEL, 
					(void *)&g_srv_sensor_motion_cntx.detail_info.sensor_data, 1);
#endif

				/* Trace */

			//	kal_prompt_trace(MOD_MMI,"[mcube]mCube_LibProcessData : %d", mcube_result);

				sprintf((char *)x,"%6.5f",(float)g_srv_sensor_motion_cntx.detail_info.sensor_data.mcube_acc_x);
				sprintf((char *)y,"%6.5f",(float)g_srv_sensor_motion_cntx.detail_info.sensor_data.mcube_acc_y);
				sprintf((char *)z,"%6.5f",(float)g_srv_sensor_motion_cntx.detail_info.sensor_data.mcube_acc_z);

				kal_prompt_trace(MOD_MMI,"[mcube] x:%s, y:%s, z:%s, status: %x, time:%d", 
					x,
					y,
					z,
					g_srv_sensor_motion_cntx.detail_info.sensor_data.status,
					g_srv_sensor_motion_cntx.detail_info.sensor_data.timeStampMs);
			}
			/*if need mute shake detect for a while*/
			if (need_mute)
			{
				g_srv_sensor_motion_cntx.is_shake_mute = MMI_TRUE;
				gui_start_timer(MOTION_SHAKE_DET_MUTE_DURATION, srv_sensor_motion_shake_det_mute_timeout);
			}
			/*callback app with data or send message to right task*/
			srv_sensor_motion_update_hldr();
			for (i = 0; i < MOTION_SUPPORT_APP_NUM; i++)
			{
				if (g_srv_sensor_motion_cntx.app[i].mod_id == MOD_NIL)
				{
					MMI_TRACE(MMI_MEDIA_TRC_G10_MMI_PRINT, MMI_TRC_MOTION_SEND_MSG_MOD_EQ_NIL,i);
					break;
				}
				if (g_srv_sensor_motion_cntx.app[i].mod_id != MOTION_RUN_MOD &&
					g_srv_sensor_motion_cntx.app[i].is_msg_send == MMI_FALSE)
				{
					message.oslMsgId = MSG_ID_MDI_MOTION_UPDATE_RSP;
					message.oslDataPtr = NULL;
					message.oslPeerBuffPtr = NULL;
					message.oslSrcId = MOTION_RUN_MOD;
					message.oslDestId = g_srv_sensor_motion_cntx.app[i].mod_id;
					OslMsgSendExtQueue(&message);
					g_srv_sensor_motion_cntx.app[i].is_msg_send = MMI_TRUE;
				}
			}
		}
	}
	/* set this flag false, allows next messge */
	g_srv_sensor_motion_cntx.is_msg_send = MMI_FALSE;
}



/* Through callback data get detail info for advanced shake */

static void srv_sensor_motion_cal_advanced_shake(void *data, U32 index)
{
	/*----------------------------------------------------------------*/
	/* Local Variables                                                */
	/*----------------------------------------------------------------*/
	U8 shakeData = 0;
	/*
	uint8_t shake_x;
	uint8_t shake_y;
	uint8_t shake_z;
	*/

	/*----------------------------------------------------------------*/
	/* Code body                                                      */
	/*----------------------------------------------------------------*/
	shakeData = (*(int*)data & 0xFF);

	g_srv_sensor_motion_cntx.app[index].advanced_shake.direction = shakeData;

}

/* Through callback data get detail info for tap */

static srv_sensor_motion_cal_tap(void *data, U32 index)
{
	/*----------------------------------------------------------------*/
	/* Local Variables												 */
	/*----------------------------------------------------------------*/

	/*----------------------------------------------------------------*/
	/* Code body 													 */
	/*----------------------------------------------------------------*/
	g_srv_sensor_motion_cntx.app[index].tap.tap_direct = SRV_SENSOR_MOTION_TAP_DONE;

}

/* Through callback data get detail info for double tap */

static srv_sensor_motion_cal_doubletap(void *data, U32 index)
{
	/*----------------------------------------------------------------*/
	/* Local Variables												 */
	/*----------------------------------------------------------------*/

	/*----------------------------------------------------------------*/
	/* Code body													 */
	/*----------------------------------------------------------------*/
	g_srv_sensor_motion_cntx.app[index].double_tap.double_tap_direct = SRV_SENSOR_MOTION_TAP_DONE;

}


/* Through callback data get detail info for turnover (flip) */

static void srv_sensor_motion_cal_turnover(void *data, U32 index)
{
	/*----------------------------------------------------------------*/
	/* Local Variables												 */
	/*----------------------------------------------------------------*/

	/*----------------------------------------------------------------*/
	/* Code body 													 */
	/*----------------------------------------------------------------*/

	mCubeFlipEvent_t *flip = (mCubeFlipEvent_t*)data;

	switch (*flip)
	{
	case FLIP_FRONT_TO_BACK:
		g_srv_sensor_motion_cntx.app[index].turnover.direction= SRV_SENSOR_MOTION_TURNOVER_DOWN;
		break;
	case FLIP_BACK_TO_FRONT:
		g_srv_sensor_motion_cntx.app[index].turnover.direction= SRV_SENSOR_MOTION_TURNOVER_UP;
		break;
	default:
		g_srv_sensor_motion_cntx.app[index].turnover.direction= SRV_SENSOR_MOTION_TURNOVER_NONE;
		break;
	}
}


#ifdef __MOTION_SENSOR_ADVANCED_GESTURE__
static void srv_sensor_motion_cal_step(void *data, U32 index)
{
	/*----------------------------------------------------------------*/
	/* Local Variables												 */
	/*----------------------------------------------------------------*/

	/*----------------------------------------------------------------*/
	/* Code body													 */
	/*----------------------------------------------------------------*/
	g_srv_sensor_motion_cntx.app[index].step.action = SRV_SENSOR_MOTION_ACTION_STEP;	
}
#endif /*__MOTION_SENSOR_ADVANCED_GESTURE__*/

static void srv_sensor_motion_cal_drop(void *data, U32 index)
{	
	/*----------------------------------------------------------------*/
	/* Local Variables												 */
	/*----------------------------------------------------------------*/

	/*----------------------------------------------------------------*/
	/* Code body													 */
	/*----------------------------------------------------------------*/
	g_srv_sensor_motion_cntx.app[index].drop.action = SRV_SENSOR_MOTION_ACTION_DROP;	
}






#ifdef __MTK_TARGET__

/** mCube_LibHandler
*
* This function is invoked by the mCube Sensor Library to provide event
* notifications, and when interaction with the sensor driver is required.
*/
mCubeLibResult_t mCube_LibHandler(mCubeLibSens_t sensor,
								  mCubeLibEvent_t event,
								  void *data)
{

	module_type 	   module_id;	

	mCubeLibResult_t retVal = MCUBE_LIB_SUCCESS;
	U8 i = 0;
	module_id = stack_get_active_module_id();


//	kal_prompt_trace(MOD_MMI,"[mcube]Call mCube_LibHandler %d ", event);

	if (MCUBE_LIB_ACCEL == sensor) {		

		for (i = 0; i<MOTION_SUPPORT_APP_NUM; i++)
		{
			if (g_srv_sensor_motion_cntx.app[i].mod_id == module_id)
			{
				/* Advanced shake callback */

				if(g_srv_sensor_motion_cntx.app[i].advanced_shake_callback!= NULL &&
					MCUBE_LIB_SHAKE == event)
				{
					/* call back direct function*/
					/* Get shake data */
					srv_sensor_motion_cal_advanced_shake(data,i);

					/* Call back */
					g_srv_sensor_motion_cntx.app[i].advanced_shake_callback(SRV_SENSOR_MOTION_ADVANCED_SHAKE, 
						&(g_srv_sensor_motion_cntx.app[i].advanced_shake),
						g_srv_sensor_motion_cntx.app[i].advanced_shake_user_data);

					/* Trace */
					kal_prompt_trace(MOD_MMI,"[mcube]advanced_shake_callback! %d",
						g_srv_sensor_motion_cntx.app[i].advanced_shake.direction);

				}

				/* Tap callback */
				if(g_srv_sensor_motion_cntx.app[i].tap_callback != NULL &&
					MCUBE_LIB_TAP == event)
				{

					srv_sensor_motion_cal_tap(data,i);
					g_srv_sensor_motion_cntx.app[i].tap_callback(SRV_SENSOR_MOTION_TAP,
						&(g_srv_sensor_motion_cntx.app[i].tap),
						g_srv_sensor_motion_cntx.app[i].tap_user_data);

					/* Trace */
					kal_prompt_trace(MOD_MMI,"Motion sensor: tap callback!!!");

				}

				/* Double Tap callback */
				if(g_srv_sensor_motion_cntx.app[i].double_tap_callback != NULL &&
					MCUBE_LIB_DOUBLE_TAP == event)
				{
					srv_sensor_motion_cal_doubletap(data,i);
					g_srv_sensor_motion_cntx.app[i].double_tap_callback(SRV_SENSOR_MOTION_DOUBLE_TAP,
						&(g_srv_sensor_motion_cntx.app[i].double_tap),
						g_srv_sensor_motion_cntx.app[i].double_tap_user_data);
					/* Trace */
					kal_prompt_trace(MOD_MMI,"Motion sensor: double tap callback!!!");
				}	

				/* Turnover callback */
				if(g_srv_sensor_motion_cntx.app[i].turnover_callback != NULL &&
					MCUBE_LIB_FLIP == event)
				{

					srv_sensor_motion_cal_turnover(data,i);

					g_srv_sensor_motion_cntx.app[i].turnover_callback(SRV_SENSOR_MOTION_TURNOVER,
						&(g_srv_sensor_motion_cntx.app[i].turnover),
						g_srv_sensor_motion_cntx.app[i].turnover_user_data);

					kal_prompt_trace(MOD_MMI,"Motion sensor: flip callback!!!");
				}


#ifdef __MOTION_SENSOR_ADVANCED_GESTURE__
				/* Step callback */
				if(g_srv_sensor_motion_cntx.app[i].step_callback != NULL &&
					MCUBE_LIB_STEP == event)
				{
					kal_prompt_trace(MOD_MMI,"Motion sensor: step callback!!!");

					srv_sensor_motion_cal_step(data,i);

					g_srv_sensor_motion_cntx.app[i].step_callback(SRV_SENSOR_MOTION_STEP,
						&(g_srv_sensor_motion_cntx.app[i].step),
						g_srv_sensor_motion_cntx.app[i].step_user_data);
				}
#endif /*__MOTION_SENSOR_ADVANCED_GESTURE__*/

				/* Drop callback */
				if(g_srv_sensor_motion_cntx.app[i].drop_callback != NULL &&
					MCUBE_LIB_DROP == event)
				{
					kal_prompt_trace(MOD_MMI,"Motion sensor: drop callback!!!");

					srv_sensor_motion_cal_drop(data,i);

					g_srv_sensor_motion_cntx.app[i].drop_callback(SRV_SENSOR_MOTION_DROP,
						&(g_srv_sensor_motion_cntx.app[i].drop),
						g_srv_sensor_motion_cntx.app[i].drop_user_data);
				}

				g_srv_sensor_motion_cntx.app[i].is_msg_send = MMI_FALSE;
			}
		}				 

	}
	return retVal;
}

#endif /* __MTK_TARGET__*/




/*****************************************************************************
* FUNCTION
*  srv_sensor_motion_update_hldr
* DESCRIPTION
*  the function run at caller task to callback the application
* PARAMETERS
*  void
* RETURNS
*  void
*****************************************************************************/
static void srv_sensor_motion_update_hldr(void)
{
	/*----------------------------------------------------------------*/
	/* Local Variables                                                */
	/*----------------------------------------------------------------*/
	module_type        module_id;   
	U32 i;

	/*----------------------------------------------------------------*/
	/* Code Body                                                      */
	/*----------------------------------------------------------------*/
	module_id = stack_get_active_module_id();

	/*This is for an abnormal case. If there is no sensor chip or sensor chip is
	abnormal, acc_x,acc_y,acc_z will be all zero, but it is wrong, so sensor service
	will not callback application */

	if (g_srv_sensor_motion_cntx.detail_info.acc_x == 0 &&
		g_srv_sensor_motion_cntx.detail_info.acc_y == 0 &&
		g_srv_sensor_motion_cntx.detail_info.acc_z == 0 )
	{
		kal_prompt_trace(MOD_MMI,"Motion sensor: It is an abnormal case,acc_x=0,acc_y=0,acc_z=0");
		return;
	}
	

	for (i = 0; i<MOTION_SUPPORT_APP_NUM; i++)
	{
		if (g_srv_sensor_motion_cntx.app[i].mod_id == module_id)
		{
			/*call app with last right data*/

			if(g_srv_sensor_motion_cntx.app[i].direct_callback != NULL &&
				g_srv_sensor_motion_cntx.app[i].direct.direct != g_srv_sensor_motion_cntx.app[i].prev_direct)
			{
				/* call back direct function*/
				//srv_sensor_type_enum sensor_type, void *sensor_data, void *user_data
				g_srv_sensor_motion_cntx.app[i].direct_callback(SRV_SENSOR_MOTION_DIRECT, 
					&(g_srv_sensor_motion_cntx.app[i].direct),
					g_srv_sensor_motion_cntx.app[i].direct_user_data);

			}
			if(g_srv_sensor_motion_cntx.app[i].shake_callback != NULL &&
				g_srv_sensor_motion_cntx.app[i].shake.action != SRV_SENSOR_MOTION_SHAKE_NULL)
			{

				kal_prompt_trace(MOD_MMI,"Motion sensor: Shake callback!!!");


				g_srv_sensor_motion_cntx.app[i].shake_callback(SRV_SENSOR_MOTION_SHAKE,
					&(g_srv_sensor_motion_cntx.app[i].shake),
					g_srv_sensor_motion_cntx.app[i].shake_user_data);
				g_srv_sensor_motion_cntx.app[i].shake.action = SRV_SENSOR_MOTION_SHAKE_NULL;
			}
			if(g_srv_sensor_motion_cntx.app[i].tilt_callback != NULL)
			{
				g_srv_sensor_motion_cntx.app[i].tilt_callback(SRV_SENSOR_MOTION_TILT,
					&(g_srv_sensor_motion_cntx.app[i].tilt),
					g_srv_sensor_motion_cntx.app[i].tilt_user_data);

				kal_prompt_trace(MOD_MMI,"Filter noise x=%d,y=%d,z=%d",
					g_srv_sensor_motion_cntx.app[i].tilt.acc.x,
					g_srv_sensor_motion_cntx.app[i].tilt.acc.y,
					g_srv_sensor_motion_cntx.app[i].tilt.acc.z);

				kal_prompt_trace(MOD_MMI,"Filter noise (raw) x=%d,y=%d,z=%d",
					g_srv_sensor_motion_cntx.app[i].tilt.acc.x,
					g_srv_sensor_motion_cntx.app[i].tilt.acc.y,
					g_srv_sensor_motion_cntx.app[i].tilt.acc.z);

				
			}            
			srv_sensor_motion_push_direct2history(
				g_srv_sensor_motion_cntx.app[i].direct.direct, 
				&g_srv_sensor_motion_cntx.app[i]);
			g_srv_sensor_motion_cntx.app[i].is_msg_send = MMI_FALSE;
		}
	}
}


/*****************************************************************************
* FUNCTION
*  mdi_motion_shake_add_to_history
* DESCRIPTION
*  add a data to history
* PARAMETERS
*  *history           [IN]        pointer to the history structure
* RETURNS
*  void
*****************************************************************************/
static void srv_sensor_motion_shake_add_to_history(srv_sensor_motion_acc_history_struct *history)
{
	/*----------------------------------------------------------------*/
	/* Local Variables                                                */
	/*----------------------------------------------------------------*/

	/*----------------------------------------------------------------*/
	/* Code Body                                                      */
	/*----------------------------------------------------------------*/
	memcpy(&g_srv_sensor_motion_cntx.history[1],
		&g_srv_sensor_motion_cntx.history[0],
		sizeof(srv_sensor_motion_acc_history_struct) * (MOTION_SHAKE_HISTORY_DEPTH - 1));
	memcpy(&g_srv_sensor_motion_cntx.history[0], history, sizeof(srv_sensor_motion_acc_history_struct));
}





/*****************************************************************************
* FUNCTION
*  mdi_motion_tilt_calc_angle_gxyz_by_xy
* DESCRIPTION
*  calc gx gy gz angle by acc of x y only
* PARAMETERS
*  int_x           [IN]        acc at x axis with direction
*  int_y           [IN]        acc at y axis with direction
*  int_z           [IN]        dummy input now
*  *out_angle_gx   [IN]        the pointer to the gx angle output
*  *out_angle_gy   [IN]        the pointer to the gy angle output
*  *out_angle_gz   [IN]        the pointer to the gz angle output
* RETURNS
*  void
*****************************************************************************/
static void srv_sensor_motion_calc_angle_gxyz_by_xy(
	S32 int_x, S32 int_y, S32 int_z, 
	S32 *out_angle_gx, S32 *out_angle_gy, S32 *out_angle_gz)
{
	/*----------------------------------------------------------------*/
	/* Local Variables                                                */
	/*----------------------------------------------------------------*/
	U32 square_g;
	float actual_g;

	/*----------------------------------------------------------------*/
	/* Code Body                                                      */
	/*----------------------------------------------------------------*/
	square_g = int_x * int_x + int_y * int_y + int_z * int_z;

	actual_g = sqrt(square_g);

	if (int_x >= 0)
	{
		*out_angle_gx = (S32)(acos(abs(int_x)/actual_g)/PI * 180.0);
	}
	else
	{
		*out_angle_gx = 180.0 - (S32)(acos(abs(int_x)/actual_g)/PI * 180.0);
	}
	if (int_y >=0)
	{
		*out_angle_gy = (S32)(acos(abs(int_y)/actual_g)/PI * 180.0);
	}
	else
	{
		*out_angle_gy = 180.0 - (S32)(acos(abs(int_y)/actual_g)/PI * 180.0);
	}
	if (int_z >= 0)
	{
		*out_angle_gz = (S32)(acos(abs(int_z)/actual_g)/PI * 180.0);
	}
	else
	{
		*out_angle_gz = 180.0 - (S32)(acos(abs(int_z)/actual_g)/PI * 180.0);
	}

}

/*****************************************************************************
* FUNCTION
*  srv_sensor_motion_direct_debounce
* DESCRIPTION
*  Direct debounce
* PARAMETERS
*  void
* RETURNS
*  void
*****************************************************************************/

static void srv_sensor_motion_direct_debounce(void)
{
	/*----------------------------------------------------------------*/
	/* Local Variables                                                */
	/*----------------------------------------------------------------*/

	/*----------------------------------------------------------------*/
	/* Code Body                                                      */
	/*----------------------------------------------------------------*/
	if (g_direction == g_old_direction)
	{
	    //kal_prompt_trace(MOD_MMI,"Sensor service g_real_direction = g_old_direction");
		g_real_direction = g_old_direction;
	}
	else
	{
		g_real_direction = SRV_SENSOR_MOTION_ANGLE_OTHER;
		//kal_prompt_trace(MOD_MMI,"Sensor service SRV_SENSOR_MOTION_ANGLE_OTHER");
	}
}

/*****************************************************************************
* FUNCTION
*  mdi_motion_tilt_calc_axis_direct
* DESCRIPTION
*  calc if near the axis of x y or z
* PARAMETERS
*  sensitive       [IN]        sensitive
*  angle_gx        [IN]        angle of g and x axis
*  angle_gy        [IN]        angle of g and y axis
*  angle_gz        [IN]        angle of g and z axis
*  acc_x           [IN]        acc of x axis
*  acc_y           [IN]        acc of y axis
* RETURNS
*  mdi_motion_angle_enum the direction of axis    
*****************************************************************************/
static srv_sensor_motion_direct_enum
srv_sensor_motion_calc_axis_direct(U8 sensitive, S32 angle_gx, S32 angle_gy, S32 angle_gz, S32 acc_x, S32 acc_y)
{
	/*----------------------------------------------------------------*/
	/* Local Variables                                                */
	/*----------------------------------------------------------------*/
	U8 threshold = MOTION_ANGLE_SENS_NORMAL_THRESHOLD;
	//U32 boundary_down, boundary_up;

	/*----------------------------------------------------------------*/
	/* Code Body                                                      */
	/*----------------------------------------------------------------*/

	if(sensitive > 40)
	{
		sensitive = 40;
	}
	if(sensitive < 0)
	{
		sensitive = 0;
	}

	threshold = sensitive;

	if (angle_gx <= threshold)
	{
		g_direction = SRV_SENSOR_MOTION_ANGLE_GX0;
	}
	else if (angle_gx >= MOTION_ANGLE_180 - threshold)
	{
		g_direction = SRV_SENSOR_MOTION_ANGLE_GX180;
	}
	else if (angle_gy <= threshold)
	{
		g_direction = SRV_SENSOR_MOTION_ANGLE_GY0;
	}
	else if (angle_gy >= MOTION_ANGLE_180 - threshold)
	{
		g_direction = SRV_SENSOR_MOTION_ANGLE_GY180;
	}
	else if (angle_gz <= threshold)
	{
		g_direction = SRV_SENSOR_MOTION_ANGLE_GZ0;
	}
	else if (angle_gz > MOTION_ANGLE_180 - threshold)
	{
		g_direction = SRV_SENSOR_MOTION_ANGLE_GZ180;
	}
	
	else 
		g_direction = SRV_SENSOR_MOTION_ANGLE_OTHER;



	if (g_direction != g_old_direction)
	{
		g_old_direction = g_direction;
		StopTimer(SRV_MOTION_DIRECT_TIMER_ID);
		StartTimer(SRV_MOTION_DIRECT_TIMER_ID, MOTION_DIRECT_DEBOUNCE, srv_sensor_motion_direct_debounce);
	}

    //kal_prompt_trace(MOD_MMI,"Sensor service direction is %d",g_real_direction);
	return g_real_direction;
}


/*****************************************************************************
* FUNCTION
*  mdi_motion_tilt_calc_action
* DESCRIPTION
*  calc the rotate action
* PARAMETERS
*  old_direct      [IN]        old direction of axis direction
*  new_direct      [IN]        new direction of axis direction
* RETURNS
*  mdi_motion_tilt_calc_action the aciton of rotate
*****************************************************************************/
static srv_sensor_motion_action_enum
srv_sensor_motion_calc_action(srv_sensor_motion_direct_enum old_direct, srv_sensor_motion_direct_enum new_direct)
{
	/*----------------------------------------------------------------*/
	/* Local Variables                                                */
	/*----------------------------------------------------------------*/
	srv_sensor_motion_action_enum action = SRV_SENSOR_MOTION_ACTION_NULL;

	/*----------------------------------------------------------------*/
	/* Code Body                                                      */
	/*----------------------------------------------------------------*/
	switch (new_direct)
	{
		/*Y equ 0*/
	case SRV_SENSOR_MOTION_ANGLE_GY0:
		switch (old_direct)
		{
		case SRV_SENSOR_MOTION_ANGLE_GX0:
			action = SRV_SENSOR_MOTION_ACTION_ROLL_RIGHT;
			break;
		case SRV_SENSOR_MOTION_ANGLE_GX180:
			action = SRV_SENSOR_MOTION_ACTION_ROLL_LEFT;
			break;
		case SRV_SENSOR_MOTION_ANGLE_GZ0:
			action = SRV_SENSOR_MOTION_ACTION_PITCH_BACKWARD;
			break;
		case SRV_SENSOR_MOTION_ANGLE_GZ180:
			action = SRV_SENSOR_MOTION_ACTION_PITCH_FORWARD;
			break;
		default:
			break;
		}
		break;
		/*Y equ 180*/
	case SRV_SENSOR_MOTION_ANGLE_GY180:
		switch (old_direct)
		{
		case SRV_SENSOR_MOTION_ANGLE_GX0:
			action = SRV_SENSOR_MOTION_ACTION_ROLL_LEFT;
			break;
		case SRV_SENSOR_MOTION_ANGLE_GX180:
			action = SRV_SENSOR_MOTION_ACTION_ROLL_RIGHT;
			break;
		case SRV_SENSOR_MOTION_ANGLE_GZ0:
			action = SRV_SENSOR_MOTION_ACTION_PITCH_BACKWARD;
			break;
		case SRV_SENSOR_MOTION_ANGLE_GZ180:
			action = SRV_SENSOR_MOTION_ACTION_PITCH_FORWARD;
			break;
		default:
			break;
		}
		break;
		/*X equ 0*/
	case SRV_SENSOR_MOTION_ANGLE_GX0:
		switch (old_direct)
		{
		case SRV_SENSOR_MOTION_ANGLE_GY0:
			action = SRV_SENSOR_MOTION_ACTION_ROLL_LEFT;
			break;
		case SRV_SENSOR_MOTION_ANGLE_GY180:
			action = SRV_SENSOR_MOTION_ACTION_ROLL_RIGHT;
			break;
		case SRV_SENSOR_MOTION_ANGLE_GZ0:
			action = SRV_SENSOR_MOTION_ACTION_YAW_RIGHT;
			break;
		case SRV_SENSOR_MOTION_ANGLE_GZ180:
			action = SRV_SENSOR_MOTION_ACTION_YAW_LEFT;
			break;
		default:
			break;
		}
		break;
		/*X equ 180*/
	case SRV_SENSOR_MOTION_ANGLE_GX180:
		switch (old_direct)
		{
		case SRV_SENSOR_MOTION_ANGLE_GY0:
			action = SRV_SENSOR_MOTION_ACTION_ROLL_RIGHT;
			break;
		case SRV_SENSOR_MOTION_ANGLE_GY180:
			action = SRV_SENSOR_MOTION_ACTION_ROLL_LEFT;
			break;
		case SRV_SENSOR_MOTION_ANGLE_GZ0:
			action = SRV_SENSOR_MOTION_ACTION_YAW_LEFT;
			break;
		case SRV_SENSOR_MOTION_ANGLE_GZ180:
			action = SRV_SENSOR_MOTION_ACTION_YAW_RIGHT;
			break;
		default:
			break;
		}
		break;
		/*Z equ 0*/
	case SRV_SENSOR_MOTION_ANGLE_GZ0:
		switch (old_direct)
		{
		case SRV_SENSOR_MOTION_ANGLE_GX0:
			action = SRV_SENSOR_MOTION_ACTION_YAW_LEFT;
			break;
		case SRV_SENSOR_MOTION_ANGLE_GX180:
			action = SRV_SENSOR_MOTION_ACTION_YAW_RIGHT;
			break;
		case SRV_SENSOR_MOTION_ANGLE_GY0:
			action = SRV_SENSOR_MOTION_ACTION_PITCH_BACKWARD;
			break;
		case SRV_SENSOR_MOTION_ANGLE_GY180:
			action = SRV_SENSOR_MOTION_ACTION_PITCH_FORWARD;
			break;
		default:
			break;
		}
		break;
		/*Z equ 180*/
	case SRV_SENSOR_MOTION_ANGLE_GZ180:
		switch (old_direct)
		{
		case SRV_SENSOR_MOTION_ANGLE_GX0:
			action = SRV_SENSOR_MOTION_ACTION_YAW_RIGHT;
			break;
		case SRV_SENSOR_MOTION_ANGLE_GX180:
			action = SRV_SENSOR_MOTION_ACTION_YAW_LEFT;
			break;
		case SRV_SENSOR_MOTION_ANGLE_GY0:
			action = SRV_SENSOR_MOTION_ACTION_PITCH_FORWARD;
			break;
		case SRV_SENSOR_MOTION_ANGLE_GY180:
			action = SRV_SENSOR_MOTION_ACTION_PITCH_BACKWARD;
			break;
		default:
			break;
		}
		break;
	default:
		break;
	}
	return action;
}

/*****************************************************************************
* FUNCTION
*  mdi_motion_shake_calc_direct
* DESCRIPTION
*  calc direction of shake action
* PARAMETERS
*  void
* RETURNS
*  void
*****************************************************************************/
static srv_sensor_motion_shake_action_enum srv_sensor_motion_calc_shake(srv_sensor_motion_acc_history_struct *history, 
																		srv_sensor_motion_shake_sensitive_enum sensitive)
{
	/*----------------------------------------------------------------*/
	/* Local Variables                                                */
	/*----------------------------------------------------------------*/
	U32 i, shake_index = 0, threshold = MOTION_SHAKE_THRESHOLD_NORMAL;
	srv_sensor_motion_shake_action_enum    shake_action = SRV_SENSOR_MOTION_SHAKE_NULL;


	/*----------------------------------------------------------------*/
	/* Code Body                                                      */
	/*----------------------------------------------------------------*/
	if (g_srv_sensor_motion_cntx.is_shake_mute == MMI_TRUE)
	{
		return shake_action;
	}
	switch (sensitive)
	{
	case SRV_SENSOR_MOTION_SHAKE_WEAK:
		threshold = MOTION_SHAKE_THRESHOLD_LOW;
		break;
	case SRV_SENSOR_MOTION_SHAKE_MEDIUM:
		threshold = MOTION_SHAKE_THRESHOLD_NORMAL;
		break;
	case SRV_SENSOR_MOTION_SHAKE_STRONG:
		threshold = MOTION_SHAKE_THRESHOLD_HIGH;
		break;
	default:
		ASSERT(0);
	}
	MMI_TRACE(MMI_MEDIA_TRC_G10_MMI_PRINT, MMI_TRC_MOTION_SHAKE_HISTORY,
		history[0].acc_x,
		history[1].acc_x,
		history[2].acc_x,
		history[3].acc_x,
		history[4].acc_x,
		threshold);
	for (i = 0; i < MOTION_SHAKE_HISTORY_DEPTH - 1 && i < 3; i++)
	{
		if (abs(history[i].acc_x - history[i+1].acc_x) > threshold)
		{
			shake_index = i;
			break;
		}
	}
	for (i = shake_index + 1; i < MOTION_SHAKE_HISTORY_DEPTH - 1; i++)
	{
		if (abs(history[i].acc_x - history[i+1].acc_x) > threshold)
		{
			if (history[shake_index].acc_x - history[shake_index + 1].acc_x > 0 &&
				history[i].acc_x - history[i+1].acc_x < 0)
			{
				shake_action = SRV_SENSOR_MOTION_SHAKING;
				MMI_TRACE(MMI_MEDIA_TRC_G10_MMI_PRINT, MMI_TRC_MOTION_SHAKE_DETECT_DATA,
					shake_action,
					shake_index,
					i,
					i+1,
					history[shake_index].acc_x,
					history[i].acc_x,
					history[i+1].acc_x);
				break;
			}
			else if (history[shake_index].acc_x - history[shake_index + 1].acc_x < 0 &&
				history[i].acc_x - history[i+1].acc_x > 0)
			{
				shake_action = SRV_SENSOR_MOTION_SHAKING;
				MMI_TRACE(MMI_MEDIA_TRC_G10_MMI_PRINT, MMI_TRC_MOTION_SHAKE_DETECT_DATA,
					shake_action,
					shake_index,
					i,
					i+1,
					history[shake_index].acc_x,
					history[i].acc_x,
					history[i+1].acc_x);
				break;
			}
		}
	}
#if 0  /*this part can check up down shake but not work well*/
/* under construction !*/
#endif
	return shake_action;
}


/*****************************************************************************
* FUNCTION
*  mdi_motion_push_action2history
* DESCRIPTION
*  push the action to the history
* PARAMETERS
*  motion_direct   [IN]    direction
*  motion_action   [IN]    action
*  *app_tilt       [IN]    the structure to push the behaver to
* RETURNS
*  void
*****************************************************************************/
static void srv_sensor_motion_push_direct2history(
	srv_sensor_motion_direct_enum motion_direct,
	srv_sensor_motion_app_struct *app_tilt)
{
	/*----------------------------------------------------------------*/
	/* Local Variables                                                */
	/*----------------------------------------------------------------*/

	/*----------------------------------------------------------------*/
	/* Code Body                                                      */
	/*----------------------------------------------------------------*/
	MOTION_ENTER_PROTECT;
	if (motion_direct != SRV_SENSOR_MOTION_ANGLE_OTHER)
	{
		app_tilt->prev_direct = motion_direct;
	}
	MOTION_EXIT_PROTECT;
}


#endif  /*MDI_MOTION_DRIVER_AVAIALBE*/


/*****************************************************************************
* FUNCTION
*  mdi_motion_message_dispatcher
* DESCRIPTION
*  dispatcher of the mdi motion, this will get message for task needed
* PARAMETERS
*  ilm_ptr        [IN]        ilm_struct data
* RETURNS           
*  MMI_TRUE    message is consumed by this function
*  MMI_FALSE   message is not consumed by this function
*****************************************************************************/
MMI_BOOL srv_sensor_message_dispatcher(void *ilm_ptr)
{
	/*----------------------------------------------------------------*/
	/* Local Variables                                                */
	/*----------------------------------------------------------------*/
#ifdef MDI_MOTION_DRIVER_AVAIALBE
	ilm_struct *ilm_p = ilm_ptr;
	MMI_BOOL handle_msg = MMI_TRUE;

	/*----------------------------------------------------------------*/
	/* Code Body                                                      */
	/*----------------------------------------------------------------*/    
	switch(ilm_p->msg_id)
	{
	case MSG_ID_MDI_MOTION_DATA_STATE_RSP:
		srv_sensor_motion_data_hdlr(ilm_p->local_para_ptr);
		break;
	case MSG_ID_MDI_MOTION_UPDATE_RSP:
		srv_sensor_motion_update_hldr();
		break;
	default:
		handle_msg = MMI_FALSE;
		break;
	}
	return handle_msg;
#else
	return MMI_FALSE;
#endif
}


/*****************************************************************************
* FUNCTION
*  mdi_motion_is_around
* DESCRIPTION
*  check x is around point with threshold
* PARAMETERS
*  x           [IN]    the number want to check
*  point       [IN]    the point want to check if been around
*  threshold   [IN]    the r
* RETURNS
*  MMI_TRUE    x is around point
*  MMI_TRUE    x is not around point
*****************************************************************************/
#if 0
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
#endif





#endif /* MOTION_SENSOR_SUPPORT */ 



#ifdef MOTION_SENSOR_SUPPORT

/*****************************************************************************
* FUNCTION
*   srv_sensor_is_available
* DESCRIPTION
*   Check whether the sensor(motion ,or future other sensors...) is enable
*
* PARAMETERS
*   handle:           [IN] the sensor type which need to be check 
* RETURNS
*  MMI_BOOL
*****************************************************************************/
extern MMI_BOOL srv_sensor_is_available(srv_sensor_type_enum sensor_type)
{
	switch(sensor_type)
	{
	case SRV_SENSOR_MOTION_DIRECT:	
	case SRV_SENSOR_MOTION_SHAKE:
	case SRV_SENSOR_MOTION_TILT:
		{
#ifdef MOTION_SENSOR_SUPPORT
			return MMI_TRUE;
#endif
			break;
		}
	default:
		break;
	}

	return MMI_FALSE;
}


/* When motion sensor switch is off, service will notify each application */
static srv_sensor_motion_notify_each_app()
{
	U32 i = 0;

	for (i = 0; i<MOTION_SUPPORT_APP_NUM; i++)
	{
		if (g_srv_sensor_motion_cntx.app[i].advanced_shake_callback != NULL)
		{
			g_srv_sensor_motion_cntx.app[i].advanced_shake_callback
				(SRV_SENSOR_DISABLE, 
				NULL,
				g_srv_sensor_motion_cntx.app[i].advanced_shake_user_data);
		}
		if (g_srv_sensor_motion_cntx.app[i].double_tap_callback != NULL)
		{
			g_srv_sensor_motion_cntx.app[i].double_tap_callback
				(SRV_SENSOR_DISABLE, 
				NULL,
				g_srv_sensor_motion_cntx.app[i].double_tap_user_data);
		}
		if (g_srv_sensor_motion_cntx.app[i].tap_callback != NULL)
		{
			g_srv_sensor_motion_cntx.app[i].tap_callback
				(SRV_SENSOR_DISABLE, 
				NULL,
				g_srv_sensor_motion_cntx.app[i].tap_user_data);
		}
		if (g_srv_sensor_motion_cntx.app[i].turnover_callback != NULL)
		{
			g_srv_sensor_motion_cntx.app[i].turnover_callback
				(SRV_SENSOR_DISABLE, 
				NULL,
				g_srv_sensor_motion_cntx.app[i].turnover_user_data);
		}
		if (g_srv_sensor_motion_cntx.app[i].drop_callback != NULL)
		{
			g_srv_sensor_motion_cntx.app[i].drop_callback
				(SRV_SENSOR_DISABLE, 
				NULL,
				g_srv_sensor_motion_cntx.app[i].drop_user_data);
		}
		if (g_srv_sensor_motion_cntx.app[i].step_callback != NULL)
		{
			g_srv_sensor_motion_cntx.app[i].step_callback
				(SRV_SENSOR_DISABLE, 
				NULL,
				g_srv_sensor_motion_cntx.app[i].step_user_data);
		}

		if (g_srv_sensor_motion_cntx.app[i].tilt_callback!= NULL)
		{
			g_srv_sensor_motion_cntx.app[i].tilt_callback
				(SRV_SENSOR_DISABLE, 
				NULL,
				g_srv_sensor_motion_cntx.app[i].tilt_user_data);
		}

		if (g_srv_sensor_motion_cntx.app[i].direct_callback!= NULL)
		{
			g_srv_sensor_motion_cntx.app[i].direct_callback
				(SRV_SENSOR_DISABLE, 
				NULL,
				g_srv_sensor_motion_cntx.app[i].direct_user_data);
		}

		if (g_srv_sensor_motion_cntx.app[i].shake_callback!= NULL)
		{
			g_srv_sensor_motion_cntx.app[i].shake_callback
				(SRV_SENSOR_DISABLE, 
				NULL,
				g_srv_sensor_motion_cntx.app[i].shake_user_data);
		}
	}
}



/* Enable motion sensor, if ture enable , false disable */
void srv_sensor_motion_set_switch(MMI_BOOL is_enable)
{
	if (is_enable == MMI_TRUE)
	{
		if (g_motion_sensor_able == MMI_FALSE)
		{
			MMI_TRACE(MMI_MEDIA_TRC_G10_MMI_PRINT, MMI_TRC_MOTION_POWER_ON);
			srv_sensor_motion_power_on();
			g_srv_sensor_motion_cntx.is_shake_mute = MMI_TRUE;
			gui_start_timer(MOTION_SHAKE_DET_MUTE_DURATION, srv_sensor_motion_shake_det_mute_timeout);
			g_srv_sensor_motion_cntx.is_msg_send = MMI_FALSE;
#ifdef MDI_MOTION_DRIVER_AVAIALBE
			/* register callback to retrive data */
			motion_sensor_cb_registration(srv_sensor_motion_driver_data_state_hdlr, NULL);
			SetProtocolEventHandler(srv_sensor_motion_data_hdlr, MSG_ID_MDI_MOTION_DATA_STATE_RSP);
			motion_sensor_conf_filter(MOTION_THRESHOLD_FOR_DRV);
			/* star sampling */
			motion_sensor_sample(MMI_TRUE);

#endif /* MDI_MOTION_DRIVER_AVAIALBE */ 
		}

		g_motion_sensor_able = MMI_TRUE;
	}
	else
	{
		SetProtocolEventHandler(NULL, MSG_ID_MDI_MOTION_DATA_STATE_RSP);
		srv_sensor_motion_power_off();  //if all request are closed , power off motion
		srv_sensor_motion_notify_each_app();
		g_motion_sensor_able = MMI_FALSE;

	}
	sensor_srv_motion_write_switch(g_motion_sensor_able);
}

MMI_BOOL srv_sensor_motion_get_switch()
{
	return g_motion_sensor_able;
}




extern SRV_SENSOR_RESULT srv_sensor_motion_enable_mode(SRV_SENSOR_HANDLE handler , U16 sensor_mode)
{
	U32 i;

	for (i = 0; i<MOTION_SUPPORT_MAX_MODE; i++)
	{
		if (g_motion_sensor_tab[i].flag == 1 && g_motion_sensor_tab[i].hdlr == handler)
		{
			g_motion_sensor_tab[i].mode = g_motion_sensor_tab[i].mode | sensor_mode;
			return SRV_SENSOR_MOTION_SUCCEED;
		}
	}

	/* No same handler */
	if (i == MOTION_SUPPORT_MAX_MODE)
	{
		for (i = 0; i<MOTION_SUPPORT_MAX_MODE; i++)
		{
			if (g_motion_sensor_tab[i].flag == NULL) //empty slot
			{
				g_motion_sensor_tab[i].hdlr = handler;  //external handler
				g_motion_sensor_tab[i].mode =  sensor_mode;
				g_motion_sensor_tab[i].flag = 1;
				return SRV_SENSOR_MOTION_SUCCEED;
			}
		}
	}

	return SRV_SENSOR_MOTION_NO_SLOT;

}

extern SRV_SENSOR_RESULT srv_sensor_motion_disable_mode(SRV_SENSOR_HANDLE handler, U16 sensor_mode)
{	
	U32 i = 0;
	for (i = 0; i<MOTION_SUPPORT_MAX_MODE; i++)
	{
		if (g_motion_sensor_tab[i].hdlr == handler) 
		{
			kal_prompt_trace(MOD_MMI,"[mcube]disable mode");

			g_motion_sensor_tab[i].mode = g_motion_sensor_tab[i].mode & ~(sensor_mode);
			if (g_motion_sensor_tab[i].mode == SRV_SENSOR_MOTION_NORAML_MODE)
			{
				g_motion_sensor_tab[i].hdlr = NULL;
				g_motion_sensor_tab[i].flag = NULL;
			}
			break;
		}
	}

	if (i == MOTION_SUPPORT_MAX_MODE)
		return SRV_SENSOR_MOTION_INVALID_PARAM;
	else
		return SRV_SENSOR_MOTION_SUCCEED;
}

static void srv_sensor_motion_remove_mode(SRV_SENSOR_HANDLE handler)
{
	U32 i = 0;
	for (i = 0; i<MOTION_SUPPORT_MAX_MODE; i++)
	{
		if (g_motion_sensor_tab[i].hdlr == handler) //empty slot
		{
			kal_prompt_trace(MOD_MMI,"[mcube]Remove mode");
			g_motion_sensor_tab[i].hdlr = NULL;
			g_motion_sensor_tab[i].flag = NULL;
			g_motion_sensor_tab[i].mode = NULL;
			break;
		}

	}
}

static U16 srv_sensor_motion_check_mode()
{
	U32 i = 0;
	U16 sensor_mode = SRV_SENSOR_MOTION_NORAML_MODE;

	for (i = 0; i<MOTION_SUPPORT_MAX_MODE; i++)
	{
		if (g_motion_sensor_tab[i].flag == 1)
		{
			sensor_mode = sensor_mode | g_motion_sensor_tab[i].mode;
		}
	}

	return sensor_mode;	
}


#if 0
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
#endif



#ifdef MDI_MOTION_DRIVER_AVAIALBE
#pragma arm section code = "SECONDARY_ROCODE", rodata = "SECONDARY_RODATA"


static void srv_sensor_motion_cali_hdlr(void *msg_ptr)
{
	srv_sensor_motion_cali_msg_struct *msg_data_ptr;
	mmi_event_struct evt;

	msg_data_ptr = (srv_sensor_motion_cali_msg_struct *)msg_ptr;



	//kal_prompt_trace(MOD_MMI,"[Cali] srv_sensor_motion_cali_hdlr !!");

	switch (msg_data_ptr->cali_status)
	{
	case SRV_SENSOR_MOTION_CALI_FINISHED:

#ifdef MDI_MOTION_DRIVER_AVAIALBE
		motion_sensor_write_cali_to_nvram();
#endif /*MDI_MOTION_DRIVER_AVAIALBE*/

		MMI_FRM_INIT_EVENT(&evt, EVT_ID_SENSOR_MOTION_CALI_FINISHED);

		mmi_frm_cb_emit_post_event(&evt);
		break;
	case SRV_SENSOR_MOTION_CALI_TIMEOUT:
		MMI_FRM_INIT_EVENT(&evt, EVT_ID_SENSOR_MOTION_CALI_TIMEOUT);
		mmi_frm_cb_emit_post_event(&evt);
		break;

		/*
		case SRV_SENSOR_MOTION_CALI_NVRAM_WRITE_FAIL:
		MMI_FRM_INIT_EVENT(&evt, EVT_ID_SENSOR_MOTION_CALI_WRITE_FAIL);
		mmi_frm_cb_emit_post_event(&evt);
		break;
		*/
	}

}

srv_sensor_motion_cali_callback(void *parameter, Motion_Sensor_Cali_Status_enum state)
{
	/*----------------------------------------------------------------*/
	/* Local Variables												*/
	/*----------------------------------------------------------------*/
	MYQUEUE message;
	srv_sensor_motion_cali_msg_struct *msg_data_ptr;

	/*----------------------------------------------------------------*/
	/* Code Body														*/
	/*----------------------------------------------------------------*/
	/*
	if (g_srv_sensor_motion_cntx.is_msg_send == MMI_TRUE)
	{
	return;
	}
	*/

//	kal_prompt_trace(MOD_MMI,"[Cali] srv_sensor_motion_cali_callback !!");

	msg_data_ptr = OslConstructDataPtr(sizeof(srv_sensor_motion_cali_msg_struct));


	switch (state)
	{
	case MS_CALI_FINISHED:
		msg_data_ptr->cali_status = SRV_SENSOR_MOTION_CALI_FINISHED;
		break;

	case MS_CALI_TIMEOUT:
		msg_data_ptr->cali_status = SRV_SENSOR_MOTION_CALI_TIMEOUT;
		break;
	}
	message.oslMsgId = MSG_ID_SRV_SENSOR_MOTION_CALI_RSP;
	message.oslDataPtr = (oslParaType*) msg_data_ptr;
	message.oslPeerBuffPtr = NULL;
	// message.oslSrcId = MOD_DRV_HISR;
	message.oslSrcId = stack_get_active_module_id();
	message.oslDestId = MOTION_RUN_MOD;
	OslMsgSendExtQueue(&message);

}

#pragma arm section code, rodata


#endif /*MDI_MOTION_DRIVER_AVAIALBE*/


extern void srv_sensor_motion_start_cali()
{

#ifdef MDI_MOTION_DRIVER_AVAIALBE

	U32 i = 0;
	MMI_BOOL flag = MMI_TRUE;


	for (i=0; i<MOTION_SUPPORT_APP_NUM; i++)
	{
		if (srv_sensor_callback_handler_count(i) != 0)
		{
			flag = MMI_FALSE;
		//	kal_prompt_trace(MOD_MMI, "have app reg not power on");
			break;
		}
	}
	if (flag)
	{
		kal_prompt_trace(MOD_MMI, " need power on");
		motion_sensor_power(MMI_TRUE);
		//motion_sensor_conf_sample_period(MOTION_SAMPLE_PERIOD);
		motion_sensor_flush_buff();	

		motion_sensor_conf_filter(MOTION_THRESHOLD_FOR_DRV);
		/* star sampling */
		motion_sensor_sample(MMI_TRUE);
	}

	motion_sensor_cali_cb_registration(srv_sensor_motion_cali_callback);





	SetProtocolEventHandler(srv_sensor_motion_cali_hdlr, MSG_ID_SRV_SENSOR_MOTION_CALI_RSP);
	motion_sensor_start_cali();

	//kal_prompt_trace(MOD_MMI,"[Cali] srv_sensor_motion_start_cali !!");

	kal_prompt_trace(MOD_MMI,"Cali hdlr(srv) %x ",srv_sensor_motion_cali_callback);

#endif /*MDI_MOTION_DRIVER_AVAIALBE*/
}

extern void srv_sensor_motion_stop_cali()
{
	U32 i = 0;
	MMI_BOOL flag = MMI_TRUE;

	SetProtocolEventHandler(NULL, MSG_ID_SRV_SENSOR_MOTION_CALI_RSP);


	for (i=0; i<MOTION_SUPPORT_APP_NUM; i++)
	{
		if (srv_sensor_callback_handler_count(i) != 0)
		{
			flag = MMI_FALSE;
			break;
		}
	}

	if (flag)
	{
		srv_sensor_motion_power_off(); 
	}	
}


static MMI_BOOL srv_sensor_motion_set_flip_paremeters(srv_sensor_motion_flip_para_struct * para)
{
	/*----------------------------------------------------------------*/
	/* Local Variables												  */
	/*----------------------------------------------------------------*/
	mCubeLibInit_t initData;

	/*----------------------------------------------------------------*/
	/* Code Body													  */
	/*----------------------------------------------------------------*/ 
	initData.accelInit.accelOrientation = CHIP_TOP_RIGHT_DOWN;
	initData.accelInit.accelPolaBafrInit.enableUpdate = MMI_TRUE;
	initData.accelInit.accelPolaBafrInit.polaUpdRateHz = 10;

	g_mcube_init.flipEnable = 
		initData.accelInit.accelPolaBafrInit.flipCntrl.flipEnable = para->flipEnable;
	g_mcube_init.flipFilter = 
		initData.accelInit.accelPolaBafrInit.flipCntrl.flipFilter = para->flipFilter;
	g_mcube_init.flipThreshold = 
		initData.accelInit.accelPolaBafrInit.flipCntrl.flipThreshold = para->flipThreshold;
	g_mcube_init.tFlipDebounce = 
		initData.accelInit.accelPolaBafrInit.flipCntrl.tFlipDebounce = para->tFlipDebounce;
	g_mcube_init.tFlipInterval =
		initData.accelInit.accelPolaBafrInit.flipCntrl.tFlipInterval = para->tFlipInterval;

#ifdef __MTK_TARGET__

	if (g_is_reg_mcube == MMI_TRUE) //mcube is open,some app register mcube
	{
		mCube_LibClose();
		mCube_LibOpen(&initData);
	}

#endif

	return MMI_TRUE;

}

static MMI_BOOL srv_sensor_motion_get_flip_paremeters(srv_sensor_motion_flip_para_struct * para)
{
	para->flipEnable = g_mcube_init.flipEnable;
	para->flipFilter = g_mcube_init.flipFilter;
	para->flipThreshold = g_mcube_init.flipThreshold;
	para->tFlipDebounce = g_mcube_init.tFlipDebounce;
	para->tFlipInterval = g_mcube_init.tFlipInterval;
	return MMI_TRUE;
}



static MMI_BOOL srv_sensor_motion_get_tap_paremeters(srv_sensor_motion_get_tap_struct * ms_params)
{

#ifdef MDI_MOTION_DRIVER_AVAIALBE

	//srv_sensor_motion_get_tap_struct tap_plus;
	//srv_sensor_motion_get_tap_struct tap_threshold;

	MotionSensorQueryStruct tap_plus;
	MotionSensorQueryStruct tap_threshold;
	MMI_BOOL res1,res2;


	res1 = motion_sensor_get_sensor_params(MS_TAP_PULSE, &tap_plus);
	res2 = motion_sensor_get_sensor_params(MS_TAP_THRESHOLD, &tap_threshold);

	if (res1 && res2)
	{
		ms_params->get_tap_plus.curr_val = tap_plus.curr_val;
		ms_params->get_tap_plus.max_val = tap_plus.max_val;
		ms_params->get_tap_plus.min_val = tap_plus.min_val;

		ms_params->get_tap_threshold.curr_val = tap_threshold.curr_val;
		ms_params->get_tap_threshold.max_val = tap_threshold.max_val;
		ms_params->get_tap_threshold.min_val = tap_threshold.min_val;

		return MMI_TRUE;
	}
	else

#endif /*MDI_MOTION_DRIVER_AVAIALBE*/
		return MMI_FALSE;

}

static MMI_BOOL srv_sensor_motion_set_tap_parameters(srv_sensor_motion_set_tap_struct * ms_params)
{
	MMI_BOOL res = MMI_TRUE;

#ifdef MDI_MOTION_DRIVER_AVAIALBE

	res = motion_sensor_set_sensor_params(MS_TAP_PULSE, ms_params->set_tap_plus);
	if (res)
		res = motion_sensor_set_sensor_params(MS_TAP_THRESHOLD , ms_params->set_tap_threshold);
#endif /*MDI_MOTION_DRIVER_AVAIALBE*/

	return res;
}


extern MMI_BOOL srv_sensor_motion_set_parameters(srv_sensor_motion_set_type_enum type,void * para)
{
	switch (type)
	{
	case SRV_SENSOR_MOTION_SET_TAP:
		srv_sensor_motion_set_tap_parameters(para);
		return MMI_TRUE;
	case SRV_SENSOR_MOTION_SET_FLIP:
		srv_sensor_motion_set_flip_paremeters(para);
		return MMI_TRUE;
	default:
		break;
	}

	return MMI_FALSE;
}


extern MMI_BOOL srv_sensor_motion_get_parameters(srv_sensor_motion_set_type_enum type,void * para)
{
	switch (type)
	{
	case SRV_SENSOR_MOTION_SET_TAP:
		srv_sensor_motion_get_tap_paremeters(para);
		return MMI_TRUE;
	case  SRV_SENSOR_MOTION_SET_FLIP:
		srv_sensor_motion_get_flip_paremeters(para);
		return MMI_TRUE;
	default:
		break;
	}

	return MMI_FALSE;
}

#endif /*MOTION_SENSOR_SUPPORT*/







#ifdef __PXS_ENABLE__
/*****************************************************************************
* FUNCTION
*  srv_sensor_pxs_poll_hdlr
* DESCRIPTION
*  This function is the timer expiry to poll if detect object away
* PARAMETERS
*  void
* RETURNS
*  void
*****************************************************************************/ 
static void srv_sensor_pxs_poll_hdlr(void)
{
	/*----------------------------------------------------------------*/
	/* Local Variables                                                */
	/*----------------------------------------------------------------*/
	MMI_BOOL is_detected = MMI_FALSE;
	mmi_event_struct evt;

	/*----------------------------------------------------------------*/
	/* Code Body                                                      */
	/*----------------------------------------------------------------*/
	srv_sensor_pxs_get_status(&is_detected);

	if (is_detected == MMI_TRUE)
	{
		/* start the timer to poll the proximity again */
		StartTimer(SRV_PXS_TIMER_ID, SENSOR_PXS_REPEAT_POLLING_TIME, srv_sensor_pxs_poll_hdlr);
	}
	else
	{
		/* the proximity detect the object away, do the notification via callback manager */
		MMI_FRM_INIT_EVENT(&evt, EVT_ID_SENSOR_PXS_DETECT_OBJ_AWAY);
		mmi_frm_cb_emit_post_event(&evt);
	}
}


/*****************************************************************************
* FUNCTION
*  srv_sensor_pxs_detect_obj_hdlr
* DESCRIPTION
*  This function is the protocol event handler of MSG_ID_SRV_SENSOR_PXS_DETECT_OBJECT_IND
* PARAMETERS
*  msg :      [IN] the local param of ilm
* RETURNS
*  void
*****************************************************************************/ 
static void srv_sensor_pxs_detect_obj_hdlr(void *msg)
{
	/*----------------------------------------------------------------*/
	/* Local Variables                                                */
	/*----------------------------------------------------------------*/
	mmi_event_struct evt;

	/*----------------------------------------------------------------*/
	/* Code Body                                                      */
	/*----------------------------------------------------------------*/
	g_pxs_is_indication_in_queue = MMI_FALSE;

	MMI_TRACE(MMI_MEDIA_TRC_G10_MMI_PRINT, TRC_SRV_SENSOR_PXS_DET_OBJ_CLOSE);
	MMI_FRM_INIT_EVENT(&evt, EVT_ID_SENSOR_PXS_DETECT_OBJ_CLOSE);
	mmi_frm_cb_emit_post_event(&evt);

	/* We will start the timer to polling the proximity detect the object away. */
	StartTimer(SRV_PXS_TIMER_ID, SENSOR_PXS_FIRST_POLLING_TIME, srv_sensor_pxs_poll_hdlr);
}
#endif /*  __PXS_ENABLE__ */


/*****************************************************************************
* FUNCTION
*  srv_sensor_motion_init
* DESCRIPTION
*  init MDI Camera
* PARAMETERS
*  void
* RETURNS
*  MDI_RESULT
*****************************************************************************/
#if defined (MOTION_SENSOR_SUPPORT)||(__PXS_ENABLE__)
mmi_ret srv_sensor_init(mmi_event_struct *evt)
{
	/*----------------------------------------------------------------*/
	/* Local Variables                                                */
	/*----------------------------------------------------------------*/

	/*----------------------------------------------------------------*/
	/* Code Body                                                      */
	/*----------------------------------------------------------------*/

#ifdef MOTION_SENSOR_SUPPORT
	srv_sensor_motion_init(evt);
#endif

#ifdef __PXS_ENABLE__

	SetProtocolEventHandler(srv_sensor_pxs_detect_obj_hdlr, MSG_ID_SRV_SENSOR_PXS_DETECT_OBJECT_IND);

#endif /* __PXS_ENABLE__ */

	return MMI_RET_OK;
}
#endif /* (MOTION_SENSOR_SUPPORT)||( __PXS_ENABLE__) */


#pragma arm section code = "SECONDARY_ROCODE", rodata = "SECONDARY_RODATA"
/*****************************************************************************
* FUNCTION
*  srv_sensor_pxs_cb
* DESCRIPTION
*  This function is the notify callback of PXS
* PARAMETERS
*  fgObjectDetected :      [IN] detect the object close or away
* RETURNS
*  void
*****************************************************************************/ 
#ifdef __PXS_ENABLE__
static void srv_sensor_pxs_cb(DCL_BOOL fgObjectDetected)
{
	/*----------------------------------------------------------------*/
	/* Local Variables                                                */
	/*----------------------------------------------------------------*/
	//ilm_struct *pxs_ilm;
	ilm_struct pxs_ilm;

	/*----------------------------------------------------------------*/
	/* Code Body                                                      */
	/*----------------------------------------------------------------*/

	if (DCL_TRUE == fgObjectDetected && MMI_FALSE == g_pxs_is_indication_in_queue)
	{
		/*
		* When the priximity detect the object close and MMI queue doesn't have
		* MSG_ID_SRV_SENSOR_PXS_DETECT_OBJECT_IND, we will send the primitive.
		*/
		g_pxs_is_indication_in_queue = MMI_TRUE;
		//DRV_BuildPrimitive(pxs_ilm, MOD_DRV_HISR, MOD_MMI, MSG_ID_SRV_SENSOR_PXS_DETECT_OBJECT_IND, NULL);
		// msg_send_ext_queue(pxs_ilm);

		pxs_ilm.oslMsgId = MSG_ID_SRV_SENSOR_PXS_DETECT_OBJECT_IND;
		pxs_ilm.oslDataPtr = NULL;
		pxs_ilm.oslPeerBuffPtr = NULL;
		pxs_ilm.oslSrcId = MOD_DRV_HISR;
		pxs_ilm.oslDestId = MOD_MMI;
		pxs_ilm.sap_id = DRIVER_PS_SAP;

		OslMsgSendExtQueue(&pxs_ilm);
	}
}
#endif /*  __PXS_ENABLE__ */
#pragma arm section code, rodata

#ifdef __PXS_ENABLE__

static void srv_sensor_pxs_set_debounce(void * val)
{
	/*----------------------------------------------------------------*/
	/* Local Variables                                                */
	/*----------------------------------------------------------------*/	
	DCL_HANDLE	 pxs_handle;
	PXS_CTRL_SET_DEBOUNCE_T pxs_debounce;
	srv_sensor_pxs_set_debounce_struct *srv_sensor = (srv_sensor_pxs_set_debounce_struct *)val;

	/*----------------------------------------------------------------*/
	/* Code Body                                                      */
	/*----------------------------------------------------------------*/
	pxs_handle = DclPXS_Open(DCL_PXS, NULL);

	pxs_debounce.u4TopAreaDebounce = (DCL_UINT32)(srv_sensor->top_area_debounce);          //5000ms
	pxs_debounce.u4LargerAreaDebounce = (DCL_UINT32)(srv_sensor->larger_area_debounce);    //0ms

	DclPXS_Control(pxs_handle, PXS_CMD_SET_DEBOUNCE, (DCL_CTRL_DATA_T *)&pxs_debounce);
	DclPXS_Close(pxs_handle);

}

/*****************************************************************************
* FUNCTION
*  srv_sensor_pxs_set_value
* DESCRIPTION
*  This function is to set value
* PARAMETERS
*	type:      [IN]For example SRV_SENSOR_PXS_SET_DEBOUNCE
*  val:       pointer of parameter
* RETURNS
*  MMI_BOOL
*****************************************************************************/ 
extern MMI_BOOL srv_sensor_pxs_set_value(srv_sensor_pxs_set_param_type_enum type,void *val)
{
	/*----------------------------------------------------------------*/
	/* Local Variables                                                */
	/*----------------------------------------------------------------*/
	U32 i = 0;

	/*----------------------------------------------------------------*/
	/* Code Body                                                      */
	/*----------------------------------------------------------------*/
	for (i=0; i<SRV_SENSOR_PXS_PARAM_TYPE_TOTAL; i++)
	{
		if (g_sensor_srv_pxs_set_val_tab[i].pxs_set_type == type)
		{
			g_sensor_srv_pxs_set_val_tab[i].internal_set_handler(val);
			break;
		}
	}
	if (SRV_SENSOR_PXS_PARAM_TYPE_TOTAL == i)
	{
		return MMI_FALSE;
	}
	return MMI_TRUE;
}

#endif /*  __PXS_ENABLE__ */




/*****************************************************************************
* FUNCTION
*  srv_sensor_pxs_turn_on
* DESCRIPTION
*  This function is to turn on the proximity device
* PARAMETERS
*  void
* RETURNS
*  MMI_TRUE - turn on the proximity device successfully
*  MMI_FALSE - turn on the proximity device fail
*****************************************************************************/ 
MMI_BOOL srv_sensor_pxs_turn_on(void)
{
#ifdef __PXS_ENABLE__
	/*----------------------------------------------------------------*/
	/* Local Variables                                                */
	/*----------------------------------------------------------------*/
	DCL_STATUS          pxs_status = STATUS_FAIL;
	PXS_CTRL_ENABLE_T   dcl_ctrl_pxs_enable;
	PXS_CTRL_CONFIG_T   dcl_ctrol_pxs_config;
	mmi_event_struct    evt;

	/*----------------------------------------------------------------*/
	/* Code Body                                                      */
	/*----------------------------------------------------------------*/
	if(g_pxs_user_count == 0)
	{
		/* proximity sensor init */
		if (g_pxs_handle > DCL_HANDLE_NONE)
		{
			DclPXS_Close(g_pxs_handle);
		}
		/* get the proximity instance */
		g_pxs_handle = DclPXS_Open(DCL_PXS, 0);
		/* Could get the proximity device */
		MMI_TRACE(MMI_MEDIA_TRC_G10_MMI_PRINT, TRC_SRV_SENSOR_PXS_OPEN_DEVICE, g_pxs_handle);

		if (g_pxs_handle > DCL_HANDLE_NONE)
		{
			dcl_ctrl_pxs_enable.fgEnable = DCL_TRUE;
			pxs_status = DclPXS_Control(g_pxs_handle, PXS_CMD_ENABLE, (DCL_CTRL_DATA_T  *) &dcl_ctrl_pxs_enable);
			dcl_ctrol_pxs_config.fgNotify = DCL_TRUE;
			dcl_ctrol_pxs_config.NotifyCallback = srv_sensor_pxs_cb;
			pxs_status = DclPXS_Control(g_pxs_handle, PXS_CMD_MODIFY_CONFIG, (DCL_CTRL_DATA_T  *) &dcl_ctrol_pxs_config);
		}

		MMI_TRACE(MMI_MEDIA_TRC_G10_MMI_PRINT, TRC_SRV_SENSOR_PXS_TURN_ON, g_pxs_handle, pxs_status);
		if (pxs_status < 0)
		{
			g_pxs_res_status = MMI_FALSE;
		}
		else
		{
			MMI_FRM_INIT_EVENT(&evt, EVT_ID_SENSOR_PXS_TURN_ON_NOTIFY);
			mmi_frm_cb_emit_post_event(&evt);
			g_pxs_res_status =  MMI_TRUE;
		}
		// kal_prompt_trace(MOD_MMI, "[SRV_SENSOR_PXS] TURN ON! %d",g_pxs_res_status);
	}
	g_pxs_user_count++;

	// kal_prompt_trace(MOD_MMI, "[SRV_SENSOR_PXS] g_pxs_user_count %d",g_pxs_user_count);
	return g_pxs_res_status;
#else
	return MMI_FALSE;
#endif /*  __PXS_ENABLE__ */
}


/*****************************************************************************
* FUNCTION
*  srv_sensor_pxs_turn_off
* DESCRIPTION
*  This function is to turn off the proximity device
* PARAMETERS
*  void
* RETURNS
*  void
*****************************************************************************/ 
void srv_sensor_pxs_turn_off(void)
{
#ifdef __PXS_ENABLE__
	/*----------------------------------------------------------------*/
	/* Local Variables                                                */
	/*----------------------------------------------------------------*/
	DCL_STATUS          pxs_status;
	PXS_CTRL_ENABLE_T   dcl_ctrl_pxs_enable;
	mmi_event_struct    evt;

	/*----------------------------------------------------------------*/
	/* Code Body                                                      */
	/*----------------------------------------------------------------*/
	g_pxs_user_count--;
	//kal_prompt_trace(MOD_MMI, "[SRV_SENSOR_PXS] g_pxs_user_count %d",g_pxs_user_count);
	if (g_pxs_user_count == 0)
	{
		MMI_TRACE(MMI_MEDIA_TRC_G10_MMI_PRINT, TRC_SRV_SENSOR_PXS_TURN_OFF, g_pxs_handle);
		StopTimer(SRV_PXS_TIMER_ID);
		if (g_pxs_handle > DCL_HANDLE_NONE)
		{
			dcl_ctrl_pxs_enable.fgEnable = DCL_FALSE;
			pxs_status = DclPXS_Control(g_pxs_handle, PXS_CMD_ENABLE, (DCL_CTRL_DATA_T  *) &dcl_ctrl_pxs_enable);
		}

		/* reset PXS handle */
		DclPXS_Close(g_pxs_handle);
		g_pxs_handle = DCL_HANDLE_NONE;
		MMI_FRM_INIT_EVENT(&evt, EVT_ID_SENSOR_PXS_TURN_OFF_NOTIFY);
		mmi_frm_cb_emit_post_event(&evt);

		g_pxs_res_status = MMI_FALSE;
		// kal_prompt_trace(MOD_MMI, "[SRV_SENSOR_PXS] TURN OFF! %d",g_pxs_res_status);
	}

#endif /*  __PXS_ENABLE__ */
}


/*****************************************************************************
* FUNCTION
*  srv_sensor_pxs_get_status
* DESCRIPTION
*  This function is to get the object status from the proximity device
* PARAMETERS
*  fgObjectDetected :  [OUT] get the if detect the object close
* RETURNS
*  MMI_TRUE - get the status of the proximity device successfully
*  MMI_FALSE - get the status of the proximity device fail
*****************************************************************************/ 
MMI_BOOL srv_sensor_pxs_get_status(MMI_BOOL *obj_detected)
{
#ifdef __PXS_ENABLE__
	/*----------------------------------------------------------------*/
	/* Local Variables                                                */
	/*----------------------------------------------------------------*/
	DCL_STATUS pxs_status = STATUS_FAIL;
	PXS_CTRL_GET_DETECT_STATUS_T dcl_ctrl_pxs_get_detect_status;

	/*----------------------------------------------------------------*/
	/* Code Body                                                      */
	/*----------------------------------------------------------------*/
	/* the default value is not non-detected object */
	*obj_detected = MMI_FALSE;


	if (g_pxs_handle > DCL_HANDLE_NONE)
	{
		pxs_status = DclPXS_Control(g_pxs_handle, PXS_CMD_GET_DETECT_STATUS, (DCL_CTRL_DATA_T  *) &dcl_ctrl_pxs_get_detect_status);
		*obj_detected = (MMI_BOOL)dcl_ctrl_pxs_get_detect_status.fgDetected;
	}

	MMI_TRACE(MMI_MEDIA_TRC_G10_MMI_PRINT, TRC_SRV_SENSOR_PXS_GET_STATUS, g_pxs_handle, *obj_detected, pxs_status);
	return (pxs_status < 0)? MMI_FALSE : MMI_TRUE;
#else
	return MMI_FALSE;
#endif /*  __PXS_ENABLE__ */
}


/*****************************************************************************
* FUNCTION
*  srv_sensor_pxs_get_raw_data
* DESCRIPTION
*  This function is to get PXS raw data
* PARAMETERS
*  em_param :  [OUT] raw data
* RETURNS
*  MMI_TRUE - get PXS raw data successfully
*  MMI_FALSE - get PXS raw data fail
*****************************************************************************/ 
extern MMI_BOOL srv_sensor_pxs_get_raw_data(U16 *em_param)
{
#ifdef __PXS_ENABLE__
	/*----------------------------------------------------------------*/
	/* Local Variables                                                */
	/*----------------------------------------------------------------*/
	DCL_STATUS pxs_status = STATUS_FAIL;
	PXS_CTRL_RAW_DATA_T raw_data = {0};

	/*----------------------------------------------------------------*/
	/* Code Body                                                      */
	/*----------------------------------------------------------------*/
	if (g_pxs_handle > DCL_HANDLE_NONE)
	{
		pxs_status = DclPXS_Control(g_pxs_handle, PXS_CMD_GET_RAW_DATA, (DCL_CTRL_DATA_T  *) &raw_data);
		*em_param = raw_data.u2Data;
	}

	MMI_TRACE(MMI_MEDIA_TRC_G10_MMI_PRINT, TRC_SRV_SENSOR_PXS_GET_RAW_DATA, *em_param, pxs_status);
	return (pxs_status < 0)? MMI_FALSE : MMI_TRUE;
#else
	return MMI_FALSE;
#endif /*  __PXS_ENABLE__ */
}




#if 0
/* under construction !*/
/* under construction !*/
#ifndef __SRV_SENSOR_PXS_UT__
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
#else
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
#endif
/* under construction !*/
/* under construction !*/
#endif /* if 0 */


#ifdef MOTION_SENSOR_SUPPORT

/*Temp API to avoid link error*/

void UTL_AssertFailed(
					  char const * const fileName,
					  uint32_t           lineNumber)
{

	(void)fileName;
	(void)lineNumber;

#if defined(ANDROID)
	__assert(fileName, lineNumber, "");

#elif ((TOOLCHAIN == TOOLCHAIN_gcc) || (TOOLCHAIN == TOOLCHAIN_ads))
	/* TODO */

#else
	_assert(0, fileName, lineNumber);

#endif

	return;

}  /* UTL_AssertFailed() */

/* Close NVM file */
mCubeLibResult_t mCube_StorageClose(mCubeStorageId_t nvmId)
{
	mCubeLibResult_t retVal = MCUBE_LIB_SUCCESS;

	// mcube_motion_pal_dbg_print("%s: called\r\n", __func__);

	return retVal;
}


/* Initialize the storage module by closing any previously opened
* NVM files prior to invalidating the descriptors.
*/
mCubeLibResult_t mCube_StorageInit(void)
{
	mCubeLibResult_t retVal = MCUBE_LIB_SUCCESS;

	//mcube_motion_pal_dbg_print("%s: called\r\n", __func__);

	return retVal;
}


/* Open specified storage device */
mCubeLibResult_t mCube_StorageOpen(mCubeStorageId_t nvmId)
{
	mCubeLibResult_t retVal = MCUBE_LIB_SUCCESS;

	// mcube_motion_pal_dbg_print("%s: called\r\n", __func__);

	return retVal;
}

mCubeLibResult_t mCube_StorageRead(uint16_t nvmId,
								   uint32_t offset,
								   uint8_t *data,
								   uint32_t len)
{
	//mCubeLibResult_t retVal = MCUBE_LIB_SUCCESS;
	mCubeLibResult_t retVal = MCUBE_LIB_FAILURE;

	// mcube_motion_pal_dbg_print("%s: called\r\n", __func__);

	return retVal;
}

mCubeLibResult_t mCube_StorageWrite(uint16_t nvmId,
									uint32_t offset,
									uint8_t *data,
									uint32_t len)
{
	//mCubeLibResult_t retVal = MCUBE_LIB_SUCCESS;
   	mCubeLibResult_t retVal = MCUBE_LIB_FAILURE;

	//mcube_motion_pal_dbg_print("%s: called\r\n", __func__);

	return retVal;
}

/* Calculates the CRC32 value for the given input data utilizing
* a pre-generated ROM table (crc32Table).  In order to utilize
* a different polynomial, the CRC table must be re-generated.
*/
uint32_t UTL_CalcCrc32(uint8_t *dataPtr, int length)
{
	/*
	uint8_t tabOffset;
	int i;
	uint32_t crcResult = CRC32_INIT_VALUE;

	for(i = 0; i < length; i++)
	{
	tabOffset = dataPtr[i] ^ (crcResult >> 24);
	crcResult = (crcResult << 8) ^ crc32Table[tabOffset];
	}
	*/
	return 1;
}



#endif /*MOTION_SENSOR_SUPPORT*/