|
Step 1: Code Example for the Ethernet Interface |
Top Previous Next |
This and other projects in the Code Examples section are published on our website under the name "test_dhcp_lib".
Here is a simple example of using DHCP on the Ethernet (net.) interface. Notice how the callback_dhcp_ok() procedure is implemented! This is a good illustration of the protracted explanation in the Operations Details topic (not that we think you will read it all).
Note also the use of the NET_AVAILABLE define. This comes from the platform. If the Ethernet interface is available on the currently selected platform, then NET_AVAILABLE is "1". Similarly, if the Wi-Fi interface is available, then WLN_AVAILABLE is "1". Using such defines you can create applications that will compile on any platform, as long as this platform has at least the Ethernet, or the Wi-Fi interface.
Here is a sample debug output we've got after running the code:
DHCP(net)> ---START--- DHCP(net)> TX discovery message DHCP(net)> RX offer message DHCP(net)> TX request message DHCP(net)> RX confirmation message DHCP(net)> ---OK(ip: 192.168.1.246, gateway: 192.168.1.1, netmask: 255.255.255.0, lease: 529200 sec.)---
|
|
If you are compiling on one of the "W" platforms (that is, platforms with Wi-Fi), then you need to disable the wln. object, or this project won't compile. Go Project -> Settings -> Customize and set Wi-Fi (wln.) object to "Disabled". |
And here is the code itself...
global.tbh: |
'DEFINES------------------------------------------------------------- #define DHCP_DEBUG_PRINT 1 'to see debug output by the DHCP lib
'INCLUDES------------------------------------------------------------ include "sock\trunk\sock.tbh" 'this lib is necessary for the DHCP lib's operation include "dhcp\trunk\dhcp.tbh"
|
main.tbs: |
include "global.tbh"
'==================================================================== sub on_sys_init() #if NET_AVAILABLE dhcp_start(PL_SOCK_INTERFACE_NET,"","") #endif end sub
'-------------------------------------------------------------------- sub on_sys_timer() dhcp_proc_timer() end sub
'-------------------------------------------------------------------- sub on_sock_data_arrival() dhcp_proc_data() end sub
|
device.tbs: |
include "global.tbh"
'==================================================================== sub callback_dhcp_ok(renew as no_yes, interface as pl_sock_interfaces, byref ip as string, byref gateway_ip as string, byref netmask as string, lease_time as dword) dim f as byte
#if NET_AVAILABLE if interface=PL_SOCK_INTERFACE_NET then if renew=YES and net.ip<>ip then 'this is a lease renewal and the DHCP server has issues new IP 'it is better to reboot than deal with the implications of the changed IP sys.reboot end if
if net.ip<>ip then 'no need to close sockets -- this is definitely NOT a renewal an the DHCP lib 'has already closed all socket connections on this interface prior to setting 'the IP to 0.0.0.0 net.ip=ip net.gatewayip=gateway_ip net.netmask=netmask end if end if #endif end sub
'-------------------------------------------------------------------- sub callback_dhcp_failure(interface as pl_sock_interfaces,failure_code as en_dhcp_status_codes) end sub
'-------------------------------------------------------------------- sub callback_dhcp_pre_clear_ip(interface as pl_sock_interfaces) end sub
'-------------------------------------------------------------------- sub callback_dhcp_pre_buffrq(required_buff_pages as byte) end sub
'-------------------------------------------------------------------- sub callback_dhcp_buff_released() end sub
|