Shigeki KOMATSU / Mbed 2 deprecated Sparkfun_CC3000_WiFi_OSCtranceiver

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