1 //*****************************************************************************
2 //
3 //! \file dhcp.h
4 //! \brief DHCP APIs Header file.
5 //! \details Processig DHCP protocol as DISCOVER, OFFER, REQUEST, ACK, NACK and DECLINE.
6 //! \version 1.1.0
7 //! \date 2013/11/18
8 //! \par  Revision history
9 //!       <2013/11/18> 1st Release
10 //!       <2012/12/20> V1.1.0
11 //!         1. Move unreferenced DEFINE to dhcp.c
12 //!       <2012/12/26> V1.1.1
13 //! \author Eric Jung & MidnightCow
14 //! \copyright
15 //!
16 //! Copyright (c)  2013, WIZnet Co., LTD.
17 //! All rights reserved.
18 //!
19 //! Redistribution and use in source and binary forms, with or without
20 //! modification, are permitted provided that the following conditions
21 //! are met:
22 //!
23 //!     * Redistributions of source code must retain the above copyright
24 //! notice, this list of conditions and the following disclaimer.
25 //!     * Redistributions in binary form must reproduce the above copyright
26 //! notice, this list of conditions and the following disclaimer in the
27 //! documentation and/or other materials provided with the distribution.
28 //!     * Neither the name of the <ORGANIZATION> nor the names of its
29 //! contributors may be used to endorse or promote products derived
30 //! from this software without specific prior written permission.
31 //!
32 //! THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
33 //! AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
34 //! IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
35 //! ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
36 //! LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
37 //! CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
38 //! SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
39 //! INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
40 //! CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
41 //! ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
42 //! THE POSSIBILITY OF SUCH DAMAGE.
43 //
44 //*****************************************************************************
45 #ifndef _DHCP_H_
46 #define _DHCP_H_
47 
48 /*
49  * @brief
50  * @details If you want to display debug & processing message, Define _DHCP_DEBUG_
51  * @note    If defined, it depends on <stdio.h>
52  */
53 
54 //#define _DHCP_DEBUG_
55 
56 /* Retry to processing DHCP */
57 #define	MAX_DHCP_RETRY          2        ///< Maximum retry count
58 #define	DHCP_WAIT_TIME          10       ///< Wait Time 10s
59 
60 /* UDP port numbers for DHCP */
61 #define DHCP_SERVER_PORT      		67	      ///< DHCP server port number
62 #define DHCP_CLIENT_PORT         	68	      ///< DHCP client port number
63 
64 #define MAGIC_COOKIE             0x63825363  ///< Any number. You can be modified it any number
65 
66 #define DCHP_HOST_NAME           "WIZnet\0"
67 
68 /*
69  * @brief return value of @ref DHCP_run()
70  */
71 enum
72 {
73    DHCP_FAILED = 0,  ///< Processing Fail
74    DHCP_RUNNING,     ///< Processing DHCP protocol
75    DHCP_IP_ASSIGN,   ///< First Occupy IP from DHPC server      (if cbfunc == null, act as default default_ip_assign)
76    DHCP_IP_CHANGED,  ///< Change IP address by new IP address from DHCP (if cbfunc == null, act as default default_ip_update)
77    DHCP_IP_LEASED,   ///< Stand by
78    DHCP_STOPPED      ///< Stop processing DHCP protocol
79 };
80 
81 /*
82  * @brief DHCP client initialization (outside of the main loop)
83  * @param s   - socket number
84  * @param buf - buffer for processing DHCP message
85  */
86 void DHCP_init(uint8_t s, uint8_t * buf);
87 
88 /*
89  * @brief DHCP 1s Tick Timer handler
90  * @note SHOULD BE register to your system 1s Tick timer handler
91  */
92 void DHCP_time_handler(void);
93 
94 /*
95  * @brief Register call back function
96  * @param ip_assign   - callback func when IP is assigned from DHCP server first
97  * @param ip_update   - callback func when IP is changed
98  * @prarm ip_conflict - callback func when the assigned IP is conflict with others.
99  */
100 void reg_dhcp_cbfunc(void(*ip_assign)(void), void(*ip_update)(void), void(*ip_conflict)(void));
101 
102 /*
103  * @brief DHCP client in the main loop
104  * @return    The value is as the follow \n
105  *            @ref DHCP_FAILED     \n
106  *            @ref DHCP_RUNNING    \n
107  *            @ref DHCP_IP_ASSIGN  \n
108  *            @ref DHCP_IP_CHANGED \n
109  * 			  @ref DHCP_IP_LEASED  \n
110  *            @ref DHCP_STOPPED    \n
111  *
112  * @note This function is always called by you main task.
113  */
114 uint8_t DHCP_run(void);
115 
116 /*
117  * @brief Stop DHCP processing
118  * @note If you want to restart. call DHCP_init() and DHCP_run()
119  */
120 void    DHCP_stop(void);
121 
122 /* Get Network information assigned from DHCP server */
123 /*
124  * @brief Get IP address
125  * @param ip  - IP address to be returned
126  */
127 void getIPfromDHCP(uint8_t* ip);
128 /*
129  * @brief Get Gateway address
130  * @param ip  - Gateway address to be returned
131  */
132 void getGWfromDHCP(uint8_t* ip);
133 /*
134  * @brief Get Subnet mask value
135  * @param ip  - Subnet mask to be returned
136  */
137 void getSNfromDHCP(uint8_t* ip);
138 /*
139  * @brief Get DNS address
140  * @param ip  - DNS address to be returned
141  */
142 void getDNSfromDHCP(uint8_t* ip);
143 
144 /*
145  * @brief Get the leased time by DHCP sever
146  * @return unit 1s
147  */
148 uint32_t getDHCPLeasetime(void);
149 
150 #endif	/* _DHCP_H_ */
151