1 // *********************************************************************
    2 //   Custom DPA Handler code template                                  *
    3 // *********************************************************************
    4 // Copyright (c) IQRF Tech s.r.o.
    5 //
    6 // File:    $RCSfile: CustomDpaHandler-Template-OptimizedSwitch.c,v $
    7 // Version: $Revision: 1.21 $
    8 // Date:    $Date: 2019/05/06 07:13:14 $
    9 //
   10 // Revision history:
   11 //   2019/01/10  Release for DPA 4.00
   12 //   2017/08/14  Release for DPA 3.01
   13 //   2017/03/13  Release for DPA 3.00
   14 //   2015/08/05  Release for DPA 2.20
   15 //   2014/10/31  Release for DPA 2.10
   16 //   2014/04/30  Release for DPA 2.00
   17 //
   18 // *********************************************************************
   19 
   20 // Online DPA documentation http://www.iqrf.org/DpaTechGuide/
   21 
   22 // Default IQRF include (modify the path according to your setup)
   23 #include "IQRF.h"
   24 
   25 // Uncomment to implement Custom DPA Handler for Coordinator
   26 //#define COORDINATOR_CUSTOM_HANDLER
   27 
   28 // Default DPA header (modify the path according to your setup)
   29 #include "DPA.h"
   30 // Default Custom DPA Handler header (modify the path according to your setup)
   31 #include "DPAcustomHandler.h"
   32 
   33 //############################################################################################
   34 
   35 // Place for global variables shared among CustomDpaHandler() and other function, otherwise local [static] variables are recommended
   36 // example: uns8 globalCounter;
   37 
   38 // Must be the 1st defined function in the source code in order to be placed at the correct FLASH location!
   39 //############################################################################################
   40 bit CustomDpaHandler()
   41 //############################################################################################
   42 {
   43   // Handler presence mark
   44   clrwdt();
   45 
   46   // Place for local static variables used only within CustomDpaHandler() among more events
   47   // example: static bit interruptOccured;
   48 
   49   // Detect DPA event to handle
   50   W = DpaEvent_LAST - GetDpaEvent();
   51   if ( !Carry )
   52     goto DpaHandleReturnFALSE;
   53 
   54   skip( W );
   55 #pragma computedGoto 1
   56   // Reverse order of events because of subtraction
   57   goto _DpaEvent_BondingButton;
   58   goto _DpaEvent_FrcResponseTime;
   59   goto _DpaEvent_UserDpaValue;
   60   goto DpaHandleReturnFALSE; // Formerly DpaEvent_AuthorizePreBonding
   61   goto _DpaEvent_PeerToPeer;
   62   goto _DpaEvent_BeforeSendingDpaResponse;
   63   goto _DpaEvent_ReceiveDpaRequest;
   64   goto _DpaEvent_IFaceReceive;
   65   goto _DpaEvent_ReceiveDpaResponse;
   66   goto _DpaEvent_FrcValue;
   67   goto _DpaEvent_DisableInterrupts;
   68   goto _DpaEvent_Reset;
   69   goto _DpaEvent_AfterSleep;
   70   goto _DpaEvent_BeforeSleep;
   71   goto _DpaEvent_AfterRouting;
   72   goto _DpaEvent_Notification;
   73   goto _DpaEvent_Init;
   74   goto _DpaEvent_Idle;
   75   goto _DpaEvent_Interrupt;
   76   goto _DpaEvent_DpaRequest;
   77 #pragma computedGoto 0
   78   ;
   79   // -------------------------------------------------
   80 _DpaEvent_DpaRequest:
   81   // Called to interpret DPA request for peripherals
   82   // -------------------------------------------------
   83   // Peripheral enumeration
   84   IfDpaEnumPeripherals_Else_PeripheralInfo_Else_PeripheralRequest()
   85   {
   86     _DpaMessage.EnumPeripheralsAnswer.UserPerNr = 0; // ?
   87     // FlagUserPer( _DpaMessage.EnumPeripheralsAnswer.UserPer, PNUM_USER + 0 ); // ?
   88     _DpaMessage.EnumPeripheralsAnswer.HWPID = 0x000F; // ????
   89     _DpaMessage.EnumPeripheralsAnswer.HWPIDver = 0; // ????
   90 
   91 DpaHandleReturnTRUE:
   92     return TRUE;
   93   }
   94   // -------------------------------------------------
   95   // Get information about peripheral
   96   else
   97   {
   98     if ( _PNUM == PNUM_USER + 0 ) // ?
   99     {
  100       _DpaMessage.PeripheralInfoAnswer.PerT = 0; // PERIPHERAL_TYPE_?
  101       _DpaMessage.PeripheralInfoAnswer.PerTE = 0; // PERIPHERAL_TYPE_EXTENDED_?
  102       _DpaMessage.PeripheralInfoAnswer.Par1 = 0; // ?
  103       _DpaMessage.PeripheralInfoAnswer.Par2 = 0; // ?
  104       goto DpaHandleReturnTRUE;
  105     }
  106 
  107     goto DpaHandleReturnFALSE;
  108   }
  109   // -------------------------------------------------
  110     // Handle peripheral command
  111   if ( _PNUM == PNUM_USER + 0 ) // ?
  112   {
  113     if ( _PCMD == 0 ) // ????
  114     {
  115       goto DpaHandleReturnTRUE;
  116     }
  117   }
  118 
  119   goto DpaHandleReturnFALSE;
  120 
  121   // -------------------------------------------------
  122 _DpaEvent_Interrupt:
  123 #ifdef DpaEvent_Interrupt
  124   // Do an extra quick background interrupt work
  125   // ! 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.
  126   // ! Make sure the event is the 1st _in the main switch statement at the handler routine.This ensures that the event is handled as the 1st one.
  127   // ! 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.
  128   // ! Only global variables or local ones marked by static keyword can be used to allow reentrancy.
  129   // ! Make sure race condition does not occur when accessing those variables at other places.
  130   // ! 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.
  131   // ! Do not call any OS functions except setINDFx().
  132   // ! Do not use any OS variables especially for writing access.
  133   // ! 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.
  134 
  135   return Carry;
  136 #endif
  137   // -------------------------------------------------
  138 _DpaEvent_Idle:
  139   // Do a quick background work when RF packet is not received
  140   goto DpaHandleReturnFALSE;
  141 
  142   // -------------------------------------------------
  143 _DpaEvent_Reset:
  144   // Called after module is reset
  145   //goto DpaHandleReturnTRUE; // return TRUE only if you handle node bonding/unbonding
  146   goto DpaHandleReturnFALSE;
  147 
  148   // -------------------------------------------------
  149 _DpaEvent_BondingButton:
  150   // Called to allow a bonding button customization 
  151   //goto DpaHandleReturnTRUE; // return TRUE to handle bonding button
  152   goto DpaHandleReturnFALSE;
  153 
  154   // -------------------------------------------------
  155 _DpaEvent_Init:
  156   // Do a one time initialization work before main loop starts
  157   goto DpaHandleReturnFALSE;
  158 
  159   // -------------------------------------------------
  160 _DpaEvent_ReceiveDpaRequest:
  161   // Called after DPA request was received
  162   //goto DpaHandleReturnTRUE; // return TRUE to skip default processing
  163   goto DpaHandleReturnFALSE;
  164 
  165   // -------------------------------------------------
  166 _DpaEvent_BeforeSendingDpaResponse:
  167   // Called before sending DPA response back to originator of DPA response
  168   goto DpaHandleReturnFALSE;
  169 
  170   // -------------------------------------------------
  171 _DpaEvent_Notification:
  172   // Called after DPA request was processed and after DPA response was sent
  173   goto DpaHandleReturnFALSE;
  174 
  175   // -------------------------------------------------
  176 _DpaEvent_AfterRouting:
  177   // Called after Notification and after routing of the DPA response was finished
  178   goto DpaHandleReturnFALSE;
  179 
  180   // -------------------------------------------------
  181 _DpaEvent_FrcValue:
  182 #ifdef DpaEvent_FrcValue
  183   // Called to get FRC value
  184 #endif
  185   goto DpaHandleReturnFALSE;
  186 
  187   // -------------------------------------------------
  188 _DpaEvent_FrcResponseTime:
  189 #ifdef DpaEvent_FrcResponseTime
  190   // Called to get FRC response time
  191 #endif
  192   goto DpaHandleReturnFALSE;
  193 
  194   // -------------------------------------------------
  195 _DpaEvent_BeforeSleep:
  196 #ifdef DpaEvent_BeforeSleep
  197   // Called before going to sleep
  198 #endif
  199   goto DpaHandleReturnFALSE;
  200 
  201   // -------------------------------------------------
  202 _DpaEvent_AfterSleep:
  203 #ifdef DpaEvent_AfterSleep
  204   // Called after woken up after sleep
  205 #endif
  206   goto DpaHandleReturnFALSE;
  207 
  208   // -------------------------------------------------
  209 _DpaEvent_DisableInterrupts:
  210   // Called when device needs all hardware interrupts to be disabled (before Reset, Restart, LoadCode, Remove bond and run RFPGM)
  211   goto DpaHandleReturnFALSE;
  212 
  213   // -------------------------------------------------
  214 _DpaEvent_ReceiveDpaResponse:
  215 #ifdef DpaEvent_ReceiveDpaResponse
  216   // Called after DPA response was received at coordinator
  217   //goto DpaHandleReturnTRUE; // return TRUE to skip default processing
  218 #endif
  219   goto DpaHandleReturnFALSE;
  220 
  221   // -------------------------------------------------
  222 _DpaEvent_IFaceReceive:
  223 #ifdef DpaEvent_IFaceReceive
  224   // Called after DPA request from interface master was received at coordinator
  225   //goto DpaHandleReturnTRUE; // return TRUE to skip default processing
  226 #endif
  227   goto DpaHandleReturnFALSE;
  228 
  229   // -------------------------------------------------
  230 _DpaEvent_PeerToPeer:
  231   // Called when peer-to-peer (non-networking) packet is received
  232   goto DpaHandleReturnFALSE;
  233 
  234   // -------------------------------------------------
  235 _DpaEvent_UserDpaValue:
  236   // Called when DPA is required to return User defined DPA value in the response
  237   goto DpaHandleReturnFALSE;
  238 
  239   // -------------------------------------------------
  240 
  241 DpaHandleReturnFALSE:
  242   return FALSE;
  243 }
  244 //############################################################################################
  245 // 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) 
  246 #include "DPAcustomHandler.h"
  247 //############################################################################################