main.c 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301
  1. #include "audio_output.h"
  2. #include "audio_receiver.h"
  3. #include "buttons.h"
  4. #include "spiram_task.h"
  5. #include "display.h"
  6. #include "dns_server.h"
  7. #include "ethernet.h"
  8. #include "led.h"
  9. #include "hap.h"
  10. #include "mdns_airplay.h"
  11. #include "nvs_flash.h"
  12. #include "playback_control.h"
  13. #include "ptp_clock.h"
  14. #include "rtsp_server.h"
  15. #include "settings.h"
  16. #include "web_server.h"
  17. #include "log_stream.h"
  18. #include "wifi.h"
  19. #include "spiffs_storage.h"
  20. #ifdef CONFIG_BT_A2DP_ENABLE
  21. #include "a2dp_sink.h"
  22. #include "rtsp_events.h"
  23. #endif
  24. #include "iot_board.h"
  25. #include "panel_uart.h"
  26. #include "esp_log.h"
  27. #include "freertos/FreeRTOS.h"
  28. #include "freertos/task.h"
  29. static const char *TAG = "main";
  30. // AP mode IP address (192.168.4.1 in network byte order)
  31. #define AP_IP_ADDR 0x0104A8C0
  32. static bool s_airplay_started = false;
  33. static bool s_airplay_infrastructure_ready = false;
  34. static void start_airplay_services(void) {
  35. if (s_airplay_started) {
  36. return;
  37. }
  38. ESP_LOGI(TAG, "Starting AirPlay services...");
  39. // One-time infrastructure init (PTP, HAP, audio receiver/output)
  40. if (!s_airplay_infrastructure_ready) {
  41. esp_err_t err = ptp_clock_init();
  42. if (err != ESP_OK && err != ESP_ERR_INVALID_STATE) {
  43. ESP_LOGE(TAG, "Failed to init PTP clock: %s", esp_err_to_name(err));
  44. s_airplay_started = false;
  45. return;
  46. }
  47. ESP_ERROR_CHECK(hap_init());
  48. ESP_ERROR_CHECK(audio_receiver_init());
  49. mdns_airplay_init();
  50. s_airplay_infrastructure_ready = true;
  51. }
  52. audio_output_start();
  53. ESP_ERROR_CHECK(rtsp_server_start());
  54. s_airplay_started = true;
  55. playback_control_set_source(PLAYBACK_SOURCE_AIRPLAY);
  56. ESP_LOGI(TAG, "AirPlay ready");
  57. }
  58. #ifdef CONFIG_BT_A2DP_ENABLE
  59. static void stop_airplay_services(void) {
  60. if (!s_airplay_started) {
  61. return;
  62. }
  63. ESP_LOGI(TAG, "Stopping AirPlay services...");
  64. rtsp_server_stop();
  65. audio_output_stop();
  66. s_airplay_started = false;
  67. playback_control_set_source(PLAYBACK_SOURCE_NONE);
  68. ESP_LOGI(TAG, "AirPlay stopped");
  69. }
  70. #endif
  71. static void network_monitor_task(void *pvParameters) {
  72. (void)pvParameters;
  73. bool had_network = ethernet_is_connected() || wifi_is_connected();
  74. bool dns_running = !had_network;
  75. bool wifi_started = wifi_is_connected() || !ethernet_is_connected();
  76. bool had_eth = ethernet_is_connected();
  77. // Start captive portal DNS if no network yet
  78. if (dns_running) {
  79. dns_server_start(AP_IP_ADDR);
  80. }
  81. while (1) {
  82. vTaskDelay(pdMS_TO_TICKS(2000));
  83. bool eth_up = ethernet_is_connected();
  84. bool wifi_up = wifi_is_connected();
  85. bool has_network = eth_up || wifi_up;
  86. // Ethernet just came up — stop WiFi entirely
  87. if (eth_up && !had_eth && wifi_started) {
  88. ESP_LOGI(TAG, "Ethernet connected — stopping WiFi");
  89. wifi_stop();
  90. wifi_started = false;
  91. wifi_up = false;
  92. }
  93. // Ethernet dropped — bring up WiFi (AP + STA)
  94. if (!eth_up && had_eth) {
  95. ESP_LOGI(TAG, "Ethernet down — starting WiFi as fallback");
  96. wifi_init_apsta(NULL, NULL);
  97. wifi_started = true;
  98. }
  99. had_eth = eth_up;
  100. has_network = eth_up || wifi_is_connected();
  101. if (has_network == had_network) {
  102. continue;
  103. }
  104. if (has_network) {
  105. ESP_LOGI(TAG, "Network up (eth=%s, wifi=%s)", eth_up ? "yes" : "no",
  106. wifi_up ? "yes" : "no");
  107. start_airplay_services();
  108. if (dns_running) {
  109. dns_server_stop();
  110. dns_running = false;
  111. }
  112. } else {
  113. if (!dns_running) {
  114. dns_server_start(AP_IP_ADDR);
  115. dns_running = true;
  116. }
  117. }
  118. had_network = has_network;
  119. }
  120. }
  121. #ifdef CONFIG_BT_A2DP_ENABLE
  122. static void on_bt_state_changed(bool connected) {
  123. panel_uart_set_bt_state(connected, false);
  124. if (connected) {
  125. ESP_LOGI(TAG, "BT connected — disabling AirPlay");
  126. stop_airplay_services();
  127. playback_control_set_source(PLAYBACK_SOURCE_BLUETOOTH);
  128. } else {
  129. ESP_LOGI(TAG, "BT disconnected — re-enabling AirPlay");
  130. playback_control_set_source(PLAYBACK_SOURCE_NONE);
  131. if (ethernet_is_connected() || wifi_is_connected()) {
  132. start_airplay_services();
  133. }
  134. }
  135. }
  136. static void on_airplay_client_event(rtsp_event_t event,
  137. const rtsp_event_data_t *data,
  138. void *user_data) {
  139. (void)data;
  140. (void)user_data;
  141. if (bt_a2dp_sink_is_connected()) {
  142. return;
  143. }
  144. switch (event) {
  145. case RTSP_EVENT_CLIENT_CONNECTED:
  146. ESP_LOGI(TAG, "AirPlay client connected — disabling BT");
  147. bt_a2dp_sink_set_discoverable(false);
  148. break;
  149. case RTSP_EVENT_PAUSED:
  150. // V1 grace period active — keep BT hidden so the phone reconnects
  151. // to AirPlay rather than falling back to BT.
  152. ESP_LOGI(TAG, "AirPlay paused — keeping BT hidden");
  153. break;
  154. case RTSP_EVENT_DISCONNECTED:
  155. ESP_LOGI(TAG, "AirPlay client disconnected — enabling BT");
  156. bt_a2dp_sink_set_discoverable(true);
  157. break;
  158. default:
  159. break;
  160. }
  161. }
  162. #endif
  163. void app_main(void) {
  164. // Initialize NVS
  165. esp_err_t ret = nvs_flash_init();
  166. if (ret == ESP_ERR_NVS_NO_FREE_PAGES ||
  167. ret == ESP_ERR_NVS_NEW_VERSION_FOUND) {
  168. ESP_ERROR_CHECK(nvs_flash_erase());
  169. ret = nvs_flash_init();
  170. }
  171. ESP_ERROR_CHECK(ret);
  172. ESP_ERROR_CHECK(settings_init());
  173. spiffs_storage_init();
  174. log_stream_init();
  175. ESP_ERROR_CHECK(playback_control_init());
  176. led_init();
  177. // Initialize board-specific hardware (includes I2C/SPI bus for display and
  178. // DAC)
  179. ESP_LOGI(TAG, "Board: %s", iot_board_get_info());
  180. esp_err_t err = iot_board_init();
  181. if (err != ESP_OK) {
  182. ESP_LOGE(TAG, "Board init failed: %s", esp_err_to_name(err));
  183. } else {
  184. panel_uart_init();
  185. }
  186. // 立即初始化音频硬件输出(包括 I2S 通道和 DAC),以便蓝牙上电即可在无网状态下使用
  187. ESP_ERROR_CHECK(audio_output_init());
  188. // Pass the board-owned bus to the display so it reuses it rather than
  189. // creating a duplicate bus on the same pins.
  190. #if defined(CONFIG_DISPLAY_BUS_SPI)
  191. display_init(iot_board_get_handle(BOARD_SPI_DISP_ID));
  192. #else
  193. display_init(iot_board_get_handle(BOARD_I2C_DISP_ID));
  194. #endif
  195. // Initialize LVGL-dependent board resources (e.g., touch input) after
  196. // display/LVGL port is ready.
  197. iot_board_init_lvgl_resources();
  198. // Try ethernet first
  199. bool eth_available = false;
  200. err = ethernet_init();
  201. if (err == ESP_OK) {
  202. // Wait for ethernet link + DHCP (up to 5s for link, then 10s more for DHCP)
  203. ESP_LOGI(TAG, "Waiting for ethernet...");
  204. for (int i = 0; i < 25 && !ethernet_is_link_up(); i++) {
  205. vTaskDelay(pdMS_TO_TICKS(200));
  206. }
  207. if (ethernet_is_link_up() && !ethernet_is_connected()) {
  208. ESP_LOGI(TAG, "Ethernet link up, waiting for DHCP...");
  209. for (int i = 0; i < 50 && !ethernet_is_connected(); i++) {
  210. vTaskDelay(pdMS_TO_TICKS(200));
  211. }
  212. }
  213. eth_available = ethernet_is_connected();
  214. if (eth_available) {
  215. ESP_LOGI(TAG, "Ethernet connected");
  216. } else {
  217. ESP_LOGI(TAG, "Ethernet not connected (cable?), will use WiFi");
  218. }
  219. } else if (err != ESP_ERR_NOT_SUPPORTED) {
  220. ESP_LOGW(TAG, "Ethernet init failed: %s", esp_err_to_name(err));
  221. }
  222. // Start WiFi only if ethernet is not available
  223. if (!eth_available) {
  224. wifi_init_apsta(NULL, NULL);
  225. // Wait for initial WiFi connection if credentials exist
  226. if (settings_has_wifi_credentials()) {
  227. if (!wifi_wait_connected(30000)) {
  228. ESP_LOGI(TAG, "Connect to 'ESP32-AirPlay-Setup' -> http://192.168.4.1");
  229. }
  230. } else {
  231. ESP_LOGI(TAG, "Connect to 'ESP32-AirPlay-Setup' -> http://192.168.4.1");
  232. }
  233. } else {
  234. ESP_LOGI(TAG, "Ethernet connected — skipping WiFi");
  235. }
  236. // Start services that work on any interface
  237. web_server_start(80);
  238. task_create_spiram(network_monitor_task, "net_mon", 4096, NULL, 5, NULL,
  239. NULL);
  240. bool connected = eth_available || wifi_is_connected();
  241. if (connected) {
  242. start_airplay_services();
  243. }
  244. #ifdef CONFIG_BT_A2DP_ENABLE
  245. // Initialize Bluetooth A2DP Sink
  246. {
  247. char bt_name[65];
  248. settings_get_device_name(bt_name, sizeof(bt_name));
  249. esp_err_t bt_err = bt_a2dp_sink_init(bt_name, on_bt_state_changed);
  250. if (bt_err != ESP_OK) {
  251. ESP_LOGE(TAG, "BT A2DP init failed: %s", esp_err_to_name(bt_err));
  252. } else {
  253. rtsp_events_register(on_airplay_client_event, NULL);
  254. }
  255. }
  256. #endif
  257. buttons_init();
  258. while (1) {
  259. vTaskDelay(pdMS_TO_TICKS(10000));
  260. }
  261. }