inline_generator.py 7.7 KB

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