Step 2: Code Example for the Wi-Fi Interface

Top  Previous  Next

This and other projects in the Code Examples section are published on our website under the name "test_dhcp_lib".

 

Now let's add DHCP for the Wi-Fi interface (wln.) to the previous sample code.

In order to even get to the DHCP stage, the Wi-Fi interface must first associate with the access point. We use our own handy WLN library for that. Notice the use of the WLN_AVAILABLE define. This example does not address Wi-Fi WPA security, see Trying WPA for inspiration.

Sample debug output follows. Notice some error messages there. This is because our device, although working with two interfaces, was actually connected to the same LAN with the same DHCP server. DHCP messages are sent as broadcasts and both interfaces will get the same message. This results in one interface getting messages intended for the other interface and rejecting them as erroneous. This does not affect the operation of the library.

 

 

DHCP(net)> ---START---

DHCP(wln)> ---START---

DHCP(net)> TX discovery message

DHCP(net)> RX offer message

DHCP(net)> INFO: RX unexpected message (wasn't expecting anything at the moment)

DHCP(net)> INFO: RX unexpected, invalid, or unrelated message (it was discarded)

DHCP(net)> TX request message

DHCP(net)> RX confirmation message

DHCP(net)> ---OK(ip: 192.168.1.215, gateway: 192.168.1.1, netmask: 255.255.255.0, lease: 529200 sec.)---

DHCP(wln)> Wi-Fi interface associated with the AP -- DHCP (re)started

DHCP(wln)> TX discovery message

DHCP(wln)> RX offer message

DHCP(wln)> INFO: RX unexpected message (wasn't expecting anything at the moment)

DHCP(wln)> INFO: RX unexpected, invalid, or unrelated message (it was discarded)

DHCP(wln)> TX request message

DHCP(wln)> RX confirmation message

DHCP(wln)> ---OK(ip: 192.168.1.216, gateway: 192.168.1.1, netmask: 255.255.255.0, lease: 529200 sec.)---

 

 

note_warning-wt

If you disabled the wln. object in the previous step, the you need to enable it now. Plus, you need to test on one of the "W" platforms (that is, platforms with Wi-Fi). Go Project -> Settings -> Customize and set Wi-Fi (wln.) object to "Enabled".

 

And now the code (plus don't forget to add ga1000fw.bin into your project)...

 

global.tbh:

 

'DEFINES-------------------------------------------------------------

#define DHCP_DEBUG_PRINT 1 'to see debug output by the DHCP lib

 

'this is for WLN library

#if PLATFORM_ID=EM500 or PLATFORM_ID=EM500W

 #define WLN_RESET_MODE 1 'there will be no dedicated reset, and all other lines are fixed

#elif PLATFORM_ID=EM1206 or PLATFORM_ID=EM1206W

 #define WLN_CLK PL_IO_NUM_14

 #define WLN_CS PL_IO_NUM_15

 #define WLN_DI PL_IO_NUM_12

 #define WLN_DO PL_IO_NUM_13

 #define WLN_RST PL_IO_NUM_11

#else

 'EM1000, NB1010,...

 #define WLN_CLK PL_IO_NUM_53

 #define WLN_CS PL_IO_NUM_49

 #define WLN_DI PL_IO_NUM_52

 #define WLN_DO PL_IO_NUM_50

 #define WLN_RST PL_IO_NUM_51

#endif

 

'INCLUDES------------------------------------------------------------

include "sock\trunk\sock.tbh" 'this lib is necessary for the DHCP lib's operation

include "wln\trunk\wln.tbh" 'this lib is necessary for the DHCP on Wi-Fi interface

include "dhcp\trunk\dhcp.tbh"

 

 

main.tbs:

 

include "global.tbh"

 

'====================================================================

sub on_sys_init()

 

 #if NET_AVAILABLE

         dhcp_start(PL_SOCK_INTERFACE_NET,"","")

 #endif

 

 #if WLN_AVAILABLE

         wln_start("TIBB1",WLN_SECURITY_MODE_WEP64,"1234567890",PL_WLN_DOMAIN_FCC) '<-- set what you need here, see WLN lib docs

         dhcp_start(PL_SOCK_INTERFACE_WLN,"","")

 #endif

end sub

 

'--------------------------------------------------------------------

sub on_sys_timer()

 dhcp_proc_timer()

 

 #if WLN_AVAILABLE

         wln_proc_timer() 'see WLN lib docs

 #endif

end sub

 

'--------------------------------------------------------------------

sub on_sock_data_arrival()

 dhcp_proc_data()

 

 #if WLN_AVAILABLE

         wln_proc_data() 'see WLN lib docs

 #endif

end sub

 

'--------------------------------------------------------------------

#if WLN_AVAILABLE

sub on_wln_task_complete(completed_task as pl_wln_tasks)

 wln_proc_task_complete(completed_task) 'see WLN lib docs

end sub

#endif

 

'--------------------------------------------------------------------

#if WLN_AVAILABLE

sub on_wln_event(wln_event as pl_wln_events)

 wln_proc_event(wln_event)

end sub

#endif

 

 

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

 

 #if WLN_AVAILABLE

         if interface=PL_SOCK_INTERFACE_WLN then

                 if renew=YES and wln.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 wln.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

                         wln.ip=ip

                         wln.gatewayip=gateway_ip

                         wln.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

 

'--------------------------------------------------------------------

#if WLN_AVAILABLE

sub callback_wln_ok()

end sub

#endif

 

'--------------------------------------------------------------------

#if WLN_AVAILABLE

sub callback_wln_failure(wln_state as en_wln_status_codes)

end sub

#endif

 

'--------------------------------------------------------------------

#if WLN_AVAILABLE

sub callback_wln_pre_buffrq(required_buff_pages as byte)

end sub

#endif

 

'--------------------------------------------------------------------

#if WLN_AVAILABLE

sub callback_wln_rescan_result(current_rssi as byte, scan_rssi as byte, different_ap as no_yes)

end sub

#endif