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