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