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