NetServices

This documentation is for the Segundo Equipo version of the NetServices library.

This library is based on Donatien's source V1.04 (5 August 2010).

The following improvements and bug fixes are made:

#pragma diag_remark directives are added to the following files to turn compiler warnings into remarks (which are not normally displayed)

  • EthernetNetIf.cpp
  • lwipNetUdpSocket.cpp
  • igmp.c
  • mem.c
  • RPCHandler.cpp
  • MySQLClient.cpp
  • NTPClient.cpp

base64.h

  • fixed bug that prevented Base64::encode from working with a string with embedded null characters
  • required for authentication in SMTPClient

EmailMessage.cpp / .h

eth_drv.cpp / .h

  • only creates Ethernet object if required (init may be called without previous call to eth_free)
  • add method to allow access to Ethernet object pointer (may be null if not initialised)

Ethernet* eth_interface();

EthernetNetIf.cpp / .h

  • implemented getHwAddr() method to return array containing hardware address
  • turn off debug (uncomment line 34 in .cpp if required), IP address can be printed instead using getIp() method and hardware address using getHwAddr()

IpAddr ethIp = eth.getIp();
printf("Connected ok, IP : %d.%d.%d.%d\n", ethIp[0], ethIp[1], ethIp[2], ethIp[3]);
const char* hwAddr = eth.getHwAddr();
printf("HW address : %02x:%02x:%02x:%02x:%02x:%02x\n",
   hwAddr[0], hwAddr[1], hwAddr[2],
   hwAddr[3], hwAddr[4], hwAddr[5]);
  • optional hostname in DHCP constructor

// set hostname ready for DHCP
EthernetNetIf eth("mbedSE");

» Import this library into a program

Public Member Functions

  EthernetNetIf (const char *hostname=NULL)
  Instantiates the Interface and register it against the stack, DHCP will be used.
  EthernetNetIf ( IpAddr ip, IpAddr netmask, IpAddr gateway, IpAddr dns)
  Instantiates the Interface and register it against the stack, DHCP will not be used.
EthernetErr   setup (int timeout_ms=15000)
  Brings the interface up.
const char *  getHwAddr () const
  Returns an array containing the hardware address.
const char *  getHostname () const
  Returns a pointer to the hostname set in the constructor.
IpAddr   getIp () const
  Returns the IP of the interface once it's connected.
  • allow setup to be called multiple times by deleting and recreating network objects if previously setup
    • dhcp structure creation is moved to EthernetNetIf (to avoid memory leak) and passed to netif using dhcp_set_struct()
    • designed to be used so that setup can be called until successful as in the code sample below
    • note that behaviour when calling setup after a previous successful call is undefined

#define HOSTNAME "mbedSE"
EthernetNetIf eth(HOSTNAME);
EthernetErr ethErr;
do {
    printf("Setting up...\n");
    ethErr = eth.setup();
    if (ethErr) printf("Timeout\n", ethErr);
} while (ethErr != ETH_OK);

mem.c

  • added MEM_POSITION to ram_heap

memp.c

  • added MEM_POSITION to memp
  • added MEM_POSITION to memp_bases
  • added MEM_POSITION to memp_memory

lwipopts.h

  • turned off SIO_FIFO_DEBUG and TCPDUMP_DEBUG
  • defined LWIP_NETIF_HOSTNAME in DHCP options section

Warning

MEM_POSITION becomes effective (due to mem.c and memp.c changes) and is defined as AHBSRAM1 (not AHBSRAM0)

lwipopts2.h

  • deleted (not used)

netCfg.h

  • disabled all options apart from NET_ETH and NET_LWIP_STACK

NTPClient.cpp

  • modified code (line 150) so that time is adjusted to limit offset range only if necessary (otherwise unconditional adjustment to end of July 2010 is undesirable since NTP could subsequently fail)

if ((int)time(NULL) < 1280000000) set_time( 1280000000 ); //End of July 2010... just there to limit offset range

RPCHandler.cpp

  • modified cleanReq method to decode URLs (not just space and +) using url_decode already available in url.h

#include "url.h"
...
void RPCHandler::cleanReq(char* data)
{
    char* decoded = url_decode(data);
    strcpy(data, decoded);
    free(decoded);
/*    
  char* p;
  static const char* lGarbage[2] = {"%20", "+"};
  for(int i = 0; i < 2; i++)
  {
    while( p = strstr(data, lGarbage[i]) )
    {
      memset((void*) p, ' ', strlen(lGarbage[i]));
    }
  }
*/  
}

SMTPClient.cpp / .h

timers.c

  • reset next_timeout pointer on initialise (allows EthernetNetIf::setup to be called more than once)

void sys_timeouts_init(void)
{
  next_timeout = NULL;
...



1 related question:


6 comments:

22 Dec 2010

Using the NTPClient to set the current time seems to eat all available memory (less than 240 bytes left). I'm not really sure where this comes from, since I experience the malloc problem described here: http://mbed.org/forum/bugs-suggestions/topic/1593/ .

When trying to build a short program to reproduce this, I found out that it seems to be heavily influenced by the program layout (e.g. changing the text of my debug printf() message made the bug go away...).

What I'm observing is that after an ntp.setTime() call, the memory available for malloc drops below 240 bytes (which is the last malloc I see executing in the AvailableMemory() call). But it doesn't matter how much memory is available on the beginning - if I allocate 16000 bytes before the NTP call, the time is still set and I also get less than 240 bytes left (but I can free my 16000 bytes and be happy).

I also see similiar problems when using HTTPClient together with HTTPStream - I have plenty of memory available, but not afterwards (or, in my case, in the loop handling the received data).

This issue might be related to the malloc problem I linked to - maybe it's just the internal memory handling which gets messed up.

My test program is published as http://mbed.org/users/hlipka/programs/ntp_mem .

03 Jan 2011

user Hendrik Lipka wrote:

Using the NTPClient to set the current time seems to eat all available memory (less than 240 bytes left). I'm not really sure where this comes from, since I experience the malloc problem described here: http://mbed.org/forum/bugs-suggestions/topic/1593/ .

Additionally, I see a similiar behaviour when using an HTTP stream. I see the OOM problem when receiving data (when the polling is done). But here my workaround with pre-allocating some memory doesn't work, because I don't know the amount of free memory at this point...

10 Jan 2011

user Hendrik Lipka wrote:

Using the NTPClient to set the current time seems to eat all available memory (less than 240 bytes left).

It gets stranger: I have found out that this problem happens exactly the moment the NTPClient object gets deleted (the destructor is called) - but only when the object has actually been used. So either one of the used member objects does something wrong during destruction, or it really is a compiler bug.

06 Feb 2011

Is it possible to dynamically set the hostname under program control instead of using a #define? The constructor must be outside the scope of a function, and I would like to set the hostname just prior to initalizing the network under program control, in case there are multiple mbeds on a network and all run the identical software.

17 Aug 2011

The EthernetNetIf destructor crashes if the EthernetNetIf object was created using the non-DHCP constructor. The following patch fixes the issue:

  <NetServices>/if/eth/EthernetNetIf.cpp line 57
  
  m_useDhcp = false;
  m_pDhcp = NULL; /* <== added this line */
  m_setup = false;
25 Nov 2011

Am I the only one that have trouble finding a ethernet library that actually works.

Some crash on repeated initialization (memoryleak) while others cant be probed for available buffer size. (Calling send with -1 as length )

Some fixes are posted, but I cant seem to find the source to post it in.

All in all I find myself wasting lots of time.

What exact library (preferably source) do you guys use?