Pehr Hovey / Mbed 2 deprecated mbed_osc

Dependencies:   mbed

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers mbed_system_osc.cpp Source File

mbed_system_osc.cpp

00001 /*
00002  * Pehr Hovey
00003  *
00004  * mBed OSC - SystemOsc subsystem
00005  * Get/set info about the system like ip address, hostname
00006  * Based on code from Make Controller
00007  */
00008 /*********************************************************************************
00009 
00010  Copyright 2006-2009 MakingThings
00011 
00012  Licensed under the Apache License,
00013  Version 2.0 (the "License"); you may not use this file except in compliance
00014  with the License. You may obtain a copy of the License at
00015 
00016  http://www.apache.org/licenses/LICENSE-2.0
00017 
00018  Unless required by applicable law or agreed to in writing, software distributed
00019  under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
00020  CONDITIONS OF ANY KIND, either express or implied. See the License for
00021  the specific language governing permissions and limitations under the License.
00022 
00023 *********************************************************************************/
00024 
00025 #include "mbed_system_osc.h"
00026 #include "osc_sys.h"
00027 #include "mbed_osc.h" //to get the netserver
00028 #include "mbed.h"
00029 
00030 static char* SystemOsc_Name = "system"; //the OSC container for this address
00031 static char* SystemOsc_PropertyNames[] = { "serialnumber", "version","name", "ipinfo", "mac","hostname",
00032                                             0 }; // must have a trailing 0
00033 
00034 int SystemOsc_PropertySet( int property, char* typedata, int channel );
00035 int SystemOsc_PropertyGet( int property, int channel );
00036 
00037 const char* SystemOsc_GetName( void )
00038 {
00039   return SystemOsc_Name;
00040 }
00041 // need to allow this to accept non-int arguments
00042 int SystemOsc_ReceiveMessage( int channel, char* message, int length )
00043 {
00044   //printf("SystemOsc received a message: %s\r\n",message);
00045     int status = Osc_GeneralReceiverHelper( channel, message, length,
00046                                 SystemOsc_Name,
00047                                 SystemOsc_PropertySet, SystemOsc_PropertyGet,
00048                                 SystemOsc_PropertyNames );
00049 
00050   if ( status != CONTROLLER_OK ){
00051       printf("SystemOsc error, status is: %d\r\n",status);
00052       return Osc_SendError( channel, SystemOsc_Name, status );
00053   }
00054 
00055   return CONTROLLER_OK;
00056 }
00057 
00058 int SystemOsc_Poll( )
00059 {
00060   return CONTROLLER_OK;
00061 }
00062 
00063 // Set the index'd property, property with the value
00064 //Not everything can be set
00065 int SystemOsc_PropertySet( int property, char* typedata, int channel )
00066 {
00067     //printf("SysOSC property SET: %d data: %s\r\n",property, typedata);
00068     int value = 0;
00069   switch ( property )
00070   {
00071 
00072     case 0: // serialnumber
00073     {
00074       int count = Osc_ExtractData( typedata, "i", &value );
00075       if ( count != 1 )
00076         return Osc_SubsystemError( channel, SystemOsc_Name, "Incorrect data - need an int" );
00077 
00078       //nothing yet System_SetSerialNumber( value );
00079       break;
00080     }
00081     case 1: //version
00082     {
00083 
00084         break;
00085     }
00086     case 2: // name
00087     {
00088       int count = Osc_ExtractData( typedata, "s", &value );
00089       if ( count != 1 )
00090         return Osc_SubsystemError( channel, SystemOsc_Name, "Incorrect data - need a string" );
00091 
00092       break;
00093     }
00094     case 5: //hostname
00095     {
00096         char *newname;
00097         int count = Osc_ExtractData( typedata, "s", &newname );
00098         if ( count != 1 )
00099                 return Osc_SubsystemError( channel, SystemOsc_Name, "Incorrect data - need a string" );
00100         NetServer * net = getNetServer();
00101         net->setHostname(newname);
00102         break;
00103     }
00104 
00105   }
00106   return CONTROLLER_OK;
00107 }
00108 //A hack, from mbed forums, to  get MAC adress directly
00109 //This is useful since the NetServer doesnt seem to give direct access to the netif to get it
00110 extern "C" void mbed_mac_address(char *mac);
00111 
00112 // Get the value of a property
00113 int SystemOsc_PropertyGet( int property, int channel )
00114 {
00115     //printf("SysOSC property GET: %d\r\n",property);
00116     int value = 0;
00117     char * oscaddr =  getScratch(); //a reserved scratch space
00118     //Construct the return OSC-address
00119      snprintf( oscaddr, OSC_SCRATCH_SIZE, "/%s/%s", SystemOsc_Name, SystemOsc_PropertyNames[ property ] );
00120   switch ( property )
00121   {
00122     case 0: // serialnumber
00123       value = 12345;//hardcoded for now
00124 
00125       Osc_CreateMessage( channel, oscaddr, ",i", value );
00126       break;
00127     case 1: // version
00128     {
00129       char versionString[50];
00130       snprintf(versionString, 50, "%s",MBED_LIBRARY_VERSION);
00131 
00132       Osc_CreateMessage( channel, oscaddr, ",s", versionString );
00133       break;
00134     }
00135 
00136     case 3: // ipinfo
00137     {
00138         NetServer * net = getNetServer();
00139         struct ip_addr  ipaddr = net->getIPAddr();
00140 
00141         char ipstring[25];
00142         snprintf(ipstring, 25,"%hhu.%hhu.%hhu.%hhu", (ipaddr.addr)&0xFF, (ipaddr.addr>>8)&0xFF, (ipaddr.addr>>16)&0xFF, (ipaddr.addr>>24)&0xFF);
00143 
00144         int port = UDP_RECV_PORT; //where is it listening?
00145 
00146         //snprintf( oscaddr, OSC_SCRATCH_SIZE, "/%s/%s", SystemOsc_Name, SystemOsc_PropertyNames[ property ] );
00147          int rval = Osc_CreateMessage( channel, oscaddr, ",si", ipstring,port );
00148 
00149       break;
00150     }
00151     case 4: // mac (MAC address of ethernet port)
00152         char mac[6];
00153 
00154         mbed_mac_address(mac); // update mac to the unique mbed mac address
00155 
00156         char mac_str[50];
00157 
00158         snprintf(mac_str,50,"%02x:%02x:%02x:%02x:%02x:%02x",
00159                 (char*) mac[0],
00160                 (char*) mac[1],
00161                 (char*) mac[2],
00162                 (char*) mac[3],
00163                 (char*) mac[4],
00164                 (char*) mac[5]);
00165 
00166 
00167       Osc_CreateMessage( channel, oscaddr, ",s", mac_str );
00168       break;
00169     case 2: // name (same as hostname for now)
00170     case 5: // hostname
00171     {
00172         NetServer * net = getNetServer();
00173     const char * hname = net->getHostname();
00174 
00175     Osc_CreateMessage( channel, oscaddr, ",s", hname );
00176       break;
00177     }
00178 
00179   }
00180 
00181   return CONTROLLER_OK;
00182 }
00183 
00184 
00185