OSCBundle.h 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  1. /*
  2. Written by Yotam Mann, The Center for New Music and Audio Technologies,
  3. University of California, Berkeley. Copyright (c) 2012, 2013, The Regents of
  4. the University of California (Regents).
  5. Permission to use, copy, modify, distribute, and distribute modified versions
  6. of this software and its documentation without fee and without a signed
  7. licensing agreement, is hereby granted, provided that the above copyright
  8. notice, this paragraph and the following two paragraphs appear in all copies,
  9. modifications, and distributions.
  10. IN NO EVENT SHALL REGENTS BE LIABLE TO ANY PARTY FOR DIRECT, INDIRECT,
  11. SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES, INCLUDING LOST PROFITS, ARISING
  12. OUT OF THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION, EVEN IF REGENTS HAS
  13. BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  14. REGENTS SPECIFICALLY DISCLAIMS ANY WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
  15. THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
  16. PURPOSE. THE SOFTWARE AND ACCOMPANYING DOCUMENTATION, IF ANY, PROVIDED
  17. HEREUNDER IS PROVIDED "AS IS". REGENTS HAS NO OBLIGATION TO PROVIDE
  18. MAINTENANCE, SUPPORT, UPDATES, ENHANCEMENTS, OR MODIFICATIONS.
  19. */
  20. #ifndef OSCBUNDLE_h
  21. #define OSCBUNDLE_h
  22. #include "OSCMessage.h"
  23. extern osctime_t zerotime;
  24. class OSCBundle
  25. {
  26. private:
  27. /*=============================================================================
  28. PRIVATE VARIABLES
  29. =============================================================================*/
  30. //the array of messages contained in the bundle
  31. OSCMessage ** messages;
  32. //the number of messages in the array
  33. int numMessages;
  34. osctime_t timetag;
  35. //error codes
  36. OSCErrorCode error;
  37. /*=============================================================================
  38. DECODING INCOMING BYTES
  39. =============================================================================*/
  40. //the decoding states for incoming bytes
  41. enum DecodeState {
  42. STANDBY,
  43. HEADER,
  44. TIMETAG,
  45. MESSAGE_SIZE,
  46. MESSAGE,
  47. } decodeState;
  48. //stores incoming bytes until they can be decoded
  49. uint8_t * incomingBuffer;
  50. int incomingBufferSize;
  51. //the size of the incoming message
  52. int incomingMessageSize;
  53. //adds a byte to the buffer
  54. void addToIncomingBuffer(uint8_t);
  55. //clears the incoming buffer
  56. void clearIncomingBuffer();
  57. //decoding functions
  58. void decode(uint8_t);
  59. void decodeTimetag();
  60. void decodeHeader();
  61. void decodeMessage(uint8_t);
  62. //just a placeholder while filling
  63. OSCMessage & add();
  64. public:
  65. /*=============================================================================
  66. CONSTRUCTORS / DESTRUCTOR
  67. =============================================================================*/
  68. //default timetag of
  69. OSCBundle(osctime_t = zerotime);
  70. //DESTRUCTOR
  71. ~OSCBundle();
  72. //clears all of the OSCMessages inside
  73. OSCBundle& empty();
  74. /*=============================================================================
  75. SETTERS
  76. =============================================================================*/
  77. //start a new OSC Message in the bundle
  78. OSCMessage & add(const char * address);
  79. //add with nothing in it produces an invalid osc message
  80. //copies an existing message into the bundle
  81. OSCMessage & add(OSCMessage & msg);
  82. template <typename T>
  83. OSCBundle& setTimetag(T t){
  84. timetag = (osctime_t) t;
  85. return *this;
  86. }
  87. //sets the timetag from a buffer
  88. OSCBundle& setTimetag(uint8_t * buff){
  89. memcpy(&timetag, buff, 8);
  90. return *this;
  91. }
  92. /*=============================================================================
  93. GETTERS
  94. =============================================================================*/
  95. //gets the message the matches the address string
  96. //will do regex matching
  97. OSCMessage * getOSCMessage(char * addr);
  98. //get message by position
  99. OSCMessage * getOSCMessage(int position);
  100. /*=============================================================================
  101. MATCHING
  102. =============================================================================*/
  103. //if the bundle contains a message that matches the pattern,
  104. //call the function callback on that message
  105. bool dispatch(const char * pattern, void (*callback)(OSCMessage&), int = 0);
  106. //like dispatch, but allows for partial matches
  107. //the address match offset is sent as an argument to the callback
  108. bool route(const char * pattern, void (*callback)(OSCMessage&, int), int = 0);
  109. /*=============================================================================
  110. SIZE
  111. =============================================================================*/
  112. //returns the number of messages in the bundle;
  113. int size();
  114. /*=============================================================================
  115. ERROR
  116. =============================================================================*/
  117. bool hasError();
  118. OSCErrorCode getError();
  119. /*=============================================================================
  120. SENDING
  121. =============================================================================*/
  122. OSCBundle& send(Print &p);
  123. /*=============================================================================
  124. FILLING
  125. =============================================================================*/
  126. OSCBundle& fill(uint8_t incomingByte);
  127. OSCBundle& fill(const uint8_t * incomingBytes, int length);
  128. };
  129. #endif