(uncpomplete) port of the cnmat osc library for Arduino/Teensy. Upstream url: https://github.com/CNMAT/OSC
(uncpomplete) port of the cnmat osc library for Arduino/Teensy. Upstream url: https://github.com/CNMAT/OSC
Compiles, but not Tested in any kind. OSCMessage::send(); and OSCBundle::send() don't work/are commented out due to Platform specifics I first have to figure out.
OSCMessage.cpp@1:18009e3041ea, 2014-05-16 (annotated)
- Committer:
- aspeteRakete
- Date:
- Fri May 16 22:10:49 2014 +0000
- Revision:
- 1:18009e3041ea
- Child:
- 2:61caa2495215
OSCMessage Portiert: ::send(Print &p) derzeit auskommentiert.
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
aspeteRakete | 1:18009e3041ea | 1 | /* |
aspeteRakete | 1:18009e3041ea | 2 | Written by Yotam Mann, The Center for New Music and Audio Technologies, |
aspeteRakete | 1:18009e3041ea | 3 | University of California, Berkeley. Copyright (c) 2012, The Regents of |
aspeteRakete | 1:18009e3041ea | 4 | the University of California (Regents). |
aspeteRakete | 1:18009e3041ea | 5 | |
aspeteRakete | 1:18009e3041ea | 6 | Permission to use, copy, modify, distribute, and distribute modified versions |
aspeteRakete | 1:18009e3041ea | 7 | of this software and its documentation without fee and without a signed |
aspeteRakete | 1:18009e3041ea | 8 | licensing agreement, is hereby granted, provided that the above copyright |
aspeteRakete | 1:18009e3041ea | 9 | notice, this paragraph and the following two paragraphs appear in all copies, |
aspeteRakete | 1:18009e3041ea | 10 | modifications, and distributions. |
aspeteRakete | 1:18009e3041ea | 11 | |
aspeteRakete | 1:18009e3041ea | 12 | IN NO EVENT SHALL REGENTS BE LIABLE TO ANY PARTY FOR DIRECT, INDIRECT, |
aspeteRakete | 1:18009e3041ea | 13 | SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES, INCLUDING LOST PROFITS, ARISING |
aspeteRakete | 1:18009e3041ea | 14 | OUT OF THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION, EVEN IF REGENTS HAS |
aspeteRakete | 1:18009e3041ea | 15 | BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
aspeteRakete | 1:18009e3041ea | 16 | |
aspeteRakete | 1:18009e3041ea | 17 | REGENTS SPECIFICALLY DISCLAIMS ANY WARRANTIES, INCLUDING, BUT NOT LIMITED TO, |
aspeteRakete | 1:18009e3041ea | 18 | THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR |
aspeteRakete | 1:18009e3041ea | 19 | PURPOSE. THE SOFTWARE AND ACCOMPANYING DOCUMENTATION, IF ANY, PROVIDED |
aspeteRakete | 1:18009e3041ea | 20 | HEREUNDER IS PROVIDED "AS IS". REGENTS HAS NO OBLIGATION TO PROVIDE |
aspeteRakete | 1:18009e3041ea | 21 | MAINTENANCE, SUPPORT, UPDATES, ENHANCEMENTS, OR MODIFICATIONS. |
aspeteRakete | 1:18009e3041ea | 22 | |
aspeteRakete | 1:18009e3041ea | 23 | For bug reports and feature requests please email me at yotam@cnmat.berkeley.edu |
aspeteRakete | 1:18009e3041ea | 24 | */ |
aspeteRakete | 1:18009e3041ea | 25 | |
aspeteRakete | 1:18009e3041ea | 26 | #include "OSCMessage.h" |
aspeteRakete | 1:18009e3041ea | 27 | #include "OSCMatch.h" |
aspeteRakete | 1:18009e3041ea | 28 | |
aspeteRakete | 1:18009e3041ea | 29 | /*============================================================================= |
aspeteRakete | 1:18009e3041ea | 30 | CONSTRUCTORS / DESTRUCTOR |
aspeteRakete | 1:18009e3041ea | 31 | =============================================================================*/ |
aspeteRakete | 1:18009e3041ea | 32 | |
aspeteRakete | 1:18009e3041ea | 33 | //constructor with address |
aspeteRakete | 1:18009e3041ea | 34 | OSCMessage::OSCMessage(const char * _address){ |
aspeteRakete | 1:18009e3041ea | 35 | setupMessage(); |
aspeteRakete | 1:18009e3041ea | 36 | setAddress(_address); |
aspeteRakete | 1:18009e3041ea | 37 | } |
aspeteRakete | 1:18009e3041ea | 38 | |
aspeteRakete | 1:18009e3041ea | 39 | //constructor with nothing |
aspeteRakete | 1:18009e3041ea | 40 | //just a placeholder since the message is invalid |
aspeteRakete | 1:18009e3041ea | 41 | OSCMessage::OSCMessage(){ |
aspeteRakete | 1:18009e3041ea | 42 | setupMessage(); |
aspeteRakete | 1:18009e3041ea | 43 | error = INVALID_OSC; |
aspeteRakete | 1:18009e3041ea | 44 | } |
aspeteRakete | 1:18009e3041ea | 45 | |
aspeteRakete | 1:18009e3041ea | 46 | //variable length constructor |
aspeteRakete | 1:18009e3041ea | 47 | //for example OSCMessage msg("/address", "isf", 1, "two", 3.0); |
aspeteRakete | 1:18009e3041ea | 48 | /* |
aspeteRakete | 1:18009e3041ea | 49 | OSCMessage::OSCMessage(const char * _address, char * types, ... ){ |
aspeteRakete | 1:18009e3041ea | 50 | setupMessage(_address); |
aspeteRakete | 1:18009e3041ea | 51 | } |
aspeteRakete | 1:18009e3041ea | 52 | */ |
aspeteRakete | 1:18009e3041ea | 53 | |
aspeteRakete | 1:18009e3041ea | 54 | //sets up a new message |
aspeteRakete | 1:18009e3041ea | 55 | void OSCMessage::setupMessage(){ |
aspeteRakete | 1:18009e3041ea | 56 | address = NULL; |
aspeteRakete | 1:18009e3041ea | 57 | //setup the attributes |
aspeteRakete | 1:18009e3041ea | 58 | dataCount = 0; |
aspeteRakete | 1:18009e3041ea | 59 | error = OSC_OK; |
aspeteRakete | 1:18009e3041ea | 60 | //setup the space for data |
aspeteRakete | 1:18009e3041ea | 61 | data = NULL; |
aspeteRakete | 1:18009e3041ea | 62 | //setup for filling the message |
aspeteRakete | 1:18009e3041ea | 63 | incomingBuffer = NULL; |
aspeteRakete | 1:18009e3041ea | 64 | incomingBufferSize = 0; |
aspeteRakete | 1:18009e3041ea | 65 | incomingBufferFree = 0; |
aspeteRakete | 1:18009e3041ea | 66 | clearIncomingBuffer(); |
aspeteRakete | 1:18009e3041ea | 67 | //set the decode state |
aspeteRakete | 1:18009e3041ea | 68 | decodeState = STANDBY; |
aspeteRakete | 1:18009e3041ea | 69 | } |
aspeteRakete | 1:18009e3041ea | 70 | |
aspeteRakete | 1:18009e3041ea | 71 | //DESTRUCTOR |
aspeteRakete | 1:18009e3041ea | 72 | OSCMessage::~OSCMessage(){ |
aspeteRakete | 1:18009e3041ea | 73 | //free everything that needs to be freed |
aspeteRakete | 1:18009e3041ea | 74 | //free the address |
aspeteRakete | 1:18009e3041ea | 75 | free(address); |
aspeteRakete | 1:18009e3041ea | 76 | //free the data |
aspeteRakete | 1:18009e3041ea | 77 | empty(); |
aspeteRakete | 1:18009e3041ea | 78 | //free the filling buffer |
aspeteRakete | 1:18009e3041ea | 79 | free(incomingBuffer); |
aspeteRakete | 1:18009e3041ea | 80 | } |
aspeteRakete | 1:18009e3041ea | 81 | |
aspeteRakete | 1:18009e3041ea | 82 | void OSCMessage::empty(){ |
aspeteRakete | 1:18009e3041ea | 83 | error = OSC_OK; |
aspeteRakete | 1:18009e3041ea | 84 | //free each of hte data in the array |
aspeteRakete | 1:18009e3041ea | 85 | for (int i = 0; i < dataCount; i++){ |
aspeteRakete | 1:18009e3041ea | 86 | OSCData * datum = getOSCData(i); |
aspeteRakete | 1:18009e3041ea | 87 | //explicitly destruct the data |
aspeteRakete | 1:18009e3041ea | 88 | //datum->~OSCData(); |
aspeteRakete | 1:18009e3041ea | 89 | delete datum; |
aspeteRakete | 1:18009e3041ea | 90 | } |
aspeteRakete | 1:18009e3041ea | 91 | //and free the array |
aspeteRakete | 1:18009e3041ea | 92 | free(data); |
aspeteRakete | 1:18009e3041ea | 93 | data = NULL; |
aspeteRakete | 1:18009e3041ea | 94 | dataCount = 0; |
aspeteRakete | 1:18009e3041ea | 95 | clearIncomingBuffer(); |
aspeteRakete | 1:18009e3041ea | 96 | } |
aspeteRakete | 1:18009e3041ea | 97 | |
aspeteRakete | 1:18009e3041ea | 98 | //COPY |
aspeteRakete | 1:18009e3041ea | 99 | OSCMessage::OSCMessage(OSCMessage * msg){ |
aspeteRakete | 1:18009e3041ea | 100 | //start with a message with the same address |
aspeteRakete | 1:18009e3041ea | 101 | setupMessage(); |
aspeteRakete | 1:18009e3041ea | 102 | setAddress(msg->address); |
aspeteRakete | 1:18009e3041ea | 103 | //add each of the data to the other message |
aspeteRakete | 1:18009e3041ea | 104 | for (int i = 0; i < msg->dataCount; i++){ |
aspeteRakete | 1:18009e3041ea | 105 | add(msg->data[i]); |
aspeteRakete | 1:18009e3041ea | 106 | } |
aspeteRakete | 1:18009e3041ea | 107 | } |
aspeteRakete | 1:18009e3041ea | 108 | |
aspeteRakete | 1:18009e3041ea | 109 | /*============================================================================= |
aspeteRakete | 1:18009e3041ea | 110 | GETTING DATA |
aspeteRakete | 1:18009e3041ea | 111 | =============================================================================*/ |
aspeteRakete | 1:18009e3041ea | 112 | |
aspeteRakete | 1:18009e3041ea | 113 | OSCData * OSCMessage::getOSCData(int position){ |
aspeteRakete | 1:18009e3041ea | 114 | if (position < dataCount){ |
aspeteRakete | 1:18009e3041ea | 115 | OSCData * datum = data[position]; |
aspeteRakete | 1:18009e3041ea | 116 | return datum; |
aspeteRakete | 1:18009e3041ea | 117 | } else { |
aspeteRakete | 1:18009e3041ea | 118 | error = INDEX_OUT_OF_BOUNDS; |
aspeteRakete | 1:18009e3041ea | 119 | return NULL; |
aspeteRakete | 1:18009e3041ea | 120 | } |
aspeteRakete | 1:18009e3041ea | 121 | } |
aspeteRakete | 1:18009e3041ea | 122 | |
aspeteRakete | 1:18009e3041ea | 123 | int32_t OSCMessage::getInt(int position){ |
aspeteRakete | 1:18009e3041ea | 124 | OSCData * datum = getOSCData(position); |
aspeteRakete | 1:18009e3041ea | 125 | if (!hasError()){ |
aspeteRakete | 1:18009e3041ea | 126 | return datum->getInt(); |
aspeteRakete | 1:18009e3041ea | 127 | } else { |
aspeteRakete | 1:18009e3041ea | 128 | return NULL; |
aspeteRakete | 1:18009e3041ea | 129 | } |
aspeteRakete | 1:18009e3041ea | 130 | } |
aspeteRakete | 1:18009e3041ea | 131 | uint64_t OSCMessage::getTime(int position){ |
aspeteRakete | 1:18009e3041ea | 132 | OSCData * datum = getOSCData(position); |
aspeteRakete | 1:18009e3041ea | 133 | if (!hasError()){ |
aspeteRakete | 1:18009e3041ea | 134 | return datum->getTime(); |
aspeteRakete | 1:18009e3041ea | 135 | } else { |
aspeteRakete | 1:18009e3041ea | 136 | return NULL; |
aspeteRakete | 1:18009e3041ea | 137 | } |
aspeteRakete | 1:18009e3041ea | 138 | } |
aspeteRakete | 1:18009e3041ea | 139 | float OSCMessage::getFloat(int position){ |
aspeteRakete | 1:18009e3041ea | 140 | OSCData * datum = getOSCData(position); |
aspeteRakete | 1:18009e3041ea | 141 | if (!hasError()){ |
aspeteRakete | 1:18009e3041ea | 142 | return datum->getFloat(); |
aspeteRakete | 1:18009e3041ea | 143 | } else { |
aspeteRakete | 1:18009e3041ea | 144 | return NULL; |
aspeteRakete | 1:18009e3041ea | 145 | } |
aspeteRakete | 1:18009e3041ea | 146 | } |
aspeteRakete | 1:18009e3041ea | 147 | |
aspeteRakete | 1:18009e3041ea | 148 | double OSCMessage::getDouble(int position){ |
aspeteRakete | 1:18009e3041ea | 149 | OSCData * datum = getOSCData(position); |
aspeteRakete | 1:18009e3041ea | 150 | if (!hasError()){ |
aspeteRakete | 1:18009e3041ea | 151 | return datum->getDouble(); |
aspeteRakete | 1:18009e3041ea | 152 | } else { |
aspeteRakete | 1:18009e3041ea | 153 | return NULL; |
aspeteRakete | 1:18009e3041ea | 154 | } |
aspeteRakete | 1:18009e3041ea | 155 | } |
aspeteRakete | 1:18009e3041ea | 156 | |
aspeteRakete | 1:18009e3041ea | 157 | int OSCMessage::getString(int position, char * buffer, int bufferSize){ |
aspeteRakete | 1:18009e3041ea | 158 | OSCData * datum = getOSCData(position); |
aspeteRakete | 1:18009e3041ea | 159 | if (!hasError()){ |
aspeteRakete | 1:18009e3041ea | 160 | //the number of bytes to copy is the smaller between the buffer size and the datum's byte length |
aspeteRakete | 1:18009e3041ea | 161 | int copyBytes = bufferSize < datum->bytes? bufferSize : datum->bytes; |
aspeteRakete | 1:18009e3041ea | 162 | return datum->getString(buffer, copyBytes); |
aspeteRakete | 1:18009e3041ea | 163 | } else { |
aspeteRakete | 1:18009e3041ea | 164 | return NULL; |
aspeteRakete | 1:18009e3041ea | 165 | } |
aspeteRakete | 1:18009e3041ea | 166 | } |
aspeteRakete | 1:18009e3041ea | 167 | |
aspeteRakete | 1:18009e3041ea | 168 | int OSCMessage::getBlob(int position, uint8_t * buffer, int bufferSize){ |
aspeteRakete | 1:18009e3041ea | 169 | OSCData * datum = getOSCData(position); |
aspeteRakete | 1:18009e3041ea | 170 | if (!hasError()){ |
aspeteRakete | 1:18009e3041ea | 171 | //the number of bytes to copy is the smaller between the buffer size and the datum's byte length |
aspeteRakete | 1:18009e3041ea | 172 | int copyBytes = bufferSize < datum->bytes? bufferSize : datum->bytes; |
aspeteRakete | 1:18009e3041ea | 173 | return datum->getBlob(buffer, copyBytes); |
aspeteRakete | 1:18009e3041ea | 174 | } else { |
aspeteRakete | 1:18009e3041ea | 175 | return NULL; |
aspeteRakete | 1:18009e3041ea | 176 | } |
aspeteRakete | 1:18009e3041ea | 177 | } |
aspeteRakete | 1:18009e3041ea | 178 | |
aspeteRakete | 1:18009e3041ea | 179 | char OSCMessage::getType(int position){ |
aspeteRakete | 1:18009e3041ea | 180 | OSCData * datum = getOSCData(position); |
aspeteRakete | 1:18009e3041ea | 181 | if (!hasError()){ |
aspeteRakete | 1:18009e3041ea | 182 | return datum->type; |
aspeteRakete | 1:18009e3041ea | 183 | } else { |
aspeteRakete | 1:18009e3041ea | 184 | return NULL; |
aspeteRakete | 1:18009e3041ea | 185 | } |
aspeteRakete | 1:18009e3041ea | 186 | } |
aspeteRakete | 1:18009e3041ea | 187 | |
aspeteRakete | 1:18009e3041ea | 188 | int OSCMessage::getDataLength(int position){ |
aspeteRakete | 1:18009e3041ea | 189 | OSCData * datum = getOSCData(position); |
aspeteRakete | 1:18009e3041ea | 190 | if (!hasError()){ |
aspeteRakete | 1:18009e3041ea | 191 | return datum->bytes; |
aspeteRakete | 1:18009e3041ea | 192 | } else { |
aspeteRakete | 1:18009e3041ea | 193 | return 0; |
aspeteRakete | 1:18009e3041ea | 194 | } |
aspeteRakete | 1:18009e3041ea | 195 | } |
aspeteRakete | 1:18009e3041ea | 196 | |
aspeteRakete | 1:18009e3041ea | 197 | /*============================================================================= |
aspeteRakete | 1:18009e3041ea | 198 | TESTING DATA |
aspeteRakete | 1:18009e3041ea | 199 | =============================================================================*/ |
aspeteRakete | 1:18009e3041ea | 200 | |
aspeteRakete | 1:18009e3041ea | 201 | bool OSCMessage::testType(int position, char type){ |
aspeteRakete | 1:18009e3041ea | 202 | OSCData * datum = getOSCData(position); |
aspeteRakete | 1:18009e3041ea | 203 | if (!hasError()){ |
aspeteRakete | 1:18009e3041ea | 204 | return datum->type == type; |
aspeteRakete | 1:18009e3041ea | 205 | } else { |
aspeteRakete | 1:18009e3041ea | 206 | return false; |
aspeteRakete | 1:18009e3041ea | 207 | } |
aspeteRakete | 1:18009e3041ea | 208 | } |
aspeteRakete | 1:18009e3041ea | 209 | |
aspeteRakete | 1:18009e3041ea | 210 | bool OSCMessage::isInt(int position){ |
aspeteRakete | 1:18009e3041ea | 211 | return testType(position, 'i'); |
aspeteRakete | 1:18009e3041ea | 212 | } |
aspeteRakete | 1:18009e3041ea | 213 | |
aspeteRakete | 1:18009e3041ea | 214 | bool OSCMessage::isTime(int position){ |
aspeteRakete | 1:18009e3041ea | 215 | return testType(position, 't'); |
aspeteRakete | 1:18009e3041ea | 216 | } |
aspeteRakete | 1:18009e3041ea | 217 | |
aspeteRakete | 1:18009e3041ea | 218 | |
aspeteRakete | 1:18009e3041ea | 219 | bool OSCMessage::isFloat(int position){ |
aspeteRakete | 1:18009e3041ea | 220 | return testType(position, 'f'); |
aspeteRakete | 1:18009e3041ea | 221 | } |
aspeteRakete | 1:18009e3041ea | 222 | |
aspeteRakete | 1:18009e3041ea | 223 | bool OSCMessage::isBlob(int position){ |
aspeteRakete | 1:18009e3041ea | 224 | return testType(position, 'b'); |
aspeteRakete | 1:18009e3041ea | 225 | } |
aspeteRakete | 1:18009e3041ea | 226 | |
aspeteRakete | 1:18009e3041ea | 227 | bool OSCMessage::isChar(int position){ |
aspeteRakete | 1:18009e3041ea | 228 | return testType(position, 'c'); |
aspeteRakete | 1:18009e3041ea | 229 | } |
aspeteRakete | 1:18009e3041ea | 230 | |
aspeteRakete | 1:18009e3041ea | 231 | bool OSCMessage::isString(int position){ |
aspeteRakete | 1:18009e3041ea | 232 | return testType(position, 's'); |
aspeteRakete | 1:18009e3041ea | 233 | } |
aspeteRakete | 1:18009e3041ea | 234 | |
aspeteRakete | 1:18009e3041ea | 235 | bool OSCMessage::isDouble(int position){ |
aspeteRakete | 1:18009e3041ea | 236 | return testType(position, 'd'); |
aspeteRakete | 1:18009e3041ea | 237 | } |
aspeteRakete | 1:18009e3041ea | 238 | bool OSCMessage::isBoolean(int position){ |
aspeteRakete | 1:18009e3041ea | 239 | return testType(position, 'T') || testType(position, 'F'); |
aspeteRakete | 1:18009e3041ea | 240 | } |
aspeteRakete | 1:18009e3041ea | 241 | |
aspeteRakete | 1:18009e3041ea | 242 | |
aspeteRakete | 1:18009e3041ea | 243 | /*============================================================================= |
aspeteRakete | 1:18009e3041ea | 244 | PATTERN MATCHING |
aspeteRakete | 1:18009e3041ea | 245 | =============================================================================*/ |
aspeteRakete | 1:18009e3041ea | 246 | |
aspeteRakete | 1:18009e3041ea | 247 | int OSCMessage::match(const char * pattern, int addr_offset){ |
aspeteRakete | 1:18009e3041ea | 248 | int pattern_offset; |
aspeteRakete | 1:18009e3041ea | 249 | int address_offset; |
aspeteRakete | 1:18009e3041ea | 250 | int ret = osc_match(address + addr_offset, pattern, &pattern_offset, &address_offset); |
aspeteRakete | 1:18009e3041ea | 251 | char * next = (char *) (address + addr_offset + pattern_offset); |
aspeteRakete | 1:18009e3041ea | 252 | if (ret==3){ |
aspeteRakete | 1:18009e3041ea | 253 | return pattern_offset; |
aspeteRakete | 1:18009e3041ea | 254 | } else if (pattern_offset > 0 && *next == '/'){ |
aspeteRakete | 1:18009e3041ea | 255 | return pattern_offset; |
aspeteRakete | 1:18009e3041ea | 256 | } else { |
aspeteRakete | 1:18009e3041ea | 257 | return 0; |
aspeteRakete | 1:18009e3041ea | 258 | } |
aspeteRakete | 1:18009e3041ea | 259 | } |
aspeteRakete | 1:18009e3041ea | 260 | |
aspeteRakete | 1:18009e3041ea | 261 | bool OSCMessage::fullMatch( const char * pattern, int addr_offset){ |
aspeteRakete | 1:18009e3041ea | 262 | int pattern_offset; |
aspeteRakete | 1:18009e3041ea | 263 | int address_offset; |
aspeteRakete | 1:18009e3041ea | 264 | int ret = osc_match(address + addr_offset, pattern, &address_offset, &pattern_offset); |
aspeteRakete | 1:18009e3041ea | 265 | return (ret==3); |
aspeteRakete | 1:18009e3041ea | 266 | } |
aspeteRakete | 1:18009e3041ea | 267 | |
aspeteRakete | 1:18009e3041ea | 268 | bool OSCMessage::dispatch(const char * pattern, void (*callback)(OSCMessage &), int addr_offset){ |
aspeteRakete | 1:18009e3041ea | 269 | if (fullMatch(pattern, addr_offset)){ |
aspeteRakete | 1:18009e3041ea | 270 | callback(*this); |
aspeteRakete | 1:18009e3041ea | 271 | return true; |
aspeteRakete | 1:18009e3041ea | 272 | } else { |
aspeteRakete | 1:18009e3041ea | 273 | return false; |
aspeteRakete | 1:18009e3041ea | 274 | } |
aspeteRakete | 1:18009e3041ea | 275 | } |
aspeteRakete | 1:18009e3041ea | 276 | |
aspeteRakete | 1:18009e3041ea | 277 | bool OSCMessage::route(const char * pattern, void (*callback)(OSCMessage &, int), int initial_offset){ |
aspeteRakete | 1:18009e3041ea | 278 | int match_offset = match(pattern, initial_offset); |
aspeteRakete | 1:18009e3041ea | 279 | if (match_offset>0){ |
aspeteRakete | 1:18009e3041ea | 280 | callback(*this, match_offset + initial_offset); |
aspeteRakete | 1:18009e3041ea | 281 | return true; |
aspeteRakete | 1:18009e3041ea | 282 | } else { |
aspeteRakete | 1:18009e3041ea | 283 | return false; |
aspeteRakete | 1:18009e3041ea | 284 | } |
aspeteRakete | 1:18009e3041ea | 285 | } |
aspeteRakete | 1:18009e3041ea | 286 | |
aspeteRakete | 1:18009e3041ea | 287 | /*============================================================================= |
aspeteRakete | 1:18009e3041ea | 288 | ADDRESS |
aspeteRakete | 1:18009e3041ea | 289 | =============================================================================*/ |
aspeteRakete | 1:18009e3041ea | 290 | |
aspeteRakete | 1:18009e3041ea | 291 | int OSCMessage::getAddress(char * buffer, int offset){ |
aspeteRakete | 1:18009e3041ea | 292 | strcpy(buffer, address+offset); |
aspeteRakete | 1:18009e3041ea | 293 | return strlen(buffer); |
aspeteRakete | 1:18009e3041ea | 294 | } |
aspeteRakete | 1:18009e3041ea | 295 | |
aspeteRakete | 1:18009e3041ea | 296 | int OSCMessage::getAddress(char * buffer, int offset, int len){ |
aspeteRakete | 1:18009e3041ea | 297 | strncpy(buffer, address+offset, len); |
aspeteRakete | 1:18009e3041ea | 298 | return strlen(buffer); |
aspeteRakete | 1:18009e3041ea | 299 | } |
aspeteRakete | 1:18009e3041ea | 300 | |
aspeteRakete | 1:18009e3041ea | 301 | void OSCMessage::setAddress(const char * _address){ |
aspeteRakete | 1:18009e3041ea | 302 | //free the previous address |
aspeteRakete | 1:18009e3041ea | 303 | free(address); // are we sure address was allocated? |
aspeteRakete | 1:18009e3041ea | 304 | //copy the address |
aspeteRakete | 1:18009e3041ea | 305 | char * addressMemory = (char *) malloc( (strlen(_address) + 1) * sizeof(char) ); |
aspeteRakete | 1:18009e3041ea | 306 | if (addressMemory == NULL){ |
aspeteRakete | 1:18009e3041ea | 307 | error = ALLOCFAILED; |
aspeteRakete | 1:18009e3041ea | 308 | address = NULL; |
aspeteRakete | 1:18009e3041ea | 309 | } else { |
aspeteRakete | 1:18009e3041ea | 310 | strcpy(addressMemory, _address); |
aspeteRakete | 1:18009e3041ea | 311 | address = addressMemory; |
aspeteRakete | 1:18009e3041ea | 312 | } |
aspeteRakete | 1:18009e3041ea | 313 | } |
aspeteRakete | 1:18009e3041ea | 314 | |
aspeteRakete | 1:18009e3041ea | 315 | /*============================================================================= |
aspeteRakete | 1:18009e3041ea | 316 | SIZE |
aspeteRakete | 1:18009e3041ea | 317 | =============================================================================*/ |
aspeteRakete | 1:18009e3041ea | 318 | |
aspeteRakete | 1:18009e3041ea | 319 | #ifdef SLOWpadcalculation |
aspeteRakete | 1:18009e3041ea | 320 | int OSCMessage::padSize(int _bytes){ |
aspeteRakete | 1:18009e3041ea | 321 | int space = (_bytes + 3) / 4; |
aspeteRakete | 1:18009e3041ea | 322 | space *= 4; |
aspeteRakete | 1:18009e3041ea | 323 | return space - _bytes; |
aspeteRakete | 1:18009e3041ea | 324 | } |
aspeteRakete | 1:18009e3041ea | 325 | #else |
aspeteRakete | 1:18009e3041ea | 326 | static inline int padSize(int bytes) { return (4- (bytes&03))&3; } |
aspeteRakete | 1:18009e3041ea | 327 | #endif |
aspeteRakete | 1:18009e3041ea | 328 | //returns the number of OSCData in the OSCMessage |
aspeteRakete | 1:18009e3041ea | 329 | int OSCMessage::size(){ |
aspeteRakete | 1:18009e3041ea | 330 | return dataCount; |
aspeteRakete | 1:18009e3041ea | 331 | } |
aspeteRakete | 1:18009e3041ea | 332 | |
aspeteRakete | 1:18009e3041ea | 333 | int OSCMessage::bytes(){ |
aspeteRakete | 1:18009e3041ea | 334 | int messageSize = 0; |
aspeteRakete | 1:18009e3041ea | 335 | //send the address |
aspeteRakete | 1:18009e3041ea | 336 | int addrLen = strlen(address) + 1; |
aspeteRakete | 1:18009e3041ea | 337 | messageSize += addrLen; |
aspeteRakete | 1:18009e3041ea | 338 | //padding amount |
aspeteRakete | 1:18009e3041ea | 339 | int addrPad = padSize(addrLen); |
aspeteRakete | 1:18009e3041ea | 340 | messageSize += addrPad; |
aspeteRakete | 1:18009e3041ea | 341 | //add the comma seperator |
aspeteRakete | 1:18009e3041ea | 342 | messageSize += 1; |
aspeteRakete | 1:18009e3041ea | 343 | //add the types |
aspeteRakete | 1:18009e3041ea | 344 | messageSize += dataCount; |
aspeteRakete | 1:18009e3041ea | 345 | //pad the types |
aspeteRakete | 1:18009e3041ea | 346 | int typePad = padSize(dataCount + 1); //for the comma |
aspeteRakete | 1:18009e3041ea | 347 | if (typePad == 0){ |
aspeteRakete | 1:18009e3041ea | 348 | typePad = 4; // to make sure the type string is null terminated |
aspeteRakete | 1:18009e3041ea | 349 | } |
aspeteRakete | 1:18009e3041ea | 350 | messageSize+=typePad; |
aspeteRakete | 1:18009e3041ea | 351 | //then the data |
aspeteRakete | 1:18009e3041ea | 352 | for (int i = 0; i < dataCount; i++){ |
aspeteRakete | 1:18009e3041ea | 353 | OSCData * datum = getOSCData(i); |
aspeteRakete | 1:18009e3041ea | 354 | messageSize+=datum->bytes; |
aspeteRakete | 1:18009e3041ea | 355 | messageSize += padSize(datum->bytes); |
aspeteRakete | 1:18009e3041ea | 356 | } |
aspeteRakete | 1:18009e3041ea | 357 | return messageSize; |
aspeteRakete | 1:18009e3041ea | 358 | } |
aspeteRakete | 1:18009e3041ea | 359 | |
aspeteRakete | 1:18009e3041ea | 360 | /*============================================================================= |
aspeteRakete | 1:18009e3041ea | 361 | ERROR HANDLING |
aspeteRakete | 1:18009e3041ea | 362 | =============================================================================*/ |
aspeteRakete | 1:18009e3041ea | 363 | |
aspeteRakete | 1:18009e3041ea | 364 | bool OSCMessage::hasError(){ |
aspeteRakete | 1:18009e3041ea | 365 | bool retError = error != OSC_OK; |
aspeteRakete | 1:18009e3041ea | 366 | //test each of the data |
aspeteRakete | 1:18009e3041ea | 367 | for (int i = 0; i < dataCount; i++){ |
aspeteRakete | 1:18009e3041ea | 368 | OSCData * datum = getOSCData(i); |
aspeteRakete | 1:18009e3041ea | 369 | retError |= datum->error != OSC_OK; |
aspeteRakete | 1:18009e3041ea | 370 | } |
aspeteRakete | 1:18009e3041ea | 371 | return retError; |
aspeteRakete | 1:18009e3041ea | 372 | } |
aspeteRakete | 1:18009e3041ea | 373 | |
aspeteRakete | 1:18009e3041ea | 374 | OSCErrorCode OSCMessage::getError(){ |
aspeteRakete | 1:18009e3041ea | 375 | return error; |
aspeteRakete | 1:18009e3041ea | 376 | } |
aspeteRakete | 1:18009e3041ea | 377 | |
aspeteRakete | 1:18009e3041ea | 378 | /*============================================================================= |
aspeteRakete | 1:18009e3041ea | 379 | SENDING |
aspeteRakete | 1:18009e3041ea | 380 | =============================================================================*/ |
aspeteRakete | 1:18009e3041ea | 381 | |
aspeteRakete | 1:18009e3041ea | 382 | /* |
aspeteRakete | 1:18009e3041ea | 383 | void OSCMessage::send(Print &p){ |
aspeteRakete | 1:18009e3041ea | 384 | //don't send a message with errors |
aspeteRakete | 1:18009e3041ea | 385 | if (hasError()){ |
aspeteRakete | 1:18009e3041ea | 386 | return; |
aspeteRakete | 1:18009e3041ea | 387 | } |
aspeteRakete | 1:18009e3041ea | 388 | uint8_t nullChar = '\0'; |
aspeteRakete | 1:18009e3041ea | 389 | //send the address |
aspeteRakete | 1:18009e3041ea | 390 | int addrLen = strlen(address) + 1; |
aspeteRakete | 1:18009e3041ea | 391 | //padding amount |
aspeteRakete | 1:18009e3041ea | 392 | int addrPad = padSize(addrLen); |
aspeteRakete | 1:18009e3041ea | 393 | //write it to the stream |
aspeteRakete | 1:18009e3041ea | 394 | p.write((uint8_t *) address, addrLen); |
aspeteRakete | 1:18009e3041ea | 395 | //add the padding |
aspeteRakete | 1:18009e3041ea | 396 | while(addrPad--){ |
aspeteRakete | 1:18009e3041ea | 397 | p.write(nullChar); |
aspeteRakete | 1:18009e3041ea | 398 | } |
aspeteRakete | 1:18009e3041ea | 399 | //add the comma seperator |
aspeteRakete | 1:18009e3041ea | 400 | p.write((uint8_t) ','); |
aspeteRakete | 1:18009e3041ea | 401 | //add the types |
aspeteRakete | 1:18009e3041ea | 402 | #ifdef PAULSSUGGESTION |
aspeteRakete | 1:18009e3041ea | 403 | // Paul suggested buffering on the stack |
aspeteRakete | 1:18009e3041ea | 404 | // to improve performance. The problem is this could exhaust the stack |
aspeteRakete | 1:18009e3041ea | 405 | // for long complex messages |
aspeteRakete | 1:18009e3041ea | 406 | { |
aspeteRakete | 1:18009e3041ea | 407 | uint8_t typstr[dataCount]; |
aspeteRakete | 1:18009e3041ea | 408 | |
aspeteRakete | 1:18009e3041ea | 409 | for (int i = 0; i < dataCount; i++){ |
aspeteRakete | 1:18009e3041ea | 410 | typstr[i] = getType(i); |
aspeteRakete | 1:18009e3041ea | 411 | } |
aspeteRakete | 1:18009e3041ea | 412 | p.write(typstr,dataCount); |
aspeteRakete | 1:18009e3041ea | 413 | } |
aspeteRakete | 1:18009e3041ea | 414 | #else |
aspeteRakete | 1:18009e3041ea | 415 | for (int i = 0; i < dataCount; i++){ |
aspeteRakete | 1:18009e3041ea | 416 | p.write((uint8_t) getType(i)); |
aspeteRakete | 1:18009e3041ea | 417 | } |
aspeteRakete | 1:18009e3041ea | 418 | #endif |
aspeteRakete | 1:18009e3041ea | 419 | //pad the types |
aspeteRakete | 1:18009e3041ea | 420 | int typePad = padSize(dataCount + 1); // 1 is for the comma |
aspeteRakete | 1:18009e3041ea | 421 | if (typePad == 0){ |
aspeteRakete | 1:18009e3041ea | 422 | typePad = 4; // This is because the type string has to be null terminated |
aspeteRakete | 1:18009e3041ea | 423 | } |
aspeteRakete | 1:18009e3041ea | 424 | while(typePad--){ |
aspeteRakete | 1:18009e3041ea | 425 | p.write(nullChar); |
aspeteRakete | 1:18009e3041ea | 426 | } |
aspeteRakete | 1:18009e3041ea | 427 | //write the data |
aspeteRakete | 1:18009e3041ea | 428 | for (int i = 0; i < dataCount; i++){ |
aspeteRakete | 1:18009e3041ea | 429 | OSCData * datum = getOSCData(i); |
aspeteRakete | 1:18009e3041ea | 430 | if ((datum->type == 's') || (datum->type == 'b')){ |
aspeteRakete | 1:18009e3041ea | 431 | p.write(datum->data.b, datum->bytes); |
aspeteRakete | 1:18009e3041ea | 432 | int dataPad = padSize(datum->bytes); |
aspeteRakete | 1:18009e3041ea | 433 | while(dataPad--){ |
aspeteRakete | 1:18009e3041ea | 434 | p.write(nullChar); |
aspeteRakete | 1:18009e3041ea | 435 | } |
aspeteRakete | 1:18009e3041ea | 436 | } else if(datum->type == 'b'){ |
aspeteRakete | 1:18009e3041ea | 437 | p.write(datum->data.b, datum->bytes); |
aspeteRakete | 1:18009e3041ea | 438 | int dataPad = padSize(datum->bytes); |
aspeteRakete | 1:18009e3041ea | 439 | while(dataPad--){ |
aspeteRakete | 1:18009e3041ea | 440 | p.write(nullChar); |
aspeteRakete | 1:18009e3041ea | 441 | } |
aspeteRakete | 1:18009e3041ea | 442 | } else if (datum->type == 'd'){ |
aspeteRakete | 1:18009e3041ea | 443 | double d = BigEndian(datum->data.d); |
aspeteRakete | 1:18009e3041ea | 444 | uint8_t * ptr = (uint8_t *) &d; |
aspeteRakete | 1:18009e3041ea | 445 | p.write(ptr, 8); |
aspeteRakete | 1:18009e3041ea | 446 | } else if (datum->type == 't'){ |
aspeteRakete | 1:18009e3041ea | 447 | uint64_t d = BigEndian(datum->data.l); |
aspeteRakete | 1:18009e3041ea | 448 | uint8_t * ptr = (uint8_t *) &d; |
aspeteRakete | 1:18009e3041ea | 449 | p.write(ptr, 8); |
aspeteRakete | 1:18009e3041ea | 450 | |
aspeteRakete | 1:18009e3041ea | 451 | } else if (datum->type == 'T' || datum->type == 'F') |
aspeteRakete | 1:18009e3041ea | 452 | { } |
aspeteRakete | 1:18009e3041ea | 453 | else { // float or int |
aspeteRakete | 1:18009e3041ea | 454 | uint32_t i = BigEndian(datum->data.i); |
aspeteRakete | 1:18009e3041ea | 455 | uint8_t * ptr = (uint8_t *) &i; |
aspeteRakete | 1:18009e3041ea | 456 | p.write(ptr, datum->bytes); |
aspeteRakete | 1:18009e3041ea | 457 | } |
aspeteRakete | 1:18009e3041ea | 458 | } |
aspeteRakete | 1:18009e3041ea | 459 | }*/ |
aspeteRakete | 1:18009e3041ea | 460 | |
aspeteRakete | 1:18009e3041ea | 461 | /*============================================================================= |
aspeteRakete | 1:18009e3041ea | 462 | FILLING |
aspeteRakete | 1:18009e3041ea | 463 | =============================================================================*/ |
aspeteRakete | 1:18009e3041ea | 464 | |
aspeteRakete | 1:18009e3041ea | 465 | void OSCMessage::fill(uint8_t incomingByte){ |
aspeteRakete | 1:18009e3041ea | 466 | decode(incomingByte); |
aspeteRakete | 1:18009e3041ea | 467 | } |
aspeteRakete | 1:18009e3041ea | 468 | |
aspeteRakete | 1:18009e3041ea | 469 | void OSCMessage::fill(uint8_t * incomingBytes, int length){ |
aspeteRakete | 1:18009e3041ea | 470 | while (length--){ |
aspeteRakete | 1:18009e3041ea | 471 | decode(*incomingBytes++); |
aspeteRakete | 1:18009e3041ea | 472 | } |
aspeteRakete | 1:18009e3041ea | 473 | } |
aspeteRakete | 1:18009e3041ea | 474 | |
aspeteRakete | 1:18009e3041ea | 475 | /*============================================================================= |
aspeteRakete | 1:18009e3041ea | 476 | DECODING |
aspeteRakete | 1:18009e3041ea | 477 | =============================================================================*/ |
aspeteRakete | 1:18009e3041ea | 478 | |
aspeteRakete | 1:18009e3041ea | 479 | void OSCMessage::decodeAddress(){ |
aspeteRakete | 1:18009e3041ea | 480 | setAddress((char *) incomingBuffer); |
aspeteRakete | 1:18009e3041ea | 481 | //change the error from invalide message |
aspeteRakete | 1:18009e3041ea | 482 | error = OSC_OK; |
aspeteRakete | 1:18009e3041ea | 483 | clearIncomingBuffer(); |
aspeteRakete | 1:18009e3041ea | 484 | } |
aspeteRakete | 1:18009e3041ea | 485 | |
aspeteRakete | 1:18009e3041ea | 486 | void OSCMessage::decodeType(uint8_t incomingByte){ |
aspeteRakete | 1:18009e3041ea | 487 | char type = incomingByte; |
aspeteRakete | 1:18009e3041ea | 488 | add(type); |
aspeteRakete | 1:18009e3041ea | 489 | } |
aspeteRakete | 1:18009e3041ea | 490 | |
aspeteRakete | 1:18009e3041ea | 491 | void OSCMessage::decodeData(uint8_t incomingByte){ |
aspeteRakete | 1:18009e3041ea | 492 | //get the first OSCData to re-set |
aspeteRakete | 1:18009e3041ea | 493 | for (int i = 0; i < dataCount; i++){ |
aspeteRakete | 1:18009e3041ea | 494 | OSCData * datum = getOSCData(i); |
aspeteRakete | 1:18009e3041ea | 495 | if (datum->error == INVALID_OSC){ |
aspeteRakete | 1:18009e3041ea | 496 | //set the contents of datum with the data received |
aspeteRakete | 1:18009e3041ea | 497 | switch (datum->type){ |
aspeteRakete | 1:18009e3041ea | 498 | case 'i': |
aspeteRakete | 1:18009e3041ea | 499 | if (incomingBufferSize == 4){ |
aspeteRakete | 1:18009e3041ea | 500 | //parse the buffer as an int |
aspeteRakete | 1:18009e3041ea | 501 | union { |
aspeteRakete | 1:18009e3041ea | 502 | int32_t i; |
aspeteRakete | 1:18009e3041ea | 503 | uint8_t b[4]; |
aspeteRakete | 1:18009e3041ea | 504 | } u; |
aspeteRakete | 1:18009e3041ea | 505 | memcpy(u.b, incomingBuffer, 4); |
aspeteRakete | 1:18009e3041ea | 506 | int32_t dataVal = BigEndian(u.i); |
aspeteRakete | 1:18009e3041ea | 507 | set(i, dataVal); |
aspeteRakete | 1:18009e3041ea | 508 | clearIncomingBuffer(); |
aspeteRakete | 1:18009e3041ea | 509 | } |
aspeteRakete | 1:18009e3041ea | 510 | break; |
aspeteRakete | 1:18009e3041ea | 511 | case 'f': |
aspeteRakete | 1:18009e3041ea | 512 | if (incomingBufferSize == 4){ |
aspeteRakete | 1:18009e3041ea | 513 | //parse the buffer as an int |
aspeteRakete | 1:18009e3041ea | 514 | union { |
aspeteRakete | 1:18009e3041ea | 515 | float f; |
aspeteRakete | 1:18009e3041ea | 516 | uint8_t b[4]; |
aspeteRakete | 1:18009e3041ea | 517 | } u; |
aspeteRakete | 1:18009e3041ea | 518 | memcpy(u.b, incomingBuffer, 4); |
aspeteRakete | 1:18009e3041ea | 519 | float dataVal = BigEndian(u.f); |
aspeteRakete | 1:18009e3041ea | 520 | set(i, dataVal); |
aspeteRakete | 1:18009e3041ea | 521 | clearIncomingBuffer(); |
aspeteRakete | 1:18009e3041ea | 522 | } |
aspeteRakete | 1:18009e3041ea | 523 | break; |
aspeteRakete | 1:18009e3041ea | 524 | case 'd': |
aspeteRakete | 1:18009e3041ea | 525 | if (incomingBufferSize == 8){ |
aspeteRakete | 1:18009e3041ea | 526 | //parse the buffer as an int |
aspeteRakete | 1:18009e3041ea | 527 | union { |
aspeteRakete | 1:18009e3041ea | 528 | double d; |
aspeteRakete | 1:18009e3041ea | 529 | uint8_t b[8]; |
aspeteRakete | 1:18009e3041ea | 530 | } u; |
aspeteRakete | 1:18009e3041ea | 531 | memcpy(u.b, incomingBuffer, 8); |
aspeteRakete | 1:18009e3041ea | 532 | double dataVal = BigEndian(u.d); |
aspeteRakete | 1:18009e3041ea | 533 | set(i, dataVal); |
aspeteRakete | 1:18009e3041ea | 534 | clearIncomingBuffer(); |
aspeteRakete | 1:18009e3041ea | 535 | } |
aspeteRakete | 1:18009e3041ea | 536 | break; |
aspeteRakete | 1:18009e3041ea | 537 | case 't': |
aspeteRakete | 1:18009e3041ea | 538 | if (incomingBufferSize == 8){ |
aspeteRakete | 1:18009e3041ea | 539 | //parse the buffer as an int |
aspeteRakete | 1:18009e3041ea | 540 | union { |
aspeteRakete | 1:18009e3041ea | 541 | uint64_t d; |
aspeteRakete | 1:18009e3041ea | 542 | uint8_t b[8]; |
aspeteRakete | 1:18009e3041ea | 543 | } u; |
aspeteRakete | 1:18009e3041ea | 544 | memcpy(u.b, incomingBuffer, 8); |
aspeteRakete | 1:18009e3041ea | 545 | uint64_t dataVal = BigEndian(u.d); |
aspeteRakete | 1:18009e3041ea | 546 | set(i, dataVal); |
aspeteRakete | 1:18009e3041ea | 547 | clearIncomingBuffer(); |
aspeteRakete | 1:18009e3041ea | 548 | } |
aspeteRakete | 1:18009e3041ea | 549 | break; |
aspeteRakete | 1:18009e3041ea | 550 | |
aspeteRakete | 1:18009e3041ea | 551 | case 's': |
aspeteRakete | 1:18009e3041ea | 552 | if (incomingByte == 0){ |
aspeteRakete | 1:18009e3041ea | 553 | char * str = (char *) incomingBuffer; |
aspeteRakete | 1:18009e3041ea | 554 | set(i, str); |
aspeteRakete | 1:18009e3041ea | 555 | clearIncomingBuffer(); |
aspeteRakete | 1:18009e3041ea | 556 | decodeState = DATA_PADDING; |
aspeteRakete | 1:18009e3041ea | 557 | } |
aspeteRakete | 1:18009e3041ea | 558 | break; |
aspeteRakete | 1:18009e3041ea | 559 | case 'b': |
aspeteRakete | 1:18009e3041ea | 560 | if (incomingBufferSize > 4){ |
aspeteRakete | 1:18009e3041ea | 561 | //compute the expected blob size |
aspeteRakete | 1:18009e3041ea | 562 | union { |
aspeteRakete | 1:18009e3041ea | 563 | uint32_t i; |
aspeteRakete | 1:18009e3041ea | 564 | uint8_t b[4]; |
aspeteRakete | 1:18009e3041ea | 565 | } u; |
aspeteRakete | 1:18009e3041ea | 566 | memcpy(u.b, incomingBuffer, 4); |
aspeteRakete | 1:18009e3041ea | 567 | uint32_t blobLength = BigEndian(u.i); |
aspeteRakete | 1:18009e3041ea | 568 | if (incomingBufferSize == blobLength + 4){ |
aspeteRakete | 1:18009e3041ea | 569 | set(i, incomingBuffer + 4, blobLength); |
aspeteRakete | 1:18009e3041ea | 570 | clearIncomingBuffer(); |
aspeteRakete | 1:18009e3041ea | 571 | decodeState = DATA_PADDING; |
aspeteRakete | 1:18009e3041ea | 572 | } |
aspeteRakete | 1:18009e3041ea | 573 | |
aspeteRakete | 1:18009e3041ea | 574 | } |
aspeteRakete | 1:18009e3041ea | 575 | break; |
aspeteRakete | 1:18009e3041ea | 576 | } |
aspeteRakete | 1:18009e3041ea | 577 | //break out of the for loop once we've selected the first invalid message |
aspeteRakete | 1:18009e3041ea | 578 | break; |
aspeteRakete | 1:18009e3041ea | 579 | } |
aspeteRakete | 1:18009e3041ea | 580 | } |
aspeteRakete | 1:18009e3041ea | 581 | } |
aspeteRakete | 1:18009e3041ea | 582 | |
aspeteRakete | 1:18009e3041ea | 583 | //does not validate the incoming OSC for correctness |
aspeteRakete | 1:18009e3041ea | 584 | void OSCMessage::decode(uint8_t incomingByte){ |
aspeteRakete | 1:18009e3041ea | 585 | addToIncomingBuffer(incomingByte); |
aspeteRakete | 1:18009e3041ea | 586 | switch (decodeState){ |
aspeteRakete | 1:18009e3041ea | 587 | case STANDBY: |
aspeteRakete | 1:18009e3041ea | 588 | if (incomingByte == '/'){ |
aspeteRakete | 1:18009e3041ea | 589 | decodeState = ADDRESS; |
aspeteRakete | 1:18009e3041ea | 590 | } |
aspeteRakete | 1:18009e3041ea | 591 | break; |
aspeteRakete | 1:18009e3041ea | 592 | case ADDRESS: |
aspeteRakete | 1:18009e3041ea | 593 | if (incomingByte == 0){ |
aspeteRakete | 1:18009e3041ea | 594 | //end of the address |
aspeteRakete | 1:18009e3041ea | 595 | //decode the address |
aspeteRakete | 1:18009e3041ea | 596 | decodeAddress(); |
aspeteRakete | 1:18009e3041ea | 597 | //next state |
aspeteRakete | 1:18009e3041ea | 598 | decodeState = ADDRESS_PADDING; |
aspeteRakete | 1:18009e3041ea | 599 | } |
aspeteRakete | 1:18009e3041ea | 600 | break; |
aspeteRakete | 1:18009e3041ea | 601 | case ADDRESS_PADDING: |
aspeteRakete | 1:18009e3041ea | 602 | //it does not count the padding |
aspeteRakete | 1:18009e3041ea | 603 | if (incomingByte==','){ |
aspeteRakete | 1:18009e3041ea | 604 | //next state |
aspeteRakete | 1:18009e3041ea | 605 | decodeState = TYPES; |
aspeteRakete | 1:18009e3041ea | 606 | clearIncomingBuffer(); |
aspeteRakete | 1:18009e3041ea | 607 | } |
aspeteRakete | 1:18009e3041ea | 608 | break; |
aspeteRakete | 1:18009e3041ea | 609 | case TYPES: |
aspeteRakete | 1:18009e3041ea | 610 | if (incomingByte != 0){ |
aspeteRakete | 1:18009e3041ea | 611 | //next state |
aspeteRakete | 1:18009e3041ea | 612 | decodeType(incomingByte); |
aspeteRakete | 1:18009e3041ea | 613 | } else { |
aspeteRakete | 1:18009e3041ea | 614 | decodeState = TYPES_PADDING; |
aspeteRakete | 1:18009e3041ea | 615 | } |
aspeteRakete | 1:18009e3041ea | 616 | //FALL THROUGH to test if it should go to the data state |
aspeteRakete | 1:18009e3041ea | 617 | case TYPES_PADDING: { |
aspeteRakete | 1:18009e3041ea | 618 | //compute the padding size for the types |
aspeteRakete | 1:18009e3041ea | 619 | //to determine the start of the data section |
aspeteRakete | 1:18009e3041ea | 620 | int typePad = padSize(dataCount + 1); // 1 is the comma |
aspeteRakete | 1:18009e3041ea | 621 | if (typePad == 0){ |
aspeteRakete | 1:18009e3041ea | 622 | typePad = 4; // to make sure it will be null terminated |
aspeteRakete | 1:18009e3041ea | 623 | } |
aspeteRakete | 1:18009e3041ea | 624 | if (incomingBufferSize == (typePad + dataCount)){ |
aspeteRakete | 1:18009e3041ea | 625 | clearIncomingBuffer(); |
aspeteRakete | 1:18009e3041ea | 626 | decodeState = DATA; |
aspeteRakete | 1:18009e3041ea | 627 | } |
aspeteRakete | 1:18009e3041ea | 628 | } |
aspeteRakete | 1:18009e3041ea | 629 | break; |
aspeteRakete | 1:18009e3041ea | 630 | case DATA: |
aspeteRakete | 1:18009e3041ea | 631 | decodeData(incomingByte); |
aspeteRakete | 1:18009e3041ea | 632 | break; |
aspeteRakete | 1:18009e3041ea | 633 | case DATA_PADDING:{ |
aspeteRakete | 1:18009e3041ea | 634 | //get hte last valid data |
aspeteRakete | 1:18009e3041ea | 635 | for (int i = dataCount - 1; i >= 0; i--){ |
aspeteRakete | 1:18009e3041ea | 636 | OSCData * datum = getOSCData(i); |
aspeteRakete | 1:18009e3041ea | 637 | if (datum->error == OSC_OK){ |
aspeteRakete | 1:18009e3041ea | 638 | //compute the padding size for the data |
aspeteRakete | 1:18009e3041ea | 639 | int dataPad = padSize(datum->bytes); |
aspeteRakete | 1:18009e3041ea | 640 | if (incomingBufferSize == dataPad){ |
aspeteRakete | 1:18009e3041ea | 641 | clearIncomingBuffer(); |
aspeteRakete | 1:18009e3041ea | 642 | decodeState = DATA; |
aspeteRakete | 1:18009e3041ea | 643 | } |
aspeteRakete | 1:18009e3041ea | 644 | break; |
aspeteRakete | 1:18009e3041ea | 645 | } |
aspeteRakete | 1:18009e3041ea | 646 | } |
aspeteRakete | 1:18009e3041ea | 647 | } |
aspeteRakete | 1:18009e3041ea | 648 | break; |
aspeteRakete | 1:18009e3041ea | 649 | |
aspeteRakete | 1:18009e3041ea | 650 | } |
aspeteRakete | 1:18009e3041ea | 651 | } |
aspeteRakete | 1:18009e3041ea | 652 | |
aspeteRakete | 1:18009e3041ea | 653 | |
aspeteRakete | 1:18009e3041ea | 654 | /*============================================================================= |
aspeteRakete | 1:18009e3041ea | 655 | INCOMING BUFFER MANAGEMENT |
aspeteRakete | 1:18009e3041ea | 656 | =============================================================================*/ |
aspeteRakete | 1:18009e3041ea | 657 | #define OSCPREALLOCATEIZE 16 |
aspeteRakete | 1:18009e3041ea | 658 | void OSCMessage::addToIncomingBuffer(uint8_t incomingByte){ |
aspeteRakete | 1:18009e3041ea | 659 | //realloc some space for the new byte and stick it on the end |
aspeteRakete | 1:18009e3041ea | 660 | if(incomingBufferFree>0) |
aspeteRakete | 1:18009e3041ea | 661 | { |
aspeteRakete | 1:18009e3041ea | 662 | incomingBuffer[incomingBufferSize++] = incomingByte; |
aspeteRakete | 1:18009e3041ea | 663 | incomingBufferFree--; |
aspeteRakete | 1:18009e3041ea | 664 | } |
aspeteRakete | 1:18009e3041ea | 665 | else |
aspeteRakete | 1:18009e3041ea | 666 | { |
aspeteRakete | 1:18009e3041ea | 667 | |
aspeteRakete | 1:18009e3041ea | 668 | incomingBuffer = (uint8_t *) realloc ( incomingBuffer, incomingBufferSize + 1 + OSCPREALLOCATEIZE); |
aspeteRakete | 1:18009e3041ea | 669 | if (incomingBuffer != NULL){ |
aspeteRakete | 1:18009e3041ea | 670 | incomingBuffer[incomingBufferSize++] = incomingByte; |
aspeteRakete | 1:18009e3041ea | 671 | incomingBufferFree = OSCPREALLOCATEIZE; |
aspeteRakete | 1:18009e3041ea | 672 | } else { |
aspeteRakete | 1:18009e3041ea | 673 | error = ALLOCFAILED; |
aspeteRakete | 1:18009e3041ea | 674 | } |
aspeteRakete | 1:18009e3041ea | 675 | } |
aspeteRakete | 1:18009e3041ea | 676 | } |
aspeteRakete | 1:18009e3041ea | 677 | |
aspeteRakete | 1:18009e3041ea | 678 | void OSCMessage::clearIncomingBuffer(){ |
aspeteRakete | 1:18009e3041ea | 679 | incomingBuffer = (uint8_t *) realloc ( incomingBuffer, OSCPREALLOCATEIZE); |
aspeteRakete | 1:18009e3041ea | 680 | if (incomingBuffer != NULL){ |
aspeteRakete | 1:18009e3041ea | 681 | incomingBufferFree = OSCPREALLOCATEIZE; |
aspeteRakete | 1:18009e3041ea | 682 | } else { |
aspeteRakete | 1:18009e3041ea | 683 | error = ALLOCFAILED; |
aspeteRakete | 1:18009e3041ea | 684 | incomingBuffer = NULL; |
aspeteRakete | 1:18009e3041ea | 685 | |
aspeteRakete | 1:18009e3041ea | 686 | } |
aspeteRakete | 1:18009e3041ea | 687 | incomingBufferSize = 0; |
aspeteRakete | 1:18009e3041ea | 688 | } |