aboutsummaryrefslogtreecommitdiff
path: root/quantum/connection/connection.c
blob: 1ff88768762d79cc246143c28bb687a84f9bc954 (plain)
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
// Copyright 2025 QMK
// SPDX-License-Identifier: GPL-2.0-or-later
#include "connection.h"
#include "eeconfig.h"
#include "usb_util.h"
#include "util.h"

#ifdef BLUETOOTH_ENABLE
#    include "bluetooth.h"
#endif

// ======== DEPRECATED DEFINES - DO NOT USE ========
#ifdef OUTPUT_DEFAULT
#    undef CONNECTION_HOST_DEFAULT
#    define CONNECTION_HOST_DEFAULT OUTPUT_DEFAULT
#endif

__attribute__((weak)) void set_output_user(uint8_t output) {}
// ========

#define CONNECTION_HOST_INVALID 0xFF

#ifndef CONNECTION_HOST_DEFAULT
#    define CONNECTION_HOST_DEFAULT CONNECTION_HOST_AUTO
#endif

static const connection_host_t host_candidates[] = {
    CONNECTION_HOST_AUTO,
    CONNECTION_HOST_USB,
#ifdef BLUETOOTH_ENABLE
    CONNECTION_HOST_BLUETOOTH,
#endif
#if 0
    CONNECTION_HOST_2P4GHZ,
#endif
};

#define HOST_CANDIDATES_COUNT ARRAY_SIZE(host_candidates)

static connection_config_t config = {.desired_host = CONNECTION_HOST_INVALID};

void eeconfig_update_connection_default(void) {
    config.desired_host = CONNECTION_HOST_DEFAULT;

    eeconfig_update_connection(&config);
}

void connection_init(void) {
    eeconfig_read_connection(&config);
    if (config.desired_host == CONNECTION_HOST_INVALID) {
        eeconfig_update_connection_default();
    }
}

__attribute__((weak)) void connection_host_changed_user(connection_host_t host) {}
__attribute__((weak)) void connection_host_changed_kb(connection_host_t host) {}

static void handle_host_changed(void) {
    connection_host_changed_user(config.desired_host);
    connection_host_changed_kb(config.desired_host);

    // TODO: Remove deprecated callback
    set_output_user(config.desired_host);
}

void connection_set_host_noeeprom(connection_host_t host) {
    if (config.desired_host == host) {
        return;
    }

    config.desired_host = host;

    handle_host_changed();
}

void connection_set_host(connection_host_t host) {
    connection_set_host_noeeprom(host);

    eeconfig_update_connection(&config);
}

void connection_next_host_noeeprom(void) {
    uint8_t next = 0;
    for (uint8_t i = 0; i < HOST_CANDIDATES_COUNT; i++) {
        if (host_candidates[i] == config.desired_host) {
            next = i == HOST_CANDIDATES_COUNT - 1 ? 0 : i + 1;
            break;
        }
    }

    connection_set_host_noeeprom(host_candidates[next]);
}

void connection_next_host(void) {
    connection_next_host_noeeprom();

    eeconfig_update_connection(&config);
}

void connection_prev_host_noeeprom(void) {
    uint8_t next = 0;
    for (uint8_t i = 0; i < HOST_CANDIDATES_COUNT; i++) {
        if (host_candidates[i] == config.desired_host) {
            next = i == 0 ? HOST_CANDIDATES_COUNT - 1 : i - 1;
            break;
        }
    }

    connection_set_host_noeeprom(host_candidates[next]);
}

void connection_prev_host(void) {
    connection_prev_host_noeeprom();

    eeconfig_update_connection(&config);
}

connection_host_t connection_get_host_raw(void) {
    return config.desired_host;
}

connection_host_t connection_auto_detect_host(void) {
    if (usb_connected_state()) {
        return CONNECTION_HOST_USB;
    }

#ifdef BLUETOOTH_ENABLE
    if (bluetooth_is_connected()) {
        return CONNECTION_HOST_BLUETOOTH;
    }
#endif

    return CONNECTION_HOST_NONE;
}

connection_host_t connection_get_host(void) {
    if (config.desired_host == CONNECTION_HOST_AUTO) {
        return connection_auto_detect_host();
    }
    return config.desired_host;
}