Table of Contents

Ethernet

Necessary knowledge: [HW] Controller module

The following part of library contains HomeLab Ethernet functions.

Constants

  • static uint8_t mymac[6] = {0x54,0x55,0x58,0x10,0x00,0x24} - controller MAC address (you can choose a random address, but it is important to be unique in the network).
  • static uint8_t myip[4] = {192,168,1,100} - IP address corresponding to LAN, you must choose different last digit, if it is already present in the network.
  • static uint8_t buf[BUFFER_SIZE+1] - size of data buffer (usually 600+1 B).

Functions

  • void ethernet_standby(void)

Brings Ethernet controller into sleep mode (low energy consumption).

  • void ethernet_init(uint8_t *mac, uint8_t *ip)

Ethernet controller initial setup with MAC and IP addresses.

  Parameters:
  * //*mac// - MAC address array.
  * //*ip// - IP address array.
  • uint16_t ethernet_get_packet (uint16_t maxlen, uint8_t* packet)

Polls network until it receives packet addressed to it and containing data.

  Parameters:
  * //maxlen// - maximum allowed data buffer size.
  * //*buffer// - array where to save received data for further operations.
  * If the packet has arrived, returns the length of the received packet in bytes and in opposite case returns 0.
  • uint16_t ethernet_analyse_packet(uint8_t *buf,uint16_t plen)

Analyzes if received packet contains info about URL. Will respond to ping if needed.

  Parameters:
  * //*buf// - data array to analyze.
  * //plen// - length of received packet in bytes.
  * Tagastab URL-i esimese tähemärgi aadressi. Kui URL on tühi, siis tagastab 0.
  * Returns the address of the first char in URL. If URL is empty, it will return 0.
  • uint16_t ethernet_load_data (uint8_t *buf,uint16_t pos, const prog_char *progmem_s)

Loads HTML-code for displaying webpage from controller program memory into TCP send buffer.

  Parameters:
  * //*buf// - data arrya to send using TCP.
  * //pos// - data end address in buffer which must be maintained if data is loaded in queue. The first address is 0.
  * //*progmem_s// - string name that is saved into program memory and from where data is loaded into send array.
  * Retruns end address of data array which can be input as a parameter for loading next HTML-code.
  • void ethernet_print_webpage (uint8_t *buf,uint16_t dlen)

Dislays webpage according to preloaded HTML-code.

  Parameters:
  * //*buf// - Data array where HTLM is preloaded.
  * //dplen// - End address of data in buffer to be displayed as HTML-code.

 

Example

#include <string.h>
#include <homelab/module/ethernet.h>
#include <homelab/pin.h>
 
//	Function for compiling webpage
uint16_t ethernet_load_webpage(uint8_t on_off);
 
// MAC and IP must be unique in network,
// same addresses in two devices are not allowed.
// Random MAC address (usually no need to change it)
static uint8_t mymac[6] = {0x54,0x55,0x58,0x10,0x00,0x24};
 
// IP according to LAN (you need to choose the last number)
static uint8_t myip[4] = {192,168,1,100};
 
//length of data array
static uint16_t gPlen; 
 
//Data buffer size
#define BUFFER_SIZE 600
static uint8_t buf[BUFFER_SIZE+1];
 
//LED PB7 variable
pin debug_led = PIN(B, 7);
 
 
int main(void)
{
 
	uint16_t pktlen;
	char *str;  
 
	// LED PB7 is output
	pin_setup_output(debug_led);
 
	// Etherneti initialization with given addresses
	ethernet_init(mymac, myip);			
 
        while(1)
	{
	// Receive packets until it receives addressed to him with data
    	pktlen=ethernet_get_packet(BUFFER_SIZE, buf);
 
        // Performs packet preconditioning and answer to "ping" packet. 
        // Returns packet URL.
        gPlen=ethernet_analyse_packet(buf,pktlen);
 
	// If URL contains info, start analyzing it
        if (gPlen!=0)
	{               
            // Load URL address part into "str". 
            // The first 4 digits are IP address numbers.
            str = (char *)&(buf[gPlen+4]);
 
			// Find string "/1" from URL              
			if (strncmp("/1",str,2)==0)
			{
	                        // Load webpage
				gPlen = ethernet_load_webpage(0);
 
				// LED on
				pin_clear(debug_led);		
 
	                 }
			// At next, find string "/0" from URL
			else if (strncmp("/0",str,2)==0)
			{
	                        // Load webpage
				gPlen = ethernet_load_webpage(1);
 
				// LED on
				pin_set(debug_led);
 
	                 }
			// In other cases load webpage according to LED condition
	                else 
			{	           
	                   gPlen=ethernet_load_webpage(pin_get_value(debug_led));
	                }                                    
 
			// Display preloaded webpage                
                        ethernet_print_webpage (buf,gPlen);    			            
		}
    }
    return (0);
}
 
 
// Webpage will be loaded by writing data into TCP send buffer
uint16_t ethernet_load_webpage(uint8_t on_off)
{
    uint16_t plen=0; 
 
	// Load HTML-code into buffer to send it
	// Those large strings are saved into program memory using PSTR macro,
	// to save SRAM
 
	// Load webpage header
 
	plen=ethernet_load_data(buf,0,PSTR
	("HTTP/1.0 200 OK\r\nContent-Type: text/html\r\nPragma: no-cache\r\n\r\n"));
 
	plen=ethernet_load_data(buf,plen,PSTR("<center><p>Homelab PB7 LED: "));
 
    // if LED is off, display it
    if (on_off)
    {
      plen=ethernet_load_data(buf,plen,PSTR("<font color=\"#00FF00\"> OFF</font>"));
    }
    // if LED is on
    else
    {
      plen=ethernet_load_data(buf,plen,PSTR("<font color=\"#FF0000\"> ON</font>"));
    }
    // Lae "Refresh" nupp
    plen=ethernet_load_data(buf,plen,PSTR
    (" <small><a href=\".\">[refresh status]</a></small></p>\n<p><a href=\"."));
 
    // Load the button for LED condition change.
    if (on_off)
    {
        plen=ethernet_load_data(buf,plen,PSTR("/1\">Switch on</a><p>"));
 
    }
    else
    {
         plen=ethernet_load_data(buf,plen,PSTR("/0\">Switch off</a><p>"));
 
    } 
 
    // Return the end address of data in buffer
    return(plen);
}
en/software/homelab/library/module/ethernet.txt · Last modified: 2020/07/20 09:00 by 127.0.0.1
CC Attribution-Share Alike 4.0 International
www.chimeric.de Valid CSS Driven by DokuWiki do yourself a favour and use a real browser - get firefox!! Recent changes RSS feed Valid XHTML 1.0