inline_generator.py 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234
  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.constants import GPL2_HEADER_C_LIKE, GENERATED_HEADER_C_LIKE
  5. from qmk.xap.common import latest_xap_defs, route_conditions
  6. def _get_c_type(xap_type):
  7. if xap_type == 'bool':
  8. return 'bool'
  9. elif xap_type == 'u8':
  10. return 'uint8_t'
  11. elif xap_type == 'u16':
  12. return 'uint16_t'
  13. elif xap_type == 'u32':
  14. return 'uint32_t'
  15. elif xap_type == 'u64':
  16. return 'uint64_t'
  17. elif xap_type == 'struct':
  18. return 'struct'
  19. elif xap_type == 'string':
  20. return 'const char *'
  21. return 'unknown'
  22. def _get_route_type(container):
  23. if 'routes' in container:
  24. return 'XAP_ROUTE'
  25. elif 'return_execute' in container:
  26. return 'XAP_EXECUTE'
  27. elif 'return_constant' in container:
  28. if container['return_type'] == 'u32':
  29. return 'XAP_VALUE'
  30. elif container['return_type'] == 'struct':
  31. return 'XAP_CONST_MEM'
  32. elif container['return_type'] == 'string':
  33. return 'XAP_CONST_MEM'
  34. elif 'return_getter' in container:
  35. if container['return_type'] == 'u32':
  36. return 'XAP_GETTER'
  37. return 'UNSUPPORTED'
  38. def _append_routing_table_declaration(lines, container, container_id, route_stack):
  39. route_stack.append(container)
  40. route_name = to_snake('_'.join([r['define'] for r in route_stack]))
  41. if 'routes' in container:
  42. pass
  43. elif 'return_execute' in container:
  44. execute = container['return_execute']
  45. lines.append('')
  46. lines.append(f'bool xap_respond_{execute}(xap_token_t token, const uint8_t *data, size_t data_len);')
  47. elif 'return_constant' in container:
  48. if container['return_type'] == 'u32':
  49. pass
  50. elif container['return_type'] == 'struct':
  51. lines.append('')
  52. lines.append(f'static const struct {route_name}_t {{')
  53. for member in container['return_struct_members']:
  54. member_type = _get_c_type(member['type'])
  55. member_name = to_snake(member['name'])
  56. lines.append(f' const {member_type} {member_name};')
  57. lines.append(f'}} {route_name}_data PROGMEM = {{')
  58. for constant in container['return_constant']:
  59. lines.append(f' {constant},')
  60. lines.append('};')
  61. elif container['return_type'] == 'string':
  62. constant = container['return_constant']
  63. lines.append('')
  64. lines.append(f'static const char {route_name}_str[] PROGMEM = {constant};')
  65. elif 'return_getter' in container:
  66. if container['return_type'] == 'u32':
  67. lines.append('')
  68. lines.append(f'extern uint32_t {route_name}_getter(void);')
  69. elif container['return_type'] == 'struct':
  70. pass
  71. route_stack.pop()
  72. def _append_routing_table_entry_flags(lines, container, container_id, route_stack):
  73. is_secure = 1 if ('secure' in container and container['secure'] is True) else 0
  74. lines.append(' .flags = {')
  75. lines.append(f' .type = {_get_route_type(container)},')
  76. lines.append(f' .is_secure = {is_secure},')
  77. lines.append(' },')
  78. def _append_routing_table_entry_route(lines, container, container_id, route_stack):
  79. route_name = to_snake('_'.join([r['define'] for r in route_stack]))
  80. lines.append(f' .child_routes = {route_name}_table,')
  81. lines.append(f' .child_routes_len = sizeof({route_name}_table)/sizeof(xap_route_t),')
  82. def _append_routing_table_entry_execute(lines, container, container_id, route_stack):
  83. value = container['return_execute']
  84. lines.append(f' .handler = xap_respond_{value},')
  85. def _append_routing_table_entry_u32value(lines, container, container_id, route_stack):
  86. value = container['return_constant']
  87. lines.append(f' .u32value = {value},')
  88. def _append_routing_table_entry_u32getter(lines, container, container_id, route_stack):
  89. route_name = to_snake('_'.join([r['define'] for r in route_stack]))
  90. lines.append(f' .u32getter = &{route_name}_getter,')
  91. def _append_routing_table_entry_const_data(lines, container, container_id, route_stack):
  92. route_name = to_snake('_'.join([r['define'] for r in route_stack]))
  93. lines.append(f' .const_data = &{route_name}_data,')
  94. lines.append(f' .const_data_len = sizeof({route_name}_data),')
  95. def _append_routing_table_entry_string(lines, container, container_id, route_stack):
  96. route_name = to_snake('_'.join([r['define'] for r in route_stack]))
  97. lines.append(f' .const_data = {route_name}_str,')
  98. lines.append(f' .const_data_len = sizeof({route_name}_str) - 1,')
  99. def _append_routing_table_entry(lines, container, container_id, route_stack):
  100. route_stack.append(container)
  101. route_name = '_'.join([r['define'] for r in route_stack])
  102. condition = route_conditions(route_stack)
  103. if condition:
  104. lines.append(f'#if {condition}')
  105. lines.append(f' [{route_name}] = {{')
  106. _append_routing_table_entry_flags(lines, container, container_id, route_stack)
  107. if 'routes' in container:
  108. _append_routing_table_entry_route(lines, container, container_id, route_stack)
  109. elif 'return_execute' in container:
  110. _append_routing_table_entry_execute(lines, container, container_id, route_stack)
  111. elif 'return_constant' in container:
  112. if container['return_type'] == 'u32':
  113. _append_routing_table_entry_u32value(lines, container, container_id, route_stack)
  114. elif container['return_type'] == 'struct':
  115. _append_routing_table_entry_const_data(lines, container, container_id, route_stack)
  116. elif container['return_type'] == 'string':
  117. _append_routing_table_entry_string(lines, container, container_id, route_stack)
  118. elif 'return_getter' in container:
  119. if container['return_type'] == 'u32':
  120. _append_routing_table_entry_u32getter(lines, container, container_id, route_stack)
  121. lines.append(' },')
  122. if condition:
  123. lines.append(f'#endif // {condition}')
  124. route_stack.pop()
  125. def _append_routing_tables(lines, container, container_id=None, route_stack=None):
  126. """Handles building the list of the XAP routes, combining parent and child names together, as well as the route number.
  127. """
  128. if route_stack is None:
  129. route_stack = [container]
  130. else:
  131. route_stack.append(container)
  132. route_name = to_snake('_'.join([r['define'] for r in route_stack]))
  133. condition = route_conditions(route_stack)
  134. if 'routes' in container:
  135. for route_id in container['routes']:
  136. route = container['routes'][route_id]
  137. _append_routing_tables(lines, route, route_id, route_stack)
  138. for route_id in container['routes']:
  139. route = container['routes'][route_id]
  140. _append_routing_table_declaration(lines, route, route_id, route_stack)
  141. lines.append('')
  142. if condition:
  143. lines.append(f'#if {condition}')
  144. lines.append(f'static const xap_route_t {route_name}_table[] PROGMEM = {{')
  145. for route_id in container['routes']:
  146. route = container['routes'][route_id]
  147. _append_routing_table_entry(lines, route, route_id, route_stack)
  148. lines.append('};')
  149. if condition:
  150. lines.append(f'#endif // {condition}')
  151. lines.append('')
  152. route_stack.pop()
  153. def generate_inline(output_file):
  154. """Generates the XAP protocol header file, generated during normal build.
  155. """
  156. xap_defs = latest_xap_defs()
  157. # Preamble
  158. lines = [GPL2_HEADER_C_LIKE, GENERATED_HEADER_C_LIKE, '']
  159. # Add all the generated code
  160. _append_routing_tables(lines, xap_defs)
  161. # Generate the full output
  162. xap_generated_inl = '\n'.join(lines)
  163. # Clean up newlines
  164. while "\n\n\n" in xap_generated_inl:
  165. xap_generated_inl = xap_generated_inl.replace("\n\n\n", "\n\n")
  166. if output_file:
  167. if output_file.name == '-':
  168. print(xap_generated_inl)
  169. else:
  170. output_file.parent.mkdir(parents=True, exist_ok=True)
  171. if output_file.exists():
  172. output_file.replace(output_file.parent / (output_file.name + '.bak'))
  173. output_file.write_text(xap_generated_inl)