fsc_bt.c 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. #include "config.h"
  2. #include "console.h"
  3. #include "board.h"
  4. static bt_sw_cfg_t g_bt_cfg;
  5. static TaskHandle_t fsc_bt_task_handle = NULL;
  6. void fsc_bt_register_pcm_interface(void *itfc)
  7. {
  8. memcpy((void*)&g_bt_cfg, itfc, sizeof(bt_sw_cfg_t));
  9. }
  10. static void fsc_bt_callback(char * cAtStr)
  11. {
  12. char* cmd = NULL;
  13. //printf("fsc_bt_callback %s\r\n", cAtStr);
  14. if (0) {
  15. } else if (0 == strncmp(cAtStr, "+VER", 4)) {
  16. cmd = "AT+ADDR\r\n";
  17. console_send_atcmd(cmd, strlen(cmd));//get mac addr
  18. } else if (0 == strncmp(cAtStr, "+ADDR=", 6)) {
  19. char cmd_str[64] = {0};
  20. sprintf(cmd_str, "AT+NAME=EY_%s\r\n", (cAtStr + 6));
  21. console_send_atcmd(cmd_str, strlen(cmd_str));//get mac addr
  22. }
  23. }
  24. static int fsc_bt_play_state_callback(BT_PLAY_STATE_E state, unsigned short samplerate, unsigned char channel)
  25. {
  26. printf("fsc_bt_play_state_callback state %d samplerate %d channel %d\r\n", state, samplerate, channel);
  27. return 0;
  28. }
  29. static int fsc_bt_a2dp_pcm_data_callback(unsigned char* buffer, unsigned short length)
  30. {
  31. /* À¶ÑÀÒôÀÖ²¥·ÅÊý¾Ý ²ÉÑùÂÊΪ44100»ò48000£¬2 channel 16bit£¬Ö±½ÓÊä³öµ½I2SÉ豸 */
  32. //printf("a2dp length %d\r\n", length);
  33. return 0;
  34. }
  35. static int fsc_bt_hfp_spk_pcm_data_callback(unsigned char* buffer, unsigned short length)
  36. {
  37. /* À¶ÑÀµç»°ÏÂÐÐÊý¾Ý ²ÉÑùÂÊΪ8000»ò16000£¬1 channel 16bit£¬Ö±½ÓÊä³öµ½I2SÉ豸 */
  38. //printf("spk length %d\r\n", length);
  39. return 0;
  40. }
  41. #if 0
  42. static int fsc_bt_hfp_mic_pcm_data_callback(unsigned char* buffer, unsigned short length)
  43. {
  44. /* À¶ÑÀµç»°ÉÏÐÐÊý¾Ý ²ÉÑùÂÊΪ8000»ò16000£¬1 channel 16bit£¬°´ÕÕ²ÎÊýlength»ñÈ¡micÊý¾ÝÌî³äbuffer */
  45. //printf("mic length %d\r\n", length);
  46. return 0;
  47. }
  48. #endif
  49. static void bt_task(void* arg)
  50. {
  51. // bluetooth stack require none volatile storage to store configuration (e.g. device name) and paired record link key and etc.
  52. // the file name is "bw_conf0.db" and "bw_conf1.db"
  53. // do not setup uart in itpInit , as blueware will do this job.
  54. bt_hw_cfg_t bt_hw_cfg = {BT_RESET_IO, BT_UART_PORT};
  55. bt_sw_cfg_t bt_sw_cfg;
  56. // for AT-Command sent from upper layer application to blueware
  57. tx_queue = xQueueCreate(AT_CMD_TX_QUEUE_LEN, (unsigned portBASE_TYPE) sizeof(char)*AT_CMD_PAYLOAD_LEN);
  58. // for AT-Response sent from blueware to upper layer application
  59. rx_queue = xQueueCreate(AT_CMD_RX_QUEUE_LEN, (unsigned portBASE_TYPE) sizeof(char)*AT_CMD_PAYLOAD_LEN);
  60. if(!tx_queue || !rx_queue)
  61. {
  62. goto die;
  63. }
  64. console_init(fsc_bt_callback);
  65. bt_hw_cfg.bt_en_pin = BT_RESET_IO;
  66. bt_hw_cfg.uartport = BT_UART_PORT;
  67. bt_sw_cfg.rx_queue = rx_queue;
  68. bt_sw_cfg.tx_queue = tx_queue;
  69. bt_sw_cfg.a2dp_resampler = 0;
  70. bt_sw_cfg.debug_mode = 0; // debug toggle
  71. #if CARLINK_EC
  72. bt_sw_cfg.ble_connection_type = BLE_EASY_CONNECTION;
  73. #else
  74. bt_sw_cfg.ble_connection_type = BLE_ERYA_CONNECTION;
  75. #endif
  76. if (g_bt_cfg.play_state_cb)
  77. bt_sw_cfg.play_state_cb = g_bt_cfg.play_state_cb;
  78. else
  79. bt_sw_cfg.play_state_cb = fsc_bt_play_state_callback;
  80. if (g_bt_cfg.a2dp_cb)
  81. bt_sw_cfg.a2dp_cb = g_bt_cfg.a2dp_cb;
  82. else
  83. bt_sw_cfg.a2dp_cb = fsc_bt_a2dp_pcm_data_callback;
  84. if (g_bt_cfg.hfp_spk_cb)
  85. bt_sw_cfg.hfp_spk_cb = g_bt_cfg.hfp_spk_cb;
  86. else
  87. bt_sw_cfg.hfp_spk_cb = fsc_bt_hfp_spk_pcm_data_callback;
  88. #if 0
  89. if (g_bt_cfg.hfp_mic_cb)
  90. bt_sw_cfg.hfp_mic_cb = g_bt_cfg.hfp_mic_cb;
  91. else
  92. bt_sw_cfg.hfp_mic_cb = fsc_bt_hfp_mic_pcm_data_callback;
  93. #endif
  94. fscbt_init(&bt_hw_cfg, &bt_sw_cfg);
  95. while(!initialize_timeout)
  96. {
  97. // do nothing else here
  98. fscbt_run();
  99. }
  100. die:
  101. console_deinit();
  102. }
  103. int fsc_bt_main(void)
  104. {
  105. if (fsc_bt_task_handle)
  106. return 0;
  107. /* Create a task to process uart rx data */
  108. if (xTaskCreate(bt_task, "bt_main", 4096, NULL,
  109. configMAX_PRIORITIES / 3, &fsc_bt_task_handle) != pdPASS) {
  110. printf("create fsc_bt_thread task fail.\n");
  111. return -1;
  112. }
  113. return 0;
  114. }