     1: /*********************************************************************
     2:  *
     3:  *                  Announce Module for Microchip TCP/IP Stack
     4:  *
     5:  *********************************************************************
     6:  * FileName:        announce.c
     7:  * Dependencies:    UDP.h
     8:  * Processor:       PIC18
     9:  * Complier:        MCC18 v1.00.50 or higher
    10:  *                  HITECH PICC-18 V8.10PL1 or higher
    11:  * Company:         Microchip Technology, Inc.
    12:  *
    13:  * Software License Agreement
    14:  *
    15:  * This software is owned by Microchip Technology Inc. ("Microchip") 
    16:  * and is supplied to you for use exclusively as described in the 
    17:  * associated software agreement.  This software is protected by 
    18:  * software and other intellectual property laws.  Any use in 
    19:  * violation of the software license may subject the user to criminal 
    20:  * sanctions as well as civil liability.  Copyright 2006 Microchip
    21:  * Technology Inc.  All rights reserved.
    22:  *
    23:  * This software is provided "AS IS."  MICROCHIP DISCLAIMS ALL 
    24:  * WARRANTIES, EXPRESS, IMPLIED, STATUTORY OR OTHERWISE, NOT LIMITED 
    25:  * TO MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, AND 
    26:  * INFRINGEMENT.  Microchip shall in no event be liable for special, 
    27:  * incidental, or consequential damages.
    28:  *
    29:  *
    30:  * Author               Date    Comment
    31:  *~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    32:  * Howard Schlunder     10/7/04 Original
    33:  * Howard Schlunder             2/9/05  Simplified MAC address to text 
    34:  *                                                              conversion logic
    35:  * Howard Schlunder             2/14/05 Fixed subnet broadcast calculation
    36:  ********************************************************************/
    37: #define THIS_IS_ANNOUNCE
    38: 
    39: #include "UDP.h"
    40: #include "Helpers.h"
    41: 
    42: #if !defined(STACK_USE_ANNOUNCE)
    43:     #error Announce module included while STACK_USE_ANNOUNCE is not defined
    44: #endif
    45: 
    46: #define ANNOUNCE_PORT   30303
    47: 
    48: 
    49: /*********************************************************************
    50:  * Function:        void AnnounceIP(void)
    51:  *
    52:  * PreCondition:    Stack is initialized()
    53:  *
    54:  * Input:           None
    55:  *
    56:  * Output:          None
    57:  *
    58:  * Side Effects:    None
    59:  *
    60:  * Overview:        AnnounceIP opens a UDP socket and transmits a 
    61:  *                                      broadcast packet to port 30303.  If a computer is
    62:  *                                      on the same subnet and a utility is looking for 
    63:  *                                      packets on the UDP port, it will receive the 
    64:  *                                      broadcast.  For this application, it is used to 
    65:  *                                      announce the change of this board's IP address.
    66:  *                                      The messages can be viewed with the MCHPDetect.exe
    67:  *                                      program.
    68:  *
    69:  * Note:            A UDP socket must be available before this 
    70:  *                                      function is called.  It is freed at the end of 
    71:  *                                      the function.  MAX_UDP_SOCKETS may need to be 
    72:  *                                      increased if other modules use UDP sockets.
    73:  ********************************************************************/
    74: void AnnounceIP(void)
    75: {
    76:         UDP_SOCKET      MySocket;
    77:         NODE_INFO       Remote;
    78:         BYTE            i;
    79:         
    80:         // Set the socket's destination to be a broadcast over our IP 
    81:         // subnet
    82:         // Set the MAC destination to be a broadcast
    83:         Remote.MACAddr.v[0] = 0xFF;
    84:         Remote.MACAddr.v[1] = 0xFF;
    85:         Remote.MACAddr.v[2] = 0xFF;
    86:         Remote.MACAddr.v[3] = 0xFF;
    87:         Remote.MACAddr.v[4] = 0xFF;
    88:         Remote.MACAddr.v[5] = 0xFF;
    89:         
    90:         // Set the IP subnet's broadcast address
    91:         Remote.IPAddr.Val = (AppConfig.MyIPAddr.Val & AppConfig.MyMask.Val) | 
    92:                                                  ~AppConfig.MyMask.Val;
    93:         
    94:         // Open a UDP socket for outbound transmission
    95:         MySocket = UDPOpen(2860, &Remote, ANNOUNCE_PORT);
    96:         
    97:         // Abort operation if no UDP sockets are available
    98:         // If this ever happens, incrementing MAX_UDP_SOCKETS in 
    99:         // StackTsk.h may help (at the expense of more global memory 
   100:         // resources).
   101:         if( MySocket == INVALID_UDP_SOCKET )
   102:                 return;
   103:         
   104:         // Make certain the socket can be written to
   105:         while( !UDPIsPutReady(MySocket) );
   106:         
   107:         // Begin sending our MAC address in human readable form.
   108:         // The MAC address theoretically could be obtained from the 
   109:         // packet header when the computer receives our UDP packet, 
   110:         // however, in practice, the OS will abstract away the useful
   111:         // information and it would be difficult to obtain.  It also 
   112:         // would be lost if this broadcast packet were forwarded by a
   113:         // router to a different portion of the network (note that 
   114:         // broadcasts are normally not forwarded by routers).
   115:         UDPPut('M');
   116:         UDPPut('A');
   117:         UDPPut('C');
   118:         UDPPut(' ');
   119:         UDPPut('A');
   120:         UDPPut('d');
   121:         UDPPut('d');
   122:         UDPPut('r');
   123:         UDPPut('e');
   124:         UDPPut('s');
   125:         UDPPut('s');
   126:         UDPPut(':');
   127:         UDPPut(' ');
   128:         
   129:         // Convert the MAC address bytes to hex (text) and then send it
   130:         i = 0;
   131:         while(1)
   132:         {
   133:                 UDPPut(btohexa_high(AppConfig.MyMACAddr.v[i]));
   134:             UDPPut(btohexa_low(AppConfig.MyMACAddr.v[i]));
   135:             if(++i == 6)
   136:                 break;
   137:             UDPPut('-');
   138:         }
   139: 
   140:         // Send some other human readable information.
   141:         UDPPut('.');
   142:         UDPPut(' ');
   143:         UDPPut('M');
   144:         UDPPut('y');
   145:         UDPPut(' ');
   146:         UDPPut('I');
   147:         UDPPut('P');
   148:         UDPPut(' ');
   149:         UDPPut('A');
   150:         UDPPut('d');
   151:         UDPPut('d');
   152:         UDPPut('r');
   153:         UDPPut('e');
   154:         UDPPut('s');
   155:         UDPPut('s');
   156:         UDPPut(' ');
   157:         UDPPut('h');
   158:         UDPPut('a');
   159:         UDPPut('s');
   160:         UDPPut(' ');
   161:         UDPPut('c');
   162:         UDPPut('h');
   163:         UDPPut('a');
   164:         UDPPut('n');
   165:         UDPPut('g');
   166:         UDPPut('e');
   167:         UDPPut('d');
   168:         UDPPut('.');
   169:         
   170:         // Send the packet
   171:         UDPFlush();
   172:         
   173:         // Close the socket so it can be used by other modules
   174:         UDPClose(MySocket);
   175: }
