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
OSCData.cpp@10:936c3afce828, 2018-12-04 (annotated)
- Committer:
- Ibiltari
- Date:
- Tue Dec 04 19:58:14 2018 +0100
- Revision:
- 10:936c3afce828
- Parent:
- 9:e4528f622bcc
- Child:
- 16:36d28d8e5491
String send working, receive needs fix
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
Ibiltari |
7:98280cef1c4f | 1 | |
Ibiltari |
7:98280cef1c4f | 2 | #include "OSCData.h" |
Ibiltari |
7:98280cef1c4f | 3 | |
Ibiltari |
7:98280cef1c4f | 4 | |
Ibiltari |
7:98280cef1c4f | 5 | |
Ibiltari |
7:98280cef1c4f | 6 | /*============================================================================= |
Ibiltari |
7:98280cef1c4f | 7 | CONSTRUCTORS |
Ibiltari |
7:98280cef1c4f | 8 | |
Ibiltari |
7:98280cef1c4f | 9 | overloaded methods for each of the types which will |
Ibiltari |
7:98280cef1c4f | 10 | set the type flag, the size (in bytes), and the data |
Ibiltari |
7:98280cef1c4f | 11 | =============================================================================*/ |
Ibiltari |
7:98280cef1c4f | 12 | |
Ibiltari |
10:936c3afce828 | 13 | |
Ibiltari |
7:98280cef1c4f | 14 | OSCData::OSCData(const char * s){ |
Ibiltari |
7:98280cef1c4f | 15 | error = OSC_OK; |
Ibiltari |
7:98280cef1c4f | 16 | type = 's'; |
Ibiltari |
7:98280cef1c4f | 17 | bytes = (strlen(s) + 1); |
Ibiltari |
7:98280cef1c4f | 18 | //own the data |
Ibiltari |
7:98280cef1c4f | 19 | char * mem = (char *) malloc(bytes); |
Ibiltari |
7:98280cef1c4f | 20 | if (mem == NULL){ |
Ibiltari |
7:98280cef1c4f | 21 | error = ALLOCFAILED; |
Ibiltari |
7:98280cef1c4f | 22 | } else { |
Ibiltari |
7:98280cef1c4f | 23 | strcpy(mem, s); |
Ibiltari |
7:98280cef1c4f | 24 | data.s = mem; |
Ibiltari |
7:98280cef1c4f | 25 | } |
Ibiltari |
7:98280cef1c4f | 26 | } |
Ibiltari |
7:98280cef1c4f | 27 | |
Ibiltari |
10:936c3afce828 | 28 | |
Ibiltari |
7:98280cef1c4f | 29 | |
Ibiltari |
7:98280cef1c4f | 30 | |
Ibiltari |
7:98280cef1c4f | 31 | OSCData::OSCData(int i){ |
Ibiltari |
7:98280cef1c4f | 32 | error = OSC_OK; |
Ibiltari |
7:98280cef1c4f | 33 | type = 'i'; |
Ibiltari |
7:98280cef1c4f | 34 | bytes = 4; |
Ibiltari |
7:98280cef1c4f | 35 | data.i = i; |
Ibiltari |
7:98280cef1c4f | 36 | } |
Ibiltari |
7:98280cef1c4f | 37 | OSCData::OSCData(long int i){ |
Ibiltari |
7:98280cef1c4f | 38 | error = OSC_OK; |
Ibiltari |
7:98280cef1c4f | 39 | type = 'i'; |
Ibiltari |
7:98280cef1c4f | 40 | bytes = 4; |
Ibiltari |
7:98280cef1c4f | 41 | data.i = i; |
Ibiltari |
7:98280cef1c4f | 42 | } |
Ibiltari |
10:936c3afce828 | 43 | |
Ibiltari |
7:98280cef1c4f | 44 | OSCData::OSCData(unsigned int i){ |
Ibiltari |
7:98280cef1c4f | 45 | error = OSC_OK; |
Ibiltari |
7:98280cef1c4f | 46 | type = 'i'; |
Ibiltari |
7:98280cef1c4f | 47 | bytes = 4; |
Ibiltari |
7:98280cef1c4f | 48 | data.i = i; |
Ibiltari |
7:98280cef1c4f | 49 | } |
Ibiltari |
10:936c3afce828 | 50 | |
Ibiltari |
7:98280cef1c4f | 51 | |
Ibiltari |
7:98280cef1c4f | 52 | OSCData::OSCData(float f){ |
Ibiltari |
7:98280cef1c4f | 53 | error = OSC_OK; |
Ibiltari |
7:98280cef1c4f | 54 | type = 'f'; |
Ibiltari |
7:98280cef1c4f | 55 | bytes = 4; |
Ibiltari |
7:98280cef1c4f | 56 | data.f = f; |
Ibiltari |
7:98280cef1c4f | 57 | } |
Ibiltari |
7:98280cef1c4f | 58 | |
Ibiltari |
7:98280cef1c4f | 59 | |
Ibiltari |
7:98280cef1c4f | 60 | OSCData::OSCData(bool b){ |
Ibiltari |
7:98280cef1c4f | 61 | error = OSC_OK; |
Ibiltari |
7:98280cef1c4f | 62 | type = b?'T':'F'; |
Ibiltari |
7:98280cef1c4f | 63 | bytes = 0; |
Ibiltari |
7:98280cef1c4f | 64 | } |
Ibiltari |
7:98280cef1c4f | 65 | |
Ibiltari |
7:98280cef1c4f | 66 | |
Ibiltari |
7:98280cef1c4f | 67 | OSCData::OSCData(double d){ |
Ibiltari |
7:98280cef1c4f | 68 | error = OSC_OK; |
Ibiltari |
7:98280cef1c4f | 69 | bytes = sizeof(double); |
Ibiltari |
7:98280cef1c4f | 70 | //if it's not 8 bytes it's not a true double |
Ibiltari |
7:98280cef1c4f | 71 | if (bytes == 8){ |
Ibiltari |
7:98280cef1c4f | 72 | type = 'd'; |
Ibiltari |
7:98280cef1c4f | 73 | data.d = d; |
Ibiltari |
7:98280cef1c4f | 74 | } else { |
Ibiltari |
7:98280cef1c4f | 75 | type = 'f'; |
Ibiltari |
7:98280cef1c4f | 76 | data.f = d; |
Ibiltari |
7:98280cef1c4f | 77 | } |
Ibiltari |
7:98280cef1c4f | 78 | } |
Ibiltari |
9:e4528f622bcc | 79 | |
Ibiltari |
10:936c3afce828 | 80 | |
Ibiltari |
7:98280cef1c4f | 81 | OSCData::OSCData(uint8_t * b, int len){ |
Ibiltari |
7:98280cef1c4f | 82 | error = OSC_OK; |
Ibiltari |
7:98280cef1c4f | 83 | type = 'b'; |
Ibiltari |
7:98280cef1c4f | 84 | bytes = len + 4; |
Ibiltari |
7:98280cef1c4f | 85 | //add the size to the front of the blob |
Ibiltari |
7:98280cef1c4f | 86 | uint32_t len32 = (uint32_t) len; |
Ibiltari |
7:98280cef1c4f | 87 | //make sure the length is endian-safe |
Ibiltari |
7:98280cef1c4f | 88 | len32 = BigEndian(len32); |
Ibiltari |
7:98280cef1c4f | 89 | uint8_t * lenPtr = (uint8_t *) (& len32); |
Ibiltari |
7:98280cef1c4f | 90 | //own the data |
Ibiltari |
7:98280cef1c4f | 91 | if(bytes>0) |
Ibiltari |
7:98280cef1c4f | 92 | { |
Ibiltari |
7:98280cef1c4f | 93 | |
Ibiltari |
7:98280cef1c4f | 94 | uint8_t * mem = (uint8_t * ) malloc(bytes); |
Ibiltari |
7:98280cef1c4f | 95 | if (mem == NULL){ |
Ibiltari |
7:98280cef1c4f | 96 | error = ALLOCFAILED; |
Ibiltari |
7:98280cef1c4f | 97 | } else { |
Ibiltari |
7:98280cef1c4f | 98 | //copy over the blob length |
Ibiltari |
7:98280cef1c4f | 99 | memcpy(mem, lenPtr, 4); |
Ibiltari |
7:98280cef1c4f | 100 | //copy over the blob data |
Ibiltari |
7:98280cef1c4f | 101 | memcpy(mem + 4, b, len); |
Ibiltari |
7:98280cef1c4f | 102 | data.b = mem; |
Ibiltari |
7:98280cef1c4f | 103 | } |
Ibiltari |
7:98280cef1c4f | 104 | } |
Ibiltari |
7:98280cef1c4f | 105 | else |
Ibiltari |
7:98280cef1c4f | 106 | data.b = 0; |
Ibiltari |
7:98280cef1c4f | 107 | } |
Ibiltari |
10:936c3afce828 | 108 | |
Ibiltari |
7:98280cef1c4f | 109 | OSCData::OSCData (OSCData * datum){ |
Ibiltari |
7:98280cef1c4f | 110 | error = OSC_OK; |
Ibiltari |
7:98280cef1c4f | 111 | type = datum->type; |
Ibiltari |
7:98280cef1c4f | 112 | bytes = datum->bytes; |
Ibiltari |
7:98280cef1c4f | 113 | if ( (type == 'i') || (type == 'f') || (type == 'd') || (type == 't') |
Ibiltari |
7:98280cef1c4f | 114 | || (type == 'h') || (type == 'c') || (type == 'r') || (type == 'm') |
Ibiltari |
7:98280cef1c4f | 115 | ) |
Ibiltari |
7:98280cef1c4f | 116 | { |
Ibiltari |
7:98280cef1c4f | 117 | data = datum->data; |
Ibiltari |
10:936c3afce828 | 118 | } else if ((type == 's') || (type == 'b')){ |
Ibiltari |
7:98280cef1c4f | 119 | //allocate a new piece of memory |
Ibiltari |
7:98280cef1c4f | 120 | uint8_t * mem = (uint8_t * ) malloc(bytes); |
Ibiltari |
7:98280cef1c4f | 121 | if (mem == NULL){ |
Ibiltari |
7:98280cef1c4f | 122 | error = ALLOCFAILED; |
Ibiltari |
7:98280cef1c4f | 123 | } else { |
Ibiltari |
7:98280cef1c4f | 124 | //copy over the blob length |
Ibiltari |
7:98280cef1c4f | 125 | memcpy(mem, datum->data.b, bytes); |
Ibiltari |
7:98280cef1c4f | 126 | data.b = mem; |
Ibiltari |
7:98280cef1c4f | 127 | } |
Ibiltari |
10:936c3afce828 | 128 | } |
Ibiltari |
7:98280cef1c4f | 129 | } |
Ibiltari |
7:98280cef1c4f | 130 | |
Ibiltari |
7:98280cef1c4f | 131 | //DESTRUCTOR |
Ibiltari |
7:98280cef1c4f | 132 | OSCData::~OSCData(){ |
Ibiltari |
10:936c3afce828 | 133 | |
Ibiltari |
7:98280cef1c4f | 134 | //if there are no bytes, there is nothing to free |
Ibiltari |
7:98280cef1c4f | 135 | if (bytes>0){ |
Ibiltari |
7:98280cef1c4f | 136 | //if the data is of type 's' or 'b', need to free that memory |
Ibiltari |
7:98280cef1c4f | 137 | if (type == 's'){ |
Ibiltari |
7:98280cef1c4f | 138 | free(data.s); |
Ibiltari |
7:98280cef1c4f | 139 | }else if( type == 'b'){ |
Ibiltari |
7:98280cef1c4f | 140 | free(data.b); |
Ibiltari |
7:98280cef1c4f | 141 | } |
Ibiltari |
7:98280cef1c4f | 142 | } |
Ibiltari |
10:936c3afce828 | 143 | |
Ibiltari |
7:98280cef1c4f | 144 | } |
Ibiltari |
7:98280cef1c4f | 145 | |
Ibiltari |
7:98280cef1c4f | 146 | //sets just the type as a message placeholder |
Ibiltari |
7:98280cef1c4f | 147 | //no data |
Ibiltari |
7:98280cef1c4f | 148 | OSCData::OSCData(char t){ |
Ibiltari |
7:98280cef1c4f | 149 | error = INVALID_OSC; |
Ibiltari |
7:98280cef1c4f | 150 | type = t; |
Ibiltari |
7:98280cef1c4f | 151 | bytes = 0; |
Ibiltari |
7:98280cef1c4f | 152 | } |
Ibiltari |
7:98280cef1c4f | 153 | |
Ibiltari |
7:98280cef1c4f | 154 | /*============================================================================= |
Ibiltari |
7:98280cef1c4f | 155 | GETTERS |
Ibiltari |
7:98280cef1c4f | 156 | |
Ibiltari |
7:98280cef1c4f | 157 | perform a safety check to make sure the data type matches the request |
Ibiltari |
7:98280cef1c4f | 158 | otherwise returns NULL |
Ibiltari |
7:98280cef1c4f | 159 | =============================================================================*/ |
Ibiltari |
7:98280cef1c4f | 160 | |
Ibiltari |
7:98280cef1c4f | 161 | int32_t OSCData::getInt(){ |
Ibiltari |
7:98280cef1c4f | 162 | if (type == 'i'){ |
Ibiltari |
7:98280cef1c4f | 163 | return data.i; |
Ibiltari |
7:98280cef1c4f | 164 | } else { |
Ibiltari |
7:98280cef1c4f | 165 | return -1; |
Ibiltari |
7:98280cef1c4f | 166 | } |
Ibiltari |
7:98280cef1c4f | 167 | } |
Ibiltari |
7:98280cef1c4f | 168 | |
Ibiltari |
7:98280cef1c4f | 169 | float OSCData::getFloat(){ |
Ibiltari |
7:98280cef1c4f | 170 | if (type == 'f'){ |
Ibiltari |
7:98280cef1c4f | 171 | return data.f; |
Ibiltari |
7:98280cef1c4f | 172 | } else { |
Ibiltari |
7:98280cef1c4f | 173 | return -1; |
Ibiltari |
7:98280cef1c4f | 174 | } |
Ibiltari |
7:98280cef1c4f | 175 | } |
Ibiltari |
7:98280cef1c4f | 176 | |
Ibiltari |
7:98280cef1c4f | 177 | double OSCData::getDouble(){ |
Ibiltari |
7:98280cef1c4f | 178 | if (type == 'd'){ |
Ibiltari |
7:98280cef1c4f | 179 | return data.d; |
Ibiltari |
7:98280cef1c4f | 180 | } else { |
Ibiltari |
7:98280cef1c4f | 181 | return -1; |
Ibiltari |
7:98280cef1c4f | 182 | } |
Ibiltari |
7:98280cef1c4f | 183 | } |
Ibiltari |
7:98280cef1c4f | 184 | bool OSCData::getBoolean(){ |
Ibiltari |
7:98280cef1c4f | 185 | if (type == 'T'){ |
Ibiltari |
7:98280cef1c4f | 186 | return true; |
Ibiltari |
7:98280cef1c4f | 187 | } else if (type=='F'){ |
Ibiltari |
7:98280cef1c4f | 188 | return false; |
Ibiltari |
10:936c3afce828 | 189 | }else{ |
Ibiltari |
10:936c3afce828 | 190 | return -1; |
Ibiltari |
7:98280cef1c4f | 191 | } |
Ibiltari |
10:936c3afce828 | 192 | |
Ibiltari |
10:936c3afce828 | 193 | |
Ibiltari |
7:98280cef1c4f | 194 | } |
Ibiltari |
7:98280cef1c4f | 195 | |
Ibiltari |
7:98280cef1c4f | 196 | |
Ibiltari |
7:98280cef1c4f | 197 | // no-safety-check straightforward way to fill the passed buffer |
Ibiltari |
7:98280cef1c4f | 198 | // with the received string |
Ibiltari |
7:98280cef1c4f | 199 | int OSCData::getString(char * strBuffer){ |
Ibiltari |
7:98280cef1c4f | 200 | if (type == 's'){ |
Ibiltari |
7:98280cef1c4f | 201 | strncpy(strBuffer, data.s, bytes); |
Ibiltari |
7:98280cef1c4f | 202 | return bytes; |
Ibiltari |
7:98280cef1c4f | 203 | } else { |
Ibiltari |
7:98280cef1c4f | 204 | return -1; |
Ibiltari |
7:98280cef1c4f | 205 | } |
Ibiltari |
7:98280cef1c4f | 206 | } |
Ibiltari |
7:98280cef1c4f | 207 | |
Ibiltari |
7:98280cef1c4f | 208 | // it's possible to pass strBuffer's size as argument (length) |
Ibiltari |
7:98280cef1c4f | 209 | // in order to check that it won't be overflown |
Ibiltari |
7:98280cef1c4f | 210 | int OSCData::getString(char * strBuffer, int length){ |
Ibiltari |
7:98280cef1c4f | 211 | if (type == 's' && bytes <= length){ |
Ibiltari |
7:98280cef1c4f | 212 | strncpy(strBuffer, data.s, bytes); |
Ibiltari |
7:98280cef1c4f | 213 | return bytes; |
Ibiltari |
7:98280cef1c4f | 214 | } else { |
Ibiltari |
7:98280cef1c4f | 215 | return -1; |
Ibiltari |
7:98280cef1c4f | 216 | } |
Ibiltari |
7:98280cef1c4f | 217 | } |
Ibiltari |
7:98280cef1c4f | 218 | |
Ibiltari |
7:98280cef1c4f | 219 | // Here we can get only a part of the blob |
Ibiltari |
7:98280cef1c4f | 220 | int OSCData::getString(char * strBuffer, int length, int offset, int size){ |
Ibiltari |
7:98280cef1c4f | 221 | if (type == 's' && size <= bytes && size <= length){ |
Ibiltari |
7:98280cef1c4f | 222 | strncpy(strBuffer, data.s + offset, size); |
Ibiltari |
7:98280cef1c4f | 223 | return size; |
Ibiltari |
7:98280cef1c4f | 224 | } else { |
Ibiltari |
7:98280cef1c4f | 225 | return -1; |
Ibiltari |
7:98280cef1c4f | 226 | } |
Ibiltari |
7:98280cef1c4f | 227 | } |
Ibiltari |
10:936c3afce828 | 228 | |
Ibiltari |
7:98280cef1c4f | 229 | // no-safety-check straightforward way to fill the passed buffer |
Ibiltari |
7:98280cef1c4f | 230 | // with the contents of the received blob |
Ibiltari |
7:98280cef1c4f | 231 | int OSCData::getBlob(uint8_t * blobBuffer){ |
Ibiltari |
7:98280cef1c4f | 232 | // read the blob length |
Ibiltari |
7:98280cef1c4f | 233 | int blobLength = getBlobLength(); |
Ibiltari |
7:98280cef1c4f | 234 | |
Ibiltari |
7:98280cef1c4f | 235 | if (type == 'b'){ |
Ibiltari |
7:98280cef1c4f | 236 | memcpy(blobBuffer, data.b + 4, blobLength); |
Ibiltari |
7:98280cef1c4f | 237 | return blobLength; |
Ibiltari |
7:98280cef1c4f | 238 | } else { |
Ibiltari |
7:98280cef1c4f | 239 | return -1; |
Ibiltari |
7:98280cef1c4f | 240 | } |
Ibiltari |
7:98280cef1c4f | 241 | } |
Ibiltari |
7:98280cef1c4f | 242 | |
Ibiltari |
7:98280cef1c4f | 243 | // it's possible to pass blobBuffer's size as argument (length) |
Ibiltari |
7:98280cef1c4f | 244 | // in order to check that it won't be overflown |
Ibiltari |
7:98280cef1c4f | 245 | int OSCData::getBlob(uint8_t * blobBuffer, int length){ |
Ibiltari |
7:98280cef1c4f | 246 | //jump over the first 4 bytes which encode the length |
Ibiltari |
7:98280cef1c4f | 247 | int blobLength = bytes-4; |
Ibiltari |
7:98280cef1c4f | 248 | if (type == 'b' && blobLength <= length){ |
Ibiltari |
7:98280cef1c4f | 249 | memcpy(blobBuffer, data.b + 4, blobLength); |
Ibiltari |
7:98280cef1c4f | 250 | return blobLength; |
Ibiltari |
7:98280cef1c4f | 251 | } else { |
Ibiltari |
7:98280cef1c4f | 252 | return -1; |
Ibiltari |
7:98280cef1c4f | 253 | } |
Ibiltari |
7:98280cef1c4f | 254 | } |
Ibiltari |
7:98280cef1c4f | 255 | |
Ibiltari |
7:98280cef1c4f | 256 | // Here we can get only a part of the blob |
Ibiltari |
7:98280cef1c4f | 257 | int OSCData::getBlob(uint8_t * blobBuffer, int length, int offset, int size){ |
Ibiltari |
7:98280cef1c4f | 258 | //jump over the first 4 bytes which encode the length |
Ibiltari |
7:98280cef1c4f | 259 | int blobLength = bytes-4; |
Ibiltari |
7:98280cef1c4f | 260 | if (type == 'b' && size <= blobLength && size <= length){ |
Ibiltari |
7:98280cef1c4f | 261 | memcpy(blobBuffer, data.b + 4 + offset, size); |
Ibiltari |
7:98280cef1c4f | 262 | return size; |
Ibiltari |
7:98280cef1c4f | 263 | } else { |
Ibiltari |
7:98280cef1c4f | 264 | return -1; |
Ibiltari |
7:98280cef1c4f | 265 | } |
Ibiltari |
7:98280cef1c4f | 266 | } |
Ibiltari |
7:98280cef1c4f | 267 | |
Ibiltari |
7:98280cef1c4f | 268 | |
Ibiltari |
7:98280cef1c4f | 269 | |
Ibiltari |
7:98280cef1c4f | 270 | int OSCData::getBlobLength(){ |
Ibiltari |
7:98280cef1c4f | 271 | if (type == 'b'){ |
Ibiltari |
7:98280cef1c4f | 272 | //jump over the first 4 bytes which encode the length |
Ibiltari |
7:98280cef1c4f | 273 | return bytes-4; |
Ibiltari |
7:98280cef1c4f | 274 | } |
Ibiltari |
7:98280cef1c4f | 275 | return -1; |
Ibiltari |
7:98280cef1c4f | 276 | } |
Ibiltari |
10:936c3afce828 | 277 |