1 // ********************************************************************************
    2 //   Custom DPA Handler code example - Intercepting DPA request and DPA responses *
    3 // ********************************************************************************
    4 // Copyright (c) IQRF Tech s.r.o.
    5 //
    6 // File:    $RCSfile: CustomDpaHandler-HookDpa.c,v $
    7 // Version: $Revision: 1.24 $
    8 // Date:    $Date: 2018/10/08 15:56:05 $
    9 //
   10 // Revision history:
   11 //   2017/03/14  Release for DPA 3.00
   12 //   2015/08/05  Release for DPA 2.20
   13 //   2014/10/31  Release for DPA 2.10
   14 //   2014/04/30  Release for DPA 2.00
   15 //
   16 // ********************************************************************************
   17 
   18 // Online DPA documentation http://www.iqrf.org/DpaTechGuide/
   19 
   20 // Default IQRF include (modify the path according to your setup)
   21 #include "IQRF.h"
   22 
   23 // Uncomment to implement Custom DPA Handler for Coordinator
   24 //#define COORDINATOR_CUSTOM_HANDLER
   25 
   26 // Default DPA header (modify the path according to your setup)
   27 #include "DPA.h"
   28 // Default Custom DPA Handler header (modify the path according to your setup)
   29 #include "DPAcustomHandler.h"
   30 
   31 // This example show advance techniques intercepting DPA requests and responses
   32 // This example works only at STD mode, not at LP mode
   33 
   34 // Must be the 1st defined function in the source code in order to be placed at the correct FLASH location!
   35 //############################################################################################
   36 bit CustomDpaHandler()
   37 //############################################################################################
   38 {
   39   // Handler presence mark
   40   clrwdt();
   41 
   42   // Detect DPA event to handle
   43   switch ( GetDpaEvent() )
   44   {
   45 #ifdef DpaEvent_Interrupt
   46     // -------------------------------------------------
   47     case DpaEvent_Interrupt:
   48       // Do an extra quick background interrupt work
   49       // ! The time spent handling this event is critical.If there is no interrupt to handle return immediately otherwise keep the code as fast as possible.
   50       // ! Make sure the event is the 1st case in the main switch statement at the handler routine.This ensures that the event is handled as the 1st one.
   51       // ! It is desirable that this event is handled with immediate return even if it is not used by the custom handler because the Interrupt event is raised on every MCU interrupt and the “empty” return handler ensures the shortest possible interrupt routine response time.
   52       // ! Only global variables or local ones marked by static keyword can be used to allow reentrancy.
   53       // ! Make sure race condition does not occur when accessing those variables at other places.
   54       // ! Make sure( inspect.lst file generated by C compiler ) compiler does not create any hidden temporary local variable( occurs when using division, multiplication or bit shifts ) at the event handler code.The name of such variable is usually Cnumbercnt.
   55       // ! Do not call any OS functions except setINDFx().
   56       // ! Do not use any OS variables especially for writing access.
   57       // ! All above rules apply also to any other function being called from the event handler code, although calling any function from Interrupt event is not recommended because of additional MCU stack usage.
   58 
   59       return Carry;
   60 #endif
   61 
   62       // -------------------------------------------------
   63     case DpaEvent_ReceiveDpaRequest:
   64       // Called after DPA request was received
   65 
   66       // Do not allow request from Interface
   67       if ( TX == LOCAL_ADDRESS )
   68       {
   69         DpaApiSetPeripheralError( ERROR_NADR );
   70 
   71 ReturnFromRequestError:
   72         _PCMD |= RESPONSE_FLAG;
   73         return TRUE;
   74       }
   75 
   76       // If LEDR is ON, then write protect RAM peripheral by returning ERROR_FAIL
   77       if ( _PNUM == PNUM_RAM && _PCMD == CMD_RAM_WRITE && _LEDR == 1 )
   78       {
   79         DpaApiSetPeripheralError( ERROR_FAIL );
   80         goto ReturnFromRequestError;
   81       }
   82 
   83       break;
   84 
   85       // -------------------------------------------------
   86     case DpaEvent_BeforeSendingDpaResponse:
   87       // Called before sending DPA response back to originator of DPA response
   88 
   89       // If LEDG is ON, then add +1 to all read bytes from RAM peripheral
   90       if ( _PNUM == PNUM_RAM && _PCMD == CMD_RAM_READ && _LEDG == 1 )
   91       {
   92         FSR0 = _DpaMessage.Response.PData - 1;
   93         uns8 loop = _DpaDataLength;
   94         do
   95         {
   96           setINDF0( *++FSR0 + 1 );
   97         } while ( --loop != 0 );
   98       }
   99 
  100       break;
  101   }
  102 
  103   return FALSE;
  104 }
  105 //############################################################################################
  106 // Default Custom DPA Handler header; 2nd include to implement Code bumper to detect too long code of the Custom DPA Handler (modify the path according to your setup) 
  107 #include "DPAcustomHandler.h"
  108 //############################################################################################