main.c 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. // Copyright 2015-2016 Espressif Systems (Shanghai) PTE LTD
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. // http://www.apache.org/licenses/LICENSE-2.0
  7. //
  8. // Unless required by applicable law or agreed to in writing, software
  9. // distributed under the License is distributed on an "AS IS" BASIS,
  10. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  11. // See the License for the specific language governing permissions and
  12. // limitations under the License.
  13. #include <stdio.h>
  14. #include <stdlib.h>
  15. #include <unistd.h>
  16. #include <string.h>
  17. #include "freertos/FreeRTOS.h"
  18. #include "freertos/task.h"
  19. #include "nvs.h"
  20. #include "nvs_flash.h"
  21. #include "esp_system.h"
  22. #include "esp_log.h"
  23. #include "bt.h"
  24. #include "bt_app_core.h"
  25. #include "bt_app_av.h"
  26. #include "esp_bt_main.h"
  27. #include "esp_bt_device.h"
  28. #include "esp_gap_bt_api.h"
  29. #include "esp_a2dp_api.h"
  30. #include "esp_avrc_api.h"
  31. /* event for handler "bt_av_hdl_stack_up */
  32. enum {
  33. BT_APP_EVT_STACK_UP = 0,
  34. };
  35. /* handler for bluetooth stack enabled events */
  36. static void bt_av_hdl_stack_evt(uint16_t event, void *p_param);
  37. void app_main()
  38. {
  39. nvs_flash_init();
  40. esp_bt_controller_config_t bt_cfg = BT_CONTROLLER_INIT_CONFIG_DEFAULT();
  41. if (esp_bt_controller_init(&bt_cfg) != ESP_OK) {
  42. ESP_LOGE(BT_AV_TAG, "%s initialize controller failed\n", __func__);
  43. return;
  44. }
  45. if (esp_bt_controller_enable(ESP_BT_MODE_BTDM) != ESP_OK) {
  46. ESP_LOGE(BT_AV_TAG, "%s enable controller failed\n", __func__);
  47. return;
  48. }
  49. if (esp_bluedroid_init() != ESP_OK) {
  50. ESP_LOGE(BT_AV_TAG, "%s initialize bluedroid failed\n", __func__);
  51. return;
  52. }
  53. if (esp_bluedroid_enable() != ESP_OK) {
  54. ESP_LOGE(BT_AV_TAG, "%s enable bluedroid failed\n", __func__);
  55. return;
  56. }
  57. /* create application task */
  58. bt_app_task_start_up();
  59. /* Bluetooth device name, connection mode and profile set up */
  60. bt_app_work_dispatch(bt_av_hdl_stack_evt, BT_APP_EVT_STACK_UP, NULL, 0, NULL);
  61. }
  62. static void bt_av_hdl_stack_evt(uint16_t event, void *p_param)
  63. {
  64. ESP_LOGD(BT_AV_TAG, "%s evt %d", __func__, event);
  65. switch (event) {
  66. case BT_APP_EVT_STACK_UP: {
  67. /* set up device name */
  68. char *dev_name = "ESP_SPEAKER";
  69. esp_bt_dev_set_device_name(dev_name);
  70. /* initialize A2DP sink */
  71. esp_a2d_register_callback(&bt_app_a2d_cb);
  72. esp_a2d_register_data_callback(bt_app_a2d_data_cb);
  73. esp_a2d_sink_init();
  74. /* initialize AVRCP controller */
  75. esp_avrc_ct_init();
  76. esp_avrc_ct_register_callback(bt_app_rc_ct_cb);
  77. /* set discoverable and connectable mode, wait to be connected */
  78. esp_bt_gap_set_scan_mode(ESP_BT_SCAN_MODE_CONNECTABLE_DISCOVERABLE);
  79. break;
  80. }
  81. default:
  82. ESP_LOGE(BT_AV_TAG, "%s unhandled evt %d", __func__, event);
  83. break;
  84. }
  85. }