David Smart / WiflyInterface Featured

Dependents:   Smart-WiFly-WebServer PUB_WiflyInterface_Demo

Fork of WiflyInterface by mbed official

Embed: (wiki syntax)

« Back to documentation index

EthernetInterface Class Reference

EthernetInterface Class Reference

Interface using Wifly to connect to an IP-based network. More...

#include <EthernetInterface.h>

Inherits Wifly.

Public Member Functions

 EthernetInterface (PinName tx, PinName rx, PinName reset, PinName tcp_status, const char *ssid=NULL, const char *phrase=NULL, Security sec=NONE)
 Construct the WiFlyInterface.
int init ()
 Initialize the interface with DHCP.
int init (const char *ip, const char *mask, const char *gateway)
 Initialize the interface with a static IP address.
void setSecurity (const char *ssid=NULL, const char *phrase=NULL, Security sec=NONE)
 Set security parameters for the interface.
int connect ()
 Connect Bring the interface up, start DHCP if needed.
int disconnect ()
 Disconnect Bring the interface down.
char * getMACAddress ()
 Get the MAC address of your Ethernet interface.
char * getIPAddress ()
 Get IP address.
const char * getName (void)
 getName
int get_connection_speed (void)
 get the speed of the connection.
void SetSecurity (const char *ssid, const char *phrase, Security sec)
 Optional means to set/reset the security.
bool configure ()
 Configure the wifi module with the parameters contained in the constructor.
bool join ()
 Connect the wifi module to the ssid contained in the constructor.
bool connect (const char *host, int port)
 Open a tcp connection with the specified host on the specified port.
bool setProtocol (Protocol p)
 Set the protocol (UDP or TCP)
void reset ()
 Reset the wifi module.
bool reboot ()
 Reboot the wifi module.
int readable ()
 Check if characters are available.
int writeable ()
 Check if characters are available.
bool is_connected ()
 Check if associated with an access point (was checking if tcp link is active)
char getc ()
 Read a character.
void flush ()
 Flush the buffer.
int putc (char c)
 Write a character.
bool cmdMode ()
 To enter in command mode (we can configure the module)
bool exit ()
 To exit the command mode.
bool close ()
 Close a tcp connection, and exit command mode.
int send (const char *str, int len, const char *ACK=NULL, char *res=NULL, int timeout=DEFAULT_WAIT_RESP_TIMEOUT)
 Send a string to the wifi module by serial port.
bool sendCommand (const char *cmd, const char *ack=NULL, char *res=NULL, int timeout=DEFAULT_WAIT_RESP_TIMEOUT)
 Put the device in command mode.
bool isDHCP ()
 Return true if the module is using dhcp.
bool gethostbyname (const char *host, char *ip)
 gets the host ip address
bool baud (int baudrate)
 Set the baud rate between the ARM and the WiFly.
void setConnectionState (bool state)
 Sets the connection state.
char * getWiflyVersionString ()
 Get the version information from the Wifly module.
float getWiflyVersion ()
 Get the software version from the Wifly module.
bool SWUpdateWifly (const char *file)
 Update the software in the Wifly module.
bool isCmdMode ()
 determine if the module is in command mode

Protected Member Functions

bool FixPhrase (char *dst, size_t dstLen, const char *src)
 Allocate space for the parameter (ssid or passphrase) and then fix it (change ' ' to '$').
void GatherLogonInfo ()
 Gather the Wifly module version information for later queries.

Detailed Description

Interface using Wifly to connect to an IP-based network.

Definition at line 36 of file EthernetInterface.h.


Constructor & Destructor Documentation

EthernetInterface ( PinName  tx,
PinName  rx,
PinName  reset,
PinName  tcp_status,
const char *  ssid = NULL,
const char *  phrase = NULL,
Security  sec = NONE 
)

Construct the WiFlyInterface.

API Precedence:

    * EthernetInterface() -->
    *            +------------------+              +---------------------------+
    *            v                  ^              v                           ^
    * -> init() -+----------------->+-> connect() -+-------------------------->+-> disconnect() --|
    *            |                  ^              +-> getName() --------------+
    *            +-> setName() -----+              +-> getIPAddress() ---------+
    *            |                  |              +-> getMACAddress() --------+
    *            +-> setSecurity() -+              +-> Wifly APIs -------------+
    *                                              +-> get_connection_speed() -+
    * 
Note:
The optional ssid, phrase, and sec parameters may be deferred, or overridden prior to the connect().
Parameters:
txmbed pin to use for tx line of Serial interface
rxmbed pin to use for rx line of Serial interface
resetreset pin of the wifi module ()
tcp_statusconnection status pin of the wifi module (GPIO 6)
ssidoptional parameter which is the ssid of the network
phraseoptional parameter which is the WEP or WPA key/phrase
secoptional security type (NONE[default], WEP_128 or WPA)

Definition at line 24 of file EthernetInterface.cpp.


Member Function Documentation

bool baud ( int  baudrate ) [inherited]

Set the baud rate between the ARM and the WiFly.

This will set the WiFly module baud rate first and then set the ARM interface to match it. If it cannot get the proper acknowledge response, it will go on a hunt through the range of standard baud rates.

Note:
Baud rate must be one of 2400, 4800, 9600, 19200, 38400, 57600, 115200, 230400, 460800, or 921600. (See Wifly manual 2.3.64)
Baud rate of 230400 has been seen to be marginal w/o flow control.
Setting a baud rate to a 460800 or above may be unrecoverable without resetting the Wifly module.
Parameters:
baudrateis the desired baudrate.
Returns:
true if it succeeded, which means that communications can continue,
false if it failed to establish a communication link.

Definition at line 726 of file Wifly.cpp.

bool close (  ) [inherited]

Close a tcp connection, and exit command mode.

Returns:
true if successful

Definition at line 518 of file Wifly.cpp.

bool cmdMode (  ) [inherited]

To enter in command mode (we can configure the module)

Returns:
true if successful, false otherwise

Definition at line 394 of file Wifly.cpp.

bool configure (  ) [inherited]

Configure the wifi module with the parameters contained in the constructor.

Returns:
true if successful, false otherwise.

Definition at line 85 of file Wifly.cpp.

int connect (  )

Connect Bring the interface up, start DHCP if needed.

Returns:
0 on success, a negative number on failure

Definition at line 60 of file EthernetInterface.cpp.

bool connect ( const char *  host,
int  port 
) [inherited]

Open a tcp connection with the specified host on the specified port.

Parameters:
hosthost (can be either an ip address or a name. If a name is provided, a dns request will be established)
portport
Returns:
true if successful

Definition at line 269 of file Wifly.cpp.

int disconnect (  )

Disconnect Bring the interface down.

Returns:
0 on success, a negative number on failure

Reimplemented from Wifly.

Definition at line 65 of file EthernetInterface.cpp.

bool exit (  ) [inherited]

To exit the command mode.

Returns:
true if successful, false otherwise

Definition at line 549 of file Wifly.cpp.

bool FixPhrase ( char *  dst,
size_t  dstLen,
const char *  src 
) [protected, inherited]

Allocate space for the parameter (ssid or passphrase) and then fix it (change ' ' to '$').

Parameters:
dstis a reference to the private member pointer.
dstLenis the size of the destination buffer
srcis a pointer to a passed in string.
Returns:
true if the src phrase was placed in the dst buffer.

Definition at line 768 of file Wifly.cpp.

void flush (  ) [inherited]

Flush the buffer.

Definition at line 349 of file Wifly.cpp.

void GatherLogonInfo (  ) [protected, inherited]

Gather the Wifly module version information for later queries.

Definition at line 786 of file Wifly.cpp.

int get_connection_speed ( void   )

get the speed of the connection.

Returns:
the connection speed, which can range from 1 to 54 (Mb/s)
0 if the information could not be acquired.

Definition at line 168 of file EthernetInterface.cpp.

char getc (  ) [inherited]

Read a character.

Returns:
the character read

Definition at line 573 of file Wifly.cpp.

bool gethostbyname ( const char *  host,
char *  ip 
) [inherited]

gets the host ip address

Parameters:
hostis a pointer to the host string to look up
ipcontains the host IP in a string after the lookup.
sizeof_ipis the size of the buffer pointed to by ip
Returns:
true if successful

Definition at line 304 of file Wifly.cpp.

char * getIPAddress (  )

Get IP address.

Returns:
a pointer to a string containing the IP address

Definition at line 108 of file EthernetInterface.cpp.

char * getMACAddress (  )

Get the MAC address of your Ethernet interface.

Returns:
a pointer to a string containing the MAC address

Definition at line 73 of file EthernetInterface.cpp.

const char * getName ( void   )

getName

Get the network name for this device.

Returns:
pointer to the name (or null)

Definition at line 163 of file EthernetInterface.cpp.

float getWiflyVersion (  ) [inherited]

Get the software version from the Wifly module.

This extracts the basic version number (e.g. 2.38, 4.00) as a float.

Returns:
the software version number as a float.

Definition at line 823 of file Wifly.cpp.

char * getWiflyVersionString (  ) [inherited]

Get the version information from the Wifly module.

Returns:
the version information as a string, or NULL

Definition at line 830 of file Wifly.cpp.

int init ( const char *  ip,
const char *  mask,
const char *  gateway 
)

Initialize the interface with a static IP address.

Initialize the interface and configure it with the following static configuration (no connection at this point).

Parameters:
ipthe IP address to use
maskthe IP address mask
gatewaythe gateway to use
Returns:
0 on success, a negative number on failure

Definition at line 42 of file EthernetInterface.cpp.

int init (  )

Initialize the interface with DHCP.

Initialize the interface and configure it to use DHCP (no connection at this point).

Returns:
0 on success, a negative number on failure

Definition at line 33 of file EthernetInterface.cpp.

bool is_connected ( void   ) [inherited]

Check if associated with an access point (was checking if tcp link is active)

Follow this example to improve the automatic recovery after a lost association.

 WiflyInterface eth(p28, p27, p23, p24, "ssid", "pass", WPA);
 
 if (0 == eth.init()) {
     eth.baud(230400);     // speed up the interface
     do {
         if (0 == eth.connect()) {
             linkup = true;    // led indicator
             while (eth.is_connected()) {
                 wait_ms(1000);
             }
             linkup = false;   // led indicator
             eth.disconnect();
             wait_ms(1000);
         }
     } while (1);
 } else {
     wait(5);    // ... failed to initialize, rebooting...
     mbed_reset();
 }
Returns:
true if successful

Definition at line 470 of file Wifly.cpp.

bool isCmdMode (  ) [inherited]

determine if the module is in command mode

Returns:
true if in command mode, false otherwise

Definition at line 373 of file Wifly.h.

bool isDHCP (  ) [inherited]

Return true if the module is using dhcp.

Returns:
true if the module is using dhcp

Definition at line 289 of file Wifly.h.

bool join (  ) [inherited]

Connect the wifi module to the ssid contained in the constructor.

Returns:
true if connected, false otherwise

Definition at line 200 of file Wifly.cpp.

int putc ( char  c ) [inherited]

Write a character.

Parameters:
thecharacter which will be written
Returns:
the character written

Definition at line 541 of file Wifly.cpp.

int readable (  ) [inherited]

Check if characters are available.

Returns:
number of available characters

Definition at line 561 of file Wifly.cpp.

bool reboot (  ) [inherited]

Reboot the wifi module.

Returns:
true if it could send the command, false otherwise

Definition at line 503 of file Wifly.cpp.

void reset (  ) [inherited]

Reset the wifi module.

Definition at line 491 of file Wifly.cpp.

int send ( const char *  str,
int  len,
const char *  ACK = NULL,
char *  res = NULL,
int  timeout = DEFAULT_WAIT_RESP_TIMEOUT 
) [inherited]

Send a string to the wifi module by serial port.

This function desactivates the user interrupt handler when a character is received to analyze the response from the wifi module.

Useful to send a command to the module and wait a response.

Parameters:
strstring to be sent
lenstring length
ACKstring which must be acknowledge by the wifi module. If ACK == NULL, no string has to be acknowledged. (default: NULL)
resthis field will contain the response from the wifi module, result of a command sent. This field is available only if ACK == NULL AND res != NULL (default: NULL)
timeoutis the time in msec to wait for the acknowledge
Returns:
true if ACK has been found in the response from the wifi module.
false if there is not a correct response in the timeout.

Definition at line 600 of file Wifly.cpp.

bool sendCommand ( const char *  cmd,
const char *  ack = NULL,
char *  res = NULL,
int  timeout = DEFAULT_WAIT_RESP_TIMEOUT 
) [inherited]

Put the device in command mode.

Parameters:
strstring to be sent
ACKstring which must be acknowledge by the wifi module. If ACK == NULL, no string has to be acknoledged. (default: "NO")
resthis field will contain the response from the wifi module, result of a command sent. This field is available only if ACK = "NO" AND res != NULL (default: NULL)
timeoutis the time in msec to wait for the acknowledge
Returns:
true if successful, false if it failed.

Definition at line 368 of file Wifly.cpp.

void setConnectionState ( bool  state ) [inherited]

Sets the connection state.

Typically used by external apps that detect an incoming connection.

Parameters:
statesets the connection state to true or false

Definition at line 866 of file Wifly.cpp.

bool setProtocol ( Protocol  p ) [inherited]

Set the protocol (UDP or TCP)

Parameters:
pprotocol
Returns:
true if successful

Definition at line 219 of file Wifly.cpp.

void setSecurity ( const char *  ssid = NULL,
const char *  phrase = NULL,
Security  sec = NONE 
)

Set security parameters for the interface.

Prior to connect, this interface permits changing the security parameters. This can be most useful when the Wifly module may be used where there are multiple access points to be connected to. With this, the interface can be brought online far enough to scan for available access points.

Parameters:
ssidoptional parameter which is the ssid of the network
phraseoptional parameter which is the WEP or WPA key/phrase
secoptional security type (NONE[default], WEP_128 or WPA)

Definition at line 55 of file EthernetInterface.cpp.

void SetSecurity ( const char *  ssid,
const char *  phrase,
Security  sec 
) [inherited]

Optional means to set/reset the security.

If join() has not been called, then you can revise the security parameters from those in the constructor.

Parameters:
ssidssid of the network
phraseWEP or WPA key
secSecurity type (NONE, WEP_128, WPA1, WPA | WPA_MIXED, WPA2_PSK, WEP_64 )

Definition at line 76 of file Wifly.cpp.

bool SWUpdateWifly ( const char *  file ) [inherited]

Update the software in the Wifly module.

This attempts to connect to the microchip site, download a software update, install it as the primary image, and reboot to activate that image. It is compile-time defined to try for 30 seconds.

Parameters:
fileis the name of the file to fetch, to update to. The strlen(file) cannot exceed 60, and is typically like this: "Wifly7-475.mif"
Returns:
true if success or false for failure.

Definition at line 836 of file Wifly.cpp.

int writeable (  ) [inherited]

Check if characters are available.

Returns:
number of available characters

Definition at line 567 of file Wifly.cpp.