| Line No. | Rev | Author | Line |
|---|---|---|---|
| 1 | 32 | kaklik | /********************************************************************* |
| 2 | * |
||
| 3 | * Announce Client and Server |
||
| 4 | * Module for Microchip TCP/IP Stack |
||
| 5 | * -Provides device hostname and IP address discovery on a local |
||
| 6 | * Ethernet subnet (same broadcast domain) |
||
| 7 | * -Reference: None. Hopefully AN833 in the future. |
||
| 8 | * |
||
| 9 | ********************************************************************* |
||
| 10 | * FileName: Announce.c |
||
| 11 | * Dependencies: UDP |
||
| 12 | * Processor: PIC18, PIC24F, PIC24H, dsPIC30F, dsPIC33F, PIC32 |
||
| 13 | * Compiler: Microchip C32 v1.05 or higher |
||
| 14 | * Microchip C30 v3.12 or higher |
||
| 15 | * Microchip C18 v3.30 or higher |
||
| 16 | * HI-TECH PICC-18 PRO 9.63PL2 or higher |
||
| 17 | * Company: Microchip Technology, Inc. |
||
| 18 | * |
||
| 19 | * Software License Agreement |
||
| 20 | * |
||
| 21 | * Copyright (C) 2002-2009 Microchip Technology Inc. All rights |
||
| 22 | * reserved. |
||
| 23 | * |
||
| 24 | * Microchip licenses to you the right to use, modify, copy, and |
||
| 25 | * distribute: |
||
| 26 | * (i) the Software when embedded on a Microchip microcontroller or |
||
| 27 | * digital signal controller product ("Device") which is |
||
| 28 | * integrated into Licensee's product; or |
||
| 29 | * (ii) ONLY the Software driver source files ENC28J60.c, ENC28J60.h, |
||
| 30 | * ENCX24J600.c and ENCX24J600.h ported to a non-Microchip device |
||
| 31 | * used in conjunction with a Microchip ethernet controller for |
||
| 32 | * the sole purpose of interfacing with the ethernet controller. |
||
| 33 | * |
||
| 34 | * You should refer to the license agreement accompanying this |
||
| 35 | * Software for additional information regarding your rights and |
||
| 36 | * obligations. |
||
| 37 | * |
||
| 38 | * THE SOFTWARE AND DOCUMENTATION ARE PROVIDED "AS IS" WITHOUT |
||
| 39 | * WARRANTY OF ANY KIND, EITHER EXPRESS OR IMPLIED, INCLUDING WITHOUT |
||
| 40 | * LIMITATION, ANY WARRANTY OF MERCHANTABILITY, FITNESS FOR A |
||
| 41 | * PARTICULAR PURPOSE, TITLE AND NON-INFRINGEMENT. IN NO EVENT SHALL |
||
| 42 | * MICROCHIP BE LIABLE FOR ANY INCIDENTAL, SPECIAL, INDIRECT OR |
||
| 43 | * CONSEQUENTIAL DAMAGES, LOST PROFITS OR LOST DATA, COST OF |
||
| 44 | * PROCUREMENT OF SUBSTITUTE GOODS, TECHNOLOGY OR SERVICES, ANY CLAIMS |
||
| 45 | * BY THIRD PARTIES (INCLUDING BUT NOT LIMITED TO ANY DEFENSE |
||
| 46 | * THEREOF), ANY CLAIMS FOR INDEMNITY OR CONTRIBUTION, OR OTHER |
||
| 47 | * SIMILAR COSTS, WHETHER ASSERTED ON THE BASIS OF CONTRACT, TORT |
||
| 48 | * (INCLUDING NEGLIGENCE), BREACH OF WARRANTY, OR OTHERWISE. |
||
| 49 | * |
||
| 50 | * |
||
| 51 | * Author Date Comment |
||
| 52 | *~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ |
||
| 53 | * Howard Schlunder 10/7/04 Original |
||
| 54 | * Howard Schlunder 2/9/05 Simplified MAC address to text |
||
| 55 | * conversion logic |
||
| 56 | * Howard Schlunder 2/14/05 Fixed subnet broadcast calculation |
||
| 57 | * Howard Schlunder 10/3/06 Fixed a remoteNode saving bug |
||
| 58 | ********************************************************************/ |
||
| 59 | #define __ANNOUNCE_C |
||
| 60 | |||
| 61 | #include "TCPIPConfig.h" |
||
| 62 | |||
| 63 | #if defined(STACK_USE_ANNOUNCE) |
||
| 64 | |||
| 65 | #include "TCPIP Stack/TCPIP.h" |
||
| 66 | |||
| 67 | // The announce port |
||
| 68 | #define ANNOUNCE_PORT 30303 |
||
| 69 | |||
| 70 | |||
| 71 | |||
| 72 | extern NODE_INFO remoteNode; |
||
| 73 | |||
| 74 | /********************************************************************* |
||
| 75 | * Function: void AnnounceIP(void) |
||
| 76 | * |
||
| 77 | * Summary: Transmits an Announce packet. |
||
| 78 | * |
||
| 79 | * PreCondition: Stack is initialized() |
||
| 80 | * |
||
| 81 | * Input: None |
||
| 82 | * |
||
| 83 | * Output: None |
||
| 84 | * |
||
| 85 | * Side Effects: None |
||
| 86 | * |
||
| 87 | * Overview: AnnounceIP opens a UDP socket and transmits a |
||
| 88 | * broadcast packet to port 30303. If a computer is |
||
| 89 | * on the same subnet and a utility is looking for |
||
| 90 | * packets on the UDP port, it will receive the |
||
| 91 | * broadcast. For this application, it is used to |
||
| 92 | * announce the change of this board's IP address. |
||
| 93 | * The messages can be viewed with the MCHPDetect.exe |
||
| 94 | * program. |
||
| 95 | * |
||
| 96 | * Note: A UDP socket must be available before this |
||
| 97 | * function is called. It is freed at the end of |
||
| 98 | * the function. MAX_UDP_SOCKETS may need to be |
||
| 99 | * increased if other modules use UDP sockets. |
||
| 100 | ********************************************************************/ |
||
| 101 | void AnnounceIP(void) |
||
| 102 | { |
||
| 103 | UDP_SOCKET MySocket; |
||
| 104 | BYTE i; |
||
| 105 | |||
| 106 | // Open a UDP socket for outbound broadcast transmission |
||
| 107 | MySocket = UDPOpen(2860, NULL, ANNOUNCE_PORT); |
||
| 108 | |||
| 109 | // Abort operation if no UDP sockets are available |
||
| 110 | // If this ever happens, incrementing MAX_UDP_SOCKETS in |
||
| 111 | // StackTsk.h may help (at the expense of more global memory |
||
| 112 | // resources). |
||
| 113 | if(MySocket == INVALID_UDP_SOCKET) |
||
| 114 | return; |
||
| 115 | |||
| 116 | // Make certain the socket can be written to |
||
| 117 | while(!UDPIsPutReady(MySocket)); |
||
| 118 | |||
| 119 | // Begin sending our MAC address in human readable form. |
||
| 120 | // The MAC address theoretically could be obtained from the |
||
| 121 | // packet header when the computer receives our UDP packet, |
||
| 122 | // however, in practice, the OS will abstract away the useful |
||
| 123 | // information and it would be difficult to obtain. It also |
||
| 124 | // would be lost if this broadcast packet were forwarded by a |
||
| 125 | // router to a different portion of the network (note that |
||
| 126 | // broadcasts are normally not forwarded by routers). |
||
| 127 | UDPPutArray((BYTE*)AppConfig.NetBIOSName, sizeof(AppConfig.NetBIOSName)-1); |
||
| 128 | UDPPut('\r'); |
||
| 129 | UDPPut('\n'); |
||
| 130 | |||
| 131 | // Convert the MAC address bytes to hex (text) and then send it |
||
| 132 | i = 0; |
||
| 133 | while(1) |
||
| 134 | { |
||
| 135 | UDPPut(btohexa_high(AppConfig.MyMACAddr.v[i])); |
||
| 136 | UDPPut(btohexa_low(AppConfig.MyMACAddr.v[i])); |
||
| 137 | if(++i == 6u) |
||
| 138 | break; |
||
| 139 | UDPPut('-'); |
||
| 140 | } |
||
| 141 | |||
| 142 | // Send some other human readable information. |
||
| 143 | UDPPutROMString((ROM BYTE*)"\r\nDHCP/Power event occurred"); |
||
| 144 | |||
| 145 | // Send the packet |
||
| 146 | UDPFlush(); |
||
| 147 | |||
| 148 | // Close the socket so it can be used by other modules |
||
| 149 | UDPClose(MySocket); |
||
| 150 | } |
||
| 151 | |||
| 152 | /********************************************************************* |
||
| 153 | * Function: void DiscoveryTask(void) |
||
| 154 | * |
||
| 155 | * Summary: Announce callback task. |
||
| 156 | * |
||
| 157 | * PreCondition: Stack is initialized() |
||
| 158 | * |
||
| 159 | * Input: None |
||
| 160 | * |
||
| 161 | * Output: None |
||
| 162 | * |
||
| 163 | * Side Effects: None |
||
| 164 | * |
||
| 165 | * Overview: Recurring task used to listen for Discovery |
||
| 166 | * messages on the specified ANNOUNCE_PORT. These |
||
| 167 | * messages can be sent using the Microchip Device |
||
| 168 | * Discoverer tool. If one is received, this |
||
| 169 | * function will transmit a reply. |
||
| 170 | * |
||
| 171 | * Note: A UDP socket must be available before this |
||
| 172 | * function is called. It is freed at the end of |
||
| 173 | * the function. MAX_UDP_SOCKETS may need to be |
||
| 174 | * increased if other modules use UDP sockets. |
||
| 175 | ********************************************************************/ |
||
| 176 | void DiscoveryTask(void) |
||
| 177 | { |
||
| 178 | static enum { |
||
| 179 | DISCOVERY_HOME = 0, |
||
| 180 | DISCOVERY_LISTEN, |
||
| 181 | DISCOVERY_REQUEST_RECEIVED, |
||
| 182 | DISCOVERY_DISABLED |
||
| 183 | } DiscoverySM = DISCOVERY_HOME; |
||
| 184 | |||
| 185 | static UDP_SOCKET MySocket; |
||
| 186 | BYTE i; |
||
| 187 | |||
| 188 | switch(DiscoverySM) |
||
| 189 | { |
||
| 190 | case DISCOVERY_HOME: |
||
| 191 | // Open a UDP socket for inbound and outbound transmission |
||
| 192 | // Since we expect to only receive broadcast packets and |
||
| 193 | // only send unicast packets directly to the node we last |
||
| 194 | // received from, the remote NodeInfo parameter can be anything |
||
| 195 | MySocket = UDPOpen(ANNOUNCE_PORT, NULL, ANNOUNCE_PORT); |
||
| 196 | |||
| 197 | if(MySocket == INVALID_UDP_SOCKET) |
||
| 198 | return; |
||
| 199 | else |
||
| 200 | DiscoverySM++; |
||
| 201 | break; |
||
| 202 | |||
| 203 | case DISCOVERY_LISTEN: |
||
| 204 | // Do nothing if no data is waiting |
||
| 205 | if(!UDPIsGetReady(MySocket)) |
||
| 206 | return; |
||
| 207 | |||
| 208 | // See if this is a discovery query or reply |
||
| 209 | UDPGet(&i); |
||
| 210 | UDPDiscard(); |
||
| 211 | if(i != 'D') |
||
| 212 | return; |
||
| 213 | |||
| 214 | // We received a discovery request, reply when we can |
||
| 215 | DiscoverySM++; |
||
| 216 | |||
| 217 | // Change the destination to the unicast address of the last received packet |
||
| 218 | memcpy((void*)&UDPSocketInfo[MySocket].remoteNode, (const void*)&remoteNode, sizeof(remoteNode)); |
||
| 219 | |||
| 220 | // No break needed. If we get down here, we are now ready for the DISCOVERY_REQUEST_RECEIVED state |
||
| 221 | |||
| 222 | case DISCOVERY_REQUEST_RECEIVED: |
||
| 223 | if(!UDPIsPutReady(MySocket)) |
||
| 224 | return; |
||
| 225 | |||
| 226 | // Begin sending our MAC address in human readable form. |
||
| 227 | // The MAC address theoretically could be obtained from the |
||
| 228 | // packet header when the computer receives our UDP packet, |
||
| 229 | // however, in practice, the OS will abstract away the useful |
||
| 230 | // information and it would be difficult to obtain. It also |
||
| 231 | // would be lost if this broadcast packet were forwarded by a |
||
| 232 | // router to a different portion of the network (note that |
||
| 233 | // broadcasts are normally not forwarded by routers). |
||
| 234 | UDPPutArray((BYTE*)AppConfig.NetBIOSName, sizeof(AppConfig.NetBIOSName)-1); |
||
| 235 | UDPPut('\r'); |
||
| 236 | UDPPut('\n'); |
||
| 237 | |||
| 238 | // Convert the MAC address bytes to hex (text) and then send it |
||
| 239 | i = 0; |
||
| 240 | while(1) |
||
| 241 | { |
||
| 242 | UDPPut(btohexa_high(AppConfig.MyMACAddr.v[i])); |
||
| 243 | UDPPut(btohexa_low(AppConfig.MyMACAddr.v[i])); |
||
| 244 | if(++i == 6u) |
||
| 245 | break; |
||
| 246 | UDPPut('-'); |
||
| 247 | } |
||
| 248 | UDPPut('\r'); |
||
| 249 | UDPPut('\n'); |
||
| 250 | |||
| 251 | // Send the packet |
||
| 252 | UDPFlush(); |
||
| 253 | |||
| 254 | // Listen for other discovery requests |
||
| 255 | DiscoverySM = DISCOVERY_LISTEN; |
||
| 256 | break; |
||
| 257 | |||
| 258 | case DISCOVERY_DISABLED: |
||
| 259 | break; |
||
| 260 | } |
||
| 261 | |||
| 262 | } |
||
| 263 | |||
| 264 | |||
| 265 | #endif //#if defined(STACK_USE_ANNOUNCE) |
Powered by WebSVN v2.8.3