Shigeki KOMATSU / Mbed 2 deprecated Sparkfun_CC3000_WiFi_OSCreceiver

Dependencies:   cc3000_hostdriver_mbedsocket mbed

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers OSCmsgCodec.cpp Source File

OSCmsgCodec.cpp

00001 /*
00002 <----------------------------------------------------------------------------------
00003 
00004  OSC message Codec(encoder/decoder)
00005  
00006  version: 1.2 (2014/ 8/30) decoder bufix 
00007  version: 1.1 (2013/11/20) support BIG_ENDIAN by #define
00008  version: 1.0 (2013/11/10)
00009 
00010  Copyright (C) 2011,2013,2014 S.Komatsu
00011  released under the MIT License: http://mbed.org/license/mit
00012 
00013  please refer to: http://opensoundcontrol.org/introduction-osc for OSC(Open Sound Control)
00014 
00015   The followings are supported:
00016 
00017     Features: 
00018      Packet Parsing (Client)
00019      Packet Construction (Server)
00020      Bundle NOT Support
00021      Timetag NOT Support
00022 
00023     Type Support: 
00024      i: int32
00025      b: blob
00026      s: string
00027      f: float32
00028      m: MIDI message(port id, status byte, data1, data2) // I don't know the detail
00029      
00030   Change Log:
00031    Bug(string length is not correct in encoding) Fix on 2013/11/10 (v0.9 -> v1.0)
00032 
00033 >----------------------------------------------------------------------------------
00034 */
00035 
00036 #include "OSCmsgCodec.h"
00037 //#define BIG_ENDIAN
00038 
00039 int lenAlign4B(int len) {
00040     if ((len % 4) == 0) {return len; }
00041     else {return len+4-(len % 4);}
00042 }
00043 
00044 
00045 int encOSCmsg(char *packet , union OSCarg *msg){
00046   // output buffer must be cleared before call this
00047   char *p, *s, *d, *typeTag; 
00048   char c;
00049 
00050   p=packet;
00051   d=p;
00052   s=msg[0].address; // address
00053   for(int i=0; i<strlen(msg[0].address); i++) *d++ = *s++;
00054   *d=0; // terminator
00055 //  p += 4*((strlen(msg[0].address)+1)/4+1);
00056   p += lenAlign4B(strlen(msg[0].address)+1);
00057   // 
00058   s=msg[1].typeTag;
00059   d=p;
00060   for(int i=0; i<strlen(msg[1].typeTag); i++) *d++ = *s++; 
00061   *d=0; // terminator   
00062 //  p += 4*((strlen(msg[1].s)+1)/4+1);
00063   p += lenAlign4B(strlen(msg[1].typeTag)+1);
00064   //
00065   typeTag=msg[1].s+1; // skip ','
00066   for(int n=0; n<strlen(typeTag); n++){
00067     c = typeTag[n];
00068     if (('s'==c)) {
00069       s=msg[n+2].s;
00070       d=p;
00071       for(int i=0; i<strlen(msg[n+2].s); i++) *d++ = *s++;
00072       *d=0; // terminater    
00073 //     p += 4*((strlen(msg[n+2].s)+1)/4+1);
00074       p += lenAlign4B(strlen(msg[n+2].s)+1);
00075     } 
00076     else if (('i'==c)||('f'==c)) {
00077 #ifdef BIG_ENDIAN
00078       // no change endian (big to big)
00079       p[0]=msg[n+2]._b[0];
00080       p[1]=msg[n+2]._b[1];
00081       p[2]=msg[n+2]._b[2];
00082       p[3]=msg[n+2]._b[3];
00083 #else
00084       // change endian (little to big)
00085       p[0]=msg[n+2]._b[3];
00086       p[1]=msg[n+2]._b[2];
00087       p[2]=msg[n+2]._b[1];
00088       p[3]=msg[n+2]._b[0];
00089 #endif
00090         p +=4;  
00091     } 
00092     else if ('b'==c) {
00093       // put length of blog
00094 #ifdef BIG_ENDIAN
00095       // no change endian (big to big)
00096       p[0]=msg[n+2]._b[0];
00097       p[1]=msg[n+2]._b[1];
00098       p[2]=msg[n+2]._b[2];
00099       p[3]=msg[n+2]._b[3];
00100 #else
00101       // change endian (little to big)
00102       p[0]=msg[n+2]._b[3];
00103       p[1]=msg[n+2]._b[2];
00104       p[2]=msg[n+2]._b[1];
00105       p[3]=msg[n+2]._b[0];
00106 #endif
00107       p +=4;  
00108       // get ponter of blog (copy to msg[n].blog.p)
00109       s=msg[n+2].blob.p;
00110       d=p;
00111       for(int i=0; i<msg[n+2].blob.len; i++) *d++ = *s++;    
00112       p += 4*(msg[n+2].blob.len/4+1);       
00113     } 
00114     else if ('m'==c) {
00115       // get midi data (copy to msg[n].m[])
00116       p[0]=msg[n+2].m[0]; 
00117       p[1]=msg[n+2].m[1]; 
00118       p[2]=msg[n+2].m[2];
00119       p[3]=msg[n+2].m[3];
00120       p +=4;  
00121     } 
00122     else {
00123       //printf("*** Not Supported TypeTag:%s ****\n",typeTag);
00124     }
00125   };
00126   return (p-packet); // return packet size    
00127 };
00128     
00129 void decOSCmsg(char *packet , union OSCarg *msg){
00130   // Caution: the returned result points to packet as blobs or strings (not newly allocatd)
00131   char *p, *typeTag; 
00132   char c; int n;
00133 
00134   msg[0].address = packet; // address
00135   msg[1].typeTag = packet+4*((strlen(msg[0].s)+1)/4+1);//typeTag
00136   typeTag=msg[1].s+1; // skip ','
00137   
00138   // bugfix 2014/8/30
00139   if (strlen(typeTag)%2 == 0) p= msg[1].s+4*((strlen(msg[1].s)+1)/4); 
00140   else  p= msg[1].s+4*((strlen(msg[1].s)+1)/4+1);
00141   
00142   for(n=0; n<strlen(typeTag); n++){
00143     c = typeTag[n];
00144     if (('s'==c)) {
00145       msg[n+2].s=p;
00146       //p += 4*((strlen(msg[n+2].s)+1)/4+1);
00147       p += lenAlign4B(strlen(msg[n+2].s)+1);
00148     } 
00149     else if (('i'==c)||('f'==c)) {
00150 #ifdef BIG_ENDIAN
00151       // no change endian (big to big)
00152       msg[n+2]._b[0]=p[0];
00153       msg[n+2]._b[1]=p[1];
00154       msg[n+2]._b[2]=p[2];
00155       msg[n+2]._b[3]=p[3];
00156 #else
00157       // change endian (big to little)
00158       msg[n+2]._b[3]=p[0];
00159       msg[n+2]._b[2]=p[1];
00160       msg[n+2]._b[1]=p[2];
00161       msg[n+2]._b[0]=p[3];
00162 #endif
00163       p +=4;  
00164     } 
00165     else if ('b'==c) {
00166       // get lenth of blog (copy to msg[n].blog.len)
00167 #ifdef BIG_ENDIAN
00168       // no change endian (big to big)
00169       msg[n+2]._b[0]=p[0];
00170       msg[n+2]._b[1]=p[1];
00171       msg[n+2]._b[2]=p[2];
00172       msg[n+2]._b[3]=p[3];
00173 #else
00174       // change endian (big to little)
00175       msg[n+2]._b[3]=p[0];
00176       msg[n+2]._b[2]=p[1];
00177       msg[n+2]._b[1]=p[2];
00178       msg[n+2]._b[0]=p[3];
00179 #endif
00180       p +=4;
00181       // get ponter of blog (copy to msg[n].blog.p)
00182       msg[n+2].blob.p=p;
00183       //p += 4*(msg[n+2].blob.len/4+1);       
00184       p += lenAlign4B(msg[n+2].blob.len+1);
00185     } 
00186     else if ('m'==c) {
00187       // get midi data (copy to msg[n].m[])
00188       msg[n+2].m[0]=p[0]; 
00189       msg[n+2].m[1]=p[1]; 
00190       msg[n+2].m[2]=p[2];
00191       msg[n+2].m[3]=p[3];
00192       p +=4;  
00193     } 
00194     else {
00195       //printf("*** Not Supported TypeTag:%s ****\n",typeTag);
00196     }
00197  };
00198 };