forked from CNMAT/OSC
-
Notifications
You must be signed in to change notification settings - Fork 1
/
OSCMessage.cpp
executable file
·699 lines (630 loc) · 20.3 KB
/
OSCMessage.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
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
/*
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]
*/
#include "OSCMessage.h"
#include "OSCMatch.h"
#include "OSCTiming.h"
extern osctime_t zerotime;
/*=============================================================================
CONSTRUCTORS / DESTRUCTOR
=============================================================================*/
//constructor with address
OSCMessage::OSCMessage(const char * _address){
setupMessage();
setAddress(_address);
}
//constructor with nothing
//just a placeholder since the message is invalid
OSCMessage::OSCMessage(){
setupMessage();
error = INVALID_OSC;
}
//variable length constructor
//for example OSCMessage msg("/address", "isf", 1, "two", 3.0);
/*
OSCMessage::OSCMessage(const char * _address, char * types, ... ){
setupMessage(_address);
}
*/
//sets up a new message
void OSCMessage::setupMessage(){
address = NULL;
//setup the attributes
dataCount = 0;
error = OSC_OK;
//setup the space for data
data = NULL;
//setup for filling the message
incomingBuffer = NULL;
incomingBufferSize = 0;
incomingBufferFree = 0;
clearIncomingBuffer();
//set the decode state
decodeState = STANDBY;
}
//DESTRUCTOR
OSCMessage::~OSCMessage(){
//free everything that needs to be freed
//free the address
free(address);
//free the data
empty();
//free the filling buffer
free(incomingBuffer);
}
void OSCMessage::empty(){
error = OSC_OK;
//free each of hte data in the array
for (int i = 0; i < dataCount; i++){
OSCData * datum = getOSCData(i);
//explicitly destruct the data
//datum->~OSCData();
delete datum;
}
//and free the array
free(data);
data = NULL;
dataCount = 0;
decodeState = STANDBY;
clearIncomingBuffer();
}
//COPY
OSCMessage::OSCMessage(OSCMessage * msg){
//start with a message with the same address
setupMessage();
setAddress(msg->address);
//add each of the data to the other message
for (int i = 0; i < msg->dataCount; i++){
add(msg->data[i]);
}
}
/*=============================================================================
GETTING DATA
=============================================================================*/
OSCData * OSCMessage::getOSCData(int position){
if (position < dataCount){
OSCData * datum = data[position];
return datum;
} else {
error = INDEX_OUT_OF_BOUNDS;
return NULL;
}
}
int32_t OSCMessage::getInt(int position){
OSCData * datum = getOSCData(position);
if (!hasError()){
return datum->getInt();
} else {
return -1;
}
}
osctime_t OSCMessage::getTime(int position){
OSCData * datum = getOSCData(position);
if (!hasError()){
return datum->getTime();
} else {
return zerotime;
}
}
float OSCMessage::getFloat(int position){
OSCData * datum = getOSCData(position);
if (!hasError()){
return datum->getFloat();
} else {
return -1;
}
}
double OSCMessage::getDouble(int position){
OSCData * datum = getOSCData(position);
if (!hasError()){
return datum->getDouble();
} else {
return -1;
}
}
bool OSCMessage::getBoolean(int position){
OSCData * datum = getOSCData(position);
if (!hasError()){
return datum->getBoolean();
} else {
return NULL;
}
}
int OSCMessage::getString(int position, char * buffer, int bufferSize){
OSCData * datum = getOSCData(position);
if (!hasError()){
//the number of bytes to copy is the smaller between the buffer size and the datum's byte length
int copyBytes = bufferSize < datum->bytes? bufferSize : datum->bytes;
return datum->getString(buffer, copyBytes);
} else {
return -1;
}
}
int OSCMessage::getBlob(int position, uint8_t * buffer, int bufferSize){
OSCData * datum = getOSCData(position);
if (!hasError()){
//the number of bytes to copy is the smaller between the buffer size and the datum's byte length
int copyBytes = bufferSize < datum->bytes? bufferSize : datum->bytes;
return datum->getBlob(buffer, copyBytes);
} else {
return -1;
}
}
char OSCMessage::getType(int position){
OSCData * datum = getOSCData(position);
if (!hasError()){
return datum->type;
} else {
return '\0';
}
}
int OSCMessage::getDataLength(int position){
OSCData * datum = getOSCData(position);
if (!hasError()){
return datum->bytes;
} else {
return 0;
}
}
/*=============================================================================
TESTING DATA
=============================================================================*/
bool OSCMessage::testType(int position, char type){
OSCData * datum = getOSCData(position);
if (!hasError()){
return datum->type == type;
} else {
return false;
}
}
bool OSCMessage::isInt(int position){
return testType(position, 'i');
}
bool OSCMessage::isTime(int position){
return testType(position, 't');
}
bool OSCMessage::isFloat(int position){
return testType(position, 'f');
}
bool OSCMessage::isBlob(int position){
return testType(position, 'b');
}
bool OSCMessage::isChar(int position){
return testType(position, 'c');
}
bool OSCMessage::isString(int position){
return testType(position, 's');
}
bool OSCMessage::isDouble(int position){
return testType(position, 'd');
}
bool OSCMessage::isBoolean(int position){
return testType(position, 'T') || testType(position, 'F');
}
/*=============================================================================
PATTERN MATCHING
=============================================================================*/
int OSCMessage::match(const char * pattern, int addr_offset){
int pattern_offset;
int address_offset;
int ret = osc_match(address + addr_offset, pattern, &pattern_offset, &address_offset);
char * next = (char *) (address + addr_offset + pattern_offset);
if (ret==3){
return pattern_offset;
} else if (pattern_offset > 0 && *next == '/'){
return pattern_offset;
} else {
return 0;
}
}
bool OSCMessage::fullMatch( const char * pattern, int addr_offset){
int pattern_offset;
int address_offset;
int ret = osc_match(address + addr_offset, pattern, &address_offset, &pattern_offset);
return (ret==3);
}
bool OSCMessage::dispatch(const char * pattern, void (*callback)(OSCMessage &), int addr_offset){
if (fullMatch(pattern, addr_offset)){
callback(*this);
return true;
} else {
return false;
}
}
bool OSCMessage::route(const char * pattern, void (*callback)(OSCMessage &, int), int initial_offset){
int match_offset = match(pattern, initial_offset);
if (match_offset>0){
callback(*this, match_offset + initial_offset);
return true;
} else {
return false;
}
}
/*=============================================================================
ADDRESS
=============================================================================*/
int OSCMessage::getAddress(char * buffer, int offset){
strcpy(buffer, address+offset);
return strlen(buffer);
}
int OSCMessage::getAddress(char * buffer, int offset, int len){
strncpy(buffer, address+offset, len);
return strlen(buffer);
}
void OSCMessage::setAddress(const char * _address){
//free the previous address
free(address); // are we sure address was allocated?
//copy the address
char * addressMemory = (char *) malloc( (strlen(_address) + 1) * sizeof(char) );
if (addressMemory == NULL){
error = ALLOCFAILED;
address = NULL;
} else {
strcpy(addressMemory, _address);
address = addressMemory;
}
}
/*=============================================================================
SIZE
=============================================================================*/
#ifdef SLOWpadcalculation
int OSCMessage::padSize(int _bytes){
int space = (_bytes + 3) / 4;
space *= 4;
return space - _bytes;
}
#else
static inline int padSize(int bytes) { return (4- (bytes&03))&3; }
#endif
//returns the number of OSCData in the OSCMessage
int OSCMessage::size(){
return dataCount;
}
int OSCMessage::bytes(){
int messageSize = 0;
//send the address
int addrLen = strlen(address) + 1;
messageSize += addrLen;
//padding amount
int addrPad = padSize(addrLen);
messageSize += addrPad;
//add the comma seperator
messageSize += 1;
//add the types
messageSize += dataCount;
//pad the types
int typePad = padSize(dataCount + 1); //for the comma
if (typePad == 0){
typePad = 4; // to make sure the type string is null terminated
}
messageSize+=typePad;
//then the data
for (int i = 0; i < dataCount; i++){
OSCData * datum = getOSCData(i);
messageSize+=datum->bytes;
messageSize += padSize(datum->bytes);
}
return messageSize;
}
/*=============================================================================
ERROR HANDLING
=============================================================================*/
bool OSCMessage::hasError(){
bool retError = error != OSC_OK;
//test each of the data
for (int i = 0; i < dataCount; i++){
OSCData * datum = getOSCData(i);
retError |= datum->error != OSC_OK;
}
return retError;
}
OSCErrorCode OSCMessage::getError(){
return error;
}
/*=============================================================================
SENDING
=============================================================================*/
void OSCMessage::send(Print &p){
//don't send a message with errors
if (hasError()){
return;
}
uint8_t nullChar = '\0';
//send the address
int addrLen = strlen(address) + 1;
//padding amount
int addrPad = padSize(addrLen);
//write it to the stream
p.write((uint8_t *) address, addrLen);
//add the padding
while(addrPad--){
p.write(nullChar);
}
//add the comma seperator
p.write((uint8_t) ',');
//add the types
#ifdef PAULSSUGGESTION
// Paul suggested buffering on the stack
// to improve performance. The problem is this could exhaust the stack
// for long complex messages
{
uint8_t typstr[dataCount];
for (int i = 0; i < dataCount; i++){
typstr[i] = getType(i);
}
p.write(typstr,dataCount);
}
#else
for (int i = 0; i < dataCount; i++){
p.write((uint8_t) getType(i));
}
#endif
//pad the types
int typePad = padSize(dataCount + 1); // 1 is for the comma
if (typePad == 0){
typePad = 4; // This is because the type string has to be null terminated
}
while(typePad--){
p.write(nullChar);
}
//write the data
for (int i = 0; i < dataCount; i++){
OSCData * datum = getOSCData(i);
if ((datum->type == 's') || (datum->type == 'b')){
p.write(datum->data.b, datum->bytes);
int dataPad = padSize(datum->bytes);
while(dataPad--){
p.write(nullChar);
}
} else if (datum->type == 'd'){
double d = BigEndian(datum->data.d);
uint8_t * ptr = (uint8_t *) &d;
p.write(ptr, 8);
} else if (datum->type == 't'){
osctime_t time = datum->data.time;
uint32_t d = BigEndian(time.seconds);
uint8_t * ptr = (uint8_t *) &d;
p.write(ptr, 4);
d = BigEndian(time.fractionofseconds);
ptr = (uint8_t *) &d;
p.write(ptr, 4);
} else if (datum->type == 'T' || datum->type == 'F')
{ }
else { // float or int
uint32_t i = BigEndian(datum->data.i);
uint8_t * ptr = (uint8_t *) &i;
p.write(ptr, datum->bytes);
}
}
}
/*=============================================================================
FILLING
=============================================================================*/
void OSCMessage::fill(uint8_t incomingByte){
decode(incomingByte);
}
void OSCMessage::fill(uint8_t * incomingBytes, int length){
while (length--){
decode(*incomingBytes++);
}
}
/*=============================================================================
DECODING
=============================================================================*/
void OSCMessage::decodeAddress(){
setAddress((char *) incomingBuffer);
//change the error from invalide message
error = OSC_OK;
clearIncomingBuffer();
}
void OSCMessage::decodeType(uint8_t incomingByte){
char type = incomingByte;
add(type);
}
void OSCMessage::decodeData(uint8_t incomingByte){
//get the first OSCData to re-set
for (int i = 0; i < dataCount; i++){
OSCData * datum = getOSCData(i);
if (datum->error == INVALID_OSC){
//set the contents of datum with the data received
switch (datum->type){
case 'i':
if (incomingBufferSize == 4){
//parse the buffer as an int
union {
int32_t i;
uint8_t b[4];
} u;
memcpy(u.b, incomingBuffer, 4);
int32_t dataVal = BigEndian(u.i);
set(i, dataVal);
clearIncomingBuffer();
}
break;
case 'f':
if (incomingBufferSize == 4){
//parse the buffer as an int
union {
float f;
uint8_t b[4];
} u;
memcpy(u.b, incomingBuffer, 4);
float dataVal = BigEndian(u.f);
set(i, dataVal);
clearIncomingBuffer();
}
break;
case 'd':
if (incomingBufferSize == 8){
//parse the buffer as an int
union {
double d;
uint8_t b[8];
} u;
memcpy(u.b, incomingBuffer, 8);
double dataVal = BigEndian(u.d);
set(i, dataVal);
clearIncomingBuffer();
}
break;
case 't':
if (incomingBufferSize == 8){
//parse the buffer as an int
union {
osctime_t t;
uint8_t b[8];
} u;
memcpy(u.b, incomingBuffer, 8);
u.t.seconds = BigEndian(u.t.seconds);
u.t.fractionofseconds = BigEndian(u.t.fractionofseconds);
set(i, u.t);
clearIncomingBuffer();
}
break;
case 's':
if (incomingByte == 0){
char * str = (char *) incomingBuffer;
set(i, str);
clearIncomingBuffer();
decodeState = DATA_PADDING;
}
break;
case 'b':
if (incomingBufferSize > 4){
//compute the expected blob size
union {
uint32_t i;
uint8_t b[4];
} u;
memcpy(u.b, incomingBuffer, 4);
uint32_t blobLength = BigEndian(u.i);
if (incomingBufferSize == blobLength + 4){
set(i, incomingBuffer + 4, blobLength);
clearIncomingBuffer();
decodeState = DATA_PADDING;
}
}
break;
}
//break out of the for loop once we've selected the first invalid message
break;
}
}
}
//does not validate the incoming OSC for correctness
void OSCMessage::decode(uint8_t incomingByte){
addToIncomingBuffer(incomingByte);
switch (decodeState){
case STANDBY:
if (incomingByte == '/'){
decodeState = ADDRESS;
}
break;
case ADDRESS:
if (incomingByte == 0){
//end of the address
//decode the address
decodeAddress();
//next state
decodeState = ADDRESS_PADDING;
}
break;
case ADDRESS_PADDING:
//it does not count the padding
if (incomingByte==','){
//next state
decodeState = TYPES;
clearIncomingBuffer();
}
break;
case TYPES:
if (incomingByte != 0){
//next state
decodeType(incomingByte);
} else {
decodeState = TYPES_PADDING;
}
//FALL THROUGH to test if it should go to the data state
case TYPES_PADDING: {
//compute the padding size for the types
//to determine the start of the data section
int typePad = padSize(dataCount + 1); // 1 is the comma
if (typePad == 0){
typePad = 4; // to make sure it will be null terminated
}
if (incomingBufferSize == (typePad + dataCount)){
clearIncomingBuffer();
decodeState = DATA;
}
}
break;
case DATA:
decodeData(incomingByte);
break;
case DATA_PADDING:{
//get the last valid data
for (int i = dataCount - 1; i >= 0; i--){
OSCData * datum = getOSCData(i);
if (datum->error == OSC_OK){
//compute the padding size for the data
int dataPad = padSize(datum->bytes);
if (incomingBufferSize == dataPad){
clearIncomingBuffer();
decodeState = DATA;
}
break;
}
}
}
break;
}
}
/*=============================================================================
INCOMING BUFFER MANAGEMENT
=============================================================================*/
#define OSCPREALLOCATEIZE 16
void OSCMessage::addToIncomingBuffer(uint8_t incomingByte){
//realloc some space for the new byte and stick it on the end
if(incomingBufferFree>0)
{
incomingBuffer[incomingBufferSize++] = incomingByte;
incomingBufferFree--;
}
else
{
incomingBuffer = (uint8_t *) realloc ( incomingBuffer, incomingBufferSize + 1 + OSCPREALLOCATEIZE);
if (incomingBuffer != NULL){
incomingBuffer[incomingBufferSize++] = incomingByte;
incomingBufferFree = OSCPREALLOCATEIZE;
} else {
error = ALLOCFAILED;
}
}
}
void OSCMessage::clearIncomingBuffer(){
incomingBuffer = (uint8_t *) realloc ( incomingBuffer, OSCPREALLOCATEIZE);
if (incomingBuffer != NULL){
incomingBufferFree = OSCPREALLOCATEIZE;
} else {
error = ALLOCFAILED;
incomingBuffer = NULL;
}
incomingBufferSize = 0;
}