1
0

inline_generator.py 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291
  1. """This script generates the XAP protocol generated header to be compiled into QMK.
  2. """
  3. from qmk.casing import to_snake
  4. from qmk.commands import dump_lines
  5. from qmk.constants import GPL2_HEADER_C_LIKE, GENERATED_HEADER_C_LIKE
  6. from qmk.xap.common import merge_xap_defs, route_conditions
  7. def _get_c_type(xap_type):
  8. if xap_type == 'bool':
  9. return 'bool'
  10. elif xap_type == 'u8':
  11. return 'uint8_t'
  12. elif xap_type == 'u16':
  13. return 'uint16_t'
  14. elif xap_type == 'u32':
  15. return 'uint32_t'
  16. elif xap_type == 'u64':
  17. return 'uint64_t'
  18. elif xap_type == 'struct':
  19. return 'struct'
  20. elif xap_type == 'string':
  21. return 'const char *'
  22. return 'unknown'
  23. def _get_c_size(xap_type):
  24. if xap_type == 'u8':
  25. return 'sizeof(uint8_t)'
  26. elif xap_type == 'u16':
  27. return 'sizeof(uint16_t)'
  28. elif xap_type == 'u32':
  29. return 'sizeof(uint32_t)'
  30. elif xap_type == 'u64':
  31. return 8
  32. elif xap_type == 'u8[32]':
  33. return 32
  34. return 0
  35. def _get_route_type(container):
  36. if 'routes' in container:
  37. return 'XAP_ROUTE'
  38. elif 'return_execute' in container:
  39. return 'XAP_EXECUTE'
  40. elif 'return_value' in container:
  41. if container['return_type'] == 'u8':
  42. return 'XAP_VALUE'
  43. elif 'return_constant' in container:
  44. if container['return_type'] == 'u8':
  45. return 'XAP_CONST_MEM'
  46. elif container['return_type'] == 'u16':
  47. return 'XAP_CONST_MEM'
  48. elif container['return_type'] == 'u32':
  49. return 'XAP_CONST_MEM'
  50. elif container['return_type'] == 'u64':
  51. return 'XAP_CONST_MEM'
  52. elif container['return_type'] == 'struct':
  53. return 'XAP_CONST_MEM'
  54. elif container['return_type'] == 'string':
  55. return 'XAP_CONST_MEM'
  56. elif 'return_getter' in container:
  57. if container['return_type'] == 'u32':
  58. return 'XAP_GETTER'
  59. return 'UNSUPPORTED'
  60. def _append_routing_table_declaration(lines, container, container_id, route_stack):
  61. route_stack.append(container)
  62. route_name = to_snake('_'.join([r['define'] for r in route_stack]))
  63. if 'routes' in container:
  64. pass
  65. elif 'return_execute' in container:
  66. execute = container['return_execute']
  67. lines.append(f'bool xap_respond_{execute}(xap_token_t token, const uint8_t *data, size_t data_len);')
  68. # elif 'return_value' in container:
  69. # value = container['return_value']
  70. # return_type = container['return_type']
  71. # lines.append('')
  72. # lines.append(f'{_get_c_type(return_type)} {value} = 0;')
  73. elif 'return_constant' in container:
  74. if container['return_type'] == 'u8':
  75. constant = container['return_constant']
  76. lines.append('')
  77. lines.append(f'static const uint8_t {route_name}_data PROGMEM = {constant};')
  78. elif container['return_type'] == 'u16':
  79. constant = container['return_constant']
  80. lines.append('')
  81. lines.append(f'static const uint16_t {route_name}_data PROGMEM = {constant};')
  82. elif container['return_type'] == 'u32':
  83. constant = container['return_constant']
  84. lines.append('')
  85. lines.append(f'static const uint32_t {route_name}_data PROGMEM = {constant};')
  86. elif container['return_type'] == 'u64':
  87. constant = container['return_constant']
  88. lines.append('')
  89. lines.append(f'static const uint64_t {route_name}_data PROGMEM = {constant};')
  90. elif container['return_type'] == 'struct':
  91. lines.append('')
  92. lines.append(f'static const {route_name}_t {route_name}_data PROGMEM = {{')
  93. for constant in container['return_constant']:
  94. lines.append(f' {constant},')
  95. lines.append('};')
  96. elif container['return_type'] == 'string':
  97. constant = container['return_constant']
  98. lines.append('')
  99. lines.append(f'static const char {route_name}_str[] PROGMEM = {constant};')
  100. elif 'return_getter' in container:
  101. if container['return_type'] == 'u32':
  102. lines.append('')
  103. lines.append(f'extern uint32_t {route_name}_getter(void);')
  104. elif container['return_type'] == 'struct':
  105. pass
  106. route_stack.pop()
  107. def _append_routing_table_entry_flags(lines, container, container_id, route_stack):
  108. pem_map = {
  109. None: 'ROUTE_PERMISSIONS_INSECURE',
  110. 'secure': 'ROUTE_PERMISSIONS_SECURE',
  111. }
  112. is_secure = pem_map[container.get('permissions', None)]
  113. lines.append(' .flags = {')
  114. lines.append(f' .type = {_get_route_type(container)},')
  115. lines.append(f' .secure = {is_secure},')
  116. lines.append(' },')
  117. def _append_routing_table_entry_route(lines, container, container_id, route_stack):
  118. route_name = to_snake('_'.join([r['define'] for r in route_stack]))
  119. lines.append(f' .child_routes = {route_name}_table,')
  120. lines.append(f' .child_routes_len = sizeof({route_name}_table)/sizeof(xap_route_t),')
  121. def _append_routing_table_entry_execute(lines, container, container_id, route_stack):
  122. value = container['return_execute']
  123. lines.append(f' .handler = xap_respond_{value},')
  124. def _append_routing_table_entry_value(lines, container, container_id, route_stack):
  125. value = container['return_value']
  126. lines.append(f' .const_data = &{value},')
  127. lines.append(f' .const_data_len = sizeof({value}),')
  128. def _append_routing_table_entry_u32getter(lines, container, container_id, route_stack):
  129. route_name = to_snake('_'.join([r['define'] for r in route_stack]))
  130. lines.append(f' .u32getter = &{route_name}_getter,')
  131. def _append_routing_table_entry_const_data(lines, container, container_id, route_stack):
  132. route_name = to_snake('_'.join([r['define'] for r in route_stack]))
  133. lines.append(f' .const_data = &{route_name}_data,')
  134. lines.append(f' .const_data_len = sizeof({route_name}_data),')
  135. def _append_routing_table_entry_string(lines, container, container_id, route_stack):
  136. route_name = to_snake('_'.join([r['define'] for r in route_stack]))
  137. lines.append(f' .const_data = {route_name}_str,')
  138. lines.append(f' .const_data_len = sizeof({route_name}_str) - 1,')
  139. def _append_routing_table_entry(lines, container, container_id, route_stack):
  140. route_stack.append(container)
  141. route_name = '_'.join([r['define'] for r in route_stack])
  142. condition = route_conditions(route_stack)
  143. if condition:
  144. lines.append(f'#if {condition}')
  145. lines.append(f' [{route_name}] = {{')
  146. _append_routing_table_entry_flags(lines, container, container_id, route_stack)
  147. if 'routes' in container:
  148. _append_routing_table_entry_route(lines, container, container_id, route_stack)
  149. elif 'return_execute' in container:
  150. _append_routing_table_entry_execute(lines, container, container_id, route_stack)
  151. elif 'return_value' in container:
  152. _append_routing_table_entry_value(lines, container, container_id, route_stack)
  153. elif 'return_constant' in container:
  154. if container['return_type'] == 'u8':
  155. _append_routing_table_entry_const_data(lines, container, container_id, route_stack)
  156. elif container['return_type'] == 'u16':
  157. _append_routing_table_entry_const_data(lines, container, container_id, route_stack)
  158. elif container['return_type'] == 'u32':
  159. _append_routing_table_entry_const_data(lines, container, container_id, route_stack)
  160. elif container['return_type'] == 'u64':
  161. _append_routing_table_entry_const_data(lines, container, container_id, route_stack)
  162. elif container['return_type'] == 'struct':
  163. _append_routing_table_entry_const_data(lines, container, container_id, route_stack)
  164. elif container['return_type'] == 'string':
  165. _append_routing_table_entry_string(lines, container, container_id, route_stack)
  166. elif 'return_getter' in container:
  167. if container['return_type'] == 'u32':
  168. _append_routing_table_entry_u32getter(lines, container, container_id, route_stack)
  169. lines.append(' },')
  170. if condition:
  171. lines.append(f'#endif // {condition}')
  172. route_stack.pop()
  173. def _append_routing_tables(lines, container, container_id=None, route_stack=None):
  174. """Handles building the list of the XAP routes, combining parent and child names together, as well as the route number.
  175. """
  176. if route_stack is None:
  177. route_stack = [container]
  178. else:
  179. route_stack.append(container)
  180. route_name = to_snake('_'.join([r['define'] for r in route_stack]))
  181. condition = route_conditions(route_stack)
  182. if 'routes' in container:
  183. for route_id in container['routes']:
  184. route = container['routes'][route_id]
  185. _append_routing_tables(lines, route, route_id, route_stack)
  186. for route_id in container['routes']:
  187. route = container['routes'][route_id]
  188. _append_routing_table_declaration(lines, route, route_id, route_stack)
  189. lines.append('')
  190. if condition:
  191. lines.append(f'#if {condition}')
  192. lines.append(f'static const xap_route_t {route_name}_table[] PROGMEM = {{')
  193. for route_id in container['routes']:
  194. route = container['routes'][route_id]
  195. _append_routing_table_entry(lines, route, route_id, route_stack)
  196. lines.append('};')
  197. if condition:
  198. lines.append(f'#endif // {condition}')
  199. lines.append('')
  200. route_stack.pop()
  201. def _append_broadcast_messages(lines, container):
  202. """TODO:
  203. """
  204. broadcast_messages = container.get('broadcast_messages', {})
  205. broadcast_prefix = broadcast_messages['define_prefix']
  206. for key, value in broadcast_messages['messages'].items():
  207. define = value.get('define')
  208. name = to_snake(f'{broadcast_prefix}_{define}')
  209. if 'return_type' in value:
  210. ret_type = _get_c_type(value['return_type'])
  211. lines.append(f'void {name}({ret_type} value) {{ xap_broadcast({key}, &value, sizeof(value)); }}')
  212. else:
  213. lines.append(f'void {name}(const void *data, size_t length){{ xap_broadcast({key}, data, length); }}')
  214. def generate_inline(output_file, keyboard, keymap):
  215. """Generates the XAP protocol header file, generated during normal build.
  216. """
  217. xap_defs = merge_xap_defs(keyboard, keymap)
  218. # Preamble
  219. lines = [GPL2_HEADER_C_LIKE, GENERATED_HEADER_C_LIKE, '']
  220. # Add all the generated code
  221. _append_broadcast_messages(lines, xap_defs)
  222. _append_routing_tables(lines, xap_defs)
  223. dump_lines(output_file, lines)