connection.c (3451B)
1 // Copyright 2025 QMK 2 // SPDX-License-Identifier: GPL-2.0-or-later 3 #include "connection.h" 4 #include "eeconfig.h" 5 #include "usb_util.h" 6 #include "util.h" 7 8 #ifdef BLUETOOTH_ENABLE 9 # include "bluetooth.h" 10 #endif 11 12 // ======== DEPRECATED DEFINES - DO NOT USE ======== 13 #ifdef OUTPUT_DEFAULT 14 # undef CONNECTION_HOST_DEFAULT 15 # define CONNECTION_HOST_DEFAULT OUTPUT_DEFAULT 16 #endif 17 18 __attribute__((weak)) void set_output_user(uint8_t output) {} 19 // ======== 20 21 #define CONNECTION_HOST_INVALID 0xFF 22 23 #ifndef CONNECTION_HOST_DEFAULT 24 # define CONNECTION_HOST_DEFAULT CONNECTION_HOST_AUTO 25 #endif 26 27 static const connection_host_t host_candidates[] = { 28 CONNECTION_HOST_AUTO, 29 CONNECTION_HOST_USB, 30 #ifdef BLUETOOTH_ENABLE 31 CONNECTION_HOST_BLUETOOTH, 32 #endif 33 #if 0 34 CONNECTION_HOST_2P4GHZ, 35 #endif 36 }; 37 38 #define HOST_CANDIDATES_COUNT ARRAY_SIZE(host_candidates) 39 40 static connection_config_t config = {.desired_host = CONNECTION_HOST_INVALID}; 41 42 void eeconfig_update_connection_default(void) { 43 config.desired_host = CONNECTION_HOST_DEFAULT; 44 45 eeconfig_update_connection(&config); 46 } 47 48 void connection_init(void) { 49 eeconfig_read_connection(&config); 50 if (config.desired_host == CONNECTION_HOST_INVALID) { 51 eeconfig_update_connection_default(); 52 } 53 } 54 55 __attribute__((weak)) void connection_host_changed_user(connection_host_t host) {} 56 __attribute__((weak)) void connection_host_changed_kb(connection_host_t host) {} 57 58 static void handle_host_changed(void) { 59 connection_host_changed_user(config.desired_host); 60 connection_host_changed_kb(config.desired_host); 61 62 // TODO: Remove deprecated callback 63 set_output_user(config.desired_host); 64 } 65 66 void connection_set_host_noeeprom(connection_host_t host) { 67 if (config.desired_host == host) { 68 return; 69 } 70 71 config.desired_host = host; 72 73 handle_host_changed(); 74 } 75 76 void connection_set_host(connection_host_t host) { 77 connection_set_host_noeeprom(host); 78 79 eeconfig_update_connection(&config); 80 } 81 82 void connection_next_host_noeeprom(void) { 83 uint8_t next = 0; 84 for (uint8_t i = 0; i < HOST_CANDIDATES_COUNT; i++) { 85 if (host_candidates[i] == config.desired_host) { 86 next = i == HOST_CANDIDATES_COUNT - 1 ? 0 : i + 1; 87 break; 88 } 89 } 90 91 connection_set_host_noeeprom(host_candidates[next]); 92 } 93 94 void connection_next_host(void) { 95 connection_next_host_noeeprom(); 96 97 eeconfig_update_connection(&config); 98 } 99 100 void connection_prev_host_noeeprom(void) { 101 uint8_t next = 0; 102 for (uint8_t i = 0; i < HOST_CANDIDATES_COUNT; i++) { 103 if (host_candidates[i] == config.desired_host) { 104 next = i == 0 ? HOST_CANDIDATES_COUNT - 1 : i - 1; 105 break; 106 } 107 } 108 109 connection_set_host_noeeprom(host_candidates[next]); 110 } 111 112 void connection_prev_host(void) { 113 connection_prev_host_noeeprom(); 114 115 eeconfig_update_connection(&config); 116 } 117 118 connection_host_t connection_get_host_raw(void) { 119 return config.desired_host; 120 } 121 122 connection_host_t connection_auto_detect_host(void) { 123 if (usb_connected_state()) { 124 return CONNECTION_HOST_USB; 125 } 126 127 #ifdef BLUETOOTH_ENABLE 128 if (bluetooth_is_connected()) { 129 return CONNECTION_HOST_BLUETOOTH; 130 } 131 #endif 132 133 return CONNECTION_HOST_NONE; 134 } 135 136 connection_host_t connection_get_host(void) { 137 if (config.desired_host == CONNECTION_HOST_AUTO) { 138 return connection_auto_detect_host(); 139 } 140 return config.desired_host; 141 }