1 // *********************************************************************
    2 //   Custom DPA Handler code template                                  *
    3 // *********************************************************************
    4 // Copyright (c) MICRORISC s.r.o.
    5 //
    6 // File:    $RCSfile: CustomDpaHandler-Template-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 (unused event handlers can be commented out or even deleted)
   60   switch ( GetDpaEvent() )
   61   {
   62     // -------------------------------------------------
   63     case DpaEvent_Interrupt:
   64       // Do an extra quick background interrupt work
   65       // ! 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.
   66       // ! 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.
   67       // ! 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.
   68       // ! Only global variables or local ones marked by static keyword can be used to allow reentrancy.
   69       // ! Make sure race condition does not occur when accessing those variables at other places.
   70       // ! 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.
   71       // ! Do not call any OS functions except setINDFx().
   72       // ! Do not use any OS variables especially for writing access.
   73       // ! 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.
   74       // https://doc.iqrf.org/DpaTechGuide/pages/EventInterrupt.html
   75       return Carry;
   76 
   77       // -------------------------------------------------
   78     case DpaEvent_Idle:
   79       // Do a quick background work when RF packet is not received
   80       // https://doc.iqrf.org/DpaTechGuide/pages/idle.html
   81       break;
   82 
   83       // -------------------------------------------------
   84     case DpaEvent_Reset:
   85       // Called after module is reset
   86       // https://doc.iqrf.org/DpaTechGuide/pages/ResetEvent.html
   87 
   88       //goto DpaHandleReturnTRUE; // return TRUE only if you handle node bonding/unbonding
   89       break;
   90 
   91       // -------------------------------------------------
   92 #if defined( DpaEvent_BondingButton )
   93     case DpaEvent_BondingButton:
   94       // Called to allow a bonding button customization
   95       // https://doc.iqrf.org/DpaTechGuide/pages/bondingbutton.html
   96 
   97       //goto DpaHandleReturnTRUE; // return TRUE to handle bonding button
   98       break;
   99 #endif
  100 
  101       // -------------------------------------------------
  102     case DpaEvent_Indicate:
  103       // Called to allow a customization of the device indication
  104       // https://doc.iqrf.org/DpaTechGuide/pages/IndicateEvent.html
  105 
  106       //goto DpaHandleReturnTRUE; // return TRUE to skip default indication
  107       break;
  108 
  109       // -------------------------------------------------
  110     case DpaEvent_Init:
  111       // Do a one time initialization before main loop starts
  112       // https://doc.iqrf.org/DpaTechGuide/pages/init.html
  113       break;
  114 
  115       // -------------------------------------------------
  116     case DpaEvent_ReceiveDpaRequest:
  117       // Called after DPA request was received
  118       // https://doc.iqrf.org/DpaTechGuide/pages/receivedparequest.html
  119 
  120       //goto DpaHandleReturnTRUE; // return TRUE to skip default processing
  121       break;
  122 
  123       // -------------------------------------------------
  124     case DpaEvent_BeforeSendingDpaResponse:
  125       // Called before sending DPA response back to originator of DPA response
  126       // https://doc.iqrf.org/DpaTechGuide/pages/beforesendingdparesponse.html
  127       break;
  128 
  129       // -------------------------------------------------
  130     case DpaEvent_Notification:
  131       // Called after DPA request was processed and after DPA response was sent
  132       // https://doc.iqrf.org/DpaTechGuide/pages/notification.html
  133       break;
  134 
  135       // -------------------------------------------------
  136     case DpaEvent_AfterRouting:
  137       // Called after Notification and after routing of the DPA response was finished
  138       // https://doc.iqrf.org/DpaTechGuide/pages/afterrouting.html
  139       break;
  140 
  141       // -------------------------------------------------
  142     case DpaEvent_FrcValue:
  143       // Called to get FRC value
  144       // https://doc.iqrf.org/DpaTechGuide/pages/frcvalue.html
  145       break;
  146 
  147       // -------------------------------------------------
  148     case DpaEvent_FrcResponseTime:
  149       // Called to get FRC response time
  150       // https://doc.iqrf.org/DpaTechGuide/pages/frcresponsetime.html
  151       break;
  152 
  153       // -------------------------------------------------
  154     case DpaEvent_BeforeSleep:
  155       // Called before going to sleep
  156       // https://doc.iqrf.org/DpaTechGuide/pages/beforesleep.html
  157       break;
  158 
  159       // -------------------------------------------------
  160     case DpaEvent_AfterSleep:
  161       // Called after woken up after sleep
  162       // https://doc.iqrf.org/DpaTechGuide/pages/aftersleep.html
  163       break;
  164 
  165       // -------------------------------------------------
  166     case DpaEvent_DisableInterrupts:
  167       // Called when device needs all hardware interrupts to be disabled (before Reset, Restart, LoadCode, Remove bond and run RFPGM)
  168       // https://doc.iqrf.org/DpaTechGuide/pages/eventDisableInterrupts.html
  169       break;
  170 
  171       // -------------------------------------------------
  172     case DpaEvent_PeerToPeer:
  173       // Called when peer-to-peer (non-networking) packet is received
  174       // https://doc.iqrf.org/DpaTechGuide/pages/peertopeer.html
  175       break;
  176 
  177       // -------------------------------------------------
  178     case DpaEvent_UserDpaValue:
  179       // Called when DPA is required to return User defined DPA value in the response
  180       // https://doc.iqrf.org/DpaTechGuide/pages/userdpavalue.html
  181       break;
  182 
  183       // -------------------------------------------------
  184     case DpaEvent_InStandby:
  185       // Called set WDT during Standby
  186       // https://doc.iqrf.org/DpaTechGuide/pages/instandby.html
  187 
  188       //goto DpaHandleReturnTRUE; // return TRUE to indicate that userReg1 contains WDT settings
  189       break;
  190 
  191       // -------------------------------------------------
  192     case DpaEvent_VerifyLocalFrc:
  193       // Called to verify local FRC command
  194       // https://doc.iqrf.org/DpaTechGuide/pages/verifylocalfrc.html
  195 
  196       //goto DpaHandleReturnTRUE; // return TRUE allow FRC command
  197       break;
  198 
  199       // -------------------------------------------------
  200 #ifdef DpaEvent_MenuActivated
  201     case DpaEvent_MenuActivated:
  202       // Called to customize DPA menu
  203       // https://doc.iqrf.org/DpaTechGuide/pages/menuactivated.html
  204 
  205       //goto DpaHandleReturnTRUE; // return TRUE to allow customizing menu specified by userReg1
  206       break;
  207 #endif
  208 
  209       // -------------------------------------------------
  210 #ifdef DpaEvent_MenuItemSelected
  211     case DpaEvent_MenuItemSelected:
  212       // Called to indicate "OK" or "Error" for selected menu item
  213       // https://doc.iqrf.org/DpaTechGuide/pages/menuitemselected.html
  214 
  215       //goto DpaHandleReturnTRUE; // return TRUE to indicate "OK" for menu item specified by userReg1, otherwise to indicate Error
  216       break;
  217 #endif
  218 
  219       // -------------------------------------------------
  220 #ifdef DpaEvent_MenuItemFinalize
  221     case DpaEvent_MenuItemFinalize:
  222       // Called to finalize menu item execution
  223       // https://doc.iqrf.org/DpaTechGuide/pages/menuitemfinalize.html
  224 
  225       break;
  226 #endif
  227 
  228       // -------------------------------------------------
  229     case DpaEvent_DpaRequest:
  230       // Called to interpret DPA request for peripherals
  231       // https://doc.iqrf.org/DpaTechGuide/pages/EventDpaRequest.html
  232       IfDpaEnumPeripherals_Else_PeripheralInfo_Else_PeripheralRequest()
  233       {
  234         // -------------------------------------------------
  235         // Peripheral enumeration
  236         // https://doc.iqrf.org/DpaTechGuide/pages/enumerate-peripherals.html
  237 
  238         _DpaMessage.EnumPeripheralsAnswer.UserPerNr |= 0; // ?
  239         // FlagUserPer( _DpaMessage.EnumPeripheralsAnswer.UserPer, PNUM_USER + 0 ); // ?
  240         _DpaMessage.EnumPeripheralsAnswer.HWPID |= 0x000F; // ????
  241         _DpaMessage.EnumPeripheralsAnswer.HWPIDver |= 0; // ????
  242 
  243 DpaHandleReturnTRUE:
  244         return TRUE;
  245       }
  246       else
  247       {
  248       // -------------------------------------------------
  249       // Get information about peripheral
  250       // https://doc.iqrf.org/DpaTechGuide/pages/get-peripheral-info.html
  251 
  252       if ( _PNUM == PNUM_USER + 0 ) // ?
  253       {
  254         _DpaMessage.PeripheralInfoAnswer.PerT = 0; // PERIPHERAL_TYPE_?
  255         _DpaMessage.PeripheralInfoAnswer.PerTE = 0; // PERIPHERAL_TYPE_EXTENDED_?
  256         _DpaMessage.PeripheralInfoAnswer.Par1 = 0; // ?
  257         _DpaMessage.PeripheralInfoAnswer.Par2 = 0; // ?
  258         goto DpaHandleReturnTRUE;
  259       }
  260 
  261       break;
  262       }
  263 
  264       // -------------------------------------------------
  265       // Handle peripheral command
  266       // https://doc.iqrf.org/DpaTechGuide/pages/handle-peripheral-request.html
  267 
  268       if ( _PNUM == PNUM_USER + 0 ) // ?
  269       {
  270         if ( _PCMD == 0 ) // ????
  271         {
  272           goto DpaHandleReturnTRUE;
  273         }
  274       }
  275 
  276       break;
  277   }
  278 
  279 DpaHandleReturnFALSE:
  280   return FALSE;
  281 }
  282 
  283 //############################################################################################
  284 // Uncomment the following includes if the respective component is needed
  285 //#include "NFC.c"
  286 
  287 // 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)
  288 #include "DPAcustomHandler.h"
  289 //############################################################################################