1 // *********************************************************************
    2 //   Custom DPA Handler code template                                  *
    3 // *********************************************************************
    4 // Copyright (c) MICRORISC s.r.o.
    5 //
    6 // File:    $RCSfile: CustomDpaHandler-Template-OptimizedSwitch-Node.c,v $
    7 // Version: $Revision: 1.26 $
    8 // Date:    $Date: 2025/10/20 17:01:10 $
    9 //
   10 // Revision history:
   11 //   2025/05/30  Release for DPA 4.33
   12 //   2023/03/07  Release for DPA 4.30
   13 //   2022/10/05  Release for DPA 4.18
   14 //   2022/02/24  Release for DPA 4.17
   15 //   2021/08/20  Release for DPA 4.16
   16 //   2020/09/03  Release for DPA 4.15
   17 //   2020/02/27  Release for DPA 4.13
   18 //   2019/01/10  Release for DPA 4.00
   19 //   2017/08/14  Release for DPA 3.01
   20 //   2017/03/13  Release for DPA 3.00
   21 //   2015/08/05  Release for DPA 2.20
   22 //   2014/10/31  Release for DPA 2.10
   23 //   2014/04/30  Release for DPA 2.00
   24 //
   25 // *********************************************************************
   26 
   27 // Online DPA documentation https://doc.iqrf.org/DpaTechGuide/
   28 
   29 // Default IQRF include (modify the path according to your setup)
   30 #include "IQRF.h"
   31 
   32 // Default DPA header (modify the path according to your setup)
   33 #include "DPA.h"
   34 // Default Custom DPA Handler header (modify the path according to your setup)
   35 #include "DPAcustomHandler.h"
   36 
   37 // Uncomment the following includes if the respective component is needed
   38 // IQRF standards header (modify the path according to your setup)
   39 //#include "IQRFstandard.h"
   40 //#include "IQRF_HWPID.h"
   41 //#include "NFC.c"
   42 
   43 //############################################################################################
   44 
   45 // Place for global variables shared among CustomDpaHandler() and other function, otherwise local [static] variables are recommended
   46 // example: uns8 globalCounter;
   47 
   48 // Must be the 1st defined function in the source code in order to be placed at the correct FLASH location!
   49 //############################################################################################
   50 // https://doc.iqrf.org/DpaTechGuide/pages/custom-dpa-handler.html
   51 bit CustomDpaHandler()
   52 //############################################################################################
   53 {
   54   // Handler presence mark
   55   clrwdt();
   56 
   57   // Place for local static variables used only within CustomDpaHandler() among more events
   58   // example: static bit interruptOccured;
   59 
   60   // Detect DPA event to handle
   61   // Do not handle "out of range" events. The handled event with the highest value can be used instead of DpaEvent_LAST and "computedGoto" section made thus event shorter.
   62   W = DpaEvent_LAST - GetDpaEvent();
   63   if ( !Carry )
   64     goto DpaHandleReturnFALSE;
   65 
   66   skip( W );
   67 #pragma computedGoto 1
   68   // Reverse order of events because of subtraction
   69 #if DpaEvent_LAST == DpaEvent_AsyncResponse
   70   goto _DpaEvent_AsyncResponse;
   71   goto _DpaEvent_AsyncRequest;
   72   goto _DpaEvent_InStandby;
   73   goto _DpaEvent_MenuItemFinalize;
   74   goto _DpaEvent_MenuItemSelected;
   75   goto _DpaEvent_MenuActivated;
   76 #endif
   77   goto _DpaEvent_VerifyLocalFrc;
   78   goto _DpaEvent_Indicate;
   79   goto _DpaEvent_BondingButton;
   80   goto _DpaEvent_FrcResponseTime;
   81   goto _DpaEvent_UserDpaValue;
   82   goto DpaHandleReturnFALSE; // Formerly DpaEvent_AuthorizePreBonding
   83   goto _DpaEvent_PeerToPeer;
   84   goto _DpaEvent_BeforeSendingDpaResponse;
   85   goto _DpaEvent_ReceiveDpaRequest;
   86   goto DpaHandleReturnFALSE; // _DpaEvent_IFaceReceive : not implemented at Node
   87   goto DpaHandleReturnFALSE; // _DpaEvent_ReceiveDpaResponse : not implemented at Node
   88   goto _DpaEvent_FrcValue;
   89   goto _DpaEvent_DisableInterrupts;
   90   goto _DpaEvent_Reset;
   91   goto _DpaEvent_AfterSleep;
   92   goto _DpaEvent_BeforeSleep;
   93   goto _DpaEvent_AfterRouting;
   94   goto _DpaEvent_Notification;
   95   goto _DpaEvent_Init;
   96   goto _DpaEvent_Idle;
   97   goto _DpaEvent_Interrupt;
   98   goto _DpaEvent_DpaRequest;
   99 #pragma computedGoto 0
  100   ;
  101   // -------------------------------------------------
  102 _DpaEvent_DpaRequest:
  103   // Called to interpret DPA request for peripherals
  104   // https://doc.iqrf.org/DpaTechGuide/pages/EventDpaRequest.html
  105   // -------------------------------------------------
  106   // Peripheral enumeration
  107   // https://doc.iqrf.org/DpaTechGuide/pages/enumerate-peripherals.html
  108   IfDpaEnumPeripherals_Else_PeripheralInfo_Else_PeripheralRequest()
  109   {
  110     _DpaMessage.EnumPeripheralsAnswer.UserPerNr |= 0; // ?
  111     // FlagUserPer( _DpaMessage.EnumPeripheralsAnswer.UserPer, PNUM_USER + 0 ); // ?
  112     _DpaMessage.EnumPeripheralsAnswer.HWPID |= 0x000F; // ????
  113     _DpaMessage.EnumPeripheralsAnswer.HWPIDver |= 0; // ????
  114 
  115 DpaHandleReturnTRUE:
  116     return TRUE;
  117   }
  118   // -------------------------------------------------
  119   // Get information about peripheral
  120   // https://doc.iqrf.org/DpaTechGuide/pages/get-peripheral-info.html
  121   else
  122   {
  123     if ( _PNUM == PNUM_USER + 0 ) // ?
  124     {
  125       _DpaMessage.PeripheralInfoAnswer.PerT = 0; // PERIPHERAL_TYPE_?
  126       _DpaMessage.PeripheralInfoAnswer.PerTE = 0; // PERIPHERAL_TYPE_EXTENDED_?
  127       _DpaMessage.PeripheralInfoAnswer.Par1 = 0; // ?
  128       _DpaMessage.PeripheralInfoAnswer.Par2 = 0; // ?
  129       goto DpaHandleReturnTRUE;
  130     }
  131 
  132     goto DpaHandleReturnFALSE;
  133   }
  134   // -------------------------------------------------
  135   // Handle peripheral command
  136   // https://doc.iqrf.org/DpaTechGuide/pages/handle-peripheral-request.html
  137   if ( _PNUM == PNUM_USER + 0 ) // ?
  138   {
  139     if ( _PCMD == 0 ) // ????
  140     {
  141       goto DpaHandleReturnTRUE;
  142     }
  143   }
  144 
  145   goto DpaHandleReturnFALSE;
  146 
  147   // -------------------------------------------------
  148 _DpaEvent_Interrupt:
  149   // Do an extra quick background interrupt work
  150   // ! 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.
  151   // ! 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.
  152   // ! 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.
  153   // ! Only global variables or local ones marked by static keyword can be used to allow reentrancy.
  154   // ! Make sure race condition does not occur when accessing those variables at other places.
  155   // ! 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.
  156   // ! Do not call any OS functions except setINDFx().
  157   // ! Do not use any OS variables especially for writing access.
  158   // ! 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.
  159   // https://doc.iqrf.org/DpaTechGuide/pages/EventInterrupt.html
  160   return Carry;
  161   // -------------------------------------------------
  162 _DpaEvent_Idle:
  163   // Do a quick background work when RF packet is not received
  164   // https://doc.iqrf.org/DpaTechGuide/pages/idle.html
  165   goto DpaHandleReturnFALSE;
  166 
  167   // -------------------------------------------------
  168 _DpaEvent_Reset:
  169   // Called after module is reset
  170   // https://doc.iqrf.org/DpaTechGuide/pages/ResetEvent.html
  171 
  172   //goto DpaHandleReturnTRUE; // return TRUE only if you handle node bonding/unbonding
  173   goto DpaHandleReturnFALSE;
  174 
  175   // -------------------------------------------------
  176 _DpaEvent_BondingButton:
  177   // Called to allow a bonding button customization
  178   // https://doc.iqrf.org/DpaTechGuide/pages/bondingbutton.html
  179 
  180   //goto DpaHandleReturnTRUE; // return TRUE to handle bonding button
  181   goto DpaHandleReturnFALSE;
  182 
  183   // -------------------------------------------------
  184 _DpaEvent_Indicate:
  185   // Called to allow a customization of the device indication
  186   // https://doc.iqrf.org/DpaTechGuide/pages/IndicateEvent.html
  187 
  188   //goto DpaHandleReturnTRUE; // return TRUE to skip default indication
  189   goto DpaHandleReturnFALSE;
  190 
  191   // -------------------------------------------------
  192 _DpaEvent_Init:
  193   // Do a one time initialization before main loop starts
  194   // https://doc.iqrf.org/DpaTechGuide/pages/init.html
  195   goto DpaHandleReturnFALSE;
  196 
  197   // -------------------------------------------------
  198 _DpaEvent_ReceiveDpaRequest:
  199   // Called after DPA request was received
  200   // https://doc.iqrf.org/DpaTechGuide/pages/receivedparequest.html
  201 
  202   //goto DpaHandleReturnTRUE; // return TRUE to skip default processing
  203   goto DpaHandleReturnFALSE;
  204 
  205   // -------------------------------------------------
  206 _DpaEvent_BeforeSendingDpaResponse:
  207   // Called before sending DPA response back to originator of DPA response
  208   // https://doc.iqrf.org/DpaTechGuide/pages/beforesendingdparesponse.html
  209 
  210   goto DpaHandleReturnFALSE;
  211 
  212   // -------------------------------------------------
  213 _DpaEvent_Notification:
  214   // Called after DPA request was processed and after DPA response was sent
  215   // https://doc.iqrf.org/DpaTechGuide/pages/notification.html
  216   goto DpaHandleReturnFALSE;
  217 
  218   // -------------------------------------------------
  219 _DpaEvent_AfterRouting:
  220   // Called after Notification and after routing of the DPA response was finished
  221   // https://doc.iqrf.org/DpaTechGuide/pages/afterrouting.html
  222   goto DpaHandleReturnFALSE;
  223 
  224   // -------------------------------------------------
  225 _DpaEvent_FrcValue:
  226   // Called to get FRC value
  227   // https://doc.iqrf.org/DpaTechGuide/pages/frcvalue.html
  228   goto DpaHandleReturnFALSE;
  229 
  230   // -------------------------------------------------
  231 _DpaEvent_FrcResponseTime:
  232   // Called to get FRC response time
  233   // https://doc.iqrf.org/DpaTechGuide/pages/frcresponsetime.html
  234   goto DpaHandleReturnFALSE;
  235 
  236   // -------------------------------------------------
  237 _DpaEvent_BeforeSleep:
  238   // Called before going to sleep
  239   // https://doc.iqrf.org/DpaTechGuide/pages/beforesleep.html
  240   goto DpaHandleReturnFALSE;
  241 
  242   // -------------------------------------------------
  243 _DpaEvent_AfterSleep:
  244   // Called after woken up after sleep
  245    // https://doc.iqrf.org/DpaTechGuide/pages/aftersleep.html
  246   goto DpaHandleReturnFALSE;
  247 
  248   // -------------------------------------------------
  249 _DpaEvent_DisableInterrupts:
  250   // Called when device needs all hardware interrupts to be disabled (before Reset, Restart, LoadCode, Remove bond and run RFPGM)
  251   // https://doc.iqrf.org/DpaTechGuide/pages/eventDisableInterrupts.html
  252   goto DpaHandleReturnFALSE;
  253 
  254   // -------------------------------------------------
  255 _DpaEvent_PeerToPeer:
  256   // Called when peer-to-peer (non-networking) packet is received
  257   // https://doc.iqrf.org/DpaTechGuide/pages/peertopeer.html
  258   goto DpaHandleReturnFALSE;
  259 
  260   // -------------------------------------------------
  261 _DpaEvent_UserDpaValue:
  262   // Called when DPA is required to return User defined DPA value in the response
  263   // https://doc.iqrf.org/DpaTechGuide/pages/userdpavalue.html
  264   goto DpaHandleReturnFALSE;
  265 
  266   // -------------------------------------------------
  267 _DpaEvent_VerifyLocalFrc:
  268   // Called to verify local FRC command
  269   // https://doc.iqrf.org/DpaTechGuide/pages/verifylocalfrc.html
  270 
  271   //goto DpaHandleReturnTRUE; // return TRUE allow FRC command
  272   goto DpaHandleReturnFALSE;
  273 
  274   // -------------------------------------------------
  275 #ifdef DpaEvent_MenuActivated
  276   _DpaEvent_MenuActivated :
  277   // Called to customize DPA menu
  278   // https://doc.iqrf.org/DpaTechGuide/pages/menuactivated.html
  279 
  280   //goto DpaHandleReturnTRUE; // return TRUE to allow customizing menu specified by userReg1
  281   goto DpaHandleReturnFALSE;
  282 #endif
  283 
  284   // -------------------------------------------------
  285 #ifdef DpaEvent_MenuItemSelected
  286   _DpaEvent_MenuItemSelected :
  287   // Called to indicate "OK" or "Error" for selected menu item
  288   // https://doc.iqrf.org/DpaTechGuide/pages/menuitemselected.html
  289 
  290   //goto DpaHandleReturnTRUE; // return TRUE to indicate "OK" for menu item specified by userReg1, otherwise to indicate Error
  291   goto DpaHandleReturnFALSE;
  292 #endif
  293 
  294   // -------------------------------------------------
  295 #ifdef DpaEvent_MenuItemFinalize
  296   _DpaEvent_MenuItemFinalize :
  297   // Called to finalize menu item execution
  298   // https://doc.iqrf.org/DpaTechGuide/pages/menuitemfinalize.html
  299 
  300   goto DpaHandleReturnFALSE;
  301 #endif
  302 
  303   // -------------------------------------------------
  304 #ifdef DpaEvent_InStandby
  305   _DpaEvent_InStandby :
  306   // Called to set WDT during Standby
  307   // https://doc.iqrf.org/DpaTechGuide/pages/instandby.html
  308 
  309   //goto DpaHandleReturnTRUE; // return TRUE to indicate that userReg1 contains WDT settings
  310   goto DpaHandleReturnFALSE;
  311 #endif
  312 
  313   // -------------------------------------------------
  314 #ifdef DpaEvent_AsyncRequest
  315   _DpaEvent_AsyncRequest :
  316   // Optionally passed DPA Request to execute to the DPA
  317   // https://doc.iqrf.org/DpaTechGuide/pages/asyncrequest.html
  318 
  319   //goto DpaHandleReturnTRUE; // return TRUE to pass the DPA Request
  320   goto DpaHandleReturnFALSE;
  321 #endif
  322 
  323   // -------------------------------------------------
  324 #ifdef DpaEvent_AsyncResponse
  325   _DpaEvent_AsyncResponse :
  326   // Called with DPA response for DPA request from DpaEvent_MenuItemFinalize
  327   // https://doc.iqrf.org/DpaTechGuide/pages/asyncresponse.html
  328 
  329   goto DpaHandleReturnFALSE;
  330 #endif
  331   // -------------------------------------------------
  332 
  333 DpaHandleReturnFALSE:
  334   return FALSE;
  335 }
  336 
  337 //############################################################################################
  338 // Uncomment the following includes if the respective component is needed
  339 //#include "NFC.c"
  340 
  341 // 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)
  342 #include "DPAcustomHandler.h"
  343 //############################################################################################