/[H8]/trunk/docs/Microchip TCP_IP stack/TCPIP Stack/GenericTCPClient.c
ViewVC logotype

Contents of /trunk/docs/Microchip TCP_IP stack/TCPIP Stack/GenericTCPClient.c

Parent Directory Parent Directory | Revision Log Revision Log


Revision 15 - (show annotations) (download)
Thu Apr 19 09:01:15 2007 UTC (17 years, 1 month ago) by hedin
File MIME type: text/plain
File size: 7554 byte(s)
added the TCP/IP stack, source code.
1 /*********************************************************************
2 *
3 * Generic TCP Client Example Application
4 * Module for Microchip TCP/IP Stack
5 * -Implements an example HTTP client and should be used as a basis
6 * for creating new TCP client applications
7 * -Reference: None. Hopefully AN833 in the future.
8 *
9 *********************************************************************
10 * FileName: GenericTCPClient.c
11 * Dependencies: TCP.h, DNS.h
12 * Processor: PIC18, PIC24F, PIC24H, dsPIC30, dsPIC33F
13 * Complier: Microchip C18 v3.03 or higher
14 * Microchip C30 v2.02 or higher
15 * Company: Microchip Technology, Inc.
16 *
17 * Software License Agreement
18 *
19 * Copyright © 2002-2007 Microchip Technology Inc. All rights
20 * reserved.
21 *
22 * Microchip licenses to you the right to use, modify, copy, and
23 * distribute:
24 * (i) the Software when embedded on a Microchip microcontroller or
25 * digital signal controller product (“Device”) which is
26 * integrated into Licensee’s product; or
27 * (ii) ONLY the Software driver source files ENC28J60.c and
28 * ENC28J60.h ported to a non-Microchip device used in
29 * conjunction with a Microchip ethernet controller for the
30 * sole purpose of interfacing with the ethernet controller.
31 *
32 * You should refer to the license agreement accompanying this
33 * Software for additional information regarding your rights and
34 * obligations.
35 *
36 * THE SOFTWARE AND DOCUMENTATION ARE PROVIDED “AS IS” WITHOUT
37 * WARRANTY OF ANY KIND, EITHER EXPRESS OR IMPLIED, INCLUDING WITHOUT
38 * LIMITATION, ANY WARRANTY OF MERCHANTABILITY, FITNESS FOR A
39 * PARTICULAR PURPOSE, TITLE AND NON-INFRINGEMENT. IN NO EVENT SHALL
40 * MICROCHIP BE LIABLE FOR ANY INCIDENTAL, SPECIAL, INDIRECT OR
41 * CONSEQUENTIAL DAMAGES, LOST PROFITS OR LOST DATA, COST OF
42 * PROCUREMENT OF SUBSTITUTE GOODS, TECHNOLOGY OR SERVICES, ANY CLAIMS
43 * BY THIRD PARTIES (INCLUDING BUT NOT LIMITED TO ANY DEFENSE
44 * THEREOF), ANY CLAIMS FOR INDEMNITY OR CONTRIBUTION, OR OTHER
45 * SIMILAR COSTS, WHETHER ASSERTED ON THE BASIS OF CONTRACT, TORT
46 * (INCLUDING NEGLIGENCE), BREACH OF WARRANTY, OR OTHERWISE.
47 *
48 *
49 * Author Date Comment
50 *~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
51 * Howard Schlunder 8/01/06 Original
52 ********************************************************************/
53 #define __GENERICTCPCLIENT_C
54
55 #include "TCPIP Stack/TCPIP.h"
56
57 #if defined(STACK_USE_GENERIC_TCP_CLIENT_EXAMPLE)
58
59
60 //TODO: Define proper server address here
61 BYTE ServerName[] = "www.google.com";
62 WORD ServerPort = 80;
63
64 // This is specific to this HTTP Client example
65 ROM BYTE RemoteURL[] = "/search?as_q=Microchip&as_sitesearch=microchip.com";
66
67
68 /*********************************************************************
69 * Function: void GenericTCPClient(void)
70 *
71 * PreCondition: Stack is initialized()
72 *
73 * Input: None
74 *
75 * Output: None
76 *
77 * Side Effects: None
78 *
79 * Overview: None
80 *
81 * Note: None
82 ********************************************************************/
83 void GenericTCPClient(void)
84 {
85 BYTE i;
86 BYTE *StringPtr;
87 static TICK Timer;
88 static TCP_SOCKET MySocket = INVALID_SOCKET;
89 static NODE_INFO Server;
90 static enum _GenericTCPExampleState
91 {
92 SM_HOME = 0,
93 SM_NAME_RESOLVE,
94 SM_ARP_START_RESOLVE,
95 SM_ARP_RESOLVE,
96 SM_SOCKET_OBTAIN,
97 SM_SOCKET_OBTAINED,
98 SM_PROCESS_RESPONSE,
99 SM_DISCONNECT,
100 SM_DONE
101 } GenericTCPExampleState = SM_DONE;
102
103 switch(GenericTCPExampleState)
104 {
105 case SM_HOME:
106 // Obtain ownership of the DNS resolution module
107 if(!DNSBeginUsage())
108 break;
109
110 // Obtain the IP address associated with the common ServerName
111 DNSResolve(ServerName, DNS_TYPE_A);
112 GenericTCPExampleState++;
113 break;
114
115 case SM_NAME_RESOLVE:
116 // Wait for the DNS server to return the requested IP address
117 if(!DNSIsResolved(&Server.IPAddr))
118 break;
119
120 // Release the DNS module, we no longer need it
121 if(!DNSEndUsage())
122 {
123 // An invalid IP address was returned from the DNS
124 // server. Quit and fail permanantly if host is not valid.
125 GenericTCPExampleState = SM_DONE;
126 break;
127 }
128
129 GenericTCPExampleState++;
130
131 case SM_ARP_START_RESOLVE:
132 // Obtain the MAC address associated with the server's IP address (either direct MAC address on same subnet, or the MAC address of the Gateway machine)
133 ARPResolve(&Server.IPAddr);
134 Timer = TickGet();
135 GenericTCPExampleState++;
136 break;
137
138 case SM_ARP_RESOLVE:
139 // Wait for the MAC address to finish being obtained
140 if(!ARPIsResolved(&Server.IPAddr, &Server.MACAddr))
141 {
142 // Time out if too much time is spent in this state
143 if(TickGet()-Timer > 1*TICK_SECOND)
144 {
145 // Retransmit ARP request
146 GenericTCPExampleState--;
147 }
148 break;
149 }
150 GenericTCPExampleState++;
151
152 case SM_SOCKET_OBTAIN:
153 // Connect a socket to the remote TCP server
154 MySocket = TCPConnect(&Server, ServerPort);
155
156 // Abort operation if no TCP sockets are available
157 // If this ever happens, incrementing MAX_TCP_SOCKETS in
158 // StackTsk.h may help (at the expense of more global memory
159 // resources).
160 if(MySocket == INVALID_SOCKET)
161 break;
162
163 GenericTCPExampleState++;
164 Timer = TickGet();
165 break;
166
167 case SM_SOCKET_OBTAINED:
168 // Wait for the remote server to accept our connection request
169 if(!TCPIsConnected(MySocket))
170 {
171 // Time out if too much time is spent in this state
172 if(TickGet()-Timer > 5*TICK_SECOND)
173 {
174 // Close the socket so it can be used by other modules
175 TCPDisconnect(MySocket);
176 MySocket = INVALID_SOCKET;
177 GenericTCPExampleState--;
178 }
179 break;
180 }
181
182 Timer = TickGet();
183
184 // Make certain the socket can be written to
185 if(!TCPIsPutReady(MySocket))
186 break;
187
188 // Place the application protocol data into the transmit buffer. For this example, we are connected to an HTTP server, so we'll send an HTTP GET request.
189 TCPPutROMString(MySocket, (ROM BYTE*)"GET ");
190 TCPPutROMString(MySocket, RemoteURL);
191 TCPPutROMString(MySocket, (ROM BYTE*)" HTTP/1.1\r\nHost: ");
192 TCPPutString(MySocket, ServerName);
193 TCPPutROMString(MySocket, (ROM BYTE*)"\r\n\r\n");
194
195 // Send the packet
196 TCPFlush(MySocket);
197 GenericTCPExampleState++;
198
199 case SM_PROCESS_RESPONSE:
200 // Check to see if the remote node has disconnected from us or sent us any application data
201 // If application data is available, write it to the UART
202 if(!TCPIsConnected(MySocket))
203 {
204 GenericTCPExampleState++;
205 }
206
207 if(!TCPIsGetReady(MySocket))
208 break;
209
210 // Obtain the server reply
211 while(TCPGet(MySocket, &i))
212 {
213 while(BusyUART());
214 WriteUART(i);
215 }
216
217 break;
218
219 case SM_DISCONNECT:
220 // Close the socket so it can be used by other modules
221 // For this application, we wish to stay connected, but this state will still get entered if the remote server decides to disconnect
222 TCPDisconnect(MySocket);
223 MySocket = INVALID_SOCKET;
224 GenericTCPExampleState = SM_DONE;
225 break;
226
227 case SM_DONE:
228 // Do nothing unless the user pushes BUTTON1 and wants to restart the whole connection/download process
229 if(BUTTON1_IO == 0u)
230 GenericTCPExampleState = SM_HOME;
231 break;
232 }
233 }
234
235 #endif //#if defined(STACK_USE_GENERIC_TCP_CLIENT_EXAMPLE)

  ViewVC Help
Powered by ViewVC 1.1.20