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.58 $
    8 // Date:    $Date: 2023/11/03 16:32:29 $
    9 //
   10 // Revision history:
   11 //   2023/03/07  Release for DPA 4.30
   12 //   2022/10/05  Release for DPA 4.18
   13 //   2022/02/24  Release for DPA 4.17
   14 //   2021/08/20  Release for DPA 4.16
   15 //   2020/09/03  Release for DPA 4.15
   16 //   2020/02/27  Release for DPA 4.13
   17 //   2019/01/10  Release for DPA 4.00
   18 //   2017/08/14  Release for DPA 3.01
   19 //   2017/03/13  Release for DPA 3.00
   20 //   2015/08/05  Release for DPA 2.20
   21 //   2014/10/31  Release for DPA 2.10
   22 //   2014/04/30  Release for DPA 2.00
   23 //
   24 // *********************************************************************
   25 
   26 // Online DPA documentation https://doc.iqrf.org/DpaTechGuide/
   27 
   28 // Default IQRF include (modify the path according to your setup)
   29 #include "IQRF.h"
   30 
   31 // Uncomment to implement Custom DPA Handler for Coordinator
   32 //#define COORDINATOR_CUSTOM_HANDLER
   33 
   34 // Default DPA header (modify the path according to your setup)
   35 #include "DPA.h"
   36 // Default Custom DPA Handler header (modify the path according to your setup)
   37 #include "DPAcustomHandler.h"
   38 
   39 // Uncomment the following includes if the respective component is needed
   40 // IQRF standards header (modify the path according to your setup)
   41 //#include "IQRFstandard.h"
   42 //#include "IQRF_HWPID.h"
   43 //#include "NFC.c"
   44 
   45 //############################################################################################
   46 
   47 // Place for global variables shared among CustomDpaHandler() and other function, otherwise local [static] variables are recommended
   48 // example: uns8 globalCounter;
   49 
   50 // Must be the 1st defined function in the source code in order to be placed at the correct FLASH location!
   51 //############################################################################################
   52 // https://doc.iqrf.org/DpaTechGuide/pages/custom-dpa-handler.html
   53 bit CustomDpaHandler()
   54 //############################################################################################
   55 {
   56   // Handler presence mark
   57   clrwdt();
   58 
   59   // Place for local static variables used only within CustomDpaHandler() among more events
   60   // example: static bit interruptOccured;
   61 
   62   // Detect DPA event to handle
   63   // 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.
   64   W = DpaEvent_LAST - GetDpaEvent();
   65   if ( !Carry )
   66     goto DpaHandleReturnFALSE;
   67 
   68   skip( W );
   69 #pragma computedGoto 1
   70   // Reverse order of events because of subtraction
   71 #if DpaEvent_LAST == DpaEvent_InStandby
   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 _DpaEvent_IFaceReceive;
   87   goto _DpaEvent_ReceiveDpaResponse;
   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 #ifdef DpaEvent_Interrupt
  150   // Do an extra quick background interrupt work
  151   // ! 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.
  152   // ! 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.
  153   // ! 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.
  154   // ! Only global variables or local ones marked by static keyword can be used to allow reentrancy.
  155   // ! Make sure race condition does not occur when accessing those variables at other places.
  156   // ! 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.
  157   // ! Do not call any OS functions except setINDFx().
  158   // ! Do not use any OS variables especially for writing access.
  159   // ! 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.
  160   // https://doc.iqrf.org/DpaTechGuide/pages/EventInterrupt.html
  161 #endif
  162   return Carry;
  163   // -------------------------------------------------
  164 _DpaEvent_Idle:
  165   // Do a quick background work when RF packet is not received
  166   // https://doc.iqrf.org/DpaTechGuide/pages/idle.html
  167   goto DpaHandleReturnFALSE;
  168 
  169   // -------------------------------------------------
  170 _DpaEvent_Reset:
  171   // Called after module is reset
  172   // https://doc.iqrf.org/DpaTechGuide/pages/ResetEvent.html
  173 
  174   //goto DpaHandleReturnTRUE; // return TRUE only if you handle node bonding/unbonding
  175   goto DpaHandleReturnFALSE;
  176 
  177   // -------------------------------------------------
  178 _DpaEvent_BondingButton:
  179 #ifdef DpaEvent_BondingButton
  180   // Called to allow a bonding button customization
  181   // https://doc.iqrf.org/DpaTechGuide/pages/bondingbutton.html
  182 
  183   //goto DpaHandleReturnTRUE; // return TRUE to handle bonding button
  184 #endif
  185   goto DpaHandleReturnFALSE;
  186 
  187   // -------------------------------------------------
  188 _DpaEvent_Indicate:
  189 #ifdef DpaEvent_BondingButton
  190   // Called to allow a customization of the device indication
  191   // https://doc.iqrf.org/DpaTechGuide/pages/IndicateEvent.html
  192 
  193   //goto DpaHandleReturnTRUE; // return TRUE to skip default indication
  194 #endif
  195   goto DpaHandleReturnFALSE;
  196 
  197   // -------------------------------------------------
  198 _DpaEvent_Init:
  199   // Do a one time initialization before main loop starts
  200   // https://doc.iqrf.org/DpaTechGuide/pages/init.html
  201   goto DpaHandleReturnFALSE;
  202 
  203   // -------------------------------------------------
  204 _DpaEvent_ReceiveDpaRequest:
  205   // Called after DPA request was received
  206   // https://doc.iqrf.org/DpaTechGuide/pages/receivedparequest.html
  207 
  208   //goto DpaHandleReturnTRUE; // return TRUE to skip default processing
  209   goto DpaHandleReturnFALSE;
  210 
  211   // -------------------------------------------------
  212 _DpaEvent_BeforeSendingDpaResponse:
  213   // Called before sending DPA response back to originator of DPA response
  214   // https://doc.iqrf.org/DpaTechGuide/pages/beforesendingdparesponse.html
  215 
  216   goto DpaHandleReturnFALSE;
  217 
  218   // -------------------------------------------------
  219 _DpaEvent_Notification:
  220   // Called after DPA request was processed and after DPA response was sent
  221   // https://doc.iqrf.org/DpaTechGuide/pages/notification.html
  222   goto DpaHandleReturnFALSE;
  223 
  224   // -------------------------------------------------
  225 _DpaEvent_AfterRouting:
  226   // Called after Notification and after routing of the DPA response was finished
  227   // https://doc.iqrf.org/DpaTechGuide/pages/afterrouting.html
  228   goto DpaHandleReturnFALSE;
  229 
  230   // -------------------------------------------------
  231 _DpaEvent_FrcValue:
  232 #ifdef DpaEvent_FrcValue
  233   // Called to get FRC value
  234   // https://doc.iqrf.org/DpaTechGuide/pages/frcvalue.html
  235 #endif
  236   goto DpaHandleReturnFALSE;
  237 
  238   // -------------------------------------------------
  239 _DpaEvent_FrcResponseTime:
  240 #ifdef DpaEvent_FrcResponseTime
  241   // Called to get FRC response time
  242   // https://doc.iqrf.org/DpaTechGuide/pages/frcresponsetime.html
  243 #endif
  244   goto DpaHandleReturnFALSE;
  245 
  246   // -------------------------------------------------
  247 _DpaEvent_BeforeSleep:
  248 #ifdef DpaEvent_BeforeSleep
  249   // Called before going to sleep
  250   // https://doc.iqrf.org/DpaTechGuide/pages/beforesleep.html
  251 #endif
  252   goto DpaHandleReturnFALSE;
  253 
  254   // -------------------------------------------------
  255 _DpaEvent_AfterSleep:
  256 #ifdef DpaEvent_AfterSleep
  257   // Called after woken up after sleep
  258   // https://doc.iqrf.org/DpaTechGuide/pages/aftersleep.html
  259 #endif
  260   goto DpaHandleReturnFALSE;
  261 
  262   // -------------------------------------------------
  263 _DpaEvent_DisableInterrupts:
  264   // Called when device needs all hardware interrupts to be disabled (before Reset, Restart, LoadCode, Remove bond and run RFPGM)
  265   // https://doc.iqrf.org/DpaTechGuide/pages/eventDisableInterrupts.html
  266   goto DpaHandleReturnFALSE;
  267 
  268   // -------------------------------------------------
  269 _DpaEvent_ReceiveDpaResponse:
  270 #ifdef DpaEvent_ReceiveDpaResponse
  271   // Called after DPA response was received at coordinator
  272   // https://doc.iqrf.org/DpaTechGuide/pages/receivedparesponse.html
  273 
  274   //goto DpaHandleReturnTRUE; // return TRUE to skip default processing
  275 #endif
  276   goto DpaHandleReturnFALSE;
  277 
  278   // -------------------------------------------------
  279 _DpaEvent_IFaceReceive:
  280 #ifdef DpaEvent_IFaceReceive
  281   // Called after DPA request from interface master was received at coordinator
  282   // https://doc.iqrf.org/DpaTechGuide/pages/ifacereceive.html
  283 
  284   //goto DpaHandleReturnTRUE; // return TRUE to skip default processing
  285 #endif
  286   goto DpaHandleReturnFALSE;
  287 
  288   // -------------------------------------------------
  289 _DpaEvent_PeerToPeer:
  290   // Called when peer-to-peer (non-networking) packet is received
  291   // https://doc.iqrf.org/DpaTechGuide/pages/peertopeer.html
  292   goto DpaHandleReturnFALSE;
  293 
  294   // -------------------------------------------------
  295 _DpaEvent_UserDpaValue:
  296   // Called when DPA is required to return User defined DPA value in the response
  297   // https://doc.iqrf.org/DpaTechGuide/pages/userdpavalue.html
  298   goto DpaHandleReturnFALSE;
  299 
  300   // -------------------------------------------------
  301 _DpaEvent_VerifyLocalFrc:
  302 #ifdef DpaEvent_VerifyLocalFrc
  303   // Called to verify local FRC command
  304   // https://doc.iqrf.org/DpaTechGuide/pages/verifylocalfrc.html
  305 
  306   //goto DpaHandleReturnTRUE; // return TRUE allow FRC command
  307 #endif
  308   goto DpaHandleReturnFALSE;
  309 
  310   // -------------------------------------------------
  311 #ifdef DpaEvent_MenuActivated
  312   _DpaEvent_MenuActivated :
  313   // Called to customize DPA menu
  314   // https://doc.iqrf.org/DpaTechGuide/pages/menuactivated.html
  315 
  316   //goto DpaHandleReturnTRUE; // return TRUE to allow customizing menu specified by userReg1
  317   goto DpaHandleReturnFALSE;
  318 #endif
  319 
  320   // -------------------------------------------------
  321 #ifdef DpaEvent_MenuItemSelected
  322   _DpaEvent_MenuItemSelected :
  323   // Called to indicate "OK" or "Error" for selected menu item
  324   // https://doc.iqrf.org/DpaTechGuide/pages/menuitemselected.html
  325 
  326   //goto DpaHandleReturnTRUE; // return TRUE to indicate "OK" for menu item specified by userReg1, otherwise to indicate Error
  327   goto DpaHandleReturnFALSE;
  328 #endif
  329 
  330   // -------------------------------------------------
  331 #ifdef DpaEvent_MenuItemFinalize
  332   _DpaEvent_MenuItemFinalize :
  333   // Called to finalize menu item execution
  334   // https://doc.iqrf.org/DpaTechGuide/pages/menuitemfinalize.html
  335   goto DpaHandleReturnFALSE;
  336 #endif
  337 
  338   // -------------------------------------------------
  339 #ifdef DpaEvent_InStandby
  340   _DpaEvent_InStandby :
  341   // Called to set WDT during Standby
  342   // https://doc.iqrf.org/DpaTechGuide/pages/instandby.html
  343 
  344   //goto DpaHandleReturnTRUE; // return TRUE to indicate that userReg1 contains WDT settings
  345   goto DpaHandleReturnFALSE;
  346 #endif
  347   // -------------------------------------------------
  348 
  349 DpaHandleReturnFALSE:
  350   return FALSE;
  351 }
  352 
  353 //############################################################################################
  354 // Uncomment the following includes if the respective component is needed
  355 //#include "NFC.c"
  356 
  357 // 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)
  358 #include "DPAcustomHandler.h"
  359 //############################################################################################