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