modded version CNMAT/OSC https://github.com/CNMAT/OSC
Fork of osc-cnmat by
OSCData.cpp@4:107c23eb31b6, 2016-02-13 (annotated)
- Committer:
- casiotone401
- Date:
- Sat Feb 13 11:29:14 2016 +0000
- Revision:
- 4:107c23eb31b6
- Parent:
- 0:36705c54059a
OSCMessage::send & OSCBundle::send supports return OSC buffer
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
aspeteRakete | 0:36705c54059a | 1 | |
aspeteRakete | 0:36705c54059a | 2 | #include "OSCData.h" |
aspeteRakete | 0:36705c54059a | 3 | |
aspeteRakete | 0:36705c54059a | 4 | /*============================================================================= |
aspeteRakete | 0:36705c54059a | 5 | CONSTRUCTORS |
aspeteRakete | 0:36705c54059a | 6 | |
aspeteRakete | 0:36705c54059a | 7 | overloaded methods for each of the types which will |
aspeteRakete | 0:36705c54059a | 8 | set the type flag, the size (in bytes), and the data |
aspeteRakete | 0:36705c54059a | 9 | =============================================================================*/ |
aspeteRakete | 0:36705c54059a | 10 | |
aspeteRakete | 0:36705c54059a | 11 | |
aspeteRakete | 0:36705c54059a | 12 | OSCData::OSCData(const char * s){ |
aspeteRakete | 0:36705c54059a | 13 | error = OSC_OK; |
aspeteRakete | 0:36705c54059a | 14 | type = 's'; |
aspeteRakete | 0:36705c54059a | 15 | bytes = (strlen(s) + 1); |
aspeteRakete | 0:36705c54059a | 16 | //own the data |
aspeteRakete | 0:36705c54059a | 17 | char * mem = (char *) malloc(bytes); |
aspeteRakete | 0:36705c54059a | 18 | if (mem == NULL){ |
aspeteRakete | 0:36705c54059a | 19 | error = ALLOCFAILED; |
aspeteRakete | 0:36705c54059a | 20 | } else { |
aspeteRakete | 0:36705c54059a | 21 | strcpy(mem, s); |
aspeteRakete | 0:36705c54059a | 22 | data.s = mem; |
aspeteRakete | 0:36705c54059a | 23 | } |
aspeteRakete | 0:36705c54059a | 24 | } |
aspeteRakete | 0:36705c54059a | 25 | |
aspeteRakete | 0:36705c54059a | 26 | OSCData::OSCData(int i){ |
aspeteRakete | 0:36705c54059a | 27 | error = OSC_OK; |
aspeteRakete | 0:36705c54059a | 28 | type = 'i'; |
aspeteRakete | 0:36705c54059a | 29 | //cast it to an int32 |
aspeteRakete | 0:36705c54059a | 30 | bytes = sizeof(int32_t); |
aspeteRakete | 0:36705c54059a | 31 | int32_t i32 = (int32_t) i; |
aspeteRakete | 0:36705c54059a | 32 | data.i = i32; |
aspeteRakete | 0:36705c54059a | 33 | } |
aspeteRakete | 0:36705c54059a | 34 | |
aspeteRakete | 0:36705c54059a | 35 | |
aspeteRakete | 0:36705c54059a | 36 | OSCData::OSCData(float f){ |
aspeteRakete | 0:36705c54059a | 37 | error = OSC_OK; |
aspeteRakete | 0:36705c54059a | 38 | type = 'f'; |
aspeteRakete | 0:36705c54059a | 39 | bytes = sizeof(float); |
aspeteRakete | 0:36705c54059a | 40 | data.f = f; |
aspeteRakete | 0:36705c54059a | 41 | } |
aspeteRakete | 0:36705c54059a | 42 | |
aspeteRakete | 0:36705c54059a | 43 | OSCData::OSCData(uint64_t t){ |
aspeteRakete | 0:36705c54059a | 44 | error = OSC_OK; |
aspeteRakete | 0:36705c54059a | 45 | type = 't'; |
aspeteRakete | 0:36705c54059a | 46 | bytes = sizeof(uint64_t ); |
aspeteRakete | 0:36705c54059a | 47 | data.l = t; |
aspeteRakete | 0:36705c54059a | 48 | } |
aspeteRakete | 0:36705c54059a | 49 | OSCData::OSCData(bool b){ |
aspeteRakete | 0:36705c54059a | 50 | error = OSC_OK; |
aspeteRakete | 0:36705c54059a | 51 | type = b?'T':'F'; |
aspeteRakete | 0:36705c54059a | 52 | bytes = 0; |
aspeteRakete | 0:36705c54059a | 53 | } |
aspeteRakete | 0:36705c54059a | 54 | |
aspeteRakete | 0:36705c54059a | 55 | |
aspeteRakete | 0:36705c54059a | 56 | OSCData::OSCData(double d){ |
aspeteRakete | 0:36705c54059a | 57 | error = OSC_OK; |
aspeteRakete | 0:36705c54059a | 58 | bytes = sizeof(double); |
aspeteRakete | 0:36705c54059a | 59 | //if it's not 8 bits it's not a true double |
aspeteRakete | 0:36705c54059a | 60 | if (bytes == 8){ |
aspeteRakete | 0:36705c54059a | 61 | type = 'd'; |
aspeteRakete | 0:36705c54059a | 62 | data.d = d; |
aspeteRakete | 0:36705c54059a | 63 | } else { |
aspeteRakete | 0:36705c54059a | 64 | type = 'f'; |
aspeteRakete | 0:36705c54059a | 65 | data.f = d; |
aspeteRakete | 0:36705c54059a | 66 | } |
aspeteRakete | 0:36705c54059a | 67 | } |
aspeteRakete | 0:36705c54059a | 68 | |
aspeteRakete | 0:36705c54059a | 69 | OSCData::OSCData(uint8_t * b, int len){ |
aspeteRakete | 0:36705c54059a | 70 | error = OSC_OK; |
aspeteRakete | 0:36705c54059a | 71 | type = 'b'; |
aspeteRakete | 0:36705c54059a | 72 | bytes = len + 4; |
aspeteRakete | 0:36705c54059a | 73 | //add the size to the front of the blob |
aspeteRakete | 0:36705c54059a | 74 | uint32_t len32 = (uint32_t) len; |
aspeteRakete | 0:36705c54059a | 75 | //make sure the length is endian-safe |
aspeteRakete | 0:36705c54059a | 76 | len32 = BigEndian(len32); |
aspeteRakete | 0:36705c54059a | 77 | uint8_t * lenPtr = (uint8_t *) (& len32); |
aspeteRakete | 0:36705c54059a | 78 | //own the data |
aspeteRakete | 0:36705c54059a | 79 | if(bytes>0) |
aspeteRakete | 0:36705c54059a | 80 | { |
aspeteRakete | 0:36705c54059a | 81 | |
aspeteRakete | 0:36705c54059a | 82 | uint8_t * mem = (uint8_t * ) malloc(bytes); |
aspeteRakete | 0:36705c54059a | 83 | if (mem == NULL){ |
aspeteRakete | 0:36705c54059a | 84 | error = ALLOCFAILED; |
aspeteRakete | 0:36705c54059a | 85 | } else { |
aspeteRakete | 0:36705c54059a | 86 | //copy over the blob length |
aspeteRakete | 0:36705c54059a | 87 | memcpy(mem, lenPtr, 4); |
aspeteRakete | 0:36705c54059a | 88 | //copy over the blob data |
aspeteRakete | 0:36705c54059a | 89 | memcpy(mem + 4, b, len); |
aspeteRakete | 0:36705c54059a | 90 | data.b = mem; |
aspeteRakete | 0:36705c54059a | 91 | } |
aspeteRakete | 0:36705c54059a | 92 | } |
aspeteRakete | 0:36705c54059a | 93 | else |
aspeteRakete | 0:36705c54059a | 94 | data.b = 0; |
aspeteRakete | 0:36705c54059a | 95 | } |
aspeteRakete | 0:36705c54059a | 96 | |
aspeteRakete | 0:36705c54059a | 97 | OSCData::OSCData (OSCData * datum){ |
aspeteRakete | 0:36705c54059a | 98 | error = OSC_OK; |
aspeteRakete | 0:36705c54059a | 99 | type = datum->type; |
aspeteRakete | 0:36705c54059a | 100 | bytes = datum->bytes; |
aspeteRakete | 0:36705c54059a | 101 | if (type == 'i' || type == 'f' || type == 'd'|| type == 'y'){ |
aspeteRakete | 0:36705c54059a | 102 | data = datum->data; |
aspeteRakete | 0:36705c54059a | 103 | } else if (type == 's' || type == 'b'){ |
aspeteRakete | 0:36705c54059a | 104 | //allocate a new peice of memory |
aspeteRakete | 0:36705c54059a | 105 | uint8_t * mem = (uint8_t * ) malloc(bytes); |
aspeteRakete | 0:36705c54059a | 106 | if (mem == NULL){ |
aspeteRakete | 0:36705c54059a | 107 | error = ALLOCFAILED; |
aspeteRakete | 0:36705c54059a | 108 | } else { |
aspeteRakete | 0:36705c54059a | 109 | //copy over the blob length |
aspeteRakete | 0:36705c54059a | 110 | memcpy(mem, datum->data.b, bytes); |
aspeteRakete | 0:36705c54059a | 111 | data.b = mem; |
aspeteRakete | 0:36705c54059a | 112 | } |
aspeteRakete | 0:36705c54059a | 113 | } |
aspeteRakete | 0:36705c54059a | 114 | } |
aspeteRakete | 0:36705c54059a | 115 | |
aspeteRakete | 0:36705c54059a | 116 | //DESTRUCTOR |
aspeteRakete | 0:36705c54059a | 117 | OSCData::~OSCData(){ |
aspeteRakete | 0:36705c54059a | 118 | //if there are no bytes, there is nothing to free |
aspeteRakete | 0:36705c54059a | 119 | if (bytes>0){ |
aspeteRakete | 0:36705c54059a | 120 | //if the data is of type 's' or 'b', need to free that memory |
aspeteRakete | 0:36705c54059a | 121 | if (type == 's'){ |
aspeteRakete | 0:36705c54059a | 122 | free(data.s); |
aspeteRakete | 0:36705c54059a | 123 | }else if( type == 'b'){ |
aspeteRakete | 0:36705c54059a | 124 | free(data.b); |
aspeteRakete | 0:36705c54059a | 125 | } |
aspeteRakete | 0:36705c54059a | 126 | } |
aspeteRakete | 0:36705c54059a | 127 | } |
aspeteRakete | 0:36705c54059a | 128 | |
aspeteRakete | 0:36705c54059a | 129 | //sets just the type as a message placeholder |
aspeteRakete | 0:36705c54059a | 130 | //no data |
aspeteRakete | 0:36705c54059a | 131 | OSCData::OSCData(char t){ |
aspeteRakete | 0:36705c54059a | 132 | error = INVALID_OSC; |
aspeteRakete | 0:36705c54059a | 133 | type = t; |
aspeteRakete | 0:36705c54059a | 134 | bytes = 0; |
aspeteRakete | 0:36705c54059a | 135 | } |
aspeteRakete | 0:36705c54059a | 136 | |
aspeteRakete | 0:36705c54059a | 137 | /*============================================================================= |
aspeteRakete | 0:36705c54059a | 138 | GETTERS |
aspeteRakete | 0:36705c54059a | 139 | |
aspeteRakete | 0:36705c54059a | 140 | perform a safety check to make sure the data type matches the request |
aspeteRakete | 0:36705c54059a | 141 | otherwise returns NULL |
aspeteRakete | 0:36705c54059a | 142 | =============================================================================*/ |
aspeteRakete | 0:36705c54059a | 143 | |
aspeteRakete | 0:36705c54059a | 144 | int32_t OSCData::getInt(){ |
aspeteRakete | 0:36705c54059a | 145 | if (type == 'i'){ |
aspeteRakete | 0:36705c54059a | 146 | return data.i; |
aspeteRakete | 0:36705c54059a | 147 | } else { |
aspeteRakete | 0:36705c54059a | 148 | return NULL; |
aspeteRakete | 0:36705c54059a | 149 | } |
aspeteRakete | 0:36705c54059a | 150 | } |
aspeteRakete | 0:36705c54059a | 151 | uint64_t OSCData::getTime(){ |
aspeteRakete | 0:36705c54059a | 152 | if (type == 't'){ |
aspeteRakete | 0:36705c54059a | 153 | return data.l; |
aspeteRakete | 0:36705c54059a | 154 | } else { |
aspeteRakete | 0:36705c54059a | 155 | return NULL; |
aspeteRakete | 0:36705c54059a | 156 | } |
aspeteRakete | 0:36705c54059a | 157 | } |
aspeteRakete | 0:36705c54059a | 158 | float OSCData::getFloat(){ |
aspeteRakete | 0:36705c54059a | 159 | if (type == 'f'){ |
aspeteRakete | 0:36705c54059a | 160 | return data.f; |
aspeteRakete | 0:36705c54059a | 161 | } else { |
aspeteRakete | 0:36705c54059a | 162 | return NULL; |
aspeteRakete | 0:36705c54059a | 163 | } |
aspeteRakete | 0:36705c54059a | 164 | } |
aspeteRakete | 0:36705c54059a | 165 | |
aspeteRakete | 0:36705c54059a | 166 | double OSCData::getDouble(){ |
aspeteRakete | 0:36705c54059a | 167 | if (type == 'd'){ |
aspeteRakete | 0:36705c54059a | 168 | return data.d; |
aspeteRakete | 0:36705c54059a | 169 | } else { |
aspeteRakete | 0:36705c54059a | 170 | return NULL; |
aspeteRakete | 0:36705c54059a | 171 | } |
aspeteRakete | 0:36705c54059a | 172 | } |
aspeteRakete | 0:36705c54059a | 173 | bool OSCData::getBoolean(){ |
aspeteRakete | 0:36705c54059a | 174 | |
aspeteRakete | 0:36705c54059a | 175 | return type=='T'; |
aspeteRakete | 0:36705c54059a | 176 | } |
aspeteRakete | 0:36705c54059a | 177 | |
aspeteRakete | 0:36705c54059a | 178 | int OSCData::getString(char * strBuffer, int length){ |
aspeteRakete | 0:36705c54059a | 179 | if (type == 's' && bytes <= length){ |
aspeteRakete | 0:36705c54059a | 180 | strncpy(strBuffer, data.s, bytes); |
aspeteRakete | 0:36705c54059a | 181 | return bytes; |
aspeteRakete | 0:36705c54059a | 182 | } else { |
aspeteRakete | 0:36705c54059a | 183 | return NULL; |
aspeteRakete | 0:36705c54059a | 184 | } |
aspeteRakete | 0:36705c54059a | 185 | } |
aspeteRakete | 0:36705c54059a | 186 | |
aspeteRakete | 0:36705c54059a | 187 | int OSCData::getBlob(uint8_t * blobBuffer, int length){ |
aspeteRakete | 0:36705c54059a | 188 | if (type == 'b' && bytes <= length){ |
aspeteRakete | 0:36705c54059a | 189 | memcpy(blobBuffer, data.b, bytes); |
aspeteRakete | 0:36705c54059a | 190 | return bytes; |
aspeteRakete | 0:36705c54059a | 191 | } else { |
aspeteRakete | 0:36705c54059a | 192 | return NULL; |
aspeteRakete | 0:36705c54059a | 193 | } |
aspeteRakete | 0:36705c54059a | 194 | } |