Implement codegen for more data types - codegen for return_execute stubs removed

This commit is contained in:
zvecr 2022-04-01 23:37:58 +01:00
parent d968e6c005
commit e7d9d6675c
6 changed files with 223 additions and 67 deletions

View file

@ -24,6 +24,64 @@
return_purpose: capabilities
return_constant: XAP_ROUTE_DYNAMIC_KEYMAP_CAPABILITIES
}
0x01: {
type: command
name: Get Layer Count
define: GET_LAYER_COUNT
description: TODO
return_type: u8
return_constant: DYNAMIC_KEYMAP_LAYER_COUNT
}
0x02: {
type: command
name: Get Keycode
define: GET_KEYMAP_KEYCODE
description: TODO
request_type: struct
request_struct_members: [
{
type: u8
name: Layer
},
{
type: u8
name: Row
},
{
type: u8
name: Column
}
]
return_type: u16
return_execute: dynamic_keymap_get_keycode
}
0x03: {
type: command
name: Set Keycode
define: SET_KEYMAP_KEYCODE
description: TODO
request_type: struct
request_struct_members: [
{
type: u8
name: Layer
},
{
type: u8
name: Row
},
{
type: u8
name: Column
},
{
type: u16
name: Keycode
}
]
return_type: u8
return_execute: dynamic_keymap_set_keycode
}
}
}

View file

@ -58,8 +58,12 @@ def _xap_transaction(device, sub, route, ret_len, *args):
args_data = []
args_len = 2
if len(args) == 1:
args_len += 2
args_data = args[0].to_bytes(2, byteorder='little')
if isinstance(args[0], (bytes, bytearray)):
args_len += len(args[0])
args_data = args[0]
else:
args_len += 2
args_data = args[0].to_bytes(2, byteorder='little')
padding_len = 64 - 3 - args_len
padding = b"\x00" * padding_len
@ -156,4 +160,23 @@ def xap(cli):
if cli.args.list:
return _list_devices()
cli.log.warn("TODO: Device specific stuff")
# Connect to first available device
dev = _search()[0]
device = hid.Device(path=dev['path'])
cli.log.info("Connected to:%04x:%04x %s %s", dev['vendor_id'], dev['product_id'], dev['manufacturer_string'], dev['product_string'])
# get layer count
layers = _xap_transaction(device, 0x04, 0x01, 1)
layers = int.from_bytes(layers, "little")
print(f'layers:{layers}')
# get keycode [layer:0, row:0, col:0]
keycode = _xap_transaction(device, 0x04, 0x02, 2, b"\x00\x00\x00")
keycode = int.from_bytes(keycode, "little")
keycode_map = {
0x29: 'KC_ESCAPE'
}
print('keycode:' + keycode_map.get(keycode, 'unknown'))
# Reboot
# _xap_transaction(device, 0x01, 0x07, 1)

View file

@ -3,6 +3,7 @@
import re
from fnvhash import fnv1a_32
from qmk.casing import to_snake
from qmk.commands import dump_lines
from qmk.git import git_get_version
from qmk.constants import GPL2_HEADER_C_LIKE, GENERATED_HEADER_C_LIKE
@ -112,7 +113,58 @@ def _append_route_capabilities(lines, container, container_id=None, route_stack=
route_stack.pop()
def _append_types(lines, container):
def _append_route_types(lines, container, container_id=None, route_stack=None):
"""Handles creating
"""
if route_stack is None:
route_stack = [container]
else:
route_stack.append(container)
route_name = to_snake('_'.join([r['define'] for r in route_stack]))
# Inbound
if 'request_struct_members' in container:
request_struct_members = container['request_struct_members']
lines.append(f'typedef struct {{')
for member in request_struct_members:
member_type = _get_c_type(member['type'])
member_name = to_snake(member['name'])
lines.append(f' {member_type} {member_name};')
lines.append(f'}} {route_name}_arg_t;')
elif 'request_type' in container:
request_type = container['request_type']
lines.append(f'typedef {_get_c_type(request_type)} {route_name}_arg_t;')
# Outbound
qualifier = 'const' if 'return_constant' in container else ''
if 'return_struct_members' in container:
return_struct_members = container['return_struct_members']
lines.append(f'typedef struct {{')
for member in return_struct_members:
member_type = _get_c_type(member['type'])
member_name = f'{qualifier} {to_snake(member["name"])}'
lines.append(f' {member_type} {member_name};')
lines.append(f'}} {route_name}_t;')
elif 'return_type' in container:
return_type = container['return_type']
if return_type == 'u8[32]':
lines.append(f'typedef struct {{ uint8_t x[32]; }} {route_name}_t;')
else:
lines.append(f'typedef {_get_c_type(return_type)} {route_name}_t;')
# Recurse
if 'routes' in container:
for route_id in container['routes']:
route = container['routes'][route_id]
_append_route_types(lines, route, route_id, route_stack)
route_stack.pop()
def _append_internal_types(lines, container):
"""Handles creating the various constants, types, defines, etc.
"""
response_flags = container.get('response_flags', {})
@ -167,7 +219,9 @@ def generate_header(output_file, keyboard):
lines.append('')
# Types
_append_types(lines, xap_defs)
_append_internal_types(lines, xap_defs)
lines.append('')
_append_route_types(lines, xap_defs)
lines.append('')
# Append the route and command defines

View file

@ -47,7 +47,11 @@ def _get_route_type(container):
if container['return_type'] == 'u8':
return 'XAP_VALUE'
elif 'return_constant' in container:
if container['return_type'] == 'u32':
if container['return_type'] == 'u8':
return 'XAP_CONST_MEM'
elif container['return_type'] == 'u16':
return 'XAP_CONST_MEM'
elif container['return_type'] == 'u32':
return 'XAP_CONST_MEM'
elif container['return_type'] == 'struct':
return 'XAP_CONST_MEM'
@ -69,42 +73,7 @@ def _append_routing_table_declaration(lines, container, container_id, route_stac
elif 'return_execute' in container:
execute = container['return_execute']
request_type = container.get('request_type', None)
return_type = container['return_type']
lines.append(
f'''
bool xap_respond_{execute}(xap_token_t token, const uint8_t *data, size_t data_len) {{
if (data_len != {_get_c_size(request_type)}) {{
xap_respond_failure(token, 0);
return false;
}}
uint8_t ret[{_get_c_size(return_type)}] = {{0}};
'''
)
if not request_type:
lines.append(f'''
bool {execute}(uint8_t *ret, uint8_t ret_len);
if(!{execute}(ret, sizeof(ret))) {{
xap_respond_failure(token, 0);
return false;
}}
''')
else:
lines.append(
f'''
{_get_c_type(request_type)} *argp = ({_get_c_type(request_type)} *)&data[0];
bool {execute}({_get_c_type(request_type)} arg, uint8_t *ret, uint8_t ret_len);
if(!{execute}(*argp, ret, sizeof(ret))) {{
xap_respond_failure(token, 0);
return false;
}}
'''
)
lines.append('''
return xap_respond_data(token, ret, sizeof(ret));
}''')
lines.append(f'bool xap_respond_{execute}(xap_token_t token, const uint8_t *data, size_t data_len);')
# elif 'return_value' in container:
# value = container['return_value']
@ -114,21 +83,24 @@ bool xap_respond_{execute}(xap_token_t token, const uint8_t *data, size_t data_l
elif 'return_constant' in container:
if container['return_type'] == 'u32':
if container['return_type'] == 'u8':
constant = container['return_constant']
lines.append('')
lines.append(f'static const uint8_t {route_name}_data PROGMEM = {constant};')
elif container['return_type'] == 'u16':
constant = container['return_constant']
lines.append('')
lines.append(f'static const uint16_t {route_name}_data PROGMEM = {constant};')
elif container['return_type'] == 'u32':
constant = container['return_constant']
lines.append('')
lines.append(f'static const uint32_t {route_name}_data PROGMEM = {constant};')
elif container['return_type'] == 'struct':
lines.append('')
lines.append(f'static const struct {route_name}_t {{')
for member in container['return_struct_members']:
member_type = _get_c_type(member['type'])
member_name = to_snake(member['name'])
lines.append(f' const {member_type} {member_name};')
lines.append(f'}} {route_name}_data PROGMEM = {{')
lines.append(f'static const {route_name}_t {route_name}_data PROGMEM = {{')
for constant in container['return_constant']:
lines.append(f' {constant},')
@ -212,7 +184,11 @@ def _append_routing_table_entry(lines, container, container_id, route_stack):
elif 'return_value' in container:
_append_routing_table_entry_value(lines, container, container_id, route_stack)
elif 'return_constant' in container:
if container['return_type'] == 'u32':
if container['return_type'] == 'u8':
_append_routing_table_entry_const_data(lines, container, container_id, route_stack)
elif container['return_type'] == 'u16':
_append_routing_table_entry_const_data(lines, container, container_id, route_stack)
elif container['return_type'] == 'u32':
_append_routing_table_entry_const_data(lines, container, container_id, route_stack)
elif container['return_type'] == 'struct':
_append_routing_table_entry_const_data(lines, container, container_id, route_stack)

View file

@ -29,21 +29,6 @@ bool get_info_json_chunk(uint16_t offset, uint8_t *data, uint8_t data_len) {
uint8_t secure_status = 2;
// TODO: how to set this if "custom" is just an empty stub
#ifndef BOOTLOADER_JUMP_SUPPORTED
# define BOOTLOADER_JUMP_SUPPORTED
#endif
#ifdef BOOTLOADER_JUMP_SUPPORTED
bool request_bootloader_jump(uint8_t *data, uint8_t data_len) {
data[0] = secure_status == 2;
// TODO: post to deferred queue so this request can return?
reset_keyboard();
return true;
}
#endif
#define QSTR2(z) #z
#define QSTR(z) QSTR2(z)

View file

@ -17,6 +17,10 @@
#include <quantum.h>
#include <xap.h>
void xap_respond_success(xap_token_t token) {
xap_send(token, XAP_RESPONSE_FLAG_SUCCESS, NULL, 0);
}
void xap_respond_failure(xap_token_t token, xap_response_flags_t response_flags) {
xap_send(token, response_flags, NULL, 0);
}
@ -40,3 +44,59 @@ bool xap_respond_u32(xap_token_t token, uint32_t value) {
uint32_t xap_route_qmk_ffffffffffffffff_getter(void) {
return 0x12345678;
}
bool xap_respond_get_info_json_chunk(xap_token_t token, const void *data, size_t length) {
if(length != sizeof(uint16_t)){
return false;
}
uint16_t offset = *((uint16_t*)data);
xap_route_qmk_info_query_t ret = {0};
bool get_info_json_chunk(uint16_t offset, uint8_t *data, uint8_t data_len);
get_info_json_chunk(offset, (uint8_t *)&ret, sizeof(ret));
return xap_respond_data(token, &ret, sizeof(ret));
}
// TODO: how to set this if "custom" is just an empty stub
#ifndef BOOTLOADER_JUMP_SUPPORTED
# define BOOTLOADER_JUMP_SUPPORTED
#endif
#ifdef BOOTLOADER_JUMP_SUPPORTED
bool xap_respond_request_bootloader_jump(xap_token_t token, const void *data, size_t length) {
extern uint8_t secure_status;
uint8_t ret = secure_status == 2;
// TODO: post to deferred queue so this request can return?
bool res = xap_respond_data(token, &ret, sizeof(ret));
reset_keyboard();
return res;
}
#endif
#if ((defined(DYNAMIC_KEYMAP_ENABLE)))
bool xap_respond_dynamic_keymap_get_keycode(xap_token_t token, const void *data, size_t length) {
if(length != sizeof(xap_route_dynamic_keymap_get_keymap_keycode_arg_t)){
return false;
}
xap_route_dynamic_keymap_get_keymap_keycode_arg_t* arg = (xap_route_dynamic_keymap_get_keymap_keycode_arg_t*)data;
uint16_t keycode = dynamic_keymap_get_keycode(arg->layer, arg->row, arg->column);
return xap_respond_data(token, &keycode, sizeof(keycode));
}
bool xap_respond_dynamic_keymap_set_keycode(xap_token_t token, const void *data, size_t length) {
if(length != sizeof(xap_route_dynamic_keymap_set_keymap_keycode_arg_t)){
return false;
}
xap_route_dynamic_keymap_set_keymap_keycode_arg_t* arg = (xap_route_dynamic_keymap_set_keymap_keycode_arg_t*)data;
dynamic_keymap_set_keycode(arg->layer, arg->row, arg->column, arg->keycode);
xap_respond_success(token);
return true;
}
#endif