1 // ********************************************************************************
    2 //   Custom DPA Handler code example - Intercepting DPA request and DPA responses *
    3 // ********************************************************************************
    4 // Copyright (c) MICRORISC s.r.o.
    5 //
    6 // File:    $RCSfile: CustomDpaHandler-HookDpa.c,v $
    7 // Version: $Revision: 1.34 $
    8 // Date:    $Date: 2022/02/25 09:41:25 $
    9 //
   10 // Revision history:
   11 //   2022/02/24  Release for DPA 4.17
   12 //   2017/03/14  Release for DPA 3.00
   13 //   2015/08/05  Release for DPA 2.20
   14 //   2014/10/31  Release for DPA 2.10
   15 //   2014/04/30  Release for DPA 2.00
   16 //
   17 // ********************************************************************************
   18 
   19 // Online DPA documentation https://doc.iqrf.org/DpaTechGuide/
   20 
   21 // Default IQRF include (modify the path according to your setup)
   22 #include "IQRF.h"
   23 
   24 // Uncomment to implement Custom DPA Handler for Coordinator
   25 //#define COORDINATOR_CUSTOM_HANDLER
   26 
   27 // Default DPA header (modify the path according to your setup)
   28 #include "DPA.h"
   29 // Default Custom DPA Handler header (modify the path according to your setup)
   30 #include "DPAcustomHandler.h"
   31 
   32 // This example show advance techniques intercepting DPA requests and responses
   33 // This example works only at STD mode, not at LP mode
   34 
   35 // Must be the 1st defined function in the source code in order to be placed at the correct FLASH location!
   36 //############################################################################################
   37 bit CustomDpaHandler()
   38 //############################################################################################
   39 {
   40   // Handler presence mark
   41   clrwdt();
   42 
   43   // Detect DPA event to handle
   44   switch ( GetDpaEvent() )
   45   {
   46 #ifdef DpaEvent_Interrupt
   47     // -------------------------------------------------
   48     case DpaEvent_Interrupt:
   49       // Do an extra quick background interrupt work
   50       // ! 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.
   51       // ! 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.
   52       // ! 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.
   53       // ! Only global variables or local ones marked by static keyword can be used to allow reentrancy.
   54       // ! Make sure race condition does not occur when accessing those variables at other places.
   55       // ! 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.
   56       // ! Do not call any OS functions except setINDFx().
   57       // ! Do not use any OS variables especially for writing access.
   58       // ! 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.
   59 
   60       return Carry;
   61 #endif
   62 
   63       // -------------------------------------------------
   64     case DpaEvent_ReceiveDpaRequest:
   65       // Called after DPA request was received
   66 
   67       // Do not allow request from Interface
   68       if ( TX == LOCAL_ADDRESS )
   69       {
   70         DpaApiSetPeripheralError( ERROR_NADR );
   71 
   72 ReturnFromRequestError:
   73         _PCMD |= RESPONSE_FLAG;
   74         return TRUE;
   75       }
   76 
   77       // If LEDR is ON, then write protect RAM peripheral by returning ERROR_FAIL
   78       if ( _PNUM == PNUM_RAM && _PCMD == CMD_RAM_WRITE && _LEDR == 1 )
   79       {
   80         DpaApiSetPeripheralError( ERROR_FAIL );
   81         goto ReturnFromRequestError;
   82       }
   83 
   84       break;
   85 
   86       // -------------------------------------------------
   87     case DpaEvent_BeforeSendingDpaResponse:
   88       // Called before sending DPA response back to originator of DPA response
   89 
   90       // If LEDG is ON, then add +1 to all read bytes from RAM peripheral
   91       if ( _PNUM == PNUM_RAM && _PCMD == CMD_RAM_READ && _LEDG == 1 )
   92       {
   93         FSR0 = _DpaMessage.Response.PData - 1;
   94         uns8 loop = _DpaDataLength;
   95         do
   96         {
   97           setINDF0( *++FSR0 + 1 );
   98         } while ( --loop != 0 );
   99       }
  100 
  101       break;
  102   }
  103 
  104   return FALSE;
  105 }
  106 //############################################################################################
  107 // Default Custom DPA Handler header; 2nd include implementing a Code bumper to detect too long code of the Custom DPA Handler (modify the path according to your setup)
  108 #include "DPAcustomHandler.h"
  109 //############################################################################################