qmk_firmware/quantum/xap/xap_handlers.c

318 lines
9.2 KiB
C
Raw Normal View History

/* Copyright 2021 Nick Brassel (@tzarc)
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include <quantum.h>
#include <xap.h>
2022-04-18 23:04:22 +00:00
#include "hardware_id.h"
2022-04-10 23:43:18 +00:00
#include "secure.h"
#ifndef SECURE_ENABLE
# define secure_get_status() SECURE_UNLOCKED
# define secure_request_unlock()
# define secure_lock()
#endif
#ifdef DYNAMIC_KEYMAP_ENABLE
# define keymap_max_layer_count() DYNAMIC_KEYMAP_LAYER_COUNT
#else
# define keymap_max_layer_count() keymap_layer_count()
#endif
void xap_respond_success(xap_token_t token) {
xap_send(token, XAP_RESPONSE_FLAG_SUCCESS, NULL, 0);
}
2022-03-09 09:01:20 +00:00
void xap_respond_failure(xap_token_t token, xap_response_flags_t response_flags) {
xap_send(token, response_flags, NULL, 0);
}
bool xap_respond_data(xap_token_t token, const void *data, size_t length) {
xap_send(token, XAP_RESPONSE_FLAG_SUCCESS, data, length);
return true;
}
bool xap_respond_data_P(xap_token_t token, const void *data, size_t length) {
uint8_t blob[length];
memcpy_P(blob, data, length);
return xap_respond_data(token, blob, length);
}
bool xap_respond_u32(xap_token_t token, uint32_t value) {
xap_send(token, XAP_RESPONSE_FLAG_SUCCESS, &value, sizeof(value));
return true;
}
2022-03-09 09:01:20 +00:00
uint32_t xap_route_qmk_ffffffffffffffff_getter(void) {
return 0x12345678;
}
2022-06-19 21:04:30 +00:00
bool xap_respond_get_config_blob_chunk(xap_token_t token, const void *data, size_t length) {
2022-04-01 23:11:22 +00:00
if (length != sizeof(uint16_t)) {
return false;
}
2022-06-19 21:04:30 +00:00
uint16_t offset = *((uint16_t *)data);
2022-06-19 21:04:30 +00:00
xap_route_qmk_config_blob_chunk_t ret = {0};
bool get_config_blob_chunk(uint16_t offset, uint8_t * data, uint8_t data_len);
get_config_blob_chunk(offset, (uint8_t *)&ret, sizeof(ret));
return xap_respond_data(token, &ret, sizeof(ret));
}
2022-04-10 23:43:18 +00:00
bool xap_respond_secure_status(xap_token_t token, const void *data, size_t length) {
uint8_t ret = secure_get_status();
return xap_respond_data(token, &ret, sizeof(ret));
}
bool xap_respond_secure_unlock(xap_token_t token, const void *data, size_t length) {
secure_request_unlock();
return xap_respond_data(token, NULL, 0);
}
bool xap_respond_secure_lock(xap_token_t token, const void *data, size_t length) {
secure_lock();
return xap_respond_data(token, NULL, 0);
}
#ifdef BOOTLOADER_JUMP_SUPPORTED
bool xap_respond_request_bootloader_jump(xap_token_t token, const void *data, size_t length) {
2022-06-19 21:04:30 +00:00
uint8_t ret = secure_is_unlocked();
// TODO: post to deferred queue so this request can return?
bool res = xap_respond_data(token, &ret, sizeof(ret));
reset_keyboard();
return res;
}
#endif
2022-04-18 23:04:22 +00:00
bool xap_respond_get_hardware_id(xap_token_t token, const void *data, size_t length) {
hardware_id_t ret = get_hardware_id();
return xap_respond_data(token, &ret, sizeof(ret));
}
bool xap_respond_keymap_get_layer_count(xap_token_t token, const void *data, size_t length) {
uint8_t ret = keymap_max_layer_count();
return xap_respond_data(token, &ret, sizeof(ret));
}
2022-06-19 21:04:30 +00:00
bool xap_respond_get_keymap_keycode(xap_token_t token, const void *data, size_t length) {
if (length != sizeof(xap_route_keymap_get_keymap_keycode_arg_t)) {
return false;
}
2022-06-19 21:04:30 +00:00
xap_route_keymap_get_keymap_keycode_arg_t *arg = (xap_route_keymap_get_keymap_keycode_arg_t *)data;
if (arg->layer >= keymap_max_layer_count()) {
return false;
}
2022-06-19 21:04:30 +00:00
keypos_t pos = MAKE_KEYPOS(arg->row, arg->column);
2022-06-19 21:04:30 +00:00
uint16_t keycode = keymap_key_to_keycode(arg->layer, pos);
return xap_respond_data(token, &keycode, sizeof(keycode));
}
2022-06-19 21:04:30 +00:00
#if ((defined(ENCODER_MAP_ENABLE)))
bool xap_respond_get_encoder_keycode(xap_token_t token, const void *data, size_t length) {
if (length != sizeof(xap_route_keymap_get_encoder_keycode_arg_t)) {
return false;
}
2022-06-19 21:04:30 +00:00
xap_route_keymap_get_encoder_keycode_arg_t *arg = (xap_route_keymap_get_encoder_keycode_arg_t *)data;
if (arg->layer >= keymap_max_layer_count()) {
return false;
}
2022-06-19 21:04:30 +00:00
keypos_t pos = MAKE_KEYPOS(arg->clockwise ? KEYLOC_ENCODER_CW : KEYLOC_ENCODER_CCW, arg->encoder);
uint16_t keycode = keymap_key_to_keycode(arg->layer, pos);
return xap_respond_data(token, &keycode, sizeof(keycode));
}
#endif
2022-04-20 21:38:06 +00:00
2022-06-19 21:04:30 +00:00
#if ((defined(DYNAMIC_KEYMAP_ENABLE)))
bool xap_respond_dynamic_keymap_set_keycode(xap_token_t token, const void *data, size_t length) {
if (length != sizeof(xap_route_remapping_set_keymap_keycode_arg_t)) {
2022-04-20 21:38:06 +00:00
return false;
}
2022-06-19 21:04:30 +00:00
xap_route_remapping_set_keymap_keycode_arg_t *arg = (xap_route_remapping_set_keymap_keycode_arg_t *)data;
2022-04-20 21:38:06 +00:00
if (arg->layer >= keymap_max_layer_count()) {
return false;
}
2022-06-19 21:04:30 +00:00
dynamic_keymap_set_keycode(arg->layer, arg->row, arg->column, arg->keycode);
xap_respond_success(token);
return true;
2022-04-20 21:38:06 +00:00
}
2022-06-19 21:04:30 +00:00
#endif
2022-04-20 21:38:06 +00:00
2022-06-19 21:04:30 +00:00
#if ((defined(DYNAMIC_KEYMAP_ENABLE) && defined(ENCODER_MAP_ENABLE)))
2022-04-20 21:38:06 +00:00
bool xap_respond_dynamic_encoder_set_keycode(xap_token_t token, const void *data, size_t length) {
2022-06-19 21:04:30 +00:00
if (length != sizeof(xap_route_remapping_set_encoder_keycode_arg_t)) {
2022-04-20 21:38:06 +00:00
return false;
}
2022-06-19 21:04:30 +00:00
xap_route_remapping_set_encoder_keycode_arg_t *arg = (xap_route_remapping_set_encoder_keycode_arg_t *)data;
2022-04-20 21:38:06 +00:00
if (arg->layer >= keymap_max_layer_count()) {
return false;
}
2022-04-20 21:38:06 +00:00
dynamic_keymap_set_encoder(arg->layer, arg->encoder, arg->clockwise, arg->keycode);
xap_respond_success(token);
return true;
}
2022-06-19 21:04:30 +00:00
#endif
2022-09-26 17:09:36 +00:00
2022-09-28 22:47:12 +00:00
#if ((defined(BACKLIGHT_ENABLE)))
# include "backlight.h"
extern backlight_config_t backlight_config;
bool xap_respond_get_backlight_config(xap_token_t token, const void *data, size_t length) {
xap_route_lighting_backlight_get_config_t ret;
ret.enable = backlight_config.enable;
ret.mode = backlight_config.breathing;
ret.val = backlight_config.level;
return xap_respond_data(token, &ret, sizeof(ret));
}
#endif
2022-09-26 17:09:36 +00:00
#if ((defined(RGBLIGHT_ENABLE)))
2022-09-28 22:47:12 +00:00
# include "rgblight.h"
2022-09-26 17:09:36 +00:00
extern rgblight_config_t rgblight_config;
2022-09-28 22:47:12 +00:00
// static uint8_t effect_map[][2] = {
// {1, RGBLIGHT_MODE_STATIC_LIGHT},
// # ifdef RGBLIGHT_EFFECT_BREATHING
// {2, RGBLIGHT_MODE_BREATHING},
// {3, RGBLIGHT_MODE_BREATHING + 1},
// {4, RGBLIGHT_MODE_BREATHING + 2},
// {5, RGBLIGHT_MODE_BREATHING + 3},
// # endif
// /*...*/
// };
// static uint8_t xap2rgb(uint8_t val) {
// for(uint8_t i = 0; i < ARRAY_SIZE(effect_map); i++){
// if (effect_map[i][0] == val) {
// return effect_map[i][1];
// }
// }
// return 0;
// }
// static uint8_t rgb2xap(uint8_t val) {
// for(uint8_t i = 0; i < ARRAY_SIZE(effect_map); i++){
// if (effect_map[i][1] == val) {
// return effect_map[i][0];
// }
// }
// return 0;
// }
static uint8_t effect_map[43] = {
0, //
RGBLIGHT_MODE_STATIC_LIGHT, //
# ifdef RGBLIGHT_EFFECT_BREATHING
RGBLIGHT_MODE_BREATHING,
RGBLIGHT_MODE_BREATHING + 1,
RGBLIGHT_MODE_BREATHING + 2,
RGBLIGHT_MODE_BREATHING + 3,
# endif
0,
/*...*/
};
// static uint8_t xap2rgb(uint8_t val) {
// return effect_map[val];
// }
static uint8_t rgb2xap(uint8_t val) {
for (uint8_t i = 0; i < ARRAY_SIZE(effect_map); i++) {
if (effect_map[i] == val) {
return i;
}
}
return 0;
}
2022-09-26 17:09:36 +00:00
bool xap_respond_get_rgblight_config(xap_token_t token, const void *data, size_t length) {
xap_route_lighting_rgblight_get_config_t ret;
ret.enable = rgblight_config.enable;
2022-09-28 22:47:12 +00:00
ret.mode = rgb2xap(rgblight_config.mode);
2022-09-26 17:09:36 +00:00
ret.hue = rgblight_config.hue;
ret.sat = rgblight_config.sat;
ret.val = rgblight_config.val;
ret.speed = rgblight_config.speed;
return xap_respond_data(token, &ret, sizeof(ret));
}
#endif
2022-09-28 22:47:12 +00:00
#if ((defined(RGB_MATRIX_ENABLE)))
# include "rgb_matrix.h"
extern rgb_config_t rgb_matrix_config;
static uint8_t effect_map[43] = {
0, //
RGB_MATRIX_SOLID_COLOR, //
# ifdef ENABLE_RGB_MATRIX_ALPHAS_MODS
RGB_MATRIX_ALPHAS_MODS,
# endif
0,
/*...*/
};
// static uint8_t xap2rgb(uint8_t val) {
// return effect_map[val];
// }
static uint8_t rgb2xap(uint8_t val) {
for (uint8_t i = 0; i < ARRAY_SIZE(effect_map); i++) {
if (effect_map[i] == val) {
return i;
}
}
return 0;
}
bool xap_respond_get_rgb_matrix_config(xap_token_t token, const void *data, size_t length) {
xap_route_lighting_rgb_matrix_get_config_t ret;
ret.enable = rgb_matrix_config.enable;
ret.mode = rgb2xap(rgb_matrix_config.mode);
ret.hue = rgb_matrix_config.hsv.h;
ret.sat = rgb_matrix_config.hsv.s;
ret.val = rgb_matrix_config.hsv.v;
ret.speed = rgb_matrix_config.speed;
ret.flags = rgb_matrix_config.flags;
return xap_respond_data(token, &ret, sizeof(ret));
}
#endif