Fork of, triyin to rewrite for mbed 5. WIP not yet finished but working. ----------------------- An Open Sound Control library for the mbed, created to be compatible with Recotana's OSCClass library (http://recotana.com) for the Arduino with Ethernet shield. It also uses parts of the OSC Transceiver(Sender/Receiver) code by xshige written by: Alvaro Cassinelli, October 2011 tweaked by: Toby Harris / *spark audio-visual, March 2012

Committer:
Ibiltari
Date:
Thu Nov 04 12:41:38 2021 +0100
Revision:
19:35408d190f4e
Parent:
17:9479c15a9d54
Update to mbed 6

Who changed what in which revision?

UserRevisionLine numberNew contents of line
Ibiltari 7:98280cef1c4f 1 /* mbed OSC Library
Ibiltari 7:98280cef1c4f 2 This is an Open Sound Control library for the mbed, created to be compatible with Recotana's OSCClass library (http://recotana.com) for the
Ibiltari 7:98280cef1c4f 3 Arduino with Ethernet shield. It also uses parts of the OSC Transceiver(Sender/Receiver) code by xshige
Ibiltari 7:98280cef1c4f 4 written by: Alvaro Cassinelli, October 2011
Ibiltari 7:98280cef1c4f 5 tweaked by: Toby Harris / *spark audio-visual, March 2012
Ibiltari 7:98280cef1c4f 6
Ibiltari 7:98280cef1c4f 7 This library is free software; you can redistribute it and/or
Ibiltari 7:98280cef1c4f 8 modify it under the terms of the GNU Lesser General Public
Ibiltari 7:98280cef1c4f 9 License version 2.1 as published by the Free Software Foundation.
Ibiltari 7:98280cef1c4f 10 Open Sound Control http://opensoundcontrol.org/
Ibiltari 7:98280cef1c4f 11 */
Ibiltari 7:98280cef1c4f 12
Ibiltari 7:98280cef1c4f 13 #ifndef mbedOSC_h
Ibiltari 7:98280cef1c4f 14 #define mbedOSC_h
Ibiltari 7:98280cef1c4f 15
Ibiltari 7:98280cef1c4f 16 #include "mbed.h"
Ibiltari 7:98280cef1c4f 17 #include "EthernetInterface.h"
Ibiltari 7:98280cef1c4f 18 #include "UDPSocket.h"
Ibiltari 7:98280cef1c4f 19 #include "OSCData.h"
Ibiltari 7:98280cef1c4f 20
Ibiltari 7:98280cef1c4f 21 class OSCMessage
Ibiltari 7:98280cef1c4f 22 {
Ibiltari 7:98280cef1c4f 23
Ibiltari 7:98280cef1c4f 24 private:
Ibiltari 7:98280cef1c4f 25
Ibiltari 7:98280cef1c4f 26 //friends
Ibiltari 7:98280cef1c4f 27 friend class OSCBundle;
Ibiltari 7:98280cef1c4f 28
Ibiltari 7:98280cef1c4f 29
Ibiltari 7:98280cef1c4f 30 /*=============================================================================
Ibiltari 7:98280cef1c4f 31 PRIVATE VARIABLES
Ibiltari 7:98280cef1c4f 32 =============================================================================*/
Ibiltari 7:98280cef1c4f 33
Ibiltari 7:98280cef1c4f 34 //the address
Ibiltari 7:98280cef1c4f 35 char * address;
Ibiltari 7:98280cef1c4f 36
Ibiltari 7:98280cef1c4f 37 //the data
Ibiltari 7:98280cef1c4f 38 OSCData ** data;
Ibiltari 7:98280cef1c4f 39
Ibiltari 7:98280cef1c4f 40 //the number of OSCData in the data array
Ibiltari 7:98280cef1c4f 41 int dataCount;
Ibiltari 7:98280cef1c4f 42
Ibiltari 7:98280cef1c4f 43 //error codes for potential runtime problems
Ibiltari 7:98280cef1c4f 44 OSCErrorCode error;
Ibiltari 7:98280cef1c4f 45
Ibiltari 7:98280cef1c4f 46 /*=============================================================================
Ibiltari 7:98280cef1c4f 47 DECODING INCOMING BYTES
Ibiltari 7:98280cef1c4f 48 =============================================================================*/
Ibiltari 7:98280cef1c4f 49
Ibiltari 7:98280cef1c4f 50 //the decoding states for incoming bytes
Ibiltari 7:98280cef1c4f 51 enum DecodeState {
Ibiltari 7:98280cef1c4f 52 STANDBY,
Ibiltari 7:98280cef1c4f 53 ADDRESS,
Ibiltari 7:98280cef1c4f 54 ADDRESS_PADDING,
Ibiltari 7:98280cef1c4f 55 TYPES,
Ibiltari 7:98280cef1c4f 56 TYPES_PADDING,
Ibiltari 7:98280cef1c4f 57 DATA,
Ibiltari 7:98280cef1c4f 58 DATA_PADDING,
Ibiltari 7:98280cef1c4f 59 DONE,
Ibiltari 7:98280cef1c4f 60 } decodeState;
Ibiltari 7:98280cef1c4f 61
Ibiltari 7:98280cef1c4f 62 //stores incoming bytes until they can be decoded
Ibiltari 7:98280cef1c4f 63 uint8_t * incomingBuffer;
Ibiltari 7:98280cef1c4f 64 int incomingBufferSize; // how many bytes are stored
Ibiltari 7:98280cef1c4f 65 int incomingBufferFree; // how many bytes are allocated but unused
Ibiltari 7:98280cef1c4f 66
Ibiltari 7:98280cef1c4f 67 //adds a byte to the buffer
Ibiltari 7:98280cef1c4f 68 void addToIncomingBuffer(uint8_t);
Ibiltari 7:98280cef1c4f 69 //clears the incoming buffer
Ibiltari 7:98280cef1c4f 70 void clearIncomingBuffer();
Ibiltari 7:98280cef1c4f 71
Ibiltari 7:98280cef1c4f 72 //decoding function
Ibiltari 7:98280cef1c4f 73 void decode(uint8_t);
Ibiltari 7:98280cef1c4f 74 void decodeAddress();
Ibiltari 7:98280cef1c4f 75 void decodeType(uint8_t);
Ibiltari 7:98280cef1c4f 76 void decodeData(uint8_t);
Ibiltari 7:98280cef1c4f 77
Ibiltari 7:98280cef1c4f 78 /*=============================================================================
Ibiltari 7:98280cef1c4f 79 HELPER FUNCTIONS
Ibiltari 7:98280cef1c4f 80 =============================================================================*/
Ibiltari 7:98280cef1c4f 81
Ibiltari 7:98280cef1c4f 82 void setupMessage();
Ibiltari 7:98280cef1c4f 83
Ibiltari 7:98280cef1c4f 84 //compares the OSCData's type char to a test char
Ibiltari 7:98280cef1c4f 85 bool testType(int position, char type);
Ibiltari 7:98280cef1c4f 86
Ibiltari 7:98280cef1c4f 87 //returns the number of bytes to pad to make it 4-bit aligned
Ibiltari 7:98280cef1c4f 88 int padSize(int bytes);
Ibiltari 7:98280cef1c4f 89
Ibiltari 7:98280cef1c4f 90 public:
Ibiltari 7:98280cef1c4f 91
Ibiltari 7:98280cef1c4f 92 //returns the OSCData at that position
Ibiltari 7:98280cef1c4f 93 OSCData * getOSCData(int);
Ibiltari 7:98280cef1c4f 94
Ibiltari 7:98280cef1c4f 95 /*=============================================================================
Ibiltari 7:98280cef1c4f 96 CONSTRUCTORS / DESTRUCTOR
Ibiltari 7:98280cef1c4f 97 =============================================================================*/
Ibiltari 7:98280cef1c4f 98
Ibiltari 7:98280cef1c4f 99 //new constructor needs an address
Ibiltari 7:98280cef1c4f 100 OSCMessage (const char * _address);
Ibiltari 7:98280cef1c4f 101 //no address
Ibiltari 7:98280cef1c4f 102 //placeholder since it's invalide OSC
Ibiltari 7:98280cef1c4f 103 OSCMessage();
Ibiltari 7:98280cef1c4f 104
Ibiltari 7:98280cef1c4f 105 //can optionally accept all of the data after the address
Ibiltari 7:98280cef1c4f 106 //OSCMessage(const char * _address, char * types, ... );
Ibiltari 7:98280cef1c4f 107 //created from another OSCMessage
Ibiltari 7:98280cef1c4f 108 OSCMessage (OSCMessage *);
Ibiltari 7:98280cef1c4f 109
ibiltari 13:c2fe6b720f94 110
Ibiltari 7:98280cef1c4f 111 //DESTRUCTOR
Ibiltari 7:98280cef1c4f 112 ~OSCMessage();
Ibiltari 7:98280cef1c4f 113
Ibiltari 7:98280cef1c4f 114 //empties all of the data
Ibiltari 7:98280cef1c4f 115 OSCMessage& empty();
Ibiltari 7:98280cef1c4f 116
Ibiltari 9:e4528f622bcc 117 void copy(OSCMessage * msg);
Ibiltari 9:e4528f622bcc 118
Ibiltari 9:e4528f622bcc 119
Ibiltari 7:98280cef1c4f 120 /*=============================================================================
Ibiltari 7:98280cef1c4f 121 SETTING DATA
Ibiltari 7:98280cef1c4f 122 =============================================================================*/
Ibiltari 7:98280cef1c4f 123
Ibiltari 7:98280cef1c4f 124 //returns the OSCMessage so that multiple 'add's can be strung together
Ibiltari 7:98280cef1c4f 125 template <typename T>
Ibiltari 7:98280cef1c4f 126 OSCMessage& add(T datum){
Ibiltari 7:98280cef1c4f 127 //make a piece of data
Ibiltari 7:98280cef1c4f 128 OSCData * d = new OSCData(datum);
Ibiltari 7:98280cef1c4f 129 //check if it has any errors
Ibiltari 7:98280cef1c4f 130 if (d->error == ALLOCFAILED){
Ibiltari 7:98280cef1c4f 131 error = ALLOCFAILED;
Ibiltari 7:98280cef1c4f 132 } else {
Ibiltari 7:98280cef1c4f 133 //resize the data array
Ibiltari 7:98280cef1c4f 134 OSCData ** dataMem = (OSCData **) realloc(data, sizeof(OSCData *) * (dataCount + 1));
Ibiltari 7:98280cef1c4f 135 if (dataMem == NULL){
Ibiltari 7:98280cef1c4f 136 error = ALLOCFAILED;
Ibiltari 7:98280cef1c4f 137 } else {
Ibiltari 7:98280cef1c4f 138 data = dataMem;
Ibiltari 7:98280cef1c4f 139 //add data to the end of the array
Ibiltari 7:98280cef1c4f 140 data[dataCount] = d;
Ibiltari 7:98280cef1c4f 141 //increment the data size
Ibiltari 7:98280cef1c4f 142 dataCount++;
Ibiltari 7:98280cef1c4f 143 }
Ibiltari 7:98280cef1c4f 144 }
Ibiltari 7:98280cef1c4f 145 return *this;
Ibiltari 7:98280cef1c4f 146 }
Ibiltari 10:936c3afce828 147
Ibiltari 7:98280cef1c4f 148 //blob specific add
Ibiltari 7:98280cef1c4f 149 OSCMessage& add(uint8_t * blob, int length){
Ibiltari 7:98280cef1c4f 150 //make a piece of data
Ibiltari 7:98280cef1c4f 151 OSCData * d = new OSCData(blob, length);
Ibiltari 7:98280cef1c4f 152 //check if it has any errors
Ibiltari 7:98280cef1c4f 153 if (d->error == ALLOCFAILED){
Ibiltari 7:98280cef1c4f 154 error = ALLOCFAILED;
Ibiltari 7:98280cef1c4f 155 } else {
Ibiltari 7:98280cef1c4f 156 //resize the data array
Ibiltari 7:98280cef1c4f 157 OSCData ** dataMem = (OSCData **) realloc(data, sizeof(OSCData *) * (dataCount + 1));
Ibiltari 7:98280cef1c4f 158 if (dataMem == NULL){
Ibiltari 7:98280cef1c4f 159 error = ALLOCFAILED;
Ibiltari 7:98280cef1c4f 160 } else {
Ibiltari 7:98280cef1c4f 161 data = dataMem;
Ibiltari 7:98280cef1c4f 162 //add data to the end of the array
Ibiltari 7:98280cef1c4f 163 data[dataCount] = d;
Ibiltari 7:98280cef1c4f 164 //increment the data size
Ibiltari 7:98280cef1c4f 165 dataCount++;
Ibiltari 7:98280cef1c4f 166 }
Ibiltari 7:98280cef1c4f 167 }
Ibiltari 7:98280cef1c4f 168 return *this;
Ibiltari 7:98280cef1c4f 169 }
Ibiltari 10:936c3afce828 170
Ibiltari 7:98280cef1c4f 171 //sets the data at a position
Ibiltari 7:98280cef1c4f 172 template <typename T>
Ibiltari 7:98280cef1c4f 173 OSCMessage& set(int position, T datum){
Ibiltari 7:98280cef1c4f 174 if (position < dataCount){
Ibiltari 7:98280cef1c4f 175 //replace the OSCData with a new one
Ibiltari 7:98280cef1c4f 176 OSCData * oldDatum = getOSCData(position);
Ibiltari 7:98280cef1c4f 177 //destroy the old one
Ibiltari 7:98280cef1c4f 178 delete oldDatum;
Ibiltari 7:98280cef1c4f 179 //make a new one
Ibiltari 7:98280cef1c4f 180 OSCData * newDatum = new OSCData(datum);
Ibiltari 7:98280cef1c4f 181 //test if there was an error
Ibiltari 7:98280cef1c4f 182 if (newDatum->error == ALLOCFAILED){
Ibiltari 7:98280cef1c4f 183 error = ALLOCFAILED;
Ibiltari 7:98280cef1c4f 184 } else {
Ibiltari 7:98280cef1c4f 185 //otherwise, put it in the data array
Ibiltari 7:98280cef1c4f 186 data[position] = newDatum;
Ibiltari 7:98280cef1c4f 187 }
Ibiltari 7:98280cef1c4f 188 } else if (position == (dataCount)){
Ibiltari 7:98280cef1c4f 189 //add the data to the end
Ibiltari 7:98280cef1c4f 190 add(datum);
Ibiltari 7:98280cef1c4f 191 } else {
Ibiltari 7:98280cef1c4f 192 //else out of bounds error
Ibiltari 7:98280cef1c4f 193 error = INDEX_OUT_OF_BOUNDS;
Ibiltari 7:98280cef1c4f 194 }
Ibiltari 7:98280cef1c4f 195 return *this;
Ibiltari 7:98280cef1c4f 196 }
Ibiltari 10:936c3afce828 197
Ibiltari 7:98280cef1c4f 198 //blob specific setter
Ibiltari 7:98280cef1c4f 199 OSCMessage& set(int position, uint8_t * blob, int length){
Ibiltari 7:98280cef1c4f 200 if (position < dataCount){
Ibiltari 7:98280cef1c4f 201 //replace the OSCData with a new one
Ibiltari 7:98280cef1c4f 202 OSCData * oldDatum = getOSCData(position);
Ibiltari 7:98280cef1c4f 203 //destroy the old one
Ibiltari 7:98280cef1c4f 204 delete oldDatum;
Ibiltari 7:98280cef1c4f 205 //make a new one
Ibiltari 7:98280cef1c4f 206 OSCData * newDatum = new OSCData(blob, length);
Ibiltari 7:98280cef1c4f 207 //test if there was an error
Ibiltari 7:98280cef1c4f 208 if (newDatum->error == ALLOCFAILED){
Ibiltari 7:98280cef1c4f 209 error = ALLOCFAILED;
Ibiltari 7:98280cef1c4f 210 } else {
Ibiltari 7:98280cef1c4f 211 //otherwise, put it in the data array
Ibiltari 7:98280cef1c4f 212 data[position] = newDatum;
Ibiltari 7:98280cef1c4f 213 }
Ibiltari 7:98280cef1c4f 214 } else if (position == (dataCount)){
Ibiltari 7:98280cef1c4f 215 //add the data to the end
Ibiltari 7:98280cef1c4f 216 add(blob, length);
Ibiltari 7:98280cef1c4f 217 } else {
Ibiltari 7:98280cef1c4f 218 //else out of bounds error
Ibiltari 7:98280cef1c4f 219 error = INDEX_OUT_OF_BOUNDS;
Ibiltari 7:98280cef1c4f 220 }
Ibiltari 7:98280cef1c4f 221 return *this;
Ibiltari 7:98280cef1c4f 222 }
Ibiltari 10:936c3afce828 223
Ibiltari 7:98280cef1c4f 224 OSCMessage& setAddress(const char *);
Ibiltari 7:98280cef1c4f 225
Ibiltari 7:98280cef1c4f 226 /*=============================================================================
Ibiltari 7:98280cef1c4f 227 GETTING DATA
Ibiltari 7:98280cef1c4f 228
Ibiltari 7:98280cef1c4f 229 getters take a position as an argument
Ibiltari 7:98280cef1c4f 230 =============================================================================*/
Ibiltari 7:98280cef1c4f 231
Ibiltari 7:98280cef1c4f 232 int32_t getInt(int);
Ibiltari 16:36d28d8e5491 233 osctime_t getTime(int);
Ibiltari 7:98280cef1c4f 234
Ibiltari 7:98280cef1c4f 235 float getFloat(int);
Ibiltari 7:98280cef1c4f 236 double getDouble(int);
Ibiltari 7:98280cef1c4f 237 bool getBoolean(int);
Ibiltari 7:98280cef1c4f 238
Ibiltari 7:98280cef1c4f 239 //return the copied string's length
Ibiltari 7:98280cef1c4f 240 int getString(int, char *);
Ibiltari 7:98280cef1c4f 241 //check that it won't overflow the passed buffer's size with a third argument
Ibiltari 7:98280cef1c4f 242 int getString(int, char *, int);
Ibiltari 7:98280cef1c4f 243 //offset and size can be defined in order to only query a part of the string
Ibiltari 7:98280cef1c4f 244 int getString(int, char *, int, int, int);
Ibiltari 7:98280cef1c4f 245
Ibiltari 7:98280cef1c4f 246 //returns the number of unsigned int8's copied into the buffer
Ibiltari 7:98280cef1c4f 247 int getBlob(int, uint8_t *);
Ibiltari 7:98280cef1c4f 248 //check that it won't overflow the passed buffer's size with a third argument
Ibiltari 7:98280cef1c4f 249 int getBlob(int, uint8_t *, int);
Ibiltari 7:98280cef1c4f 250 //offset and size can be defined in order to only query a part of the blob's content
Ibiltari 7:98280cef1c4f 251 int getBlob(int, uint8_t *, int, int, int);
Ibiltari 7:98280cef1c4f 252
Ibiltari 7:98280cef1c4f 253
Ibiltari 7:98280cef1c4f 254 // returns the length of blob
Ibiltari 7:98280cef1c4f 255 uint32_t getBlobLength(int position);
Ibiltari 7:98280cef1c4f 256
Ibiltari 7:98280cef1c4f 257 //returns the number of bytes of the data at that position
Ibiltari 7:98280cef1c4f 258 int getDataLength(int);
Ibiltari 7:98280cef1c4f 259
Ibiltari 7:98280cef1c4f 260 //returns the type at the position
Ibiltari 7:98280cef1c4f 261 char getType(int);
Ibiltari 7:98280cef1c4f 262
Ibiltari 7:98280cef1c4f 263 //put the address in the buffer
Ibiltari 7:98280cef1c4f 264 const char * getAddress( int offset = 0);
Ibiltari 7:98280cef1c4f 265 int getAddress(char * buffer, int offset, int len);
Ibiltari 7:98280cef1c4f 266
Ibiltari 17:9479c15a9d54 267 int getAddressLength(int offset = 0);
Ibiltari 7:98280cef1c4f 268
Ibiltari 7:98280cef1c4f 269
Ibiltari 7:98280cef1c4f 270 /*=============================================================================
Ibiltari 7:98280cef1c4f 271 TESTING DATA
Ibiltari 7:98280cef1c4f 272
Ibiltari 7:98280cef1c4f 273 testers take a position as an argument
Ibiltari 7:98280cef1c4f 274 =============================================================================*/
Ibiltari 7:98280cef1c4f 275
Ibiltari 7:98280cef1c4f 276 bool isInt(int);
Ibiltari 7:98280cef1c4f 277 bool isFloat(int);
Ibiltari 7:98280cef1c4f 278 bool isBlob(int);
Ibiltari 7:98280cef1c4f 279 bool isChar(int);
Ibiltari 7:98280cef1c4f 280 bool isString(int);
Ibiltari 7:98280cef1c4f 281 bool isDouble(int);
Ibiltari 7:98280cef1c4f 282 bool isBoolean(int);
Ibiltari 7:98280cef1c4f 283 bool isTime(int);
Ibiltari 7:98280cef1c4f 284
Ibiltari 7:98280cef1c4f 285 /*=============================================================================
Ibiltari 7:98280cef1c4f 286 PATTERN MATCHING
Ibiltari 7:98280cef1c4f 287 =============================================================================*/
ibiltari 12:f2c792ac1aca 288
Ibiltari 7:98280cef1c4f 289 //match the pattern against the address
Ibiltari 7:98280cef1c4f 290 //returns true only for a complete match
Ibiltari 7:98280cef1c4f 291 bool fullMatch( const char * pattern, int = 0);
Ibiltari 7:98280cef1c4f 292
Ibiltari 7:98280cef1c4f 293 //returns the number of characters matched in the address
Ibiltari 7:98280cef1c4f 294 int match( const char * pattern, int = 0);
Ibiltari 7:98280cef1c4f 295
Ibiltari 7:98280cef1c4f 296 //calls the function with the message as the arg if it was a full match
Ibiltari 7:98280cef1c4f 297 bool dispatch(const char * pattern, void (*callback)(OSCMessage &), int = 0);
Ibiltari 7:98280cef1c4f 298
Ibiltari 7:98280cef1c4f 299 //like dispatch, but allows for partial matches
Ibiltari 7:98280cef1c4f 300 //the address match offset is sent as an argument to the callback
Ibiltari 7:98280cef1c4f 301 //also room for an option address offset to allow for multiple nested routes
Ibiltari 7:98280cef1c4f 302 bool route(const char * pattern, void (*callback)(OSCMessage &, int), int = 0);
Ibiltari 7:98280cef1c4f 303
ibiltari 12:f2c792ac1aca 304
Ibiltari 7:98280cef1c4f 305
Ibiltari 7:98280cef1c4f 306 /*=============================================================================
Ibiltari 7:98280cef1c4f 307 SIZE
Ibiltari 7:98280cef1c4f 308 =============================================================================*/
Ibiltari 7:98280cef1c4f 309
Ibiltari 7:98280cef1c4f 310 //the number of data that the message contains
Ibiltari 7:98280cef1c4f 311 int size();
Ibiltari 7:98280cef1c4f 312
Ibiltari 7:98280cef1c4f 313 //computes the number of bytes the OSCMessage occupies if everything is 32-bit aligned
Ibiltari 7:98280cef1c4f 314 int bytes();
Ibiltari 7:98280cef1c4f 315
Ibiltari 7:98280cef1c4f 316 /*=============================================================================
Ibiltari 7:98280cef1c4f 317 TRANSMISSION
Ibiltari 7:98280cef1c4f 318 =============================================================================*/
Ibiltari 7:98280cef1c4f 319
Ibiltari 7:98280cef1c4f 320 //send the message
Ibiltari 19:35408d190f4e 321 OSCMessage& send(UDPSocket &p, const SocketAddress &address);
Ibiltari 10:936c3afce828 322
Ibiltari 7:98280cef1c4f 323
Ibiltari 7:98280cef1c4f 324 //fill the message from a byte stream
Ibiltari 7:98280cef1c4f 325 OSCMessage& fill(uint8_t);
Ibiltari 7:98280cef1c4f 326 OSCMessage& fill(uint8_t *, int);
Ibiltari 7:98280cef1c4f 327
Ibiltari 7:98280cef1c4f 328 /*=============================================================================
Ibiltari 7:98280cef1c4f 329 ERROR
Ibiltari 7:98280cef1c4f 330 =============================================================================*/
Ibiltari 7:98280cef1c4f 331
Ibiltari 7:98280cef1c4f 332 bool hasError();
Ibiltari 7:98280cef1c4f 333
Ibiltari 7:98280cef1c4f 334 OSCErrorCode getError();
Ibiltari 7:98280cef1c4f 335
Ibiltari 7:98280cef1c4f 336 };
Ibiltari 7:98280cef1c4f 337
Ibiltari 7:98280cef1c4f 338
Ibiltari 7:98280cef1c4f 339 #endif