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.50 $
    8 // Date:    $Date: 2022/02/25 09:41:25 $
    9 //
   10 // Revision history:
   11 //   2022/02/24  Release for DPA 4.17
   12 //   2021/08/20  Release for DPA 4.16
   13 //   2020/09/03  Release for DPA 4.15
   14 //   2020/02/27  Release for DPA 4.13
   15 //   2019/01/10  Release for DPA 4.00
   16 //   2017/08/14  Release for DPA 3.01
   17 //   2017/03/13  Release for DPA 3.00
   18 //   2015/08/05  Release for DPA 2.20
   19 //   2014/10/31  Release for DPA 2.10
   20 //   2014/04/30  Release for DPA 2.00
   21 //
   22 // *********************************************************************
   23 
   24 // Online DPA documentation https://doc.iqrf.org/DpaTechGuide/
   25 
   26 // Default IQRF include (modify the path according to your setup)
   27 #include "IQRF.h"
   28 
   29 // Uncomment to implement Custom DPA Handler for Coordinator
   30 //#define COORDINATOR_CUSTOM_HANDLER
   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   goto _DpaEvent_VerifyLocalFrc;
   70   goto _DpaEvent_Indicate;
   71   goto _DpaEvent_BondingButton;
   72   goto _DpaEvent_FrcResponseTime;
   73   goto _DpaEvent_UserDpaValue;
   74   goto DpaHandleReturnFALSE; // Formerly DpaEvent_AuthorizePreBonding
   75   goto _DpaEvent_PeerToPeer;
   76   goto _DpaEvent_BeforeSendingDpaResponse;
   77   goto _DpaEvent_ReceiveDpaRequest;
   78   goto _DpaEvent_IFaceReceive;
   79   goto _DpaEvent_ReceiveDpaResponse;
   80   goto _DpaEvent_FrcValue;
   81   goto _DpaEvent_DisableInterrupts;
   82   goto _DpaEvent_Reset;
   83   goto _DpaEvent_AfterSleep;
   84   goto _DpaEvent_BeforeSleep;
   85   goto _DpaEvent_AfterRouting;
   86   goto _DpaEvent_Notification;
   87   goto _DpaEvent_Init;
   88   goto _DpaEvent_Idle;
   89   goto _DpaEvent_Interrupt;
   90   goto _DpaEvent_DpaRequest;
   91 #pragma computedGoto 0
   92   ;
   93   // -------------------------------------------------
   94 _DpaEvent_DpaRequest:
   95   // Called to interpret DPA request for peripherals
   96   // https://doc.iqrf.org/DpaTechGuide/pages/EventDpaRequest.html
   97   // -------------------------------------------------
   98   // Peripheral enumeration
   99   // https://doc.iqrf.org/DpaTechGuide/pages/enumerate-peripherals.html
  100   IfDpaEnumPeripherals_Else_PeripheralInfo_Else_PeripheralRequest()
  101   {
  102     _DpaMessage.EnumPeripheralsAnswer.UserPerNr |= 0; // ?
  103     // FlagUserPer( _DpaMessage.EnumPeripheralsAnswer.UserPer, PNUM_USER + 0 ); // ?
  104     _DpaMessage.EnumPeripheralsAnswer.HWPID |= 0x000F; // ????
  105     _DpaMessage.EnumPeripheralsAnswer.HWPIDver |= 0; // ????
  106 
  107 DpaHandleReturnTRUE:
  108     return TRUE;
  109   }
  110   // -------------------------------------------------
  111   // Get information about peripheral
  112   // https://doc.iqrf.org/DpaTechGuide/pages/get-peripheral-info.html
  113   else
  114   {
  115     if ( _PNUM == PNUM_USER + 0 ) // ?
  116     {
  117       _DpaMessage.PeripheralInfoAnswer.PerT = 0; // PERIPHERAL_TYPE_?
  118       _DpaMessage.PeripheralInfoAnswer.PerTE = 0; // PERIPHERAL_TYPE_EXTENDED_?
  119       _DpaMessage.PeripheralInfoAnswer.Par1 = 0; // ?
  120       _DpaMessage.PeripheralInfoAnswer.Par2 = 0; // ?
  121       goto DpaHandleReturnTRUE;
  122     }
  123 
  124     goto DpaHandleReturnFALSE;
  125   }
  126   // -------------------------------------------------
  127   // Handle peripheral command
  128   // https://doc.iqrf.org/DpaTechGuide/pages/handle-peripheral-request.html
  129   if ( _PNUM == PNUM_USER + 0 ) // ?
  130   {
  131     if ( _PCMD == 0 ) // ????
  132     {
  133       goto DpaHandleReturnTRUE;
  134     }
  135   }
  136 
  137   goto DpaHandleReturnFALSE;
  138 
  139   // -------------------------------------------------
  140 _DpaEvent_Interrupt:
  141 #ifdef DpaEvent_Interrupt
  142   // Do an extra quick background interrupt work
  143   // ! 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.
  144   // ! 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.
  145   // ! 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.
  146   // ! Only global variables or local ones marked by static keyword can be used to allow reentrancy.
  147   // ! Make sure race condition does not occur when accessing those variables at other places.
  148   // ! 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.
  149   // ! Do not call any OS functions except setINDFx().
  150   // ! Do not use any OS variables especially for writing access.
  151   // ! 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.
  152   // https://doc.iqrf.org/DpaTechGuide/pages/EventInterrupt.html
  153 #endif
  154   return Carry;
  155   // -------------------------------------------------
  156 _DpaEvent_Idle:
  157   // Do a quick background work when RF packet is not received
  158   // https://doc.iqrf.org/DpaTechGuide/pages/idle.html
  159   goto DpaHandleReturnFALSE;
  160 
  161   // -------------------------------------------------
  162 _DpaEvent_Reset:
  163   // Called after module is reset
  164   // https://doc.iqrf.org/DpaTechGuide/pages/ResetEvent.html
  165 
  166   //goto DpaHandleReturnTRUE; // return TRUE only if you handle node bonding/unbonding
  167   goto DpaHandleReturnFALSE;
  168 
  169   // -------------------------------------------------
  170 _DpaEvent_BondingButton:
  171 #ifdef DpaEvent_BondingButton
  172   // Called to allow a bonding button customization
  173   // https://doc.iqrf.org/DpaTechGuide/pages/bondingbutton.html
  174 
  175   //goto DpaHandleReturnTRUE; // return TRUE to handle bonding button
  176 #endif
  177   goto DpaHandleReturnFALSE;
  178 
  179   // -------------------------------------------------
  180 _DpaEvent_Indicate:
  181 #ifdef DpaEvent_BondingButton
  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 #endif
  187   goto DpaHandleReturnFALSE;
  188 
  189   // -------------------------------------------------
  190 _DpaEvent_Init:
  191   // Do a one time initialization before main loop starts
  192   // https://doc.iqrf.org/DpaTechGuide/pages/init.html
  193   goto DpaHandleReturnFALSE;
  194 
  195   // -------------------------------------------------
  196 _DpaEvent_ReceiveDpaRequest:
  197   // Called after DPA request was received
  198   // https://doc.iqrf.org/DpaTechGuide/pages/receivedparequest.html
  199 
  200   //goto DpaHandleReturnTRUE; // return TRUE to skip default processing
  201   goto DpaHandleReturnFALSE;
  202 
  203   // -------------------------------------------------
  204 _DpaEvent_BeforeSendingDpaResponse:
  205   // Called before sending DPA response back to originator of DPA response
  206   // https://doc.iqrf.org/DpaTechGuide/pages/beforesendingdparesponse.html
  207 
  208   goto DpaHandleReturnFALSE;
  209 
  210   // -------------------------------------------------
  211 _DpaEvent_Notification:
  212   // Called after DPA request was processed and after DPA response was sent
  213   // https://doc.iqrf.org/DpaTechGuide/pages/notification.html
  214   goto DpaHandleReturnFALSE;
  215 
  216   // -------------------------------------------------
  217 _DpaEvent_AfterRouting:
  218   // Called after Notification and after routing of the DPA response was finished
  219   // https://doc.iqrf.org/DpaTechGuide/pages/afterrouting.html
  220   goto DpaHandleReturnFALSE;
  221 
  222   // -------------------------------------------------
  223 _DpaEvent_FrcValue:
  224 #ifdef DpaEvent_FrcValue
  225   // Called to get FRC value
  226   // https://doc.iqrf.org/DpaTechGuide/pages/frcvalue.html
  227 #endif
  228   goto DpaHandleReturnFALSE;
  229 
  230   // -------------------------------------------------
  231 _DpaEvent_FrcResponseTime:
  232 #ifdef DpaEvent_FrcResponseTime
  233   // Called to get FRC response time
  234   // https://doc.iqrf.org/DpaTechGuide/pages/frcresponsetime.html
  235 #endif
  236   goto DpaHandleReturnFALSE;
  237 
  238   // -------------------------------------------------
  239 _DpaEvent_BeforeSleep:
  240 #ifdef DpaEvent_BeforeSleep
  241   // Called before going to sleep
  242   // https://doc.iqrf.org/DpaTechGuide/pages/beforesleep.html
  243 #endif
  244   goto DpaHandleReturnFALSE;
  245 
  246   // -------------------------------------------------
  247 _DpaEvent_AfterSleep:
  248 #ifdef DpaEvent_AfterSleep
  249   // Called after woken up after sleep
  250   // https://doc.iqrf.org/DpaTechGuide/pages/aftersleep.html
  251 #endif
  252   goto DpaHandleReturnFALSE;
  253 
  254   // -------------------------------------------------
  255 _DpaEvent_DisableInterrupts:
  256   // Called when device needs all hardware interrupts to be disabled (before Reset, Restart, LoadCode, Remove bond and run RFPGM)
  257   // https://doc.iqrf.org/DpaTechGuide/pages/eventDisableInterrupts.html
  258   goto DpaHandleReturnFALSE;
  259 
  260   // -------------------------------------------------
  261 _DpaEvent_ReceiveDpaResponse:
  262 #ifdef DpaEvent_ReceiveDpaResponse
  263   // Called after DPA response was received at coordinator
  264   // https://doc.iqrf.org/DpaTechGuide/pages/receivedparesponse.html
  265 
  266   //goto DpaHandleReturnTRUE; // return TRUE to skip default processing
  267 #endif
  268   goto DpaHandleReturnFALSE;
  269 
  270   // -------------------------------------------------
  271 _DpaEvent_IFaceReceive:
  272 #ifdef DpaEvent_IFaceReceive
  273   // Called after DPA request from interface master was received at coordinator
  274   // https://doc.iqrf.org/DpaTechGuide/pages/ifacereceive.html
  275 
  276   //goto DpaHandleReturnTRUE; // return TRUE to skip default processing
  277 #endif
  278   goto DpaHandleReturnFALSE;
  279 
  280   // -------------------------------------------------
  281 _DpaEvent_PeerToPeer:
  282   // Called when peer-to-peer (non-networking) packet is received
  283   // https://doc.iqrf.org/DpaTechGuide/pages/peertopeer.html
  284   goto DpaHandleReturnFALSE;
  285 
  286   // -------------------------------------------------
  287 _DpaEvent_UserDpaValue:
  288   // Called when DPA is required to return User defined DPA value in the response
  289   // https://doc.iqrf.org/DpaTechGuide/pages/userdpavalue.html
  290   goto DpaHandleReturnFALSE;
  291 
  292   // -------------------------------------------------
  293 _DpaEvent_VerifyLocalFrc:
  294 #ifdef DpaEvent_VerifyLocalFrc
  295   // Called to verify local FRC command
  296   // https://doc.iqrf.org/DpaTechGuide/pages/verifylocalfrc.html
  297 
  298   //goto DpaHandleReturnTRUE; // return TRUE allow FRC command
  299 #endif
  300   goto DpaHandleReturnFALSE;
  301 
  302   // -------------------------------------------------
  303 
  304 DpaHandleReturnFALSE:
  305   return FALSE;
  306 }
  307 
  308 //############################################################################################
  309 // Uncomment the following includes if the respective component is needed
  310 //#include "NFC.c"
  311 
  312 // 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)
  313 #include "DPAcustomHandler.h"
  314 //############################################################################################