simpleserial.h 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. // simpleserial.h
  2. // Generic module for interpreting SimpleSerial commands
  3. #ifndef SIMPLESERIAL_H
  4. #define SIMPLESERIAL_H
  5. #include <stdint.h>
  6. #define SS_VER_1_0 0
  7. #define SS_VER_1_1 1
  8. #define SS_VER_2_0 2
  9. #define SS_VER_2_1 3
  10. // Set up the SimpleSerial module
  11. // This prepares any internal commands
  12. void simpleserial_init(void);
  13. // Add a command to the SimpleSerial module
  14. // Args:
  15. // - c: The character designating this command
  16. // - len: The number of bytes expected
  17. // - fp: A pointer to a callback, which is called after receiving data
  18. // - fl: Bitwise OR'd CMD_FLAG_* values. Defaults to CMD_FLAG_NONE when
  19. // calling simpleserial_addcmd()
  20. // Example: simpleserial_addcmd('p', 16, encrypt_text)
  21. // - Calls encrypt_text() with a 16 byte array after receiving a line
  22. // like p00112233445566778899AABBCCDDEEFF\n
  23. // Notes:
  24. // - Maximum of 10 active commands
  25. // - Maximum length of 64 bytes
  26. // - Returns 1 if either of these fail; otherwise 0
  27. // - The callback function returns a number in [0x00, 0xFF] as a status code;
  28. // in protocol v1.1, this status code is returned through a "z" message
  29. #if SS_VER == SS_VER_2_1
  30. int simpleserial_addcmd(char c, unsigned int len, uint8_t (*fp)(uint8_t, uint8_t, uint8_t, uint8_t*));
  31. #else
  32. #define CMD_FLAG_NONE 0x00
  33. // If this flag is set, the command supports variable length payload.
  34. // The first byte (hex-encoded) indicates the length.
  35. #define CMD_FLAG_LEN 0x01
  36. int simpleserial_addcmd_flags(char c, unsigned int len, uint8_t (*fp)(uint8_t*, uint8_t), uint8_t);
  37. int simpleserial_addcmd(char c, unsigned int len, uint8_t (*fp)(uint8_t*, uint8_t));
  38. #endif
  39. // Attempt to process a command
  40. // If a full string is found, the relevant callback function is called
  41. // Might return without calling a callback for several reasons:
  42. // - First character didn't match any known commands
  43. // - One of the characters wasn't in [0-9|A-F|a-f]
  44. // - Data was too short or too long
  45. void simpleserial_get(void);
  46. // Write some data to the serial port
  47. // Prepends the character c to the start of the line
  48. // Example: simpleserial_put('r', 16, ciphertext)
  49. void simpleserial_put(char c, uint8_t size, uint8_t* output);
  50. typedef enum ss_err_cmd {
  51. SS_ERR_OK,
  52. SS_ERR_CMD,
  53. SS_ERR_CRC,
  54. SS_ERR_TIMEOUT,
  55. SS_ERR_LEN,
  56. SS_ERR_FRAME_BYTE
  57. } ss_err_cmd;
  58. #endif // SIMPLESERIAL_H