-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathGenericTCPClient.c
223 lines (191 loc) · 7.53 KB
/
GenericTCPClient.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
/*********************************************************************
*
* Generic TCP Client Example Application
* Module for Microchip TCP/IP Stack
* -Implements an example HTTP client and should be used as a basis
* for creating new TCP client applications
* -Reference: None. Hopefully AN833 in the future.
*
*********************************************************************
* FileName: GenericTCPClient.c
* Dependencies: TCP, DNS, ARP, Tick
* Processor: PIC18, PIC24F, PIC24H, dsPIC30F, dsPIC33F, PIC32
* Compiler: Microchip C32 v1.05 or higher
* Microchip C30 v3.12 or higher
* Microchip C18 v3.30 or higher
* HI-TECH PICC-18 PRO 9.63PL2 or higher
* Company: Microchip Technology, Inc.
*
* Software License Agreement
*
* Copyright (C) 2002-2009 Microchip Technology Inc. All rights
* reserved.
*
* Microchip licenses to you the right to use, modify, copy, and
* distribute:
* (i) the Software when embedded on a Microchip microcontroller or
* digital signal controller product ("Device") which is
* integrated into Licensee's product; or
* (ii) ONLY the Software driver source files ENC28J60.c, ENC28J60.h,
* ENCX24J600.c and ENCX24J600.h ported to a non-Microchip device
* used in conjunction with a Microchip ethernet controller for
* the sole purpose of interfacing with the ethernet controller.
*
* You should refer to the license agreement accompanying this
* Software for additional information regarding your rights and
* obligations.
*
* THE SOFTWARE AND DOCUMENTATION ARE PROVIDED "AS IS" WITHOUT
* WARRANTY OF ANY KIND, EITHER EXPRESS OR IMPLIED, INCLUDING WITHOUT
* LIMITATION, ANY WARRANTY OF MERCHANTABILITY, FITNESS FOR A
* PARTICULAR PURPOSE, TITLE AND NON-INFRINGEMENT. IN NO EVENT SHALL
* MICROCHIP BE LIABLE FOR ANY INCIDENTAL, SPECIAL, INDIRECT OR
* CONSEQUENTIAL DAMAGES, LOST PROFITS OR LOST DATA, COST OF
* PROCUREMENT OF SUBSTITUTE GOODS, TECHNOLOGY OR SERVICES, ANY CLAIMS
* BY THIRD PARTIES (INCLUDING BUT NOT LIMITED TO ANY DEFENSE
* THEREOF), ANY CLAIMS FOR INDEMNITY OR CONTRIBUTION, OR OTHER
* SIMILAR COSTS, WHETHER ASSERTED ON THE BASIS OF CONTRACT, TORT
* (INCLUDING NEGLIGENCE), BREACH OF WARRANTY, OR OTHERWISE.
*
*
* Author Date Comment
*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
* Howard Schlunder 8/01/06 Original
********************************************************************/
#define __GENERICTCPCLIENT_C
#include "TCPIPConfig.h"
#if defined(STACK_USE_GENERIC_TCP_CLIENT_EXAMPLE)
#include "TCPIP Stack/TCPIP.h"
// Defines the server to be accessed for this application
static BYTE ServerName[] = "www.google.com";
// Defines the port to be accessed for this application
static WORD ServerPort = 80;
// Defines the URL to be requested by this HTTP client
static ROM BYTE RemoteURL[] = "/search?as_q=Microchip&as_sitesearch=microchip.com";
/*****************************************************************************
Function:
void GenericTCPClient(void)
Summary:
Implements a simple HTTP client (over TCP).
Description:
This function implements a simple HTTP client, which operates over TCP.
The function is called periodically by the stack, and waits for BUTTON1
to be pressed. When the button is pressed, the application opens a TCP
connection to an Internet search engine, performs a search for the word
"Microchip" on "microchip.com", and prints the resulting HTML page to
the UART.
This example can be used as a model for many TCP and HTTP client
applications.
Precondition:
TCP is initialized.
Parameters:
None
Returns:
None
***************************************************************************/
void GenericTCPClient(void)
{
BYTE i;
WORD w;
BYTE vBuffer[9];
static DWORD Timer;
static TCP_SOCKET MySocket = INVALID_SOCKET;
static enum _GenericTCPExampleState
{
SM_HOME = 0,
SM_SOCKET_OBTAINED,
SM_PROCESS_RESPONSE,
SM_DISCONNECT,
SM_DONE
} GenericTCPExampleState = SM_DONE;
switch(GenericTCPExampleState)
{
case SM_HOME:
// Connect a socket to the remote TCP server
MySocket = TCPOpen((DWORD)&ServerName[0], TCP_OPEN_RAM_HOST, ServerPort, TCP_PURPOSE_GENERIC_TCP_CLIENT);
// Abort operation if no TCP socket of type TCP_PURPOSE_GENERIC_TCP_CLIENT is available
// If this ever happens, you need to go add one to TCPIPConfig.h
if(MySocket == INVALID_SOCKET)
break;
#if defined(STACK_USE_UART)
putrsUART((ROM char*)"\r\n\r\nConnecting using Microchip TCP API...\r\n");
#endif
GenericTCPExampleState++;
Timer = TickGet();
break;
case SM_SOCKET_OBTAINED:
// Wait for the remote server to accept our connection request
if(!TCPIsConnected(MySocket))
{
// Time out if too much time is spent in this state
if(TickGet()-Timer > 5*TICK_SECOND)
{
// Close the socket so it can be used by other modules
TCPDisconnect(MySocket);
MySocket = INVALID_SOCKET;
GenericTCPExampleState--;
}
break;
}
Timer = TickGet();
// Make certain the socket can be written to
if(TCPIsPutReady(MySocket) < 125u)
break;
// 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.
TCPPutROMString(MySocket, (ROM BYTE*)"GET ");
TCPPutROMString(MySocket, RemoteURL);
TCPPutROMString(MySocket, (ROM BYTE*)" HTTP/1.0\r\nHost: ");
TCPPutString(MySocket, ServerName);
TCPPutROMString(MySocket, (ROM BYTE*)"\r\nConnection: close\r\n\r\n");
// Send the packet
TCPFlush(MySocket);
GenericTCPExampleState++;
break;
case SM_PROCESS_RESPONSE:
// Check to see if the remote node has disconnected from us or sent us any application data
// If application data is available, write it to the UART
if(!TCPIsConnected(MySocket))
{
GenericTCPExampleState = SM_DISCONNECT;
// Do not break; We might still have data in the TCP RX FIFO waiting for us
}
// Get count of RX bytes waiting
w = TCPIsGetReady(MySocket);
// Obtian and print the server reply
i = sizeof(vBuffer)-1;
vBuffer[i] = '\0';
while(w)
{
if(w < i)
{
i = w;
vBuffer[i] = '\0';
}
w -= TCPGetArray(MySocket, vBuffer, i);
#if defined(STACK_USE_UART)
putsUART((char*)vBuffer);
#endif
// putsUART is a blocking call which will slow down the rest of the stack
// if we shovel the whole TCP RX FIFO into the serial port all at once.
// Therefore, let's break out after only one chunk most of the time. The
// only exception is when the remote node disconncets from us and we need to
// use up all the data before changing states.
if(GenericTCPExampleState == SM_PROCESS_RESPONSE)
break;
}
break;
case SM_DISCONNECT:
// Close the socket so it can be used by other modules
// For this application, we wish to stay connected, but this state will still get entered if the remote server decides to disconnect
TCPDisconnect(MySocket);
MySocket = INVALID_SOCKET;
GenericTCPExampleState = SM_DONE;
break;
case SM_DONE:
// Do nothing unless the user pushes BUTTON1 and wants to restart the whole connection/download process
if(BUTTON1_IO == 0u)
GenericTCPExampleState = SM_HOME;
break;
}
}
#endif //#if defined(STACK_USE_GENERIC_TCP_CLIENT_EXAMPLE)