bt_host_main.c 47.5 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
#include "os_config.h"

#include "c_def.h"
#include "debug.h"
#include "oem.h"

#include "include/bt_def.h"
#include "include/bluetooth.h"
#include "include/hci.h"

#include "regmap.h"

#include "hw_dma.h"
#include "hw_pll.h"

#include "fifo.h"
#include "ht_hci_main.h"

#include "bt_lib_mask.h"
#include "malloc.h"
#include "audio_com.h"
#include "audio_dec.h"
#include "app_cmd.h"
#include "app_main.h"
#include "app_dac.h"
#include "app_sdram.h"
#include "optek_link.h"
#include "hw_timer.h"
#include "include/bt_def.h"
#include "include/bluetooth.h"
#include "include/hci_core.h"
#include "bt_common.h"


BT_STATUS_s bt_status;

extern struct bt_search_data *pSearchData;
extern FIFO *hfp_upstream_fifo;

const char *bt_lmp_data_get(void);
const char *bt_lmp_time_get(void);
const char *bt_lmp_version_get(void);

const char *bt_hci_data_get(void);
const char *bt_hci_time_get(void);
const char *bt_hci_version_get(void);

bool BtHCI_hfp_is_calling(void);

void app_nav_bt_codec_reinit (void);

extern U32 __bt_conn_text_rom_start[];
extern U32 __bt_conn_text_ram_start[];
extern U32 __bt_conn_text_ram_end[];

int app_bt_conn_code_text_copy(void)
{
	int len;
	
	if (app_main_data.iram_code_flag != SHARE_IRAM_BT_CONN_CODE)
	{
		len = __bt_conn_text_ram_end - __bt_conn_text_ram_start;
		CFasm_memcpy(__bt_conn_text_ram_start, __bt_conn_text_rom_start, len*4);
		app_main_data.iram_code_flag = SHARE_IRAM_BT_CONN_CODE;
	}
}

u8 codec_trans_id;
void app_dec_optek_psm_reinit (void)
{
	TX_INTERRUPT_SAVE_AREA;
	
	TX_DISABLE;
	codec_trans_id++;
	TX_RESTORE;	

}

u8 multiroom_debug;

void hci_read_clock_after_stream (void);


void hci_read_bt_hci_clock_sink(void);
void hci_read_bt_hci_clock_src(void);

void hci_read_bt_hci_clock_cb(void)
{
	memset(pSearchData,0,sizeof(struct bt_search_data));
}

u8 bt_clock_src_is_valid(void);

u8 bt_clock_sink_is_valid (void);

/*
	return 0, change to no ntp sync mode
	return 1, continue ntp sync mode
*/
u8 sbc_sync_continue_cb (void)
{

}

/*
	return 1->clock sink is not valid, need continue check
	return 2->clock is not valid with sink clock
	return 0->clock is valid with sink clock
*/

u8 avdtp_check_ntp_cb (u32 nt)
{

}

#define MOBILEPHONE_NTP
#define MOBILEPHONE_A2DP_DELAY	230		//ms
#define MOBILEPHONE_A2DP_DELAY_wo_NTP	(76*512)		/*76*512(2.9) ms*/
#define MAX_MULTIROOM_SYNC_BUF_SIZE		(96*512)			//0x8000			/*96*2.9 ms*/
#define MAX_MULTIROOM_SYNC_BUF_INT_SIZE		(MAX_MULTIROOM_SYNC_BUF_SIZE - 8*512)


/*called by avdtp, return delay report val, unit is 1/10 ms*/
u16 avdtp_get_delay_report_cb(void)
{
	return (MOBILEPHONE_A2DP_DELAY*10);						//1/10ms				
}

u32 dec_get_no_ntp_delay(void)
{
	return MOBILEPHONE_A2DP_DELAY_wo_NTP;						//1/10ms				
}

u32 dec_get_max_multirom_sync_buf_size(void)
{
	return MAX_MULTIROOM_SYNC_BUF_SIZE;						//1/10ms				
}




U32 app_dec_a2dp_stream_write(U8 *ptr, U32 len, int nt, u8 frames) __attribute__ ((section (".internal_ram_1_text")));

/*
	if nt = -1, no time stamp
*/
extern int decode_type;
U32 app_dec_a2dp_stream_write(U8 *ptr, U32 len, int nt, u8 frames)
{
	U32 xlen;

	if(BtHCI_hfp_is_calling())
		return;
//	if (app_main_data.playing_stream_status == STREAM_INIT)
//		return;

//	if(app_main_data.media != MEDIA_BT_HCI)
//		return 0;

	if (decode_type != DECODE_SBC)
		return;
	
	if (AUDIO_FIFO_STREAM_IS_VAILD() == FALSE)
	{
		return;
	}
#if 0
	u32 fifo_wlen;
	BT_TIME_STAMP bt_time_stamp;


	if (bt_multiroom_stream_enable == 0)
		return;

	

	if (app_main_data.media != MEDIA_BT_HCI)
	{
		return 0;
	}

	if(hfp_ring||hfp_calling)
		return;

	if (bt_clock_sink_is_valid() == FALSE) {
		//DBG_Printf ("Sink clk is invalid\n\r");
		return 0;
	}

	//DBG_Printf ("SBC pack len %d\n\r", len);
	//return;

	if (wCodecBusy == FALSE) {
		//waiting data
		if (len < 160) {
			//only one frame 
			return 0;
		}
	}

	if (nt != -1) {

#ifdef MOBILEPHONE_NTP
	
				
#if 0			//checking abs delay
		otk_stream_cn++;
		//if (otk_stream_cn > 400  && otk_stream_cn < 408) {
		if (otk_stream_cn % 200 == 0) {
			u32 optek_ts;				/*625/8=78.125us/oneunit*/
			u32 optek_ts_ms;

			optek_ts = bt_clock_read_sink();
			optek_ts_ms = ((optek_ts/8) * 5)/8;

			otk_stream_cn = 1;
			//should be synced with Bluetooth Timer, but now only 0?
			DBG_Printf ("NTP Sink 0x%x(RTP TS) 0x%x(BT CLK)\n\r",nt,bt_clock_read_sink());
			//DBG_Printf ("NTP Sink 0x%x(DT) 0x%x(BT)\n\r",nt,hci_clk_sink_debug);
			DBG_Printf ("A2DP Stream abs delay %d\n\r", (optek_ts_ms-nt));
		}
#endif

		nt += MOBILEPHONE_A2DP_DELAY;
		//nt = ((nt * 16 *4)/5);			//optek bt time
		nt = (int) ((((unsigned int)nt * 32)/5) * 2);				//nt is about 27 bit
		nt &= 0x3FFFFFFF;						//30 bit local bt clock

#else		//MOBILEPHONE_NTP
		
		nt = 0;			//study later

#endif		//MOBILEPHONE_NTP
	}
	else {
		nt = 0;
	}


	if ( (sdp_get_mask() & BT_APP_NEED_MASTER) != 0x0) {
		//device is master, no ntp
		nt = 0;
	}

	bt_time_stamp.time = nt;
	bt_time_stamp.frame = frames;
		
	fifo_wlen = fifo_put_data(&multirom_clock_fifo,&bt_time_stamp,sizeof(BT_TIME_STAMP));
		DBG_Assert (fifo_wlen == sizeof(BT_TIME_STAMP));


	if (sink_stream_started == 0) {
		sink_stream_started = 1;
		sink_stream_decoding_start_time = nt;
	}
#endif

#if 1	
	xlen = (U32) AUDIO_FIFO_STREAM_PUT_DATA (ptr, len);

	if (xlen != len)
	{
		//asm ("break 1,1");
		DBG_Printf("A2DP Stream overflow\n\r");
		//codec err, dead???

		//codec err, dead???
		//ukMsgSend(app_nav_bt_codec_reinit);			
		//return;
	}
#endif

#if 1	
	xlen = (U32) AUDIO_FIFO_STREAM_DATA_LEN ();


	if (app_main_data.playing_stream_status == STREAM_DEC_INIT || app_main_data.playing_stream_status == STREAM_WAITING_DATA) 
	{
		if (xlen > AUDIO_FIFO_STREAM_FIFO_LEN()*3/4)
		{
			//xEventGroupSetBits(event_grop, STREAM_EVENT);
			uDecSend(DECODE_FRAME,TRUE);
			app_main_data.playing_stream_status = STREAM_MEDIA;
		}
	}
#endif	
}


#define OTK_PSM_HEADER_SIZE		13			//same as RTP header, should be same as otk_psm.c

void otk_psm_head_vol (void);
void otk_psm_head_eq (void);
void otk_psm_head_srf (u16 srf);

extern U16 pre_audio_freq;


typedef struct {
	unsigned char cmd				:	1;
	unsigned char volume			:	7;

	unsigned char node				:	6;				/*0-63*/
	unsigned char media				:	2;				/*aux 0, bt 1*/

	unsigned char eq				:	4;
	unsigned char audio_sf			:	4;				/*should be 44100 or 48000?*/
	unsigned char mute				:	1;
	unsigned char codec_trans_id	:	7;
} CSRC_HEADER __attribute__((packed));

extern u32 i_samp_freq;


//the following three functions are called by sbcdec for using pcm down sample enc later
u32 Optek_psm_stream_send_for_BT(void *data, u32 len, u32 srf);
void Optek_psm_set_send_package_len_by_frame_len (u16 len);
void Optek_psm_send_trigger_by_sbcdec (void);

void Optek_psm_stream_tx_by_sbcdec(u8 *buf, u16 len)
{
}

void Optek_psm_set_send_package_len_by_frame_len_by_sbcdec(u16 len)
{
}


void Optek_psm_send_trigger_by_sbcdec (void)
{
}


u8 clock_diff_err_num;

void Optek_psm_stream_pll_adj (i32 diff) 
{

}

#define MIN_NTP_MARGIN	(512*6*2)		//about 1 a2dp stream

//only for bt src
U32 Optek_psm_stream_ntp_cb (void)
{

}

#define BT_NODE_DELAY 0//50			//50*625/8 = 3.9ms
u16 rx_2_tx_debug = 0;

void Optek_psm_rx_2_tx_stream_header_cb (u8 *pbuf)
{

}


void Optek_psm_stream_header_cb (u8 *pbuf, u32 ntp_time)
{

}


void otk_psm_ui_send_err(void);

u8 bt_clock_sink_is_valid (void);

U32 app_dec_multi_speaker_stream_write(U8 *ptr, U32 len)
{

}

void app_dec_sbc_adjust_fini (void)
{
}


/* page: 0 page and discover off, 1 discover on and page off, 2 discover off and page on, 3 discover on and page on*/
/* fast: 0 fast connection disable, 1 fast connection disable*/
void kBtHCI_page(u8 page, u8 fast);
void app_bt_set_scan_enable_mode(U8 scan_mode)
{
	switch (scan_mode)
	{
		case enBT_BOTH_SCAN_DISABLE:
			DBG_Printf ("non-connectable and non-discaverable\n\r");
			kBtHCI_page(0,0);
			break;
		case enBT_INQUIRY_SCAN_ENABLE:
			DBG_Printf ("discaverable\n\r");
			kBtHCI_page(1,1);
			break;
		case enBT_PAGE_SCAN_ENABLE:
			DBG_Printf ("connectable\n\r");
			kBtHCI_page(2,0);
			break;
		case enBT_BOTH_SCAN_ENABLE:
			DBG_Printf ("connectable and discaverable\n\r");
			kBtHCI_page(3,1);
			break;
		default:
			DBG_Printf ("non-connectable and non-discaverable\n\r");
			kBtHCI_page(0,0);
			break; 
	}
	bt_status.bt_scan_mode = scan_mode;
}


enum {
	HP_NO_SEARCHING = 0,
		HP_SEARCHING,
		HP_REQ_SEARCHING
};

u8 hp_searching = HP_NO_SEARCHING;

//for forcing bt diconnect
U8 bt_force_disconnect = FALSE;

//for forcing bt reconnect
U8 bt_froce_pairing;

//for judging connect type
U8 bt_connect_type = 0;

U8 bt_auto_diconnect = FALSE;
U8 bt_auto_connect_from_source;
U8 bt_avrcp_tick = 0;

/************************************/
/************************************/
u8 a2dp_sample_freq;
extern u8 g_rx_present;

u8 hp_conn_req;

//U8 bt_hci_main_status;
//U8 bt_hci_play_status;
/* play status */
/*
#define AVRCP_PLAY_STATUS_STOPPED   0x00
#define AVRCP_PLAY_STATUS_PLAYING   0x01
#define AVRCP_PLAY_STATUS_PAUSED    0x02
#define AVRCP_PLAY_STATUS_FWD_SEEK  0x03
#define AVRCP_PLAY_STATUS_REV_SEEK  0x04
#define AVRCP_PLAY_STATUS_ERROR     0xFF
#define AVRCP_PLAY_STATUS_UNKNOWN   0xFE			//added by Optek
*/


const char *bt_hci_data_get(void);
const char *bt_hci_time_get(void);
const char *bt_hci_version_get(void);


u8 avrcp_get_status (void);
int hci_Optek_PSM_conn_present(void);
int hci_A2DP_conn_present(void);

void uiBtHCI_Optek_module_link(u8 chan);
void uiBtHCI_Optek_module_tx_link(u8 chan);
void uiBtHCI_set_inquiry(void);
u8 bt_flash_is_btaddr_present(void);
u8 bt_flash_is_multi_speaker_btaddr_present(void);
void kBtHCI_adv_enable(void);
void kBtHCI_adv_disable(void);
void optek_link_role_init(void);
extern U8 NVDS_buffer[0x800];

extern const u8 tx_bdaddr[];
void app_bt_init(void)
{
	U32 cnt = 0;
	int temp;

	DBG_Printf("hci ver:%s,build time:%s %s\r\n",bt_hci_version_get(),bt_hci_time_get(),bt_hci_data_get());
	hfp_upstream_fifo = NULL;
	//debug
	malloc_init();

	memset(&bt_status,0,sizeof(bt_status));

	hci_dev_init();
	Optek_psm_init();  //lizong

	temp = 0;

	//init sdp db
	sdp_init();


	temp |= BT_RF_OTK5288;

#ifdef BT_VOLUME_SYC
	temp |= AVRCP_VOL_SYNC_MASK;
#endif	

#ifdef BT_TX_CHAN
	//temp |= BT_TX_APP;
	//temp |= BT_NAME_EXT;
	//temp |= BT_APP_NEED_MASTER;
#endif

#ifdef BT_HFP_ENABLE
	temp |= SDP_HFP_MASK;
	temp |= BT_HFP_BATT_LEV; //optional   , report local batt lev to phone 
#endif

#ifdef BT_OUTOPLAY_ENABLE
	//temp |= AVRCP_AUTOPLAY_MASK;
#endif

#ifdef BT_W_EEPROM
	//temp |= BT_w_EEPROM;
#endif

	temp |= BLE_EXT_ADV_MASK;

	sdp_set_mask(temp);		

	if ( (sdp_get_mask() & BT_w_EEPROM) != 0x0) 
	{
		//init bt eeprom data
		app_nav_get_bt_data();
	}

#ifdef BT_HEADPHONE
	bt_set_hp_enable();
#else
	bt_set_hp_disable();
#endif
	
	hp_conn_req = 0;			
	//app_main_data.tws_status = 0;

	bt_status.bt_main_status = enBT_OPENING;

	U8 *p;
	p = bt_flash_get_btaddr();
	//p = tx_bdaddr;
	
	CFasm_memcpy(&NVDS_buffer[7],p,6);

retry:
	DBG_Printf("bt hci init start\r\n");	
	ht_hci_init ();
	if (ht_hci_open () != 0) {
		if (cnt++ < 3)
		{
			DBG_Printf ("BT HCI open err, retry again\n\r");
			goto retry;
		}
		bt_status.bt_main_status = enBT_ERROR;
		DBG_Printf("bt hci init fail!\r\n");
		DBG_Assert (FALSE);
	}
	else {
		ht_hci_set_multi_speaker_mode(BT_NO_MULTI_SPEAKER);

		//debug
//		kBtHCI_adv_enable();
		//delayms(5000);
		#ifdef OPTEK_LINK_ENABLE
		if (app_main_data.share_link_role != SL_ROLE_BT)
		{
			optek_link_role_init();

			#ifdef LC3_ENCODE_ENABLE
			app_lc3_encoding_code_text_copy_init();
			int app_lc3_encoding_code_text_copy(void);
			app_lc3_encoding_code_text_copy();
			#endif

			#ifdef LC3_DECODE_ENABLE
			app_lc3_decoding_code_text_copy_init();
			#endif

		}
		#endif

//		uiBtHCI_relink();	
		bt_status.bt_main_status = enBT_DISCONNECTED;
		bt_status.bt_avrcp_status = enBT_AVRCP_UNKNOWN;
		DBG_Printf("bt hci init success\r\n");
	}
}

void app_nav_detach(void)
{

}
void a2dp_status_change_notify(u8 status)
{
	bt_status.bt_a2dp_status = status;
	DBG_Printf("a2dp status change notify, new status:%d\r\n",bt_status.bt_a2dp_status);
}

void avrcp_status_change_notify(u8 play_status)
{
	bt_status.bt_avrcp_status = play_status;
	DBG_Printf("play status change cb new status:%d\r\n",bt_status.bt_avrcp_status);
}

void avrcp_track_change_notify(void)
{
	asm ("NOP");
}

/*
int app_nav_bt_status_get(void)
{
	int temp;
	int ret;

	temp = avrcp_get_status ();

	if (bt_status.bt_avrcp_status != temp)
	{
		bt_status.bt_avrcp_status = temp;
		DBG_Printf("detect avrcp new status:%d\r\n",bt_status.bt_avrcp_status);
	}

	return bt_status.bt_avrcp_status;
}
*/

static const char *metadata_to_str(u32 id)
{
#if 0
	switch (id)
	{
	case AVRCP_MEDIA_ATTRIBUTE_TITLE:
		return "Title";
	case AVRCP_MEDIA_ATTRIBUTE_ARTIST:
		return "Artist";
	case AVRCP_MEDIA_ATTRIBUTE_ALBUM:
		return "Album";
	case AVRCP_MEDIA_ATTRIBUTE_GENRE:
		return "Genre";
	case AVRCP_MEDIA_ATTRIBUTE_TRACK:
		return "TrackNumber";
	case AVRCP_MEDIA_ATTRIBUTE_N_TRACKS:
		return "NumberOfTracks";
	case AVRCP_MEDIA_ATTRIBUTE_DURATION:
		return "Duration";
	}
	
	return NULL;
#endif
}

//please note, this function is called by BT stack
void avrcp_parse_attribute_list_cb(u8 *operands, u8 count)
{

}


//debug
void app_cmd_ble_key_pressed(u8 key)
{

}

void app_cmd_ble_key_released(u8 key)
{
}


//debug
void app_cmd_ble_debug_vol(u8 level)
{

	
}


void avrcp_remote_vol_up (void)
{
#ifdef BT_VOLUME_SYC
	DBG_Puts("bt avrcp rmt vol+\n\r");
	app_nav_mp3_plus();
	//app_cmd_bt_vol_up(TRUE);
#endif
}

void avrcp_remote_vol_down (void)
{
#ifdef BT_VOLUME_SYC
	DBG_Puts("bt avrcp rmt vol-\n\r");
	app_nav_mp3_minus();
	//app_cmd_bt_vol_up(FALSE);
#endif
}

/* AVRCP absolute vol shoud be 0-127*/
void avrcp_remote_absolute_vol (u8 absolute_vol)
{	
#ifdef BT_VOLUME_SYC
	u8 vol;

	DBG_Puts("vol syc from bt device\n\r");

	if (bt_avrcp_tick < 2) {
		DBG_Printf("bt remote_absolute_vol:%d\n\r", bt_avrcp_tick);
		bt_avrcp_tick++;
		return;
	}

	if (absolute_vol > 127) {
		absolute_vol = 127;
	}

	vol = (absolute_vol * VOLUME_MAX + 126) / 127;
	app_cmd_bt_absolute_vol(vol);
#endif
}

/* AVRCP hp vol change*/
void avrcp_hp_vol_change_cb (u8 absolute_vol)
{	
#ifdef BT_VOLUME_SYC
	u8 vol;
	
	DBG_Printf("hp vol change:%d\n\r", absolute_vol);
	
	if (absolute_vol > 127) {
		absolute_vol = 127;
	}
	
	vol = (absolute_vol * VOLUME_MAX + 126) / 127;
	app_main_data.volume = vol;
#endif
}

//
void app_cmd_bt_absolute_vol(U8 vol)
{
#ifdef BT_VOLUME_SYC
	int temp;

#if 1
	if (vol == app_main_data.volume)
	{
	}
	else if (vol > app_main_data.volume)
	{
		temp = vol -  app_main_data.volume;
		do
		{
			app_nav_mp3_plus();
			temp --;
		//} while (temp--);
		} while (temp);
	}
	else
	{
		temp = app_main_data.volume -  vol;
		do
		{
			app_nav_mp3_minus();
			temp --;
		//} while (temp --);
		} while (temp);
	}
#else
	app_main_data.volume = vol;

#if defined AUDIO_CODEC_USED_VOL
	AUDIOdevice.Set_Volume(app_main_data.volume);
#elif defined AUDIO_AMP_USED_VOL
	audioAmp_Dev.Set_Volume(app_main_data.volume);
#endif
#endif

#endif
}

void app_cmd_bt_vol_up(U8 up)
{
	//need more stuty
	if (up == TRUE) {
		if (app_main_data.volume < VOLUME_MAX) {
			app_main_data.volume++;
		}
	}
	else {
		if (app_main_data.volume > 0) {
			app_main_data.volume--;
		}
	}
	
#if defined AUDIO_CODEC_USED_VOL
	AUDIOdevice.Set_Volume(app_main_data.volume);
#elif defined AUDIO_AMP_USED_VOL
	audioAmp_Dev.Set_Volume(app_main_data.volume);
#endif	
}

void app_nav_bt_hci_play(void)
{
	DBG_Puts("app nav bt paly\n\r");
	kBtHCI_play();
}

void kBtHCI_next_up(void)
{
	DBG_Printf("kBtHCI_next_up\n\r");
	if (0)//(key_process_envs.key_hold_counts > 0)
	{
		//key_hold_counter = 0;
		kBtHCI_ffd_release();
	}
	else {
#ifdef BT_ID3_DISPLAY_EANBLE
		app_main_data.disp_info = DISP_INFO_WAITING;
		track_id3_ready = FALSE;
		track_id3_rx_flag = DISP_INFO_TITLE;
		memset( id3_tag.title, 0, (MAX_ID3_TEXT + 1)*2);
		memset( id3_tag.artist, 0, (MAX_ID3_TEXT + 1)*2);
		memset( id3_tag.album, 0, (MAX_ID3_TEXT + 1)*2);
#endif
		
		kBtHCI_next ();
	}

}

void kBtHCI_prev_up(void)
{
	DBG_Printf("kBtHCI_prev_up\n\r");
	if (0)//(key_process_envs.key_hold_counts > 0)
	{
		//key_hold_counter = 0;
		kBtHCI_fbw_release();	
	}
	else {
#ifdef BT_ID3_DISPLAY_EANBLE
		app_main_data.disp_info = DISP_INFO_WAITING;
		track_id3_ready = FALSE;
		track_id3_rx_flag = DISP_INFO_TITLE;
		memset( id3_tag.title, 0, (MAX_ID3_TEXT + 1)*2);
		memset( id3_tag.artist, 0, (MAX_ID3_TEXT + 1)*2);
		memset( id3_tag.album, 0, (MAX_ID3_TEXT + 1)*2);
#endif

		kBtHCI_prev ();
	}	

}

void kBtHCI_next_hold(void)
{
	kBtHCI_ffd_press();
	//key_hold_counter++;
}

void kBtHCI_prev_hold(void)
{
	kBtHCI_fbw_press();
	//key_hold_counter++;
}



void BtHCI_a2dp_conn(void)
{
	DBG_Printf("bt conn success\r\n");
	bt_status.bt_main_status = enBT_CONNECTED;
	app_bt_set_scan_enable_mode(enBT_BOTH_SCAN_DISABLE);
}

void BtHCI_disconn_one(u32 link_loss)
{
	DBG_Printf ("disconn one\n\r");
#ifndef BT_MULTI_LINK
	BtHCI_disconn(link_loss);
#endif
}

void BtHCI_disconn(u32 link_loss)
{
//app_timer_relink_monitor_time_clr();
//	if (app_main_data.tws_role == TWS_R) {
//		return;	
//	}
	DBG_Printf("bt disconnet\r\n");

	if (link_loss == TRUE) {
		DBG_Printf ("A2DP link loss\n\r");
	}
	else
	{
		//if (bt_hci_device_conns)
		//	app_prompt_tone_1_play();
	}

#ifdef BT_RELINK_ENABLE
	if(link_loss && (bt_status.bt_main_status == enBT_CONNECTED))
	{
	      DBG_KEYPrintf("link_loss ==%d\n",link_loss);
	      //app_nav_rmt_convert_bt_hci();
		  app_timer_bt_reLinktTimer_time_set(1000);
	}
#endif	
	if ((app_main_data.media == MEDIA_BT_HCI)&&(!app_main_data.standby_status))
	{
		app_bt_set_scan_enable_mode(enBT_BOTH_SCAN_ENABLE);
	}
	
	bt_avrcp_tick = 0;
	bt_status.bt_main_status = enBT_DISCONNECTED;
	bt_status.bt_avrcp_status = enBT_AVRCP_UNKNOWN;	
	
#if 0//def BT_ID3_DISPLAY_EANBLE
	track_id3_ready = FALSE;
	
	memset( id3_tag.title, 0, (MAX_ID3_TEXT + 1)*2);
	memset( id3_tag.artist, 0, (MAX_ID3_TEXT + 1)*2);
	memset( id3_tag.album, 0, (MAX_ID3_TEXT + 1)*2);
#endif
	
	
}

#if 0
void BtHCI_set_dec(u32 freq)
{
	if (freq == 44100) 
	{
		audio_pll_set(SR_44100_SPDIF_1x);
		DBG_Printf("SBC sample freq 44100\n\r");
	}
	else if (freq == 48000) 
	{
		audio_pll_set(SR_48000_SPDIF_1x);
		DBG_Printf("SBC sample freq 48000\n\r");
	}
	else if (freq == 32000) {
		audio_pll_set(SR_32000_SPDIF_1x);
		DBG_Printf("SBC sample freq 32000\n\r");
	}
	else {
		DBG_Assert (FALSE);
		DBG_Printf("SBC sample freq err\n\r");
	}
}
#endif

/*
define in a2dp_codec.h
#define A2DP_CODEC_SBC			0x00
#define A2DP_CODEC_MPEG12		0x01
#define A2DP_CODEC_MPEG24		0x02
#define A2DP_CODEC_ATRAC		0x03
#define A2DP_CODEC_VENDOR		0xFF
*/

void BtHCI_set_dec(u32 val)
{
	U8 dec;
	U8 freq;
	int src_freq;
	U8 *pStreamBuf;
	U32 size;
	
	if (BtHCI_hfp_is_calling())
	{
		return;
	}
	
	//if (bt_get_hp_status() == TRUE)
	//	return;

	//app_dac_mute_enable();

	dec = (val & 0xFF);
	freq = ((val & 0xFF00) >> 8);

	a2dp_sample_freq = freq;
	if (freq == 0x02)
	{
		//src_freq = SF_BASE_44100;
		src_freq = 44100;
		DBG_Printf("SBC sample freq 44100\n\r");
	}
	else if (freq == 0x01)
	{
		//src_freq = SF_BASE_48000;
		src_freq = 48000;
		DBG_Printf("SBC sample freq 48000\n\r");
	}
	else
	{
		//src_freq = SF_BASE_32000;
		src_freq = 32000;
		DBG_Printf("SBC sample freq 32000\n\r");
	} 

	app_audio_clock_freq_setting(src_freq);
	
	app_cmd_DecoderExit();	

	//at current time, only support SBC
	DBG_Assert (dec == 0x0);
	uDecSend (DECODE_SET, DECODE_SBC);			

	//app_main_data.media = MEDIA_UNKNOWN;			//stop write a2dp stream

#ifdef STREAM_BUFFER_SIZE_UNFIXED
	pStreamBuf = BT_STREAM_START;
	size = BT_STREAM_END - BT_STREAM_START;
#else
	pStreamBuf = stream_buffer;
	#ifdef STREAM_BUFFER_USED_POINTER
	size = STREAM_BUF_MAX_SIZE;
	#else
	size = sizeof(stream_buffer);
	#endif
#endif

	AUDIO_FIFO_STREAM_FLUSH();
	//fifo_file_stream_flush(&audio_env.stream_fifo)

	AUDIO_FIFO_STREAM_OPEN(pStreamBuf, size);
	//fifo_init(&audio_env.stream_fifo, pStreamBuf, size, TRUE);

	AUDIO_FIFO_STREAM_CREATE(0,STREAM_LENTH_UNKNOWN,0,0);
	//fifo_create_file_stream(&audio_env.stream_fifo, 0, STREAM_LENTH_UNKNOWN, 0, 0);

	app_main_data.playing_stream_status = STREAM_DEC_INIT;

	//app_main_data.playing_stream_status = STREAM_WAITING_DATA;
	//app_main_data.media = MEDIA_BT_HCI;
}

void uiBtHCI_set_pause (U32 sess);

void bt_hci_set_pause (void *sess)
{
}

void bt_hci_pasue_cb(void *sess)
{
	
}




void BtHCI_avrcp_conn(void)
{

}

/*iOS 9.21 bug, no avrcp play/pause*/
void BtHCI_a2dp_stream_start_cb(void)
{
	/*clear dec err count*/
}


void BtHCI_a2dp_relink_fail(void)
{
	//DBG_Printf ("Relink Fail\n\r");
	//uiBtHCI_relink();			//debug	
	DBG_Printf ("Relink Fail\n\r");
#ifdef BT_RELINK_ENABLE
	if(app_main_data.media == MEDIA_BT_HCI)
	{
		app_timer_bt_reLinktTimer_time_set(6000);
	}
#endif
}

void uiBtHCI_Optek_module_link(u8 chan);
void uiBtHCI_set_inquiry(void);

void BtHCI_otk_psm_conn_fail(void)
{

}

void BtHCI_otk_psm_tx_conn_fail(void)
{
}


void BtHCI_otk_psm_no_macid(void)
{
}

void BtHCI_conn_complete_cb(u8 status)
{
}

extern u16 dec_frame_cn;




void BtHCI_OTK_PSM_conn(void)
{
}

/*****************************/
/*****************************/
extern int hfp_mic_in_1st_chk;
extern int hfp_mic_stream_wlen;

//u8 sbc_sample_freq = 0x02;			//44100

#if 1//def BT_HFP_ENABLE
u8 hfp_calling,hfp_ring;
#endif

void BtHCI_sco_conn(void)
{
#ifdef BT_HFP_ENABLE
	U8 *pStreamBuf;
	U32 size;

	DBG_Printf("sco connect\r\n");

	app_cmd_DecoderExit();

	//da_pp_hfp_filter();
	app_dac_receive_pcm_enable(FALSE);

#ifdef	CVSD_CODEC_BY_OPTEK
	//audio_pll_set(SR_32000_SPDIF_1x);				// 64Khz
	//hw_audio_pll_clk_fre_set(SF_BASE_32000, SPDIF_SF_1x);
	app_audio_clock_freq_setting(32000);
#else
	//audio_pll_set(SR_48000_SPDIF_1x);				//8kHz x 6
	//hw_audio_pll_clk_fre_set(SF_BASE_48000, SPDIF_SF_1x);
	app_audio_clock_freq_setting(48000);
#endif

	//audio_com_init();


#ifdef STREAM_BUFFER_SIZE_UNFIXED
	pStreamBuf = BT_HFP_STREAM_START;
	size = BT_STREAM_END - BT_HFP_STREAM_START;
#else
	pStreamBuf = stream_buffer;
	#ifdef STREAM_BUFFER_USED_POINTER
	size = STREAM_BUF_MAX_SIZE;
	#else
	size = sizeof(stream_buffer);
	#endif
#endif

	AUDIO_FIFO_STREAM_FLUSH();
	//fifo_file_stream_flush(&audio_env.stream_fifo)

	AUDIO_FIFO_STREAM_OPEN(pStreamBuf, size);
	//fifo_init(&audio_env.stream_fifo, pStreamBuf, size, TRUE);

	AUDIO_FIFO_STREAM_CREATE(0,STREAM_LENTH_UNKNOWN,0,0);
	//fifo_create_file_stream(&audio_env.stream_fifo, 0, STREAM_LENTH_UNKNOWN, 0, 0);

	app_dac_receive_pcm_enable(TRUE);

	app_main_data.playing_stream_status = STREAM_WAITING_DATA;	
	uDecSend (DECODE_SET, DECODE_HFP);

	app_nav_window_set(&bt_hfp_window, 0);
	app_timer_window_time_set (app_main_data.window->window_time);	
#endif	
}

void BtHCI_sco_disconn(void)
{
#ifdef BT_HFP_ENABLE
	U8 *pStreamBuf;
	U32 size;

	DBG_Printf("SCO disconn\n\r");

	app_cmd_DecoderExit();
	//da_pp_music_eq();

	BtHCI_SBC_freq(a2dp_sample_freq);
	app_dac_receive_pcm_enable(FALSE);	

	uDecSend (DECODE_SET, DECODE_SBC);	


#ifdef STREAM_BUFFER_SIZE_UNFIXED
	pStreamBuf = BT_STREAM_START;
	size = BT_STREAM_END - BT_STREAM_START;
#else
	pStreamBuf = stream_buffer;
	#ifdef STREAM_BUFFER_USED_POINTER
	size = STREAM_BUF_MAX_SIZE;
	#else
	size = sizeof(stream_buffer);
	#endif
#endif

	AUDIO_FIFO_STREAM_FLUSH();
	//fifo_file_stream_flush(&audio_env.stream_fifo)

	AUDIO_FIFO_STREAM_OPEN(pStreamBuf, size);
	//fifo_init(&audio_env.stream_fifo, pStreamBuf, size, TRUE);

	AUDIO_FIFO_STREAM_CREATE(0,STREAM_LENTH_UNKNOWN,0,0);
	//fifo_create_file_stream(&audio_env.stream_fifo, 0, STREAM_LENTH_UNKNOWN, 0, 0);


	app_main_data.playing_stream_status = STREAM_DEC_INIT;
	//entry_id3_flag = ID3_UNKNOWN;					
	//app_main_data.coming_stream_status = STREAM_MEDIA;
	//app_main_data.coming_stream_type = STREAM_DATA_CDROM;			

	app_nav_window_set(&bt_hci_window, 0);
	app_timer_window_time_set (app_main_data.window->window_time);
#endif
}

void BtHCI_hfp_init(void)
{
#ifdef BT_HFP_ENABLE
	hfp_calling = 0;
	hfp_ring = 0;
#endif
}

bool BtHCI_hfp_is_calling(void)
{
#ifdef BT_HFP_ENABLE
	return 	(hfp_calling || hfp_ring);
#else
	return FALSE;	
#endif	
}

void BtHCI_HFP_call(u8 call)
{
#ifdef BT_HFP_ENABLE
	if (call == 1) {
		hfp_calling = 0x01;

		app_nav_window_set(&bt_hfp_window, 0);
		app_timer_window_time_set (app_main_data.window->window_time);
	}
	else {
		hfp_calling = 0x0;

		app_nav_window_set(&bt_hci_window, 0);
		app_timer_window_time_set (app_main_data.window->window_time);
	}

	hfp_ring = 0x0;
#endif
}

void BtHCI_HFP_callsetup(u8 callsetup)
{
#ifdef BT_HFP_ENABLE
	if (callsetup == 1) {
		//DBG_Assert(FALSE);
		hfp_ring = 0x01;

		app_nav_window_set(&bt_hfp_window, 0);
		app_timer_window_time_set (app_main_data.window->window_time);
	}
	else {
		hfp_ring = 0x0;

		if (hfp_calling == 0) {
			app_nav_window_set(&bt_hci_window, 0);
			app_timer_window_time_set (app_main_data.window->window_time);
		}
	}
#endif
}

void BtHCI_HFP_callheld(u8 callheld)
{
	//DBG_Assert(FALSE);
}

void BtHCI_ring(void)
{
#ifdef BT_HFP_ENABLE
	hfp_ring = 0x01;

	app_nav_window_set(&bt_hfp_window, 0);
	app_timer_window_time_set (app_main_data.window->window_time);
#endif
}

void BtHCI_clip(u8 *s)
{
	//DBG_Assert(FALSE);
}

u8 Bt_HCI_get_avrcp_speaker_vol(void)
{
	return ((app_main_data.volume * 127)/VOLUME_MAX);	/*avrcp vol 0=127*/
}

u8 Bt_HCI_get_hfp_speaker_vol(void)
{
}

/*Callback from mobile phone, VGM value should be 0-15*/
void BtHCI_hfp_vgm(u8 v)
{
	DBG_Assert(FALSE);
}

/*Callback from mobile phone, VGS value should be 0-15*/
void BtHCI_hfp_vgs(u8 v)
{
	u8 vol;
	DBG_Printf ("VGS %d\n\r", v);

	if (v > 15)
		v = 15;

	vol = (v * VOLUME_MAX) / 15;
	if (vol > 0) {
		vol -= 1;
		app_main_data.volume = vol;
		app_cmd_vol_up(TRUE);	
	}
	else {
		vol = 1;
		app_main_data.volume = vol;
		app_cmd_vol_up(FALSE);	
	}
}
void BtHCI_phone_key (void)
{
	if (hfp_ring == 0x01) {
		//accept phone
		//DBG_Assert(FALSE);
		uiBtHCI_hfp_accept ();
	}
	else if (hfp_calling == 0x01) {
		//terminate calling
		//DBG_Assert(FALSE);
		uiBtHCI_hfp_term();
	}
}


void BtHCI_hfp_source_key (void)
{
/*
	if (hfp_ring == 0x01) {
		uiBtHCI_hfp_term();
		delayms(100);	
	}
	else if (hfp_calling == 0x01) {
		//terminate calling
		//DBG_Assert(FALSE);
		uiBtHCI_hfp_term();
		delayms(100);
	}
*/

	uiBtHCI_hfp_term();
	delayms(100);

	app_nav_source_released ();
}

void BtHCI_SBC_freq(u8 freq)
{
	if(BtHCI_hfp_is_calling())
		return;

	/* from a2dp, bit 1->48000, bit 1->44100, bit 2->3200 and bit 3->1600 */
	a2dp_sample_freq = freq;
	if (freq == 0x02) {
		//audio_pll_set(SR_44100_SPDIF_1x);
		hw_audio_pll_clk_fre_set(SF_BASE_44100,SPDIF_SF_1x);
		DBG_Printf("SBC sample freq 44100\n\r");
	}
	else if (freq == 0x01) {
		//audio_pll_set(SR_48000_SPDIF_1x);
		hw_audio_pll_clk_fre_set(SF_BASE_48000,SPDIF_SF_1x);
		DBG_Printf("SBC sample freq 48000\n\r");
	}
	else {
		//audio_pll_set(SR_32000_SPDIF_1x);
		hw_audio_pll_clk_fre_set(SF_BASE_32000,SPDIF_SF_1x);
		DBG_Printf("SBC sample freq 32000\n\r");
	} 

	//otk_psm_conn_ready ();
}



void app_nav_bt_avrcp_vol(void)
{
#ifdef BT_VOLUME_SET
	u8 vol;


	if ( (app_main_data.media != MEDIA_BT_HCI) && (bt_get_hp_status() == FALSE) )
	{
		return;
	}

	DBG_Puts("set vol to bt dev\n\r");
	//app_cmd_bt_vol_up(TRUE);

	vol = Bt_HCI_get_avrcp_speaker_vol();
	kBtHCI_avrcp_vol(vol);					//vol sync
#endif


}

void app_nav_bt_avrcp_vol_up(void)
{
	u8 vol;

	app_cmd_bt_vol_up(TRUE);
	
	vol = Bt_HCI_get_avrcp_speaker_vol();
	kBtHCI_avrcp_vol(vol);					//vol sync
}

void app_nav_avrcp_vol_up_hold(void)
{
	u8 vol;
	
	app_timer_key_hold_set_timer (300);
	app_cmd_bt_vol_up(TRUE);
	
	vol = Bt_HCI_get_avrcp_speaker_vol();
	kBtHCI_avrcp_vol(vol);					//vol sync
}

void app_nav_bt_avrcp_vol_down(void)
{
	u8 vol;
	app_cmd_bt_vol_up(FALSE);
	
	vol = Bt_HCI_get_avrcp_speaker_vol();
	kBtHCI_avrcp_vol(vol);					//vol sync
}

void app_nav_bt_avrcp_vol_down_hold(void)
{
	u8 vol;
	app_timer_key_hold_set_timer (300);
	app_cmd_bt_vol_up(FALSE);
	
	vol = Bt_HCI_get_avrcp_speaker_vol();
	kBtHCI_avrcp_vol(vol);					//vol sync
}


void app_nav_hfp_vol_up(void)
{
	u8 vol;
	app_cmd_bt_vol_up(TRUE);
	
	vol = Bt_HCI_get_hfp_speaker_vol();
	uiBtHCI_hfp_vgs(vol);					//vol sync
}

void app_nav_hfp_vol_up_hold(void)
{
	u8 vol;
	
	app_timer_key_hold_set_timer (300);
	app_cmd_bt_vol_up(TRUE);
	
	vol = Bt_HCI_get_hfp_speaker_vol();
	uiBtHCI_hfp_vgs(vol);					//vol sync
}

void app_nav_hfp_vol_down(void)
{
	u8 vol;
	app_cmd_bt_vol_up(FALSE);
	
	vol = Bt_HCI_get_hfp_speaker_vol();
	uiBtHCI_hfp_vgs(vol);					//vol sync
}

void app_nav_hfp_vol_down_hold(void)
{
	u8 vol;
	app_timer_key_hold_set_timer (300);
	app_cmd_bt_vol_up(FALSE);
	
	vol = Bt_HCI_get_hfp_speaker_vol();
	uiBtHCI_hfp_vgs(vol);					//vol sync
}



/*********************************
/********************************/
void app_nav_bt_medial_fini(void)
{

}

void app_nav_bt_connect(void)
{
	if (bt_get_hp_status() == TRUE) {
		//keep current status
		return;
	}

	if (app_main_data.standby_status)
	{
		app_main_data.media = MEDIA_BT_HCI;
		app_nav_standby_release();
	}
	else
	{
		app_nav_rmt_convert_bt_hci();
	}
}

void app_nav_BT_manual_pair(void)
{
}

void app_nav_bt_display(void)
{
}

void app_nav_bt_relink(void)
{
#ifdef BT_RELINK_ENABLE
	DBG_Puts("BT reLink\n\r");

	if (app_main_data.media == MEDIA_BT_HCI)
	{
		if (bt_status.bt_main_status != enBT_CONNECTED)
		{
			DBG_Puts("BT reLink1\n\r");
			uiBtHCI_relink();
		}
	}
#endif
}


U32 app_nav_bt_source (void)
{
}


/*
	Optek psm intefare
*/

/*
void otk_psm_ui_vol (u8 vol)
{
	DBG_Assert(FALSE);
}

void otk_psm_ui_srf(u8 srf)
{
	DBG_Assert(FALSE);
}

void otk_psm_ui_mute(u8 mute)
{
	DBG_Assert(FALSE);
}

void otk_psm_ui_poff (void)
{
	DBG_Assert(FALSE);
}
*/

//const u8 OEM_Company_ID[3] = {0x94,0x9F,0x3F};
const u8 OEM_Company_ID[3] = {0x3F,0x9F,0x94};		//Optek Company ID 94-9F-3F
//u8 model_company_id[] = {0x6A,0xE6,0x4A};		//Sharp company ID
//const u8 model_name[] = {"Philips BT3900"};
//const u8 model_name[] = {"HT-MT300/301"};
const u8 model_name[] = {"YSP-1600"};

extern const u8 bt_name [];

u16 multi_speaker_inquiry_addr_cb (u8 *bt_addr)
{
	if (bt_get_hp_status() == TRUE){
		return TRUE;
	}

	if (bt_addr[3] == OEM_Company_ID[0] && bt_addr[4] == OEM_Company_ID[1] &&
		bt_addr[5] == OEM_Company_ID[2])
	{
		return TRUE;
	}
	else {
		//return TRUE;		//debug
		return FALSE;
	}
}

/*when multi speaker slave get remote name, filter the host by bt name*/ 
u16 multi_speaker_remote_name_cb (U8 *name)
{
#ifdef	BT_HEADPHONE
	DBG_Printf("no need name match\n\r");
	return TRUE;
#else
	u16 len;

	len = strlen (bt_name);
	
	//if (strcmp(model_name,bt_name) == 0) {
	if (memcmp(bt_name,name,len) == 0) {		//suport name ext
		DBG_Printf("Model name match\n\r");
		return TRUE;
	} 
	else {
		return FALSE;
	}
#endif
}

void multi_speaker_inquiry_fini (void)
{

}

void multi_speaker_no_proper_inquiry (void)
{
	if (bt_get_hp_status() == FALSE) {
		DBG_Printf("No rsp, Search again\n\r");
		uiBtHCI_set_inquiry();				//try to get Subw Host address
	}
	else {
		DBG_Printf("No BT Headphone\n\r");
	}
}



void otk_psm_ui_send_vol (u8 vol);
void otk_psm_ui_send_EQ (u8 eq);
void otk_psm_ui_send_srf(u8 srf);
void otk_psm_ui_send_mute(u8 mute);
void otk_psm_ui_send_poff (void);

//#include "..\profile\otk_psm\otk_psm_srf.h"
#include "..\media\bt_hci\profile\otk_psm\otk_psm_srf.h"

void otk_psm_ui_srf(u8 srf)
{
}

extern U8 spdif_sample_base;


void otk_psm_aux_init (void)
{

}

u16 bt_hci_conn_cb (u8 *btaddr)
{
	DBG_Printf("rev bt conn req\r\n");
	app_bt_set_scan_enable_mode(enBT_BOTH_SCAN_DISABLE);
	return TRUE;
}

/*
	return	0 -> TWS/Multispk Master/ Normal
	return	1 -> TWS Slave	
	return  2 -> Mutlispk Slave
*/
u16 bt_hci_get_multispk_role_cb (void)
{
	if (1){//(app_main_data.tws_role == TWS_L) {		
		return 0;	
	}		
	else {
		//return 1;			//multispk slave
		return 2;			//tws slave	
	}
}

u32 bt_hci_rx_conn_cb(void)
{
	if (1)
		return TRUE;
	else
		return FALSE;
}

u32 bt_hci_new_rx_conn_cb(void)
{
	//if (accept new subw conn)
	if (1)
		return TRUE;
	else
		return FALSE;
}

void otk_psm_disconn (void)
{	
}

void BtHCI_OTK_PSM_disconn(void)
{

}

void BtHci_OTK_PSM_tx_fatal_err(void)
{
}
void BtHCI_OTK_PSM_stream_tx_disconn(void)
{

		
}

/*
if link_loss is 0, uplink power off
if uplink want power off downlink, please call the following function first 
void uiBtHCI_Optek_module_tx_close(void) then bt_hci_flush_multi_speaker() or bt_hci_flush_all()
*/

void BtHCI_OTK_PSM_stream_rx_disconn(u32 link_loss)
{
}

void otk_psm_conn_ready (void)
{

}

void otk_psm_conn_stream_tx_ready (void)
{

}

void otk_psm_conn_stream_rx_ready (void)
{
}


void bt_tx_test_1(void)
{
}

void bt_tx_test_2(void)
{
}

u8 Bt_HCI_get_ui_vol(void)
{
}

u8 Bt_HCI_get_ui_eq(void)
{
}

u8 bt_hci_set_subw_eq(void)
{
}


void bt_hci_32k_sim (void)
{
}


/*
	Optek psm intefare
*/
void otk_psm_head_vol (void)
{
}

void otk_psm_head_eq (void)
{
}

void otk_psm_head_srf (u16 srf)
{
}

void otk_psm_ui_vol (u8 vol)
{
 }

void otk_psm_ui_eq (u8 eq_bass)
{
}


void otk_psm_ui_slave_srf(u8 srf)
{

}
void otk_psm_ui_mute(u8 mute)
{
}
void otk_psm_ui_poff (void)
{
}

void otk_psm_conn_ok (void)
{
	
}

/*0 master, 1 slave*/
void otk_psm_role_switch (u8 rol)
{
	//DBG_Assert (FALSE);
}


//BLE ui
void bt_hci_ble_conn (void)
{
}

void kBtHCI_adv_enable(void);

void bt_hci_ble_disconn (void)
{
	kBtHCI_adv_enable();
}

const u8 ble_device_name [] = {"SHARP RC"};
//const u8 ble_device_name [] = {"PHILIPS RC"};
extern const u8 ble_name[];

int bt_hci_ble_adv_name_match (u8 *pbuf)
{
}

#define BLE_ADV_ACCEPT_RSSI	-80

int bt_hci_ble_rssi (unsigned char rssi)
{
}

int bt_hci_new_up_multispk_conn (void)
{
}

void bt_hci_ble_scan_report (void)
{
}

void app_ble_update_rxdata(U8 *p)
{
}
void app_nav_ble_set_volume(void)
{
}

void app_nav_ble_set_eq(void)
{

}

void hw_pwm_duty_set (U16 duty, U16 period);
U8 vm_vol_steps;
void app_nav_ble_set_vm_vol(void)
{

}

extern U8 bleread_buf[32];
U32 BtRemoter_EnData(U8 *dst, U8 *data, U32 size);
void att_key_notification_send_ldrc (u32 parm1, u32 parm2, u32 parm3);

void app_ble_send_vol(void)
{	


}
void app_ble_send_bass(void)
{

}

void app_ble_send_souce(void)
{

}

void app_ble_send_vm_vol(void)
{	


}

U8 light_colour_state;
void app_ble_send_colour_model(void)
{	


}

void app_ble_send_led_power_on_or_off(void)
{	


}

void app_ble_send_colour(void)
{	


}


U16 light_mode_count;
void BtRemoter_DecodePacket(U8 * data, U32 len)
{
}



void BtRemoter_AcceptData(U8 *buf, U32 size)
{
}

extern U8 bleread_buf[32];

U32 BtRemoter_EnData(U8 *bleread_buf, U8 *data, U32 size)
{
}


/************************************************************************************************************/

u8 hfp_iphone_batt_cb (void)
{
	return 7;			//0-9
}

void bt_flash_multiroom_set_master (void)
{


}

void bt_flash_multiroom_set_slave (void)
{

}

extern u8 bt_multiroom_stream_enable;
void uiBtHCI_multiroom_tx_fifo_clr(void);

void app_nav_bt_codec_reinit (void)
{
	long len;
	
	//app_dac_mute_enable();

	DBG_Printf ("Codec reinit\n\r");
	app_cmd_DecoderExit();

	//app_main_data.playing_stream_status = STREAM_INIT;	//stop write
	AUDIO_FIFO_STREAM_FLUSH();
	AUDIO_FIFO_STREAM_CREATE(0,STREAM_LENTH_UNKNOWN,0,0);
	csb_rx_count = 0;
	app_main_data.playing_stream_status = STREAM_WAITING_DATA;

#if 0
	if (app_main_data.tws_role != TWS_R)
	{
		uDecSend (DECODE_SET, DECODE_SBC);
	}
	else
	{
		if (app_main_data.bt_tx_enc_format == BT_TX_ENC_SBC) {
			uDecSend (DECODE_SET, DECODE_SBC);
		}
		else if (app_main_data.bt_tx_enc_format == BT_TX_ENC_PCM_DOWNSAMPLE) {
			uDecSend (DECODE_SET, DOCODE_PCM_DOWNSAMPLE_BY_8);
		}
	}
#endif
/*
	else {
		DBG_Assert (FALSE);
	}
*/
}

//callback from bt stack
void avdtp_rtp_seq_not_continouse_cb (void)
{
}


void uiBtHCI_hfp_iphone_batt(u8 batt);

//test
//the address should be get from other method
const u8 tws_test_btaddr[6] = {0x00,0x00,0x01,0x3F,0x9f,0x94};
void uiBtHCI_Optek_module_link_for_test(u8 *addr);

void app_nav_bt_tws_test (void)
{
}


/****************************************************************************************************************
 *																												*
 *											BT Headphone														*
 *																												*
 ****************************************************************************************************************/

//BM6
//const u8 test_hp_macid_a[6] = {0x98,0x18,0xb5,0x3f,0x9f,0x94};
//Bose HP
//const u8 test_hp_macid_a[6] = {0x0c,0xfd,0xa0,0x8a,0x0c,0x00};	           //00 0c 8a a0 fd 0c
//Onkyo X6
//const u8 test_hp_macid_a[6] = {0x28, 0x01, 0x87, 0x94, 0xa6, 0x0c};	           //0x0c a6 94 87 01 28

//const u8 test_hp_macid_a[6] = {0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF};	           //0x0c a6 94 87 01 28

//8670
//const u8 test_hp_macid_a[6] = {0x03,0xff,0x0,0x5b,0x02,0x00};	           //00 0c 8a a0 fd 0c

//RDA 0x005850fffff   0x00 58 50 ff ff ff
//const u8 test_hp_macid_a[6] = {0xff,0xff,0xff,0x50,0x58,0x00};	           //00 0c 8a a0 fd 0c

//sound port 00 1d df 6e cb c0
//const u8 test_hp_macid_a[6] = {0xc0,0xcb,0x6e,0xdf,0x1d,0x00};	           //00 0c 8a a0 fd 0c

//Philips HP
//0x00 1e 7c 28 ff 67
//const u8 test_hp_macid_a[6] = {0x67,0xff,0x28,0x7c,0x1e,0x00};	           //00 0c 8a a0 fd 0c

//Optek BT Multiroom 21d
//const u8 test_hp_macid_a[6] = {0x25,0x9d,0x21,0x3f,0x9f,0x94};            //6A:E6:4A:02:44:27

//Trainer HP
//0x0c a6 94 fd 3e b1
//const u8 test_hp_macid_a[6] = {0xb1,0x3e,0xfd,0x94,0xa6,0x0c};	           //00 0c 8a a0 fd 0c

//Optek Demo
const u8 test_hp_macid_a[6] = {0x25,0x9d,0x21,0x3f,0x9f,0x94}; 



u8 hp_conn_mode;
u8 hp_conn_index;
u8 hp_conn_macid[6];

/*
parm 1, hp addr source. 0->froam bt flash deta, 1->fraom seach dev list, 2->from MAC ID ptr
parm 2, index
parm 3, macid ptr
*/
void uiBtHCI_HP_link(u8 mode, u8 index, u8 *macid);
u8 bt_get_search_data_index (void);
u8 bt_get_search_data_max_rssi_index (void);

void app_nav_bt_hp_search_end (void)
{

}


void BtHCI_otk_no_hp_macid(void)
{
	//uiBtHCI_Optek_module_link(0);		//poweron subwoofer
	DBG_Printf ("OTK no HP mac id\n\r");
}

int hci_conn_present(void);
u16 ml_device_get_conns(void);



void app_nav_conn_hp (void)
{


	//DBG_Assert (FALSE);
}

void BtHCI_hp_conn_fail(void)
{
}

#define MAX_HP_CONN 1		

void app_hav_hp_search (void)
{	
}

u8 bt_hci_get_multi_link_max_conn (void)
{
	return 1;
}

void BtHCI_hp_disconn(u32 link_loss)
{

}


void BtHCI_hp_conn(void)
{
}

/*only valid for realteck*/
u8 hci_write_voice_setting_cvsd_cb (void)
{
#if 1//def	CVSD_CODEC_BY_OPTEK	
	return 0x01;					//cvsd enable
#else
	return 0x0;					//cvsd disable
#endif
}

u8 bt_inquiry_need_to_check_name_cb (void)
{
	return 0;			//for simple headphone pairing
	//return 1;			//others need to check model name
}

void hci_LDON_High (void)
{
//	RADIO_LDON;
}

void hci_LDON_Low(void)
{
	//RADIO_LDOFF;
}

U8 app_cmd_first_power_on (void)
{
	return TRUE;				/*first power on*/	
	//return FALSE;			/*not first power on*/	
}

//unit is 625us
u16 optek_psm_flash_time_cb (void)
{
	return (60);
}

u8 optek_hci_inquiry_mode_cb (void)
{
//	if (bt_get_hp_status() == TRUE)
//		return 0;
//	else
		return 2;
}

#if 1//def BT_HFP_ENABLE

void app_dec_hfp_close_wstream (void)
{
}

int hfp_mic_in_1st_chk;
int hfp_mic_stream_wlen;

//for pcm: 8K, len = 120, 60samples --> 7.5ms ??
//for cvsd: 64k, len = 30, 30x8 samples --> 3.75ms

#define	HPF_STREAM_DISCARD_LEN	CVSD_64K_10MS_BYTES*(400/5)	// 400ms

#ifdef	CVSD_CODEC_BY_OPTEK
#define	HFP_STREAM_MIN_LEN		CVSD_64K_10MS_BYTES*4	// 160, 5msx4 = 20ms
//#define	HFP_STREAM_MIN_LEN		CVSD_64K_10MS_BYTES*8	// 320, 5msx8 = 40ms
#else
#define	HFP_STREAM_MIN_LEN		HFP_PCM_8K_10MS_BYTES*4	// 320
#endif

#define HFP_G711_M	2
//#define HFP_G711_M	4				//low pass order 4, high pass order 8

//#define HFP_G711_M	8				//low pass order 4, high pass order 4

const int hfp_dsp_coef[HFP_G711_M*6] = {
#if 0	
/*
		high pass 300Hz
		3f074447   81f17772   3f074447   40000000   81fde93d   3e1afa59
		3dbc6faa   848720ad   3dbc6faa   40000000   84935126   3b850fcc
		
		
		low pass 3400, -76db at 4000
		2dc18b5d   b0dbef3c   2dc18b5d   40000000   8f3326b0   3d2bdf46
		2ad5ee74   b6c92c54   2ad5ee74   40000000   94e47f6d   379089cf
		26e38de3   bf5e33a0   26e38de3   40000000   9b99ec39   318b632c
		21a9e3c8   cb1dcd66   21a9e3c8   40000000   a3d17bf1   2aa01905
		1b01be76   da573031   1b01be76   40000000   add8267f   2282869f
		1332d873   ec5b124e   1332d873   40000000   b9553cf3   196b8641
		0b842d1d   fe28ea9d   0b842d1d   40000000   c47fefc5   10b15513
		0677e4ee   09e21202   0677e4ee   40000000   cbcba3bd   0b063821
*/

		0x40000000, 0x3f074447, 0x81f17772, 0x3f074447, 0x81fde93d, 0x3e1afa59,
		0x40000000, 0x3dbc6faa, 0x848720ad, 0x3dbc6faa, 0x84935126, 0x3b850fcc,

/*
		0x40000000, 0x2dc18b5d, 0xb0dbef3c, 0x2dc18b5d, 0x8f3326b0, 0x3d2bdf46,
		0x40000000, 0x2ad5ee74, 0xb6c92c54, 0x2ad5ee74, 0x94e47f6d, 0x379089cf,
		0x40000000, 0x26e38de3, 0xbf5e33a0, 0x26e38de3, 0x9b99ec39, 0x318b632c,
		0x40000000, 0x21a9e3c8, 0xcb1dcd66, 0x21a9e3c8, 0xa3d17bf1, 0x2aa01905,
		0x40000000, 0x1b01be76, 0xda573031, 0x1b01be76, 0xadd8267f, 0x2282869f,
		0x40000000, 0x1332d873, 0xec5b124e, 0x1332d873, 0xb9553cf3, 0x196b8641,
		0x40000000, 0x0b842d1d, 0xfe28ea9d, 0x0b842d1d, 0xc47fefc5, 0x10b15513,
		0x40000000, 0x0677e4ee, 0x09e21202, 0x0677e4ee, 0xcbcba3bd, 0x0b063821
*/

		//3400Khz
		0x40000000,   0x02ad24eb,   0x055a49d6,   0x02ad24eb,   0x9ccf7cf6,   0x2de516b6,
		0x40000000,   0x023aefcf,   0x0475df9d,   0x023aefcf,   0xad5831d5,   0x1b938d66
#else
	0x40000000,0x063f9666,0x0c7f2ccc,0x063f9666,0xc3a90445,0x15555555,
	0x40000000,0x063f9666,0x0c7f2ccc,0x063f9666,0xc3a90445,0x15555555,
#endif

};

void *hfpG711Handle;

extern u8 test_conn;

u32 hfp_drc_get_fifo_size (void);
u16 app_dec_get_mic_adc_buf_size (void);
extern FIFO *drsc_fifo;
extern u8 *drsc_fifo_buf;

#define MAX_MIC_FIFO_BUF	60 * 4

#define MAX_ADC_FRAMEG_BUF	(240*2*2*3)    //mono to stereo,16bit to 24bit and 16K to 48k 

FIFO *hfp_upstream_fifo;

void hfp_dsp_init(void)
{
	hfpG711Handle = NULL;
	DBG_Printf("HFP DSP init\n\r");
}

void app_dac_adc_fifo_init(void);

void aec_bufferAlloc(void);
void aec_bufferFree(void);

void cvsd_bufAlloc(void);
void cvsd_bufFree(void);

void aecfunc_bufAlloc(void);
void aecfunc_bufFree(void);

extern U16 *hfp_cvsdDecBuf;
U16 app_dec_getCvsdDecBufSize( void );

void hfp_dsp_open(void)
{	
}
void hfp_dsp_fini(void)
{
	DBG_Printf("HFP DSP fini\n\r");
}

void app_nav_bt_sco_decode_fini(void);

void hfp_dsp_close(void)
{

}

U16 hfp_size;
U32 app_dec_hfp_stream_write(U8 *ptr, U32 len)
{
#if 0
/*
	DBG_PIN_HIGH;
	DBG_PIN_LOW;
	if (len != 60 || ptr[0] != 1)
	{
		DBG_PIN_HIGH2;
		DBG_PIN_LOW2;
		DBG_Printf("w:%d,0x%x,0x%x,0x%x,0x%x\n\r",len,ptr[0],ptr[1],ptr[2],ptr[3]);
	}*/
#if 0
	static U32 l_time;
	U32 ctime,wtime;

	ctime = read_ccount();
	wtime = (ctime - l_time)/64;
	l_time = ctime;
	if (wtime > 7600 || wtime < 7400)
	{
		DBG_Printf("%d\r\n",wtime);
	}
#endif
	return len;
#endif

	U32 xlen;

	if (decode_type != DECODE_HFP)
		return;

	if (AUDIO_FIFO_STREAM_IS_VAILD() == FALSE)
	{
		return;
	}

#if 1	
	xlen = (U32) AUDIO_FIFO_STREAM_PUT_DATA(ptr, len);
	if (xlen != len)
	{
		//asm ("break 1,1");
		DBG_Printf("hfp Stream overflow\n\r");
		//codec err, dead???

		//codec err, dead???
		//ukMsgSend(app_nav_codec_reinit);
		//return;
	}
	
	//xEventGroupSetBits(event_grop, AUDIO_DECODE_EVENT);

	//xlen = (U32) fifo_get_data (&app_main_data.stream_fifo, ptr, len);
#endif

#if 1
	xlen = (U32) AUDIO_FIFO_STREAM_DATA_LEN();

	if (app_main_data.playing_stream_status == STREAM_DEC_INIT || app_main_data.playing_stream_status == STREAM_WAITING_DATA) 
	{
		//if (xlen > AUDIO_FIFO_STREAM_FIFO_LEN()/2) 
		if (xlen > len*4) 
		{
			//xEventGroupSetBits(event_grop, STREAM_EVENT);
			uDecSend(DECODE_FRAME,TRUE);
			app_main_data.playing_stream_status = STREAM_MEDIA;		
		}
	}
#endif	
}

extern U8 hfp_far_in;
u8 mic_underflow;

U8 index = 0;

U32 app_dec_hfp_mic_stream_read(U8 *ptr, U32 len)
{
#if 1
	static U8 seq_num = 0x08;
	u32 dlen,wlen;

	if (hfp_upstream_fifo == NULL)
	{
		memset(ptr,0,len);
		return 0;
	}

	wlen = fifo_get_fifo_data_wlen(hfp_upstream_fifo);
	dlen = fifo_get_fifo_data_len(hfp_upstream_fifo);

	if (hfp_get_codec() != 2)
	{
		if (dlen >= len) 
		{
			dlen = fifo_get_data (hfp_upstream_fifo, ptr,len);
		}
		else
		{
			memset(ptr,0,len);
			dlen = 0;		
		}
				
		return dlen;
	}


	ptr[0] = 0x01;
	ptr[len-1] = 0;

	switch (seq_num)
	{
		case 0x08:
			seq_num = 0x38;
			break;
		case 0x38:
			seq_num = 0xc8;
			break;
		case 0xc8:
			seq_num = 0xf8;
			break;
		case 0xf8:
			seq_num = 0x08;
			break;
		default :
			seq_num = 0x08;
			break;
	}
	ptr[1] = seq_num;

	if (wlen <= 57*2)
	{
		memset(ptr,0,len);
		return 0;
	}
	else if (dlen >= 57) 
	{
		dlen = fifo_get_data (hfp_upstream_fifo, ptr+2,57);
	}
	else 
	{
		DBG_Printf ("MIC data underflow 1\n\r");
		memset(ptr,0,len);
		return 0;		
	}

	return len;		// 120 bytes
#else	
//	memset(ptr,0xa0,len);
//	CFasm_memcpy(ptr,p_msbc,len);
//	p_msbc += len;
//	return len;
	//DBG_Printf("hfp read len:%d\n",len);
	short *p = ptr;
	short data,i;
	if (index < 1)
	{
		//memset(ptr,-0x50,len);
		data = -0x3000;
	} 
	else if(index < 2)
	{
		//memset(ptr,0x40,len);
		data = 0x3000;
	}

	for (i=0;i<len/2;i++)
	{
		*p++ = data;
	}
	if (++index >= 2)
		index = 0;
	return len;
#endif	
}

// all 10ms based
#define	HFP_CVSD_DEC_SIZE		CVSD_64K_10MS_BYTES				// 40
#define	HFP_MIC_IN_SIZE			MIC_PCM_32K_10MS_STEREO_BYTES	// 640

u16 app_dec_get_mic_adc_buf_size (void)
{
//	return HFP_MIC_IN_SIZE;		// 320*4
	return MAX_ADC_FRAMEG_BUF;	// 240*12
}

U16 *hfp_cvsdDecBuf;
U16 app_dec_getCvsdDecBufSize( void )
{
	//return HFP_CVSD_DEC_SIZE;	// 80
}


int cvsdFilter( short data );
void aecfunc_mic2errsig(void);

void audio_pll_lock (i32 err);

void mic_cvsdEncode( void )
{

}

int cvsd_read( U8 *data, int bytes );

#endif //BT_HFP_ENABLE

#if 0
U32 misSysCLK1p25ms (void)
{

}

U32 misSysCLK2BtClk (void)
{

}
#else

U32 misSysCLK1p25ms (void)
{
	return ((32000*5)/4);						//96000->1ms
}

U32 misSysCLK2BtClk (void)
{
	return (((32/2) * 625)/4);						//(96/312.5)
}
#endif

void avdtp_stream_start_cb (void)
{

}

static const u8 pin_code[16] = {'0','0','0','0','x','x','x','x','x','x','x','x','x','x','x','x'};

const U8 *optek_hci_pin_code_cb(void)
{
	return (const U8 *)pin_code;
}

void hci_bt_clock_sink(void)
{
}

void hci_bt_clock_src(void)
{
}

U8 bt_clock_sink_set_invalid(void)
{
	return FALSE;
}

U8 bt_clock_src_is_valid(void)
{
	return FALSE;
}

void bt_clk_adaption_process (void)
{
    u32 base_cnt_rxsync_1st;
    u16 fine_cnt_rxsync_1st;
    u32  base_cnt_rxsync_last; 
    u16 fine_cnt_rxsync_last;
    i32 swap_cn;
    u8 succ;
    i32 delta;
    i32 dis_hus;
	double drift;	

    succ = ld_acl_get_slave_time_sync_info(&base_cnt_rxsync_1st, &fine_cnt_rxsync_1st,
                                            &base_cnt_rxsync_last, &fine_cnt_rxsync_last,
                                            &swap_cn);

	DBG_Printf("base last:%d,base 1st:%d,fine last:%d,fine 1st:%d,swap_cn:%d\n\r",base_cnt_rxsync_last,base_cnt_rxsync_1st,fine_cnt_rxsync_last,fine_cnt_rxsync_1st,swap_cn);

	if (succ)
    { 
        if (base_cnt_rxsync_last >= base_cnt_rxsync_1st)
        	dis_hus = base_cnt_rxsync_last - base_cnt_rxsync_1st;
        else
        	dis_hus = base_cnt_rxsync_last + 0x10000000- base_cnt_rxsync_1st;
        
		if (dis_hus < 1000000*2/625)
			return;

		dis_hus = dis_hus*625 + fine_cnt_rxsync_last - fine_cnt_rxsync_1st;


		delta = swap_cn*625 + fine_cnt_rxsync_last - fine_cnt_rxsync_1st;

        //drift = (fs-fm) /fs    // fm    
        drift = (double) delta/(double)(dis_hus);
/*
		DBG_Printf("diff sample:%d\n\r",diff_sample);

		if (diff_sample > 0)
		{
			drift -=  3.0/(double)(dis_hus);
		}
		else if (diff_sample < 0)
		{
			drift += 3.0/(double)(dis_hus);
		}
*/
		void hw_audio_pll_clk_adj_by_drift (double drift);

		hw_audio_pll_clk_adj_by_drift(drift);

    }
}

void get_class_of_device_service(U8 class[3])
{
#if 1//headphone
	class[0] = 0x18;//Minor Device Class:Headphones
	class[1] = 0x04;//Major Device Class:Audio/Video (headset, speaker, stereo, video display etc	
	class[2] = 0x24;//Major Service Class :Rendering (printing, speaker etc)  and Audio (speaker, microphone, headset service etc)	
#else //speaker
	class[0] = 0x14;//Minor Device Class:loudspeaker
	class[1] = 0x04;//Major Device Class:Audio/Video (headset, speaker, stereo, video display etc	
	class[2] = 0x24;//Major Service Class :Rendering (printing, speaker etc)  and Audio (speaker, microphone, headset service etc)	
#endif
}

//u8?ld_acl_get_slave_time_sync_info?