-
Notifications
You must be signed in to change notification settings - Fork 138
/
OSCData.cpp
398 lines (361 loc) · 7.94 KB
/
OSCData.cpp
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
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
#include "OSCData.h"
osctime_t zerotime = {0,0};
oscrgba_t zeroRgba = {0,0,0,0};
oscmidi_t zeroMidi = {0,0,0,0};
oscevent_t zeroEvent = OSC_NULL;
/*=============================================================================
CONSTRUCTORS
overloaded methods for each of the types which will
set the type flag, the size (in bytes), and the data
=============================================================================*/
OSCData::OSCData(const char * s){
error = OSC_OK;
type = 's';
bytes = (strlen(s) + 1);
//own the data
char * mem = (char *) malloc(bytes);
if (mem == NULL){
error = ALLOCFAILED;
} else {
strcpy(mem, s);
data.s = mem;
}
}
#if INT_MAX!=2147483647
OSCData::OSCData(intOSC_t i){
error = OSC_OK;
type = 'i';
bytes = 4;
data.i = i;
}
#endif
OSCData::OSCData(int i){
error = OSC_OK;
type = 'i';
bytes = 4;
data.i = i;
}
OSCData::OSCData(unsigned int i){
error = OSC_OK;
type = 'i';
bytes = 4;
data.i = i;
}
OSCData::OSCData(int64_t i){
error = OSC_OK;
type = 'h';
bytes = 8;
data.i = i;
}
OSCData::OSCData(oscrgba_t rgba){
error = OSC_OK;
type = 'r';
bytes = 4;
data.rgba = rgba;
}
OSCData::OSCData(oscmidi_t midi){
error = OSC_OK;
type = 'm';
bytes = 4;
data.midi = midi;
}
#if defined(__SAM3X8E__)
OSCData::OSCData(int16_t i){
error = OSC_OK;
type = 'i';
bytes = 4;
data.i = i;
}
#endif
OSCData::OSCData(float f){
error = OSC_OK;
type = 'f';
bytes = 4;
data.f = f;
}
OSCData::OSCData(osctime_t t){
error = OSC_OK;
type = 't';
bytes = 8;
data.time = t;
}
OSCData::OSCData(oscevent_t event){
error = OSC_OK;
type = (event==OSC_IMPULSE)?'I':'N';
bytes = 0;
}
OSCData::OSCData(boolean b){
error = OSC_OK;
type = b?'T':'F';
bytes = 0;
}
OSCData::OSCData(double d){
error = OSC_OK;
bytes = sizeof(double);
//if it's not 8 bytes it's not a true double
if (bytes == 8){
type = 'd';
data.d = d;
} else {
type = 'f';
data.f = d;
}
}
OSCData::OSCData(uint8_t * b, int len){
error = OSC_OK;
type = 'b';
bytes = len + 4;
//add the size to the front of the blob
uint32_t len32 = (uint32_t) len;
//make sure the length is endian-safe
len32 = BigEndian(len32);
uint8_t * lenPtr = (uint8_t *) (& len32);
//own the data
if(bytes>0)
{
uint8_t * mem = (uint8_t * ) malloc(bytes);
if (mem == NULL){
error = ALLOCFAILED;
} else {
//copy over the blob length
memcpy(mem, lenPtr, 4);
//copy over the blob data
memcpy(mem + 4, b, len);
data.b = mem;
}
}
else
data.b = 0;
}
OSCData::OSCData (OSCData * datum){
error = OSC_OK;
type = datum->type;
bytes = datum->bytes;
if ( (type == 'i') || (type == 'f') || (type == 'd') || (type == 't')
|| (type == 'h') || (type == 'c') || (type == 'r') || (type == 'm')
)
{
data = datum->data;
} else if ((type == 's') || (type == 'b')){
//allocate a new piece of memory
uint8_t * mem = (uint8_t * ) malloc(bytes);
if (mem == NULL){
error = ALLOCFAILED;
} else {
//copy over the blob length
memcpy(mem, datum->data.b, bytes);
data.b = mem;
}
}
}
//DESTRUCTOR
OSCData::~OSCData(){
//if there are no bytes, there is nothing to free
if (bytes>0){
//if the data is of type 's' or 'b', need to free that memory
if (type == 's'){
free(data.s);
}else if( type == 'b'){
free(data.b);
}
}
}
//sets just the type as a message placeholder
//no data
OSCData::OSCData(char t){
error = (t == 'T' || t == 'F') ? OSC_OK : INVALID_OSC;
type = t;
bytes = 0;
}
/*=============================================================================
GETTERS
perform a safety check to make sure the data type matches the request
otherwise returns NULL
=============================================================================*/
int64_t OSCData::getInt64(){
if (type == 'h'){
return data.i;
} else {
#ifndef ESPxx
return (int64_t)NULL;
#else
return -1;
#endif
}
}
int32_t OSCData::getInt(){
if (type == 'i'){
return data.i;
} else {
#ifndef ESPxx
return (int32_t)NULL;
#else
return -1;
#endif
}
}
osctime_t OSCData::getTime(){
if (type == 't'){
return data.time;
} else {
return zerotime;
}
}
float OSCData::getFloat(){
if (type == 'f'){
return data.f;
} else {
#ifndef ESPxx
return (float)NULL;
#else
return -1;
#endif
}
}
double OSCData::getDouble(){
if (type == 'd'){
return data.d;
} else {
#ifndef ESPxx
return (double)NULL;
#else
return -1;
#endif
}
}
bool OSCData::getBoolean(){
if (type == 'T'){
return true;
} else if (type=='F'){
return false;
}
else
#ifndef ESPxx
return NULL;
#else
return -1;
#endif
}
oscevent_t OSCData::getEvent() {
if (type == 'N'){
return OSC_NULL;
} else if (type=='I'){
return OSC_IMPULSE;
} else {
return zeroEvent;
}
}
oscrgba_t OSCData::getRgba() {
if (type == 'r'){
return data.rgba;
} else {
return zeroRgba;
}
}
oscmidi_t OSCData::getMidi() {
if (type == 'm'){
return data.midi;
} else {
return zeroMidi;
}
}
// no-safety-check straightforward way to fill the passed buffer
// with the received string
int OSCData::getString(char * strBuffer){
if (type == 's'){
strncpy(strBuffer, data.s, bytes);
return bytes;
} else {
#ifndef ESPxx
return (int)NULL;
#else
return -1;
#endif
}
}
// it's possible to pass strBuffer's size as argument (length)
// in order to check that it won't be overflown
int OSCData::getString(char * strBuffer, int length){
if (type == 's' && bytes <= length){
strncpy(strBuffer, data.s, bytes);
return bytes;
} else {
#ifndef ESPxx
return (int)NULL;
#else
return -1;
#endif
}
}
// Here we can get only a part of the string
int OSCData::getString(char * strBuffer, int length, int offset, int size)
{
int maxLen = bytes - offset;
if (type == 's' && maxLen >= 0 && size <= maxLen && size <= length){
strncpy(strBuffer, data.s + offset, size);
return size;
} else {
#ifndef ESPxx
return (int)NULL;
#else
return -1;
#endif
}
}
// no-safety-check straightforward way to fill the passed buffer
// with the contents of the received blob
int OSCData::getBlob(uint8_t * blobBuffer){
// read the blob length
int blobLength = getBlobLength();
if (type == 'b'){
memcpy(blobBuffer, data.b + 4, blobLength);
return blobLength;
} else {
#ifndef ESPxx
return (int)NULL;
#else
return -1;
#endif
}
}
// it's possible to pass blobBuffer's size as argument (length)
// in order to check that it won't be overflown
int OSCData::getBlob(uint8_t * blobBuffer, int length){
//jump over the first 4 bytes which encode the length
int blobLength = bytes-4;
if (type == 'b' && blobLength <= length){
memcpy(blobBuffer, data.b + 4, blobLength);
return blobLength;
} else {
#ifndef ESPxx
return (int)NULL;
#else
return -1;
#endif
}
}
// Here we can get only a part of the blob
int OSCData::getBlob(uint8_t * blobBuffer, int length, int offset, int size){
//jump over the first 4 bytes which encode the length
int blobLength = bytes-4-offset;
if (type == 'b' && blobLength >= 0 && size <= blobLength && size <= length){
memcpy(blobBuffer, data.b + 4 + offset, size);
return size;
} else {
#ifndef ESPxx
return (int)NULL;
#else
return -1;
#endif
}
}
const uint8_t* OSCData::getBlob() {
return type == 'b' ? data.b + 4 : NULL;
}
int OSCData::getBlobLength(){
if (type == 'b'){
//jump over the first 4 bytes which encode the length
return bytes-4;
}
return -1;
}