1 /**************************************************************************/
2 /**************************************************************************/
3 /** */
4 /** NetX PPPoE Client stack Component */
5 /** */
6 /** This is a small demo of the high-performance NetX PPPoE Client */
7 /** stack. This demo includes IP instance, PPPoE Client and PPP Client */
8 /** stack. Create one IP instance includes two interfaces to support */
9 /** for normal IP stack and PPPoE Client, PPPoE Client can use the */
10 /** mutex of IP instance to send PPPoE message when share one Ethernet */
11 /** driver. PPPoE Client work with normal IP instance at the same time. */
12 /** */
13 /** Note1: Substitute your Ethernet driver instead of */
14 /** _nx_ram_network_driver before run this demo */
15 /** */
16 /** Note2: Prerequisite for using PPPoE. */
17 /** Redefine NX_PHYSICAL_HEADER to 24 to ensure enough space for filling*/
18 /** in physical header. Physical header:14(Ethernet header) */
19 /** + 6(PPPoE header) + 2(PPP header) + 2(four-byte aligment) */
20 /** */
21 /**************************************************************************/
22 /**************************************************************************/
23
24
25 /*****************************************************************/
26 /* NetX Stack */
27 /*****************************************************************/
28
29 /***************************/
30 /* PPP Client */
31 /***************************/
32
33 /***************************/
34 /* PPPoE Client */
35 /***************************/
36 /***************************/ /***************************/
37 /* Normal Ethernet Type */ /* PPPoE Ethernet Type */
38 /***************************/ /***************************/
39 /***************************/ /***************************/
40 /* Interface 0 */ /* Interface 1 */
41 /***************************/ /***************************/
42
43 /*****************************************************************/
44 /* Ethernet Dirver */
45 /*****************************************************************/
46
47 #include "tx_api.h"
48 #include "nx_api.h"
49 #include "nx_ppp.h"
50 #include "nx_pppoe_client.h"
51
52 #ifndef NX_DISABLE_IPV4
53
54 /* Defined NX_PPP_PPPOE_ENABLE if using PPP, since PPP module has been modified to match PPPoE module under this definition. */
55 #ifdef NX_PPP_PPPOE_ENABLE
56
57 /* If the driver is not initialized in other module, define NX_PPPOE_CLIENT_INITIALIZE_DRIVER_ENABLE to initialize the driver in PPPoE module .
58 In this demo, the driver has been initialized in IP module. */
59 #ifndef NX_PPPOE_CLIENT_INITIALIZE_DRIVER_ENABLE
60
61 /* Define the block size. */
62 #define NX_PACKET_POOL_SIZE ((1536 + sizeof(NX_PACKET)) * 30)
63 #define DEMO_STACK_SIZE 2048
64 #define PPPOE_THREAD_SIZE 2048
65
66 /* Define the ThreadX and NetX object control blocks... */
67 TX_THREAD thread_0;
68
69 /* Define the packet pool and IP instance for normal IP instnace. */
70 NX_PACKET_POOL pool_0;
71 NX_IP ip_0;
72
73 /* Define the PPP Client instance. */
74 NX_PPP ppp_client;
75
76 /* Define the PPPoE Client instance. */
77 NX_PPPOE_CLIENT pppoe_client;
78
79 /* Define the counters. */
80 CHAR *pointer;
81 ULONG error_counter;
82
83 /* Define thread prototypes. */
84 void thread_0_entry(ULONG thread_input);
85
86 /***** Substitute your PPP driver entry function here *********/
87 extern void _nx_ppp_driver(NX_IP_DRIVER *driver_req_ptr);
88
89 /***** Substitute your Ethernet driver entry function here *********/
90 extern void _nx_ram_network_driver(NX_IP_DRIVER *driver_req_ptr);
91
92 /* Define the porting layer function for PPP */
93 void ppp_client_packet_send(NX_PACKET *packet_ptr);
94 void pppoe_client_packet_receive(NX_PACKET *packet_ptr);
95
96 /* Define main entry point. */
97
main()98 int main()
99 {
100
101 /* Enter the ThreadX kernel. */
102 tx_kernel_enter();
103 }
104
generate_login(CHAR * name,CHAR * password)105 UINT generate_login(CHAR *name, CHAR *password)
106 {
107
108 /* Make a name and password, called "myname" and "mypassword". */
109 name[0] = 'm';
110 name[1] = 'y';
111 name[2] = 'n';
112 name[3] = 'a';
113 name[4] = 'm';
114 name[5] = 'e';
115 name[6] = (CHAR) 0;
116
117 password[0] = 'm';
118 password[1] = 'y';
119 password[2] = 'p';
120 password[3] = 'a';
121 password[4] = 's';
122 password[5] = 's';
123 password[6] = 'w';
124 password[7] = 'o';
125 password[8] = 'r';
126 password[9] = 'd';
127 password[10] = (CHAR) 0;
128
129 return(NX_SUCCESS);
130 }
131
132 /* Define what the initial system looks like. */
133
tx_application_define(void * first_unused_memory)134 void tx_application_define(void *first_unused_memory)
135 {
136
137 UINT status;
138
139 /* Setup the working pointer. */
140 pointer = (CHAR *) first_unused_memory;
141
142 /* Initialize the NetX system. */
143 nx_system_initialize();
144
145 /* Create a packet pool for normal IP instance. */
146 status = nx_packet_pool_create(&pool_0, "NetX Main Packet Pool",
147 (1536 + sizeof(NX_PACKET)),
148 pointer, NX_PACKET_POOL_SIZE);
149 pointer = pointer + NX_PACKET_POOL_SIZE;
150
151 /* Check for error. */
152 if (status)
153 error_counter++;
154
155 /* Create an normal IP instance. */
156 status = nx_ip_create(&ip_0, "NetX IP Instance", IP_ADDRESS(192, 168, 100, 44), 0xFFFFFF00UL, &pool_0, _nx_ram_network_driver,
157 pointer, 2048, 1);
158 pointer = pointer + 2048;
159
160 /* Check for error. */
161 if (status)
162 error_counter++;
163
164 /* Create the PPP instance. */
165 status = nx_ppp_create(&ppp_client, "PPP Instance", &ip_0, pointer, 2048, 1, &pool_0, NX_NULL, NX_NULL);
166 pointer = pointer + 2048;
167
168 /* Check for PPP create error. */
169 if (status)
170 error_counter++;
171
172 /* Set the PPP packet send function. */
173 status = nx_ppp_packet_send_set(&ppp_client, ppp_client_packet_send);
174
175 /* Check for PPP packet send function set error. */
176 if (status)
177 error_counter++;
178
179 /* Define IP address. This PPP instance is effectively the client since it doesn't have any IP addresses. */
180 status = nx_ppp_ip_address_assign(&ppp_client, IP_ADDRESS(0, 0, 0, 0), IP_ADDRESS(0, 0, 0, 0));
181
182 /* Check for PPP IP address assign error. */
183 if (status)
184 error_counter++;
185
186 /* Setup PAP, this PPP instance is effectively the since it generates the name and password for the peer.. */
187 status = nx_ppp_pap_enable(&ppp_client, generate_login, NX_NULL);
188
189 /* Check for PPP PAP enable error. */
190 if (status)
191 error_counter++;
192
193 /* Attach an interface for PPP. */
194 status = nx_ip_interface_attach(&ip_0, "Second Interface For PPP", IP_ADDRESS(0, 0, 0, 0), 0, nx_ppp_driver);
195
196 /* Check for error. */
197 if (status)
198 error_counter++;
199
200 /* Enable ARP and supply ARP cache memory for Normal IP Instance. */
201 status = nx_arp_enable(&ip_0, (void *) pointer, 1024);
202 pointer = pointer + 1024;
203
204 /* Check for ARP enable errors. */
205 if (status)
206 error_counter++;
207
208 /* Enable ICMP */
209 status = nx_icmp_enable(&ip_0);
210 if(status)
211 error_counter++;
212
213 /* Enable UDP traffic. */
214 status = nx_udp_enable(&ip_0);
215 if (status)
216 error_counter++;
217
218 /* Enable TCP traffic. */
219 status = nx_tcp_enable(&ip_0);
220 if (status)
221 error_counter++;
222
223 /* Create the main thread. */
224 tx_thread_create(&thread_0, "thread 0", thread_0_entry, 0,
225 pointer, DEMO_STACK_SIZE,
226 4, 4, TX_NO_TIME_SLICE, TX_AUTO_START);
227 pointer = pointer + DEMO_STACK_SIZE;
228
229 }
230
231 /* Define the test threads. */
232
thread_0_entry(ULONG thread_input)233 void thread_0_entry(ULONG thread_input)
234 {
235 UINT status;
236 ULONG ip_status;
237
238 /* Create the PPPoE instance. */
239 status = nx_pppoe_client_create(&pppoe_client, (UCHAR *)"PPPoE Client", &ip_0, 0, &pool_0, pointer, PPPOE_THREAD_SIZE, 4, _nx_ram_network_driver, pppoe_client_packet_receive);
240 pointer = pointer + PPPOE_THREAD_SIZE;
241 if (status)
242 {
243 error_counter++;
244 return;
245 }
246
247 /* Establish PPPoE Client sessione. */
248 status = nx_pppoe_client_session_connect(&pppoe_client, NX_WAIT_FOREVER);
249 if (status)
250 {
251 error_counter++;
252 return;
253 }
254
255 /* Wait for the link to come up. */
256 status = nx_ip_interface_status_check(&ip_0, 1, NX_IP_ADDRESS_RESOLVED, &ip_status, NX_WAIT_FOREVER);
257 if (status)
258 {
259 error_counter++;
260 return;
261 }
262
263 /* Get the PPPoE Server physical address and Session ID after establish PPPoE Session. */
264 /*
265 status = nx_pppoe_client_session_get(&pppoe_client, &server_mac_msw, &server_mac_lsw, &session_id);
266 if (status)
267 error_counter++;
268 */
269 }
270
271 /* PPPoE Client receive function. */
pppoe_client_packet_receive(NX_PACKET * packet_ptr)272 void pppoe_client_packet_receive(NX_PACKET *packet_ptr)
273 {
274
275 /* Call PPP Client to receive the PPP data fame. */
276 nx_ppp_packet_receive(&ppp_client, packet_ptr);
277 }
278
279 /* PPP Client send function. */
ppp_client_packet_send(NX_PACKET * packet_ptr)280 void ppp_client_packet_send(NX_PACKET *packet_ptr)
281 {
282
283 /* Directly Call PPPoE send function to send out the data through PPPoE module. */
284 nx_pppoe_client_session_packet_send(&pppoe_client, packet_ptr);
285 }
286 #endif /* NX_PPPOE_CLIENT_INITIALIZE_DRIVER_ENABLE */
287
288 #endif /* NX_PPP_PPPOE_ENABLE */
289
290 #endif /* NX_DISABLE_IPV4 */
291