forked from kofal.net/zmk
chore: clang-format the codebase.
* Use the LLVM style * Override indent width (8) and column limit (100) * Fixes #142.
This commit is contained in:
160
app/src/ble.c
160
app/src/ble.c
@@ -45,7 +45,6 @@ static u8_t passkey_digit = 0;
|
||||
#define PROFILE_COUNT CONFIG_BT_MAX_PAIRED
|
||||
#endif
|
||||
|
||||
|
||||
static struct zmk_ble_profile profiles[PROFILE_COUNT];
|
||||
static u8_t active_profile;
|
||||
|
||||
@@ -53,13 +52,12 @@ static const struct bt_data zmk_ble_ad[] = {
|
||||
BT_DATA_BYTES(BT_DATA_FLAGS, (BT_LE_AD_GENERAL | BT_LE_AD_NO_BREDR)),
|
||||
BT_DATA_BYTES(BT_DATA_UUID16_SOME,
|
||||
#if !IS_ENABLED(CONFIG_ZMK_SPLIT_BLE_ROLE_PERIPHERAL)
|
||||
0x12, 0x18, /* HID Service */
|
||||
0x12, 0x18, /* HID Service */
|
||||
#endif
|
||||
0x0f, 0x18 /* Battery Service */
|
||||
),
|
||||
),
|
||||
#if IS_ENABLED(CONFIG_ZMK_SPLIT_BLE_ROLE_PERIPHERAL)
|
||||
BT_DATA_BYTES(BT_DATA_UUID128_ALL,
|
||||
ZMK_SPLIT_BT_SERVICE_UUID)
|
||||
BT_DATA_BYTES(BT_DATA_UUID128_ALL, ZMK_SPLIT_BT_SERVICE_UUID)
|
||||
#endif
|
||||
};
|
||||
|
||||
@@ -69,8 +67,7 @@ static bt_addr_le_t peripheral_addr;
|
||||
|
||||
#endif /* IS_ENABLED(CONFIG_ZMK_SPLIT_BLE_ROLE_CENTRAL) */
|
||||
|
||||
static void raise_profile_changed_event()
|
||||
{
|
||||
static void raise_profile_changed_event() {
|
||||
struct ble_active_profile_changed *ev = new_ble_active_profile_changed();
|
||||
ev->index = active_profile;
|
||||
ev->profile = &profiles[active_profile];
|
||||
@@ -78,13 +75,11 @@ static void raise_profile_changed_event()
|
||||
ZMK_EVENT_RAISE(ev);
|
||||
}
|
||||
|
||||
static bool active_profile_is_open()
|
||||
{
|
||||
static bool active_profile_is_open() {
|
||||
return !bt_addr_le_cmp(&profiles[active_profile].peer, BT_ADDR_LE_ANY);
|
||||
}
|
||||
|
||||
void set_profile_address(u8_t index, const bt_addr_le_t *addr)
|
||||
{
|
||||
void set_profile_address(u8_t index, const bt_addr_le_t *addr) {
|
||||
char setting_name[15];
|
||||
char addr_str[BT_ADDR_LE_STR_LEN];
|
||||
|
||||
@@ -97,8 +92,7 @@ void set_profile_address(u8_t index, const bt_addr_le_t *addr)
|
||||
raise_profile_changed_event();
|
||||
}
|
||||
|
||||
int zmk_ble_adv_pause()
|
||||
{
|
||||
int zmk_ble_adv_pause() {
|
||||
int err = bt_le_adv_stop();
|
||||
if (err) {
|
||||
LOG_ERR("Failed to stop advertising (err %d)", err);
|
||||
@@ -108,16 +102,12 @@ int zmk_ble_adv_pause()
|
||||
return 0;
|
||||
};
|
||||
|
||||
int zmk_ble_adv_resume()
|
||||
{
|
||||
LOG_DBG("active_profile %d, directed? %s", active_profile, active_profile_is_open() ? "no" : "yes");
|
||||
int zmk_ble_adv_resume() {
|
||||
LOG_DBG("active_profile %d, directed? %s", active_profile,
|
||||
active_profile_is_open() ? "no" : "yes");
|
||||
|
||||
int err = bt_le_adv_start(
|
||||
BT_LE_ADV_CONN_NAME,
|
||||
zmk_ble_ad, ARRAY_SIZE(zmk_ble_ad),
|
||||
NULL, 0);
|
||||
if (err)
|
||||
{
|
||||
int err = bt_le_adv_start(BT_LE_ADV_CONN_NAME, zmk_ble_ad, ARRAY_SIZE(zmk_ble_ad), NULL, 0);
|
||||
if (err) {
|
||||
LOG_ERR("Advertising failed to start (err %d)", err);
|
||||
return err;
|
||||
}
|
||||
@@ -125,10 +115,9 @@ int zmk_ble_adv_resume()
|
||||
return 0;
|
||||
};
|
||||
|
||||
int zmk_ble_clear_bonds()
|
||||
{
|
||||
int zmk_ble_clear_bonds() {
|
||||
LOG_DBG("");
|
||||
|
||||
|
||||
if (bt_addr_le_cmp(&profiles[active_profile].peer, BT_ADDR_LE_ANY)) {
|
||||
LOG_DBG("Unpairing!");
|
||||
bt_unpair(BT_ID_DEFAULT, &profiles[active_profile].peer);
|
||||
@@ -138,8 +127,7 @@ int zmk_ble_clear_bonds()
|
||||
return 0;
|
||||
};
|
||||
|
||||
int zmk_ble_prof_select(u8_t index)
|
||||
{
|
||||
int zmk_ble_prof_select(u8_t index) {
|
||||
LOG_DBG("profile %d", index);
|
||||
if (active_profile == index) {
|
||||
return 0;
|
||||
@@ -148,35 +136,26 @@ int zmk_ble_prof_select(u8_t index)
|
||||
active_profile = index;
|
||||
return settings_save_one("ble/active_profile", &active_profile, sizeof(active_profile));
|
||||
|
||||
raise_profile_changed_event();
|
||||
raise_profile_changed_event();
|
||||
};
|
||||
|
||||
int zmk_ble_prof_next()
|
||||
{
|
||||
int zmk_ble_prof_next() {
|
||||
LOG_DBG("");
|
||||
return zmk_ble_prof_select((active_profile + 1) % PROFILE_COUNT);
|
||||
};
|
||||
|
||||
int zmk_ble_prof_prev()
|
||||
{
|
||||
int zmk_ble_prof_prev() {
|
||||
LOG_DBG("");
|
||||
return zmk_ble_prof_select((active_profile + PROFILE_COUNT - 1) % PROFILE_COUNT);
|
||||
};
|
||||
|
||||
bt_addr_le_t *zmk_ble_active_profile_addr()
|
||||
{
|
||||
return &profiles[active_profile].peer;
|
||||
}
|
||||
bt_addr_le_t *zmk_ble_active_profile_addr() { return &profiles[active_profile].peer; }
|
||||
|
||||
char *zmk_ble_active_profile_name()
|
||||
{
|
||||
return profiles[active_profile].name;
|
||||
}
|
||||
char *zmk_ble_active_profile_name() { return profiles[active_profile].name; }
|
||||
|
||||
#if IS_ENABLED(CONFIG_ZMK_SPLIT_BLE_ROLE_CENTRAL)
|
||||
|
||||
void zmk_ble_set_peripheral_addr(bt_addr_le_t *addr)
|
||||
{
|
||||
void zmk_ble_set_peripheral_addr(bt_addr_le_t *addr) {
|
||||
memcpy(&peripheral_addr, addr, sizeof(bt_addr_le_t));
|
||||
settings_save_one("ble/peripheral_address", addr, sizeof(bt_addr_le_t));
|
||||
}
|
||||
@@ -185,8 +164,8 @@ void zmk_ble_set_peripheral_addr(bt_addr_le_t *addr)
|
||||
|
||||
#if IS_ENABLED(CONFIG_SETTINGS)
|
||||
|
||||
static int ble_profiles_handle_set(const char *name, size_t len, settings_read_cb read_cb, void *cb_arg)
|
||||
{
|
||||
static int ble_profiles_handle_set(const char *name, size_t len, settings_read_cb read_cb,
|
||||
void *cb_arg) {
|
||||
const char *next;
|
||||
|
||||
LOG_DBG("Setting BLE value %s", log_strdup(name));
|
||||
@@ -200,7 +179,8 @@ static int ble_profiles_handle_set(const char *name, size_t len, settings_read_c
|
||||
}
|
||||
|
||||
if (len != sizeof(struct zmk_ble_profile)) {
|
||||
LOG_ERR("Invalid profile size (got %d expected %d)", len, sizeof(struct zmk_ble_profile));
|
||||
LOG_ERR("Invalid profile size (got %d expected %d)", len,
|
||||
sizeof(struct zmk_ble_profile));
|
||||
return -EINVAL;
|
||||
}
|
||||
|
||||
@@ -247,19 +227,14 @@ static int ble_profiles_handle_set(const char *name, size_t len, settings_read_c
|
||||
return 0;
|
||||
};
|
||||
|
||||
struct settings_handler profiles_handler = {
|
||||
.name = "ble",
|
||||
.h_set = ble_profiles_handle_set
|
||||
};
|
||||
struct settings_handler profiles_handler = {.name = "ble", .h_set = ble_profiles_handle_set};
|
||||
#endif /* IS_ENABLED(CONFIG_SETTINGS) */
|
||||
|
||||
static void connected(struct bt_conn *conn, u8_t err)
|
||||
{
|
||||
static void connected(struct bt_conn *conn, u8_t err) {
|
||||
char addr[BT_ADDR_LE_STR_LEN];
|
||||
bt_addr_le_to_str(bt_conn_get_dst(conn), addr, sizeof(addr));
|
||||
|
||||
if (err)
|
||||
{
|
||||
if (err) {
|
||||
LOG_WRN("Failed to connect to %s (%u)", log_strdup(addr), err);
|
||||
return;
|
||||
}
|
||||
@@ -272,14 +247,12 @@ static void connected(struct bt_conn *conn, u8_t err)
|
||||
bt_conn_le_phy_update(conn, BT_CONN_LE_PHY_PARAM_2M);
|
||||
#endif
|
||||
|
||||
if (bt_conn_set_security(conn, BT_SECURITY_L2))
|
||||
{
|
||||
if (bt_conn_set_security(conn, BT_SECURITY_L2)) {
|
||||
LOG_ERR("Failed to set security");
|
||||
}
|
||||
}
|
||||
|
||||
static void disconnected(struct bt_conn *conn, u8_t reason)
|
||||
{
|
||||
static void disconnected(struct bt_conn *conn, u8_t reason) {
|
||||
char addr[BT_ADDR_LE_STR_LEN];
|
||||
|
||||
bt_addr_le_to_str(bt_conn_get_dst(conn), addr, sizeof(addr));
|
||||
@@ -287,29 +260,24 @@ static void disconnected(struct bt_conn *conn, u8_t reason)
|
||||
LOG_DBG("Disconnected from %s (reason 0x%02x)", log_strdup(addr), reason);
|
||||
|
||||
#if IS_ENABLED(CONFIG_ZMK_SPLIT_BLE_ROLE_CENTRAL)
|
||||
// if (bt_addr_le_cmp(&peripheral_addr, BT_ADDR_LE_ANY) && bt_addr_le_cmp(&peripheral_addr, bt_conn_get_dst(conn))) {
|
||||
// if (bt_addr_le_cmp(&peripheral_addr, BT_ADDR_LE_ANY) && bt_addr_le_cmp(&peripheral_addr,
|
||||
// bt_conn_get_dst(conn))) {
|
||||
// zmk_ble_adv_resume();
|
||||
// }
|
||||
#else
|
||||
#else
|
||||
// zmk_ble_adv_resume();
|
||||
#endif
|
||||
}
|
||||
|
||||
static void security_changed(struct bt_conn *conn, bt_security_t level,
|
||||
enum bt_security_err err)
|
||||
{
|
||||
static void security_changed(struct bt_conn *conn, bt_security_t level, enum bt_security_err err) {
|
||||
char addr[BT_ADDR_LE_STR_LEN];
|
||||
|
||||
bt_addr_le_to_str(bt_conn_get_dst(conn), addr, sizeof(addr));
|
||||
|
||||
if (!err)
|
||||
{
|
||||
if (!err) {
|
||||
LOG_DBG("Security changed: %s level %u", log_strdup(addr), level);
|
||||
}
|
||||
else
|
||||
{
|
||||
LOG_ERR("Security failed: %s level %u err %d", log_strdup(addr), level,
|
||||
err);
|
||||
} else {
|
||||
LOG_ERR("Security failed: %s level %u err %d", log_strdup(addr), level, err);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -319,8 +287,7 @@ static struct bt_conn_cb conn_callbacks = {
|
||||
.security_changed = security_changed,
|
||||
};
|
||||
|
||||
static void auth_passkey_display(struct bt_conn *conn, unsigned int passkey)
|
||||
{
|
||||
static void auth_passkey_display(struct bt_conn *conn, unsigned int passkey) {
|
||||
char addr[BT_ADDR_LE_STR_LEN];
|
||||
|
||||
bt_addr_le_to_str(bt_conn_get_dst(conn), addr, sizeof(addr));
|
||||
@@ -330,8 +297,7 @@ static void auth_passkey_display(struct bt_conn *conn, unsigned int passkey)
|
||||
|
||||
#ifdef CONFIG_ZMK_BLE_PASSKEY_ENTRY
|
||||
|
||||
static void auth_passkey_entry(struct bt_conn *conn)
|
||||
{
|
||||
static void auth_passkey_entry(struct bt_conn *conn) {
|
||||
char addr[BT_ADDR_LE_STR_LEN];
|
||||
|
||||
bt_addr_le_to_str(bt_conn_get_dst(conn), addr, sizeof(addr));
|
||||
@@ -342,14 +308,12 @@ static void auth_passkey_entry(struct bt_conn *conn)
|
||||
|
||||
#endif
|
||||
|
||||
static void auth_cancel(struct bt_conn *conn)
|
||||
{
|
||||
static void auth_cancel(struct bt_conn *conn) {
|
||||
char addr[BT_ADDR_LE_STR_LEN];
|
||||
|
||||
bt_addr_le_to_str(bt_conn_get_dst(conn), addr, sizeof(addr));
|
||||
|
||||
if (auth_passkey_entry_conn)
|
||||
{
|
||||
if (auth_passkey_entry_conn) {
|
||||
bt_conn_unref(auth_passkey_entry_conn);
|
||||
auth_passkey_entry_conn = NULL;
|
||||
}
|
||||
@@ -360,8 +324,8 @@ static void auth_cancel(struct bt_conn *conn)
|
||||
}
|
||||
|
||||
#if !IS_ENABLED(CONFIG_ZMK_SPLIT_BLE_ROLE_PERIPHERAL)
|
||||
static enum bt_security_err auth_pairing_accept(struct bt_conn *conn, const struct bt_conn_pairing_feat *const feat)
|
||||
{
|
||||
static enum bt_security_err auth_pairing_accept(struct bt_conn *conn,
|
||||
const struct bt_conn_pairing_feat *const feat) {
|
||||
struct bt_conn_info info;
|
||||
bt_conn_get_info(conn, &info);
|
||||
|
||||
@@ -375,8 +339,7 @@ static enum bt_security_err auth_pairing_accept(struct bt_conn *conn, const stru
|
||||
};
|
||||
#endif /* !IS_ENABLED(CONFIG_ZMK_SPLIT_BLE_ROLE_PERIPHERAL) */
|
||||
|
||||
static void auth_pairing_complete(struct bt_conn *conn, bool bonded)
|
||||
{
|
||||
static void auth_pairing_complete(struct bt_conn *conn, bool bonded) {
|
||||
struct bt_conn_info info;
|
||||
char addr[BT_ADDR_LE_STR_LEN];
|
||||
const bt_addr_le_t *dst = bt_conn_get_dst(conn);
|
||||
@@ -413,12 +376,9 @@ static struct bt_conn_auth_cb zmk_ble_auth_cb_display = {
|
||||
.cancel = auth_cancel,
|
||||
};
|
||||
|
||||
|
||||
static void zmk_ble_ready(int err)
|
||||
{
|
||||
static void zmk_ble_ready(int err) {
|
||||
LOG_DBG("ready? %d", err);
|
||||
if (err)
|
||||
{
|
||||
if (err) {
|
||||
LOG_ERR("Bluetooth init failed (err %d)", err);
|
||||
return;
|
||||
}
|
||||
@@ -426,12 +386,10 @@ static void zmk_ble_ready(int err)
|
||||
zmk_ble_adv_resume();
|
||||
}
|
||||
|
||||
static int zmk_ble_init(struct device *_arg)
|
||||
{
|
||||
static int zmk_ble_init(struct device *_arg) {
|
||||
int err = bt_enable(NULL);
|
||||
|
||||
if (err)
|
||||
{
|
||||
if (err) {
|
||||
LOG_ERR("BLUETOOTH FAILED (%d)", err);
|
||||
return err;
|
||||
}
|
||||
@@ -475,8 +433,7 @@ static int zmk_ble_init(struct device *_arg)
|
||||
return 0;
|
||||
}
|
||||
|
||||
int zmk_ble_unpair_all()
|
||||
{
|
||||
int zmk_ble_unpair_all() {
|
||||
int resp = 0;
|
||||
for (int i = BT_ID_DEFAULT; i < CONFIG_BT_ID_MAX; i++) {
|
||||
|
||||
@@ -490,17 +447,14 @@ int zmk_ble_unpair_all()
|
||||
return resp;
|
||||
};
|
||||
|
||||
bool zmk_ble_handle_key_user(struct zmk_key_event *key_event)
|
||||
{
|
||||
bool zmk_ble_handle_key_user(struct zmk_key_event *key_event) {
|
||||
zmk_key key = key_event->key;
|
||||
|
||||
if (!auth_passkey_entry_conn)
|
||||
{
|
||||
if (!auth_passkey_entry_conn) {
|
||||
return true;
|
||||
}
|
||||
|
||||
if (key < NUM_1 || key > NUM_0)
|
||||
{
|
||||
if (key < NUM_1 || key > NUM_0) {
|
||||
return true;
|
||||
}
|
||||
|
||||
@@ -508,11 +462,9 @@ bool zmk_ble_handle_key_user(struct zmk_key_event *key_event)
|
||||
|
||||
passkey_entries[passkey_digit++] = val;
|
||||
|
||||
if (passkey_digit == 6)
|
||||
{
|
||||
if (passkey_digit == 6) {
|
||||
u32_t passkey = 0;
|
||||
for (int i = 5; i >= 0; i--)
|
||||
{
|
||||
for (int i = 5; i >= 0; i--) {
|
||||
passkey = (passkey * 10) + val;
|
||||
}
|
||||
bt_conn_auth_passkey_entry(auth_passkey_entry_conn, passkey);
|
||||
@@ -523,6 +475,4 @@ bool zmk_ble_handle_key_user(struct zmk_key_event *key_event)
|
||||
return false;
|
||||
}
|
||||
|
||||
SYS_INIT(zmk_ble_init,
|
||||
APPLICATION,
|
||||
CONFIG_ZMK_BLE_INIT_PRIORITY);
|
||||
SYS_INIT(zmk_ble_init, APPLICATION, CONFIG_ZMK_BLE_INIT_PRIORITY);
|
||||
|
||||
Reference in New Issue
Block a user