inline_generator.py 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433
  1. """This script generates the XAP protocol generated header to be compiled into QMK.
  2. """
  3. import re
  4. from pathlib import Path
  5. from qmk.casing import to_snake
  6. from qmk.commands import dump_lines
  7. from qmk.constants import GPL2_HEADER_C_LIKE, GENERATED_HEADER_C_LIKE
  8. from qmk.xap.common import merge_xap_defs, route_conditions
  9. from qmk.json_schema import json_load
  10. PREFIX_MAP = {
  11. 'rgblight': {
  12. 'ifdef': 'RGBLIGHT_EFFECT',
  13. 'def': 'RGBLIGHT_MODE',
  14. },
  15. 'rgb_matrix': {
  16. 'ifdef': 'ENABLE_RGB_MATRIX',
  17. 'def': 'RGB_MATRIX',
  18. },
  19. 'led_matrix': {
  20. 'ifdef': 'ENABLE_LED_MATRIX',
  21. 'def': 'LED_MATRIX',
  22. },
  23. }
  24. def _get_lighting_spec(xap_defs, feature):
  25. version = xap_defs['uses'][feature]
  26. spec = json_load(Path(f'data/constants/{feature}_{version}.json'))
  27. # preprocess for gross rgblight "mode + n"
  28. for obj in spec.get('effects', {}).values():
  29. define = obj['key']
  30. offset = 0
  31. found = re.match('(.*)_(\\d+)$', define)
  32. if found:
  33. define = found.group(1)
  34. offset = int(found.group(2)) - 1
  35. obj['define'] = define
  36. obj['offset'] = offset
  37. return spec
  38. def _get_c_type(xap_type):
  39. if xap_type == 'bool':
  40. return 'bool'
  41. elif xap_type == 'u8':
  42. return 'uint8_t'
  43. elif xap_type == 'u16':
  44. return 'uint16_t'
  45. elif xap_type == 'u32':
  46. return 'uint32_t'
  47. elif xap_type == 'u64':
  48. return 'uint64_t'
  49. elif xap_type == 'struct':
  50. return 'struct'
  51. elif xap_type == 'string':
  52. return 'const char *'
  53. return 'unknown'
  54. def _get_c_size(xap_type):
  55. if xap_type == 'u8':
  56. return 'sizeof(uint8_t)'
  57. elif xap_type == 'u16':
  58. return 'sizeof(uint16_t)'
  59. elif xap_type == 'u32':
  60. return 'sizeof(uint32_t)'
  61. elif xap_type == 'u64':
  62. return 8
  63. elif xap_type == 'u8[32]':
  64. return 32
  65. return 0
  66. def _get_route_type(container):
  67. if 'routes' in container:
  68. return 'XAP_ROUTE'
  69. elif 'return_execute' in container:
  70. return 'XAP_EXECUTE'
  71. elif 'return_value' in container:
  72. if container['return_type'] == 'u8':
  73. return 'XAP_VALUE'
  74. elif 'return_constant' in container:
  75. if container['return_type'] == 'u8':
  76. return 'XAP_CONST_MEM'
  77. elif container['return_type'] == 'u16':
  78. return 'XAP_CONST_MEM'
  79. elif container['return_type'] == 'u32':
  80. return 'XAP_CONST_MEM'
  81. elif container['return_type'] == 'u64':
  82. return 'XAP_CONST_MEM'
  83. elif container['return_type'] == 'struct':
  84. return 'XAP_CONST_MEM'
  85. elif container['return_type'] == 'string':
  86. return 'XAP_CONST_MEM'
  87. elif 'return_getter' in container:
  88. if container['return_type'] == 'u32':
  89. return 'XAP_GETTER'
  90. return 'UNSUPPORTED'
  91. def _append_routing_table_declaration(lines, container, container_id, route_stack):
  92. route_stack.append(container)
  93. route_name = to_snake('_'.join([r['define'] for r in route_stack]))
  94. condition = route_conditions(route_stack)
  95. if condition:
  96. lines.append(f'#if {condition}')
  97. if 'routes' in container:
  98. pass
  99. elif 'return_execute' in container:
  100. execute = container['return_execute']
  101. lines.append(f'bool xap_respond_{execute}(xap_token_t token, const uint8_t *data, size_t data_len);')
  102. # elif 'return_value' in container:
  103. # value = container['return_value']
  104. # return_type = container['return_type']
  105. # lines.append('')
  106. # lines.append(f'{_get_c_type(return_type)} {value} = 0;')
  107. elif 'return_constant' in container:
  108. if container['return_type'] == 'u8':
  109. constant = container['return_constant']
  110. lines.append('')
  111. lines.append(f'static const uint8_t {route_name}_data PROGMEM = {constant};')
  112. elif container['return_type'] == 'u16':
  113. constant = container['return_constant']
  114. lines.append('')
  115. lines.append(f'static const uint16_t {route_name}_data PROGMEM = {constant};')
  116. elif container['return_type'] == 'u32':
  117. constant = container['return_constant']
  118. lines.append('')
  119. lines.append(f'static const uint32_t {route_name}_data PROGMEM = {constant};')
  120. elif container['return_type'] == 'u64':
  121. constant = container['return_constant']
  122. lines.append('')
  123. lines.append(f'static const uint64_t {route_name}_data PROGMEM = {constant};')
  124. elif container['return_type'] == 'struct':
  125. lines.append('')
  126. lines.append(f'static const {route_name}_t {route_name}_data PROGMEM = {{')
  127. for constant in container['return_constant']:
  128. lines.append(f' {constant},')
  129. lines.append('};')
  130. elif container['return_type'] == 'string':
  131. constant = container['return_constant']
  132. lines.append('')
  133. lines.append(f'static const char {route_name}_str[] PROGMEM = {constant};')
  134. elif 'return_getter' in container:
  135. if container['return_type'] == 'u32':
  136. lines.append('')
  137. lines.append(f'extern uint32_t {route_name}_getter(void);')
  138. elif container['return_type'] == 'struct':
  139. pass
  140. if condition:
  141. lines.append(f'#endif // {condition}')
  142. lines.append('')
  143. route_stack.pop()
  144. def _append_routing_table_entry_flags(lines, container, container_id, route_stack):
  145. pem_map = {
  146. None: 'ROUTE_PERMISSIONS_INSECURE',
  147. 'secure': 'ROUTE_PERMISSIONS_SECURE',
  148. }
  149. is_secure = pem_map[container.get('permissions', None)]
  150. lines.append(' .flags = {')
  151. lines.append(f' .type = {_get_route_type(container)},')
  152. lines.append(f' .secure = {is_secure},')
  153. lines.append(' },')
  154. def _append_routing_table_entry_route(lines, container, container_id, route_stack):
  155. route_name = to_snake('_'.join([r['define'] for r in route_stack]))
  156. lines.append(f' .child_routes = {route_name}_table,')
  157. lines.append(f' .child_routes_len = sizeof({route_name}_table)/sizeof(xap_route_t),')
  158. def _append_routing_table_entry_execute(lines, container, container_id, route_stack):
  159. value = container['return_execute']
  160. lines.append(f' .handler = xap_respond_{value},')
  161. def _append_routing_table_entry_value(lines, container, container_id, route_stack):
  162. value = container['return_value']
  163. lines.append(f' .const_data = &{value},')
  164. lines.append(f' .const_data_len = sizeof({value}),')
  165. def _append_routing_table_entry_u32getter(lines, container, container_id, route_stack):
  166. route_name = to_snake('_'.join([r['define'] for r in route_stack]))
  167. lines.append(f' .u32getter = &{route_name}_getter,')
  168. def _append_routing_table_entry_const_data(lines, container, container_id, route_stack):
  169. route_name = to_snake('_'.join([r['define'] for r in route_stack]))
  170. lines.append(f' .const_data = &{route_name}_data,')
  171. lines.append(f' .const_data_len = sizeof({route_name}_data),')
  172. def _append_routing_table_entry_string(lines, container, container_id, route_stack):
  173. route_name = to_snake('_'.join([r['define'] for r in route_stack]))
  174. lines.append(f' .const_data = {route_name}_str,')
  175. lines.append(f' .const_data_len = sizeof({route_name}_str) - 1,')
  176. def _append_routing_table_entry(lines, container, container_id, route_stack):
  177. route_stack.append(container)
  178. route_name = '_'.join([r['define'] for r in route_stack])
  179. condition = route_conditions(route_stack)
  180. if condition:
  181. lines.append(f'#if {condition}')
  182. lines.append(f' [{route_name}] = {{')
  183. _append_routing_table_entry_flags(lines, container, container_id, route_stack)
  184. if 'routes' in container:
  185. _append_routing_table_entry_route(lines, container, container_id, route_stack)
  186. elif 'return_execute' in container:
  187. _append_routing_table_entry_execute(lines, container, container_id, route_stack)
  188. elif 'return_value' in container:
  189. _append_routing_table_entry_value(lines, container, container_id, route_stack)
  190. elif 'return_constant' in container:
  191. if container['return_type'] == 'u8':
  192. _append_routing_table_entry_const_data(lines, container, container_id, route_stack)
  193. elif container['return_type'] == 'u16':
  194. _append_routing_table_entry_const_data(lines, container, container_id, route_stack)
  195. elif container['return_type'] == 'u32':
  196. _append_routing_table_entry_const_data(lines, container, container_id, route_stack)
  197. elif container['return_type'] == 'u64':
  198. _append_routing_table_entry_const_data(lines, container, container_id, route_stack)
  199. elif container['return_type'] == 'struct':
  200. _append_routing_table_entry_const_data(lines, container, container_id, route_stack)
  201. elif container['return_type'] == 'string':
  202. _append_routing_table_entry_string(lines, container, container_id, route_stack)
  203. elif 'return_getter' in container:
  204. if container['return_type'] == 'u32':
  205. _append_routing_table_entry_u32getter(lines, container, container_id, route_stack)
  206. lines.append(' },')
  207. if condition:
  208. lines.append(f'#endif // {condition}')
  209. route_stack.pop()
  210. def _append_routing_tables(lines, container, container_id=None, route_stack=None):
  211. """Handles building the list of the XAP routes, combining parent and child names together, as well as the route number.
  212. """
  213. if route_stack is None:
  214. route_stack = [container]
  215. else:
  216. route_stack.append(container)
  217. route_name = to_snake('_'.join([r['define'] for r in route_stack]))
  218. condition = route_conditions(route_stack)
  219. if 'routes' in container:
  220. for route_id in container['routes']:
  221. route = container['routes'][route_id]
  222. _append_routing_tables(lines, route, route_id, route_stack)
  223. for route_id in container['routes']:
  224. route = container['routes'][route_id]
  225. _append_routing_table_declaration(lines, route, route_id, route_stack)
  226. lines.append('')
  227. if condition:
  228. lines.append(f'#if {condition}')
  229. lines.append(f'static const xap_route_t {route_name}_table[] PROGMEM = {{')
  230. for route_id in container['routes']:
  231. route = container['routes'][route_id]
  232. _append_routing_table_entry(lines, route, route_id, route_stack)
  233. lines.append('};')
  234. if condition:
  235. lines.append(f'#endif // {condition}')
  236. lines.append('')
  237. route_stack.pop()
  238. def _append_broadcast_messages(lines, container):
  239. """TODO:
  240. """
  241. broadcast_messages = container.get('broadcast_messages', {})
  242. broadcast_prefix = broadcast_messages['define_prefix']
  243. for key, value in broadcast_messages['messages'].items():
  244. define = value.get('define')
  245. name = to_snake(f'{broadcast_prefix}_{define}')
  246. if 'return_type' in value:
  247. ret_type = _get_c_type(value['return_type'])
  248. lines.append(f'void {name}({ret_type} value) {{ xap_broadcast({key}, &value, sizeof(value)); }}')
  249. else:
  250. lines.append(f'void {name}(const void *data, size_t length){{ xap_broadcast({key}, data, length); }}')
  251. def _append_lighting_map(lines, feature, spec):
  252. """TODO:
  253. """
  254. groups = spec.get('groups', {})
  255. ifdef_prefix = PREFIX_MAP[feature]['ifdef']
  256. def_prefix = PREFIX_MAP[feature]['def']
  257. lines.append(f'static uint8_t {feature}_effect_map[][2] = {{')
  258. for id, obj in spec.get('effects', {}).items():
  259. define = obj['define']
  260. offset = f' + {obj["offset"]}' if obj['offset'] else ''
  261. line = f'''
  262. #ifdef {ifdef_prefix}_{define}
  263. {{ {id}, {def_prefix}_{define}{offset}}},
  264. #endif'''
  265. group = groups.get(obj.get('group', None), {}).get('define', None)
  266. if group:
  267. line = f'''
  268. #ifdef {group}
  269. {line}
  270. #endif'''
  271. lines.append(line)
  272. lines.append('};')
  273. # add helper funcs
  274. lines.append(
  275. f'''
  276. uint8_t {feature}2xap(uint8_t val) {{
  277. for(uint8_t i = 0; i < ARRAY_SIZE({feature}_effect_map); i++) {{
  278. if ({feature}_effect_map[i][1] == val)
  279. return {feature}_effect_map[i][0];
  280. }}
  281. return 0xFF;
  282. }}
  283. uint8_t xap2{feature}(uint8_t val) {{
  284. for(uint8_t i = 0; i < ARRAY_SIZE({feature}_effect_map); i++) {{
  285. if ({feature}_effect_map[i][0] == val)
  286. return {feature}_effect_map[i][1];
  287. }}
  288. return 0xFF;
  289. }}'''
  290. )
  291. def _append_lighting_bitmask(lines, feature, spec):
  292. """TODO:
  293. """
  294. groups = spec.get('groups', {})
  295. ifdef_prefix = PREFIX_MAP[feature]['ifdef']
  296. lines.append(f'enum {{ ENABLED_{feature.upper()}_EFFECTS = 0')
  297. for id, obj in spec.get('effects', {}).items():
  298. define = obj['define']
  299. line = f'''
  300. #ifdef {ifdef_prefix}_{define}
  301. | (1ULL << {id})
  302. #endif'''
  303. group = groups.get(obj.get('group', None), {}).get('define', None)
  304. if group:
  305. line = f'''
  306. #ifdef {group}
  307. {line}
  308. #endif'''
  309. lines.append(line)
  310. lines.append('};')
  311. def _append_lighting_mapping(lines, xap_defs):
  312. """TODO:
  313. """
  314. # TODO: remove bodge for always enabled effects
  315. lines.append('''
  316. #define RGBLIGHT_EFFECT_STATIC_LIGHT
  317. #define ENABLE_RGB_MATRIX_SOLID_COLOR
  318. #define ENABLE_LED_MATRIX_SOLID
  319. ''')
  320. for feature in PREFIX_MAP.keys():
  321. spec = _get_lighting_spec(xap_defs, feature)
  322. lines.append(f'#ifdef {feature.upper()}_ENABLE')
  323. _append_lighting_map(lines, feature, spec)
  324. _append_lighting_bitmask(lines, feature, spec)
  325. lines.append(f'#endif //{feature.upper()}_ENABLE')
  326. def generate_inline(output_file, keyboard, keymap):
  327. """Generates the XAP protocol header file, generated during normal build.
  328. """
  329. xap_defs = merge_xap_defs(keyboard, keymap)
  330. # Preamble
  331. lines = [GPL2_HEADER_C_LIKE, GENERATED_HEADER_C_LIKE, '']
  332. # TODO: gen somewhere else?
  333. _append_lighting_mapping(lines, xap_defs)
  334. # Add all the generated code
  335. _append_broadcast_messages(lines, xap_defs)
  336. _append_routing_tables(lines, xap_defs)
  337. dump_lines(output_file, lines)