Microchip® Advanced Software Framework

tinydhcpserver.c File Reference

Tiny DHCP Server.

Copyright (c) 2016-2018 Microchip Technology Inc. and its subsidiaries.

#include "conf_tinyservices.h"
#include "tinyservices.h"
#include "wifi_private.h"
#include "lwip/dhcp.h"
#include "lwip/tcpip.h"
#include <string.h>

Macros

#define LWIP_DHCP_MAX_CLIENT   8
 

Functions

static uint16_t lwip_dhcp_find_option (struct pbuf *pbuf_in, uint8_t option, uint8_t *opt_val, uint8_t *opt_size)
 Find an option in a DHCP packet. More...
 
static ip_addr_t lwip_dhcp_get_ip_address (uint8_t *mac)
 Find a IP address for MAC. More...
 
void lwip_dhcp_register_mac (uint8_t *mac)
 
static void lwip_dhcp_server_fn (void *arg, struct udp_pcb *dhcp_pcb_recv, struct pbuf *pbuf_in, struct ip_addr *client_addr, uint16_t port)
 Receive DHCP discover, request and inform packets and respond to them by supplying one IP Address for the client and setting the DNS server address. More...
 
void lwip_dhcp_unregister_mac (uint8_t *mac)
 
static void lwip_dhcpserver_start_threadsafe (void *arg)
 Start the Tiny DNS Server. More...
 
void lwip_tiny_dhcpserver_start (void)
 Start the Tiny DHCP Server. More...
 
void lwip_tiny_dhcpserver_stop (void)
 Stop a Tiny DHCP Server. More...
 
static void lwip_tiny_dhcpserver_stop_threadsafe (void *arg)
 Stop a Tiny DHCP Server. More...
 

Variables

static struct udp_pcbdhcp_pcb = 0
 
static ip_addr_t ip_addr_c_broadcast = { 0xff05a8c0 }
 
static uint8_t lwip_dhcp_known_mac [LWIP_DHCP_MAX_CLIENT][6]
 

static uint16_t lwip_dhcp_find_option ( struct pbuf pbuf_in,
uint8_t  option,
uint8_t *  opt_val,
uint8_t *  opt_size 
)
static

Find an option in a DHCP packet.

Parameters
pbuf_inthe pbuf containing the a DHCP packet.
optionan unsigned char representing the option to search for.
opt_vala pointer to place the value of the option located.
opt_sizea pointer to the size of the option located.
Returns
1 if option found, 0 if option not found.

References DHCP_OPTIONS_OFS, pbuf_copy_partial(), and pbuf_get_at().

Referenced by lwip_dhcp_server_fn().

static ip_addr_t lwip_dhcp_get_ip_address ( uint8_t *  mac)
static

Find a IP address for MAC.

Returns
IP address if available, NULL if not.

References lwip_dhcp_known_mac, and LWIP_DHCP_MAX_CLIENT.

Referenced by lwip_dhcp_server_fn().

void lwip_dhcp_register_mac ( uint8_t *  mac)
static void lwip_dhcp_server_fn ( void arg,
struct udp_pcb dhcp_pcb_recv,
struct pbuf pbuf_in,
struct ip_addr client_addr,
uint16_t  port 
)
static
void lwip_dhcp_unregister_mac ( uint8_t *  mac)
static void lwip_dhcpserver_start_threadsafe ( void arg)
static

Start the Tiny DNS Server.

Create a new UDP PCB, bind that PCB to the DHCP_SERVER_PORT and set up a receiver for that PCB.

This variant of the start function must only be called from within LWIP's main thread.

This function must be invoked via lwip_tiny_dhcpserver_start

References Assert, DHCP_SERVER_PORT, IP_ADDR_ANY, lwip_dhcp_known_mac, LWIP_DHCP_MAX_CLIENT, lwip_dhcp_server_fn(), udp_bind(), udp_new(), udp_recv(), and UNUSED.

Referenced by lwip_tiny_dhcpserver_start().

void lwip_tiny_dhcpserver_start ( void  )

Start the Tiny DHCP Server.

Start Tiny DHCP server.

Create a new UDP PCB, bind that PCB to the DHCP_SERVER_PORT and set up a receiver for that PCB.

Thread-safe variant of this function. This function posts the LWIP-thread-only variant of this function (with dummy argument) to the LWIP thread's mailbox and returns.

NOTE: This function cannot wait. It may be called from within the LWIP main thread of execution.

References lwip_dhcpserver_start_threadsafe(), and tcpip_callback.

Referenced by net_interface_up_imp().

void lwip_tiny_dhcpserver_stop ( void  )

Stop a Tiny DHCP Server.

Stop Tiny DHCP server.

Disconnect and remove the UDP PCB.

Thread-safe variant of this function. This function posts the lwip-main-thread only function and a dummy argument to the main lwip thread.

NOTE: This function cannot wait. It may be called from within the LWIP main thread of execution.

References lwip_tiny_dhcpserver_stop_threadsafe(), and tcpip_callback.

Referenced by net_interface_down_imp().

static void lwip_tiny_dhcpserver_stop_threadsafe ( void arg)
static

Stop a Tiny DHCP Server.

Disconnect and remove the UDP PCB.

This function must only be called from within LWIP's main thread. It must be invoked via lwip_tiny_dhcp_server_stop.

References udp_disconnect(), udp_remove(), and UNUSED.

Referenced by lwip_tiny_dhcpserver_stop().

struct udp_pcb* dhcp_pcb = 0
static
ip_addr_t ip_addr_c_broadcast = { 0xff05a8c0 }
static

Referenced by lwip_dhcp_server_fn().