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

Committer:
Ibiltari
Date:
Thu Nov 04 12:41:38 2021 +0100
Revision:
19:35408d190f4e
Parent:
16:36d28d8e5491
Update to mbed 6

Who changed what in which revision?

UserRevisionLine numberNew contents of line
Ibiltari 7:98280cef1c4f 1
Ibiltari 7:98280cef1c4f 2 #include "OSCData.h"
Ibiltari 7:98280cef1c4f 3
Ibiltari 7:98280cef1c4f 4
Ibiltari 16:36d28d8e5491 5 osctime_t zerotime = {0,0};
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 16:36d28d8e5491 169 osctime_t OSCData::getTime(){
Ibiltari 16:36d28d8e5491 170 if (type == 't'){
Ibiltari 16:36d28d8e5491 171 return data.time;
Ibiltari 16:36d28d8e5491 172 } else {
Ibiltari 16:36d28d8e5491 173
Ibiltari 16:36d28d8e5491 174 return zerotime;
Ibiltari 16:36d28d8e5491 175 }
Ibiltari 16:36d28d8e5491 176 }
Ibiltari 16:36d28d8e5491 177
Ibiltari 7:98280cef1c4f 178 float OSCData::getFloat(){
Ibiltari 7:98280cef1c4f 179 if (type == 'f'){
Ibiltari 7:98280cef1c4f 180 return data.f;
Ibiltari 7:98280cef1c4f 181 } else {
Ibiltari 7:98280cef1c4f 182 return -1;
Ibiltari 7:98280cef1c4f 183 }
Ibiltari 7:98280cef1c4f 184 }
Ibiltari 7:98280cef1c4f 185
Ibiltari 7:98280cef1c4f 186 double OSCData::getDouble(){
Ibiltari 7:98280cef1c4f 187 if (type == 'd'){
Ibiltari 7:98280cef1c4f 188 return data.d;
Ibiltari 7:98280cef1c4f 189 } else {
Ibiltari 7:98280cef1c4f 190 return -1;
Ibiltari 7:98280cef1c4f 191 }
Ibiltari 7:98280cef1c4f 192 }
Ibiltari 7:98280cef1c4f 193 bool OSCData::getBoolean(){
Ibiltari 7:98280cef1c4f 194 if (type == 'T'){
Ibiltari 7:98280cef1c4f 195 return true;
Ibiltari 7:98280cef1c4f 196 } else if (type=='F'){
Ibiltari 7:98280cef1c4f 197 return false;
Ibiltari 10:936c3afce828 198 }else{
Ibiltari 10:936c3afce828 199 return -1;
Ibiltari 7:98280cef1c4f 200 }
Ibiltari 10:936c3afce828 201
Ibiltari 10:936c3afce828 202
Ibiltari 7:98280cef1c4f 203 }
Ibiltari 7:98280cef1c4f 204
Ibiltari 7:98280cef1c4f 205
Ibiltari 7:98280cef1c4f 206 // no-safety-check straightforward way to fill the passed buffer
Ibiltari 7:98280cef1c4f 207 // with the received string
Ibiltari 7:98280cef1c4f 208 int OSCData::getString(char * strBuffer){
Ibiltari 7:98280cef1c4f 209 if (type == 's'){
Ibiltari 7:98280cef1c4f 210 strncpy(strBuffer, data.s, bytes);
Ibiltari 7:98280cef1c4f 211 return bytes;
Ibiltari 7:98280cef1c4f 212 } else {
Ibiltari 7:98280cef1c4f 213 return -1;
Ibiltari 7:98280cef1c4f 214 }
Ibiltari 7:98280cef1c4f 215 }
Ibiltari 7:98280cef1c4f 216
Ibiltari 7:98280cef1c4f 217 // it's possible to pass strBuffer's size as argument (length)
Ibiltari 7:98280cef1c4f 218 // in order to check that it won't be overflown
Ibiltari 7:98280cef1c4f 219 int OSCData::getString(char * strBuffer, int length){
Ibiltari 7:98280cef1c4f 220 if (type == 's' && bytes <= length){
Ibiltari 7:98280cef1c4f 221 strncpy(strBuffer, data.s, bytes);
Ibiltari 7:98280cef1c4f 222 return bytes;
Ibiltari 7:98280cef1c4f 223 } else {
Ibiltari 7:98280cef1c4f 224 return -1;
Ibiltari 7:98280cef1c4f 225 }
Ibiltari 7:98280cef1c4f 226 }
Ibiltari 7:98280cef1c4f 227
Ibiltari 7:98280cef1c4f 228 // Here we can get only a part of the blob
Ibiltari 7:98280cef1c4f 229 int OSCData::getString(char * strBuffer, int length, int offset, int size){
Ibiltari 7:98280cef1c4f 230 if (type == 's' && size <= bytes && size <= length){
Ibiltari 7:98280cef1c4f 231 strncpy(strBuffer, data.s + offset, size);
Ibiltari 7:98280cef1c4f 232 return size;
Ibiltari 7:98280cef1c4f 233 } else {
Ibiltari 7:98280cef1c4f 234 return -1;
Ibiltari 7:98280cef1c4f 235 }
Ibiltari 7:98280cef1c4f 236 }
Ibiltari 10:936c3afce828 237
Ibiltari 7:98280cef1c4f 238 // no-safety-check straightforward way to fill the passed buffer
Ibiltari 7:98280cef1c4f 239 // with the contents of the received blob
Ibiltari 7:98280cef1c4f 240 int OSCData::getBlob(uint8_t * blobBuffer){
Ibiltari 7:98280cef1c4f 241 // read the blob length
Ibiltari 7:98280cef1c4f 242 int blobLength = getBlobLength();
Ibiltari 7:98280cef1c4f 243
Ibiltari 7:98280cef1c4f 244 if (type == 'b'){
Ibiltari 7:98280cef1c4f 245 memcpy(blobBuffer, data.b + 4, blobLength);
Ibiltari 7:98280cef1c4f 246 return blobLength;
Ibiltari 7:98280cef1c4f 247 } else {
Ibiltari 7:98280cef1c4f 248 return -1;
Ibiltari 7:98280cef1c4f 249 }
Ibiltari 7:98280cef1c4f 250 }
Ibiltari 7:98280cef1c4f 251
Ibiltari 7:98280cef1c4f 252 // it's possible to pass blobBuffer's size as argument (length)
Ibiltari 7:98280cef1c4f 253 // in order to check that it won't be overflown
Ibiltari 7:98280cef1c4f 254 int OSCData::getBlob(uint8_t * blobBuffer, int length){
Ibiltari 7:98280cef1c4f 255 //jump over the first 4 bytes which encode the length
Ibiltari 7:98280cef1c4f 256 int blobLength = bytes-4;
Ibiltari 7:98280cef1c4f 257 if (type == 'b' && blobLength <= length){
Ibiltari 7:98280cef1c4f 258 memcpy(blobBuffer, data.b + 4, blobLength);
Ibiltari 7:98280cef1c4f 259 return blobLength;
Ibiltari 7:98280cef1c4f 260 } else {
Ibiltari 7:98280cef1c4f 261 return -1;
Ibiltari 7:98280cef1c4f 262 }
Ibiltari 7:98280cef1c4f 263 }
Ibiltari 7:98280cef1c4f 264
Ibiltari 7:98280cef1c4f 265 // Here we can get only a part of the blob
Ibiltari 7:98280cef1c4f 266 int OSCData::getBlob(uint8_t * blobBuffer, int length, int offset, int size){
Ibiltari 7:98280cef1c4f 267 //jump over the first 4 bytes which encode the length
Ibiltari 7:98280cef1c4f 268 int blobLength = bytes-4;
Ibiltari 7:98280cef1c4f 269 if (type == 'b' && size <= blobLength && size <= length){
Ibiltari 7:98280cef1c4f 270 memcpy(blobBuffer, data.b + 4 + offset, size);
Ibiltari 7:98280cef1c4f 271 return size;
Ibiltari 7:98280cef1c4f 272 } else {
Ibiltari 7:98280cef1c4f 273 return -1;
Ibiltari 7:98280cef1c4f 274 }
Ibiltari 7:98280cef1c4f 275 }
Ibiltari 7:98280cef1c4f 276
Ibiltari 7:98280cef1c4f 277
Ibiltari 7:98280cef1c4f 278
Ibiltari 7:98280cef1c4f 279 int OSCData::getBlobLength(){
Ibiltari 7:98280cef1c4f 280 if (type == 'b'){
Ibiltari 7:98280cef1c4f 281 //jump over the first 4 bytes which encode the length
Ibiltari 7:98280cef1c4f 282 return bytes-4;
Ibiltari 7:98280cef1c4f 283 }
Ibiltari 7:98280cef1c4f 284 return -1;
Ibiltari 7:98280cef1c4f 285 }
Ibiltari 10:936c3afce828 286