message_queue.h 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. #pragma once
  2. #include <furi.h>
  3. #include "protocol.h"
  4. #include "constants.h"
  5. /**
  6. * Struct to contain and operate on given data as per the protocol specification
  7. */
  8. typedef struct {
  9. /// Pointer to the beginning of the queue of bytes
  10. uint8_t* messageQueue;
  11. /// Contains the size of messageQueue
  12. size_t queueSize;
  13. /// Location of where we are currently writing
  14. uint8_t* writePtr;
  15. /// Location of where we are currently reading from
  16. uint8_t* readPtr;
  17. } MessageQueue;
  18. /**
  19. * Allocates memory to store the queue
  20. *
  21. * @return Pointer to the newly created queue
  22. */
  23. MessageQueue* message_queue_alloc();
  24. /**
  25. * Destructs all memory that is stored at the pointer and sets pointer to null
  26. *
  27. * @param queue Queue to operate on
  28. */
  29. void message_queue_free(MessageQueue* queue);
  30. /**
  31. * Decides if the queue has a full message available
  32. *
  33. * @param queue Queue to check
  34. * @return If a message is available
  35. */
  36. bool message_queue_has_message(MessageQueue* queue);
  37. /**
  38. * Add a byte to the message queue
  39. *
  40. * @param queue Queue to operate on
  41. * @param data Byte to add to the queue
  42. */
  43. void message_queue_push_byte(MessageQueue* queue, uint8_t data);
  44. /**
  45. * Wipes the entire message queue
  46. *
  47. * @param queue Queue to wipe
  48. */
  49. void message_queue_wipe(MessageQueue* queue);
  50. /**
  51. * Loops through queue and ensures that all present bytes are valid according to the protocol,
  52. * if they are not it will wipe the queue
  53. *
  54. * @param queue Queue to validate
  55. * @return If it was validated successfully. If it was wiped it will return false
  56. */
  57. bool message_queue_validate(MessageQueue* queue);
  58. /**
  59. * Pops a command off of the queue and saves into dest
  60. *
  61. * @note This will call validate_wipe and clear the queue if it detects bad instructions
  62. *
  63. * @param queue Queue to pop from
  64. * @param dest Where to save the command to
  65. * @return If there was a command to pop and a valid command entered the dest
  66. */
  67. bool message_queue_pop_message(MessageQueue* queue, PwnCommand* dest);