Details | Last modification | View Log | SVN | RSS feed
| Rev | Author | Line No. | Line |
|---|---|---|---|
| 363 | johboh | 1 | /********************************************************************* |
| 2 | * |
||
| 3 | * Generic TCP Client Example Application |
||
| 4 | * -Implements an example HTTP client and should be used as a basis |
||
| 5 | * for creating new TCP client applications |
||
| 6 | * |
||
| 7 | ********************************************************************* |
||
| 8 | * FileName: GenericTCPClient.c |
||
| 9 | * Dependencies: TCP.h, DNS.h |
||
| 10 | * Processor: PIC18, PIC24F, PIC24H, dsPIC30, dsPIC33F |
||
| 11 | * Complier: Microchip C18 v3.03 or higher |
||
| 12 | * Microchip C30 v2.02 or higher |
||
| 13 | * Company: Microchip Technology, Inc. |
||
| 14 | * |
||
| 15 | * Software License Agreement |
||
| 16 | * |
||
| 17 | * This software is owned by Microchip Technology Inc. ("Microchip") |
||
| 18 | * and is supplied to you for use exclusively as described in the |
||
| 19 | * associated software agreement. This software is protected by |
||
| 20 | * software and other intellectual property laws. Any use in |
||
| 21 | * violation of the software license may subject the user to criminal |
||
| 22 | * sanctions as well as civil liability. Copyright 2006 Microchip |
||
| 23 | * Technology Inc. All rights reserved. |
||
| 24 | * |
||
| 25 | * This software is provided "AS IS." MICROCHIP DISCLAIMS ALL |
||
| 26 | * WARRANTIES, EXPRESS, IMPLIED, STATUTORY OR OTHERWISE, NOT LIMITED |
||
| 27 | * TO MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, AND |
||
| 28 | * INFRINGEMENT. Microchip shall in no event be liable for special, |
||
| 29 | * incidental, or consequential damages. |
||
| 30 | * |
||
| 31 | * |
||
| 32 | * Author Date Comment |
||
| 33 | *~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ |
||
| 34 | * Howard Schlunder 8/01/06 Original |
||
| 35 | ********************************************************************/ |
||
| 36 | #define THIS_IS_TCP_CLIENT_EXAMPLE |
||
| 37 | |||
| 38 | #include "..\Include\DNS.h" |
||
| 39 | #include "..\Include\Helpers.h" |
||
| 40 | #include "..\Include\TCP.h" |
||
| 41 | #include "..\Include\ARPTsk.h" |
||
| 42 | #include "..\Include\UART.h" |
||
| 43 | |||
| 44 | #if defined(STACK_USE_GENERIC_TCP_EXAMPLE) |
||
| 45 | |||
| 46 | //TODO: Define proper server address here |
||
| 47 | BYTE ServerName[] = "johboh.csbnet.se"; |
||
| 48 | WORD ServerPort = 80; |
||
| 49 | |||
| 50 | // This is specific to this HTTP Client example |
||
| 51 | BYTE RemoteURL[] = "/time.php"; |
||
| 52 | |||
| 53 | |||
| 54 | typedef enum _GenericT |
||
| 55 | { |
||
| 56 | SM_HOME = 0, |
||
| 57 | SM_NAME_RESOLVE, |
||
| 58 | SM_ARP_START_RESOLVE, |
||
| 59 | SM_ARP_RESOLVE, |
||
| 60 | SM_SOCKET_OBTAIN, |
||
| 61 | SM_SOCKET_OBTAINED, |
||
| 62 | SM_PROCESS_RESPONSE, |
||
| 63 | SM_DISCONNECT, |
||
| 64 | SM_DONE |
||
| 65 | } GenericT; |
||
| 66 | |||
| 67 | /********************************************************************* |
||
| 68 | * Function: void GenericTCPClient(void) |
||
| 69 | * |
||
| 70 | * PreCondition: Stack is initialized() |
||
| 71 | * |
||
| 72 | * Input: None |
||
| 73 | * |
||
| 74 | * Output: None |
||
| 75 | * |
||
| 76 | * Side Effects: None |
||
| 77 | * |
||
| 78 | * Overview: None |
||
| 79 | * |
||
| 80 | * Note: None |
||
| 81 | ********************************************************************/ |
||
| 82 | void GenericTCPClient(void) |
||
| 83 | { |
||
| 84 | BYTE i; |
||
| 85 | BYTE *StringPtr; |
||
| 86 | static TICK Timer; |
||
| 87 | static TCP_SOCKET MySocket = INVALID_SOCKET; |
||
| 88 | static NODE_INFO Server; |
||
| 89 | static GenericT GenericTCPExampleState = SM_DONE; |
||
| 90 | |||
| 91 | switch(GenericTCPExampleState) |
||
| 92 | { |
||
| 93 | case SM_HOME: |
||
| 94 | // Obtain the IP address associated with the common ServerName |
||
| 95 | DNSResolve(ServerName); |
||
| 96 | GenericTCPExampleState++; |
||
| 97 | break; |
||
| 98 | |||
| 99 | case SM_NAME_RESOLVE: |
||
| 100 | // Wait for the DNS server to return the requested IP address |
||
| 101 | if(!DNSIsResolved(&Server.IPAddr)) |
||
| 102 | break; |
||
| 103 | GenericTCPExampleState++; |
||
| 104 | break; |
||
| 105 | |||
| 106 | case SM_ARP_START_RESOLVE: |
||
| 107 | // 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) |
||
| 108 | ARPResolve(&Server.IPAddr); |
||
| 109 | Timer = TickGet(); |
||
| 110 | GenericTCPExampleState++; |
||
| 111 | break; |
||
| 112 | |||
| 113 | case SM_ARP_RESOLVE: |
||
| 114 | // Wait for the MAC address to finish being obtained |
||
| 115 | if(!ARPIsResolved(&Server.IPAddr, &Server.MACAddr)) |
||
| 116 | { |
||
| 117 | // Time out if too much time is spent in this state |
||
| 118 | if(TickGet()-Timer > 1*TICK_SECOND) |
||
| 119 | { |
||
| 120 | // Retransmit ARP request |
||
| 121 | GenericTCPExampleState--; |
||
| 122 | } |
||
| 123 | break; |
||
| 124 | } |
||
| 125 | GenericTCPExampleState++; |
||
| 126 | break; |
||
| 127 | |||
| 128 | case SM_SOCKET_OBTAIN: |
||
| 129 | // Connect a socket to the remote TCP server |
||
| 130 | MySocket = TCPConnect(&Server, ServerPort); |
||
| 131 | |||
| 132 | // Abort operation if no TCP sockets are available |
||
| 133 | // If this ever happens, incrementing MAX_TCP_SOCKETS in |
||
| 134 | // StackTsk.h may help (at the expense of more global memory |
||
| 135 | // resources). |
||
| 136 | if(MySocket == INVALID_SOCKET) |
||
| 137 | break; |
||
| 138 | |||
| 139 | GenericTCPExampleState++; |
||
| 140 | Timer = TickGet(); |
||
| 141 | break; |
||
| 142 | |||
| 143 | case SM_SOCKET_OBTAINED: |
||
| 144 | // Wait for the remote server to accept our connection request |
||
| 145 | if(!TCPIsConnected(MySocket)) |
||
| 146 | { |
||
| 147 | // Time out if too much time is spent in this state |
||
| 148 | if(TickGet()-Timer > 5*TICK_SECOND) |
||
| 149 | { |
||
| 150 | // Close the socket so it can be used by other modules |
||
| 151 | TCPDisconnect(MySocket); |
||
| 152 | MySocket = INVALID_SOCKET; |
||
| 153 | GenericTCPExampleState--; |
||
| 154 | } |
||
| 155 | break; |
||
| 156 | } |
||
| 157 | |||
| 158 | Timer = TickGet(); |
||
| 159 | |||
| 160 | // Make certain the socket can be written to |
||
| 161 | if(!TCPIsPutReady(MySocket)) |
||
| 162 | break; |
||
| 163 | |||
| 164 | // 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. |
||
| 165 | TCPPut(MySocket, 'G'); |
||
| 166 | TCPPut(MySocket, 'E'); |
||
| 167 | TCPPut(MySocket, 'T'); |
||
| 168 | TCPPut(MySocket, ' '); |
||
| 169 | { |
||
| 170 | StringPtr = RemoteURL; |
||
| 171 | for(i = 0; i < strlen(RemoteURL); i++) |
||
| 172 | { |
||
| 173 | TCPPut(MySocket, *StringPtr++); |
||
| 174 | } |
||
| 175 | } |
||
| 176 | TCPPut(MySocket, ' '); |
||
| 177 | TCPPut(MySocket, 'H'); |
||
| 178 | TCPPut(MySocket, 'T'); |
||
| 179 | TCPPut(MySocket, 'T'); |
||
| 180 | TCPPut(MySocket, 'P'); |
||
| 181 | TCPPut(MySocket, '/'); |
||
| 182 | TCPPut(MySocket, '1'); |
||
| 183 | TCPPut(MySocket, '.'); |
||
| 184 | TCPPut(MySocket, '1'); |
||
| 185 | TCPPut(MySocket, '\r'); |
||
| 186 | TCPPut(MySocket, '\n'); |
||
| 187 | TCPPut(MySocket, 'H'); |
||
| 188 | TCPPut(MySocket, 'o'); |
||
| 189 | TCPPut(MySocket, 's'); |
||
| 190 | TCPPut(MySocket, 't'); |
||
| 191 | TCPPut(MySocket, ':'); |
||
| 192 | TCPPut(MySocket, ' '); |
||
| 193 | { |
||
| 194 | StringPtr = ServerName; |
||
| 195 | |||
| 196 | for(i = 0; i < strlen(ServerName); i++) |
||
| 197 | { |
||
| 198 | TCPPut(MySocket, *StringPtr++); |
||
| 199 | } |
||
| 200 | } |
||
| 201 | TCPPut(MySocket, '\r'); |
||
| 202 | TCPPut(MySocket, '\n'); |
||
| 203 | TCPPut(MySocket, '\r'); |
||
| 204 | TCPPut(MySocket, '\n'); |
||
| 205 | |||
| 206 | // Send the packet |
||
| 207 | TCPFlush(MySocket); |
||
| 208 | GenericTCPExampleState++; |
||
| 209 | break; |
||
| 210 | |||
| 211 | case SM_PROCESS_RESPONSE: |
||
| 212 | // Check to see if the remote node has disconnected from us or sent us any application data |
||
| 213 | // If application data is available, write it to the UART |
||
| 214 | if(!TCPIsConnected(MySocket)) |
||
| 215 | { |
||
| 216 | GenericTCPExampleState++; |
||
| 217 | } |
||
| 218 | |||
| 219 | if(!TCPIsGetReady(MySocket)) |
||
| 220 | break; |
||
| 221 | |||
| 222 | // Obtain the server reply |
||
| 223 | while(TCPGet(MySocket, &i)) |
||
| 224 | { |
||
| 225 | while(BusyUART()); |
||
| 226 | WriteUART(i); |
||
| 227 | } |
||
| 228 | |||
| 229 | break; |
||
| 230 | |||
| 231 | case SM_DISCONNECT: |
||
| 232 | // Close the socket so it can be used by other modules |
||
| 233 | // For this application, we wish to stay connected, but this state will still get entered if the remote server decides to disconnect |
||
| 234 | TCPDisconnect(MySocket); |
||
| 235 | MySocket = INVALID_SOCKET; |
||
| 236 | GenericTCPExampleState = SM_DONE; |
||
| 237 | break; |
||
| 238 | |||
| 239 | case SM_DONE: |
||
| 240 | putrsUART("done..\r\n"); |
||
| 241 | // Do nothing unless the user pushes BUTTON1 and wants to restart the whole connection/download process |
||
| 242 | if(BUTTON0_IO == 0) |
||
| 243 | { |
||
| 244 | GenericTCPExampleState = SM_HOME; |
||
| 245 | putrsUART("Go!\r\n"); |
||
| 246 | } |
||
| 247 | break; |
||
| 248 | } |
||
| 249 | } |
||
| 250 | |||
| 251 | #endif //#if defined(STACK_USE_GENERIC_TCP_EXAMPLE) |