Modified official MBED EthernetInterface The only changes are to allow hostname to be set and got

Dependencies:   Socket lwip-eth lwip-sys lwip

Dependents:   SpideyWallWeb RdGasUseMonitor

Fork of EthernetInterface by mbed official

Files at this revision

API Documentation at this revision

Comitter:
Bobty
Date:
Mon Aug 31 08:58:07 2015 +0000
Parent:
49:2fc406e2553f
Commit message:
Added setName and getName to allow hostname to be changed

Changed in this revision

EthernetInterface.cpp Show annotated file Show diff for this revision Revisions of this file
EthernetInterface.h Show annotated file Show diff for this revision Revisions of this file
diff -r 2fc406e2553f -r 8e692841213c EthernetInterface.cpp
--- a/EthernetInterface.cpp	Thu Mar 12 16:58:57 2015 +0000
+++ b/EthernetInterface.cpp	Mon Aug 31 08:58:07 2015 +0000
@@ -40,6 +40,8 @@
 static Semaphore netif_linked(0);
 static Semaphore netif_up(0);
 
+static char myName[33];  // holds the name, when setName() is called.
+
 static void tcpip_init_done(void *arg) {
     tcpip_inited.release();
 }
@@ -153,4 +155,21 @@
     return networkmask;
 }
 
+int EthernetInterface::setName(const char * myname) {
+    strncpy(myName, myname, 32);
+    myName[32] = '\0';  // be sure it is NULL terminated.
+    // make the name 'safe'
+    for (int i=0; i<32 && myName[i]; i++) {
+        if (!(myName[i] >= '0' && myName[i] <= '9')
+        &&  !(myName[i] >= 'A' && myName[i] <= 'Z')
+        &&  !(myName[i] >= 'a' && myName[i] <= 'z'))
+            myName[i] = '-';
+    }
+    netif_set_hostname(&netif, myName);
+    return 0;
+}
 
+char* EthernetInterface::getName() {
+    return netif_get_hostname(&netif);
+
+}
diff -r 2fc406e2553f -r 8e692841213c EthernetInterface.h
--- a/EthernetInterface.h	Thu Mar 12 16:58:57 2015 +0000
+++ b/EthernetInterface.h	Mon Aug 31 08:58:07 2015 +0000
@@ -79,6 +79,26 @@
    * \return a pointer to a string containing the Network mask
    */
   static char* getNetworkMask();
+  
+  /** setName 
+  *
+  * Set the network name for this device. Apply this before
+  * calling 'connect'.
+  *
+  * \param myname is the name to assign for this node. 
+  *        Only the first 32 characters will be used if the 
+  *        name is longer.
+  *        Only '0'-'9', 'A'-'Z', 'a'-'z' are accepted,
+  *        any others are converted to '-'.
+  * \return 0 on success, a negative number on failure.
+  */
+  static int setName(const char * myname);
+  
+  /** Get the hostname
+  * \return a pointer to a string containing the hostname
+  */
+  static char* getName();
+
 };
 
 #include "TCPSocketConnection.h"