Control of mbed using OSC. Based on code from the Make Controller. Right now you can turn the onboard LEDs on/off and toggle 8 digital out pins. More I/O will be done in the future.
mbed_system_osc.cpp@0:439354122597, 2010-03-17 (annotated)
- Committer:
- pehrhovey
- Date:
- Wed Mar 17 03:17:38 2010 +0000
- Revision:
- 0:439354122597
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
pehrhovey | 0:439354122597 | 1 | /* |
pehrhovey | 0:439354122597 | 2 | * Pehr Hovey |
pehrhovey | 0:439354122597 | 3 | * |
pehrhovey | 0:439354122597 | 4 | * mBed OSC - SystemOsc subsystem |
pehrhovey | 0:439354122597 | 5 | * Get/set info about the system like ip address, hostname |
pehrhovey | 0:439354122597 | 6 | * Based on code from Make Controller |
pehrhovey | 0:439354122597 | 7 | */ |
pehrhovey | 0:439354122597 | 8 | /********************************************************************************* |
pehrhovey | 0:439354122597 | 9 | |
pehrhovey | 0:439354122597 | 10 | Copyright 2006-2009 MakingThings |
pehrhovey | 0:439354122597 | 11 | |
pehrhovey | 0:439354122597 | 12 | Licensed under the Apache License, |
pehrhovey | 0:439354122597 | 13 | Version 2.0 (the "License"); you may not use this file except in compliance |
pehrhovey | 0:439354122597 | 14 | with the License. You may obtain a copy of the License at |
pehrhovey | 0:439354122597 | 15 | |
pehrhovey | 0:439354122597 | 16 | http://www.apache.org/licenses/LICENSE-2.0 |
pehrhovey | 0:439354122597 | 17 | |
pehrhovey | 0:439354122597 | 18 | Unless required by applicable law or agreed to in writing, software distributed |
pehrhovey | 0:439354122597 | 19 | under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR |
pehrhovey | 0:439354122597 | 20 | CONDITIONS OF ANY KIND, either express or implied. See the License for |
pehrhovey | 0:439354122597 | 21 | the specific language governing permissions and limitations under the License. |
pehrhovey | 0:439354122597 | 22 | |
pehrhovey | 0:439354122597 | 23 | *********************************************************************************/ |
pehrhovey | 0:439354122597 | 24 | |
pehrhovey | 0:439354122597 | 25 | #include "mbed_system_osc.h" |
pehrhovey | 0:439354122597 | 26 | #include "osc_sys.h" |
pehrhovey | 0:439354122597 | 27 | #include "mbed_osc.h" //to get the netserver |
pehrhovey | 0:439354122597 | 28 | #include "mbed.h" |
pehrhovey | 0:439354122597 | 29 | |
pehrhovey | 0:439354122597 | 30 | static char* SystemOsc_Name = "system"; //the OSC container for this address |
pehrhovey | 0:439354122597 | 31 | static char* SystemOsc_PropertyNames[] = { "serialnumber", "version","name", "ipinfo", "mac","hostname", |
pehrhovey | 0:439354122597 | 32 | 0 }; // must have a trailing 0 |
pehrhovey | 0:439354122597 | 33 | |
pehrhovey | 0:439354122597 | 34 | int SystemOsc_PropertySet( int property, char* typedata, int channel ); |
pehrhovey | 0:439354122597 | 35 | int SystemOsc_PropertyGet( int property, int channel ); |
pehrhovey | 0:439354122597 | 36 | |
pehrhovey | 0:439354122597 | 37 | const char* SystemOsc_GetName( void ) |
pehrhovey | 0:439354122597 | 38 | { |
pehrhovey | 0:439354122597 | 39 | return SystemOsc_Name; |
pehrhovey | 0:439354122597 | 40 | } |
pehrhovey | 0:439354122597 | 41 | // need to allow this to accept non-int arguments |
pehrhovey | 0:439354122597 | 42 | int SystemOsc_ReceiveMessage( int channel, char* message, int length ) |
pehrhovey | 0:439354122597 | 43 | { |
pehrhovey | 0:439354122597 | 44 | //printf("SystemOsc received a message: %s\r\n",message); |
pehrhovey | 0:439354122597 | 45 | int status = Osc_GeneralReceiverHelper( channel, message, length, |
pehrhovey | 0:439354122597 | 46 | SystemOsc_Name, |
pehrhovey | 0:439354122597 | 47 | SystemOsc_PropertySet, SystemOsc_PropertyGet, |
pehrhovey | 0:439354122597 | 48 | SystemOsc_PropertyNames ); |
pehrhovey | 0:439354122597 | 49 | |
pehrhovey | 0:439354122597 | 50 | if ( status != CONTROLLER_OK ){ |
pehrhovey | 0:439354122597 | 51 | printf("SystemOsc error, status is: %d\r\n",status); |
pehrhovey | 0:439354122597 | 52 | return Osc_SendError( channel, SystemOsc_Name, status ); |
pehrhovey | 0:439354122597 | 53 | } |
pehrhovey | 0:439354122597 | 54 | |
pehrhovey | 0:439354122597 | 55 | return CONTROLLER_OK; |
pehrhovey | 0:439354122597 | 56 | } |
pehrhovey | 0:439354122597 | 57 | |
pehrhovey | 0:439354122597 | 58 | int SystemOsc_Poll( ) |
pehrhovey | 0:439354122597 | 59 | { |
pehrhovey | 0:439354122597 | 60 | return CONTROLLER_OK; |
pehrhovey | 0:439354122597 | 61 | } |
pehrhovey | 0:439354122597 | 62 | |
pehrhovey | 0:439354122597 | 63 | // Set the index'd property, property with the value |
pehrhovey | 0:439354122597 | 64 | //Not everything can be set |
pehrhovey | 0:439354122597 | 65 | int SystemOsc_PropertySet( int property, char* typedata, int channel ) |
pehrhovey | 0:439354122597 | 66 | { |
pehrhovey | 0:439354122597 | 67 | //printf("SysOSC property SET: %d data: %s\r\n",property, typedata); |
pehrhovey | 0:439354122597 | 68 | int value = 0; |
pehrhovey | 0:439354122597 | 69 | switch ( property ) |
pehrhovey | 0:439354122597 | 70 | { |
pehrhovey | 0:439354122597 | 71 | |
pehrhovey | 0:439354122597 | 72 | case 0: // serialnumber |
pehrhovey | 0:439354122597 | 73 | { |
pehrhovey | 0:439354122597 | 74 | int count = Osc_ExtractData( typedata, "i", &value ); |
pehrhovey | 0:439354122597 | 75 | if ( count != 1 ) |
pehrhovey | 0:439354122597 | 76 | return Osc_SubsystemError( channel, SystemOsc_Name, "Incorrect data - need an int" ); |
pehrhovey | 0:439354122597 | 77 | |
pehrhovey | 0:439354122597 | 78 | //nothing yet System_SetSerialNumber( value ); |
pehrhovey | 0:439354122597 | 79 | break; |
pehrhovey | 0:439354122597 | 80 | } |
pehrhovey | 0:439354122597 | 81 | case 1: //version |
pehrhovey | 0:439354122597 | 82 | { |
pehrhovey | 0:439354122597 | 83 | |
pehrhovey | 0:439354122597 | 84 | break; |
pehrhovey | 0:439354122597 | 85 | } |
pehrhovey | 0:439354122597 | 86 | case 2: // name |
pehrhovey | 0:439354122597 | 87 | { |
pehrhovey | 0:439354122597 | 88 | int count = Osc_ExtractData( typedata, "s", &value ); |
pehrhovey | 0:439354122597 | 89 | if ( count != 1 ) |
pehrhovey | 0:439354122597 | 90 | return Osc_SubsystemError( channel, SystemOsc_Name, "Incorrect data - need a string" ); |
pehrhovey | 0:439354122597 | 91 | |
pehrhovey | 0:439354122597 | 92 | break; |
pehrhovey | 0:439354122597 | 93 | } |
pehrhovey | 0:439354122597 | 94 | case 5: //hostname |
pehrhovey | 0:439354122597 | 95 | { |
pehrhovey | 0:439354122597 | 96 | char *newname; |
pehrhovey | 0:439354122597 | 97 | int count = Osc_ExtractData( typedata, "s", &newname ); |
pehrhovey | 0:439354122597 | 98 | if ( count != 1 ) |
pehrhovey | 0:439354122597 | 99 | return Osc_SubsystemError( channel, SystemOsc_Name, "Incorrect data - need a string" ); |
pehrhovey | 0:439354122597 | 100 | NetServer * net = getNetServer(); |
pehrhovey | 0:439354122597 | 101 | net->setHostname(newname); |
pehrhovey | 0:439354122597 | 102 | break; |
pehrhovey | 0:439354122597 | 103 | } |
pehrhovey | 0:439354122597 | 104 | |
pehrhovey | 0:439354122597 | 105 | } |
pehrhovey | 0:439354122597 | 106 | return CONTROLLER_OK; |
pehrhovey | 0:439354122597 | 107 | } |
pehrhovey | 0:439354122597 | 108 | //A hack, from mbed forums, to get MAC adress directly |
pehrhovey | 0:439354122597 | 109 | //This is useful since the NetServer doesnt seem to give direct access to the netif to get it |
pehrhovey | 0:439354122597 | 110 | extern "C" void mbed_mac_address(char *mac); |
pehrhovey | 0:439354122597 | 111 | |
pehrhovey | 0:439354122597 | 112 | // Get the value of a property |
pehrhovey | 0:439354122597 | 113 | int SystemOsc_PropertyGet( int property, int channel ) |
pehrhovey | 0:439354122597 | 114 | { |
pehrhovey | 0:439354122597 | 115 | //printf("SysOSC property GET: %d\r\n",property); |
pehrhovey | 0:439354122597 | 116 | int value = 0; |
pehrhovey | 0:439354122597 | 117 | char * oscaddr = getScratch(); //a reserved scratch space |
pehrhovey | 0:439354122597 | 118 | //Construct the return OSC-address |
pehrhovey | 0:439354122597 | 119 | snprintf( oscaddr, OSC_SCRATCH_SIZE, "/%s/%s", SystemOsc_Name, SystemOsc_PropertyNames[ property ] ); |
pehrhovey | 0:439354122597 | 120 | switch ( property ) |
pehrhovey | 0:439354122597 | 121 | { |
pehrhovey | 0:439354122597 | 122 | case 0: // serialnumber |
pehrhovey | 0:439354122597 | 123 | value = 12345;//hardcoded for now |
pehrhovey | 0:439354122597 | 124 | |
pehrhovey | 0:439354122597 | 125 | Osc_CreateMessage( channel, oscaddr, ",i", value ); |
pehrhovey | 0:439354122597 | 126 | break; |
pehrhovey | 0:439354122597 | 127 | case 1: // version |
pehrhovey | 0:439354122597 | 128 | { |
pehrhovey | 0:439354122597 | 129 | char versionString[50]; |
pehrhovey | 0:439354122597 | 130 | snprintf(versionString, 50, "%s",MBED_LIBRARY_VERSION); |
pehrhovey | 0:439354122597 | 131 | |
pehrhovey | 0:439354122597 | 132 | Osc_CreateMessage( channel, oscaddr, ",s", versionString ); |
pehrhovey | 0:439354122597 | 133 | break; |
pehrhovey | 0:439354122597 | 134 | } |
pehrhovey | 0:439354122597 | 135 | |
pehrhovey | 0:439354122597 | 136 | case 3: // ipinfo |
pehrhovey | 0:439354122597 | 137 | { |
pehrhovey | 0:439354122597 | 138 | NetServer * net = getNetServer(); |
pehrhovey | 0:439354122597 | 139 | struct ip_addr ipaddr = net->getIPAddr(); |
pehrhovey | 0:439354122597 | 140 | |
pehrhovey | 0:439354122597 | 141 | char ipstring[25]; |
pehrhovey | 0:439354122597 | 142 | snprintf(ipstring, 25,"%hhu.%hhu.%hhu.%hhu", (ipaddr.addr)&0xFF, (ipaddr.addr>>8)&0xFF, (ipaddr.addr>>16)&0xFF, (ipaddr.addr>>24)&0xFF); |
pehrhovey | 0:439354122597 | 143 | |
pehrhovey | 0:439354122597 | 144 | int port = UDP_RECV_PORT; //where is it listening? |
pehrhovey | 0:439354122597 | 145 | |
pehrhovey | 0:439354122597 | 146 | //snprintf( oscaddr, OSC_SCRATCH_SIZE, "/%s/%s", SystemOsc_Name, SystemOsc_PropertyNames[ property ] ); |
pehrhovey | 0:439354122597 | 147 | int rval = Osc_CreateMessage( channel, oscaddr, ",si", ipstring,port ); |
pehrhovey | 0:439354122597 | 148 | |
pehrhovey | 0:439354122597 | 149 | break; |
pehrhovey | 0:439354122597 | 150 | } |
pehrhovey | 0:439354122597 | 151 | case 4: // mac (MAC address of ethernet port) |
pehrhovey | 0:439354122597 | 152 | char mac[6]; |
pehrhovey | 0:439354122597 | 153 | |
pehrhovey | 0:439354122597 | 154 | mbed_mac_address(mac); // update mac to the unique mbed mac address |
pehrhovey | 0:439354122597 | 155 | |
pehrhovey | 0:439354122597 | 156 | char mac_str[50]; |
pehrhovey | 0:439354122597 | 157 | |
pehrhovey | 0:439354122597 | 158 | snprintf(mac_str,50,"%02x:%02x:%02x:%02x:%02x:%02x", |
pehrhovey | 0:439354122597 | 159 | (char*) mac[0], |
pehrhovey | 0:439354122597 | 160 | (char*) mac[1], |
pehrhovey | 0:439354122597 | 161 | (char*) mac[2], |
pehrhovey | 0:439354122597 | 162 | (char*) mac[3], |
pehrhovey | 0:439354122597 | 163 | (char*) mac[4], |
pehrhovey | 0:439354122597 | 164 | (char*) mac[5]); |
pehrhovey | 0:439354122597 | 165 | |
pehrhovey | 0:439354122597 | 166 | |
pehrhovey | 0:439354122597 | 167 | Osc_CreateMessage( channel, oscaddr, ",s", mac_str ); |
pehrhovey | 0:439354122597 | 168 | break; |
pehrhovey | 0:439354122597 | 169 | case 2: // name (same as hostname for now) |
pehrhovey | 0:439354122597 | 170 | case 5: // hostname |
pehrhovey | 0:439354122597 | 171 | { |
pehrhovey | 0:439354122597 | 172 | NetServer * net = getNetServer(); |
pehrhovey | 0:439354122597 | 173 | const char * hname = net->getHostname(); |
pehrhovey | 0:439354122597 | 174 | |
pehrhovey | 0:439354122597 | 175 | Osc_CreateMessage( channel, oscaddr, ",s", hname ); |
pehrhovey | 0:439354122597 | 176 | break; |
pehrhovey | 0:439354122597 | 177 | } |
pehrhovey | 0:439354122597 | 178 | |
pehrhovey | 0:439354122597 | 179 | } |
pehrhovey | 0:439354122597 | 180 | |
pehrhovey | 0:439354122597 | 181 | return CONTROLLER_OK; |
pehrhovey | 0:439354122597 | 182 | } |
pehrhovey | 0:439354122597 | 183 | |
pehrhovey | 0:439354122597 | 184 | |
pehrhovey | 0:439354122597 | 185 |