1 |
hedin |
15 |
/*********************************************************************
|
2 |
|
|
*
|
3 |
|
|
* Generic TCP Server Example Application
|
4 |
|
|
* Module for Microchip TCP/IP Stack
|
5 |
|
|
* -Implements an example "ToUpper" TCP server on port 9760 and
|
6 |
|
|
* should be used as a basis for creating new TCP server
|
7 |
|
|
* applications
|
8 |
|
|
* -Reference: None. Hopefully AN833 in the future.
|
9 |
|
|
*
|
10 |
|
|
*********************************************************************
|
11 |
|
|
* FileName: GenericTCPServer.c
|
12 |
|
|
* Dependencies: TCP.h
|
13 |
|
|
* Processor: PIC18, PIC24F, PIC24H, dsPIC30, dsPIC33F
|
14 |
|
|
* Complier: Microchip C18 v3.03 or higher
|
15 |
|
|
* Microchip C30 v2.01 or higher
|
16 |
|
|
* Company: Microchip Technology, Inc.
|
17 |
|
|
*
|
18 |
|
|
* Software License Agreement
|
19 |
|
|
*
|
20 |
|
|
* Copyright © 2002-2007 Microchip Technology Inc. All rights
|
21 |
|
|
* reserved.
|
22 |
|
|
*
|
23 |
|
|
* Microchip licenses to you the right to use, modify, copy, and
|
24 |
|
|
* distribute:
|
25 |
|
|
* (i) the Software when embedded on a Microchip microcontroller or
|
26 |
|
|
* digital signal controller product (“Device”) which is
|
27 |
|
|
* integrated into Licensee’s product; or
|
28 |
|
|
* (ii) ONLY the Software driver source files ENC28J60.c and
|
29 |
|
|
* ENC28J60.h ported to a non-Microchip device used in
|
30 |
|
|
* conjunction with a Microchip ethernet controller for the
|
31 |
|
|
* sole purpose of interfacing with the ethernet controller.
|
32 |
|
|
*
|
33 |
|
|
* You should refer to the license agreement accompanying this
|
34 |
|
|
* Software for additional information regarding your rights and
|
35 |
|
|
* obligations.
|
36 |
|
|
*
|
37 |
|
|
* THE SOFTWARE AND DOCUMENTATION ARE PROVIDED “AS IS” WITHOUT
|
38 |
|
|
* WARRANTY OF ANY KIND, EITHER EXPRESS OR IMPLIED, INCLUDING WITHOUT
|
39 |
|
|
* LIMITATION, ANY WARRANTY OF MERCHANTABILITY, FITNESS FOR A
|
40 |
|
|
* PARTICULAR PURPOSE, TITLE AND NON-INFRINGEMENT. IN NO EVENT SHALL
|
41 |
|
|
* MICROCHIP BE LIABLE FOR ANY INCIDENTAL, SPECIAL, INDIRECT OR
|
42 |
|
|
* CONSEQUENTIAL DAMAGES, LOST PROFITS OR LOST DATA, COST OF
|
43 |
|
|
* PROCUREMENT OF SUBSTITUTE GOODS, TECHNOLOGY OR SERVICES, ANY CLAIMS
|
44 |
|
|
* BY THIRD PARTIES (INCLUDING BUT NOT LIMITED TO ANY DEFENSE
|
45 |
|
|
* THEREOF), ANY CLAIMS FOR INDEMNITY OR CONTRIBUTION, OR OTHER
|
46 |
|
|
* SIMILAR COSTS, WHETHER ASSERTED ON THE BASIS OF CONTRACT, TORT
|
47 |
|
|
* (INCLUDING NEGLIGENCE), BREACH OF WARRANTY, OR OTHERWISE.
|
48 |
|
|
*
|
49 |
|
|
*
|
50 |
|
|
* Author Date Comment
|
51 |
|
|
*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
52 |
|
|
* Howard Schlunder 10/19/06 Original
|
53 |
|
|
********************************************************************/
|
54 |
|
|
#define __GENERICTCPSERVER_C
|
55 |
|
|
|
56 |
|
|
#include "TCPIP Stack/TCPIP.h"
|
57 |
|
|
|
58 |
|
|
#if defined(STACK_USE_GENERIC_TCP_SERVER_EXAMPLE)
|
59 |
|
|
|
60 |
|
|
|
61 |
|
|
#define SERVER_PORT 9760
|
62 |
|
|
|
63 |
|
|
|
64 |
|
|
/*********************************************************************
|
65 |
|
|
* Function: void GenericTCPServer(void)
|
66 |
|
|
*
|
67 |
|
|
* PreCondition: Stack is initialized()
|
68 |
|
|
*
|
69 |
|
|
* Input: None
|
70 |
|
|
*
|
71 |
|
|
* Output: None
|
72 |
|
|
*
|
73 |
|
|
* Side Effects: None
|
74 |
|
|
*
|
75 |
|
|
* Overview: None
|
76 |
|
|
*
|
77 |
|
|
* Note: None
|
78 |
|
|
********************************************************************/
|
79 |
|
|
void GenericTCPServer(void)
|
80 |
|
|
{
|
81 |
|
|
BYTE c;
|
82 |
|
|
static TCP_SOCKET MySocket;
|
83 |
|
|
static enum _TCPServerState
|
84 |
|
|
{
|
85 |
|
|
SM_HOME = 0,
|
86 |
|
|
SM_LISTENING,
|
87 |
|
|
} TCPServerState = SM_HOME;
|
88 |
|
|
static BYTE AppTXBuffer[33];
|
89 |
|
|
static BYTE *AppTXStart = AppTXBuffer;
|
90 |
|
|
static BYTE *AppTXEnd = AppTXBuffer;
|
91 |
|
|
|
92 |
|
|
switch(TCPServerState)
|
93 |
|
|
{
|
94 |
|
|
case SM_HOME:
|
95 |
|
|
// Allocate a socket for this server to listen and accept connections on
|
96 |
|
|
MySocket = TCPListen(SERVER_PORT);
|
97 |
|
|
if(MySocket == INVALID_SOCKET)
|
98 |
|
|
{
|
99 |
|
|
#ifdef USE_LCD
|
100 |
|
|
strcpypgm2ram((char*)LCDText, "Error: Increase MAX_TCP_SOCKETS");
|
101 |
|
|
LCDUpdate();
|
102 |
|
|
#endif
|
103 |
|
|
return;
|
104 |
|
|
}
|
105 |
|
|
|
106 |
|
|
TCPServerState++;
|
107 |
|
|
break;
|
108 |
|
|
|
109 |
|
|
case SM_LISTENING:
|
110 |
|
|
// See if anyone is connected to us
|
111 |
|
|
if(!TCPIsConnected(MySocket))
|
112 |
|
|
{
|
113 |
|
|
// Zero out the App TX buffer and do nothing
|
114 |
|
|
AppTXEnd = AppTXStart;
|
115 |
|
|
return;
|
116 |
|
|
}
|
117 |
|
|
|
118 |
|
|
// Receive any bytes that have been sent to us.
|
119 |
|
|
if(TCPIsGetReady(MySocket))
|
120 |
|
|
{
|
121 |
|
|
// Need to use LED7, disable BUTTON2 functionality on Explorer 16
|
122 |
|
|
LED7_TRIS = 0;
|
123 |
|
|
|
124 |
|
|
while(TCPGet(MySocket, &c))
|
125 |
|
|
{
|
126 |
|
|
// Convert the character to uppercase and transmit it back
|
127 |
|
|
// to the sender
|
128 |
|
|
if(c >= 'a' && c <= 'z')
|
129 |
|
|
c -= ('a' - 'A');
|
130 |
|
|
|
131 |
|
|
*AppTXEnd++ = c;
|
132 |
|
|
if(AppTXEnd > AppTXBuffer + sizeof(AppTXBuffer))
|
133 |
|
|
AppTXEnd = AppTXBuffer;
|
134 |
|
|
}
|
135 |
|
|
|
136 |
|
|
// Restore BUTTON2 functionality on Explorer 16
|
137 |
|
|
LED7_TRIS = 1;
|
138 |
|
|
}
|
139 |
|
|
|
140 |
|
|
// Transmit any bytes that are waiting to be sent.
|
141 |
|
|
c = 0x00;
|
142 |
|
|
while(TCPIsPutReady(MySocket) && (AppTXStart != AppTXEnd))
|
143 |
|
|
{
|
144 |
|
|
c = 0xFF;
|
145 |
|
|
TCPPut(MySocket, *AppTXStart++);
|
146 |
|
|
if(AppTXStart > AppTXBuffer + sizeof(AppTXBuffer))
|
147 |
|
|
AppTXStart = AppTXBuffer;
|
148 |
|
|
}
|
149 |
|
|
// Flush the packet if we placed anything in the transmit buffer
|
150 |
|
|
if(c)
|
151 |
|
|
TCPFlush(MySocket);
|
152 |
|
|
|
153 |
|
|
|
154 |
|
|
break;
|
155 |
|
|
}
|
156 |
|
|
}
|
157 |
|
|
|
158 |
|
|
#endif //#if defined(STACK_USE_GENERIC_TCP_SERVER_EXAMPLE)
|