|
| 1 | +# Wiznet 5500 DHCP |
| 2 | + |
| 3 | +This library class enables Dynamic Host Configuration Protocol (*DHCP*) functionality for the Wiznet W5500 chip [W5500](http://wizwiki.net/wiki/lib/exe/fetch.php?media=products:w5500:w5500_ds_v106e_141230.pdf). It also requires the Wiznet W5500 driver. |
| 4 | +**To add this code to your project, add `#require W5500.DHCP.device.nut:1.0.0` after `#require W5500.device.nut:1.0.0` to the top of your device code.** |
| 5 | + |
| 6 | +## Class W5500.DHCP |
| 7 | + |
| 8 | +### Constructor: W5500.DHCP(*wiz*) |
| 9 | +Instantiates a new W5500.DHCP object and passes in the wiznet main driver. |
| 10 | + |
| 11 | +#### Example Code: |
| 12 | +```squirrel |
| 13 | +// Setup for an Imp 005 |
| 14 | +// Initialise SPI port |
| 15 | +interruptPin <- hardware.pinXC; |
| 16 | +resetPin <- hardware.pinXA; |
| 17 | +spiSpeed <- 1000; |
| 18 | +spi <- hardware.spi0; |
| 19 | +spi.configure(CLOCK_IDLE_LOW | MSB_FIRST | USE_CS_L, spiSpeed); |
| 20 | +
|
| 21 | +// Initialise Wiznet and DHCP |
| 22 | +wiz <- W5500(interruptPin, spi, null, resetPin); |
| 23 | +dhcp <- W5500.DHCP(wiz); |
| 24 | +``` |
| 25 | + |
| 26 | +## Class Methods |
| 27 | + |
| 28 | +### onLease(*callback*) |
| 29 | +The *onLease()* method sets the function to be called when an IP address is leased. It can be used before or after `dhcp.init()` is called. The callback function has an error parameter which informs the user of timeouts and errors. |
| 30 | + |
| 31 | +#### Parameters |
| 32 | +| Key | Data Type | Required | Default Value | Description | |
| 33 | +| -------------------- | ----------- | -------- | ------------- | -------------------------------------------------------------- | |
| 34 | +| *callback* | `function`| Yes| N/A| The function to be fired when a DHCP lease is established | |
| 35 | + |
| 36 | +#### Callback Parameters |
| 37 | +| Key | Data Type |Description | |
| 38 | +| -----------| ----------- |------------------------------------------------- | |
| 39 | +| *error* | `string` | A string error message | |
| 40 | + |
| 41 | +#### Error Messages |
| 42 | +|Error Message | Description | |
| 43 | +|-------------------------------|---------------------------------------------| |
| 44 | +|Offer Timeout |Discovery message sent with no response | |
| 45 | +|Ack Timeout |Request message sent with no response | |
| 46 | +|Renewal Timeout |Max Renewal attempts reached. Restart. | |
| 47 | +|Renewal Failed |Lease renewal failed | |
| 48 | +|Request Declined |Requested IP not able to be leased | |
| 49 | +|IP in use |Offered IP is currently in use | |
| 50 | +|All connections in use |All Wiznet sockets are in use | |
| 51 | + |
| 52 | + |
| 53 | + |
| 54 | +#### Example Code: |
| 55 | +```squirrel |
| 56 | +dhcp.onLease(function(error) { |
| 57 | + if (error) return server.error(error); |
| 58 | +}); |
| 59 | +dhcp.renewLease(); |
| 60 | +``` |
| 61 | + |
| 62 | +### renewLease() |
| 63 | +The *renewLease()* method renews the lease or requests a new lease. When this is complete the onLease() callback will be called |
| 64 | + |
| 65 | +#### Example Code: |
| 66 | +```squirrel |
| 67 | +dhcp.onLease(function(error) { |
| 68 | + // Run this code when IP address is obtained |
| 69 | +}); |
| 70 | +dhcp.renewLease(); |
| 71 | +``` |
| 72 | + |
| 73 | + |
| 74 | +### getIP() |
| 75 | +The *getIP()* method returns the leased IP as an array of four integers. |
| 76 | + |
| 77 | +#### Example Code: |
| 78 | +```squirrel |
| 79 | +local ip = dhcp.getIP(); |
| 80 | +server.log(format("ip = %d.%d.%d.%d", ip[0], ip[1], ip[2], ip[3])) |
| 81 | +``` |
| 82 | + |
| 83 | + |
| 84 | +### getSubnetMask() |
| 85 | +The *getSubnetMask()* method returns the DNS as an array of four integers. |
| 86 | + |
| 87 | +#### Example Code: |
| 88 | +```squirrel |
| 89 | +local subnet_mask = dhcp.getSubnetMask(); |
| 90 | +server.log(format("subnet mask = %d.%d.%d.%d", subnet_mask[0], subnet_mask[1], subnet_mask[2], subnet_mask[3])) |
| 91 | +``` |
| 92 | + |
| 93 | + |
| 94 | +### getRouterAddress() |
| 95 | +The *getRouterAddress()* method returns the gateway address as an array of four integers. |
| 96 | + |
| 97 | +#### Example Code: |
| 98 | +```squirrel |
| 99 | +local router = dhcp.getRouterAddress(); |
| 100 | +server.log(format("router = %d.%d.%d.%d", router[0], router[1], router[2], router[3])) |
| 101 | +``` |
| 102 | + |
| 103 | + |
| 104 | +### getLeaseTime() |
| 105 | +The *getLeaseTime()* method returns the lease duration as an integer. |
| 106 | + |
| 107 | +#### Example Code: |
| 108 | +```squirrel |
| 109 | +local leasetime = dhcp.getLeaseTime(); |
| 110 | +``` |
| 111 | + |
| 112 | + |
| 113 | +### getDNS() |
| 114 | +The *getDNS()* method returns the DNS as an array of DNS entries, each an array of four integers. |
| 115 | + |
| 116 | +#### Example Code: |
| 117 | +```squirrel |
| 118 | +local dns = dhcp.getDNS(); |
| 119 | +``` |
| 120 | + |
| 121 | + |
| 122 | + |
| 123 | +## Extended Example on IMP0005 |
| 124 | +View [example.device.nut](example.device.nut) for an extended example. |
| 125 | + |
| 126 | +## License |
| 127 | +The Wiznet code is licensed under the [MIT License](./LICENSE). |
0 commit comments