-
Notifications
You must be signed in to change notification settings - Fork 49
/
OSCMessage.cpp
261 lines (157 loc) · 4.58 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
/*
ArdOSC 2.1 - OSC Library for Arduino.
-------- Lisence -----------------------------------------------------------
ArdOSC
The MIT License
Copyright (c) 2009 - 2011 recotana( http://recotana.com ) All right reserved
*/
#include <stdlib.h>
#include <string.h>
#include <stdarg.h>
#include "OSCcommon/OSCcommon.h"
#include "OSCCommon/OSCMessage.h"
OSCMessage::OSCMessage(void)
{
_oscAddress = NULL;
_oscAdrSize = 0;
_oscAdrAlignmentSize = 0;
_argsNum = 0;
_argsAlignmentSize = 0;
_port = 0;
}
OSCMessage::OSCMessage(const char *_oscAddr)
{
_argsNum = 0;
_argsAlignmentSize = 0;
_port = 0;
setOSCAddress(_oscAddr);
}
OSCMessage::~OSCMessage(void)
{
flush();
}
void OSCMessage::flush(void){
if ( _oscAdrSize == 0 ) return;
free(_oscAddress);
_oscAddress = NULL;
_oscAdrSize = 0;
_oscAdrAlignmentSize = 0;
OSCArg *tmp;
for ( uint8_t i = 0 ; i<_argsNum ; i++ ){
tmp = _args[i];
delete tmp;
}
_argsNum = 0;
_argsAlignmentSize = 0;
_port = 0;
}
void OSCMessage::setAddress(uint8_t *_ipAddr , uint16_t _portNum){
setIpAddress(_ipAddr);
_port = _portNum;
}
void OSCMessage::setIpAddress(uint8_t *_ipAddr){
memcpy( _ip , _ipAddr , 4 );
}
uint8_t* OSCMessage::getIpAddress(void){
return _ip;
}
void OSCMessage::setPortNumber(uint16_t _portNum){
_port = _portNum;
}
uint16_t OSCMessage::getPortNumber(){
return _port;
}
int16_t OSCMessage::setOSCAddress(const char *_address){
_oscAdrSize = (uint16_t)strlen(_address);
if( _oscAdrSize > kMaxOSCAdrCharactor ){
flush();
return -1;
}
if( _oscAddress != NULL ) free(_oscAddress);
_oscAddress = (char*)calloc(1,_oscAdrSize+1);
strcpy( _oscAddress , _address) ;
_oscAdrAlignmentSize = CULC_ALIGNMENT(_oscAdrSize);
return 1;
}
uint16_t OSCMessage::getMessageSize(void){
return _oscAdrAlignmentSize + _typeTagAlignmentSize + _argsAlignmentSize;
}
uint16_t OSCMessage::getArgAlignmentSize(uint8_t _index){
return _args[_index]->_alignmentSize;
}
char *OSCMessage::getOSCAddress(void){
return _oscAddress;
}
int16_t OSCMessage::getArgsNum(void){
return _argsNum;
}
char OSCMessage::getArgTypeTag(int16_t _index) {
if(_index>_argsNum) return -1;
return _args[_index]->_typeTag;
}
void OSCMessage::swap(uint8_t *data1, uint8_t *data2){
data1[0] = data2[3];
data1[1] = data2[2];
data1[2] = data2[1];
data1[3] = data2[0];
}
int16_t OSCMessage::beginMessage(const char *_address){
flush();
return setOSCAddress(_address);
}
int16_t OSCMessage::setArgData(char _type , void *_value , uint8_t _byte,bool _enableAlignment){
_args[_argsNum]= new OSCArg( _type , _value , _byte , _enableAlignment );
uint16_t alignSize = _args[_argsNum]->_alignmentSize;
_argsAlignmentSize += alignSize;
_argsNum++;
_typeTagAlignmentSize = CULC_ALIGNMENT( _argsNum + 1 );
return alignSize;
}
int16_t OSCMessage::addArgInt32(int32_t _value){
if( _argsNum > kMaxAugument ) return -1;
uint8_t tmpValue[4];
uint8_t *data = (uint8_t*)&_value;
swap( tmpValue , data );
setArgData( kTagInt32 , tmpValue , 4 , false );
return 1;
}
int32_t OSCMessage::getArgInt32(int16_t _index){
if ( _index > _argsNum ) return -1;
uint8_t tmpValue[4];
uint8_t *_bin = (uint8_t*)_args[_index]->_argData;
swap( tmpValue , _bin );
return *(int32_t*)tmpValue;
}
#ifdef _USE_FLOAT_
int16_t OSCMessage::addArgFloat(float _value){
if( _argsNum > kMaxAugument ) return -1;
uint8_t tmpValue[4];
uint8_t *data = (uint8_t*)&_value;
swap( tmpValue , data );
setArgData( kTagFloat , tmpValue , 4 , false );
return 1;
}
float OSCMessage::getArgFloat(int16_t _index){
if ( _index > _argsNum ) return -1;
uint8_t tmpValue[4];
uint8_t *_bin = (uint8_t*)_args[_index]->_argData;
swap( tmpValue , _bin );
return *(float*)tmpValue;
}
#endif
#ifdef _USE_STRING_
int16_t OSCMessage::addArgString(const char* _value){
if (_argsNum > kMaxAugument ) return -1;
setArgData( kTagString , (void*)_value , strlen(_value) , true );
return 1;
}
int16_t OSCMessage::getArgString(int16_t _index , char *_rcvstr){
if ( _index > _argsNum ) return -1;
strcpy( _rcvstr , (char*)_args[_index]->_argData );
return 1;
}
int16_t OSCMessage::getArgStringSize(int16_t _index){
if ( _index > _argsNum ) return -1;
return _args[_index]->_dataSize+1;
}
#endif