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.42 $
    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 // Uncomment to implement Custom DPA Handler for Coordinator
   29 //#define COORDINATOR_CUSTOM_HANDLER
   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   goto _DpaEvent_VerifyLocalFrc;
   69   goto _DpaEvent_Indicate;
   70   goto _DpaEvent_BondingButton;
   71   goto _DpaEvent_FrcResponseTime;
   72   goto _DpaEvent_UserDpaValue;
   73   goto DpaHandleReturnFALSE; // Formerly DpaEvent_AuthorizePreBonding
   74   goto _DpaEvent_PeerToPeer;
   75   goto _DpaEvent_BeforeSendingDpaResponse;
   76   goto _DpaEvent_ReceiveDpaRequest;
   77   goto _DpaEvent_IFaceReceive;
   78   goto _DpaEvent_ReceiveDpaResponse;
   79   goto _DpaEvent_FrcValue;
   80   goto _DpaEvent_DisableInterrupts;
   81   goto _DpaEvent_Reset;
   82   goto _DpaEvent_AfterSleep;
   83   goto _DpaEvent_BeforeSleep;
   84   goto _DpaEvent_AfterRouting;
   85   goto _DpaEvent_Notification;
   86   goto _DpaEvent_Init;
   87   goto _DpaEvent_Idle;
   88   goto _DpaEvent_Interrupt;
   89   goto _DpaEvent_DpaRequest;
   90 #pragma computedGoto 0
   91   ;
   92   // -------------------------------------------------
   93 _DpaEvent_DpaRequest:
   94   // Called to interpret DPA request for peripherals
   95   // https://doc.iqrf.org/DpaTechGuide/pages/EventDpaRequest.html
   96   // -------------------------------------------------
   97   // Peripheral enumeration
   98   // https://doc.iqrf.org/DpaTechGuide/pages/enumerate-peripherals.html
   99   IfDpaEnumPeripherals_Else_PeripheralInfo_Else_PeripheralRequest()
  100   {
  101     _DpaMessage.EnumPeripheralsAnswer.UserPerNr = 0; // ?
  102     // FlagUserPer( _DpaMessage.EnumPeripheralsAnswer.UserPer, PNUM_USER + 0 ); // ?
  103     _DpaMessage.EnumPeripheralsAnswer.HWPID = 0x000F; // ????
  104     _DpaMessage.EnumPeripheralsAnswer.HWPIDver = 0; // ????
  105 
  106 DpaHandleReturnTRUE:
  107     return TRUE;
  108   }
  109   // -------------------------------------------------
  110   // Get information about peripheral
  111   // https://doc.iqrf.org/DpaTechGuide/pages/get-peripheral-info.html
  112   else
  113   {
  114     if ( _PNUM == PNUM_USER + 0 ) // ?
  115     {
  116       _DpaMessage.PeripheralInfoAnswer.PerT = 0; // PERIPHERAL_TYPE_?
  117       _DpaMessage.PeripheralInfoAnswer.PerTE = 0; // PERIPHERAL_TYPE_EXTENDED_?
  118       _DpaMessage.PeripheralInfoAnswer.Par1 = 0; // ?
  119       _DpaMessage.PeripheralInfoAnswer.Par2 = 0; // ?
  120       goto DpaHandleReturnTRUE;
  121     }
  122 
  123     goto DpaHandleReturnFALSE;
  124   }
  125   // -------------------------------------------------
  126   // Handle peripheral command
  127   // https://doc.iqrf.org/DpaTechGuide/pages/handle-peripheral-request.html
  128   if ( _PNUM == PNUM_USER + 0 ) // ?
  129   {
  130     if ( _PCMD == 0 ) // ????
  131     {
  132       goto DpaHandleReturnTRUE;
  133     }
  134   }
  135 
  136   goto DpaHandleReturnFALSE;
  137 
  138   // -------------------------------------------------
  139 _DpaEvent_Interrupt:
  140 #ifdef DpaEvent_Interrupt
  141   // Do an extra quick background interrupt work
  142   // ! 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.
  143   // ! 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.
  144   // ! 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.
  145   // ! Only global variables or local ones marked by static keyword can be used to allow reentrancy.
  146   // ! Make sure race condition does not occur when accessing those variables at other places.
  147   // ! 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.
  148   // ! Do not call any OS functions except setINDFx().
  149   // ! Do not use any OS variables especially for writing access.
  150   // ! 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.
  151   // https://doc.iqrf.org/DpaTechGuide/pages/EventInterrupt.html
  152 #endif
  153   return Carry;
  154   // -------------------------------------------------
  155 _DpaEvent_Idle:
  156   // Do a quick background work when RF packet is not received
  157   // https://doc.iqrf.org/DpaTechGuide/pages/idle.html
  158   goto DpaHandleReturnFALSE;
  159 
  160   // -------------------------------------------------
  161 _DpaEvent_Reset:
  162   // Called after module is reset
  163   // https://doc.iqrf.org/DpaTechGuide/pages/ResetEvent.html
  164 
  165   //goto DpaHandleReturnTRUE; // return TRUE only if you handle node bonding/unbonding
  166   goto DpaHandleReturnFALSE;
  167 
  168   // -------------------------------------------------
  169 _DpaEvent_BondingButton:
  170 #ifdef DpaEvent_BondingButton
  171   // Called to allow a bonding button customization
  172   // https://doc.iqrf.org/DpaTechGuide/pages/bondingbutton.html
  173 
  174   //goto DpaHandleReturnTRUE; // return TRUE to handle bonding button
  175 #endif
  176   goto DpaHandleReturnFALSE;
  177 
  178   // -------------------------------------------------
  179 _DpaEvent_Indicate:
  180 #ifdef DpaEvent_BondingButton
  181   // Called to allow a customization of the device indication
  182   // https://doc.iqrf.org/DpaTechGuide/pages/IndicateEvent.html
  183 
  184   //goto DpaHandleReturnTRUE; // return TRUE to skip default indication
  185 #endif
  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 #ifdef DpaEvent_FrcValue
  224   // Called to get FRC value
  225   // https://doc.iqrf.org/DpaTechGuide/pages/frcvalue.html
  226 #endif
  227   goto DpaHandleReturnFALSE;
  228 
  229   // -------------------------------------------------
  230 _DpaEvent_FrcResponseTime:
  231 #ifdef DpaEvent_FrcResponseTime
  232   // Called to get FRC response time
  233   // https://doc.iqrf.org/DpaTechGuide/pages/frcresponsetime.html
  234 #endif
  235   goto DpaHandleReturnFALSE;
  236 
  237   // -------------------------------------------------
  238 _DpaEvent_BeforeSleep:
  239 #ifdef DpaEvent_BeforeSleep
  240   // Called before going to sleep
  241   // https://doc.iqrf.org/DpaTechGuide/pages/beforesleep.html
  242 #endif
  243   goto DpaHandleReturnFALSE;
  244 
  245   // -------------------------------------------------
  246 _DpaEvent_AfterSleep:
  247 #ifdef DpaEvent_AfterSleep
  248   // Called after woken up after sleep
  249   // https://doc.iqrf.org/DpaTechGuide/pages/aftersleep.html
  250 #endif
  251   goto DpaHandleReturnFALSE;
  252 
  253   // -------------------------------------------------
  254 _DpaEvent_DisableInterrupts:
  255   // Called when device needs all hardware interrupts to be disabled (before Reset, Restart, LoadCode, Remove bond and run RFPGM)
  256   // https://doc.iqrf.org/DpaTechGuide/pages/eventDisableInterrupts.html
  257   goto DpaHandleReturnFALSE;
  258 
  259   // -------------------------------------------------
  260 _DpaEvent_ReceiveDpaResponse:
  261 #ifdef DpaEvent_ReceiveDpaResponse
  262   // Called after DPA response was received at coordinator
  263   // https://doc.iqrf.org/DpaTechGuide/pages/receivedparesponse.html
  264 
  265   //goto DpaHandleReturnTRUE; // return TRUE to skip default processing
  266 #endif
  267   goto DpaHandleReturnFALSE;
  268 
  269   // -------------------------------------------------
  270 _DpaEvent_IFaceReceive:
  271 #ifdef DpaEvent_IFaceReceive
  272   // Called after DPA request from interface master was received at coordinator
  273   // https://doc.iqrf.org/DpaTechGuide/pages/ifacereceive.html
  274 
  275   //goto DpaHandleReturnTRUE; // return TRUE to skip default processing
  276 #endif
  277   goto DpaHandleReturnFALSE;
  278 
  279   // -------------------------------------------------
  280 _DpaEvent_PeerToPeer:
  281   // Called when peer-to-peer (non-networking) packet is received
  282   // https://doc.iqrf.org/DpaTechGuide/pages/peertopeer.html
  283   goto DpaHandleReturnFALSE;
  284 
  285   // -------------------------------------------------
  286 _DpaEvent_UserDpaValue:
  287   // Called when DPA is required to return User defined DPA value in the response
  288   // https://doc.iqrf.org/DpaTechGuide/pages/userdpavalue.html
  289   goto DpaHandleReturnFALSE;
  290 
  291   // -------------------------------------------------
  292 _DpaEvent_VerifyLocalFrc:
  293 #ifdef DpaEvent_VerifyLocalFrc
  294   // Called to verify local FRC command
  295   // https://doc.iqrf.org/DpaTechGuide/pages/verifylocalfrc.html
  296 
  297   //goto DpaHandleReturnTRUE; // return TRUE allow FRC command
  298 #endif
  299   goto DpaHandleReturnFALSE;
  300 
  301   // -------------------------------------------------
  302 
  303 DpaHandleReturnFALSE:
  304   return FALSE;
  305 }
  306 
  307 //############################################################################################
  308 // Uncomment the following includes if the respective component is needed
  309 //#include "NFC.c"
  310 
  311 // 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)
  312 #include "DPAcustomHandler.h"
  313 //############################################################################################