You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

78 lines
4.9 KiB

2 years ago
  1. # QMK Firmware XAP Specs
  2. This document describes the requirements of the QMK XAP ("extensible application protocol") API.
  3. ## Types
  4. **All integral types are little-endian.**
  5. | Name | Definition |
  6. | -- | -- |
  7. | _struct{}_ | A structure of data, packing different objects together. Data is "compacted" -- there are no padding bytes between fields. Equivalent to a packed C-style `struct`. The order in which they're defined matches the order of the data in the response packet. |
  8. | _type[n]_ | An array of `type`, with array extent of `N` -- e.g. `u8[2]` signifies two consecutive octets. |
  9. | _u16_ | An unsigned 16-bit integral, commonly seen as `uint16_t` from _stdint.h_. |
  10. | _u32_ | An unsigned 32-bit integral, commonly seen as `uint32_t` from _stdint.h_. |
  11. | _u64_ | An unsigned 64-bit integral, commonly seen as `uint64_t` from _stdint.h_. |
  12. | _u8_ | An unsigned 8-bit integral (octet, or byte), commonly seen as `uint8_t` from _stdint.h_. |
  13. ## Definitions
  14. This list defines the terms used across the entire set of XAP protocol documentation.
  15. | Name | Definition |
  16. | -- | -- |
  17. | _Capability_ | A way to determine if certain functionality is enabled in the firmware. Any _subsystem_ that provides build-time restriction of functionality must provide a _route_ for a _capabilities query_. |
  18. | _Handler_ | A piece of code that is executed when a specific _route_ is received. |
  19. | _ID_ | A single octet / 8-bit byte. |
  20. | _Payload_ | Any received data appended to the _route_, which gets delivered to the _handler_ when received. |
  21. | _Response_ | The data sent back to the host during execution of a _handler_. |
  22. | _Response Flags_ | An `u8` containing the status of the request. |
  23. | _Route_ | A sequence of _IDs_ describing the route to invoke a _handler_. |
  24. | _Secure Route_ | A _route_ which has potentially destructive consequences, necessitating prior approval by the user before executing. |
  25. | _Subsystem_ | A high-level area of functionality within XAP. |
  26. | _Token_ | A `u16` associated with a specific request as well as its corresponding response. |
  27. | _Unlock sequence_ | A physical sequence initiated by the user to enable execution of _secure routes_. |
  28. ## Requests and Responses
  29. Communication generally follows a request/response pattern.
  30. Each request needs to include a _token_ -- this `u16` value prefixes each outbound request from the host application and its corresponding response, allowing repsonse messages to be correlated with their request, even if multiple host applications are communicating with the firmware simultaneously. Host applications should randomly generate a token ID for **every** outbound request, unless using a reserved token defined below.
  31. This token is followed by a `u8` signifying the length of data in the request.
  32. Two token values are reserved: `0x0000` and `0xFFFF`:
  33. * `0x0000`: A message sent by a host application may use this token if no response is to be sent -- a "fire and forget" message.
  34. * `0xFFFF`: Signifies a "broadcast" message sent by the firmware without prompting from the host application. Broadcast messages are defined later in this document.
  35. Any request will generate at least one corresponding response, with the exception of messages using reserved tokens. Maximum total message length is 128 bytes due to RAM constraints.
  36. Response messages will always be prefixed by the originating request _token_, directly followed by that request's _response flags_, then the response payload length:
  37. | Bit 7 | Bit 6 | Bit 5 | Bit 4 | Bit 3 | Bit 2 | Bit 1 | Bit 0 |
  38. |--|--|--|--|--|--|--|--|
  39. | Unlocked | Unlocking | - | - | - | - | Secure Failure | Success |
  40. * `Bit 7`: When this bit is set, an _unlock sequence_ has completed, and _secure routes_ may be invoked.
  41. * `Bit 6`: When this bit is set, an _unlock sequence_ is in progress.
  42. * `Bit 1`: When this bit is set, the requested _route_ was marked _secure_ but an _unlock sequence_ has not completed.
  43. * `Bit 0`: When this bit is set, the request was successfully handled. If not set, all payload data should be disregarded, and the request retried if appropriate (with a new token).
  44. ### Example "conversation":
  45. **Request** -- version query:
  46. | Byte | 0 | 1 | 2 | 3 | 4 |
  47. | --- | --- | --- | --- | --- | --- |
  48. | **Purpose** | Token | Token | Payload Length | Route | Route |
  49. | **Value** | `0x43` | `0x2B` | `0x02` | `0x00` | `0x00` |
  50. **Response** -- matching token, successful flag, payload of `0x03170192` = 3.17.192:
  51. | Byte | 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 |
  52. | --- | --- | --- | --- | --- | --- | --- | --- | --- |
  53. | **Purpose** | Token | Token | Response Flags | Payload Length | Payload | Payload | Payload | Payload |
  54. | **Value** | `0x43` | `0x2B` | `0x01` | `0x04` | `0x92` | `0x01` | `0x17` | `0x03` |
  55. ## Broadcast messages
  56. Broadcast messages may be sent by the firmware to the host, without a corresponding inbound request. Each broadcast message uses the token `0xFFFF`, and does not expect a response from the host. Tokens are followed by an _ID_ signifying the type of broadcast, with corresponding _payload_.