Subversion Repositories HomeAutomation

Rev

Rev 363 | Blame | Compare with Previous | Last modification | View Log | SVN | RSS feed

  1. /*********************************************************************
  2.  *
  3.  *                  ARP Server Module for Microchip TCP/IP Stack
  4.  *
  5.  *********************************************************************
  6.  * FileName:        ARPTsk.c
  7.  * Dependencies:    Compiler.h
  8.  *                  string.h
  9.  *                  ARP.h
  10.  *                  ARPTsk.h
  11.  * Processor:       PIC18, PIC24F, PIC24H, dsPIC30F, dsPIC33F
  12.  * Complier:        Microchip C18 v3.02 or higher
  13.  *                  Microchip C30 v2.01 or higher
  14.  * Company:         Microchip Technology, Inc.
  15.  *
  16.  * Software License Agreement
  17.  *
  18.  * This software is owned by Microchip Technology Inc. ("Microchip")
  19.  * and is supplied to you for use exclusively as described in the
  20.  * associated software agreement.  This software is protected by
  21.  * software and other intellectual property laws.  Any use in
  22.  * violation of the software license may subject the user to criminal
  23.  * sanctions as well as civil liability.  Copyright 2006 Microchip
  24.  * Technology Inc.  All rights reserved.
  25.  *
  26.  * This software is provided "AS IS."  MICROCHIP DISCLAIMS ALL
  27.  * WARRANTIES, EXPRESS, IMPLIED, STATUTORY OR OTHERWISE, NOT LIMITED
  28.  * TO MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, AND
  29.  * INFRINGEMENT.  Microchip shall in no event be liable for special,
  30.  * incidental, or consequential damages.
  31.  *
  32.  *
  33.  * Author               Date    Comment
  34.  *~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  35.  * Nilesh Rajbharti     8/20/01 Original        (Rev 1.0)
  36.  * Nilesh Rajbharti     2/9/02  Cleanup
  37.  * Nilesh Rajbharti     5/22/02 Rev 2.0 (See version.log for detail)
  38.  ********************************************************************/
  39.  
  40. #include "..\Include\Compiler.h"
  41. #include <string.h>
  42.  
  43. #include "..\Include\ARP.h"
  44. #include "..\Include\ARPTsk.h"
  45.  
  46. /*
  47.  * ARP Task FSM States
  48.  */
  49. typedef enum _ARP_STATE
  50. {
  51.     SM_ARP_IDLE,
  52.     SM_ARP_REPLY
  53. } ARP_STATE;
  54.  
  55.  
  56. /*
  57.  * This ARP task caches one ARP response.
  58.  */
  59. static ARP_STATE smARP;
  60.  
  61. #ifdef STACK_CLIENT_MODE
  62. static NODE_INFO Cache;
  63. #endif
  64.  
  65.  
  66. /*********************************************************************
  67.  * Function:        void ARPInit(void)
  68.  *
  69.  * PreCondition:    None
  70.  *
  71.  * Input:           None
  72.  *
  73.  * Output:          ARP Cache is initialized.
  74.  *
  75.  * Side Effects:    None
  76.  *
  77.  * Overview:        None
  78.  *
  79.  * Note:            None
  80.  ********************************************************************/
  81. void ARPInit(void)
  82. {
  83.     smARP = SM_ARP_IDLE;
  84.  
  85. #ifdef STACK_CLIENT_MODE
  86.     Cache.MACAddr.v[0] = 0xff;
  87.     Cache.MACAddr.v[1] = 0xff;
  88.     Cache.MACAddr.v[2] = 0xff;
  89.     Cache.MACAddr.v[3] = 0xff;
  90.     Cache.MACAddr.v[4] = 0xff;
  91.     Cache.MACAddr.v[5] = 0xff;
  92.  
  93.     Cache.IPAddr.Val = 0x0;
  94. #endif
  95. }
  96.  
  97.  
  98.  
  99. /*********************************************************************
  100.  * Function:        BOOL ARPProcess(void)
  101.  *
  102.  * PreCondition:    ARP packet is ready in MAC buffer.
  103.  *
  104.  * Input:           None
  105.  *
  106.  * Output:          ARP FSM is executed.
  107.  *
  108.  * Side Effects:    None
  109.  *
  110.  * Overview:        None
  111.  *
  112.  * Note:            None
  113.  ********************************************************************/
  114. BOOL ARPProcess(void)
  115. {
  116.     NODE_INFO remoteNode;
  117.     BYTE opCode;
  118.  
  119.     switch(smARP)
  120.     {
  121.     case SM_ARP_IDLE:
  122.         if ( !ARPGet(&remoteNode, &opCode) )
  123.             break;
  124.  
  125.         if ( opCode == ARP_REPLY )
  126.         {
  127. #ifdef STACK_CLIENT_MODE
  128.             Cache.MACAddr = remoteNode.MACAddr;
  129.             Cache.IPAddr.Val = remoteNode.IPAddr.Val;
  130. #endif
  131.             break;
  132.         }
  133.         else
  134.             smARP = SM_ARP_REPLY;
  135.  
  136.     default:
  137.         if(ARPPut(&remoteNode, ARP_REPLY))
  138.         {
  139.             smARP = SM_ARP_IDLE;
  140.         }
  141.         else
  142.             return FALSE;
  143.         break;
  144.     }
  145.     return TRUE;
  146. }
  147.  
  148.  
  149. /*********************************************************************
  150.  * Function:        void ARPResolve(IP_ADDR* IPAddr)
  151.  *
  152.  * PreCondition:    MACIsTxReady(TRUE) returns TRUE
  153.  *
  154.  * Input:           IPAddr  - IP Address to be resolved.
  155.  *
  156.  * Output:          None
  157.  *
  158.  * Side Effects:    None
  159.  *
  160.  * Overview:        An ARP request is sent.
  161.  *
  162.  * Note:            This function is available only when
  163.  *                  STACK_CLIENT_MODE is defined.
  164.  ********************************************************************/
  165. #ifdef STACK_CLIENT_MODE
  166. void ARPResolve(IP_ADDR *IPAddr)
  167. {
  168.     NODE_INFO remoteNode;
  169.  
  170.     remoteNode.IPAddr = *IPAddr;
  171.  
  172.     ARPPut(&remoteNode, ARP_REQUEST);
  173. }
  174. #endif
  175.  
  176.  
  177.  
  178. /*********************************************************************
  179.  * Function:        BOOL ARPIsResolved(IP_ADDR* IPAddr,
  180.  *                                      MAC_ADDR *MACAddr)
  181.  *
  182.  * PreCondition:    None
  183.  *
  184.  * Input:           IPAddr      - IPAddress to be resolved.
  185.  *                  MACAddr     - Buffer to hold corresponding
  186.  *                                MAC Address.
  187.  *
  188.  * Output:          TRUE if given IP Address has been resolved.
  189.  *                  FALSE otherwise.
  190.  *
  191.  * Side Effects:    None
  192.  *
  193.  * Overview:        None
  194.  *
  195.  * Note:            This function is available only when
  196.  *                  STACK_CLIENT_MODE is defined.
  197.  ********************************************************************/
  198. #ifdef STACK_CLIENT_MODE
  199. BOOL ARPIsResolved(IP_ADDR *IPAddr, MAC_ADDR *MACAddr)
  200. {
  201.     if(Cache.IPAddr.Val == IPAddr->Val || Cache.IPAddr.Val == AppConfig.MyGateway.Val)
  202.     {
  203.         *MACAddr = Cache.MACAddr;
  204.         return TRUE;
  205.     }
  206.     return FALSE;
  207. }
  208. #endif
  209.  
  210.