forked from CNMAT/OSC
-
Notifications
You must be signed in to change notification settings - Fork 1
/
OSCMessage.h
executable file
·330 lines (261 loc) · 9.32 KB
/
OSCMessage.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
/*
Written by Yotam Mann, The Center for New Music and Audio Technologies,
University of California, Berkeley. Copyright (c) 2012, The Regents of
the University of California (Regents).
Permission to use, copy, modify, distribute, and distribute modified versions
of this software and its documentation without fee and without a signed
licensing agreement, is hereby granted, provided that the above copyright
notice, this paragraph and the following two paragraphs appear in all copies,
modifications, and distributions.
IN NO EVENT SHALL REGENTS BE LIABLE TO ANY PARTY FOR DIRECT, INDIRECT,
SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES, INCLUDING LOST PROFITS, ARISING
OUT OF THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION, EVEN IF REGENTS HAS
BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
REGENTS SPECIFICALLY DISCLAIMS ANY WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
PURPOSE. THE SOFTWARE AND ACCOMPANYING DOCUMENTATION, IF ANY, PROVIDED
HEREUNDER IS PROVIDED "AS IS". REGENTS HAS NO OBLIGATION TO PROVIDE
MAINTENANCE, SUPPORT, UPDATES, ENHANCEMENTS, OR MODIFICATIONS.
For bug reports and feature requests please email me at [email protected]
*/
#ifndef OSCMESSAGE_h
#define OSCMESSAGE_h
#include "OSCData.h"
#include <Print.h>
class OSCMessage
{
private:
//friends
friend class OSCBundle;
/*=============================================================================
PRIVATE VARIABLES
=============================================================================*/
//the address
char * address;
//the data
OSCData ** data;
//the number of OSCData in the data array
int dataCount;
//error codes for potential runtime problems
OSCErrorCode error;
/*=============================================================================
DECODING INCOMING BYTES
=============================================================================*/
//the decoding states for incoming bytes
enum DecodeState {
STANDBY,
ADDRESS,
ADDRESS_PADDING,
TYPES,
TYPES_PADDING,
DATA,
DATA_PADDING,
DONE,
} decodeState;
//stores incoming bytes until they can be decoded
uint8_t * incomingBuffer;
int incomingBufferSize; // how many bytes are stored
int incomingBufferFree; // how many bytes are allocated but unused
//adds a byte to the buffer
void addToIncomingBuffer(uint8_t);
//clears the incoming buffer
void clearIncomingBuffer();
//decoding function
void decode(uint8_t);
void decodeAddress();
void decodeType(uint8_t);
void decodeData(uint8_t);
/*=============================================================================
HELPER FUNCTIONS
=============================================================================*/
void setupMessage();
//compares the OSCData's type char to a test char
bool testType(int position, char type);
//returns the number of bytes to pad to make it 4-bit aligned
// int padSize(int bytes);
public:
//returns the OSCData at that position
OSCData * getOSCData(int);
/*=============================================================================
CONSTRUCTORS / DESTRUCTOR
=============================================================================*/
//new constructor needs an address
OSCMessage (const char * _address);
//no address
//placeholder since it's invalide OSC
OSCMessage();
//can optionally accept all of the data after the address
//OSCMessage(const char * _address, char * types, ... );
//created from another OSCMessage
OSCMessage (OSCMessage *);
//DESTRUCTOR
~OSCMessage();
//empties all of the data
void empty();
/*=============================================================================
SETTING DATA
=============================================================================*/
//returns the OSCMessage so that multiple 'add's can be strung together
template <typename T>
OSCMessage& add(T datum){
//make a piece of data
OSCData * d = new OSCData(datum);
//check if it has any errors
if (d->error == ALLOCFAILED){
error = ALLOCFAILED;
} else {
//resize the data array
OSCData ** dataMem = (OSCData **) realloc(data, sizeof(OSCData *) * (dataCount + 1));
if (dataMem == NULL){
error = ALLOCFAILED;
} else {
data = dataMem;
//add data to the end of the array
data[dataCount] = d;
//increment the data size
dataCount++;
}
}
return *this;
}
//blob specific add
OSCMessage& add(uint8_t * blob, int length){
//make a piece of data
OSCData * d = new OSCData(blob, length);
//check if it has any errors
if (d->error == ALLOCFAILED){
error = ALLOCFAILED;
} else {
//resize the data array
OSCData ** dataMem = (OSCData **) realloc(data, sizeof(OSCData *) * (dataCount + 1));
if (dataMem == NULL){
error = ALLOCFAILED;
} else {
data = dataMem;
//add data to the end of the array
data[dataCount] = d;
//increment the data size
dataCount++;
}
}
return *this;
}
//sets the data at a position
template <typename T>
void set(int position, T datum){
if (position < dataCount){
//replace the OSCData with a new one
OSCData * oldDatum = getOSCData(position);
//destroy the old one
delete oldDatum;
//make a new one
OSCData * newDatum = new OSCData(datum);
//test if there was an error
if (newDatum->error == ALLOCFAILED){
error = ALLOCFAILED;
} else {
//otherwise, put it in the data array
data[position] = newDatum;
}
} else if (position == (dataCount)){
//add the data to the end
add(datum);
} else {
//else out of bounds error
error = INDEX_OUT_OF_BOUNDS;
}
}
//blob specific setter
void set(int position, uint8_t * blob, int length){
if (position < dataCount){
//replace the OSCData with a new one
OSCData * oldDatum = getOSCData(position);
//destroy the old one
delete oldDatum;
//make a new one
OSCData * newDatum = new OSCData(blob, length);
//test if there was an error
if (newDatum->error == ALLOCFAILED){
error = ALLOCFAILED;
} else {
//otherwise, put it in the data array
data[position] = newDatum;
}
} else if (position == (dataCount)){
//add the data to the end
add(blob, length);
} else {
//else out of bounds error
error = INDEX_OUT_OF_BOUNDS;
}
}
void setAddress(const char *);
/*=============================================================================
GETTING DATA
getters take a position as an argument
=============================================================================*/
int32_t getInt(int);
osctime_t getTime(int);
float getFloat(int);
double getDouble(int);
bool getBoolean(int);
//return the copied string's length
int getString(int, char *, int);
//returns the number of unsigned int8's copied into the buffer
int getBlob(int, uint8_t *, int);
//returns the number of bytes of the data at that position
int getDataLength(int);
//returns the type at the position
char getType(int);
//put the address in the buffer
int getAddress(char * buffer, int offset = 0);
int getAddress(char * buffer, int offset, int len);
// TODO: int getAddressLength(int offset = 0);
/*=============================================================================
TESTING DATA
testers take a position as an argument
=============================================================================*/
bool isInt(int);
bool isFloat(int);
bool isBlob(int);
bool isChar(int);
bool isString(int);
bool isDouble(int);
bool isBoolean(int);
bool isTime(int);
/*=============================================================================
PATTERN MATCHING
=============================================================================*/
//match the pattern against the address
//returns true only for a complete match
bool fullMatch( const char * pattern, int = 0);
//returns the number of characters matched in the address
int match( const char * pattern, int = 0);
//calls the function with the message as the arg if it was a full match
bool dispatch(const char * pattern, void (*callback)(OSCMessage &), int = 0);
//like dispatch, but allows for partial matches
//the address match offset is sent as an argument to the callback
//also room for an option address offset to allow for multiple nested routes
bool route(const char * pattern, void (*callback)(OSCMessage &, int), int = 0);
/*=============================================================================
SIZE
=============================================================================*/
//the number of data that the message contains
int size();
//computes the number of bytes the OSCMessage occupies if everything is 32-bit aligned
int bytes();
/*=============================================================================
TRANSMISSION
=============================================================================*/
//send the message
void send(Print &p);
//fill the message from a byte stream
void fill(uint8_t);
void fill(uint8_t *, int);
/*=============================================================================
ERROR
=============================================================================*/
bool hasError();
OSCErrorCode getError();
};
#endif