inline_generator.py 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283
  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 latest_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'] == 'u32':
  45. return 'XAP_CONST_MEM'
  46. elif container['return_type'] == 'struct':
  47. return 'XAP_CONST_MEM'
  48. elif container['return_type'] == 'string':
  49. return 'XAP_CONST_MEM'
  50. elif 'return_getter' in container:
  51. if container['return_type'] == 'u32':
  52. return 'XAP_GETTER'
  53. return 'UNSUPPORTED'
  54. def _append_routing_table_declaration(lines, container, container_id, route_stack):
  55. route_stack.append(container)
  56. route_name = to_snake('_'.join([r['define'] for r in route_stack]))
  57. if 'routes' in container:
  58. pass
  59. elif 'return_execute' in container:
  60. execute = container['return_execute']
  61. request_type = container.get('request_type', None)
  62. return_type = container['return_type']
  63. lines.append(
  64. f'''
  65. bool xap_respond_{execute}(xap_token_t token, const uint8_t *data, size_t data_len) {{
  66. if (data_len != {_get_c_size(request_type)}) {{
  67. xap_respond_failure(token, 0);
  68. return false;
  69. }}
  70. uint8_t ret[{_get_c_size(return_type)}] = {{0}};
  71. '''
  72. )
  73. if not request_type:
  74. lines.append(f'''
  75. bool {execute}(uint8_t *ret, uint8_t ret_len);
  76. if(!{execute}(ret, sizeof(ret))) {{
  77. xap_respond_failure(token, 0);
  78. return false;
  79. }}
  80. ''')
  81. else:
  82. lines.append(
  83. f'''
  84. {_get_c_type(request_type)} *argp = ({_get_c_type(request_type)} *)&data[0];
  85. bool {execute}({_get_c_type(request_type)} arg, uint8_t *ret, uint8_t ret_len);
  86. if(!{execute}(*argp, ret, sizeof(ret))) {{
  87. xap_respond_failure(token, 0);
  88. return false;
  89. }}
  90. '''
  91. )
  92. lines.append('''
  93. return xap_respond_data(token, ret, sizeof(ret));
  94. }''')
  95. # elif 'return_value' in container:
  96. # value = container['return_value']
  97. # return_type = container['return_type']
  98. # lines.append('')
  99. # lines.append(f'{_get_c_type(return_type)} {value} = 0;')
  100. elif 'return_constant' in container:
  101. if container['return_type'] == 'u32':
  102. constant = container['return_constant']
  103. lines.append('')
  104. lines.append(f'static const uint32_t {route_name}_data PROGMEM = {constant};')
  105. elif container['return_type'] == 'struct':
  106. lines.append('')
  107. lines.append(f'static const struct {route_name}_t {{')
  108. for member in container['return_struct_members']:
  109. member_type = _get_c_type(member['type'])
  110. member_name = to_snake(member['name'])
  111. lines.append(f' const {member_type} {member_name};')
  112. lines.append(f'}} {route_name}_data PROGMEM = {{')
  113. for constant in container['return_constant']:
  114. lines.append(f' {constant},')
  115. lines.append('};')
  116. elif container['return_type'] == 'string':
  117. constant = container['return_constant']
  118. lines.append('')
  119. lines.append(f'static const char {route_name}_str[] PROGMEM = {constant};')
  120. elif 'return_getter' in container:
  121. if container['return_type'] == 'u32':
  122. lines.append('')
  123. lines.append(f'extern uint32_t {route_name}_getter(void);')
  124. elif container['return_type'] == 'struct':
  125. pass
  126. route_stack.pop()
  127. def _append_routing_table_entry_flags(lines, container, container_id, route_stack):
  128. is_secure = 1 if ('secure' in container and container['secure'] is True) else 0
  129. lines.append(' .flags = {')
  130. lines.append(f' .type = {_get_route_type(container)},')
  131. lines.append(f' .is_secure = {is_secure},')
  132. lines.append(' },')
  133. def _append_routing_table_entry_route(lines, container, container_id, route_stack):
  134. route_name = to_snake('_'.join([r['define'] for r in route_stack]))
  135. lines.append(f' .child_routes = {route_name}_table,')
  136. lines.append(f' .child_routes_len = sizeof({route_name}_table)/sizeof(xap_route_t),')
  137. def _append_routing_table_entry_execute(lines, container, container_id, route_stack):
  138. value = container['return_execute']
  139. lines.append(f' .handler = xap_respond_{value},')
  140. def _append_routing_table_entry_value(lines, container, container_id, route_stack):
  141. value = container['return_value']
  142. lines.append(f' .const_data = &{value},')
  143. lines.append(f' .const_data_len = sizeof({value}),')
  144. def _append_routing_table_entry_u32getter(lines, container, container_id, route_stack):
  145. route_name = to_snake('_'.join([r['define'] for r in route_stack]))
  146. lines.append(f' .u32getter = &{route_name}_getter,')
  147. def _append_routing_table_entry_const_data(lines, container, container_id, route_stack):
  148. route_name = to_snake('_'.join([r['define'] for r in route_stack]))
  149. lines.append(f' .const_data = &{route_name}_data,')
  150. lines.append(f' .const_data_len = sizeof({route_name}_data),')
  151. def _append_routing_table_entry_string(lines, container, container_id, route_stack):
  152. route_name = to_snake('_'.join([r['define'] for r in route_stack]))
  153. lines.append(f' .const_data = {route_name}_str,')
  154. lines.append(f' .const_data_len = sizeof({route_name}_str) - 1,')
  155. def _append_routing_table_entry(lines, container, container_id, route_stack):
  156. route_stack.append(container)
  157. route_name = '_'.join([r['define'] for r in route_stack])
  158. condition = route_conditions(route_stack)
  159. if condition:
  160. lines.append(f'#if {condition}')
  161. lines.append(f' [{route_name}] = {{')
  162. _append_routing_table_entry_flags(lines, container, container_id, route_stack)
  163. if 'routes' in container:
  164. _append_routing_table_entry_route(lines, container, container_id, route_stack)
  165. elif 'return_execute' in container:
  166. _append_routing_table_entry_execute(lines, container, container_id, route_stack)
  167. elif 'return_value' in container:
  168. _append_routing_table_entry_value(lines, container, container_id, route_stack)
  169. elif 'return_constant' in container:
  170. if container['return_type'] == 'u32':
  171. _append_routing_table_entry_const_data(lines, container, container_id, route_stack)
  172. elif container['return_type'] == 'struct':
  173. _append_routing_table_entry_const_data(lines, container, container_id, route_stack)
  174. elif container['return_type'] == 'string':
  175. _append_routing_table_entry_string(lines, container, container_id, route_stack)
  176. elif 'return_getter' in container:
  177. if container['return_type'] == 'u32':
  178. _append_routing_table_entry_u32getter(lines, container, container_id, route_stack)
  179. lines.append(' },')
  180. if condition:
  181. lines.append(f'#endif // {condition}')
  182. route_stack.pop()
  183. def _append_routing_tables(lines, container, container_id=None, route_stack=None):
  184. """Handles building the list of the XAP routes, combining parent and child names together, as well as the route number.
  185. """
  186. if route_stack is None:
  187. route_stack = [container]
  188. else:
  189. route_stack.append(container)
  190. route_name = to_snake('_'.join([r['define'] for r in route_stack]))
  191. condition = route_conditions(route_stack)
  192. if 'routes' in container:
  193. for route_id in container['routes']:
  194. route = container['routes'][route_id]
  195. _append_routing_tables(lines, route, route_id, route_stack)
  196. for route_id in container['routes']:
  197. route = container['routes'][route_id]
  198. _append_routing_table_declaration(lines, route, route_id, route_stack)
  199. lines.append('')
  200. if condition:
  201. lines.append(f'#if {condition}')
  202. lines.append(f'static const xap_route_t {route_name}_table[] PROGMEM = {{')
  203. for route_id in container['routes']:
  204. route = container['routes'][route_id]
  205. _append_routing_table_entry(lines, route, route_id, route_stack)
  206. lines.append('};')
  207. if condition:
  208. lines.append(f'#endif // {condition}')
  209. lines.append('')
  210. route_stack.pop()
  211. def generate_inline(output_file):
  212. """Generates the XAP protocol header file, generated during normal build.
  213. """
  214. xap_defs = latest_xap_defs()
  215. # Preamble
  216. lines = [GPL2_HEADER_C_LIKE, GENERATED_HEADER_C_LIKE, '']
  217. # Add all the generated code
  218. _append_routing_tables(lines, xap_defs)
  219. dump_lines(output_file, lines)