annotate network/TCPIP.c @ 110:4eb5a746d7af avr-http

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