comparison network/TCPIP.c @ 107:56d09a0969b5 avr-http

Import uIP and the PPP implementation from https://code.google.com/p/avrusbmodem/
author Matt Johnston <matt@ucc.asn.au>
date Fri, 07 Sep 2012 23:53:53 +0800
parents
children
comparison
equal deleted inserted replaced
106:5c8404549cc0 107:56d09a0969b5
1 /*
2 LUFA Powered Wireless 3G Modem Host
3
4 Copyright (C) Mike Alexander, 2010.
5 Copyright (C) Dean Camera, 2010.
6 */
7
8 /*
9 Copyright 2010 Mike Alexander (mike [at] mikealex [dot] com)
10 Copyright 2010 Dean Camera (dean [at] fourwalledcubicle [dot] com)
11
12 Permission to use, copy, modify, distribute, and sell this
13 software and its documentation for any purpose is hereby granted
14 without fee, provided that the above copyright notice appear in
15 all copies and that both that the copyright notice and this
16 permission notice and warranty disclaimer appear in supporting
17 documentation, and that the name of the author not be used in
18 advertising or publicity pertaining to distribution of the
19 software without specific, written prior permission.
20
21 The author disclaim all warranties with regard to this
22 software, including all implied warranties of merchantability
23 and fitness. In no event shall the author be liable for any
24 special, indirect or consequential damages or any damages
25 whatsoever resulting from loss of use, data or profits, whether
26 in an action of contract, negligence or other tortious action,
27 arising out of or in connection with the use or performance of
28 this software.
29 */
30
31 #define INCLUDE_FROM_TCPIP_C
32 #include "TCPIP.h"
33
34 uint8_t IPAddr1, IPAddr2, IPAddr3, IPAddr4;
35 static uip_ipaddr_t RemoteIPAddress;
36 static struct uip_conn* TCPConnection;
37 static struct timer Periodic_Timer;
38
39 bool TCPIP_Connect(void)
40 {
41 // Connect to the remote machine (www.example.com)
42 uip_ipaddr(&RemoteIPAddress, 192, 0, 32, 10);
43 TCPConnection = uip_connect(&RemoteIPAddress, UIP_HTONS(80));
44
45 if (TCPConnection != NULL)
46 {
47 Debug_Print("Connecting to host\r\n");
48 return true;
49 }
50 else
51 {
52 Debug_Print("Failed to Connect\r\n");
53 return false;
54 }
55 }
56
57 void TCPIP_TCPCallback(void)
58 {
59 if (uip_acked())
60 Debug_Print("[ACK] ");
61
62 if (uip_newdata())
63 {
64 Debug_Print("New Data:\r\n");
65 TCPIP_QueueData(uip_appdata, uip_datalen());
66
67 if (TCPIP_IsDataQueueFull())
68 uip_stop();
69 }
70
71 if (uip_connected())
72 {
73 Debug_Print("Connected - Maximum Segment Size: 0x"); Debug_PrintHex(uip_mss() / 256); Debug_PrintHex(uip_mss() & 255);
74 Debug_Print("\r\n");
75 }
76
77 if (uip_closed())
78 {
79 Debug_Print("Closed - Reconnecting...");
80 _delay_ms(1000);
81 ConnectedState = LINKMANAGEMENT_STATE_ConnectToRemoteHost;
82 }
83
84 if (uip_aborted())
85 {
86 Debug_Print("Aborted - Reconnecting... ");
87 _delay_ms(1000);
88 ConnectedState = LINKMANAGEMENT_STATE_ConnectToRemoteHost;
89 }
90
91 if (uip_timedout())
92 {
93 Debug_Print("Timeout - Reconnecting...");
94 uip_abort();
95 _delay_ms(1000);
96 ConnectedState = LINKMANAGEMENT_STATE_ConnectToRemoteHost;
97 }
98
99 if (uip_poll() && (SystemTicks > 3000))
100 {
101 SystemTicks = 0;
102
103 Debug_Print("\r\nSending GET\r\n");
104 TCPIP_SendGET();
105 }
106
107 if (uip_rexmit())
108 {
109 Debug_Print("\r\nRetransmit GET\r\n");
110 TCPIP_SendGET();
111 }
112
113 if (uip_poll() && uip_stopped(TCPConnection))
114 {
115 if (!(TCPIP_IsDataQueueFull()))
116 uip_restart();
117 }
118 }
119
120 static void TCPIP_SendGET(void)
121 {
122 const char GETRequest[] = "GET / HTTP/1.1\r\n"
123 "Host: www.example.com\r\n"
124 "Connection: Keep-Alive\r\n\r\n";
125
126 uip_send(GETRequest, strlen(GETRequest));
127 }
128
129 static void TCPIP_QueueData(const char* Data,
130 const uint16_t Length)
131 {
132 if (Length > 0)
133 WatchdogTicks = 0; // Reset the timeout counter
134
135 for (uint16_t i = 0; i < Length; i++)
136 putchar(Data[i]);
137
138 Debug_Print("\r\n");
139 }
140
141 static bool TCPIP_IsDataQueueFull(void)
142 {
143 return false;
144 }
145
146 void TCPIP_InitializeTCPStack(void)
147 {
148 Debug_Print("Init TCP Stack\r\n");
149
150 // Periodic Connection Timer Initialization
151 timer_set(&Periodic_Timer, CLOCK_SECOND / 2);
152
153 // uIP Initialization
154 network_init();
155 clock_init();
156 uip_init();
157
158 // Set this machine's IP address
159 uip_ipaddr_t LocalIPAddress;
160 uip_ipaddr(&LocalIPAddress, IPAddr1, IPAddr2, IPAddr3, IPAddr4);
161 uip_sethostaddr(&LocalIPAddress);
162
163 ConnectedState = LINKMANAGEMENT_STATE_ConnectToRemoteHost;
164 SystemTicks = 2000; // Make the first CONNECT happen straight away
165 }
166
167 void TCPIP_ConnectToRemoteHost(void)
168 {
169 if (SystemTicks > 1000) // Try to connect every 1 second
170 {
171 SystemTicks = 0;
172
173 if (TCPIP_Connect())
174 {
175 SystemTicks = 3001; // Make the first GET happen straight away
176 uip_len = 0;
177 ConnectedState = LINKMANAGEMENT_STATE_ManageTCPConnection;
178 }
179 }
180 }
181
182 void TCPIP_GotNewPacket(void)
183 {
184 uip_input(); // Call the TCP/IP stack with the new packet
185
186 if (uip_len > 0) // If the above function invocation resulted in data that should be sent out
187 network_send(IP); // on the network, the global variable uip_len is set to a value > 0.
188 }
189
190 void TCPIP_TCPIPTask(void)
191 {
192 if (timer_expired(&Periodic_Timer))
193 {
194 timer_reset(&Periodic_Timer);
195
196 Debug_PrintChar('*');
197
198 for (uint8_t i = 0; i < UIP_CONNS; i++)
199 {
200 uip_periodic(i);
201
202 if (uip_len > 0) // If the above function invocation resulted in data that should be sent out on the network,
203 network_send(IP); //the global variable uip_len is set to a value > 0.
204 }
205 }
206 }