OSC meeseage tranceiver(Sender/Receiver) for Sparkfun CC3000 WiFi Shield. Supports the following boards: FRDM-KL25Z,ST Nucleo F401RE,ST Nucleo F030R8,LPCXpresso1549,Seeduino-Arch-Pro.
Dependencies: cc3000_hostdriver_mbedsocket mbed
OSCmsgCodec.cpp@1:e62251d890c1, 2014-09-03 (annotated)
- Committer:
- xshige
- Date:
- Wed Sep 03 13:12:02 2014 +0000
- Revision:
- 1:e62251d890c1
- Parent:
- 0:eadd1c588343
added supporting FRDM-K64F.;
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
xshige | 0:eadd1c588343 | 1 | /* |
xshige | 0:eadd1c588343 | 2 | <---------------------------------------------------------------------------------- |
xshige | 0:eadd1c588343 | 3 | |
xshige | 0:eadd1c588343 | 4 | OSC message Codec(encoder/decoder) |
xshige | 0:eadd1c588343 | 5 | |
xshige | 0:eadd1c588343 | 6 | version: 1.3 (2014/ 8/31) encoder bufix(enoceder returns byte length) |
xshige | 0:eadd1c588343 | 7 | version: 1.2 (2014/ 8/30) decoder bufix |
xshige | 0:eadd1c588343 | 8 | version: 1.1 (2013/11/20) support BIG_ENDIAN by #define |
xshige | 0:eadd1c588343 | 9 | version: 1.0 (2013/11/10) |
xshige | 0:eadd1c588343 | 10 | |
xshige | 0:eadd1c588343 | 11 | Copyright (C) 2011,2013,2014 S.Komatsu |
xshige | 0:eadd1c588343 | 12 | released under the MIT License: http://mbed.org/license/mit |
xshige | 0:eadd1c588343 | 13 | |
xshige | 0:eadd1c588343 | 14 | please refer to: http://opensoundcontrol.org/introduction-osc for OSC(Open Sound Control) |
xshige | 0:eadd1c588343 | 15 | |
xshige | 0:eadd1c588343 | 16 | The followings are supported: |
xshige | 0:eadd1c588343 | 17 | |
xshige | 0:eadd1c588343 | 18 | Features: |
xshige | 0:eadd1c588343 | 19 | Packet Parsing (Client) |
xshige | 0:eadd1c588343 | 20 | Packet Construction (Server) |
xshige | 0:eadd1c588343 | 21 | Bundle NOT Support |
xshige | 0:eadd1c588343 | 22 | Timetag NOT Support |
xshige | 0:eadd1c588343 | 23 | |
xshige | 0:eadd1c588343 | 24 | Type Support: |
xshige | 0:eadd1c588343 | 25 | i: int32 |
xshige | 0:eadd1c588343 | 26 | b: blob |
xshige | 0:eadd1c588343 | 27 | s: string |
xshige | 0:eadd1c588343 | 28 | f: float32 |
xshige | 0:eadd1c588343 | 29 | m: MIDI message(port id, status byte, data1, data2) // I don't know the detail |
xshige | 0:eadd1c588343 | 30 | |
xshige | 0:eadd1c588343 | 31 | Change Log: |
xshige | 0:eadd1c588343 | 32 | Bug(string length is not correct in encoding) Fix on 2013/11/10 (v0.9 -> v1.0) |
xshige | 0:eadd1c588343 | 33 | |
xshige | 0:eadd1c588343 | 34 | >---------------------------------------------------------------------------------- |
xshige | 0:eadd1c588343 | 35 | */ |
xshige | 0:eadd1c588343 | 36 | |
xshige | 0:eadd1c588343 | 37 | #include "OSCmsgCodec.h" |
xshige | 0:eadd1c588343 | 38 | //#define BIG_ENDIAN |
xshige | 0:eadd1c588343 | 39 | |
xshige | 0:eadd1c588343 | 40 | int lenAlign4B(int len) { |
xshige | 0:eadd1c588343 | 41 | if ((len % 4) == 0) {return len; } |
xshige | 0:eadd1c588343 | 42 | else {return len+4-(len % 4);} |
xshige | 0:eadd1c588343 | 43 | } |
xshige | 0:eadd1c588343 | 44 | |
xshige | 0:eadd1c588343 | 45 | |
xshige | 0:eadd1c588343 | 46 | int encOSCmsg(char *packet , union OSCarg *msg){ |
xshige | 0:eadd1c588343 | 47 | // *** important notice *** |
xshige | 0:eadd1c588343 | 48 | // output buffer must be cleared before call this |
xshige | 0:eadd1c588343 | 49 | char *p, *s, *d, *typeTag; |
xshige | 0:eadd1c588343 | 50 | char c; |
xshige | 0:eadd1c588343 | 51 | |
xshige | 0:eadd1c588343 | 52 | p=packet; |
xshige | 0:eadd1c588343 | 53 | d=p; |
xshige | 0:eadd1c588343 | 54 | s=msg[0].address; // address |
xshige | 0:eadd1c588343 | 55 | for(int i=0; i<strlen(msg[0].address); i++) *d++ = *s++; |
xshige | 0:eadd1c588343 | 56 | *d=0; // terminator |
xshige | 0:eadd1c588343 | 57 | // p += 4*((strlen(msg[0].address)+1)/4+1); |
xshige | 0:eadd1c588343 | 58 | p += lenAlign4B(strlen(msg[0].address)+1); |
xshige | 0:eadd1c588343 | 59 | // |
xshige | 0:eadd1c588343 | 60 | s=msg[1].typeTag; |
xshige | 0:eadd1c588343 | 61 | d=p; |
xshige | 0:eadd1c588343 | 62 | for(int i=0; i<strlen(msg[1].typeTag); i++) *d++ = *s++; |
xshige | 0:eadd1c588343 | 63 | *d=0; // terminator |
xshige | 0:eadd1c588343 | 64 | // p += 4*((strlen(msg[1].s)+1)/4+1); |
xshige | 0:eadd1c588343 | 65 | p += lenAlign4B(strlen(msg[1].typeTag)+1); |
xshige | 0:eadd1c588343 | 66 | // |
xshige | 0:eadd1c588343 | 67 | typeTag=msg[1].s+1; // skip ',' |
xshige | 0:eadd1c588343 | 68 | for(int n=0; n<strlen(typeTag); n++){ |
xshige | 0:eadd1c588343 | 69 | c = typeTag[n]; |
xshige | 0:eadd1c588343 | 70 | if (('s'==c)) { |
xshige | 0:eadd1c588343 | 71 | s=msg[n+2].s; |
xshige | 0:eadd1c588343 | 72 | d=p; |
xshige | 0:eadd1c588343 | 73 | for(int i=0; i<strlen(msg[n+2].s); i++) *d++ = *s++; |
xshige | 0:eadd1c588343 | 74 | *d=0; // terminater |
xshige | 0:eadd1c588343 | 75 | // p += 4*((strlen(msg[n+2].s)+1)/4+1); |
xshige | 0:eadd1c588343 | 76 | p += lenAlign4B(strlen(msg[n+2].s)+1); |
xshige | 0:eadd1c588343 | 77 | } |
xshige | 0:eadd1c588343 | 78 | else if (('i'==c)||('f'==c)) { |
xshige | 0:eadd1c588343 | 79 | #ifdef BIG_ENDIAN |
xshige | 0:eadd1c588343 | 80 | // no change endian (big to big) |
xshige | 0:eadd1c588343 | 81 | p[0]=msg[n+2]._b[0]; |
xshige | 0:eadd1c588343 | 82 | p[1]=msg[n+2]._b[1]; |
xshige | 0:eadd1c588343 | 83 | p[2]=msg[n+2]._b[2]; |
xshige | 0:eadd1c588343 | 84 | p[3]=msg[n+2]._b[3]; |
xshige | 0:eadd1c588343 | 85 | #else |
xshige | 0:eadd1c588343 | 86 | // change endian (little to big) |
xshige | 0:eadd1c588343 | 87 | p[0]=msg[n+2]._b[3]; |
xshige | 0:eadd1c588343 | 88 | p[1]=msg[n+2]._b[2]; |
xshige | 0:eadd1c588343 | 89 | p[2]=msg[n+2]._b[1]; |
xshige | 0:eadd1c588343 | 90 | p[3]=msg[n+2]._b[0]; |
xshige | 0:eadd1c588343 | 91 | #endif |
xshige | 0:eadd1c588343 | 92 | p +=4; |
xshige | 0:eadd1c588343 | 93 | } |
xshige | 0:eadd1c588343 | 94 | else if ('b'==c) { |
xshige | 0:eadd1c588343 | 95 | // put length of blog |
xshige | 0:eadd1c588343 | 96 | #ifdef BIG_ENDIAN |
xshige | 0:eadd1c588343 | 97 | // no change endian (big to big) |
xshige | 0:eadd1c588343 | 98 | p[0]=msg[n+2]._b[0]; |
xshige | 0:eadd1c588343 | 99 | p[1]=msg[n+2]._b[1]; |
xshige | 0:eadd1c588343 | 100 | p[2]=msg[n+2]._b[2]; |
xshige | 0:eadd1c588343 | 101 | |
xshige | 0:eadd1c588343 | 102 | p[3]=msg[n+2]._b[3]; |
xshige | 0:eadd1c588343 | 103 | #else |
xshige | 0:eadd1c588343 | 104 | // change endian (little to big) |
xshige | 0:eadd1c588343 | 105 | p[0]=msg[n+2]._b[3]; |
xshige | 0:eadd1c588343 | 106 | p[1]=msg[n+2]._b[2]; |
xshige | 0:eadd1c588343 | 107 | p[2]=msg[n+2]._b[1]; |
xshige | 0:eadd1c588343 | 108 | p[3]=msg[n+2]._b[0]; |
xshige | 0:eadd1c588343 | 109 | #endif |
xshige | 0:eadd1c588343 | 110 | p +=4; |
xshige | 0:eadd1c588343 | 111 | // get ponter of blog (copy to msg[n].blog.p) |
xshige | 0:eadd1c588343 | 112 | s=msg[n+2].blob.p; |
xshige | 0:eadd1c588343 | 113 | d=p; |
xshige | 0:eadd1c588343 | 114 | for(int i=0; i<msg[n+2].blob.len; i++) *d++ = *s++; |
xshige | 0:eadd1c588343 | 115 | p += 4*(msg[n+2].blob.len/4+1); |
xshige | 0:eadd1c588343 | 116 | } |
xshige | 0:eadd1c588343 | 117 | else if ('m'==c) { |
xshige | 0:eadd1c588343 | 118 | // get midi data (copy to msg[n].m[]) |
xshige | 0:eadd1c588343 | 119 | p[0]=msg[n+2].m[0]; |
xshige | 0:eadd1c588343 | 120 | p[1]=msg[n+2].m[1]; |
xshige | 0:eadd1c588343 | 121 | p[2]=msg[n+2].m[2]; |
xshige | 0:eadd1c588343 | 122 | p[3]=msg[n+2].m[3]; |
xshige | 0:eadd1c588343 | 123 | p +=4; |
xshige | 0:eadd1c588343 | 124 | } |
xshige | 0:eadd1c588343 | 125 | else { |
xshige | 0:eadd1c588343 | 126 | //printf("*** Not Supported TypeTag:%s ****\n",typeTag); |
xshige | 0:eadd1c588343 | 127 | } |
xshige | 0:eadd1c588343 | 128 | }; |
xshige | 0:eadd1c588343 | 129 | //return (p-packet); // return packet size |
xshige | 0:eadd1c588343 | 130 | // bugfix 2014/8/31 |
xshige | 0:eadd1c588343 | 131 | return sizeof(char)*(p-packet); // return byte length |
xshige | 0:eadd1c588343 | 132 | }; |
xshige | 0:eadd1c588343 | 133 | |
xshige | 0:eadd1c588343 | 134 | void decOSCmsg(char *packet , union OSCarg *msg){ |
xshige | 0:eadd1c588343 | 135 | // Caution: the returned result points to packet as blobs or strings (not newly allocatd) |
xshige | 0:eadd1c588343 | 136 | char *p, *typeTag; |
xshige | 0:eadd1c588343 | 137 | char c; int n; |
xshige | 0:eadd1c588343 | 138 | |
xshige | 0:eadd1c588343 | 139 | msg[0].address = packet; // address |
xshige | 0:eadd1c588343 | 140 | msg[1].typeTag = packet+4*((strlen(msg[0].s)+1)/4+1);//typeTag |
xshige | 0:eadd1c588343 | 141 | typeTag=msg[1].s+1; // skip ',' |
xshige | 0:eadd1c588343 | 142 | |
xshige | 0:eadd1c588343 | 143 | // bugfix 2014/8/30 |
xshige | 0:eadd1c588343 | 144 | if (strlen(typeTag)%2 == 0) p= msg[1].s+4*((strlen(msg[1].s)+1)/4); |
xshige | 0:eadd1c588343 | 145 | else p= msg[1].s+4*((strlen(msg[1].s)+1)/4+1); |
xshige | 0:eadd1c588343 | 146 | |
xshige | 0:eadd1c588343 | 147 | for(n=0; n<strlen(typeTag); n++){ |
xshige | 0:eadd1c588343 | 148 | c = typeTag[n]; |
xshige | 0:eadd1c588343 | 149 | if (('s'==c)) { |
xshige | 0:eadd1c588343 | 150 | msg[n+2].s=p; |
xshige | 0:eadd1c588343 | 151 | //p += 4*((strlen(msg[n+2].s)+1)/4+1); |
xshige | 0:eadd1c588343 | 152 | p += lenAlign4B(strlen(msg[n+2].s)+1); |
xshige | 0:eadd1c588343 | 153 | } |
xshige | 0:eadd1c588343 | 154 | else if (('i'==c)||('f'==c)) { |
xshige | 0:eadd1c588343 | 155 | #ifdef BIG_ENDIAN |
xshige | 0:eadd1c588343 | 156 | // no change endian (big to big) |
xshige | 0:eadd1c588343 | 157 | msg[n+2]._b[0]=p[0]; |
xshige | 0:eadd1c588343 | 158 | msg[n+2]._b[1]=p[1]; |
xshige | 0:eadd1c588343 | 159 | msg[n+2]._b[2]=p[2]; |
xshige | 0:eadd1c588343 | 160 | msg[n+2]._b[3]=p[3]; |
xshige | 0:eadd1c588343 | 161 | #else |
xshige | 0:eadd1c588343 | 162 | // change endian (big to little) |
xshige | 0:eadd1c588343 | 163 | msg[n+2]._b[3]=p[0]; |
xshige | 0:eadd1c588343 | 164 | msg[n+2]._b[2]=p[1]; |
xshige | 0:eadd1c588343 | 165 | msg[n+2]._b[1]=p[2]; |
xshige | 0:eadd1c588343 | 166 | msg[n+2]._b[0]=p[3]; |
xshige | 0:eadd1c588343 | 167 | #endif |
xshige | 0:eadd1c588343 | 168 | p +=4; |
xshige | 0:eadd1c588343 | 169 | } |
xshige | 0:eadd1c588343 | 170 | else if ('b'==c) { |
xshige | 0:eadd1c588343 | 171 | // get lenth of blog (copy to msg[n].blog.len) |
xshige | 0:eadd1c588343 | 172 | #ifdef BIG_ENDIAN |
xshige | 0:eadd1c588343 | 173 | // no change endian (big to big) |
xshige | 0:eadd1c588343 | 174 | msg[n+2]._b[0]=p[0]; |
xshige | 0:eadd1c588343 | 175 | msg[n+2]._b[1]=p[1]; |
xshige | 0:eadd1c588343 | 176 | msg[n+2]._b[2]=p[2]; |
xshige | 0:eadd1c588343 | 177 | msg[n+2]._b[3]=p[3]; |
xshige | 0:eadd1c588343 | 178 | #else |
xshige | 0:eadd1c588343 | 179 | // change endian (big to little) |
xshige | 0:eadd1c588343 | 180 | msg[n+2]._b[3]=p[0]; |
xshige | 0:eadd1c588343 | 181 | msg[n+2]._b[2]=p[1]; |
xshige | 0:eadd1c588343 | 182 | msg[n+2]._b[1]=p[2]; |
xshige | 0:eadd1c588343 | 183 | msg[n+2]._b[0]=p[3]; |
xshige | 0:eadd1c588343 | 184 | #endif |
xshige | 0:eadd1c588343 | 185 | p +=4; |
xshige | 0:eadd1c588343 | 186 | // get ponter of blog (copy to msg[n].blog.p) |
xshige | 0:eadd1c588343 | 187 | msg[n+2].blob.p=p; |
xshige | 0:eadd1c588343 | 188 | //p += 4*(msg[n+2].blob.len/4+1); |
xshige | 0:eadd1c588343 | 189 | p += lenAlign4B(msg[n+2].blob.len+1); |
xshige | 0:eadd1c588343 | 190 | } |
xshige | 0:eadd1c588343 | 191 | else if ('m'==c) { |
xshige | 0:eadd1c588343 | 192 | // get midi data (copy to msg[n].m[]) |
xshige | 0:eadd1c588343 | 193 | msg[n+2].m[0]=p[0]; |
xshige | 0:eadd1c588343 | 194 | msg[n+2].m[1]=p[1]; |
xshige | 0:eadd1c588343 | 195 | msg[n+2].m[2]=p[2]; |
xshige | 0:eadd1c588343 | 196 | msg[n+2].m[3]=p[3]; |
xshige | 0:eadd1c588343 | 197 | p +=4; |
xshige | 0:eadd1c588343 | 198 | } |
xshige | 0:eadd1c588343 | 199 | else { |
xshige | 0:eadd1c588343 | 200 | //printf("*** Not Supported TypeTag:%s ****\n",typeTag); |
xshige | 0:eadd1c588343 | 201 | } |
xshige | 0:eadd1c588343 | 202 | }; |
xshige | 0:eadd1c588343 | 203 | }; |